1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright 1998, 2000 Marshall Kirk McKusick. 5 * Copyright 2009, 2010 Jeffrey W. Roberson <jeff@FreeBSD.org> 6 * All rights reserved. 7 * 8 * The soft updates code is derived from the appendix of a University 9 * of Michigan technical report (Gregory R. Ganger and Yale N. Patt, 10 * "Soft Updates: A Solution to the Metadata Update Problem in File 11 * Systems", CSE-TR-254-95, August 1995). 12 * 13 * Further information about soft updates can be obtained from: 14 * 15 * Marshall Kirk McKusick http://www.mckusick.com/softdep/ 16 * 1614 Oxford Street mckusick@mckusick.com 17 * Berkeley, CA 94709-1608 +1-510-843-9542 18 * USA 19 * 20 * Redistribution and use in source and binary forms, with or without 21 * modification, are permitted provided that the following conditions 22 * are met: 23 * 24 * 1. Redistributions of source code must retain the above copyright 25 * notice, this list of conditions and the following disclaimer. 26 * 2. Redistributions in binary form must reproduce the above copyright 27 * notice, this list of conditions and the following disclaimer in the 28 * documentation and/or other materials provided with the distribution. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 33 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 35 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 36 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 37 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 38 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 39 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * from: @(#)ffs_softdep.c 9.59 (McKusick) 6/21/00 42 */ 43 44 #include <sys/cdefs.h> 45 __FBSDID("$FreeBSD$"); 46 47 #include "opt_ffs.h" 48 #include "opt_quota.h" 49 #include "opt_ddb.h" 50 51 #include <sys/param.h> 52 #include <sys/kernel.h> 53 #include <sys/systm.h> 54 #include <sys/bio.h> 55 #include <sys/buf.h> 56 #include <sys/kdb.h> 57 #include <sys/kthread.h> 58 #include <sys/ktr.h> 59 #include <sys/limits.h> 60 #include <sys/lock.h> 61 #include <sys/malloc.h> 62 #include <sys/mount.h> 63 #include <sys/mutex.h> 64 #include <sys/namei.h> 65 #include <sys/priv.h> 66 #include <sys/proc.h> 67 #include <sys/racct.h> 68 #include <sys/rwlock.h> 69 #include <sys/stat.h> 70 #include <sys/sysctl.h> 71 #include <sys/syslog.h> 72 #include <sys/vnode.h> 73 #include <sys/conf.h> 74 75 #include <ufs/ufs/dir.h> 76 #include <ufs/ufs/extattr.h> 77 #include <ufs/ufs/quota.h> 78 #include <ufs/ufs/inode.h> 79 #include <ufs/ufs/ufsmount.h> 80 #include <ufs/ffs/fs.h> 81 #include <ufs/ffs/softdep.h> 82 #include <ufs/ffs/ffs_extern.h> 83 #include <ufs/ufs/ufs_extern.h> 84 85 #include <vm/vm.h> 86 #include <vm/vm_extern.h> 87 #include <vm/vm_object.h> 88 89 #include <geom/geom.h> 90 #include <geom/geom_vfs.h> 91 92 #include <ddb/ddb.h> 93 94 #define KTR_SUJ 0 /* Define to KTR_SPARE. */ 95 96 #ifndef SOFTUPDATES 97 98 int 99 softdep_flushfiles(oldmnt, flags, td) 100 struct mount *oldmnt; 101 int flags; 102 struct thread *td; 103 { 104 105 panic("softdep_flushfiles called"); 106 } 107 108 int 109 softdep_mount(devvp, mp, fs, cred) 110 struct vnode *devvp; 111 struct mount *mp; 112 struct fs *fs; 113 struct ucred *cred; 114 { 115 116 return (0); 117 } 118 119 void 120 softdep_initialize() 121 { 122 123 return; 124 } 125 126 void 127 softdep_uninitialize() 128 { 129 130 return; 131 } 132 133 void 134 softdep_unmount(mp) 135 struct mount *mp; 136 { 137 138 panic("softdep_unmount called"); 139 } 140 141 void 142 softdep_setup_sbupdate(ump, fs, bp) 143 struct ufsmount *ump; 144 struct fs *fs; 145 struct buf *bp; 146 { 147 148 panic("softdep_setup_sbupdate called"); 149 } 150 151 void 152 softdep_setup_inomapdep(bp, ip, newinum, mode) 153 struct buf *bp; 154 struct inode *ip; 155 ino_t newinum; 156 int mode; 157 { 158 159 panic("softdep_setup_inomapdep called"); 160 } 161 162 void 163 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags) 164 struct buf *bp; 165 struct mount *mp; 166 ufs2_daddr_t newblkno; 167 int frags; 168 int oldfrags; 169 { 170 171 panic("softdep_setup_blkmapdep called"); 172 } 173 174 void 175 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 176 struct inode *ip; 177 ufs_lbn_t lbn; 178 ufs2_daddr_t newblkno; 179 ufs2_daddr_t oldblkno; 180 long newsize; 181 long oldsize; 182 struct buf *bp; 183 { 184 185 panic("softdep_setup_allocdirect called"); 186 } 187 188 void 189 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 190 struct inode *ip; 191 ufs_lbn_t lbn; 192 ufs2_daddr_t newblkno; 193 ufs2_daddr_t oldblkno; 194 long newsize; 195 long oldsize; 196 struct buf *bp; 197 { 198 199 panic("softdep_setup_allocext called"); 200 } 201 202 void 203 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 204 struct inode *ip; 205 ufs_lbn_t lbn; 206 struct buf *bp; 207 int ptrno; 208 ufs2_daddr_t newblkno; 209 ufs2_daddr_t oldblkno; 210 struct buf *nbp; 211 { 212 213 panic("softdep_setup_allocindir_page called"); 214 } 215 216 void 217 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 218 struct buf *nbp; 219 struct inode *ip; 220 struct buf *bp; 221 int ptrno; 222 ufs2_daddr_t newblkno; 223 { 224 225 panic("softdep_setup_allocindir_meta called"); 226 } 227 228 void 229 softdep_journal_freeblocks(ip, cred, length, flags) 230 struct inode *ip; 231 struct ucred *cred; 232 off_t length; 233 int flags; 234 { 235 236 panic("softdep_journal_freeblocks called"); 237 } 238 239 void 240 softdep_journal_fsync(ip) 241 struct inode *ip; 242 { 243 244 panic("softdep_journal_fsync called"); 245 } 246 247 void 248 softdep_setup_freeblocks(ip, length, flags) 249 struct inode *ip; 250 off_t length; 251 int flags; 252 { 253 254 panic("softdep_setup_freeblocks called"); 255 } 256 257 void 258 softdep_freefile(pvp, ino, mode) 259 struct vnode *pvp; 260 ino_t ino; 261 int mode; 262 { 263 264 panic("softdep_freefile called"); 265 } 266 267 int 268 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 269 struct buf *bp; 270 struct inode *dp; 271 off_t diroffset; 272 ino_t newinum; 273 struct buf *newdirbp; 274 int isnewblk; 275 { 276 277 panic("softdep_setup_directory_add called"); 278 } 279 280 void 281 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 282 struct buf *bp; 283 struct inode *dp; 284 caddr_t base; 285 caddr_t oldloc; 286 caddr_t newloc; 287 int entrysize; 288 { 289 290 panic("softdep_change_directoryentry_offset called"); 291 } 292 293 void 294 softdep_setup_remove(bp, dp, ip, isrmdir) 295 struct buf *bp; 296 struct inode *dp; 297 struct inode *ip; 298 int isrmdir; 299 { 300 301 panic("softdep_setup_remove called"); 302 } 303 304 void 305 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 306 struct buf *bp; 307 struct inode *dp; 308 struct inode *ip; 309 ino_t newinum; 310 int isrmdir; 311 { 312 313 panic("softdep_setup_directory_change called"); 314 } 315 316 void 317 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 318 struct mount *mp; 319 struct buf *bp; 320 ufs2_daddr_t blkno; 321 int frags; 322 struct workhead *wkhd; 323 { 324 325 panic("%s called", __FUNCTION__); 326 } 327 328 void 329 softdep_setup_inofree(mp, bp, ino, wkhd) 330 struct mount *mp; 331 struct buf *bp; 332 ino_t ino; 333 struct workhead *wkhd; 334 { 335 336 panic("%s called", __FUNCTION__); 337 } 338 339 void 340 softdep_setup_unlink(dp, ip) 341 struct inode *dp; 342 struct inode *ip; 343 { 344 345 panic("%s called", __FUNCTION__); 346 } 347 348 void 349 softdep_setup_link(dp, ip) 350 struct inode *dp; 351 struct inode *ip; 352 { 353 354 panic("%s called", __FUNCTION__); 355 } 356 357 void 358 softdep_revert_link(dp, ip) 359 struct inode *dp; 360 struct inode *ip; 361 { 362 363 panic("%s called", __FUNCTION__); 364 } 365 366 void 367 softdep_setup_rmdir(dp, ip) 368 struct inode *dp; 369 struct inode *ip; 370 { 371 372 panic("%s called", __FUNCTION__); 373 } 374 375 void 376 softdep_revert_rmdir(dp, ip) 377 struct inode *dp; 378 struct inode *ip; 379 { 380 381 panic("%s called", __FUNCTION__); 382 } 383 384 void 385 softdep_setup_create(dp, ip) 386 struct inode *dp; 387 struct inode *ip; 388 { 389 390 panic("%s called", __FUNCTION__); 391 } 392 393 void 394 softdep_revert_create(dp, ip) 395 struct inode *dp; 396 struct inode *ip; 397 { 398 399 panic("%s called", __FUNCTION__); 400 } 401 402 void 403 softdep_setup_mkdir(dp, ip) 404 struct inode *dp; 405 struct inode *ip; 406 { 407 408 panic("%s called", __FUNCTION__); 409 } 410 411 void 412 softdep_revert_mkdir(dp, ip) 413 struct inode *dp; 414 struct inode *ip; 415 { 416 417 panic("%s called", __FUNCTION__); 418 } 419 420 void 421 softdep_setup_dotdot_link(dp, ip) 422 struct inode *dp; 423 struct inode *ip; 424 { 425 426 panic("%s called", __FUNCTION__); 427 } 428 429 int 430 softdep_prealloc(vp, waitok) 431 struct vnode *vp; 432 int waitok; 433 { 434 435 panic("%s called", __FUNCTION__); 436 } 437 438 int 439 softdep_journal_lookup(mp, vpp) 440 struct mount *mp; 441 struct vnode **vpp; 442 { 443 444 return (ENOENT); 445 } 446 447 void 448 softdep_change_linkcnt(ip) 449 struct inode *ip; 450 { 451 452 panic("softdep_change_linkcnt called"); 453 } 454 455 void 456 softdep_load_inodeblock(ip) 457 struct inode *ip; 458 { 459 460 panic("softdep_load_inodeblock called"); 461 } 462 463 void 464 softdep_update_inodeblock(ip, bp, waitfor) 465 struct inode *ip; 466 struct buf *bp; 467 int waitfor; 468 { 469 470 panic("softdep_update_inodeblock called"); 471 } 472 473 int 474 softdep_fsync(vp) 475 struct vnode *vp; /* the "in_core" copy of the inode */ 476 { 477 478 return (0); 479 } 480 481 void 482 softdep_fsync_mountdev(vp) 483 struct vnode *vp; 484 { 485 486 return; 487 } 488 489 int 490 softdep_flushworklist(oldmnt, countp, td) 491 struct mount *oldmnt; 492 int *countp; 493 struct thread *td; 494 { 495 496 *countp = 0; 497 return (0); 498 } 499 500 int 501 softdep_sync_metadata(struct vnode *vp) 502 { 503 504 panic("softdep_sync_metadata called"); 505 } 506 507 int 508 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 509 { 510 511 panic("softdep_sync_buf called"); 512 } 513 514 int 515 softdep_slowdown(vp) 516 struct vnode *vp; 517 { 518 519 panic("softdep_slowdown called"); 520 } 521 522 int 523 softdep_request_cleanup(fs, vp, cred, resource) 524 struct fs *fs; 525 struct vnode *vp; 526 struct ucred *cred; 527 int resource; 528 { 529 530 return (0); 531 } 532 533 int 534 softdep_check_suspend(struct mount *mp, 535 struct vnode *devvp, 536 int softdep_depcnt, 537 int softdep_accdepcnt, 538 int secondary_writes, 539 int secondary_accwrites) 540 { 541 struct bufobj *bo; 542 int error; 543 544 (void) softdep_depcnt, 545 (void) softdep_accdepcnt; 546 547 bo = &devvp->v_bufobj; 548 ASSERT_BO_WLOCKED(bo); 549 550 MNT_ILOCK(mp); 551 while (mp->mnt_secondary_writes != 0) { 552 BO_UNLOCK(bo); 553 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 554 (PUSER - 1) | PDROP, "secwr", 0); 555 BO_LOCK(bo); 556 MNT_ILOCK(mp); 557 } 558 559 /* 560 * Reasons for needing more work before suspend: 561 * - Dirty buffers on devvp. 562 * - Secondary writes occurred after start of vnode sync loop 563 */ 564 error = 0; 565 if (bo->bo_numoutput > 0 || 566 bo->bo_dirty.bv_cnt > 0 || 567 secondary_writes != 0 || 568 mp->mnt_secondary_writes != 0 || 569 secondary_accwrites != mp->mnt_secondary_accwrites) 570 error = EAGAIN; 571 BO_UNLOCK(bo); 572 return (error); 573 } 574 575 void 576 softdep_get_depcounts(struct mount *mp, 577 int *softdepactivep, 578 int *softdepactiveaccp) 579 { 580 (void) mp; 581 *softdepactivep = 0; 582 *softdepactiveaccp = 0; 583 } 584 585 void 586 softdep_buf_append(bp, wkhd) 587 struct buf *bp; 588 struct workhead *wkhd; 589 { 590 591 panic("softdep_buf_appendwork called"); 592 } 593 594 void 595 softdep_inode_append(ip, cred, wkhd) 596 struct inode *ip; 597 struct ucred *cred; 598 struct workhead *wkhd; 599 { 600 601 panic("softdep_inode_appendwork called"); 602 } 603 604 void 605 softdep_freework(wkhd) 606 struct workhead *wkhd; 607 { 608 609 panic("softdep_freework called"); 610 } 611 612 int 613 softdep_prerename(fdvp, fvp, tdvp, tvp) 614 struct vnode *fdvp; 615 struct vnode *fvp; 616 struct vnode *tdvp; 617 struct vnode *tvp; 618 { 619 620 panic("softdep_prerename called"); 621 } 622 623 int 624 softdep_prelink(dvp, vp) 625 struct vnode *dvp; 626 struct vnode *vp; 627 { 628 629 panic("softdep_prelink called"); 630 } 631 632 #else 633 634 FEATURE(softupdates, "FFS soft-updates support"); 635 636 static SYSCTL_NODE(_debug, OID_AUTO, softdep, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 637 "soft updates stats"); 638 static SYSCTL_NODE(_debug_softdep, OID_AUTO, total, 639 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 640 "total dependencies allocated"); 641 static SYSCTL_NODE(_debug_softdep, OID_AUTO, highuse, 642 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 643 "high use dependencies allocated"); 644 static SYSCTL_NODE(_debug_softdep, OID_AUTO, current, 645 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 646 "current dependencies allocated"); 647 static SYSCTL_NODE(_debug_softdep, OID_AUTO, write, 648 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 649 "current dependencies written"); 650 651 unsigned long dep_current[D_LAST + 1]; 652 unsigned long dep_highuse[D_LAST + 1]; 653 unsigned long dep_total[D_LAST + 1]; 654 unsigned long dep_write[D_LAST + 1]; 655 656 #define SOFTDEP_TYPE(type, str, long) \ 657 static MALLOC_DEFINE(M_ ## type, #str, long); \ 658 SYSCTL_ULONG(_debug_softdep_total, OID_AUTO, str, CTLFLAG_RD, \ 659 &dep_total[D_ ## type], 0, ""); \ 660 SYSCTL_ULONG(_debug_softdep_current, OID_AUTO, str, CTLFLAG_RD, \ 661 &dep_current[D_ ## type], 0, ""); \ 662 SYSCTL_ULONG(_debug_softdep_highuse, OID_AUTO, str, CTLFLAG_RD, \ 663 &dep_highuse[D_ ## type], 0, ""); \ 664 SYSCTL_ULONG(_debug_softdep_write, OID_AUTO, str, CTLFLAG_RD, \ 665 &dep_write[D_ ## type], 0, ""); 666 667 SOFTDEP_TYPE(PAGEDEP, pagedep, "File page dependencies"); 668 SOFTDEP_TYPE(INODEDEP, inodedep, "Inode dependencies"); 669 SOFTDEP_TYPE(BMSAFEMAP, bmsafemap, 670 "Block or frag allocated from cyl group map"); 671 SOFTDEP_TYPE(NEWBLK, newblk, "New block or frag allocation dependency"); 672 SOFTDEP_TYPE(ALLOCDIRECT, allocdirect, "Block or frag dependency for an inode"); 673 SOFTDEP_TYPE(INDIRDEP, indirdep, "Indirect block dependencies"); 674 SOFTDEP_TYPE(ALLOCINDIR, allocindir, "Block dependency for an indirect block"); 675 SOFTDEP_TYPE(FREEFRAG, freefrag, "Previously used frag for an inode"); 676 SOFTDEP_TYPE(FREEBLKS, freeblks, "Blocks freed from an inode"); 677 SOFTDEP_TYPE(FREEFILE, freefile, "Inode deallocated"); 678 SOFTDEP_TYPE(DIRADD, diradd, "New directory entry"); 679 SOFTDEP_TYPE(MKDIR, mkdir, "New directory"); 680 SOFTDEP_TYPE(DIRREM, dirrem, "Directory entry deleted"); 681 SOFTDEP_TYPE(NEWDIRBLK, newdirblk, "Unclaimed new directory block"); 682 SOFTDEP_TYPE(FREEWORK, freework, "free an inode block"); 683 SOFTDEP_TYPE(FREEDEP, freedep, "track a block free"); 684 SOFTDEP_TYPE(JADDREF, jaddref, "Journal inode ref add"); 685 SOFTDEP_TYPE(JREMREF, jremref, "Journal inode ref remove"); 686 SOFTDEP_TYPE(JMVREF, jmvref, "Journal inode ref move"); 687 SOFTDEP_TYPE(JNEWBLK, jnewblk, "Journal new block"); 688 SOFTDEP_TYPE(JFREEBLK, jfreeblk, "Journal free block"); 689 SOFTDEP_TYPE(JFREEFRAG, jfreefrag, "Journal free frag"); 690 SOFTDEP_TYPE(JSEG, jseg, "Journal segment"); 691 SOFTDEP_TYPE(JSEGDEP, jsegdep, "Journal segment complete"); 692 SOFTDEP_TYPE(SBDEP, sbdep, "Superblock write dependency"); 693 SOFTDEP_TYPE(JTRUNC, jtrunc, "Journal inode truncation"); 694 SOFTDEP_TYPE(JFSYNC, jfsync, "Journal fsync complete"); 695 696 static MALLOC_DEFINE(M_SENTINEL, "sentinel", "Worklist sentinel"); 697 698 static MALLOC_DEFINE(M_SAVEDINO, "savedino", "Saved inodes"); 699 static MALLOC_DEFINE(M_JBLOCKS, "jblocks", "Journal block locations"); 700 static MALLOC_DEFINE(M_MOUNTDATA, "softdep", "Softdep per-mount data"); 701 702 #define M_SOFTDEP_FLAGS (M_WAITOK) 703 704 /* 705 * translate from workitem type to memory type 706 * MUST match the defines above, such that memtype[D_XXX] == M_XXX 707 */ 708 static struct malloc_type *memtype[] = { 709 NULL, 710 M_PAGEDEP, 711 M_INODEDEP, 712 M_BMSAFEMAP, 713 M_NEWBLK, 714 M_ALLOCDIRECT, 715 M_INDIRDEP, 716 M_ALLOCINDIR, 717 M_FREEFRAG, 718 M_FREEBLKS, 719 M_FREEFILE, 720 M_DIRADD, 721 M_MKDIR, 722 M_DIRREM, 723 M_NEWDIRBLK, 724 M_FREEWORK, 725 M_FREEDEP, 726 M_JADDREF, 727 M_JREMREF, 728 M_JMVREF, 729 M_JNEWBLK, 730 M_JFREEBLK, 731 M_JFREEFRAG, 732 M_JSEG, 733 M_JSEGDEP, 734 M_SBDEP, 735 M_JTRUNC, 736 M_JFSYNC, 737 M_SENTINEL 738 }; 739 740 #define DtoM(type) (memtype[type]) 741 742 /* 743 * Names of malloc types. 744 */ 745 #define TYPENAME(type) \ 746 ((unsigned)(type) <= D_LAST && (unsigned)(type) >= D_FIRST ? \ 747 memtype[type]->ks_shortdesc : "???") 748 /* 749 * End system adaptation definitions. 750 */ 751 752 #define DOTDOT_OFFSET offsetof(struct dirtemplate, dotdot_ino) 753 #define DOT_OFFSET offsetof(struct dirtemplate, dot_ino) 754 755 /* 756 * Internal function prototypes. 757 */ 758 static void check_clear_deps(struct mount *); 759 static void softdep_error(char *, int); 760 static int softdep_prerename_vnode(struct ufsmount *, struct vnode *); 761 static int softdep_process_worklist(struct mount *, int); 762 static int softdep_waitidle(struct mount *, int); 763 static void drain_output(struct vnode *); 764 static struct buf *getdirtybuf(struct buf *, struct rwlock *, int); 765 static int check_inodedep_free(struct inodedep *); 766 static void clear_remove(struct mount *); 767 static void clear_inodedeps(struct mount *); 768 static void unlinked_inodedep(struct mount *, struct inodedep *); 769 static void clear_unlinked_inodedep(struct inodedep *); 770 static struct inodedep *first_unlinked_inodedep(struct ufsmount *); 771 static int flush_pagedep_deps(struct vnode *, struct mount *, 772 struct diraddhd *, struct buf *); 773 static int free_pagedep(struct pagedep *); 774 static int flush_newblk_dep(struct vnode *, struct mount *, ufs_lbn_t); 775 static int flush_inodedep_deps(struct vnode *, struct mount *, ino_t); 776 static int flush_deplist(struct allocdirectlst *, int, int *); 777 static int sync_cgs(struct mount *, int); 778 static int handle_written_filepage(struct pagedep *, struct buf *, int); 779 static int handle_written_sbdep(struct sbdep *, struct buf *); 780 static void initiate_write_sbdep(struct sbdep *); 781 static void diradd_inode_written(struct diradd *, struct inodedep *); 782 static int handle_written_indirdep(struct indirdep *, struct buf *, 783 struct buf**, int); 784 static int handle_written_inodeblock(struct inodedep *, struct buf *, int); 785 static int jnewblk_rollforward(struct jnewblk *, struct fs *, struct cg *, 786 uint8_t *); 787 static int handle_written_bmsafemap(struct bmsafemap *, struct buf *, int); 788 static void handle_written_jaddref(struct jaddref *); 789 static void handle_written_jremref(struct jremref *); 790 static void handle_written_jseg(struct jseg *, struct buf *); 791 static void handle_written_jnewblk(struct jnewblk *); 792 static void handle_written_jblkdep(struct jblkdep *); 793 static void handle_written_jfreefrag(struct jfreefrag *); 794 static void complete_jseg(struct jseg *); 795 static void complete_jsegs(struct jseg *); 796 static void jseg_write(struct ufsmount *ump, struct jseg *, uint8_t *); 797 static void jaddref_write(struct jaddref *, struct jseg *, uint8_t *); 798 static void jremref_write(struct jremref *, struct jseg *, uint8_t *); 799 static void jmvref_write(struct jmvref *, struct jseg *, uint8_t *); 800 static void jtrunc_write(struct jtrunc *, struct jseg *, uint8_t *); 801 static void jfsync_write(struct jfsync *, struct jseg *, uint8_t *data); 802 static void jnewblk_write(struct jnewblk *, struct jseg *, uint8_t *); 803 static void jfreeblk_write(struct jfreeblk *, struct jseg *, uint8_t *); 804 static void jfreefrag_write(struct jfreefrag *, struct jseg *, uint8_t *); 805 static inline void inoref_write(struct inoref *, struct jseg *, 806 struct jrefrec *); 807 static void handle_allocdirect_partdone(struct allocdirect *, 808 struct workhead *); 809 static struct jnewblk *cancel_newblk(struct newblk *, struct worklist *, 810 struct workhead *); 811 static void indirdep_complete(struct indirdep *); 812 static int indirblk_lookup(struct mount *, ufs2_daddr_t); 813 static void indirblk_insert(struct freework *); 814 static void indirblk_remove(struct freework *); 815 static void handle_allocindir_partdone(struct allocindir *); 816 static void initiate_write_filepage(struct pagedep *, struct buf *); 817 static void initiate_write_indirdep(struct indirdep*, struct buf *); 818 static void handle_written_mkdir(struct mkdir *, int); 819 static int jnewblk_rollback(struct jnewblk *, struct fs *, struct cg *, 820 uint8_t *); 821 static void initiate_write_bmsafemap(struct bmsafemap *, struct buf *); 822 static void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *); 823 static void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *); 824 static void handle_workitem_freefile(struct freefile *); 825 static int handle_workitem_remove(struct dirrem *, int); 826 static struct dirrem *newdirrem(struct buf *, struct inode *, 827 struct inode *, int, struct dirrem **); 828 static struct indirdep *indirdep_lookup(struct mount *, struct inode *, 829 struct buf *); 830 static void cancel_indirdep(struct indirdep *, struct buf *, 831 struct freeblks *); 832 static void free_indirdep(struct indirdep *); 833 static void free_diradd(struct diradd *, struct workhead *); 834 static void merge_diradd(struct inodedep *, struct diradd *); 835 static void complete_diradd(struct diradd *); 836 static struct diradd *diradd_lookup(struct pagedep *, int); 837 static struct jremref *cancel_diradd_dotdot(struct inode *, struct dirrem *, 838 struct jremref *); 839 static struct jremref *cancel_mkdir_dotdot(struct inode *, struct dirrem *, 840 struct jremref *); 841 static void cancel_diradd(struct diradd *, struct dirrem *, struct jremref *, 842 struct jremref *, struct jremref *); 843 static void dirrem_journal(struct dirrem *, struct jremref *, struct jremref *, 844 struct jremref *); 845 static void cancel_allocindir(struct allocindir *, struct buf *bp, 846 struct freeblks *, int); 847 static int setup_trunc_indir(struct freeblks *, struct inode *, 848 ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t); 849 static void complete_trunc_indir(struct freework *); 850 static void trunc_indirdep(struct indirdep *, struct freeblks *, struct buf *, 851 int); 852 static void complete_mkdir(struct mkdir *); 853 static void free_newdirblk(struct newdirblk *); 854 static void free_jremref(struct jremref *); 855 static void free_jaddref(struct jaddref *); 856 static void free_jsegdep(struct jsegdep *); 857 static void free_jsegs(struct jblocks *); 858 static void rele_jseg(struct jseg *); 859 static void free_jseg(struct jseg *, struct jblocks *); 860 static void free_jnewblk(struct jnewblk *); 861 static void free_jblkdep(struct jblkdep *); 862 static void free_jfreefrag(struct jfreefrag *); 863 static void free_freedep(struct freedep *); 864 static void journal_jremref(struct dirrem *, struct jremref *, 865 struct inodedep *); 866 static void cancel_jnewblk(struct jnewblk *, struct workhead *); 867 static int cancel_jaddref(struct jaddref *, struct inodedep *, 868 struct workhead *); 869 static void cancel_jfreefrag(struct jfreefrag *); 870 static inline void setup_freedirect(struct freeblks *, struct inode *, 871 int, int); 872 static inline void setup_freeext(struct freeblks *, struct inode *, int, int); 873 static inline void setup_freeindir(struct freeblks *, struct inode *, int, 874 ufs_lbn_t, int); 875 static inline struct freeblks *newfreeblks(struct mount *, struct inode *); 876 static void freeblks_free(struct ufsmount *, struct freeblks *, int); 877 static void indir_trunc(struct freework *, ufs2_daddr_t, ufs_lbn_t); 878 static ufs2_daddr_t blkcount(struct fs *, ufs2_daddr_t, off_t); 879 static int trunc_check_buf(struct buf *, int *, ufs_lbn_t, int, int); 880 static void trunc_dependencies(struct inode *, struct freeblks *, ufs_lbn_t, 881 int, int); 882 static void trunc_pages(struct inode *, off_t, ufs2_daddr_t, int); 883 static int cancel_pagedep(struct pagedep *, struct freeblks *, int); 884 static int deallocate_dependencies(struct buf *, struct freeblks *, int); 885 static void newblk_freefrag(struct newblk*); 886 static void free_newblk(struct newblk *); 887 static void cancel_allocdirect(struct allocdirectlst *, 888 struct allocdirect *, struct freeblks *); 889 static int check_inode_unwritten(struct inodedep *); 890 static int free_inodedep(struct inodedep *); 891 static void freework_freeblock(struct freework *, u_long); 892 static void freework_enqueue(struct freework *); 893 static int handle_workitem_freeblocks(struct freeblks *, int); 894 static int handle_complete_freeblocks(struct freeblks *, int); 895 static void handle_workitem_indirblk(struct freework *); 896 static void handle_written_freework(struct freework *); 897 static void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *); 898 static struct worklist *jnewblk_merge(struct worklist *, struct worklist *, 899 struct workhead *); 900 static struct freefrag *setup_allocindir_phase2(struct buf *, struct inode *, 901 struct inodedep *, struct allocindir *, ufs_lbn_t); 902 static struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t, 903 ufs2_daddr_t, ufs_lbn_t); 904 static void handle_workitem_freefrag(struct freefrag *); 905 static struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long, 906 ufs_lbn_t, u_long); 907 static void allocdirect_merge(struct allocdirectlst *, 908 struct allocdirect *, struct allocdirect *); 909 static struct freefrag *allocindir_merge(struct allocindir *, 910 struct allocindir *); 911 static int bmsafemap_find(struct bmsafemap_hashhead *, int, 912 struct bmsafemap **); 913 static struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *, 914 int cg, struct bmsafemap *); 915 static int newblk_find(struct newblk_hashhead *, ufs2_daddr_t, int, 916 struct newblk **); 917 static int newblk_lookup(struct mount *, ufs2_daddr_t, int, struct newblk **); 918 static int inodedep_find(struct inodedep_hashhead *, ino_t, 919 struct inodedep **); 920 static int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **); 921 static int pagedep_lookup(struct mount *, struct buf *bp, ino_t, ufs_lbn_t, 922 int, struct pagedep **); 923 static int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t, 924 struct pagedep **); 925 static void pause_timer(void *); 926 static int request_cleanup(struct mount *, int); 927 static int softdep_request_cleanup_flush(struct mount *, struct ufsmount *); 928 static void schedule_cleanup(struct mount *); 929 static void softdep_ast_cleanup_proc(struct thread *); 930 static struct ufsmount *softdep_bp_to_mp(struct buf *bp); 931 static int process_worklist_item(struct mount *, int, int); 932 static void process_removes(struct vnode *); 933 static void process_truncates(struct vnode *); 934 static void jwork_move(struct workhead *, struct workhead *); 935 static void jwork_insert(struct workhead *, struct jsegdep *); 936 static void add_to_worklist(struct worklist *, int); 937 static void wake_worklist(struct worklist *); 938 static void wait_worklist(struct worklist *, char *); 939 static void remove_from_worklist(struct worklist *); 940 static void softdep_flush(void *); 941 static void softdep_flushjournal(struct mount *); 942 static int softdep_speedup(struct ufsmount *); 943 static void worklist_speedup(struct mount *); 944 static int journal_mount(struct mount *, struct fs *, struct ucred *); 945 static void journal_unmount(struct ufsmount *); 946 static int journal_space(struct ufsmount *, int); 947 static void journal_suspend(struct ufsmount *); 948 static int journal_unsuspend(struct ufsmount *ump); 949 static void add_to_journal(struct worklist *); 950 static void remove_from_journal(struct worklist *); 951 static bool softdep_excess_items(struct ufsmount *, int); 952 static void softdep_process_journal(struct mount *, struct worklist *, int); 953 static struct jremref *newjremref(struct dirrem *, struct inode *, 954 struct inode *ip, off_t, nlink_t); 955 static struct jaddref *newjaddref(struct inode *, ino_t, off_t, int16_t, 956 uint16_t); 957 static inline void newinoref(struct inoref *, ino_t, ino_t, off_t, nlink_t, 958 uint16_t); 959 static inline struct jsegdep *inoref_jseg(struct inoref *); 960 static struct jmvref *newjmvref(struct inode *, ino_t, off_t, off_t); 961 static struct jfreeblk *newjfreeblk(struct freeblks *, ufs_lbn_t, 962 ufs2_daddr_t, int); 963 static void adjust_newfreework(struct freeblks *, int); 964 static struct jtrunc *newjtrunc(struct freeblks *, off_t, int); 965 static void move_newblock_dep(struct jaddref *, struct inodedep *); 966 static void cancel_jfreeblk(struct freeblks *, ufs2_daddr_t); 967 static struct jfreefrag *newjfreefrag(struct freefrag *, struct inode *, 968 ufs2_daddr_t, long, ufs_lbn_t); 969 static struct freework *newfreework(struct ufsmount *, struct freeblks *, 970 struct freework *, ufs_lbn_t, ufs2_daddr_t, int, int, int); 971 static int jwait(struct worklist *, int); 972 static struct inodedep *inodedep_lookup_ip(struct inode *); 973 static int bmsafemap_backgroundwrite(struct bmsafemap *, struct buf *); 974 static struct freefile *handle_bufwait(struct inodedep *, struct workhead *); 975 static void handle_jwork(struct workhead *); 976 static struct mkdir *setup_newdir(struct diradd *, ino_t, ino_t, struct buf *, 977 struct mkdir **); 978 static struct jblocks *jblocks_create(void); 979 static ufs2_daddr_t jblocks_alloc(struct jblocks *, int, int *); 980 static void jblocks_free(struct jblocks *, struct mount *, int); 981 static void jblocks_destroy(struct jblocks *); 982 static void jblocks_add(struct jblocks *, ufs2_daddr_t, int); 983 984 /* 985 * Exported softdep operations. 986 */ 987 static void softdep_disk_io_initiation(struct buf *); 988 static void softdep_disk_write_complete(struct buf *); 989 static void softdep_deallocate_dependencies(struct buf *); 990 static int softdep_count_dependencies(struct buf *bp, int); 991 992 /* 993 * Global lock over all of soft updates. 994 */ 995 static struct mtx lk; 996 MTX_SYSINIT(softdep_lock, &lk, "global softdep", MTX_DEF); 997 998 #define ACQUIRE_GBLLOCK(lk) mtx_lock(lk) 999 #define FREE_GBLLOCK(lk) mtx_unlock(lk) 1000 #define GBLLOCK_OWNED(lk) mtx_assert((lk), MA_OWNED) 1001 1002 /* 1003 * Per-filesystem soft-updates locking. 1004 */ 1005 #define LOCK_PTR(ump) (&(ump)->um_softdep->sd_fslock) 1006 #define TRY_ACQUIRE_LOCK(ump) rw_try_wlock(&(ump)->um_softdep->sd_fslock) 1007 #define ACQUIRE_LOCK(ump) rw_wlock(&(ump)->um_softdep->sd_fslock) 1008 #define FREE_LOCK(ump) rw_wunlock(&(ump)->um_softdep->sd_fslock) 1009 #define LOCK_OWNED(ump) rw_assert(&(ump)->um_softdep->sd_fslock, \ 1010 RA_WLOCKED) 1011 1012 #define BUF_AREC(bp) lockallowrecurse(&(bp)->b_lock) 1013 #define BUF_NOREC(bp) lockdisablerecurse(&(bp)->b_lock) 1014 1015 /* 1016 * Worklist queue management. 1017 * These routines require that the lock be held. 1018 */ 1019 #ifndef /* NOT */ INVARIANTS 1020 #define WORKLIST_INSERT(head, item) do { \ 1021 (item)->wk_state |= ONWORKLIST; \ 1022 LIST_INSERT_HEAD(head, item, wk_list); \ 1023 } while (0) 1024 #define WORKLIST_REMOVE(item) do { \ 1025 (item)->wk_state &= ~ONWORKLIST; \ 1026 LIST_REMOVE(item, wk_list); \ 1027 } while (0) 1028 #define WORKLIST_INSERT_UNLOCKED WORKLIST_INSERT 1029 #define WORKLIST_REMOVE_UNLOCKED WORKLIST_REMOVE 1030 1031 #else /* INVARIANTS */ 1032 static void worklist_insert(struct workhead *, struct worklist *, int, 1033 const char *, int); 1034 static void worklist_remove(struct worklist *, int, const char *, int); 1035 1036 #define WORKLIST_INSERT(head, item) \ 1037 worklist_insert(head, item, 1, __func__, __LINE__) 1038 #define WORKLIST_INSERT_UNLOCKED(head, item)\ 1039 worklist_insert(head, item, 0, __func__, __LINE__) 1040 #define WORKLIST_REMOVE(item)\ 1041 worklist_remove(item, 1, __func__, __LINE__) 1042 #define WORKLIST_REMOVE_UNLOCKED(item)\ 1043 worklist_remove(item, 0, __func__, __LINE__) 1044 1045 static void 1046 worklist_insert(head, item, locked, func, line) 1047 struct workhead *head; 1048 struct worklist *item; 1049 int locked; 1050 const char *func; 1051 int line; 1052 { 1053 1054 if (locked) 1055 LOCK_OWNED(VFSTOUFS(item->wk_mp)); 1056 if (item->wk_state & ONWORKLIST) 1057 panic("worklist_insert: %p %s(0x%X) already on list, " 1058 "added in function %s at line %d", 1059 item, TYPENAME(item->wk_type), item->wk_state, 1060 item->wk_func, item->wk_line); 1061 item->wk_state |= ONWORKLIST; 1062 item->wk_func = func; 1063 item->wk_line = line; 1064 LIST_INSERT_HEAD(head, item, wk_list); 1065 } 1066 1067 static void 1068 worklist_remove(item, locked, func, line) 1069 struct worklist *item; 1070 int locked; 1071 const char *func; 1072 int line; 1073 { 1074 1075 if (locked) 1076 LOCK_OWNED(VFSTOUFS(item->wk_mp)); 1077 if ((item->wk_state & ONWORKLIST) == 0) 1078 panic("worklist_remove: %p %s(0x%X) not on list, " 1079 "removed in function %s at line %d", 1080 item, TYPENAME(item->wk_type), item->wk_state, 1081 item->wk_func, item->wk_line); 1082 item->wk_state &= ~ONWORKLIST; 1083 item->wk_func = func; 1084 item->wk_line = line; 1085 LIST_REMOVE(item, wk_list); 1086 } 1087 #endif /* INVARIANTS */ 1088 1089 /* 1090 * Merge two jsegdeps keeping only the oldest one as newer references 1091 * can't be discarded until after older references. 1092 */ 1093 static inline struct jsegdep * 1094 jsegdep_merge(struct jsegdep *one, struct jsegdep *two) 1095 { 1096 struct jsegdep *swp; 1097 1098 if (two == NULL) 1099 return (one); 1100 1101 if (one->jd_seg->js_seq > two->jd_seg->js_seq) { 1102 swp = one; 1103 one = two; 1104 two = swp; 1105 } 1106 WORKLIST_REMOVE(&two->jd_list); 1107 free_jsegdep(two); 1108 1109 return (one); 1110 } 1111 1112 /* 1113 * If two freedeps are compatible free one to reduce list size. 1114 */ 1115 static inline struct freedep * 1116 freedep_merge(struct freedep *one, struct freedep *two) 1117 { 1118 if (two == NULL) 1119 return (one); 1120 1121 if (one->fd_freework == two->fd_freework) { 1122 WORKLIST_REMOVE(&two->fd_list); 1123 free_freedep(two); 1124 } 1125 return (one); 1126 } 1127 1128 /* 1129 * Move journal work from one list to another. Duplicate freedeps and 1130 * jsegdeps are coalesced to keep the lists as small as possible. 1131 */ 1132 static void 1133 jwork_move(dst, src) 1134 struct workhead *dst; 1135 struct workhead *src; 1136 { 1137 struct freedep *freedep; 1138 struct jsegdep *jsegdep; 1139 struct worklist *wkn; 1140 struct worklist *wk; 1141 1142 KASSERT(dst != src, 1143 ("jwork_move: dst == src")); 1144 freedep = NULL; 1145 jsegdep = NULL; 1146 LIST_FOREACH_SAFE(wk, dst, wk_list, wkn) { 1147 if (wk->wk_type == D_JSEGDEP) 1148 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1149 else if (wk->wk_type == D_FREEDEP) 1150 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1151 } 1152 1153 while ((wk = LIST_FIRST(src)) != NULL) { 1154 WORKLIST_REMOVE(wk); 1155 WORKLIST_INSERT(dst, wk); 1156 if (wk->wk_type == D_JSEGDEP) { 1157 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1158 continue; 1159 } 1160 if (wk->wk_type == D_FREEDEP) 1161 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1162 } 1163 } 1164 1165 static void 1166 jwork_insert(dst, jsegdep) 1167 struct workhead *dst; 1168 struct jsegdep *jsegdep; 1169 { 1170 struct jsegdep *jsegdepn; 1171 struct worklist *wk; 1172 1173 LIST_FOREACH(wk, dst, wk_list) 1174 if (wk->wk_type == D_JSEGDEP) 1175 break; 1176 if (wk == NULL) { 1177 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1178 return; 1179 } 1180 jsegdepn = WK_JSEGDEP(wk); 1181 if (jsegdep->jd_seg->js_seq < jsegdepn->jd_seg->js_seq) { 1182 WORKLIST_REMOVE(wk); 1183 free_jsegdep(jsegdepn); 1184 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1185 } else 1186 free_jsegdep(jsegdep); 1187 } 1188 1189 /* 1190 * Routines for tracking and managing workitems. 1191 */ 1192 static void workitem_free(struct worklist *, int); 1193 static void workitem_alloc(struct worklist *, int, struct mount *); 1194 static void workitem_reassign(struct worklist *, int); 1195 1196 #define WORKITEM_FREE(item, type) \ 1197 workitem_free((struct worklist *)(item), (type)) 1198 #define WORKITEM_REASSIGN(item, type) \ 1199 workitem_reassign((struct worklist *)(item), (type)) 1200 1201 static void 1202 workitem_free(item, type) 1203 struct worklist *item; 1204 int type; 1205 { 1206 struct ufsmount *ump; 1207 1208 #ifdef INVARIANTS 1209 if (item->wk_state & ONWORKLIST) 1210 panic("workitem_free: %s(0x%X) still on list, " 1211 "added in function %s at line %d", 1212 TYPENAME(item->wk_type), item->wk_state, 1213 item->wk_func, item->wk_line); 1214 if (item->wk_type != type && type != D_NEWBLK) 1215 panic("workitem_free: type mismatch %s != %s", 1216 TYPENAME(item->wk_type), TYPENAME(type)); 1217 #endif 1218 if (item->wk_state & IOWAITING) 1219 wakeup(item); 1220 ump = VFSTOUFS(item->wk_mp); 1221 LOCK_OWNED(ump); 1222 KASSERT(ump->softdep_deps > 0, 1223 ("workitem_free: %s: softdep_deps going negative", 1224 ump->um_fs->fs_fsmnt)); 1225 if (--ump->softdep_deps == 0 && ump->softdep_req) 1226 wakeup(&ump->softdep_deps); 1227 KASSERT(dep_current[item->wk_type] > 0, 1228 ("workitem_free: %s: dep_current[%s] going negative", 1229 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1230 KASSERT(ump->softdep_curdeps[item->wk_type] > 0, 1231 ("workitem_free: %s: softdep_curdeps[%s] going negative", 1232 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1233 atomic_subtract_long(&dep_current[item->wk_type], 1); 1234 ump->softdep_curdeps[item->wk_type] -= 1; 1235 #ifdef INVARIANTS 1236 LIST_REMOVE(item, wk_all); 1237 #endif 1238 free(item, DtoM(type)); 1239 } 1240 1241 static void 1242 workitem_alloc(item, type, mp) 1243 struct worklist *item; 1244 int type; 1245 struct mount *mp; 1246 { 1247 struct ufsmount *ump; 1248 1249 item->wk_type = type; 1250 item->wk_mp = mp; 1251 item->wk_state = 0; 1252 1253 ump = VFSTOUFS(mp); 1254 ACQUIRE_GBLLOCK(&lk); 1255 dep_current[type]++; 1256 if (dep_current[type] > dep_highuse[type]) 1257 dep_highuse[type] = dep_current[type]; 1258 dep_total[type]++; 1259 FREE_GBLLOCK(&lk); 1260 ACQUIRE_LOCK(ump); 1261 ump->softdep_curdeps[type] += 1; 1262 ump->softdep_deps++; 1263 ump->softdep_accdeps++; 1264 #ifdef INVARIANTS 1265 LIST_INSERT_HEAD(&ump->softdep_alldeps[type], item, wk_all); 1266 #endif 1267 FREE_LOCK(ump); 1268 } 1269 1270 static void 1271 workitem_reassign(item, newtype) 1272 struct worklist *item; 1273 int newtype; 1274 { 1275 struct ufsmount *ump; 1276 1277 ump = VFSTOUFS(item->wk_mp); 1278 LOCK_OWNED(ump); 1279 KASSERT(ump->softdep_curdeps[item->wk_type] > 0, 1280 ("workitem_reassign: %s: softdep_curdeps[%s] going negative", 1281 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1282 ump->softdep_curdeps[item->wk_type] -= 1; 1283 ump->softdep_curdeps[newtype] += 1; 1284 KASSERT(dep_current[item->wk_type] > 0, 1285 ("workitem_reassign: %s: dep_current[%s] going negative", 1286 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1287 ACQUIRE_GBLLOCK(&lk); 1288 dep_current[newtype]++; 1289 dep_current[item->wk_type]--; 1290 if (dep_current[newtype] > dep_highuse[newtype]) 1291 dep_highuse[newtype] = dep_current[newtype]; 1292 dep_total[newtype]++; 1293 FREE_GBLLOCK(&lk); 1294 item->wk_type = newtype; 1295 } 1296 1297 /* 1298 * Workitem queue management 1299 */ 1300 static int max_softdeps; /* maximum number of structs before slowdown */ 1301 static int tickdelay = 2; /* number of ticks to pause during slowdown */ 1302 static int proc_waiting; /* tracks whether we have a timeout posted */ 1303 static int *stat_countp; /* statistic to count in proc_waiting timeout */ 1304 static struct callout softdep_callout; 1305 static int req_clear_inodedeps; /* syncer process flush some inodedeps */ 1306 static int req_clear_remove; /* syncer process flush some freeblks */ 1307 static int softdep_flushcache = 0; /* Should we do BIO_FLUSH? */ 1308 1309 /* 1310 * runtime statistics 1311 */ 1312 static int stat_flush_threads; /* number of softdep flushing threads */ 1313 static int stat_worklist_push; /* number of worklist cleanups */ 1314 static int stat_delayed_inact; /* number of delayed inactivation cleanups */ 1315 static int stat_blk_limit_push; /* number of times block limit neared */ 1316 static int stat_ino_limit_push; /* number of times inode limit neared */ 1317 static int stat_blk_limit_hit; /* number of times block slowdown imposed */ 1318 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */ 1319 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */ 1320 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */ 1321 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */ 1322 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */ 1323 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */ 1324 static int stat_jaddref; /* bufs redirtied as ino bitmap can not write */ 1325 static int stat_jnewblk; /* bufs redirtied as blk bitmap can not write */ 1326 static int stat_journal_min; /* Times hit journal min threshold */ 1327 static int stat_journal_low; /* Times hit journal low threshold */ 1328 static int stat_journal_wait; /* Times blocked in jwait(). */ 1329 static int stat_jwait_filepage; /* Times blocked in jwait() for filepage. */ 1330 static int stat_jwait_freeblks; /* Times blocked in jwait() for freeblks. */ 1331 static int stat_jwait_inode; /* Times blocked in jwait() for inodes. */ 1332 static int stat_jwait_newblk; /* Times blocked in jwait() for newblks. */ 1333 static int stat_cleanup_high_delay; /* Maximum cleanup delay (in ticks) */ 1334 static int stat_cleanup_blkrequests; /* Number of block cleanup requests */ 1335 static int stat_cleanup_inorequests; /* Number of inode cleanup requests */ 1336 static int stat_cleanup_retries; /* Number of cleanups that needed to flush */ 1337 static int stat_cleanup_failures; /* Number of cleanup requests that failed */ 1338 static int stat_emptyjblocks; /* Number of potentially empty journal blocks */ 1339 1340 SYSCTL_INT(_debug_softdep, OID_AUTO, max_softdeps, CTLFLAG_RW, 1341 &max_softdeps, 0, ""); 1342 SYSCTL_INT(_debug_softdep, OID_AUTO, tickdelay, CTLFLAG_RW, 1343 &tickdelay, 0, ""); 1344 SYSCTL_INT(_debug_softdep, OID_AUTO, flush_threads, CTLFLAG_RD, 1345 &stat_flush_threads, 0, ""); 1346 SYSCTL_INT(_debug_softdep, OID_AUTO, worklist_push, 1347 CTLFLAG_RW | CTLFLAG_STATS, &stat_worklist_push, 0,""); 1348 SYSCTL_INT(_debug_softdep, OID_AUTO, delayed_inactivations, CTLFLAG_RD, 1349 &stat_delayed_inact, 0, ""); 1350 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_push, 1351 CTLFLAG_RW | CTLFLAG_STATS, &stat_blk_limit_push, 0,""); 1352 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_push, 1353 CTLFLAG_RW | CTLFLAG_STATS, &stat_ino_limit_push, 0,""); 1354 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_hit, 1355 CTLFLAG_RW | CTLFLAG_STATS, &stat_blk_limit_hit, 0, ""); 1356 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_hit, 1357 CTLFLAG_RW | CTLFLAG_STATS, &stat_ino_limit_hit, 0, ""); 1358 SYSCTL_INT(_debug_softdep, OID_AUTO, sync_limit_hit, 1359 CTLFLAG_RW | CTLFLAG_STATS, &stat_sync_limit_hit, 0, ""); 1360 SYSCTL_INT(_debug_softdep, OID_AUTO, indir_blk_ptrs, 1361 CTLFLAG_RW | CTLFLAG_STATS, &stat_indir_blk_ptrs, 0, ""); 1362 SYSCTL_INT(_debug_softdep, OID_AUTO, inode_bitmap, 1363 CTLFLAG_RW | CTLFLAG_STATS, &stat_inode_bitmap, 0, ""); 1364 SYSCTL_INT(_debug_softdep, OID_AUTO, direct_blk_ptrs, 1365 CTLFLAG_RW | CTLFLAG_STATS, &stat_direct_blk_ptrs, 0, ""); 1366 SYSCTL_INT(_debug_softdep, OID_AUTO, dir_entry, 1367 CTLFLAG_RW | CTLFLAG_STATS, &stat_dir_entry, 0, ""); 1368 SYSCTL_INT(_debug_softdep, OID_AUTO, jaddref_rollback, 1369 CTLFLAG_RW | CTLFLAG_STATS, &stat_jaddref, 0, ""); 1370 SYSCTL_INT(_debug_softdep, OID_AUTO, jnewblk_rollback, 1371 CTLFLAG_RW | CTLFLAG_STATS, &stat_jnewblk, 0, ""); 1372 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_low, 1373 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_low, 0, ""); 1374 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_min, 1375 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_min, 0, ""); 1376 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_wait, 1377 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_wait, 0, ""); 1378 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_filepage, 1379 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_filepage, 0, ""); 1380 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_freeblks, 1381 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_freeblks, 0, ""); 1382 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_inode, 1383 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_inode, 0, ""); 1384 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_newblk, 1385 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_newblk, 0, ""); 1386 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_blkrequests, 1387 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_blkrequests, 0, ""); 1388 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_inorequests, 1389 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_inorequests, 0, ""); 1390 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_high_delay, 1391 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_high_delay, 0, ""); 1392 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_retries, 1393 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_retries, 0, ""); 1394 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_failures, 1395 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_failures, 0, ""); 1396 1397 SYSCTL_INT(_debug_softdep, OID_AUTO, flushcache, CTLFLAG_RW, 1398 &softdep_flushcache, 0, ""); 1399 SYSCTL_INT(_debug_softdep, OID_AUTO, emptyjblocks, CTLFLAG_RD, 1400 &stat_emptyjblocks, 0, ""); 1401 1402 SYSCTL_DECL(_vfs_ffs); 1403 1404 /* Whether to recompute the summary at mount time */ 1405 static int compute_summary_at_mount = 0; 1406 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW, 1407 &compute_summary_at_mount, 0, "Recompute summary at mount"); 1408 static int print_threads = 0; 1409 SYSCTL_INT(_debug_softdep, OID_AUTO, print_threads, CTLFLAG_RW, 1410 &print_threads, 0, "Notify flusher thread start/stop"); 1411 1412 /* List of all filesystems mounted with soft updates */ 1413 static TAILQ_HEAD(, mount_softdeps) softdepmounts; 1414 1415 static void 1416 get_parent_vp_unlock_bp(struct mount *mp, struct buf *bp, 1417 struct diraddhd *diraddhdp, struct diraddhd *unfinishedp) 1418 { 1419 struct diradd *dap; 1420 1421 /* 1422 * Requeue unfinished dependencies before 1423 * unlocking buffer, which could make 1424 * diraddhdp invalid. 1425 */ 1426 ACQUIRE_LOCK(VFSTOUFS(mp)); 1427 while ((dap = LIST_FIRST(unfinishedp)) != NULL) { 1428 LIST_REMOVE(dap, da_pdlist); 1429 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist); 1430 } 1431 FREE_LOCK(VFSTOUFS(mp)); 1432 1433 bp->b_vflags &= ~BV_SCANNED; 1434 BUF_NOREC(bp); 1435 BUF_UNLOCK(bp); 1436 } 1437 1438 /* 1439 * This function fetches inode inum on mount point mp. We already 1440 * hold a locked vnode vp, and might have a locked buffer bp belonging 1441 * to vp. 1442 1443 * We must not block on acquiring the new inode lock as we will get 1444 * into a lock-order reversal with the buffer lock and possibly get a 1445 * deadlock. Thus if we cannot instantiate the requested vnode 1446 * without sleeping on its lock, we must unlock the vnode and the 1447 * buffer before doing a blocking on the vnode lock. We return 1448 * ERELOOKUP if we have had to unlock either the vnode or the buffer so 1449 * that the caller can reassess its state. 1450 * 1451 * Top-level VFS code (for syscalls and other consumers, e.g. callers 1452 * of VOP_FSYNC() in syncer) check for ERELOOKUP and restart at safe 1453 * point. 1454 * 1455 * Since callers expect to operate on fully constructed vnode, we also 1456 * recheck v_data after relock, and return ENOENT if NULL. 1457 * 1458 * If unlocking bp, we must unroll dequeueing its unfinished 1459 * dependencies, and clear scan flag, before unlocking. If unlocking 1460 * vp while it is under deactivation, we re-queue deactivation. 1461 */ 1462 static int 1463 get_parent_vp(struct vnode *vp, struct mount *mp, ino_t inum, struct buf *bp, 1464 struct diraddhd *diraddhdp, struct diraddhd *unfinishedp, 1465 struct vnode **rvp) 1466 { 1467 struct vnode *pvp; 1468 int error; 1469 bool bplocked; 1470 1471 ASSERT_VOP_ELOCKED(vp, "child vnode must be locked"); 1472 for (bplocked = true, pvp = NULL;;) { 1473 error = ffs_vgetf(mp, inum, LK_EXCLUSIVE | LK_NOWAIT, &pvp, 1474 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP); 1475 if (error == 0) { 1476 /* 1477 * Since we could have unlocked vp, the inode 1478 * number could no longer indicate a 1479 * constructed node. In this case, we must 1480 * restart the syscall. 1481 */ 1482 if (VTOI(pvp)->i_mode == 0 || !bplocked) { 1483 if (bp != NULL && bplocked) 1484 get_parent_vp_unlock_bp(mp, bp, 1485 diraddhdp, unfinishedp); 1486 if (VTOI(pvp)->i_mode == 0) 1487 vgone(pvp); 1488 error = ERELOOKUP; 1489 goto out2; 1490 } 1491 goto out1; 1492 } 1493 if (bp != NULL && bplocked) { 1494 get_parent_vp_unlock_bp(mp, bp, diraddhdp, unfinishedp); 1495 bplocked = false; 1496 } 1497 1498 /* 1499 * Do not drop vnode lock while inactivating during 1500 * vunref. This would result in leaks of the VI flags 1501 * and reclaiming of non-truncated vnode. Instead, 1502 * re-schedule inactivation hoping that we would be 1503 * able to sync inode later. 1504 */ 1505 if ((vp->v_iflag & VI_DOINGINACT) != 0 && 1506 (vp->v_vflag & VV_UNREF) != 0) { 1507 VI_LOCK(vp); 1508 vp->v_iflag |= VI_OWEINACT; 1509 VI_UNLOCK(vp); 1510 return (ERELOOKUP); 1511 } 1512 1513 VOP_UNLOCK(vp); 1514 error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &pvp, 1515 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP); 1516 if (error != 0) { 1517 MPASS(error != ERELOOKUP); 1518 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1519 break; 1520 } 1521 if (VTOI(pvp)->i_mode == 0) { 1522 vgone(pvp); 1523 vput(pvp); 1524 pvp = NULL; 1525 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1526 error = ERELOOKUP; 1527 break; 1528 } 1529 error = vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT); 1530 if (error == 0) 1531 break; 1532 vput(pvp); 1533 pvp = NULL; 1534 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1535 if (vp->v_data == NULL) { 1536 error = ENOENT; 1537 break; 1538 } 1539 } 1540 if (bp != NULL) { 1541 MPASS(!bplocked); 1542 error = ERELOOKUP; 1543 } 1544 out2: 1545 if (error != 0 && pvp != NULL) { 1546 vput(pvp); 1547 pvp = NULL; 1548 } 1549 out1: 1550 *rvp = pvp; 1551 ASSERT_VOP_ELOCKED(vp, "child vnode must be locked on return"); 1552 return (error); 1553 } 1554 1555 /* 1556 * This function cleans the worklist for a filesystem. 1557 * Each filesystem running with soft dependencies gets its own 1558 * thread to run in this function. The thread is started up in 1559 * softdep_mount and shutdown in softdep_unmount. They show up 1560 * as part of the kernel "bufdaemon" process whose process 1561 * entry is available in bufdaemonproc. 1562 */ 1563 static int searchfailed; 1564 extern struct proc *bufdaemonproc; 1565 static void 1566 softdep_flush(addr) 1567 void *addr; 1568 { 1569 struct mount *mp; 1570 struct thread *td; 1571 struct ufsmount *ump; 1572 int cleanups; 1573 1574 td = curthread; 1575 td->td_pflags |= TDP_NORUNNINGBUF; 1576 mp = (struct mount *)addr; 1577 ump = VFSTOUFS(mp); 1578 atomic_add_int(&stat_flush_threads, 1); 1579 ACQUIRE_LOCK(ump); 1580 ump->softdep_flags &= ~FLUSH_STARTING; 1581 wakeup(&ump->softdep_flushtd); 1582 FREE_LOCK(ump); 1583 if (print_threads) { 1584 if (stat_flush_threads == 1) 1585 printf("Running %s at pid %d\n", bufdaemonproc->p_comm, 1586 bufdaemonproc->p_pid); 1587 printf("Start thread %s\n", td->td_name); 1588 } 1589 for (;;) { 1590 while (softdep_process_worklist(mp, 0) > 0 || 1591 (MOUNTEDSUJ(mp) && 1592 VFSTOUFS(mp)->softdep_jblocks->jb_suspended)) 1593 kthread_suspend_check(); 1594 ACQUIRE_LOCK(ump); 1595 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1596 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, 1597 "sdflush", hz / 2); 1598 ump->softdep_flags &= ~FLUSH_CLEANUP; 1599 /* 1600 * Check to see if we are done and need to exit. 1601 */ 1602 if ((ump->softdep_flags & FLUSH_EXIT) == 0) { 1603 FREE_LOCK(ump); 1604 continue; 1605 } 1606 ump->softdep_flags &= ~FLUSH_EXIT; 1607 cleanups = ump->um_softdep->sd_cleanups; 1608 FREE_LOCK(ump); 1609 wakeup(&ump->softdep_flags); 1610 if (print_threads) { 1611 printf("Stop thread %s: searchfailed %d, " 1612 "did cleanups %d\n", 1613 td->td_name, searchfailed, cleanups); 1614 } 1615 atomic_subtract_int(&stat_flush_threads, 1); 1616 kthread_exit(); 1617 panic("kthread_exit failed\n"); 1618 } 1619 } 1620 1621 static void 1622 worklist_speedup(mp) 1623 struct mount *mp; 1624 { 1625 struct ufsmount *ump; 1626 1627 ump = VFSTOUFS(mp); 1628 LOCK_OWNED(ump); 1629 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1630 ump->softdep_flags |= FLUSH_CLEANUP; 1631 wakeup(&ump->softdep_flushtd); 1632 } 1633 1634 static void 1635 softdep_send_speedup(struct ufsmount *ump, off_t shortage, u_int flags) 1636 { 1637 struct buf *bp; 1638 1639 if ((ump->um_flags & UM_CANSPEEDUP) == 0) 1640 return; 1641 1642 bp = malloc(sizeof(*bp), M_TRIM, M_WAITOK | M_ZERO); 1643 bp->b_iocmd = BIO_SPEEDUP; 1644 bp->b_ioflags = flags; 1645 bp->b_bcount = omin(shortage, LONG_MAX); 1646 g_vfs_strategy(ump->um_bo, bp); 1647 bufwait(bp); 1648 free(bp, M_TRIM); 1649 } 1650 1651 static int 1652 softdep_speedup(ump) 1653 struct ufsmount *ump; 1654 { 1655 struct ufsmount *altump; 1656 struct mount_softdeps *sdp; 1657 1658 LOCK_OWNED(ump); 1659 worklist_speedup(ump->um_mountp); 1660 bd_speedup(); 1661 /* 1662 * If we have global shortages, then we need other 1663 * filesystems to help with the cleanup. Here we wakeup a 1664 * flusher thread for a filesystem that is over its fair 1665 * share of resources. 1666 */ 1667 if (req_clear_inodedeps || req_clear_remove) { 1668 ACQUIRE_GBLLOCK(&lk); 1669 TAILQ_FOREACH(sdp, &softdepmounts, sd_next) { 1670 if ((altump = sdp->sd_ump) == ump) 1671 continue; 1672 if (((req_clear_inodedeps && 1673 altump->softdep_curdeps[D_INODEDEP] > 1674 max_softdeps / stat_flush_threads) || 1675 (req_clear_remove && 1676 altump->softdep_curdeps[D_DIRREM] > 1677 (max_softdeps / 2) / stat_flush_threads)) && 1678 TRY_ACQUIRE_LOCK(altump)) 1679 break; 1680 } 1681 if (sdp == NULL) { 1682 searchfailed++; 1683 FREE_GBLLOCK(&lk); 1684 } else { 1685 /* 1686 * Move to the end of the list so we pick a 1687 * different one on out next try. 1688 */ 1689 TAILQ_REMOVE(&softdepmounts, sdp, sd_next); 1690 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next); 1691 FREE_GBLLOCK(&lk); 1692 if ((altump->softdep_flags & 1693 (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1694 altump->softdep_flags |= FLUSH_CLEANUP; 1695 altump->um_softdep->sd_cleanups++; 1696 wakeup(&altump->softdep_flushtd); 1697 FREE_LOCK(altump); 1698 } 1699 } 1700 return (speedup_syncer()); 1701 } 1702 1703 /* 1704 * Add an item to the end of the work queue. 1705 * This routine requires that the lock be held. 1706 * This is the only routine that adds items to the list. 1707 * The following routine is the only one that removes items 1708 * and does so in order from first to last. 1709 */ 1710 1711 #define WK_HEAD 0x0001 /* Add to HEAD. */ 1712 #define WK_NODELAY 0x0002 /* Process immediately. */ 1713 1714 static void 1715 add_to_worklist(wk, flags) 1716 struct worklist *wk; 1717 int flags; 1718 { 1719 struct ufsmount *ump; 1720 1721 ump = VFSTOUFS(wk->wk_mp); 1722 LOCK_OWNED(ump); 1723 if (wk->wk_state & ONWORKLIST) 1724 panic("add_to_worklist: %s(0x%X) already on list", 1725 TYPENAME(wk->wk_type), wk->wk_state); 1726 wk->wk_state |= ONWORKLIST; 1727 if (ump->softdep_on_worklist == 0) { 1728 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1729 ump->softdep_worklist_tail = wk; 1730 } else if (flags & WK_HEAD) { 1731 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1732 } else { 1733 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list); 1734 ump->softdep_worklist_tail = wk; 1735 } 1736 ump->softdep_on_worklist += 1; 1737 if (flags & WK_NODELAY) 1738 worklist_speedup(wk->wk_mp); 1739 } 1740 1741 /* 1742 * Remove the item to be processed. If we are removing the last 1743 * item on the list, we need to recalculate the tail pointer. 1744 */ 1745 static void 1746 remove_from_worklist(wk) 1747 struct worklist *wk; 1748 { 1749 struct ufsmount *ump; 1750 1751 ump = VFSTOUFS(wk->wk_mp); 1752 if (ump->softdep_worklist_tail == wk) 1753 ump->softdep_worklist_tail = 1754 (struct worklist *)wk->wk_list.le_prev; 1755 WORKLIST_REMOVE(wk); 1756 ump->softdep_on_worklist -= 1; 1757 } 1758 1759 static void 1760 wake_worklist(wk) 1761 struct worklist *wk; 1762 { 1763 if (wk->wk_state & IOWAITING) { 1764 wk->wk_state &= ~IOWAITING; 1765 wakeup(wk); 1766 } 1767 } 1768 1769 static void 1770 wait_worklist(wk, wmesg) 1771 struct worklist *wk; 1772 char *wmesg; 1773 { 1774 struct ufsmount *ump; 1775 1776 ump = VFSTOUFS(wk->wk_mp); 1777 wk->wk_state |= IOWAITING; 1778 msleep(wk, LOCK_PTR(ump), PVM, wmesg, 0); 1779 } 1780 1781 /* 1782 * Process that runs once per second to handle items in the background queue. 1783 * 1784 * Note that we ensure that everything is done in the order in which they 1785 * appear in the queue. The code below depends on this property to ensure 1786 * that blocks of a file are freed before the inode itself is freed. This 1787 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated 1788 * until all the old ones have been purged from the dependency lists. 1789 */ 1790 static int 1791 softdep_process_worklist(mp, full) 1792 struct mount *mp; 1793 int full; 1794 { 1795 int cnt, matchcnt; 1796 struct ufsmount *ump; 1797 long starttime; 1798 1799 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp")); 1800 ump = VFSTOUFS(mp); 1801 if (ump->um_softdep == NULL) 1802 return (0); 1803 matchcnt = 0; 1804 ACQUIRE_LOCK(ump); 1805 starttime = time_second; 1806 softdep_process_journal(mp, NULL, full ? MNT_WAIT : 0); 1807 check_clear_deps(mp); 1808 while (ump->softdep_on_worklist > 0) { 1809 if ((cnt = process_worklist_item(mp, 10, LK_NOWAIT)) == 0) 1810 break; 1811 else 1812 matchcnt += cnt; 1813 check_clear_deps(mp); 1814 /* 1815 * We do not generally want to stop for buffer space, but if 1816 * we are really being a buffer hog, we will stop and wait. 1817 */ 1818 if (should_yield()) { 1819 FREE_LOCK(ump); 1820 kern_yield(PRI_USER); 1821 bwillwrite(); 1822 ACQUIRE_LOCK(ump); 1823 } 1824 /* 1825 * Never allow processing to run for more than one 1826 * second. This gives the syncer thread the opportunity 1827 * to pause if appropriate. 1828 */ 1829 if (!full && starttime != time_second) 1830 break; 1831 } 1832 if (full == 0) 1833 journal_unsuspend(ump); 1834 FREE_LOCK(ump); 1835 return (matchcnt); 1836 } 1837 1838 /* 1839 * Process all removes associated with a vnode if we are running out of 1840 * journal space. Any other process which attempts to flush these will 1841 * be unable as we have the vnodes locked. 1842 */ 1843 static void 1844 process_removes(vp) 1845 struct vnode *vp; 1846 { 1847 struct inodedep *inodedep; 1848 struct dirrem *dirrem; 1849 struct ufsmount *ump; 1850 struct mount *mp; 1851 ino_t inum; 1852 1853 mp = vp->v_mount; 1854 ump = VFSTOUFS(mp); 1855 LOCK_OWNED(ump); 1856 inum = VTOI(vp)->i_number; 1857 for (;;) { 1858 top: 1859 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1860 return; 1861 LIST_FOREACH(dirrem, &inodedep->id_dirremhd, dm_inonext) { 1862 /* 1863 * If another thread is trying to lock this vnode 1864 * it will fail but we must wait for it to do so 1865 * before we can proceed. 1866 */ 1867 if (dirrem->dm_state & INPROGRESS) { 1868 wait_worklist(&dirrem->dm_list, "pwrwait"); 1869 goto top; 1870 } 1871 if ((dirrem->dm_state & (COMPLETE | ONWORKLIST)) == 1872 (COMPLETE | ONWORKLIST)) 1873 break; 1874 } 1875 if (dirrem == NULL) 1876 return; 1877 remove_from_worklist(&dirrem->dm_list); 1878 FREE_LOCK(ump); 1879 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1880 panic("process_removes: suspended filesystem"); 1881 handle_workitem_remove(dirrem, 0); 1882 vn_finished_secondary_write(mp); 1883 ACQUIRE_LOCK(ump); 1884 } 1885 } 1886 1887 /* 1888 * Process all truncations associated with a vnode if we are running out 1889 * of journal space. This is called when the vnode lock is already held 1890 * and no other process can clear the truncation. This function returns 1891 * a value greater than zero if it did any work. 1892 */ 1893 static void 1894 process_truncates(vp) 1895 struct vnode *vp; 1896 { 1897 struct inodedep *inodedep; 1898 struct freeblks *freeblks; 1899 struct ufsmount *ump; 1900 struct mount *mp; 1901 ino_t inum; 1902 int cgwait; 1903 1904 mp = vp->v_mount; 1905 ump = VFSTOUFS(mp); 1906 LOCK_OWNED(ump); 1907 inum = VTOI(vp)->i_number; 1908 for (;;) { 1909 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1910 return; 1911 cgwait = 0; 1912 TAILQ_FOREACH(freeblks, &inodedep->id_freeblklst, fb_next) { 1913 /* Journal entries not yet written. */ 1914 if (!LIST_EMPTY(&freeblks->fb_jblkdephd)) { 1915 jwait(&LIST_FIRST( 1916 &freeblks->fb_jblkdephd)->jb_list, 1917 MNT_WAIT); 1918 break; 1919 } 1920 /* Another thread is executing this item. */ 1921 if (freeblks->fb_state & INPROGRESS) { 1922 wait_worklist(&freeblks->fb_list, "ptrwait"); 1923 break; 1924 } 1925 /* Freeblks is waiting on a inode write. */ 1926 if ((freeblks->fb_state & COMPLETE) == 0) { 1927 FREE_LOCK(ump); 1928 ffs_update(vp, 1); 1929 ACQUIRE_LOCK(ump); 1930 break; 1931 } 1932 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST)) == 1933 (ALLCOMPLETE | ONWORKLIST)) { 1934 remove_from_worklist(&freeblks->fb_list); 1935 freeblks->fb_state |= INPROGRESS; 1936 FREE_LOCK(ump); 1937 if (vn_start_secondary_write(NULL, &mp, 1938 V_NOWAIT)) 1939 panic("process_truncates: " 1940 "suspended filesystem"); 1941 handle_workitem_freeblocks(freeblks, 0); 1942 vn_finished_secondary_write(mp); 1943 ACQUIRE_LOCK(ump); 1944 break; 1945 } 1946 if (freeblks->fb_cgwait) 1947 cgwait++; 1948 } 1949 if (cgwait) { 1950 FREE_LOCK(ump); 1951 sync_cgs(mp, MNT_WAIT); 1952 ffs_sync_snap(mp, MNT_WAIT); 1953 ACQUIRE_LOCK(ump); 1954 continue; 1955 } 1956 if (freeblks == NULL) 1957 break; 1958 } 1959 return; 1960 } 1961 1962 /* 1963 * Process one item on the worklist. 1964 */ 1965 static int 1966 process_worklist_item(mp, target, flags) 1967 struct mount *mp; 1968 int target; 1969 int flags; 1970 { 1971 struct worklist sentinel; 1972 struct worklist *wk; 1973 struct ufsmount *ump; 1974 int matchcnt; 1975 int error; 1976 1977 KASSERT(mp != NULL, ("process_worklist_item: NULL mp")); 1978 /* 1979 * If we are being called because of a process doing a 1980 * copy-on-write, then it is not safe to write as we may 1981 * recurse into the copy-on-write routine. 1982 */ 1983 if (curthread->td_pflags & TDP_COWINPROGRESS) 1984 return (-1); 1985 PHOLD(curproc); /* Don't let the stack go away. */ 1986 ump = VFSTOUFS(mp); 1987 LOCK_OWNED(ump); 1988 matchcnt = 0; 1989 sentinel.wk_mp = NULL; 1990 sentinel.wk_type = D_SENTINEL; 1991 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, &sentinel, wk_list); 1992 for (wk = LIST_NEXT(&sentinel, wk_list); wk != NULL; 1993 wk = LIST_NEXT(&sentinel, wk_list)) { 1994 if (wk->wk_type == D_SENTINEL) { 1995 LIST_REMOVE(&sentinel, wk_list); 1996 LIST_INSERT_AFTER(wk, &sentinel, wk_list); 1997 continue; 1998 } 1999 if (wk->wk_state & INPROGRESS) 2000 panic("process_worklist_item: %p already in progress.", 2001 wk); 2002 wk->wk_state |= INPROGRESS; 2003 remove_from_worklist(wk); 2004 FREE_LOCK(ump); 2005 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 2006 panic("process_worklist_item: suspended filesystem"); 2007 switch (wk->wk_type) { 2008 case D_DIRREM: 2009 /* removal of a directory entry */ 2010 error = handle_workitem_remove(WK_DIRREM(wk), flags); 2011 break; 2012 2013 case D_FREEBLKS: 2014 /* releasing blocks and/or fragments from a file */ 2015 error = handle_workitem_freeblocks(WK_FREEBLKS(wk), 2016 flags); 2017 break; 2018 2019 case D_FREEFRAG: 2020 /* releasing a fragment when replaced as a file grows */ 2021 handle_workitem_freefrag(WK_FREEFRAG(wk)); 2022 error = 0; 2023 break; 2024 2025 case D_FREEFILE: 2026 /* releasing an inode when its link count drops to 0 */ 2027 handle_workitem_freefile(WK_FREEFILE(wk)); 2028 error = 0; 2029 break; 2030 2031 default: 2032 panic("%s_process_worklist: Unknown type %s", 2033 "softdep", TYPENAME(wk->wk_type)); 2034 /* NOTREACHED */ 2035 } 2036 vn_finished_secondary_write(mp); 2037 ACQUIRE_LOCK(ump); 2038 if (error == 0) { 2039 if (++matchcnt == target) 2040 break; 2041 continue; 2042 } 2043 /* 2044 * We have to retry the worklist item later. Wake up any 2045 * waiters who may be able to complete it immediately and 2046 * add the item back to the head so we don't try to execute 2047 * it again. 2048 */ 2049 wk->wk_state &= ~INPROGRESS; 2050 wake_worklist(wk); 2051 add_to_worklist(wk, WK_HEAD); 2052 } 2053 /* Sentinal could've become the tail from remove_from_worklist. */ 2054 if (ump->softdep_worklist_tail == &sentinel) 2055 ump->softdep_worklist_tail = 2056 (struct worklist *)sentinel.wk_list.le_prev; 2057 LIST_REMOVE(&sentinel, wk_list); 2058 PRELE(curproc); 2059 return (matchcnt); 2060 } 2061 2062 /* 2063 * Move dependencies from one buffer to another. 2064 */ 2065 int 2066 softdep_move_dependencies(oldbp, newbp) 2067 struct buf *oldbp; 2068 struct buf *newbp; 2069 { 2070 struct worklist *wk, *wktail; 2071 struct ufsmount *ump; 2072 int dirty; 2073 2074 if ((wk = LIST_FIRST(&oldbp->b_dep)) == NULL) 2075 return (0); 2076 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 2077 ("softdep_move_dependencies called on non-softdep filesystem")); 2078 dirty = 0; 2079 wktail = NULL; 2080 ump = VFSTOUFS(wk->wk_mp); 2081 ACQUIRE_LOCK(ump); 2082 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) { 2083 LIST_REMOVE(wk, wk_list); 2084 if (wk->wk_type == D_BMSAFEMAP && 2085 bmsafemap_backgroundwrite(WK_BMSAFEMAP(wk), newbp)) 2086 dirty = 1; 2087 if (wktail == NULL) 2088 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list); 2089 else 2090 LIST_INSERT_AFTER(wktail, wk, wk_list); 2091 wktail = wk; 2092 } 2093 FREE_LOCK(ump); 2094 2095 return (dirty); 2096 } 2097 2098 /* 2099 * Purge the work list of all items associated with a particular mount point. 2100 */ 2101 int 2102 softdep_flushworklist(oldmnt, countp, td) 2103 struct mount *oldmnt; 2104 int *countp; 2105 struct thread *td; 2106 { 2107 struct vnode *devvp; 2108 struct ufsmount *ump; 2109 int count, error; 2110 2111 /* 2112 * Alternately flush the block device associated with the mount 2113 * point and process any dependencies that the flushing 2114 * creates. We continue until no more worklist dependencies 2115 * are found. 2116 */ 2117 *countp = 0; 2118 error = 0; 2119 ump = VFSTOUFS(oldmnt); 2120 devvp = ump->um_devvp; 2121 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) { 2122 *countp += count; 2123 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 2124 error = VOP_FSYNC(devvp, MNT_WAIT, td); 2125 VOP_UNLOCK(devvp); 2126 if (error != 0) 2127 break; 2128 } 2129 return (error); 2130 } 2131 2132 #define SU_WAITIDLE_RETRIES 20 2133 static int 2134 softdep_waitidle(struct mount *mp, int flags __unused) 2135 { 2136 struct ufsmount *ump; 2137 struct vnode *devvp; 2138 struct thread *td; 2139 int error, i; 2140 2141 ump = VFSTOUFS(mp); 2142 KASSERT(ump->um_softdep != NULL, 2143 ("softdep_waitidle called on non-softdep filesystem")); 2144 devvp = ump->um_devvp; 2145 td = curthread; 2146 error = 0; 2147 ACQUIRE_LOCK(ump); 2148 for (i = 0; i < SU_WAITIDLE_RETRIES && ump->softdep_deps != 0; i++) { 2149 ump->softdep_req = 1; 2150 KASSERT((flags & FORCECLOSE) == 0 || 2151 ump->softdep_on_worklist == 0, 2152 ("softdep_waitidle: work added after flush")); 2153 msleep(&ump->softdep_deps, LOCK_PTR(ump), PVM | PDROP, 2154 "softdeps", 10 * hz); 2155 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 2156 error = VOP_FSYNC(devvp, MNT_WAIT, td); 2157 VOP_UNLOCK(devvp); 2158 ACQUIRE_LOCK(ump); 2159 if (error != 0) 2160 break; 2161 } 2162 ump->softdep_req = 0; 2163 if (i == SU_WAITIDLE_RETRIES && error == 0 && ump->softdep_deps != 0) { 2164 error = EBUSY; 2165 printf("softdep_waitidle: Failed to flush worklist for %p\n", 2166 mp); 2167 } 2168 FREE_LOCK(ump); 2169 return (error); 2170 } 2171 2172 /* 2173 * Flush all vnodes and worklist items associated with a specified mount point. 2174 */ 2175 int 2176 softdep_flushfiles(oldmnt, flags, td) 2177 struct mount *oldmnt; 2178 int flags; 2179 struct thread *td; 2180 { 2181 struct ufsmount *ump; 2182 #ifdef QUOTA 2183 int i; 2184 #endif 2185 int error, early, depcount, loopcnt, retry_flush_count, retry; 2186 int morework; 2187 2188 ump = VFSTOUFS(oldmnt); 2189 KASSERT(ump->um_softdep != NULL, 2190 ("softdep_flushfiles called on non-softdep filesystem")); 2191 loopcnt = 10; 2192 retry_flush_count = 3; 2193 retry_flush: 2194 error = 0; 2195 2196 /* 2197 * Alternately flush the vnodes associated with the mount 2198 * point and process any dependencies that the flushing 2199 * creates. In theory, this loop can happen at most twice, 2200 * but we give it a few extra just to be sure. 2201 */ 2202 for (; loopcnt > 0; loopcnt--) { 2203 /* 2204 * Do another flush in case any vnodes were brought in 2205 * as part of the cleanup operations. 2206 */ 2207 early = retry_flush_count == 1 || (oldmnt->mnt_kern_flag & 2208 MNTK_UNMOUNT) == 0 ? 0 : EARLYFLUSH; 2209 if ((error = ffs_flushfiles(oldmnt, flags | early, td)) != 0) 2210 break; 2211 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 || 2212 depcount == 0) 2213 break; 2214 } 2215 /* 2216 * If we are unmounting then it is an error to fail. If we 2217 * are simply trying to downgrade to read-only, then filesystem 2218 * activity can keep us busy forever, so we just fail with EBUSY. 2219 */ 2220 if (loopcnt == 0) { 2221 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) 2222 panic("softdep_flushfiles: looping"); 2223 error = EBUSY; 2224 } 2225 if (!error) 2226 error = softdep_waitidle(oldmnt, flags); 2227 if (!error) { 2228 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) { 2229 retry = 0; 2230 MNT_ILOCK(oldmnt); 2231 morework = oldmnt->mnt_nvnodelistsize > 0; 2232 #ifdef QUOTA 2233 UFS_LOCK(ump); 2234 for (i = 0; i < MAXQUOTAS; i++) { 2235 if (ump->um_quotas[i] != NULLVP) 2236 morework = 1; 2237 } 2238 UFS_UNLOCK(ump); 2239 #endif 2240 if (morework) { 2241 if (--retry_flush_count > 0) { 2242 retry = 1; 2243 loopcnt = 3; 2244 } else 2245 error = EBUSY; 2246 } 2247 MNT_IUNLOCK(oldmnt); 2248 if (retry) 2249 goto retry_flush; 2250 } 2251 } 2252 return (error); 2253 } 2254 2255 /* 2256 * Structure hashing. 2257 * 2258 * There are four types of structures that can be looked up: 2259 * 1) pagedep structures identified by mount point, inode number, 2260 * and logical block. 2261 * 2) inodedep structures identified by mount point and inode number. 2262 * 3) newblk structures identified by mount point and 2263 * physical block number. 2264 * 4) bmsafemap structures identified by mount point and 2265 * cylinder group number. 2266 * 2267 * The "pagedep" and "inodedep" dependency structures are hashed 2268 * separately from the file blocks and inodes to which they correspond. 2269 * This separation helps when the in-memory copy of an inode or 2270 * file block must be replaced. It also obviates the need to access 2271 * an inode or file page when simply updating (or de-allocating) 2272 * dependency structures. Lookup of newblk structures is needed to 2273 * find newly allocated blocks when trying to associate them with 2274 * their allocdirect or allocindir structure. 2275 * 2276 * The lookup routines optionally create and hash a new instance when 2277 * an existing entry is not found. The bmsafemap lookup routine always 2278 * allocates a new structure if an existing one is not found. 2279 */ 2280 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */ 2281 2282 /* 2283 * Structures and routines associated with pagedep caching. 2284 */ 2285 #define PAGEDEP_HASH(ump, inum, lbn) \ 2286 (&(ump)->pagedep_hashtbl[((inum) + (lbn)) & (ump)->pagedep_hash_size]) 2287 2288 static int 2289 pagedep_find(pagedephd, ino, lbn, pagedeppp) 2290 struct pagedep_hashhead *pagedephd; 2291 ino_t ino; 2292 ufs_lbn_t lbn; 2293 struct pagedep **pagedeppp; 2294 { 2295 struct pagedep *pagedep; 2296 2297 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 2298 if (ino == pagedep->pd_ino && lbn == pagedep->pd_lbn) { 2299 *pagedeppp = pagedep; 2300 return (1); 2301 } 2302 } 2303 *pagedeppp = NULL; 2304 return (0); 2305 } 2306 /* 2307 * Look up a pagedep. Return 1 if found, 0 otherwise. 2308 * If not found, allocate if DEPALLOC flag is passed. 2309 * Found or allocated entry is returned in pagedeppp. 2310 */ 2311 static int 2312 pagedep_lookup(mp, bp, ino, lbn, flags, pagedeppp) 2313 struct mount *mp; 2314 struct buf *bp; 2315 ino_t ino; 2316 ufs_lbn_t lbn; 2317 int flags; 2318 struct pagedep **pagedeppp; 2319 { 2320 struct pagedep *pagedep; 2321 struct pagedep_hashhead *pagedephd; 2322 struct worklist *wk; 2323 struct ufsmount *ump; 2324 int ret; 2325 int i; 2326 2327 ump = VFSTOUFS(mp); 2328 LOCK_OWNED(ump); 2329 if (bp) { 2330 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 2331 if (wk->wk_type == D_PAGEDEP) { 2332 *pagedeppp = WK_PAGEDEP(wk); 2333 return (1); 2334 } 2335 } 2336 } 2337 pagedephd = PAGEDEP_HASH(ump, ino, lbn); 2338 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp); 2339 if (ret) { 2340 if (((*pagedeppp)->pd_state & ONWORKLIST) == 0 && bp) 2341 WORKLIST_INSERT(&bp->b_dep, &(*pagedeppp)->pd_list); 2342 return (1); 2343 } 2344 if ((flags & DEPALLOC) == 0) 2345 return (0); 2346 FREE_LOCK(ump); 2347 pagedep = malloc(sizeof(struct pagedep), 2348 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO); 2349 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp); 2350 ACQUIRE_LOCK(ump); 2351 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp); 2352 if (*pagedeppp) { 2353 /* 2354 * This should never happen since we only create pagedeps 2355 * with the vnode lock held. Could be an assert. 2356 */ 2357 WORKITEM_FREE(pagedep, D_PAGEDEP); 2358 return (ret); 2359 } 2360 pagedep->pd_ino = ino; 2361 pagedep->pd_lbn = lbn; 2362 LIST_INIT(&pagedep->pd_dirremhd); 2363 LIST_INIT(&pagedep->pd_pendinghd); 2364 for (i = 0; i < DAHASHSZ; i++) 2365 LIST_INIT(&pagedep->pd_diraddhd[i]); 2366 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash); 2367 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list); 2368 *pagedeppp = pagedep; 2369 return (0); 2370 } 2371 2372 /* 2373 * Structures and routines associated with inodedep caching. 2374 */ 2375 #define INODEDEP_HASH(ump, inum) \ 2376 (&(ump)->inodedep_hashtbl[(inum) & (ump)->inodedep_hash_size]) 2377 2378 static int 2379 inodedep_find(inodedephd, inum, inodedeppp) 2380 struct inodedep_hashhead *inodedephd; 2381 ino_t inum; 2382 struct inodedep **inodedeppp; 2383 { 2384 struct inodedep *inodedep; 2385 2386 LIST_FOREACH(inodedep, inodedephd, id_hash) 2387 if (inum == inodedep->id_ino) 2388 break; 2389 if (inodedep) { 2390 *inodedeppp = inodedep; 2391 return (1); 2392 } 2393 *inodedeppp = NULL; 2394 2395 return (0); 2396 } 2397 /* 2398 * Look up an inodedep. Return 1 if found, 0 if not found. 2399 * If not found, allocate if DEPALLOC flag is passed. 2400 * Found or allocated entry is returned in inodedeppp. 2401 */ 2402 static int 2403 inodedep_lookup(mp, inum, flags, inodedeppp) 2404 struct mount *mp; 2405 ino_t inum; 2406 int flags; 2407 struct inodedep **inodedeppp; 2408 { 2409 struct inodedep *inodedep; 2410 struct inodedep_hashhead *inodedephd; 2411 struct ufsmount *ump; 2412 struct fs *fs; 2413 2414 ump = VFSTOUFS(mp); 2415 LOCK_OWNED(ump); 2416 fs = ump->um_fs; 2417 inodedephd = INODEDEP_HASH(ump, inum); 2418 2419 if (inodedep_find(inodedephd, inum, inodedeppp)) 2420 return (1); 2421 if ((flags & DEPALLOC) == 0) 2422 return (0); 2423 /* 2424 * If the system is over its limit and our filesystem is 2425 * responsible for more than our share of that usage and 2426 * we are not in a rush, request some inodedep cleanup. 2427 */ 2428 if (softdep_excess_items(ump, D_INODEDEP)) 2429 schedule_cleanup(mp); 2430 else 2431 FREE_LOCK(ump); 2432 inodedep = malloc(sizeof(struct inodedep), 2433 M_INODEDEP, M_SOFTDEP_FLAGS); 2434 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp); 2435 ACQUIRE_LOCK(ump); 2436 if (inodedep_find(inodedephd, inum, inodedeppp)) { 2437 WORKITEM_FREE(inodedep, D_INODEDEP); 2438 return (1); 2439 } 2440 inodedep->id_fs = fs; 2441 inodedep->id_ino = inum; 2442 inodedep->id_state = ALLCOMPLETE; 2443 inodedep->id_nlinkdelta = 0; 2444 inodedep->id_nlinkwrote = -1; 2445 inodedep->id_savedino1 = NULL; 2446 inodedep->id_savedsize = -1; 2447 inodedep->id_savedextsize = -1; 2448 inodedep->id_savednlink = -1; 2449 inodedep->id_bmsafemap = NULL; 2450 inodedep->id_mkdiradd = NULL; 2451 LIST_INIT(&inodedep->id_dirremhd); 2452 LIST_INIT(&inodedep->id_pendinghd); 2453 LIST_INIT(&inodedep->id_inowait); 2454 LIST_INIT(&inodedep->id_bufwait); 2455 TAILQ_INIT(&inodedep->id_inoreflst); 2456 TAILQ_INIT(&inodedep->id_inoupdt); 2457 TAILQ_INIT(&inodedep->id_newinoupdt); 2458 TAILQ_INIT(&inodedep->id_extupdt); 2459 TAILQ_INIT(&inodedep->id_newextupdt); 2460 TAILQ_INIT(&inodedep->id_freeblklst); 2461 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash); 2462 *inodedeppp = inodedep; 2463 return (0); 2464 } 2465 2466 /* 2467 * Structures and routines associated with newblk caching. 2468 */ 2469 #define NEWBLK_HASH(ump, inum) \ 2470 (&(ump)->newblk_hashtbl[(inum) & (ump)->newblk_hash_size]) 2471 2472 static int 2473 newblk_find(newblkhd, newblkno, flags, newblkpp) 2474 struct newblk_hashhead *newblkhd; 2475 ufs2_daddr_t newblkno; 2476 int flags; 2477 struct newblk **newblkpp; 2478 { 2479 struct newblk *newblk; 2480 2481 LIST_FOREACH(newblk, newblkhd, nb_hash) { 2482 if (newblkno != newblk->nb_newblkno) 2483 continue; 2484 /* 2485 * If we're creating a new dependency don't match those that 2486 * have already been converted to allocdirects. This is for 2487 * a frag extend. 2488 */ 2489 if ((flags & DEPALLOC) && newblk->nb_list.wk_type != D_NEWBLK) 2490 continue; 2491 break; 2492 } 2493 if (newblk) { 2494 *newblkpp = newblk; 2495 return (1); 2496 } 2497 *newblkpp = NULL; 2498 return (0); 2499 } 2500 2501 /* 2502 * Look up a newblk. Return 1 if found, 0 if not found. 2503 * If not found, allocate if DEPALLOC flag is passed. 2504 * Found or allocated entry is returned in newblkpp. 2505 */ 2506 static int 2507 newblk_lookup(mp, newblkno, flags, newblkpp) 2508 struct mount *mp; 2509 ufs2_daddr_t newblkno; 2510 int flags; 2511 struct newblk **newblkpp; 2512 { 2513 struct newblk *newblk; 2514 struct newblk_hashhead *newblkhd; 2515 struct ufsmount *ump; 2516 2517 ump = VFSTOUFS(mp); 2518 LOCK_OWNED(ump); 2519 newblkhd = NEWBLK_HASH(ump, newblkno); 2520 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) 2521 return (1); 2522 if ((flags & DEPALLOC) == 0) 2523 return (0); 2524 if (softdep_excess_items(ump, D_NEWBLK) || 2525 softdep_excess_items(ump, D_ALLOCDIRECT) || 2526 softdep_excess_items(ump, D_ALLOCINDIR)) 2527 schedule_cleanup(mp); 2528 else 2529 FREE_LOCK(ump); 2530 newblk = malloc(sizeof(union allblk), M_NEWBLK, 2531 M_SOFTDEP_FLAGS | M_ZERO); 2532 workitem_alloc(&newblk->nb_list, D_NEWBLK, mp); 2533 ACQUIRE_LOCK(ump); 2534 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) { 2535 WORKITEM_FREE(newblk, D_NEWBLK); 2536 return (1); 2537 } 2538 newblk->nb_freefrag = NULL; 2539 LIST_INIT(&newblk->nb_indirdeps); 2540 LIST_INIT(&newblk->nb_newdirblk); 2541 LIST_INIT(&newblk->nb_jwork); 2542 newblk->nb_state = ATTACHED; 2543 newblk->nb_newblkno = newblkno; 2544 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash); 2545 *newblkpp = newblk; 2546 return (0); 2547 } 2548 2549 /* 2550 * Structures and routines associated with freed indirect block caching. 2551 */ 2552 #define INDIR_HASH(ump, blkno) \ 2553 (&(ump)->indir_hashtbl[(blkno) & (ump)->indir_hash_size]) 2554 2555 /* 2556 * Lookup an indirect block in the indir hash table. The freework is 2557 * removed and potentially freed. The caller must do a blocking journal 2558 * write before writing to the blkno. 2559 */ 2560 static int 2561 indirblk_lookup(mp, blkno) 2562 struct mount *mp; 2563 ufs2_daddr_t blkno; 2564 { 2565 struct freework *freework; 2566 struct indir_hashhead *wkhd; 2567 struct ufsmount *ump; 2568 2569 ump = VFSTOUFS(mp); 2570 wkhd = INDIR_HASH(ump, blkno); 2571 TAILQ_FOREACH(freework, wkhd, fw_next) { 2572 if (freework->fw_blkno != blkno) 2573 continue; 2574 indirblk_remove(freework); 2575 return (1); 2576 } 2577 return (0); 2578 } 2579 2580 /* 2581 * Insert an indirect block represented by freework into the indirblk 2582 * hash table so that it may prevent the block from being re-used prior 2583 * to the journal being written. 2584 */ 2585 static void 2586 indirblk_insert(freework) 2587 struct freework *freework; 2588 { 2589 struct jblocks *jblocks; 2590 struct jseg *jseg; 2591 struct ufsmount *ump; 2592 2593 ump = VFSTOUFS(freework->fw_list.wk_mp); 2594 jblocks = ump->softdep_jblocks; 2595 jseg = TAILQ_LAST(&jblocks->jb_segs, jseglst); 2596 if (jseg == NULL) 2597 return; 2598 2599 LIST_INSERT_HEAD(&jseg->js_indirs, freework, fw_segs); 2600 TAILQ_INSERT_HEAD(INDIR_HASH(ump, freework->fw_blkno), freework, 2601 fw_next); 2602 freework->fw_state &= ~DEPCOMPLETE; 2603 } 2604 2605 static void 2606 indirblk_remove(freework) 2607 struct freework *freework; 2608 { 2609 struct ufsmount *ump; 2610 2611 ump = VFSTOUFS(freework->fw_list.wk_mp); 2612 LIST_REMOVE(freework, fw_segs); 2613 TAILQ_REMOVE(INDIR_HASH(ump, freework->fw_blkno), freework, fw_next); 2614 freework->fw_state |= DEPCOMPLETE; 2615 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 2616 WORKITEM_FREE(freework, D_FREEWORK); 2617 } 2618 2619 /* 2620 * Executed during filesystem system initialization before 2621 * mounting any filesystems. 2622 */ 2623 void 2624 softdep_initialize() 2625 { 2626 2627 TAILQ_INIT(&softdepmounts); 2628 #ifdef __LP64__ 2629 max_softdeps = desiredvnodes * 4; 2630 #else 2631 max_softdeps = desiredvnodes * 2; 2632 #endif 2633 2634 /* initialise bioops hack */ 2635 bioops.io_start = softdep_disk_io_initiation; 2636 bioops.io_complete = softdep_disk_write_complete; 2637 bioops.io_deallocate = softdep_deallocate_dependencies; 2638 bioops.io_countdeps = softdep_count_dependencies; 2639 softdep_ast_cleanup = softdep_ast_cleanup_proc; 2640 2641 /* Initialize the callout with an mtx. */ 2642 callout_init_mtx(&softdep_callout, &lk, 0); 2643 } 2644 2645 /* 2646 * Executed after all filesystems have been unmounted during 2647 * filesystem module unload. 2648 */ 2649 void 2650 softdep_uninitialize() 2651 { 2652 2653 /* clear bioops hack */ 2654 bioops.io_start = NULL; 2655 bioops.io_complete = NULL; 2656 bioops.io_deallocate = NULL; 2657 bioops.io_countdeps = NULL; 2658 softdep_ast_cleanup = NULL; 2659 2660 callout_drain(&softdep_callout); 2661 } 2662 2663 /* 2664 * Called at mount time to notify the dependency code that a 2665 * filesystem wishes to use it. 2666 */ 2667 int 2668 softdep_mount(devvp, mp, fs, cred) 2669 struct vnode *devvp; 2670 struct mount *mp; 2671 struct fs *fs; 2672 struct ucred *cred; 2673 { 2674 struct csum_total cstotal; 2675 struct mount_softdeps *sdp; 2676 struct ufsmount *ump; 2677 struct cg *cgp; 2678 struct buf *bp; 2679 u_int cyl, i; 2680 int error; 2681 2682 ump = VFSTOUFS(mp); 2683 2684 sdp = malloc(sizeof(struct mount_softdeps), M_MOUNTDATA, 2685 M_WAITOK | M_ZERO); 2686 rw_init(&sdp->sd_fslock, "SUrw"); 2687 sdp->sd_ump = ump; 2688 LIST_INIT(&sdp->sd_workitem_pending); 2689 LIST_INIT(&sdp->sd_journal_pending); 2690 TAILQ_INIT(&sdp->sd_unlinked); 2691 LIST_INIT(&sdp->sd_dirtycg); 2692 sdp->sd_worklist_tail = NULL; 2693 sdp->sd_on_worklist = 0; 2694 sdp->sd_deps = 0; 2695 LIST_INIT(&sdp->sd_mkdirlisthd); 2696 sdp->sd_pdhash = hashinit(desiredvnodes / 5, M_PAGEDEP, 2697 &sdp->sd_pdhashsize); 2698 sdp->sd_pdnextclean = 0; 2699 sdp->sd_idhash = hashinit(desiredvnodes, M_INODEDEP, 2700 &sdp->sd_idhashsize); 2701 sdp->sd_idnextclean = 0; 2702 sdp->sd_newblkhash = hashinit(max_softdeps / 2, M_NEWBLK, 2703 &sdp->sd_newblkhashsize); 2704 sdp->sd_bmhash = hashinit(1024, M_BMSAFEMAP, &sdp->sd_bmhashsize); 2705 i = 1 << (ffs(desiredvnodes / 10) - 1); 2706 sdp->sd_indirhash = malloc(i * sizeof(struct indir_hashhead), 2707 M_FREEWORK, M_WAITOK); 2708 sdp->sd_indirhashsize = i - 1; 2709 for (i = 0; i <= sdp->sd_indirhashsize; i++) 2710 TAILQ_INIT(&sdp->sd_indirhash[i]); 2711 #ifdef INVARIANTS 2712 for (i = 0; i <= D_LAST; i++) 2713 LIST_INIT(&sdp->sd_alldeps[i]); 2714 #endif 2715 ACQUIRE_GBLLOCK(&lk); 2716 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next); 2717 FREE_GBLLOCK(&lk); 2718 2719 ump->um_softdep = sdp; 2720 MNT_ILOCK(mp); 2721 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP; 2722 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) { 2723 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) | 2724 MNTK_SOFTDEP | MNTK_NOASYNC; 2725 } 2726 MNT_IUNLOCK(mp); 2727 2728 if ((fs->fs_flags & FS_SUJ) && 2729 (error = journal_mount(mp, fs, cred)) != 0) { 2730 printf("Failed to start journal: %d\n", error); 2731 softdep_unmount(mp); 2732 return (error); 2733 } 2734 /* 2735 * Start our flushing thread in the bufdaemon process. 2736 */ 2737 ACQUIRE_LOCK(ump); 2738 ump->softdep_flags |= FLUSH_STARTING; 2739 FREE_LOCK(ump); 2740 kproc_kthread_add(&softdep_flush, mp, &bufdaemonproc, 2741 &ump->softdep_flushtd, 0, 0, "softdepflush", "%s worker", 2742 mp->mnt_stat.f_mntonname); 2743 ACQUIRE_LOCK(ump); 2744 while ((ump->softdep_flags & FLUSH_STARTING) != 0) { 2745 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, "sdstart", 2746 hz / 2); 2747 } 2748 FREE_LOCK(ump); 2749 /* 2750 * When doing soft updates, the counters in the 2751 * superblock may have gotten out of sync. Recomputation 2752 * can take a long time and can be deferred for background 2753 * fsck. However, the old behavior of scanning the cylinder 2754 * groups and recalculating them at mount time is available 2755 * by setting vfs.ffs.compute_summary_at_mount to one. 2756 */ 2757 if (compute_summary_at_mount == 0 || fs->fs_clean != 0) 2758 return (0); 2759 bzero(&cstotal, sizeof cstotal); 2760 for (cyl = 0; cyl < fs->fs_ncg; cyl++) { 2761 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)), 2762 fs->fs_cgsize, cred, &bp)) != 0) { 2763 brelse(bp); 2764 softdep_unmount(mp); 2765 return (error); 2766 } 2767 cgp = (struct cg *)bp->b_data; 2768 cstotal.cs_nffree += cgp->cg_cs.cs_nffree; 2769 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree; 2770 cstotal.cs_nifree += cgp->cg_cs.cs_nifree; 2771 cstotal.cs_ndir += cgp->cg_cs.cs_ndir; 2772 fs->fs_cs(fs, cyl) = cgp->cg_cs; 2773 brelse(bp); 2774 } 2775 #ifdef INVARIANTS 2776 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal)) 2777 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt); 2778 #endif 2779 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal); 2780 return (0); 2781 } 2782 2783 void 2784 softdep_unmount(mp) 2785 struct mount *mp; 2786 { 2787 struct ufsmount *ump; 2788 struct mount_softdeps *ums; 2789 2790 ump = VFSTOUFS(mp); 2791 KASSERT(ump->um_softdep != NULL, 2792 ("softdep_unmount called on non-softdep filesystem")); 2793 MNT_ILOCK(mp); 2794 mp->mnt_flag &= ~MNT_SOFTDEP; 2795 if ((mp->mnt_flag & MNT_SUJ) == 0) { 2796 MNT_IUNLOCK(mp); 2797 } else { 2798 mp->mnt_flag &= ~MNT_SUJ; 2799 MNT_IUNLOCK(mp); 2800 journal_unmount(ump); 2801 } 2802 /* 2803 * Shut down our flushing thread. Check for NULL is if 2804 * softdep_mount errors out before the thread has been created. 2805 */ 2806 if (ump->softdep_flushtd != NULL) { 2807 ACQUIRE_LOCK(ump); 2808 ump->softdep_flags |= FLUSH_EXIT; 2809 wakeup(&ump->softdep_flushtd); 2810 while ((ump->softdep_flags & FLUSH_EXIT) != 0) { 2811 msleep(&ump->softdep_flags, LOCK_PTR(ump), PVM, 2812 "sdwait", 0); 2813 } 2814 KASSERT((ump->softdep_flags & FLUSH_EXIT) == 0, 2815 ("Thread shutdown failed")); 2816 FREE_LOCK(ump); 2817 } 2818 2819 /* 2820 * We are no longer have softdep structure attached to ump. 2821 */ 2822 ums = ump->um_softdep; 2823 ACQUIRE_GBLLOCK(&lk); 2824 TAILQ_REMOVE(&softdepmounts, ums, sd_next); 2825 FREE_GBLLOCK(&lk); 2826 ump->um_softdep = NULL; 2827 2828 KASSERT(ums->sd_on_journal == 0, 2829 ("ump %p ums %p on_journal %d", ump, ums, ums->sd_on_journal)); 2830 KASSERT(ums->sd_on_worklist == 0, 2831 ("ump %p ums %p on_worklist %d", ump, ums, ums->sd_on_worklist)); 2832 KASSERT(ums->sd_deps == 0, 2833 ("ump %p ums %p deps %d", ump, ums, ums->sd_deps)); 2834 2835 /* 2836 * Free up our resources. 2837 */ 2838 rw_destroy(&ums->sd_fslock); 2839 hashdestroy(ums->sd_pdhash, M_PAGEDEP, ums->sd_pdhashsize); 2840 hashdestroy(ums->sd_idhash, M_INODEDEP, ums->sd_idhashsize); 2841 hashdestroy(ums->sd_newblkhash, M_NEWBLK, ums->sd_newblkhashsize); 2842 hashdestroy(ums->sd_bmhash, M_BMSAFEMAP, ums->sd_bmhashsize); 2843 free(ums->sd_indirhash, M_FREEWORK); 2844 #ifdef INVARIANTS 2845 for (int i = 0; i <= D_LAST; i++) { 2846 KASSERT(ums->sd_curdeps[i] == 0, 2847 ("Unmount %s: Dep type %s != 0 (%ld)", ump->um_fs->fs_fsmnt, 2848 TYPENAME(i), ums->sd_curdeps[i])); 2849 KASSERT(LIST_EMPTY(&ums->sd_alldeps[i]), 2850 ("Unmount %s: Dep type %s not empty (%p)", 2851 ump->um_fs->fs_fsmnt, 2852 TYPENAME(i), LIST_FIRST(&ums->sd_alldeps[i]))); 2853 } 2854 #endif 2855 free(ums, M_MOUNTDATA); 2856 } 2857 2858 static struct jblocks * 2859 jblocks_create(void) 2860 { 2861 struct jblocks *jblocks; 2862 2863 jblocks = malloc(sizeof(*jblocks), M_JBLOCKS, M_WAITOK | M_ZERO); 2864 TAILQ_INIT(&jblocks->jb_segs); 2865 jblocks->jb_avail = 10; 2866 jblocks->jb_extent = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2867 M_JBLOCKS, M_WAITOK | M_ZERO); 2868 2869 return (jblocks); 2870 } 2871 2872 static ufs2_daddr_t 2873 jblocks_alloc(jblocks, bytes, actual) 2874 struct jblocks *jblocks; 2875 int bytes; 2876 int *actual; 2877 { 2878 ufs2_daddr_t daddr; 2879 struct jextent *jext; 2880 int freecnt; 2881 int blocks; 2882 2883 blocks = bytes / DEV_BSIZE; 2884 jext = &jblocks->jb_extent[jblocks->jb_head]; 2885 freecnt = jext->je_blocks - jblocks->jb_off; 2886 if (freecnt == 0) { 2887 jblocks->jb_off = 0; 2888 if (++jblocks->jb_head > jblocks->jb_used) 2889 jblocks->jb_head = 0; 2890 jext = &jblocks->jb_extent[jblocks->jb_head]; 2891 freecnt = jext->je_blocks; 2892 } 2893 if (freecnt > blocks) 2894 freecnt = blocks; 2895 *actual = freecnt * DEV_BSIZE; 2896 daddr = jext->je_daddr + jblocks->jb_off; 2897 jblocks->jb_off += freecnt; 2898 jblocks->jb_free -= freecnt; 2899 2900 return (daddr); 2901 } 2902 2903 static void 2904 jblocks_free(jblocks, mp, bytes) 2905 struct jblocks *jblocks; 2906 struct mount *mp; 2907 int bytes; 2908 { 2909 2910 LOCK_OWNED(VFSTOUFS(mp)); 2911 jblocks->jb_free += bytes / DEV_BSIZE; 2912 if (jblocks->jb_suspended) 2913 worklist_speedup(mp); 2914 wakeup(jblocks); 2915 } 2916 2917 static void 2918 jblocks_destroy(jblocks) 2919 struct jblocks *jblocks; 2920 { 2921 2922 if (jblocks->jb_extent) 2923 free(jblocks->jb_extent, M_JBLOCKS); 2924 free(jblocks, M_JBLOCKS); 2925 } 2926 2927 static void 2928 jblocks_add(jblocks, daddr, blocks) 2929 struct jblocks *jblocks; 2930 ufs2_daddr_t daddr; 2931 int blocks; 2932 { 2933 struct jextent *jext; 2934 2935 jblocks->jb_blocks += blocks; 2936 jblocks->jb_free += blocks; 2937 jext = &jblocks->jb_extent[jblocks->jb_used]; 2938 /* Adding the first block. */ 2939 if (jext->je_daddr == 0) { 2940 jext->je_daddr = daddr; 2941 jext->je_blocks = blocks; 2942 return; 2943 } 2944 /* Extending the last extent. */ 2945 if (jext->je_daddr + jext->je_blocks == daddr) { 2946 jext->je_blocks += blocks; 2947 return; 2948 } 2949 /* Adding a new extent. */ 2950 if (++jblocks->jb_used == jblocks->jb_avail) { 2951 jblocks->jb_avail *= 2; 2952 jext = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2953 M_JBLOCKS, M_WAITOK | M_ZERO); 2954 memcpy(jext, jblocks->jb_extent, 2955 sizeof(struct jextent) * jblocks->jb_used); 2956 free(jblocks->jb_extent, M_JBLOCKS); 2957 jblocks->jb_extent = jext; 2958 } 2959 jext = &jblocks->jb_extent[jblocks->jb_used]; 2960 jext->je_daddr = daddr; 2961 jext->je_blocks = blocks; 2962 return; 2963 } 2964 2965 int 2966 softdep_journal_lookup(mp, vpp) 2967 struct mount *mp; 2968 struct vnode **vpp; 2969 { 2970 struct componentname cnp; 2971 struct vnode *dvp; 2972 ino_t sujournal; 2973 int error; 2974 2975 error = VFS_VGET(mp, UFS_ROOTINO, LK_EXCLUSIVE, &dvp); 2976 if (error) 2977 return (error); 2978 bzero(&cnp, sizeof(cnp)); 2979 cnp.cn_nameiop = LOOKUP; 2980 cnp.cn_flags = ISLASTCN; 2981 cnp.cn_thread = curthread; 2982 cnp.cn_cred = curthread->td_ucred; 2983 cnp.cn_pnbuf = SUJ_FILE; 2984 cnp.cn_nameptr = SUJ_FILE; 2985 cnp.cn_namelen = strlen(SUJ_FILE); 2986 error = ufs_lookup_ino(dvp, NULL, &cnp, &sujournal); 2987 vput(dvp); 2988 if (error != 0) 2989 return (error); 2990 error = VFS_VGET(mp, sujournal, LK_EXCLUSIVE, vpp); 2991 return (error); 2992 } 2993 2994 /* 2995 * Open and verify the journal file. 2996 */ 2997 static int 2998 journal_mount(mp, fs, cred) 2999 struct mount *mp; 3000 struct fs *fs; 3001 struct ucred *cred; 3002 { 3003 struct jblocks *jblocks; 3004 struct ufsmount *ump; 3005 struct vnode *vp; 3006 struct inode *ip; 3007 ufs2_daddr_t blkno; 3008 int bcount; 3009 int error; 3010 int i; 3011 3012 ump = VFSTOUFS(mp); 3013 ump->softdep_journal_tail = NULL; 3014 ump->softdep_on_journal = 0; 3015 ump->softdep_accdeps = 0; 3016 ump->softdep_req = 0; 3017 ump->softdep_jblocks = NULL; 3018 error = softdep_journal_lookup(mp, &vp); 3019 if (error != 0) { 3020 printf("Failed to find journal. Use tunefs to create one\n"); 3021 return (error); 3022 } 3023 ip = VTOI(vp); 3024 if (ip->i_size < SUJ_MIN) { 3025 error = ENOSPC; 3026 goto out; 3027 } 3028 bcount = lblkno(fs, ip->i_size); /* Only use whole blocks. */ 3029 jblocks = jblocks_create(); 3030 for (i = 0; i < bcount; i++) { 3031 error = ufs_bmaparray(vp, i, &blkno, NULL, NULL, NULL); 3032 if (error) 3033 break; 3034 jblocks_add(jblocks, blkno, fsbtodb(fs, fs->fs_frag)); 3035 } 3036 if (error) { 3037 jblocks_destroy(jblocks); 3038 goto out; 3039 } 3040 jblocks->jb_low = jblocks->jb_free / 3; /* Reserve 33%. */ 3041 jblocks->jb_min = jblocks->jb_free / 10; /* Suspend at 10%. */ 3042 ump->softdep_jblocks = jblocks; 3043 3044 MNT_ILOCK(mp); 3045 mp->mnt_flag |= MNT_SUJ; 3046 MNT_IUNLOCK(mp); 3047 3048 /* 3049 * Only validate the journal contents if the 3050 * filesystem is clean, otherwise we write the logs 3051 * but they'll never be used. If the filesystem was 3052 * still dirty when we mounted it the journal is 3053 * invalid and a new journal can only be valid if it 3054 * starts from a clean mount. 3055 */ 3056 if (fs->fs_clean) { 3057 DIP_SET(ip, i_modrev, fs->fs_mtime); 3058 ip->i_flags |= IN_MODIFIED; 3059 ffs_update(vp, 1); 3060 } 3061 out: 3062 vput(vp); 3063 return (error); 3064 } 3065 3066 static void 3067 journal_unmount(ump) 3068 struct ufsmount *ump; 3069 { 3070 3071 if (ump->softdep_jblocks) 3072 jblocks_destroy(ump->softdep_jblocks); 3073 ump->softdep_jblocks = NULL; 3074 } 3075 3076 /* 3077 * Called when a journal record is ready to be written. Space is allocated 3078 * and the journal entry is created when the journal is flushed to stable 3079 * store. 3080 */ 3081 static void 3082 add_to_journal(wk) 3083 struct worklist *wk; 3084 { 3085 struct ufsmount *ump; 3086 3087 ump = VFSTOUFS(wk->wk_mp); 3088 LOCK_OWNED(ump); 3089 if (wk->wk_state & ONWORKLIST) 3090 panic("add_to_journal: %s(0x%X) already on list", 3091 TYPENAME(wk->wk_type), wk->wk_state); 3092 wk->wk_state |= ONWORKLIST | DEPCOMPLETE; 3093 if (LIST_EMPTY(&ump->softdep_journal_pending)) { 3094 ump->softdep_jblocks->jb_age = ticks; 3095 LIST_INSERT_HEAD(&ump->softdep_journal_pending, wk, wk_list); 3096 } else 3097 LIST_INSERT_AFTER(ump->softdep_journal_tail, wk, wk_list); 3098 ump->softdep_journal_tail = wk; 3099 ump->softdep_on_journal += 1; 3100 } 3101 3102 /* 3103 * Remove an arbitrary item for the journal worklist maintain the tail 3104 * pointer. This happens when a new operation obviates the need to 3105 * journal an old operation. 3106 */ 3107 static void 3108 remove_from_journal(wk) 3109 struct worklist *wk; 3110 { 3111 struct ufsmount *ump; 3112 3113 ump = VFSTOUFS(wk->wk_mp); 3114 LOCK_OWNED(ump); 3115 #ifdef INVARIANTS 3116 { 3117 struct worklist *wkn; 3118 3119 LIST_FOREACH(wkn, &ump->softdep_journal_pending, wk_list) 3120 if (wkn == wk) 3121 break; 3122 if (wkn == NULL) 3123 panic("remove_from_journal: %p is not in journal", wk); 3124 } 3125 #endif 3126 /* 3127 * We emulate a TAILQ to save space in most structures which do not 3128 * require TAILQ semantics. Here we must update the tail position 3129 * when removing the tail which is not the final entry. This works 3130 * only if the worklist linkage are at the beginning of the structure. 3131 */ 3132 if (ump->softdep_journal_tail == wk) 3133 ump->softdep_journal_tail = 3134 (struct worklist *)wk->wk_list.le_prev; 3135 WORKLIST_REMOVE(wk); 3136 ump->softdep_on_journal -= 1; 3137 } 3138 3139 /* 3140 * Check for journal space as well as dependency limits so the prelink 3141 * code can throttle both journaled and non-journaled filesystems. 3142 * Threshold is 0 for low and 1 for min. 3143 */ 3144 static int 3145 journal_space(ump, thresh) 3146 struct ufsmount *ump; 3147 int thresh; 3148 { 3149 struct jblocks *jblocks; 3150 int limit, avail; 3151 3152 jblocks = ump->softdep_jblocks; 3153 if (jblocks == NULL) 3154 return (1); 3155 /* 3156 * We use a tighter restriction here to prevent request_cleanup() 3157 * running in threads from running into locks we currently hold. 3158 * We have to be over the limit and our filesystem has to be 3159 * responsible for more than our share of that usage. 3160 */ 3161 limit = (max_softdeps / 10) * 9; 3162 if (dep_current[D_INODEDEP] > limit && 3163 ump->softdep_curdeps[D_INODEDEP] > limit / stat_flush_threads) 3164 return (0); 3165 if (thresh) 3166 thresh = jblocks->jb_min; 3167 else 3168 thresh = jblocks->jb_low; 3169 avail = (ump->softdep_on_journal * JREC_SIZE) / DEV_BSIZE; 3170 avail = jblocks->jb_free - avail; 3171 3172 return (avail > thresh); 3173 } 3174 3175 static void 3176 journal_suspend(ump) 3177 struct ufsmount *ump; 3178 { 3179 struct jblocks *jblocks; 3180 struct mount *mp; 3181 bool set; 3182 3183 mp = UFSTOVFS(ump); 3184 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) 3185 return; 3186 3187 jblocks = ump->softdep_jblocks; 3188 vfs_op_enter(mp); 3189 set = false; 3190 MNT_ILOCK(mp); 3191 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) { 3192 stat_journal_min++; 3193 mp->mnt_kern_flag |= MNTK_SUSPEND; 3194 mp->mnt_susp_owner = ump->softdep_flushtd; 3195 set = true; 3196 } 3197 jblocks->jb_suspended = 1; 3198 MNT_IUNLOCK(mp); 3199 if (!set) 3200 vfs_op_exit(mp); 3201 } 3202 3203 static int 3204 journal_unsuspend(struct ufsmount *ump) 3205 { 3206 struct jblocks *jblocks; 3207 struct mount *mp; 3208 3209 mp = UFSTOVFS(ump); 3210 jblocks = ump->softdep_jblocks; 3211 3212 if (jblocks != NULL && jblocks->jb_suspended && 3213 journal_space(ump, jblocks->jb_min)) { 3214 jblocks->jb_suspended = 0; 3215 FREE_LOCK(ump); 3216 mp->mnt_susp_owner = curthread; 3217 vfs_write_resume(mp, 0); 3218 ACQUIRE_LOCK(ump); 3219 return (1); 3220 } 3221 return (0); 3222 } 3223 3224 /* 3225 * Called before any allocation function to be certain that there is 3226 * sufficient space in the journal prior to creating any new records. 3227 * Since in the case of block allocation we may have multiple locked 3228 * buffers at the time of the actual allocation we can not block 3229 * when the journal records are created. Doing so would create a deadlock 3230 * if any of these buffers needed to be flushed to reclaim space. Instead 3231 * we require a sufficiently large amount of available space such that 3232 * each thread in the system could have passed this allocation check and 3233 * still have sufficient free space. With 20% of a minimum journal size 3234 * of 1MB we have 6553 records available. 3235 */ 3236 int 3237 softdep_prealloc(vp, waitok) 3238 struct vnode *vp; 3239 int waitok; 3240 { 3241 struct ufsmount *ump; 3242 3243 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 3244 ("softdep_prealloc called on non-softdep filesystem")); 3245 /* 3246 * Nothing to do if we are not running journaled soft updates. 3247 * If we currently hold the snapshot lock, we must avoid 3248 * handling other resources that could cause deadlock. Do not 3249 * touch quotas vnode since it is typically recursed with 3250 * other vnode locks held. 3251 */ 3252 if (DOINGSUJ(vp) == 0 || IS_SNAPSHOT(VTOI(vp)) || 3253 (vp->v_vflag & VV_SYSTEM) != 0) 3254 return (0); 3255 ump = VFSTOUFS(vp->v_mount); 3256 ACQUIRE_LOCK(ump); 3257 if (journal_space(ump, 0)) { 3258 FREE_LOCK(ump); 3259 return (0); 3260 } 3261 stat_journal_low++; 3262 FREE_LOCK(ump); 3263 if (waitok == MNT_NOWAIT) 3264 return (ENOSPC); 3265 /* 3266 * Attempt to sync this vnode once to flush any journal 3267 * work attached to it. 3268 */ 3269 if ((curthread->td_pflags & TDP_COWINPROGRESS) == 0) 3270 ffs_syncvnode(vp, waitok, 0); 3271 ACQUIRE_LOCK(ump); 3272 process_removes(vp); 3273 process_truncates(vp); 3274 if (journal_space(ump, 0) == 0) { 3275 softdep_speedup(ump); 3276 if (journal_space(ump, 1) == 0) 3277 journal_suspend(ump); 3278 } 3279 FREE_LOCK(ump); 3280 3281 return (0); 3282 } 3283 3284 /* 3285 * Try hard to sync all data and metadata for the vnode, and workitems 3286 * flushing which might conflict with the vnode lock. This is a 3287 * helper for softdep_prerename(). 3288 */ 3289 static int 3290 softdep_prerename_vnode(ump, vp) 3291 struct ufsmount *ump; 3292 struct vnode *vp; 3293 { 3294 int error; 3295 3296 ASSERT_VOP_ELOCKED(vp, "prehandle"); 3297 if (vp->v_data == NULL) 3298 return (0); 3299 error = VOP_FSYNC(vp, MNT_WAIT, curthread); 3300 if (error != 0) 3301 return (error); 3302 ACQUIRE_LOCK(ump); 3303 process_removes(vp); 3304 process_truncates(vp); 3305 FREE_LOCK(ump); 3306 return (0); 3307 } 3308 3309 /* 3310 * Must be called from VOP_RENAME() after all vnodes are locked. 3311 * Ensures that there is enough journal space for rename. It is 3312 * sufficiently different from softdep_prelink() by having to handle 3313 * four vnodes. 3314 */ 3315 int 3316 softdep_prerename(fdvp, fvp, tdvp, tvp) 3317 struct vnode *fdvp; 3318 struct vnode *fvp; 3319 struct vnode *tdvp; 3320 struct vnode *tvp; 3321 { 3322 struct ufsmount *ump; 3323 int error; 3324 3325 ump = VFSTOUFS(fdvp->v_mount); 3326 3327 if (journal_space(ump, 0)) 3328 return (0); 3329 3330 VOP_UNLOCK(tdvp); 3331 VOP_UNLOCK(fvp); 3332 if (tvp != NULL && tvp != tdvp) 3333 VOP_UNLOCK(tvp); 3334 3335 error = softdep_prerename_vnode(ump, fdvp); 3336 VOP_UNLOCK(fdvp); 3337 if (error != 0) 3338 return (error); 3339 3340 VOP_LOCK(fvp, LK_EXCLUSIVE | LK_RETRY); 3341 error = softdep_prerename_vnode(ump, fvp); 3342 VOP_UNLOCK(fvp); 3343 if (error != 0) 3344 return (error); 3345 3346 if (tdvp != fdvp) { 3347 VOP_LOCK(tdvp, LK_EXCLUSIVE | LK_RETRY); 3348 error = softdep_prerename_vnode(ump, tdvp); 3349 VOP_UNLOCK(tdvp); 3350 if (error != 0) 3351 return (error); 3352 } 3353 3354 if (tvp != fvp && tvp != NULL) { 3355 VOP_LOCK(tvp, LK_EXCLUSIVE | LK_RETRY); 3356 error = softdep_prerename_vnode(ump, tvp); 3357 VOP_UNLOCK(tvp); 3358 if (error != 0) 3359 return (error); 3360 } 3361 3362 ACQUIRE_LOCK(ump); 3363 softdep_speedup(ump); 3364 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT); 3365 if (journal_space(ump, 0) == 0) { 3366 softdep_speedup(ump); 3367 if (journal_space(ump, 1) == 0) 3368 journal_suspend(ump); 3369 } 3370 FREE_LOCK(ump); 3371 return (ERELOOKUP); 3372 } 3373 3374 /* 3375 * Before adjusting a link count on a vnode verify that we have sufficient 3376 * journal space. If not, process operations that depend on the currently 3377 * locked pair of vnodes to try to flush space as the syncer, buf daemon, 3378 * and softdep flush threads can not acquire these locks to reclaim space. 3379 * 3380 * Returns 0 if all owned locks are still valid and were not dropped 3381 * in the process, in other case it returns either an error from sync, 3382 * or ERELOOKUP if any of the locks were re-acquired. In the later 3383 * case, the state of the vnodes cannot be relied upon and our VFS 3384 * syscall must be restarted at top level from the lookup. 3385 */ 3386 int 3387 softdep_prelink(dvp, vp) 3388 struct vnode *dvp; 3389 struct vnode *vp; 3390 { 3391 struct ufsmount *ump; 3392 3393 ASSERT_VOP_ELOCKED(dvp, "prelink dvp"); 3394 if (vp != NULL) 3395 ASSERT_VOP_ELOCKED(vp, "prelink vp"); 3396 ump = VFSTOUFS(dvp->v_mount); 3397 3398 /* 3399 * Nothing to do if we have sufficient journal space. We skip 3400 * flushing when vp is a snapshot to avoid deadlock where 3401 * another thread is trying to update the inodeblock for dvp 3402 * and is waiting on snaplk that vp holds. 3403 */ 3404 if (journal_space(ump, 0) || (vp != NULL && IS_SNAPSHOT(VTOI(vp)))) 3405 return (0); 3406 3407 stat_journal_low++; 3408 if (vp != NULL) { 3409 VOP_UNLOCK(dvp); 3410 ffs_syncvnode(vp, MNT_NOWAIT, 0); 3411 vn_lock_pair(dvp, false, vp, true); 3412 if (dvp->v_data == NULL) 3413 return (ERELOOKUP); 3414 } 3415 if (vp != NULL) 3416 VOP_UNLOCK(vp); 3417 ffs_syncvnode(dvp, MNT_WAIT, 0); 3418 VOP_UNLOCK(dvp); 3419 3420 /* Process vp before dvp as it may create .. removes. */ 3421 if (vp != NULL) { 3422 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3423 if (vp->v_data == NULL) { 3424 vn_lock_pair(dvp, false, vp, true); 3425 return (ERELOOKUP); 3426 } 3427 ACQUIRE_LOCK(ump); 3428 process_removes(vp); 3429 process_truncates(vp); 3430 FREE_LOCK(ump); 3431 VOP_UNLOCK(vp); 3432 } 3433 3434 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 3435 if (dvp->v_data == NULL) { 3436 vn_lock_pair(dvp, true, vp, false); 3437 return (ERELOOKUP); 3438 } 3439 3440 ACQUIRE_LOCK(ump); 3441 process_removes(dvp); 3442 process_truncates(dvp); 3443 VOP_UNLOCK(dvp); 3444 softdep_speedup(ump); 3445 3446 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT); 3447 if (journal_space(ump, 0) == 0) { 3448 softdep_speedup(ump); 3449 if (journal_space(ump, 1) == 0) 3450 journal_suspend(ump); 3451 } 3452 FREE_LOCK(ump); 3453 3454 vn_lock_pair(dvp, false, vp, false); 3455 return (ERELOOKUP); 3456 } 3457 3458 static void 3459 jseg_write(ump, jseg, data) 3460 struct ufsmount *ump; 3461 struct jseg *jseg; 3462 uint8_t *data; 3463 { 3464 struct jsegrec *rec; 3465 3466 rec = (struct jsegrec *)data; 3467 rec->jsr_seq = jseg->js_seq; 3468 rec->jsr_oldest = jseg->js_oldseq; 3469 rec->jsr_cnt = jseg->js_cnt; 3470 rec->jsr_blocks = jseg->js_size / ump->um_devvp->v_bufobj.bo_bsize; 3471 rec->jsr_crc = 0; 3472 rec->jsr_time = ump->um_fs->fs_mtime; 3473 } 3474 3475 static inline void 3476 inoref_write(inoref, jseg, rec) 3477 struct inoref *inoref; 3478 struct jseg *jseg; 3479 struct jrefrec *rec; 3480 { 3481 3482 inoref->if_jsegdep->jd_seg = jseg; 3483 rec->jr_ino = inoref->if_ino; 3484 rec->jr_parent = inoref->if_parent; 3485 rec->jr_nlink = inoref->if_nlink; 3486 rec->jr_mode = inoref->if_mode; 3487 rec->jr_diroff = inoref->if_diroff; 3488 } 3489 3490 static void 3491 jaddref_write(jaddref, jseg, data) 3492 struct jaddref *jaddref; 3493 struct jseg *jseg; 3494 uint8_t *data; 3495 { 3496 struct jrefrec *rec; 3497 3498 rec = (struct jrefrec *)data; 3499 rec->jr_op = JOP_ADDREF; 3500 inoref_write(&jaddref->ja_ref, jseg, rec); 3501 } 3502 3503 static void 3504 jremref_write(jremref, jseg, data) 3505 struct jremref *jremref; 3506 struct jseg *jseg; 3507 uint8_t *data; 3508 { 3509 struct jrefrec *rec; 3510 3511 rec = (struct jrefrec *)data; 3512 rec->jr_op = JOP_REMREF; 3513 inoref_write(&jremref->jr_ref, jseg, rec); 3514 } 3515 3516 static void 3517 jmvref_write(jmvref, jseg, data) 3518 struct jmvref *jmvref; 3519 struct jseg *jseg; 3520 uint8_t *data; 3521 { 3522 struct jmvrec *rec; 3523 3524 rec = (struct jmvrec *)data; 3525 rec->jm_op = JOP_MVREF; 3526 rec->jm_ino = jmvref->jm_ino; 3527 rec->jm_parent = jmvref->jm_parent; 3528 rec->jm_oldoff = jmvref->jm_oldoff; 3529 rec->jm_newoff = jmvref->jm_newoff; 3530 } 3531 3532 static void 3533 jnewblk_write(jnewblk, jseg, data) 3534 struct jnewblk *jnewblk; 3535 struct jseg *jseg; 3536 uint8_t *data; 3537 { 3538 struct jblkrec *rec; 3539 3540 jnewblk->jn_jsegdep->jd_seg = jseg; 3541 rec = (struct jblkrec *)data; 3542 rec->jb_op = JOP_NEWBLK; 3543 rec->jb_ino = jnewblk->jn_ino; 3544 rec->jb_blkno = jnewblk->jn_blkno; 3545 rec->jb_lbn = jnewblk->jn_lbn; 3546 rec->jb_frags = jnewblk->jn_frags; 3547 rec->jb_oldfrags = jnewblk->jn_oldfrags; 3548 } 3549 3550 static void 3551 jfreeblk_write(jfreeblk, jseg, data) 3552 struct jfreeblk *jfreeblk; 3553 struct jseg *jseg; 3554 uint8_t *data; 3555 { 3556 struct jblkrec *rec; 3557 3558 jfreeblk->jf_dep.jb_jsegdep->jd_seg = jseg; 3559 rec = (struct jblkrec *)data; 3560 rec->jb_op = JOP_FREEBLK; 3561 rec->jb_ino = jfreeblk->jf_ino; 3562 rec->jb_blkno = jfreeblk->jf_blkno; 3563 rec->jb_lbn = jfreeblk->jf_lbn; 3564 rec->jb_frags = jfreeblk->jf_frags; 3565 rec->jb_oldfrags = 0; 3566 } 3567 3568 static void 3569 jfreefrag_write(jfreefrag, jseg, data) 3570 struct jfreefrag *jfreefrag; 3571 struct jseg *jseg; 3572 uint8_t *data; 3573 { 3574 struct jblkrec *rec; 3575 3576 jfreefrag->fr_jsegdep->jd_seg = jseg; 3577 rec = (struct jblkrec *)data; 3578 rec->jb_op = JOP_FREEBLK; 3579 rec->jb_ino = jfreefrag->fr_ino; 3580 rec->jb_blkno = jfreefrag->fr_blkno; 3581 rec->jb_lbn = jfreefrag->fr_lbn; 3582 rec->jb_frags = jfreefrag->fr_frags; 3583 rec->jb_oldfrags = 0; 3584 } 3585 3586 static void 3587 jtrunc_write(jtrunc, jseg, data) 3588 struct jtrunc *jtrunc; 3589 struct jseg *jseg; 3590 uint8_t *data; 3591 { 3592 struct jtrncrec *rec; 3593 3594 jtrunc->jt_dep.jb_jsegdep->jd_seg = jseg; 3595 rec = (struct jtrncrec *)data; 3596 rec->jt_op = JOP_TRUNC; 3597 rec->jt_ino = jtrunc->jt_ino; 3598 rec->jt_size = jtrunc->jt_size; 3599 rec->jt_extsize = jtrunc->jt_extsize; 3600 } 3601 3602 static void 3603 jfsync_write(jfsync, jseg, data) 3604 struct jfsync *jfsync; 3605 struct jseg *jseg; 3606 uint8_t *data; 3607 { 3608 struct jtrncrec *rec; 3609 3610 rec = (struct jtrncrec *)data; 3611 rec->jt_op = JOP_SYNC; 3612 rec->jt_ino = jfsync->jfs_ino; 3613 rec->jt_size = jfsync->jfs_size; 3614 rec->jt_extsize = jfsync->jfs_extsize; 3615 } 3616 3617 static void 3618 softdep_flushjournal(mp) 3619 struct mount *mp; 3620 { 3621 struct jblocks *jblocks; 3622 struct ufsmount *ump; 3623 3624 if (MOUNTEDSUJ(mp) == 0) 3625 return; 3626 ump = VFSTOUFS(mp); 3627 jblocks = ump->softdep_jblocks; 3628 ACQUIRE_LOCK(ump); 3629 while (ump->softdep_on_journal) { 3630 jblocks->jb_needseg = 1; 3631 softdep_process_journal(mp, NULL, MNT_WAIT); 3632 } 3633 FREE_LOCK(ump); 3634 } 3635 3636 static void softdep_synchronize_completed(struct bio *); 3637 static void softdep_synchronize(struct bio *, struct ufsmount *, void *); 3638 3639 static void 3640 softdep_synchronize_completed(bp) 3641 struct bio *bp; 3642 { 3643 struct jseg *oldest; 3644 struct jseg *jseg; 3645 struct ufsmount *ump; 3646 3647 /* 3648 * caller1 marks the last segment written before we issued the 3649 * synchronize cache. 3650 */ 3651 jseg = bp->bio_caller1; 3652 if (jseg == NULL) { 3653 g_destroy_bio(bp); 3654 return; 3655 } 3656 ump = VFSTOUFS(jseg->js_list.wk_mp); 3657 ACQUIRE_LOCK(ump); 3658 oldest = NULL; 3659 /* 3660 * Mark all the journal entries waiting on the synchronize cache 3661 * as completed so they may continue on. 3662 */ 3663 while (jseg != NULL && (jseg->js_state & COMPLETE) == 0) { 3664 jseg->js_state |= COMPLETE; 3665 oldest = jseg; 3666 jseg = TAILQ_PREV(jseg, jseglst, js_next); 3667 } 3668 /* 3669 * Restart deferred journal entry processing from the oldest 3670 * completed jseg. 3671 */ 3672 if (oldest) 3673 complete_jsegs(oldest); 3674 3675 FREE_LOCK(ump); 3676 g_destroy_bio(bp); 3677 } 3678 3679 /* 3680 * Send BIO_FLUSH/SYNCHRONIZE CACHE to the device to enforce write ordering 3681 * barriers. The journal must be written prior to any blocks that depend 3682 * on it and the journal can not be released until the blocks have be 3683 * written. This code handles both barriers simultaneously. 3684 */ 3685 static void 3686 softdep_synchronize(bp, ump, caller1) 3687 struct bio *bp; 3688 struct ufsmount *ump; 3689 void *caller1; 3690 { 3691 3692 bp->bio_cmd = BIO_FLUSH; 3693 bp->bio_flags |= BIO_ORDERED; 3694 bp->bio_data = NULL; 3695 bp->bio_offset = ump->um_cp->provider->mediasize; 3696 bp->bio_length = 0; 3697 bp->bio_done = softdep_synchronize_completed; 3698 bp->bio_caller1 = caller1; 3699 g_io_request(bp, ump->um_cp); 3700 } 3701 3702 /* 3703 * Flush some journal records to disk. 3704 */ 3705 static void 3706 softdep_process_journal(mp, needwk, flags) 3707 struct mount *mp; 3708 struct worklist *needwk; 3709 int flags; 3710 { 3711 struct jblocks *jblocks; 3712 struct ufsmount *ump; 3713 struct worklist *wk; 3714 struct jseg *jseg; 3715 struct buf *bp; 3716 struct bio *bio; 3717 uint8_t *data; 3718 struct fs *fs; 3719 int shouldflush; 3720 int segwritten; 3721 int jrecmin; /* Minimum records per block. */ 3722 int jrecmax; /* Maximum records per block. */ 3723 int size; 3724 int cnt; 3725 int off; 3726 int devbsize; 3727 3728 ump = VFSTOUFS(mp); 3729 if (ump->um_softdep == NULL || ump->um_softdep->sd_jblocks == NULL) 3730 return; 3731 shouldflush = softdep_flushcache; 3732 bio = NULL; 3733 jseg = NULL; 3734 LOCK_OWNED(ump); 3735 fs = ump->um_fs; 3736 jblocks = ump->softdep_jblocks; 3737 devbsize = ump->um_devvp->v_bufobj.bo_bsize; 3738 /* 3739 * We write anywhere between a disk block and fs block. The upper 3740 * bound is picked to prevent buffer cache fragmentation and limit 3741 * processing time per I/O. 3742 */ 3743 jrecmin = (devbsize / JREC_SIZE) - 1; /* -1 for seg header */ 3744 jrecmax = (fs->fs_bsize / devbsize) * jrecmin; 3745 segwritten = 0; 3746 for (;;) { 3747 cnt = ump->softdep_on_journal; 3748 /* 3749 * Criteria for writing a segment: 3750 * 1) We have a full block. 3751 * 2) We're called from jwait() and haven't found the 3752 * journal item yet. 3753 * 3) Always write if needseg is set. 3754 * 4) If we are called from process_worklist and have 3755 * not yet written anything we write a partial block 3756 * to enforce a 1 second maximum latency on journal 3757 * entries. 3758 */ 3759 if (cnt < (jrecmax - 1) && needwk == NULL && 3760 jblocks->jb_needseg == 0 && (segwritten || cnt == 0)) 3761 break; 3762 cnt++; 3763 /* 3764 * Verify some free journal space. softdep_prealloc() should 3765 * guarantee that we don't run out so this is indicative of 3766 * a problem with the flow control. Try to recover 3767 * gracefully in any event. 3768 */ 3769 while (jblocks->jb_free == 0) { 3770 if (flags != MNT_WAIT) 3771 break; 3772 printf("softdep: Out of journal space!\n"); 3773 softdep_speedup(ump); 3774 msleep(jblocks, LOCK_PTR(ump), PRIBIO, "jblocks", hz); 3775 } 3776 FREE_LOCK(ump); 3777 jseg = malloc(sizeof(*jseg), M_JSEG, M_SOFTDEP_FLAGS); 3778 workitem_alloc(&jseg->js_list, D_JSEG, mp); 3779 LIST_INIT(&jseg->js_entries); 3780 LIST_INIT(&jseg->js_indirs); 3781 jseg->js_state = ATTACHED; 3782 if (shouldflush == 0) 3783 jseg->js_state |= COMPLETE; 3784 else if (bio == NULL) 3785 bio = g_alloc_bio(); 3786 jseg->js_jblocks = jblocks; 3787 bp = geteblk(fs->fs_bsize, 0); 3788 ACQUIRE_LOCK(ump); 3789 /* 3790 * If there was a race while we were allocating the block 3791 * and jseg the entry we care about was likely written. 3792 * We bail out in both the WAIT and NOWAIT case and assume 3793 * the caller will loop if the entry it cares about is 3794 * not written. 3795 */ 3796 cnt = ump->softdep_on_journal; 3797 if (cnt + jblocks->jb_needseg == 0 || jblocks->jb_free == 0) { 3798 bp->b_flags |= B_INVAL | B_NOCACHE; 3799 WORKITEM_FREE(jseg, D_JSEG); 3800 FREE_LOCK(ump); 3801 brelse(bp); 3802 ACQUIRE_LOCK(ump); 3803 break; 3804 } 3805 /* 3806 * Calculate the disk block size required for the available 3807 * records rounded to the min size. 3808 */ 3809 if (cnt == 0) 3810 size = devbsize; 3811 else if (cnt < jrecmax) 3812 size = howmany(cnt, jrecmin) * devbsize; 3813 else 3814 size = fs->fs_bsize; 3815 /* 3816 * Allocate a disk block for this journal data and account 3817 * for truncation of the requested size if enough contiguous 3818 * space was not available. 3819 */ 3820 bp->b_blkno = jblocks_alloc(jblocks, size, &size); 3821 bp->b_lblkno = bp->b_blkno; 3822 bp->b_offset = bp->b_blkno * DEV_BSIZE; 3823 bp->b_bcount = size; 3824 bp->b_flags &= ~B_INVAL; 3825 bp->b_flags |= B_VALIDSUSPWRT | B_NOCOPY; 3826 /* 3827 * Initialize our jseg with cnt records. Assign the next 3828 * sequence number to it and link it in-order. 3829 */ 3830 cnt = MIN(cnt, (size / devbsize) * jrecmin); 3831 jseg->js_buf = bp; 3832 jseg->js_cnt = cnt; 3833 jseg->js_refs = cnt + 1; /* Self ref. */ 3834 jseg->js_size = size; 3835 jseg->js_seq = jblocks->jb_nextseq++; 3836 if (jblocks->jb_oldestseg == NULL) 3837 jblocks->jb_oldestseg = jseg; 3838 jseg->js_oldseq = jblocks->jb_oldestseg->js_seq; 3839 TAILQ_INSERT_TAIL(&jblocks->jb_segs, jseg, js_next); 3840 if (jblocks->jb_writeseg == NULL) 3841 jblocks->jb_writeseg = jseg; 3842 /* 3843 * Start filling in records from the pending list. 3844 */ 3845 data = bp->b_data; 3846 off = 0; 3847 3848 /* 3849 * Always put a header on the first block. 3850 * XXX As with below, there might not be a chance to get 3851 * into the loop. Ensure that something valid is written. 3852 */ 3853 jseg_write(ump, jseg, data); 3854 off += JREC_SIZE; 3855 data = bp->b_data + off; 3856 3857 /* 3858 * XXX Something is wrong here. There's no work to do, 3859 * but we need to perform and I/O and allow it to complete 3860 * anyways. 3861 */ 3862 if (LIST_EMPTY(&ump->softdep_journal_pending)) 3863 stat_emptyjblocks++; 3864 3865 while ((wk = LIST_FIRST(&ump->softdep_journal_pending)) 3866 != NULL) { 3867 if (cnt == 0) 3868 break; 3869 /* Place a segment header on every device block. */ 3870 if ((off % devbsize) == 0) { 3871 jseg_write(ump, jseg, data); 3872 off += JREC_SIZE; 3873 data = bp->b_data + off; 3874 } 3875 if (wk == needwk) 3876 needwk = NULL; 3877 remove_from_journal(wk); 3878 wk->wk_state |= INPROGRESS; 3879 WORKLIST_INSERT(&jseg->js_entries, wk); 3880 switch (wk->wk_type) { 3881 case D_JADDREF: 3882 jaddref_write(WK_JADDREF(wk), jseg, data); 3883 break; 3884 case D_JREMREF: 3885 jremref_write(WK_JREMREF(wk), jseg, data); 3886 break; 3887 case D_JMVREF: 3888 jmvref_write(WK_JMVREF(wk), jseg, data); 3889 break; 3890 case D_JNEWBLK: 3891 jnewblk_write(WK_JNEWBLK(wk), jseg, data); 3892 break; 3893 case D_JFREEBLK: 3894 jfreeblk_write(WK_JFREEBLK(wk), jseg, data); 3895 break; 3896 case D_JFREEFRAG: 3897 jfreefrag_write(WK_JFREEFRAG(wk), jseg, data); 3898 break; 3899 case D_JTRUNC: 3900 jtrunc_write(WK_JTRUNC(wk), jseg, data); 3901 break; 3902 case D_JFSYNC: 3903 jfsync_write(WK_JFSYNC(wk), jseg, data); 3904 break; 3905 default: 3906 panic("process_journal: Unknown type %s", 3907 TYPENAME(wk->wk_type)); 3908 /* NOTREACHED */ 3909 } 3910 off += JREC_SIZE; 3911 data = bp->b_data + off; 3912 cnt--; 3913 } 3914 3915 /* Clear any remaining space so we don't leak kernel data */ 3916 if (size > off) 3917 bzero(data, size - off); 3918 3919 /* 3920 * Write this one buffer and continue. 3921 */ 3922 segwritten = 1; 3923 jblocks->jb_needseg = 0; 3924 WORKLIST_INSERT(&bp->b_dep, &jseg->js_list); 3925 FREE_LOCK(ump); 3926 bp->b_xflags |= BX_CVTENXIO; 3927 pbgetvp(ump->um_devvp, bp); 3928 /* 3929 * We only do the blocking wait once we find the journal 3930 * entry we're looking for. 3931 */ 3932 if (needwk == NULL && flags == MNT_WAIT) 3933 bwrite(bp); 3934 else 3935 bawrite(bp); 3936 ACQUIRE_LOCK(ump); 3937 } 3938 /* 3939 * If we wrote a segment issue a synchronize cache so the journal 3940 * is reflected on disk before the data is written. Since reclaiming 3941 * journal space also requires writing a journal record this 3942 * process also enforces a barrier before reclamation. 3943 */ 3944 if (segwritten && shouldflush) { 3945 softdep_synchronize(bio, ump, 3946 TAILQ_LAST(&jblocks->jb_segs, jseglst)); 3947 } else if (bio) 3948 g_destroy_bio(bio); 3949 /* 3950 * If we've suspended the filesystem because we ran out of journal 3951 * space either try to sync it here to make some progress or 3952 * unsuspend it if we already have. 3953 */ 3954 if (flags == 0 && jblocks->jb_suspended) { 3955 if (journal_unsuspend(ump)) 3956 return; 3957 FREE_LOCK(ump); 3958 VFS_SYNC(mp, MNT_NOWAIT); 3959 ffs_sbupdate(ump, MNT_WAIT, 0); 3960 ACQUIRE_LOCK(ump); 3961 } 3962 } 3963 3964 /* 3965 * Complete a jseg, allowing all dependencies awaiting journal writes 3966 * to proceed. Each journal dependency also attaches a jsegdep to dependent 3967 * structures so that the journal segment can be freed to reclaim space. 3968 */ 3969 static void 3970 complete_jseg(jseg) 3971 struct jseg *jseg; 3972 { 3973 struct worklist *wk; 3974 struct jmvref *jmvref; 3975 #ifdef INVARIANTS 3976 int i = 0; 3977 #endif 3978 3979 while ((wk = LIST_FIRST(&jseg->js_entries)) != NULL) { 3980 WORKLIST_REMOVE(wk); 3981 wk->wk_state &= ~INPROGRESS; 3982 wk->wk_state |= COMPLETE; 3983 KASSERT(i++ < jseg->js_cnt, 3984 ("handle_written_jseg: overflow %d >= %d", 3985 i - 1, jseg->js_cnt)); 3986 switch (wk->wk_type) { 3987 case D_JADDREF: 3988 handle_written_jaddref(WK_JADDREF(wk)); 3989 break; 3990 case D_JREMREF: 3991 handle_written_jremref(WK_JREMREF(wk)); 3992 break; 3993 case D_JMVREF: 3994 rele_jseg(jseg); /* No jsegdep. */ 3995 jmvref = WK_JMVREF(wk); 3996 LIST_REMOVE(jmvref, jm_deps); 3997 if ((jmvref->jm_pagedep->pd_state & ONWORKLIST) == 0) 3998 free_pagedep(jmvref->jm_pagedep); 3999 WORKITEM_FREE(jmvref, D_JMVREF); 4000 break; 4001 case D_JNEWBLK: 4002 handle_written_jnewblk(WK_JNEWBLK(wk)); 4003 break; 4004 case D_JFREEBLK: 4005 handle_written_jblkdep(&WK_JFREEBLK(wk)->jf_dep); 4006 break; 4007 case D_JTRUNC: 4008 handle_written_jblkdep(&WK_JTRUNC(wk)->jt_dep); 4009 break; 4010 case D_JFSYNC: 4011 rele_jseg(jseg); /* No jsegdep. */ 4012 WORKITEM_FREE(wk, D_JFSYNC); 4013 break; 4014 case D_JFREEFRAG: 4015 handle_written_jfreefrag(WK_JFREEFRAG(wk)); 4016 break; 4017 default: 4018 panic("handle_written_jseg: Unknown type %s", 4019 TYPENAME(wk->wk_type)); 4020 /* NOTREACHED */ 4021 } 4022 } 4023 /* Release the self reference so the structure may be freed. */ 4024 rele_jseg(jseg); 4025 } 4026 4027 /* 4028 * Determine which jsegs are ready for completion processing. Waits for 4029 * synchronize cache to complete as well as forcing in-order completion 4030 * of journal entries. 4031 */ 4032 static void 4033 complete_jsegs(jseg) 4034 struct jseg *jseg; 4035 { 4036 struct jblocks *jblocks; 4037 struct jseg *jsegn; 4038 4039 jblocks = jseg->js_jblocks; 4040 /* 4041 * Don't allow out of order completions. If this isn't the first 4042 * block wait for it to write before we're done. 4043 */ 4044 if (jseg != jblocks->jb_writeseg) 4045 return; 4046 /* Iterate through available jsegs processing their entries. */ 4047 while (jseg && (jseg->js_state & ALLCOMPLETE) == ALLCOMPLETE) { 4048 jblocks->jb_oldestwrseq = jseg->js_oldseq; 4049 jsegn = TAILQ_NEXT(jseg, js_next); 4050 complete_jseg(jseg); 4051 jseg = jsegn; 4052 } 4053 jblocks->jb_writeseg = jseg; 4054 /* 4055 * Attempt to free jsegs now that oldestwrseq may have advanced. 4056 */ 4057 free_jsegs(jblocks); 4058 } 4059 4060 /* 4061 * Mark a jseg as DEPCOMPLETE and throw away the buffer. Attempt to handle 4062 * the final completions. 4063 */ 4064 static void 4065 handle_written_jseg(jseg, bp) 4066 struct jseg *jseg; 4067 struct buf *bp; 4068 { 4069 4070 if (jseg->js_refs == 0) 4071 panic("handle_written_jseg: No self-reference on %p", jseg); 4072 jseg->js_state |= DEPCOMPLETE; 4073 /* 4074 * We'll never need this buffer again, set flags so it will be 4075 * discarded. 4076 */ 4077 bp->b_flags |= B_INVAL | B_NOCACHE; 4078 pbrelvp(bp); 4079 complete_jsegs(jseg); 4080 } 4081 4082 static inline struct jsegdep * 4083 inoref_jseg(inoref) 4084 struct inoref *inoref; 4085 { 4086 struct jsegdep *jsegdep; 4087 4088 jsegdep = inoref->if_jsegdep; 4089 inoref->if_jsegdep = NULL; 4090 4091 return (jsegdep); 4092 } 4093 4094 /* 4095 * Called once a jremref has made it to stable store. The jremref is marked 4096 * complete and we attempt to free it. Any pagedeps writes sleeping waiting 4097 * for the jremref to complete will be awoken by free_jremref. 4098 */ 4099 static void 4100 handle_written_jremref(jremref) 4101 struct jremref *jremref; 4102 { 4103 struct inodedep *inodedep; 4104 struct jsegdep *jsegdep; 4105 struct dirrem *dirrem; 4106 4107 /* Grab the jsegdep. */ 4108 jsegdep = inoref_jseg(&jremref->jr_ref); 4109 /* 4110 * Remove us from the inoref list. 4111 */ 4112 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 4113 0, &inodedep) == 0) 4114 panic("handle_written_jremref: Lost inodedep"); 4115 TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 4116 /* 4117 * Complete the dirrem. 4118 */ 4119 dirrem = jremref->jr_dirrem; 4120 jremref->jr_dirrem = NULL; 4121 LIST_REMOVE(jremref, jr_deps); 4122 jsegdep->jd_state |= jremref->jr_state & MKDIR_PARENT; 4123 jwork_insert(&dirrem->dm_jwork, jsegdep); 4124 if (LIST_EMPTY(&dirrem->dm_jremrefhd) && 4125 (dirrem->dm_state & COMPLETE) != 0) 4126 add_to_worklist(&dirrem->dm_list, 0); 4127 free_jremref(jremref); 4128 } 4129 4130 /* 4131 * Called once a jaddref has made it to stable store. The dependency is 4132 * marked complete and any dependent structures are added to the inode 4133 * bufwait list to be completed as soon as it is written. If a bitmap write 4134 * depends on this entry we move the inode into the inodedephd of the 4135 * bmsafemap dependency and attempt to remove the jaddref from the bmsafemap. 4136 */ 4137 static void 4138 handle_written_jaddref(jaddref) 4139 struct jaddref *jaddref; 4140 { 4141 struct jsegdep *jsegdep; 4142 struct inodedep *inodedep; 4143 struct diradd *diradd; 4144 struct mkdir *mkdir; 4145 4146 /* Grab the jsegdep. */ 4147 jsegdep = inoref_jseg(&jaddref->ja_ref); 4148 mkdir = NULL; 4149 diradd = NULL; 4150 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 4151 0, &inodedep) == 0) 4152 panic("handle_written_jaddref: Lost inodedep."); 4153 if (jaddref->ja_diradd == NULL) 4154 panic("handle_written_jaddref: No dependency"); 4155 if (jaddref->ja_diradd->da_list.wk_type == D_DIRADD) { 4156 diradd = jaddref->ja_diradd; 4157 WORKLIST_INSERT(&inodedep->id_bufwait, &diradd->da_list); 4158 } else if (jaddref->ja_state & MKDIR_PARENT) { 4159 mkdir = jaddref->ja_mkdir; 4160 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir->md_list); 4161 } else if (jaddref->ja_state & MKDIR_BODY) 4162 mkdir = jaddref->ja_mkdir; 4163 else 4164 panic("handle_written_jaddref: Unknown dependency %p", 4165 jaddref->ja_diradd); 4166 jaddref->ja_diradd = NULL; /* also clears ja_mkdir */ 4167 /* 4168 * Remove us from the inode list. 4169 */ 4170 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps); 4171 /* 4172 * The mkdir may be waiting on the jaddref to clear before freeing. 4173 */ 4174 if (mkdir) { 4175 KASSERT(mkdir->md_list.wk_type == D_MKDIR, 4176 ("handle_written_jaddref: Incorrect type for mkdir %s", 4177 TYPENAME(mkdir->md_list.wk_type))); 4178 mkdir->md_jaddref = NULL; 4179 diradd = mkdir->md_diradd; 4180 mkdir->md_state |= DEPCOMPLETE; 4181 complete_mkdir(mkdir); 4182 } 4183 jwork_insert(&diradd->da_jwork, jsegdep); 4184 if (jaddref->ja_state & NEWBLOCK) { 4185 inodedep->id_state |= ONDEPLIST; 4186 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_inodedephd, 4187 inodedep, id_deps); 4188 } 4189 free_jaddref(jaddref); 4190 } 4191 4192 /* 4193 * Called once a jnewblk journal is written. The allocdirect or allocindir 4194 * is placed in the bmsafemap to await notification of a written bitmap. If 4195 * the operation was canceled we add the segdep to the appropriate 4196 * dependency to free the journal space once the canceling operation 4197 * completes. 4198 */ 4199 static void 4200 handle_written_jnewblk(jnewblk) 4201 struct jnewblk *jnewblk; 4202 { 4203 struct bmsafemap *bmsafemap; 4204 struct freefrag *freefrag; 4205 struct freework *freework; 4206 struct jsegdep *jsegdep; 4207 struct newblk *newblk; 4208 4209 /* Grab the jsegdep. */ 4210 jsegdep = jnewblk->jn_jsegdep; 4211 jnewblk->jn_jsegdep = NULL; 4212 if (jnewblk->jn_dep == NULL) 4213 panic("handle_written_jnewblk: No dependency for the segdep."); 4214 switch (jnewblk->jn_dep->wk_type) { 4215 case D_NEWBLK: 4216 case D_ALLOCDIRECT: 4217 case D_ALLOCINDIR: 4218 /* 4219 * Add the written block to the bmsafemap so it can 4220 * be notified when the bitmap is on disk. 4221 */ 4222 newblk = WK_NEWBLK(jnewblk->jn_dep); 4223 newblk->nb_jnewblk = NULL; 4224 if ((newblk->nb_state & GOINGAWAY) == 0) { 4225 bmsafemap = newblk->nb_bmsafemap; 4226 newblk->nb_state |= ONDEPLIST; 4227 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, 4228 nb_deps); 4229 } 4230 jwork_insert(&newblk->nb_jwork, jsegdep); 4231 break; 4232 case D_FREEFRAG: 4233 /* 4234 * A newblock being removed by a freefrag when replaced by 4235 * frag extension. 4236 */ 4237 freefrag = WK_FREEFRAG(jnewblk->jn_dep); 4238 freefrag->ff_jdep = NULL; 4239 jwork_insert(&freefrag->ff_jwork, jsegdep); 4240 break; 4241 case D_FREEWORK: 4242 /* 4243 * A direct block was removed by truncate. 4244 */ 4245 freework = WK_FREEWORK(jnewblk->jn_dep); 4246 freework->fw_jnewblk = NULL; 4247 jwork_insert(&freework->fw_freeblks->fb_jwork, jsegdep); 4248 break; 4249 default: 4250 panic("handle_written_jnewblk: Unknown type %d.", 4251 jnewblk->jn_dep->wk_type); 4252 } 4253 jnewblk->jn_dep = NULL; 4254 free_jnewblk(jnewblk); 4255 } 4256 4257 /* 4258 * Cancel a jfreefrag that won't be needed, probably due to colliding with 4259 * an in-flight allocation that has not yet been committed. Divorce us 4260 * from the freefrag and mark it DEPCOMPLETE so that it may be added 4261 * to the worklist. 4262 */ 4263 static void 4264 cancel_jfreefrag(jfreefrag) 4265 struct jfreefrag *jfreefrag; 4266 { 4267 struct freefrag *freefrag; 4268 4269 if (jfreefrag->fr_jsegdep) { 4270 free_jsegdep(jfreefrag->fr_jsegdep); 4271 jfreefrag->fr_jsegdep = NULL; 4272 } 4273 freefrag = jfreefrag->fr_freefrag; 4274 jfreefrag->fr_freefrag = NULL; 4275 free_jfreefrag(jfreefrag); 4276 freefrag->ff_state |= DEPCOMPLETE; 4277 CTR1(KTR_SUJ, "cancel_jfreefrag: blkno %jd", freefrag->ff_blkno); 4278 } 4279 4280 /* 4281 * Free a jfreefrag when the parent freefrag is rendered obsolete. 4282 */ 4283 static void 4284 free_jfreefrag(jfreefrag) 4285 struct jfreefrag *jfreefrag; 4286 { 4287 4288 if (jfreefrag->fr_state & INPROGRESS) 4289 WORKLIST_REMOVE(&jfreefrag->fr_list); 4290 else if (jfreefrag->fr_state & ONWORKLIST) 4291 remove_from_journal(&jfreefrag->fr_list); 4292 if (jfreefrag->fr_freefrag != NULL) 4293 panic("free_jfreefrag: Still attached to a freefrag."); 4294 WORKITEM_FREE(jfreefrag, D_JFREEFRAG); 4295 } 4296 4297 /* 4298 * Called when the journal write for a jfreefrag completes. The parent 4299 * freefrag is added to the worklist if this completes its dependencies. 4300 */ 4301 static void 4302 handle_written_jfreefrag(jfreefrag) 4303 struct jfreefrag *jfreefrag; 4304 { 4305 struct jsegdep *jsegdep; 4306 struct freefrag *freefrag; 4307 4308 /* Grab the jsegdep. */ 4309 jsegdep = jfreefrag->fr_jsegdep; 4310 jfreefrag->fr_jsegdep = NULL; 4311 freefrag = jfreefrag->fr_freefrag; 4312 if (freefrag == NULL) 4313 panic("handle_written_jfreefrag: No freefrag."); 4314 freefrag->ff_state |= DEPCOMPLETE; 4315 freefrag->ff_jdep = NULL; 4316 jwork_insert(&freefrag->ff_jwork, jsegdep); 4317 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 4318 add_to_worklist(&freefrag->ff_list, 0); 4319 jfreefrag->fr_freefrag = NULL; 4320 free_jfreefrag(jfreefrag); 4321 } 4322 4323 /* 4324 * Called when the journal write for a jfreeblk completes. The jfreeblk 4325 * is removed from the freeblks list of pending journal writes and the 4326 * jsegdep is moved to the freeblks jwork to be completed when all blocks 4327 * have been reclaimed. 4328 */ 4329 static void 4330 handle_written_jblkdep(jblkdep) 4331 struct jblkdep *jblkdep; 4332 { 4333 struct freeblks *freeblks; 4334 struct jsegdep *jsegdep; 4335 4336 /* Grab the jsegdep. */ 4337 jsegdep = jblkdep->jb_jsegdep; 4338 jblkdep->jb_jsegdep = NULL; 4339 freeblks = jblkdep->jb_freeblks; 4340 LIST_REMOVE(jblkdep, jb_deps); 4341 jwork_insert(&freeblks->fb_jwork, jsegdep); 4342 /* 4343 * If the freeblks is all journaled, we can add it to the worklist. 4344 */ 4345 if (LIST_EMPTY(&freeblks->fb_jblkdephd) && 4346 (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 4347 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 4348 4349 free_jblkdep(jblkdep); 4350 } 4351 4352 static struct jsegdep * 4353 newjsegdep(struct worklist *wk) 4354 { 4355 struct jsegdep *jsegdep; 4356 4357 jsegdep = malloc(sizeof(*jsegdep), M_JSEGDEP, M_SOFTDEP_FLAGS); 4358 workitem_alloc(&jsegdep->jd_list, D_JSEGDEP, wk->wk_mp); 4359 jsegdep->jd_seg = NULL; 4360 4361 return (jsegdep); 4362 } 4363 4364 static struct jmvref * 4365 newjmvref(dp, ino, oldoff, newoff) 4366 struct inode *dp; 4367 ino_t ino; 4368 off_t oldoff; 4369 off_t newoff; 4370 { 4371 struct jmvref *jmvref; 4372 4373 jmvref = malloc(sizeof(*jmvref), M_JMVREF, M_SOFTDEP_FLAGS); 4374 workitem_alloc(&jmvref->jm_list, D_JMVREF, ITOVFS(dp)); 4375 jmvref->jm_list.wk_state = ATTACHED | DEPCOMPLETE; 4376 jmvref->jm_parent = dp->i_number; 4377 jmvref->jm_ino = ino; 4378 jmvref->jm_oldoff = oldoff; 4379 jmvref->jm_newoff = newoff; 4380 4381 return (jmvref); 4382 } 4383 4384 /* 4385 * Allocate a new jremref that tracks the removal of ip from dp with the 4386 * directory entry offset of diroff. Mark the entry as ATTACHED and 4387 * DEPCOMPLETE as we have all the information required for the journal write 4388 * and the directory has already been removed from the buffer. The caller 4389 * is responsible for linking the jremref into the pagedep and adding it 4390 * to the journal to write. The MKDIR_PARENT flag is set if we're doing 4391 * a DOTDOT addition so handle_workitem_remove() can properly assign 4392 * the jsegdep when we're done. 4393 */ 4394 static struct jremref * 4395 newjremref(struct dirrem *dirrem, struct inode *dp, struct inode *ip, 4396 off_t diroff, nlink_t nlink) 4397 { 4398 struct jremref *jremref; 4399 4400 jremref = malloc(sizeof(*jremref), M_JREMREF, M_SOFTDEP_FLAGS); 4401 workitem_alloc(&jremref->jr_list, D_JREMREF, ITOVFS(dp)); 4402 jremref->jr_state = ATTACHED; 4403 newinoref(&jremref->jr_ref, ip->i_number, dp->i_number, diroff, 4404 nlink, ip->i_mode); 4405 jremref->jr_dirrem = dirrem; 4406 4407 return (jremref); 4408 } 4409 4410 static inline void 4411 newinoref(struct inoref *inoref, ino_t ino, ino_t parent, off_t diroff, 4412 nlink_t nlink, uint16_t mode) 4413 { 4414 4415 inoref->if_jsegdep = newjsegdep(&inoref->if_list); 4416 inoref->if_diroff = diroff; 4417 inoref->if_ino = ino; 4418 inoref->if_parent = parent; 4419 inoref->if_nlink = nlink; 4420 inoref->if_mode = mode; 4421 } 4422 4423 /* 4424 * Allocate a new jaddref to track the addition of ino to dp at diroff. The 4425 * directory offset may not be known until later. The caller is responsible 4426 * adding the entry to the journal when this information is available. nlink 4427 * should be the link count prior to the addition and mode is only required 4428 * to have the correct FMT. 4429 */ 4430 static struct jaddref * 4431 newjaddref(struct inode *dp, ino_t ino, off_t diroff, int16_t nlink, 4432 uint16_t mode) 4433 { 4434 struct jaddref *jaddref; 4435 4436 jaddref = malloc(sizeof(*jaddref), M_JADDREF, M_SOFTDEP_FLAGS); 4437 workitem_alloc(&jaddref->ja_list, D_JADDREF, ITOVFS(dp)); 4438 jaddref->ja_state = ATTACHED; 4439 jaddref->ja_mkdir = NULL; 4440 newinoref(&jaddref->ja_ref, ino, dp->i_number, diroff, nlink, mode); 4441 4442 return (jaddref); 4443 } 4444 4445 /* 4446 * Create a new free dependency for a freework. The caller is responsible 4447 * for adjusting the reference count when it has the lock held. The freedep 4448 * will track an outstanding bitmap write that will ultimately clear the 4449 * freework to continue. 4450 */ 4451 static struct freedep * 4452 newfreedep(struct freework *freework) 4453 { 4454 struct freedep *freedep; 4455 4456 freedep = malloc(sizeof(*freedep), M_FREEDEP, M_SOFTDEP_FLAGS); 4457 workitem_alloc(&freedep->fd_list, D_FREEDEP, freework->fw_list.wk_mp); 4458 freedep->fd_freework = freework; 4459 4460 return (freedep); 4461 } 4462 4463 /* 4464 * Free a freedep structure once the buffer it is linked to is written. If 4465 * this is the last reference to the freework schedule it for completion. 4466 */ 4467 static void 4468 free_freedep(freedep) 4469 struct freedep *freedep; 4470 { 4471 struct freework *freework; 4472 4473 freework = freedep->fd_freework; 4474 freework->fw_freeblks->fb_cgwait--; 4475 if (--freework->fw_ref == 0) 4476 freework_enqueue(freework); 4477 WORKITEM_FREE(freedep, D_FREEDEP); 4478 } 4479 4480 /* 4481 * Allocate a new freework structure that may be a level in an indirect 4482 * when parent is not NULL or a top level block when it is. The top level 4483 * freework structures are allocated without the per-filesystem lock held 4484 * and before the freeblks is visible outside of softdep_setup_freeblocks(). 4485 */ 4486 static struct freework * 4487 newfreework(ump, freeblks, parent, lbn, nb, frags, off, journal) 4488 struct ufsmount *ump; 4489 struct freeblks *freeblks; 4490 struct freework *parent; 4491 ufs_lbn_t lbn; 4492 ufs2_daddr_t nb; 4493 int frags; 4494 int off; 4495 int journal; 4496 { 4497 struct freework *freework; 4498 4499 freework = malloc(sizeof(*freework), M_FREEWORK, M_SOFTDEP_FLAGS); 4500 workitem_alloc(&freework->fw_list, D_FREEWORK, freeblks->fb_list.wk_mp); 4501 freework->fw_state = ATTACHED; 4502 freework->fw_jnewblk = NULL; 4503 freework->fw_freeblks = freeblks; 4504 freework->fw_parent = parent; 4505 freework->fw_lbn = lbn; 4506 freework->fw_blkno = nb; 4507 freework->fw_frags = frags; 4508 freework->fw_indir = NULL; 4509 freework->fw_ref = (MOUNTEDSUJ(UFSTOVFS(ump)) == 0 || 4510 lbn >= -UFS_NXADDR) ? 0 : NINDIR(ump->um_fs) + 1; 4511 freework->fw_start = freework->fw_off = off; 4512 if (journal) 4513 newjfreeblk(freeblks, lbn, nb, frags); 4514 if (parent == NULL) { 4515 ACQUIRE_LOCK(ump); 4516 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 4517 freeblks->fb_ref++; 4518 FREE_LOCK(ump); 4519 } 4520 4521 return (freework); 4522 } 4523 4524 /* 4525 * Eliminate a jfreeblk for a block that does not need journaling. 4526 */ 4527 static void 4528 cancel_jfreeblk(freeblks, blkno) 4529 struct freeblks *freeblks; 4530 ufs2_daddr_t blkno; 4531 { 4532 struct jfreeblk *jfreeblk; 4533 struct jblkdep *jblkdep; 4534 4535 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) { 4536 if (jblkdep->jb_list.wk_type != D_JFREEBLK) 4537 continue; 4538 jfreeblk = WK_JFREEBLK(&jblkdep->jb_list); 4539 if (jfreeblk->jf_blkno == blkno) 4540 break; 4541 } 4542 if (jblkdep == NULL) 4543 return; 4544 CTR1(KTR_SUJ, "cancel_jfreeblk: blkno %jd", blkno); 4545 free_jsegdep(jblkdep->jb_jsegdep); 4546 LIST_REMOVE(jblkdep, jb_deps); 4547 WORKITEM_FREE(jfreeblk, D_JFREEBLK); 4548 } 4549 4550 /* 4551 * Allocate a new jfreeblk to journal top level block pointer when truncating 4552 * a file. The caller must add this to the worklist when the per-filesystem 4553 * lock is held. 4554 */ 4555 static struct jfreeblk * 4556 newjfreeblk(freeblks, lbn, blkno, frags) 4557 struct freeblks *freeblks; 4558 ufs_lbn_t lbn; 4559 ufs2_daddr_t blkno; 4560 int frags; 4561 { 4562 struct jfreeblk *jfreeblk; 4563 4564 jfreeblk = malloc(sizeof(*jfreeblk), M_JFREEBLK, M_SOFTDEP_FLAGS); 4565 workitem_alloc(&jfreeblk->jf_dep.jb_list, D_JFREEBLK, 4566 freeblks->fb_list.wk_mp); 4567 jfreeblk->jf_dep.jb_jsegdep = newjsegdep(&jfreeblk->jf_dep.jb_list); 4568 jfreeblk->jf_dep.jb_freeblks = freeblks; 4569 jfreeblk->jf_ino = freeblks->fb_inum; 4570 jfreeblk->jf_lbn = lbn; 4571 jfreeblk->jf_blkno = blkno; 4572 jfreeblk->jf_frags = frags; 4573 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jfreeblk->jf_dep, jb_deps); 4574 4575 return (jfreeblk); 4576 } 4577 4578 /* 4579 * The journal is only prepared to handle full-size block numbers, so we 4580 * have to adjust the record to reflect the change to a full-size block. 4581 * For example, suppose we have a block made up of fragments 8-15 and 4582 * want to free its last two fragments. We are given a request that says: 4583 * FREEBLK ino=5, blkno=14, lbn=0, frags=2, oldfrags=0 4584 * where frags are the number of fragments to free and oldfrags are the 4585 * number of fragments to keep. To block align it, we have to change it to 4586 * have a valid full-size blkno, so it becomes: 4587 * FREEBLK ino=5, blkno=8, lbn=0, frags=2, oldfrags=6 4588 */ 4589 static void 4590 adjust_newfreework(freeblks, frag_offset) 4591 struct freeblks *freeblks; 4592 int frag_offset; 4593 { 4594 struct jfreeblk *jfreeblk; 4595 4596 KASSERT((LIST_FIRST(&freeblks->fb_jblkdephd) != NULL && 4597 LIST_FIRST(&freeblks->fb_jblkdephd)->jb_list.wk_type == D_JFREEBLK), 4598 ("adjust_newfreework: Missing freeblks dependency")); 4599 4600 jfreeblk = WK_JFREEBLK(LIST_FIRST(&freeblks->fb_jblkdephd)); 4601 jfreeblk->jf_blkno -= frag_offset; 4602 jfreeblk->jf_frags += frag_offset; 4603 } 4604 4605 /* 4606 * Allocate a new jtrunc to track a partial truncation. 4607 */ 4608 static struct jtrunc * 4609 newjtrunc(freeblks, size, extsize) 4610 struct freeblks *freeblks; 4611 off_t size; 4612 int extsize; 4613 { 4614 struct jtrunc *jtrunc; 4615 4616 jtrunc = malloc(sizeof(*jtrunc), M_JTRUNC, M_SOFTDEP_FLAGS); 4617 workitem_alloc(&jtrunc->jt_dep.jb_list, D_JTRUNC, 4618 freeblks->fb_list.wk_mp); 4619 jtrunc->jt_dep.jb_jsegdep = newjsegdep(&jtrunc->jt_dep.jb_list); 4620 jtrunc->jt_dep.jb_freeblks = freeblks; 4621 jtrunc->jt_ino = freeblks->fb_inum; 4622 jtrunc->jt_size = size; 4623 jtrunc->jt_extsize = extsize; 4624 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jtrunc->jt_dep, jb_deps); 4625 4626 return (jtrunc); 4627 } 4628 4629 /* 4630 * If we're canceling a new bitmap we have to search for another ref 4631 * to move into the bmsafemap dep. This might be better expressed 4632 * with another structure. 4633 */ 4634 static void 4635 move_newblock_dep(jaddref, inodedep) 4636 struct jaddref *jaddref; 4637 struct inodedep *inodedep; 4638 { 4639 struct inoref *inoref; 4640 struct jaddref *jaddrefn; 4641 4642 jaddrefn = NULL; 4643 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4644 inoref = TAILQ_NEXT(inoref, if_deps)) { 4645 if ((jaddref->ja_state & NEWBLOCK) && 4646 inoref->if_list.wk_type == D_JADDREF) { 4647 jaddrefn = (struct jaddref *)inoref; 4648 break; 4649 } 4650 } 4651 if (jaddrefn == NULL) 4652 return; 4653 jaddrefn->ja_state &= ~(ATTACHED | UNDONE); 4654 jaddrefn->ja_state |= jaddref->ja_state & 4655 (ATTACHED | UNDONE | NEWBLOCK); 4656 jaddref->ja_state &= ~(ATTACHED | UNDONE | NEWBLOCK); 4657 jaddref->ja_state |= ATTACHED; 4658 LIST_REMOVE(jaddref, ja_bmdeps); 4659 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_jaddrefhd, jaddrefn, 4660 ja_bmdeps); 4661 } 4662 4663 /* 4664 * Cancel a jaddref either before it has been written or while it is being 4665 * written. This happens when a link is removed before the add reaches 4666 * the disk. The jaddref dependency is kept linked into the bmsafemap 4667 * and inode to prevent the link count or bitmap from reaching the disk 4668 * until handle_workitem_remove() re-adjusts the counts and bitmaps as 4669 * required. 4670 * 4671 * Returns 1 if the canceled addref requires journaling of the remove and 4672 * 0 otherwise. 4673 */ 4674 static int 4675 cancel_jaddref(jaddref, inodedep, wkhd) 4676 struct jaddref *jaddref; 4677 struct inodedep *inodedep; 4678 struct workhead *wkhd; 4679 { 4680 struct inoref *inoref; 4681 struct jsegdep *jsegdep; 4682 int needsj; 4683 4684 KASSERT((jaddref->ja_state & COMPLETE) == 0, 4685 ("cancel_jaddref: Canceling complete jaddref")); 4686 if (jaddref->ja_state & (INPROGRESS | COMPLETE)) 4687 needsj = 1; 4688 else 4689 needsj = 0; 4690 if (inodedep == NULL) 4691 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 4692 0, &inodedep) == 0) 4693 panic("cancel_jaddref: Lost inodedep"); 4694 /* 4695 * We must adjust the nlink of any reference operation that follows 4696 * us so that it is consistent with the in-memory reference. This 4697 * ensures that inode nlink rollbacks always have the correct link. 4698 */ 4699 if (needsj == 0) { 4700 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4701 inoref = TAILQ_NEXT(inoref, if_deps)) { 4702 if (inoref->if_state & GOINGAWAY) 4703 break; 4704 inoref->if_nlink--; 4705 } 4706 } 4707 jsegdep = inoref_jseg(&jaddref->ja_ref); 4708 if (jaddref->ja_state & NEWBLOCK) 4709 move_newblock_dep(jaddref, inodedep); 4710 wake_worklist(&jaddref->ja_list); 4711 jaddref->ja_mkdir = NULL; 4712 if (jaddref->ja_state & INPROGRESS) { 4713 jaddref->ja_state &= ~INPROGRESS; 4714 WORKLIST_REMOVE(&jaddref->ja_list); 4715 jwork_insert(wkhd, jsegdep); 4716 } else { 4717 free_jsegdep(jsegdep); 4718 if (jaddref->ja_state & DEPCOMPLETE) 4719 remove_from_journal(&jaddref->ja_list); 4720 } 4721 jaddref->ja_state |= (GOINGAWAY | DEPCOMPLETE); 4722 /* 4723 * Leave NEWBLOCK jaddrefs on the inodedep so handle_workitem_remove 4724 * can arrange for them to be freed with the bitmap. Otherwise we 4725 * no longer need this addref attached to the inoreflst and it 4726 * will incorrectly adjust nlink if we leave it. 4727 */ 4728 if ((jaddref->ja_state & NEWBLOCK) == 0) { 4729 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 4730 if_deps); 4731 jaddref->ja_state |= COMPLETE; 4732 free_jaddref(jaddref); 4733 return (needsj); 4734 } 4735 /* 4736 * Leave the head of the list for jsegdeps for fast merging. 4737 */ 4738 if (LIST_FIRST(wkhd) != NULL) { 4739 jaddref->ja_state |= ONWORKLIST; 4740 LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list); 4741 } else 4742 WORKLIST_INSERT(wkhd, &jaddref->ja_list); 4743 4744 return (needsj); 4745 } 4746 4747 /* 4748 * Attempt to free a jaddref structure when some work completes. This 4749 * should only succeed once the entry is written and all dependencies have 4750 * been notified. 4751 */ 4752 static void 4753 free_jaddref(jaddref) 4754 struct jaddref *jaddref; 4755 { 4756 4757 if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE) 4758 return; 4759 if (jaddref->ja_ref.if_jsegdep) 4760 panic("free_jaddref: segdep attached to jaddref %p(0x%X)\n", 4761 jaddref, jaddref->ja_state); 4762 if (jaddref->ja_state & NEWBLOCK) 4763 LIST_REMOVE(jaddref, ja_bmdeps); 4764 if (jaddref->ja_state & (INPROGRESS | ONWORKLIST)) 4765 panic("free_jaddref: Bad state %p(0x%X)", 4766 jaddref, jaddref->ja_state); 4767 if (jaddref->ja_mkdir != NULL) 4768 panic("free_jaddref: Work pending, 0x%X\n", jaddref->ja_state); 4769 WORKITEM_FREE(jaddref, D_JADDREF); 4770 } 4771 4772 /* 4773 * Free a jremref structure once it has been written or discarded. 4774 */ 4775 static void 4776 free_jremref(jremref) 4777 struct jremref *jremref; 4778 { 4779 4780 if (jremref->jr_ref.if_jsegdep) 4781 free_jsegdep(jremref->jr_ref.if_jsegdep); 4782 if (jremref->jr_state & INPROGRESS) 4783 panic("free_jremref: IO still pending"); 4784 WORKITEM_FREE(jremref, D_JREMREF); 4785 } 4786 4787 /* 4788 * Free a jnewblk structure. 4789 */ 4790 static void 4791 free_jnewblk(jnewblk) 4792 struct jnewblk *jnewblk; 4793 { 4794 4795 if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE) 4796 return; 4797 LIST_REMOVE(jnewblk, jn_deps); 4798 if (jnewblk->jn_dep != NULL) 4799 panic("free_jnewblk: Dependency still attached."); 4800 WORKITEM_FREE(jnewblk, D_JNEWBLK); 4801 } 4802 4803 /* 4804 * Cancel a jnewblk which has been been made redundant by frag extension. 4805 */ 4806 static void 4807 cancel_jnewblk(jnewblk, wkhd) 4808 struct jnewblk *jnewblk; 4809 struct workhead *wkhd; 4810 { 4811 struct jsegdep *jsegdep; 4812 4813 CTR1(KTR_SUJ, "cancel_jnewblk: blkno %jd", jnewblk->jn_blkno); 4814 jsegdep = jnewblk->jn_jsegdep; 4815 if (jnewblk->jn_jsegdep == NULL || jnewblk->jn_dep == NULL) 4816 panic("cancel_jnewblk: Invalid state"); 4817 jnewblk->jn_jsegdep = NULL; 4818 jnewblk->jn_dep = NULL; 4819 jnewblk->jn_state |= GOINGAWAY; 4820 if (jnewblk->jn_state & INPROGRESS) { 4821 jnewblk->jn_state &= ~INPROGRESS; 4822 WORKLIST_REMOVE(&jnewblk->jn_list); 4823 jwork_insert(wkhd, jsegdep); 4824 } else { 4825 free_jsegdep(jsegdep); 4826 remove_from_journal(&jnewblk->jn_list); 4827 } 4828 wake_worklist(&jnewblk->jn_list); 4829 WORKLIST_INSERT(wkhd, &jnewblk->jn_list); 4830 } 4831 4832 static void 4833 free_jblkdep(jblkdep) 4834 struct jblkdep *jblkdep; 4835 { 4836 4837 if (jblkdep->jb_list.wk_type == D_JFREEBLK) 4838 WORKITEM_FREE(jblkdep, D_JFREEBLK); 4839 else if (jblkdep->jb_list.wk_type == D_JTRUNC) 4840 WORKITEM_FREE(jblkdep, D_JTRUNC); 4841 else 4842 panic("free_jblkdep: Unexpected type %s", 4843 TYPENAME(jblkdep->jb_list.wk_type)); 4844 } 4845 4846 /* 4847 * Free a single jseg once it is no longer referenced in memory or on 4848 * disk. Reclaim journal blocks and dependencies waiting for the segment 4849 * to disappear. 4850 */ 4851 static void 4852 free_jseg(jseg, jblocks) 4853 struct jseg *jseg; 4854 struct jblocks *jblocks; 4855 { 4856 struct freework *freework; 4857 4858 /* 4859 * Free freework structures that were lingering to indicate freed 4860 * indirect blocks that forced journal write ordering on reallocate. 4861 */ 4862 while ((freework = LIST_FIRST(&jseg->js_indirs)) != NULL) 4863 indirblk_remove(freework); 4864 if (jblocks->jb_oldestseg == jseg) 4865 jblocks->jb_oldestseg = TAILQ_NEXT(jseg, js_next); 4866 TAILQ_REMOVE(&jblocks->jb_segs, jseg, js_next); 4867 jblocks_free(jblocks, jseg->js_list.wk_mp, jseg->js_size); 4868 KASSERT(LIST_EMPTY(&jseg->js_entries), 4869 ("free_jseg: Freed jseg has valid entries.")); 4870 WORKITEM_FREE(jseg, D_JSEG); 4871 } 4872 4873 /* 4874 * Free all jsegs that meet the criteria for being reclaimed and update 4875 * oldestseg. 4876 */ 4877 static void 4878 free_jsegs(jblocks) 4879 struct jblocks *jblocks; 4880 { 4881 struct jseg *jseg; 4882 4883 /* 4884 * Free only those jsegs which have none allocated before them to 4885 * preserve the journal space ordering. 4886 */ 4887 while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) { 4888 /* 4889 * Only reclaim space when nothing depends on this journal 4890 * set and another set has written that it is no longer 4891 * valid. 4892 */ 4893 if (jseg->js_refs != 0) { 4894 jblocks->jb_oldestseg = jseg; 4895 return; 4896 } 4897 if ((jseg->js_state & ALLCOMPLETE) != ALLCOMPLETE) 4898 break; 4899 if (jseg->js_seq > jblocks->jb_oldestwrseq) 4900 break; 4901 /* 4902 * We can free jsegs that didn't write entries when 4903 * oldestwrseq == js_seq. 4904 */ 4905 if (jseg->js_seq == jblocks->jb_oldestwrseq && 4906 jseg->js_cnt != 0) 4907 break; 4908 free_jseg(jseg, jblocks); 4909 } 4910 /* 4911 * If we exited the loop above we still must discover the 4912 * oldest valid segment. 4913 */ 4914 if (jseg) 4915 for (jseg = jblocks->jb_oldestseg; jseg != NULL; 4916 jseg = TAILQ_NEXT(jseg, js_next)) 4917 if (jseg->js_refs != 0) 4918 break; 4919 jblocks->jb_oldestseg = jseg; 4920 /* 4921 * The journal has no valid records but some jsegs may still be 4922 * waiting on oldestwrseq to advance. We force a small record 4923 * out to permit these lingering records to be reclaimed. 4924 */ 4925 if (jblocks->jb_oldestseg == NULL && !TAILQ_EMPTY(&jblocks->jb_segs)) 4926 jblocks->jb_needseg = 1; 4927 } 4928 4929 /* 4930 * Release one reference to a jseg and free it if the count reaches 0. This 4931 * should eventually reclaim journal space as well. 4932 */ 4933 static void 4934 rele_jseg(jseg) 4935 struct jseg *jseg; 4936 { 4937 4938 KASSERT(jseg->js_refs > 0, 4939 ("free_jseg: Invalid refcnt %d", jseg->js_refs)); 4940 if (--jseg->js_refs != 0) 4941 return; 4942 free_jsegs(jseg->js_jblocks); 4943 } 4944 4945 /* 4946 * Release a jsegdep and decrement the jseg count. 4947 */ 4948 static void 4949 free_jsegdep(jsegdep) 4950 struct jsegdep *jsegdep; 4951 { 4952 4953 if (jsegdep->jd_seg) 4954 rele_jseg(jsegdep->jd_seg); 4955 WORKITEM_FREE(jsegdep, D_JSEGDEP); 4956 } 4957 4958 /* 4959 * Wait for a journal item to make it to disk. Initiate journal processing 4960 * if required. 4961 */ 4962 static int 4963 jwait(wk, waitfor) 4964 struct worklist *wk; 4965 int waitfor; 4966 { 4967 4968 LOCK_OWNED(VFSTOUFS(wk->wk_mp)); 4969 /* 4970 * Blocking journal waits cause slow synchronous behavior. Record 4971 * stats on the frequency of these blocking operations. 4972 */ 4973 if (waitfor == MNT_WAIT) { 4974 stat_journal_wait++; 4975 switch (wk->wk_type) { 4976 case D_JREMREF: 4977 case D_JMVREF: 4978 stat_jwait_filepage++; 4979 break; 4980 case D_JTRUNC: 4981 case D_JFREEBLK: 4982 stat_jwait_freeblks++; 4983 break; 4984 case D_JNEWBLK: 4985 stat_jwait_newblk++; 4986 break; 4987 case D_JADDREF: 4988 stat_jwait_inode++; 4989 break; 4990 default: 4991 break; 4992 } 4993 } 4994 /* 4995 * If IO has not started we process the journal. We can't mark the 4996 * worklist item as IOWAITING because we drop the lock while 4997 * processing the journal and the worklist entry may be freed after 4998 * this point. The caller may call back in and re-issue the request. 4999 */ 5000 if ((wk->wk_state & INPROGRESS) == 0) { 5001 softdep_process_journal(wk->wk_mp, wk, waitfor); 5002 if (waitfor != MNT_WAIT) 5003 return (EBUSY); 5004 return (0); 5005 } 5006 if (waitfor != MNT_WAIT) 5007 return (EBUSY); 5008 wait_worklist(wk, "jwait"); 5009 return (0); 5010 } 5011 5012 /* 5013 * Lookup an inodedep based on an inode pointer and set the nlinkdelta as 5014 * appropriate. This is a convenience function to reduce duplicate code 5015 * for the setup and revert functions below. 5016 */ 5017 static struct inodedep * 5018 inodedep_lookup_ip(ip) 5019 struct inode *ip; 5020 { 5021 struct inodedep *inodedep; 5022 5023 KASSERT(ip->i_nlink >= ip->i_effnlink, 5024 ("inodedep_lookup_ip: bad delta")); 5025 (void) inodedep_lookup(ITOVFS(ip), ip->i_number, DEPALLOC, 5026 &inodedep); 5027 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 5028 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 5029 5030 return (inodedep); 5031 } 5032 5033 /* 5034 * Called prior to creating a new inode and linking it to a directory. The 5035 * jaddref structure must already be allocated by softdep_setup_inomapdep 5036 * and it is discovered here so we can initialize the mode and update 5037 * nlinkdelta. 5038 */ 5039 void 5040 softdep_setup_create(dp, ip) 5041 struct inode *dp; 5042 struct inode *ip; 5043 { 5044 struct inodedep *inodedep; 5045 struct jaddref *jaddref; 5046 struct vnode *dvp; 5047 5048 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5049 ("softdep_setup_create called on non-softdep filesystem")); 5050 KASSERT(ip->i_nlink == 1, 5051 ("softdep_setup_create: Invalid link count.")); 5052 dvp = ITOV(dp); 5053 ACQUIRE_LOCK(ITOUMP(dp)); 5054 inodedep = inodedep_lookup_ip(ip); 5055 if (DOINGSUJ(dvp)) { 5056 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 5057 inoreflst); 5058 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 5059 ("softdep_setup_create: No addref structure present.")); 5060 } 5061 FREE_LOCK(ITOUMP(dp)); 5062 } 5063 5064 /* 5065 * Create a jaddref structure to track the addition of a DOTDOT link when 5066 * we are reparenting an inode as part of a rename. This jaddref will be 5067 * found by softdep_setup_directory_change. Adjusts nlinkdelta for 5068 * non-journaling softdep. 5069 */ 5070 void 5071 softdep_setup_dotdot_link(dp, ip) 5072 struct inode *dp; 5073 struct inode *ip; 5074 { 5075 struct inodedep *inodedep; 5076 struct jaddref *jaddref; 5077 struct vnode *dvp; 5078 5079 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5080 ("softdep_setup_dotdot_link called on non-softdep filesystem")); 5081 dvp = ITOV(dp); 5082 jaddref = NULL; 5083 /* 5084 * We don't set MKDIR_PARENT as this is not tied to a mkdir and 5085 * is used as a normal link would be. 5086 */ 5087 if (DOINGSUJ(dvp)) 5088 jaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 5089 dp->i_effnlink - 1, dp->i_mode); 5090 ACQUIRE_LOCK(ITOUMP(dp)); 5091 inodedep = inodedep_lookup_ip(dp); 5092 if (jaddref) 5093 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 5094 if_deps); 5095 FREE_LOCK(ITOUMP(dp)); 5096 } 5097 5098 /* 5099 * Create a jaddref structure to track a new link to an inode. The directory 5100 * offset is not known until softdep_setup_directory_add or 5101 * softdep_setup_directory_change. Adjusts nlinkdelta for non-journaling 5102 * softdep. 5103 */ 5104 void 5105 softdep_setup_link(dp, ip) 5106 struct inode *dp; 5107 struct inode *ip; 5108 { 5109 struct inodedep *inodedep; 5110 struct jaddref *jaddref; 5111 struct vnode *dvp; 5112 5113 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5114 ("softdep_setup_link called on non-softdep filesystem")); 5115 dvp = ITOV(dp); 5116 jaddref = NULL; 5117 if (DOINGSUJ(dvp)) 5118 jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1, 5119 ip->i_mode); 5120 ACQUIRE_LOCK(ITOUMP(dp)); 5121 inodedep = inodedep_lookup_ip(ip); 5122 if (jaddref) 5123 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 5124 if_deps); 5125 FREE_LOCK(ITOUMP(dp)); 5126 } 5127 5128 /* 5129 * Called to create the jaddref structures to track . and .. references as 5130 * well as lookup and further initialize the incomplete jaddref created 5131 * by softdep_setup_inomapdep when the inode was allocated. Adjusts 5132 * nlinkdelta for non-journaling softdep. 5133 */ 5134 void 5135 softdep_setup_mkdir(dp, ip) 5136 struct inode *dp; 5137 struct inode *ip; 5138 { 5139 struct inodedep *inodedep; 5140 struct jaddref *dotdotaddref; 5141 struct jaddref *dotaddref; 5142 struct jaddref *jaddref; 5143 struct vnode *dvp; 5144 5145 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5146 ("softdep_setup_mkdir called on non-softdep filesystem")); 5147 dvp = ITOV(dp); 5148 dotaddref = dotdotaddref = NULL; 5149 if (DOINGSUJ(dvp)) { 5150 dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1, 5151 ip->i_mode); 5152 dotaddref->ja_state |= MKDIR_BODY; 5153 dotdotaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 5154 dp->i_effnlink - 1, dp->i_mode); 5155 dotdotaddref->ja_state |= MKDIR_PARENT; 5156 } 5157 ACQUIRE_LOCK(ITOUMP(dp)); 5158 inodedep = inodedep_lookup_ip(ip); 5159 if (DOINGSUJ(dvp)) { 5160 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 5161 inoreflst); 5162 KASSERT(jaddref != NULL, 5163 ("softdep_setup_mkdir: No addref structure present.")); 5164 KASSERT(jaddref->ja_parent == dp->i_number, 5165 ("softdep_setup_mkdir: bad parent %ju", 5166 (uintmax_t)jaddref->ja_parent)); 5167 TAILQ_INSERT_BEFORE(&jaddref->ja_ref, &dotaddref->ja_ref, 5168 if_deps); 5169 } 5170 inodedep = inodedep_lookup_ip(dp); 5171 if (DOINGSUJ(dvp)) 5172 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, 5173 &dotdotaddref->ja_ref, if_deps); 5174 FREE_LOCK(ITOUMP(dp)); 5175 } 5176 5177 /* 5178 * Called to track nlinkdelta of the inode and parent directories prior to 5179 * unlinking a directory. 5180 */ 5181 void 5182 softdep_setup_rmdir(dp, ip) 5183 struct inode *dp; 5184 struct inode *ip; 5185 { 5186 struct vnode *dvp; 5187 5188 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5189 ("softdep_setup_rmdir called on non-softdep filesystem")); 5190 dvp = ITOV(dp); 5191 ACQUIRE_LOCK(ITOUMP(dp)); 5192 (void) inodedep_lookup_ip(ip); 5193 (void) inodedep_lookup_ip(dp); 5194 FREE_LOCK(ITOUMP(dp)); 5195 } 5196 5197 /* 5198 * Called to track nlinkdelta of the inode and parent directories prior to 5199 * unlink. 5200 */ 5201 void 5202 softdep_setup_unlink(dp, ip) 5203 struct inode *dp; 5204 struct inode *ip; 5205 { 5206 struct vnode *dvp; 5207 5208 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5209 ("softdep_setup_unlink called on non-softdep filesystem")); 5210 dvp = ITOV(dp); 5211 ACQUIRE_LOCK(ITOUMP(dp)); 5212 (void) inodedep_lookup_ip(ip); 5213 (void) inodedep_lookup_ip(dp); 5214 FREE_LOCK(ITOUMP(dp)); 5215 } 5216 5217 /* 5218 * Called to release the journal structures created by a failed non-directory 5219 * creation. Adjusts nlinkdelta for non-journaling softdep. 5220 */ 5221 void 5222 softdep_revert_create(dp, ip) 5223 struct inode *dp; 5224 struct inode *ip; 5225 { 5226 struct inodedep *inodedep; 5227 struct jaddref *jaddref; 5228 struct vnode *dvp; 5229 5230 KASSERT(MOUNTEDSOFTDEP(ITOVFS((dp))) != 0, 5231 ("softdep_revert_create called on non-softdep filesystem")); 5232 dvp = ITOV(dp); 5233 ACQUIRE_LOCK(ITOUMP(dp)); 5234 inodedep = inodedep_lookup_ip(ip); 5235 if (DOINGSUJ(dvp)) { 5236 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 5237 inoreflst); 5238 KASSERT(jaddref->ja_parent == dp->i_number, 5239 ("softdep_revert_create: addref parent mismatch")); 5240 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 5241 } 5242 FREE_LOCK(ITOUMP(dp)); 5243 } 5244 5245 /* 5246 * Called to release the journal structures created by a failed link 5247 * addition. Adjusts nlinkdelta for non-journaling softdep. 5248 */ 5249 void 5250 softdep_revert_link(dp, ip) 5251 struct inode *dp; 5252 struct inode *ip; 5253 { 5254 struct inodedep *inodedep; 5255 struct jaddref *jaddref; 5256 struct vnode *dvp; 5257 5258 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5259 ("softdep_revert_link called on non-softdep filesystem")); 5260 dvp = ITOV(dp); 5261 ACQUIRE_LOCK(ITOUMP(dp)); 5262 inodedep = inodedep_lookup_ip(ip); 5263 if (DOINGSUJ(dvp)) { 5264 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 5265 inoreflst); 5266 KASSERT(jaddref->ja_parent == dp->i_number, 5267 ("softdep_revert_link: addref parent mismatch")); 5268 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 5269 } 5270 FREE_LOCK(ITOUMP(dp)); 5271 } 5272 5273 /* 5274 * Called to release the journal structures created by a failed mkdir 5275 * attempt. Adjusts nlinkdelta for non-journaling softdep. 5276 */ 5277 void 5278 softdep_revert_mkdir(dp, ip) 5279 struct inode *dp; 5280 struct inode *ip; 5281 { 5282 struct inodedep *inodedep; 5283 struct jaddref *jaddref; 5284 struct jaddref *dotaddref; 5285 struct vnode *dvp; 5286 5287 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5288 ("softdep_revert_mkdir called on non-softdep filesystem")); 5289 dvp = ITOV(dp); 5290 5291 ACQUIRE_LOCK(ITOUMP(dp)); 5292 inodedep = inodedep_lookup_ip(dp); 5293 if (DOINGSUJ(dvp)) { 5294 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 5295 inoreflst); 5296 KASSERT(jaddref->ja_parent == ip->i_number, 5297 ("softdep_revert_mkdir: dotdot addref parent mismatch")); 5298 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 5299 } 5300 inodedep = inodedep_lookup_ip(ip); 5301 if (DOINGSUJ(dvp)) { 5302 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 5303 inoreflst); 5304 KASSERT(jaddref->ja_parent == dp->i_number, 5305 ("softdep_revert_mkdir: addref parent mismatch")); 5306 dotaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 5307 inoreflst, if_deps); 5308 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 5309 KASSERT(dotaddref->ja_parent == ip->i_number, 5310 ("softdep_revert_mkdir: dot addref parent mismatch")); 5311 cancel_jaddref(dotaddref, inodedep, &inodedep->id_inowait); 5312 } 5313 FREE_LOCK(ITOUMP(dp)); 5314 } 5315 5316 /* 5317 * Called to correct nlinkdelta after a failed rmdir. 5318 */ 5319 void 5320 softdep_revert_rmdir(dp, ip) 5321 struct inode *dp; 5322 struct inode *ip; 5323 { 5324 5325 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5326 ("softdep_revert_rmdir called on non-softdep filesystem")); 5327 ACQUIRE_LOCK(ITOUMP(dp)); 5328 (void) inodedep_lookup_ip(ip); 5329 (void) inodedep_lookup_ip(dp); 5330 FREE_LOCK(ITOUMP(dp)); 5331 } 5332 5333 /* 5334 * Protecting the freemaps (or bitmaps). 5335 * 5336 * To eliminate the need to execute fsck before mounting a filesystem 5337 * after a power failure, one must (conservatively) guarantee that the 5338 * on-disk copy of the bitmaps never indicate that a live inode or block is 5339 * free. So, when a block or inode is allocated, the bitmap should be 5340 * updated (on disk) before any new pointers. When a block or inode is 5341 * freed, the bitmap should not be updated until all pointers have been 5342 * reset. The latter dependency is handled by the delayed de-allocation 5343 * approach described below for block and inode de-allocation. The former 5344 * dependency is handled by calling the following procedure when a block or 5345 * inode is allocated. When an inode is allocated an "inodedep" is created 5346 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk. 5347 * Each "inodedep" is also inserted into the hash indexing structure so 5348 * that any additional link additions can be made dependent on the inode 5349 * allocation. 5350 * 5351 * The ufs filesystem maintains a number of free block counts (e.g., per 5352 * cylinder group, per cylinder and per <cylinder, rotational position> pair) 5353 * in addition to the bitmaps. These counts are used to improve efficiency 5354 * during allocation and therefore must be consistent with the bitmaps. 5355 * There is no convenient way to guarantee post-crash consistency of these 5356 * counts with simple update ordering, for two main reasons: (1) The counts 5357 * and bitmaps for a single cylinder group block are not in the same disk 5358 * sector. If a disk write is interrupted (e.g., by power failure), one may 5359 * be written and the other not. (2) Some of the counts are located in the 5360 * superblock rather than the cylinder group block. So, we focus our soft 5361 * updates implementation on protecting the bitmaps. When mounting a 5362 * filesystem, we recompute the auxiliary counts from the bitmaps. 5363 */ 5364 5365 /* 5366 * Called just after updating the cylinder group block to allocate an inode. 5367 */ 5368 void 5369 softdep_setup_inomapdep(bp, ip, newinum, mode) 5370 struct buf *bp; /* buffer for cylgroup block with inode map */ 5371 struct inode *ip; /* inode related to allocation */ 5372 ino_t newinum; /* new inode number being allocated */ 5373 int mode; 5374 { 5375 struct inodedep *inodedep; 5376 struct bmsafemap *bmsafemap; 5377 struct jaddref *jaddref; 5378 struct mount *mp; 5379 struct fs *fs; 5380 5381 mp = ITOVFS(ip); 5382 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5383 ("softdep_setup_inomapdep called on non-softdep filesystem")); 5384 fs = VFSTOUFS(mp)->um_fs; 5385 jaddref = NULL; 5386 5387 /* 5388 * Allocate the journal reference add structure so that the bitmap 5389 * can be dependent on it. 5390 */ 5391 if (MOUNTEDSUJ(mp)) { 5392 jaddref = newjaddref(ip, newinum, 0, 0, mode); 5393 jaddref->ja_state |= NEWBLOCK; 5394 } 5395 5396 /* 5397 * Create a dependency for the newly allocated inode. 5398 * Panic if it already exists as something is seriously wrong. 5399 * Otherwise add it to the dependency list for the buffer holding 5400 * the cylinder group map from which it was allocated. 5401 * 5402 * We have to preallocate a bmsafemap entry in case it is needed 5403 * in bmsafemap_lookup since once we allocate the inodedep, we 5404 * have to finish initializing it before we can FREE_LOCK(). 5405 * By preallocating, we avoid FREE_LOCK() while doing a malloc 5406 * in bmsafemap_lookup. We cannot call bmsafemap_lookup before 5407 * creating the inodedep as it can be freed during the time 5408 * that we FREE_LOCK() while allocating the inodedep. We must 5409 * call workitem_alloc() before entering the locked section as 5410 * it also acquires the lock and we must avoid trying doing so 5411 * recursively. 5412 */ 5413 bmsafemap = malloc(sizeof(struct bmsafemap), 5414 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5415 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5416 ACQUIRE_LOCK(ITOUMP(ip)); 5417 if ((inodedep_lookup(mp, newinum, DEPALLOC, &inodedep))) 5418 panic("softdep_setup_inomapdep: dependency %p for new" 5419 "inode already exists", inodedep); 5420 bmsafemap = bmsafemap_lookup(mp, bp, ino_to_cg(fs, newinum), bmsafemap); 5421 if (jaddref) { 5422 LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps); 5423 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 5424 if_deps); 5425 } else { 5426 inodedep->id_state |= ONDEPLIST; 5427 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps); 5428 } 5429 inodedep->id_bmsafemap = bmsafemap; 5430 inodedep->id_state &= ~DEPCOMPLETE; 5431 FREE_LOCK(ITOUMP(ip)); 5432 } 5433 5434 /* 5435 * Called just after updating the cylinder group block to 5436 * allocate block or fragment. 5437 */ 5438 void 5439 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags) 5440 struct buf *bp; /* buffer for cylgroup block with block map */ 5441 struct mount *mp; /* filesystem doing allocation */ 5442 ufs2_daddr_t newblkno; /* number of newly allocated block */ 5443 int frags; /* Number of fragments. */ 5444 int oldfrags; /* Previous number of fragments for extend. */ 5445 { 5446 struct newblk *newblk; 5447 struct bmsafemap *bmsafemap; 5448 struct jnewblk *jnewblk; 5449 struct ufsmount *ump; 5450 struct fs *fs; 5451 5452 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5453 ("softdep_setup_blkmapdep called on non-softdep filesystem")); 5454 ump = VFSTOUFS(mp); 5455 fs = ump->um_fs; 5456 jnewblk = NULL; 5457 /* 5458 * Create a dependency for the newly allocated block. 5459 * Add it to the dependency list for the buffer holding 5460 * the cylinder group map from which it was allocated. 5461 */ 5462 if (MOUNTEDSUJ(mp)) { 5463 jnewblk = malloc(sizeof(*jnewblk), M_JNEWBLK, M_SOFTDEP_FLAGS); 5464 workitem_alloc(&jnewblk->jn_list, D_JNEWBLK, mp); 5465 jnewblk->jn_jsegdep = newjsegdep(&jnewblk->jn_list); 5466 jnewblk->jn_state = ATTACHED; 5467 jnewblk->jn_blkno = newblkno; 5468 jnewblk->jn_frags = frags; 5469 jnewblk->jn_oldfrags = oldfrags; 5470 #ifdef INVARIANTS 5471 { 5472 struct cg *cgp; 5473 uint8_t *blksfree; 5474 long bno; 5475 int i; 5476 5477 cgp = (struct cg *)bp->b_data; 5478 blksfree = cg_blksfree(cgp); 5479 bno = dtogd(fs, jnewblk->jn_blkno); 5480 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; 5481 i++) { 5482 if (isset(blksfree, bno + i)) 5483 panic("softdep_setup_blkmapdep: " 5484 "free fragment %d from %d-%d " 5485 "state 0x%X dep %p", i, 5486 jnewblk->jn_oldfrags, 5487 jnewblk->jn_frags, 5488 jnewblk->jn_state, 5489 jnewblk->jn_dep); 5490 } 5491 } 5492 #endif 5493 } 5494 5495 CTR3(KTR_SUJ, 5496 "softdep_setup_blkmapdep: blkno %jd frags %d oldfrags %d", 5497 newblkno, frags, oldfrags); 5498 ACQUIRE_LOCK(ump); 5499 if (newblk_lookup(mp, newblkno, DEPALLOC, &newblk) != 0) 5500 panic("softdep_setup_blkmapdep: found block"); 5501 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp, 5502 dtog(fs, newblkno), NULL); 5503 if (jnewblk) { 5504 jnewblk->jn_dep = (struct worklist *)newblk; 5505 LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps); 5506 } else { 5507 newblk->nb_state |= ONDEPLIST; 5508 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps); 5509 } 5510 newblk->nb_bmsafemap = bmsafemap; 5511 newblk->nb_jnewblk = jnewblk; 5512 FREE_LOCK(ump); 5513 } 5514 5515 #define BMSAFEMAP_HASH(ump, cg) \ 5516 (&(ump)->bmsafemap_hashtbl[(cg) & (ump)->bmsafemap_hash_size]) 5517 5518 static int 5519 bmsafemap_find(bmsafemaphd, cg, bmsafemapp) 5520 struct bmsafemap_hashhead *bmsafemaphd; 5521 int cg; 5522 struct bmsafemap **bmsafemapp; 5523 { 5524 struct bmsafemap *bmsafemap; 5525 5526 LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash) 5527 if (bmsafemap->sm_cg == cg) 5528 break; 5529 if (bmsafemap) { 5530 *bmsafemapp = bmsafemap; 5531 return (1); 5532 } 5533 *bmsafemapp = NULL; 5534 5535 return (0); 5536 } 5537 5538 /* 5539 * Find the bmsafemap associated with a cylinder group buffer. 5540 * If none exists, create one. The buffer must be locked when 5541 * this routine is called and this routine must be called with 5542 * the softdep lock held. To avoid giving up the lock while 5543 * allocating a new bmsafemap, a preallocated bmsafemap may be 5544 * provided. If it is provided but not needed, it is freed. 5545 */ 5546 static struct bmsafemap * 5547 bmsafemap_lookup(mp, bp, cg, newbmsafemap) 5548 struct mount *mp; 5549 struct buf *bp; 5550 int cg; 5551 struct bmsafemap *newbmsafemap; 5552 { 5553 struct bmsafemap_hashhead *bmsafemaphd; 5554 struct bmsafemap *bmsafemap, *collision; 5555 struct worklist *wk; 5556 struct ufsmount *ump; 5557 5558 ump = VFSTOUFS(mp); 5559 LOCK_OWNED(ump); 5560 KASSERT(bp != NULL, ("bmsafemap_lookup: missing buffer")); 5561 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 5562 if (wk->wk_type == D_BMSAFEMAP) { 5563 if (newbmsafemap) 5564 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5565 return (WK_BMSAFEMAP(wk)); 5566 } 5567 } 5568 bmsafemaphd = BMSAFEMAP_HASH(ump, cg); 5569 if (bmsafemap_find(bmsafemaphd, cg, &bmsafemap) == 1) { 5570 if (newbmsafemap) 5571 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5572 return (bmsafemap); 5573 } 5574 if (newbmsafemap) { 5575 bmsafemap = newbmsafemap; 5576 } else { 5577 FREE_LOCK(ump); 5578 bmsafemap = malloc(sizeof(struct bmsafemap), 5579 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5580 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5581 ACQUIRE_LOCK(ump); 5582 } 5583 bmsafemap->sm_buf = bp; 5584 LIST_INIT(&bmsafemap->sm_inodedephd); 5585 LIST_INIT(&bmsafemap->sm_inodedepwr); 5586 LIST_INIT(&bmsafemap->sm_newblkhd); 5587 LIST_INIT(&bmsafemap->sm_newblkwr); 5588 LIST_INIT(&bmsafemap->sm_jaddrefhd); 5589 LIST_INIT(&bmsafemap->sm_jnewblkhd); 5590 LIST_INIT(&bmsafemap->sm_freehd); 5591 LIST_INIT(&bmsafemap->sm_freewr); 5592 if (bmsafemap_find(bmsafemaphd, cg, &collision) == 1) { 5593 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 5594 return (collision); 5595 } 5596 bmsafemap->sm_cg = cg; 5597 LIST_INSERT_HEAD(bmsafemaphd, bmsafemap, sm_hash); 5598 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 5599 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list); 5600 return (bmsafemap); 5601 } 5602 5603 /* 5604 * Direct block allocation dependencies. 5605 * 5606 * When a new block is allocated, the corresponding disk locations must be 5607 * initialized (with zeros or new data) before the on-disk inode points to 5608 * them. Also, the freemap from which the block was allocated must be 5609 * updated (on disk) before the inode's pointer. These two dependencies are 5610 * independent of each other and are needed for all file blocks and indirect 5611 * blocks that are pointed to directly by the inode. Just before the 5612 * "in-core" version of the inode is updated with a newly allocated block 5613 * number, a procedure (below) is called to setup allocation dependency 5614 * structures. These structures are removed when the corresponding 5615 * dependencies are satisfied or when the block allocation becomes obsolete 5616 * (i.e., the file is deleted, the block is de-allocated, or the block is a 5617 * fragment that gets upgraded). All of these cases are handled in 5618 * procedures described later. 5619 * 5620 * When a file extension causes a fragment to be upgraded, either to a larger 5621 * fragment or to a full block, the on-disk location may change (if the 5622 * previous fragment could not simply be extended). In this case, the old 5623 * fragment must be de-allocated, but not until after the inode's pointer has 5624 * been updated. In most cases, this is handled by later procedures, which 5625 * will construct a "freefrag" structure to be added to the workitem queue 5626 * when the inode update is complete (or obsolete). The main exception to 5627 * this is when an allocation occurs while a pending allocation dependency 5628 * (for the same block pointer) remains. This case is handled in the main 5629 * allocation dependency setup procedure by immediately freeing the 5630 * unreferenced fragments. 5631 */ 5632 void 5633 softdep_setup_allocdirect(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 5634 struct inode *ip; /* inode to which block is being added */ 5635 ufs_lbn_t off; /* block pointer within inode */ 5636 ufs2_daddr_t newblkno; /* disk block number being added */ 5637 ufs2_daddr_t oldblkno; /* previous block number, 0 unless frag */ 5638 long newsize; /* size of new block */ 5639 long oldsize; /* size of new block */ 5640 struct buf *bp; /* bp for allocated block */ 5641 { 5642 struct allocdirect *adp, *oldadp; 5643 struct allocdirectlst *adphead; 5644 struct freefrag *freefrag; 5645 struct inodedep *inodedep; 5646 struct pagedep *pagedep; 5647 struct jnewblk *jnewblk; 5648 struct newblk *newblk; 5649 struct mount *mp; 5650 ufs_lbn_t lbn; 5651 5652 lbn = bp->b_lblkno; 5653 mp = ITOVFS(ip); 5654 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5655 ("softdep_setup_allocdirect called on non-softdep filesystem")); 5656 if (oldblkno && oldblkno != newblkno) 5657 /* 5658 * The usual case is that a smaller fragment that 5659 * was just allocated has been replaced with a bigger 5660 * fragment or a full-size block. If it is marked as 5661 * B_DELWRI, the current contents have not been written 5662 * to disk. It is possible that the block was written 5663 * earlier, but very uncommon. If the block has never 5664 * been written, there is no need to send a BIO_DELETE 5665 * for it when it is freed. The gain from avoiding the 5666 * TRIMs for the common case of unwritten blocks far 5667 * exceeds the cost of the write amplification for the 5668 * uncommon case of failing to send a TRIM for a block 5669 * that had been written. 5670 */ 5671 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn, 5672 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY); 5673 else 5674 freefrag = NULL; 5675 5676 CTR6(KTR_SUJ, 5677 "softdep_setup_allocdirect: ino %d blkno %jd oldblkno %jd " 5678 "off %jd newsize %ld oldsize %d", 5679 ip->i_number, newblkno, oldblkno, off, newsize, oldsize); 5680 ACQUIRE_LOCK(ITOUMP(ip)); 5681 if (off >= UFS_NDADDR) { 5682 if (lbn > 0) 5683 panic("softdep_setup_allocdirect: bad lbn %jd, off %jd", 5684 lbn, off); 5685 /* allocating an indirect block */ 5686 if (oldblkno != 0) 5687 panic("softdep_setup_allocdirect: non-zero indir"); 5688 } else { 5689 if (off != lbn) 5690 panic("softdep_setup_allocdirect: lbn %jd != off %jd", 5691 lbn, off); 5692 /* 5693 * Allocating a direct block. 5694 * 5695 * If we are allocating a directory block, then we must 5696 * allocate an associated pagedep to track additions and 5697 * deletions. 5698 */ 5699 if ((ip->i_mode & IFMT) == IFDIR) 5700 pagedep_lookup(mp, bp, ip->i_number, off, DEPALLOC, 5701 &pagedep); 5702 } 5703 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5704 panic("softdep_setup_allocdirect: lost block"); 5705 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5706 ("softdep_setup_allocdirect: newblk already initialized")); 5707 /* 5708 * Convert the newblk to an allocdirect. 5709 */ 5710 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT); 5711 adp = (struct allocdirect *)newblk; 5712 newblk->nb_freefrag = freefrag; 5713 adp->ad_offset = off; 5714 adp->ad_oldblkno = oldblkno; 5715 adp->ad_newsize = newsize; 5716 adp->ad_oldsize = oldsize; 5717 5718 /* 5719 * Finish initializing the journal. 5720 */ 5721 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5722 jnewblk->jn_ino = ip->i_number; 5723 jnewblk->jn_lbn = lbn; 5724 add_to_journal(&jnewblk->jn_list); 5725 } 5726 if (freefrag && freefrag->ff_jdep != NULL && 5727 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5728 add_to_journal(freefrag->ff_jdep); 5729 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 5730 adp->ad_inodedep = inodedep; 5731 5732 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5733 /* 5734 * The list of allocdirects must be kept in sorted and ascending 5735 * order so that the rollback routines can quickly determine the 5736 * first uncommitted block (the size of the file stored on disk 5737 * ends at the end of the lowest committed fragment, or if there 5738 * are no fragments, at the end of the highest committed block). 5739 * Since files generally grow, the typical case is that the new 5740 * block is to be added at the end of the list. We speed this 5741 * special case by checking against the last allocdirect in the 5742 * list before laboriously traversing the list looking for the 5743 * insertion point. 5744 */ 5745 adphead = &inodedep->id_newinoupdt; 5746 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5747 if (oldadp == NULL || oldadp->ad_offset <= off) { 5748 /* insert at end of list */ 5749 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5750 if (oldadp != NULL && oldadp->ad_offset == off) 5751 allocdirect_merge(adphead, adp, oldadp); 5752 FREE_LOCK(ITOUMP(ip)); 5753 return; 5754 } 5755 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5756 if (oldadp->ad_offset >= off) 5757 break; 5758 } 5759 if (oldadp == NULL) 5760 panic("softdep_setup_allocdirect: lost entry"); 5761 /* insert in middle of list */ 5762 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5763 if (oldadp->ad_offset == off) 5764 allocdirect_merge(adphead, adp, oldadp); 5765 5766 FREE_LOCK(ITOUMP(ip)); 5767 } 5768 5769 /* 5770 * Merge a newer and older journal record to be stored either in a 5771 * newblock or freefrag. This handles aggregating journal records for 5772 * fragment allocation into a second record as well as replacing a 5773 * journal free with an aborted journal allocation. A segment for the 5774 * oldest record will be placed on wkhd if it has been written. If not 5775 * the segment for the newer record will suffice. 5776 */ 5777 static struct worklist * 5778 jnewblk_merge(new, old, wkhd) 5779 struct worklist *new; 5780 struct worklist *old; 5781 struct workhead *wkhd; 5782 { 5783 struct jnewblk *njnewblk; 5784 struct jnewblk *jnewblk; 5785 5786 /* Handle NULLs to simplify callers. */ 5787 if (new == NULL) 5788 return (old); 5789 if (old == NULL) 5790 return (new); 5791 /* Replace a jfreefrag with a jnewblk. */ 5792 if (new->wk_type == D_JFREEFRAG) { 5793 if (WK_JNEWBLK(old)->jn_blkno != WK_JFREEFRAG(new)->fr_blkno) 5794 panic("jnewblk_merge: blkno mismatch: %p, %p", 5795 old, new); 5796 cancel_jfreefrag(WK_JFREEFRAG(new)); 5797 return (old); 5798 } 5799 if (old->wk_type != D_JNEWBLK || new->wk_type != D_JNEWBLK) 5800 panic("jnewblk_merge: Bad type: old %d new %d\n", 5801 old->wk_type, new->wk_type); 5802 /* 5803 * Handle merging of two jnewblk records that describe 5804 * different sets of fragments in the same block. 5805 */ 5806 jnewblk = WK_JNEWBLK(old); 5807 njnewblk = WK_JNEWBLK(new); 5808 if (jnewblk->jn_blkno != njnewblk->jn_blkno) 5809 panic("jnewblk_merge: Merging disparate blocks."); 5810 /* 5811 * The record may be rolled back in the cg. 5812 */ 5813 if (jnewblk->jn_state & UNDONE) { 5814 jnewblk->jn_state &= ~UNDONE; 5815 njnewblk->jn_state |= UNDONE; 5816 njnewblk->jn_state &= ~ATTACHED; 5817 } 5818 /* 5819 * We modify the newer addref and free the older so that if neither 5820 * has been written the most up-to-date copy will be on disk. If 5821 * both have been written but rolled back we only temporarily need 5822 * one of them to fix the bits when the cg write completes. 5823 */ 5824 jnewblk->jn_state |= ATTACHED | COMPLETE; 5825 njnewblk->jn_oldfrags = jnewblk->jn_oldfrags; 5826 cancel_jnewblk(jnewblk, wkhd); 5827 WORKLIST_REMOVE(&jnewblk->jn_list); 5828 free_jnewblk(jnewblk); 5829 return (new); 5830 } 5831 5832 /* 5833 * Replace an old allocdirect dependency with a newer one. 5834 */ 5835 static void 5836 allocdirect_merge(adphead, newadp, oldadp) 5837 struct allocdirectlst *adphead; /* head of list holding allocdirects */ 5838 struct allocdirect *newadp; /* allocdirect being added */ 5839 struct allocdirect *oldadp; /* existing allocdirect being checked */ 5840 { 5841 struct worklist *wk; 5842 struct freefrag *freefrag; 5843 5844 freefrag = NULL; 5845 LOCK_OWNED(VFSTOUFS(newadp->ad_list.wk_mp)); 5846 if (newadp->ad_oldblkno != oldadp->ad_newblkno || 5847 newadp->ad_oldsize != oldadp->ad_newsize || 5848 newadp->ad_offset >= UFS_NDADDR) 5849 panic("%s %jd != new %jd || old size %ld != new %ld", 5850 "allocdirect_merge: old blkno", 5851 (intmax_t)newadp->ad_oldblkno, 5852 (intmax_t)oldadp->ad_newblkno, 5853 newadp->ad_oldsize, oldadp->ad_newsize); 5854 newadp->ad_oldblkno = oldadp->ad_oldblkno; 5855 newadp->ad_oldsize = oldadp->ad_oldsize; 5856 /* 5857 * If the old dependency had a fragment to free or had never 5858 * previously had a block allocated, then the new dependency 5859 * can immediately post its freefrag and adopt the old freefrag. 5860 * This action is done by swapping the freefrag dependencies. 5861 * The new dependency gains the old one's freefrag, and the 5862 * old one gets the new one and then immediately puts it on 5863 * the worklist when it is freed by free_newblk. It is 5864 * not possible to do this swap when the old dependency had a 5865 * non-zero size but no previous fragment to free. This condition 5866 * arises when the new block is an extension of the old block. 5867 * Here, the first part of the fragment allocated to the new 5868 * dependency is part of the block currently claimed on disk by 5869 * the old dependency, so cannot legitimately be freed until the 5870 * conditions for the new dependency are fulfilled. 5871 */ 5872 freefrag = newadp->ad_freefrag; 5873 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) { 5874 newadp->ad_freefrag = oldadp->ad_freefrag; 5875 oldadp->ad_freefrag = freefrag; 5876 } 5877 /* 5878 * If we are tracking a new directory-block allocation, 5879 * move it from the old allocdirect to the new allocdirect. 5880 */ 5881 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) { 5882 WORKLIST_REMOVE(wk); 5883 if (!LIST_EMPTY(&oldadp->ad_newdirblk)) 5884 panic("allocdirect_merge: extra newdirblk"); 5885 WORKLIST_INSERT(&newadp->ad_newdirblk, wk); 5886 } 5887 TAILQ_REMOVE(adphead, oldadp, ad_next); 5888 /* 5889 * We need to move any journal dependencies over to the freefrag 5890 * that releases this block if it exists. Otherwise we are 5891 * extending an existing block and we'll wait until that is 5892 * complete to release the journal space and extend the 5893 * new journal to cover this old space as well. 5894 */ 5895 if (freefrag == NULL) { 5896 if (oldadp->ad_newblkno != newadp->ad_newblkno) 5897 panic("allocdirect_merge: %jd != %jd", 5898 oldadp->ad_newblkno, newadp->ad_newblkno); 5899 newadp->ad_block.nb_jnewblk = (struct jnewblk *) 5900 jnewblk_merge(&newadp->ad_block.nb_jnewblk->jn_list, 5901 &oldadp->ad_block.nb_jnewblk->jn_list, 5902 &newadp->ad_block.nb_jwork); 5903 oldadp->ad_block.nb_jnewblk = NULL; 5904 cancel_newblk(&oldadp->ad_block, NULL, 5905 &newadp->ad_block.nb_jwork); 5906 } else { 5907 wk = (struct worklist *) cancel_newblk(&oldadp->ad_block, 5908 &freefrag->ff_list, &freefrag->ff_jwork); 5909 freefrag->ff_jdep = jnewblk_merge(freefrag->ff_jdep, wk, 5910 &freefrag->ff_jwork); 5911 } 5912 free_newblk(&oldadp->ad_block); 5913 } 5914 5915 /* 5916 * Allocate a jfreefrag structure to journal a single block free. 5917 */ 5918 static struct jfreefrag * 5919 newjfreefrag(freefrag, ip, blkno, size, lbn) 5920 struct freefrag *freefrag; 5921 struct inode *ip; 5922 ufs2_daddr_t blkno; 5923 long size; 5924 ufs_lbn_t lbn; 5925 { 5926 struct jfreefrag *jfreefrag; 5927 struct fs *fs; 5928 5929 fs = ITOFS(ip); 5930 jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG, 5931 M_SOFTDEP_FLAGS); 5932 workitem_alloc(&jfreefrag->fr_list, D_JFREEFRAG, ITOVFS(ip)); 5933 jfreefrag->fr_jsegdep = newjsegdep(&jfreefrag->fr_list); 5934 jfreefrag->fr_state = ATTACHED | DEPCOMPLETE; 5935 jfreefrag->fr_ino = ip->i_number; 5936 jfreefrag->fr_lbn = lbn; 5937 jfreefrag->fr_blkno = blkno; 5938 jfreefrag->fr_frags = numfrags(fs, size); 5939 jfreefrag->fr_freefrag = freefrag; 5940 5941 return (jfreefrag); 5942 } 5943 5944 /* 5945 * Allocate a new freefrag structure. 5946 */ 5947 static struct freefrag * 5948 newfreefrag(ip, blkno, size, lbn, key) 5949 struct inode *ip; 5950 ufs2_daddr_t blkno; 5951 long size; 5952 ufs_lbn_t lbn; 5953 u_long key; 5954 { 5955 struct freefrag *freefrag; 5956 struct ufsmount *ump; 5957 struct fs *fs; 5958 5959 CTR4(KTR_SUJ, "newfreefrag: ino %d blkno %jd size %ld lbn %jd", 5960 ip->i_number, blkno, size, lbn); 5961 ump = ITOUMP(ip); 5962 fs = ump->um_fs; 5963 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag) 5964 panic("newfreefrag: frag size"); 5965 freefrag = malloc(sizeof(struct freefrag), 5966 M_FREEFRAG, M_SOFTDEP_FLAGS); 5967 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ump)); 5968 freefrag->ff_state = ATTACHED; 5969 LIST_INIT(&freefrag->ff_jwork); 5970 freefrag->ff_inum = ip->i_number; 5971 freefrag->ff_vtype = ITOV(ip)->v_type; 5972 freefrag->ff_blkno = blkno; 5973 freefrag->ff_fragsize = size; 5974 freefrag->ff_key = key; 5975 5976 if (MOUNTEDSUJ(UFSTOVFS(ump))) { 5977 freefrag->ff_jdep = (struct worklist *) 5978 newjfreefrag(freefrag, ip, blkno, size, lbn); 5979 } else { 5980 freefrag->ff_state |= DEPCOMPLETE; 5981 freefrag->ff_jdep = NULL; 5982 } 5983 5984 return (freefrag); 5985 } 5986 5987 /* 5988 * This workitem de-allocates fragments that were replaced during 5989 * file block allocation. 5990 */ 5991 static void 5992 handle_workitem_freefrag(freefrag) 5993 struct freefrag *freefrag; 5994 { 5995 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp); 5996 struct workhead wkhd; 5997 5998 CTR3(KTR_SUJ, 5999 "handle_workitem_freefrag: ino %d blkno %jd size %ld", 6000 freefrag->ff_inum, freefrag->ff_blkno, freefrag->ff_fragsize); 6001 /* 6002 * It would be illegal to add new completion items to the 6003 * freefrag after it was schedule to be done so it must be 6004 * safe to modify the list head here. 6005 */ 6006 LIST_INIT(&wkhd); 6007 ACQUIRE_LOCK(ump); 6008 LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list); 6009 /* 6010 * If the journal has not been written we must cancel it here. 6011 */ 6012 if (freefrag->ff_jdep) { 6013 if (freefrag->ff_jdep->wk_type != D_JNEWBLK) 6014 panic("handle_workitem_freefrag: Unexpected type %d\n", 6015 freefrag->ff_jdep->wk_type); 6016 cancel_jnewblk(WK_JNEWBLK(freefrag->ff_jdep), &wkhd); 6017 } 6018 FREE_LOCK(ump); 6019 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno, 6020 freefrag->ff_fragsize, freefrag->ff_inum, freefrag->ff_vtype, 6021 &wkhd, freefrag->ff_key); 6022 ACQUIRE_LOCK(ump); 6023 WORKITEM_FREE(freefrag, D_FREEFRAG); 6024 FREE_LOCK(ump); 6025 } 6026 6027 /* 6028 * Set up a dependency structure for an external attributes data block. 6029 * This routine follows much of the structure of softdep_setup_allocdirect. 6030 * See the description of softdep_setup_allocdirect above for details. 6031 */ 6032 void 6033 softdep_setup_allocext(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 6034 struct inode *ip; 6035 ufs_lbn_t off; 6036 ufs2_daddr_t newblkno; 6037 ufs2_daddr_t oldblkno; 6038 long newsize; 6039 long oldsize; 6040 struct buf *bp; 6041 { 6042 struct allocdirect *adp, *oldadp; 6043 struct allocdirectlst *adphead; 6044 struct freefrag *freefrag; 6045 struct inodedep *inodedep; 6046 struct jnewblk *jnewblk; 6047 struct newblk *newblk; 6048 struct mount *mp; 6049 struct ufsmount *ump; 6050 ufs_lbn_t lbn; 6051 6052 mp = ITOVFS(ip); 6053 ump = VFSTOUFS(mp); 6054 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6055 ("softdep_setup_allocext called on non-softdep filesystem")); 6056 KASSERT(off < UFS_NXADDR, 6057 ("softdep_setup_allocext: lbn %lld > UFS_NXADDR", (long long)off)); 6058 6059 lbn = bp->b_lblkno; 6060 if (oldblkno && oldblkno != newblkno) 6061 /* 6062 * The usual case is that a smaller fragment that 6063 * was just allocated has been replaced with a bigger 6064 * fragment or a full-size block. If it is marked as 6065 * B_DELWRI, the current contents have not been written 6066 * to disk. It is possible that the block was written 6067 * earlier, but very uncommon. If the block has never 6068 * been written, there is no need to send a BIO_DELETE 6069 * for it when it is freed. The gain from avoiding the 6070 * TRIMs for the common case of unwritten blocks far 6071 * exceeds the cost of the write amplification for the 6072 * uncommon case of failing to send a TRIM for a block 6073 * that had been written. 6074 */ 6075 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn, 6076 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY); 6077 else 6078 freefrag = NULL; 6079 6080 ACQUIRE_LOCK(ump); 6081 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 6082 panic("softdep_setup_allocext: lost block"); 6083 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 6084 ("softdep_setup_allocext: newblk already initialized")); 6085 /* 6086 * Convert the newblk to an allocdirect. 6087 */ 6088 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT); 6089 adp = (struct allocdirect *)newblk; 6090 newblk->nb_freefrag = freefrag; 6091 adp->ad_offset = off; 6092 adp->ad_oldblkno = oldblkno; 6093 adp->ad_newsize = newsize; 6094 adp->ad_oldsize = oldsize; 6095 adp->ad_state |= EXTDATA; 6096 6097 /* 6098 * Finish initializing the journal. 6099 */ 6100 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 6101 jnewblk->jn_ino = ip->i_number; 6102 jnewblk->jn_lbn = lbn; 6103 add_to_journal(&jnewblk->jn_list); 6104 } 6105 if (freefrag && freefrag->ff_jdep != NULL && 6106 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 6107 add_to_journal(freefrag->ff_jdep); 6108 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6109 adp->ad_inodedep = inodedep; 6110 6111 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 6112 /* 6113 * The list of allocdirects must be kept in sorted and ascending 6114 * order so that the rollback routines can quickly determine the 6115 * first uncommitted block (the size of the file stored on disk 6116 * ends at the end of the lowest committed fragment, or if there 6117 * are no fragments, at the end of the highest committed block). 6118 * Since files generally grow, the typical case is that the new 6119 * block is to be added at the end of the list. We speed this 6120 * special case by checking against the last allocdirect in the 6121 * list before laboriously traversing the list looking for the 6122 * insertion point. 6123 */ 6124 adphead = &inodedep->id_newextupdt; 6125 oldadp = TAILQ_LAST(adphead, allocdirectlst); 6126 if (oldadp == NULL || oldadp->ad_offset <= off) { 6127 /* insert at end of list */ 6128 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 6129 if (oldadp != NULL && oldadp->ad_offset == off) 6130 allocdirect_merge(adphead, adp, oldadp); 6131 FREE_LOCK(ump); 6132 return; 6133 } 6134 TAILQ_FOREACH(oldadp, adphead, ad_next) { 6135 if (oldadp->ad_offset >= off) 6136 break; 6137 } 6138 if (oldadp == NULL) 6139 panic("softdep_setup_allocext: lost entry"); 6140 /* insert in middle of list */ 6141 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 6142 if (oldadp->ad_offset == off) 6143 allocdirect_merge(adphead, adp, oldadp); 6144 FREE_LOCK(ump); 6145 } 6146 6147 /* 6148 * Indirect block allocation dependencies. 6149 * 6150 * The same dependencies that exist for a direct block also exist when 6151 * a new block is allocated and pointed to by an entry in a block of 6152 * indirect pointers. The undo/redo states described above are also 6153 * used here. Because an indirect block contains many pointers that 6154 * may have dependencies, a second copy of the entire in-memory indirect 6155 * block is kept. The buffer cache copy is always completely up-to-date. 6156 * The second copy, which is used only as a source for disk writes, 6157 * contains only the safe pointers (i.e., those that have no remaining 6158 * update dependencies). The second copy is freed when all pointers 6159 * are safe. The cache is not allowed to replace indirect blocks with 6160 * pending update dependencies. If a buffer containing an indirect 6161 * block with dependencies is written, these routines will mark it 6162 * dirty again. It can only be successfully written once all the 6163 * dependencies are removed. The ffs_fsync routine in conjunction with 6164 * softdep_sync_metadata work together to get all the dependencies 6165 * removed so that a file can be successfully written to disk. Three 6166 * procedures are used when setting up indirect block pointer 6167 * dependencies. The division is necessary because of the organization 6168 * of the "balloc" routine and because of the distinction between file 6169 * pages and file metadata blocks. 6170 */ 6171 6172 /* 6173 * Allocate a new allocindir structure. 6174 */ 6175 static struct allocindir * 6176 newallocindir(ip, ptrno, newblkno, oldblkno, lbn) 6177 struct inode *ip; /* inode for file being extended */ 6178 int ptrno; /* offset of pointer in indirect block */ 6179 ufs2_daddr_t newblkno; /* disk block number being added */ 6180 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 6181 ufs_lbn_t lbn; 6182 { 6183 struct newblk *newblk; 6184 struct allocindir *aip; 6185 struct freefrag *freefrag; 6186 struct jnewblk *jnewblk; 6187 6188 if (oldblkno) 6189 freefrag = newfreefrag(ip, oldblkno, ITOFS(ip)->fs_bsize, lbn, 6190 SINGLETON_KEY); 6191 else 6192 freefrag = NULL; 6193 ACQUIRE_LOCK(ITOUMP(ip)); 6194 if (newblk_lookup(ITOVFS(ip), newblkno, 0, &newblk) == 0) 6195 panic("new_allocindir: lost block"); 6196 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 6197 ("newallocindir: newblk already initialized")); 6198 WORKITEM_REASSIGN(newblk, D_ALLOCINDIR); 6199 newblk->nb_freefrag = freefrag; 6200 aip = (struct allocindir *)newblk; 6201 aip->ai_offset = ptrno; 6202 aip->ai_oldblkno = oldblkno; 6203 aip->ai_lbn = lbn; 6204 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 6205 jnewblk->jn_ino = ip->i_number; 6206 jnewblk->jn_lbn = lbn; 6207 add_to_journal(&jnewblk->jn_list); 6208 } 6209 if (freefrag && freefrag->ff_jdep != NULL && 6210 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 6211 add_to_journal(freefrag->ff_jdep); 6212 return (aip); 6213 } 6214 6215 /* 6216 * Called just before setting an indirect block pointer 6217 * to a newly allocated file page. 6218 */ 6219 void 6220 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 6221 struct inode *ip; /* inode for file being extended */ 6222 ufs_lbn_t lbn; /* allocated block number within file */ 6223 struct buf *bp; /* buffer with indirect blk referencing page */ 6224 int ptrno; /* offset of pointer in indirect block */ 6225 ufs2_daddr_t newblkno; /* disk block number being added */ 6226 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 6227 struct buf *nbp; /* buffer holding allocated page */ 6228 { 6229 struct inodedep *inodedep; 6230 struct freefrag *freefrag; 6231 struct allocindir *aip; 6232 struct pagedep *pagedep; 6233 struct mount *mp; 6234 struct ufsmount *ump; 6235 6236 mp = ITOVFS(ip); 6237 ump = VFSTOUFS(mp); 6238 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6239 ("softdep_setup_allocindir_page called on non-softdep filesystem")); 6240 KASSERT(lbn == nbp->b_lblkno, 6241 ("softdep_setup_allocindir_page: lbn %jd != lblkno %jd", 6242 lbn, bp->b_lblkno)); 6243 CTR4(KTR_SUJ, 6244 "softdep_setup_allocindir_page: ino %d blkno %jd oldblkno %jd " 6245 "lbn %jd", ip->i_number, newblkno, oldblkno, lbn); 6246 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page"); 6247 aip = newallocindir(ip, ptrno, newblkno, oldblkno, lbn); 6248 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6249 /* 6250 * If we are allocating a directory page, then we must 6251 * allocate an associated pagedep to track additions and 6252 * deletions. 6253 */ 6254 if ((ip->i_mode & IFMT) == IFDIR) 6255 pagedep_lookup(mp, nbp, ip->i_number, lbn, DEPALLOC, &pagedep); 6256 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 6257 freefrag = setup_allocindir_phase2(bp, ip, inodedep, aip, lbn); 6258 FREE_LOCK(ump); 6259 if (freefrag) 6260 handle_workitem_freefrag(freefrag); 6261 } 6262 6263 /* 6264 * Called just before setting an indirect block pointer to a 6265 * newly allocated indirect block. 6266 */ 6267 void 6268 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 6269 struct buf *nbp; /* newly allocated indirect block */ 6270 struct inode *ip; /* inode for file being extended */ 6271 struct buf *bp; /* indirect block referencing allocated block */ 6272 int ptrno; /* offset of pointer in indirect block */ 6273 ufs2_daddr_t newblkno; /* disk block number being added */ 6274 { 6275 struct inodedep *inodedep; 6276 struct allocindir *aip; 6277 struct ufsmount *ump; 6278 ufs_lbn_t lbn; 6279 6280 ump = ITOUMP(ip); 6281 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 6282 ("softdep_setup_allocindir_meta called on non-softdep filesystem")); 6283 CTR3(KTR_SUJ, 6284 "softdep_setup_allocindir_meta: ino %d blkno %jd ptrno %d", 6285 ip->i_number, newblkno, ptrno); 6286 lbn = nbp->b_lblkno; 6287 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta"); 6288 aip = newallocindir(ip, ptrno, newblkno, 0, lbn); 6289 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep); 6290 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 6291 if (setup_allocindir_phase2(bp, ip, inodedep, aip, lbn)) 6292 panic("softdep_setup_allocindir_meta: Block already existed"); 6293 FREE_LOCK(ump); 6294 } 6295 6296 static void 6297 indirdep_complete(indirdep) 6298 struct indirdep *indirdep; 6299 { 6300 struct allocindir *aip; 6301 6302 LIST_REMOVE(indirdep, ir_next); 6303 indirdep->ir_state |= DEPCOMPLETE; 6304 6305 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) { 6306 LIST_REMOVE(aip, ai_next); 6307 free_newblk(&aip->ai_block); 6308 } 6309 /* 6310 * If this indirdep is not attached to a buf it was simply waiting 6311 * on completion to clear completehd. free_indirdep() asserts 6312 * that nothing is dangling. 6313 */ 6314 if ((indirdep->ir_state & ONWORKLIST) == 0) 6315 free_indirdep(indirdep); 6316 } 6317 6318 static struct indirdep * 6319 indirdep_lookup(mp, ip, bp) 6320 struct mount *mp; 6321 struct inode *ip; 6322 struct buf *bp; 6323 { 6324 struct indirdep *indirdep, *newindirdep; 6325 struct newblk *newblk; 6326 struct ufsmount *ump; 6327 struct worklist *wk; 6328 struct fs *fs; 6329 ufs2_daddr_t blkno; 6330 6331 ump = VFSTOUFS(mp); 6332 LOCK_OWNED(ump); 6333 indirdep = NULL; 6334 newindirdep = NULL; 6335 fs = ump->um_fs; 6336 for (;;) { 6337 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 6338 if (wk->wk_type != D_INDIRDEP) 6339 continue; 6340 indirdep = WK_INDIRDEP(wk); 6341 break; 6342 } 6343 /* Found on the buffer worklist, no new structure to free. */ 6344 if (indirdep != NULL && newindirdep == NULL) 6345 return (indirdep); 6346 if (indirdep != NULL && newindirdep != NULL) 6347 panic("indirdep_lookup: simultaneous create"); 6348 /* None found on the buffer and a new structure is ready. */ 6349 if (indirdep == NULL && newindirdep != NULL) 6350 break; 6351 /* None found and no new structure available. */ 6352 FREE_LOCK(ump); 6353 newindirdep = malloc(sizeof(struct indirdep), 6354 M_INDIRDEP, M_SOFTDEP_FLAGS); 6355 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, mp); 6356 newindirdep->ir_state = ATTACHED; 6357 if (I_IS_UFS1(ip)) 6358 newindirdep->ir_state |= UFS1FMT; 6359 TAILQ_INIT(&newindirdep->ir_trunc); 6360 newindirdep->ir_saveddata = NULL; 6361 LIST_INIT(&newindirdep->ir_deplisthd); 6362 LIST_INIT(&newindirdep->ir_donehd); 6363 LIST_INIT(&newindirdep->ir_writehd); 6364 LIST_INIT(&newindirdep->ir_completehd); 6365 if (bp->b_blkno == bp->b_lblkno) { 6366 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp, 6367 NULL, NULL); 6368 bp->b_blkno = blkno; 6369 } 6370 newindirdep->ir_freeblks = NULL; 6371 newindirdep->ir_savebp = 6372 getblk(ump->um_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0); 6373 newindirdep->ir_bp = bp; 6374 BUF_KERNPROC(newindirdep->ir_savebp); 6375 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount); 6376 ACQUIRE_LOCK(ump); 6377 } 6378 indirdep = newindirdep; 6379 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list); 6380 /* 6381 * If the block is not yet allocated we don't set DEPCOMPLETE so 6382 * that we don't free dependencies until the pointers are valid. 6383 * This could search b_dep for D_ALLOCDIRECT/D_ALLOCINDIR rather 6384 * than using the hash. 6385 */ 6386 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk)) 6387 LIST_INSERT_HEAD(&newblk->nb_indirdeps, indirdep, ir_next); 6388 else 6389 indirdep->ir_state |= DEPCOMPLETE; 6390 return (indirdep); 6391 } 6392 6393 /* 6394 * Called to finish the allocation of the "aip" allocated 6395 * by one of the two routines above. 6396 */ 6397 static struct freefrag * 6398 setup_allocindir_phase2(bp, ip, inodedep, aip, lbn) 6399 struct buf *bp; /* in-memory copy of the indirect block */ 6400 struct inode *ip; /* inode for file being extended */ 6401 struct inodedep *inodedep; /* Inodedep for ip */ 6402 struct allocindir *aip; /* allocindir allocated by the above routines */ 6403 ufs_lbn_t lbn; /* Logical block number for this block. */ 6404 { 6405 struct fs *fs; 6406 struct indirdep *indirdep; 6407 struct allocindir *oldaip; 6408 struct freefrag *freefrag; 6409 struct mount *mp; 6410 struct ufsmount *ump; 6411 6412 mp = ITOVFS(ip); 6413 ump = VFSTOUFS(mp); 6414 LOCK_OWNED(ump); 6415 fs = ump->um_fs; 6416 if (bp->b_lblkno >= 0) 6417 panic("setup_allocindir_phase2: not indir blk"); 6418 KASSERT(aip->ai_offset >= 0 && aip->ai_offset < NINDIR(fs), 6419 ("setup_allocindir_phase2: Bad offset %d", aip->ai_offset)); 6420 indirdep = indirdep_lookup(mp, ip, bp); 6421 KASSERT(indirdep->ir_savebp != NULL, 6422 ("setup_allocindir_phase2 NULL ir_savebp")); 6423 aip->ai_indirdep = indirdep; 6424 /* 6425 * Check for an unwritten dependency for this indirect offset. If 6426 * there is, merge the old dependency into the new one. This happens 6427 * as a result of reallocblk only. 6428 */ 6429 freefrag = NULL; 6430 if (aip->ai_oldblkno != 0) { 6431 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) { 6432 if (oldaip->ai_offset == aip->ai_offset) { 6433 freefrag = allocindir_merge(aip, oldaip); 6434 goto done; 6435 } 6436 } 6437 LIST_FOREACH(oldaip, &indirdep->ir_donehd, ai_next) { 6438 if (oldaip->ai_offset == aip->ai_offset) { 6439 freefrag = allocindir_merge(aip, oldaip); 6440 goto done; 6441 } 6442 } 6443 } 6444 done: 6445 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next); 6446 return (freefrag); 6447 } 6448 6449 /* 6450 * Merge two allocindirs which refer to the same block. Move newblock 6451 * dependencies and setup the freefrags appropriately. 6452 */ 6453 static struct freefrag * 6454 allocindir_merge(aip, oldaip) 6455 struct allocindir *aip; 6456 struct allocindir *oldaip; 6457 { 6458 struct freefrag *freefrag; 6459 struct worklist *wk; 6460 6461 if (oldaip->ai_newblkno != aip->ai_oldblkno) 6462 panic("allocindir_merge: blkno"); 6463 aip->ai_oldblkno = oldaip->ai_oldblkno; 6464 freefrag = aip->ai_freefrag; 6465 aip->ai_freefrag = oldaip->ai_freefrag; 6466 oldaip->ai_freefrag = NULL; 6467 KASSERT(freefrag != NULL, ("setup_allocindir_phase2: No freefrag")); 6468 /* 6469 * If we are tracking a new directory-block allocation, 6470 * move it from the old allocindir to the new allocindir. 6471 */ 6472 if ((wk = LIST_FIRST(&oldaip->ai_newdirblk)) != NULL) { 6473 WORKLIST_REMOVE(wk); 6474 if (!LIST_EMPTY(&oldaip->ai_newdirblk)) 6475 panic("allocindir_merge: extra newdirblk"); 6476 WORKLIST_INSERT(&aip->ai_newdirblk, wk); 6477 } 6478 /* 6479 * We can skip journaling for this freefrag and just complete 6480 * any pending journal work for the allocindir that is being 6481 * removed after the freefrag completes. 6482 */ 6483 if (freefrag->ff_jdep) 6484 cancel_jfreefrag(WK_JFREEFRAG(freefrag->ff_jdep)); 6485 LIST_REMOVE(oldaip, ai_next); 6486 freefrag->ff_jdep = (struct worklist *)cancel_newblk(&oldaip->ai_block, 6487 &freefrag->ff_list, &freefrag->ff_jwork); 6488 free_newblk(&oldaip->ai_block); 6489 6490 return (freefrag); 6491 } 6492 6493 static inline void 6494 setup_freedirect(freeblks, ip, i, needj) 6495 struct freeblks *freeblks; 6496 struct inode *ip; 6497 int i; 6498 int needj; 6499 { 6500 struct ufsmount *ump; 6501 ufs2_daddr_t blkno; 6502 int frags; 6503 6504 blkno = DIP(ip, i_db[i]); 6505 if (blkno == 0) 6506 return; 6507 DIP_SET(ip, i_db[i], 0); 6508 ump = ITOUMP(ip); 6509 frags = sblksize(ump->um_fs, ip->i_size, i); 6510 frags = numfrags(ump->um_fs, frags); 6511 newfreework(ump, freeblks, NULL, i, blkno, frags, 0, needj); 6512 } 6513 6514 static inline void 6515 setup_freeext(freeblks, ip, i, needj) 6516 struct freeblks *freeblks; 6517 struct inode *ip; 6518 int i; 6519 int needj; 6520 { 6521 struct ufsmount *ump; 6522 ufs2_daddr_t blkno; 6523 int frags; 6524 6525 blkno = ip->i_din2->di_extb[i]; 6526 if (blkno == 0) 6527 return; 6528 ip->i_din2->di_extb[i] = 0; 6529 ump = ITOUMP(ip); 6530 frags = sblksize(ump->um_fs, ip->i_din2->di_extsize, i); 6531 frags = numfrags(ump->um_fs, frags); 6532 newfreework(ump, freeblks, NULL, -1 - i, blkno, frags, 0, needj); 6533 } 6534 6535 static inline void 6536 setup_freeindir(freeblks, ip, i, lbn, needj) 6537 struct freeblks *freeblks; 6538 struct inode *ip; 6539 int i; 6540 ufs_lbn_t lbn; 6541 int needj; 6542 { 6543 struct ufsmount *ump; 6544 ufs2_daddr_t blkno; 6545 6546 blkno = DIP(ip, i_ib[i]); 6547 if (blkno == 0) 6548 return; 6549 DIP_SET(ip, i_ib[i], 0); 6550 ump = ITOUMP(ip); 6551 newfreework(ump, freeblks, NULL, lbn, blkno, ump->um_fs->fs_frag, 6552 0, needj); 6553 } 6554 6555 static inline struct freeblks * 6556 newfreeblks(mp, ip) 6557 struct mount *mp; 6558 struct inode *ip; 6559 { 6560 struct freeblks *freeblks; 6561 6562 freeblks = malloc(sizeof(struct freeblks), 6563 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO); 6564 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp); 6565 LIST_INIT(&freeblks->fb_jblkdephd); 6566 LIST_INIT(&freeblks->fb_jwork); 6567 freeblks->fb_ref = 0; 6568 freeblks->fb_cgwait = 0; 6569 freeblks->fb_state = ATTACHED; 6570 freeblks->fb_uid = ip->i_uid; 6571 freeblks->fb_inum = ip->i_number; 6572 freeblks->fb_vtype = ITOV(ip)->v_type; 6573 freeblks->fb_modrev = DIP(ip, i_modrev); 6574 freeblks->fb_devvp = ITODEVVP(ip); 6575 freeblks->fb_chkcnt = 0; 6576 freeblks->fb_len = 0; 6577 6578 return (freeblks); 6579 } 6580 6581 static void 6582 trunc_indirdep(indirdep, freeblks, bp, off) 6583 struct indirdep *indirdep; 6584 struct freeblks *freeblks; 6585 struct buf *bp; 6586 int off; 6587 { 6588 struct allocindir *aip, *aipn; 6589 6590 /* 6591 * The first set of allocindirs won't be in savedbp. 6592 */ 6593 LIST_FOREACH_SAFE(aip, &indirdep->ir_deplisthd, ai_next, aipn) 6594 if (aip->ai_offset > off) 6595 cancel_allocindir(aip, bp, freeblks, 1); 6596 LIST_FOREACH_SAFE(aip, &indirdep->ir_donehd, ai_next, aipn) 6597 if (aip->ai_offset > off) 6598 cancel_allocindir(aip, bp, freeblks, 1); 6599 /* 6600 * These will exist in savedbp. 6601 */ 6602 LIST_FOREACH_SAFE(aip, &indirdep->ir_writehd, ai_next, aipn) 6603 if (aip->ai_offset > off) 6604 cancel_allocindir(aip, NULL, freeblks, 0); 6605 LIST_FOREACH_SAFE(aip, &indirdep->ir_completehd, ai_next, aipn) 6606 if (aip->ai_offset > off) 6607 cancel_allocindir(aip, NULL, freeblks, 0); 6608 } 6609 6610 /* 6611 * Follow the chain of indirects down to lastlbn creating a freework 6612 * structure for each. This will be used to start indir_trunc() at 6613 * the right offset and create the journal records for the parrtial 6614 * truncation. A second step will handle the truncated dependencies. 6615 */ 6616 static int 6617 setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno) 6618 struct freeblks *freeblks; 6619 struct inode *ip; 6620 ufs_lbn_t lbn; 6621 ufs_lbn_t lastlbn; 6622 ufs2_daddr_t blkno; 6623 { 6624 struct indirdep *indirdep; 6625 struct indirdep *indirn; 6626 struct freework *freework; 6627 struct newblk *newblk; 6628 struct mount *mp; 6629 struct ufsmount *ump; 6630 struct buf *bp; 6631 uint8_t *start; 6632 uint8_t *end; 6633 ufs_lbn_t lbnadd; 6634 int level; 6635 int error; 6636 int off; 6637 6638 freework = NULL; 6639 if (blkno == 0) 6640 return (0); 6641 mp = freeblks->fb_list.wk_mp; 6642 ump = VFSTOUFS(mp); 6643 /* 6644 * Here, calls to VOP_BMAP() will fail. However, we already have 6645 * the on-disk address, so we just pass it to bread() instead of 6646 * having bread() attempt to calculate it using VOP_BMAP(). 6647 */ 6648 error = ffs_breadz(ump, ITOV(ip), lbn, blkptrtodb(ump, blkno), 6649 (int)mp->mnt_stat.f_iosize, NULL, NULL, 0, NOCRED, 0, NULL, &bp); 6650 if (error) 6651 return (error); 6652 level = lbn_level(lbn); 6653 lbnadd = lbn_offset(ump->um_fs, level); 6654 /* 6655 * Compute the offset of the last block we want to keep. Store 6656 * in the freework the first block we want to completely free. 6657 */ 6658 off = (lastlbn - -(lbn + level)) / lbnadd; 6659 if (off + 1 == NINDIR(ump->um_fs)) 6660 goto nowork; 6661 freework = newfreework(ump, freeblks, NULL, lbn, blkno, 0, off + 1, 0); 6662 /* 6663 * Link the freework into the indirdep. This will prevent any new 6664 * allocations from proceeding until we are finished with the 6665 * truncate and the block is written. 6666 */ 6667 ACQUIRE_LOCK(ump); 6668 indirdep = indirdep_lookup(mp, ip, bp); 6669 if (indirdep->ir_freeblks) 6670 panic("setup_trunc_indir: indirdep already truncated."); 6671 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, freework, fw_next); 6672 freework->fw_indir = indirdep; 6673 /* 6674 * Cancel any allocindirs that will not make it to disk. 6675 * We have to do this for all copies of the indirdep that 6676 * live on this newblk. 6677 */ 6678 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 6679 if (newblk_lookup(mp, dbtofsb(ump->um_fs, bp->b_blkno), 0, 6680 &newblk) == 0) 6681 panic("setup_trunc_indir: lost block"); 6682 LIST_FOREACH(indirn, &newblk->nb_indirdeps, ir_next) 6683 trunc_indirdep(indirn, freeblks, bp, off); 6684 } else 6685 trunc_indirdep(indirdep, freeblks, bp, off); 6686 FREE_LOCK(ump); 6687 /* 6688 * Creation is protected by the buf lock. The saveddata is only 6689 * needed if a full truncation follows a partial truncation but it 6690 * is difficult to allocate in that case so we fetch it anyway. 6691 */ 6692 if (indirdep->ir_saveddata == NULL) 6693 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 6694 M_SOFTDEP_FLAGS); 6695 nowork: 6696 /* Fetch the blkno of the child and the zero start offset. */ 6697 if (I_IS_UFS1(ip)) { 6698 blkno = ((ufs1_daddr_t *)bp->b_data)[off]; 6699 start = (uint8_t *)&((ufs1_daddr_t *)bp->b_data)[off+1]; 6700 } else { 6701 blkno = ((ufs2_daddr_t *)bp->b_data)[off]; 6702 start = (uint8_t *)&((ufs2_daddr_t *)bp->b_data)[off+1]; 6703 } 6704 if (freework) { 6705 /* Zero the truncated pointers. */ 6706 end = bp->b_data + bp->b_bcount; 6707 bzero(start, end - start); 6708 bdwrite(bp); 6709 } else 6710 bqrelse(bp); 6711 if (level == 0) 6712 return (0); 6713 lbn++; /* adjust level */ 6714 lbn -= (off * lbnadd); 6715 return setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno); 6716 } 6717 6718 /* 6719 * Complete the partial truncation of an indirect block setup by 6720 * setup_trunc_indir(). This zeros the truncated pointers in the saved 6721 * copy and writes them to disk before the freeblks is allowed to complete. 6722 */ 6723 static void 6724 complete_trunc_indir(freework) 6725 struct freework *freework; 6726 { 6727 struct freework *fwn; 6728 struct indirdep *indirdep; 6729 struct ufsmount *ump; 6730 struct buf *bp; 6731 uintptr_t start; 6732 int count; 6733 6734 ump = VFSTOUFS(freework->fw_list.wk_mp); 6735 LOCK_OWNED(ump); 6736 indirdep = freework->fw_indir; 6737 for (;;) { 6738 bp = indirdep->ir_bp; 6739 /* See if the block was discarded. */ 6740 if (bp == NULL) 6741 break; 6742 /* Inline part of getdirtybuf(). We dont want bremfree. */ 6743 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) 6744 break; 6745 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 6746 LOCK_PTR(ump)) == 0) 6747 BUF_UNLOCK(bp); 6748 ACQUIRE_LOCK(ump); 6749 } 6750 freework->fw_state |= DEPCOMPLETE; 6751 TAILQ_REMOVE(&indirdep->ir_trunc, freework, fw_next); 6752 /* 6753 * Zero the pointers in the saved copy. 6754 */ 6755 if (indirdep->ir_state & UFS1FMT) 6756 start = sizeof(ufs1_daddr_t); 6757 else 6758 start = sizeof(ufs2_daddr_t); 6759 start *= freework->fw_start; 6760 count = indirdep->ir_savebp->b_bcount - start; 6761 start += (uintptr_t)indirdep->ir_savebp->b_data; 6762 bzero((char *)start, count); 6763 /* 6764 * We need to start the next truncation in the list if it has not 6765 * been started yet. 6766 */ 6767 fwn = TAILQ_FIRST(&indirdep->ir_trunc); 6768 if (fwn != NULL) { 6769 if (fwn->fw_freeblks == indirdep->ir_freeblks) 6770 TAILQ_REMOVE(&indirdep->ir_trunc, fwn, fw_next); 6771 if ((fwn->fw_state & ONWORKLIST) == 0) 6772 freework_enqueue(fwn); 6773 } 6774 /* 6775 * If bp is NULL the block was fully truncated, restore 6776 * the saved block list otherwise free it if it is no 6777 * longer needed. 6778 */ 6779 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 6780 if (bp == NULL) 6781 bcopy(indirdep->ir_saveddata, 6782 indirdep->ir_savebp->b_data, 6783 indirdep->ir_savebp->b_bcount); 6784 free(indirdep->ir_saveddata, M_INDIRDEP); 6785 indirdep->ir_saveddata = NULL; 6786 } 6787 /* 6788 * When bp is NULL there is a full truncation pending. We 6789 * must wait for this full truncation to be journaled before 6790 * we can release this freework because the disk pointers will 6791 * never be written as zero. 6792 */ 6793 if (bp == NULL) { 6794 if (LIST_EMPTY(&indirdep->ir_freeblks->fb_jblkdephd)) 6795 handle_written_freework(freework); 6796 else 6797 WORKLIST_INSERT(&indirdep->ir_freeblks->fb_freeworkhd, 6798 &freework->fw_list); 6799 if (fwn == NULL) { 6800 freework->fw_indir = (void *)0x0000deadbeef0000; 6801 bp = indirdep->ir_savebp; 6802 indirdep->ir_savebp = NULL; 6803 free_indirdep(indirdep); 6804 FREE_LOCK(ump); 6805 brelse(bp); 6806 ACQUIRE_LOCK(ump); 6807 } 6808 } else { 6809 /* Complete when the real copy is written. */ 6810 WORKLIST_INSERT(&bp->b_dep, &freework->fw_list); 6811 BUF_UNLOCK(bp); 6812 } 6813 } 6814 6815 /* 6816 * Calculate the number of blocks we are going to release where datablocks 6817 * is the current total and length is the new file size. 6818 */ 6819 static ufs2_daddr_t 6820 blkcount(fs, datablocks, length) 6821 struct fs *fs; 6822 ufs2_daddr_t datablocks; 6823 off_t length; 6824 { 6825 off_t totblks, numblks; 6826 6827 totblks = 0; 6828 numblks = howmany(length, fs->fs_bsize); 6829 if (numblks <= UFS_NDADDR) { 6830 totblks = howmany(length, fs->fs_fsize); 6831 goto out; 6832 } 6833 totblks = blkstofrags(fs, numblks); 6834 numblks -= UFS_NDADDR; 6835 /* 6836 * Count all single, then double, then triple indirects required. 6837 * Subtracting one indirects worth of blocks for each pass 6838 * acknowledges one of each pointed to by the inode. 6839 */ 6840 for (;;) { 6841 totblks += blkstofrags(fs, howmany(numblks, NINDIR(fs))); 6842 numblks -= NINDIR(fs); 6843 if (numblks <= 0) 6844 break; 6845 numblks = howmany(numblks, NINDIR(fs)); 6846 } 6847 out: 6848 totblks = fsbtodb(fs, totblks); 6849 /* 6850 * Handle sparse files. We can't reclaim more blocks than the inode 6851 * references. We will correct it later in handle_complete_freeblks() 6852 * when we know the real count. 6853 */ 6854 if (totblks > datablocks) 6855 return (0); 6856 return (datablocks - totblks); 6857 } 6858 6859 /* 6860 * Handle freeblocks for journaled softupdate filesystems. 6861 * 6862 * Contrary to normal softupdates, we must preserve the block pointers in 6863 * indirects until their subordinates are free. This is to avoid journaling 6864 * every block that is freed which may consume more space than the journal 6865 * itself. The recovery program will see the free block journals at the 6866 * base of the truncated area and traverse them to reclaim space. The 6867 * pointers in the inode may be cleared immediately after the journal 6868 * records are written because each direct and indirect pointer in the 6869 * inode is recorded in a journal. This permits full truncation to proceed 6870 * asynchronously. The write order is journal -> inode -> cgs -> indirects. 6871 * 6872 * The algorithm is as follows: 6873 * 1) Traverse the in-memory state and create journal entries to release 6874 * the relevant blocks and full indirect trees. 6875 * 2) Traverse the indirect block chain adding partial truncation freework 6876 * records to indirects in the path to lastlbn. The freework will 6877 * prevent new allocation dependencies from being satisfied in this 6878 * indirect until the truncation completes. 6879 * 3) Read and lock the inode block, performing an update with the new size 6880 * and pointers. This prevents truncated data from becoming valid on 6881 * disk through step 4. 6882 * 4) Reap unsatisfied dependencies that are beyond the truncated area, 6883 * eliminate journal work for those records that do not require it. 6884 * 5) Schedule the journal records to be written followed by the inode block. 6885 * 6) Allocate any necessary frags for the end of file. 6886 * 7) Zero any partially truncated blocks. 6887 * 6888 * From this truncation proceeds asynchronously using the freework and 6889 * indir_trunc machinery. The file will not be extended again into a 6890 * partially truncated indirect block until all work is completed but 6891 * the normal dependency mechanism ensures that it is rolled back/forward 6892 * as appropriate. Further truncation may occur without delay and is 6893 * serialized in indir_trunc(). 6894 */ 6895 void 6896 softdep_journal_freeblocks(ip, cred, length, flags) 6897 struct inode *ip; /* The inode whose length is to be reduced */ 6898 struct ucred *cred; 6899 off_t length; /* The new length for the file */ 6900 int flags; /* IO_EXT and/or IO_NORMAL */ 6901 { 6902 struct freeblks *freeblks, *fbn; 6903 struct worklist *wk, *wkn; 6904 struct inodedep *inodedep; 6905 struct jblkdep *jblkdep; 6906 struct allocdirect *adp, *adpn; 6907 struct ufsmount *ump; 6908 struct fs *fs; 6909 struct buf *bp; 6910 struct vnode *vp; 6911 struct mount *mp; 6912 daddr_t dbn; 6913 ufs2_daddr_t extblocks, datablocks; 6914 ufs_lbn_t tmpval, lbn, lastlbn; 6915 int frags, lastoff, iboff, allocblock, needj, error, i; 6916 6917 ump = ITOUMP(ip); 6918 mp = UFSTOVFS(ump); 6919 fs = ump->um_fs; 6920 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6921 ("softdep_journal_freeblocks called on non-softdep filesystem")); 6922 vp = ITOV(ip); 6923 needj = 1; 6924 iboff = -1; 6925 allocblock = 0; 6926 extblocks = 0; 6927 datablocks = 0; 6928 frags = 0; 6929 freeblks = newfreeblks(mp, ip); 6930 ACQUIRE_LOCK(ump); 6931 /* 6932 * If we're truncating a removed file that will never be written 6933 * we don't need to journal the block frees. The canceled journals 6934 * for the allocations will suffice. 6935 */ 6936 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6937 if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED && 6938 length == 0) 6939 needj = 0; 6940 CTR3(KTR_SUJ, "softdep_journal_freeblks: ip %d length %ld needj %d", 6941 ip->i_number, length, needj); 6942 FREE_LOCK(ump); 6943 /* 6944 * Calculate the lbn that we are truncating to. This results in -1 6945 * if we're truncating the 0 bytes. So it is the last lbn we want 6946 * to keep, not the first lbn we want to truncate. 6947 */ 6948 lastlbn = lblkno(fs, length + fs->fs_bsize - 1) - 1; 6949 lastoff = blkoff(fs, length); 6950 /* 6951 * Compute frags we are keeping in lastlbn. 0 means all. 6952 */ 6953 if (lastlbn >= 0 && lastlbn < UFS_NDADDR) { 6954 frags = fragroundup(fs, lastoff); 6955 /* adp offset of last valid allocdirect. */ 6956 iboff = lastlbn; 6957 } else if (lastlbn > 0) 6958 iboff = UFS_NDADDR; 6959 if (fs->fs_magic == FS_UFS2_MAGIC) 6960 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 6961 /* 6962 * Handle normal data blocks and indirects. This section saves 6963 * values used after the inode update to complete frag and indirect 6964 * truncation. 6965 */ 6966 if ((flags & IO_NORMAL) != 0) { 6967 /* 6968 * Handle truncation of whole direct and indirect blocks. 6969 */ 6970 for (i = iboff + 1; i < UFS_NDADDR; i++) 6971 setup_freedirect(freeblks, ip, i, needj); 6972 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR; 6973 i < UFS_NIADDR; 6974 i++, lbn += tmpval, tmpval *= NINDIR(fs)) { 6975 /* Release a whole indirect tree. */ 6976 if (lbn > lastlbn) { 6977 setup_freeindir(freeblks, ip, i, -lbn -i, 6978 needj); 6979 continue; 6980 } 6981 iboff = i + UFS_NDADDR; 6982 /* 6983 * Traverse partially truncated indirect tree. 6984 */ 6985 if (lbn <= lastlbn && lbn + tmpval - 1 > lastlbn) 6986 setup_trunc_indir(freeblks, ip, -lbn - i, 6987 lastlbn, DIP(ip, i_ib[i])); 6988 } 6989 /* 6990 * Handle partial truncation to a frag boundary. 6991 */ 6992 if (frags) { 6993 ufs2_daddr_t blkno; 6994 long oldfrags; 6995 6996 oldfrags = blksize(fs, ip, lastlbn); 6997 blkno = DIP(ip, i_db[lastlbn]); 6998 if (blkno && oldfrags != frags) { 6999 oldfrags -= frags; 7000 oldfrags = numfrags(fs, oldfrags); 7001 blkno += numfrags(fs, frags); 7002 newfreework(ump, freeblks, NULL, lastlbn, 7003 blkno, oldfrags, 0, needj); 7004 if (needj) 7005 adjust_newfreework(freeblks, 7006 numfrags(fs, frags)); 7007 } else if (blkno == 0) 7008 allocblock = 1; 7009 } 7010 /* 7011 * Add a journal record for partial truncate if we are 7012 * handling indirect blocks. Non-indirects need no extra 7013 * journaling. 7014 */ 7015 if (length != 0 && lastlbn >= UFS_NDADDR) { 7016 UFS_INODE_SET_FLAG(ip, IN_TRUNCATED); 7017 newjtrunc(freeblks, length, 0); 7018 } 7019 ip->i_size = length; 7020 DIP_SET(ip, i_size, ip->i_size); 7021 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE); 7022 datablocks = DIP(ip, i_blocks) - extblocks; 7023 if (length != 0) 7024 datablocks = blkcount(fs, datablocks, length); 7025 freeblks->fb_len = length; 7026 } 7027 if ((flags & IO_EXT) != 0) { 7028 for (i = 0; i < UFS_NXADDR; i++) 7029 setup_freeext(freeblks, ip, i, needj); 7030 ip->i_din2->di_extsize = 0; 7031 datablocks += extblocks; 7032 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE); 7033 } 7034 #ifdef QUOTA 7035 /* Reference the quotas in case the block count is wrong in the end. */ 7036 quotaref(vp, freeblks->fb_quota); 7037 (void) chkdq(ip, -datablocks, NOCRED, FORCE); 7038 #endif 7039 freeblks->fb_chkcnt = -datablocks; 7040 UFS_LOCK(ump); 7041 fs->fs_pendingblocks += datablocks; 7042 UFS_UNLOCK(ump); 7043 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 7044 /* 7045 * Handle truncation of incomplete alloc direct dependencies. We 7046 * hold the inode block locked to prevent incomplete dependencies 7047 * from reaching the disk while we are eliminating those that 7048 * have been truncated. This is a partially inlined ffs_update(). 7049 */ 7050 ufs_itimes(vp); 7051 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED); 7052 dbn = fsbtodb(fs, ino_to_fsba(fs, ip->i_number)); 7053 error = ffs_breadz(ump, ump->um_devvp, dbn, dbn, (int)fs->fs_bsize, 7054 NULL, NULL, 0, cred, 0, NULL, &bp); 7055 if (error) { 7056 softdep_error("softdep_journal_freeblocks", error); 7057 return; 7058 } 7059 if (bp->b_bufsize == fs->fs_bsize) 7060 bp->b_flags |= B_CLUSTEROK; 7061 softdep_update_inodeblock(ip, bp, 0); 7062 if (ump->um_fstype == UFS1) { 7063 *((struct ufs1_dinode *)bp->b_data + 7064 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1; 7065 } else { 7066 ffs_update_dinode_ckhash(fs, ip->i_din2); 7067 *((struct ufs2_dinode *)bp->b_data + 7068 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2; 7069 } 7070 ACQUIRE_LOCK(ump); 7071 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 7072 if ((inodedep->id_state & IOSTARTED) != 0) 7073 panic("softdep_setup_freeblocks: inode busy"); 7074 /* 7075 * Add the freeblks structure to the list of operations that 7076 * must await the zero'ed inode being written to disk. If we 7077 * still have a bitmap dependency (needj), then the inode 7078 * has never been written to disk, so we can process the 7079 * freeblks below once we have deleted the dependencies. 7080 */ 7081 if (needj) 7082 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 7083 else 7084 freeblks->fb_state |= COMPLETE; 7085 if ((flags & IO_NORMAL) != 0) { 7086 TAILQ_FOREACH_SAFE(adp, &inodedep->id_inoupdt, ad_next, adpn) { 7087 if (adp->ad_offset > iboff) 7088 cancel_allocdirect(&inodedep->id_inoupdt, adp, 7089 freeblks); 7090 /* 7091 * Truncate the allocdirect. We could eliminate 7092 * or modify journal records as well. 7093 */ 7094 else if (adp->ad_offset == iboff && frags) 7095 adp->ad_newsize = frags; 7096 } 7097 } 7098 if ((flags & IO_EXT) != 0) 7099 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 7100 cancel_allocdirect(&inodedep->id_extupdt, adp, 7101 freeblks); 7102 /* 7103 * Scan the bufwait list for newblock dependencies that will never 7104 * make it to disk. 7105 */ 7106 LIST_FOREACH_SAFE(wk, &inodedep->id_bufwait, wk_list, wkn) { 7107 if (wk->wk_type != D_ALLOCDIRECT) 7108 continue; 7109 adp = WK_ALLOCDIRECT(wk); 7110 if (((flags & IO_NORMAL) != 0 && (adp->ad_offset > iboff)) || 7111 ((flags & IO_EXT) != 0 && (adp->ad_state & EXTDATA))) { 7112 cancel_jfreeblk(freeblks, adp->ad_newblkno); 7113 cancel_newblk(WK_NEWBLK(wk), NULL, &freeblks->fb_jwork); 7114 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 7115 } 7116 } 7117 /* 7118 * Add journal work. 7119 */ 7120 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) 7121 add_to_journal(&jblkdep->jb_list); 7122 FREE_LOCK(ump); 7123 bdwrite(bp); 7124 /* 7125 * Truncate dependency structures beyond length. 7126 */ 7127 trunc_dependencies(ip, freeblks, lastlbn, frags, flags); 7128 /* 7129 * This is only set when we need to allocate a fragment because 7130 * none existed at the end of a frag-sized file. It handles only 7131 * allocating a new, zero filled block. 7132 */ 7133 if (allocblock) { 7134 ip->i_size = length - lastoff; 7135 DIP_SET(ip, i_size, ip->i_size); 7136 error = UFS_BALLOC(vp, length - 1, 1, cred, BA_CLRBUF, &bp); 7137 if (error != 0) { 7138 softdep_error("softdep_journal_freeblks", error); 7139 return; 7140 } 7141 ip->i_size = length; 7142 DIP_SET(ip, i_size, length); 7143 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE); 7144 allocbuf(bp, frags); 7145 ffs_update(vp, 0); 7146 bawrite(bp); 7147 } else if (lastoff != 0 && vp->v_type != VDIR) { 7148 int size; 7149 7150 /* 7151 * Zero the end of a truncated frag or block. 7152 */ 7153 size = sblksize(fs, length, lastlbn); 7154 error = bread(vp, lastlbn, size, cred, &bp); 7155 if (error == 0) { 7156 bzero((char *)bp->b_data + lastoff, size - lastoff); 7157 bawrite(bp); 7158 } else if (!ffs_fsfail_cleanup(ump, error)) { 7159 softdep_error("softdep_journal_freeblks", error); 7160 return; 7161 } 7162 } 7163 ACQUIRE_LOCK(ump); 7164 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 7165 TAILQ_INSERT_TAIL(&inodedep->id_freeblklst, freeblks, fb_next); 7166 freeblks->fb_state |= DEPCOMPLETE | ONDEPLIST; 7167 /* 7168 * We zero earlier truncations so they don't erroneously 7169 * update i_blocks. 7170 */ 7171 if (freeblks->fb_len == 0 && (flags & IO_NORMAL) != 0) 7172 TAILQ_FOREACH(fbn, &inodedep->id_freeblklst, fb_next) 7173 fbn->fb_len = 0; 7174 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE && 7175 LIST_EMPTY(&freeblks->fb_jblkdephd)) 7176 freeblks->fb_state |= INPROGRESS; 7177 else 7178 freeblks = NULL; 7179 FREE_LOCK(ump); 7180 if (freeblks) 7181 handle_workitem_freeblocks(freeblks, 0); 7182 trunc_pages(ip, length, extblocks, flags); 7183 7184 } 7185 7186 /* 7187 * Flush a JOP_SYNC to the journal. 7188 */ 7189 void 7190 softdep_journal_fsync(ip) 7191 struct inode *ip; 7192 { 7193 struct jfsync *jfsync; 7194 struct ufsmount *ump; 7195 7196 ump = ITOUMP(ip); 7197 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 7198 ("softdep_journal_fsync called on non-softdep filesystem")); 7199 if ((ip->i_flag & IN_TRUNCATED) == 0) 7200 return; 7201 ip->i_flag &= ~IN_TRUNCATED; 7202 jfsync = malloc(sizeof(*jfsync), M_JFSYNC, M_SOFTDEP_FLAGS | M_ZERO); 7203 workitem_alloc(&jfsync->jfs_list, D_JFSYNC, UFSTOVFS(ump)); 7204 jfsync->jfs_size = ip->i_size; 7205 jfsync->jfs_ino = ip->i_number; 7206 ACQUIRE_LOCK(ump); 7207 add_to_journal(&jfsync->jfs_list); 7208 jwait(&jfsync->jfs_list, MNT_WAIT); 7209 FREE_LOCK(ump); 7210 } 7211 7212 /* 7213 * Block de-allocation dependencies. 7214 * 7215 * When blocks are de-allocated, the on-disk pointers must be nullified before 7216 * the blocks are made available for use by other files. (The true 7217 * requirement is that old pointers must be nullified before new on-disk 7218 * pointers are set. We chose this slightly more stringent requirement to 7219 * reduce complexity.) Our implementation handles this dependency by updating 7220 * the inode (or indirect block) appropriately but delaying the actual block 7221 * de-allocation (i.e., freemap and free space count manipulation) until 7222 * after the updated versions reach stable storage. After the disk is 7223 * updated, the blocks can be safely de-allocated whenever it is convenient. 7224 * This implementation handles only the common case of reducing a file's 7225 * length to zero. Other cases are handled by the conventional synchronous 7226 * write approach. 7227 * 7228 * The ffs implementation with which we worked double-checks 7229 * the state of the block pointers and file size as it reduces 7230 * a file's length. Some of this code is replicated here in our 7231 * soft updates implementation. The freeblks->fb_chkcnt field is 7232 * used to transfer a part of this information to the procedure 7233 * that eventually de-allocates the blocks. 7234 * 7235 * This routine should be called from the routine that shortens 7236 * a file's length, before the inode's size or block pointers 7237 * are modified. It will save the block pointer information for 7238 * later release and zero the inode so that the calling routine 7239 * can release it. 7240 */ 7241 void 7242 softdep_setup_freeblocks(ip, length, flags) 7243 struct inode *ip; /* The inode whose length is to be reduced */ 7244 off_t length; /* The new length for the file */ 7245 int flags; /* IO_EXT and/or IO_NORMAL */ 7246 { 7247 struct ufs1_dinode *dp1; 7248 struct ufs2_dinode *dp2; 7249 struct freeblks *freeblks; 7250 struct inodedep *inodedep; 7251 struct allocdirect *adp; 7252 struct ufsmount *ump; 7253 struct buf *bp; 7254 struct fs *fs; 7255 ufs2_daddr_t extblocks, datablocks; 7256 struct mount *mp; 7257 int i, delay, error; 7258 ufs_lbn_t tmpval; 7259 ufs_lbn_t lbn; 7260 7261 ump = ITOUMP(ip); 7262 mp = UFSTOVFS(ump); 7263 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 7264 ("softdep_setup_freeblocks called on non-softdep filesystem")); 7265 CTR2(KTR_SUJ, "softdep_setup_freeblks: ip %d length %ld", 7266 ip->i_number, length); 7267 KASSERT(length == 0, ("softdep_setup_freeblocks: non-zero length")); 7268 fs = ump->um_fs; 7269 if ((error = bread(ump->um_devvp, 7270 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 7271 (int)fs->fs_bsize, NOCRED, &bp)) != 0) { 7272 if (!ffs_fsfail_cleanup(ump, error)) 7273 softdep_error("softdep_setup_freeblocks", error); 7274 return; 7275 } 7276 freeblks = newfreeblks(mp, ip); 7277 extblocks = 0; 7278 datablocks = 0; 7279 if (fs->fs_magic == FS_UFS2_MAGIC) 7280 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 7281 if ((flags & IO_NORMAL) != 0) { 7282 for (i = 0; i < UFS_NDADDR; i++) 7283 setup_freedirect(freeblks, ip, i, 0); 7284 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR; 7285 i < UFS_NIADDR; 7286 i++, lbn += tmpval, tmpval *= NINDIR(fs)) 7287 setup_freeindir(freeblks, ip, i, -lbn -i, 0); 7288 ip->i_size = 0; 7289 DIP_SET(ip, i_size, 0); 7290 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE); 7291 datablocks = DIP(ip, i_blocks) - extblocks; 7292 } 7293 if ((flags & IO_EXT) != 0) { 7294 for (i = 0; i < UFS_NXADDR; i++) 7295 setup_freeext(freeblks, ip, i, 0); 7296 ip->i_din2->di_extsize = 0; 7297 datablocks += extblocks; 7298 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE); 7299 } 7300 #ifdef QUOTA 7301 /* Reference the quotas in case the block count is wrong in the end. */ 7302 quotaref(ITOV(ip), freeblks->fb_quota); 7303 (void) chkdq(ip, -datablocks, NOCRED, FORCE); 7304 #endif 7305 freeblks->fb_chkcnt = -datablocks; 7306 UFS_LOCK(ump); 7307 fs->fs_pendingblocks += datablocks; 7308 UFS_UNLOCK(ump); 7309 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 7310 /* 7311 * Push the zero'ed inode to its disk buffer so that we are free 7312 * to delete its dependencies below. Once the dependencies are gone 7313 * the buffer can be safely released. 7314 */ 7315 if (ump->um_fstype == UFS1) { 7316 dp1 = ((struct ufs1_dinode *)bp->b_data + 7317 ino_to_fsbo(fs, ip->i_number)); 7318 ip->i_din1->di_freelink = dp1->di_freelink; 7319 *dp1 = *ip->i_din1; 7320 } else { 7321 dp2 = ((struct ufs2_dinode *)bp->b_data + 7322 ino_to_fsbo(fs, ip->i_number)); 7323 ip->i_din2->di_freelink = dp2->di_freelink; 7324 ffs_update_dinode_ckhash(fs, ip->i_din2); 7325 *dp2 = *ip->i_din2; 7326 } 7327 /* 7328 * Find and eliminate any inode dependencies. 7329 */ 7330 ACQUIRE_LOCK(ump); 7331 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 7332 if ((inodedep->id_state & IOSTARTED) != 0) 7333 panic("softdep_setup_freeblocks: inode busy"); 7334 /* 7335 * Add the freeblks structure to the list of operations that 7336 * must await the zero'ed inode being written to disk. If we 7337 * still have a bitmap dependency (delay == 0), then the inode 7338 * has never been written to disk, so we can process the 7339 * freeblks below once we have deleted the dependencies. 7340 */ 7341 delay = (inodedep->id_state & DEPCOMPLETE); 7342 if (delay) 7343 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 7344 else 7345 freeblks->fb_state |= COMPLETE; 7346 /* 7347 * Because the file length has been truncated to zero, any 7348 * pending block allocation dependency structures associated 7349 * with this inode are obsolete and can simply be de-allocated. 7350 * We must first merge the two dependency lists to get rid of 7351 * any duplicate freefrag structures, then purge the merged list. 7352 * If we still have a bitmap dependency, then the inode has never 7353 * been written to disk, so we can free any fragments without delay. 7354 */ 7355 if (flags & IO_NORMAL) { 7356 merge_inode_lists(&inodedep->id_newinoupdt, 7357 &inodedep->id_inoupdt); 7358 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 7359 cancel_allocdirect(&inodedep->id_inoupdt, adp, 7360 freeblks); 7361 } 7362 if (flags & IO_EXT) { 7363 merge_inode_lists(&inodedep->id_newextupdt, 7364 &inodedep->id_extupdt); 7365 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 7366 cancel_allocdirect(&inodedep->id_extupdt, adp, 7367 freeblks); 7368 } 7369 FREE_LOCK(ump); 7370 bdwrite(bp); 7371 trunc_dependencies(ip, freeblks, -1, 0, flags); 7372 ACQUIRE_LOCK(ump); 7373 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 7374 (void) free_inodedep(inodedep); 7375 freeblks->fb_state |= DEPCOMPLETE; 7376 /* 7377 * If the inode with zeroed block pointers is now on disk 7378 * we can start freeing blocks. 7379 */ 7380 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 7381 freeblks->fb_state |= INPROGRESS; 7382 else 7383 freeblks = NULL; 7384 FREE_LOCK(ump); 7385 if (freeblks) 7386 handle_workitem_freeblocks(freeblks, 0); 7387 trunc_pages(ip, length, extblocks, flags); 7388 } 7389 7390 /* 7391 * Eliminate pages from the page cache that back parts of this inode and 7392 * adjust the vnode pager's idea of our size. This prevents stale data 7393 * from hanging around in the page cache. 7394 */ 7395 static void 7396 trunc_pages(ip, length, extblocks, flags) 7397 struct inode *ip; 7398 off_t length; 7399 ufs2_daddr_t extblocks; 7400 int flags; 7401 { 7402 struct vnode *vp; 7403 struct fs *fs; 7404 ufs_lbn_t lbn; 7405 off_t end, extend; 7406 7407 vp = ITOV(ip); 7408 fs = ITOFS(ip); 7409 extend = OFF_TO_IDX(lblktosize(fs, -extblocks)); 7410 if ((flags & IO_EXT) != 0) 7411 vn_pages_remove(vp, extend, 0); 7412 if ((flags & IO_NORMAL) == 0) 7413 return; 7414 BO_LOCK(&vp->v_bufobj); 7415 drain_output(vp); 7416 BO_UNLOCK(&vp->v_bufobj); 7417 /* 7418 * The vnode pager eliminates file pages we eliminate indirects 7419 * below. 7420 */ 7421 vnode_pager_setsize(vp, length); 7422 /* 7423 * Calculate the end based on the last indirect we want to keep. If 7424 * the block extends into indirects we can just use the negative of 7425 * its lbn. Doubles and triples exist at lower numbers so we must 7426 * be careful not to remove those, if they exist. double and triple 7427 * indirect lbns do not overlap with others so it is not important 7428 * to verify how many levels are required. 7429 */ 7430 lbn = lblkno(fs, length); 7431 if (lbn >= UFS_NDADDR) { 7432 /* Calculate the virtual lbn of the triple indirect. */ 7433 lbn = -lbn - (UFS_NIADDR - 1); 7434 end = OFF_TO_IDX(lblktosize(fs, lbn)); 7435 } else 7436 end = extend; 7437 vn_pages_remove(vp, OFF_TO_IDX(OFF_MAX), end); 7438 } 7439 7440 /* 7441 * See if the buf bp is in the range eliminated by truncation. 7442 */ 7443 static int 7444 trunc_check_buf(bp, blkoffp, lastlbn, lastoff, flags) 7445 struct buf *bp; 7446 int *blkoffp; 7447 ufs_lbn_t lastlbn; 7448 int lastoff; 7449 int flags; 7450 { 7451 ufs_lbn_t lbn; 7452 7453 *blkoffp = 0; 7454 /* Only match ext/normal blocks as appropriate. */ 7455 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) || 7456 ((flags & IO_NORMAL) == 0 && (bp->b_xflags & BX_ALTDATA) == 0)) 7457 return (0); 7458 /* ALTDATA is always a full truncation. */ 7459 if ((bp->b_xflags & BX_ALTDATA) != 0) 7460 return (1); 7461 /* -1 is full truncation. */ 7462 if (lastlbn == -1) 7463 return (1); 7464 /* 7465 * If this is a partial truncate we only want those 7466 * blocks and indirect blocks that cover the range 7467 * we're after. 7468 */ 7469 lbn = bp->b_lblkno; 7470 if (lbn < 0) 7471 lbn = -(lbn + lbn_level(lbn)); 7472 if (lbn < lastlbn) 7473 return (0); 7474 /* Here we only truncate lblkno if it's partial. */ 7475 if (lbn == lastlbn) { 7476 if (lastoff == 0) 7477 return (0); 7478 *blkoffp = lastoff; 7479 } 7480 return (1); 7481 } 7482 7483 /* 7484 * Eliminate any dependencies that exist in memory beyond lblkno:off 7485 */ 7486 static void 7487 trunc_dependencies(ip, freeblks, lastlbn, lastoff, flags) 7488 struct inode *ip; 7489 struct freeblks *freeblks; 7490 ufs_lbn_t lastlbn; 7491 int lastoff; 7492 int flags; 7493 { 7494 struct bufobj *bo; 7495 struct vnode *vp; 7496 struct buf *bp; 7497 int blkoff; 7498 7499 /* 7500 * We must wait for any I/O in progress to finish so that 7501 * all potential buffers on the dirty list will be visible. 7502 * Once they are all there, walk the list and get rid of 7503 * any dependencies. 7504 */ 7505 vp = ITOV(ip); 7506 bo = &vp->v_bufobj; 7507 BO_LOCK(bo); 7508 drain_output(vp); 7509 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) 7510 bp->b_vflags &= ~BV_SCANNED; 7511 restart: 7512 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) { 7513 if (bp->b_vflags & BV_SCANNED) 7514 continue; 7515 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 7516 bp->b_vflags |= BV_SCANNED; 7517 continue; 7518 } 7519 KASSERT(bp->b_bufobj == bo, ("Wrong object in buffer")); 7520 if ((bp = getdirtybuf(bp, BO_LOCKPTR(bo), MNT_WAIT)) == NULL) 7521 goto restart; 7522 BO_UNLOCK(bo); 7523 if (deallocate_dependencies(bp, freeblks, blkoff)) 7524 bqrelse(bp); 7525 else 7526 brelse(bp); 7527 BO_LOCK(bo); 7528 goto restart; 7529 } 7530 /* 7531 * Now do the work of vtruncbuf while also matching indirect blocks. 7532 */ 7533 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) 7534 bp->b_vflags &= ~BV_SCANNED; 7535 cleanrestart: 7536 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) { 7537 if (bp->b_vflags & BV_SCANNED) 7538 continue; 7539 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 7540 bp->b_vflags |= BV_SCANNED; 7541 continue; 7542 } 7543 if (BUF_LOCK(bp, 7544 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 7545 BO_LOCKPTR(bo)) == ENOLCK) { 7546 BO_LOCK(bo); 7547 goto cleanrestart; 7548 } 7549 bp->b_vflags |= BV_SCANNED; 7550 bremfree(bp); 7551 if (blkoff != 0) { 7552 allocbuf(bp, blkoff); 7553 bqrelse(bp); 7554 } else { 7555 bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF; 7556 brelse(bp); 7557 } 7558 BO_LOCK(bo); 7559 goto cleanrestart; 7560 } 7561 drain_output(vp); 7562 BO_UNLOCK(bo); 7563 } 7564 7565 static int 7566 cancel_pagedep(pagedep, freeblks, blkoff) 7567 struct pagedep *pagedep; 7568 struct freeblks *freeblks; 7569 int blkoff; 7570 { 7571 struct jremref *jremref; 7572 struct jmvref *jmvref; 7573 struct dirrem *dirrem, *tmp; 7574 int i; 7575 7576 /* 7577 * Copy any directory remove dependencies to the list 7578 * to be processed after the freeblks proceeds. If 7579 * directory entry never made it to disk they 7580 * can be dumped directly onto the work list. 7581 */ 7582 LIST_FOREACH_SAFE(dirrem, &pagedep->pd_dirremhd, dm_next, tmp) { 7583 /* Skip this directory removal if it is intended to remain. */ 7584 if (dirrem->dm_offset < blkoff) 7585 continue; 7586 /* 7587 * If there are any dirrems we wait for the journal write 7588 * to complete and then restart the buf scan as the lock 7589 * has been dropped. 7590 */ 7591 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) { 7592 jwait(&jremref->jr_list, MNT_WAIT); 7593 return (ERESTART); 7594 } 7595 LIST_REMOVE(dirrem, dm_next); 7596 dirrem->dm_dirinum = pagedep->pd_ino; 7597 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list); 7598 } 7599 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) { 7600 jwait(&jmvref->jm_list, MNT_WAIT); 7601 return (ERESTART); 7602 } 7603 /* 7604 * When we're partially truncating a pagedep we just want to flush 7605 * journal entries and return. There can not be any adds in the 7606 * truncated portion of the directory and newblk must remain if 7607 * part of the block remains. 7608 */ 7609 if (blkoff != 0) { 7610 struct diradd *dap; 7611 7612 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 7613 if (dap->da_offset > blkoff) 7614 panic("cancel_pagedep: diradd %p off %d > %d", 7615 dap, dap->da_offset, blkoff); 7616 for (i = 0; i < DAHASHSZ; i++) 7617 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) 7618 if (dap->da_offset > blkoff) 7619 panic("cancel_pagedep: diradd %p off %d > %d", 7620 dap, dap->da_offset, blkoff); 7621 return (0); 7622 } 7623 /* 7624 * There should be no directory add dependencies present 7625 * as the directory could not be truncated until all 7626 * children were removed. 7627 */ 7628 KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL, 7629 ("deallocate_dependencies: pendinghd != NULL")); 7630 for (i = 0; i < DAHASHSZ; i++) 7631 KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL, 7632 ("deallocate_dependencies: diraddhd != NULL")); 7633 if ((pagedep->pd_state & NEWBLOCK) != 0) 7634 free_newdirblk(pagedep->pd_newdirblk); 7635 if (free_pagedep(pagedep) == 0) 7636 panic("Failed to free pagedep %p", pagedep); 7637 return (0); 7638 } 7639 7640 /* 7641 * Reclaim any dependency structures from a buffer that is about to 7642 * be reallocated to a new vnode. The buffer must be locked, thus, 7643 * no I/O completion operations can occur while we are manipulating 7644 * its associated dependencies. The mutex is held so that other I/O's 7645 * associated with related dependencies do not occur. 7646 */ 7647 static int 7648 deallocate_dependencies(bp, freeblks, off) 7649 struct buf *bp; 7650 struct freeblks *freeblks; 7651 int off; 7652 { 7653 struct indirdep *indirdep; 7654 struct pagedep *pagedep; 7655 struct worklist *wk, *wkn; 7656 struct ufsmount *ump; 7657 7658 ump = softdep_bp_to_mp(bp); 7659 if (ump == NULL) 7660 goto done; 7661 ACQUIRE_LOCK(ump); 7662 LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) { 7663 switch (wk->wk_type) { 7664 case D_INDIRDEP: 7665 indirdep = WK_INDIRDEP(wk); 7666 if (bp->b_lblkno >= 0 || 7667 bp->b_blkno != indirdep->ir_savebp->b_lblkno) 7668 panic("deallocate_dependencies: not indir"); 7669 cancel_indirdep(indirdep, bp, freeblks); 7670 continue; 7671 7672 case D_PAGEDEP: 7673 pagedep = WK_PAGEDEP(wk); 7674 if (cancel_pagedep(pagedep, freeblks, off)) { 7675 FREE_LOCK(ump); 7676 return (ERESTART); 7677 } 7678 continue; 7679 7680 case D_ALLOCINDIR: 7681 /* 7682 * Simply remove the allocindir, we'll find it via 7683 * the indirdep where we can clear pointers if 7684 * needed. 7685 */ 7686 WORKLIST_REMOVE(wk); 7687 continue; 7688 7689 case D_FREEWORK: 7690 /* 7691 * A truncation is waiting for the zero'd pointers 7692 * to be written. It can be freed when the freeblks 7693 * is journaled. 7694 */ 7695 WORKLIST_REMOVE(wk); 7696 wk->wk_state |= ONDEPLIST; 7697 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 7698 break; 7699 7700 case D_ALLOCDIRECT: 7701 if (off != 0) 7702 continue; 7703 /* FALLTHROUGH */ 7704 default: 7705 panic("deallocate_dependencies: Unexpected type %s", 7706 TYPENAME(wk->wk_type)); 7707 /* NOTREACHED */ 7708 } 7709 } 7710 FREE_LOCK(ump); 7711 done: 7712 /* 7713 * Don't throw away this buf, we were partially truncating and 7714 * some deps may always remain. 7715 */ 7716 if (off) { 7717 allocbuf(bp, off); 7718 bp->b_vflags |= BV_SCANNED; 7719 return (EBUSY); 7720 } 7721 bp->b_flags |= B_INVAL | B_NOCACHE; 7722 7723 return (0); 7724 } 7725 7726 /* 7727 * An allocdirect is being canceled due to a truncate. We must make sure 7728 * the journal entry is released in concert with the blkfree that releases 7729 * the storage. Completed journal entries must not be released until the 7730 * space is no longer pointed to by the inode or in the bitmap. 7731 */ 7732 static void 7733 cancel_allocdirect(adphead, adp, freeblks) 7734 struct allocdirectlst *adphead; 7735 struct allocdirect *adp; 7736 struct freeblks *freeblks; 7737 { 7738 struct freework *freework; 7739 struct newblk *newblk; 7740 struct worklist *wk; 7741 7742 TAILQ_REMOVE(adphead, adp, ad_next); 7743 newblk = (struct newblk *)adp; 7744 freework = NULL; 7745 /* 7746 * Find the correct freework structure. 7747 */ 7748 LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) { 7749 if (wk->wk_type != D_FREEWORK) 7750 continue; 7751 freework = WK_FREEWORK(wk); 7752 if (freework->fw_blkno == newblk->nb_newblkno) 7753 break; 7754 } 7755 if (freework == NULL) 7756 panic("cancel_allocdirect: Freework not found"); 7757 /* 7758 * If a newblk exists at all we still have the journal entry that 7759 * initiated the allocation so we do not need to journal the free. 7760 */ 7761 cancel_jfreeblk(freeblks, freework->fw_blkno); 7762 /* 7763 * If the journal hasn't been written the jnewblk must be passed 7764 * to the call to ffs_blkfree that reclaims the space. We accomplish 7765 * this by linking the journal dependency into the freework to be 7766 * freed when freework_freeblock() is called. If the journal has 7767 * been written we can simply reclaim the journal space when the 7768 * freeblks work is complete. 7769 */ 7770 freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list, 7771 &freeblks->fb_jwork); 7772 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 7773 } 7774 7775 /* 7776 * Cancel a new block allocation. May be an indirect or direct block. We 7777 * remove it from various lists and return any journal record that needs to 7778 * be resolved by the caller. 7779 * 7780 * A special consideration is made for indirects which were never pointed 7781 * at on disk and will never be found once this block is released. 7782 */ 7783 static struct jnewblk * 7784 cancel_newblk(newblk, wk, wkhd) 7785 struct newblk *newblk; 7786 struct worklist *wk; 7787 struct workhead *wkhd; 7788 { 7789 struct jnewblk *jnewblk; 7790 7791 CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno); 7792 7793 newblk->nb_state |= GOINGAWAY; 7794 /* 7795 * Previously we traversed the completedhd on each indirdep 7796 * attached to this newblk to cancel them and gather journal 7797 * work. Since we need only the oldest journal segment and 7798 * the lowest point on the tree will always have the oldest 7799 * journal segment we are free to release the segments 7800 * of any subordinates and may leave the indirdep list to 7801 * indirdep_complete() when this newblk is freed. 7802 */ 7803 if (newblk->nb_state & ONDEPLIST) { 7804 newblk->nb_state &= ~ONDEPLIST; 7805 LIST_REMOVE(newblk, nb_deps); 7806 } 7807 if (newblk->nb_state & ONWORKLIST) 7808 WORKLIST_REMOVE(&newblk->nb_list); 7809 /* 7810 * If the journal entry hasn't been written we save a pointer to 7811 * the dependency that frees it until it is written or the 7812 * superseding operation completes. 7813 */ 7814 jnewblk = newblk->nb_jnewblk; 7815 if (jnewblk != NULL && wk != NULL) { 7816 newblk->nb_jnewblk = NULL; 7817 jnewblk->jn_dep = wk; 7818 } 7819 if (!LIST_EMPTY(&newblk->nb_jwork)) 7820 jwork_move(wkhd, &newblk->nb_jwork); 7821 /* 7822 * When truncating we must free the newdirblk early to remove 7823 * the pagedep from the hash before returning. 7824 */ 7825 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7826 free_newdirblk(WK_NEWDIRBLK(wk)); 7827 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7828 panic("cancel_newblk: extra newdirblk"); 7829 7830 return (jnewblk); 7831 } 7832 7833 /* 7834 * Schedule the freefrag associated with a newblk to be released once 7835 * the pointers are written and the previous block is no longer needed. 7836 */ 7837 static void 7838 newblk_freefrag(newblk) 7839 struct newblk *newblk; 7840 { 7841 struct freefrag *freefrag; 7842 7843 if (newblk->nb_freefrag == NULL) 7844 return; 7845 freefrag = newblk->nb_freefrag; 7846 newblk->nb_freefrag = NULL; 7847 freefrag->ff_state |= COMPLETE; 7848 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 7849 add_to_worklist(&freefrag->ff_list, 0); 7850 } 7851 7852 /* 7853 * Free a newblk. Generate a new freefrag work request if appropriate. 7854 * This must be called after the inode pointer and any direct block pointers 7855 * are valid or fully removed via truncate or frag extension. 7856 */ 7857 static void 7858 free_newblk(newblk) 7859 struct newblk *newblk; 7860 { 7861 struct indirdep *indirdep; 7862 struct worklist *wk; 7863 7864 KASSERT(newblk->nb_jnewblk == NULL, 7865 ("free_newblk: jnewblk %p still attached", newblk->nb_jnewblk)); 7866 KASSERT(newblk->nb_list.wk_type != D_NEWBLK, 7867 ("free_newblk: unclaimed newblk")); 7868 LOCK_OWNED(VFSTOUFS(newblk->nb_list.wk_mp)); 7869 newblk_freefrag(newblk); 7870 if (newblk->nb_state & ONDEPLIST) 7871 LIST_REMOVE(newblk, nb_deps); 7872 if (newblk->nb_state & ONWORKLIST) 7873 WORKLIST_REMOVE(&newblk->nb_list); 7874 LIST_REMOVE(newblk, nb_hash); 7875 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7876 free_newdirblk(WK_NEWDIRBLK(wk)); 7877 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7878 panic("free_newblk: extra newdirblk"); 7879 while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL) 7880 indirdep_complete(indirdep); 7881 handle_jwork(&newblk->nb_jwork); 7882 WORKITEM_FREE(newblk, D_NEWBLK); 7883 } 7884 7885 /* 7886 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep. 7887 */ 7888 static void 7889 free_newdirblk(newdirblk) 7890 struct newdirblk *newdirblk; 7891 { 7892 struct pagedep *pagedep; 7893 struct diradd *dap; 7894 struct worklist *wk; 7895 7896 LOCK_OWNED(VFSTOUFS(newdirblk->db_list.wk_mp)); 7897 WORKLIST_REMOVE(&newdirblk->db_list); 7898 /* 7899 * If the pagedep is still linked onto the directory buffer 7900 * dependency chain, then some of the entries on the 7901 * pd_pendinghd list may not be committed to disk yet. In 7902 * this case, we will simply clear the NEWBLOCK flag and 7903 * let the pd_pendinghd list be processed when the pagedep 7904 * is next written. If the pagedep is no longer on the buffer 7905 * dependency chain, then all the entries on the pd_pending 7906 * list are committed to disk and we can free them here. 7907 */ 7908 pagedep = newdirblk->db_pagedep; 7909 pagedep->pd_state &= ~NEWBLOCK; 7910 if ((pagedep->pd_state & ONWORKLIST) == 0) { 7911 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 7912 free_diradd(dap, NULL); 7913 /* 7914 * If no dependencies remain, the pagedep will be freed. 7915 */ 7916 free_pagedep(pagedep); 7917 } 7918 /* Should only ever be one item in the list. */ 7919 while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) { 7920 WORKLIST_REMOVE(wk); 7921 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 7922 } 7923 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 7924 } 7925 7926 /* 7927 * Prepare an inode to be freed. The actual free operation is not 7928 * done until the zero'ed inode has been written to disk. 7929 */ 7930 void 7931 softdep_freefile(pvp, ino, mode) 7932 struct vnode *pvp; 7933 ino_t ino; 7934 int mode; 7935 { 7936 struct inode *ip = VTOI(pvp); 7937 struct inodedep *inodedep; 7938 struct freefile *freefile; 7939 struct freeblks *freeblks; 7940 struct ufsmount *ump; 7941 7942 ump = ITOUMP(ip); 7943 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 7944 ("softdep_freefile called on non-softdep filesystem")); 7945 /* 7946 * This sets up the inode de-allocation dependency. 7947 */ 7948 freefile = malloc(sizeof(struct freefile), 7949 M_FREEFILE, M_SOFTDEP_FLAGS); 7950 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount); 7951 freefile->fx_mode = mode; 7952 freefile->fx_oldinum = ino; 7953 freefile->fx_devvp = ump->um_devvp; 7954 LIST_INIT(&freefile->fx_jwork); 7955 UFS_LOCK(ump); 7956 ump->um_fs->fs_pendinginodes += 1; 7957 UFS_UNLOCK(ump); 7958 7959 /* 7960 * If the inodedep does not exist, then the zero'ed inode has 7961 * been written to disk. If the allocated inode has never been 7962 * written to disk, then the on-disk inode is zero'ed. In either 7963 * case we can free the file immediately. If the journal was 7964 * canceled before being written the inode will never make it to 7965 * disk and we must send the canceled journal entrys to 7966 * ffs_freefile() to be cleared in conjunction with the bitmap. 7967 * Any blocks waiting on the inode to write can be safely freed 7968 * here as it will never been written. 7969 */ 7970 ACQUIRE_LOCK(ump); 7971 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7972 if (inodedep) { 7973 /* 7974 * Clear out freeblks that no longer need to reference 7975 * this inode. 7976 */ 7977 while ((freeblks = 7978 TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) { 7979 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, 7980 fb_next); 7981 freeblks->fb_state &= ~ONDEPLIST; 7982 } 7983 /* 7984 * Remove this inode from the unlinked list. 7985 */ 7986 if (inodedep->id_state & UNLINKED) { 7987 /* 7988 * Save the journal work to be freed with the bitmap 7989 * before we clear UNLINKED. Otherwise it can be lost 7990 * if the inode block is written. 7991 */ 7992 handle_bufwait(inodedep, &freefile->fx_jwork); 7993 clear_unlinked_inodedep(inodedep); 7994 /* 7995 * Re-acquire inodedep as we've dropped the 7996 * per-filesystem lock in clear_unlinked_inodedep(). 7997 */ 7998 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7999 } 8000 } 8001 if (inodedep == NULL || check_inode_unwritten(inodedep)) { 8002 FREE_LOCK(ump); 8003 handle_workitem_freefile(freefile); 8004 return; 8005 } 8006 if ((inodedep->id_state & DEPCOMPLETE) == 0) 8007 inodedep->id_state |= GOINGAWAY; 8008 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list); 8009 FREE_LOCK(ump); 8010 if (ip->i_number == ino) 8011 UFS_INODE_SET_FLAG(ip, IN_MODIFIED); 8012 } 8013 8014 /* 8015 * Check to see if an inode has never been written to disk. If 8016 * so free the inodedep and return success, otherwise return failure. 8017 * 8018 * If we still have a bitmap dependency, then the inode has never 8019 * been written to disk. Drop the dependency as it is no longer 8020 * necessary since the inode is being deallocated. We set the 8021 * ALLCOMPLETE flags since the bitmap now properly shows that the 8022 * inode is not allocated. Even if the inode is actively being 8023 * written, it has been rolled back to its zero'ed state, so we 8024 * are ensured that a zero inode is what is on the disk. For short 8025 * lived files, this change will usually result in removing all the 8026 * dependencies from the inode so that it can be freed immediately. 8027 */ 8028 static int 8029 check_inode_unwritten(inodedep) 8030 struct inodedep *inodedep; 8031 { 8032 8033 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 8034 8035 if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 || 8036 !LIST_EMPTY(&inodedep->id_dirremhd) || 8037 !LIST_EMPTY(&inodedep->id_pendinghd) || 8038 !LIST_EMPTY(&inodedep->id_bufwait) || 8039 !LIST_EMPTY(&inodedep->id_inowait) || 8040 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 8041 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 8042 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 8043 !TAILQ_EMPTY(&inodedep->id_extupdt) || 8044 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 8045 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 8046 inodedep->id_mkdiradd != NULL || 8047 inodedep->id_nlinkdelta != 0) 8048 return (0); 8049 /* 8050 * Another process might be in initiate_write_inodeblock_ufs[12] 8051 * trying to allocate memory without holding "Softdep Lock". 8052 */ 8053 if ((inodedep->id_state & IOSTARTED) != 0 && 8054 inodedep->id_savedino1 == NULL) 8055 return (0); 8056 8057 if (inodedep->id_state & ONDEPLIST) 8058 LIST_REMOVE(inodedep, id_deps); 8059 inodedep->id_state &= ~ONDEPLIST; 8060 inodedep->id_state |= ALLCOMPLETE; 8061 inodedep->id_bmsafemap = NULL; 8062 if (inodedep->id_state & ONWORKLIST) 8063 WORKLIST_REMOVE(&inodedep->id_list); 8064 if (inodedep->id_savedino1 != NULL) { 8065 free(inodedep->id_savedino1, M_SAVEDINO); 8066 inodedep->id_savedino1 = NULL; 8067 } 8068 if (free_inodedep(inodedep) == 0) 8069 panic("check_inode_unwritten: busy inode"); 8070 return (1); 8071 } 8072 8073 static int 8074 check_inodedep_free(inodedep) 8075 struct inodedep *inodedep; 8076 { 8077 8078 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 8079 if ((inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE || 8080 !LIST_EMPTY(&inodedep->id_dirremhd) || 8081 !LIST_EMPTY(&inodedep->id_pendinghd) || 8082 !LIST_EMPTY(&inodedep->id_bufwait) || 8083 !LIST_EMPTY(&inodedep->id_inowait) || 8084 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 8085 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 8086 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 8087 !TAILQ_EMPTY(&inodedep->id_extupdt) || 8088 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 8089 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 8090 inodedep->id_mkdiradd != NULL || 8091 inodedep->id_nlinkdelta != 0 || 8092 inodedep->id_savedino1 != NULL) 8093 return (0); 8094 return (1); 8095 } 8096 8097 /* 8098 * Try to free an inodedep structure. Return 1 if it could be freed. 8099 */ 8100 static int 8101 free_inodedep(inodedep) 8102 struct inodedep *inodedep; 8103 { 8104 8105 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 8106 if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 || 8107 !check_inodedep_free(inodedep)) 8108 return (0); 8109 if (inodedep->id_state & ONDEPLIST) 8110 LIST_REMOVE(inodedep, id_deps); 8111 LIST_REMOVE(inodedep, id_hash); 8112 WORKITEM_FREE(inodedep, D_INODEDEP); 8113 return (1); 8114 } 8115 8116 /* 8117 * Free the block referenced by a freework structure. The parent freeblks 8118 * structure is released and completed when the final cg bitmap reaches 8119 * the disk. This routine may be freeing a jnewblk which never made it to 8120 * disk in which case we do not have to wait as the operation is undone 8121 * in memory immediately. 8122 */ 8123 static void 8124 freework_freeblock(freework, key) 8125 struct freework *freework; 8126 u_long key; 8127 { 8128 struct freeblks *freeblks; 8129 struct jnewblk *jnewblk; 8130 struct ufsmount *ump; 8131 struct workhead wkhd; 8132 struct fs *fs; 8133 int bsize; 8134 int needj; 8135 8136 ump = VFSTOUFS(freework->fw_list.wk_mp); 8137 LOCK_OWNED(ump); 8138 /* 8139 * Handle partial truncate separately. 8140 */ 8141 if (freework->fw_indir) { 8142 complete_trunc_indir(freework); 8143 return; 8144 } 8145 freeblks = freework->fw_freeblks; 8146 fs = ump->um_fs; 8147 needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0; 8148 bsize = lfragtosize(fs, freework->fw_frags); 8149 LIST_INIT(&wkhd); 8150 /* 8151 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives 8152 * on the indirblk hashtable and prevents premature freeing. 8153 */ 8154 freework->fw_state |= DEPCOMPLETE; 8155 /* 8156 * SUJ needs to wait for the segment referencing freed indirect 8157 * blocks to expire so that we know the checker will not confuse 8158 * a re-allocated indirect block with its old contents. 8159 */ 8160 if (needj && freework->fw_lbn <= -UFS_NDADDR) 8161 indirblk_insert(freework); 8162 /* 8163 * If we are canceling an existing jnewblk pass it to the free 8164 * routine, otherwise pass the freeblk which will ultimately 8165 * release the freeblks. If we're not journaling, we can just 8166 * free the freeblks immediately. 8167 */ 8168 jnewblk = freework->fw_jnewblk; 8169 if (jnewblk != NULL) { 8170 cancel_jnewblk(jnewblk, &wkhd); 8171 needj = 0; 8172 } else if (needj) { 8173 freework->fw_state |= DELAYEDFREE; 8174 freeblks->fb_cgwait++; 8175 WORKLIST_INSERT(&wkhd, &freework->fw_list); 8176 } 8177 FREE_LOCK(ump); 8178 freeblks_free(ump, freeblks, btodb(bsize)); 8179 CTR4(KTR_SUJ, 8180 "freework_freeblock: ino %jd blkno %jd lbn %jd size %d", 8181 freeblks->fb_inum, freework->fw_blkno, freework->fw_lbn, bsize); 8182 ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, bsize, 8183 freeblks->fb_inum, freeblks->fb_vtype, &wkhd, key); 8184 ACQUIRE_LOCK(ump); 8185 /* 8186 * The jnewblk will be discarded and the bits in the map never 8187 * made it to disk. We can immediately free the freeblk. 8188 */ 8189 if (needj == 0) 8190 handle_written_freework(freework); 8191 } 8192 8193 /* 8194 * We enqueue freework items that need processing back on the freeblks and 8195 * add the freeblks to the worklist. This makes it easier to find all work 8196 * required to flush a truncation in process_truncates(). 8197 */ 8198 static void 8199 freework_enqueue(freework) 8200 struct freework *freework; 8201 { 8202 struct freeblks *freeblks; 8203 8204 freeblks = freework->fw_freeblks; 8205 if ((freework->fw_state & INPROGRESS) == 0) 8206 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 8207 if ((freeblks->fb_state & 8208 (ONWORKLIST | INPROGRESS | ALLCOMPLETE)) == ALLCOMPLETE && 8209 LIST_EMPTY(&freeblks->fb_jblkdephd)) 8210 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 8211 } 8212 8213 /* 8214 * Start, continue, or finish the process of freeing an indirect block tree. 8215 * The free operation may be paused at any point with fw_off containing the 8216 * offset to restart from. This enables us to implement some flow control 8217 * for large truncates which may fan out and generate a huge number of 8218 * dependencies. 8219 */ 8220 static void 8221 handle_workitem_indirblk(freework) 8222 struct freework *freework; 8223 { 8224 struct freeblks *freeblks; 8225 struct ufsmount *ump; 8226 struct fs *fs; 8227 8228 freeblks = freework->fw_freeblks; 8229 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8230 fs = ump->um_fs; 8231 if (freework->fw_state & DEPCOMPLETE) { 8232 handle_written_freework(freework); 8233 return; 8234 } 8235 if (freework->fw_off == NINDIR(fs)) { 8236 freework_freeblock(freework, SINGLETON_KEY); 8237 return; 8238 } 8239 freework->fw_state |= INPROGRESS; 8240 FREE_LOCK(ump); 8241 indir_trunc(freework, fsbtodb(fs, freework->fw_blkno), 8242 freework->fw_lbn); 8243 ACQUIRE_LOCK(ump); 8244 } 8245 8246 /* 8247 * Called when a freework structure attached to a cg buf is written. The 8248 * ref on either the parent or the freeblks structure is released and 8249 * the freeblks is added back to the worklist if there is more work to do. 8250 */ 8251 static void 8252 handle_written_freework(freework) 8253 struct freework *freework; 8254 { 8255 struct freeblks *freeblks; 8256 struct freework *parent; 8257 8258 freeblks = freework->fw_freeblks; 8259 parent = freework->fw_parent; 8260 if (freework->fw_state & DELAYEDFREE) 8261 freeblks->fb_cgwait--; 8262 freework->fw_state |= COMPLETE; 8263 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 8264 WORKITEM_FREE(freework, D_FREEWORK); 8265 if (parent) { 8266 if (--parent->fw_ref == 0) 8267 freework_enqueue(parent); 8268 return; 8269 } 8270 if (--freeblks->fb_ref != 0) 8271 return; 8272 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) == 8273 ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd)) 8274 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 8275 } 8276 8277 /* 8278 * This workitem routine performs the block de-allocation. 8279 * The workitem is added to the pending list after the updated 8280 * inode block has been written to disk. As mentioned above, 8281 * checks regarding the number of blocks de-allocated (compared 8282 * to the number of blocks allocated for the file) are also 8283 * performed in this function. 8284 */ 8285 static int 8286 handle_workitem_freeblocks(freeblks, flags) 8287 struct freeblks *freeblks; 8288 int flags; 8289 { 8290 struct freework *freework; 8291 struct newblk *newblk; 8292 struct allocindir *aip; 8293 struct ufsmount *ump; 8294 struct worklist *wk; 8295 u_long key; 8296 8297 KASSERT(LIST_EMPTY(&freeblks->fb_jblkdephd), 8298 ("handle_workitem_freeblocks: Journal entries not written.")); 8299 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8300 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum); 8301 ACQUIRE_LOCK(ump); 8302 while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) { 8303 WORKLIST_REMOVE(wk); 8304 switch (wk->wk_type) { 8305 case D_DIRREM: 8306 wk->wk_state |= COMPLETE; 8307 add_to_worklist(wk, 0); 8308 continue; 8309 8310 case D_ALLOCDIRECT: 8311 free_newblk(WK_NEWBLK(wk)); 8312 continue; 8313 8314 case D_ALLOCINDIR: 8315 aip = WK_ALLOCINDIR(wk); 8316 freework = NULL; 8317 if (aip->ai_state & DELAYEDFREE) { 8318 FREE_LOCK(ump); 8319 freework = newfreework(ump, freeblks, NULL, 8320 aip->ai_lbn, aip->ai_newblkno, 8321 ump->um_fs->fs_frag, 0, 0); 8322 ACQUIRE_LOCK(ump); 8323 } 8324 newblk = WK_NEWBLK(wk); 8325 if (newblk->nb_jnewblk) { 8326 freework->fw_jnewblk = newblk->nb_jnewblk; 8327 newblk->nb_jnewblk->jn_dep = &freework->fw_list; 8328 newblk->nb_jnewblk = NULL; 8329 } 8330 free_newblk(newblk); 8331 continue; 8332 8333 case D_FREEWORK: 8334 freework = WK_FREEWORK(wk); 8335 if (freework->fw_lbn <= -UFS_NDADDR) 8336 handle_workitem_indirblk(freework); 8337 else 8338 freework_freeblock(freework, key); 8339 continue; 8340 default: 8341 panic("handle_workitem_freeblocks: Unknown type %s", 8342 TYPENAME(wk->wk_type)); 8343 } 8344 } 8345 if (freeblks->fb_ref != 0) { 8346 freeblks->fb_state &= ~INPROGRESS; 8347 wake_worklist(&freeblks->fb_list); 8348 freeblks = NULL; 8349 } 8350 FREE_LOCK(ump); 8351 ffs_blkrelease_finish(ump, key); 8352 if (freeblks) 8353 return handle_complete_freeblocks(freeblks, flags); 8354 return (0); 8355 } 8356 8357 /* 8358 * Handle completion of block free via truncate. This allows fs_pending 8359 * to track the actual free block count more closely than if we only updated 8360 * it at the end. We must be careful to handle cases where the block count 8361 * on free was incorrect. 8362 */ 8363 static void 8364 freeblks_free(ump, freeblks, blocks) 8365 struct ufsmount *ump; 8366 struct freeblks *freeblks; 8367 int blocks; 8368 { 8369 struct fs *fs; 8370 ufs2_daddr_t remain; 8371 8372 UFS_LOCK(ump); 8373 remain = -freeblks->fb_chkcnt; 8374 freeblks->fb_chkcnt += blocks; 8375 if (remain > 0) { 8376 if (remain < blocks) 8377 blocks = remain; 8378 fs = ump->um_fs; 8379 fs->fs_pendingblocks -= blocks; 8380 } 8381 UFS_UNLOCK(ump); 8382 } 8383 8384 /* 8385 * Once all of the freework workitems are complete we can retire the 8386 * freeblocks dependency and any journal work awaiting completion. This 8387 * can not be called until all other dependencies are stable on disk. 8388 */ 8389 static int 8390 handle_complete_freeblocks(freeblks, flags) 8391 struct freeblks *freeblks; 8392 int flags; 8393 { 8394 struct inodedep *inodedep; 8395 struct inode *ip; 8396 struct vnode *vp; 8397 struct fs *fs; 8398 struct ufsmount *ump; 8399 ufs2_daddr_t spare; 8400 8401 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8402 fs = ump->um_fs; 8403 flags = LK_EXCLUSIVE | flags; 8404 spare = freeblks->fb_chkcnt; 8405 8406 /* 8407 * If we did not release the expected number of blocks we may have 8408 * to adjust the inode block count here. Only do so if it wasn't 8409 * a truncation to zero and the modrev still matches. 8410 */ 8411 if (spare && freeblks->fb_len != 0) { 8412 if (ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_inum, 8413 flags, &vp, FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP) != 0) 8414 return (EBUSY); 8415 ip = VTOI(vp); 8416 if (ip->i_mode == 0) { 8417 vgone(vp); 8418 } else if (DIP(ip, i_modrev) == freeblks->fb_modrev) { 8419 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare); 8420 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 8421 /* 8422 * We must wait so this happens before the 8423 * journal is reclaimed. 8424 */ 8425 ffs_update(vp, 1); 8426 } 8427 vput(vp); 8428 } 8429 if (spare < 0) { 8430 UFS_LOCK(ump); 8431 fs->fs_pendingblocks += spare; 8432 UFS_UNLOCK(ump); 8433 } 8434 #ifdef QUOTA 8435 /* Handle spare. */ 8436 if (spare) 8437 quotaadj(freeblks->fb_quota, ump, -spare); 8438 quotarele(freeblks->fb_quota); 8439 #endif 8440 ACQUIRE_LOCK(ump); 8441 if (freeblks->fb_state & ONDEPLIST) { 8442 inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum, 8443 0, &inodedep); 8444 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next); 8445 freeblks->fb_state &= ~ONDEPLIST; 8446 if (TAILQ_EMPTY(&inodedep->id_freeblklst)) 8447 free_inodedep(inodedep); 8448 } 8449 /* 8450 * All of the freeblock deps must be complete prior to this call 8451 * so it's now safe to complete earlier outstanding journal entries. 8452 */ 8453 handle_jwork(&freeblks->fb_jwork); 8454 WORKITEM_FREE(freeblks, D_FREEBLKS); 8455 FREE_LOCK(ump); 8456 return (0); 8457 } 8458 8459 /* 8460 * Release blocks associated with the freeblks and stored in the indirect 8461 * block dbn. If level is greater than SINGLE, the block is an indirect block 8462 * and recursive calls to indirtrunc must be used to cleanse other indirect 8463 * blocks. 8464 * 8465 * This handles partial and complete truncation of blocks. Partial is noted 8466 * with goingaway == 0. In this case the freework is completed after the 8467 * zero'd indirects are written to disk. For full truncation the freework 8468 * is completed after the block is freed. 8469 */ 8470 static void 8471 indir_trunc(freework, dbn, lbn) 8472 struct freework *freework; 8473 ufs2_daddr_t dbn; 8474 ufs_lbn_t lbn; 8475 { 8476 struct freework *nfreework; 8477 struct workhead wkhd; 8478 struct freeblks *freeblks; 8479 struct buf *bp; 8480 struct fs *fs; 8481 struct indirdep *indirdep; 8482 struct mount *mp; 8483 struct ufsmount *ump; 8484 ufs1_daddr_t *bap1; 8485 ufs2_daddr_t nb, nnb, *bap2; 8486 ufs_lbn_t lbnadd, nlbn; 8487 u_long key; 8488 int nblocks, ufs1fmt, freedblocks; 8489 int goingaway, freedeps, needj, level, cnt, i, error; 8490 8491 freeblks = freework->fw_freeblks; 8492 mp = freeblks->fb_list.wk_mp; 8493 ump = VFSTOUFS(mp); 8494 fs = ump->um_fs; 8495 /* 8496 * Get buffer of block pointers to be freed. There are three cases: 8497 * 8498 * 1) Partial truncate caches the indirdep pointer in the freework 8499 * which provides us a back copy to the save bp which holds the 8500 * pointers we want to clear. When this completes the zero 8501 * pointers are written to the real copy. 8502 * 2) The indirect is being completely truncated, cancel_indirdep() 8503 * eliminated the real copy and placed the indirdep on the saved 8504 * copy. The indirdep and buf are discarded when this completes. 8505 * 3) The indirect was not in memory, we read a copy off of the disk 8506 * using the devvp and drop and invalidate the buffer when we're 8507 * done. 8508 */ 8509 goingaway = 1; 8510 indirdep = NULL; 8511 if (freework->fw_indir != NULL) { 8512 goingaway = 0; 8513 indirdep = freework->fw_indir; 8514 bp = indirdep->ir_savebp; 8515 if (bp == NULL || bp->b_blkno != dbn) 8516 panic("indir_trunc: Bad saved buf %p blkno %jd", 8517 bp, (intmax_t)dbn); 8518 } else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) { 8519 /* 8520 * The lock prevents the buf dep list from changing and 8521 * indirects on devvp should only ever have one dependency. 8522 */ 8523 indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep)); 8524 if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0) 8525 panic("indir_trunc: Bad indirdep %p from buf %p", 8526 indirdep, bp); 8527 } else { 8528 error = ffs_breadz(ump, freeblks->fb_devvp, dbn, dbn, 8529 (int)fs->fs_bsize, NULL, NULL, 0, NOCRED, 0, NULL, &bp); 8530 if (error) 8531 return; 8532 } 8533 ACQUIRE_LOCK(ump); 8534 /* Protects against a race with complete_trunc_indir(). */ 8535 freework->fw_state &= ~INPROGRESS; 8536 /* 8537 * If we have an indirdep we need to enforce the truncation order 8538 * and discard it when it is complete. 8539 */ 8540 if (indirdep) { 8541 if (freework != TAILQ_FIRST(&indirdep->ir_trunc) && 8542 !TAILQ_EMPTY(&indirdep->ir_trunc)) { 8543 /* 8544 * Add the complete truncate to the list on the 8545 * indirdep to enforce in-order processing. 8546 */ 8547 if (freework->fw_indir == NULL) 8548 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, 8549 freework, fw_next); 8550 FREE_LOCK(ump); 8551 return; 8552 } 8553 /* 8554 * If we're goingaway, free the indirdep. Otherwise it will 8555 * linger until the write completes. 8556 */ 8557 if (goingaway) { 8558 KASSERT(indirdep->ir_savebp == bp, 8559 ("indir_trunc: losing ir_savebp %p", 8560 indirdep->ir_savebp)); 8561 indirdep->ir_savebp = NULL; 8562 free_indirdep(indirdep); 8563 } 8564 } 8565 FREE_LOCK(ump); 8566 /* Initialize pointers depending on block size. */ 8567 if (ump->um_fstype == UFS1) { 8568 bap1 = (ufs1_daddr_t *)bp->b_data; 8569 nb = bap1[freework->fw_off]; 8570 ufs1fmt = 1; 8571 bap2 = NULL; 8572 } else { 8573 bap2 = (ufs2_daddr_t *)bp->b_data; 8574 nb = bap2[freework->fw_off]; 8575 ufs1fmt = 0; 8576 bap1 = NULL; 8577 } 8578 level = lbn_level(lbn); 8579 needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0; 8580 lbnadd = lbn_offset(fs, level); 8581 nblocks = btodb(fs->fs_bsize); 8582 nfreework = freework; 8583 freedeps = 0; 8584 cnt = 0; 8585 /* 8586 * Reclaim blocks. Traverses into nested indirect levels and 8587 * arranges for the current level to be freed when subordinates 8588 * are free when journaling. 8589 */ 8590 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum); 8591 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) { 8592 if (UFS_CHECK_BLKNO(mp, freeblks->fb_inum, nb, 8593 fs->fs_bsize) != 0) 8594 nb = 0; 8595 if (i != NINDIR(fs) - 1) { 8596 if (ufs1fmt) 8597 nnb = bap1[i+1]; 8598 else 8599 nnb = bap2[i+1]; 8600 } else 8601 nnb = 0; 8602 if (nb == 0) 8603 continue; 8604 cnt++; 8605 if (level != 0) { 8606 nlbn = (lbn + 1) - (i * lbnadd); 8607 if (needj != 0) { 8608 nfreework = newfreework(ump, freeblks, freework, 8609 nlbn, nb, fs->fs_frag, 0, 0); 8610 freedeps++; 8611 } 8612 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn); 8613 } else { 8614 struct freedep *freedep; 8615 8616 /* 8617 * Attempt to aggregate freedep dependencies for 8618 * all blocks being released to the same CG. 8619 */ 8620 LIST_INIT(&wkhd); 8621 if (needj != 0 && 8622 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) { 8623 freedep = newfreedep(freework); 8624 WORKLIST_INSERT_UNLOCKED(&wkhd, 8625 &freedep->fd_list); 8626 freedeps++; 8627 } 8628 CTR3(KTR_SUJ, 8629 "indir_trunc: ino %jd blkno %jd size %d", 8630 freeblks->fb_inum, nb, fs->fs_bsize); 8631 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb, 8632 fs->fs_bsize, freeblks->fb_inum, 8633 freeblks->fb_vtype, &wkhd, key); 8634 } 8635 } 8636 ffs_blkrelease_finish(ump, key); 8637 if (goingaway) { 8638 bp->b_flags |= B_INVAL | B_NOCACHE; 8639 brelse(bp); 8640 } 8641 freedblocks = 0; 8642 if (level == 0) 8643 freedblocks = (nblocks * cnt); 8644 if (needj == 0) 8645 freedblocks += nblocks; 8646 freeblks_free(ump, freeblks, freedblocks); 8647 /* 8648 * If we are journaling set up the ref counts and offset so this 8649 * indirect can be completed when its children are free. 8650 */ 8651 if (needj) { 8652 ACQUIRE_LOCK(ump); 8653 freework->fw_off = i; 8654 freework->fw_ref += freedeps; 8655 freework->fw_ref -= NINDIR(fs) + 1; 8656 if (level == 0) 8657 freeblks->fb_cgwait += freedeps; 8658 if (freework->fw_ref == 0) 8659 freework_freeblock(freework, SINGLETON_KEY); 8660 FREE_LOCK(ump); 8661 return; 8662 } 8663 /* 8664 * If we're not journaling we can free the indirect now. 8665 */ 8666 dbn = dbtofsb(fs, dbn); 8667 CTR3(KTR_SUJ, 8668 "indir_trunc 2: ino %jd blkno %jd size %d", 8669 freeblks->fb_inum, dbn, fs->fs_bsize); 8670 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize, 8671 freeblks->fb_inum, freeblks->fb_vtype, NULL, SINGLETON_KEY); 8672 /* Non SUJ softdep does single-threaded truncations. */ 8673 if (freework->fw_blkno == dbn) { 8674 freework->fw_state |= ALLCOMPLETE; 8675 ACQUIRE_LOCK(ump); 8676 handle_written_freework(freework); 8677 FREE_LOCK(ump); 8678 } 8679 return; 8680 } 8681 8682 /* 8683 * Cancel an allocindir when it is removed via truncation. When bp is not 8684 * NULL the indirect never appeared on disk and is scheduled to be freed 8685 * independently of the indir so we can more easily track journal work. 8686 */ 8687 static void 8688 cancel_allocindir(aip, bp, freeblks, trunc) 8689 struct allocindir *aip; 8690 struct buf *bp; 8691 struct freeblks *freeblks; 8692 int trunc; 8693 { 8694 struct indirdep *indirdep; 8695 struct freefrag *freefrag; 8696 struct newblk *newblk; 8697 8698 newblk = (struct newblk *)aip; 8699 LIST_REMOVE(aip, ai_next); 8700 /* 8701 * We must eliminate the pointer in bp if it must be freed on its 8702 * own due to partial truncate or pending journal work. 8703 */ 8704 if (bp && (trunc || newblk->nb_jnewblk)) { 8705 /* 8706 * Clear the pointer and mark the aip to be freed 8707 * directly if it never existed on disk. 8708 */ 8709 aip->ai_state |= DELAYEDFREE; 8710 indirdep = aip->ai_indirdep; 8711 if (indirdep->ir_state & UFS1FMT) 8712 ((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8713 else 8714 ((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8715 } 8716 /* 8717 * When truncating the previous pointer will be freed via 8718 * savedbp. Eliminate the freefrag which would dup free. 8719 */ 8720 if (trunc && (freefrag = newblk->nb_freefrag) != NULL) { 8721 newblk->nb_freefrag = NULL; 8722 if (freefrag->ff_jdep) 8723 cancel_jfreefrag( 8724 WK_JFREEFRAG(freefrag->ff_jdep)); 8725 jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork); 8726 WORKITEM_FREE(freefrag, D_FREEFRAG); 8727 } 8728 /* 8729 * If the journal hasn't been written the jnewblk must be passed 8730 * to the call to ffs_blkfree that reclaims the space. We accomplish 8731 * this by leaving the journal dependency on the newblk to be freed 8732 * when a freework is created in handle_workitem_freeblocks(). 8733 */ 8734 cancel_newblk(newblk, NULL, &freeblks->fb_jwork); 8735 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 8736 } 8737 8738 /* 8739 * Create the mkdir dependencies for . and .. in a new directory. Link them 8740 * in to a newdirblk so any subsequent additions are tracked properly. The 8741 * caller is responsible for adding the mkdir1 dependency to the journal 8742 * and updating id_mkdiradd. This function returns with the per-filesystem 8743 * lock held. 8744 */ 8745 static struct mkdir * 8746 setup_newdir(dap, newinum, dinum, newdirbp, mkdirp) 8747 struct diradd *dap; 8748 ino_t newinum; 8749 ino_t dinum; 8750 struct buf *newdirbp; 8751 struct mkdir **mkdirp; 8752 { 8753 struct newblk *newblk; 8754 struct pagedep *pagedep; 8755 struct inodedep *inodedep; 8756 struct newdirblk *newdirblk; 8757 struct mkdir *mkdir1, *mkdir2; 8758 struct worklist *wk; 8759 struct jaddref *jaddref; 8760 struct ufsmount *ump; 8761 struct mount *mp; 8762 8763 mp = dap->da_list.wk_mp; 8764 ump = VFSTOUFS(mp); 8765 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK, 8766 M_SOFTDEP_FLAGS); 8767 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8768 LIST_INIT(&newdirblk->db_mkdir); 8769 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8770 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp); 8771 mkdir1->md_state = ATTACHED | MKDIR_BODY; 8772 mkdir1->md_diradd = dap; 8773 mkdir1->md_jaddref = NULL; 8774 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8775 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp); 8776 mkdir2->md_state = ATTACHED | MKDIR_PARENT; 8777 mkdir2->md_diradd = dap; 8778 mkdir2->md_jaddref = NULL; 8779 if (MOUNTEDSUJ(mp) == 0) { 8780 mkdir1->md_state |= DEPCOMPLETE; 8781 mkdir2->md_state |= DEPCOMPLETE; 8782 } 8783 /* 8784 * Dependency on "." and ".." being written to disk. 8785 */ 8786 mkdir1->md_buf = newdirbp; 8787 ACQUIRE_LOCK(VFSTOUFS(mp)); 8788 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir1, md_mkdirs); 8789 /* 8790 * We must link the pagedep, allocdirect, and newdirblk for 8791 * the initial file page so the pointer to the new directory 8792 * is not written until the directory contents are live and 8793 * any subsequent additions are not marked live until the 8794 * block is reachable via the inode. 8795 */ 8796 if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0) 8797 panic("setup_newdir: lost pagedep"); 8798 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list) 8799 if (wk->wk_type == D_ALLOCDIRECT) 8800 break; 8801 if (wk == NULL) 8802 panic("setup_newdir: lost allocdirect"); 8803 if (pagedep->pd_state & NEWBLOCK) 8804 panic("setup_newdir: NEWBLOCK already set"); 8805 newblk = WK_NEWBLK(wk); 8806 pagedep->pd_state |= NEWBLOCK; 8807 pagedep->pd_newdirblk = newdirblk; 8808 newdirblk->db_pagedep = pagedep; 8809 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8810 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list); 8811 /* 8812 * Look up the inodedep for the parent directory so that we 8813 * can link mkdir2 into the pending dotdot jaddref or 8814 * the inode write if there is none. If the inode is 8815 * ALLCOMPLETE and no jaddref is present all dependencies have 8816 * been satisfied and mkdir2 can be freed. 8817 */ 8818 inodedep_lookup(mp, dinum, 0, &inodedep); 8819 if (MOUNTEDSUJ(mp)) { 8820 if (inodedep == NULL) 8821 panic("setup_newdir: Lost parent."); 8822 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8823 inoreflst); 8824 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum && 8825 (jaddref->ja_state & MKDIR_PARENT), 8826 ("setup_newdir: bad dotdot jaddref %p", jaddref)); 8827 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8828 mkdir2->md_jaddref = jaddref; 8829 jaddref->ja_mkdir = mkdir2; 8830 } else if (inodedep == NULL || 8831 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 8832 dap->da_state &= ~MKDIR_PARENT; 8833 WORKITEM_FREE(mkdir2, D_MKDIR); 8834 mkdir2 = NULL; 8835 } else { 8836 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8837 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list); 8838 } 8839 *mkdirp = mkdir2; 8840 8841 return (mkdir1); 8842 } 8843 8844 /* 8845 * Directory entry addition dependencies. 8846 * 8847 * When adding a new directory entry, the inode (with its incremented link 8848 * count) must be written to disk before the directory entry's pointer to it. 8849 * Also, if the inode is newly allocated, the corresponding freemap must be 8850 * updated (on disk) before the directory entry's pointer. These requirements 8851 * are met via undo/redo on the directory entry's pointer, which consists 8852 * simply of the inode number. 8853 * 8854 * As directory entries are added and deleted, the free space within a 8855 * directory block can become fragmented. The ufs filesystem will compact 8856 * a fragmented directory block to make space for a new entry. When this 8857 * occurs, the offsets of previously added entries change. Any "diradd" 8858 * dependency structures corresponding to these entries must be updated with 8859 * the new offsets. 8860 */ 8861 8862 /* 8863 * This routine is called after the in-memory inode's link 8864 * count has been incremented, but before the directory entry's 8865 * pointer to the inode has been set. 8866 */ 8867 int 8868 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 8869 struct buf *bp; /* buffer containing directory block */ 8870 struct inode *dp; /* inode for directory */ 8871 off_t diroffset; /* offset of new entry in directory */ 8872 ino_t newinum; /* inode referenced by new directory entry */ 8873 struct buf *newdirbp; /* non-NULL => contents of new mkdir */ 8874 int isnewblk; /* entry is in a newly allocated block */ 8875 { 8876 int offset; /* offset of new entry within directory block */ 8877 ufs_lbn_t lbn; /* block in directory containing new entry */ 8878 struct fs *fs; 8879 struct diradd *dap; 8880 struct newblk *newblk; 8881 struct pagedep *pagedep; 8882 struct inodedep *inodedep; 8883 struct newdirblk *newdirblk; 8884 struct mkdir *mkdir1, *mkdir2; 8885 struct jaddref *jaddref; 8886 struct ufsmount *ump; 8887 struct mount *mp; 8888 int isindir; 8889 8890 mp = ITOVFS(dp); 8891 ump = VFSTOUFS(mp); 8892 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 8893 ("softdep_setup_directory_add called on non-softdep filesystem")); 8894 /* 8895 * Whiteouts have no dependencies. 8896 */ 8897 if (newinum == UFS_WINO) { 8898 if (newdirbp != NULL) 8899 bdwrite(newdirbp); 8900 return (0); 8901 } 8902 jaddref = NULL; 8903 mkdir1 = mkdir2 = NULL; 8904 fs = ump->um_fs; 8905 lbn = lblkno(fs, diroffset); 8906 offset = blkoff(fs, diroffset); 8907 dap = malloc(sizeof(struct diradd), M_DIRADD, 8908 M_SOFTDEP_FLAGS|M_ZERO); 8909 workitem_alloc(&dap->da_list, D_DIRADD, mp); 8910 dap->da_offset = offset; 8911 dap->da_newinum = newinum; 8912 dap->da_state = ATTACHED; 8913 LIST_INIT(&dap->da_jwork); 8914 isindir = bp->b_lblkno >= UFS_NDADDR; 8915 newdirblk = NULL; 8916 if (isnewblk && 8917 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) { 8918 newdirblk = malloc(sizeof(struct newdirblk), 8919 M_NEWDIRBLK, M_SOFTDEP_FLAGS); 8920 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8921 LIST_INIT(&newdirblk->db_mkdir); 8922 } 8923 /* 8924 * If we're creating a new directory setup the dependencies and set 8925 * the dap state to wait for them. Otherwise it's COMPLETE and 8926 * we can move on. 8927 */ 8928 if (newdirbp == NULL) { 8929 dap->da_state |= DEPCOMPLETE; 8930 ACQUIRE_LOCK(ump); 8931 } else { 8932 dap->da_state |= MKDIR_BODY | MKDIR_PARENT; 8933 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp, 8934 &mkdir2); 8935 } 8936 /* 8937 * Link into parent directory pagedep to await its being written. 8938 */ 8939 pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep); 8940 #ifdef INVARIANTS 8941 if (diradd_lookup(pagedep, offset) != NULL) 8942 panic("softdep_setup_directory_add: %p already at off %d\n", 8943 diradd_lookup(pagedep, offset), offset); 8944 #endif 8945 dap->da_pagedep = pagedep; 8946 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap, 8947 da_pdlist); 8948 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 8949 /* 8950 * If we're journaling, link the diradd into the jaddref so it 8951 * may be completed after the journal entry is written. Otherwise, 8952 * link the diradd into its inodedep. If the inode is not yet 8953 * written place it on the bufwait list, otherwise do the post-inode 8954 * write processing to put it on the id_pendinghd list. 8955 */ 8956 if (MOUNTEDSUJ(mp)) { 8957 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8958 inoreflst); 8959 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 8960 ("softdep_setup_directory_add: bad jaddref %p", jaddref)); 8961 jaddref->ja_diroff = diroffset; 8962 jaddref->ja_diradd = dap; 8963 add_to_journal(&jaddref->ja_list); 8964 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) 8965 diradd_inode_written(dap, inodedep); 8966 else 8967 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 8968 /* 8969 * Add the journal entries for . and .. links now that the primary 8970 * link is written. 8971 */ 8972 if (mkdir1 != NULL && MOUNTEDSUJ(mp)) { 8973 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 8974 inoreflst, if_deps); 8975 KASSERT(jaddref != NULL && 8976 jaddref->ja_ino == jaddref->ja_parent && 8977 (jaddref->ja_state & MKDIR_BODY), 8978 ("softdep_setup_directory_add: bad dot jaddref %p", 8979 jaddref)); 8980 mkdir1->md_jaddref = jaddref; 8981 jaddref->ja_mkdir = mkdir1; 8982 /* 8983 * It is important that the dotdot journal entry 8984 * is added prior to the dot entry since dot writes 8985 * both the dot and dotdot links. These both must 8986 * be added after the primary link for the journal 8987 * to remain consistent. 8988 */ 8989 add_to_journal(&mkdir2->md_jaddref->ja_list); 8990 add_to_journal(&jaddref->ja_list); 8991 } 8992 /* 8993 * If we are adding a new directory remember this diradd so that if 8994 * we rename it we can keep the dot and dotdot dependencies. If 8995 * we are adding a new name for an inode that has a mkdiradd we 8996 * must be in rename and we have to move the dot and dotdot 8997 * dependencies to this new name. The old name is being orphaned 8998 * soon. 8999 */ 9000 if (mkdir1 != NULL) { 9001 if (inodedep->id_mkdiradd != NULL) 9002 panic("softdep_setup_directory_add: Existing mkdir"); 9003 inodedep->id_mkdiradd = dap; 9004 } else if (inodedep->id_mkdiradd) 9005 merge_diradd(inodedep, dap); 9006 if (newdirblk != NULL) { 9007 /* 9008 * There is nothing to do if we are already tracking 9009 * this block. 9010 */ 9011 if ((pagedep->pd_state & NEWBLOCK) != 0) { 9012 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 9013 FREE_LOCK(ump); 9014 return (0); 9015 } 9016 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk) 9017 == 0) 9018 panic("softdep_setup_directory_add: lost entry"); 9019 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 9020 pagedep->pd_state |= NEWBLOCK; 9021 pagedep->pd_newdirblk = newdirblk; 9022 newdirblk->db_pagedep = pagedep; 9023 FREE_LOCK(ump); 9024 /* 9025 * If we extended into an indirect signal direnter to sync. 9026 */ 9027 if (isindir) 9028 return (1); 9029 return (0); 9030 } 9031 FREE_LOCK(ump); 9032 return (0); 9033 } 9034 9035 /* 9036 * This procedure is called to change the offset of a directory 9037 * entry when compacting a directory block which must be owned 9038 * exclusively by the caller. Note that the actual entry movement 9039 * must be done in this procedure to ensure that no I/O completions 9040 * occur while the move is in progress. 9041 */ 9042 void 9043 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 9044 struct buf *bp; /* Buffer holding directory block. */ 9045 struct inode *dp; /* inode for directory */ 9046 caddr_t base; /* address of dp->i_offset */ 9047 caddr_t oldloc; /* address of old directory location */ 9048 caddr_t newloc; /* address of new directory location */ 9049 int entrysize; /* size of directory entry */ 9050 { 9051 int offset, oldoffset, newoffset; 9052 struct pagedep *pagedep; 9053 struct jmvref *jmvref; 9054 struct diradd *dap; 9055 struct direct *de; 9056 struct mount *mp; 9057 struct ufsmount *ump; 9058 ufs_lbn_t lbn; 9059 int flags; 9060 9061 mp = ITOVFS(dp); 9062 ump = VFSTOUFS(mp); 9063 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 9064 ("softdep_change_directoryentry_offset called on " 9065 "non-softdep filesystem")); 9066 de = (struct direct *)oldloc; 9067 jmvref = NULL; 9068 flags = 0; 9069 /* 9070 * Moves are always journaled as it would be too complex to 9071 * determine if any affected adds or removes are present in the 9072 * journal. 9073 */ 9074 if (MOUNTEDSUJ(mp)) { 9075 flags = DEPALLOC; 9076 jmvref = newjmvref(dp, de->d_ino, 9077 I_OFFSET(dp) + (oldloc - base), 9078 I_OFFSET(dp) + (newloc - base)); 9079 } 9080 lbn = lblkno(ump->um_fs, I_OFFSET(dp)); 9081 offset = blkoff(ump->um_fs, I_OFFSET(dp)); 9082 oldoffset = offset + (oldloc - base); 9083 newoffset = offset + (newloc - base); 9084 ACQUIRE_LOCK(ump); 9085 if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0) 9086 goto done; 9087 dap = diradd_lookup(pagedep, oldoffset); 9088 if (dap) { 9089 dap->da_offset = newoffset; 9090 newoffset = DIRADDHASH(newoffset); 9091 oldoffset = DIRADDHASH(oldoffset); 9092 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE && 9093 newoffset != oldoffset) { 9094 LIST_REMOVE(dap, da_pdlist); 9095 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset], 9096 dap, da_pdlist); 9097 } 9098 } 9099 done: 9100 if (jmvref) { 9101 jmvref->jm_pagedep = pagedep; 9102 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps); 9103 add_to_journal(&jmvref->jm_list); 9104 } 9105 bcopy(oldloc, newloc, entrysize); 9106 FREE_LOCK(ump); 9107 } 9108 9109 /* 9110 * Move the mkdir dependencies and journal work from one diradd to another 9111 * when renaming a directory. The new name must depend on the mkdir deps 9112 * completing as the old name did. Directories can only have one valid link 9113 * at a time so one must be canonical. 9114 */ 9115 static void 9116 merge_diradd(inodedep, newdap) 9117 struct inodedep *inodedep; 9118 struct diradd *newdap; 9119 { 9120 struct diradd *olddap; 9121 struct mkdir *mkdir, *nextmd; 9122 struct ufsmount *ump; 9123 short state; 9124 9125 olddap = inodedep->id_mkdiradd; 9126 inodedep->id_mkdiradd = newdap; 9127 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 9128 newdap->da_state &= ~DEPCOMPLETE; 9129 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9130 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 9131 mkdir = nextmd) { 9132 nextmd = LIST_NEXT(mkdir, md_mkdirs); 9133 if (mkdir->md_diradd != olddap) 9134 continue; 9135 mkdir->md_diradd = newdap; 9136 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY); 9137 newdap->da_state |= state; 9138 olddap->da_state &= ~state; 9139 if ((olddap->da_state & 9140 (MKDIR_PARENT | MKDIR_BODY)) == 0) 9141 break; 9142 } 9143 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 9144 panic("merge_diradd: unfound ref"); 9145 } 9146 /* 9147 * Any mkdir related journal items are not safe to be freed until 9148 * the new name is stable. 9149 */ 9150 jwork_move(&newdap->da_jwork, &olddap->da_jwork); 9151 olddap->da_state |= DEPCOMPLETE; 9152 complete_diradd(olddap); 9153 } 9154 9155 /* 9156 * Move the diradd to the pending list when all diradd dependencies are 9157 * complete. 9158 */ 9159 static void 9160 complete_diradd(dap) 9161 struct diradd *dap; 9162 { 9163 struct pagedep *pagedep; 9164 9165 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 9166 if (dap->da_state & DIRCHG) 9167 pagedep = dap->da_previous->dm_pagedep; 9168 else 9169 pagedep = dap->da_pagedep; 9170 LIST_REMOVE(dap, da_pdlist); 9171 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 9172 } 9173 } 9174 9175 /* 9176 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal 9177 * add entries and conditonally journal the remove. 9178 */ 9179 static void 9180 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref) 9181 struct diradd *dap; 9182 struct dirrem *dirrem; 9183 struct jremref *jremref; 9184 struct jremref *dotremref; 9185 struct jremref *dotdotremref; 9186 { 9187 struct inodedep *inodedep; 9188 struct jaddref *jaddref; 9189 struct inoref *inoref; 9190 struct ufsmount *ump; 9191 struct mkdir *mkdir; 9192 9193 /* 9194 * If no remove references were allocated we're on a non-journaled 9195 * filesystem and can skip the cancel step. 9196 */ 9197 if (jremref == NULL) { 9198 free_diradd(dap, NULL); 9199 return; 9200 } 9201 /* 9202 * Cancel the primary name an free it if it does not require 9203 * journaling. 9204 */ 9205 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum, 9206 0, &inodedep) != 0) { 9207 /* Abort the addref that reference this diradd. */ 9208 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 9209 if (inoref->if_list.wk_type != D_JADDREF) 9210 continue; 9211 jaddref = (struct jaddref *)inoref; 9212 if (jaddref->ja_diradd != dap) 9213 continue; 9214 if (cancel_jaddref(jaddref, inodedep, 9215 &dirrem->dm_jwork) == 0) { 9216 free_jremref(jremref); 9217 jremref = NULL; 9218 } 9219 break; 9220 } 9221 } 9222 /* 9223 * Cancel subordinate names and free them if they do not require 9224 * journaling. 9225 */ 9226 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 9227 ump = VFSTOUFS(dap->da_list.wk_mp); 9228 LIST_FOREACH(mkdir, &ump->softdep_mkdirlisthd, md_mkdirs) { 9229 if (mkdir->md_diradd != dap) 9230 continue; 9231 if ((jaddref = mkdir->md_jaddref) == NULL) 9232 continue; 9233 mkdir->md_jaddref = NULL; 9234 if (mkdir->md_state & MKDIR_PARENT) { 9235 if (cancel_jaddref(jaddref, NULL, 9236 &dirrem->dm_jwork) == 0) { 9237 free_jremref(dotdotremref); 9238 dotdotremref = NULL; 9239 } 9240 } else { 9241 if (cancel_jaddref(jaddref, inodedep, 9242 &dirrem->dm_jwork) == 0) { 9243 free_jremref(dotremref); 9244 dotremref = NULL; 9245 } 9246 } 9247 } 9248 } 9249 9250 if (jremref) 9251 journal_jremref(dirrem, jremref, inodedep); 9252 if (dotremref) 9253 journal_jremref(dirrem, dotremref, inodedep); 9254 if (dotdotremref) 9255 journal_jremref(dirrem, dotdotremref, NULL); 9256 jwork_move(&dirrem->dm_jwork, &dap->da_jwork); 9257 free_diradd(dap, &dirrem->dm_jwork); 9258 } 9259 9260 /* 9261 * Free a diradd dependency structure. 9262 */ 9263 static void 9264 free_diradd(dap, wkhd) 9265 struct diradd *dap; 9266 struct workhead *wkhd; 9267 { 9268 struct dirrem *dirrem; 9269 struct pagedep *pagedep; 9270 struct inodedep *inodedep; 9271 struct mkdir *mkdir, *nextmd; 9272 struct ufsmount *ump; 9273 9274 ump = VFSTOUFS(dap->da_list.wk_mp); 9275 LOCK_OWNED(ump); 9276 LIST_REMOVE(dap, da_pdlist); 9277 if (dap->da_state & ONWORKLIST) 9278 WORKLIST_REMOVE(&dap->da_list); 9279 if ((dap->da_state & DIRCHG) == 0) { 9280 pagedep = dap->da_pagedep; 9281 } else { 9282 dirrem = dap->da_previous; 9283 pagedep = dirrem->dm_pagedep; 9284 dirrem->dm_dirinum = pagedep->pd_ino; 9285 dirrem->dm_state |= COMPLETE; 9286 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9287 add_to_worklist(&dirrem->dm_list, 0); 9288 } 9289 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum, 9290 0, &inodedep) != 0) 9291 if (inodedep->id_mkdiradd == dap) 9292 inodedep->id_mkdiradd = NULL; 9293 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 9294 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 9295 mkdir = nextmd) { 9296 nextmd = LIST_NEXT(mkdir, md_mkdirs); 9297 if (mkdir->md_diradd != dap) 9298 continue; 9299 dap->da_state &= 9300 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 9301 LIST_REMOVE(mkdir, md_mkdirs); 9302 if (mkdir->md_state & ONWORKLIST) 9303 WORKLIST_REMOVE(&mkdir->md_list); 9304 if (mkdir->md_jaddref != NULL) 9305 panic("free_diradd: Unexpected jaddref"); 9306 WORKITEM_FREE(mkdir, D_MKDIR); 9307 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) 9308 break; 9309 } 9310 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 9311 panic("free_diradd: unfound ref"); 9312 } 9313 if (inodedep) 9314 free_inodedep(inodedep); 9315 /* 9316 * Free any journal segments waiting for the directory write. 9317 */ 9318 handle_jwork(&dap->da_jwork); 9319 WORKITEM_FREE(dap, D_DIRADD); 9320 } 9321 9322 /* 9323 * Directory entry removal dependencies. 9324 * 9325 * When removing a directory entry, the entry's inode pointer must be 9326 * zero'ed on disk before the corresponding inode's link count is decremented 9327 * (possibly freeing the inode for re-use). This dependency is handled by 9328 * updating the directory entry but delaying the inode count reduction until 9329 * after the directory block has been written to disk. After this point, the 9330 * inode count can be decremented whenever it is convenient. 9331 */ 9332 9333 /* 9334 * This routine should be called immediately after removing 9335 * a directory entry. The inode's link count should not be 9336 * decremented by the calling procedure -- the soft updates 9337 * code will do this task when it is safe. 9338 */ 9339 void 9340 softdep_setup_remove(bp, dp, ip, isrmdir) 9341 struct buf *bp; /* buffer containing directory block */ 9342 struct inode *dp; /* inode for the directory being modified */ 9343 struct inode *ip; /* inode for directory entry being removed */ 9344 int isrmdir; /* indicates if doing RMDIR */ 9345 { 9346 struct dirrem *dirrem, *prevdirrem; 9347 struct inodedep *inodedep; 9348 struct ufsmount *ump; 9349 int direct; 9350 9351 ump = ITOUMP(ip); 9352 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9353 ("softdep_setup_remove called on non-softdep filesystem")); 9354 /* 9355 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want 9356 * newdirrem() to setup the full directory remove which requires 9357 * isrmdir > 1. 9358 */ 9359 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 9360 /* 9361 * Add the dirrem to the inodedep's pending remove list for quick 9362 * discovery later. 9363 */ 9364 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) 9365 panic("softdep_setup_remove: Lost inodedep."); 9366 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 9367 dirrem->dm_state |= ONDEPLIST; 9368 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9369 9370 /* 9371 * If the COMPLETE flag is clear, then there were no active 9372 * entries and we want to roll back to a zeroed entry until 9373 * the new inode is committed to disk. If the COMPLETE flag is 9374 * set then we have deleted an entry that never made it to 9375 * disk. If the entry we deleted resulted from a name change, 9376 * then the old name still resides on disk. We cannot delete 9377 * its inode (returned to us in prevdirrem) until the zeroed 9378 * directory entry gets to disk. The new inode has never been 9379 * referenced on the disk, so can be deleted immediately. 9380 */ 9381 if ((dirrem->dm_state & COMPLETE) == 0) { 9382 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem, 9383 dm_next); 9384 FREE_LOCK(ump); 9385 } else { 9386 if (prevdirrem != NULL) 9387 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, 9388 prevdirrem, dm_next); 9389 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino; 9390 direct = LIST_EMPTY(&dirrem->dm_jremrefhd); 9391 FREE_LOCK(ump); 9392 if (direct) 9393 handle_workitem_remove(dirrem, 0); 9394 } 9395 } 9396 9397 /* 9398 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the 9399 * pd_pendinghd list of a pagedep. 9400 */ 9401 static struct diradd * 9402 diradd_lookup(pagedep, offset) 9403 struct pagedep *pagedep; 9404 int offset; 9405 { 9406 struct diradd *dap; 9407 9408 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist) 9409 if (dap->da_offset == offset) 9410 return (dap); 9411 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 9412 if (dap->da_offset == offset) 9413 return (dap); 9414 return (NULL); 9415 } 9416 9417 /* 9418 * Search for a .. diradd dependency in a directory that is being removed. 9419 * If the directory was renamed to a new parent we have a diradd rather 9420 * than a mkdir for the .. entry. We need to cancel it now before 9421 * it is found in truncate(). 9422 */ 9423 static struct jremref * 9424 cancel_diradd_dotdot(ip, dirrem, jremref) 9425 struct inode *ip; 9426 struct dirrem *dirrem; 9427 struct jremref *jremref; 9428 { 9429 struct pagedep *pagedep; 9430 struct diradd *dap; 9431 struct worklist *wk; 9432 9433 if (pagedep_lookup(ITOVFS(ip), NULL, ip->i_number, 0, 0, &pagedep) == 0) 9434 return (jremref); 9435 dap = diradd_lookup(pagedep, DOTDOT_OFFSET); 9436 if (dap == NULL) 9437 return (jremref); 9438 cancel_diradd(dap, dirrem, jremref, NULL, NULL); 9439 /* 9440 * Mark any journal work as belonging to the parent so it is freed 9441 * with the .. reference. 9442 */ 9443 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9444 wk->wk_state |= MKDIR_PARENT; 9445 return (NULL); 9446 } 9447 9448 /* 9449 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to 9450 * replace it with a dirrem/diradd pair as a result of re-parenting a 9451 * directory. This ensures that we don't simultaneously have a mkdir and 9452 * a diradd for the same .. entry. 9453 */ 9454 static struct jremref * 9455 cancel_mkdir_dotdot(ip, dirrem, jremref) 9456 struct inode *ip; 9457 struct dirrem *dirrem; 9458 struct jremref *jremref; 9459 { 9460 struct inodedep *inodedep; 9461 struct jaddref *jaddref; 9462 struct ufsmount *ump; 9463 struct mkdir *mkdir; 9464 struct diradd *dap; 9465 struct mount *mp; 9466 9467 mp = ITOVFS(ip); 9468 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9469 return (jremref); 9470 dap = inodedep->id_mkdiradd; 9471 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0) 9472 return (jremref); 9473 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9474 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 9475 mkdir = LIST_NEXT(mkdir, md_mkdirs)) 9476 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT) 9477 break; 9478 if (mkdir == NULL) 9479 panic("cancel_mkdir_dotdot: Unable to find mkdir\n"); 9480 if ((jaddref = mkdir->md_jaddref) != NULL) { 9481 mkdir->md_jaddref = NULL; 9482 jaddref->ja_state &= ~MKDIR_PARENT; 9483 if (inodedep_lookup(mp, jaddref->ja_ino, 0, &inodedep) == 0) 9484 panic("cancel_mkdir_dotdot: Lost parent inodedep"); 9485 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) { 9486 journal_jremref(dirrem, jremref, inodedep); 9487 jremref = NULL; 9488 } 9489 } 9490 if (mkdir->md_state & ONWORKLIST) 9491 WORKLIST_REMOVE(&mkdir->md_list); 9492 mkdir->md_state |= ALLCOMPLETE; 9493 complete_mkdir(mkdir); 9494 return (jremref); 9495 } 9496 9497 static void 9498 journal_jremref(dirrem, jremref, inodedep) 9499 struct dirrem *dirrem; 9500 struct jremref *jremref; 9501 struct inodedep *inodedep; 9502 { 9503 9504 if (inodedep == NULL) 9505 if (inodedep_lookup(jremref->jr_list.wk_mp, 9506 jremref->jr_ref.if_ino, 0, &inodedep) == 0) 9507 panic("journal_jremref: Lost inodedep"); 9508 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps); 9509 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 9510 add_to_journal(&jremref->jr_list); 9511 } 9512 9513 static void 9514 dirrem_journal(dirrem, jremref, dotremref, dotdotremref) 9515 struct dirrem *dirrem; 9516 struct jremref *jremref; 9517 struct jremref *dotremref; 9518 struct jremref *dotdotremref; 9519 { 9520 struct inodedep *inodedep; 9521 9522 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0, 9523 &inodedep) == 0) 9524 panic("dirrem_journal: Lost inodedep"); 9525 journal_jremref(dirrem, jremref, inodedep); 9526 if (dotremref) 9527 journal_jremref(dirrem, dotremref, inodedep); 9528 if (dotdotremref) 9529 journal_jremref(dirrem, dotdotremref, NULL); 9530 } 9531 9532 /* 9533 * Allocate a new dirrem if appropriate and return it along with 9534 * its associated pagedep. Called without a lock, returns with lock. 9535 */ 9536 static struct dirrem * 9537 newdirrem(bp, dp, ip, isrmdir, prevdirremp) 9538 struct buf *bp; /* buffer containing directory block */ 9539 struct inode *dp; /* inode for the directory being modified */ 9540 struct inode *ip; /* inode for directory entry being removed */ 9541 int isrmdir; /* indicates if doing RMDIR */ 9542 struct dirrem **prevdirremp; /* previously referenced inode, if any */ 9543 { 9544 int offset; 9545 ufs_lbn_t lbn; 9546 struct diradd *dap; 9547 struct dirrem *dirrem; 9548 struct pagedep *pagedep; 9549 struct jremref *jremref; 9550 struct jremref *dotremref; 9551 struct jremref *dotdotremref; 9552 struct vnode *dvp; 9553 struct ufsmount *ump; 9554 9555 /* 9556 * Whiteouts have no deletion dependencies. 9557 */ 9558 if (ip == NULL) 9559 panic("newdirrem: whiteout"); 9560 dvp = ITOV(dp); 9561 ump = ITOUMP(dp); 9562 9563 /* 9564 * If the system is over its limit and our filesystem is 9565 * responsible for more than our share of that usage and 9566 * we are not a snapshot, request some inodedep cleanup. 9567 * Limiting the number of dirrem structures will also limit 9568 * the number of freefile and freeblks structures. 9569 */ 9570 ACQUIRE_LOCK(ump); 9571 if (!IS_SNAPSHOT(ip) && softdep_excess_items(ump, D_DIRREM)) 9572 schedule_cleanup(UFSTOVFS(ump)); 9573 else 9574 FREE_LOCK(ump); 9575 dirrem = malloc(sizeof(struct dirrem), M_DIRREM, M_SOFTDEP_FLAGS | 9576 M_ZERO); 9577 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount); 9578 LIST_INIT(&dirrem->dm_jremrefhd); 9579 LIST_INIT(&dirrem->dm_jwork); 9580 dirrem->dm_state = isrmdir ? RMDIR : 0; 9581 dirrem->dm_oldinum = ip->i_number; 9582 *prevdirremp = NULL; 9583 /* 9584 * Allocate remove reference structures to track journal write 9585 * dependencies. We will always have one for the link and 9586 * when doing directories we will always have one more for dot. 9587 * When renaming a directory we skip the dotdot link change so 9588 * this is not needed. 9589 */ 9590 jremref = dotremref = dotdotremref = NULL; 9591 if (DOINGSUJ(dvp)) { 9592 if (isrmdir) { 9593 jremref = newjremref(dirrem, dp, ip, I_OFFSET(dp), 9594 ip->i_effnlink + 2); 9595 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET, 9596 ip->i_effnlink + 1); 9597 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET, 9598 dp->i_effnlink + 1); 9599 dotdotremref->jr_state |= MKDIR_PARENT; 9600 } else 9601 jremref = newjremref(dirrem, dp, ip, I_OFFSET(dp), 9602 ip->i_effnlink + 1); 9603 } 9604 ACQUIRE_LOCK(ump); 9605 lbn = lblkno(ump->um_fs, I_OFFSET(dp)); 9606 offset = blkoff(ump->um_fs, I_OFFSET(dp)); 9607 pagedep_lookup(UFSTOVFS(ump), bp, dp->i_number, lbn, DEPALLOC, 9608 &pagedep); 9609 dirrem->dm_pagedep = pagedep; 9610 dirrem->dm_offset = offset; 9611 /* 9612 * If we're renaming a .. link to a new directory, cancel any 9613 * existing MKDIR_PARENT mkdir. If it has already been canceled 9614 * the jremref is preserved for any potential diradd in this 9615 * location. This can not coincide with a rmdir. 9616 */ 9617 if (I_OFFSET(dp) == DOTDOT_OFFSET) { 9618 if (isrmdir) 9619 panic("newdirrem: .. directory change during remove?"); 9620 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref); 9621 } 9622 /* 9623 * If we're removing a directory search for the .. dependency now and 9624 * cancel it. Any pending journal work will be added to the dirrem 9625 * to be completed when the workitem remove completes. 9626 */ 9627 if (isrmdir) 9628 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref); 9629 /* 9630 * Check for a diradd dependency for the same directory entry. 9631 * If present, then both dependencies become obsolete and can 9632 * be de-allocated. 9633 */ 9634 dap = diradd_lookup(pagedep, offset); 9635 if (dap == NULL) { 9636 /* 9637 * Link the jremref structures into the dirrem so they are 9638 * written prior to the pagedep. 9639 */ 9640 if (jremref) 9641 dirrem_journal(dirrem, jremref, dotremref, 9642 dotdotremref); 9643 return (dirrem); 9644 } 9645 /* 9646 * Must be ATTACHED at this point. 9647 */ 9648 if ((dap->da_state & ATTACHED) == 0) 9649 panic("newdirrem: not ATTACHED"); 9650 if (dap->da_newinum != ip->i_number) 9651 panic("newdirrem: inum %ju should be %ju", 9652 (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum); 9653 /* 9654 * If we are deleting a changed name that never made it to disk, 9655 * then return the dirrem describing the previous inode (which 9656 * represents the inode currently referenced from this entry on disk). 9657 */ 9658 if ((dap->da_state & DIRCHG) != 0) { 9659 *prevdirremp = dap->da_previous; 9660 dap->da_state &= ~DIRCHG; 9661 dap->da_pagedep = pagedep; 9662 } 9663 /* 9664 * We are deleting an entry that never made it to disk. 9665 * Mark it COMPLETE so we can delete its inode immediately. 9666 */ 9667 dirrem->dm_state |= COMPLETE; 9668 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref); 9669 #ifdef INVARIANTS 9670 if (isrmdir == 0) { 9671 struct worklist *wk; 9672 9673 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9674 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT)) 9675 panic("bad wk %p (0x%X)\n", wk, wk->wk_state); 9676 } 9677 #endif 9678 9679 return (dirrem); 9680 } 9681 9682 /* 9683 * Directory entry change dependencies. 9684 * 9685 * Changing an existing directory entry requires that an add operation 9686 * be completed first followed by a deletion. The semantics for the addition 9687 * are identical to the description of adding a new entry above except 9688 * that the rollback is to the old inode number rather than zero. Once 9689 * the addition dependency is completed, the removal is done as described 9690 * in the removal routine above. 9691 */ 9692 9693 /* 9694 * This routine should be called immediately after changing 9695 * a directory entry. The inode's link count should not be 9696 * decremented by the calling procedure -- the soft updates 9697 * code will perform this task when it is safe. 9698 */ 9699 void 9700 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 9701 struct buf *bp; /* buffer containing directory block */ 9702 struct inode *dp; /* inode for the directory being modified */ 9703 struct inode *ip; /* inode for directory entry being removed */ 9704 ino_t newinum; /* new inode number for changed entry */ 9705 int isrmdir; /* indicates if doing RMDIR */ 9706 { 9707 int offset; 9708 struct diradd *dap = NULL; 9709 struct dirrem *dirrem, *prevdirrem; 9710 struct pagedep *pagedep; 9711 struct inodedep *inodedep; 9712 struct jaddref *jaddref; 9713 struct mount *mp; 9714 struct ufsmount *ump; 9715 9716 mp = ITOVFS(dp); 9717 ump = VFSTOUFS(mp); 9718 offset = blkoff(ump->um_fs, I_OFFSET(dp)); 9719 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 9720 ("softdep_setup_directory_change called on non-softdep filesystem")); 9721 9722 /* 9723 * Whiteouts do not need diradd dependencies. 9724 */ 9725 if (newinum != UFS_WINO) { 9726 dap = malloc(sizeof(struct diradd), 9727 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO); 9728 workitem_alloc(&dap->da_list, D_DIRADD, mp); 9729 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE; 9730 dap->da_offset = offset; 9731 dap->da_newinum = newinum; 9732 LIST_INIT(&dap->da_jwork); 9733 } 9734 9735 /* 9736 * Allocate a new dirrem and ACQUIRE_LOCK. 9737 */ 9738 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 9739 pagedep = dirrem->dm_pagedep; 9740 /* 9741 * The possible values for isrmdir: 9742 * 0 - non-directory file rename 9743 * 1 - directory rename within same directory 9744 * inum - directory rename to new directory of given inode number 9745 * When renaming to a new directory, we are both deleting and 9746 * creating a new directory entry, so the link count on the new 9747 * directory should not change. Thus we do not need the followup 9748 * dirrem which is usually done in handle_workitem_remove. We set 9749 * the DIRCHG flag to tell handle_workitem_remove to skip the 9750 * followup dirrem. 9751 */ 9752 if (isrmdir > 1) 9753 dirrem->dm_state |= DIRCHG; 9754 9755 /* 9756 * Whiteouts have no additional dependencies, 9757 * so just put the dirrem on the correct list. 9758 */ 9759 if (newinum == UFS_WINO) { 9760 if ((dirrem->dm_state & COMPLETE) == 0) { 9761 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem, 9762 dm_next); 9763 } else { 9764 dirrem->dm_dirinum = pagedep->pd_ino; 9765 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9766 add_to_worklist(&dirrem->dm_list, 0); 9767 } 9768 FREE_LOCK(ump); 9769 return; 9770 } 9771 /* 9772 * Add the dirrem to the inodedep's pending remove list for quick 9773 * discovery later. A valid nlinkdelta ensures that this lookup 9774 * will not fail. 9775 */ 9776 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9777 panic("softdep_setup_directory_change: Lost inodedep."); 9778 dirrem->dm_state |= ONDEPLIST; 9779 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9780 9781 /* 9782 * If the COMPLETE flag is clear, then there were no active 9783 * entries and we want to roll back to the previous inode until 9784 * the new inode is committed to disk. If the COMPLETE flag is 9785 * set, then we have deleted an entry that never made it to disk. 9786 * If the entry we deleted resulted from a name change, then the old 9787 * inode reference still resides on disk. Any rollback that we do 9788 * needs to be to that old inode (returned to us in prevdirrem). If 9789 * the entry we deleted resulted from a create, then there is 9790 * no entry on the disk, so we want to roll back to zero rather 9791 * than the uncommitted inode. In either of the COMPLETE cases we 9792 * want to immediately free the unwritten and unreferenced inode. 9793 */ 9794 if ((dirrem->dm_state & COMPLETE) == 0) { 9795 dap->da_previous = dirrem; 9796 } else { 9797 if (prevdirrem != NULL) { 9798 dap->da_previous = prevdirrem; 9799 } else { 9800 dap->da_state &= ~DIRCHG; 9801 dap->da_pagedep = pagedep; 9802 } 9803 dirrem->dm_dirinum = pagedep->pd_ino; 9804 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9805 add_to_worklist(&dirrem->dm_list, 0); 9806 } 9807 /* 9808 * Lookup the jaddref for this journal entry. We must finish 9809 * initializing it and make the diradd write dependent on it. 9810 * If we're not journaling, put it on the id_bufwait list if the 9811 * inode is not yet written. If it is written, do the post-inode 9812 * write processing to put it on the id_pendinghd list. 9813 */ 9814 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 9815 if (MOUNTEDSUJ(mp)) { 9816 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 9817 inoreflst); 9818 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 9819 ("softdep_setup_directory_change: bad jaddref %p", 9820 jaddref)); 9821 jaddref->ja_diroff = I_OFFSET(dp); 9822 jaddref->ja_diradd = dap; 9823 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9824 dap, da_pdlist); 9825 add_to_journal(&jaddref->ja_list); 9826 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 9827 dap->da_state |= COMPLETE; 9828 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 9829 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 9830 } else { 9831 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9832 dap, da_pdlist); 9833 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 9834 } 9835 /* 9836 * If we're making a new name for a directory that has not been 9837 * committed when need to move the dot and dotdot references to 9838 * this new name. 9839 */ 9840 if (inodedep->id_mkdiradd && I_OFFSET(dp) != DOTDOT_OFFSET) 9841 merge_diradd(inodedep, dap); 9842 FREE_LOCK(ump); 9843 } 9844 9845 /* 9846 * Called whenever the link count on an inode is changed. 9847 * It creates an inode dependency so that the new reference(s) 9848 * to the inode cannot be committed to disk until the updated 9849 * inode has been written. 9850 */ 9851 void 9852 softdep_change_linkcnt(ip) 9853 struct inode *ip; /* the inode with the increased link count */ 9854 { 9855 struct inodedep *inodedep; 9856 struct ufsmount *ump; 9857 9858 ump = ITOUMP(ip); 9859 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9860 ("softdep_change_linkcnt called on non-softdep filesystem")); 9861 ACQUIRE_LOCK(ump); 9862 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep); 9863 if (ip->i_nlink < ip->i_effnlink) 9864 panic("softdep_change_linkcnt: bad delta"); 9865 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9866 FREE_LOCK(ump); 9867 } 9868 9869 /* 9870 * Attach a sbdep dependency to the superblock buf so that we can keep 9871 * track of the head of the linked list of referenced but unlinked inodes. 9872 */ 9873 void 9874 softdep_setup_sbupdate(ump, fs, bp) 9875 struct ufsmount *ump; 9876 struct fs *fs; 9877 struct buf *bp; 9878 { 9879 struct sbdep *sbdep; 9880 struct worklist *wk; 9881 9882 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9883 ("softdep_setup_sbupdate called on non-softdep filesystem")); 9884 LIST_FOREACH(wk, &bp->b_dep, wk_list) 9885 if (wk->wk_type == D_SBDEP) 9886 break; 9887 if (wk != NULL) 9888 return; 9889 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS); 9890 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump)); 9891 sbdep->sb_fs = fs; 9892 sbdep->sb_ump = ump; 9893 ACQUIRE_LOCK(ump); 9894 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list); 9895 FREE_LOCK(ump); 9896 } 9897 9898 /* 9899 * Return the first unlinked inodedep which is ready to be the head of the 9900 * list. The inodedep and all those after it must have valid next pointers. 9901 */ 9902 static struct inodedep * 9903 first_unlinked_inodedep(ump) 9904 struct ufsmount *ump; 9905 { 9906 struct inodedep *inodedep; 9907 struct inodedep *idp; 9908 9909 LOCK_OWNED(ump); 9910 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst); 9911 inodedep; inodedep = idp) { 9912 if ((inodedep->id_state & UNLINKNEXT) == 0) 9913 return (NULL); 9914 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9915 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0) 9916 break; 9917 if ((inodedep->id_state & UNLINKPREV) == 0) 9918 break; 9919 } 9920 return (inodedep); 9921 } 9922 9923 /* 9924 * Set the sujfree unlinked head pointer prior to writing a superblock. 9925 */ 9926 static void 9927 initiate_write_sbdep(sbdep) 9928 struct sbdep *sbdep; 9929 { 9930 struct inodedep *inodedep; 9931 struct fs *bpfs; 9932 struct fs *fs; 9933 9934 bpfs = sbdep->sb_fs; 9935 fs = sbdep->sb_ump->um_fs; 9936 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9937 if (inodedep) { 9938 fs->fs_sujfree = inodedep->id_ino; 9939 inodedep->id_state |= UNLINKPREV; 9940 } else 9941 fs->fs_sujfree = 0; 9942 bpfs->fs_sujfree = fs->fs_sujfree; 9943 /* 9944 * Because we have made changes to the superblock, we need to 9945 * recompute its check-hash. 9946 */ 9947 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs); 9948 } 9949 9950 /* 9951 * After a superblock is written determine whether it must be written again 9952 * due to a changing unlinked list head. 9953 */ 9954 static int 9955 handle_written_sbdep(sbdep, bp) 9956 struct sbdep *sbdep; 9957 struct buf *bp; 9958 { 9959 struct inodedep *inodedep; 9960 struct fs *fs; 9961 9962 LOCK_OWNED(sbdep->sb_ump); 9963 fs = sbdep->sb_fs; 9964 /* 9965 * If the superblock doesn't match the in-memory list start over. 9966 */ 9967 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9968 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) || 9969 (inodedep == NULL && fs->fs_sujfree != 0)) { 9970 bdirty(bp); 9971 return (1); 9972 } 9973 WORKITEM_FREE(sbdep, D_SBDEP); 9974 if (fs->fs_sujfree == 0) 9975 return (0); 9976 /* 9977 * Now that we have a record of this inode in stable store allow it 9978 * to be written to free up pending work. Inodes may see a lot of 9979 * write activity after they are unlinked which we must not hold up. 9980 */ 9981 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 9982 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS) 9983 panic("handle_written_sbdep: Bad inodedep %p (0x%X)", 9984 inodedep, inodedep->id_state); 9985 if (inodedep->id_state & UNLINKONLIST) 9986 break; 9987 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST; 9988 } 9989 9990 return (0); 9991 } 9992 9993 /* 9994 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list. 9995 */ 9996 static void 9997 unlinked_inodedep(mp, inodedep) 9998 struct mount *mp; 9999 struct inodedep *inodedep; 10000 { 10001 struct ufsmount *ump; 10002 10003 ump = VFSTOUFS(mp); 10004 LOCK_OWNED(ump); 10005 if (MOUNTEDSUJ(mp) == 0) 10006 return; 10007 ump->um_fs->fs_fmod = 1; 10008 if (inodedep->id_state & UNLINKED) 10009 panic("unlinked_inodedep: %p already unlinked\n", inodedep); 10010 inodedep->id_state |= UNLINKED; 10011 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked); 10012 } 10013 10014 /* 10015 * Remove an inodedep from the unlinked inodedep list. This may require 10016 * disk writes if the inode has made it that far. 10017 */ 10018 static void 10019 clear_unlinked_inodedep(inodedep) 10020 struct inodedep *inodedep; 10021 { 10022 struct ufs2_dinode *dip; 10023 struct ufsmount *ump; 10024 struct inodedep *idp; 10025 struct inodedep *idn; 10026 struct fs *fs, *bpfs; 10027 struct buf *bp; 10028 daddr_t dbn; 10029 ino_t ino; 10030 ino_t nino; 10031 ino_t pino; 10032 int error; 10033 10034 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10035 fs = ump->um_fs; 10036 ino = inodedep->id_ino; 10037 error = 0; 10038 for (;;) { 10039 LOCK_OWNED(ump); 10040 KASSERT((inodedep->id_state & UNLINKED) != 0, 10041 ("clear_unlinked_inodedep: inodedep %p not unlinked", 10042 inodedep)); 10043 /* 10044 * If nothing has yet been written simply remove us from 10045 * the in memory list and return. This is the most common 10046 * case where handle_workitem_remove() loses the final 10047 * reference. 10048 */ 10049 if ((inodedep->id_state & UNLINKLINKS) == 0) 10050 break; 10051 /* 10052 * If we have a NEXT pointer and no PREV pointer we can simply 10053 * clear NEXT's PREV and remove ourselves from the list. Be 10054 * careful not to clear PREV if the superblock points at 10055 * next as well. 10056 */ 10057 idn = TAILQ_NEXT(inodedep, id_unlinked); 10058 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) { 10059 if (idn && fs->fs_sujfree != idn->id_ino) 10060 idn->id_state &= ~UNLINKPREV; 10061 break; 10062 } 10063 /* 10064 * Here we have an inodedep which is actually linked into 10065 * the list. We must remove it by forcing a write to the 10066 * link before us, whether it be the superblock or an inode. 10067 * Unfortunately the list may change while we're waiting 10068 * on the buf lock for either resource so we must loop until 10069 * we lock the right one. If both the superblock and an 10070 * inode point to this inode we must clear the inode first 10071 * followed by the superblock. 10072 */ 10073 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 10074 pino = 0; 10075 if (idp && (idp->id_state & UNLINKNEXT)) 10076 pino = idp->id_ino; 10077 FREE_LOCK(ump); 10078 if (pino == 0) { 10079 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 10080 (int)fs->fs_sbsize, 0, 0, 0); 10081 } else { 10082 dbn = fsbtodb(fs, ino_to_fsba(fs, pino)); 10083 error = ffs_breadz(ump, ump->um_devvp, dbn, dbn, 10084 (int)fs->fs_bsize, NULL, NULL, 0, NOCRED, 0, NULL, 10085 &bp); 10086 } 10087 ACQUIRE_LOCK(ump); 10088 if (error) 10089 break; 10090 /* If the list has changed restart the loop. */ 10091 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 10092 nino = 0; 10093 if (idp && (idp->id_state & UNLINKNEXT)) 10094 nino = idp->id_ino; 10095 if (nino != pino || 10096 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) { 10097 FREE_LOCK(ump); 10098 brelse(bp); 10099 ACQUIRE_LOCK(ump); 10100 continue; 10101 } 10102 nino = 0; 10103 idn = TAILQ_NEXT(inodedep, id_unlinked); 10104 if (idn) 10105 nino = idn->id_ino; 10106 /* 10107 * Remove us from the in memory list. After this we cannot 10108 * access the inodedep. 10109 */ 10110 KASSERT((inodedep->id_state & UNLINKED) != 0, 10111 ("clear_unlinked_inodedep: inodedep %p not unlinked", 10112 inodedep)); 10113 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 10114 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 10115 FREE_LOCK(ump); 10116 /* 10117 * The predecessor's next pointer is manually updated here 10118 * so that the NEXT flag is never cleared for an element 10119 * that is in the list. 10120 */ 10121 if (pino == 0) { 10122 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 10123 bpfs = (struct fs *)bp->b_data; 10124 ffs_oldfscompat_write(bpfs, ump); 10125 softdep_setup_sbupdate(ump, bpfs, bp); 10126 /* 10127 * Because we may have made changes to the superblock, 10128 * we need to recompute its check-hash. 10129 */ 10130 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs); 10131 } else if (fs->fs_magic == FS_UFS1_MAGIC) { 10132 ((struct ufs1_dinode *)bp->b_data + 10133 ino_to_fsbo(fs, pino))->di_freelink = nino; 10134 } else { 10135 dip = (struct ufs2_dinode *)bp->b_data + 10136 ino_to_fsbo(fs, pino); 10137 dip->di_freelink = nino; 10138 ffs_update_dinode_ckhash(fs, dip); 10139 } 10140 /* 10141 * If the bwrite fails we have no recourse to recover. The 10142 * filesystem is corrupted already. 10143 */ 10144 bwrite(bp); 10145 ACQUIRE_LOCK(ump); 10146 /* 10147 * If the superblock pointer still needs to be cleared force 10148 * a write here. 10149 */ 10150 if (fs->fs_sujfree == ino) { 10151 FREE_LOCK(ump); 10152 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 10153 (int)fs->fs_sbsize, 0, 0, 0); 10154 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 10155 bpfs = (struct fs *)bp->b_data; 10156 ffs_oldfscompat_write(bpfs, ump); 10157 softdep_setup_sbupdate(ump, bpfs, bp); 10158 /* 10159 * Because we may have made changes to the superblock, 10160 * we need to recompute its check-hash. 10161 */ 10162 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs); 10163 bwrite(bp); 10164 ACQUIRE_LOCK(ump); 10165 } 10166 10167 if (fs->fs_sujfree != ino) 10168 return; 10169 panic("clear_unlinked_inodedep: Failed to clear free head"); 10170 } 10171 if (inodedep->id_ino == fs->fs_sujfree) 10172 panic("clear_unlinked_inodedep: Freeing head of free list"); 10173 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 10174 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 10175 return; 10176 } 10177 10178 /* 10179 * This workitem decrements the inode's link count. 10180 * If the link count reaches zero, the file is removed. 10181 */ 10182 static int 10183 handle_workitem_remove(dirrem, flags) 10184 struct dirrem *dirrem; 10185 int flags; 10186 { 10187 struct inodedep *inodedep; 10188 struct workhead dotdotwk; 10189 struct worklist *wk; 10190 struct ufsmount *ump; 10191 struct mount *mp; 10192 struct vnode *vp; 10193 struct inode *ip; 10194 ino_t oldinum; 10195 10196 if (dirrem->dm_state & ONWORKLIST) 10197 panic("handle_workitem_remove: dirrem %p still on worklist", 10198 dirrem); 10199 oldinum = dirrem->dm_oldinum; 10200 mp = dirrem->dm_list.wk_mp; 10201 ump = VFSTOUFS(mp); 10202 flags |= LK_EXCLUSIVE; 10203 if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ | 10204 FFSV_FORCEINODEDEP) != 0) 10205 return (EBUSY); 10206 ip = VTOI(vp); 10207 MPASS(ip->i_mode != 0); 10208 ACQUIRE_LOCK(ump); 10209 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0) 10210 panic("handle_workitem_remove: lost inodedep"); 10211 if (dirrem->dm_state & ONDEPLIST) 10212 LIST_REMOVE(dirrem, dm_inonext); 10213 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 10214 ("handle_workitem_remove: Journal entries not written.")); 10215 10216 /* 10217 * Move all dependencies waiting on the remove to complete 10218 * from the dirrem to the inode inowait list to be completed 10219 * after the inode has been updated and written to disk. 10220 * 10221 * Any marked MKDIR_PARENT are saved to be completed when the 10222 * dotdot ref is removed unless DIRCHG is specified. For 10223 * directory change operations there will be no further 10224 * directory writes and the jsegdeps need to be moved along 10225 * with the rest to be completed when the inode is free or 10226 * stable in the inode free list. 10227 */ 10228 LIST_INIT(&dotdotwk); 10229 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) { 10230 WORKLIST_REMOVE(wk); 10231 if ((dirrem->dm_state & DIRCHG) == 0 && 10232 wk->wk_state & MKDIR_PARENT) { 10233 wk->wk_state &= ~MKDIR_PARENT; 10234 WORKLIST_INSERT(&dotdotwk, wk); 10235 continue; 10236 } 10237 WORKLIST_INSERT(&inodedep->id_inowait, wk); 10238 } 10239 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list); 10240 /* 10241 * Normal file deletion. 10242 */ 10243 if ((dirrem->dm_state & RMDIR) == 0) { 10244 ip->i_nlink--; 10245 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: file ino " 10246 "%ju negative i_nlink %d", (intmax_t)ip->i_number, 10247 ip->i_nlink)); 10248 DIP_SET(ip, i_nlink, ip->i_nlink); 10249 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 10250 if (ip->i_nlink < ip->i_effnlink) 10251 panic("handle_workitem_remove: bad file delta"); 10252 if (ip->i_nlink == 0) 10253 unlinked_inodedep(mp, inodedep); 10254 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 10255 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 10256 ("handle_workitem_remove: worklist not empty. %s", 10257 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type))); 10258 WORKITEM_FREE(dirrem, D_DIRREM); 10259 FREE_LOCK(ump); 10260 goto out; 10261 } 10262 /* 10263 * Directory deletion. Decrement reference count for both the 10264 * just deleted parent directory entry and the reference for ".". 10265 * Arrange to have the reference count on the parent decremented 10266 * to account for the loss of "..". 10267 */ 10268 ip->i_nlink -= 2; 10269 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: directory ino " 10270 "%ju negative i_nlink %d", (intmax_t)ip->i_number, ip->i_nlink)); 10271 DIP_SET(ip, i_nlink, ip->i_nlink); 10272 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 10273 if (ip->i_nlink < ip->i_effnlink) 10274 panic("handle_workitem_remove: bad dir delta"); 10275 if (ip->i_nlink == 0) 10276 unlinked_inodedep(mp, inodedep); 10277 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 10278 /* 10279 * Rename a directory to a new parent. Since, we are both deleting 10280 * and creating a new directory entry, the link count on the new 10281 * directory should not change. Thus we skip the followup dirrem. 10282 */ 10283 if (dirrem->dm_state & DIRCHG) { 10284 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 10285 ("handle_workitem_remove: DIRCHG and worklist not empty.")); 10286 WORKITEM_FREE(dirrem, D_DIRREM); 10287 FREE_LOCK(ump); 10288 goto out; 10289 } 10290 dirrem->dm_state = ONDEPLIST; 10291 dirrem->dm_oldinum = dirrem->dm_dirinum; 10292 /* 10293 * Place the dirrem on the parent's diremhd list. 10294 */ 10295 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0) 10296 panic("handle_workitem_remove: lost dir inodedep"); 10297 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 10298 /* 10299 * If the allocated inode has never been written to disk, then 10300 * the on-disk inode is zero'ed and we can remove the file 10301 * immediately. When journaling if the inode has been marked 10302 * unlinked and not DEPCOMPLETE we know it can never be written. 10303 */ 10304 inodedep_lookup(mp, oldinum, 0, &inodedep); 10305 if (inodedep == NULL || 10306 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED || 10307 check_inode_unwritten(inodedep)) { 10308 FREE_LOCK(ump); 10309 vput(vp); 10310 return handle_workitem_remove(dirrem, flags); 10311 } 10312 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list); 10313 FREE_LOCK(ump); 10314 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 10315 out: 10316 ffs_update(vp, 0); 10317 vput(vp); 10318 return (0); 10319 } 10320 10321 /* 10322 * Inode de-allocation dependencies. 10323 * 10324 * When an inode's link count is reduced to zero, it can be de-allocated. We 10325 * found it convenient to postpone de-allocation until after the inode is 10326 * written to disk with its new link count (zero). At this point, all of the 10327 * on-disk inode's block pointers are nullified and, with careful dependency 10328 * list ordering, all dependencies related to the inode will be satisfied and 10329 * the corresponding dependency structures de-allocated. So, if/when the 10330 * inode is reused, there will be no mixing of old dependencies with new 10331 * ones. This artificial dependency is set up by the block de-allocation 10332 * procedure above (softdep_setup_freeblocks) and completed by the 10333 * following procedure. 10334 */ 10335 static void 10336 handle_workitem_freefile(freefile) 10337 struct freefile *freefile; 10338 { 10339 struct workhead wkhd; 10340 struct fs *fs; 10341 struct ufsmount *ump; 10342 int error; 10343 #ifdef INVARIANTS 10344 struct inodedep *idp; 10345 #endif 10346 10347 ump = VFSTOUFS(freefile->fx_list.wk_mp); 10348 fs = ump->um_fs; 10349 #ifdef INVARIANTS 10350 ACQUIRE_LOCK(ump); 10351 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp); 10352 FREE_LOCK(ump); 10353 if (error) 10354 panic("handle_workitem_freefile: inodedep %p survived", idp); 10355 #endif 10356 UFS_LOCK(ump); 10357 fs->fs_pendinginodes -= 1; 10358 UFS_UNLOCK(ump); 10359 LIST_INIT(&wkhd); 10360 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list); 10361 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp, 10362 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0) 10363 softdep_error("handle_workitem_freefile", error); 10364 ACQUIRE_LOCK(ump); 10365 WORKITEM_FREE(freefile, D_FREEFILE); 10366 FREE_LOCK(ump); 10367 } 10368 10369 /* 10370 * Helper function which unlinks marker element from work list and returns 10371 * the next element on the list. 10372 */ 10373 static __inline struct worklist * 10374 markernext(struct worklist *marker) 10375 { 10376 struct worklist *next; 10377 10378 next = LIST_NEXT(marker, wk_list); 10379 LIST_REMOVE(marker, wk_list); 10380 return next; 10381 } 10382 10383 /* 10384 * Disk writes. 10385 * 10386 * The dependency structures constructed above are most actively used when file 10387 * system blocks are written to disk. No constraints are placed on when a 10388 * block can be written, but unsatisfied update dependencies are made safe by 10389 * modifying (or replacing) the source memory for the duration of the disk 10390 * write. When the disk write completes, the memory block is again brought 10391 * up-to-date. 10392 * 10393 * In-core inode structure reclamation. 10394 * 10395 * Because there are a finite number of "in-core" inode structures, they are 10396 * reused regularly. By transferring all inode-related dependencies to the 10397 * in-memory inode block and indexing them separately (via "inodedep"s), we 10398 * can allow "in-core" inode structures to be reused at any time and avoid 10399 * any increase in contention. 10400 * 10401 * Called just before entering the device driver to initiate a new disk I/O. 10402 * The buffer must be locked, thus, no I/O completion operations can occur 10403 * while we are manipulating its associated dependencies. 10404 */ 10405 static void 10406 softdep_disk_io_initiation(bp) 10407 struct buf *bp; /* structure describing disk write to occur */ 10408 { 10409 struct worklist *wk; 10410 struct worklist marker; 10411 struct inodedep *inodedep; 10412 struct freeblks *freeblks; 10413 struct jblkdep *jblkdep; 10414 struct newblk *newblk; 10415 struct ufsmount *ump; 10416 10417 /* 10418 * We only care about write operations. There should never 10419 * be dependencies for reads. 10420 */ 10421 if (bp->b_iocmd != BIO_WRITE) 10422 panic("softdep_disk_io_initiation: not write"); 10423 10424 if (bp->b_vflags & BV_BKGRDINPROG) 10425 panic("softdep_disk_io_initiation: Writing buffer with " 10426 "background write in progress: %p", bp); 10427 10428 ump = softdep_bp_to_mp(bp); 10429 if (ump == NULL) 10430 return; 10431 10432 marker.wk_type = D_LAST + 1; /* Not a normal workitem */ 10433 PHOLD(curproc); /* Don't swap out kernel stack */ 10434 ACQUIRE_LOCK(ump); 10435 /* 10436 * Do any necessary pre-I/O processing. 10437 */ 10438 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL; 10439 wk = markernext(&marker)) { 10440 LIST_INSERT_AFTER(wk, &marker, wk_list); 10441 switch (wk->wk_type) { 10442 case D_PAGEDEP: 10443 initiate_write_filepage(WK_PAGEDEP(wk), bp); 10444 continue; 10445 10446 case D_INODEDEP: 10447 inodedep = WK_INODEDEP(wk); 10448 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) 10449 initiate_write_inodeblock_ufs1(inodedep, bp); 10450 else 10451 initiate_write_inodeblock_ufs2(inodedep, bp); 10452 continue; 10453 10454 case D_INDIRDEP: 10455 initiate_write_indirdep(WK_INDIRDEP(wk), bp); 10456 continue; 10457 10458 case D_BMSAFEMAP: 10459 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp); 10460 continue; 10461 10462 case D_JSEG: 10463 WK_JSEG(wk)->js_buf = NULL; 10464 continue; 10465 10466 case D_FREEBLKS: 10467 freeblks = WK_FREEBLKS(wk); 10468 jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd); 10469 /* 10470 * We have to wait for the freeblks to be journaled 10471 * before we can write an inodeblock with updated 10472 * pointers. Be careful to arrange the marker so 10473 * we revisit the freeblks if it's not removed by 10474 * the first jwait(). 10475 */ 10476 if (jblkdep != NULL) { 10477 LIST_REMOVE(&marker, wk_list); 10478 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10479 jwait(&jblkdep->jb_list, MNT_WAIT); 10480 } 10481 continue; 10482 case D_ALLOCDIRECT: 10483 case D_ALLOCINDIR: 10484 /* 10485 * We have to wait for the jnewblk to be journaled 10486 * before we can write to a block if the contents 10487 * may be confused with an earlier file's indirect 10488 * at recovery time. Handle the marker as described 10489 * above. 10490 */ 10491 newblk = WK_NEWBLK(wk); 10492 if (newblk->nb_jnewblk != NULL && 10493 indirblk_lookup(newblk->nb_list.wk_mp, 10494 newblk->nb_newblkno)) { 10495 LIST_REMOVE(&marker, wk_list); 10496 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10497 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 10498 } 10499 continue; 10500 10501 case D_SBDEP: 10502 initiate_write_sbdep(WK_SBDEP(wk)); 10503 continue; 10504 10505 case D_MKDIR: 10506 case D_FREEWORK: 10507 case D_FREEDEP: 10508 case D_JSEGDEP: 10509 continue; 10510 10511 default: 10512 panic("handle_disk_io_initiation: Unexpected type %s", 10513 TYPENAME(wk->wk_type)); 10514 /* NOTREACHED */ 10515 } 10516 } 10517 FREE_LOCK(ump); 10518 PRELE(curproc); /* Allow swapout of kernel stack */ 10519 } 10520 10521 /* 10522 * Called from within the procedure above to deal with unsatisfied 10523 * allocation dependencies in a directory. The buffer must be locked, 10524 * thus, no I/O completion operations can occur while we are 10525 * manipulating its associated dependencies. 10526 */ 10527 static void 10528 initiate_write_filepage(pagedep, bp) 10529 struct pagedep *pagedep; 10530 struct buf *bp; 10531 { 10532 struct jremref *jremref; 10533 struct jmvref *jmvref; 10534 struct dirrem *dirrem; 10535 struct diradd *dap; 10536 struct direct *ep; 10537 int i; 10538 10539 if (pagedep->pd_state & IOSTARTED) { 10540 /* 10541 * This can only happen if there is a driver that does not 10542 * understand chaining. Here biodone will reissue the call 10543 * to strategy for the incomplete buffers. 10544 */ 10545 printf("initiate_write_filepage: already started\n"); 10546 return; 10547 } 10548 pagedep->pd_state |= IOSTARTED; 10549 /* 10550 * Wait for all journal remove dependencies to hit the disk. 10551 * We can not allow any potentially conflicting directory adds 10552 * to be visible before removes and rollback is too difficult. 10553 * The per-filesystem lock may be dropped and re-acquired, however 10554 * we hold the buf locked so the dependency can not go away. 10555 */ 10556 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) 10557 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) 10558 jwait(&jremref->jr_list, MNT_WAIT); 10559 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) 10560 jwait(&jmvref->jm_list, MNT_WAIT); 10561 for (i = 0; i < DAHASHSZ; i++) { 10562 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 10563 ep = (struct direct *) 10564 ((char *)bp->b_data + dap->da_offset); 10565 if (ep->d_ino != dap->da_newinum) 10566 panic("%s: dir inum %ju != new %ju", 10567 "initiate_write_filepage", 10568 (uintmax_t)ep->d_ino, 10569 (uintmax_t)dap->da_newinum); 10570 if (dap->da_state & DIRCHG) 10571 ep->d_ino = dap->da_previous->dm_oldinum; 10572 else 10573 ep->d_ino = 0; 10574 dap->da_state &= ~ATTACHED; 10575 dap->da_state |= UNDONE; 10576 } 10577 } 10578 } 10579 10580 /* 10581 * Version of initiate_write_inodeblock that handles UFS1 dinodes. 10582 * Note that any bug fixes made to this routine must be done in the 10583 * version found below. 10584 * 10585 * Called from within the procedure above to deal with unsatisfied 10586 * allocation dependencies in an inodeblock. The buffer must be 10587 * locked, thus, no I/O completion operations can occur while we 10588 * are manipulating its associated dependencies. 10589 */ 10590 static void 10591 initiate_write_inodeblock_ufs1(inodedep, bp) 10592 struct inodedep *inodedep; 10593 struct buf *bp; /* The inode block */ 10594 { 10595 struct allocdirect *adp, *lastadp; 10596 struct ufs1_dinode *dp; 10597 struct ufs1_dinode *sip; 10598 struct inoref *inoref; 10599 struct ufsmount *ump; 10600 struct fs *fs; 10601 ufs_lbn_t i; 10602 #ifdef INVARIANTS 10603 ufs_lbn_t prevlbn = 0; 10604 #endif 10605 int deplist; 10606 10607 if (inodedep->id_state & IOSTARTED) 10608 panic("initiate_write_inodeblock_ufs1: already started"); 10609 inodedep->id_state |= IOSTARTED; 10610 fs = inodedep->id_fs; 10611 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10612 LOCK_OWNED(ump); 10613 dp = (struct ufs1_dinode *)bp->b_data + 10614 ino_to_fsbo(fs, inodedep->id_ino); 10615 10616 /* 10617 * If we're on the unlinked list but have not yet written our 10618 * next pointer initialize it here. 10619 */ 10620 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10621 struct inodedep *inon; 10622 10623 inon = TAILQ_NEXT(inodedep, id_unlinked); 10624 dp->di_freelink = inon ? inon->id_ino : 0; 10625 } 10626 /* 10627 * If the bitmap is not yet written, then the allocated 10628 * inode cannot be written to disk. 10629 */ 10630 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10631 if (inodedep->id_savedino1 != NULL) 10632 panic("initiate_write_inodeblock_ufs1: I/O underway"); 10633 FREE_LOCK(ump); 10634 sip = malloc(sizeof(struct ufs1_dinode), 10635 M_SAVEDINO, M_SOFTDEP_FLAGS); 10636 ACQUIRE_LOCK(ump); 10637 inodedep->id_savedino1 = sip; 10638 *inodedep->id_savedino1 = *dp; 10639 bzero((caddr_t)dp, sizeof(struct ufs1_dinode)); 10640 dp->di_gen = inodedep->id_savedino1->di_gen; 10641 dp->di_freelink = inodedep->id_savedino1->di_freelink; 10642 return; 10643 } 10644 /* 10645 * If no dependencies, then there is nothing to roll back. 10646 */ 10647 inodedep->id_savedsize = dp->di_size; 10648 inodedep->id_savedextsize = 0; 10649 inodedep->id_savednlink = dp->di_nlink; 10650 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10651 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10652 return; 10653 /* 10654 * Revert the link count to that of the first unwritten journal entry. 10655 */ 10656 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10657 if (inoref) 10658 dp->di_nlink = inoref->if_nlink; 10659 /* 10660 * Set the dependencies to busy. 10661 */ 10662 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10663 adp = TAILQ_NEXT(adp, ad_next)) { 10664 #ifdef INVARIANTS 10665 if (deplist != 0 && prevlbn >= adp->ad_offset) 10666 panic("softdep_write_inodeblock: lbn order"); 10667 prevlbn = adp->ad_offset; 10668 if (adp->ad_offset < UFS_NDADDR && 10669 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10670 panic("initiate_write_inodeblock_ufs1: " 10671 "direct pointer #%jd mismatch %d != %jd", 10672 (intmax_t)adp->ad_offset, 10673 dp->di_db[adp->ad_offset], 10674 (intmax_t)adp->ad_newblkno); 10675 if (adp->ad_offset >= UFS_NDADDR && 10676 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno) 10677 panic("initiate_write_inodeblock_ufs1: " 10678 "indirect pointer #%jd mismatch %d != %jd", 10679 (intmax_t)adp->ad_offset - UFS_NDADDR, 10680 dp->di_ib[adp->ad_offset - UFS_NDADDR], 10681 (intmax_t)adp->ad_newblkno); 10682 deplist |= 1 << adp->ad_offset; 10683 if ((adp->ad_state & ATTACHED) == 0) 10684 panic("initiate_write_inodeblock_ufs1: " 10685 "Unknown state 0x%x", adp->ad_state); 10686 #endif /* INVARIANTS */ 10687 adp->ad_state &= ~ATTACHED; 10688 adp->ad_state |= UNDONE; 10689 } 10690 /* 10691 * The on-disk inode cannot claim to be any larger than the last 10692 * fragment that has been written. Otherwise, the on-disk inode 10693 * might have fragments that were not the last block in the file 10694 * which would corrupt the filesystem. 10695 */ 10696 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10697 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10698 if (adp->ad_offset >= UFS_NDADDR) 10699 break; 10700 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10701 /* keep going until hitting a rollback to a frag */ 10702 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10703 continue; 10704 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10705 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) { 10706 #ifdef INVARIANTS 10707 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10708 panic("initiate_write_inodeblock_ufs1: " 10709 "lost dep1"); 10710 #endif /* INVARIANTS */ 10711 dp->di_db[i] = 0; 10712 } 10713 for (i = 0; i < UFS_NIADDR; i++) { 10714 #ifdef INVARIANTS 10715 if (dp->di_ib[i] != 0 && 10716 (deplist & ((1 << UFS_NDADDR) << i)) == 0) 10717 panic("initiate_write_inodeblock_ufs1: " 10718 "lost dep2"); 10719 #endif /* INVARIANTS */ 10720 dp->di_ib[i] = 0; 10721 } 10722 return; 10723 } 10724 /* 10725 * If we have zero'ed out the last allocated block of the file, 10726 * roll back the size to the last currently allocated block. 10727 * We know that this last allocated block is a full-sized as 10728 * we already checked for fragments in the loop above. 10729 */ 10730 if (lastadp != NULL && 10731 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10732 for (i = lastadp->ad_offset; i >= 0; i--) 10733 if (dp->di_db[i] != 0) 10734 break; 10735 dp->di_size = (i + 1) * fs->fs_bsize; 10736 } 10737 /* 10738 * The only dependencies are for indirect blocks. 10739 * 10740 * The file size for indirect block additions is not guaranteed. 10741 * Such a guarantee would be non-trivial to achieve. The conventional 10742 * synchronous write implementation also does not make this guarantee. 10743 * Fsck should catch and fix discrepancies. Arguably, the file size 10744 * can be over-estimated without destroying integrity when the file 10745 * moves into the indirect blocks (i.e., is large). If we want to 10746 * postpone fsck, we are stuck with this argument. 10747 */ 10748 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10749 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0; 10750 } 10751 10752 /* 10753 * Version of initiate_write_inodeblock that handles UFS2 dinodes. 10754 * Note that any bug fixes made to this routine must be done in the 10755 * version found above. 10756 * 10757 * Called from within the procedure above to deal with unsatisfied 10758 * allocation dependencies in an inodeblock. The buffer must be 10759 * locked, thus, no I/O completion operations can occur while we 10760 * are manipulating its associated dependencies. 10761 */ 10762 static void 10763 initiate_write_inodeblock_ufs2(inodedep, bp) 10764 struct inodedep *inodedep; 10765 struct buf *bp; /* The inode block */ 10766 { 10767 struct allocdirect *adp, *lastadp; 10768 struct ufs2_dinode *dp; 10769 struct ufs2_dinode *sip; 10770 struct inoref *inoref; 10771 struct ufsmount *ump; 10772 struct fs *fs; 10773 ufs_lbn_t i; 10774 #ifdef INVARIANTS 10775 ufs_lbn_t prevlbn = 0; 10776 #endif 10777 int deplist; 10778 10779 if (inodedep->id_state & IOSTARTED) 10780 panic("initiate_write_inodeblock_ufs2: already started"); 10781 inodedep->id_state |= IOSTARTED; 10782 fs = inodedep->id_fs; 10783 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10784 LOCK_OWNED(ump); 10785 dp = (struct ufs2_dinode *)bp->b_data + 10786 ino_to_fsbo(fs, inodedep->id_ino); 10787 10788 /* 10789 * If we're on the unlinked list but have not yet written our 10790 * next pointer initialize it here. 10791 */ 10792 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10793 struct inodedep *inon; 10794 10795 inon = TAILQ_NEXT(inodedep, id_unlinked); 10796 dp->di_freelink = inon ? inon->id_ino : 0; 10797 ffs_update_dinode_ckhash(fs, dp); 10798 } 10799 /* 10800 * If the bitmap is not yet written, then the allocated 10801 * inode cannot be written to disk. 10802 */ 10803 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10804 if (inodedep->id_savedino2 != NULL) 10805 panic("initiate_write_inodeblock_ufs2: I/O underway"); 10806 FREE_LOCK(ump); 10807 sip = malloc(sizeof(struct ufs2_dinode), 10808 M_SAVEDINO, M_SOFTDEP_FLAGS); 10809 ACQUIRE_LOCK(ump); 10810 inodedep->id_savedino2 = sip; 10811 *inodedep->id_savedino2 = *dp; 10812 bzero((caddr_t)dp, sizeof(struct ufs2_dinode)); 10813 dp->di_gen = inodedep->id_savedino2->di_gen; 10814 dp->di_freelink = inodedep->id_savedino2->di_freelink; 10815 return; 10816 } 10817 /* 10818 * If no dependencies, then there is nothing to roll back. 10819 */ 10820 inodedep->id_savedsize = dp->di_size; 10821 inodedep->id_savedextsize = dp->di_extsize; 10822 inodedep->id_savednlink = dp->di_nlink; 10823 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10824 TAILQ_EMPTY(&inodedep->id_extupdt) && 10825 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10826 return; 10827 /* 10828 * Revert the link count to that of the first unwritten journal entry. 10829 */ 10830 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10831 if (inoref) 10832 dp->di_nlink = inoref->if_nlink; 10833 10834 /* 10835 * Set the ext data dependencies to busy. 10836 */ 10837 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10838 adp = TAILQ_NEXT(adp, ad_next)) { 10839 #ifdef INVARIANTS 10840 if (deplist != 0 && prevlbn >= adp->ad_offset) 10841 panic("initiate_write_inodeblock_ufs2: lbn order"); 10842 prevlbn = adp->ad_offset; 10843 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno) 10844 panic("initiate_write_inodeblock_ufs2: " 10845 "ext pointer #%jd mismatch %jd != %jd", 10846 (intmax_t)adp->ad_offset, 10847 (intmax_t)dp->di_extb[adp->ad_offset], 10848 (intmax_t)adp->ad_newblkno); 10849 deplist |= 1 << adp->ad_offset; 10850 if ((adp->ad_state & ATTACHED) == 0) 10851 panic("initiate_write_inodeblock_ufs2: Unknown " 10852 "state 0x%x", adp->ad_state); 10853 #endif /* INVARIANTS */ 10854 adp->ad_state &= ~ATTACHED; 10855 adp->ad_state |= UNDONE; 10856 } 10857 /* 10858 * The on-disk inode cannot claim to be any larger than the last 10859 * fragment that has been written. Otherwise, the on-disk inode 10860 * might have fragments that were not the last block in the ext 10861 * data which would corrupt the filesystem. 10862 */ 10863 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10864 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10865 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno; 10866 /* keep going until hitting a rollback to a frag */ 10867 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10868 continue; 10869 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10870 for (i = adp->ad_offset + 1; i < UFS_NXADDR; i++) { 10871 #ifdef INVARIANTS 10872 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0) 10873 panic("initiate_write_inodeblock_ufs2: " 10874 "lost dep1"); 10875 #endif /* INVARIANTS */ 10876 dp->di_extb[i] = 0; 10877 } 10878 lastadp = NULL; 10879 break; 10880 } 10881 /* 10882 * If we have zero'ed out the last allocated block of the ext 10883 * data, roll back the size to the last currently allocated block. 10884 * We know that this last allocated block is a full-sized as 10885 * we already checked for fragments in the loop above. 10886 */ 10887 if (lastadp != NULL && 10888 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10889 for (i = lastadp->ad_offset; i >= 0; i--) 10890 if (dp->di_extb[i] != 0) 10891 break; 10892 dp->di_extsize = (i + 1) * fs->fs_bsize; 10893 } 10894 /* 10895 * Set the file data dependencies to busy. 10896 */ 10897 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10898 adp = TAILQ_NEXT(adp, ad_next)) { 10899 #ifdef INVARIANTS 10900 if (deplist != 0 && prevlbn >= adp->ad_offset) 10901 panic("softdep_write_inodeblock: lbn order"); 10902 if ((adp->ad_state & ATTACHED) == 0) 10903 panic("inodedep %p and adp %p not attached", inodedep, adp); 10904 prevlbn = adp->ad_offset; 10905 if (!ffs_fsfail_cleanup(ump, 0) && 10906 adp->ad_offset < UFS_NDADDR && 10907 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10908 panic("initiate_write_inodeblock_ufs2: " 10909 "direct pointer #%jd mismatch %jd != %jd", 10910 (intmax_t)adp->ad_offset, 10911 (intmax_t)dp->di_db[adp->ad_offset], 10912 (intmax_t)adp->ad_newblkno); 10913 if (!ffs_fsfail_cleanup(ump, 0) && 10914 adp->ad_offset >= UFS_NDADDR && 10915 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno) 10916 panic("initiate_write_inodeblock_ufs2: " 10917 "indirect pointer #%jd mismatch %jd != %jd", 10918 (intmax_t)adp->ad_offset - UFS_NDADDR, 10919 (intmax_t)dp->di_ib[adp->ad_offset - UFS_NDADDR], 10920 (intmax_t)adp->ad_newblkno); 10921 deplist |= 1 << adp->ad_offset; 10922 if ((adp->ad_state & ATTACHED) == 0) 10923 panic("initiate_write_inodeblock_ufs2: Unknown " 10924 "state 0x%x", adp->ad_state); 10925 #endif /* INVARIANTS */ 10926 adp->ad_state &= ~ATTACHED; 10927 adp->ad_state |= UNDONE; 10928 } 10929 /* 10930 * The on-disk inode cannot claim to be any larger than the last 10931 * fragment that has been written. Otherwise, the on-disk inode 10932 * might have fragments that were not the last block in the file 10933 * which would corrupt the filesystem. 10934 */ 10935 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10936 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10937 if (adp->ad_offset >= UFS_NDADDR) 10938 break; 10939 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10940 /* keep going until hitting a rollback to a frag */ 10941 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10942 continue; 10943 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10944 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) { 10945 #ifdef INVARIANTS 10946 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10947 panic("initiate_write_inodeblock_ufs2: " 10948 "lost dep2"); 10949 #endif /* INVARIANTS */ 10950 dp->di_db[i] = 0; 10951 } 10952 for (i = 0; i < UFS_NIADDR; i++) { 10953 #ifdef INVARIANTS 10954 if (dp->di_ib[i] != 0 && 10955 (deplist & ((1 << UFS_NDADDR) << i)) == 0) 10956 panic("initiate_write_inodeblock_ufs2: " 10957 "lost dep3"); 10958 #endif /* INVARIANTS */ 10959 dp->di_ib[i] = 0; 10960 } 10961 ffs_update_dinode_ckhash(fs, dp); 10962 return; 10963 } 10964 /* 10965 * If we have zero'ed out the last allocated block of the file, 10966 * roll back the size to the last currently allocated block. 10967 * We know that this last allocated block is a full-sized as 10968 * we already checked for fragments in the loop above. 10969 */ 10970 if (lastadp != NULL && 10971 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10972 for (i = lastadp->ad_offset; i >= 0; i--) 10973 if (dp->di_db[i] != 0) 10974 break; 10975 dp->di_size = (i + 1) * fs->fs_bsize; 10976 } 10977 /* 10978 * The only dependencies are for indirect blocks. 10979 * 10980 * The file size for indirect block additions is not guaranteed. 10981 * Such a guarantee would be non-trivial to achieve. The conventional 10982 * synchronous write implementation also does not make this guarantee. 10983 * Fsck should catch and fix discrepancies. Arguably, the file size 10984 * can be over-estimated without destroying integrity when the file 10985 * moves into the indirect blocks (i.e., is large). If we want to 10986 * postpone fsck, we are stuck with this argument. 10987 */ 10988 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10989 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0; 10990 ffs_update_dinode_ckhash(fs, dp); 10991 } 10992 10993 /* 10994 * Cancel an indirdep as a result of truncation. Release all of the 10995 * children allocindirs and place their journal work on the appropriate 10996 * list. 10997 */ 10998 static void 10999 cancel_indirdep(indirdep, bp, freeblks) 11000 struct indirdep *indirdep; 11001 struct buf *bp; 11002 struct freeblks *freeblks; 11003 { 11004 struct allocindir *aip; 11005 11006 /* 11007 * None of the indirect pointers will ever be visible, 11008 * so they can simply be tossed. GOINGAWAY ensures 11009 * that allocated pointers will be saved in the buffer 11010 * cache until they are freed. Note that they will 11011 * only be able to be found by their physical address 11012 * since the inode mapping the logical address will 11013 * be gone. The save buffer used for the safe copy 11014 * was allocated in setup_allocindir_phase2 using 11015 * the physical address so it could be used for this 11016 * purpose. Hence we swap the safe copy with the real 11017 * copy, allowing the safe copy to be freed and holding 11018 * on to the real copy for later use in indir_trunc. 11019 */ 11020 if (indirdep->ir_state & GOINGAWAY) 11021 panic("cancel_indirdep: already gone"); 11022 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 11023 indirdep->ir_state |= DEPCOMPLETE; 11024 LIST_REMOVE(indirdep, ir_next); 11025 } 11026 indirdep->ir_state |= GOINGAWAY; 11027 /* 11028 * Pass in bp for blocks still have journal writes 11029 * pending so we can cancel them on their own. 11030 */ 11031 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != NULL) 11032 cancel_allocindir(aip, bp, freeblks, 0); 11033 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) 11034 cancel_allocindir(aip, NULL, freeblks, 0); 11035 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) 11036 cancel_allocindir(aip, NULL, freeblks, 0); 11037 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) 11038 cancel_allocindir(aip, NULL, freeblks, 0); 11039 /* 11040 * If there are pending partial truncations we need to keep the 11041 * old block copy around until they complete. This is because 11042 * the current b_data is not a perfect superset of the available 11043 * blocks. 11044 */ 11045 if (TAILQ_EMPTY(&indirdep->ir_trunc)) 11046 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount); 11047 else 11048 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 11049 WORKLIST_REMOVE(&indirdep->ir_list); 11050 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list); 11051 indirdep->ir_bp = NULL; 11052 indirdep->ir_freeblks = freeblks; 11053 } 11054 11055 /* 11056 * Free an indirdep once it no longer has new pointers to track. 11057 */ 11058 static void 11059 free_indirdep(indirdep) 11060 struct indirdep *indirdep; 11061 { 11062 11063 KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc), 11064 ("free_indirdep: Indir trunc list not empty.")); 11065 KASSERT(LIST_EMPTY(&indirdep->ir_completehd), 11066 ("free_indirdep: Complete head not empty.")); 11067 KASSERT(LIST_EMPTY(&indirdep->ir_writehd), 11068 ("free_indirdep: write head not empty.")); 11069 KASSERT(LIST_EMPTY(&indirdep->ir_donehd), 11070 ("free_indirdep: done head not empty.")); 11071 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd), 11072 ("free_indirdep: deplist head not empty.")); 11073 KASSERT((indirdep->ir_state & DEPCOMPLETE), 11074 ("free_indirdep: %p still on newblk list.", indirdep)); 11075 KASSERT(indirdep->ir_saveddata == NULL, 11076 ("free_indirdep: %p still has saved data.", indirdep)); 11077 KASSERT(indirdep->ir_savebp == NULL, 11078 ("free_indirdep: %p still has savebp buffer.", indirdep)); 11079 if (indirdep->ir_state & ONWORKLIST) 11080 WORKLIST_REMOVE(&indirdep->ir_list); 11081 WORKITEM_FREE(indirdep, D_INDIRDEP); 11082 } 11083 11084 /* 11085 * Called before a write to an indirdep. This routine is responsible for 11086 * rolling back pointers to a safe state which includes only those 11087 * allocindirs which have been completed. 11088 */ 11089 static void 11090 initiate_write_indirdep(indirdep, bp) 11091 struct indirdep *indirdep; 11092 struct buf *bp; 11093 { 11094 struct ufsmount *ump; 11095 11096 indirdep->ir_state |= IOSTARTED; 11097 if (indirdep->ir_state & GOINGAWAY) 11098 panic("disk_io_initiation: indirdep gone"); 11099 /* 11100 * If there are no remaining dependencies, this will be writing 11101 * the real pointers. 11102 */ 11103 if (LIST_EMPTY(&indirdep->ir_deplisthd) && 11104 TAILQ_EMPTY(&indirdep->ir_trunc)) 11105 return; 11106 /* 11107 * Replace up-to-date version with safe version. 11108 */ 11109 if (indirdep->ir_saveddata == NULL) { 11110 ump = VFSTOUFS(indirdep->ir_list.wk_mp); 11111 LOCK_OWNED(ump); 11112 FREE_LOCK(ump); 11113 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 11114 M_SOFTDEP_FLAGS); 11115 ACQUIRE_LOCK(ump); 11116 } 11117 indirdep->ir_state &= ~ATTACHED; 11118 indirdep->ir_state |= UNDONE; 11119 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 11120 bcopy(indirdep->ir_savebp->b_data, bp->b_data, 11121 bp->b_bcount); 11122 } 11123 11124 /* 11125 * Called when an inode has been cleared in a cg bitmap. This finally 11126 * eliminates any canceled jaddrefs 11127 */ 11128 void 11129 softdep_setup_inofree(mp, bp, ino, wkhd) 11130 struct mount *mp; 11131 struct buf *bp; 11132 ino_t ino; 11133 struct workhead *wkhd; 11134 { 11135 struct worklist *wk, *wkn; 11136 struct inodedep *inodedep; 11137 struct ufsmount *ump; 11138 uint8_t *inosused; 11139 struct cg *cgp; 11140 struct fs *fs; 11141 11142 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 11143 ("softdep_setup_inofree called on non-softdep filesystem")); 11144 ump = VFSTOUFS(mp); 11145 ACQUIRE_LOCK(ump); 11146 if (!ffs_fsfail_cleanup(ump, 0)) { 11147 fs = ump->um_fs; 11148 cgp = (struct cg *)bp->b_data; 11149 inosused = cg_inosused(cgp); 11150 if (isset(inosused, ino % fs->fs_ipg)) 11151 panic("softdep_setup_inofree: inode %ju not freed.", 11152 (uintmax_t)ino); 11153 } 11154 if (inodedep_lookup(mp, ino, 0, &inodedep)) 11155 panic("softdep_setup_inofree: ino %ju has existing inodedep %p", 11156 (uintmax_t)ino, inodedep); 11157 if (wkhd) { 11158 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) { 11159 if (wk->wk_type != D_JADDREF) 11160 continue; 11161 WORKLIST_REMOVE(wk); 11162 /* 11163 * We can free immediately even if the jaddref 11164 * isn't attached in a background write as now 11165 * the bitmaps are reconciled. 11166 */ 11167 wk->wk_state |= COMPLETE | ATTACHED; 11168 free_jaddref(WK_JADDREF(wk)); 11169 } 11170 jwork_move(&bp->b_dep, wkhd); 11171 } 11172 FREE_LOCK(ump); 11173 } 11174 11175 /* 11176 * Called via ffs_blkfree() after a set of frags has been cleared from a cg 11177 * map. Any dependencies waiting for the write to clear are added to the 11178 * buf's list and any jnewblks that are being canceled are discarded 11179 * immediately. 11180 */ 11181 void 11182 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 11183 struct mount *mp; 11184 struct buf *bp; 11185 ufs2_daddr_t blkno; 11186 int frags; 11187 struct workhead *wkhd; 11188 { 11189 struct bmsafemap *bmsafemap; 11190 struct jnewblk *jnewblk; 11191 struct ufsmount *ump; 11192 struct worklist *wk; 11193 struct fs *fs; 11194 #ifdef INVARIANTS 11195 uint8_t *blksfree; 11196 struct cg *cgp; 11197 ufs2_daddr_t jstart; 11198 ufs2_daddr_t jend; 11199 ufs2_daddr_t end; 11200 long bno; 11201 int i; 11202 #endif 11203 11204 CTR3(KTR_SUJ, 11205 "softdep_setup_blkfree: blkno %jd frags %d wk head %p", 11206 blkno, frags, wkhd); 11207 11208 ump = VFSTOUFS(mp); 11209 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 11210 ("softdep_setup_blkfree called on non-softdep filesystem")); 11211 ACQUIRE_LOCK(ump); 11212 /* Lookup the bmsafemap so we track when it is dirty. */ 11213 fs = ump->um_fs; 11214 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 11215 /* 11216 * Detach any jnewblks which have been canceled. They must linger 11217 * until the bitmap is cleared again by ffs_blkfree() to prevent 11218 * an unjournaled allocation from hitting the disk. 11219 */ 11220 if (wkhd) { 11221 while ((wk = LIST_FIRST(wkhd)) != NULL) { 11222 CTR2(KTR_SUJ, 11223 "softdep_setup_blkfree: blkno %jd wk type %d", 11224 blkno, wk->wk_type); 11225 WORKLIST_REMOVE(wk); 11226 if (wk->wk_type != D_JNEWBLK) { 11227 WORKLIST_INSERT(&bmsafemap->sm_freehd, wk); 11228 continue; 11229 } 11230 jnewblk = WK_JNEWBLK(wk); 11231 KASSERT(jnewblk->jn_state & GOINGAWAY, 11232 ("softdep_setup_blkfree: jnewblk not canceled.")); 11233 #ifdef INVARIANTS 11234 /* 11235 * Assert that this block is free in the bitmap 11236 * before we discard the jnewblk. 11237 */ 11238 cgp = (struct cg *)bp->b_data; 11239 blksfree = cg_blksfree(cgp); 11240 bno = dtogd(fs, jnewblk->jn_blkno); 11241 for (i = jnewblk->jn_oldfrags; 11242 i < jnewblk->jn_frags; i++) { 11243 if (isset(blksfree, bno + i)) 11244 continue; 11245 panic("softdep_setup_blkfree: not free"); 11246 } 11247 #endif 11248 /* 11249 * Even if it's not attached we can free immediately 11250 * as the new bitmap is correct. 11251 */ 11252 wk->wk_state |= COMPLETE | ATTACHED; 11253 free_jnewblk(jnewblk); 11254 } 11255 } 11256 11257 #ifdef INVARIANTS 11258 /* 11259 * Assert that we are not freeing a block which has an outstanding 11260 * allocation dependency. 11261 */ 11262 fs = VFSTOUFS(mp)->um_fs; 11263 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 11264 end = blkno + frags; 11265 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 11266 /* 11267 * Don't match against blocks that will be freed when the 11268 * background write is done. 11269 */ 11270 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) == 11271 (COMPLETE | DEPCOMPLETE)) 11272 continue; 11273 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags; 11274 jend = jnewblk->jn_blkno + jnewblk->jn_frags; 11275 if ((blkno >= jstart && blkno < jend) || 11276 (end > jstart && end <= jend)) { 11277 printf("state 0x%X %jd - %d %d dep %p\n", 11278 jnewblk->jn_state, jnewblk->jn_blkno, 11279 jnewblk->jn_oldfrags, jnewblk->jn_frags, 11280 jnewblk->jn_dep); 11281 panic("softdep_setup_blkfree: " 11282 "%jd-%jd(%d) overlaps with %jd-%jd", 11283 blkno, end, frags, jstart, jend); 11284 } 11285 } 11286 #endif 11287 FREE_LOCK(ump); 11288 } 11289 11290 /* 11291 * Revert a block allocation when the journal record that describes it 11292 * is not yet written. 11293 */ 11294 static int 11295 jnewblk_rollback(jnewblk, fs, cgp, blksfree) 11296 struct jnewblk *jnewblk; 11297 struct fs *fs; 11298 struct cg *cgp; 11299 uint8_t *blksfree; 11300 { 11301 ufs1_daddr_t fragno; 11302 long cgbno, bbase; 11303 int frags, blk; 11304 int i; 11305 11306 frags = 0; 11307 cgbno = dtogd(fs, jnewblk->jn_blkno); 11308 /* 11309 * We have to test which frags need to be rolled back. We may 11310 * be operating on a stale copy when doing background writes. 11311 */ 11312 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) 11313 if (isclr(blksfree, cgbno + i)) 11314 frags++; 11315 if (frags == 0) 11316 return (0); 11317 /* 11318 * This is mostly ffs_blkfree() sans some validation and 11319 * superblock updates. 11320 */ 11321 if (frags == fs->fs_frag) { 11322 fragno = fragstoblks(fs, cgbno); 11323 ffs_setblock(fs, blksfree, fragno); 11324 ffs_clusteracct(fs, cgp, fragno, 1); 11325 cgp->cg_cs.cs_nbfree++; 11326 } else { 11327 cgbno += jnewblk->jn_oldfrags; 11328 bbase = cgbno - fragnum(fs, cgbno); 11329 /* Decrement the old frags. */ 11330 blk = blkmap(fs, blksfree, bbase); 11331 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 11332 /* Deallocate the fragment */ 11333 for (i = 0; i < frags; i++) 11334 setbit(blksfree, cgbno + i); 11335 cgp->cg_cs.cs_nffree += frags; 11336 /* Add back in counts associated with the new frags */ 11337 blk = blkmap(fs, blksfree, bbase); 11338 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 11339 /* If a complete block has been reassembled, account for it. */ 11340 fragno = fragstoblks(fs, bbase); 11341 if (ffs_isblock(fs, blksfree, fragno)) { 11342 cgp->cg_cs.cs_nffree -= fs->fs_frag; 11343 ffs_clusteracct(fs, cgp, fragno, 1); 11344 cgp->cg_cs.cs_nbfree++; 11345 } 11346 } 11347 stat_jnewblk++; 11348 jnewblk->jn_state &= ~ATTACHED; 11349 jnewblk->jn_state |= UNDONE; 11350 11351 return (frags); 11352 } 11353 11354 static void 11355 initiate_write_bmsafemap(bmsafemap, bp) 11356 struct bmsafemap *bmsafemap; 11357 struct buf *bp; /* The cg block. */ 11358 { 11359 struct jaddref *jaddref; 11360 struct jnewblk *jnewblk; 11361 uint8_t *inosused; 11362 uint8_t *blksfree; 11363 struct cg *cgp; 11364 struct fs *fs; 11365 ino_t ino; 11366 11367 /* 11368 * If this is a background write, we did this at the time that 11369 * the copy was made, so do not need to do it again. 11370 */ 11371 if (bmsafemap->sm_state & IOSTARTED) 11372 return; 11373 bmsafemap->sm_state |= IOSTARTED; 11374 /* 11375 * Clear any inode allocations which are pending journal writes. 11376 */ 11377 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) { 11378 cgp = (struct cg *)bp->b_data; 11379 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11380 inosused = cg_inosused(cgp); 11381 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) { 11382 ino = jaddref->ja_ino % fs->fs_ipg; 11383 if (isset(inosused, ino)) { 11384 if ((jaddref->ja_mode & IFMT) == IFDIR) 11385 cgp->cg_cs.cs_ndir--; 11386 cgp->cg_cs.cs_nifree++; 11387 clrbit(inosused, ino); 11388 jaddref->ja_state &= ~ATTACHED; 11389 jaddref->ja_state |= UNDONE; 11390 stat_jaddref++; 11391 } else 11392 panic("initiate_write_bmsafemap: inode %ju " 11393 "marked free", (uintmax_t)jaddref->ja_ino); 11394 } 11395 } 11396 /* 11397 * Clear any block allocations which are pending journal writes. 11398 */ 11399 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 11400 cgp = (struct cg *)bp->b_data; 11401 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11402 blksfree = cg_blksfree(cgp); 11403 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 11404 if (jnewblk_rollback(jnewblk, fs, cgp, blksfree)) 11405 continue; 11406 panic("initiate_write_bmsafemap: block %jd " 11407 "marked free", jnewblk->jn_blkno); 11408 } 11409 } 11410 /* 11411 * Move allocation lists to the written lists so they can be 11412 * cleared once the block write is complete. 11413 */ 11414 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr, 11415 inodedep, id_deps); 11416 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 11417 newblk, nb_deps); 11418 LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist, 11419 wk_list); 11420 } 11421 11422 void 11423 softdep_handle_error(struct buf *bp) 11424 { 11425 struct ufsmount *ump; 11426 11427 ump = softdep_bp_to_mp(bp); 11428 if (ump == NULL) 11429 return; 11430 11431 if (ffs_fsfail_cleanup(ump, bp->b_error)) { 11432 /* 11433 * No future writes will succeed, so the on-disk image is safe. 11434 * Pretend that this write succeeded so that the softdep state 11435 * will be cleaned up naturally. 11436 */ 11437 bp->b_ioflags &= ~BIO_ERROR; 11438 bp->b_error = 0; 11439 } 11440 } 11441 11442 /* 11443 * This routine is called during the completion interrupt 11444 * service routine for a disk write (from the procedure called 11445 * by the device driver to inform the filesystem caches of 11446 * a request completion). It should be called early in this 11447 * procedure, before the block is made available to other 11448 * processes or other routines are called. 11449 * 11450 */ 11451 static void 11452 softdep_disk_write_complete(bp) 11453 struct buf *bp; /* describes the completed disk write */ 11454 { 11455 struct worklist *wk; 11456 struct worklist *owk; 11457 struct ufsmount *ump; 11458 struct workhead reattach; 11459 struct freeblks *freeblks; 11460 struct buf *sbp; 11461 11462 ump = softdep_bp_to_mp(bp); 11463 KASSERT(LIST_EMPTY(&bp->b_dep) || ump != NULL, 11464 ("softdep_disk_write_complete: softdep_bp_to_mp returned NULL " 11465 "with outstanding dependencies for buffer %p", bp)); 11466 if (ump == NULL) 11467 return; 11468 if ((bp->b_ioflags & BIO_ERROR) != 0) 11469 softdep_handle_error(bp); 11470 /* 11471 * If an error occurred while doing the write, then the data 11472 * has not hit the disk and the dependencies cannot be processed. 11473 * But we do have to go through and roll forward any dependencies 11474 * that were rolled back before the disk write. 11475 */ 11476 sbp = NULL; 11477 ACQUIRE_LOCK(ump); 11478 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) { 11479 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 11480 switch (wk->wk_type) { 11481 case D_PAGEDEP: 11482 handle_written_filepage(WK_PAGEDEP(wk), bp, 0); 11483 continue; 11484 11485 case D_INODEDEP: 11486 handle_written_inodeblock(WK_INODEDEP(wk), 11487 bp, 0); 11488 continue; 11489 11490 case D_BMSAFEMAP: 11491 handle_written_bmsafemap(WK_BMSAFEMAP(wk), 11492 bp, 0); 11493 continue; 11494 11495 case D_INDIRDEP: 11496 handle_written_indirdep(WK_INDIRDEP(wk), 11497 bp, &sbp, 0); 11498 continue; 11499 default: 11500 /* nothing to roll forward */ 11501 continue; 11502 } 11503 } 11504 FREE_LOCK(ump); 11505 if (sbp) 11506 brelse(sbp); 11507 return; 11508 } 11509 LIST_INIT(&reattach); 11510 11511 /* 11512 * Ump SU lock must not be released anywhere in this code segment. 11513 */ 11514 owk = NULL; 11515 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) { 11516 WORKLIST_REMOVE(wk); 11517 atomic_add_long(&dep_write[wk->wk_type], 1); 11518 if (wk == owk) 11519 panic("duplicate worklist: %p\n", wk); 11520 owk = wk; 11521 switch (wk->wk_type) { 11522 case D_PAGEDEP: 11523 if (handle_written_filepage(WK_PAGEDEP(wk), bp, 11524 WRITESUCCEEDED)) 11525 WORKLIST_INSERT(&reattach, wk); 11526 continue; 11527 11528 case D_INODEDEP: 11529 if (handle_written_inodeblock(WK_INODEDEP(wk), bp, 11530 WRITESUCCEEDED)) 11531 WORKLIST_INSERT(&reattach, wk); 11532 continue; 11533 11534 case D_BMSAFEMAP: 11535 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp, 11536 WRITESUCCEEDED)) 11537 WORKLIST_INSERT(&reattach, wk); 11538 continue; 11539 11540 case D_MKDIR: 11541 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 11542 continue; 11543 11544 case D_ALLOCDIRECT: 11545 wk->wk_state |= COMPLETE; 11546 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL); 11547 continue; 11548 11549 case D_ALLOCINDIR: 11550 wk->wk_state |= COMPLETE; 11551 handle_allocindir_partdone(WK_ALLOCINDIR(wk)); 11552 continue; 11553 11554 case D_INDIRDEP: 11555 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp, 11556 WRITESUCCEEDED)) 11557 WORKLIST_INSERT(&reattach, wk); 11558 continue; 11559 11560 case D_FREEBLKS: 11561 wk->wk_state |= COMPLETE; 11562 freeblks = WK_FREEBLKS(wk); 11563 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE && 11564 LIST_EMPTY(&freeblks->fb_jblkdephd)) 11565 add_to_worklist(wk, WK_NODELAY); 11566 continue; 11567 11568 case D_FREEWORK: 11569 handle_written_freework(WK_FREEWORK(wk)); 11570 break; 11571 11572 case D_JSEGDEP: 11573 free_jsegdep(WK_JSEGDEP(wk)); 11574 continue; 11575 11576 case D_JSEG: 11577 handle_written_jseg(WK_JSEG(wk), bp); 11578 continue; 11579 11580 case D_SBDEP: 11581 if (handle_written_sbdep(WK_SBDEP(wk), bp)) 11582 WORKLIST_INSERT(&reattach, wk); 11583 continue; 11584 11585 case D_FREEDEP: 11586 free_freedep(WK_FREEDEP(wk)); 11587 continue; 11588 11589 default: 11590 panic("handle_disk_write_complete: Unknown type %s", 11591 TYPENAME(wk->wk_type)); 11592 /* NOTREACHED */ 11593 } 11594 } 11595 /* 11596 * Reattach any requests that must be redone. 11597 */ 11598 while ((wk = LIST_FIRST(&reattach)) != NULL) { 11599 WORKLIST_REMOVE(wk); 11600 WORKLIST_INSERT(&bp->b_dep, wk); 11601 } 11602 FREE_LOCK(ump); 11603 if (sbp) 11604 brelse(sbp); 11605 } 11606 11607 /* 11608 * Called from within softdep_disk_write_complete above. 11609 */ 11610 static void 11611 handle_allocdirect_partdone(adp, wkhd) 11612 struct allocdirect *adp; /* the completed allocdirect */ 11613 struct workhead *wkhd; /* Work to do when inode is writtne. */ 11614 { 11615 struct allocdirectlst *listhead; 11616 struct allocdirect *listadp; 11617 struct inodedep *inodedep; 11618 long bsize; 11619 11620 LOCK_OWNED(VFSTOUFS(adp->ad_block.nb_list.wk_mp)); 11621 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11622 return; 11623 /* 11624 * The on-disk inode cannot claim to be any larger than the last 11625 * fragment that has been written. Otherwise, the on-disk inode 11626 * might have fragments that were not the last block in the file 11627 * which would corrupt the filesystem. Thus, we cannot free any 11628 * allocdirects after one whose ad_oldblkno claims a fragment as 11629 * these blocks must be rolled back to zero before writing the inode. 11630 * We check the currently active set of allocdirects in id_inoupdt 11631 * or id_extupdt as appropriate. 11632 */ 11633 inodedep = adp->ad_inodedep; 11634 bsize = inodedep->id_fs->fs_bsize; 11635 if (adp->ad_state & EXTDATA) 11636 listhead = &inodedep->id_extupdt; 11637 else 11638 listhead = &inodedep->id_inoupdt; 11639 TAILQ_FOREACH(listadp, listhead, ad_next) { 11640 /* found our block */ 11641 if (listadp == adp) 11642 break; 11643 /* continue if ad_oldlbn is not a fragment */ 11644 if (listadp->ad_oldsize == 0 || 11645 listadp->ad_oldsize == bsize) 11646 continue; 11647 /* hit a fragment */ 11648 return; 11649 } 11650 /* 11651 * If we have reached the end of the current list without 11652 * finding the just finished dependency, then it must be 11653 * on the future dependency list. Future dependencies cannot 11654 * be freed until they are moved to the current list. 11655 */ 11656 if (listadp == NULL) { 11657 #ifdef INVARIANTS 11658 if (adp->ad_state & EXTDATA) 11659 listhead = &inodedep->id_newextupdt; 11660 else 11661 listhead = &inodedep->id_newinoupdt; 11662 TAILQ_FOREACH(listadp, listhead, ad_next) 11663 /* found our block */ 11664 if (listadp == adp) 11665 break; 11666 if (listadp == NULL) 11667 panic("handle_allocdirect_partdone: lost dep"); 11668 #endif /* INVARIANTS */ 11669 return; 11670 } 11671 /* 11672 * If we have found the just finished dependency, then queue 11673 * it along with anything that follows it that is complete. 11674 * Since the pointer has not yet been written in the inode 11675 * as the dependency prevents it, place the allocdirect on the 11676 * bufwait list where it will be freed once the pointer is 11677 * valid. 11678 */ 11679 if (wkhd == NULL) 11680 wkhd = &inodedep->id_bufwait; 11681 for (; adp; adp = listadp) { 11682 listadp = TAILQ_NEXT(adp, ad_next); 11683 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11684 return; 11685 TAILQ_REMOVE(listhead, adp, ad_next); 11686 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list); 11687 } 11688 } 11689 11690 /* 11691 * Called from within softdep_disk_write_complete above. This routine 11692 * completes successfully written allocindirs. 11693 */ 11694 static void 11695 handle_allocindir_partdone(aip) 11696 struct allocindir *aip; /* the completed allocindir */ 11697 { 11698 struct indirdep *indirdep; 11699 11700 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE) 11701 return; 11702 indirdep = aip->ai_indirdep; 11703 LIST_REMOVE(aip, ai_next); 11704 /* 11705 * Don't set a pointer while the buffer is undergoing IO or while 11706 * we have active truncations. 11707 */ 11708 if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) { 11709 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next); 11710 return; 11711 } 11712 if (indirdep->ir_state & UFS1FMT) 11713 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11714 aip->ai_newblkno; 11715 else 11716 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11717 aip->ai_newblkno; 11718 /* 11719 * Await the pointer write before freeing the allocindir. 11720 */ 11721 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next); 11722 } 11723 11724 /* 11725 * Release segments held on a jwork list. 11726 */ 11727 static void 11728 handle_jwork(wkhd) 11729 struct workhead *wkhd; 11730 { 11731 struct worklist *wk; 11732 11733 while ((wk = LIST_FIRST(wkhd)) != NULL) { 11734 WORKLIST_REMOVE(wk); 11735 switch (wk->wk_type) { 11736 case D_JSEGDEP: 11737 free_jsegdep(WK_JSEGDEP(wk)); 11738 continue; 11739 case D_FREEDEP: 11740 free_freedep(WK_FREEDEP(wk)); 11741 continue; 11742 case D_FREEFRAG: 11743 rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep)); 11744 WORKITEM_FREE(wk, D_FREEFRAG); 11745 continue; 11746 case D_FREEWORK: 11747 handle_written_freework(WK_FREEWORK(wk)); 11748 continue; 11749 default: 11750 panic("handle_jwork: Unknown type %s\n", 11751 TYPENAME(wk->wk_type)); 11752 } 11753 } 11754 } 11755 11756 /* 11757 * Handle the bufwait list on an inode when it is safe to release items 11758 * held there. This normally happens after an inode block is written but 11759 * may be delayed and handled later if there are pending journal items that 11760 * are not yet safe to be released. 11761 */ 11762 static struct freefile * 11763 handle_bufwait(inodedep, refhd) 11764 struct inodedep *inodedep; 11765 struct workhead *refhd; 11766 { 11767 struct jaddref *jaddref; 11768 struct freefile *freefile; 11769 struct worklist *wk; 11770 11771 freefile = NULL; 11772 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) { 11773 WORKLIST_REMOVE(wk); 11774 switch (wk->wk_type) { 11775 case D_FREEFILE: 11776 /* 11777 * We defer adding freefile to the worklist 11778 * until all other additions have been made to 11779 * ensure that it will be done after all the 11780 * old blocks have been freed. 11781 */ 11782 if (freefile != NULL) 11783 panic("handle_bufwait: freefile"); 11784 freefile = WK_FREEFILE(wk); 11785 continue; 11786 11787 case D_MKDIR: 11788 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT); 11789 continue; 11790 11791 case D_DIRADD: 11792 diradd_inode_written(WK_DIRADD(wk), inodedep); 11793 continue; 11794 11795 case D_FREEFRAG: 11796 wk->wk_state |= COMPLETE; 11797 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE) 11798 add_to_worklist(wk, 0); 11799 continue; 11800 11801 case D_DIRREM: 11802 wk->wk_state |= COMPLETE; 11803 add_to_worklist(wk, 0); 11804 continue; 11805 11806 case D_ALLOCDIRECT: 11807 case D_ALLOCINDIR: 11808 free_newblk(WK_NEWBLK(wk)); 11809 continue; 11810 11811 case D_JNEWBLK: 11812 wk->wk_state |= COMPLETE; 11813 free_jnewblk(WK_JNEWBLK(wk)); 11814 continue; 11815 11816 /* 11817 * Save freed journal segments and add references on 11818 * the supplied list which will delay their release 11819 * until the cg bitmap is cleared on disk. 11820 */ 11821 case D_JSEGDEP: 11822 if (refhd == NULL) 11823 free_jsegdep(WK_JSEGDEP(wk)); 11824 else 11825 WORKLIST_INSERT(refhd, wk); 11826 continue; 11827 11828 case D_JADDREF: 11829 jaddref = WK_JADDREF(wk); 11830 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 11831 if_deps); 11832 /* 11833 * Transfer any jaddrefs to the list to be freed with 11834 * the bitmap if we're handling a removed file. 11835 */ 11836 if (refhd == NULL) { 11837 wk->wk_state |= COMPLETE; 11838 free_jaddref(jaddref); 11839 } else 11840 WORKLIST_INSERT(refhd, wk); 11841 continue; 11842 11843 default: 11844 panic("handle_bufwait: Unknown type %p(%s)", 11845 wk, TYPENAME(wk->wk_type)); 11846 /* NOTREACHED */ 11847 } 11848 } 11849 return (freefile); 11850 } 11851 /* 11852 * Called from within softdep_disk_write_complete above to restore 11853 * in-memory inode block contents to their most up-to-date state. Note 11854 * that this routine is always called from interrupt level with further 11855 * interrupts from this device blocked. 11856 * 11857 * If the write did not succeed, we will do all the roll-forward 11858 * operations, but we will not take the actions that will allow its 11859 * dependencies to be processed. 11860 */ 11861 static int 11862 handle_written_inodeblock(inodedep, bp, flags) 11863 struct inodedep *inodedep; 11864 struct buf *bp; /* buffer containing the inode block */ 11865 int flags; 11866 { 11867 struct freefile *freefile; 11868 struct allocdirect *adp, *nextadp; 11869 struct ufs1_dinode *dp1 = NULL; 11870 struct ufs2_dinode *dp2 = NULL; 11871 struct workhead wkhd; 11872 int hadchanges, fstype; 11873 ino_t freelink; 11874 11875 LIST_INIT(&wkhd); 11876 hadchanges = 0; 11877 freefile = NULL; 11878 if ((inodedep->id_state & IOSTARTED) == 0) 11879 panic("handle_written_inodeblock: not started"); 11880 inodedep->id_state &= ~IOSTARTED; 11881 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) { 11882 fstype = UFS1; 11883 dp1 = (struct ufs1_dinode *)bp->b_data + 11884 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11885 freelink = dp1->di_freelink; 11886 } else { 11887 fstype = UFS2; 11888 dp2 = (struct ufs2_dinode *)bp->b_data + 11889 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11890 freelink = dp2->di_freelink; 11891 } 11892 /* 11893 * Leave this inodeblock dirty until it's in the list. 11894 */ 11895 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED && 11896 (flags & WRITESUCCEEDED)) { 11897 struct inodedep *inon; 11898 11899 inon = TAILQ_NEXT(inodedep, id_unlinked); 11900 if ((inon == NULL && freelink == 0) || 11901 (inon && inon->id_ino == freelink)) { 11902 if (inon) 11903 inon->id_state |= UNLINKPREV; 11904 inodedep->id_state |= UNLINKNEXT; 11905 } 11906 hadchanges = 1; 11907 } 11908 /* 11909 * If we had to rollback the inode allocation because of 11910 * bitmaps being incomplete, then simply restore it. 11911 * Keep the block dirty so that it will not be reclaimed until 11912 * all associated dependencies have been cleared and the 11913 * corresponding updates written to disk. 11914 */ 11915 if (inodedep->id_savedino1 != NULL) { 11916 hadchanges = 1; 11917 if (fstype == UFS1) 11918 *dp1 = *inodedep->id_savedino1; 11919 else 11920 *dp2 = *inodedep->id_savedino2; 11921 free(inodedep->id_savedino1, M_SAVEDINO); 11922 inodedep->id_savedino1 = NULL; 11923 if ((bp->b_flags & B_DELWRI) == 0) 11924 stat_inode_bitmap++; 11925 bdirty(bp); 11926 /* 11927 * If the inode is clear here and GOINGAWAY it will never 11928 * be written. Process the bufwait and clear any pending 11929 * work which may include the freefile. 11930 */ 11931 if (inodedep->id_state & GOINGAWAY) 11932 goto bufwait; 11933 return (1); 11934 } 11935 if (flags & WRITESUCCEEDED) 11936 inodedep->id_state |= COMPLETE; 11937 /* 11938 * Roll forward anything that had to be rolled back before 11939 * the inode could be updated. 11940 */ 11941 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) { 11942 nextadp = TAILQ_NEXT(adp, ad_next); 11943 if (adp->ad_state & ATTACHED) 11944 panic("handle_written_inodeblock: new entry"); 11945 if (fstype == UFS1) { 11946 if (adp->ad_offset < UFS_NDADDR) { 11947 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11948 panic("%s %s #%jd mismatch %d != %jd", 11949 "handle_written_inodeblock:", 11950 "direct pointer", 11951 (intmax_t)adp->ad_offset, 11952 dp1->di_db[adp->ad_offset], 11953 (intmax_t)adp->ad_oldblkno); 11954 dp1->di_db[adp->ad_offset] = adp->ad_newblkno; 11955 } else { 11956 if (dp1->di_ib[adp->ad_offset - UFS_NDADDR] != 11957 0) 11958 panic("%s: %s #%jd allocated as %d", 11959 "handle_written_inodeblock", 11960 "indirect pointer", 11961 (intmax_t)adp->ad_offset - 11962 UFS_NDADDR, 11963 dp1->di_ib[adp->ad_offset - 11964 UFS_NDADDR]); 11965 dp1->di_ib[adp->ad_offset - UFS_NDADDR] = 11966 adp->ad_newblkno; 11967 } 11968 } else { 11969 if (adp->ad_offset < UFS_NDADDR) { 11970 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11971 panic("%s: %s #%jd %s %jd != %jd", 11972 "handle_written_inodeblock", 11973 "direct pointer", 11974 (intmax_t)adp->ad_offset, "mismatch", 11975 (intmax_t)dp2->di_db[adp->ad_offset], 11976 (intmax_t)adp->ad_oldblkno); 11977 dp2->di_db[adp->ad_offset] = adp->ad_newblkno; 11978 } else { 11979 if (dp2->di_ib[adp->ad_offset - UFS_NDADDR] != 11980 0) 11981 panic("%s: %s #%jd allocated as %jd", 11982 "handle_written_inodeblock", 11983 "indirect pointer", 11984 (intmax_t)adp->ad_offset - 11985 UFS_NDADDR, 11986 (intmax_t) 11987 dp2->di_ib[adp->ad_offset - 11988 UFS_NDADDR]); 11989 dp2->di_ib[adp->ad_offset - UFS_NDADDR] = 11990 adp->ad_newblkno; 11991 } 11992 } 11993 adp->ad_state &= ~UNDONE; 11994 adp->ad_state |= ATTACHED; 11995 hadchanges = 1; 11996 } 11997 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) { 11998 nextadp = TAILQ_NEXT(adp, ad_next); 11999 if (adp->ad_state & ATTACHED) 12000 panic("handle_written_inodeblock: new entry"); 12001 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno) 12002 panic("%s: direct pointers #%jd %s %jd != %jd", 12003 "handle_written_inodeblock", 12004 (intmax_t)adp->ad_offset, "mismatch", 12005 (intmax_t)dp2->di_extb[adp->ad_offset], 12006 (intmax_t)adp->ad_oldblkno); 12007 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno; 12008 adp->ad_state &= ~UNDONE; 12009 adp->ad_state |= ATTACHED; 12010 hadchanges = 1; 12011 } 12012 if (hadchanges && (bp->b_flags & B_DELWRI) == 0) 12013 stat_direct_blk_ptrs++; 12014 /* 12015 * Reset the file size to its most up-to-date value. 12016 */ 12017 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1) 12018 panic("handle_written_inodeblock: bad size"); 12019 if (inodedep->id_savednlink > UFS_LINK_MAX) 12020 panic("handle_written_inodeblock: Invalid link count " 12021 "%jd for inodedep %p", (uintmax_t)inodedep->id_savednlink, 12022 inodedep); 12023 if (fstype == UFS1) { 12024 if (dp1->di_nlink != inodedep->id_savednlink) { 12025 dp1->di_nlink = inodedep->id_savednlink; 12026 hadchanges = 1; 12027 } 12028 if (dp1->di_size != inodedep->id_savedsize) { 12029 dp1->di_size = inodedep->id_savedsize; 12030 hadchanges = 1; 12031 } 12032 } else { 12033 if (dp2->di_nlink != inodedep->id_savednlink) { 12034 dp2->di_nlink = inodedep->id_savednlink; 12035 hadchanges = 1; 12036 } 12037 if (dp2->di_size != inodedep->id_savedsize) { 12038 dp2->di_size = inodedep->id_savedsize; 12039 hadchanges = 1; 12040 } 12041 if (dp2->di_extsize != inodedep->id_savedextsize) { 12042 dp2->di_extsize = inodedep->id_savedextsize; 12043 hadchanges = 1; 12044 } 12045 } 12046 inodedep->id_savedsize = -1; 12047 inodedep->id_savedextsize = -1; 12048 inodedep->id_savednlink = -1; 12049 /* 12050 * If there were any rollbacks in the inode block, then it must be 12051 * marked dirty so that its will eventually get written back in 12052 * its correct form. 12053 */ 12054 if (hadchanges) { 12055 if (fstype == UFS2) 12056 ffs_update_dinode_ckhash(inodedep->id_fs, dp2); 12057 bdirty(bp); 12058 } 12059 bufwait: 12060 /* 12061 * If the write did not succeed, we have done all the roll-forward 12062 * operations, but we cannot take the actions that will allow its 12063 * dependencies to be processed. 12064 */ 12065 if ((flags & WRITESUCCEEDED) == 0) 12066 return (hadchanges); 12067 /* 12068 * Process any allocdirects that completed during the update. 12069 */ 12070 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 12071 handle_allocdirect_partdone(adp, &wkhd); 12072 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 12073 handle_allocdirect_partdone(adp, &wkhd); 12074 /* 12075 * Process deallocations that were held pending until the 12076 * inode had been written to disk. Freeing of the inode 12077 * is delayed until after all blocks have been freed to 12078 * avoid creation of new <vfsid, inum, lbn> triples 12079 * before the old ones have been deleted. Completely 12080 * unlinked inodes are not processed until the unlinked 12081 * inode list is written or the last reference is removed. 12082 */ 12083 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) { 12084 freefile = handle_bufwait(inodedep, NULL); 12085 if (freefile && !LIST_EMPTY(&wkhd)) { 12086 WORKLIST_INSERT(&wkhd, &freefile->fx_list); 12087 freefile = NULL; 12088 } 12089 } 12090 /* 12091 * Move rolled forward dependency completions to the bufwait list 12092 * now that those that were already written have been processed. 12093 */ 12094 if (!LIST_EMPTY(&wkhd) && hadchanges == 0) 12095 panic("handle_written_inodeblock: bufwait but no changes"); 12096 jwork_move(&inodedep->id_bufwait, &wkhd); 12097 12098 if (freefile != NULL) { 12099 /* 12100 * If the inode is goingaway it was never written. Fake up 12101 * the state here so free_inodedep() can succeed. 12102 */ 12103 if (inodedep->id_state & GOINGAWAY) 12104 inodedep->id_state |= COMPLETE | DEPCOMPLETE; 12105 if (free_inodedep(inodedep) == 0) 12106 panic("handle_written_inodeblock: live inodedep %p", 12107 inodedep); 12108 add_to_worklist(&freefile->fx_list, 0); 12109 return (0); 12110 } 12111 12112 /* 12113 * If no outstanding dependencies, free it. 12114 */ 12115 if (free_inodedep(inodedep) || 12116 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 && 12117 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 && 12118 TAILQ_FIRST(&inodedep->id_extupdt) == 0 && 12119 LIST_FIRST(&inodedep->id_bufwait) == 0)) 12120 return (0); 12121 return (hadchanges); 12122 } 12123 12124 /* 12125 * Perform needed roll-forwards and kick off any dependencies that 12126 * can now be processed. 12127 * 12128 * If the write did not succeed, we will do all the roll-forward 12129 * operations, but we will not take the actions that will allow its 12130 * dependencies to be processed. 12131 */ 12132 static int 12133 handle_written_indirdep(indirdep, bp, bpp, flags) 12134 struct indirdep *indirdep; 12135 struct buf *bp; 12136 struct buf **bpp; 12137 int flags; 12138 { 12139 struct allocindir *aip; 12140 struct buf *sbp; 12141 int chgs; 12142 12143 if (indirdep->ir_state & GOINGAWAY) 12144 panic("handle_written_indirdep: indirdep gone"); 12145 if ((indirdep->ir_state & IOSTARTED) == 0) 12146 panic("handle_written_indirdep: IO not started"); 12147 chgs = 0; 12148 /* 12149 * If there were rollbacks revert them here. 12150 */ 12151 if (indirdep->ir_saveddata) { 12152 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount); 12153 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 12154 free(indirdep->ir_saveddata, M_INDIRDEP); 12155 indirdep->ir_saveddata = NULL; 12156 } 12157 chgs = 1; 12158 } 12159 indirdep->ir_state &= ~(UNDONE | IOSTARTED); 12160 indirdep->ir_state |= ATTACHED; 12161 /* 12162 * If the write did not succeed, we have done all the roll-forward 12163 * operations, but we cannot take the actions that will allow its 12164 * dependencies to be processed. 12165 */ 12166 if ((flags & WRITESUCCEEDED) == 0) { 12167 stat_indir_blk_ptrs++; 12168 bdirty(bp); 12169 return (1); 12170 } 12171 /* 12172 * Move allocindirs with written pointers to the completehd if 12173 * the indirdep's pointer is not yet written. Otherwise 12174 * free them here. 12175 */ 12176 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) { 12177 LIST_REMOVE(aip, ai_next); 12178 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 12179 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip, 12180 ai_next); 12181 newblk_freefrag(&aip->ai_block); 12182 continue; 12183 } 12184 free_newblk(&aip->ai_block); 12185 } 12186 /* 12187 * Move allocindirs that have finished dependency processing from 12188 * the done list to the write list after updating the pointers. 12189 */ 12190 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 12191 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) { 12192 handle_allocindir_partdone(aip); 12193 if (aip == LIST_FIRST(&indirdep->ir_donehd)) 12194 panic("disk_write_complete: not gone"); 12195 chgs = 1; 12196 } 12197 } 12198 /* 12199 * Preserve the indirdep if there were any changes or if it is not 12200 * yet valid on disk. 12201 */ 12202 if (chgs) { 12203 stat_indir_blk_ptrs++; 12204 bdirty(bp); 12205 return (1); 12206 } 12207 /* 12208 * If there were no changes we can discard the savedbp and detach 12209 * ourselves from the buf. We are only carrying completed pointers 12210 * in this case. 12211 */ 12212 sbp = indirdep->ir_savebp; 12213 sbp->b_flags |= B_INVAL | B_NOCACHE; 12214 indirdep->ir_savebp = NULL; 12215 indirdep->ir_bp = NULL; 12216 if (*bpp != NULL) 12217 panic("handle_written_indirdep: bp already exists."); 12218 *bpp = sbp; 12219 /* 12220 * The indirdep may not be freed until its parent points at it. 12221 */ 12222 if (indirdep->ir_state & DEPCOMPLETE) 12223 free_indirdep(indirdep); 12224 12225 return (0); 12226 } 12227 12228 /* 12229 * Process a diradd entry after its dependent inode has been written. 12230 */ 12231 static void 12232 diradd_inode_written(dap, inodedep) 12233 struct diradd *dap; 12234 struct inodedep *inodedep; 12235 { 12236 12237 LOCK_OWNED(VFSTOUFS(dap->da_list.wk_mp)); 12238 dap->da_state |= COMPLETE; 12239 complete_diradd(dap); 12240 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 12241 } 12242 12243 /* 12244 * Returns true if the bmsafemap will have rollbacks when written. Must only 12245 * be called with the per-filesystem lock and the buf lock on the cg held. 12246 */ 12247 static int 12248 bmsafemap_backgroundwrite(bmsafemap, bp) 12249 struct bmsafemap *bmsafemap; 12250 struct buf *bp; 12251 { 12252 int dirty; 12253 12254 LOCK_OWNED(VFSTOUFS(bmsafemap->sm_list.wk_mp)); 12255 dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) | 12256 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd); 12257 /* 12258 * If we're initiating a background write we need to process the 12259 * rollbacks as they exist now, not as they exist when IO starts. 12260 * No other consumers will look at the contents of the shadowed 12261 * buf so this is safe to do here. 12262 */ 12263 if (bp->b_xflags & BX_BKGRDMARKER) 12264 initiate_write_bmsafemap(bmsafemap, bp); 12265 12266 return (dirty); 12267 } 12268 12269 /* 12270 * Re-apply an allocation when a cg write is complete. 12271 */ 12272 static int 12273 jnewblk_rollforward(jnewblk, fs, cgp, blksfree) 12274 struct jnewblk *jnewblk; 12275 struct fs *fs; 12276 struct cg *cgp; 12277 uint8_t *blksfree; 12278 { 12279 ufs1_daddr_t fragno; 12280 ufs2_daddr_t blkno; 12281 long cgbno, bbase; 12282 int frags, blk; 12283 int i; 12284 12285 frags = 0; 12286 cgbno = dtogd(fs, jnewblk->jn_blkno); 12287 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) { 12288 if (isclr(blksfree, cgbno + i)) 12289 panic("jnewblk_rollforward: re-allocated fragment"); 12290 frags++; 12291 } 12292 if (frags == fs->fs_frag) { 12293 blkno = fragstoblks(fs, cgbno); 12294 ffs_clrblock(fs, blksfree, (long)blkno); 12295 ffs_clusteracct(fs, cgp, blkno, -1); 12296 cgp->cg_cs.cs_nbfree--; 12297 } else { 12298 bbase = cgbno - fragnum(fs, cgbno); 12299 cgbno += jnewblk->jn_oldfrags; 12300 /* If a complete block had been reassembled, account for it. */ 12301 fragno = fragstoblks(fs, bbase); 12302 if (ffs_isblock(fs, blksfree, fragno)) { 12303 cgp->cg_cs.cs_nffree += fs->fs_frag; 12304 ffs_clusteracct(fs, cgp, fragno, -1); 12305 cgp->cg_cs.cs_nbfree--; 12306 } 12307 /* Decrement the old frags. */ 12308 blk = blkmap(fs, blksfree, bbase); 12309 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 12310 /* Allocate the fragment */ 12311 for (i = 0; i < frags; i++) 12312 clrbit(blksfree, cgbno + i); 12313 cgp->cg_cs.cs_nffree -= frags; 12314 /* Add back in counts associated with the new frags */ 12315 blk = blkmap(fs, blksfree, bbase); 12316 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 12317 } 12318 return (frags); 12319 } 12320 12321 /* 12322 * Complete a write to a bmsafemap structure. Roll forward any bitmap 12323 * changes if it's not a background write. Set all written dependencies 12324 * to DEPCOMPLETE and free the structure if possible. 12325 * 12326 * If the write did not succeed, we will do all the roll-forward 12327 * operations, but we will not take the actions that will allow its 12328 * dependencies to be processed. 12329 */ 12330 static int 12331 handle_written_bmsafemap(bmsafemap, bp, flags) 12332 struct bmsafemap *bmsafemap; 12333 struct buf *bp; 12334 int flags; 12335 { 12336 struct newblk *newblk; 12337 struct inodedep *inodedep; 12338 struct jaddref *jaddref, *jatmp; 12339 struct jnewblk *jnewblk, *jntmp; 12340 struct ufsmount *ump; 12341 uint8_t *inosused; 12342 uint8_t *blksfree; 12343 struct cg *cgp; 12344 struct fs *fs; 12345 ino_t ino; 12346 int foreground; 12347 int chgs; 12348 12349 if ((bmsafemap->sm_state & IOSTARTED) == 0) 12350 panic("handle_written_bmsafemap: Not started\n"); 12351 ump = VFSTOUFS(bmsafemap->sm_list.wk_mp); 12352 chgs = 0; 12353 bmsafemap->sm_state &= ~IOSTARTED; 12354 foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0; 12355 /* 12356 * If write was successful, release journal work that was waiting 12357 * on the write. Otherwise move the work back. 12358 */ 12359 if (flags & WRITESUCCEEDED) 12360 handle_jwork(&bmsafemap->sm_freewr); 12361 else 12362 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, 12363 worklist, wk_list); 12364 12365 /* 12366 * Restore unwritten inode allocation pending jaddref writes. 12367 */ 12368 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) { 12369 cgp = (struct cg *)bp->b_data; 12370 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 12371 inosused = cg_inosused(cgp); 12372 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd, 12373 ja_bmdeps, jatmp) { 12374 if ((jaddref->ja_state & UNDONE) == 0) 12375 continue; 12376 ino = jaddref->ja_ino % fs->fs_ipg; 12377 if (isset(inosused, ino)) 12378 panic("handle_written_bmsafemap: " 12379 "re-allocated inode"); 12380 /* Do the roll-forward only if it's a real copy. */ 12381 if (foreground) { 12382 if ((jaddref->ja_mode & IFMT) == IFDIR) 12383 cgp->cg_cs.cs_ndir++; 12384 cgp->cg_cs.cs_nifree--; 12385 setbit(inosused, ino); 12386 chgs = 1; 12387 } 12388 jaddref->ja_state &= ~UNDONE; 12389 jaddref->ja_state |= ATTACHED; 12390 free_jaddref(jaddref); 12391 } 12392 } 12393 /* 12394 * Restore any block allocations which are pending journal writes. 12395 */ 12396 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 12397 cgp = (struct cg *)bp->b_data; 12398 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 12399 blksfree = cg_blksfree(cgp); 12400 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps, 12401 jntmp) { 12402 if ((jnewblk->jn_state & UNDONE) == 0) 12403 continue; 12404 /* Do the roll-forward only if it's a real copy. */ 12405 if (foreground && 12406 jnewblk_rollforward(jnewblk, fs, cgp, blksfree)) 12407 chgs = 1; 12408 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK); 12409 jnewblk->jn_state |= ATTACHED; 12410 free_jnewblk(jnewblk); 12411 } 12412 } 12413 /* 12414 * If the write did not succeed, we have done all the roll-forward 12415 * operations, but we cannot take the actions that will allow its 12416 * dependencies to be processed. 12417 */ 12418 if ((flags & WRITESUCCEEDED) == 0) { 12419 LIST_CONCAT(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 12420 newblk, nb_deps); 12421 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, 12422 worklist, wk_list); 12423 if (foreground) 12424 bdirty(bp); 12425 return (1); 12426 } 12427 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) { 12428 newblk->nb_state |= DEPCOMPLETE; 12429 newblk->nb_state &= ~ONDEPLIST; 12430 newblk->nb_bmsafemap = NULL; 12431 LIST_REMOVE(newblk, nb_deps); 12432 if (newblk->nb_list.wk_type == D_ALLOCDIRECT) 12433 handle_allocdirect_partdone( 12434 WK_ALLOCDIRECT(&newblk->nb_list), NULL); 12435 else if (newblk->nb_list.wk_type == D_ALLOCINDIR) 12436 handle_allocindir_partdone( 12437 WK_ALLOCINDIR(&newblk->nb_list)); 12438 else if (newblk->nb_list.wk_type != D_NEWBLK) 12439 panic("handle_written_bmsafemap: Unexpected type: %s", 12440 TYPENAME(newblk->nb_list.wk_type)); 12441 } 12442 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) { 12443 inodedep->id_state |= DEPCOMPLETE; 12444 inodedep->id_state &= ~ONDEPLIST; 12445 LIST_REMOVE(inodedep, id_deps); 12446 inodedep->id_bmsafemap = NULL; 12447 } 12448 LIST_REMOVE(bmsafemap, sm_next); 12449 if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) && 12450 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) && 12451 LIST_EMPTY(&bmsafemap->sm_newblkhd) && 12452 LIST_EMPTY(&bmsafemap->sm_inodedephd) && 12453 LIST_EMPTY(&bmsafemap->sm_freehd)) { 12454 LIST_REMOVE(bmsafemap, sm_hash); 12455 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 12456 return (0); 12457 } 12458 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 12459 if (foreground) 12460 bdirty(bp); 12461 return (1); 12462 } 12463 12464 /* 12465 * Try to free a mkdir dependency. 12466 */ 12467 static void 12468 complete_mkdir(mkdir) 12469 struct mkdir *mkdir; 12470 { 12471 struct diradd *dap; 12472 12473 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE) 12474 return; 12475 LIST_REMOVE(mkdir, md_mkdirs); 12476 dap = mkdir->md_diradd; 12477 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 12478 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) { 12479 dap->da_state |= DEPCOMPLETE; 12480 complete_diradd(dap); 12481 } 12482 WORKITEM_FREE(mkdir, D_MKDIR); 12483 } 12484 12485 /* 12486 * Handle the completion of a mkdir dependency. 12487 */ 12488 static void 12489 handle_written_mkdir(mkdir, type) 12490 struct mkdir *mkdir; 12491 int type; 12492 { 12493 12494 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type) 12495 panic("handle_written_mkdir: bad type"); 12496 mkdir->md_state |= COMPLETE; 12497 complete_mkdir(mkdir); 12498 } 12499 12500 static int 12501 free_pagedep(pagedep) 12502 struct pagedep *pagedep; 12503 { 12504 int i; 12505 12506 if (pagedep->pd_state & NEWBLOCK) 12507 return (0); 12508 if (!LIST_EMPTY(&pagedep->pd_dirremhd)) 12509 return (0); 12510 for (i = 0; i < DAHASHSZ; i++) 12511 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i])) 12512 return (0); 12513 if (!LIST_EMPTY(&pagedep->pd_pendinghd)) 12514 return (0); 12515 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd)) 12516 return (0); 12517 if (pagedep->pd_state & ONWORKLIST) 12518 WORKLIST_REMOVE(&pagedep->pd_list); 12519 LIST_REMOVE(pagedep, pd_hash); 12520 WORKITEM_FREE(pagedep, D_PAGEDEP); 12521 12522 return (1); 12523 } 12524 12525 /* 12526 * Called from within softdep_disk_write_complete above. 12527 * A write operation was just completed. Removed inodes can 12528 * now be freed and associated block pointers may be committed. 12529 * Note that this routine is always called from interrupt level 12530 * with further interrupts from this device blocked. 12531 * 12532 * If the write did not succeed, we will do all the roll-forward 12533 * operations, but we will not take the actions that will allow its 12534 * dependencies to be processed. 12535 */ 12536 static int 12537 handle_written_filepage(pagedep, bp, flags) 12538 struct pagedep *pagedep; 12539 struct buf *bp; /* buffer containing the written page */ 12540 int flags; 12541 { 12542 struct dirrem *dirrem; 12543 struct diradd *dap, *nextdap; 12544 struct direct *ep; 12545 int i, chgs; 12546 12547 if ((pagedep->pd_state & IOSTARTED) == 0) 12548 panic("handle_written_filepage: not started"); 12549 pagedep->pd_state &= ~IOSTARTED; 12550 if ((flags & WRITESUCCEEDED) == 0) 12551 goto rollforward; 12552 /* 12553 * Process any directory removals that have been committed. 12554 */ 12555 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) { 12556 LIST_REMOVE(dirrem, dm_next); 12557 dirrem->dm_state |= COMPLETE; 12558 dirrem->dm_dirinum = pagedep->pd_ino; 12559 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 12560 ("handle_written_filepage: Journal entries not written.")); 12561 add_to_worklist(&dirrem->dm_list, 0); 12562 } 12563 /* 12564 * Free any directory additions that have been committed. 12565 * If it is a newly allocated block, we have to wait until 12566 * the on-disk directory inode claims the new block. 12567 */ 12568 if ((pagedep->pd_state & NEWBLOCK) == 0) 12569 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 12570 free_diradd(dap, NULL); 12571 rollforward: 12572 /* 12573 * Uncommitted directory entries must be restored. 12574 */ 12575 for (chgs = 0, i = 0; i < DAHASHSZ; i++) { 12576 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap; 12577 dap = nextdap) { 12578 nextdap = LIST_NEXT(dap, da_pdlist); 12579 if (dap->da_state & ATTACHED) 12580 panic("handle_written_filepage: attached"); 12581 ep = (struct direct *) 12582 ((char *)bp->b_data + dap->da_offset); 12583 ep->d_ino = dap->da_newinum; 12584 dap->da_state &= ~UNDONE; 12585 dap->da_state |= ATTACHED; 12586 chgs = 1; 12587 /* 12588 * If the inode referenced by the directory has 12589 * been written out, then the dependency can be 12590 * moved to the pending list. 12591 */ 12592 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 12593 LIST_REMOVE(dap, da_pdlist); 12594 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, 12595 da_pdlist); 12596 } 12597 } 12598 } 12599 /* 12600 * If there were any rollbacks in the directory, then it must be 12601 * marked dirty so that its will eventually get written back in 12602 * its correct form. 12603 */ 12604 if (chgs || (flags & WRITESUCCEEDED) == 0) { 12605 if ((bp->b_flags & B_DELWRI) == 0) 12606 stat_dir_entry++; 12607 bdirty(bp); 12608 return (1); 12609 } 12610 /* 12611 * If we are not waiting for a new directory block to be 12612 * claimed by its inode, then the pagedep will be freed. 12613 * Otherwise it will remain to track any new entries on 12614 * the page in case they are fsync'ed. 12615 */ 12616 free_pagedep(pagedep); 12617 return (0); 12618 } 12619 12620 /* 12621 * Writing back in-core inode structures. 12622 * 12623 * The filesystem only accesses an inode's contents when it occupies an 12624 * "in-core" inode structure. These "in-core" structures are separate from 12625 * the page frames used to cache inode blocks. Only the latter are 12626 * transferred to/from the disk. So, when the updated contents of the 12627 * "in-core" inode structure are copied to the corresponding in-memory inode 12628 * block, the dependencies are also transferred. The following procedure is 12629 * called when copying a dirty "in-core" inode to a cached inode block. 12630 */ 12631 12632 /* 12633 * Called when an inode is loaded from disk. If the effective link count 12634 * differed from the actual link count when it was last flushed, then we 12635 * need to ensure that the correct effective link count is put back. 12636 */ 12637 void 12638 softdep_load_inodeblock(ip) 12639 struct inode *ip; /* the "in_core" copy of the inode */ 12640 { 12641 struct inodedep *inodedep; 12642 struct ufsmount *ump; 12643 12644 ump = ITOUMP(ip); 12645 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 12646 ("softdep_load_inodeblock called on non-softdep filesystem")); 12647 /* 12648 * Check for alternate nlink count. 12649 */ 12650 ip->i_effnlink = ip->i_nlink; 12651 ACQUIRE_LOCK(ump); 12652 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) { 12653 FREE_LOCK(ump); 12654 return; 12655 } 12656 if (ip->i_nlink != inodedep->id_nlinkwrote && 12657 inodedep->id_nlinkwrote != -1) { 12658 KASSERT(ip->i_nlink == 0 && 12659 (ump->um_flags & UM_FSFAIL_CLEANUP) != 0, 12660 ("read bad i_nlink value")); 12661 ip->i_effnlink = ip->i_nlink = inodedep->id_nlinkwrote; 12662 } 12663 ip->i_effnlink -= inodedep->id_nlinkdelta; 12664 KASSERT(ip->i_effnlink >= 0, 12665 ("softdep_load_inodeblock: negative i_effnlink")); 12666 FREE_LOCK(ump); 12667 } 12668 12669 /* 12670 * This routine is called just before the "in-core" inode 12671 * information is to be copied to the in-memory inode block. 12672 * Recall that an inode block contains several inodes. If 12673 * the force flag is set, then the dependencies will be 12674 * cleared so that the update can always be made. Note that 12675 * the buffer is locked when this routine is called, so we 12676 * will never be in the middle of writing the inode block 12677 * to disk. 12678 */ 12679 void 12680 softdep_update_inodeblock(ip, bp, waitfor) 12681 struct inode *ip; /* the "in_core" copy of the inode */ 12682 struct buf *bp; /* the buffer containing the inode block */ 12683 int waitfor; /* nonzero => update must be allowed */ 12684 { 12685 struct inodedep *inodedep; 12686 struct inoref *inoref; 12687 struct ufsmount *ump; 12688 struct worklist *wk; 12689 struct mount *mp; 12690 struct buf *ibp; 12691 struct fs *fs; 12692 int error; 12693 12694 ump = ITOUMP(ip); 12695 mp = UFSTOVFS(ump); 12696 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 12697 ("softdep_update_inodeblock called on non-softdep filesystem")); 12698 fs = ump->um_fs; 12699 /* 12700 * Preserve the freelink that is on disk. clear_unlinked_inodedep() 12701 * does not have access to the in-core ip so must write directly into 12702 * the inode block buffer when setting freelink. 12703 */ 12704 if (fs->fs_magic == FS_UFS1_MAGIC) 12705 DIP_SET(ip, i_freelink, ((struct ufs1_dinode *)bp->b_data + 12706 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12707 else 12708 DIP_SET(ip, i_freelink, ((struct ufs2_dinode *)bp->b_data + 12709 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12710 /* 12711 * If the effective link count is not equal to the actual link 12712 * count, then we must track the difference in an inodedep while 12713 * the inode is (potentially) tossed out of the cache. Otherwise, 12714 * if there is no existing inodedep, then there are no dependencies 12715 * to track. 12716 */ 12717 ACQUIRE_LOCK(ump); 12718 again: 12719 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12720 FREE_LOCK(ump); 12721 if (ip->i_effnlink != ip->i_nlink) 12722 panic("softdep_update_inodeblock: bad link count"); 12723 return; 12724 } 12725 KASSERT(ip->i_nlink >= inodedep->id_nlinkdelta, 12726 ("softdep_update_inodeblock inconsistent ip %p i_nlink %d " 12727 "inodedep %p id_nlinkdelta %jd", 12728 ip, ip->i_nlink, inodedep, (intmax_t)inodedep->id_nlinkdelta)); 12729 inodedep->id_nlinkwrote = ip->i_nlink; 12730 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink) 12731 panic("softdep_update_inodeblock: bad delta"); 12732 /* 12733 * If we're flushing all dependencies we must also move any waiting 12734 * for journal writes onto the bufwait list prior to I/O. 12735 */ 12736 if (waitfor) { 12737 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12738 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12739 == DEPCOMPLETE) { 12740 jwait(&inoref->if_list, MNT_WAIT); 12741 goto again; 12742 } 12743 } 12744 } 12745 /* 12746 * Changes have been initiated. Anything depending on these 12747 * changes cannot occur until this inode has been written. 12748 */ 12749 inodedep->id_state &= ~COMPLETE; 12750 if ((inodedep->id_state & ONWORKLIST) == 0) 12751 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list); 12752 /* 12753 * Any new dependencies associated with the incore inode must 12754 * now be moved to the list associated with the buffer holding 12755 * the in-memory copy of the inode. Once merged process any 12756 * allocdirects that are completed by the merger. 12757 */ 12758 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt); 12759 if (!TAILQ_EMPTY(&inodedep->id_inoupdt)) 12760 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt), 12761 NULL); 12762 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt); 12763 if (!TAILQ_EMPTY(&inodedep->id_extupdt)) 12764 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt), 12765 NULL); 12766 /* 12767 * Now that the inode has been pushed into the buffer, the 12768 * operations dependent on the inode being written to disk 12769 * can be moved to the id_bufwait so that they will be 12770 * processed when the buffer I/O completes. 12771 */ 12772 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) { 12773 WORKLIST_REMOVE(wk); 12774 WORKLIST_INSERT(&inodedep->id_bufwait, wk); 12775 } 12776 /* 12777 * Newly allocated inodes cannot be written until the bitmap 12778 * that allocates them have been written (indicated by 12779 * DEPCOMPLETE being set in id_state). If we are doing a 12780 * forced sync (e.g., an fsync on a file), we force the bitmap 12781 * to be written so that the update can be done. 12782 */ 12783 if (waitfor == 0) { 12784 FREE_LOCK(ump); 12785 return; 12786 } 12787 retry: 12788 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) { 12789 FREE_LOCK(ump); 12790 return; 12791 } 12792 ibp = inodedep->id_bmsafemap->sm_buf; 12793 ibp = getdirtybuf(ibp, LOCK_PTR(ump), MNT_WAIT); 12794 if (ibp == NULL) { 12795 /* 12796 * If ibp came back as NULL, the dependency could have been 12797 * freed while we slept. Look it up again, and check to see 12798 * that it has completed. 12799 */ 12800 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 12801 goto retry; 12802 FREE_LOCK(ump); 12803 return; 12804 } 12805 FREE_LOCK(ump); 12806 if ((error = bwrite(ibp)) != 0) 12807 softdep_error("softdep_update_inodeblock: bwrite", error); 12808 } 12809 12810 /* 12811 * Merge the a new inode dependency list (such as id_newinoupdt) into an 12812 * old inode dependency list (such as id_inoupdt). 12813 */ 12814 static void 12815 merge_inode_lists(newlisthead, oldlisthead) 12816 struct allocdirectlst *newlisthead; 12817 struct allocdirectlst *oldlisthead; 12818 { 12819 struct allocdirect *listadp, *newadp; 12820 12821 newadp = TAILQ_FIRST(newlisthead); 12822 if (newadp != NULL) 12823 LOCK_OWNED(VFSTOUFS(newadp->ad_block.nb_list.wk_mp)); 12824 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) { 12825 if (listadp->ad_offset < newadp->ad_offset) { 12826 listadp = TAILQ_NEXT(listadp, ad_next); 12827 continue; 12828 } 12829 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12830 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next); 12831 if (listadp->ad_offset == newadp->ad_offset) { 12832 allocdirect_merge(oldlisthead, newadp, 12833 listadp); 12834 listadp = newadp; 12835 } 12836 newadp = TAILQ_FIRST(newlisthead); 12837 } 12838 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) { 12839 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12840 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next); 12841 } 12842 } 12843 12844 /* 12845 * If we are doing an fsync, then we must ensure that any directory 12846 * entries for the inode have been written after the inode gets to disk. 12847 */ 12848 int 12849 softdep_fsync(vp) 12850 struct vnode *vp; /* the "in_core" copy of the inode */ 12851 { 12852 struct inodedep *inodedep; 12853 struct pagedep *pagedep; 12854 struct inoref *inoref; 12855 struct ufsmount *ump; 12856 struct worklist *wk; 12857 struct diradd *dap; 12858 struct mount *mp; 12859 struct vnode *pvp; 12860 struct inode *ip; 12861 struct buf *bp; 12862 struct fs *fs; 12863 struct thread *td = curthread; 12864 int error, flushparent, pagedep_new_block; 12865 ino_t parentino; 12866 ufs_lbn_t lbn; 12867 12868 ip = VTOI(vp); 12869 mp = vp->v_mount; 12870 ump = VFSTOUFS(mp); 12871 fs = ump->um_fs; 12872 if (MOUNTEDSOFTDEP(mp) == 0) 12873 return (0); 12874 ACQUIRE_LOCK(ump); 12875 restart: 12876 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12877 FREE_LOCK(ump); 12878 return (0); 12879 } 12880 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12881 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12882 == DEPCOMPLETE) { 12883 jwait(&inoref->if_list, MNT_WAIT); 12884 goto restart; 12885 } 12886 } 12887 if (!LIST_EMPTY(&inodedep->id_inowait) || 12888 !TAILQ_EMPTY(&inodedep->id_extupdt) || 12889 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 12890 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 12891 !TAILQ_EMPTY(&inodedep->id_newinoupdt)) 12892 panic("softdep_fsync: pending ops %p", inodedep); 12893 for (error = 0, flushparent = 0; ; ) { 12894 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL) 12895 break; 12896 if (wk->wk_type != D_DIRADD) 12897 panic("softdep_fsync: Unexpected type %s", 12898 TYPENAME(wk->wk_type)); 12899 dap = WK_DIRADD(wk); 12900 /* 12901 * Flush our parent if this directory entry has a MKDIR_PARENT 12902 * dependency or is contained in a newly allocated block. 12903 */ 12904 if (dap->da_state & DIRCHG) 12905 pagedep = dap->da_previous->dm_pagedep; 12906 else 12907 pagedep = dap->da_pagedep; 12908 parentino = pagedep->pd_ino; 12909 lbn = pagedep->pd_lbn; 12910 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) 12911 panic("softdep_fsync: dirty"); 12912 if ((dap->da_state & MKDIR_PARENT) || 12913 (pagedep->pd_state & NEWBLOCK)) 12914 flushparent = 1; 12915 else 12916 flushparent = 0; 12917 /* 12918 * If we are being fsync'ed as part of vgone'ing this vnode, 12919 * then we will not be able to release and recover the 12920 * vnode below, so we just have to give up on writing its 12921 * directory entry out. It will eventually be written, just 12922 * not now, but then the user was not asking to have it 12923 * written, so we are not breaking any promises. 12924 */ 12925 if (VN_IS_DOOMED(vp)) 12926 break; 12927 /* 12928 * We prevent deadlock by always fetching inodes from the 12929 * root, moving down the directory tree. Thus, when fetching 12930 * our parent directory, we first try to get the lock. If 12931 * that fails, we must unlock ourselves before requesting 12932 * the lock on our parent. See the comment in ufs_lookup 12933 * for details on possible races. 12934 */ 12935 FREE_LOCK(ump); 12936 error = get_parent_vp(vp, mp, parentino, NULL, NULL, NULL, 12937 &pvp); 12938 if (error == ERELOOKUP) 12939 error = 0; 12940 if (error != 0) 12941 return (error); 12942 /* 12943 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps 12944 * that are contained in direct blocks will be resolved by 12945 * doing a ffs_update. Pagedeps contained in indirect blocks 12946 * may require a complete sync'ing of the directory. So, we 12947 * try the cheap and fast ffs_update first, and if that fails, 12948 * then we do the slower ffs_syncvnode of the directory. 12949 */ 12950 if (flushparent) { 12951 int locked; 12952 12953 if ((error = ffs_update(pvp, 1)) != 0) { 12954 vput(pvp); 12955 return (error); 12956 } 12957 ACQUIRE_LOCK(ump); 12958 locked = 1; 12959 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) { 12960 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) { 12961 if (wk->wk_type != D_DIRADD) 12962 panic("softdep_fsync: Unexpected type %s", 12963 TYPENAME(wk->wk_type)); 12964 dap = WK_DIRADD(wk); 12965 if (dap->da_state & DIRCHG) 12966 pagedep = dap->da_previous->dm_pagedep; 12967 else 12968 pagedep = dap->da_pagedep; 12969 pagedep_new_block = pagedep->pd_state & NEWBLOCK; 12970 FREE_LOCK(ump); 12971 locked = 0; 12972 if (pagedep_new_block && (error = 12973 ffs_syncvnode(pvp, MNT_WAIT, 0))) { 12974 vput(pvp); 12975 return (error); 12976 } 12977 } 12978 } 12979 if (locked) 12980 FREE_LOCK(ump); 12981 } 12982 /* 12983 * Flush directory page containing the inode's name. 12984 */ 12985 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred, 12986 &bp); 12987 if (error == 0) 12988 error = bwrite(bp); 12989 else 12990 brelse(bp); 12991 vput(pvp); 12992 if (!ffs_fsfail_cleanup(ump, error)) 12993 return (error); 12994 ACQUIRE_LOCK(ump); 12995 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 12996 break; 12997 } 12998 FREE_LOCK(ump); 12999 return (0); 13000 } 13001 13002 /* 13003 * Flush all the dirty bitmaps associated with the block device 13004 * before flushing the rest of the dirty blocks so as to reduce 13005 * the number of dependencies that will have to be rolled back. 13006 * 13007 * XXX Unused? 13008 */ 13009 void 13010 softdep_fsync_mountdev(vp) 13011 struct vnode *vp; 13012 { 13013 struct buf *bp, *nbp; 13014 struct worklist *wk; 13015 struct bufobj *bo; 13016 13017 if (!vn_isdisk(vp)) 13018 panic("softdep_fsync_mountdev: vnode not a disk"); 13019 bo = &vp->v_bufobj; 13020 restart: 13021 BO_LOCK(bo); 13022 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 13023 /* 13024 * If it is already scheduled, skip to the next buffer. 13025 */ 13026 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) 13027 continue; 13028 13029 if ((bp->b_flags & B_DELWRI) == 0) 13030 panic("softdep_fsync_mountdev: not dirty"); 13031 /* 13032 * We are only interested in bitmaps with outstanding 13033 * dependencies. 13034 */ 13035 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL || 13036 wk->wk_type != D_BMSAFEMAP || 13037 (bp->b_vflags & BV_BKGRDINPROG)) { 13038 BUF_UNLOCK(bp); 13039 continue; 13040 } 13041 BO_UNLOCK(bo); 13042 bremfree(bp); 13043 (void) bawrite(bp); 13044 goto restart; 13045 } 13046 drain_output(vp); 13047 BO_UNLOCK(bo); 13048 } 13049 13050 /* 13051 * Sync all cylinder groups that were dirty at the time this function is 13052 * called. Newly dirtied cgs will be inserted before the sentinel. This 13053 * is used to flush freedep activity that may be holding up writes to a 13054 * indirect block. 13055 */ 13056 static int 13057 sync_cgs(mp, waitfor) 13058 struct mount *mp; 13059 int waitfor; 13060 { 13061 struct bmsafemap *bmsafemap; 13062 struct bmsafemap *sentinel; 13063 struct ufsmount *ump; 13064 struct buf *bp; 13065 int error; 13066 13067 sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK); 13068 sentinel->sm_cg = -1; 13069 ump = VFSTOUFS(mp); 13070 error = 0; 13071 ACQUIRE_LOCK(ump); 13072 LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next); 13073 for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL; 13074 bmsafemap = LIST_NEXT(sentinel, sm_next)) { 13075 /* Skip sentinels and cgs with no work to release. */ 13076 if (bmsafemap->sm_cg == -1 || 13077 (LIST_EMPTY(&bmsafemap->sm_freehd) && 13078 LIST_EMPTY(&bmsafemap->sm_freewr))) { 13079 LIST_REMOVE(sentinel, sm_next); 13080 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 13081 continue; 13082 } 13083 /* 13084 * If we don't get the lock and we're waiting try again, if 13085 * not move on to the next buf and try to sync it. 13086 */ 13087 bp = getdirtybuf(bmsafemap->sm_buf, LOCK_PTR(ump), waitfor); 13088 if (bp == NULL && waitfor == MNT_WAIT) 13089 continue; 13090 LIST_REMOVE(sentinel, sm_next); 13091 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 13092 if (bp == NULL) 13093 continue; 13094 FREE_LOCK(ump); 13095 if (waitfor == MNT_NOWAIT) 13096 bawrite(bp); 13097 else 13098 error = bwrite(bp); 13099 ACQUIRE_LOCK(ump); 13100 if (error) 13101 break; 13102 } 13103 LIST_REMOVE(sentinel, sm_next); 13104 FREE_LOCK(ump); 13105 free(sentinel, M_BMSAFEMAP); 13106 return (error); 13107 } 13108 13109 /* 13110 * This routine is called when we are trying to synchronously flush a 13111 * file. This routine must eliminate any filesystem metadata dependencies 13112 * so that the syncing routine can succeed. 13113 */ 13114 int 13115 softdep_sync_metadata(struct vnode *vp) 13116 { 13117 struct inode *ip; 13118 int error; 13119 13120 ip = VTOI(vp); 13121 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 13122 ("softdep_sync_metadata called on non-softdep filesystem")); 13123 /* 13124 * Ensure that any direct block dependencies have been cleared, 13125 * truncations are started, and inode references are journaled. 13126 */ 13127 ACQUIRE_LOCK(VFSTOUFS(vp->v_mount)); 13128 /* 13129 * Write all journal records to prevent rollbacks on devvp. 13130 */ 13131 if (vp->v_type == VCHR) 13132 softdep_flushjournal(vp->v_mount); 13133 error = flush_inodedep_deps(vp, vp->v_mount, ip->i_number); 13134 /* 13135 * Ensure that all truncates are written so we won't find deps on 13136 * indirect blocks. 13137 */ 13138 process_truncates(vp); 13139 FREE_LOCK(VFSTOUFS(vp->v_mount)); 13140 13141 return (error); 13142 } 13143 13144 /* 13145 * This routine is called when we are attempting to sync a buf with 13146 * dependencies. If waitfor is MNT_NOWAIT it attempts to schedule any 13147 * other IO it can but returns EBUSY if the buffer is not yet able to 13148 * be written. Dependencies which will not cause rollbacks will always 13149 * return 0. 13150 */ 13151 int 13152 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 13153 { 13154 struct indirdep *indirdep; 13155 struct pagedep *pagedep; 13156 struct allocindir *aip; 13157 struct newblk *newblk; 13158 struct ufsmount *ump; 13159 struct buf *nbp; 13160 struct worklist *wk; 13161 int i, error; 13162 13163 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 13164 ("softdep_sync_buf called on non-softdep filesystem")); 13165 /* 13166 * For VCHR we just don't want to force flush any dependencies that 13167 * will cause rollbacks. 13168 */ 13169 if (vp->v_type == VCHR) { 13170 if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0)) 13171 return (EBUSY); 13172 return (0); 13173 } 13174 ump = VFSTOUFS(vp->v_mount); 13175 ACQUIRE_LOCK(ump); 13176 /* 13177 * As we hold the buffer locked, none of its dependencies 13178 * will disappear. 13179 */ 13180 error = 0; 13181 top: 13182 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 13183 switch (wk->wk_type) { 13184 case D_ALLOCDIRECT: 13185 case D_ALLOCINDIR: 13186 newblk = WK_NEWBLK(wk); 13187 if (newblk->nb_jnewblk != NULL) { 13188 if (waitfor == MNT_NOWAIT) { 13189 error = EBUSY; 13190 goto out_unlock; 13191 } 13192 jwait(&newblk->nb_jnewblk->jn_list, waitfor); 13193 goto top; 13194 } 13195 if (newblk->nb_state & DEPCOMPLETE || 13196 waitfor == MNT_NOWAIT) 13197 continue; 13198 nbp = newblk->nb_bmsafemap->sm_buf; 13199 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 13200 if (nbp == NULL) 13201 goto top; 13202 FREE_LOCK(ump); 13203 if ((error = bwrite(nbp)) != 0) 13204 goto out; 13205 ACQUIRE_LOCK(ump); 13206 continue; 13207 13208 case D_INDIRDEP: 13209 indirdep = WK_INDIRDEP(wk); 13210 if (waitfor == MNT_NOWAIT) { 13211 if (!TAILQ_EMPTY(&indirdep->ir_trunc) || 13212 !LIST_EMPTY(&indirdep->ir_deplisthd)) { 13213 error = EBUSY; 13214 goto out_unlock; 13215 } 13216 } 13217 if (!TAILQ_EMPTY(&indirdep->ir_trunc)) 13218 panic("softdep_sync_buf: truncation pending."); 13219 restart: 13220 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 13221 newblk = (struct newblk *)aip; 13222 if (newblk->nb_jnewblk != NULL) { 13223 jwait(&newblk->nb_jnewblk->jn_list, 13224 waitfor); 13225 goto restart; 13226 } 13227 if (newblk->nb_state & DEPCOMPLETE) 13228 continue; 13229 nbp = newblk->nb_bmsafemap->sm_buf; 13230 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 13231 if (nbp == NULL) 13232 goto restart; 13233 FREE_LOCK(ump); 13234 if ((error = bwrite(nbp)) != 0) 13235 goto out; 13236 ACQUIRE_LOCK(ump); 13237 goto restart; 13238 } 13239 continue; 13240 13241 case D_PAGEDEP: 13242 /* 13243 * Only flush directory entries in synchronous passes. 13244 */ 13245 if (waitfor != MNT_WAIT) { 13246 error = EBUSY; 13247 goto out_unlock; 13248 } 13249 /* 13250 * While syncing snapshots, we must allow recursive 13251 * lookups. 13252 */ 13253 BUF_AREC(bp); 13254 /* 13255 * We are trying to sync a directory that may 13256 * have dependencies on both its own metadata 13257 * and/or dependencies on the inodes of any 13258 * recently allocated files. We walk its diradd 13259 * lists pushing out the associated inode. 13260 */ 13261 pagedep = WK_PAGEDEP(wk); 13262 for (i = 0; i < DAHASHSZ; i++) { 13263 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0) 13264 continue; 13265 error = flush_pagedep_deps(vp, wk->wk_mp, 13266 &pagedep->pd_diraddhd[i], bp); 13267 if (error != 0) { 13268 if (error != ERELOOKUP) 13269 BUF_NOREC(bp); 13270 goto out_unlock; 13271 } 13272 } 13273 BUF_NOREC(bp); 13274 continue; 13275 13276 case D_FREEWORK: 13277 case D_FREEDEP: 13278 case D_JSEGDEP: 13279 case D_JNEWBLK: 13280 continue; 13281 13282 default: 13283 panic("softdep_sync_buf: Unknown type %s", 13284 TYPENAME(wk->wk_type)); 13285 /* NOTREACHED */ 13286 } 13287 } 13288 out_unlock: 13289 FREE_LOCK(ump); 13290 out: 13291 return (error); 13292 } 13293 13294 /* 13295 * Flush the dependencies associated with an inodedep. 13296 */ 13297 static int 13298 flush_inodedep_deps(vp, mp, ino) 13299 struct vnode *vp; 13300 struct mount *mp; 13301 ino_t ino; 13302 { 13303 struct inodedep *inodedep; 13304 struct inoref *inoref; 13305 struct ufsmount *ump; 13306 int error, waitfor; 13307 13308 /* 13309 * This work is done in two passes. The first pass grabs most 13310 * of the buffers and begins asynchronously writing them. The 13311 * only way to wait for these asynchronous writes is to sleep 13312 * on the filesystem vnode which may stay busy for a long time 13313 * if the filesystem is active. So, instead, we make a second 13314 * pass over the dependencies blocking on each write. In the 13315 * usual case we will be blocking against a write that we 13316 * initiated, so when it is done the dependency will have been 13317 * resolved. Thus the second pass is expected to end quickly. 13318 * We give a brief window at the top of the loop to allow 13319 * any pending I/O to complete. 13320 */ 13321 ump = VFSTOUFS(mp); 13322 LOCK_OWNED(ump); 13323 for (error = 0, waitfor = MNT_NOWAIT; ; ) { 13324 if (error) 13325 return (error); 13326 FREE_LOCK(ump); 13327 ACQUIRE_LOCK(ump); 13328 restart: 13329 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 13330 return (0); 13331 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 13332 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 13333 == DEPCOMPLETE) { 13334 jwait(&inoref->if_list, MNT_WAIT); 13335 goto restart; 13336 } 13337 } 13338 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) || 13339 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) || 13340 flush_deplist(&inodedep->id_extupdt, waitfor, &error) || 13341 flush_deplist(&inodedep->id_newextupdt, waitfor, &error)) 13342 continue; 13343 /* 13344 * If pass2, we are done, otherwise do pass 2. 13345 */ 13346 if (waitfor == MNT_WAIT) 13347 break; 13348 waitfor = MNT_WAIT; 13349 } 13350 /* 13351 * Try freeing inodedep in case all dependencies have been removed. 13352 */ 13353 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0) 13354 (void) free_inodedep(inodedep); 13355 return (0); 13356 } 13357 13358 /* 13359 * Flush an inode dependency list. 13360 */ 13361 static int 13362 flush_deplist(listhead, waitfor, errorp) 13363 struct allocdirectlst *listhead; 13364 int waitfor; 13365 int *errorp; 13366 { 13367 struct allocdirect *adp; 13368 struct newblk *newblk; 13369 struct ufsmount *ump; 13370 struct buf *bp; 13371 13372 if ((adp = TAILQ_FIRST(listhead)) == NULL) 13373 return (0); 13374 ump = VFSTOUFS(adp->ad_list.wk_mp); 13375 LOCK_OWNED(ump); 13376 TAILQ_FOREACH(adp, listhead, ad_next) { 13377 newblk = (struct newblk *)adp; 13378 if (newblk->nb_jnewblk != NULL) { 13379 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 13380 return (1); 13381 } 13382 if (newblk->nb_state & DEPCOMPLETE) 13383 continue; 13384 bp = newblk->nb_bmsafemap->sm_buf; 13385 bp = getdirtybuf(bp, LOCK_PTR(ump), waitfor); 13386 if (bp == NULL) { 13387 if (waitfor == MNT_NOWAIT) 13388 continue; 13389 return (1); 13390 } 13391 FREE_LOCK(ump); 13392 if (waitfor == MNT_NOWAIT) 13393 bawrite(bp); 13394 else 13395 *errorp = bwrite(bp); 13396 ACQUIRE_LOCK(ump); 13397 return (1); 13398 } 13399 return (0); 13400 } 13401 13402 /* 13403 * Flush dependencies associated with an allocdirect block. 13404 */ 13405 static int 13406 flush_newblk_dep(vp, mp, lbn) 13407 struct vnode *vp; 13408 struct mount *mp; 13409 ufs_lbn_t lbn; 13410 { 13411 struct newblk *newblk; 13412 struct ufsmount *ump; 13413 struct bufobj *bo; 13414 struct inode *ip; 13415 struct buf *bp; 13416 ufs2_daddr_t blkno; 13417 int error; 13418 13419 error = 0; 13420 bo = &vp->v_bufobj; 13421 ip = VTOI(vp); 13422 blkno = DIP(ip, i_db[lbn]); 13423 if (blkno == 0) 13424 panic("flush_newblk_dep: Missing block"); 13425 ump = VFSTOUFS(mp); 13426 ACQUIRE_LOCK(ump); 13427 /* 13428 * Loop until all dependencies related to this block are satisfied. 13429 * We must be careful to restart after each sleep in case a write 13430 * completes some part of this process for us. 13431 */ 13432 for (;;) { 13433 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) { 13434 FREE_LOCK(ump); 13435 break; 13436 } 13437 if (newblk->nb_list.wk_type != D_ALLOCDIRECT) 13438 panic("flush_newblk_dep: Bad newblk %p", newblk); 13439 /* 13440 * Flush the journal. 13441 */ 13442 if (newblk->nb_jnewblk != NULL) { 13443 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 13444 continue; 13445 } 13446 /* 13447 * Write the bitmap dependency. 13448 */ 13449 if ((newblk->nb_state & DEPCOMPLETE) == 0) { 13450 bp = newblk->nb_bmsafemap->sm_buf; 13451 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 13452 if (bp == NULL) 13453 continue; 13454 FREE_LOCK(ump); 13455 error = bwrite(bp); 13456 if (error) 13457 break; 13458 ACQUIRE_LOCK(ump); 13459 continue; 13460 } 13461 /* 13462 * Write the buffer. 13463 */ 13464 FREE_LOCK(ump); 13465 BO_LOCK(bo); 13466 bp = gbincore(bo, lbn); 13467 if (bp != NULL) { 13468 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 13469 LK_INTERLOCK, BO_LOCKPTR(bo)); 13470 if (error == ENOLCK) { 13471 ACQUIRE_LOCK(ump); 13472 error = 0; 13473 continue; /* Slept, retry */ 13474 } 13475 if (error != 0) 13476 break; /* Failed */ 13477 if (bp->b_flags & B_DELWRI) { 13478 bremfree(bp); 13479 error = bwrite(bp); 13480 if (error) 13481 break; 13482 } else 13483 BUF_UNLOCK(bp); 13484 } else 13485 BO_UNLOCK(bo); 13486 /* 13487 * We have to wait for the direct pointers to 13488 * point at the newdirblk before the dependency 13489 * will go away. 13490 */ 13491 error = ffs_update(vp, 1); 13492 if (error) 13493 break; 13494 ACQUIRE_LOCK(ump); 13495 } 13496 return (error); 13497 } 13498 13499 /* 13500 * Eliminate a pagedep dependency by flushing out all its diradd dependencies. 13501 */ 13502 static int 13503 flush_pagedep_deps(pvp, mp, diraddhdp, locked_bp) 13504 struct vnode *pvp; 13505 struct mount *mp; 13506 struct diraddhd *diraddhdp; 13507 struct buf *locked_bp; 13508 { 13509 struct inodedep *inodedep; 13510 struct inoref *inoref; 13511 struct ufsmount *ump; 13512 struct diradd *dap; 13513 struct vnode *vp; 13514 int error = 0; 13515 struct buf *bp; 13516 ino_t inum; 13517 struct diraddhd unfinished; 13518 13519 LIST_INIT(&unfinished); 13520 ump = VFSTOUFS(mp); 13521 LOCK_OWNED(ump); 13522 restart: 13523 while ((dap = LIST_FIRST(diraddhdp)) != NULL) { 13524 /* 13525 * Flush ourselves if this directory entry 13526 * has a MKDIR_PARENT dependency. 13527 */ 13528 if (dap->da_state & MKDIR_PARENT) { 13529 FREE_LOCK(ump); 13530 if ((error = ffs_update(pvp, 1)) != 0) 13531 break; 13532 ACQUIRE_LOCK(ump); 13533 /* 13534 * If that cleared dependencies, go on to next. 13535 */ 13536 if (dap != LIST_FIRST(diraddhdp)) 13537 continue; 13538 /* 13539 * All MKDIR_PARENT dependencies and all the 13540 * NEWBLOCK pagedeps that are contained in direct 13541 * blocks were resolved by doing above ffs_update. 13542 * Pagedeps contained in indirect blocks may 13543 * require a complete sync'ing of the directory. 13544 * We are in the midst of doing a complete sync, 13545 * so if they are not resolved in this pass we 13546 * defer them for now as they will be sync'ed by 13547 * our caller shortly. 13548 */ 13549 LIST_REMOVE(dap, da_pdlist); 13550 LIST_INSERT_HEAD(&unfinished, dap, da_pdlist); 13551 continue; 13552 } 13553 /* 13554 * A newly allocated directory must have its "." and 13555 * ".." entries written out before its name can be 13556 * committed in its parent. 13557 */ 13558 inum = dap->da_newinum; 13559 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 13560 panic("flush_pagedep_deps: lost inode1"); 13561 /* 13562 * Wait for any pending journal adds to complete so we don't 13563 * cause rollbacks while syncing. 13564 */ 13565 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 13566 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 13567 == DEPCOMPLETE) { 13568 jwait(&inoref->if_list, MNT_WAIT); 13569 goto restart; 13570 } 13571 } 13572 if (dap->da_state & MKDIR_BODY) { 13573 FREE_LOCK(ump); 13574 error = get_parent_vp(pvp, mp, inum, locked_bp, 13575 diraddhdp, &unfinished, &vp); 13576 if (error != 0) 13577 break; 13578 error = flush_newblk_dep(vp, mp, 0); 13579 /* 13580 * If we still have the dependency we might need to 13581 * update the vnode to sync the new link count to 13582 * disk. 13583 */ 13584 if (error == 0 && dap == LIST_FIRST(diraddhdp)) 13585 error = ffs_update(vp, 1); 13586 vput(vp); 13587 if (error != 0) 13588 break; 13589 ACQUIRE_LOCK(ump); 13590 /* 13591 * If that cleared dependencies, go on to next. 13592 */ 13593 if (dap != LIST_FIRST(diraddhdp)) 13594 continue; 13595 if (dap->da_state & MKDIR_BODY) { 13596 inodedep_lookup(UFSTOVFS(ump), inum, 0, 13597 &inodedep); 13598 panic("flush_pagedep_deps: MKDIR_BODY " 13599 "inodedep %p dap %p vp %p", 13600 inodedep, dap, vp); 13601 } 13602 } 13603 /* 13604 * Flush the inode on which the directory entry depends. 13605 * Having accounted for MKDIR_PARENT and MKDIR_BODY above, 13606 * the only remaining dependency is that the updated inode 13607 * count must get pushed to disk. The inode has already 13608 * been pushed into its inode buffer (via VOP_UPDATE) at 13609 * the time of the reference count change. So we need only 13610 * locate that buffer, ensure that there will be no rollback 13611 * caused by a bitmap dependency, then write the inode buffer. 13612 */ 13613 retry: 13614 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 13615 panic("flush_pagedep_deps: lost inode"); 13616 /* 13617 * If the inode still has bitmap dependencies, 13618 * push them to disk. 13619 */ 13620 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) { 13621 bp = inodedep->id_bmsafemap->sm_buf; 13622 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 13623 if (bp == NULL) 13624 goto retry; 13625 FREE_LOCK(ump); 13626 if ((error = bwrite(bp)) != 0) 13627 break; 13628 ACQUIRE_LOCK(ump); 13629 if (dap != LIST_FIRST(diraddhdp)) 13630 continue; 13631 } 13632 /* 13633 * If the inode is still sitting in a buffer waiting 13634 * to be written or waiting for the link count to be 13635 * adjusted update it here to flush it to disk. 13636 */ 13637 if (dap == LIST_FIRST(diraddhdp)) { 13638 FREE_LOCK(ump); 13639 error = get_parent_vp(pvp, mp, inum, locked_bp, 13640 diraddhdp, &unfinished, &vp); 13641 if (error != 0) 13642 break; 13643 error = ffs_update(vp, 1); 13644 vput(vp); 13645 if (error) 13646 break; 13647 ACQUIRE_LOCK(ump); 13648 } 13649 /* 13650 * If we have failed to get rid of all the dependencies 13651 * then something is seriously wrong. 13652 */ 13653 if (dap == LIST_FIRST(diraddhdp)) { 13654 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep); 13655 panic("flush_pagedep_deps: failed to flush " 13656 "inodedep %p ino %ju dap %p", 13657 inodedep, (uintmax_t)inum, dap); 13658 } 13659 } 13660 if (error) 13661 ACQUIRE_LOCK(ump); 13662 while ((dap = LIST_FIRST(&unfinished)) != NULL) { 13663 LIST_REMOVE(dap, da_pdlist); 13664 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist); 13665 } 13666 return (error); 13667 } 13668 13669 /* 13670 * A large burst of file addition or deletion activity can drive the 13671 * memory load excessively high. First attempt to slow things down 13672 * using the techniques below. If that fails, this routine requests 13673 * the offending operations to fall back to running synchronously 13674 * until the memory load returns to a reasonable level. 13675 */ 13676 int 13677 softdep_slowdown(vp) 13678 struct vnode *vp; 13679 { 13680 struct ufsmount *ump; 13681 int jlow; 13682 int max_softdeps_hard; 13683 13684 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 13685 ("softdep_slowdown called on non-softdep filesystem")); 13686 ump = VFSTOUFS(vp->v_mount); 13687 ACQUIRE_LOCK(ump); 13688 jlow = 0; 13689 /* 13690 * Check for journal space if needed. 13691 */ 13692 if (DOINGSUJ(vp)) { 13693 if (journal_space(ump, 0) == 0) 13694 jlow = 1; 13695 } 13696 /* 13697 * If the system is under its limits and our filesystem is 13698 * not responsible for more than our share of the usage and 13699 * we are not low on journal space, then no need to slow down. 13700 */ 13701 max_softdeps_hard = max_softdeps * 11 / 10; 13702 if (dep_current[D_DIRREM] < max_softdeps_hard / 2 && 13703 dep_current[D_INODEDEP] < max_softdeps_hard && 13704 dep_current[D_INDIRDEP] < max_softdeps_hard / 1000 && 13705 dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0 && 13706 ump->softdep_curdeps[D_DIRREM] < 13707 (max_softdeps_hard / 2) / stat_flush_threads && 13708 ump->softdep_curdeps[D_INODEDEP] < 13709 max_softdeps_hard / stat_flush_threads && 13710 ump->softdep_curdeps[D_INDIRDEP] < 13711 (max_softdeps_hard / 1000) / stat_flush_threads && 13712 ump->softdep_curdeps[D_FREEBLKS] < 13713 max_softdeps_hard / stat_flush_threads) { 13714 FREE_LOCK(ump); 13715 return (0); 13716 } 13717 /* 13718 * If the journal is low or our filesystem is over its limit 13719 * then speedup the cleanup. 13720 */ 13721 if (ump->softdep_curdeps[D_INDIRDEP] < 13722 (max_softdeps_hard / 1000) / stat_flush_threads || jlow) 13723 softdep_speedup(ump); 13724 stat_sync_limit_hit += 1; 13725 FREE_LOCK(ump); 13726 /* 13727 * We only slow down the rate at which new dependencies are 13728 * generated if we are not using journaling. With journaling, 13729 * the cleanup should always be sufficient to keep things 13730 * under control. 13731 */ 13732 if (DOINGSUJ(vp)) 13733 return (0); 13734 return (1); 13735 } 13736 13737 static int 13738 softdep_request_cleanup_filter(struct vnode *vp, void *arg __unused) 13739 { 13740 return ((vp->v_iflag & VI_OWEINACT) != 0 && vp->v_usecount == 0 && 13741 ((vp->v_vflag & VV_NOSYNC) != 0 || VTOI(vp)->i_effnlink == 0)); 13742 } 13743 13744 static void 13745 softdep_request_cleanup_inactivate(struct mount *mp) 13746 { 13747 struct vnode *vp, *mvp; 13748 int error; 13749 13750 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, softdep_request_cleanup_filter, 13751 NULL) { 13752 vholdl(vp); 13753 vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY); 13754 VI_LOCK(vp); 13755 if (vp->v_data != NULL && vp->v_usecount == 0) { 13756 while ((vp->v_iflag & VI_OWEINACT) != 0) { 13757 error = vinactive(vp); 13758 if (error != 0 && error != ERELOOKUP) 13759 break; 13760 } 13761 atomic_add_int(&stat_delayed_inact, 1); 13762 } 13763 VOP_UNLOCK(vp); 13764 vdropl(vp); 13765 } 13766 } 13767 13768 /* 13769 * Called by the allocation routines when they are about to fail 13770 * in the hope that we can free up the requested resource (inodes 13771 * or disk space). 13772 * 13773 * First check to see if the work list has anything on it. If it has, 13774 * clean up entries until we successfully free the requested resource. 13775 * Because this process holds inodes locked, we cannot handle any remove 13776 * requests that might block on a locked inode as that could lead to 13777 * deadlock. If the worklist yields none of the requested resource, 13778 * start syncing out vnodes to free up the needed space. 13779 */ 13780 int 13781 softdep_request_cleanup(fs, vp, cred, resource) 13782 struct fs *fs; 13783 struct vnode *vp; 13784 struct ucred *cred; 13785 int resource; 13786 { 13787 struct ufsmount *ump; 13788 struct mount *mp; 13789 long starttime; 13790 ufs2_daddr_t needed; 13791 int error, failed_vnode; 13792 13793 /* 13794 * If we are being called because of a process doing a 13795 * copy-on-write, then it is not safe to process any 13796 * worklist items as we will recurse into the copyonwrite 13797 * routine. This will result in an incoherent snapshot. 13798 * If the vnode that we hold is a snapshot, we must avoid 13799 * handling other resources that could cause deadlock. 13800 */ 13801 if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp))) 13802 return (0); 13803 13804 if (resource == FLUSH_BLOCKS_WAIT) 13805 stat_cleanup_blkrequests += 1; 13806 else 13807 stat_cleanup_inorequests += 1; 13808 13809 mp = vp->v_mount; 13810 ump = VFSTOUFS(mp); 13811 mtx_assert(UFS_MTX(ump), MA_OWNED); 13812 UFS_UNLOCK(ump); 13813 error = ffs_update(vp, 1); 13814 if (error != 0 || MOUNTEDSOFTDEP(mp) == 0) { 13815 UFS_LOCK(ump); 13816 return (0); 13817 } 13818 /* 13819 * If we are in need of resources, start by cleaning up 13820 * any block removals associated with our inode. 13821 */ 13822 ACQUIRE_LOCK(ump); 13823 process_removes(vp); 13824 process_truncates(vp); 13825 FREE_LOCK(ump); 13826 /* 13827 * Now clean up at least as many resources as we will need. 13828 * 13829 * When requested to clean up inodes, the number that are needed 13830 * is set by the number of simultaneous writers (mnt_writeopcount) 13831 * plus a bit of slop (2) in case some more writers show up while 13832 * we are cleaning. 13833 * 13834 * When requested to free up space, the amount of space that 13835 * we need is enough blocks to allocate a full-sized segment 13836 * (fs_contigsumsize). The number of such segments that will 13837 * be needed is set by the number of simultaneous writers 13838 * (mnt_writeopcount) plus a bit of slop (2) in case some more 13839 * writers show up while we are cleaning. 13840 * 13841 * Additionally, if we are unpriviledged and allocating space, 13842 * we need to ensure that we clean up enough blocks to get the 13843 * needed number of blocks over the threshold of the minimum 13844 * number of blocks required to be kept free by the filesystem 13845 * (fs_minfree). 13846 */ 13847 if (resource == FLUSH_INODES_WAIT) { 13848 needed = vfs_mount_fetch_counter(vp->v_mount, 13849 MNT_COUNT_WRITEOPCOUNT) + 2; 13850 } else if (resource == FLUSH_BLOCKS_WAIT) { 13851 needed = (vfs_mount_fetch_counter(vp->v_mount, 13852 MNT_COUNT_WRITEOPCOUNT) + 2) * fs->fs_contigsumsize; 13853 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE)) 13854 needed += fragstoblks(fs, 13855 roundup((fs->fs_dsize * fs->fs_minfree / 100) - 13856 fs->fs_cstotal.cs_nffree, fs->fs_frag)); 13857 } else { 13858 printf("softdep_request_cleanup: Unknown resource type %d\n", 13859 resource); 13860 UFS_LOCK(ump); 13861 return (0); 13862 } 13863 starttime = time_second; 13864 retry: 13865 if (resource == FLUSH_BLOCKS_WAIT && 13866 fs->fs_cstotal.cs_nbfree <= needed) 13867 softdep_send_speedup(ump, needed * fs->fs_bsize, 13868 BIO_SPEEDUP_TRIM); 13869 if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 && 13870 fs->fs_cstotal.cs_nbfree <= needed) || 13871 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13872 fs->fs_cstotal.cs_nifree <= needed)) { 13873 ACQUIRE_LOCK(ump); 13874 if (ump->softdep_on_worklist > 0 && 13875 process_worklist_item(UFSTOVFS(ump), 13876 ump->softdep_on_worklist, LK_NOWAIT) != 0) 13877 stat_worklist_push += 1; 13878 FREE_LOCK(ump); 13879 } 13880 13881 /* 13882 * Check that there are vnodes pending inactivation. As they 13883 * have been unlinked, inactivating them will free up their 13884 * inodes. 13885 */ 13886 ACQUIRE_LOCK(ump); 13887 if (resource == FLUSH_INODES_WAIT && 13888 fs->fs_cstotal.cs_nifree <= needed && 13889 fs->fs_pendinginodes <= needed) { 13890 if ((ump->um_softdep->sd_flags & FLUSH_DI_ACTIVE) == 0) { 13891 ump->um_softdep->sd_flags |= FLUSH_DI_ACTIVE; 13892 FREE_LOCK(ump); 13893 softdep_request_cleanup_inactivate(mp); 13894 ACQUIRE_LOCK(ump); 13895 ump->um_softdep->sd_flags &= ~FLUSH_DI_ACTIVE; 13896 wakeup(&ump->um_softdep->sd_flags); 13897 } else { 13898 while ((ump->um_softdep->sd_flags & 13899 FLUSH_DI_ACTIVE) != 0) { 13900 msleep(&ump->um_softdep->sd_flags, 13901 LOCK_PTR(ump), PVM, "ffsvina", hz); 13902 } 13903 } 13904 } 13905 FREE_LOCK(ump); 13906 13907 /* 13908 * If we still need resources and there are no more worklist 13909 * entries to process to obtain them, we have to start flushing 13910 * the dirty vnodes to force the release of additional requests 13911 * to the worklist that we can then process to reap addition 13912 * resources. We walk the vnodes associated with the mount point 13913 * until we get the needed worklist requests that we can reap. 13914 * 13915 * If there are several threads all needing to clean the same 13916 * mount point, only one is allowed to walk the mount list. 13917 * When several threads all try to walk the same mount list, 13918 * they end up competing with each other and often end up in 13919 * livelock. This approach ensures that forward progress is 13920 * made at the cost of occational ENOSPC errors being returned 13921 * that might otherwise have been avoided. 13922 */ 13923 error = 1; 13924 if ((resource == FLUSH_BLOCKS_WAIT && 13925 fs->fs_cstotal.cs_nbfree <= needed) || 13926 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13927 fs->fs_cstotal.cs_nifree <= needed)) { 13928 ACQUIRE_LOCK(ump); 13929 if ((ump->um_softdep->sd_flags & FLUSH_RC_ACTIVE) == 0) { 13930 ump->um_softdep->sd_flags |= FLUSH_RC_ACTIVE; 13931 FREE_LOCK(ump); 13932 failed_vnode = softdep_request_cleanup_flush(mp, ump); 13933 ACQUIRE_LOCK(ump); 13934 ump->um_softdep->sd_flags &= ~FLUSH_RC_ACTIVE; 13935 wakeup(&ump->um_softdep->sd_flags); 13936 FREE_LOCK(ump); 13937 if (ump->softdep_on_worklist > 0) { 13938 stat_cleanup_retries += 1; 13939 if (!failed_vnode) 13940 goto retry; 13941 } 13942 } else { 13943 while ((ump->um_softdep->sd_flags & 13944 FLUSH_RC_ACTIVE) != 0) { 13945 msleep(&ump->um_softdep->sd_flags, 13946 LOCK_PTR(ump), PVM, "ffsrca", hz); 13947 } 13948 FREE_LOCK(ump); 13949 error = 0; 13950 } 13951 stat_cleanup_failures += 1; 13952 } 13953 if (time_second - starttime > stat_cleanup_high_delay) 13954 stat_cleanup_high_delay = time_second - starttime; 13955 UFS_LOCK(ump); 13956 return (error); 13957 } 13958 13959 /* 13960 * Scan the vnodes for the specified mount point flushing out any 13961 * vnodes that can be locked without waiting. Finally, try to flush 13962 * the device associated with the mount point if it can be locked 13963 * without waiting. 13964 * 13965 * We return 0 if we were able to lock every vnode in our scan. 13966 * If we had to skip one or more vnodes, we return 1. 13967 */ 13968 static int 13969 softdep_request_cleanup_flush(mp, ump) 13970 struct mount *mp; 13971 struct ufsmount *ump; 13972 { 13973 struct thread *td; 13974 struct vnode *lvp, *mvp; 13975 int failed_vnode; 13976 13977 failed_vnode = 0; 13978 td = curthread; 13979 MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) { 13980 if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) { 13981 VI_UNLOCK(lvp); 13982 continue; 13983 } 13984 if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT) != 0) { 13985 failed_vnode = 1; 13986 continue; 13987 } 13988 if (lvp->v_vflag & VV_NOSYNC) { /* unlinked */ 13989 vput(lvp); 13990 continue; 13991 } 13992 (void) ffs_syncvnode(lvp, MNT_NOWAIT, 0); 13993 vput(lvp); 13994 } 13995 lvp = ump->um_devvp; 13996 if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) { 13997 VOP_FSYNC(lvp, MNT_NOWAIT, td); 13998 VOP_UNLOCK(lvp); 13999 } 14000 return (failed_vnode); 14001 } 14002 14003 static bool 14004 softdep_excess_items(struct ufsmount *ump, int item) 14005 { 14006 14007 KASSERT(item >= 0 && item < D_LAST, ("item %d", item)); 14008 return (dep_current[item] > max_softdeps && 14009 ump->softdep_curdeps[item] > max_softdeps / 14010 stat_flush_threads); 14011 } 14012 14013 static void 14014 schedule_cleanup(struct mount *mp) 14015 { 14016 struct ufsmount *ump; 14017 struct thread *td; 14018 14019 ump = VFSTOUFS(mp); 14020 LOCK_OWNED(ump); 14021 FREE_LOCK(ump); 14022 td = curthread; 14023 if ((td->td_pflags & TDP_KTHREAD) != 0 && 14024 (td->td_proc->p_flag2 & P2_AST_SU) == 0) { 14025 /* 14026 * No ast is delivered to kernel threads, so nobody 14027 * would deref the mp. Some kernel threads 14028 * explicitely check for AST, e.g. NFS daemon does 14029 * this in the serving loop. 14030 */ 14031 return; 14032 } 14033 if (td->td_su != NULL) 14034 vfs_rel(td->td_su); 14035 vfs_ref(mp); 14036 td->td_su = mp; 14037 thread_lock(td); 14038 td->td_flags |= TDF_ASTPENDING; 14039 thread_unlock(td); 14040 } 14041 14042 static void 14043 softdep_ast_cleanup_proc(struct thread *td) 14044 { 14045 struct mount *mp; 14046 struct ufsmount *ump; 14047 int error; 14048 bool req; 14049 14050 while ((mp = td->td_su) != NULL) { 14051 td->td_su = NULL; 14052 error = vfs_busy(mp, MBF_NOWAIT); 14053 vfs_rel(mp); 14054 if (error != 0) 14055 return; 14056 if (ffs_own_mount(mp) && MOUNTEDSOFTDEP(mp)) { 14057 ump = VFSTOUFS(mp); 14058 for (;;) { 14059 req = false; 14060 ACQUIRE_LOCK(ump); 14061 if (softdep_excess_items(ump, D_INODEDEP)) { 14062 req = true; 14063 request_cleanup(mp, FLUSH_INODES); 14064 } 14065 if (softdep_excess_items(ump, D_DIRREM)) { 14066 req = true; 14067 request_cleanup(mp, FLUSH_BLOCKS); 14068 } 14069 FREE_LOCK(ump); 14070 if (softdep_excess_items(ump, D_NEWBLK) || 14071 softdep_excess_items(ump, D_ALLOCDIRECT) || 14072 softdep_excess_items(ump, D_ALLOCINDIR)) { 14073 error = vn_start_write(NULL, &mp, 14074 V_WAIT); 14075 if (error == 0) { 14076 req = true; 14077 VFS_SYNC(mp, MNT_WAIT); 14078 vn_finished_write(mp); 14079 } 14080 } 14081 if ((td->td_pflags & TDP_KTHREAD) != 0 || !req) 14082 break; 14083 } 14084 } 14085 vfs_unbusy(mp); 14086 } 14087 if ((mp = td->td_su) != NULL) { 14088 td->td_su = NULL; 14089 vfs_rel(mp); 14090 } 14091 } 14092 14093 /* 14094 * If memory utilization has gotten too high, deliberately slow things 14095 * down and speed up the I/O processing. 14096 */ 14097 static int 14098 request_cleanup(mp, resource) 14099 struct mount *mp; 14100 int resource; 14101 { 14102 struct thread *td = curthread; 14103 struct ufsmount *ump; 14104 14105 ump = VFSTOUFS(mp); 14106 LOCK_OWNED(ump); 14107 /* 14108 * We never hold up the filesystem syncer or buf daemon. 14109 */ 14110 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF)) 14111 return (0); 14112 /* 14113 * First check to see if the work list has gotten backlogged. 14114 * If it has, co-opt this process to help clean up two entries. 14115 * Because this process may hold inodes locked, we cannot 14116 * handle any remove requests that might block on a locked 14117 * inode as that could lead to deadlock. We set TDP_SOFTDEP 14118 * to avoid recursively processing the worklist. 14119 */ 14120 if (ump->softdep_on_worklist > max_softdeps / 10) { 14121 td->td_pflags |= TDP_SOFTDEP; 14122 process_worklist_item(mp, 2, LK_NOWAIT); 14123 td->td_pflags &= ~TDP_SOFTDEP; 14124 stat_worklist_push += 2; 14125 return(1); 14126 } 14127 /* 14128 * Next, we attempt to speed up the syncer process. If that 14129 * is successful, then we allow the process to continue. 14130 */ 14131 if (softdep_speedup(ump) && 14132 resource != FLUSH_BLOCKS_WAIT && 14133 resource != FLUSH_INODES_WAIT) 14134 return(0); 14135 /* 14136 * If we are resource constrained on inode dependencies, try 14137 * flushing some dirty inodes. Otherwise, we are constrained 14138 * by file deletions, so try accelerating flushes of directories 14139 * with removal dependencies. We would like to do the cleanup 14140 * here, but we probably hold an inode locked at this point and 14141 * that might deadlock against one that we try to clean. So, 14142 * the best that we can do is request the syncer daemon to do 14143 * the cleanup for us. 14144 */ 14145 switch (resource) { 14146 case FLUSH_INODES: 14147 case FLUSH_INODES_WAIT: 14148 ACQUIRE_GBLLOCK(&lk); 14149 stat_ino_limit_push += 1; 14150 req_clear_inodedeps += 1; 14151 FREE_GBLLOCK(&lk); 14152 stat_countp = &stat_ino_limit_hit; 14153 break; 14154 14155 case FLUSH_BLOCKS: 14156 case FLUSH_BLOCKS_WAIT: 14157 ACQUIRE_GBLLOCK(&lk); 14158 stat_blk_limit_push += 1; 14159 req_clear_remove += 1; 14160 FREE_GBLLOCK(&lk); 14161 stat_countp = &stat_blk_limit_hit; 14162 break; 14163 14164 default: 14165 panic("request_cleanup: unknown type"); 14166 } 14167 /* 14168 * Hopefully the syncer daemon will catch up and awaken us. 14169 * We wait at most tickdelay before proceeding in any case. 14170 */ 14171 ACQUIRE_GBLLOCK(&lk); 14172 FREE_LOCK(ump); 14173 proc_waiting += 1; 14174 if (callout_pending(&softdep_callout) == FALSE) 14175 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2, 14176 pause_timer, 0); 14177 14178 if ((td->td_pflags & TDP_KTHREAD) == 0) 14179 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0); 14180 proc_waiting -= 1; 14181 FREE_GBLLOCK(&lk); 14182 ACQUIRE_LOCK(ump); 14183 return (1); 14184 } 14185 14186 /* 14187 * Awaken processes pausing in request_cleanup and clear proc_waiting 14188 * to indicate that there is no longer a timer running. Pause_timer 14189 * will be called with the global softdep mutex (&lk) locked. 14190 */ 14191 static void 14192 pause_timer(arg) 14193 void *arg; 14194 { 14195 14196 GBLLOCK_OWNED(&lk); 14197 /* 14198 * The callout_ API has acquired mtx and will hold it around this 14199 * function call. 14200 */ 14201 *stat_countp += proc_waiting; 14202 wakeup(&proc_waiting); 14203 } 14204 14205 /* 14206 * If requested, try removing inode or removal dependencies. 14207 */ 14208 static void 14209 check_clear_deps(mp) 14210 struct mount *mp; 14211 { 14212 struct ufsmount *ump; 14213 bool suj_susp; 14214 14215 /* 14216 * Tell the lower layers that any TRIM or WRITE transactions that have 14217 * been delayed for performance reasons should proceed to help alleviate 14218 * the shortage faster. The race between checking req_* and the softdep 14219 * mutex (lk) is fine since this is an advisory operation that at most 14220 * causes deferred work to be done sooner. 14221 */ 14222 ump = VFSTOUFS(mp); 14223 suj_susp = ump->um_softdep->sd_jblocks != NULL && 14224 ump->softdep_jblocks->jb_suspended; 14225 if (req_clear_remove || req_clear_inodedeps || suj_susp) { 14226 FREE_LOCK(ump); 14227 softdep_send_speedup(ump, 0, BIO_SPEEDUP_TRIM | BIO_SPEEDUP_WRITE); 14228 ACQUIRE_LOCK(ump); 14229 } 14230 14231 /* 14232 * If we are suspended, it may be because of our using 14233 * too many inodedeps, so help clear them out. 14234 */ 14235 if (suj_susp) 14236 clear_inodedeps(mp); 14237 14238 /* 14239 * General requests for cleanup of backed up dependencies 14240 */ 14241 ACQUIRE_GBLLOCK(&lk); 14242 if (req_clear_inodedeps) { 14243 req_clear_inodedeps -= 1; 14244 FREE_GBLLOCK(&lk); 14245 clear_inodedeps(mp); 14246 ACQUIRE_GBLLOCK(&lk); 14247 wakeup(&proc_waiting); 14248 } 14249 if (req_clear_remove) { 14250 req_clear_remove -= 1; 14251 FREE_GBLLOCK(&lk); 14252 clear_remove(mp); 14253 ACQUIRE_GBLLOCK(&lk); 14254 wakeup(&proc_waiting); 14255 } 14256 FREE_GBLLOCK(&lk); 14257 } 14258 14259 /* 14260 * Flush out a directory with at least one removal dependency in an effort to 14261 * reduce the number of dirrem, freefile, and freeblks dependency structures. 14262 */ 14263 static void 14264 clear_remove(mp) 14265 struct mount *mp; 14266 { 14267 struct pagedep_hashhead *pagedephd; 14268 struct pagedep *pagedep; 14269 struct ufsmount *ump; 14270 struct vnode *vp; 14271 struct bufobj *bo; 14272 int error, cnt; 14273 ino_t ino; 14274 14275 ump = VFSTOUFS(mp); 14276 LOCK_OWNED(ump); 14277 14278 for (cnt = 0; cnt <= ump->pagedep_hash_size; cnt++) { 14279 pagedephd = &ump->pagedep_hashtbl[ump->pagedep_nextclean++]; 14280 if (ump->pagedep_nextclean > ump->pagedep_hash_size) 14281 ump->pagedep_nextclean = 0; 14282 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 14283 if (LIST_EMPTY(&pagedep->pd_dirremhd)) 14284 continue; 14285 ino = pagedep->pd_ino; 14286 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 14287 continue; 14288 FREE_LOCK(ump); 14289 14290 /* 14291 * Let unmount clear deps 14292 */ 14293 error = vfs_busy(mp, MBF_NOWAIT); 14294 if (error != 0) 14295 goto finish_write; 14296 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 14297 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP); 14298 vfs_unbusy(mp); 14299 if (error != 0) { 14300 softdep_error("clear_remove: vget", error); 14301 goto finish_write; 14302 } 14303 MPASS(VTOI(vp)->i_mode != 0); 14304 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 14305 softdep_error("clear_remove: fsync", error); 14306 bo = &vp->v_bufobj; 14307 BO_LOCK(bo); 14308 drain_output(vp); 14309 BO_UNLOCK(bo); 14310 vput(vp); 14311 finish_write: 14312 vn_finished_write(mp); 14313 ACQUIRE_LOCK(ump); 14314 return; 14315 } 14316 } 14317 } 14318 14319 /* 14320 * Clear out a block of dirty inodes in an effort to reduce 14321 * the number of inodedep dependency structures. 14322 */ 14323 static void 14324 clear_inodedeps(mp) 14325 struct mount *mp; 14326 { 14327 struct inodedep_hashhead *inodedephd; 14328 struct inodedep *inodedep; 14329 struct ufsmount *ump; 14330 struct vnode *vp; 14331 struct fs *fs; 14332 int error, cnt; 14333 ino_t firstino, lastino, ino; 14334 14335 ump = VFSTOUFS(mp); 14336 fs = ump->um_fs; 14337 LOCK_OWNED(ump); 14338 /* 14339 * Pick a random inode dependency to be cleared. 14340 * We will then gather up all the inodes in its block 14341 * that have dependencies and flush them out. 14342 */ 14343 for (cnt = 0; cnt <= ump->inodedep_hash_size; cnt++) { 14344 inodedephd = &ump->inodedep_hashtbl[ump->inodedep_nextclean++]; 14345 if (ump->inodedep_nextclean > ump->inodedep_hash_size) 14346 ump->inodedep_nextclean = 0; 14347 if ((inodedep = LIST_FIRST(inodedephd)) != NULL) 14348 break; 14349 } 14350 if (inodedep == NULL) 14351 return; 14352 /* 14353 * Find the last inode in the block with dependencies. 14354 */ 14355 firstino = rounddown2(inodedep->id_ino, INOPB(fs)); 14356 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--) 14357 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0) 14358 break; 14359 /* 14360 * Asynchronously push all but the last inode with dependencies. 14361 * Synchronously push the last inode with dependencies to ensure 14362 * that the inode block gets written to free up the inodedeps. 14363 */ 14364 for (ino = firstino; ino <= lastino; ino++) { 14365 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 14366 continue; 14367 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 14368 continue; 14369 FREE_LOCK(ump); 14370 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */ 14371 if (error != 0) { 14372 vn_finished_write(mp); 14373 ACQUIRE_LOCK(ump); 14374 return; 14375 } 14376 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 14377 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP)) != 0) { 14378 softdep_error("clear_inodedeps: vget", error); 14379 vfs_unbusy(mp); 14380 vn_finished_write(mp); 14381 ACQUIRE_LOCK(ump); 14382 return; 14383 } 14384 vfs_unbusy(mp); 14385 if (VTOI(vp)->i_mode == 0) { 14386 vgone(vp); 14387 } else if (ino == lastino) { 14388 do { 14389 error = ffs_syncvnode(vp, MNT_WAIT, 0); 14390 } while (error == ERELOOKUP); 14391 if (error != 0) 14392 softdep_error("clear_inodedeps: fsync1", error); 14393 } else { 14394 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 14395 softdep_error("clear_inodedeps: fsync2", error); 14396 BO_LOCK(&vp->v_bufobj); 14397 drain_output(vp); 14398 BO_UNLOCK(&vp->v_bufobj); 14399 } 14400 vput(vp); 14401 vn_finished_write(mp); 14402 ACQUIRE_LOCK(ump); 14403 } 14404 } 14405 14406 void 14407 softdep_buf_append(bp, wkhd) 14408 struct buf *bp; 14409 struct workhead *wkhd; 14410 { 14411 struct worklist *wk; 14412 struct ufsmount *ump; 14413 14414 if ((wk = LIST_FIRST(wkhd)) == NULL) 14415 return; 14416 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 14417 ("softdep_buf_append called on non-softdep filesystem")); 14418 ump = VFSTOUFS(wk->wk_mp); 14419 ACQUIRE_LOCK(ump); 14420 while ((wk = LIST_FIRST(wkhd)) != NULL) { 14421 WORKLIST_REMOVE(wk); 14422 WORKLIST_INSERT(&bp->b_dep, wk); 14423 } 14424 FREE_LOCK(ump); 14425 14426 } 14427 14428 void 14429 softdep_inode_append(ip, cred, wkhd) 14430 struct inode *ip; 14431 struct ucred *cred; 14432 struct workhead *wkhd; 14433 { 14434 struct buf *bp; 14435 struct fs *fs; 14436 struct ufsmount *ump; 14437 int error; 14438 14439 ump = ITOUMP(ip); 14440 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 14441 ("softdep_inode_append called on non-softdep filesystem")); 14442 fs = ump->um_fs; 14443 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 14444 (int)fs->fs_bsize, cred, &bp); 14445 if (error) { 14446 bqrelse(bp); 14447 softdep_freework(wkhd); 14448 return; 14449 } 14450 softdep_buf_append(bp, wkhd); 14451 bqrelse(bp); 14452 } 14453 14454 void 14455 softdep_freework(wkhd) 14456 struct workhead *wkhd; 14457 { 14458 struct worklist *wk; 14459 struct ufsmount *ump; 14460 14461 if ((wk = LIST_FIRST(wkhd)) == NULL) 14462 return; 14463 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 14464 ("softdep_freework called on non-softdep filesystem")); 14465 ump = VFSTOUFS(wk->wk_mp); 14466 ACQUIRE_LOCK(ump); 14467 handle_jwork(wkhd); 14468 FREE_LOCK(ump); 14469 } 14470 14471 static struct ufsmount * 14472 softdep_bp_to_mp(bp) 14473 struct buf *bp; 14474 { 14475 struct mount *mp; 14476 struct vnode *vp; 14477 14478 if (LIST_EMPTY(&bp->b_dep)) 14479 return (NULL); 14480 vp = bp->b_vp; 14481 KASSERT(vp != NULL, 14482 ("%s, buffer with dependencies lacks vnode", __func__)); 14483 14484 /* 14485 * The ump mount point is stable after we get a correct 14486 * pointer, since bp is locked and this prevents unmount from 14487 * proceeding. But to get to it, we cannot dereference bp->b_dep 14488 * head wk_mp, because we do not yet own SU ump lock and 14489 * workitem might be freed while dereferenced. 14490 */ 14491 retry: 14492 switch (vp->v_type) { 14493 case VCHR: 14494 VI_LOCK(vp); 14495 mp = vp->v_type == VCHR ? vp->v_rdev->si_mountpt : NULL; 14496 VI_UNLOCK(vp); 14497 if (mp == NULL) 14498 goto retry; 14499 break; 14500 case VREG: 14501 case VDIR: 14502 case VLNK: 14503 case VFIFO: 14504 case VSOCK: 14505 mp = vp->v_mount; 14506 break; 14507 case VBLK: 14508 vn_printf(vp, "softdep_bp_to_mp: unexpected block device\n"); 14509 /* FALLTHROUGH */ 14510 case VNON: 14511 case VBAD: 14512 case VMARKER: 14513 mp = NULL; 14514 break; 14515 default: 14516 vn_printf(vp, "unknown vnode type"); 14517 mp = NULL; 14518 break; 14519 } 14520 return (VFSTOUFS(mp)); 14521 } 14522 14523 /* 14524 * Function to determine if the buffer has outstanding dependencies 14525 * that will cause a roll-back if the buffer is written. If wantcount 14526 * is set, return number of dependencies, otherwise just yes or no. 14527 */ 14528 static int 14529 softdep_count_dependencies(bp, wantcount) 14530 struct buf *bp; 14531 int wantcount; 14532 { 14533 struct worklist *wk; 14534 struct ufsmount *ump; 14535 struct bmsafemap *bmsafemap; 14536 struct freework *freework; 14537 struct inodedep *inodedep; 14538 struct indirdep *indirdep; 14539 struct freeblks *freeblks; 14540 struct allocindir *aip; 14541 struct pagedep *pagedep; 14542 struct dirrem *dirrem; 14543 struct newblk *newblk; 14544 struct mkdir *mkdir; 14545 struct diradd *dap; 14546 int i, retval; 14547 14548 ump = softdep_bp_to_mp(bp); 14549 if (ump == NULL) 14550 return (0); 14551 retval = 0; 14552 ACQUIRE_LOCK(ump); 14553 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 14554 switch (wk->wk_type) { 14555 case D_INODEDEP: 14556 inodedep = WK_INODEDEP(wk); 14557 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 14558 /* bitmap allocation dependency */ 14559 retval += 1; 14560 if (!wantcount) 14561 goto out; 14562 } 14563 if (TAILQ_FIRST(&inodedep->id_inoupdt)) { 14564 /* direct block pointer dependency */ 14565 retval += 1; 14566 if (!wantcount) 14567 goto out; 14568 } 14569 if (TAILQ_FIRST(&inodedep->id_extupdt)) { 14570 /* direct block pointer dependency */ 14571 retval += 1; 14572 if (!wantcount) 14573 goto out; 14574 } 14575 if (TAILQ_FIRST(&inodedep->id_inoreflst)) { 14576 /* Add reference dependency. */ 14577 retval += 1; 14578 if (!wantcount) 14579 goto out; 14580 } 14581 continue; 14582 14583 case D_INDIRDEP: 14584 indirdep = WK_INDIRDEP(wk); 14585 14586 TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) { 14587 /* indirect truncation dependency */ 14588 retval += 1; 14589 if (!wantcount) 14590 goto out; 14591 } 14592 14593 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 14594 /* indirect block pointer dependency */ 14595 retval += 1; 14596 if (!wantcount) 14597 goto out; 14598 } 14599 continue; 14600 14601 case D_PAGEDEP: 14602 pagedep = WK_PAGEDEP(wk); 14603 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) { 14604 if (LIST_FIRST(&dirrem->dm_jremrefhd)) { 14605 /* Journal remove ref dependency. */ 14606 retval += 1; 14607 if (!wantcount) 14608 goto out; 14609 } 14610 } 14611 for (i = 0; i < DAHASHSZ; i++) { 14612 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 14613 /* directory entry dependency */ 14614 retval += 1; 14615 if (!wantcount) 14616 goto out; 14617 } 14618 } 14619 continue; 14620 14621 case D_BMSAFEMAP: 14622 bmsafemap = WK_BMSAFEMAP(wk); 14623 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) { 14624 /* Add reference dependency. */ 14625 retval += 1; 14626 if (!wantcount) 14627 goto out; 14628 } 14629 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) { 14630 /* Allocate block dependency. */ 14631 retval += 1; 14632 if (!wantcount) 14633 goto out; 14634 } 14635 continue; 14636 14637 case D_FREEBLKS: 14638 freeblks = WK_FREEBLKS(wk); 14639 if (LIST_FIRST(&freeblks->fb_jblkdephd)) { 14640 /* Freeblk journal dependency. */ 14641 retval += 1; 14642 if (!wantcount) 14643 goto out; 14644 } 14645 continue; 14646 14647 case D_ALLOCDIRECT: 14648 case D_ALLOCINDIR: 14649 newblk = WK_NEWBLK(wk); 14650 if (newblk->nb_jnewblk) { 14651 /* Journal allocate dependency. */ 14652 retval += 1; 14653 if (!wantcount) 14654 goto out; 14655 } 14656 continue; 14657 14658 case D_MKDIR: 14659 mkdir = WK_MKDIR(wk); 14660 if (mkdir->md_jaddref) { 14661 /* Journal reference dependency. */ 14662 retval += 1; 14663 if (!wantcount) 14664 goto out; 14665 } 14666 continue; 14667 14668 case D_FREEWORK: 14669 case D_FREEDEP: 14670 case D_JSEGDEP: 14671 case D_JSEG: 14672 case D_SBDEP: 14673 /* never a dependency on these blocks */ 14674 continue; 14675 14676 default: 14677 panic("softdep_count_dependencies: Unexpected type %s", 14678 TYPENAME(wk->wk_type)); 14679 /* NOTREACHED */ 14680 } 14681 } 14682 out: 14683 FREE_LOCK(ump); 14684 return (retval); 14685 } 14686 14687 /* 14688 * Acquire exclusive access to a buffer. 14689 * Must be called with a locked mtx parameter. 14690 * Return acquired buffer or NULL on failure. 14691 */ 14692 static struct buf * 14693 getdirtybuf(bp, lock, waitfor) 14694 struct buf *bp; 14695 struct rwlock *lock; 14696 int waitfor; 14697 { 14698 int error; 14699 14700 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) { 14701 if (waitfor != MNT_WAIT) 14702 return (NULL); 14703 error = BUF_LOCK(bp, 14704 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock); 14705 /* 14706 * Even if we successfully acquire bp here, we have dropped 14707 * lock, which may violates our guarantee. 14708 */ 14709 if (error == 0) 14710 BUF_UNLOCK(bp); 14711 else if (error != ENOLCK) 14712 panic("getdirtybuf: inconsistent lock: %d", error); 14713 rw_wlock(lock); 14714 return (NULL); 14715 } 14716 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 14717 if (lock != BO_LOCKPTR(bp->b_bufobj) && waitfor == MNT_WAIT) { 14718 rw_wunlock(lock); 14719 BO_LOCK(bp->b_bufobj); 14720 BUF_UNLOCK(bp); 14721 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 14722 bp->b_vflags |= BV_BKGRDWAIT; 14723 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), 14724 PRIBIO | PDROP, "getbuf", 0); 14725 } else 14726 BO_UNLOCK(bp->b_bufobj); 14727 rw_wlock(lock); 14728 return (NULL); 14729 } 14730 BUF_UNLOCK(bp); 14731 if (waitfor != MNT_WAIT) 14732 return (NULL); 14733 #ifdef DEBUG_VFS_LOCKS 14734 if (bp->b_vp->v_type != VCHR) 14735 ASSERT_BO_WLOCKED(bp->b_bufobj); 14736 #endif 14737 bp->b_vflags |= BV_BKGRDWAIT; 14738 rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0); 14739 return (NULL); 14740 } 14741 if ((bp->b_flags & B_DELWRI) == 0) { 14742 BUF_UNLOCK(bp); 14743 return (NULL); 14744 } 14745 bremfree(bp); 14746 return (bp); 14747 } 14748 14749 /* 14750 * Check if it is safe to suspend the file system now. On entry, 14751 * the vnode interlock for devvp should be held. Return 0 with 14752 * the mount interlock held if the file system can be suspended now, 14753 * otherwise return EAGAIN with the mount interlock held. 14754 */ 14755 int 14756 softdep_check_suspend(struct mount *mp, 14757 struct vnode *devvp, 14758 int softdep_depcnt, 14759 int softdep_accdepcnt, 14760 int secondary_writes, 14761 int secondary_accwrites) 14762 { 14763 struct bufobj *bo; 14764 struct ufsmount *ump; 14765 struct inodedep *inodedep; 14766 int error, unlinked; 14767 14768 bo = &devvp->v_bufobj; 14769 ASSERT_BO_WLOCKED(bo); 14770 14771 /* 14772 * If we are not running with soft updates, then we need only 14773 * deal with secondary writes as we try to suspend. 14774 */ 14775 if (MOUNTEDSOFTDEP(mp) == 0) { 14776 MNT_ILOCK(mp); 14777 while (mp->mnt_secondary_writes != 0) { 14778 BO_UNLOCK(bo); 14779 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 14780 (PUSER - 1) | PDROP, "secwr", 0); 14781 BO_LOCK(bo); 14782 MNT_ILOCK(mp); 14783 } 14784 14785 /* 14786 * Reasons for needing more work before suspend: 14787 * - Dirty buffers on devvp. 14788 * - Secondary writes occurred after start of vnode sync loop 14789 */ 14790 error = 0; 14791 if (bo->bo_numoutput > 0 || 14792 bo->bo_dirty.bv_cnt > 0 || 14793 secondary_writes != 0 || 14794 mp->mnt_secondary_writes != 0 || 14795 secondary_accwrites != mp->mnt_secondary_accwrites) 14796 error = EAGAIN; 14797 BO_UNLOCK(bo); 14798 return (error); 14799 } 14800 14801 /* 14802 * If we are running with soft updates, then we need to coordinate 14803 * with them as we try to suspend. 14804 */ 14805 ump = VFSTOUFS(mp); 14806 for (;;) { 14807 if (!TRY_ACQUIRE_LOCK(ump)) { 14808 BO_UNLOCK(bo); 14809 ACQUIRE_LOCK(ump); 14810 FREE_LOCK(ump); 14811 BO_LOCK(bo); 14812 continue; 14813 } 14814 MNT_ILOCK(mp); 14815 if (mp->mnt_secondary_writes != 0) { 14816 FREE_LOCK(ump); 14817 BO_UNLOCK(bo); 14818 msleep(&mp->mnt_secondary_writes, 14819 MNT_MTX(mp), 14820 (PUSER - 1) | PDROP, "secwr", 0); 14821 BO_LOCK(bo); 14822 continue; 14823 } 14824 break; 14825 } 14826 14827 unlinked = 0; 14828 if (MOUNTEDSUJ(mp)) { 14829 for (inodedep = TAILQ_FIRST(&ump->softdep_unlinked); 14830 inodedep != NULL; 14831 inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 14832 if ((inodedep->id_state & (UNLINKED | UNLINKLINKS | 14833 UNLINKONLIST)) != (UNLINKED | UNLINKLINKS | 14834 UNLINKONLIST) || 14835 !check_inodedep_free(inodedep)) 14836 continue; 14837 unlinked++; 14838 } 14839 } 14840 14841 /* 14842 * Reasons for needing more work before suspend: 14843 * - Dirty buffers on devvp. 14844 * - Softdep activity occurred after start of vnode sync loop 14845 * - Secondary writes occurred after start of vnode sync loop 14846 */ 14847 error = 0; 14848 if (bo->bo_numoutput > 0 || 14849 bo->bo_dirty.bv_cnt > 0 || 14850 softdep_depcnt != unlinked || 14851 ump->softdep_deps != unlinked || 14852 softdep_accdepcnt != ump->softdep_accdeps || 14853 secondary_writes != 0 || 14854 mp->mnt_secondary_writes != 0 || 14855 secondary_accwrites != mp->mnt_secondary_accwrites) 14856 error = EAGAIN; 14857 FREE_LOCK(ump); 14858 BO_UNLOCK(bo); 14859 return (error); 14860 } 14861 14862 /* 14863 * Get the number of dependency structures for the file system, both 14864 * the current number and the total number allocated. These will 14865 * later be used to detect that softdep processing has occurred. 14866 */ 14867 void 14868 softdep_get_depcounts(struct mount *mp, 14869 int *softdep_depsp, 14870 int *softdep_accdepsp) 14871 { 14872 struct ufsmount *ump; 14873 14874 if (MOUNTEDSOFTDEP(mp) == 0) { 14875 *softdep_depsp = 0; 14876 *softdep_accdepsp = 0; 14877 return; 14878 } 14879 ump = VFSTOUFS(mp); 14880 ACQUIRE_LOCK(ump); 14881 *softdep_depsp = ump->softdep_deps; 14882 *softdep_accdepsp = ump->softdep_accdeps; 14883 FREE_LOCK(ump); 14884 } 14885 14886 /* 14887 * Wait for pending output on a vnode to complete. 14888 */ 14889 static void 14890 drain_output(vp) 14891 struct vnode *vp; 14892 { 14893 14894 ASSERT_VOP_LOCKED(vp, "drain_output"); 14895 (void)bufobj_wwait(&vp->v_bufobj, 0, 0); 14896 } 14897 14898 /* 14899 * Called whenever a buffer that is being invalidated or reallocated 14900 * contains dependencies. This should only happen if an I/O error has 14901 * occurred. The routine is called with the buffer locked. 14902 */ 14903 static void 14904 softdep_deallocate_dependencies(bp) 14905 struct buf *bp; 14906 { 14907 14908 if ((bp->b_ioflags & BIO_ERROR) == 0) 14909 panic("softdep_deallocate_dependencies: dangling deps"); 14910 if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL) 14911 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error); 14912 else 14913 printf("softdep_deallocate_dependencies: " 14914 "got error %d while accessing filesystem\n", bp->b_error); 14915 if (bp->b_error != ENXIO) 14916 panic("softdep_deallocate_dependencies: unrecovered I/O error"); 14917 } 14918 14919 /* 14920 * Function to handle asynchronous write errors in the filesystem. 14921 */ 14922 static void 14923 softdep_error(func, error) 14924 char *func; 14925 int error; 14926 { 14927 14928 /* XXX should do something better! */ 14929 printf("%s: got error %d while accessing filesystem\n", func, error); 14930 } 14931 14932 #ifdef DDB 14933 14934 /* exported to ffs_vfsops.c */ 14935 extern void db_print_ffs(struct ufsmount *ump); 14936 void 14937 db_print_ffs(struct ufsmount *ump) 14938 { 14939 db_printf("mp %p (%s) devvp %p\n", ump->um_mountp, 14940 ump->um_mountp->mnt_stat.f_mntonname, ump->um_devvp); 14941 db_printf(" fs %p su_wl %d su_deps %d su_req %d\n", 14942 ump->um_fs, ump->softdep_on_worklist, 14943 ump->softdep_deps, ump->softdep_req); 14944 } 14945 14946 static void 14947 worklist_print(struct worklist *wk, int verbose) 14948 { 14949 14950 if (!verbose) { 14951 db_printf("%s: %p state 0x%b\n", TYPENAME(wk->wk_type), wk, 14952 (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS); 14953 return; 14954 } 14955 db_printf("worklist: %p type %s state 0x%b next %p\n ", wk, 14956 TYPENAME(wk->wk_type), (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS, 14957 LIST_NEXT(wk, wk_list)); 14958 db_print_ffs(VFSTOUFS(wk->wk_mp)); 14959 } 14960 14961 static void 14962 inodedep_print(struct inodedep *inodedep, int verbose) 14963 { 14964 14965 worklist_print(&inodedep->id_list, 0); 14966 db_printf(" fs %p ino %jd inoblk %jd delta %jd nlink %jd\n", 14967 inodedep->id_fs, 14968 (intmax_t)inodedep->id_ino, 14969 (intmax_t)fsbtodb(inodedep->id_fs, 14970 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)), 14971 (intmax_t)inodedep->id_nlinkdelta, 14972 (intmax_t)inodedep->id_savednlink); 14973 14974 if (verbose == 0) 14975 return; 14976 14977 db_printf(" bmsafemap %p, mkdiradd %p, inoreflst %p\n", 14978 inodedep->id_bmsafemap, 14979 inodedep->id_mkdiradd, 14980 TAILQ_FIRST(&inodedep->id_inoreflst)); 14981 db_printf(" dirremhd %p, pendinghd %p, bufwait %p\n", 14982 LIST_FIRST(&inodedep->id_dirremhd), 14983 LIST_FIRST(&inodedep->id_pendinghd), 14984 LIST_FIRST(&inodedep->id_bufwait)); 14985 db_printf(" inowait %p, inoupdt %p, newinoupdt %p\n", 14986 LIST_FIRST(&inodedep->id_inowait), 14987 TAILQ_FIRST(&inodedep->id_inoupdt), 14988 TAILQ_FIRST(&inodedep->id_newinoupdt)); 14989 db_printf(" extupdt %p, newextupdt %p, freeblklst %p\n", 14990 TAILQ_FIRST(&inodedep->id_extupdt), 14991 TAILQ_FIRST(&inodedep->id_newextupdt), 14992 TAILQ_FIRST(&inodedep->id_freeblklst)); 14993 db_printf(" saveino %p, savedsize %jd, savedextsize %jd\n", 14994 inodedep->id_savedino1, 14995 (intmax_t)inodedep->id_savedsize, 14996 (intmax_t)inodedep->id_savedextsize); 14997 } 14998 14999 static void 15000 newblk_print(struct newblk *nbp) 15001 { 15002 15003 worklist_print(&nbp->nb_list, 0); 15004 db_printf(" newblkno %jd\n", (intmax_t)nbp->nb_newblkno); 15005 db_printf(" jnewblk %p, bmsafemap %p, freefrag %p\n", 15006 &nbp->nb_jnewblk, 15007 &nbp->nb_bmsafemap, 15008 &nbp->nb_freefrag); 15009 db_printf(" indirdeps %p, newdirblk %p, jwork %p\n", 15010 LIST_FIRST(&nbp->nb_indirdeps), 15011 LIST_FIRST(&nbp->nb_newdirblk), 15012 LIST_FIRST(&nbp->nb_jwork)); 15013 } 15014 15015 static void 15016 allocdirect_print(struct allocdirect *adp) 15017 { 15018 15019 newblk_print(&adp->ad_block); 15020 db_printf(" oldblkno %jd, oldsize %ld, newsize %ld\n", 15021 adp->ad_oldblkno, adp->ad_oldsize, adp->ad_newsize); 15022 db_printf(" offset %d, inodedep %p\n", 15023 adp->ad_offset, adp->ad_inodedep); 15024 } 15025 15026 static void 15027 allocindir_print(struct allocindir *aip) 15028 { 15029 15030 newblk_print(&aip->ai_block); 15031 db_printf(" oldblkno %jd, lbn %jd\n", 15032 (intmax_t)aip->ai_oldblkno, (intmax_t)aip->ai_lbn); 15033 db_printf(" offset %d, indirdep %p\n", 15034 aip->ai_offset, aip->ai_indirdep); 15035 } 15036 15037 static void 15038 mkdir_print(struct mkdir *mkdir) 15039 { 15040 15041 worklist_print(&mkdir->md_list, 0); 15042 db_printf(" diradd %p, jaddref %p, buf %p\n", 15043 mkdir->md_diradd, mkdir->md_jaddref, mkdir->md_buf); 15044 } 15045 15046 DB_SHOW_COMMAND(sd_inodedep, db_show_sd_inodedep) 15047 { 15048 15049 if (have_addr == 0) { 15050 db_printf("inodedep address required\n"); 15051 return; 15052 } 15053 inodedep_print((struct inodedep*)addr, 1); 15054 } 15055 15056 DB_SHOW_COMMAND(sd_allinodedeps, db_show_sd_allinodedeps) 15057 { 15058 struct inodedep_hashhead *inodedephd; 15059 struct inodedep *inodedep; 15060 struct ufsmount *ump; 15061 int cnt; 15062 15063 if (have_addr == 0) { 15064 db_printf("ufsmount address required\n"); 15065 return; 15066 } 15067 ump = (struct ufsmount *)addr; 15068 for (cnt = 0; cnt < ump->inodedep_hash_size; cnt++) { 15069 inodedephd = &ump->inodedep_hashtbl[cnt]; 15070 LIST_FOREACH(inodedep, inodedephd, id_hash) { 15071 inodedep_print(inodedep, 0); 15072 } 15073 } 15074 } 15075 15076 DB_SHOW_COMMAND(sd_worklist, db_show_sd_worklist) 15077 { 15078 15079 if (have_addr == 0) { 15080 db_printf("worklist address required\n"); 15081 return; 15082 } 15083 worklist_print((struct worklist *)addr, 1); 15084 } 15085 15086 DB_SHOW_COMMAND(sd_workhead, db_show_sd_workhead) 15087 { 15088 struct worklist *wk; 15089 struct workhead *wkhd; 15090 15091 if (have_addr == 0) { 15092 db_printf("worklist address required " 15093 "(for example value in bp->b_dep)\n"); 15094 return; 15095 } 15096 /* 15097 * We often do not have the address of the worklist head but 15098 * instead a pointer to its first entry (e.g., we have the 15099 * contents of bp->b_dep rather than &bp->b_dep). But the back 15100 * pointer of bp->b_dep will point at the head of the list, so 15101 * we cheat and use that instead. If we are in the middle of 15102 * a list we will still get the same result, so nothing 15103 * unexpected will result. 15104 */ 15105 wk = (struct worklist *)addr; 15106 if (wk == NULL) 15107 return; 15108 wkhd = (struct workhead *)wk->wk_list.le_prev; 15109 LIST_FOREACH(wk, wkhd, wk_list) { 15110 switch(wk->wk_type) { 15111 case D_INODEDEP: 15112 inodedep_print(WK_INODEDEP(wk), 0); 15113 continue; 15114 case D_ALLOCDIRECT: 15115 allocdirect_print(WK_ALLOCDIRECT(wk)); 15116 continue; 15117 case D_ALLOCINDIR: 15118 allocindir_print(WK_ALLOCINDIR(wk)); 15119 continue; 15120 case D_MKDIR: 15121 mkdir_print(WK_MKDIR(wk)); 15122 continue; 15123 default: 15124 worklist_print(wk, 0); 15125 continue; 15126 } 15127 } 15128 } 15129 15130 DB_SHOW_COMMAND(sd_mkdir, db_show_sd_mkdir) 15131 { 15132 if (have_addr == 0) { 15133 db_printf("mkdir address required\n"); 15134 return; 15135 } 15136 mkdir_print((struct mkdir *)addr); 15137 } 15138 15139 DB_SHOW_COMMAND(sd_mkdir_list, db_show_sd_mkdir_list) 15140 { 15141 struct mkdirlist *mkdirlisthd; 15142 struct mkdir *mkdir; 15143 15144 if (have_addr == 0) { 15145 db_printf("mkdir listhead address required\n"); 15146 return; 15147 } 15148 mkdirlisthd = (struct mkdirlist *)addr; 15149 LIST_FOREACH(mkdir, mkdirlisthd, md_mkdirs) { 15150 mkdir_print(mkdir); 15151 if (mkdir->md_diradd != NULL) { 15152 db_printf(" "); 15153 worklist_print(&mkdir->md_diradd->da_list, 0); 15154 } 15155 if (mkdir->md_jaddref != NULL) { 15156 db_printf(" "); 15157 worklist_print(&mkdir->md_jaddref->ja_list, 0); 15158 } 15159 } 15160 } 15161 15162 DB_SHOW_COMMAND(sd_allocdirect, db_show_sd_allocdirect) 15163 { 15164 if (have_addr == 0) { 15165 db_printf("allocdirect address required\n"); 15166 return; 15167 } 15168 allocdirect_print((struct allocdirect *)addr); 15169 } 15170 15171 DB_SHOW_COMMAND(sd_allocindir, db_show_sd_allocindir) 15172 { 15173 if (have_addr == 0) { 15174 db_printf("allocindir address required\n"); 15175 return; 15176 } 15177 allocindir_print((struct allocindir *)addr); 15178 } 15179 15180 #endif /* DDB */ 15181 15182 #endif /* SOFTUPDATES */ 15183