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 BO_LOCK(bo); 7550 bp->b_vflags |= BV_SCANNED; 7551 BO_UNLOCK(bo); 7552 bremfree(bp); 7553 if (blkoff != 0) { 7554 allocbuf(bp, blkoff); 7555 bqrelse(bp); 7556 } else { 7557 bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF; 7558 brelse(bp); 7559 } 7560 BO_LOCK(bo); 7561 goto cleanrestart; 7562 } 7563 drain_output(vp); 7564 BO_UNLOCK(bo); 7565 } 7566 7567 static int 7568 cancel_pagedep(pagedep, freeblks, blkoff) 7569 struct pagedep *pagedep; 7570 struct freeblks *freeblks; 7571 int blkoff; 7572 { 7573 struct jremref *jremref; 7574 struct jmvref *jmvref; 7575 struct dirrem *dirrem, *tmp; 7576 int i; 7577 7578 /* 7579 * Copy any directory remove dependencies to the list 7580 * to be processed after the freeblks proceeds. If 7581 * directory entry never made it to disk they 7582 * can be dumped directly onto the work list. 7583 */ 7584 LIST_FOREACH_SAFE(dirrem, &pagedep->pd_dirremhd, dm_next, tmp) { 7585 /* Skip this directory removal if it is intended to remain. */ 7586 if (dirrem->dm_offset < blkoff) 7587 continue; 7588 /* 7589 * If there are any dirrems we wait for the journal write 7590 * to complete and then restart the buf scan as the lock 7591 * has been dropped. 7592 */ 7593 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) { 7594 jwait(&jremref->jr_list, MNT_WAIT); 7595 return (ERESTART); 7596 } 7597 LIST_REMOVE(dirrem, dm_next); 7598 dirrem->dm_dirinum = pagedep->pd_ino; 7599 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list); 7600 } 7601 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) { 7602 jwait(&jmvref->jm_list, MNT_WAIT); 7603 return (ERESTART); 7604 } 7605 /* 7606 * When we're partially truncating a pagedep we just want to flush 7607 * journal entries and return. There can not be any adds in the 7608 * truncated portion of the directory and newblk must remain if 7609 * part of the block remains. 7610 */ 7611 if (blkoff != 0) { 7612 struct diradd *dap; 7613 7614 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 7615 if (dap->da_offset > blkoff) 7616 panic("cancel_pagedep: diradd %p off %d > %d", 7617 dap, dap->da_offset, blkoff); 7618 for (i = 0; i < DAHASHSZ; i++) 7619 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) 7620 if (dap->da_offset > blkoff) 7621 panic("cancel_pagedep: diradd %p off %d > %d", 7622 dap, dap->da_offset, blkoff); 7623 return (0); 7624 } 7625 /* 7626 * There should be no directory add dependencies present 7627 * as the directory could not be truncated until all 7628 * children were removed. 7629 */ 7630 KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL, 7631 ("deallocate_dependencies: pendinghd != NULL")); 7632 for (i = 0; i < DAHASHSZ; i++) 7633 KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL, 7634 ("deallocate_dependencies: diraddhd != NULL")); 7635 if ((pagedep->pd_state & NEWBLOCK) != 0) 7636 free_newdirblk(pagedep->pd_newdirblk); 7637 if (free_pagedep(pagedep) == 0) 7638 panic("Failed to free pagedep %p", pagedep); 7639 return (0); 7640 } 7641 7642 /* 7643 * Reclaim any dependency structures from a buffer that is about to 7644 * be reallocated to a new vnode. The buffer must be locked, thus, 7645 * no I/O completion operations can occur while we are manipulating 7646 * its associated dependencies. The mutex is held so that other I/O's 7647 * associated with related dependencies do not occur. 7648 */ 7649 static int 7650 deallocate_dependencies(bp, freeblks, off) 7651 struct buf *bp; 7652 struct freeblks *freeblks; 7653 int off; 7654 { 7655 struct indirdep *indirdep; 7656 struct pagedep *pagedep; 7657 struct worklist *wk, *wkn; 7658 struct ufsmount *ump; 7659 7660 ump = softdep_bp_to_mp(bp); 7661 if (ump == NULL) 7662 goto done; 7663 ACQUIRE_LOCK(ump); 7664 LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) { 7665 switch (wk->wk_type) { 7666 case D_INDIRDEP: 7667 indirdep = WK_INDIRDEP(wk); 7668 if (bp->b_lblkno >= 0 || 7669 bp->b_blkno != indirdep->ir_savebp->b_lblkno) 7670 panic("deallocate_dependencies: not indir"); 7671 cancel_indirdep(indirdep, bp, freeblks); 7672 continue; 7673 7674 case D_PAGEDEP: 7675 pagedep = WK_PAGEDEP(wk); 7676 if (cancel_pagedep(pagedep, freeblks, off)) { 7677 FREE_LOCK(ump); 7678 return (ERESTART); 7679 } 7680 continue; 7681 7682 case D_ALLOCINDIR: 7683 /* 7684 * Simply remove the allocindir, we'll find it via 7685 * the indirdep where we can clear pointers if 7686 * needed. 7687 */ 7688 WORKLIST_REMOVE(wk); 7689 continue; 7690 7691 case D_FREEWORK: 7692 /* 7693 * A truncation is waiting for the zero'd pointers 7694 * to be written. It can be freed when the freeblks 7695 * is journaled. 7696 */ 7697 WORKLIST_REMOVE(wk); 7698 wk->wk_state |= ONDEPLIST; 7699 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 7700 break; 7701 7702 case D_ALLOCDIRECT: 7703 if (off != 0) 7704 continue; 7705 /* FALLTHROUGH */ 7706 default: 7707 panic("deallocate_dependencies: Unexpected type %s", 7708 TYPENAME(wk->wk_type)); 7709 /* NOTREACHED */ 7710 } 7711 } 7712 FREE_LOCK(ump); 7713 done: 7714 /* 7715 * Don't throw away this buf, we were partially truncating and 7716 * some deps may always remain. 7717 */ 7718 if (off) { 7719 allocbuf(bp, off); 7720 bp->b_vflags |= BV_SCANNED; 7721 return (EBUSY); 7722 } 7723 bp->b_flags |= B_INVAL | B_NOCACHE; 7724 7725 return (0); 7726 } 7727 7728 /* 7729 * An allocdirect is being canceled due to a truncate. We must make sure 7730 * the journal entry is released in concert with the blkfree that releases 7731 * the storage. Completed journal entries must not be released until the 7732 * space is no longer pointed to by the inode or in the bitmap. 7733 */ 7734 static void 7735 cancel_allocdirect(adphead, adp, freeblks) 7736 struct allocdirectlst *adphead; 7737 struct allocdirect *adp; 7738 struct freeblks *freeblks; 7739 { 7740 struct freework *freework; 7741 struct newblk *newblk; 7742 struct worklist *wk; 7743 7744 TAILQ_REMOVE(adphead, adp, ad_next); 7745 newblk = (struct newblk *)adp; 7746 freework = NULL; 7747 /* 7748 * Find the correct freework structure. 7749 */ 7750 LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) { 7751 if (wk->wk_type != D_FREEWORK) 7752 continue; 7753 freework = WK_FREEWORK(wk); 7754 if (freework->fw_blkno == newblk->nb_newblkno) 7755 break; 7756 } 7757 if (freework == NULL) 7758 panic("cancel_allocdirect: Freework not found"); 7759 /* 7760 * If a newblk exists at all we still have the journal entry that 7761 * initiated the allocation so we do not need to journal the free. 7762 */ 7763 cancel_jfreeblk(freeblks, freework->fw_blkno); 7764 /* 7765 * If the journal hasn't been written the jnewblk must be passed 7766 * to the call to ffs_blkfree that reclaims the space. We accomplish 7767 * this by linking the journal dependency into the freework to be 7768 * freed when freework_freeblock() is called. If the journal has 7769 * been written we can simply reclaim the journal space when the 7770 * freeblks work is complete. 7771 */ 7772 freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list, 7773 &freeblks->fb_jwork); 7774 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 7775 } 7776 7777 /* 7778 * Cancel a new block allocation. May be an indirect or direct block. We 7779 * remove it from various lists and return any journal record that needs to 7780 * be resolved by the caller. 7781 * 7782 * A special consideration is made for indirects which were never pointed 7783 * at on disk and will never be found once this block is released. 7784 */ 7785 static struct jnewblk * 7786 cancel_newblk(newblk, wk, wkhd) 7787 struct newblk *newblk; 7788 struct worklist *wk; 7789 struct workhead *wkhd; 7790 { 7791 struct jnewblk *jnewblk; 7792 7793 CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno); 7794 7795 newblk->nb_state |= GOINGAWAY; 7796 /* 7797 * Previously we traversed the completedhd on each indirdep 7798 * attached to this newblk to cancel them and gather journal 7799 * work. Since we need only the oldest journal segment and 7800 * the lowest point on the tree will always have the oldest 7801 * journal segment we are free to release the segments 7802 * of any subordinates and may leave the indirdep list to 7803 * indirdep_complete() when this newblk is freed. 7804 */ 7805 if (newblk->nb_state & ONDEPLIST) { 7806 newblk->nb_state &= ~ONDEPLIST; 7807 LIST_REMOVE(newblk, nb_deps); 7808 } 7809 if (newblk->nb_state & ONWORKLIST) 7810 WORKLIST_REMOVE(&newblk->nb_list); 7811 /* 7812 * If the journal entry hasn't been written we save a pointer to 7813 * the dependency that frees it until it is written or the 7814 * superseding operation completes. 7815 */ 7816 jnewblk = newblk->nb_jnewblk; 7817 if (jnewblk != NULL && wk != NULL) { 7818 newblk->nb_jnewblk = NULL; 7819 jnewblk->jn_dep = wk; 7820 } 7821 if (!LIST_EMPTY(&newblk->nb_jwork)) 7822 jwork_move(wkhd, &newblk->nb_jwork); 7823 /* 7824 * When truncating we must free the newdirblk early to remove 7825 * the pagedep from the hash before returning. 7826 */ 7827 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7828 free_newdirblk(WK_NEWDIRBLK(wk)); 7829 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7830 panic("cancel_newblk: extra newdirblk"); 7831 7832 return (jnewblk); 7833 } 7834 7835 /* 7836 * Schedule the freefrag associated with a newblk to be released once 7837 * the pointers are written and the previous block is no longer needed. 7838 */ 7839 static void 7840 newblk_freefrag(newblk) 7841 struct newblk *newblk; 7842 { 7843 struct freefrag *freefrag; 7844 7845 if (newblk->nb_freefrag == NULL) 7846 return; 7847 freefrag = newblk->nb_freefrag; 7848 newblk->nb_freefrag = NULL; 7849 freefrag->ff_state |= COMPLETE; 7850 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 7851 add_to_worklist(&freefrag->ff_list, 0); 7852 } 7853 7854 /* 7855 * Free a newblk. Generate a new freefrag work request if appropriate. 7856 * This must be called after the inode pointer and any direct block pointers 7857 * are valid or fully removed via truncate or frag extension. 7858 */ 7859 static void 7860 free_newblk(newblk) 7861 struct newblk *newblk; 7862 { 7863 struct indirdep *indirdep; 7864 struct worklist *wk; 7865 7866 KASSERT(newblk->nb_jnewblk == NULL, 7867 ("free_newblk: jnewblk %p still attached", newblk->nb_jnewblk)); 7868 KASSERT(newblk->nb_list.wk_type != D_NEWBLK, 7869 ("free_newblk: unclaimed newblk")); 7870 LOCK_OWNED(VFSTOUFS(newblk->nb_list.wk_mp)); 7871 newblk_freefrag(newblk); 7872 if (newblk->nb_state & ONDEPLIST) 7873 LIST_REMOVE(newblk, nb_deps); 7874 if (newblk->nb_state & ONWORKLIST) 7875 WORKLIST_REMOVE(&newblk->nb_list); 7876 LIST_REMOVE(newblk, nb_hash); 7877 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7878 free_newdirblk(WK_NEWDIRBLK(wk)); 7879 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7880 panic("free_newblk: extra newdirblk"); 7881 while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL) 7882 indirdep_complete(indirdep); 7883 handle_jwork(&newblk->nb_jwork); 7884 WORKITEM_FREE(newblk, D_NEWBLK); 7885 } 7886 7887 /* 7888 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep. 7889 */ 7890 static void 7891 free_newdirblk(newdirblk) 7892 struct newdirblk *newdirblk; 7893 { 7894 struct pagedep *pagedep; 7895 struct diradd *dap; 7896 struct worklist *wk; 7897 7898 LOCK_OWNED(VFSTOUFS(newdirblk->db_list.wk_mp)); 7899 WORKLIST_REMOVE(&newdirblk->db_list); 7900 /* 7901 * If the pagedep is still linked onto the directory buffer 7902 * dependency chain, then some of the entries on the 7903 * pd_pendinghd list may not be committed to disk yet. In 7904 * this case, we will simply clear the NEWBLOCK flag and 7905 * let the pd_pendinghd list be processed when the pagedep 7906 * is next written. If the pagedep is no longer on the buffer 7907 * dependency chain, then all the entries on the pd_pending 7908 * list are committed to disk and we can free them here. 7909 */ 7910 pagedep = newdirblk->db_pagedep; 7911 pagedep->pd_state &= ~NEWBLOCK; 7912 if ((pagedep->pd_state & ONWORKLIST) == 0) { 7913 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 7914 free_diradd(dap, NULL); 7915 /* 7916 * If no dependencies remain, the pagedep will be freed. 7917 */ 7918 free_pagedep(pagedep); 7919 } 7920 /* Should only ever be one item in the list. */ 7921 while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) { 7922 WORKLIST_REMOVE(wk); 7923 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 7924 } 7925 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 7926 } 7927 7928 /* 7929 * Prepare an inode to be freed. The actual free operation is not 7930 * done until the zero'ed inode has been written to disk. 7931 */ 7932 void 7933 softdep_freefile(pvp, ino, mode) 7934 struct vnode *pvp; 7935 ino_t ino; 7936 int mode; 7937 { 7938 struct inode *ip = VTOI(pvp); 7939 struct inodedep *inodedep; 7940 struct freefile *freefile; 7941 struct freeblks *freeblks; 7942 struct ufsmount *ump; 7943 7944 ump = ITOUMP(ip); 7945 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 7946 ("softdep_freefile called on non-softdep filesystem")); 7947 /* 7948 * This sets up the inode de-allocation dependency. 7949 */ 7950 freefile = malloc(sizeof(struct freefile), 7951 M_FREEFILE, M_SOFTDEP_FLAGS); 7952 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount); 7953 freefile->fx_mode = mode; 7954 freefile->fx_oldinum = ino; 7955 freefile->fx_devvp = ump->um_devvp; 7956 LIST_INIT(&freefile->fx_jwork); 7957 UFS_LOCK(ump); 7958 ump->um_fs->fs_pendinginodes += 1; 7959 UFS_UNLOCK(ump); 7960 7961 /* 7962 * If the inodedep does not exist, then the zero'ed inode has 7963 * been written to disk. If the allocated inode has never been 7964 * written to disk, then the on-disk inode is zero'ed. In either 7965 * case we can free the file immediately. If the journal was 7966 * canceled before being written the inode will never make it to 7967 * disk and we must send the canceled journal entrys to 7968 * ffs_freefile() to be cleared in conjunction with the bitmap. 7969 * Any blocks waiting on the inode to write can be safely freed 7970 * here as it will never been written. 7971 */ 7972 ACQUIRE_LOCK(ump); 7973 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7974 if (inodedep) { 7975 /* 7976 * Clear out freeblks that no longer need to reference 7977 * this inode. 7978 */ 7979 while ((freeblks = 7980 TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) { 7981 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, 7982 fb_next); 7983 freeblks->fb_state &= ~ONDEPLIST; 7984 } 7985 /* 7986 * Remove this inode from the unlinked list. 7987 */ 7988 if (inodedep->id_state & UNLINKED) { 7989 /* 7990 * Save the journal work to be freed with the bitmap 7991 * before we clear UNLINKED. Otherwise it can be lost 7992 * if the inode block is written. 7993 */ 7994 handle_bufwait(inodedep, &freefile->fx_jwork); 7995 clear_unlinked_inodedep(inodedep); 7996 /* 7997 * Re-acquire inodedep as we've dropped the 7998 * per-filesystem lock in clear_unlinked_inodedep(). 7999 */ 8000 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 8001 } 8002 } 8003 if (inodedep == NULL || check_inode_unwritten(inodedep)) { 8004 FREE_LOCK(ump); 8005 handle_workitem_freefile(freefile); 8006 return; 8007 } 8008 if ((inodedep->id_state & DEPCOMPLETE) == 0) 8009 inodedep->id_state |= GOINGAWAY; 8010 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list); 8011 FREE_LOCK(ump); 8012 if (ip->i_number == ino) 8013 UFS_INODE_SET_FLAG(ip, IN_MODIFIED); 8014 } 8015 8016 /* 8017 * Check to see if an inode has never been written to disk. If 8018 * so free the inodedep and return success, otherwise return failure. 8019 * 8020 * If we still have a bitmap dependency, then the inode has never 8021 * been written to disk. Drop the dependency as it is no longer 8022 * necessary since the inode is being deallocated. We set the 8023 * ALLCOMPLETE flags since the bitmap now properly shows that the 8024 * inode is not allocated. Even if the inode is actively being 8025 * written, it has been rolled back to its zero'ed state, so we 8026 * are ensured that a zero inode is what is on the disk. For short 8027 * lived files, this change will usually result in removing all the 8028 * dependencies from the inode so that it can be freed immediately. 8029 */ 8030 static int 8031 check_inode_unwritten(inodedep) 8032 struct inodedep *inodedep; 8033 { 8034 8035 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 8036 8037 if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 || 8038 !LIST_EMPTY(&inodedep->id_dirremhd) || 8039 !LIST_EMPTY(&inodedep->id_pendinghd) || 8040 !LIST_EMPTY(&inodedep->id_bufwait) || 8041 !LIST_EMPTY(&inodedep->id_inowait) || 8042 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 8043 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 8044 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 8045 !TAILQ_EMPTY(&inodedep->id_extupdt) || 8046 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 8047 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 8048 inodedep->id_mkdiradd != NULL || 8049 inodedep->id_nlinkdelta != 0) 8050 return (0); 8051 /* 8052 * Another process might be in initiate_write_inodeblock_ufs[12] 8053 * trying to allocate memory without holding "Softdep Lock". 8054 */ 8055 if ((inodedep->id_state & IOSTARTED) != 0 && 8056 inodedep->id_savedino1 == NULL) 8057 return (0); 8058 8059 if (inodedep->id_state & ONDEPLIST) 8060 LIST_REMOVE(inodedep, id_deps); 8061 inodedep->id_state &= ~ONDEPLIST; 8062 inodedep->id_state |= ALLCOMPLETE; 8063 inodedep->id_bmsafemap = NULL; 8064 if (inodedep->id_state & ONWORKLIST) 8065 WORKLIST_REMOVE(&inodedep->id_list); 8066 if (inodedep->id_savedino1 != NULL) { 8067 free(inodedep->id_savedino1, M_SAVEDINO); 8068 inodedep->id_savedino1 = NULL; 8069 } 8070 if (free_inodedep(inodedep) == 0) 8071 panic("check_inode_unwritten: busy inode"); 8072 return (1); 8073 } 8074 8075 static int 8076 check_inodedep_free(inodedep) 8077 struct inodedep *inodedep; 8078 { 8079 8080 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 8081 if ((inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE || 8082 !LIST_EMPTY(&inodedep->id_dirremhd) || 8083 !LIST_EMPTY(&inodedep->id_pendinghd) || 8084 !LIST_EMPTY(&inodedep->id_bufwait) || 8085 !LIST_EMPTY(&inodedep->id_inowait) || 8086 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 8087 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 8088 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 8089 !TAILQ_EMPTY(&inodedep->id_extupdt) || 8090 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 8091 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 8092 inodedep->id_mkdiradd != NULL || 8093 inodedep->id_nlinkdelta != 0 || 8094 inodedep->id_savedino1 != NULL) 8095 return (0); 8096 return (1); 8097 } 8098 8099 /* 8100 * Try to free an inodedep structure. Return 1 if it could be freed. 8101 */ 8102 static int 8103 free_inodedep(inodedep) 8104 struct inodedep *inodedep; 8105 { 8106 8107 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 8108 if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 || 8109 !check_inodedep_free(inodedep)) 8110 return (0); 8111 if (inodedep->id_state & ONDEPLIST) 8112 LIST_REMOVE(inodedep, id_deps); 8113 LIST_REMOVE(inodedep, id_hash); 8114 WORKITEM_FREE(inodedep, D_INODEDEP); 8115 return (1); 8116 } 8117 8118 /* 8119 * Free the block referenced by a freework structure. The parent freeblks 8120 * structure is released and completed when the final cg bitmap reaches 8121 * the disk. This routine may be freeing a jnewblk which never made it to 8122 * disk in which case we do not have to wait as the operation is undone 8123 * in memory immediately. 8124 */ 8125 static void 8126 freework_freeblock(freework, key) 8127 struct freework *freework; 8128 u_long key; 8129 { 8130 struct freeblks *freeblks; 8131 struct jnewblk *jnewblk; 8132 struct ufsmount *ump; 8133 struct workhead wkhd; 8134 struct fs *fs; 8135 int bsize; 8136 int needj; 8137 8138 ump = VFSTOUFS(freework->fw_list.wk_mp); 8139 LOCK_OWNED(ump); 8140 /* 8141 * Handle partial truncate separately. 8142 */ 8143 if (freework->fw_indir) { 8144 complete_trunc_indir(freework); 8145 return; 8146 } 8147 freeblks = freework->fw_freeblks; 8148 fs = ump->um_fs; 8149 needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0; 8150 bsize = lfragtosize(fs, freework->fw_frags); 8151 LIST_INIT(&wkhd); 8152 /* 8153 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives 8154 * on the indirblk hashtable and prevents premature freeing. 8155 */ 8156 freework->fw_state |= DEPCOMPLETE; 8157 /* 8158 * SUJ needs to wait for the segment referencing freed indirect 8159 * blocks to expire so that we know the checker will not confuse 8160 * a re-allocated indirect block with its old contents. 8161 */ 8162 if (needj && freework->fw_lbn <= -UFS_NDADDR) 8163 indirblk_insert(freework); 8164 /* 8165 * If we are canceling an existing jnewblk pass it to the free 8166 * routine, otherwise pass the freeblk which will ultimately 8167 * release the freeblks. If we're not journaling, we can just 8168 * free the freeblks immediately. 8169 */ 8170 jnewblk = freework->fw_jnewblk; 8171 if (jnewblk != NULL) { 8172 cancel_jnewblk(jnewblk, &wkhd); 8173 needj = 0; 8174 } else if (needj) { 8175 freework->fw_state |= DELAYEDFREE; 8176 freeblks->fb_cgwait++; 8177 WORKLIST_INSERT(&wkhd, &freework->fw_list); 8178 } 8179 FREE_LOCK(ump); 8180 freeblks_free(ump, freeblks, btodb(bsize)); 8181 CTR4(KTR_SUJ, 8182 "freework_freeblock: ino %jd blkno %jd lbn %jd size %d", 8183 freeblks->fb_inum, freework->fw_blkno, freework->fw_lbn, bsize); 8184 ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, bsize, 8185 freeblks->fb_inum, freeblks->fb_vtype, &wkhd, key); 8186 ACQUIRE_LOCK(ump); 8187 /* 8188 * The jnewblk will be discarded and the bits in the map never 8189 * made it to disk. We can immediately free the freeblk. 8190 */ 8191 if (needj == 0) 8192 handle_written_freework(freework); 8193 } 8194 8195 /* 8196 * We enqueue freework items that need processing back on the freeblks and 8197 * add the freeblks to the worklist. This makes it easier to find all work 8198 * required to flush a truncation in process_truncates(). 8199 */ 8200 static void 8201 freework_enqueue(freework) 8202 struct freework *freework; 8203 { 8204 struct freeblks *freeblks; 8205 8206 freeblks = freework->fw_freeblks; 8207 if ((freework->fw_state & INPROGRESS) == 0) 8208 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 8209 if ((freeblks->fb_state & 8210 (ONWORKLIST | INPROGRESS | ALLCOMPLETE)) == ALLCOMPLETE && 8211 LIST_EMPTY(&freeblks->fb_jblkdephd)) 8212 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 8213 } 8214 8215 /* 8216 * Start, continue, or finish the process of freeing an indirect block tree. 8217 * The free operation may be paused at any point with fw_off containing the 8218 * offset to restart from. This enables us to implement some flow control 8219 * for large truncates which may fan out and generate a huge number of 8220 * dependencies. 8221 */ 8222 static void 8223 handle_workitem_indirblk(freework) 8224 struct freework *freework; 8225 { 8226 struct freeblks *freeblks; 8227 struct ufsmount *ump; 8228 struct fs *fs; 8229 8230 freeblks = freework->fw_freeblks; 8231 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8232 fs = ump->um_fs; 8233 if (freework->fw_state & DEPCOMPLETE) { 8234 handle_written_freework(freework); 8235 return; 8236 } 8237 if (freework->fw_off == NINDIR(fs)) { 8238 freework_freeblock(freework, SINGLETON_KEY); 8239 return; 8240 } 8241 freework->fw_state |= INPROGRESS; 8242 FREE_LOCK(ump); 8243 indir_trunc(freework, fsbtodb(fs, freework->fw_blkno), 8244 freework->fw_lbn); 8245 ACQUIRE_LOCK(ump); 8246 } 8247 8248 /* 8249 * Called when a freework structure attached to a cg buf is written. The 8250 * ref on either the parent or the freeblks structure is released and 8251 * the freeblks is added back to the worklist if there is more work to do. 8252 */ 8253 static void 8254 handle_written_freework(freework) 8255 struct freework *freework; 8256 { 8257 struct freeblks *freeblks; 8258 struct freework *parent; 8259 8260 freeblks = freework->fw_freeblks; 8261 parent = freework->fw_parent; 8262 if (freework->fw_state & DELAYEDFREE) 8263 freeblks->fb_cgwait--; 8264 freework->fw_state |= COMPLETE; 8265 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 8266 WORKITEM_FREE(freework, D_FREEWORK); 8267 if (parent) { 8268 if (--parent->fw_ref == 0) 8269 freework_enqueue(parent); 8270 return; 8271 } 8272 if (--freeblks->fb_ref != 0) 8273 return; 8274 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) == 8275 ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd)) 8276 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 8277 } 8278 8279 /* 8280 * This workitem routine performs the block de-allocation. 8281 * The workitem is added to the pending list after the updated 8282 * inode block has been written to disk. As mentioned above, 8283 * checks regarding the number of blocks de-allocated (compared 8284 * to the number of blocks allocated for the file) are also 8285 * performed in this function. 8286 */ 8287 static int 8288 handle_workitem_freeblocks(freeblks, flags) 8289 struct freeblks *freeblks; 8290 int flags; 8291 { 8292 struct freework *freework; 8293 struct newblk *newblk; 8294 struct allocindir *aip; 8295 struct ufsmount *ump; 8296 struct worklist *wk; 8297 u_long key; 8298 8299 KASSERT(LIST_EMPTY(&freeblks->fb_jblkdephd), 8300 ("handle_workitem_freeblocks: Journal entries not written.")); 8301 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8302 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum); 8303 ACQUIRE_LOCK(ump); 8304 while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) { 8305 WORKLIST_REMOVE(wk); 8306 switch (wk->wk_type) { 8307 case D_DIRREM: 8308 wk->wk_state |= COMPLETE; 8309 add_to_worklist(wk, 0); 8310 continue; 8311 8312 case D_ALLOCDIRECT: 8313 free_newblk(WK_NEWBLK(wk)); 8314 continue; 8315 8316 case D_ALLOCINDIR: 8317 aip = WK_ALLOCINDIR(wk); 8318 freework = NULL; 8319 if (aip->ai_state & DELAYEDFREE) { 8320 FREE_LOCK(ump); 8321 freework = newfreework(ump, freeblks, NULL, 8322 aip->ai_lbn, aip->ai_newblkno, 8323 ump->um_fs->fs_frag, 0, 0); 8324 ACQUIRE_LOCK(ump); 8325 } 8326 newblk = WK_NEWBLK(wk); 8327 if (newblk->nb_jnewblk) { 8328 freework->fw_jnewblk = newblk->nb_jnewblk; 8329 newblk->nb_jnewblk->jn_dep = &freework->fw_list; 8330 newblk->nb_jnewblk = NULL; 8331 } 8332 free_newblk(newblk); 8333 continue; 8334 8335 case D_FREEWORK: 8336 freework = WK_FREEWORK(wk); 8337 if (freework->fw_lbn <= -UFS_NDADDR) 8338 handle_workitem_indirblk(freework); 8339 else 8340 freework_freeblock(freework, key); 8341 continue; 8342 default: 8343 panic("handle_workitem_freeblocks: Unknown type %s", 8344 TYPENAME(wk->wk_type)); 8345 } 8346 } 8347 if (freeblks->fb_ref != 0) { 8348 freeblks->fb_state &= ~INPROGRESS; 8349 wake_worklist(&freeblks->fb_list); 8350 freeblks = NULL; 8351 } 8352 FREE_LOCK(ump); 8353 ffs_blkrelease_finish(ump, key); 8354 if (freeblks) 8355 return handle_complete_freeblocks(freeblks, flags); 8356 return (0); 8357 } 8358 8359 /* 8360 * Handle completion of block free via truncate. This allows fs_pending 8361 * to track the actual free block count more closely than if we only updated 8362 * it at the end. We must be careful to handle cases where the block count 8363 * on free was incorrect. 8364 */ 8365 static void 8366 freeblks_free(ump, freeblks, blocks) 8367 struct ufsmount *ump; 8368 struct freeblks *freeblks; 8369 int blocks; 8370 { 8371 struct fs *fs; 8372 ufs2_daddr_t remain; 8373 8374 UFS_LOCK(ump); 8375 remain = -freeblks->fb_chkcnt; 8376 freeblks->fb_chkcnt += blocks; 8377 if (remain > 0) { 8378 if (remain < blocks) 8379 blocks = remain; 8380 fs = ump->um_fs; 8381 fs->fs_pendingblocks -= blocks; 8382 } 8383 UFS_UNLOCK(ump); 8384 } 8385 8386 /* 8387 * Once all of the freework workitems are complete we can retire the 8388 * freeblocks dependency and any journal work awaiting completion. This 8389 * can not be called until all other dependencies are stable on disk. 8390 */ 8391 static int 8392 handle_complete_freeblocks(freeblks, flags) 8393 struct freeblks *freeblks; 8394 int flags; 8395 { 8396 struct inodedep *inodedep; 8397 struct inode *ip; 8398 struct vnode *vp; 8399 struct fs *fs; 8400 struct ufsmount *ump; 8401 ufs2_daddr_t spare; 8402 8403 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8404 fs = ump->um_fs; 8405 flags = LK_EXCLUSIVE | flags; 8406 spare = freeblks->fb_chkcnt; 8407 8408 /* 8409 * If we did not release the expected number of blocks we may have 8410 * to adjust the inode block count here. Only do so if it wasn't 8411 * a truncation to zero and the modrev still matches. 8412 */ 8413 if (spare && freeblks->fb_len != 0) { 8414 if (ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_inum, 8415 flags, &vp, FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP) != 0) 8416 return (EBUSY); 8417 ip = VTOI(vp); 8418 if (ip->i_mode == 0) { 8419 vgone(vp); 8420 } else if (DIP(ip, i_modrev) == freeblks->fb_modrev) { 8421 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare); 8422 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 8423 /* 8424 * We must wait so this happens before the 8425 * journal is reclaimed. 8426 */ 8427 ffs_update(vp, 1); 8428 } 8429 vput(vp); 8430 } 8431 if (spare < 0) { 8432 UFS_LOCK(ump); 8433 fs->fs_pendingblocks += spare; 8434 UFS_UNLOCK(ump); 8435 } 8436 #ifdef QUOTA 8437 /* Handle spare. */ 8438 if (spare) 8439 quotaadj(freeblks->fb_quota, ump, -spare); 8440 quotarele(freeblks->fb_quota); 8441 #endif 8442 ACQUIRE_LOCK(ump); 8443 if (freeblks->fb_state & ONDEPLIST) { 8444 inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum, 8445 0, &inodedep); 8446 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next); 8447 freeblks->fb_state &= ~ONDEPLIST; 8448 if (TAILQ_EMPTY(&inodedep->id_freeblklst)) 8449 free_inodedep(inodedep); 8450 } 8451 /* 8452 * All of the freeblock deps must be complete prior to this call 8453 * so it's now safe to complete earlier outstanding journal entries. 8454 */ 8455 handle_jwork(&freeblks->fb_jwork); 8456 WORKITEM_FREE(freeblks, D_FREEBLKS); 8457 FREE_LOCK(ump); 8458 return (0); 8459 } 8460 8461 /* 8462 * Release blocks associated with the freeblks and stored in the indirect 8463 * block dbn. If level is greater than SINGLE, the block is an indirect block 8464 * and recursive calls to indirtrunc must be used to cleanse other indirect 8465 * blocks. 8466 * 8467 * This handles partial and complete truncation of blocks. Partial is noted 8468 * with goingaway == 0. In this case the freework is completed after the 8469 * zero'd indirects are written to disk. For full truncation the freework 8470 * is completed after the block is freed. 8471 */ 8472 static void 8473 indir_trunc(freework, dbn, lbn) 8474 struct freework *freework; 8475 ufs2_daddr_t dbn; 8476 ufs_lbn_t lbn; 8477 { 8478 struct freework *nfreework; 8479 struct workhead wkhd; 8480 struct freeblks *freeblks; 8481 struct buf *bp; 8482 struct fs *fs; 8483 struct indirdep *indirdep; 8484 struct mount *mp; 8485 struct ufsmount *ump; 8486 ufs1_daddr_t *bap1; 8487 ufs2_daddr_t nb, nnb, *bap2; 8488 ufs_lbn_t lbnadd, nlbn; 8489 u_long key; 8490 int nblocks, ufs1fmt, freedblocks; 8491 int goingaway, freedeps, needj, level, cnt, i, error; 8492 8493 freeblks = freework->fw_freeblks; 8494 mp = freeblks->fb_list.wk_mp; 8495 ump = VFSTOUFS(mp); 8496 fs = ump->um_fs; 8497 /* 8498 * Get buffer of block pointers to be freed. There are three cases: 8499 * 8500 * 1) Partial truncate caches the indirdep pointer in the freework 8501 * which provides us a back copy to the save bp which holds the 8502 * pointers we want to clear. When this completes the zero 8503 * pointers are written to the real copy. 8504 * 2) The indirect is being completely truncated, cancel_indirdep() 8505 * eliminated the real copy and placed the indirdep on the saved 8506 * copy. The indirdep and buf are discarded when this completes. 8507 * 3) The indirect was not in memory, we read a copy off of the disk 8508 * using the devvp and drop and invalidate the buffer when we're 8509 * done. 8510 */ 8511 goingaway = 1; 8512 indirdep = NULL; 8513 if (freework->fw_indir != NULL) { 8514 goingaway = 0; 8515 indirdep = freework->fw_indir; 8516 bp = indirdep->ir_savebp; 8517 if (bp == NULL || bp->b_blkno != dbn) 8518 panic("indir_trunc: Bad saved buf %p blkno %jd", 8519 bp, (intmax_t)dbn); 8520 } else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) { 8521 /* 8522 * The lock prevents the buf dep list from changing and 8523 * indirects on devvp should only ever have one dependency. 8524 */ 8525 indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep)); 8526 if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0) 8527 panic("indir_trunc: Bad indirdep %p from buf %p", 8528 indirdep, bp); 8529 } else { 8530 error = ffs_breadz(ump, freeblks->fb_devvp, dbn, dbn, 8531 (int)fs->fs_bsize, NULL, NULL, 0, NOCRED, 0, NULL, &bp); 8532 if (error) 8533 return; 8534 } 8535 ACQUIRE_LOCK(ump); 8536 /* Protects against a race with complete_trunc_indir(). */ 8537 freework->fw_state &= ~INPROGRESS; 8538 /* 8539 * If we have an indirdep we need to enforce the truncation order 8540 * and discard it when it is complete. 8541 */ 8542 if (indirdep) { 8543 if (freework != TAILQ_FIRST(&indirdep->ir_trunc) && 8544 !TAILQ_EMPTY(&indirdep->ir_trunc)) { 8545 /* 8546 * Add the complete truncate to the list on the 8547 * indirdep to enforce in-order processing. 8548 */ 8549 if (freework->fw_indir == NULL) 8550 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, 8551 freework, fw_next); 8552 FREE_LOCK(ump); 8553 return; 8554 } 8555 /* 8556 * If we're goingaway, free the indirdep. Otherwise it will 8557 * linger until the write completes. 8558 */ 8559 if (goingaway) { 8560 KASSERT(indirdep->ir_savebp == bp, 8561 ("indir_trunc: losing ir_savebp %p", 8562 indirdep->ir_savebp)); 8563 indirdep->ir_savebp = NULL; 8564 free_indirdep(indirdep); 8565 } 8566 } 8567 FREE_LOCK(ump); 8568 /* Initialize pointers depending on block size. */ 8569 if (ump->um_fstype == UFS1) { 8570 bap1 = (ufs1_daddr_t *)bp->b_data; 8571 nb = bap1[freework->fw_off]; 8572 ufs1fmt = 1; 8573 bap2 = NULL; 8574 } else { 8575 bap2 = (ufs2_daddr_t *)bp->b_data; 8576 nb = bap2[freework->fw_off]; 8577 ufs1fmt = 0; 8578 bap1 = NULL; 8579 } 8580 level = lbn_level(lbn); 8581 needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0; 8582 lbnadd = lbn_offset(fs, level); 8583 nblocks = btodb(fs->fs_bsize); 8584 nfreework = freework; 8585 freedeps = 0; 8586 cnt = 0; 8587 /* 8588 * Reclaim blocks. Traverses into nested indirect levels and 8589 * arranges for the current level to be freed when subordinates 8590 * are free when journaling. 8591 */ 8592 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum); 8593 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) { 8594 if (UFS_CHECK_BLKNO(mp, freeblks->fb_inum, nb, 8595 fs->fs_bsize) != 0) 8596 nb = 0; 8597 if (i != NINDIR(fs) - 1) { 8598 if (ufs1fmt) 8599 nnb = bap1[i+1]; 8600 else 8601 nnb = bap2[i+1]; 8602 } else 8603 nnb = 0; 8604 if (nb == 0) 8605 continue; 8606 cnt++; 8607 if (level != 0) { 8608 nlbn = (lbn + 1) - (i * lbnadd); 8609 if (needj != 0) { 8610 nfreework = newfreework(ump, freeblks, freework, 8611 nlbn, nb, fs->fs_frag, 0, 0); 8612 freedeps++; 8613 } 8614 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn); 8615 } else { 8616 struct freedep *freedep; 8617 8618 /* 8619 * Attempt to aggregate freedep dependencies for 8620 * all blocks being released to the same CG. 8621 */ 8622 LIST_INIT(&wkhd); 8623 if (needj != 0 && 8624 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) { 8625 freedep = newfreedep(freework); 8626 WORKLIST_INSERT_UNLOCKED(&wkhd, 8627 &freedep->fd_list); 8628 freedeps++; 8629 } 8630 CTR3(KTR_SUJ, 8631 "indir_trunc: ino %jd blkno %jd size %d", 8632 freeblks->fb_inum, nb, fs->fs_bsize); 8633 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb, 8634 fs->fs_bsize, freeblks->fb_inum, 8635 freeblks->fb_vtype, &wkhd, key); 8636 } 8637 } 8638 ffs_blkrelease_finish(ump, key); 8639 if (goingaway) { 8640 bp->b_flags |= B_INVAL | B_NOCACHE; 8641 brelse(bp); 8642 } 8643 freedblocks = 0; 8644 if (level == 0) 8645 freedblocks = (nblocks * cnt); 8646 if (needj == 0) 8647 freedblocks += nblocks; 8648 freeblks_free(ump, freeblks, freedblocks); 8649 /* 8650 * If we are journaling set up the ref counts and offset so this 8651 * indirect can be completed when its children are free. 8652 */ 8653 if (needj) { 8654 ACQUIRE_LOCK(ump); 8655 freework->fw_off = i; 8656 freework->fw_ref += freedeps; 8657 freework->fw_ref -= NINDIR(fs) + 1; 8658 if (level == 0) 8659 freeblks->fb_cgwait += freedeps; 8660 if (freework->fw_ref == 0) 8661 freework_freeblock(freework, SINGLETON_KEY); 8662 FREE_LOCK(ump); 8663 return; 8664 } 8665 /* 8666 * If we're not journaling we can free the indirect now. 8667 */ 8668 dbn = dbtofsb(fs, dbn); 8669 CTR3(KTR_SUJ, 8670 "indir_trunc 2: ino %jd blkno %jd size %d", 8671 freeblks->fb_inum, dbn, fs->fs_bsize); 8672 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize, 8673 freeblks->fb_inum, freeblks->fb_vtype, NULL, SINGLETON_KEY); 8674 /* Non SUJ softdep does single-threaded truncations. */ 8675 if (freework->fw_blkno == dbn) { 8676 freework->fw_state |= ALLCOMPLETE; 8677 ACQUIRE_LOCK(ump); 8678 handle_written_freework(freework); 8679 FREE_LOCK(ump); 8680 } 8681 return; 8682 } 8683 8684 /* 8685 * Cancel an allocindir when it is removed via truncation. When bp is not 8686 * NULL the indirect never appeared on disk and is scheduled to be freed 8687 * independently of the indir so we can more easily track journal work. 8688 */ 8689 static void 8690 cancel_allocindir(aip, bp, freeblks, trunc) 8691 struct allocindir *aip; 8692 struct buf *bp; 8693 struct freeblks *freeblks; 8694 int trunc; 8695 { 8696 struct indirdep *indirdep; 8697 struct freefrag *freefrag; 8698 struct newblk *newblk; 8699 8700 newblk = (struct newblk *)aip; 8701 LIST_REMOVE(aip, ai_next); 8702 /* 8703 * We must eliminate the pointer in bp if it must be freed on its 8704 * own due to partial truncate or pending journal work. 8705 */ 8706 if (bp && (trunc || newblk->nb_jnewblk)) { 8707 /* 8708 * Clear the pointer and mark the aip to be freed 8709 * directly if it never existed on disk. 8710 */ 8711 aip->ai_state |= DELAYEDFREE; 8712 indirdep = aip->ai_indirdep; 8713 if (indirdep->ir_state & UFS1FMT) 8714 ((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8715 else 8716 ((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8717 } 8718 /* 8719 * When truncating the previous pointer will be freed via 8720 * savedbp. Eliminate the freefrag which would dup free. 8721 */ 8722 if (trunc && (freefrag = newblk->nb_freefrag) != NULL) { 8723 newblk->nb_freefrag = NULL; 8724 if (freefrag->ff_jdep) 8725 cancel_jfreefrag( 8726 WK_JFREEFRAG(freefrag->ff_jdep)); 8727 jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork); 8728 WORKITEM_FREE(freefrag, D_FREEFRAG); 8729 } 8730 /* 8731 * If the journal hasn't been written the jnewblk must be passed 8732 * to the call to ffs_blkfree that reclaims the space. We accomplish 8733 * this by leaving the journal dependency on the newblk to be freed 8734 * when a freework is created in handle_workitem_freeblocks(). 8735 */ 8736 cancel_newblk(newblk, NULL, &freeblks->fb_jwork); 8737 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 8738 } 8739 8740 /* 8741 * Create the mkdir dependencies for . and .. in a new directory. Link them 8742 * in to a newdirblk so any subsequent additions are tracked properly. The 8743 * caller is responsible for adding the mkdir1 dependency to the journal 8744 * and updating id_mkdiradd. This function returns with the per-filesystem 8745 * lock held. 8746 */ 8747 static struct mkdir * 8748 setup_newdir(dap, newinum, dinum, newdirbp, mkdirp) 8749 struct diradd *dap; 8750 ino_t newinum; 8751 ino_t dinum; 8752 struct buf *newdirbp; 8753 struct mkdir **mkdirp; 8754 { 8755 struct newblk *newblk; 8756 struct pagedep *pagedep; 8757 struct inodedep *inodedep; 8758 struct newdirblk *newdirblk; 8759 struct mkdir *mkdir1, *mkdir2; 8760 struct worklist *wk; 8761 struct jaddref *jaddref; 8762 struct ufsmount *ump; 8763 struct mount *mp; 8764 8765 mp = dap->da_list.wk_mp; 8766 ump = VFSTOUFS(mp); 8767 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK, 8768 M_SOFTDEP_FLAGS); 8769 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8770 LIST_INIT(&newdirblk->db_mkdir); 8771 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8772 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp); 8773 mkdir1->md_state = ATTACHED | MKDIR_BODY; 8774 mkdir1->md_diradd = dap; 8775 mkdir1->md_jaddref = NULL; 8776 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8777 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp); 8778 mkdir2->md_state = ATTACHED | MKDIR_PARENT; 8779 mkdir2->md_diradd = dap; 8780 mkdir2->md_jaddref = NULL; 8781 if (MOUNTEDSUJ(mp) == 0) { 8782 mkdir1->md_state |= DEPCOMPLETE; 8783 mkdir2->md_state |= DEPCOMPLETE; 8784 } 8785 /* 8786 * Dependency on "." and ".." being written to disk. 8787 */ 8788 mkdir1->md_buf = newdirbp; 8789 ACQUIRE_LOCK(VFSTOUFS(mp)); 8790 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir1, md_mkdirs); 8791 /* 8792 * We must link the pagedep, allocdirect, and newdirblk for 8793 * the initial file page so the pointer to the new directory 8794 * is not written until the directory contents are live and 8795 * any subsequent additions are not marked live until the 8796 * block is reachable via the inode. 8797 */ 8798 if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0) 8799 panic("setup_newdir: lost pagedep"); 8800 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list) 8801 if (wk->wk_type == D_ALLOCDIRECT) 8802 break; 8803 if (wk == NULL) 8804 panic("setup_newdir: lost allocdirect"); 8805 if (pagedep->pd_state & NEWBLOCK) 8806 panic("setup_newdir: NEWBLOCK already set"); 8807 newblk = WK_NEWBLK(wk); 8808 pagedep->pd_state |= NEWBLOCK; 8809 pagedep->pd_newdirblk = newdirblk; 8810 newdirblk->db_pagedep = pagedep; 8811 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8812 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list); 8813 /* 8814 * Look up the inodedep for the parent directory so that we 8815 * can link mkdir2 into the pending dotdot jaddref or 8816 * the inode write if there is none. If the inode is 8817 * ALLCOMPLETE and no jaddref is present all dependencies have 8818 * been satisfied and mkdir2 can be freed. 8819 */ 8820 inodedep_lookup(mp, dinum, 0, &inodedep); 8821 if (MOUNTEDSUJ(mp)) { 8822 if (inodedep == NULL) 8823 panic("setup_newdir: Lost parent."); 8824 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8825 inoreflst); 8826 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum && 8827 (jaddref->ja_state & MKDIR_PARENT), 8828 ("setup_newdir: bad dotdot jaddref %p", jaddref)); 8829 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8830 mkdir2->md_jaddref = jaddref; 8831 jaddref->ja_mkdir = mkdir2; 8832 } else if (inodedep == NULL || 8833 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 8834 dap->da_state &= ~MKDIR_PARENT; 8835 WORKITEM_FREE(mkdir2, D_MKDIR); 8836 mkdir2 = NULL; 8837 } else { 8838 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8839 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list); 8840 } 8841 *mkdirp = mkdir2; 8842 8843 return (mkdir1); 8844 } 8845 8846 /* 8847 * Directory entry addition dependencies. 8848 * 8849 * When adding a new directory entry, the inode (with its incremented link 8850 * count) must be written to disk before the directory entry's pointer to it. 8851 * Also, if the inode is newly allocated, the corresponding freemap must be 8852 * updated (on disk) before the directory entry's pointer. These requirements 8853 * are met via undo/redo on the directory entry's pointer, which consists 8854 * simply of the inode number. 8855 * 8856 * As directory entries are added and deleted, the free space within a 8857 * directory block can become fragmented. The ufs filesystem will compact 8858 * a fragmented directory block to make space for a new entry. When this 8859 * occurs, the offsets of previously added entries change. Any "diradd" 8860 * dependency structures corresponding to these entries must be updated with 8861 * the new offsets. 8862 */ 8863 8864 /* 8865 * This routine is called after the in-memory inode's link 8866 * count has been incremented, but before the directory entry's 8867 * pointer to the inode has been set. 8868 */ 8869 int 8870 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 8871 struct buf *bp; /* buffer containing directory block */ 8872 struct inode *dp; /* inode for directory */ 8873 off_t diroffset; /* offset of new entry in directory */ 8874 ino_t newinum; /* inode referenced by new directory entry */ 8875 struct buf *newdirbp; /* non-NULL => contents of new mkdir */ 8876 int isnewblk; /* entry is in a newly allocated block */ 8877 { 8878 int offset; /* offset of new entry within directory block */ 8879 ufs_lbn_t lbn; /* block in directory containing new entry */ 8880 struct fs *fs; 8881 struct diradd *dap; 8882 struct newblk *newblk; 8883 struct pagedep *pagedep; 8884 struct inodedep *inodedep; 8885 struct newdirblk *newdirblk; 8886 struct mkdir *mkdir1, *mkdir2; 8887 struct jaddref *jaddref; 8888 struct ufsmount *ump; 8889 struct mount *mp; 8890 int isindir; 8891 8892 mp = ITOVFS(dp); 8893 ump = VFSTOUFS(mp); 8894 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 8895 ("softdep_setup_directory_add called on non-softdep filesystem")); 8896 /* 8897 * Whiteouts have no dependencies. 8898 */ 8899 if (newinum == UFS_WINO) { 8900 if (newdirbp != NULL) 8901 bdwrite(newdirbp); 8902 return (0); 8903 } 8904 jaddref = NULL; 8905 mkdir1 = mkdir2 = NULL; 8906 fs = ump->um_fs; 8907 lbn = lblkno(fs, diroffset); 8908 offset = blkoff(fs, diroffset); 8909 dap = malloc(sizeof(struct diradd), M_DIRADD, 8910 M_SOFTDEP_FLAGS|M_ZERO); 8911 workitem_alloc(&dap->da_list, D_DIRADD, mp); 8912 dap->da_offset = offset; 8913 dap->da_newinum = newinum; 8914 dap->da_state = ATTACHED; 8915 LIST_INIT(&dap->da_jwork); 8916 isindir = bp->b_lblkno >= UFS_NDADDR; 8917 newdirblk = NULL; 8918 if (isnewblk && 8919 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) { 8920 newdirblk = malloc(sizeof(struct newdirblk), 8921 M_NEWDIRBLK, M_SOFTDEP_FLAGS); 8922 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8923 LIST_INIT(&newdirblk->db_mkdir); 8924 } 8925 /* 8926 * If we're creating a new directory setup the dependencies and set 8927 * the dap state to wait for them. Otherwise it's COMPLETE and 8928 * we can move on. 8929 */ 8930 if (newdirbp == NULL) { 8931 dap->da_state |= DEPCOMPLETE; 8932 ACQUIRE_LOCK(ump); 8933 } else { 8934 dap->da_state |= MKDIR_BODY | MKDIR_PARENT; 8935 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp, 8936 &mkdir2); 8937 } 8938 /* 8939 * Link into parent directory pagedep to await its being written. 8940 */ 8941 pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep); 8942 #ifdef INVARIANTS 8943 if (diradd_lookup(pagedep, offset) != NULL) 8944 panic("softdep_setup_directory_add: %p already at off %d\n", 8945 diradd_lookup(pagedep, offset), offset); 8946 #endif 8947 dap->da_pagedep = pagedep; 8948 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap, 8949 da_pdlist); 8950 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 8951 /* 8952 * If we're journaling, link the diradd into the jaddref so it 8953 * may be completed after the journal entry is written. Otherwise, 8954 * link the diradd into its inodedep. If the inode is not yet 8955 * written place it on the bufwait list, otherwise do the post-inode 8956 * write processing to put it on the id_pendinghd list. 8957 */ 8958 if (MOUNTEDSUJ(mp)) { 8959 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8960 inoreflst); 8961 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 8962 ("softdep_setup_directory_add: bad jaddref %p", jaddref)); 8963 jaddref->ja_diroff = diroffset; 8964 jaddref->ja_diradd = dap; 8965 add_to_journal(&jaddref->ja_list); 8966 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) 8967 diradd_inode_written(dap, inodedep); 8968 else 8969 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 8970 /* 8971 * Add the journal entries for . and .. links now that the primary 8972 * link is written. 8973 */ 8974 if (mkdir1 != NULL && MOUNTEDSUJ(mp)) { 8975 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 8976 inoreflst, if_deps); 8977 KASSERT(jaddref != NULL && 8978 jaddref->ja_ino == jaddref->ja_parent && 8979 (jaddref->ja_state & MKDIR_BODY), 8980 ("softdep_setup_directory_add: bad dot jaddref %p", 8981 jaddref)); 8982 mkdir1->md_jaddref = jaddref; 8983 jaddref->ja_mkdir = mkdir1; 8984 /* 8985 * It is important that the dotdot journal entry 8986 * is added prior to the dot entry since dot writes 8987 * both the dot and dotdot links. These both must 8988 * be added after the primary link for the journal 8989 * to remain consistent. 8990 */ 8991 add_to_journal(&mkdir2->md_jaddref->ja_list); 8992 add_to_journal(&jaddref->ja_list); 8993 } 8994 /* 8995 * If we are adding a new directory remember this diradd so that if 8996 * we rename it we can keep the dot and dotdot dependencies. If 8997 * we are adding a new name for an inode that has a mkdiradd we 8998 * must be in rename and we have to move the dot and dotdot 8999 * dependencies to this new name. The old name is being orphaned 9000 * soon. 9001 */ 9002 if (mkdir1 != NULL) { 9003 if (inodedep->id_mkdiradd != NULL) 9004 panic("softdep_setup_directory_add: Existing mkdir"); 9005 inodedep->id_mkdiradd = dap; 9006 } else if (inodedep->id_mkdiradd) 9007 merge_diradd(inodedep, dap); 9008 if (newdirblk != NULL) { 9009 /* 9010 * There is nothing to do if we are already tracking 9011 * this block. 9012 */ 9013 if ((pagedep->pd_state & NEWBLOCK) != 0) { 9014 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 9015 FREE_LOCK(ump); 9016 return (0); 9017 } 9018 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk) 9019 == 0) 9020 panic("softdep_setup_directory_add: lost entry"); 9021 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 9022 pagedep->pd_state |= NEWBLOCK; 9023 pagedep->pd_newdirblk = newdirblk; 9024 newdirblk->db_pagedep = pagedep; 9025 FREE_LOCK(ump); 9026 /* 9027 * If we extended into an indirect signal direnter to sync. 9028 */ 9029 if (isindir) 9030 return (1); 9031 return (0); 9032 } 9033 FREE_LOCK(ump); 9034 return (0); 9035 } 9036 9037 /* 9038 * This procedure is called to change the offset of a directory 9039 * entry when compacting a directory block which must be owned 9040 * exclusively by the caller. Note that the actual entry movement 9041 * must be done in this procedure to ensure that no I/O completions 9042 * occur while the move is in progress. 9043 */ 9044 void 9045 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 9046 struct buf *bp; /* Buffer holding directory block. */ 9047 struct inode *dp; /* inode for directory */ 9048 caddr_t base; /* address of dp->i_offset */ 9049 caddr_t oldloc; /* address of old directory location */ 9050 caddr_t newloc; /* address of new directory location */ 9051 int entrysize; /* size of directory entry */ 9052 { 9053 int offset, oldoffset, newoffset; 9054 struct pagedep *pagedep; 9055 struct jmvref *jmvref; 9056 struct diradd *dap; 9057 struct direct *de; 9058 struct mount *mp; 9059 struct ufsmount *ump; 9060 ufs_lbn_t lbn; 9061 int flags; 9062 9063 mp = ITOVFS(dp); 9064 ump = VFSTOUFS(mp); 9065 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 9066 ("softdep_change_directoryentry_offset called on " 9067 "non-softdep filesystem")); 9068 de = (struct direct *)oldloc; 9069 jmvref = NULL; 9070 flags = 0; 9071 /* 9072 * Moves are always journaled as it would be too complex to 9073 * determine if any affected adds or removes are present in the 9074 * journal. 9075 */ 9076 if (MOUNTEDSUJ(mp)) { 9077 flags = DEPALLOC; 9078 jmvref = newjmvref(dp, de->d_ino, 9079 I_OFFSET(dp) + (oldloc - base), 9080 I_OFFSET(dp) + (newloc - base)); 9081 } 9082 lbn = lblkno(ump->um_fs, I_OFFSET(dp)); 9083 offset = blkoff(ump->um_fs, I_OFFSET(dp)); 9084 oldoffset = offset + (oldloc - base); 9085 newoffset = offset + (newloc - base); 9086 ACQUIRE_LOCK(ump); 9087 if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0) 9088 goto done; 9089 dap = diradd_lookup(pagedep, oldoffset); 9090 if (dap) { 9091 dap->da_offset = newoffset; 9092 newoffset = DIRADDHASH(newoffset); 9093 oldoffset = DIRADDHASH(oldoffset); 9094 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE && 9095 newoffset != oldoffset) { 9096 LIST_REMOVE(dap, da_pdlist); 9097 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset], 9098 dap, da_pdlist); 9099 } 9100 } 9101 done: 9102 if (jmvref) { 9103 jmvref->jm_pagedep = pagedep; 9104 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps); 9105 add_to_journal(&jmvref->jm_list); 9106 } 9107 bcopy(oldloc, newloc, entrysize); 9108 FREE_LOCK(ump); 9109 } 9110 9111 /* 9112 * Move the mkdir dependencies and journal work from one diradd to another 9113 * when renaming a directory. The new name must depend on the mkdir deps 9114 * completing as the old name did. Directories can only have one valid link 9115 * at a time so one must be canonical. 9116 */ 9117 static void 9118 merge_diradd(inodedep, newdap) 9119 struct inodedep *inodedep; 9120 struct diradd *newdap; 9121 { 9122 struct diradd *olddap; 9123 struct mkdir *mkdir, *nextmd; 9124 struct ufsmount *ump; 9125 short state; 9126 9127 olddap = inodedep->id_mkdiradd; 9128 inodedep->id_mkdiradd = newdap; 9129 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 9130 newdap->da_state &= ~DEPCOMPLETE; 9131 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9132 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 9133 mkdir = nextmd) { 9134 nextmd = LIST_NEXT(mkdir, md_mkdirs); 9135 if (mkdir->md_diradd != olddap) 9136 continue; 9137 mkdir->md_diradd = newdap; 9138 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY); 9139 newdap->da_state |= state; 9140 olddap->da_state &= ~state; 9141 if ((olddap->da_state & 9142 (MKDIR_PARENT | MKDIR_BODY)) == 0) 9143 break; 9144 } 9145 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 9146 panic("merge_diradd: unfound ref"); 9147 } 9148 /* 9149 * Any mkdir related journal items are not safe to be freed until 9150 * the new name is stable. 9151 */ 9152 jwork_move(&newdap->da_jwork, &olddap->da_jwork); 9153 olddap->da_state |= DEPCOMPLETE; 9154 complete_diradd(olddap); 9155 } 9156 9157 /* 9158 * Move the diradd to the pending list when all diradd dependencies are 9159 * complete. 9160 */ 9161 static void 9162 complete_diradd(dap) 9163 struct diradd *dap; 9164 { 9165 struct pagedep *pagedep; 9166 9167 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 9168 if (dap->da_state & DIRCHG) 9169 pagedep = dap->da_previous->dm_pagedep; 9170 else 9171 pagedep = dap->da_pagedep; 9172 LIST_REMOVE(dap, da_pdlist); 9173 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 9174 } 9175 } 9176 9177 /* 9178 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal 9179 * add entries and conditonally journal the remove. 9180 */ 9181 static void 9182 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref) 9183 struct diradd *dap; 9184 struct dirrem *dirrem; 9185 struct jremref *jremref; 9186 struct jremref *dotremref; 9187 struct jremref *dotdotremref; 9188 { 9189 struct inodedep *inodedep; 9190 struct jaddref *jaddref; 9191 struct inoref *inoref; 9192 struct ufsmount *ump; 9193 struct mkdir *mkdir; 9194 9195 /* 9196 * If no remove references were allocated we're on a non-journaled 9197 * filesystem and can skip the cancel step. 9198 */ 9199 if (jremref == NULL) { 9200 free_diradd(dap, NULL); 9201 return; 9202 } 9203 /* 9204 * Cancel the primary name an free it if it does not require 9205 * journaling. 9206 */ 9207 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum, 9208 0, &inodedep) != 0) { 9209 /* Abort the addref that reference this diradd. */ 9210 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 9211 if (inoref->if_list.wk_type != D_JADDREF) 9212 continue; 9213 jaddref = (struct jaddref *)inoref; 9214 if (jaddref->ja_diradd != dap) 9215 continue; 9216 if (cancel_jaddref(jaddref, inodedep, 9217 &dirrem->dm_jwork) == 0) { 9218 free_jremref(jremref); 9219 jremref = NULL; 9220 } 9221 break; 9222 } 9223 } 9224 /* 9225 * Cancel subordinate names and free them if they do not require 9226 * journaling. 9227 */ 9228 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 9229 ump = VFSTOUFS(dap->da_list.wk_mp); 9230 LIST_FOREACH(mkdir, &ump->softdep_mkdirlisthd, md_mkdirs) { 9231 if (mkdir->md_diradd != dap) 9232 continue; 9233 if ((jaddref = mkdir->md_jaddref) == NULL) 9234 continue; 9235 mkdir->md_jaddref = NULL; 9236 if (mkdir->md_state & MKDIR_PARENT) { 9237 if (cancel_jaddref(jaddref, NULL, 9238 &dirrem->dm_jwork) == 0) { 9239 free_jremref(dotdotremref); 9240 dotdotremref = NULL; 9241 } 9242 } else { 9243 if (cancel_jaddref(jaddref, inodedep, 9244 &dirrem->dm_jwork) == 0) { 9245 free_jremref(dotremref); 9246 dotremref = NULL; 9247 } 9248 } 9249 } 9250 } 9251 9252 if (jremref) 9253 journal_jremref(dirrem, jremref, inodedep); 9254 if (dotremref) 9255 journal_jremref(dirrem, dotremref, inodedep); 9256 if (dotdotremref) 9257 journal_jremref(dirrem, dotdotremref, NULL); 9258 jwork_move(&dirrem->dm_jwork, &dap->da_jwork); 9259 free_diradd(dap, &dirrem->dm_jwork); 9260 } 9261 9262 /* 9263 * Free a diradd dependency structure. 9264 */ 9265 static void 9266 free_diradd(dap, wkhd) 9267 struct diradd *dap; 9268 struct workhead *wkhd; 9269 { 9270 struct dirrem *dirrem; 9271 struct pagedep *pagedep; 9272 struct inodedep *inodedep; 9273 struct mkdir *mkdir, *nextmd; 9274 struct ufsmount *ump; 9275 9276 ump = VFSTOUFS(dap->da_list.wk_mp); 9277 LOCK_OWNED(ump); 9278 LIST_REMOVE(dap, da_pdlist); 9279 if (dap->da_state & ONWORKLIST) 9280 WORKLIST_REMOVE(&dap->da_list); 9281 if ((dap->da_state & DIRCHG) == 0) { 9282 pagedep = dap->da_pagedep; 9283 } else { 9284 dirrem = dap->da_previous; 9285 pagedep = dirrem->dm_pagedep; 9286 dirrem->dm_dirinum = pagedep->pd_ino; 9287 dirrem->dm_state |= COMPLETE; 9288 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9289 add_to_worklist(&dirrem->dm_list, 0); 9290 } 9291 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum, 9292 0, &inodedep) != 0) 9293 if (inodedep->id_mkdiradd == dap) 9294 inodedep->id_mkdiradd = NULL; 9295 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 9296 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 9297 mkdir = nextmd) { 9298 nextmd = LIST_NEXT(mkdir, md_mkdirs); 9299 if (mkdir->md_diradd != dap) 9300 continue; 9301 dap->da_state &= 9302 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 9303 LIST_REMOVE(mkdir, md_mkdirs); 9304 if (mkdir->md_state & ONWORKLIST) 9305 WORKLIST_REMOVE(&mkdir->md_list); 9306 if (mkdir->md_jaddref != NULL) 9307 panic("free_diradd: Unexpected jaddref"); 9308 WORKITEM_FREE(mkdir, D_MKDIR); 9309 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) 9310 break; 9311 } 9312 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 9313 panic("free_diradd: unfound ref"); 9314 } 9315 if (inodedep) 9316 free_inodedep(inodedep); 9317 /* 9318 * Free any journal segments waiting for the directory write. 9319 */ 9320 handle_jwork(&dap->da_jwork); 9321 WORKITEM_FREE(dap, D_DIRADD); 9322 } 9323 9324 /* 9325 * Directory entry removal dependencies. 9326 * 9327 * When removing a directory entry, the entry's inode pointer must be 9328 * zero'ed on disk before the corresponding inode's link count is decremented 9329 * (possibly freeing the inode for re-use). This dependency is handled by 9330 * updating the directory entry but delaying the inode count reduction until 9331 * after the directory block has been written to disk. After this point, the 9332 * inode count can be decremented whenever it is convenient. 9333 */ 9334 9335 /* 9336 * This routine should be called immediately after removing 9337 * a directory entry. The inode's link count should not be 9338 * decremented by the calling procedure -- the soft updates 9339 * code will do this task when it is safe. 9340 */ 9341 void 9342 softdep_setup_remove(bp, dp, ip, isrmdir) 9343 struct buf *bp; /* buffer containing directory block */ 9344 struct inode *dp; /* inode for the directory being modified */ 9345 struct inode *ip; /* inode for directory entry being removed */ 9346 int isrmdir; /* indicates if doing RMDIR */ 9347 { 9348 struct dirrem *dirrem, *prevdirrem; 9349 struct inodedep *inodedep; 9350 struct ufsmount *ump; 9351 int direct; 9352 9353 ump = ITOUMP(ip); 9354 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9355 ("softdep_setup_remove called on non-softdep filesystem")); 9356 /* 9357 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want 9358 * newdirrem() to setup the full directory remove which requires 9359 * isrmdir > 1. 9360 */ 9361 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 9362 /* 9363 * Add the dirrem to the inodedep's pending remove list for quick 9364 * discovery later. 9365 */ 9366 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) 9367 panic("softdep_setup_remove: Lost inodedep."); 9368 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 9369 dirrem->dm_state |= ONDEPLIST; 9370 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9371 9372 /* 9373 * If the COMPLETE flag is clear, then there were no active 9374 * entries and we want to roll back to a zeroed entry until 9375 * the new inode is committed to disk. If the COMPLETE flag is 9376 * set then we have deleted an entry that never made it to 9377 * disk. If the entry we deleted resulted from a name change, 9378 * then the old name still resides on disk. We cannot delete 9379 * its inode (returned to us in prevdirrem) until the zeroed 9380 * directory entry gets to disk. The new inode has never been 9381 * referenced on the disk, so can be deleted immediately. 9382 */ 9383 if ((dirrem->dm_state & COMPLETE) == 0) { 9384 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem, 9385 dm_next); 9386 FREE_LOCK(ump); 9387 } else { 9388 if (prevdirrem != NULL) 9389 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, 9390 prevdirrem, dm_next); 9391 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino; 9392 direct = LIST_EMPTY(&dirrem->dm_jremrefhd); 9393 FREE_LOCK(ump); 9394 if (direct) 9395 handle_workitem_remove(dirrem, 0); 9396 } 9397 } 9398 9399 /* 9400 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the 9401 * pd_pendinghd list of a pagedep. 9402 */ 9403 static struct diradd * 9404 diradd_lookup(pagedep, offset) 9405 struct pagedep *pagedep; 9406 int offset; 9407 { 9408 struct diradd *dap; 9409 9410 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist) 9411 if (dap->da_offset == offset) 9412 return (dap); 9413 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 9414 if (dap->da_offset == offset) 9415 return (dap); 9416 return (NULL); 9417 } 9418 9419 /* 9420 * Search for a .. diradd dependency in a directory that is being removed. 9421 * If the directory was renamed to a new parent we have a diradd rather 9422 * than a mkdir for the .. entry. We need to cancel it now before 9423 * it is found in truncate(). 9424 */ 9425 static struct jremref * 9426 cancel_diradd_dotdot(ip, dirrem, jremref) 9427 struct inode *ip; 9428 struct dirrem *dirrem; 9429 struct jremref *jremref; 9430 { 9431 struct pagedep *pagedep; 9432 struct diradd *dap; 9433 struct worklist *wk; 9434 9435 if (pagedep_lookup(ITOVFS(ip), NULL, ip->i_number, 0, 0, &pagedep) == 0) 9436 return (jremref); 9437 dap = diradd_lookup(pagedep, DOTDOT_OFFSET); 9438 if (dap == NULL) 9439 return (jremref); 9440 cancel_diradd(dap, dirrem, jremref, NULL, NULL); 9441 /* 9442 * Mark any journal work as belonging to the parent so it is freed 9443 * with the .. reference. 9444 */ 9445 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9446 wk->wk_state |= MKDIR_PARENT; 9447 return (NULL); 9448 } 9449 9450 /* 9451 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to 9452 * replace it with a dirrem/diradd pair as a result of re-parenting a 9453 * directory. This ensures that we don't simultaneously have a mkdir and 9454 * a diradd for the same .. entry. 9455 */ 9456 static struct jremref * 9457 cancel_mkdir_dotdot(ip, dirrem, jremref) 9458 struct inode *ip; 9459 struct dirrem *dirrem; 9460 struct jremref *jremref; 9461 { 9462 struct inodedep *inodedep; 9463 struct jaddref *jaddref; 9464 struct ufsmount *ump; 9465 struct mkdir *mkdir; 9466 struct diradd *dap; 9467 struct mount *mp; 9468 9469 mp = ITOVFS(ip); 9470 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9471 return (jremref); 9472 dap = inodedep->id_mkdiradd; 9473 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0) 9474 return (jremref); 9475 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9476 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 9477 mkdir = LIST_NEXT(mkdir, md_mkdirs)) 9478 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT) 9479 break; 9480 if (mkdir == NULL) 9481 panic("cancel_mkdir_dotdot: Unable to find mkdir\n"); 9482 if ((jaddref = mkdir->md_jaddref) != NULL) { 9483 mkdir->md_jaddref = NULL; 9484 jaddref->ja_state &= ~MKDIR_PARENT; 9485 if (inodedep_lookup(mp, jaddref->ja_ino, 0, &inodedep) == 0) 9486 panic("cancel_mkdir_dotdot: Lost parent inodedep"); 9487 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) { 9488 journal_jremref(dirrem, jremref, inodedep); 9489 jremref = NULL; 9490 } 9491 } 9492 if (mkdir->md_state & ONWORKLIST) 9493 WORKLIST_REMOVE(&mkdir->md_list); 9494 mkdir->md_state |= ALLCOMPLETE; 9495 complete_mkdir(mkdir); 9496 return (jremref); 9497 } 9498 9499 static void 9500 journal_jremref(dirrem, jremref, inodedep) 9501 struct dirrem *dirrem; 9502 struct jremref *jremref; 9503 struct inodedep *inodedep; 9504 { 9505 9506 if (inodedep == NULL) 9507 if (inodedep_lookup(jremref->jr_list.wk_mp, 9508 jremref->jr_ref.if_ino, 0, &inodedep) == 0) 9509 panic("journal_jremref: Lost inodedep"); 9510 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps); 9511 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 9512 add_to_journal(&jremref->jr_list); 9513 } 9514 9515 static void 9516 dirrem_journal(dirrem, jremref, dotremref, dotdotremref) 9517 struct dirrem *dirrem; 9518 struct jremref *jremref; 9519 struct jremref *dotremref; 9520 struct jremref *dotdotremref; 9521 { 9522 struct inodedep *inodedep; 9523 9524 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0, 9525 &inodedep) == 0) 9526 panic("dirrem_journal: Lost inodedep"); 9527 journal_jremref(dirrem, jremref, inodedep); 9528 if (dotremref) 9529 journal_jremref(dirrem, dotremref, inodedep); 9530 if (dotdotremref) 9531 journal_jremref(dirrem, dotdotremref, NULL); 9532 } 9533 9534 /* 9535 * Allocate a new dirrem if appropriate and return it along with 9536 * its associated pagedep. Called without a lock, returns with lock. 9537 */ 9538 static struct dirrem * 9539 newdirrem(bp, dp, ip, isrmdir, prevdirremp) 9540 struct buf *bp; /* buffer containing directory block */ 9541 struct inode *dp; /* inode for the directory being modified */ 9542 struct inode *ip; /* inode for directory entry being removed */ 9543 int isrmdir; /* indicates if doing RMDIR */ 9544 struct dirrem **prevdirremp; /* previously referenced inode, if any */ 9545 { 9546 int offset; 9547 ufs_lbn_t lbn; 9548 struct diradd *dap; 9549 struct dirrem *dirrem; 9550 struct pagedep *pagedep; 9551 struct jremref *jremref; 9552 struct jremref *dotremref; 9553 struct jremref *dotdotremref; 9554 struct vnode *dvp; 9555 struct ufsmount *ump; 9556 9557 /* 9558 * Whiteouts have no deletion dependencies. 9559 */ 9560 if (ip == NULL) 9561 panic("newdirrem: whiteout"); 9562 dvp = ITOV(dp); 9563 ump = ITOUMP(dp); 9564 9565 /* 9566 * If the system is over its limit and our filesystem is 9567 * responsible for more than our share of that usage and 9568 * we are not a snapshot, request some inodedep cleanup. 9569 * Limiting the number of dirrem structures will also limit 9570 * the number of freefile and freeblks structures. 9571 */ 9572 ACQUIRE_LOCK(ump); 9573 if (!IS_SNAPSHOT(ip) && softdep_excess_items(ump, D_DIRREM)) 9574 schedule_cleanup(UFSTOVFS(ump)); 9575 else 9576 FREE_LOCK(ump); 9577 dirrem = malloc(sizeof(struct dirrem), M_DIRREM, M_SOFTDEP_FLAGS | 9578 M_ZERO); 9579 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount); 9580 LIST_INIT(&dirrem->dm_jremrefhd); 9581 LIST_INIT(&dirrem->dm_jwork); 9582 dirrem->dm_state = isrmdir ? RMDIR : 0; 9583 dirrem->dm_oldinum = ip->i_number; 9584 *prevdirremp = NULL; 9585 /* 9586 * Allocate remove reference structures to track journal write 9587 * dependencies. We will always have one for the link and 9588 * when doing directories we will always have one more for dot. 9589 * When renaming a directory we skip the dotdot link change so 9590 * this is not needed. 9591 */ 9592 jremref = dotremref = dotdotremref = NULL; 9593 if (DOINGSUJ(dvp)) { 9594 if (isrmdir) { 9595 jremref = newjremref(dirrem, dp, ip, I_OFFSET(dp), 9596 ip->i_effnlink + 2); 9597 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET, 9598 ip->i_effnlink + 1); 9599 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET, 9600 dp->i_effnlink + 1); 9601 dotdotremref->jr_state |= MKDIR_PARENT; 9602 } else 9603 jremref = newjremref(dirrem, dp, ip, I_OFFSET(dp), 9604 ip->i_effnlink + 1); 9605 } 9606 ACQUIRE_LOCK(ump); 9607 lbn = lblkno(ump->um_fs, I_OFFSET(dp)); 9608 offset = blkoff(ump->um_fs, I_OFFSET(dp)); 9609 pagedep_lookup(UFSTOVFS(ump), bp, dp->i_number, lbn, DEPALLOC, 9610 &pagedep); 9611 dirrem->dm_pagedep = pagedep; 9612 dirrem->dm_offset = offset; 9613 /* 9614 * If we're renaming a .. link to a new directory, cancel any 9615 * existing MKDIR_PARENT mkdir. If it has already been canceled 9616 * the jremref is preserved for any potential diradd in this 9617 * location. This can not coincide with a rmdir. 9618 */ 9619 if (I_OFFSET(dp) == DOTDOT_OFFSET) { 9620 if (isrmdir) 9621 panic("newdirrem: .. directory change during remove?"); 9622 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref); 9623 } 9624 /* 9625 * If we're removing a directory search for the .. dependency now and 9626 * cancel it. Any pending journal work will be added to the dirrem 9627 * to be completed when the workitem remove completes. 9628 */ 9629 if (isrmdir) 9630 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref); 9631 /* 9632 * Check for a diradd dependency for the same directory entry. 9633 * If present, then both dependencies become obsolete and can 9634 * be de-allocated. 9635 */ 9636 dap = diradd_lookup(pagedep, offset); 9637 if (dap == NULL) { 9638 /* 9639 * Link the jremref structures into the dirrem so they are 9640 * written prior to the pagedep. 9641 */ 9642 if (jremref) 9643 dirrem_journal(dirrem, jremref, dotremref, 9644 dotdotremref); 9645 return (dirrem); 9646 } 9647 /* 9648 * Must be ATTACHED at this point. 9649 */ 9650 if ((dap->da_state & ATTACHED) == 0) 9651 panic("newdirrem: not ATTACHED"); 9652 if (dap->da_newinum != ip->i_number) 9653 panic("newdirrem: inum %ju should be %ju", 9654 (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum); 9655 /* 9656 * If we are deleting a changed name that never made it to disk, 9657 * then return the dirrem describing the previous inode (which 9658 * represents the inode currently referenced from this entry on disk). 9659 */ 9660 if ((dap->da_state & DIRCHG) != 0) { 9661 *prevdirremp = dap->da_previous; 9662 dap->da_state &= ~DIRCHG; 9663 dap->da_pagedep = pagedep; 9664 } 9665 /* 9666 * We are deleting an entry that never made it to disk. 9667 * Mark it COMPLETE so we can delete its inode immediately. 9668 */ 9669 dirrem->dm_state |= COMPLETE; 9670 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref); 9671 #ifdef INVARIANTS 9672 if (isrmdir == 0) { 9673 struct worklist *wk; 9674 9675 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9676 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT)) 9677 panic("bad wk %p (0x%X)\n", wk, wk->wk_state); 9678 } 9679 #endif 9680 9681 return (dirrem); 9682 } 9683 9684 /* 9685 * Directory entry change dependencies. 9686 * 9687 * Changing an existing directory entry requires that an add operation 9688 * be completed first followed by a deletion. The semantics for the addition 9689 * are identical to the description of adding a new entry above except 9690 * that the rollback is to the old inode number rather than zero. Once 9691 * the addition dependency is completed, the removal is done as described 9692 * in the removal routine above. 9693 */ 9694 9695 /* 9696 * This routine should be called immediately after changing 9697 * a directory entry. The inode's link count should not be 9698 * decremented by the calling procedure -- the soft updates 9699 * code will perform this task when it is safe. 9700 */ 9701 void 9702 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 9703 struct buf *bp; /* buffer containing directory block */ 9704 struct inode *dp; /* inode for the directory being modified */ 9705 struct inode *ip; /* inode for directory entry being removed */ 9706 ino_t newinum; /* new inode number for changed entry */ 9707 int isrmdir; /* indicates if doing RMDIR */ 9708 { 9709 int offset; 9710 struct diradd *dap = NULL; 9711 struct dirrem *dirrem, *prevdirrem; 9712 struct pagedep *pagedep; 9713 struct inodedep *inodedep; 9714 struct jaddref *jaddref; 9715 struct mount *mp; 9716 struct ufsmount *ump; 9717 9718 mp = ITOVFS(dp); 9719 ump = VFSTOUFS(mp); 9720 offset = blkoff(ump->um_fs, I_OFFSET(dp)); 9721 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 9722 ("softdep_setup_directory_change called on non-softdep filesystem")); 9723 9724 /* 9725 * Whiteouts do not need diradd dependencies. 9726 */ 9727 if (newinum != UFS_WINO) { 9728 dap = malloc(sizeof(struct diradd), 9729 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO); 9730 workitem_alloc(&dap->da_list, D_DIRADD, mp); 9731 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE; 9732 dap->da_offset = offset; 9733 dap->da_newinum = newinum; 9734 LIST_INIT(&dap->da_jwork); 9735 } 9736 9737 /* 9738 * Allocate a new dirrem and ACQUIRE_LOCK. 9739 */ 9740 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 9741 pagedep = dirrem->dm_pagedep; 9742 /* 9743 * The possible values for isrmdir: 9744 * 0 - non-directory file rename 9745 * 1 - directory rename within same directory 9746 * inum - directory rename to new directory of given inode number 9747 * When renaming to a new directory, we are both deleting and 9748 * creating a new directory entry, so the link count on the new 9749 * directory should not change. Thus we do not need the followup 9750 * dirrem which is usually done in handle_workitem_remove. We set 9751 * the DIRCHG flag to tell handle_workitem_remove to skip the 9752 * followup dirrem. 9753 */ 9754 if (isrmdir > 1) 9755 dirrem->dm_state |= DIRCHG; 9756 9757 /* 9758 * Whiteouts have no additional dependencies, 9759 * so just put the dirrem on the correct list. 9760 */ 9761 if (newinum == UFS_WINO) { 9762 if ((dirrem->dm_state & COMPLETE) == 0) { 9763 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem, 9764 dm_next); 9765 } else { 9766 dirrem->dm_dirinum = pagedep->pd_ino; 9767 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9768 add_to_worklist(&dirrem->dm_list, 0); 9769 } 9770 FREE_LOCK(ump); 9771 return; 9772 } 9773 /* 9774 * Add the dirrem to the inodedep's pending remove list for quick 9775 * discovery later. A valid nlinkdelta ensures that this lookup 9776 * will not fail. 9777 */ 9778 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9779 panic("softdep_setup_directory_change: Lost inodedep."); 9780 dirrem->dm_state |= ONDEPLIST; 9781 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9782 9783 /* 9784 * If the COMPLETE flag is clear, then there were no active 9785 * entries and we want to roll back to the previous inode until 9786 * the new inode is committed to disk. If the COMPLETE flag is 9787 * set, then we have deleted an entry that never made it to disk. 9788 * If the entry we deleted resulted from a name change, then the old 9789 * inode reference still resides on disk. Any rollback that we do 9790 * needs to be to that old inode (returned to us in prevdirrem). If 9791 * the entry we deleted resulted from a create, then there is 9792 * no entry on the disk, so we want to roll back to zero rather 9793 * than the uncommitted inode. In either of the COMPLETE cases we 9794 * want to immediately free the unwritten and unreferenced inode. 9795 */ 9796 if ((dirrem->dm_state & COMPLETE) == 0) { 9797 dap->da_previous = dirrem; 9798 } else { 9799 if (prevdirrem != NULL) { 9800 dap->da_previous = prevdirrem; 9801 } else { 9802 dap->da_state &= ~DIRCHG; 9803 dap->da_pagedep = pagedep; 9804 } 9805 dirrem->dm_dirinum = pagedep->pd_ino; 9806 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9807 add_to_worklist(&dirrem->dm_list, 0); 9808 } 9809 /* 9810 * Lookup the jaddref for this journal entry. We must finish 9811 * initializing it and make the diradd write dependent on it. 9812 * If we're not journaling, put it on the id_bufwait list if the 9813 * inode is not yet written. If it is written, do the post-inode 9814 * write processing to put it on the id_pendinghd list. 9815 */ 9816 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 9817 if (MOUNTEDSUJ(mp)) { 9818 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 9819 inoreflst); 9820 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 9821 ("softdep_setup_directory_change: bad jaddref %p", 9822 jaddref)); 9823 jaddref->ja_diroff = I_OFFSET(dp); 9824 jaddref->ja_diradd = dap; 9825 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9826 dap, da_pdlist); 9827 add_to_journal(&jaddref->ja_list); 9828 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 9829 dap->da_state |= COMPLETE; 9830 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 9831 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 9832 } else { 9833 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9834 dap, da_pdlist); 9835 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 9836 } 9837 /* 9838 * If we're making a new name for a directory that has not been 9839 * committed when need to move the dot and dotdot references to 9840 * this new name. 9841 */ 9842 if (inodedep->id_mkdiradd && I_OFFSET(dp) != DOTDOT_OFFSET) 9843 merge_diradd(inodedep, dap); 9844 FREE_LOCK(ump); 9845 } 9846 9847 /* 9848 * Called whenever the link count on an inode is changed. 9849 * It creates an inode dependency so that the new reference(s) 9850 * to the inode cannot be committed to disk until the updated 9851 * inode has been written. 9852 */ 9853 void 9854 softdep_change_linkcnt(ip) 9855 struct inode *ip; /* the inode with the increased link count */ 9856 { 9857 struct inodedep *inodedep; 9858 struct ufsmount *ump; 9859 9860 ump = ITOUMP(ip); 9861 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9862 ("softdep_change_linkcnt called on non-softdep filesystem")); 9863 ACQUIRE_LOCK(ump); 9864 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep); 9865 if (ip->i_nlink < ip->i_effnlink) 9866 panic("softdep_change_linkcnt: bad delta"); 9867 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9868 FREE_LOCK(ump); 9869 } 9870 9871 /* 9872 * Attach a sbdep dependency to the superblock buf so that we can keep 9873 * track of the head of the linked list of referenced but unlinked inodes. 9874 */ 9875 void 9876 softdep_setup_sbupdate(ump, fs, bp) 9877 struct ufsmount *ump; 9878 struct fs *fs; 9879 struct buf *bp; 9880 { 9881 struct sbdep *sbdep; 9882 struct worklist *wk; 9883 9884 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9885 ("softdep_setup_sbupdate called on non-softdep filesystem")); 9886 LIST_FOREACH(wk, &bp->b_dep, wk_list) 9887 if (wk->wk_type == D_SBDEP) 9888 break; 9889 if (wk != NULL) 9890 return; 9891 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS); 9892 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump)); 9893 sbdep->sb_fs = fs; 9894 sbdep->sb_ump = ump; 9895 ACQUIRE_LOCK(ump); 9896 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list); 9897 FREE_LOCK(ump); 9898 } 9899 9900 /* 9901 * Return the first unlinked inodedep which is ready to be the head of the 9902 * list. The inodedep and all those after it must have valid next pointers. 9903 */ 9904 static struct inodedep * 9905 first_unlinked_inodedep(ump) 9906 struct ufsmount *ump; 9907 { 9908 struct inodedep *inodedep; 9909 struct inodedep *idp; 9910 9911 LOCK_OWNED(ump); 9912 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst); 9913 inodedep; inodedep = idp) { 9914 if ((inodedep->id_state & UNLINKNEXT) == 0) 9915 return (NULL); 9916 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9917 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0) 9918 break; 9919 if ((inodedep->id_state & UNLINKPREV) == 0) 9920 break; 9921 } 9922 return (inodedep); 9923 } 9924 9925 /* 9926 * Set the sujfree unlinked head pointer prior to writing a superblock. 9927 */ 9928 static void 9929 initiate_write_sbdep(sbdep) 9930 struct sbdep *sbdep; 9931 { 9932 struct inodedep *inodedep; 9933 struct fs *bpfs; 9934 struct fs *fs; 9935 9936 bpfs = sbdep->sb_fs; 9937 fs = sbdep->sb_ump->um_fs; 9938 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9939 if (inodedep) { 9940 fs->fs_sujfree = inodedep->id_ino; 9941 inodedep->id_state |= UNLINKPREV; 9942 } else 9943 fs->fs_sujfree = 0; 9944 bpfs->fs_sujfree = fs->fs_sujfree; 9945 /* 9946 * Because we have made changes to the superblock, we need to 9947 * recompute its check-hash. 9948 */ 9949 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs); 9950 } 9951 9952 /* 9953 * After a superblock is written determine whether it must be written again 9954 * due to a changing unlinked list head. 9955 */ 9956 static int 9957 handle_written_sbdep(sbdep, bp) 9958 struct sbdep *sbdep; 9959 struct buf *bp; 9960 { 9961 struct inodedep *inodedep; 9962 struct fs *fs; 9963 9964 LOCK_OWNED(sbdep->sb_ump); 9965 fs = sbdep->sb_fs; 9966 /* 9967 * If the superblock doesn't match the in-memory list start over. 9968 */ 9969 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9970 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) || 9971 (inodedep == NULL && fs->fs_sujfree != 0)) { 9972 bdirty(bp); 9973 return (1); 9974 } 9975 WORKITEM_FREE(sbdep, D_SBDEP); 9976 if (fs->fs_sujfree == 0) 9977 return (0); 9978 /* 9979 * Now that we have a record of this inode in stable store allow it 9980 * to be written to free up pending work. Inodes may see a lot of 9981 * write activity after they are unlinked which we must not hold up. 9982 */ 9983 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 9984 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS) 9985 panic("handle_written_sbdep: Bad inodedep %p (0x%X)", 9986 inodedep, inodedep->id_state); 9987 if (inodedep->id_state & UNLINKONLIST) 9988 break; 9989 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST; 9990 } 9991 9992 return (0); 9993 } 9994 9995 /* 9996 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list. 9997 */ 9998 static void 9999 unlinked_inodedep(mp, inodedep) 10000 struct mount *mp; 10001 struct inodedep *inodedep; 10002 { 10003 struct ufsmount *ump; 10004 10005 ump = VFSTOUFS(mp); 10006 LOCK_OWNED(ump); 10007 if (MOUNTEDSUJ(mp) == 0) 10008 return; 10009 ump->um_fs->fs_fmod = 1; 10010 if (inodedep->id_state & UNLINKED) 10011 panic("unlinked_inodedep: %p already unlinked\n", inodedep); 10012 inodedep->id_state |= UNLINKED; 10013 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked); 10014 } 10015 10016 /* 10017 * Remove an inodedep from the unlinked inodedep list. This may require 10018 * disk writes if the inode has made it that far. 10019 */ 10020 static void 10021 clear_unlinked_inodedep(inodedep) 10022 struct inodedep *inodedep; 10023 { 10024 struct ufs2_dinode *dip; 10025 struct ufsmount *ump; 10026 struct inodedep *idp; 10027 struct inodedep *idn; 10028 struct fs *fs, *bpfs; 10029 struct buf *bp; 10030 daddr_t dbn; 10031 ino_t ino; 10032 ino_t nino; 10033 ino_t pino; 10034 int error; 10035 10036 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10037 fs = ump->um_fs; 10038 ino = inodedep->id_ino; 10039 error = 0; 10040 for (;;) { 10041 LOCK_OWNED(ump); 10042 KASSERT((inodedep->id_state & UNLINKED) != 0, 10043 ("clear_unlinked_inodedep: inodedep %p not unlinked", 10044 inodedep)); 10045 /* 10046 * If nothing has yet been written simply remove us from 10047 * the in memory list and return. This is the most common 10048 * case where handle_workitem_remove() loses the final 10049 * reference. 10050 */ 10051 if ((inodedep->id_state & UNLINKLINKS) == 0) 10052 break; 10053 /* 10054 * If we have a NEXT pointer and no PREV pointer we can simply 10055 * clear NEXT's PREV and remove ourselves from the list. Be 10056 * careful not to clear PREV if the superblock points at 10057 * next as well. 10058 */ 10059 idn = TAILQ_NEXT(inodedep, id_unlinked); 10060 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) { 10061 if (idn && fs->fs_sujfree != idn->id_ino) 10062 idn->id_state &= ~UNLINKPREV; 10063 break; 10064 } 10065 /* 10066 * Here we have an inodedep which is actually linked into 10067 * the list. We must remove it by forcing a write to the 10068 * link before us, whether it be the superblock or an inode. 10069 * Unfortunately the list may change while we're waiting 10070 * on the buf lock for either resource so we must loop until 10071 * we lock the right one. If both the superblock and an 10072 * inode point to this inode we must clear the inode first 10073 * followed by the superblock. 10074 */ 10075 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 10076 pino = 0; 10077 if (idp && (idp->id_state & UNLINKNEXT)) 10078 pino = idp->id_ino; 10079 FREE_LOCK(ump); 10080 if (pino == 0) { 10081 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 10082 (int)fs->fs_sbsize, 0, 0, 0); 10083 } else { 10084 dbn = fsbtodb(fs, ino_to_fsba(fs, pino)); 10085 error = ffs_breadz(ump, ump->um_devvp, dbn, dbn, 10086 (int)fs->fs_bsize, NULL, NULL, 0, NOCRED, 0, NULL, 10087 &bp); 10088 } 10089 ACQUIRE_LOCK(ump); 10090 if (error) 10091 break; 10092 /* If the list has changed restart the loop. */ 10093 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 10094 nino = 0; 10095 if (idp && (idp->id_state & UNLINKNEXT)) 10096 nino = idp->id_ino; 10097 if (nino != pino || 10098 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) { 10099 FREE_LOCK(ump); 10100 brelse(bp); 10101 ACQUIRE_LOCK(ump); 10102 continue; 10103 } 10104 nino = 0; 10105 idn = TAILQ_NEXT(inodedep, id_unlinked); 10106 if (idn) 10107 nino = idn->id_ino; 10108 /* 10109 * Remove us from the in memory list. After this we cannot 10110 * access the inodedep. 10111 */ 10112 KASSERT((inodedep->id_state & UNLINKED) != 0, 10113 ("clear_unlinked_inodedep: inodedep %p not unlinked", 10114 inodedep)); 10115 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 10116 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 10117 FREE_LOCK(ump); 10118 /* 10119 * The predecessor's next pointer is manually updated here 10120 * so that the NEXT flag is never cleared for an element 10121 * that is in the list. 10122 */ 10123 if (pino == 0) { 10124 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 10125 bpfs = (struct fs *)bp->b_data; 10126 ffs_oldfscompat_write(bpfs, ump); 10127 softdep_setup_sbupdate(ump, bpfs, bp); 10128 /* 10129 * Because we may have made changes to the superblock, 10130 * we need to recompute its check-hash. 10131 */ 10132 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs); 10133 } else if (fs->fs_magic == FS_UFS1_MAGIC) { 10134 ((struct ufs1_dinode *)bp->b_data + 10135 ino_to_fsbo(fs, pino))->di_freelink = nino; 10136 } else { 10137 dip = (struct ufs2_dinode *)bp->b_data + 10138 ino_to_fsbo(fs, pino); 10139 dip->di_freelink = nino; 10140 ffs_update_dinode_ckhash(fs, dip); 10141 } 10142 /* 10143 * If the bwrite fails we have no recourse to recover. The 10144 * filesystem is corrupted already. 10145 */ 10146 bwrite(bp); 10147 ACQUIRE_LOCK(ump); 10148 /* 10149 * If the superblock pointer still needs to be cleared force 10150 * a write here. 10151 */ 10152 if (fs->fs_sujfree == ino) { 10153 FREE_LOCK(ump); 10154 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 10155 (int)fs->fs_sbsize, 0, 0, 0); 10156 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 10157 bpfs = (struct fs *)bp->b_data; 10158 ffs_oldfscompat_write(bpfs, ump); 10159 softdep_setup_sbupdate(ump, bpfs, bp); 10160 /* 10161 * Because we may have made changes to the superblock, 10162 * we need to recompute its check-hash. 10163 */ 10164 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs); 10165 bwrite(bp); 10166 ACQUIRE_LOCK(ump); 10167 } 10168 10169 if (fs->fs_sujfree != ino) 10170 return; 10171 panic("clear_unlinked_inodedep: Failed to clear free head"); 10172 } 10173 if (inodedep->id_ino == fs->fs_sujfree) 10174 panic("clear_unlinked_inodedep: Freeing head of free list"); 10175 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 10176 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 10177 return; 10178 } 10179 10180 /* 10181 * This workitem decrements the inode's link count. 10182 * If the link count reaches zero, the file is removed. 10183 */ 10184 static int 10185 handle_workitem_remove(dirrem, flags) 10186 struct dirrem *dirrem; 10187 int flags; 10188 { 10189 struct inodedep *inodedep; 10190 struct workhead dotdotwk; 10191 struct worklist *wk; 10192 struct ufsmount *ump; 10193 struct mount *mp; 10194 struct vnode *vp; 10195 struct inode *ip; 10196 ino_t oldinum; 10197 10198 if (dirrem->dm_state & ONWORKLIST) 10199 panic("handle_workitem_remove: dirrem %p still on worklist", 10200 dirrem); 10201 oldinum = dirrem->dm_oldinum; 10202 mp = dirrem->dm_list.wk_mp; 10203 ump = VFSTOUFS(mp); 10204 flags |= LK_EXCLUSIVE; 10205 if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ | 10206 FFSV_FORCEINODEDEP) != 0) 10207 return (EBUSY); 10208 ip = VTOI(vp); 10209 MPASS(ip->i_mode != 0); 10210 ACQUIRE_LOCK(ump); 10211 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0) 10212 panic("handle_workitem_remove: lost inodedep"); 10213 if (dirrem->dm_state & ONDEPLIST) 10214 LIST_REMOVE(dirrem, dm_inonext); 10215 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 10216 ("handle_workitem_remove: Journal entries not written.")); 10217 10218 /* 10219 * Move all dependencies waiting on the remove to complete 10220 * from the dirrem to the inode inowait list to be completed 10221 * after the inode has been updated and written to disk. 10222 * 10223 * Any marked MKDIR_PARENT are saved to be completed when the 10224 * dotdot ref is removed unless DIRCHG is specified. For 10225 * directory change operations there will be no further 10226 * directory writes and the jsegdeps need to be moved along 10227 * with the rest to be completed when the inode is free or 10228 * stable in the inode free list. 10229 */ 10230 LIST_INIT(&dotdotwk); 10231 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) { 10232 WORKLIST_REMOVE(wk); 10233 if ((dirrem->dm_state & DIRCHG) == 0 && 10234 wk->wk_state & MKDIR_PARENT) { 10235 wk->wk_state &= ~MKDIR_PARENT; 10236 WORKLIST_INSERT(&dotdotwk, wk); 10237 continue; 10238 } 10239 WORKLIST_INSERT(&inodedep->id_inowait, wk); 10240 } 10241 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list); 10242 /* 10243 * Normal file deletion. 10244 */ 10245 if ((dirrem->dm_state & RMDIR) == 0) { 10246 ip->i_nlink--; 10247 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: file ino " 10248 "%ju negative i_nlink %d", (intmax_t)ip->i_number, 10249 ip->i_nlink)); 10250 DIP_SET(ip, i_nlink, ip->i_nlink); 10251 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 10252 if (ip->i_nlink < ip->i_effnlink) 10253 panic("handle_workitem_remove: bad file delta"); 10254 if (ip->i_nlink == 0) 10255 unlinked_inodedep(mp, inodedep); 10256 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 10257 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 10258 ("handle_workitem_remove: worklist not empty. %s", 10259 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type))); 10260 WORKITEM_FREE(dirrem, D_DIRREM); 10261 FREE_LOCK(ump); 10262 goto out; 10263 } 10264 /* 10265 * Directory deletion. Decrement reference count for both the 10266 * just deleted parent directory entry and the reference for ".". 10267 * Arrange to have the reference count on the parent decremented 10268 * to account for the loss of "..". 10269 */ 10270 ip->i_nlink -= 2; 10271 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: directory ino " 10272 "%ju negative i_nlink %d", (intmax_t)ip->i_number, ip->i_nlink)); 10273 DIP_SET(ip, i_nlink, ip->i_nlink); 10274 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 10275 if (ip->i_nlink < ip->i_effnlink) 10276 panic("handle_workitem_remove: bad dir delta"); 10277 if (ip->i_nlink == 0) 10278 unlinked_inodedep(mp, inodedep); 10279 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 10280 /* 10281 * Rename a directory to a new parent. Since, we are both deleting 10282 * and creating a new directory entry, the link count on the new 10283 * directory should not change. Thus we skip the followup dirrem. 10284 */ 10285 if (dirrem->dm_state & DIRCHG) { 10286 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 10287 ("handle_workitem_remove: DIRCHG and worklist not empty.")); 10288 WORKITEM_FREE(dirrem, D_DIRREM); 10289 FREE_LOCK(ump); 10290 goto out; 10291 } 10292 dirrem->dm_state = ONDEPLIST; 10293 dirrem->dm_oldinum = dirrem->dm_dirinum; 10294 /* 10295 * Place the dirrem on the parent's diremhd list. 10296 */ 10297 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0) 10298 panic("handle_workitem_remove: lost dir inodedep"); 10299 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 10300 /* 10301 * If the allocated inode has never been written to disk, then 10302 * the on-disk inode is zero'ed and we can remove the file 10303 * immediately. When journaling if the inode has been marked 10304 * unlinked and not DEPCOMPLETE we know it can never be written. 10305 */ 10306 inodedep_lookup(mp, oldinum, 0, &inodedep); 10307 if (inodedep == NULL || 10308 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED || 10309 check_inode_unwritten(inodedep)) { 10310 FREE_LOCK(ump); 10311 vput(vp); 10312 return handle_workitem_remove(dirrem, flags); 10313 } 10314 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list); 10315 FREE_LOCK(ump); 10316 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 10317 out: 10318 ffs_update(vp, 0); 10319 vput(vp); 10320 return (0); 10321 } 10322 10323 /* 10324 * Inode de-allocation dependencies. 10325 * 10326 * When an inode's link count is reduced to zero, it can be de-allocated. We 10327 * found it convenient to postpone de-allocation until after the inode is 10328 * written to disk with its new link count (zero). At this point, all of the 10329 * on-disk inode's block pointers are nullified and, with careful dependency 10330 * list ordering, all dependencies related to the inode will be satisfied and 10331 * the corresponding dependency structures de-allocated. So, if/when the 10332 * inode is reused, there will be no mixing of old dependencies with new 10333 * ones. This artificial dependency is set up by the block de-allocation 10334 * procedure above (softdep_setup_freeblocks) and completed by the 10335 * following procedure. 10336 */ 10337 static void 10338 handle_workitem_freefile(freefile) 10339 struct freefile *freefile; 10340 { 10341 struct workhead wkhd; 10342 struct fs *fs; 10343 struct ufsmount *ump; 10344 int error; 10345 #ifdef INVARIANTS 10346 struct inodedep *idp; 10347 #endif 10348 10349 ump = VFSTOUFS(freefile->fx_list.wk_mp); 10350 fs = ump->um_fs; 10351 #ifdef INVARIANTS 10352 ACQUIRE_LOCK(ump); 10353 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp); 10354 FREE_LOCK(ump); 10355 if (error) 10356 panic("handle_workitem_freefile: inodedep %p survived", idp); 10357 #endif 10358 UFS_LOCK(ump); 10359 fs->fs_pendinginodes -= 1; 10360 UFS_UNLOCK(ump); 10361 LIST_INIT(&wkhd); 10362 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list); 10363 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp, 10364 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0) 10365 softdep_error("handle_workitem_freefile", error); 10366 ACQUIRE_LOCK(ump); 10367 WORKITEM_FREE(freefile, D_FREEFILE); 10368 FREE_LOCK(ump); 10369 } 10370 10371 /* 10372 * Helper function which unlinks marker element from work list and returns 10373 * the next element on the list. 10374 */ 10375 static __inline struct worklist * 10376 markernext(struct worklist *marker) 10377 { 10378 struct worklist *next; 10379 10380 next = LIST_NEXT(marker, wk_list); 10381 LIST_REMOVE(marker, wk_list); 10382 return next; 10383 } 10384 10385 /* 10386 * Disk writes. 10387 * 10388 * The dependency structures constructed above are most actively used when file 10389 * system blocks are written to disk. No constraints are placed on when a 10390 * block can be written, but unsatisfied update dependencies are made safe by 10391 * modifying (or replacing) the source memory for the duration of the disk 10392 * write. When the disk write completes, the memory block is again brought 10393 * up-to-date. 10394 * 10395 * In-core inode structure reclamation. 10396 * 10397 * Because there are a finite number of "in-core" inode structures, they are 10398 * reused regularly. By transferring all inode-related dependencies to the 10399 * in-memory inode block and indexing them separately (via "inodedep"s), we 10400 * can allow "in-core" inode structures to be reused at any time and avoid 10401 * any increase in contention. 10402 * 10403 * Called just before entering the device driver to initiate a new disk I/O. 10404 * The buffer must be locked, thus, no I/O completion operations can occur 10405 * while we are manipulating its associated dependencies. 10406 */ 10407 static void 10408 softdep_disk_io_initiation(bp) 10409 struct buf *bp; /* structure describing disk write to occur */ 10410 { 10411 struct worklist *wk; 10412 struct worklist marker; 10413 struct inodedep *inodedep; 10414 struct freeblks *freeblks; 10415 struct jblkdep *jblkdep; 10416 struct newblk *newblk; 10417 struct ufsmount *ump; 10418 10419 /* 10420 * We only care about write operations. There should never 10421 * be dependencies for reads. 10422 */ 10423 if (bp->b_iocmd != BIO_WRITE) 10424 panic("softdep_disk_io_initiation: not write"); 10425 10426 if (bp->b_vflags & BV_BKGRDINPROG) 10427 panic("softdep_disk_io_initiation: Writing buffer with " 10428 "background write in progress: %p", bp); 10429 10430 ump = softdep_bp_to_mp(bp); 10431 if (ump == NULL) 10432 return; 10433 10434 marker.wk_type = D_LAST + 1; /* Not a normal workitem */ 10435 PHOLD(curproc); /* Don't swap out kernel stack */ 10436 ACQUIRE_LOCK(ump); 10437 /* 10438 * Do any necessary pre-I/O processing. 10439 */ 10440 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL; 10441 wk = markernext(&marker)) { 10442 LIST_INSERT_AFTER(wk, &marker, wk_list); 10443 switch (wk->wk_type) { 10444 case D_PAGEDEP: 10445 initiate_write_filepage(WK_PAGEDEP(wk), bp); 10446 continue; 10447 10448 case D_INODEDEP: 10449 inodedep = WK_INODEDEP(wk); 10450 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) 10451 initiate_write_inodeblock_ufs1(inodedep, bp); 10452 else 10453 initiate_write_inodeblock_ufs2(inodedep, bp); 10454 continue; 10455 10456 case D_INDIRDEP: 10457 initiate_write_indirdep(WK_INDIRDEP(wk), bp); 10458 continue; 10459 10460 case D_BMSAFEMAP: 10461 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp); 10462 continue; 10463 10464 case D_JSEG: 10465 WK_JSEG(wk)->js_buf = NULL; 10466 continue; 10467 10468 case D_FREEBLKS: 10469 freeblks = WK_FREEBLKS(wk); 10470 jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd); 10471 /* 10472 * We have to wait for the freeblks to be journaled 10473 * before we can write an inodeblock with updated 10474 * pointers. Be careful to arrange the marker so 10475 * we revisit the freeblks if it's not removed by 10476 * the first jwait(). 10477 */ 10478 if (jblkdep != NULL) { 10479 LIST_REMOVE(&marker, wk_list); 10480 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10481 jwait(&jblkdep->jb_list, MNT_WAIT); 10482 } 10483 continue; 10484 case D_ALLOCDIRECT: 10485 case D_ALLOCINDIR: 10486 /* 10487 * We have to wait for the jnewblk to be journaled 10488 * before we can write to a block if the contents 10489 * may be confused with an earlier file's indirect 10490 * at recovery time. Handle the marker as described 10491 * above. 10492 */ 10493 newblk = WK_NEWBLK(wk); 10494 if (newblk->nb_jnewblk != NULL && 10495 indirblk_lookup(newblk->nb_list.wk_mp, 10496 newblk->nb_newblkno)) { 10497 LIST_REMOVE(&marker, wk_list); 10498 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10499 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 10500 } 10501 continue; 10502 10503 case D_SBDEP: 10504 initiate_write_sbdep(WK_SBDEP(wk)); 10505 continue; 10506 10507 case D_MKDIR: 10508 case D_FREEWORK: 10509 case D_FREEDEP: 10510 case D_JSEGDEP: 10511 continue; 10512 10513 default: 10514 panic("handle_disk_io_initiation: Unexpected type %s", 10515 TYPENAME(wk->wk_type)); 10516 /* NOTREACHED */ 10517 } 10518 } 10519 FREE_LOCK(ump); 10520 PRELE(curproc); /* Allow swapout of kernel stack */ 10521 } 10522 10523 /* 10524 * Called from within the procedure above to deal with unsatisfied 10525 * allocation dependencies in a directory. The buffer must be locked, 10526 * thus, no I/O completion operations can occur while we are 10527 * manipulating its associated dependencies. 10528 */ 10529 static void 10530 initiate_write_filepage(pagedep, bp) 10531 struct pagedep *pagedep; 10532 struct buf *bp; 10533 { 10534 struct jremref *jremref; 10535 struct jmvref *jmvref; 10536 struct dirrem *dirrem; 10537 struct diradd *dap; 10538 struct direct *ep; 10539 int i; 10540 10541 if (pagedep->pd_state & IOSTARTED) { 10542 /* 10543 * This can only happen if there is a driver that does not 10544 * understand chaining. Here biodone will reissue the call 10545 * to strategy for the incomplete buffers. 10546 */ 10547 printf("initiate_write_filepage: already started\n"); 10548 return; 10549 } 10550 pagedep->pd_state |= IOSTARTED; 10551 /* 10552 * Wait for all journal remove dependencies to hit the disk. 10553 * We can not allow any potentially conflicting directory adds 10554 * to be visible before removes and rollback is too difficult. 10555 * The per-filesystem lock may be dropped and re-acquired, however 10556 * we hold the buf locked so the dependency can not go away. 10557 */ 10558 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) 10559 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) 10560 jwait(&jremref->jr_list, MNT_WAIT); 10561 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) 10562 jwait(&jmvref->jm_list, MNT_WAIT); 10563 for (i = 0; i < DAHASHSZ; i++) { 10564 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 10565 ep = (struct direct *) 10566 ((char *)bp->b_data + dap->da_offset); 10567 if (ep->d_ino != dap->da_newinum) 10568 panic("%s: dir inum %ju != new %ju", 10569 "initiate_write_filepage", 10570 (uintmax_t)ep->d_ino, 10571 (uintmax_t)dap->da_newinum); 10572 if (dap->da_state & DIRCHG) 10573 ep->d_ino = dap->da_previous->dm_oldinum; 10574 else 10575 ep->d_ino = 0; 10576 dap->da_state &= ~ATTACHED; 10577 dap->da_state |= UNDONE; 10578 } 10579 } 10580 } 10581 10582 /* 10583 * Version of initiate_write_inodeblock that handles UFS1 dinodes. 10584 * Note that any bug fixes made to this routine must be done in the 10585 * version found below. 10586 * 10587 * Called from within the procedure above to deal with unsatisfied 10588 * allocation dependencies in an inodeblock. The buffer must be 10589 * locked, thus, no I/O completion operations can occur while we 10590 * are manipulating its associated dependencies. 10591 */ 10592 static void 10593 initiate_write_inodeblock_ufs1(inodedep, bp) 10594 struct inodedep *inodedep; 10595 struct buf *bp; /* The inode block */ 10596 { 10597 struct allocdirect *adp, *lastadp; 10598 struct ufs1_dinode *dp; 10599 struct ufs1_dinode *sip; 10600 struct inoref *inoref; 10601 struct ufsmount *ump; 10602 struct fs *fs; 10603 ufs_lbn_t i; 10604 #ifdef INVARIANTS 10605 ufs_lbn_t prevlbn = 0; 10606 #endif 10607 int deplist; 10608 10609 if (inodedep->id_state & IOSTARTED) 10610 panic("initiate_write_inodeblock_ufs1: already started"); 10611 inodedep->id_state |= IOSTARTED; 10612 fs = inodedep->id_fs; 10613 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10614 LOCK_OWNED(ump); 10615 dp = (struct ufs1_dinode *)bp->b_data + 10616 ino_to_fsbo(fs, inodedep->id_ino); 10617 10618 /* 10619 * If we're on the unlinked list but have not yet written our 10620 * next pointer initialize it here. 10621 */ 10622 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10623 struct inodedep *inon; 10624 10625 inon = TAILQ_NEXT(inodedep, id_unlinked); 10626 dp->di_freelink = inon ? inon->id_ino : 0; 10627 } 10628 /* 10629 * If the bitmap is not yet written, then the allocated 10630 * inode cannot be written to disk. 10631 */ 10632 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10633 if (inodedep->id_savedino1 != NULL) 10634 panic("initiate_write_inodeblock_ufs1: I/O underway"); 10635 FREE_LOCK(ump); 10636 sip = malloc(sizeof(struct ufs1_dinode), 10637 M_SAVEDINO, M_SOFTDEP_FLAGS); 10638 ACQUIRE_LOCK(ump); 10639 inodedep->id_savedino1 = sip; 10640 *inodedep->id_savedino1 = *dp; 10641 bzero((caddr_t)dp, sizeof(struct ufs1_dinode)); 10642 dp->di_gen = inodedep->id_savedino1->di_gen; 10643 dp->di_freelink = inodedep->id_savedino1->di_freelink; 10644 return; 10645 } 10646 /* 10647 * If no dependencies, then there is nothing to roll back. 10648 */ 10649 inodedep->id_savedsize = dp->di_size; 10650 inodedep->id_savedextsize = 0; 10651 inodedep->id_savednlink = dp->di_nlink; 10652 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10653 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10654 return; 10655 /* 10656 * Revert the link count to that of the first unwritten journal entry. 10657 */ 10658 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10659 if (inoref) 10660 dp->di_nlink = inoref->if_nlink; 10661 /* 10662 * Set the dependencies to busy. 10663 */ 10664 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10665 adp = TAILQ_NEXT(adp, ad_next)) { 10666 #ifdef INVARIANTS 10667 if (deplist != 0 && prevlbn >= adp->ad_offset) 10668 panic("softdep_write_inodeblock: lbn order"); 10669 prevlbn = adp->ad_offset; 10670 if (adp->ad_offset < UFS_NDADDR && 10671 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10672 panic("initiate_write_inodeblock_ufs1: " 10673 "direct pointer #%jd mismatch %d != %jd", 10674 (intmax_t)adp->ad_offset, 10675 dp->di_db[adp->ad_offset], 10676 (intmax_t)adp->ad_newblkno); 10677 if (adp->ad_offset >= UFS_NDADDR && 10678 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno) 10679 panic("initiate_write_inodeblock_ufs1: " 10680 "indirect pointer #%jd mismatch %d != %jd", 10681 (intmax_t)adp->ad_offset - UFS_NDADDR, 10682 dp->di_ib[adp->ad_offset - UFS_NDADDR], 10683 (intmax_t)adp->ad_newblkno); 10684 deplist |= 1 << adp->ad_offset; 10685 if ((adp->ad_state & ATTACHED) == 0) 10686 panic("initiate_write_inodeblock_ufs1: " 10687 "Unknown state 0x%x", adp->ad_state); 10688 #endif /* INVARIANTS */ 10689 adp->ad_state &= ~ATTACHED; 10690 adp->ad_state |= UNDONE; 10691 } 10692 /* 10693 * The on-disk inode cannot claim to be any larger than the last 10694 * fragment that has been written. Otherwise, the on-disk inode 10695 * might have fragments that were not the last block in the file 10696 * which would corrupt the filesystem. 10697 */ 10698 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10699 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10700 if (adp->ad_offset >= UFS_NDADDR) 10701 break; 10702 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10703 /* keep going until hitting a rollback to a frag */ 10704 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10705 continue; 10706 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10707 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) { 10708 #ifdef INVARIANTS 10709 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10710 panic("initiate_write_inodeblock_ufs1: " 10711 "lost dep1"); 10712 #endif /* INVARIANTS */ 10713 dp->di_db[i] = 0; 10714 } 10715 for (i = 0; i < UFS_NIADDR; i++) { 10716 #ifdef INVARIANTS 10717 if (dp->di_ib[i] != 0 && 10718 (deplist & ((1 << UFS_NDADDR) << i)) == 0) 10719 panic("initiate_write_inodeblock_ufs1: " 10720 "lost dep2"); 10721 #endif /* INVARIANTS */ 10722 dp->di_ib[i] = 0; 10723 } 10724 return; 10725 } 10726 /* 10727 * If we have zero'ed out the last allocated block of the file, 10728 * roll back the size to the last currently allocated block. 10729 * We know that this last allocated block is a full-sized as 10730 * we already checked for fragments in the loop above. 10731 */ 10732 if (lastadp != NULL && 10733 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10734 for (i = lastadp->ad_offset; i >= 0; i--) 10735 if (dp->di_db[i] != 0) 10736 break; 10737 dp->di_size = (i + 1) * fs->fs_bsize; 10738 } 10739 /* 10740 * The only dependencies are for indirect blocks. 10741 * 10742 * The file size for indirect block additions is not guaranteed. 10743 * Such a guarantee would be non-trivial to achieve. The conventional 10744 * synchronous write implementation also does not make this guarantee. 10745 * Fsck should catch and fix discrepancies. Arguably, the file size 10746 * can be over-estimated without destroying integrity when the file 10747 * moves into the indirect blocks (i.e., is large). If we want to 10748 * postpone fsck, we are stuck with this argument. 10749 */ 10750 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10751 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0; 10752 } 10753 10754 /* 10755 * Version of initiate_write_inodeblock that handles UFS2 dinodes. 10756 * Note that any bug fixes made to this routine must be done in the 10757 * version found above. 10758 * 10759 * Called from within the procedure above to deal with unsatisfied 10760 * allocation dependencies in an inodeblock. The buffer must be 10761 * locked, thus, no I/O completion operations can occur while we 10762 * are manipulating its associated dependencies. 10763 */ 10764 static void 10765 initiate_write_inodeblock_ufs2(inodedep, bp) 10766 struct inodedep *inodedep; 10767 struct buf *bp; /* The inode block */ 10768 { 10769 struct allocdirect *adp, *lastadp; 10770 struct ufs2_dinode *dp; 10771 struct ufs2_dinode *sip; 10772 struct inoref *inoref; 10773 struct ufsmount *ump; 10774 struct fs *fs; 10775 ufs_lbn_t i; 10776 #ifdef INVARIANTS 10777 ufs_lbn_t prevlbn = 0; 10778 #endif 10779 int deplist; 10780 10781 if (inodedep->id_state & IOSTARTED) 10782 panic("initiate_write_inodeblock_ufs2: already started"); 10783 inodedep->id_state |= IOSTARTED; 10784 fs = inodedep->id_fs; 10785 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10786 LOCK_OWNED(ump); 10787 dp = (struct ufs2_dinode *)bp->b_data + 10788 ino_to_fsbo(fs, inodedep->id_ino); 10789 10790 /* 10791 * If we're on the unlinked list but have not yet written our 10792 * next pointer initialize it here. 10793 */ 10794 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10795 struct inodedep *inon; 10796 10797 inon = TAILQ_NEXT(inodedep, id_unlinked); 10798 dp->di_freelink = inon ? inon->id_ino : 0; 10799 ffs_update_dinode_ckhash(fs, dp); 10800 } 10801 /* 10802 * If the bitmap is not yet written, then the allocated 10803 * inode cannot be written to disk. 10804 */ 10805 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10806 if (inodedep->id_savedino2 != NULL) 10807 panic("initiate_write_inodeblock_ufs2: I/O underway"); 10808 FREE_LOCK(ump); 10809 sip = malloc(sizeof(struct ufs2_dinode), 10810 M_SAVEDINO, M_SOFTDEP_FLAGS); 10811 ACQUIRE_LOCK(ump); 10812 inodedep->id_savedino2 = sip; 10813 *inodedep->id_savedino2 = *dp; 10814 bzero((caddr_t)dp, sizeof(struct ufs2_dinode)); 10815 dp->di_gen = inodedep->id_savedino2->di_gen; 10816 dp->di_freelink = inodedep->id_savedino2->di_freelink; 10817 return; 10818 } 10819 /* 10820 * If no dependencies, then there is nothing to roll back. 10821 */ 10822 inodedep->id_savedsize = dp->di_size; 10823 inodedep->id_savedextsize = dp->di_extsize; 10824 inodedep->id_savednlink = dp->di_nlink; 10825 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10826 TAILQ_EMPTY(&inodedep->id_extupdt) && 10827 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10828 return; 10829 /* 10830 * Revert the link count to that of the first unwritten journal entry. 10831 */ 10832 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10833 if (inoref) 10834 dp->di_nlink = inoref->if_nlink; 10835 10836 /* 10837 * Set the ext data dependencies to busy. 10838 */ 10839 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10840 adp = TAILQ_NEXT(adp, ad_next)) { 10841 #ifdef INVARIANTS 10842 if (deplist != 0 && prevlbn >= adp->ad_offset) 10843 panic("initiate_write_inodeblock_ufs2: lbn order"); 10844 prevlbn = adp->ad_offset; 10845 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno) 10846 panic("initiate_write_inodeblock_ufs2: " 10847 "ext pointer #%jd mismatch %jd != %jd", 10848 (intmax_t)adp->ad_offset, 10849 (intmax_t)dp->di_extb[adp->ad_offset], 10850 (intmax_t)adp->ad_newblkno); 10851 deplist |= 1 << adp->ad_offset; 10852 if ((adp->ad_state & ATTACHED) == 0) 10853 panic("initiate_write_inodeblock_ufs2: Unknown " 10854 "state 0x%x", adp->ad_state); 10855 #endif /* INVARIANTS */ 10856 adp->ad_state &= ~ATTACHED; 10857 adp->ad_state |= UNDONE; 10858 } 10859 /* 10860 * The on-disk inode cannot claim to be any larger than the last 10861 * fragment that has been written. Otherwise, the on-disk inode 10862 * might have fragments that were not the last block in the ext 10863 * data which would corrupt the filesystem. 10864 */ 10865 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10866 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10867 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno; 10868 /* keep going until hitting a rollback to a frag */ 10869 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10870 continue; 10871 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10872 for (i = adp->ad_offset + 1; i < UFS_NXADDR; i++) { 10873 #ifdef INVARIANTS 10874 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0) 10875 panic("initiate_write_inodeblock_ufs2: " 10876 "lost dep1"); 10877 #endif /* INVARIANTS */ 10878 dp->di_extb[i] = 0; 10879 } 10880 lastadp = NULL; 10881 break; 10882 } 10883 /* 10884 * If we have zero'ed out the last allocated block of the ext 10885 * data, roll back the size to the last currently allocated block. 10886 * We know that this last allocated block is a full-sized as 10887 * we already checked for fragments in the loop above. 10888 */ 10889 if (lastadp != NULL && 10890 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10891 for (i = lastadp->ad_offset; i >= 0; i--) 10892 if (dp->di_extb[i] != 0) 10893 break; 10894 dp->di_extsize = (i + 1) * fs->fs_bsize; 10895 } 10896 /* 10897 * Set the file data dependencies to busy. 10898 */ 10899 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10900 adp = TAILQ_NEXT(adp, ad_next)) { 10901 #ifdef INVARIANTS 10902 if (deplist != 0 && prevlbn >= adp->ad_offset) 10903 panic("softdep_write_inodeblock: lbn order"); 10904 if ((adp->ad_state & ATTACHED) == 0) 10905 panic("inodedep %p and adp %p not attached", inodedep, adp); 10906 prevlbn = adp->ad_offset; 10907 if (!ffs_fsfail_cleanup(ump, 0) && 10908 adp->ad_offset < UFS_NDADDR && 10909 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10910 panic("initiate_write_inodeblock_ufs2: " 10911 "direct pointer #%jd mismatch %jd != %jd", 10912 (intmax_t)adp->ad_offset, 10913 (intmax_t)dp->di_db[adp->ad_offset], 10914 (intmax_t)adp->ad_newblkno); 10915 if (!ffs_fsfail_cleanup(ump, 0) && 10916 adp->ad_offset >= UFS_NDADDR && 10917 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno) 10918 panic("initiate_write_inodeblock_ufs2: " 10919 "indirect pointer #%jd mismatch %jd != %jd", 10920 (intmax_t)adp->ad_offset - UFS_NDADDR, 10921 (intmax_t)dp->di_ib[adp->ad_offset - UFS_NDADDR], 10922 (intmax_t)adp->ad_newblkno); 10923 deplist |= 1 << adp->ad_offset; 10924 if ((adp->ad_state & ATTACHED) == 0) 10925 panic("initiate_write_inodeblock_ufs2: Unknown " 10926 "state 0x%x", adp->ad_state); 10927 #endif /* INVARIANTS */ 10928 adp->ad_state &= ~ATTACHED; 10929 adp->ad_state |= UNDONE; 10930 } 10931 /* 10932 * The on-disk inode cannot claim to be any larger than the last 10933 * fragment that has been written. Otherwise, the on-disk inode 10934 * might have fragments that were not the last block in the file 10935 * which would corrupt the filesystem. 10936 */ 10937 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10938 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10939 if (adp->ad_offset >= UFS_NDADDR) 10940 break; 10941 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10942 /* keep going until hitting a rollback to a frag */ 10943 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10944 continue; 10945 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10946 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) { 10947 #ifdef INVARIANTS 10948 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10949 panic("initiate_write_inodeblock_ufs2: " 10950 "lost dep2"); 10951 #endif /* INVARIANTS */ 10952 dp->di_db[i] = 0; 10953 } 10954 for (i = 0; i < UFS_NIADDR; i++) { 10955 #ifdef INVARIANTS 10956 if (dp->di_ib[i] != 0 && 10957 (deplist & ((1 << UFS_NDADDR) << i)) == 0) 10958 panic("initiate_write_inodeblock_ufs2: " 10959 "lost dep3"); 10960 #endif /* INVARIANTS */ 10961 dp->di_ib[i] = 0; 10962 } 10963 ffs_update_dinode_ckhash(fs, dp); 10964 return; 10965 } 10966 /* 10967 * If we have zero'ed out the last allocated block of the file, 10968 * roll back the size to the last currently allocated block. 10969 * We know that this last allocated block is a full-sized as 10970 * we already checked for fragments in the loop above. 10971 */ 10972 if (lastadp != NULL && 10973 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10974 for (i = lastadp->ad_offset; i >= 0; i--) 10975 if (dp->di_db[i] != 0) 10976 break; 10977 dp->di_size = (i + 1) * fs->fs_bsize; 10978 } 10979 /* 10980 * The only dependencies are for indirect blocks. 10981 * 10982 * The file size for indirect block additions is not guaranteed. 10983 * Such a guarantee would be non-trivial to achieve. The conventional 10984 * synchronous write implementation also does not make this guarantee. 10985 * Fsck should catch and fix discrepancies. Arguably, the file size 10986 * can be over-estimated without destroying integrity when the file 10987 * moves into the indirect blocks (i.e., is large). If we want to 10988 * postpone fsck, we are stuck with this argument. 10989 */ 10990 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10991 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0; 10992 ffs_update_dinode_ckhash(fs, dp); 10993 } 10994 10995 /* 10996 * Cancel an indirdep as a result of truncation. Release all of the 10997 * children allocindirs and place their journal work on the appropriate 10998 * list. 10999 */ 11000 static void 11001 cancel_indirdep(indirdep, bp, freeblks) 11002 struct indirdep *indirdep; 11003 struct buf *bp; 11004 struct freeblks *freeblks; 11005 { 11006 struct allocindir *aip; 11007 11008 /* 11009 * None of the indirect pointers will ever be visible, 11010 * so they can simply be tossed. GOINGAWAY ensures 11011 * that allocated pointers will be saved in the buffer 11012 * cache until they are freed. Note that they will 11013 * only be able to be found by their physical address 11014 * since the inode mapping the logical address will 11015 * be gone. The save buffer used for the safe copy 11016 * was allocated in setup_allocindir_phase2 using 11017 * the physical address so it could be used for this 11018 * purpose. Hence we swap the safe copy with the real 11019 * copy, allowing the safe copy to be freed and holding 11020 * on to the real copy for later use in indir_trunc. 11021 */ 11022 if (indirdep->ir_state & GOINGAWAY) 11023 panic("cancel_indirdep: already gone"); 11024 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 11025 indirdep->ir_state |= DEPCOMPLETE; 11026 LIST_REMOVE(indirdep, ir_next); 11027 } 11028 indirdep->ir_state |= GOINGAWAY; 11029 /* 11030 * Pass in bp for blocks still have journal writes 11031 * pending so we can cancel them on their own. 11032 */ 11033 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != NULL) 11034 cancel_allocindir(aip, bp, freeblks, 0); 11035 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) 11036 cancel_allocindir(aip, NULL, freeblks, 0); 11037 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) 11038 cancel_allocindir(aip, NULL, freeblks, 0); 11039 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) 11040 cancel_allocindir(aip, NULL, freeblks, 0); 11041 /* 11042 * If there are pending partial truncations we need to keep the 11043 * old block copy around until they complete. This is because 11044 * the current b_data is not a perfect superset of the available 11045 * blocks. 11046 */ 11047 if (TAILQ_EMPTY(&indirdep->ir_trunc)) 11048 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount); 11049 else 11050 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 11051 WORKLIST_REMOVE(&indirdep->ir_list); 11052 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list); 11053 indirdep->ir_bp = NULL; 11054 indirdep->ir_freeblks = freeblks; 11055 } 11056 11057 /* 11058 * Free an indirdep once it no longer has new pointers to track. 11059 */ 11060 static void 11061 free_indirdep(indirdep) 11062 struct indirdep *indirdep; 11063 { 11064 11065 KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc), 11066 ("free_indirdep: Indir trunc list not empty.")); 11067 KASSERT(LIST_EMPTY(&indirdep->ir_completehd), 11068 ("free_indirdep: Complete head not empty.")); 11069 KASSERT(LIST_EMPTY(&indirdep->ir_writehd), 11070 ("free_indirdep: write head not empty.")); 11071 KASSERT(LIST_EMPTY(&indirdep->ir_donehd), 11072 ("free_indirdep: done head not empty.")); 11073 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd), 11074 ("free_indirdep: deplist head not empty.")); 11075 KASSERT((indirdep->ir_state & DEPCOMPLETE), 11076 ("free_indirdep: %p still on newblk list.", indirdep)); 11077 KASSERT(indirdep->ir_saveddata == NULL, 11078 ("free_indirdep: %p still has saved data.", indirdep)); 11079 KASSERT(indirdep->ir_savebp == NULL, 11080 ("free_indirdep: %p still has savebp buffer.", indirdep)); 11081 if (indirdep->ir_state & ONWORKLIST) 11082 WORKLIST_REMOVE(&indirdep->ir_list); 11083 WORKITEM_FREE(indirdep, D_INDIRDEP); 11084 } 11085 11086 /* 11087 * Called before a write to an indirdep. This routine is responsible for 11088 * rolling back pointers to a safe state which includes only those 11089 * allocindirs which have been completed. 11090 */ 11091 static void 11092 initiate_write_indirdep(indirdep, bp) 11093 struct indirdep *indirdep; 11094 struct buf *bp; 11095 { 11096 struct ufsmount *ump; 11097 11098 indirdep->ir_state |= IOSTARTED; 11099 if (indirdep->ir_state & GOINGAWAY) 11100 panic("disk_io_initiation: indirdep gone"); 11101 /* 11102 * If there are no remaining dependencies, this will be writing 11103 * the real pointers. 11104 */ 11105 if (LIST_EMPTY(&indirdep->ir_deplisthd) && 11106 TAILQ_EMPTY(&indirdep->ir_trunc)) 11107 return; 11108 /* 11109 * Replace up-to-date version with safe version. 11110 */ 11111 if (indirdep->ir_saveddata == NULL) { 11112 ump = VFSTOUFS(indirdep->ir_list.wk_mp); 11113 LOCK_OWNED(ump); 11114 FREE_LOCK(ump); 11115 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 11116 M_SOFTDEP_FLAGS); 11117 ACQUIRE_LOCK(ump); 11118 } 11119 indirdep->ir_state &= ~ATTACHED; 11120 indirdep->ir_state |= UNDONE; 11121 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 11122 bcopy(indirdep->ir_savebp->b_data, bp->b_data, 11123 bp->b_bcount); 11124 } 11125 11126 /* 11127 * Called when an inode has been cleared in a cg bitmap. This finally 11128 * eliminates any canceled jaddrefs 11129 */ 11130 void 11131 softdep_setup_inofree(mp, bp, ino, wkhd) 11132 struct mount *mp; 11133 struct buf *bp; 11134 ino_t ino; 11135 struct workhead *wkhd; 11136 { 11137 struct worklist *wk, *wkn; 11138 struct inodedep *inodedep; 11139 struct ufsmount *ump; 11140 uint8_t *inosused; 11141 struct cg *cgp; 11142 struct fs *fs; 11143 11144 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 11145 ("softdep_setup_inofree called on non-softdep filesystem")); 11146 ump = VFSTOUFS(mp); 11147 ACQUIRE_LOCK(ump); 11148 if (!ffs_fsfail_cleanup(ump, 0)) { 11149 fs = ump->um_fs; 11150 cgp = (struct cg *)bp->b_data; 11151 inosused = cg_inosused(cgp); 11152 if (isset(inosused, ino % fs->fs_ipg)) 11153 panic("softdep_setup_inofree: inode %ju not freed.", 11154 (uintmax_t)ino); 11155 } 11156 if (inodedep_lookup(mp, ino, 0, &inodedep)) 11157 panic("softdep_setup_inofree: ino %ju has existing inodedep %p", 11158 (uintmax_t)ino, inodedep); 11159 if (wkhd) { 11160 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) { 11161 if (wk->wk_type != D_JADDREF) 11162 continue; 11163 WORKLIST_REMOVE(wk); 11164 /* 11165 * We can free immediately even if the jaddref 11166 * isn't attached in a background write as now 11167 * the bitmaps are reconciled. 11168 */ 11169 wk->wk_state |= COMPLETE | ATTACHED; 11170 free_jaddref(WK_JADDREF(wk)); 11171 } 11172 jwork_move(&bp->b_dep, wkhd); 11173 } 11174 FREE_LOCK(ump); 11175 } 11176 11177 /* 11178 * Called via ffs_blkfree() after a set of frags has been cleared from a cg 11179 * map. Any dependencies waiting for the write to clear are added to the 11180 * buf's list and any jnewblks that are being canceled are discarded 11181 * immediately. 11182 */ 11183 void 11184 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 11185 struct mount *mp; 11186 struct buf *bp; 11187 ufs2_daddr_t blkno; 11188 int frags; 11189 struct workhead *wkhd; 11190 { 11191 struct bmsafemap *bmsafemap; 11192 struct jnewblk *jnewblk; 11193 struct ufsmount *ump; 11194 struct worklist *wk; 11195 struct fs *fs; 11196 #ifdef INVARIANTS 11197 uint8_t *blksfree; 11198 struct cg *cgp; 11199 ufs2_daddr_t jstart; 11200 ufs2_daddr_t jend; 11201 ufs2_daddr_t end; 11202 long bno; 11203 int i; 11204 #endif 11205 11206 CTR3(KTR_SUJ, 11207 "softdep_setup_blkfree: blkno %jd frags %d wk head %p", 11208 blkno, frags, wkhd); 11209 11210 ump = VFSTOUFS(mp); 11211 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 11212 ("softdep_setup_blkfree called on non-softdep filesystem")); 11213 ACQUIRE_LOCK(ump); 11214 /* Lookup the bmsafemap so we track when it is dirty. */ 11215 fs = ump->um_fs; 11216 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 11217 /* 11218 * Detach any jnewblks which have been canceled. They must linger 11219 * until the bitmap is cleared again by ffs_blkfree() to prevent 11220 * an unjournaled allocation from hitting the disk. 11221 */ 11222 if (wkhd) { 11223 while ((wk = LIST_FIRST(wkhd)) != NULL) { 11224 CTR2(KTR_SUJ, 11225 "softdep_setup_blkfree: blkno %jd wk type %d", 11226 blkno, wk->wk_type); 11227 WORKLIST_REMOVE(wk); 11228 if (wk->wk_type != D_JNEWBLK) { 11229 WORKLIST_INSERT(&bmsafemap->sm_freehd, wk); 11230 continue; 11231 } 11232 jnewblk = WK_JNEWBLK(wk); 11233 KASSERT(jnewblk->jn_state & GOINGAWAY, 11234 ("softdep_setup_blkfree: jnewblk not canceled.")); 11235 #ifdef INVARIANTS 11236 /* 11237 * Assert that this block is free in the bitmap 11238 * before we discard the jnewblk. 11239 */ 11240 cgp = (struct cg *)bp->b_data; 11241 blksfree = cg_blksfree(cgp); 11242 bno = dtogd(fs, jnewblk->jn_blkno); 11243 for (i = jnewblk->jn_oldfrags; 11244 i < jnewblk->jn_frags; i++) { 11245 if (isset(blksfree, bno + i)) 11246 continue; 11247 panic("softdep_setup_blkfree: not free"); 11248 } 11249 #endif 11250 /* 11251 * Even if it's not attached we can free immediately 11252 * as the new bitmap is correct. 11253 */ 11254 wk->wk_state |= COMPLETE | ATTACHED; 11255 free_jnewblk(jnewblk); 11256 } 11257 } 11258 11259 #ifdef INVARIANTS 11260 /* 11261 * Assert that we are not freeing a block which has an outstanding 11262 * allocation dependency. 11263 */ 11264 fs = VFSTOUFS(mp)->um_fs; 11265 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 11266 end = blkno + frags; 11267 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 11268 /* 11269 * Don't match against blocks that will be freed when the 11270 * background write is done. 11271 */ 11272 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) == 11273 (COMPLETE | DEPCOMPLETE)) 11274 continue; 11275 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags; 11276 jend = jnewblk->jn_blkno + jnewblk->jn_frags; 11277 if ((blkno >= jstart && blkno < jend) || 11278 (end > jstart && end <= jend)) { 11279 printf("state 0x%X %jd - %d %d dep %p\n", 11280 jnewblk->jn_state, jnewblk->jn_blkno, 11281 jnewblk->jn_oldfrags, jnewblk->jn_frags, 11282 jnewblk->jn_dep); 11283 panic("softdep_setup_blkfree: " 11284 "%jd-%jd(%d) overlaps with %jd-%jd", 11285 blkno, end, frags, jstart, jend); 11286 } 11287 } 11288 #endif 11289 FREE_LOCK(ump); 11290 } 11291 11292 /* 11293 * Revert a block allocation when the journal record that describes it 11294 * is not yet written. 11295 */ 11296 static int 11297 jnewblk_rollback(jnewblk, fs, cgp, blksfree) 11298 struct jnewblk *jnewblk; 11299 struct fs *fs; 11300 struct cg *cgp; 11301 uint8_t *blksfree; 11302 { 11303 ufs1_daddr_t fragno; 11304 long cgbno, bbase; 11305 int frags, blk; 11306 int i; 11307 11308 frags = 0; 11309 cgbno = dtogd(fs, jnewblk->jn_blkno); 11310 /* 11311 * We have to test which frags need to be rolled back. We may 11312 * be operating on a stale copy when doing background writes. 11313 */ 11314 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) 11315 if (isclr(blksfree, cgbno + i)) 11316 frags++; 11317 if (frags == 0) 11318 return (0); 11319 /* 11320 * This is mostly ffs_blkfree() sans some validation and 11321 * superblock updates. 11322 */ 11323 if (frags == fs->fs_frag) { 11324 fragno = fragstoblks(fs, cgbno); 11325 ffs_setblock(fs, blksfree, fragno); 11326 ffs_clusteracct(fs, cgp, fragno, 1); 11327 cgp->cg_cs.cs_nbfree++; 11328 } else { 11329 cgbno += jnewblk->jn_oldfrags; 11330 bbase = cgbno - fragnum(fs, cgbno); 11331 /* Decrement the old frags. */ 11332 blk = blkmap(fs, blksfree, bbase); 11333 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 11334 /* Deallocate the fragment */ 11335 for (i = 0; i < frags; i++) 11336 setbit(blksfree, cgbno + i); 11337 cgp->cg_cs.cs_nffree += frags; 11338 /* Add back in counts associated with the new frags */ 11339 blk = blkmap(fs, blksfree, bbase); 11340 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 11341 /* If a complete block has been reassembled, account for it. */ 11342 fragno = fragstoblks(fs, bbase); 11343 if (ffs_isblock(fs, blksfree, fragno)) { 11344 cgp->cg_cs.cs_nffree -= fs->fs_frag; 11345 ffs_clusteracct(fs, cgp, fragno, 1); 11346 cgp->cg_cs.cs_nbfree++; 11347 } 11348 } 11349 stat_jnewblk++; 11350 jnewblk->jn_state &= ~ATTACHED; 11351 jnewblk->jn_state |= UNDONE; 11352 11353 return (frags); 11354 } 11355 11356 static void 11357 initiate_write_bmsafemap(bmsafemap, bp) 11358 struct bmsafemap *bmsafemap; 11359 struct buf *bp; /* The cg block. */ 11360 { 11361 struct jaddref *jaddref; 11362 struct jnewblk *jnewblk; 11363 uint8_t *inosused; 11364 uint8_t *blksfree; 11365 struct cg *cgp; 11366 struct fs *fs; 11367 ino_t ino; 11368 11369 /* 11370 * If this is a background write, we did this at the time that 11371 * the copy was made, so do not need to do it again. 11372 */ 11373 if (bmsafemap->sm_state & IOSTARTED) 11374 return; 11375 bmsafemap->sm_state |= IOSTARTED; 11376 /* 11377 * Clear any inode allocations which are pending journal writes. 11378 */ 11379 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) { 11380 cgp = (struct cg *)bp->b_data; 11381 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11382 inosused = cg_inosused(cgp); 11383 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) { 11384 ino = jaddref->ja_ino % fs->fs_ipg; 11385 if (isset(inosused, ino)) { 11386 if ((jaddref->ja_mode & IFMT) == IFDIR) 11387 cgp->cg_cs.cs_ndir--; 11388 cgp->cg_cs.cs_nifree++; 11389 clrbit(inosused, ino); 11390 jaddref->ja_state &= ~ATTACHED; 11391 jaddref->ja_state |= UNDONE; 11392 stat_jaddref++; 11393 } else 11394 panic("initiate_write_bmsafemap: inode %ju " 11395 "marked free", (uintmax_t)jaddref->ja_ino); 11396 } 11397 } 11398 /* 11399 * Clear any block allocations which are pending journal writes. 11400 */ 11401 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 11402 cgp = (struct cg *)bp->b_data; 11403 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11404 blksfree = cg_blksfree(cgp); 11405 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 11406 if (jnewblk_rollback(jnewblk, fs, cgp, blksfree)) 11407 continue; 11408 panic("initiate_write_bmsafemap: block %jd " 11409 "marked free", jnewblk->jn_blkno); 11410 } 11411 } 11412 /* 11413 * Move allocation lists to the written lists so they can be 11414 * cleared once the block write is complete. 11415 */ 11416 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr, 11417 inodedep, id_deps); 11418 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 11419 newblk, nb_deps); 11420 LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist, 11421 wk_list); 11422 } 11423 11424 void 11425 softdep_handle_error(struct buf *bp) 11426 { 11427 struct ufsmount *ump; 11428 11429 ump = softdep_bp_to_mp(bp); 11430 if (ump == NULL) 11431 return; 11432 11433 if (ffs_fsfail_cleanup(ump, bp->b_error)) { 11434 /* 11435 * No future writes will succeed, so the on-disk image is safe. 11436 * Pretend that this write succeeded so that the softdep state 11437 * will be cleaned up naturally. 11438 */ 11439 bp->b_ioflags &= ~BIO_ERROR; 11440 bp->b_error = 0; 11441 } 11442 } 11443 11444 /* 11445 * This routine is called during the completion interrupt 11446 * service routine for a disk write (from the procedure called 11447 * by the device driver to inform the filesystem caches of 11448 * a request completion). It should be called early in this 11449 * procedure, before the block is made available to other 11450 * processes or other routines are called. 11451 * 11452 */ 11453 static void 11454 softdep_disk_write_complete(bp) 11455 struct buf *bp; /* describes the completed disk write */ 11456 { 11457 struct worklist *wk; 11458 struct worklist *owk; 11459 struct ufsmount *ump; 11460 struct workhead reattach; 11461 struct freeblks *freeblks; 11462 struct buf *sbp; 11463 11464 ump = softdep_bp_to_mp(bp); 11465 KASSERT(LIST_EMPTY(&bp->b_dep) || ump != NULL, 11466 ("softdep_disk_write_complete: softdep_bp_to_mp returned NULL " 11467 "with outstanding dependencies for buffer %p", bp)); 11468 if (ump == NULL) 11469 return; 11470 if ((bp->b_ioflags & BIO_ERROR) != 0) 11471 softdep_handle_error(bp); 11472 /* 11473 * If an error occurred while doing the write, then the data 11474 * has not hit the disk and the dependencies cannot be processed. 11475 * But we do have to go through and roll forward any dependencies 11476 * that were rolled back before the disk write. 11477 */ 11478 sbp = NULL; 11479 ACQUIRE_LOCK(ump); 11480 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) { 11481 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 11482 switch (wk->wk_type) { 11483 case D_PAGEDEP: 11484 handle_written_filepage(WK_PAGEDEP(wk), bp, 0); 11485 continue; 11486 11487 case D_INODEDEP: 11488 handle_written_inodeblock(WK_INODEDEP(wk), 11489 bp, 0); 11490 continue; 11491 11492 case D_BMSAFEMAP: 11493 handle_written_bmsafemap(WK_BMSAFEMAP(wk), 11494 bp, 0); 11495 continue; 11496 11497 case D_INDIRDEP: 11498 handle_written_indirdep(WK_INDIRDEP(wk), 11499 bp, &sbp, 0); 11500 continue; 11501 default: 11502 /* nothing to roll forward */ 11503 continue; 11504 } 11505 } 11506 FREE_LOCK(ump); 11507 if (sbp) 11508 brelse(sbp); 11509 return; 11510 } 11511 LIST_INIT(&reattach); 11512 11513 /* 11514 * Ump SU lock must not be released anywhere in this code segment. 11515 */ 11516 owk = NULL; 11517 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) { 11518 WORKLIST_REMOVE(wk); 11519 atomic_add_long(&dep_write[wk->wk_type], 1); 11520 if (wk == owk) 11521 panic("duplicate worklist: %p\n", wk); 11522 owk = wk; 11523 switch (wk->wk_type) { 11524 case D_PAGEDEP: 11525 if (handle_written_filepage(WK_PAGEDEP(wk), bp, 11526 WRITESUCCEEDED)) 11527 WORKLIST_INSERT(&reattach, wk); 11528 continue; 11529 11530 case D_INODEDEP: 11531 if (handle_written_inodeblock(WK_INODEDEP(wk), bp, 11532 WRITESUCCEEDED)) 11533 WORKLIST_INSERT(&reattach, wk); 11534 continue; 11535 11536 case D_BMSAFEMAP: 11537 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp, 11538 WRITESUCCEEDED)) 11539 WORKLIST_INSERT(&reattach, wk); 11540 continue; 11541 11542 case D_MKDIR: 11543 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 11544 continue; 11545 11546 case D_ALLOCDIRECT: 11547 wk->wk_state |= COMPLETE; 11548 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL); 11549 continue; 11550 11551 case D_ALLOCINDIR: 11552 wk->wk_state |= COMPLETE; 11553 handle_allocindir_partdone(WK_ALLOCINDIR(wk)); 11554 continue; 11555 11556 case D_INDIRDEP: 11557 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp, 11558 WRITESUCCEEDED)) 11559 WORKLIST_INSERT(&reattach, wk); 11560 continue; 11561 11562 case D_FREEBLKS: 11563 wk->wk_state |= COMPLETE; 11564 freeblks = WK_FREEBLKS(wk); 11565 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE && 11566 LIST_EMPTY(&freeblks->fb_jblkdephd)) 11567 add_to_worklist(wk, WK_NODELAY); 11568 continue; 11569 11570 case D_FREEWORK: 11571 handle_written_freework(WK_FREEWORK(wk)); 11572 break; 11573 11574 case D_JSEGDEP: 11575 free_jsegdep(WK_JSEGDEP(wk)); 11576 continue; 11577 11578 case D_JSEG: 11579 handle_written_jseg(WK_JSEG(wk), bp); 11580 continue; 11581 11582 case D_SBDEP: 11583 if (handle_written_sbdep(WK_SBDEP(wk), bp)) 11584 WORKLIST_INSERT(&reattach, wk); 11585 continue; 11586 11587 case D_FREEDEP: 11588 free_freedep(WK_FREEDEP(wk)); 11589 continue; 11590 11591 default: 11592 panic("handle_disk_write_complete: Unknown type %s", 11593 TYPENAME(wk->wk_type)); 11594 /* NOTREACHED */ 11595 } 11596 } 11597 /* 11598 * Reattach any requests that must be redone. 11599 */ 11600 while ((wk = LIST_FIRST(&reattach)) != NULL) { 11601 WORKLIST_REMOVE(wk); 11602 WORKLIST_INSERT(&bp->b_dep, wk); 11603 } 11604 FREE_LOCK(ump); 11605 if (sbp) 11606 brelse(sbp); 11607 } 11608 11609 /* 11610 * Called from within softdep_disk_write_complete above. 11611 */ 11612 static void 11613 handle_allocdirect_partdone(adp, wkhd) 11614 struct allocdirect *adp; /* the completed allocdirect */ 11615 struct workhead *wkhd; /* Work to do when inode is writtne. */ 11616 { 11617 struct allocdirectlst *listhead; 11618 struct allocdirect *listadp; 11619 struct inodedep *inodedep; 11620 long bsize; 11621 11622 LOCK_OWNED(VFSTOUFS(adp->ad_block.nb_list.wk_mp)); 11623 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11624 return; 11625 /* 11626 * The on-disk inode cannot claim to be any larger than the last 11627 * fragment that has been written. Otherwise, the on-disk inode 11628 * might have fragments that were not the last block in the file 11629 * which would corrupt the filesystem. Thus, we cannot free any 11630 * allocdirects after one whose ad_oldblkno claims a fragment as 11631 * these blocks must be rolled back to zero before writing the inode. 11632 * We check the currently active set of allocdirects in id_inoupdt 11633 * or id_extupdt as appropriate. 11634 */ 11635 inodedep = adp->ad_inodedep; 11636 bsize = inodedep->id_fs->fs_bsize; 11637 if (adp->ad_state & EXTDATA) 11638 listhead = &inodedep->id_extupdt; 11639 else 11640 listhead = &inodedep->id_inoupdt; 11641 TAILQ_FOREACH(listadp, listhead, ad_next) { 11642 /* found our block */ 11643 if (listadp == adp) 11644 break; 11645 /* continue if ad_oldlbn is not a fragment */ 11646 if (listadp->ad_oldsize == 0 || 11647 listadp->ad_oldsize == bsize) 11648 continue; 11649 /* hit a fragment */ 11650 return; 11651 } 11652 /* 11653 * If we have reached the end of the current list without 11654 * finding the just finished dependency, then it must be 11655 * on the future dependency list. Future dependencies cannot 11656 * be freed until they are moved to the current list. 11657 */ 11658 if (listadp == NULL) { 11659 #ifdef INVARIANTS 11660 if (adp->ad_state & EXTDATA) 11661 listhead = &inodedep->id_newextupdt; 11662 else 11663 listhead = &inodedep->id_newinoupdt; 11664 TAILQ_FOREACH(listadp, listhead, ad_next) 11665 /* found our block */ 11666 if (listadp == adp) 11667 break; 11668 if (listadp == NULL) 11669 panic("handle_allocdirect_partdone: lost dep"); 11670 #endif /* INVARIANTS */ 11671 return; 11672 } 11673 /* 11674 * If we have found the just finished dependency, then queue 11675 * it along with anything that follows it that is complete. 11676 * Since the pointer has not yet been written in the inode 11677 * as the dependency prevents it, place the allocdirect on the 11678 * bufwait list where it will be freed once the pointer is 11679 * valid. 11680 */ 11681 if (wkhd == NULL) 11682 wkhd = &inodedep->id_bufwait; 11683 for (; adp; adp = listadp) { 11684 listadp = TAILQ_NEXT(adp, ad_next); 11685 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11686 return; 11687 TAILQ_REMOVE(listhead, adp, ad_next); 11688 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list); 11689 } 11690 } 11691 11692 /* 11693 * Called from within softdep_disk_write_complete above. This routine 11694 * completes successfully written allocindirs. 11695 */ 11696 static void 11697 handle_allocindir_partdone(aip) 11698 struct allocindir *aip; /* the completed allocindir */ 11699 { 11700 struct indirdep *indirdep; 11701 11702 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE) 11703 return; 11704 indirdep = aip->ai_indirdep; 11705 LIST_REMOVE(aip, ai_next); 11706 /* 11707 * Don't set a pointer while the buffer is undergoing IO or while 11708 * we have active truncations. 11709 */ 11710 if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) { 11711 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next); 11712 return; 11713 } 11714 if (indirdep->ir_state & UFS1FMT) 11715 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11716 aip->ai_newblkno; 11717 else 11718 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11719 aip->ai_newblkno; 11720 /* 11721 * Await the pointer write before freeing the allocindir. 11722 */ 11723 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next); 11724 } 11725 11726 /* 11727 * Release segments held on a jwork list. 11728 */ 11729 static void 11730 handle_jwork(wkhd) 11731 struct workhead *wkhd; 11732 { 11733 struct worklist *wk; 11734 11735 while ((wk = LIST_FIRST(wkhd)) != NULL) { 11736 WORKLIST_REMOVE(wk); 11737 switch (wk->wk_type) { 11738 case D_JSEGDEP: 11739 free_jsegdep(WK_JSEGDEP(wk)); 11740 continue; 11741 case D_FREEDEP: 11742 free_freedep(WK_FREEDEP(wk)); 11743 continue; 11744 case D_FREEFRAG: 11745 rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep)); 11746 WORKITEM_FREE(wk, D_FREEFRAG); 11747 continue; 11748 case D_FREEWORK: 11749 handle_written_freework(WK_FREEWORK(wk)); 11750 continue; 11751 default: 11752 panic("handle_jwork: Unknown type %s\n", 11753 TYPENAME(wk->wk_type)); 11754 } 11755 } 11756 } 11757 11758 /* 11759 * Handle the bufwait list on an inode when it is safe to release items 11760 * held there. This normally happens after an inode block is written but 11761 * may be delayed and handled later if there are pending journal items that 11762 * are not yet safe to be released. 11763 */ 11764 static struct freefile * 11765 handle_bufwait(inodedep, refhd) 11766 struct inodedep *inodedep; 11767 struct workhead *refhd; 11768 { 11769 struct jaddref *jaddref; 11770 struct freefile *freefile; 11771 struct worklist *wk; 11772 11773 freefile = NULL; 11774 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) { 11775 WORKLIST_REMOVE(wk); 11776 switch (wk->wk_type) { 11777 case D_FREEFILE: 11778 /* 11779 * We defer adding freefile to the worklist 11780 * until all other additions have been made to 11781 * ensure that it will be done after all the 11782 * old blocks have been freed. 11783 */ 11784 if (freefile != NULL) 11785 panic("handle_bufwait: freefile"); 11786 freefile = WK_FREEFILE(wk); 11787 continue; 11788 11789 case D_MKDIR: 11790 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT); 11791 continue; 11792 11793 case D_DIRADD: 11794 diradd_inode_written(WK_DIRADD(wk), inodedep); 11795 continue; 11796 11797 case D_FREEFRAG: 11798 wk->wk_state |= COMPLETE; 11799 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE) 11800 add_to_worklist(wk, 0); 11801 continue; 11802 11803 case D_DIRREM: 11804 wk->wk_state |= COMPLETE; 11805 add_to_worklist(wk, 0); 11806 continue; 11807 11808 case D_ALLOCDIRECT: 11809 case D_ALLOCINDIR: 11810 free_newblk(WK_NEWBLK(wk)); 11811 continue; 11812 11813 case D_JNEWBLK: 11814 wk->wk_state |= COMPLETE; 11815 free_jnewblk(WK_JNEWBLK(wk)); 11816 continue; 11817 11818 /* 11819 * Save freed journal segments and add references on 11820 * the supplied list which will delay their release 11821 * until the cg bitmap is cleared on disk. 11822 */ 11823 case D_JSEGDEP: 11824 if (refhd == NULL) 11825 free_jsegdep(WK_JSEGDEP(wk)); 11826 else 11827 WORKLIST_INSERT(refhd, wk); 11828 continue; 11829 11830 case D_JADDREF: 11831 jaddref = WK_JADDREF(wk); 11832 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 11833 if_deps); 11834 /* 11835 * Transfer any jaddrefs to the list to be freed with 11836 * the bitmap if we're handling a removed file. 11837 */ 11838 if (refhd == NULL) { 11839 wk->wk_state |= COMPLETE; 11840 free_jaddref(jaddref); 11841 } else 11842 WORKLIST_INSERT(refhd, wk); 11843 continue; 11844 11845 default: 11846 panic("handle_bufwait: Unknown type %p(%s)", 11847 wk, TYPENAME(wk->wk_type)); 11848 /* NOTREACHED */ 11849 } 11850 } 11851 return (freefile); 11852 } 11853 /* 11854 * Called from within softdep_disk_write_complete above to restore 11855 * in-memory inode block contents to their most up-to-date state. Note 11856 * that this routine is always called from interrupt level with further 11857 * interrupts from this device blocked. 11858 * 11859 * If the write did not succeed, we will do all the roll-forward 11860 * operations, but we will not take the actions that will allow its 11861 * dependencies to be processed. 11862 */ 11863 static int 11864 handle_written_inodeblock(inodedep, bp, flags) 11865 struct inodedep *inodedep; 11866 struct buf *bp; /* buffer containing the inode block */ 11867 int flags; 11868 { 11869 struct freefile *freefile; 11870 struct allocdirect *adp, *nextadp; 11871 struct ufs1_dinode *dp1 = NULL; 11872 struct ufs2_dinode *dp2 = NULL; 11873 struct workhead wkhd; 11874 int hadchanges, fstype; 11875 ino_t freelink; 11876 11877 LIST_INIT(&wkhd); 11878 hadchanges = 0; 11879 freefile = NULL; 11880 if ((inodedep->id_state & IOSTARTED) == 0) 11881 panic("handle_written_inodeblock: not started"); 11882 inodedep->id_state &= ~IOSTARTED; 11883 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) { 11884 fstype = UFS1; 11885 dp1 = (struct ufs1_dinode *)bp->b_data + 11886 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11887 freelink = dp1->di_freelink; 11888 } else { 11889 fstype = UFS2; 11890 dp2 = (struct ufs2_dinode *)bp->b_data + 11891 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11892 freelink = dp2->di_freelink; 11893 } 11894 /* 11895 * Leave this inodeblock dirty until it's in the list. 11896 */ 11897 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED && 11898 (flags & WRITESUCCEEDED)) { 11899 struct inodedep *inon; 11900 11901 inon = TAILQ_NEXT(inodedep, id_unlinked); 11902 if ((inon == NULL && freelink == 0) || 11903 (inon && inon->id_ino == freelink)) { 11904 if (inon) 11905 inon->id_state |= UNLINKPREV; 11906 inodedep->id_state |= UNLINKNEXT; 11907 } 11908 hadchanges = 1; 11909 } 11910 /* 11911 * If we had to rollback the inode allocation because of 11912 * bitmaps being incomplete, then simply restore it. 11913 * Keep the block dirty so that it will not be reclaimed until 11914 * all associated dependencies have been cleared and the 11915 * corresponding updates written to disk. 11916 */ 11917 if (inodedep->id_savedino1 != NULL) { 11918 hadchanges = 1; 11919 if (fstype == UFS1) 11920 *dp1 = *inodedep->id_savedino1; 11921 else 11922 *dp2 = *inodedep->id_savedino2; 11923 free(inodedep->id_savedino1, M_SAVEDINO); 11924 inodedep->id_savedino1 = NULL; 11925 if ((bp->b_flags & B_DELWRI) == 0) 11926 stat_inode_bitmap++; 11927 bdirty(bp); 11928 /* 11929 * If the inode is clear here and GOINGAWAY it will never 11930 * be written. Process the bufwait and clear any pending 11931 * work which may include the freefile. 11932 */ 11933 if (inodedep->id_state & GOINGAWAY) 11934 goto bufwait; 11935 return (1); 11936 } 11937 if (flags & WRITESUCCEEDED) 11938 inodedep->id_state |= COMPLETE; 11939 /* 11940 * Roll forward anything that had to be rolled back before 11941 * the inode could be updated. 11942 */ 11943 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) { 11944 nextadp = TAILQ_NEXT(adp, ad_next); 11945 if (adp->ad_state & ATTACHED) 11946 panic("handle_written_inodeblock: new entry"); 11947 if (fstype == UFS1) { 11948 if (adp->ad_offset < UFS_NDADDR) { 11949 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11950 panic("%s %s #%jd mismatch %d != %jd", 11951 "handle_written_inodeblock:", 11952 "direct pointer", 11953 (intmax_t)adp->ad_offset, 11954 dp1->di_db[adp->ad_offset], 11955 (intmax_t)adp->ad_oldblkno); 11956 dp1->di_db[adp->ad_offset] = adp->ad_newblkno; 11957 } else { 11958 if (dp1->di_ib[adp->ad_offset - UFS_NDADDR] != 11959 0) 11960 panic("%s: %s #%jd allocated as %d", 11961 "handle_written_inodeblock", 11962 "indirect pointer", 11963 (intmax_t)adp->ad_offset - 11964 UFS_NDADDR, 11965 dp1->di_ib[adp->ad_offset - 11966 UFS_NDADDR]); 11967 dp1->di_ib[adp->ad_offset - UFS_NDADDR] = 11968 adp->ad_newblkno; 11969 } 11970 } else { 11971 if (adp->ad_offset < UFS_NDADDR) { 11972 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11973 panic("%s: %s #%jd %s %jd != %jd", 11974 "handle_written_inodeblock", 11975 "direct pointer", 11976 (intmax_t)adp->ad_offset, "mismatch", 11977 (intmax_t)dp2->di_db[adp->ad_offset], 11978 (intmax_t)adp->ad_oldblkno); 11979 dp2->di_db[adp->ad_offset] = adp->ad_newblkno; 11980 } else { 11981 if (dp2->di_ib[adp->ad_offset - UFS_NDADDR] != 11982 0) 11983 panic("%s: %s #%jd allocated as %jd", 11984 "handle_written_inodeblock", 11985 "indirect pointer", 11986 (intmax_t)adp->ad_offset - 11987 UFS_NDADDR, 11988 (intmax_t) 11989 dp2->di_ib[adp->ad_offset - 11990 UFS_NDADDR]); 11991 dp2->di_ib[adp->ad_offset - UFS_NDADDR] = 11992 adp->ad_newblkno; 11993 } 11994 } 11995 adp->ad_state &= ~UNDONE; 11996 adp->ad_state |= ATTACHED; 11997 hadchanges = 1; 11998 } 11999 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) { 12000 nextadp = TAILQ_NEXT(adp, ad_next); 12001 if (adp->ad_state & ATTACHED) 12002 panic("handle_written_inodeblock: new entry"); 12003 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno) 12004 panic("%s: direct pointers #%jd %s %jd != %jd", 12005 "handle_written_inodeblock", 12006 (intmax_t)adp->ad_offset, "mismatch", 12007 (intmax_t)dp2->di_extb[adp->ad_offset], 12008 (intmax_t)adp->ad_oldblkno); 12009 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno; 12010 adp->ad_state &= ~UNDONE; 12011 adp->ad_state |= ATTACHED; 12012 hadchanges = 1; 12013 } 12014 if (hadchanges && (bp->b_flags & B_DELWRI) == 0) 12015 stat_direct_blk_ptrs++; 12016 /* 12017 * Reset the file size to its most up-to-date value. 12018 */ 12019 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1) 12020 panic("handle_written_inodeblock: bad size"); 12021 if (inodedep->id_savednlink > UFS_LINK_MAX) 12022 panic("handle_written_inodeblock: Invalid link count " 12023 "%jd for inodedep %p", (uintmax_t)inodedep->id_savednlink, 12024 inodedep); 12025 if (fstype == UFS1) { 12026 if (dp1->di_nlink != inodedep->id_savednlink) { 12027 dp1->di_nlink = inodedep->id_savednlink; 12028 hadchanges = 1; 12029 } 12030 if (dp1->di_size != inodedep->id_savedsize) { 12031 dp1->di_size = inodedep->id_savedsize; 12032 hadchanges = 1; 12033 } 12034 } else { 12035 if (dp2->di_nlink != inodedep->id_savednlink) { 12036 dp2->di_nlink = inodedep->id_savednlink; 12037 hadchanges = 1; 12038 } 12039 if (dp2->di_size != inodedep->id_savedsize) { 12040 dp2->di_size = inodedep->id_savedsize; 12041 hadchanges = 1; 12042 } 12043 if (dp2->di_extsize != inodedep->id_savedextsize) { 12044 dp2->di_extsize = inodedep->id_savedextsize; 12045 hadchanges = 1; 12046 } 12047 } 12048 inodedep->id_savedsize = -1; 12049 inodedep->id_savedextsize = -1; 12050 inodedep->id_savednlink = -1; 12051 /* 12052 * If there were any rollbacks in the inode block, then it must be 12053 * marked dirty so that its will eventually get written back in 12054 * its correct form. 12055 */ 12056 if (hadchanges) { 12057 if (fstype == UFS2) 12058 ffs_update_dinode_ckhash(inodedep->id_fs, dp2); 12059 bdirty(bp); 12060 } 12061 bufwait: 12062 /* 12063 * If the write did not succeed, we have done all the roll-forward 12064 * operations, but we cannot take the actions that will allow its 12065 * dependencies to be processed. 12066 */ 12067 if ((flags & WRITESUCCEEDED) == 0) 12068 return (hadchanges); 12069 /* 12070 * Process any allocdirects that completed during the update. 12071 */ 12072 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 12073 handle_allocdirect_partdone(adp, &wkhd); 12074 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 12075 handle_allocdirect_partdone(adp, &wkhd); 12076 /* 12077 * Process deallocations that were held pending until the 12078 * inode had been written to disk. Freeing of the inode 12079 * is delayed until after all blocks have been freed to 12080 * avoid creation of new <vfsid, inum, lbn> triples 12081 * before the old ones have been deleted. Completely 12082 * unlinked inodes are not processed until the unlinked 12083 * inode list is written or the last reference is removed. 12084 */ 12085 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) { 12086 freefile = handle_bufwait(inodedep, NULL); 12087 if (freefile && !LIST_EMPTY(&wkhd)) { 12088 WORKLIST_INSERT(&wkhd, &freefile->fx_list); 12089 freefile = NULL; 12090 } 12091 } 12092 /* 12093 * Move rolled forward dependency completions to the bufwait list 12094 * now that those that were already written have been processed. 12095 */ 12096 if (!LIST_EMPTY(&wkhd) && hadchanges == 0) 12097 panic("handle_written_inodeblock: bufwait but no changes"); 12098 jwork_move(&inodedep->id_bufwait, &wkhd); 12099 12100 if (freefile != NULL) { 12101 /* 12102 * If the inode is goingaway it was never written. Fake up 12103 * the state here so free_inodedep() can succeed. 12104 */ 12105 if (inodedep->id_state & GOINGAWAY) 12106 inodedep->id_state |= COMPLETE | DEPCOMPLETE; 12107 if (free_inodedep(inodedep) == 0) 12108 panic("handle_written_inodeblock: live inodedep %p", 12109 inodedep); 12110 add_to_worklist(&freefile->fx_list, 0); 12111 return (0); 12112 } 12113 12114 /* 12115 * If no outstanding dependencies, free it. 12116 */ 12117 if (free_inodedep(inodedep) || 12118 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 && 12119 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 && 12120 TAILQ_FIRST(&inodedep->id_extupdt) == 0 && 12121 LIST_FIRST(&inodedep->id_bufwait) == 0)) 12122 return (0); 12123 return (hadchanges); 12124 } 12125 12126 /* 12127 * Perform needed roll-forwards and kick off any dependencies that 12128 * can now be processed. 12129 * 12130 * If the write did not succeed, we will do all the roll-forward 12131 * operations, but we will not take the actions that will allow its 12132 * dependencies to be processed. 12133 */ 12134 static int 12135 handle_written_indirdep(indirdep, bp, bpp, flags) 12136 struct indirdep *indirdep; 12137 struct buf *bp; 12138 struct buf **bpp; 12139 int flags; 12140 { 12141 struct allocindir *aip; 12142 struct buf *sbp; 12143 int chgs; 12144 12145 if (indirdep->ir_state & GOINGAWAY) 12146 panic("handle_written_indirdep: indirdep gone"); 12147 if ((indirdep->ir_state & IOSTARTED) == 0) 12148 panic("handle_written_indirdep: IO not started"); 12149 chgs = 0; 12150 /* 12151 * If there were rollbacks revert them here. 12152 */ 12153 if (indirdep->ir_saveddata) { 12154 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount); 12155 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 12156 free(indirdep->ir_saveddata, M_INDIRDEP); 12157 indirdep->ir_saveddata = NULL; 12158 } 12159 chgs = 1; 12160 } 12161 indirdep->ir_state &= ~(UNDONE | IOSTARTED); 12162 indirdep->ir_state |= ATTACHED; 12163 /* 12164 * If the write did not succeed, we have done all the roll-forward 12165 * operations, but we cannot take the actions that will allow its 12166 * dependencies to be processed. 12167 */ 12168 if ((flags & WRITESUCCEEDED) == 0) { 12169 stat_indir_blk_ptrs++; 12170 bdirty(bp); 12171 return (1); 12172 } 12173 /* 12174 * Move allocindirs with written pointers to the completehd if 12175 * the indirdep's pointer is not yet written. Otherwise 12176 * free them here. 12177 */ 12178 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) { 12179 LIST_REMOVE(aip, ai_next); 12180 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 12181 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip, 12182 ai_next); 12183 newblk_freefrag(&aip->ai_block); 12184 continue; 12185 } 12186 free_newblk(&aip->ai_block); 12187 } 12188 /* 12189 * Move allocindirs that have finished dependency processing from 12190 * the done list to the write list after updating the pointers. 12191 */ 12192 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 12193 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) { 12194 handle_allocindir_partdone(aip); 12195 if (aip == LIST_FIRST(&indirdep->ir_donehd)) 12196 panic("disk_write_complete: not gone"); 12197 chgs = 1; 12198 } 12199 } 12200 /* 12201 * Preserve the indirdep if there were any changes or if it is not 12202 * yet valid on disk. 12203 */ 12204 if (chgs) { 12205 stat_indir_blk_ptrs++; 12206 bdirty(bp); 12207 return (1); 12208 } 12209 /* 12210 * If there were no changes we can discard the savedbp and detach 12211 * ourselves from the buf. We are only carrying completed pointers 12212 * in this case. 12213 */ 12214 sbp = indirdep->ir_savebp; 12215 sbp->b_flags |= B_INVAL | B_NOCACHE; 12216 indirdep->ir_savebp = NULL; 12217 indirdep->ir_bp = NULL; 12218 if (*bpp != NULL) 12219 panic("handle_written_indirdep: bp already exists."); 12220 *bpp = sbp; 12221 /* 12222 * The indirdep may not be freed until its parent points at it. 12223 */ 12224 if (indirdep->ir_state & DEPCOMPLETE) 12225 free_indirdep(indirdep); 12226 12227 return (0); 12228 } 12229 12230 /* 12231 * Process a diradd entry after its dependent inode has been written. 12232 */ 12233 static void 12234 diradd_inode_written(dap, inodedep) 12235 struct diradd *dap; 12236 struct inodedep *inodedep; 12237 { 12238 12239 LOCK_OWNED(VFSTOUFS(dap->da_list.wk_mp)); 12240 dap->da_state |= COMPLETE; 12241 complete_diradd(dap); 12242 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 12243 } 12244 12245 /* 12246 * Returns true if the bmsafemap will have rollbacks when written. Must only 12247 * be called with the per-filesystem lock and the buf lock on the cg held. 12248 */ 12249 static int 12250 bmsafemap_backgroundwrite(bmsafemap, bp) 12251 struct bmsafemap *bmsafemap; 12252 struct buf *bp; 12253 { 12254 int dirty; 12255 12256 LOCK_OWNED(VFSTOUFS(bmsafemap->sm_list.wk_mp)); 12257 dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) | 12258 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd); 12259 /* 12260 * If we're initiating a background write we need to process the 12261 * rollbacks as they exist now, not as they exist when IO starts. 12262 * No other consumers will look at the contents of the shadowed 12263 * buf so this is safe to do here. 12264 */ 12265 if (bp->b_xflags & BX_BKGRDMARKER) 12266 initiate_write_bmsafemap(bmsafemap, bp); 12267 12268 return (dirty); 12269 } 12270 12271 /* 12272 * Re-apply an allocation when a cg write is complete. 12273 */ 12274 static int 12275 jnewblk_rollforward(jnewblk, fs, cgp, blksfree) 12276 struct jnewblk *jnewblk; 12277 struct fs *fs; 12278 struct cg *cgp; 12279 uint8_t *blksfree; 12280 { 12281 ufs1_daddr_t fragno; 12282 ufs2_daddr_t blkno; 12283 long cgbno, bbase; 12284 int frags, blk; 12285 int i; 12286 12287 frags = 0; 12288 cgbno = dtogd(fs, jnewblk->jn_blkno); 12289 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) { 12290 if (isclr(blksfree, cgbno + i)) 12291 panic("jnewblk_rollforward: re-allocated fragment"); 12292 frags++; 12293 } 12294 if (frags == fs->fs_frag) { 12295 blkno = fragstoblks(fs, cgbno); 12296 ffs_clrblock(fs, blksfree, (long)blkno); 12297 ffs_clusteracct(fs, cgp, blkno, -1); 12298 cgp->cg_cs.cs_nbfree--; 12299 } else { 12300 bbase = cgbno - fragnum(fs, cgbno); 12301 cgbno += jnewblk->jn_oldfrags; 12302 /* If a complete block had been reassembled, account for it. */ 12303 fragno = fragstoblks(fs, bbase); 12304 if (ffs_isblock(fs, blksfree, fragno)) { 12305 cgp->cg_cs.cs_nffree += fs->fs_frag; 12306 ffs_clusteracct(fs, cgp, fragno, -1); 12307 cgp->cg_cs.cs_nbfree--; 12308 } 12309 /* Decrement the old frags. */ 12310 blk = blkmap(fs, blksfree, bbase); 12311 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 12312 /* Allocate the fragment */ 12313 for (i = 0; i < frags; i++) 12314 clrbit(blksfree, cgbno + i); 12315 cgp->cg_cs.cs_nffree -= frags; 12316 /* Add back in counts associated with the new frags */ 12317 blk = blkmap(fs, blksfree, bbase); 12318 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 12319 } 12320 return (frags); 12321 } 12322 12323 /* 12324 * Complete a write to a bmsafemap structure. Roll forward any bitmap 12325 * changes if it's not a background write. Set all written dependencies 12326 * to DEPCOMPLETE and free the structure if possible. 12327 * 12328 * If the write did not succeed, we will do all the roll-forward 12329 * operations, but we will not take the actions that will allow its 12330 * dependencies to be processed. 12331 */ 12332 static int 12333 handle_written_bmsafemap(bmsafemap, bp, flags) 12334 struct bmsafemap *bmsafemap; 12335 struct buf *bp; 12336 int flags; 12337 { 12338 struct newblk *newblk; 12339 struct inodedep *inodedep; 12340 struct jaddref *jaddref, *jatmp; 12341 struct jnewblk *jnewblk, *jntmp; 12342 struct ufsmount *ump; 12343 uint8_t *inosused; 12344 uint8_t *blksfree; 12345 struct cg *cgp; 12346 struct fs *fs; 12347 ino_t ino; 12348 int foreground; 12349 int chgs; 12350 12351 if ((bmsafemap->sm_state & IOSTARTED) == 0) 12352 panic("handle_written_bmsafemap: Not started\n"); 12353 ump = VFSTOUFS(bmsafemap->sm_list.wk_mp); 12354 chgs = 0; 12355 bmsafemap->sm_state &= ~IOSTARTED; 12356 foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0; 12357 /* 12358 * If write was successful, release journal work that was waiting 12359 * on the write. Otherwise move the work back. 12360 */ 12361 if (flags & WRITESUCCEEDED) 12362 handle_jwork(&bmsafemap->sm_freewr); 12363 else 12364 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, 12365 worklist, wk_list); 12366 12367 /* 12368 * Restore unwritten inode allocation pending jaddref writes. 12369 */ 12370 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) { 12371 cgp = (struct cg *)bp->b_data; 12372 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 12373 inosused = cg_inosused(cgp); 12374 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd, 12375 ja_bmdeps, jatmp) { 12376 if ((jaddref->ja_state & UNDONE) == 0) 12377 continue; 12378 ino = jaddref->ja_ino % fs->fs_ipg; 12379 if (isset(inosused, ino)) 12380 panic("handle_written_bmsafemap: " 12381 "re-allocated inode"); 12382 /* Do the roll-forward only if it's a real copy. */ 12383 if (foreground) { 12384 if ((jaddref->ja_mode & IFMT) == IFDIR) 12385 cgp->cg_cs.cs_ndir++; 12386 cgp->cg_cs.cs_nifree--; 12387 setbit(inosused, ino); 12388 chgs = 1; 12389 } 12390 jaddref->ja_state &= ~UNDONE; 12391 jaddref->ja_state |= ATTACHED; 12392 free_jaddref(jaddref); 12393 } 12394 } 12395 /* 12396 * Restore any block allocations which are pending journal writes. 12397 */ 12398 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 12399 cgp = (struct cg *)bp->b_data; 12400 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 12401 blksfree = cg_blksfree(cgp); 12402 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps, 12403 jntmp) { 12404 if ((jnewblk->jn_state & UNDONE) == 0) 12405 continue; 12406 /* Do the roll-forward only if it's a real copy. */ 12407 if (foreground && 12408 jnewblk_rollforward(jnewblk, fs, cgp, blksfree)) 12409 chgs = 1; 12410 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK); 12411 jnewblk->jn_state |= ATTACHED; 12412 free_jnewblk(jnewblk); 12413 } 12414 } 12415 /* 12416 * If the write did not succeed, we have done all the roll-forward 12417 * operations, but we cannot take the actions that will allow its 12418 * dependencies to be processed. 12419 */ 12420 if ((flags & WRITESUCCEEDED) == 0) { 12421 LIST_CONCAT(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 12422 newblk, nb_deps); 12423 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, 12424 worklist, wk_list); 12425 if (foreground) 12426 bdirty(bp); 12427 return (1); 12428 } 12429 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) { 12430 newblk->nb_state |= DEPCOMPLETE; 12431 newblk->nb_state &= ~ONDEPLIST; 12432 newblk->nb_bmsafemap = NULL; 12433 LIST_REMOVE(newblk, nb_deps); 12434 if (newblk->nb_list.wk_type == D_ALLOCDIRECT) 12435 handle_allocdirect_partdone( 12436 WK_ALLOCDIRECT(&newblk->nb_list), NULL); 12437 else if (newblk->nb_list.wk_type == D_ALLOCINDIR) 12438 handle_allocindir_partdone( 12439 WK_ALLOCINDIR(&newblk->nb_list)); 12440 else if (newblk->nb_list.wk_type != D_NEWBLK) 12441 panic("handle_written_bmsafemap: Unexpected type: %s", 12442 TYPENAME(newblk->nb_list.wk_type)); 12443 } 12444 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) { 12445 inodedep->id_state |= DEPCOMPLETE; 12446 inodedep->id_state &= ~ONDEPLIST; 12447 LIST_REMOVE(inodedep, id_deps); 12448 inodedep->id_bmsafemap = NULL; 12449 } 12450 LIST_REMOVE(bmsafemap, sm_next); 12451 if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) && 12452 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) && 12453 LIST_EMPTY(&bmsafemap->sm_newblkhd) && 12454 LIST_EMPTY(&bmsafemap->sm_inodedephd) && 12455 LIST_EMPTY(&bmsafemap->sm_freehd)) { 12456 LIST_REMOVE(bmsafemap, sm_hash); 12457 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 12458 return (0); 12459 } 12460 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 12461 if (foreground) 12462 bdirty(bp); 12463 return (1); 12464 } 12465 12466 /* 12467 * Try to free a mkdir dependency. 12468 */ 12469 static void 12470 complete_mkdir(mkdir) 12471 struct mkdir *mkdir; 12472 { 12473 struct diradd *dap; 12474 12475 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE) 12476 return; 12477 LIST_REMOVE(mkdir, md_mkdirs); 12478 dap = mkdir->md_diradd; 12479 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 12480 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) { 12481 dap->da_state |= DEPCOMPLETE; 12482 complete_diradd(dap); 12483 } 12484 WORKITEM_FREE(mkdir, D_MKDIR); 12485 } 12486 12487 /* 12488 * Handle the completion of a mkdir dependency. 12489 */ 12490 static void 12491 handle_written_mkdir(mkdir, type) 12492 struct mkdir *mkdir; 12493 int type; 12494 { 12495 12496 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type) 12497 panic("handle_written_mkdir: bad type"); 12498 mkdir->md_state |= COMPLETE; 12499 complete_mkdir(mkdir); 12500 } 12501 12502 static int 12503 free_pagedep(pagedep) 12504 struct pagedep *pagedep; 12505 { 12506 int i; 12507 12508 if (pagedep->pd_state & NEWBLOCK) 12509 return (0); 12510 if (!LIST_EMPTY(&pagedep->pd_dirremhd)) 12511 return (0); 12512 for (i = 0; i < DAHASHSZ; i++) 12513 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i])) 12514 return (0); 12515 if (!LIST_EMPTY(&pagedep->pd_pendinghd)) 12516 return (0); 12517 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd)) 12518 return (0); 12519 if (pagedep->pd_state & ONWORKLIST) 12520 WORKLIST_REMOVE(&pagedep->pd_list); 12521 LIST_REMOVE(pagedep, pd_hash); 12522 WORKITEM_FREE(pagedep, D_PAGEDEP); 12523 12524 return (1); 12525 } 12526 12527 /* 12528 * Called from within softdep_disk_write_complete above. 12529 * A write operation was just completed. Removed inodes can 12530 * now be freed and associated block pointers may be committed. 12531 * Note that this routine is always called from interrupt level 12532 * with further interrupts from this device blocked. 12533 * 12534 * If the write did not succeed, we will do all the roll-forward 12535 * operations, but we will not take the actions that will allow its 12536 * dependencies to be processed. 12537 */ 12538 static int 12539 handle_written_filepage(pagedep, bp, flags) 12540 struct pagedep *pagedep; 12541 struct buf *bp; /* buffer containing the written page */ 12542 int flags; 12543 { 12544 struct dirrem *dirrem; 12545 struct diradd *dap, *nextdap; 12546 struct direct *ep; 12547 int i, chgs; 12548 12549 if ((pagedep->pd_state & IOSTARTED) == 0) 12550 panic("handle_written_filepage: not started"); 12551 pagedep->pd_state &= ~IOSTARTED; 12552 if ((flags & WRITESUCCEEDED) == 0) 12553 goto rollforward; 12554 /* 12555 * Process any directory removals that have been committed. 12556 */ 12557 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) { 12558 LIST_REMOVE(dirrem, dm_next); 12559 dirrem->dm_state |= COMPLETE; 12560 dirrem->dm_dirinum = pagedep->pd_ino; 12561 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 12562 ("handle_written_filepage: Journal entries not written.")); 12563 add_to_worklist(&dirrem->dm_list, 0); 12564 } 12565 /* 12566 * Free any directory additions that have been committed. 12567 * If it is a newly allocated block, we have to wait until 12568 * the on-disk directory inode claims the new block. 12569 */ 12570 if ((pagedep->pd_state & NEWBLOCK) == 0) 12571 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 12572 free_diradd(dap, NULL); 12573 rollforward: 12574 /* 12575 * Uncommitted directory entries must be restored. 12576 */ 12577 for (chgs = 0, i = 0; i < DAHASHSZ; i++) { 12578 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap; 12579 dap = nextdap) { 12580 nextdap = LIST_NEXT(dap, da_pdlist); 12581 if (dap->da_state & ATTACHED) 12582 panic("handle_written_filepage: attached"); 12583 ep = (struct direct *) 12584 ((char *)bp->b_data + dap->da_offset); 12585 ep->d_ino = dap->da_newinum; 12586 dap->da_state &= ~UNDONE; 12587 dap->da_state |= ATTACHED; 12588 chgs = 1; 12589 /* 12590 * If the inode referenced by the directory has 12591 * been written out, then the dependency can be 12592 * moved to the pending list. 12593 */ 12594 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 12595 LIST_REMOVE(dap, da_pdlist); 12596 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, 12597 da_pdlist); 12598 } 12599 } 12600 } 12601 /* 12602 * If there were any rollbacks in the directory, then it must be 12603 * marked dirty so that its will eventually get written back in 12604 * its correct form. 12605 */ 12606 if (chgs || (flags & WRITESUCCEEDED) == 0) { 12607 if ((bp->b_flags & B_DELWRI) == 0) 12608 stat_dir_entry++; 12609 bdirty(bp); 12610 return (1); 12611 } 12612 /* 12613 * If we are not waiting for a new directory block to be 12614 * claimed by its inode, then the pagedep will be freed. 12615 * Otherwise it will remain to track any new entries on 12616 * the page in case they are fsync'ed. 12617 */ 12618 free_pagedep(pagedep); 12619 return (0); 12620 } 12621 12622 /* 12623 * Writing back in-core inode structures. 12624 * 12625 * The filesystem only accesses an inode's contents when it occupies an 12626 * "in-core" inode structure. These "in-core" structures are separate from 12627 * the page frames used to cache inode blocks. Only the latter are 12628 * transferred to/from the disk. So, when the updated contents of the 12629 * "in-core" inode structure are copied to the corresponding in-memory inode 12630 * block, the dependencies are also transferred. The following procedure is 12631 * called when copying a dirty "in-core" inode to a cached inode block. 12632 */ 12633 12634 /* 12635 * Called when an inode is loaded from disk. If the effective link count 12636 * differed from the actual link count when it was last flushed, then we 12637 * need to ensure that the correct effective link count is put back. 12638 */ 12639 void 12640 softdep_load_inodeblock(ip) 12641 struct inode *ip; /* the "in_core" copy of the inode */ 12642 { 12643 struct inodedep *inodedep; 12644 struct ufsmount *ump; 12645 12646 ump = ITOUMP(ip); 12647 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 12648 ("softdep_load_inodeblock called on non-softdep filesystem")); 12649 /* 12650 * Check for alternate nlink count. 12651 */ 12652 ip->i_effnlink = ip->i_nlink; 12653 ACQUIRE_LOCK(ump); 12654 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) { 12655 FREE_LOCK(ump); 12656 return; 12657 } 12658 if (ip->i_nlink != inodedep->id_nlinkwrote && 12659 inodedep->id_nlinkwrote != -1) { 12660 KASSERT(ip->i_nlink == 0 && 12661 (ump->um_flags & UM_FSFAIL_CLEANUP) != 0, 12662 ("read bad i_nlink value")); 12663 ip->i_effnlink = ip->i_nlink = inodedep->id_nlinkwrote; 12664 } 12665 ip->i_effnlink -= inodedep->id_nlinkdelta; 12666 KASSERT(ip->i_effnlink >= 0, 12667 ("softdep_load_inodeblock: negative i_effnlink")); 12668 FREE_LOCK(ump); 12669 } 12670 12671 /* 12672 * This routine is called just before the "in-core" inode 12673 * information is to be copied to the in-memory inode block. 12674 * Recall that an inode block contains several inodes. If 12675 * the force flag is set, then the dependencies will be 12676 * cleared so that the update can always be made. Note that 12677 * the buffer is locked when this routine is called, so we 12678 * will never be in the middle of writing the inode block 12679 * to disk. 12680 */ 12681 void 12682 softdep_update_inodeblock(ip, bp, waitfor) 12683 struct inode *ip; /* the "in_core" copy of the inode */ 12684 struct buf *bp; /* the buffer containing the inode block */ 12685 int waitfor; /* nonzero => update must be allowed */ 12686 { 12687 struct inodedep *inodedep; 12688 struct inoref *inoref; 12689 struct ufsmount *ump; 12690 struct worklist *wk; 12691 struct mount *mp; 12692 struct buf *ibp; 12693 struct fs *fs; 12694 int error; 12695 12696 ump = ITOUMP(ip); 12697 mp = UFSTOVFS(ump); 12698 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 12699 ("softdep_update_inodeblock called on non-softdep filesystem")); 12700 fs = ump->um_fs; 12701 /* 12702 * Preserve the freelink that is on disk. clear_unlinked_inodedep() 12703 * does not have access to the in-core ip so must write directly into 12704 * the inode block buffer when setting freelink. 12705 */ 12706 if (fs->fs_magic == FS_UFS1_MAGIC) 12707 DIP_SET(ip, i_freelink, ((struct ufs1_dinode *)bp->b_data + 12708 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12709 else 12710 DIP_SET(ip, i_freelink, ((struct ufs2_dinode *)bp->b_data + 12711 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12712 /* 12713 * If the effective link count is not equal to the actual link 12714 * count, then we must track the difference in an inodedep while 12715 * the inode is (potentially) tossed out of the cache. Otherwise, 12716 * if there is no existing inodedep, then there are no dependencies 12717 * to track. 12718 */ 12719 ACQUIRE_LOCK(ump); 12720 again: 12721 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12722 FREE_LOCK(ump); 12723 if (ip->i_effnlink != ip->i_nlink) 12724 panic("softdep_update_inodeblock: bad link count"); 12725 return; 12726 } 12727 KASSERT(ip->i_nlink >= inodedep->id_nlinkdelta, 12728 ("softdep_update_inodeblock inconsistent ip %p i_nlink %d " 12729 "inodedep %p id_nlinkdelta %jd", 12730 ip, ip->i_nlink, inodedep, (intmax_t)inodedep->id_nlinkdelta)); 12731 inodedep->id_nlinkwrote = ip->i_nlink; 12732 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink) 12733 panic("softdep_update_inodeblock: bad delta"); 12734 /* 12735 * If we're flushing all dependencies we must also move any waiting 12736 * for journal writes onto the bufwait list prior to I/O. 12737 */ 12738 if (waitfor) { 12739 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12740 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12741 == DEPCOMPLETE) { 12742 jwait(&inoref->if_list, MNT_WAIT); 12743 goto again; 12744 } 12745 } 12746 } 12747 /* 12748 * Changes have been initiated. Anything depending on these 12749 * changes cannot occur until this inode has been written. 12750 */ 12751 inodedep->id_state &= ~COMPLETE; 12752 if ((inodedep->id_state & ONWORKLIST) == 0) 12753 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list); 12754 /* 12755 * Any new dependencies associated with the incore inode must 12756 * now be moved to the list associated with the buffer holding 12757 * the in-memory copy of the inode. Once merged process any 12758 * allocdirects that are completed by the merger. 12759 */ 12760 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt); 12761 if (!TAILQ_EMPTY(&inodedep->id_inoupdt)) 12762 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt), 12763 NULL); 12764 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt); 12765 if (!TAILQ_EMPTY(&inodedep->id_extupdt)) 12766 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt), 12767 NULL); 12768 /* 12769 * Now that the inode has been pushed into the buffer, the 12770 * operations dependent on the inode being written to disk 12771 * can be moved to the id_bufwait so that they will be 12772 * processed when the buffer I/O completes. 12773 */ 12774 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) { 12775 WORKLIST_REMOVE(wk); 12776 WORKLIST_INSERT(&inodedep->id_bufwait, wk); 12777 } 12778 /* 12779 * Newly allocated inodes cannot be written until the bitmap 12780 * that allocates them have been written (indicated by 12781 * DEPCOMPLETE being set in id_state). If we are doing a 12782 * forced sync (e.g., an fsync on a file), we force the bitmap 12783 * to be written so that the update can be done. 12784 */ 12785 if (waitfor == 0) { 12786 FREE_LOCK(ump); 12787 return; 12788 } 12789 retry: 12790 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) { 12791 FREE_LOCK(ump); 12792 return; 12793 } 12794 ibp = inodedep->id_bmsafemap->sm_buf; 12795 ibp = getdirtybuf(ibp, LOCK_PTR(ump), MNT_WAIT); 12796 if (ibp == NULL) { 12797 /* 12798 * If ibp came back as NULL, the dependency could have been 12799 * freed while we slept. Look it up again, and check to see 12800 * that it has completed. 12801 */ 12802 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 12803 goto retry; 12804 FREE_LOCK(ump); 12805 return; 12806 } 12807 FREE_LOCK(ump); 12808 if ((error = bwrite(ibp)) != 0) 12809 softdep_error("softdep_update_inodeblock: bwrite", error); 12810 } 12811 12812 /* 12813 * Merge the a new inode dependency list (such as id_newinoupdt) into an 12814 * old inode dependency list (such as id_inoupdt). 12815 */ 12816 static void 12817 merge_inode_lists(newlisthead, oldlisthead) 12818 struct allocdirectlst *newlisthead; 12819 struct allocdirectlst *oldlisthead; 12820 { 12821 struct allocdirect *listadp, *newadp; 12822 12823 newadp = TAILQ_FIRST(newlisthead); 12824 if (newadp != NULL) 12825 LOCK_OWNED(VFSTOUFS(newadp->ad_block.nb_list.wk_mp)); 12826 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) { 12827 if (listadp->ad_offset < newadp->ad_offset) { 12828 listadp = TAILQ_NEXT(listadp, ad_next); 12829 continue; 12830 } 12831 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12832 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next); 12833 if (listadp->ad_offset == newadp->ad_offset) { 12834 allocdirect_merge(oldlisthead, newadp, 12835 listadp); 12836 listadp = newadp; 12837 } 12838 newadp = TAILQ_FIRST(newlisthead); 12839 } 12840 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) { 12841 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12842 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next); 12843 } 12844 } 12845 12846 /* 12847 * If we are doing an fsync, then we must ensure that any directory 12848 * entries for the inode have been written after the inode gets to disk. 12849 */ 12850 int 12851 softdep_fsync(vp) 12852 struct vnode *vp; /* the "in_core" copy of the inode */ 12853 { 12854 struct inodedep *inodedep; 12855 struct pagedep *pagedep; 12856 struct inoref *inoref; 12857 struct ufsmount *ump; 12858 struct worklist *wk; 12859 struct diradd *dap; 12860 struct mount *mp; 12861 struct vnode *pvp; 12862 struct inode *ip; 12863 struct buf *bp; 12864 struct fs *fs; 12865 struct thread *td = curthread; 12866 int error, flushparent, pagedep_new_block; 12867 ino_t parentino; 12868 ufs_lbn_t lbn; 12869 12870 ip = VTOI(vp); 12871 mp = vp->v_mount; 12872 ump = VFSTOUFS(mp); 12873 fs = ump->um_fs; 12874 if (MOUNTEDSOFTDEP(mp) == 0) 12875 return (0); 12876 ACQUIRE_LOCK(ump); 12877 restart: 12878 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12879 FREE_LOCK(ump); 12880 return (0); 12881 } 12882 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12883 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12884 == DEPCOMPLETE) { 12885 jwait(&inoref->if_list, MNT_WAIT); 12886 goto restart; 12887 } 12888 } 12889 if (!LIST_EMPTY(&inodedep->id_inowait) || 12890 !TAILQ_EMPTY(&inodedep->id_extupdt) || 12891 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 12892 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 12893 !TAILQ_EMPTY(&inodedep->id_newinoupdt)) 12894 panic("softdep_fsync: pending ops %p", inodedep); 12895 for (error = 0, flushparent = 0; ; ) { 12896 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL) 12897 break; 12898 if (wk->wk_type != D_DIRADD) 12899 panic("softdep_fsync: Unexpected type %s", 12900 TYPENAME(wk->wk_type)); 12901 dap = WK_DIRADD(wk); 12902 /* 12903 * Flush our parent if this directory entry has a MKDIR_PARENT 12904 * dependency or is contained in a newly allocated block. 12905 */ 12906 if (dap->da_state & DIRCHG) 12907 pagedep = dap->da_previous->dm_pagedep; 12908 else 12909 pagedep = dap->da_pagedep; 12910 parentino = pagedep->pd_ino; 12911 lbn = pagedep->pd_lbn; 12912 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) 12913 panic("softdep_fsync: dirty"); 12914 if ((dap->da_state & MKDIR_PARENT) || 12915 (pagedep->pd_state & NEWBLOCK)) 12916 flushparent = 1; 12917 else 12918 flushparent = 0; 12919 /* 12920 * If we are being fsync'ed as part of vgone'ing this vnode, 12921 * then we will not be able to release and recover the 12922 * vnode below, so we just have to give up on writing its 12923 * directory entry out. It will eventually be written, just 12924 * not now, but then the user was not asking to have it 12925 * written, so we are not breaking any promises. 12926 */ 12927 if (VN_IS_DOOMED(vp)) 12928 break; 12929 /* 12930 * We prevent deadlock by always fetching inodes from the 12931 * root, moving down the directory tree. Thus, when fetching 12932 * our parent directory, we first try to get the lock. If 12933 * that fails, we must unlock ourselves before requesting 12934 * the lock on our parent. See the comment in ufs_lookup 12935 * for details on possible races. 12936 */ 12937 FREE_LOCK(ump); 12938 error = get_parent_vp(vp, mp, parentino, NULL, NULL, NULL, 12939 &pvp); 12940 if (error == ERELOOKUP) 12941 error = 0; 12942 if (error != 0) 12943 return (error); 12944 /* 12945 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps 12946 * that are contained in direct blocks will be resolved by 12947 * doing a ffs_update. Pagedeps contained in indirect blocks 12948 * may require a complete sync'ing of the directory. So, we 12949 * try the cheap and fast ffs_update first, and if that fails, 12950 * then we do the slower ffs_syncvnode of the directory. 12951 */ 12952 if (flushparent) { 12953 int locked; 12954 12955 if ((error = ffs_update(pvp, 1)) != 0) { 12956 vput(pvp); 12957 return (error); 12958 } 12959 ACQUIRE_LOCK(ump); 12960 locked = 1; 12961 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) { 12962 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) { 12963 if (wk->wk_type != D_DIRADD) 12964 panic("softdep_fsync: Unexpected type %s", 12965 TYPENAME(wk->wk_type)); 12966 dap = WK_DIRADD(wk); 12967 if (dap->da_state & DIRCHG) 12968 pagedep = dap->da_previous->dm_pagedep; 12969 else 12970 pagedep = dap->da_pagedep; 12971 pagedep_new_block = pagedep->pd_state & NEWBLOCK; 12972 FREE_LOCK(ump); 12973 locked = 0; 12974 if (pagedep_new_block && (error = 12975 ffs_syncvnode(pvp, MNT_WAIT, 0))) { 12976 vput(pvp); 12977 return (error); 12978 } 12979 } 12980 } 12981 if (locked) 12982 FREE_LOCK(ump); 12983 } 12984 /* 12985 * Flush directory page containing the inode's name. 12986 */ 12987 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred, 12988 &bp); 12989 if (error == 0) 12990 error = bwrite(bp); 12991 else 12992 brelse(bp); 12993 vput(pvp); 12994 if (!ffs_fsfail_cleanup(ump, error)) 12995 return (error); 12996 ACQUIRE_LOCK(ump); 12997 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 12998 break; 12999 } 13000 FREE_LOCK(ump); 13001 return (0); 13002 } 13003 13004 /* 13005 * Flush all the dirty bitmaps associated with the block device 13006 * before flushing the rest of the dirty blocks so as to reduce 13007 * the number of dependencies that will have to be rolled back. 13008 * 13009 * XXX Unused? 13010 */ 13011 void 13012 softdep_fsync_mountdev(vp) 13013 struct vnode *vp; 13014 { 13015 struct buf *bp, *nbp; 13016 struct worklist *wk; 13017 struct bufobj *bo; 13018 13019 if (!vn_isdisk(vp)) 13020 panic("softdep_fsync_mountdev: vnode not a disk"); 13021 bo = &vp->v_bufobj; 13022 restart: 13023 BO_LOCK(bo); 13024 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 13025 /* 13026 * If it is already scheduled, skip to the next buffer. 13027 */ 13028 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) 13029 continue; 13030 13031 if ((bp->b_flags & B_DELWRI) == 0) 13032 panic("softdep_fsync_mountdev: not dirty"); 13033 /* 13034 * We are only interested in bitmaps with outstanding 13035 * dependencies. 13036 */ 13037 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL || 13038 wk->wk_type != D_BMSAFEMAP || 13039 (bp->b_vflags & BV_BKGRDINPROG)) { 13040 BUF_UNLOCK(bp); 13041 continue; 13042 } 13043 BO_UNLOCK(bo); 13044 bremfree(bp); 13045 (void) bawrite(bp); 13046 goto restart; 13047 } 13048 drain_output(vp); 13049 BO_UNLOCK(bo); 13050 } 13051 13052 /* 13053 * Sync all cylinder groups that were dirty at the time this function is 13054 * called. Newly dirtied cgs will be inserted before the sentinel. This 13055 * is used to flush freedep activity that may be holding up writes to a 13056 * indirect block. 13057 */ 13058 static int 13059 sync_cgs(mp, waitfor) 13060 struct mount *mp; 13061 int waitfor; 13062 { 13063 struct bmsafemap *bmsafemap; 13064 struct bmsafemap *sentinel; 13065 struct ufsmount *ump; 13066 struct buf *bp; 13067 int error; 13068 13069 sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK); 13070 sentinel->sm_cg = -1; 13071 ump = VFSTOUFS(mp); 13072 error = 0; 13073 ACQUIRE_LOCK(ump); 13074 LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next); 13075 for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL; 13076 bmsafemap = LIST_NEXT(sentinel, sm_next)) { 13077 /* Skip sentinels and cgs with no work to release. */ 13078 if (bmsafemap->sm_cg == -1 || 13079 (LIST_EMPTY(&bmsafemap->sm_freehd) && 13080 LIST_EMPTY(&bmsafemap->sm_freewr))) { 13081 LIST_REMOVE(sentinel, sm_next); 13082 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 13083 continue; 13084 } 13085 /* 13086 * If we don't get the lock and we're waiting try again, if 13087 * not move on to the next buf and try to sync it. 13088 */ 13089 bp = getdirtybuf(bmsafemap->sm_buf, LOCK_PTR(ump), waitfor); 13090 if (bp == NULL && waitfor == MNT_WAIT) 13091 continue; 13092 LIST_REMOVE(sentinel, sm_next); 13093 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 13094 if (bp == NULL) 13095 continue; 13096 FREE_LOCK(ump); 13097 if (waitfor == MNT_NOWAIT) 13098 bawrite(bp); 13099 else 13100 error = bwrite(bp); 13101 ACQUIRE_LOCK(ump); 13102 if (error) 13103 break; 13104 } 13105 LIST_REMOVE(sentinel, sm_next); 13106 FREE_LOCK(ump); 13107 free(sentinel, M_BMSAFEMAP); 13108 return (error); 13109 } 13110 13111 /* 13112 * This routine is called when we are trying to synchronously flush a 13113 * file. This routine must eliminate any filesystem metadata dependencies 13114 * so that the syncing routine can succeed. 13115 */ 13116 int 13117 softdep_sync_metadata(struct vnode *vp) 13118 { 13119 struct inode *ip; 13120 int error; 13121 13122 ip = VTOI(vp); 13123 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 13124 ("softdep_sync_metadata called on non-softdep filesystem")); 13125 /* 13126 * Ensure that any direct block dependencies have been cleared, 13127 * truncations are started, and inode references are journaled. 13128 */ 13129 ACQUIRE_LOCK(VFSTOUFS(vp->v_mount)); 13130 /* 13131 * Write all journal records to prevent rollbacks on devvp. 13132 */ 13133 if (vp->v_type == VCHR) 13134 softdep_flushjournal(vp->v_mount); 13135 error = flush_inodedep_deps(vp, vp->v_mount, ip->i_number); 13136 /* 13137 * Ensure that all truncates are written so we won't find deps on 13138 * indirect blocks. 13139 */ 13140 process_truncates(vp); 13141 FREE_LOCK(VFSTOUFS(vp->v_mount)); 13142 13143 return (error); 13144 } 13145 13146 /* 13147 * This routine is called when we are attempting to sync a buf with 13148 * dependencies. If waitfor is MNT_NOWAIT it attempts to schedule any 13149 * other IO it can but returns EBUSY if the buffer is not yet able to 13150 * be written. Dependencies which will not cause rollbacks will always 13151 * return 0. 13152 */ 13153 int 13154 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 13155 { 13156 struct indirdep *indirdep; 13157 struct pagedep *pagedep; 13158 struct allocindir *aip; 13159 struct newblk *newblk; 13160 struct ufsmount *ump; 13161 struct buf *nbp; 13162 struct worklist *wk; 13163 int i, error; 13164 13165 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 13166 ("softdep_sync_buf called on non-softdep filesystem")); 13167 /* 13168 * For VCHR we just don't want to force flush any dependencies that 13169 * will cause rollbacks. 13170 */ 13171 if (vp->v_type == VCHR) { 13172 if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0)) 13173 return (EBUSY); 13174 return (0); 13175 } 13176 ump = VFSTOUFS(vp->v_mount); 13177 ACQUIRE_LOCK(ump); 13178 /* 13179 * As we hold the buffer locked, none of its dependencies 13180 * will disappear. 13181 */ 13182 error = 0; 13183 top: 13184 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 13185 switch (wk->wk_type) { 13186 case D_ALLOCDIRECT: 13187 case D_ALLOCINDIR: 13188 newblk = WK_NEWBLK(wk); 13189 if (newblk->nb_jnewblk != NULL) { 13190 if (waitfor == MNT_NOWAIT) { 13191 error = EBUSY; 13192 goto out_unlock; 13193 } 13194 jwait(&newblk->nb_jnewblk->jn_list, waitfor); 13195 goto top; 13196 } 13197 if (newblk->nb_state & DEPCOMPLETE || 13198 waitfor == MNT_NOWAIT) 13199 continue; 13200 nbp = newblk->nb_bmsafemap->sm_buf; 13201 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 13202 if (nbp == NULL) 13203 goto top; 13204 FREE_LOCK(ump); 13205 if ((error = bwrite(nbp)) != 0) 13206 goto out; 13207 ACQUIRE_LOCK(ump); 13208 continue; 13209 13210 case D_INDIRDEP: 13211 indirdep = WK_INDIRDEP(wk); 13212 if (waitfor == MNT_NOWAIT) { 13213 if (!TAILQ_EMPTY(&indirdep->ir_trunc) || 13214 !LIST_EMPTY(&indirdep->ir_deplisthd)) { 13215 error = EBUSY; 13216 goto out_unlock; 13217 } 13218 } 13219 if (!TAILQ_EMPTY(&indirdep->ir_trunc)) 13220 panic("softdep_sync_buf: truncation pending."); 13221 restart: 13222 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 13223 newblk = (struct newblk *)aip; 13224 if (newblk->nb_jnewblk != NULL) { 13225 jwait(&newblk->nb_jnewblk->jn_list, 13226 waitfor); 13227 goto restart; 13228 } 13229 if (newblk->nb_state & DEPCOMPLETE) 13230 continue; 13231 nbp = newblk->nb_bmsafemap->sm_buf; 13232 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 13233 if (nbp == NULL) 13234 goto restart; 13235 FREE_LOCK(ump); 13236 if ((error = bwrite(nbp)) != 0) 13237 goto out; 13238 ACQUIRE_LOCK(ump); 13239 goto restart; 13240 } 13241 continue; 13242 13243 case D_PAGEDEP: 13244 /* 13245 * Only flush directory entries in synchronous passes. 13246 */ 13247 if (waitfor != MNT_WAIT) { 13248 error = EBUSY; 13249 goto out_unlock; 13250 } 13251 /* 13252 * While syncing snapshots, we must allow recursive 13253 * lookups. 13254 */ 13255 BUF_AREC(bp); 13256 /* 13257 * We are trying to sync a directory that may 13258 * have dependencies on both its own metadata 13259 * and/or dependencies on the inodes of any 13260 * recently allocated files. We walk its diradd 13261 * lists pushing out the associated inode. 13262 */ 13263 pagedep = WK_PAGEDEP(wk); 13264 for (i = 0; i < DAHASHSZ; i++) { 13265 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0) 13266 continue; 13267 error = flush_pagedep_deps(vp, wk->wk_mp, 13268 &pagedep->pd_diraddhd[i], bp); 13269 if (error != 0) { 13270 if (error != ERELOOKUP) 13271 BUF_NOREC(bp); 13272 goto out_unlock; 13273 } 13274 } 13275 BUF_NOREC(bp); 13276 continue; 13277 13278 case D_FREEWORK: 13279 case D_FREEDEP: 13280 case D_JSEGDEP: 13281 case D_JNEWBLK: 13282 continue; 13283 13284 default: 13285 panic("softdep_sync_buf: Unknown type %s", 13286 TYPENAME(wk->wk_type)); 13287 /* NOTREACHED */ 13288 } 13289 } 13290 out_unlock: 13291 FREE_LOCK(ump); 13292 out: 13293 return (error); 13294 } 13295 13296 /* 13297 * Flush the dependencies associated with an inodedep. 13298 */ 13299 static int 13300 flush_inodedep_deps(vp, mp, ino) 13301 struct vnode *vp; 13302 struct mount *mp; 13303 ino_t ino; 13304 { 13305 struct inodedep *inodedep; 13306 struct inoref *inoref; 13307 struct ufsmount *ump; 13308 int error, waitfor; 13309 13310 /* 13311 * This work is done in two passes. The first pass grabs most 13312 * of the buffers and begins asynchronously writing them. The 13313 * only way to wait for these asynchronous writes is to sleep 13314 * on the filesystem vnode which may stay busy for a long time 13315 * if the filesystem is active. So, instead, we make a second 13316 * pass over the dependencies blocking on each write. In the 13317 * usual case we will be blocking against a write that we 13318 * initiated, so when it is done the dependency will have been 13319 * resolved. Thus the second pass is expected to end quickly. 13320 * We give a brief window at the top of the loop to allow 13321 * any pending I/O to complete. 13322 */ 13323 ump = VFSTOUFS(mp); 13324 LOCK_OWNED(ump); 13325 for (error = 0, waitfor = MNT_NOWAIT; ; ) { 13326 if (error) 13327 return (error); 13328 FREE_LOCK(ump); 13329 ACQUIRE_LOCK(ump); 13330 restart: 13331 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 13332 return (0); 13333 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 13334 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 13335 == DEPCOMPLETE) { 13336 jwait(&inoref->if_list, MNT_WAIT); 13337 goto restart; 13338 } 13339 } 13340 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) || 13341 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) || 13342 flush_deplist(&inodedep->id_extupdt, waitfor, &error) || 13343 flush_deplist(&inodedep->id_newextupdt, waitfor, &error)) 13344 continue; 13345 /* 13346 * If pass2, we are done, otherwise do pass 2. 13347 */ 13348 if (waitfor == MNT_WAIT) 13349 break; 13350 waitfor = MNT_WAIT; 13351 } 13352 /* 13353 * Try freeing inodedep in case all dependencies have been removed. 13354 */ 13355 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0) 13356 (void) free_inodedep(inodedep); 13357 return (0); 13358 } 13359 13360 /* 13361 * Flush an inode dependency list. 13362 */ 13363 static int 13364 flush_deplist(listhead, waitfor, errorp) 13365 struct allocdirectlst *listhead; 13366 int waitfor; 13367 int *errorp; 13368 { 13369 struct allocdirect *adp; 13370 struct newblk *newblk; 13371 struct ufsmount *ump; 13372 struct buf *bp; 13373 13374 if ((adp = TAILQ_FIRST(listhead)) == NULL) 13375 return (0); 13376 ump = VFSTOUFS(adp->ad_list.wk_mp); 13377 LOCK_OWNED(ump); 13378 TAILQ_FOREACH(adp, listhead, ad_next) { 13379 newblk = (struct newblk *)adp; 13380 if (newblk->nb_jnewblk != NULL) { 13381 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 13382 return (1); 13383 } 13384 if (newblk->nb_state & DEPCOMPLETE) 13385 continue; 13386 bp = newblk->nb_bmsafemap->sm_buf; 13387 bp = getdirtybuf(bp, LOCK_PTR(ump), waitfor); 13388 if (bp == NULL) { 13389 if (waitfor == MNT_NOWAIT) 13390 continue; 13391 return (1); 13392 } 13393 FREE_LOCK(ump); 13394 if (waitfor == MNT_NOWAIT) 13395 bawrite(bp); 13396 else 13397 *errorp = bwrite(bp); 13398 ACQUIRE_LOCK(ump); 13399 return (1); 13400 } 13401 return (0); 13402 } 13403 13404 /* 13405 * Flush dependencies associated with an allocdirect block. 13406 */ 13407 static int 13408 flush_newblk_dep(vp, mp, lbn) 13409 struct vnode *vp; 13410 struct mount *mp; 13411 ufs_lbn_t lbn; 13412 { 13413 struct newblk *newblk; 13414 struct ufsmount *ump; 13415 struct bufobj *bo; 13416 struct inode *ip; 13417 struct buf *bp; 13418 ufs2_daddr_t blkno; 13419 int error; 13420 13421 error = 0; 13422 bo = &vp->v_bufobj; 13423 ip = VTOI(vp); 13424 blkno = DIP(ip, i_db[lbn]); 13425 if (blkno == 0) 13426 panic("flush_newblk_dep: Missing block"); 13427 ump = VFSTOUFS(mp); 13428 ACQUIRE_LOCK(ump); 13429 /* 13430 * Loop until all dependencies related to this block are satisfied. 13431 * We must be careful to restart after each sleep in case a write 13432 * completes some part of this process for us. 13433 */ 13434 for (;;) { 13435 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) { 13436 FREE_LOCK(ump); 13437 break; 13438 } 13439 if (newblk->nb_list.wk_type != D_ALLOCDIRECT) 13440 panic("flush_newblk_dep: Bad newblk %p", newblk); 13441 /* 13442 * Flush the journal. 13443 */ 13444 if (newblk->nb_jnewblk != NULL) { 13445 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 13446 continue; 13447 } 13448 /* 13449 * Write the bitmap dependency. 13450 */ 13451 if ((newblk->nb_state & DEPCOMPLETE) == 0) { 13452 bp = newblk->nb_bmsafemap->sm_buf; 13453 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 13454 if (bp == NULL) 13455 continue; 13456 FREE_LOCK(ump); 13457 error = bwrite(bp); 13458 if (error) 13459 break; 13460 ACQUIRE_LOCK(ump); 13461 continue; 13462 } 13463 /* 13464 * Write the buffer. 13465 */ 13466 FREE_LOCK(ump); 13467 BO_LOCK(bo); 13468 bp = gbincore(bo, lbn); 13469 if (bp != NULL) { 13470 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 13471 LK_INTERLOCK, BO_LOCKPTR(bo)); 13472 if (error == ENOLCK) { 13473 ACQUIRE_LOCK(ump); 13474 error = 0; 13475 continue; /* Slept, retry */ 13476 } 13477 if (error != 0) 13478 break; /* Failed */ 13479 if (bp->b_flags & B_DELWRI) { 13480 bremfree(bp); 13481 error = bwrite(bp); 13482 if (error) 13483 break; 13484 } else 13485 BUF_UNLOCK(bp); 13486 } else 13487 BO_UNLOCK(bo); 13488 /* 13489 * We have to wait for the direct pointers to 13490 * point at the newdirblk before the dependency 13491 * will go away. 13492 */ 13493 error = ffs_update(vp, 1); 13494 if (error) 13495 break; 13496 ACQUIRE_LOCK(ump); 13497 } 13498 return (error); 13499 } 13500 13501 /* 13502 * Eliminate a pagedep dependency by flushing out all its diradd dependencies. 13503 */ 13504 static int 13505 flush_pagedep_deps(pvp, mp, diraddhdp, locked_bp) 13506 struct vnode *pvp; 13507 struct mount *mp; 13508 struct diraddhd *diraddhdp; 13509 struct buf *locked_bp; 13510 { 13511 struct inodedep *inodedep; 13512 struct inoref *inoref; 13513 struct ufsmount *ump; 13514 struct diradd *dap; 13515 struct vnode *vp; 13516 int error = 0; 13517 struct buf *bp; 13518 ino_t inum; 13519 struct diraddhd unfinished; 13520 13521 LIST_INIT(&unfinished); 13522 ump = VFSTOUFS(mp); 13523 LOCK_OWNED(ump); 13524 restart: 13525 while ((dap = LIST_FIRST(diraddhdp)) != NULL) { 13526 /* 13527 * Flush ourselves if this directory entry 13528 * has a MKDIR_PARENT dependency. 13529 */ 13530 if (dap->da_state & MKDIR_PARENT) { 13531 FREE_LOCK(ump); 13532 if ((error = ffs_update(pvp, 1)) != 0) 13533 break; 13534 ACQUIRE_LOCK(ump); 13535 /* 13536 * If that cleared dependencies, go on to next. 13537 */ 13538 if (dap != LIST_FIRST(diraddhdp)) 13539 continue; 13540 /* 13541 * All MKDIR_PARENT dependencies and all the 13542 * NEWBLOCK pagedeps that are contained in direct 13543 * blocks were resolved by doing above ffs_update. 13544 * Pagedeps contained in indirect blocks may 13545 * require a complete sync'ing of the directory. 13546 * We are in the midst of doing a complete sync, 13547 * so if they are not resolved in this pass we 13548 * defer them for now as they will be sync'ed by 13549 * our caller shortly. 13550 */ 13551 LIST_REMOVE(dap, da_pdlist); 13552 LIST_INSERT_HEAD(&unfinished, dap, da_pdlist); 13553 continue; 13554 } 13555 /* 13556 * A newly allocated directory must have its "." and 13557 * ".." entries written out before its name can be 13558 * committed in its parent. 13559 */ 13560 inum = dap->da_newinum; 13561 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 13562 panic("flush_pagedep_deps: lost inode1"); 13563 /* 13564 * Wait for any pending journal adds to complete so we don't 13565 * cause rollbacks while syncing. 13566 */ 13567 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 13568 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 13569 == DEPCOMPLETE) { 13570 jwait(&inoref->if_list, MNT_WAIT); 13571 goto restart; 13572 } 13573 } 13574 if (dap->da_state & MKDIR_BODY) { 13575 FREE_LOCK(ump); 13576 error = get_parent_vp(pvp, mp, inum, locked_bp, 13577 diraddhdp, &unfinished, &vp); 13578 if (error != 0) 13579 break; 13580 error = flush_newblk_dep(vp, mp, 0); 13581 /* 13582 * If we still have the dependency we might need to 13583 * update the vnode to sync the new link count to 13584 * disk. 13585 */ 13586 if (error == 0 && dap == LIST_FIRST(diraddhdp)) 13587 error = ffs_update(vp, 1); 13588 vput(vp); 13589 if (error != 0) 13590 break; 13591 ACQUIRE_LOCK(ump); 13592 /* 13593 * If that cleared dependencies, go on to next. 13594 */ 13595 if (dap != LIST_FIRST(diraddhdp)) 13596 continue; 13597 if (dap->da_state & MKDIR_BODY) { 13598 inodedep_lookup(UFSTOVFS(ump), inum, 0, 13599 &inodedep); 13600 panic("flush_pagedep_deps: MKDIR_BODY " 13601 "inodedep %p dap %p vp %p", 13602 inodedep, dap, vp); 13603 } 13604 } 13605 /* 13606 * Flush the inode on which the directory entry depends. 13607 * Having accounted for MKDIR_PARENT and MKDIR_BODY above, 13608 * the only remaining dependency is that the updated inode 13609 * count must get pushed to disk. The inode has already 13610 * been pushed into its inode buffer (via VOP_UPDATE) at 13611 * the time of the reference count change. So we need only 13612 * locate that buffer, ensure that there will be no rollback 13613 * caused by a bitmap dependency, then write the inode buffer. 13614 */ 13615 retry: 13616 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 13617 panic("flush_pagedep_deps: lost inode"); 13618 /* 13619 * If the inode still has bitmap dependencies, 13620 * push them to disk. 13621 */ 13622 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) { 13623 bp = inodedep->id_bmsafemap->sm_buf; 13624 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 13625 if (bp == NULL) 13626 goto retry; 13627 FREE_LOCK(ump); 13628 if ((error = bwrite(bp)) != 0) 13629 break; 13630 ACQUIRE_LOCK(ump); 13631 if (dap != LIST_FIRST(diraddhdp)) 13632 continue; 13633 } 13634 /* 13635 * If the inode is still sitting in a buffer waiting 13636 * to be written or waiting for the link count to be 13637 * adjusted update it here to flush it to disk. 13638 */ 13639 if (dap == LIST_FIRST(diraddhdp)) { 13640 FREE_LOCK(ump); 13641 error = get_parent_vp(pvp, mp, inum, locked_bp, 13642 diraddhdp, &unfinished, &vp); 13643 if (error != 0) 13644 break; 13645 error = ffs_update(vp, 1); 13646 vput(vp); 13647 if (error) 13648 break; 13649 ACQUIRE_LOCK(ump); 13650 } 13651 /* 13652 * If we have failed to get rid of all the dependencies 13653 * then something is seriously wrong. 13654 */ 13655 if (dap == LIST_FIRST(diraddhdp)) { 13656 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep); 13657 panic("flush_pagedep_deps: failed to flush " 13658 "inodedep %p ino %ju dap %p", 13659 inodedep, (uintmax_t)inum, dap); 13660 } 13661 } 13662 if (error) 13663 ACQUIRE_LOCK(ump); 13664 while ((dap = LIST_FIRST(&unfinished)) != NULL) { 13665 LIST_REMOVE(dap, da_pdlist); 13666 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist); 13667 } 13668 return (error); 13669 } 13670 13671 /* 13672 * A large burst of file addition or deletion activity can drive the 13673 * memory load excessively high. First attempt to slow things down 13674 * using the techniques below. If that fails, this routine requests 13675 * the offending operations to fall back to running synchronously 13676 * until the memory load returns to a reasonable level. 13677 */ 13678 int 13679 softdep_slowdown(vp) 13680 struct vnode *vp; 13681 { 13682 struct ufsmount *ump; 13683 int jlow; 13684 int max_softdeps_hard; 13685 13686 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 13687 ("softdep_slowdown called on non-softdep filesystem")); 13688 ump = VFSTOUFS(vp->v_mount); 13689 ACQUIRE_LOCK(ump); 13690 jlow = 0; 13691 /* 13692 * Check for journal space if needed. 13693 */ 13694 if (DOINGSUJ(vp)) { 13695 if (journal_space(ump, 0) == 0) 13696 jlow = 1; 13697 } 13698 /* 13699 * If the system is under its limits and our filesystem is 13700 * not responsible for more than our share of the usage and 13701 * we are not low on journal space, then no need to slow down. 13702 */ 13703 max_softdeps_hard = max_softdeps * 11 / 10; 13704 if (dep_current[D_DIRREM] < max_softdeps_hard / 2 && 13705 dep_current[D_INODEDEP] < max_softdeps_hard && 13706 dep_current[D_INDIRDEP] < max_softdeps_hard / 1000 && 13707 dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0 && 13708 ump->softdep_curdeps[D_DIRREM] < 13709 (max_softdeps_hard / 2) / stat_flush_threads && 13710 ump->softdep_curdeps[D_INODEDEP] < 13711 max_softdeps_hard / stat_flush_threads && 13712 ump->softdep_curdeps[D_INDIRDEP] < 13713 (max_softdeps_hard / 1000) / stat_flush_threads && 13714 ump->softdep_curdeps[D_FREEBLKS] < 13715 max_softdeps_hard / stat_flush_threads) { 13716 FREE_LOCK(ump); 13717 return (0); 13718 } 13719 /* 13720 * If the journal is low or our filesystem is over its limit 13721 * then speedup the cleanup. 13722 */ 13723 if (ump->softdep_curdeps[D_INDIRDEP] < 13724 (max_softdeps_hard / 1000) / stat_flush_threads || jlow) 13725 softdep_speedup(ump); 13726 stat_sync_limit_hit += 1; 13727 FREE_LOCK(ump); 13728 /* 13729 * We only slow down the rate at which new dependencies are 13730 * generated if we are not using journaling. With journaling, 13731 * the cleanup should always be sufficient to keep things 13732 * under control. 13733 */ 13734 if (DOINGSUJ(vp)) 13735 return (0); 13736 return (1); 13737 } 13738 13739 static int 13740 softdep_request_cleanup_filter(struct vnode *vp, void *arg __unused) 13741 { 13742 return ((vp->v_iflag & VI_OWEINACT) != 0 && vp->v_usecount == 0 && 13743 ((vp->v_vflag & VV_NOSYNC) != 0 || VTOI(vp)->i_effnlink == 0)); 13744 } 13745 13746 static void 13747 softdep_request_cleanup_inactivate(struct mount *mp) 13748 { 13749 struct vnode *vp, *mvp; 13750 int error; 13751 13752 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, softdep_request_cleanup_filter, 13753 NULL) { 13754 vholdl(vp); 13755 vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY); 13756 VI_LOCK(vp); 13757 if (vp->v_data != NULL && vp->v_usecount == 0) { 13758 while ((vp->v_iflag & VI_OWEINACT) != 0) { 13759 error = vinactive(vp); 13760 if (error != 0 && error != ERELOOKUP) 13761 break; 13762 } 13763 atomic_add_int(&stat_delayed_inact, 1); 13764 } 13765 VOP_UNLOCK(vp); 13766 vdropl(vp); 13767 } 13768 } 13769 13770 /* 13771 * Called by the allocation routines when they are about to fail 13772 * in the hope that we can free up the requested resource (inodes 13773 * or disk space). 13774 * 13775 * First check to see if the work list has anything on it. If it has, 13776 * clean up entries until we successfully free the requested resource. 13777 * Because this process holds inodes locked, we cannot handle any remove 13778 * requests that might block on a locked inode as that could lead to 13779 * deadlock. If the worklist yields none of the requested resource, 13780 * start syncing out vnodes to free up the needed space. 13781 */ 13782 int 13783 softdep_request_cleanup(fs, vp, cred, resource) 13784 struct fs *fs; 13785 struct vnode *vp; 13786 struct ucred *cred; 13787 int resource; 13788 { 13789 struct ufsmount *ump; 13790 struct mount *mp; 13791 long starttime; 13792 ufs2_daddr_t needed; 13793 int error, failed_vnode; 13794 13795 /* 13796 * If we are being called because of a process doing a 13797 * copy-on-write, then it is not safe to process any 13798 * worklist items as we will recurse into the copyonwrite 13799 * routine. This will result in an incoherent snapshot. 13800 * If the vnode that we hold is a snapshot, we must avoid 13801 * handling other resources that could cause deadlock. 13802 */ 13803 if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp))) 13804 return (0); 13805 13806 if (resource == FLUSH_BLOCKS_WAIT) 13807 stat_cleanup_blkrequests += 1; 13808 else 13809 stat_cleanup_inorequests += 1; 13810 13811 mp = vp->v_mount; 13812 ump = VFSTOUFS(mp); 13813 mtx_assert(UFS_MTX(ump), MA_OWNED); 13814 UFS_UNLOCK(ump); 13815 error = ffs_update(vp, 1); 13816 if (error != 0 || MOUNTEDSOFTDEP(mp) == 0) { 13817 UFS_LOCK(ump); 13818 return (0); 13819 } 13820 /* 13821 * If we are in need of resources, start by cleaning up 13822 * any block removals associated with our inode. 13823 */ 13824 ACQUIRE_LOCK(ump); 13825 process_removes(vp); 13826 process_truncates(vp); 13827 FREE_LOCK(ump); 13828 /* 13829 * Now clean up at least as many resources as we will need. 13830 * 13831 * When requested to clean up inodes, the number that are needed 13832 * is set by the number of simultaneous writers (mnt_writeopcount) 13833 * plus a bit of slop (2) in case some more writers show up while 13834 * we are cleaning. 13835 * 13836 * When requested to free up space, the amount of space that 13837 * we need is enough blocks to allocate a full-sized segment 13838 * (fs_contigsumsize). The number of such segments that will 13839 * be needed is set by the number of simultaneous writers 13840 * (mnt_writeopcount) plus a bit of slop (2) in case some more 13841 * writers show up while we are cleaning. 13842 * 13843 * Additionally, if we are unpriviledged and allocating space, 13844 * we need to ensure that we clean up enough blocks to get the 13845 * needed number of blocks over the threshold of the minimum 13846 * number of blocks required to be kept free by the filesystem 13847 * (fs_minfree). 13848 */ 13849 if (resource == FLUSH_INODES_WAIT) { 13850 needed = vfs_mount_fetch_counter(vp->v_mount, 13851 MNT_COUNT_WRITEOPCOUNT) + 2; 13852 } else if (resource == FLUSH_BLOCKS_WAIT) { 13853 needed = (vfs_mount_fetch_counter(vp->v_mount, 13854 MNT_COUNT_WRITEOPCOUNT) + 2) * fs->fs_contigsumsize; 13855 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE)) 13856 needed += fragstoblks(fs, 13857 roundup((fs->fs_dsize * fs->fs_minfree / 100) - 13858 fs->fs_cstotal.cs_nffree, fs->fs_frag)); 13859 } else { 13860 printf("softdep_request_cleanup: Unknown resource type %d\n", 13861 resource); 13862 UFS_LOCK(ump); 13863 return (0); 13864 } 13865 starttime = time_second; 13866 retry: 13867 if (resource == FLUSH_BLOCKS_WAIT && 13868 fs->fs_cstotal.cs_nbfree <= needed) 13869 softdep_send_speedup(ump, needed * fs->fs_bsize, 13870 BIO_SPEEDUP_TRIM); 13871 if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 && 13872 fs->fs_cstotal.cs_nbfree <= needed) || 13873 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13874 fs->fs_cstotal.cs_nifree <= needed)) { 13875 ACQUIRE_LOCK(ump); 13876 if (ump->softdep_on_worklist > 0 && 13877 process_worklist_item(UFSTOVFS(ump), 13878 ump->softdep_on_worklist, LK_NOWAIT) != 0) 13879 stat_worklist_push += 1; 13880 FREE_LOCK(ump); 13881 } 13882 13883 /* 13884 * Check that there are vnodes pending inactivation. As they 13885 * have been unlinked, inactivating them will free up their 13886 * inodes. 13887 */ 13888 ACQUIRE_LOCK(ump); 13889 if (resource == FLUSH_INODES_WAIT && 13890 fs->fs_cstotal.cs_nifree <= needed && 13891 fs->fs_pendinginodes <= needed) { 13892 if ((ump->um_softdep->sd_flags & FLUSH_DI_ACTIVE) == 0) { 13893 ump->um_softdep->sd_flags |= FLUSH_DI_ACTIVE; 13894 FREE_LOCK(ump); 13895 softdep_request_cleanup_inactivate(mp); 13896 ACQUIRE_LOCK(ump); 13897 ump->um_softdep->sd_flags &= ~FLUSH_DI_ACTIVE; 13898 wakeup(&ump->um_softdep->sd_flags); 13899 } else { 13900 while ((ump->um_softdep->sd_flags & 13901 FLUSH_DI_ACTIVE) != 0) { 13902 msleep(&ump->um_softdep->sd_flags, 13903 LOCK_PTR(ump), PVM, "ffsvina", hz); 13904 } 13905 } 13906 } 13907 FREE_LOCK(ump); 13908 13909 /* 13910 * If we still need resources and there are no more worklist 13911 * entries to process to obtain them, we have to start flushing 13912 * the dirty vnodes to force the release of additional requests 13913 * to the worklist that we can then process to reap addition 13914 * resources. We walk the vnodes associated with the mount point 13915 * until we get the needed worklist requests that we can reap. 13916 * 13917 * If there are several threads all needing to clean the same 13918 * mount point, only one is allowed to walk the mount list. 13919 * When several threads all try to walk the same mount list, 13920 * they end up competing with each other and often end up in 13921 * livelock. This approach ensures that forward progress is 13922 * made at the cost of occational ENOSPC errors being returned 13923 * that might otherwise have been avoided. 13924 */ 13925 error = 1; 13926 if ((resource == FLUSH_BLOCKS_WAIT && 13927 fs->fs_cstotal.cs_nbfree <= needed) || 13928 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13929 fs->fs_cstotal.cs_nifree <= needed)) { 13930 ACQUIRE_LOCK(ump); 13931 if ((ump->um_softdep->sd_flags & FLUSH_RC_ACTIVE) == 0) { 13932 ump->um_softdep->sd_flags |= FLUSH_RC_ACTIVE; 13933 FREE_LOCK(ump); 13934 failed_vnode = softdep_request_cleanup_flush(mp, ump); 13935 ACQUIRE_LOCK(ump); 13936 ump->um_softdep->sd_flags &= ~FLUSH_RC_ACTIVE; 13937 wakeup(&ump->um_softdep->sd_flags); 13938 FREE_LOCK(ump); 13939 if (ump->softdep_on_worklist > 0) { 13940 stat_cleanup_retries += 1; 13941 if (!failed_vnode) 13942 goto retry; 13943 } 13944 } else { 13945 while ((ump->um_softdep->sd_flags & 13946 FLUSH_RC_ACTIVE) != 0) { 13947 msleep(&ump->um_softdep->sd_flags, 13948 LOCK_PTR(ump), PVM, "ffsrca", hz); 13949 } 13950 FREE_LOCK(ump); 13951 error = 0; 13952 } 13953 stat_cleanup_failures += 1; 13954 } 13955 if (time_second - starttime > stat_cleanup_high_delay) 13956 stat_cleanup_high_delay = time_second - starttime; 13957 UFS_LOCK(ump); 13958 return (error); 13959 } 13960 13961 /* 13962 * Scan the vnodes for the specified mount point flushing out any 13963 * vnodes that can be locked without waiting. Finally, try to flush 13964 * the device associated with the mount point if it can be locked 13965 * without waiting. 13966 * 13967 * We return 0 if we were able to lock every vnode in our scan. 13968 * If we had to skip one or more vnodes, we return 1. 13969 */ 13970 static int 13971 softdep_request_cleanup_flush(mp, ump) 13972 struct mount *mp; 13973 struct ufsmount *ump; 13974 { 13975 struct thread *td; 13976 struct vnode *lvp, *mvp; 13977 int failed_vnode; 13978 13979 failed_vnode = 0; 13980 td = curthread; 13981 MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) { 13982 if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) { 13983 VI_UNLOCK(lvp); 13984 continue; 13985 } 13986 if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT) != 0) { 13987 failed_vnode = 1; 13988 continue; 13989 } 13990 if (lvp->v_vflag & VV_NOSYNC) { /* unlinked */ 13991 vput(lvp); 13992 continue; 13993 } 13994 (void) ffs_syncvnode(lvp, MNT_NOWAIT, 0); 13995 vput(lvp); 13996 } 13997 lvp = ump->um_devvp; 13998 if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) { 13999 VOP_FSYNC(lvp, MNT_NOWAIT, td); 14000 VOP_UNLOCK(lvp); 14001 } 14002 return (failed_vnode); 14003 } 14004 14005 static bool 14006 softdep_excess_items(struct ufsmount *ump, int item) 14007 { 14008 14009 KASSERT(item >= 0 && item < D_LAST, ("item %d", item)); 14010 return (dep_current[item] > max_softdeps && 14011 ump->softdep_curdeps[item] > max_softdeps / 14012 stat_flush_threads); 14013 } 14014 14015 static void 14016 schedule_cleanup(struct mount *mp) 14017 { 14018 struct ufsmount *ump; 14019 struct thread *td; 14020 14021 ump = VFSTOUFS(mp); 14022 LOCK_OWNED(ump); 14023 FREE_LOCK(ump); 14024 td = curthread; 14025 if ((td->td_pflags & TDP_KTHREAD) != 0 && 14026 (td->td_proc->p_flag2 & P2_AST_SU) == 0) { 14027 /* 14028 * No ast is delivered to kernel threads, so nobody 14029 * would deref the mp. Some kernel threads 14030 * explicitely check for AST, e.g. NFS daemon does 14031 * this in the serving loop. 14032 */ 14033 return; 14034 } 14035 if (td->td_su != NULL) 14036 vfs_rel(td->td_su); 14037 vfs_ref(mp); 14038 td->td_su = mp; 14039 thread_lock(td); 14040 td->td_flags |= TDF_ASTPENDING; 14041 thread_unlock(td); 14042 } 14043 14044 static void 14045 softdep_ast_cleanup_proc(struct thread *td) 14046 { 14047 struct mount *mp; 14048 struct ufsmount *ump; 14049 int error; 14050 bool req; 14051 14052 while ((mp = td->td_su) != NULL) { 14053 td->td_su = NULL; 14054 error = vfs_busy(mp, MBF_NOWAIT); 14055 vfs_rel(mp); 14056 if (error != 0) 14057 return; 14058 if (ffs_own_mount(mp) && MOUNTEDSOFTDEP(mp)) { 14059 ump = VFSTOUFS(mp); 14060 for (;;) { 14061 req = false; 14062 ACQUIRE_LOCK(ump); 14063 if (softdep_excess_items(ump, D_INODEDEP)) { 14064 req = true; 14065 request_cleanup(mp, FLUSH_INODES); 14066 } 14067 if (softdep_excess_items(ump, D_DIRREM)) { 14068 req = true; 14069 request_cleanup(mp, FLUSH_BLOCKS); 14070 } 14071 FREE_LOCK(ump); 14072 if (softdep_excess_items(ump, D_NEWBLK) || 14073 softdep_excess_items(ump, D_ALLOCDIRECT) || 14074 softdep_excess_items(ump, D_ALLOCINDIR)) { 14075 error = vn_start_write(NULL, &mp, 14076 V_WAIT); 14077 if (error == 0) { 14078 req = true; 14079 VFS_SYNC(mp, MNT_WAIT); 14080 vn_finished_write(mp); 14081 } 14082 } 14083 if ((td->td_pflags & TDP_KTHREAD) != 0 || !req) 14084 break; 14085 } 14086 } 14087 vfs_unbusy(mp); 14088 } 14089 if ((mp = td->td_su) != NULL) { 14090 td->td_su = NULL; 14091 vfs_rel(mp); 14092 } 14093 } 14094 14095 /* 14096 * If memory utilization has gotten too high, deliberately slow things 14097 * down and speed up the I/O processing. 14098 */ 14099 static int 14100 request_cleanup(mp, resource) 14101 struct mount *mp; 14102 int resource; 14103 { 14104 struct thread *td = curthread; 14105 struct ufsmount *ump; 14106 14107 ump = VFSTOUFS(mp); 14108 LOCK_OWNED(ump); 14109 /* 14110 * We never hold up the filesystem syncer or buf daemon. 14111 */ 14112 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF)) 14113 return (0); 14114 /* 14115 * First check to see if the work list has gotten backlogged. 14116 * If it has, co-opt this process to help clean up two entries. 14117 * Because this process may hold inodes locked, we cannot 14118 * handle any remove requests that might block on a locked 14119 * inode as that could lead to deadlock. We set TDP_SOFTDEP 14120 * to avoid recursively processing the worklist. 14121 */ 14122 if (ump->softdep_on_worklist > max_softdeps / 10) { 14123 td->td_pflags |= TDP_SOFTDEP; 14124 process_worklist_item(mp, 2, LK_NOWAIT); 14125 td->td_pflags &= ~TDP_SOFTDEP; 14126 stat_worklist_push += 2; 14127 return(1); 14128 } 14129 /* 14130 * Next, we attempt to speed up the syncer process. If that 14131 * is successful, then we allow the process to continue. 14132 */ 14133 if (softdep_speedup(ump) && 14134 resource != FLUSH_BLOCKS_WAIT && 14135 resource != FLUSH_INODES_WAIT) 14136 return(0); 14137 /* 14138 * If we are resource constrained on inode dependencies, try 14139 * flushing some dirty inodes. Otherwise, we are constrained 14140 * by file deletions, so try accelerating flushes of directories 14141 * with removal dependencies. We would like to do the cleanup 14142 * here, but we probably hold an inode locked at this point and 14143 * that might deadlock against one that we try to clean. So, 14144 * the best that we can do is request the syncer daemon to do 14145 * the cleanup for us. 14146 */ 14147 switch (resource) { 14148 case FLUSH_INODES: 14149 case FLUSH_INODES_WAIT: 14150 ACQUIRE_GBLLOCK(&lk); 14151 stat_ino_limit_push += 1; 14152 req_clear_inodedeps += 1; 14153 FREE_GBLLOCK(&lk); 14154 stat_countp = &stat_ino_limit_hit; 14155 break; 14156 14157 case FLUSH_BLOCKS: 14158 case FLUSH_BLOCKS_WAIT: 14159 ACQUIRE_GBLLOCK(&lk); 14160 stat_blk_limit_push += 1; 14161 req_clear_remove += 1; 14162 FREE_GBLLOCK(&lk); 14163 stat_countp = &stat_blk_limit_hit; 14164 break; 14165 14166 default: 14167 panic("request_cleanup: unknown type"); 14168 } 14169 /* 14170 * Hopefully the syncer daemon will catch up and awaken us. 14171 * We wait at most tickdelay before proceeding in any case. 14172 */ 14173 ACQUIRE_GBLLOCK(&lk); 14174 FREE_LOCK(ump); 14175 proc_waiting += 1; 14176 if (callout_pending(&softdep_callout) == FALSE) 14177 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2, 14178 pause_timer, 0); 14179 14180 if ((td->td_pflags & TDP_KTHREAD) == 0) 14181 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0); 14182 proc_waiting -= 1; 14183 FREE_GBLLOCK(&lk); 14184 ACQUIRE_LOCK(ump); 14185 return (1); 14186 } 14187 14188 /* 14189 * Awaken processes pausing in request_cleanup and clear proc_waiting 14190 * to indicate that there is no longer a timer running. Pause_timer 14191 * will be called with the global softdep mutex (&lk) locked. 14192 */ 14193 static void 14194 pause_timer(arg) 14195 void *arg; 14196 { 14197 14198 GBLLOCK_OWNED(&lk); 14199 /* 14200 * The callout_ API has acquired mtx and will hold it around this 14201 * function call. 14202 */ 14203 *stat_countp += proc_waiting; 14204 wakeup(&proc_waiting); 14205 } 14206 14207 /* 14208 * If requested, try removing inode or removal dependencies. 14209 */ 14210 static void 14211 check_clear_deps(mp) 14212 struct mount *mp; 14213 { 14214 struct ufsmount *ump; 14215 bool suj_susp; 14216 14217 /* 14218 * Tell the lower layers that any TRIM or WRITE transactions that have 14219 * been delayed for performance reasons should proceed to help alleviate 14220 * the shortage faster. The race between checking req_* and the softdep 14221 * mutex (lk) is fine since this is an advisory operation that at most 14222 * causes deferred work to be done sooner. 14223 */ 14224 ump = VFSTOUFS(mp); 14225 suj_susp = ump->um_softdep->sd_jblocks != NULL && 14226 ump->softdep_jblocks->jb_suspended; 14227 if (req_clear_remove || req_clear_inodedeps || suj_susp) { 14228 FREE_LOCK(ump); 14229 softdep_send_speedup(ump, 0, BIO_SPEEDUP_TRIM | BIO_SPEEDUP_WRITE); 14230 ACQUIRE_LOCK(ump); 14231 } 14232 14233 /* 14234 * If we are suspended, it may be because of our using 14235 * too many inodedeps, so help clear them out. 14236 */ 14237 if (suj_susp) 14238 clear_inodedeps(mp); 14239 14240 /* 14241 * General requests for cleanup of backed up dependencies 14242 */ 14243 ACQUIRE_GBLLOCK(&lk); 14244 if (req_clear_inodedeps) { 14245 req_clear_inodedeps -= 1; 14246 FREE_GBLLOCK(&lk); 14247 clear_inodedeps(mp); 14248 ACQUIRE_GBLLOCK(&lk); 14249 wakeup(&proc_waiting); 14250 } 14251 if (req_clear_remove) { 14252 req_clear_remove -= 1; 14253 FREE_GBLLOCK(&lk); 14254 clear_remove(mp); 14255 ACQUIRE_GBLLOCK(&lk); 14256 wakeup(&proc_waiting); 14257 } 14258 FREE_GBLLOCK(&lk); 14259 } 14260 14261 /* 14262 * Flush out a directory with at least one removal dependency in an effort to 14263 * reduce the number of dirrem, freefile, and freeblks dependency structures. 14264 */ 14265 static void 14266 clear_remove(mp) 14267 struct mount *mp; 14268 { 14269 struct pagedep_hashhead *pagedephd; 14270 struct pagedep *pagedep; 14271 struct ufsmount *ump; 14272 struct vnode *vp; 14273 struct bufobj *bo; 14274 int error, cnt; 14275 ino_t ino; 14276 14277 ump = VFSTOUFS(mp); 14278 LOCK_OWNED(ump); 14279 14280 for (cnt = 0; cnt <= ump->pagedep_hash_size; cnt++) { 14281 pagedephd = &ump->pagedep_hashtbl[ump->pagedep_nextclean++]; 14282 if (ump->pagedep_nextclean > ump->pagedep_hash_size) 14283 ump->pagedep_nextclean = 0; 14284 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 14285 if (LIST_EMPTY(&pagedep->pd_dirremhd)) 14286 continue; 14287 ino = pagedep->pd_ino; 14288 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 14289 continue; 14290 FREE_LOCK(ump); 14291 14292 /* 14293 * Let unmount clear deps 14294 */ 14295 error = vfs_busy(mp, MBF_NOWAIT); 14296 if (error != 0) 14297 goto finish_write; 14298 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 14299 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP); 14300 vfs_unbusy(mp); 14301 if (error != 0) { 14302 softdep_error("clear_remove: vget", error); 14303 goto finish_write; 14304 } 14305 MPASS(VTOI(vp)->i_mode != 0); 14306 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 14307 softdep_error("clear_remove: fsync", error); 14308 bo = &vp->v_bufobj; 14309 BO_LOCK(bo); 14310 drain_output(vp); 14311 BO_UNLOCK(bo); 14312 vput(vp); 14313 finish_write: 14314 vn_finished_write(mp); 14315 ACQUIRE_LOCK(ump); 14316 return; 14317 } 14318 } 14319 } 14320 14321 /* 14322 * Clear out a block of dirty inodes in an effort to reduce 14323 * the number of inodedep dependency structures. 14324 */ 14325 static void 14326 clear_inodedeps(mp) 14327 struct mount *mp; 14328 { 14329 struct inodedep_hashhead *inodedephd; 14330 struct inodedep *inodedep; 14331 struct ufsmount *ump; 14332 struct vnode *vp; 14333 struct fs *fs; 14334 int error, cnt; 14335 ino_t firstino, lastino, ino; 14336 14337 ump = VFSTOUFS(mp); 14338 fs = ump->um_fs; 14339 LOCK_OWNED(ump); 14340 /* 14341 * Pick a random inode dependency to be cleared. 14342 * We will then gather up all the inodes in its block 14343 * that have dependencies and flush them out. 14344 */ 14345 for (cnt = 0; cnt <= ump->inodedep_hash_size; cnt++) { 14346 inodedephd = &ump->inodedep_hashtbl[ump->inodedep_nextclean++]; 14347 if (ump->inodedep_nextclean > ump->inodedep_hash_size) 14348 ump->inodedep_nextclean = 0; 14349 if ((inodedep = LIST_FIRST(inodedephd)) != NULL) 14350 break; 14351 } 14352 if (inodedep == NULL) 14353 return; 14354 /* 14355 * Find the last inode in the block with dependencies. 14356 */ 14357 firstino = rounddown2(inodedep->id_ino, INOPB(fs)); 14358 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--) 14359 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0) 14360 break; 14361 /* 14362 * Asynchronously push all but the last inode with dependencies. 14363 * Synchronously push the last inode with dependencies to ensure 14364 * that the inode block gets written to free up the inodedeps. 14365 */ 14366 for (ino = firstino; ino <= lastino; ino++) { 14367 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 14368 continue; 14369 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 14370 continue; 14371 FREE_LOCK(ump); 14372 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */ 14373 if (error != 0) { 14374 vn_finished_write(mp); 14375 ACQUIRE_LOCK(ump); 14376 return; 14377 } 14378 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 14379 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP)) != 0) { 14380 softdep_error("clear_inodedeps: vget", error); 14381 vfs_unbusy(mp); 14382 vn_finished_write(mp); 14383 ACQUIRE_LOCK(ump); 14384 return; 14385 } 14386 vfs_unbusy(mp); 14387 if (VTOI(vp)->i_mode == 0) { 14388 vgone(vp); 14389 } else if (ino == lastino) { 14390 do { 14391 error = ffs_syncvnode(vp, MNT_WAIT, 0); 14392 } while (error == ERELOOKUP); 14393 if (error != 0) 14394 softdep_error("clear_inodedeps: fsync1", error); 14395 } else { 14396 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 14397 softdep_error("clear_inodedeps: fsync2", error); 14398 BO_LOCK(&vp->v_bufobj); 14399 drain_output(vp); 14400 BO_UNLOCK(&vp->v_bufobj); 14401 } 14402 vput(vp); 14403 vn_finished_write(mp); 14404 ACQUIRE_LOCK(ump); 14405 } 14406 } 14407 14408 void 14409 softdep_buf_append(bp, wkhd) 14410 struct buf *bp; 14411 struct workhead *wkhd; 14412 { 14413 struct worklist *wk; 14414 struct ufsmount *ump; 14415 14416 if ((wk = LIST_FIRST(wkhd)) == NULL) 14417 return; 14418 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 14419 ("softdep_buf_append called on non-softdep filesystem")); 14420 ump = VFSTOUFS(wk->wk_mp); 14421 ACQUIRE_LOCK(ump); 14422 while ((wk = LIST_FIRST(wkhd)) != NULL) { 14423 WORKLIST_REMOVE(wk); 14424 WORKLIST_INSERT(&bp->b_dep, wk); 14425 } 14426 FREE_LOCK(ump); 14427 14428 } 14429 14430 void 14431 softdep_inode_append(ip, cred, wkhd) 14432 struct inode *ip; 14433 struct ucred *cred; 14434 struct workhead *wkhd; 14435 { 14436 struct buf *bp; 14437 struct fs *fs; 14438 struct ufsmount *ump; 14439 int error; 14440 14441 ump = ITOUMP(ip); 14442 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 14443 ("softdep_inode_append called on non-softdep filesystem")); 14444 fs = ump->um_fs; 14445 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 14446 (int)fs->fs_bsize, cred, &bp); 14447 if (error) { 14448 bqrelse(bp); 14449 softdep_freework(wkhd); 14450 return; 14451 } 14452 softdep_buf_append(bp, wkhd); 14453 bqrelse(bp); 14454 } 14455 14456 void 14457 softdep_freework(wkhd) 14458 struct workhead *wkhd; 14459 { 14460 struct worklist *wk; 14461 struct ufsmount *ump; 14462 14463 if ((wk = LIST_FIRST(wkhd)) == NULL) 14464 return; 14465 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 14466 ("softdep_freework called on non-softdep filesystem")); 14467 ump = VFSTOUFS(wk->wk_mp); 14468 ACQUIRE_LOCK(ump); 14469 handle_jwork(wkhd); 14470 FREE_LOCK(ump); 14471 } 14472 14473 static struct ufsmount * 14474 softdep_bp_to_mp(bp) 14475 struct buf *bp; 14476 { 14477 struct mount *mp; 14478 struct vnode *vp; 14479 14480 if (LIST_EMPTY(&bp->b_dep)) 14481 return (NULL); 14482 vp = bp->b_vp; 14483 KASSERT(vp != NULL, 14484 ("%s, buffer with dependencies lacks vnode", __func__)); 14485 14486 /* 14487 * The ump mount point is stable after we get a correct 14488 * pointer, since bp is locked and this prevents unmount from 14489 * proceeding. But to get to it, we cannot dereference bp->b_dep 14490 * head wk_mp, because we do not yet own SU ump lock and 14491 * workitem might be freed while dereferenced. 14492 */ 14493 retry: 14494 switch (vp->v_type) { 14495 case VCHR: 14496 VI_LOCK(vp); 14497 mp = vp->v_type == VCHR ? vp->v_rdev->si_mountpt : NULL; 14498 VI_UNLOCK(vp); 14499 if (mp == NULL) 14500 goto retry; 14501 break; 14502 case VREG: 14503 case VDIR: 14504 case VLNK: 14505 case VFIFO: 14506 case VSOCK: 14507 mp = vp->v_mount; 14508 break; 14509 case VBLK: 14510 vn_printf(vp, "softdep_bp_to_mp: unexpected block device\n"); 14511 /* FALLTHROUGH */ 14512 case VNON: 14513 case VBAD: 14514 case VMARKER: 14515 mp = NULL; 14516 break; 14517 default: 14518 vn_printf(vp, "unknown vnode type"); 14519 mp = NULL; 14520 break; 14521 } 14522 return (VFSTOUFS(mp)); 14523 } 14524 14525 /* 14526 * Function to determine if the buffer has outstanding dependencies 14527 * that will cause a roll-back if the buffer is written. If wantcount 14528 * is set, return number of dependencies, otherwise just yes or no. 14529 */ 14530 static int 14531 softdep_count_dependencies(bp, wantcount) 14532 struct buf *bp; 14533 int wantcount; 14534 { 14535 struct worklist *wk; 14536 struct ufsmount *ump; 14537 struct bmsafemap *bmsafemap; 14538 struct freework *freework; 14539 struct inodedep *inodedep; 14540 struct indirdep *indirdep; 14541 struct freeblks *freeblks; 14542 struct allocindir *aip; 14543 struct pagedep *pagedep; 14544 struct dirrem *dirrem; 14545 struct newblk *newblk; 14546 struct mkdir *mkdir; 14547 struct diradd *dap; 14548 int i, retval; 14549 14550 ump = softdep_bp_to_mp(bp); 14551 if (ump == NULL) 14552 return (0); 14553 retval = 0; 14554 ACQUIRE_LOCK(ump); 14555 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 14556 switch (wk->wk_type) { 14557 case D_INODEDEP: 14558 inodedep = WK_INODEDEP(wk); 14559 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 14560 /* bitmap allocation dependency */ 14561 retval += 1; 14562 if (!wantcount) 14563 goto out; 14564 } 14565 if (TAILQ_FIRST(&inodedep->id_inoupdt)) { 14566 /* direct block pointer dependency */ 14567 retval += 1; 14568 if (!wantcount) 14569 goto out; 14570 } 14571 if (TAILQ_FIRST(&inodedep->id_extupdt)) { 14572 /* direct block pointer dependency */ 14573 retval += 1; 14574 if (!wantcount) 14575 goto out; 14576 } 14577 if (TAILQ_FIRST(&inodedep->id_inoreflst)) { 14578 /* Add reference dependency. */ 14579 retval += 1; 14580 if (!wantcount) 14581 goto out; 14582 } 14583 continue; 14584 14585 case D_INDIRDEP: 14586 indirdep = WK_INDIRDEP(wk); 14587 14588 TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) { 14589 /* indirect truncation dependency */ 14590 retval += 1; 14591 if (!wantcount) 14592 goto out; 14593 } 14594 14595 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 14596 /* indirect block pointer dependency */ 14597 retval += 1; 14598 if (!wantcount) 14599 goto out; 14600 } 14601 continue; 14602 14603 case D_PAGEDEP: 14604 pagedep = WK_PAGEDEP(wk); 14605 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) { 14606 if (LIST_FIRST(&dirrem->dm_jremrefhd)) { 14607 /* Journal remove ref dependency. */ 14608 retval += 1; 14609 if (!wantcount) 14610 goto out; 14611 } 14612 } 14613 for (i = 0; i < DAHASHSZ; i++) { 14614 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 14615 /* directory entry dependency */ 14616 retval += 1; 14617 if (!wantcount) 14618 goto out; 14619 } 14620 } 14621 continue; 14622 14623 case D_BMSAFEMAP: 14624 bmsafemap = WK_BMSAFEMAP(wk); 14625 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) { 14626 /* Add reference dependency. */ 14627 retval += 1; 14628 if (!wantcount) 14629 goto out; 14630 } 14631 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) { 14632 /* Allocate block dependency. */ 14633 retval += 1; 14634 if (!wantcount) 14635 goto out; 14636 } 14637 continue; 14638 14639 case D_FREEBLKS: 14640 freeblks = WK_FREEBLKS(wk); 14641 if (LIST_FIRST(&freeblks->fb_jblkdephd)) { 14642 /* Freeblk journal dependency. */ 14643 retval += 1; 14644 if (!wantcount) 14645 goto out; 14646 } 14647 continue; 14648 14649 case D_ALLOCDIRECT: 14650 case D_ALLOCINDIR: 14651 newblk = WK_NEWBLK(wk); 14652 if (newblk->nb_jnewblk) { 14653 /* Journal allocate dependency. */ 14654 retval += 1; 14655 if (!wantcount) 14656 goto out; 14657 } 14658 continue; 14659 14660 case D_MKDIR: 14661 mkdir = WK_MKDIR(wk); 14662 if (mkdir->md_jaddref) { 14663 /* Journal reference dependency. */ 14664 retval += 1; 14665 if (!wantcount) 14666 goto out; 14667 } 14668 continue; 14669 14670 case D_FREEWORK: 14671 case D_FREEDEP: 14672 case D_JSEGDEP: 14673 case D_JSEG: 14674 case D_SBDEP: 14675 /* never a dependency on these blocks */ 14676 continue; 14677 14678 default: 14679 panic("softdep_count_dependencies: Unexpected type %s", 14680 TYPENAME(wk->wk_type)); 14681 /* NOTREACHED */ 14682 } 14683 } 14684 out: 14685 FREE_LOCK(ump); 14686 return (retval); 14687 } 14688 14689 /* 14690 * Acquire exclusive access to a buffer. 14691 * Must be called with a locked mtx parameter. 14692 * Return acquired buffer or NULL on failure. 14693 */ 14694 static struct buf * 14695 getdirtybuf(bp, lock, waitfor) 14696 struct buf *bp; 14697 struct rwlock *lock; 14698 int waitfor; 14699 { 14700 int error; 14701 14702 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) { 14703 if (waitfor != MNT_WAIT) 14704 return (NULL); 14705 error = BUF_LOCK(bp, 14706 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock); 14707 /* 14708 * Even if we successfully acquire bp here, we have dropped 14709 * lock, which may violates our guarantee. 14710 */ 14711 if (error == 0) 14712 BUF_UNLOCK(bp); 14713 else if (error != ENOLCK) 14714 panic("getdirtybuf: inconsistent lock: %d", error); 14715 rw_wlock(lock); 14716 return (NULL); 14717 } 14718 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 14719 if (lock != BO_LOCKPTR(bp->b_bufobj) && waitfor == MNT_WAIT) { 14720 rw_wunlock(lock); 14721 BO_LOCK(bp->b_bufobj); 14722 BUF_UNLOCK(bp); 14723 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 14724 bp->b_vflags |= BV_BKGRDWAIT; 14725 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), 14726 PRIBIO | PDROP, "getbuf", 0); 14727 } else 14728 BO_UNLOCK(bp->b_bufobj); 14729 rw_wlock(lock); 14730 return (NULL); 14731 } 14732 BUF_UNLOCK(bp); 14733 if (waitfor != MNT_WAIT) 14734 return (NULL); 14735 #ifdef DEBUG_VFS_LOCKS 14736 if (bp->b_vp->v_type != VCHR) 14737 ASSERT_BO_WLOCKED(bp->b_bufobj); 14738 #endif 14739 bp->b_vflags |= BV_BKGRDWAIT; 14740 rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0); 14741 return (NULL); 14742 } 14743 if ((bp->b_flags & B_DELWRI) == 0) { 14744 BUF_UNLOCK(bp); 14745 return (NULL); 14746 } 14747 bremfree(bp); 14748 return (bp); 14749 } 14750 14751 /* 14752 * Check if it is safe to suspend the file system now. On entry, 14753 * the vnode interlock for devvp should be held. Return 0 with 14754 * the mount interlock held if the file system can be suspended now, 14755 * otherwise return EAGAIN with the mount interlock held. 14756 */ 14757 int 14758 softdep_check_suspend(struct mount *mp, 14759 struct vnode *devvp, 14760 int softdep_depcnt, 14761 int softdep_accdepcnt, 14762 int secondary_writes, 14763 int secondary_accwrites) 14764 { 14765 struct bufobj *bo; 14766 struct ufsmount *ump; 14767 struct inodedep *inodedep; 14768 int error, unlinked; 14769 14770 bo = &devvp->v_bufobj; 14771 ASSERT_BO_WLOCKED(bo); 14772 14773 /* 14774 * If we are not running with soft updates, then we need only 14775 * deal with secondary writes as we try to suspend. 14776 */ 14777 if (MOUNTEDSOFTDEP(mp) == 0) { 14778 MNT_ILOCK(mp); 14779 while (mp->mnt_secondary_writes != 0) { 14780 BO_UNLOCK(bo); 14781 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 14782 (PUSER - 1) | PDROP, "secwr", 0); 14783 BO_LOCK(bo); 14784 MNT_ILOCK(mp); 14785 } 14786 14787 /* 14788 * Reasons for needing more work before suspend: 14789 * - Dirty buffers on devvp. 14790 * - Secondary writes occurred after start of vnode sync loop 14791 */ 14792 error = 0; 14793 if (bo->bo_numoutput > 0 || 14794 bo->bo_dirty.bv_cnt > 0 || 14795 secondary_writes != 0 || 14796 mp->mnt_secondary_writes != 0 || 14797 secondary_accwrites != mp->mnt_secondary_accwrites) 14798 error = EAGAIN; 14799 BO_UNLOCK(bo); 14800 return (error); 14801 } 14802 14803 /* 14804 * If we are running with soft updates, then we need to coordinate 14805 * with them as we try to suspend. 14806 */ 14807 ump = VFSTOUFS(mp); 14808 for (;;) { 14809 if (!TRY_ACQUIRE_LOCK(ump)) { 14810 BO_UNLOCK(bo); 14811 ACQUIRE_LOCK(ump); 14812 FREE_LOCK(ump); 14813 BO_LOCK(bo); 14814 continue; 14815 } 14816 MNT_ILOCK(mp); 14817 if (mp->mnt_secondary_writes != 0) { 14818 FREE_LOCK(ump); 14819 BO_UNLOCK(bo); 14820 msleep(&mp->mnt_secondary_writes, 14821 MNT_MTX(mp), 14822 (PUSER - 1) | PDROP, "secwr", 0); 14823 BO_LOCK(bo); 14824 continue; 14825 } 14826 break; 14827 } 14828 14829 unlinked = 0; 14830 if (MOUNTEDSUJ(mp)) { 14831 for (inodedep = TAILQ_FIRST(&ump->softdep_unlinked); 14832 inodedep != NULL; 14833 inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 14834 if ((inodedep->id_state & (UNLINKED | UNLINKLINKS | 14835 UNLINKONLIST)) != (UNLINKED | UNLINKLINKS | 14836 UNLINKONLIST) || 14837 !check_inodedep_free(inodedep)) 14838 continue; 14839 unlinked++; 14840 } 14841 } 14842 14843 /* 14844 * Reasons for needing more work before suspend: 14845 * - Dirty buffers on devvp. 14846 * - Softdep activity occurred after start of vnode sync loop 14847 * - Secondary writes occurred after start of vnode sync loop 14848 */ 14849 error = 0; 14850 if (bo->bo_numoutput > 0 || 14851 bo->bo_dirty.bv_cnt > 0 || 14852 softdep_depcnt != unlinked || 14853 ump->softdep_deps != unlinked || 14854 softdep_accdepcnt != ump->softdep_accdeps || 14855 secondary_writes != 0 || 14856 mp->mnt_secondary_writes != 0 || 14857 secondary_accwrites != mp->mnt_secondary_accwrites) 14858 error = EAGAIN; 14859 FREE_LOCK(ump); 14860 BO_UNLOCK(bo); 14861 return (error); 14862 } 14863 14864 /* 14865 * Get the number of dependency structures for the file system, both 14866 * the current number and the total number allocated. These will 14867 * later be used to detect that softdep processing has occurred. 14868 */ 14869 void 14870 softdep_get_depcounts(struct mount *mp, 14871 int *softdep_depsp, 14872 int *softdep_accdepsp) 14873 { 14874 struct ufsmount *ump; 14875 14876 if (MOUNTEDSOFTDEP(mp) == 0) { 14877 *softdep_depsp = 0; 14878 *softdep_accdepsp = 0; 14879 return; 14880 } 14881 ump = VFSTOUFS(mp); 14882 ACQUIRE_LOCK(ump); 14883 *softdep_depsp = ump->softdep_deps; 14884 *softdep_accdepsp = ump->softdep_accdeps; 14885 FREE_LOCK(ump); 14886 } 14887 14888 /* 14889 * Wait for pending output on a vnode to complete. 14890 */ 14891 static void 14892 drain_output(vp) 14893 struct vnode *vp; 14894 { 14895 14896 ASSERT_VOP_LOCKED(vp, "drain_output"); 14897 (void)bufobj_wwait(&vp->v_bufobj, 0, 0); 14898 } 14899 14900 /* 14901 * Called whenever a buffer that is being invalidated or reallocated 14902 * contains dependencies. This should only happen if an I/O error has 14903 * occurred. The routine is called with the buffer locked. 14904 */ 14905 static void 14906 softdep_deallocate_dependencies(bp) 14907 struct buf *bp; 14908 { 14909 14910 if ((bp->b_ioflags & BIO_ERROR) == 0) 14911 panic("softdep_deallocate_dependencies: dangling deps"); 14912 if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL) 14913 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error); 14914 else 14915 printf("softdep_deallocate_dependencies: " 14916 "got error %d while accessing filesystem\n", bp->b_error); 14917 if (bp->b_error != ENXIO) 14918 panic("softdep_deallocate_dependencies: unrecovered I/O error"); 14919 } 14920 14921 /* 14922 * Function to handle asynchronous write errors in the filesystem. 14923 */ 14924 static void 14925 softdep_error(func, error) 14926 char *func; 14927 int error; 14928 { 14929 14930 /* XXX should do something better! */ 14931 printf("%s: got error %d while accessing filesystem\n", func, error); 14932 } 14933 14934 #ifdef DDB 14935 14936 /* exported to ffs_vfsops.c */ 14937 extern void db_print_ffs(struct ufsmount *ump); 14938 void 14939 db_print_ffs(struct ufsmount *ump) 14940 { 14941 db_printf("mp %p (%s) devvp %p\n", ump->um_mountp, 14942 ump->um_mountp->mnt_stat.f_mntonname, ump->um_devvp); 14943 db_printf(" fs %p su_wl %d su_deps %d su_req %d\n", 14944 ump->um_fs, ump->softdep_on_worklist, 14945 ump->softdep_deps, ump->softdep_req); 14946 } 14947 14948 static void 14949 worklist_print(struct worklist *wk, int verbose) 14950 { 14951 14952 if (!verbose) { 14953 db_printf("%s: %p state 0x%b\n", TYPENAME(wk->wk_type), wk, 14954 (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS); 14955 return; 14956 } 14957 db_printf("worklist: %p type %s state 0x%b next %p\n ", wk, 14958 TYPENAME(wk->wk_type), (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS, 14959 LIST_NEXT(wk, wk_list)); 14960 db_print_ffs(VFSTOUFS(wk->wk_mp)); 14961 } 14962 14963 static void 14964 inodedep_print(struct inodedep *inodedep, int verbose) 14965 { 14966 14967 worklist_print(&inodedep->id_list, 0); 14968 db_printf(" fs %p ino %jd inoblk %jd delta %jd nlink %jd\n", 14969 inodedep->id_fs, 14970 (intmax_t)inodedep->id_ino, 14971 (intmax_t)fsbtodb(inodedep->id_fs, 14972 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)), 14973 (intmax_t)inodedep->id_nlinkdelta, 14974 (intmax_t)inodedep->id_savednlink); 14975 14976 if (verbose == 0) 14977 return; 14978 14979 db_printf(" bmsafemap %p, mkdiradd %p, inoreflst %p\n", 14980 inodedep->id_bmsafemap, 14981 inodedep->id_mkdiradd, 14982 TAILQ_FIRST(&inodedep->id_inoreflst)); 14983 db_printf(" dirremhd %p, pendinghd %p, bufwait %p\n", 14984 LIST_FIRST(&inodedep->id_dirremhd), 14985 LIST_FIRST(&inodedep->id_pendinghd), 14986 LIST_FIRST(&inodedep->id_bufwait)); 14987 db_printf(" inowait %p, inoupdt %p, newinoupdt %p\n", 14988 LIST_FIRST(&inodedep->id_inowait), 14989 TAILQ_FIRST(&inodedep->id_inoupdt), 14990 TAILQ_FIRST(&inodedep->id_newinoupdt)); 14991 db_printf(" extupdt %p, newextupdt %p, freeblklst %p\n", 14992 TAILQ_FIRST(&inodedep->id_extupdt), 14993 TAILQ_FIRST(&inodedep->id_newextupdt), 14994 TAILQ_FIRST(&inodedep->id_freeblklst)); 14995 db_printf(" saveino %p, savedsize %jd, savedextsize %jd\n", 14996 inodedep->id_savedino1, 14997 (intmax_t)inodedep->id_savedsize, 14998 (intmax_t)inodedep->id_savedextsize); 14999 } 15000 15001 static void 15002 newblk_print(struct newblk *nbp) 15003 { 15004 15005 worklist_print(&nbp->nb_list, 0); 15006 db_printf(" newblkno %jd\n", (intmax_t)nbp->nb_newblkno); 15007 db_printf(" jnewblk %p, bmsafemap %p, freefrag %p\n", 15008 &nbp->nb_jnewblk, 15009 &nbp->nb_bmsafemap, 15010 &nbp->nb_freefrag); 15011 db_printf(" indirdeps %p, newdirblk %p, jwork %p\n", 15012 LIST_FIRST(&nbp->nb_indirdeps), 15013 LIST_FIRST(&nbp->nb_newdirblk), 15014 LIST_FIRST(&nbp->nb_jwork)); 15015 } 15016 15017 static void 15018 allocdirect_print(struct allocdirect *adp) 15019 { 15020 15021 newblk_print(&adp->ad_block); 15022 db_printf(" oldblkno %jd, oldsize %ld, newsize %ld\n", 15023 adp->ad_oldblkno, adp->ad_oldsize, adp->ad_newsize); 15024 db_printf(" offset %d, inodedep %p\n", 15025 adp->ad_offset, adp->ad_inodedep); 15026 } 15027 15028 static void 15029 allocindir_print(struct allocindir *aip) 15030 { 15031 15032 newblk_print(&aip->ai_block); 15033 db_printf(" oldblkno %jd, lbn %jd\n", 15034 (intmax_t)aip->ai_oldblkno, (intmax_t)aip->ai_lbn); 15035 db_printf(" offset %d, indirdep %p\n", 15036 aip->ai_offset, aip->ai_indirdep); 15037 } 15038 15039 static void 15040 mkdir_print(struct mkdir *mkdir) 15041 { 15042 15043 worklist_print(&mkdir->md_list, 0); 15044 db_printf(" diradd %p, jaddref %p, buf %p\n", 15045 mkdir->md_diradd, mkdir->md_jaddref, mkdir->md_buf); 15046 } 15047 15048 DB_SHOW_COMMAND(sd_inodedep, db_show_sd_inodedep) 15049 { 15050 15051 if (have_addr == 0) { 15052 db_printf("inodedep address required\n"); 15053 return; 15054 } 15055 inodedep_print((struct inodedep*)addr, 1); 15056 } 15057 15058 DB_SHOW_COMMAND(sd_allinodedeps, db_show_sd_allinodedeps) 15059 { 15060 struct inodedep_hashhead *inodedephd; 15061 struct inodedep *inodedep; 15062 struct ufsmount *ump; 15063 int cnt; 15064 15065 if (have_addr == 0) { 15066 db_printf("ufsmount address required\n"); 15067 return; 15068 } 15069 ump = (struct ufsmount *)addr; 15070 for (cnt = 0; cnt < ump->inodedep_hash_size; cnt++) { 15071 inodedephd = &ump->inodedep_hashtbl[cnt]; 15072 LIST_FOREACH(inodedep, inodedephd, id_hash) { 15073 inodedep_print(inodedep, 0); 15074 } 15075 } 15076 } 15077 15078 DB_SHOW_COMMAND(sd_worklist, db_show_sd_worklist) 15079 { 15080 15081 if (have_addr == 0) { 15082 db_printf("worklist address required\n"); 15083 return; 15084 } 15085 worklist_print((struct worklist *)addr, 1); 15086 } 15087 15088 DB_SHOW_COMMAND(sd_workhead, db_show_sd_workhead) 15089 { 15090 struct worklist *wk; 15091 struct workhead *wkhd; 15092 15093 if (have_addr == 0) { 15094 db_printf("worklist address required " 15095 "(for example value in bp->b_dep)\n"); 15096 return; 15097 } 15098 /* 15099 * We often do not have the address of the worklist head but 15100 * instead a pointer to its first entry (e.g., we have the 15101 * contents of bp->b_dep rather than &bp->b_dep). But the back 15102 * pointer of bp->b_dep will point at the head of the list, so 15103 * we cheat and use that instead. If we are in the middle of 15104 * a list we will still get the same result, so nothing 15105 * unexpected will result. 15106 */ 15107 wk = (struct worklist *)addr; 15108 if (wk == NULL) 15109 return; 15110 wkhd = (struct workhead *)wk->wk_list.le_prev; 15111 LIST_FOREACH(wk, wkhd, wk_list) { 15112 switch(wk->wk_type) { 15113 case D_INODEDEP: 15114 inodedep_print(WK_INODEDEP(wk), 0); 15115 continue; 15116 case D_ALLOCDIRECT: 15117 allocdirect_print(WK_ALLOCDIRECT(wk)); 15118 continue; 15119 case D_ALLOCINDIR: 15120 allocindir_print(WK_ALLOCINDIR(wk)); 15121 continue; 15122 case D_MKDIR: 15123 mkdir_print(WK_MKDIR(wk)); 15124 continue; 15125 default: 15126 worklist_print(wk, 0); 15127 continue; 15128 } 15129 } 15130 } 15131 15132 DB_SHOW_COMMAND(sd_mkdir, db_show_sd_mkdir) 15133 { 15134 if (have_addr == 0) { 15135 db_printf("mkdir address required\n"); 15136 return; 15137 } 15138 mkdir_print((struct mkdir *)addr); 15139 } 15140 15141 DB_SHOW_COMMAND(sd_mkdir_list, db_show_sd_mkdir_list) 15142 { 15143 struct mkdirlist *mkdirlisthd; 15144 struct mkdir *mkdir; 15145 15146 if (have_addr == 0) { 15147 db_printf("mkdir listhead address required\n"); 15148 return; 15149 } 15150 mkdirlisthd = (struct mkdirlist *)addr; 15151 LIST_FOREACH(mkdir, mkdirlisthd, md_mkdirs) { 15152 mkdir_print(mkdir); 15153 if (mkdir->md_diradd != NULL) { 15154 db_printf(" "); 15155 worklist_print(&mkdir->md_diradd->da_list, 0); 15156 } 15157 if (mkdir->md_jaddref != NULL) { 15158 db_printf(" "); 15159 worklist_print(&mkdir->md_jaddref->ja_list, 0); 15160 } 15161 } 15162 } 15163 15164 DB_SHOW_COMMAND(sd_allocdirect, db_show_sd_allocdirect) 15165 { 15166 if (have_addr == 0) { 15167 db_printf("allocdirect address required\n"); 15168 return; 15169 } 15170 allocdirect_print((struct allocdirect *)addr); 15171 } 15172 15173 DB_SHOW_COMMAND(sd_allocindir, db_show_sd_allocindir) 15174 { 15175 if (have_addr == 0) { 15176 db_printf("allocindir address required\n"); 15177 return; 15178 } 15179 allocindir_print((struct allocindir *)addr); 15180 } 15181 15182 #endif /* DDB */ 15183 15184 #endif /* SOFTUPDATES */ 15185