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, cnp) 625 struct vnode *dvp; 626 struct vnode *vp; 627 struct componentname *cnp; 628 { 629 630 panic("softdep_prelink called"); 631 } 632 633 #else 634 635 FEATURE(softupdates, "FFS soft-updates support"); 636 637 static SYSCTL_NODE(_debug, OID_AUTO, softdep, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 638 "soft updates stats"); 639 static SYSCTL_NODE(_debug_softdep, OID_AUTO, total, 640 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 641 "total dependencies allocated"); 642 static SYSCTL_NODE(_debug_softdep, OID_AUTO, highuse, 643 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 644 "high use dependencies allocated"); 645 static SYSCTL_NODE(_debug_softdep, OID_AUTO, current, 646 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 647 "current dependencies allocated"); 648 static SYSCTL_NODE(_debug_softdep, OID_AUTO, write, 649 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 650 "current dependencies written"); 651 652 unsigned long dep_current[D_LAST + 1]; 653 unsigned long dep_highuse[D_LAST + 1]; 654 unsigned long dep_total[D_LAST + 1]; 655 unsigned long dep_write[D_LAST + 1]; 656 657 #define SOFTDEP_TYPE(type, str, long) \ 658 static MALLOC_DEFINE(M_ ## type, #str, long); \ 659 SYSCTL_ULONG(_debug_softdep_total, OID_AUTO, str, CTLFLAG_RD, \ 660 &dep_total[D_ ## type], 0, ""); \ 661 SYSCTL_ULONG(_debug_softdep_current, OID_AUTO, str, CTLFLAG_RD, \ 662 &dep_current[D_ ## type], 0, ""); \ 663 SYSCTL_ULONG(_debug_softdep_highuse, OID_AUTO, str, CTLFLAG_RD, \ 664 &dep_highuse[D_ ## type], 0, ""); \ 665 SYSCTL_ULONG(_debug_softdep_write, OID_AUTO, str, CTLFLAG_RD, \ 666 &dep_write[D_ ## type], 0, ""); 667 668 SOFTDEP_TYPE(PAGEDEP, pagedep, "File page dependencies"); 669 SOFTDEP_TYPE(INODEDEP, inodedep, "Inode dependencies"); 670 SOFTDEP_TYPE(BMSAFEMAP, bmsafemap, 671 "Block or frag allocated from cyl group map"); 672 SOFTDEP_TYPE(NEWBLK, newblk, "New block or frag allocation dependency"); 673 SOFTDEP_TYPE(ALLOCDIRECT, allocdirect, "Block or frag dependency for an inode"); 674 SOFTDEP_TYPE(INDIRDEP, indirdep, "Indirect block dependencies"); 675 SOFTDEP_TYPE(ALLOCINDIR, allocindir, "Block dependency for an indirect block"); 676 SOFTDEP_TYPE(FREEFRAG, freefrag, "Previously used frag for an inode"); 677 SOFTDEP_TYPE(FREEBLKS, freeblks, "Blocks freed from an inode"); 678 SOFTDEP_TYPE(FREEFILE, freefile, "Inode deallocated"); 679 SOFTDEP_TYPE(DIRADD, diradd, "New directory entry"); 680 SOFTDEP_TYPE(MKDIR, mkdir, "New directory"); 681 SOFTDEP_TYPE(DIRREM, dirrem, "Directory entry deleted"); 682 SOFTDEP_TYPE(NEWDIRBLK, newdirblk, "Unclaimed new directory block"); 683 SOFTDEP_TYPE(FREEWORK, freework, "free an inode block"); 684 SOFTDEP_TYPE(FREEDEP, freedep, "track a block free"); 685 SOFTDEP_TYPE(JADDREF, jaddref, "Journal inode ref add"); 686 SOFTDEP_TYPE(JREMREF, jremref, "Journal inode ref remove"); 687 SOFTDEP_TYPE(JMVREF, jmvref, "Journal inode ref move"); 688 SOFTDEP_TYPE(JNEWBLK, jnewblk, "Journal new block"); 689 SOFTDEP_TYPE(JFREEBLK, jfreeblk, "Journal free block"); 690 SOFTDEP_TYPE(JFREEFRAG, jfreefrag, "Journal free frag"); 691 SOFTDEP_TYPE(JSEG, jseg, "Journal segment"); 692 SOFTDEP_TYPE(JSEGDEP, jsegdep, "Journal segment complete"); 693 SOFTDEP_TYPE(SBDEP, sbdep, "Superblock write dependency"); 694 SOFTDEP_TYPE(JTRUNC, jtrunc, "Journal inode truncation"); 695 SOFTDEP_TYPE(JFSYNC, jfsync, "Journal fsync complete"); 696 697 static MALLOC_DEFINE(M_SENTINEL, "sentinel", "Worklist sentinel"); 698 699 static MALLOC_DEFINE(M_SAVEDINO, "savedino", "Saved inodes"); 700 static MALLOC_DEFINE(M_JBLOCKS, "jblocks", "Journal block locations"); 701 static MALLOC_DEFINE(M_MOUNTDATA, "softdep", "Softdep per-mount data"); 702 703 #define M_SOFTDEP_FLAGS (M_WAITOK) 704 705 /* 706 * translate from workitem type to memory type 707 * MUST match the defines above, such that memtype[D_XXX] == M_XXX 708 */ 709 static struct malloc_type *memtype[] = { 710 NULL, 711 M_PAGEDEP, 712 M_INODEDEP, 713 M_BMSAFEMAP, 714 M_NEWBLK, 715 M_ALLOCDIRECT, 716 M_INDIRDEP, 717 M_ALLOCINDIR, 718 M_FREEFRAG, 719 M_FREEBLKS, 720 M_FREEFILE, 721 M_DIRADD, 722 M_MKDIR, 723 M_DIRREM, 724 M_NEWDIRBLK, 725 M_FREEWORK, 726 M_FREEDEP, 727 M_JADDREF, 728 M_JREMREF, 729 M_JMVREF, 730 M_JNEWBLK, 731 M_JFREEBLK, 732 M_JFREEFRAG, 733 M_JSEG, 734 M_JSEGDEP, 735 M_SBDEP, 736 M_JTRUNC, 737 M_JFSYNC, 738 M_SENTINEL 739 }; 740 741 #define DtoM(type) (memtype[type]) 742 743 /* 744 * Names of malloc types. 745 */ 746 #define TYPENAME(type) \ 747 ((unsigned)(type) <= D_LAST && (unsigned)(type) >= D_FIRST ? \ 748 memtype[type]->ks_shortdesc : "???") 749 /* 750 * End system adaptation definitions. 751 */ 752 753 #define DOTDOT_OFFSET offsetof(struct dirtemplate, dotdot_ino) 754 #define DOT_OFFSET offsetof(struct dirtemplate, dot_ino) 755 756 /* 757 * Internal function prototypes. 758 */ 759 static void check_clear_deps(struct mount *); 760 static void softdep_error(char *, int); 761 static int softdep_prerename_vnode(struct ufsmount *, struct vnode *); 762 static int softdep_process_worklist(struct mount *, int); 763 static int softdep_waitidle(struct mount *, int); 764 static void drain_output(struct vnode *); 765 static struct buf *getdirtybuf(struct buf *, struct rwlock *, int); 766 static int check_inodedep_free(struct inodedep *); 767 static void clear_remove(struct mount *); 768 static void clear_inodedeps(struct mount *); 769 static void unlinked_inodedep(struct mount *, struct inodedep *); 770 static void clear_unlinked_inodedep(struct inodedep *); 771 static struct inodedep *first_unlinked_inodedep(struct ufsmount *); 772 static int flush_pagedep_deps(struct vnode *, struct mount *, 773 struct diraddhd *, struct buf *); 774 static int free_pagedep(struct pagedep *); 775 static int flush_newblk_dep(struct vnode *, struct mount *, ufs_lbn_t); 776 static int flush_inodedep_deps(struct vnode *, struct mount *, ino_t); 777 static int flush_deplist(struct allocdirectlst *, int, int *); 778 static int sync_cgs(struct mount *, int); 779 static int handle_written_filepage(struct pagedep *, struct buf *, int); 780 static int handle_written_sbdep(struct sbdep *, struct buf *); 781 static void initiate_write_sbdep(struct sbdep *); 782 static void diradd_inode_written(struct diradd *, struct inodedep *); 783 static int handle_written_indirdep(struct indirdep *, struct buf *, 784 struct buf**, int); 785 static int handle_written_inodeblock(struct inodedep *, struct buf *, int); 786 static int jnewblk_rollforward(struct jnewblk *, struct fs *, struct cg *, 787 uint8_t *); 788 static int handle_written_bmsafemap(struct bmsafemap *, struct buf *, int); 789 static void handle_written_jaddref(struct jaddref *); 790 static void handle_written_jremref(struct jremref *); 791 static void handle_written_jseg(struct jseg *, struct buf *); 792 static void handle_written_jnewblk(struct jnewblk *); 793 static void handle_written_jblkdep(struct jblkdep *); 794 static void handle_written_jfreefrag(struct jfreefrag *); 795 static void complete_jseg(struct jseg *); 796 static void complete_jsegs(struct jseg *); 797 static void jseg_write(struct ufsmount *ump, struct jseg *, uint8_t *); 798 static void jaddref_write(struct jaddref *, struct jseg *, uint8_t *); 799 static void jremref_write(struct jremref *, struct jseg *, uint8_t *); 800 static void jmvref_write(struct jmvref *, struct jseg *, uint8_t *); 801 static void jtrunc_write(struct jtrunc *, struct jseg *, uint8_t *); 802 static void jfsync_write(struct jfsync *, struct jseg *, uint8_t *data); 803 static void jnewblk_write(struct jnewblk *, struct jseg *, uint8_t *); 804 static void jfreeblk_write(struct jfreeblk *, struct jseg *, uint8_t *); 805 static void jfreefrag_write(struct jfreefrag *, struct jseg *, uint8_t *); 806 static inline void inoref_write(struct inoref *, struct jseg *, 807 struct jrefrec *); 808 static void handle_allocdirect_partdone(struct allocdirect *, 809 struct workhead *); 810 static struct jnewblk *cancel_newblk(struct newblk *, struct worklist *, 811 struct workhead *); 812 static void indirdep_complete(struct indirdep *); 813 static int indirblk_lookup(struct mount *, ufs2_daddr_t); 814 static void indirblk_insert(struct freework *); 815 static void indirblk_remove(struct freework *); 816 static void handle_allocindir_partdone(struct allocindir *); 817 static void initiate_write_filepage(struct pagedep *, struct buf *); 818 static void initiate_write_indirdep(struct indirdep*, struct buf *); 819 static void handle_written_mkdir(struct mkdir *, int); 820 static int jnewblk_rollback(struct jnewblk *, struct fs *, struct cg *, 821 uint8_t *); 822 static void initiate_write_bmsafemap(struct bmsafemap *, struct buf *); 823 static void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *); 824 static void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *); 825 static void handle_workitem_freefile(struct freefile *); 826 static int handle_workitem_remove(struct dirrem *, int); 827 static struct dirrem *newdirrem(struct buf *, struct inode *, 828 struct inode *, int, struct dirrem **); 829 static struct indirdep *indirdep_lookup(struct mount *, struct inode *, 830 struct buf *); 831 static void cancel_indirdep(struct indirdep *, struct buf *, 832 struct freeblks *); 833 static void free_indirdep(struct indirdep *); 834 static void free_diradd(struct diradd *, struct workhead *); 835 static void merge_diradd(struct inodedep *, struct diradd *); 836 static void complete_diradd(struct diradd *); 837 static struct diradd *diradd_lookup(struct pagedep *, int); 838 static struct jremref *cancel_diradd_dotdot(struct inode *, struct dirrem *, 839 struct jremref *); 840 static struct jremref *cancel_mkdir_dotdot(struct inode *, struct dirrem *, 841 struct jremref *); 842 static void cancel_diradd(struct diradd *, struct dirrem *, struct jremref *, 843 struct jremref *, struct jremref *); 844 static void dirrem_journal(struct dirrem *, struct jremref *, struct jremref *, 845 struct jremref *); 846 static void cancel_allocindir(struct allocindir *, struct buf *bp, 847 struct freeblks *, int); 848 static int setup_trunc_indir(struct freeblks *, struct inode *, 849 ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t); 850 static void complete_trunc_indir(struct freework *); 851 static void trunc_indirdep(struct indirdep *, struct freeblks *, struct buf *, 852 int); 853 static void complete_mkdir(struct mkdir *); 854 static void free_newdirblk(struct newdirblk *); 855 static void free_jremref(struct jremref *); 856 static void free_jaddref(struct jaddref *); 857 static void free_jsegdep(struct jsegdep *); 858 static void free_jsegs(struct jblocks *); 859 static void rele_jseg(struct jseg *); 860 static void free_jseg(struct jseg *, struct jblocks *); 861 static void free_jnewblk(struct jnewblk *); 862 static void free_jblkdep(struct jblkdep *); 863 static void free_jfreefrag(struct jfreefrag *); 864 static void free_freedep(struct freedep *); 865 static void journal_jremref(struct dirrem *, struct jremref *, 866 struct inodedep *); 867 static void cancel_jnewblk(struct jnewblk *, struct workhead *); 868 static int cancel_jaddref(struct jaddref *, struct inodedep *, 869 struct workhead *); 870 static void cancel_jfreefrag(struct jfreefrag *); 871 static inline void setup_freedirect(struct freeblks *, struct inode *, 872 int, int); 873 static inline void setup_freeext(struct freeblks *, struct inode *, int, int); 874 static inline void setup_freeindir(struct freeblks *, struct inode *, int, 875 ufs_lbn_t, int); 876 static inline struct freeblks *newfreeblks(struct mount *, struct inode *); 877 static void freeblks_free(struct ufsmount *, struct freeblks *, int); 878 static void indir_trunc(struct freework *, ufs2_daddr_t, ufs_lbn_t); 879 static ufs2_daddr_t blkcount(struct fs *, ufs2_daddr_t, off_t); 880 static int trunc_check_buf(struct buf *, int *, ufs_lbn_t, int, int); 881 static void trunc_dependencies(struct inode *, struct freeblks *, ufs_lbn_t, 882 int, int); 883 static void trunc_pages(struct inode *, off_t, ufs2_daddr_t, int); 884 static int cancel_pagedep(struct pagedep *, struct freeblks *, int); 885 static int deallocate_dependencies(struct buf *, struct freeblks *, int); 886 static void newblk_freefrag(struct newblk*); 887 static void free_newblk(struct newblk *); 888 static void cancel_allocdirect(struct allocdirectlst *, 889 struct allocdirect *, struct freeblks *); 890 static int check_inode_unwritten(struct inodedep *); 891 static int free_inodedep(struct inodedep *); 892 static void freework_freeblock(struct freework *, u_long); 893 static void freework_enqueue(struct freework *); 894 static int handle_workitem_freeblocks(struct freeblks *, int); 895 static int handle_complete_freeblocks(struct freeblks *, int); 896 static void handle_workitem_indirblk(struct freework *); 897 static void handle_written_freework(struct freework *); 898 static void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *); 899 static struct worklist *jnewblk_merge(struct worklist *, struct worklist *, 900 struct workhead *); 901 static struct freefrag *setup_allocindir_phase2(struct buf *, struct inode *, 902 struct inodedep *, struct allocindir *, ufs_lbn_t); 903 static struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t, 904 ufs2_daddr_t, ufs_lbn_t); 905 static void handle_workitem_freefrag(struct freefrag *); 906 static struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long, 907 ufs_lbn_t, u_long); 908 static void allocdirect_merge(struct allocdirectlst *, 909 struct allocdirect *, struct allocdirect *); 910 static struct freefrag *allocindir_merge(struct allocindir *, 911 struct allocindir *); 912 static int bmsafemap_find(struct bmsafemap_hashhead *, int, 913 struct bmsafemap **); 914 static struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *, 915 int cg, struct bmsafemap *); 916 static int newblk_find(struct newblk_hashhead *, ufs2_daddr_t, int, 917 struct newblk **); 918 static int newblk_lookup(struct mount *, ufs2_daddr_t, int, struct newblk **); 919 static int inodedep_find(struct inodedep_hashhead *, ino_t, 920 struct inodedep **); 921 static int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **); 922 static int pagedep_lookup(struct mount *, struct buf *bp, ino_t, ufs_lbn_t, 923 int, struct pagedep **); 924 static int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t, 925 struct pagedep **); 926 static void pause_timer(void *); 927 static int request_cleanup(struct mount *, int); 928 static int softdep_request_cleanup_flush(struct mount *, struct ufsmount *); 929 static void schedule_cleanup(struct mount *); 930 static void softdep_ast_cleanup_proc(struct thread *); 931 static struct ufsmount *softdep_bp_to_mp(struct buf *bp); 932 static int process_worklist_item(struct mount *, int, int); 933 static void process_removes(struct vnode *); 934 static void process_truncates(struct vnode *); 935 static void jwork_move(struct workhead *, struct workhead *); 936 static void jwork_insert(struct workhead *, struct jsegdep *); 937 static void add_to_worklist(struct worklist *, int); 938 static void wake_worklist(struct worklist *); 939 static void wait_worklist(struct worklist *, char *); 940 static void remove_from_worklist(struct worklist *); 941 static void softdep_flush(void *); 942 static void softdep_flushjournal(struct mount *); 943 static int softdep_speedup(struct ufsmount *); 944 static void worklist_speedup(struct mount *); 945 static int journal_mount(struct mount *, struct fs *, struct ucred *); 946 static void journal_unmount(struct ufsmount *); 947 static int journal_space(struct ufsmount *, int); 948 static void journal_suspend(struct ufsmount *); 949 static int journal_unsuspend(struct ufsmount *ump); 950 static void add_to_journal(struct worklist *); 951 static void remove_from_journal(struct worklist *); 952 static bool softdep_excess_items(struct ufsmount *, int); 953 static void softdep_process_journal(struct mount *, struct worklist *, int); 954 static struct jremref *newjremref(struct dirrem *, struct inode *, 955 struct inode *ip, off_t, nlink_t); 956 static struct jaddref *newjaddref(struct inode *, ino_t, off_t, int16_t, 957 uint16_t); 958 static inline void newinoref(struct inoref *, ino_t, ino_t, off_t, nlink_t, 959 uint16_t); 960 static inline struct jsegdep *inoref_jseg(struct inoref *); 961 static struct jmvref *newjmvref(struct inode *, ino_t, off_t, off_t); 962 static struct jfreeblk *newjfreeblk(struct freeblks *, ufs_lbn_t, 963 ufs2_daddr_t, int); 964 static void adjust_newfreework(struct freeblks *, int); 965 static struct jtrunc *newjtrunc(struct freeblks *, off_t, int); 966 static void move_newblock_dep(struct jaddref *, struct inodedep *); 967 static void cancel_jfreeblk(struct freeblks *, ufs2_daddr_t); 968 static struct jfreefrag *newjfreefrag(struct freefrag *, struct inode *, 969 ufs2_daddr_t, long, ufs_lbn_t); 970 static struct freework *newfreework(struct ufsmount *, struct freeblks *, 971 struct freework *, ufs_lbn_t, ufs2_daddr_t, int, int, int); 972 static int jwait(struct worklist *, int); 973 static struct inodedep *inodedep_lookup_ip(struct inode *); 974 static int bmsafemap_backgroundwrite(struct bmsafemap *, struct buf *); 975 static struct freefile *handle_bufwait(struct inodedep *, struct workhead *); 976 static void handle_jwork(struct workhead *); 977 static struct mkdir *setup_newdir(struct diradd *, ino_t, ino_t, struct buf *, 978 struct mkdir **); 979 static struct jblocks *jblocks_create(void); 980 static ufs2_daddr_t jblocks_alloc(struct jblocks *, int, int *); 981 static void jblocks_free(struct jblocks *, struct mount *, int); 982 static void jblocks_destroy(struct jblocks *); 983 static void jblocks_add(struct jblocks *, ufs2_daddr_t, int); 984 985 /* 986 * Exported softdep operations. 987 */ 988 static void softdep_disk_io_initiation(struct buf *); 989 static void softdep_disk_write_complete(struct buf *); 990 static void softdep_deallocate_dependencies(struct buf *); 991 static int softdep_count_dependencies(struct buf *bp, int); 992 993 /* 994 * Global lock over all of soft updates. 995 */ 996 static struct mtx lk; 997 MTX_SYSINIT(softdep_lock, &lk, "global softdep", MTX_DEF); 998 999 #define ACQUIRE_GBLLOCK(lk) mtx_lock(lk) 1000 #define FREE_GBLLOCK(lk) mtx_unlock(lk) 1001 #define GBLLOCK_OWNED(lk) mtx_assert((lk), MA_OWNED) 1002 1003 /* 1004 * Per-filesystem soft-updates locking. 1005 */ 1006 #define LOCK_PTR(ump) (&(ump)->um_softdep->sd_fslock) 1007 #define TRY_ACQUIRE_LOCK(ump) rw_try_wlock(&(ump)->um_softdep->sd_fslock) 1008 #define ACQUIRE_LOCK(ump) rw_wlock(&(ump)->um_softdep->sd_fslock) 1009 #define FREE_LOCK(ump) rw_wunlock(&(ump)->um_softdep->sd_fslock) 1010 #define LOCK_OWNED(ump) rw_assert(&(ump)->um_softdep->sd_fslock, \ 1011 RA_WLOCKED) 1012 1013 #define BUF_AREC(bp) lockallowrecurse(&(bp)->b_lock) 1014 #define BUF_NOREC(bp) lockdisablerecurse(&(bp)->b_lock) 1015 1016 /* 1017 * Worklist queue management. 1018 * These routines require that the lock be held. 1019 */ 1020 #ifndef /* NOT */ INVARIANTS 1021 #define WORKLIST_INSERT(head, item) do { \ 1022 (item)->wk_state |= ONWORKLIST; \ 1023 LIST_INSERT_HEAD(head, item, wk_list); \ 1024 } while (0) 1025 #define WORKLIST_REMOVE(item) do { \ 1026 (item)->wk_state &= ~ONWORKLIST; \ 1027 LIST_REMOVE(item, wk_list); \ 1028 } while (0) 1029 #define WORKLIST_INSERT_UNLOCKED WORKLIST_INSERT 1030 #define WORKLIST_REMOVE_UNLOCKED WORKLIST_REMOVE 1031 1032 #else /* INVARIANTS */ 1033 static void worklist_insert(struct workhead *, struct worklist *, int, 1034 const char *, int); 1035 static void worklist_remove(struct worklist *, int, const char *, int); 1036 1037 #define WORKLIST_INSERT(head, item) \ 1038 worklist_insert(head, item, 1, __func__, __LINE__) 1039 #define WORKLIST_INSERT_UNLOCKED(head, item)\ 1040 worklist_insert(head, item, 0, __func__, __LINE__) 1041 #define WORKLIST_REMOVE(item)\ 1042 worklist_remove(item, 1, __func__, __LINE__) 1043 #define WORKLIST_REMOVE_UNLOCKED(item)\ 1044 worklist_remove(item, 0, __func__, __LINE__) 1045 1046 static void 1047 worklist_insert(head, item, locked, func, line) 1048 struct workhead *head; 1049 struct worklist *item; 1050 int locked; 1051 const char *func; 1052 int line; 1053 { 1054 1055 if (locked) 1056 LOCK_OWNED(VFSTOUFS(item->wk_mp)); 1057 if (item->wk_state & ONWORKLIST) 1058 panic("worklist_insert: %p %s(0x%X) already on list, " 1059 "added in function %s at line %d", 1060 item, TYPENAME(item->wk_type), item->wk_state, 1061 item->wk_func, item->wk_line); 1062 item->wk_state |= ONWORKLIST; 1063 item->wk_func = func; 1064 item->wk_line = line; 1065 LIST_INSERT_HEAD(head, item, wk_list); 1066 } 1067 1068 static void 1069 worklist_remove(item, locked, func, line) 1070 struct worklist *item; 1071 int locked; 1072 const char *func; 1073 int line; 1074 { 1075 1076 if (locked) 1077 LOCK_OWNED(VFSTOUFS(item->wk_mp)); 1078 if ((item->wk_state & ONWORKLIST) == 0) 1079 panic("worklist_remove: %p %s(0x%X) not on list, " 1080 "removed in function %s at line %d", 1081 item, TYPENAME(item->wk_type), item->wk_state, 1082 item->wk_func, item->wk_line); 1083 item->wk_state &= ~ONWORKLIST; 1084 item->wk_func = func; 1085 item->wk_line = line; 1086 LIST_REMOVE(item, wk_list); 1087 } 1088 #endif /* INVARIANTS */ 1089 1090 /* 1091 * Merge two jsegdeps keeping only the oldest one as newer references 1092 * can't be discarded until after older references. 1093 */ 1094 static inline struct jsegdep * 1095 jsegdep_merge(struct jsegdep *one, struct jsegdep *two) 1096 { 1097 struct jsegdep *swp; 1098 1099 if (two == NULL) 1100 return (one); 1101 1102 if (one->jd_seg->js_seq > two->jd_seg->js_seq) { 1103 swp = one; 1104 one = two; 1105 two = swp; 1106 } 1107 WORKLIST_REMOVE(&two->jd_list); 1108 free_jsegdep(two); 1109 1110 return (one); 1111 } 1112 1113 /* 1114 * If two freedeps are compatible free one to reduce list size. 1115 */ 1116 static inline struct freedep * 1117 freedep_merge(struct freedep *one, struct freedep *two) 1118 { 1119 if (two == NULL) 1120 return (one); 1121 1122 if (one->fd_freework == two->fd_freework) { 1123 WORKLIST_REMOVE(&two->fd_list); 1124 free_freedep(two); 1125 } 1126 return (one); 1127 } 1128 1129 /* 1130 * Move journal work from one list to another. Duplicate freedeps and 1131 * jsegdeps are coalesced to keep the lists as small as possible. 1132 */ 1133 static void 1134 jwork_move(dst, src) 1135 struct workhead *dst; 1136 struct workhead *src; 1137 { 1138 struct freedep *freedep; 1139 struct jsegdep *jsegdep; 1140 struct worklist *wkn; 1141 struct worklist *wk; 1142 1143 KASSERT(dst != src, 1144 ("jwork_move: dst == src")); 1145 freedep = NULL; 1146 jsegdep = NULL; 1147 LIST_FOREACH_SAFE(wk, dst, wk_list, wkn) { 1148 if (wk->wk_type == D_JSEGDEP) 1149 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1150 else if (wk->wk_type == D_FREEDEP) 1151 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1152 } 1153 1154 while ((wk = LIST_FIRST(src)) != NULL) { 1155 WORKLIST_REMOVE(wk); 1156 WORKLIST_INSERT(dst, wk); 1157 if (wk->wk_type == D_JSEGDEP) { 1158 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1159 continue; 1160 } 1161 if (wk->wk_type == D_FREEDEP) 1162 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1163 } 1164 } 1165 1166 static void 1167 jwork_insert(dst, jsegdep) 1168 struct workhead *dst; 1169 struct jsegdep *jsegdep; 1170 { 1171 struct jsegdep *jsegdepn; 1172 struct worklist *wk; 1173 1174 LIST_FOREACH(wk, dst, wk_list) 1175 if (wk->wk_type == D_JSEGDEP) 1176 break; 1177 if (wk == NULL) { 1178 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1179 return; 1180 } 1181 jsegdepn = WK_JSEGDEP(wk); 1182 if (jsegdep->jd_seg->js_seq < jsegdepn->jd_seg->js_seq) { 1183 WORKLIST_REMOVE(wk); 1184 free_jsegdep(jsegdepn); 1185 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1186 } else 1187 free_jsegdep(jsegdep); 1188 } 1189 1190 /* 1191 * Routines for tracking and managing workitems. 1192 */ 1193 static void workitem_free(struct worklist *, int); 1194 static void workitem_alloc(struct worklist *, int, struct mount *); 1195 static void workitem_reassign(struct worklist *, int); 1196 1197 #define WORKITEM_FREE(item, type) \ 1198 workitem_free((struct worklist *)(item), (type)) 1199 #define WORKITEM_REASSIGN(item, type) \ 1200 workitem_reassign((struct worklist *)(item), (type)) 1201 1202 static void 1203 workitem_free(item, type) 1204 struct worklist *item; 1205 int type; 1206 { 1207 struct ufsmount *ump; 1208 1209 #ifdef INVARIANTS 1210 if (item->wk_state & ONWORKLIST) 1211 panic("workitem_free: %s(0x%X) still on list, " 1212 "added in function %s at line %d", 1213 TYPENAME(item->wk_type), item->wk_state, 1214 item->wk_func, item->wk_line); 1215 if (item->wk_type != type && type != D_NEWBLK) 1216 panic("workitem_free: type mismatch %s != %s", 1217 TYPENAME(item->wk_type), TYPENAME(type)); 1218 #endif 1219 if (item->wk_state & IOWAITING) 1220 wakeup(item); 1221 ump = VFSTOUFS(item->wk_mp); 1222 LOCK_OWNED(ump); 1223 KASSERT(ump->softdep_deps > 0, 1224 ("workitem_free: %s: softdep_deps going negative", 1225 ump->um_fs->fs_fsmnt)); 1226 if (--ump->softdep_deps == 0 && ump->softdep_req) 1227 wakeup(&ump->softdep_deps); 1228 KASSERT(dep_current[item->wk_type] > 0, 1229 ("workitem_free: %s: dep_current[%s] going negative", 1230 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1231 KASSERT(ump->softdep_curdeps[item->wk_type] > 0, 1232 ("workitem_free: %s: softdep_curdeps[%s] going negative", 1233 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1234 atomic_subtract_long(&dep_current[item->wk_type], 1); 1235 ump->softdep_curdeps[item->wk_type] -= 1; 1236 #ifdef INVARIANTS 1237 LIST_REMOVE(item, wk_all); 1238 #endif 1239 free(item, DtoM(type)); 1240 } 1241 1242 static void 1243 workitem_alloc(item, type, mp) 1244 struct worklist *item; 1245 int type; 1246 struct mount *mp; 1247 { 1248 struct ufsmount *ump; 1249 1250 item->wk_type = type; 1251 item->wk_mp = mp; 1252 item->wk_state = 0; 1253 1254 ump = VFSTOUFS(mp); 1255 ACQUIRE_GBLLOCK(&lk); 1256 dep_current[type]++; 1257 if (dep_current[type] > dep_highuse[type]) 1258 dep_highuse[type] = dep_current[type]; 1259 dep_total[type]++; 1260 FREE_GBLLOCK(&lk); 1261 ACQUIRE_LOCK(ump); 1262 ump->softdep_curdeps[type] += 1; 1263 ump->softdep_deps++; 1264 ump->softdep_accdeps++; 1265 #ifdef INVARIANTS 1266 LIST_INSERT_HEAD(&ump->softdep_alldeps[type], item, wk_all); 1267 #endif 1268 FREE_LOCK(ump); 1269 } 1270 1271 static void 1272 workitem_reassign(item, newtype) 1273 struct worklist *item; 1274 int newtype; 1275 { 1276 struct ufsmount *ump; 1277 1278 ump = VFSTOUFS(item->wk_mp); 1279 LOCK_OWNED(ump); 1280 KASSERT(ump->softdep_curdeps[item->wk_type] > 0, 1281 ("workitem_reassign: %s: softdep_curdeps[%s] going negative", 1282 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1283 ump->softdep_curdeps[item->wk_type] -= 1; 1284 ump->softdep_curdeps[newtype] += 1; 1285 KASSERT(dep_current[item->wk_type] > 0, 1286 ("workitem_reassign: %s: dep_current[%s] going negative", 1287 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1288 ACQUIRE_GBLLOCK(&lk); 1289 dep_current[newtype]++; 1290 dep_current[item->wk_type]--; 1291 if (dep_current[newtype] > dep_highuse[newtype]) 1292 dep_highuse[newtype] = dep_current[newtype]; 1293 dep_total[newtype]++; 1294 FREE_GBLLOCK(&lk); 1295 item->wk_type = newtype; 1296 } 1297 1298 /* 1299 * Workitem queue management 1300 */ 1301 static int max_softdeps; /* maximum number of structs before slowdown */ 1302 static int tickdelay = 2; /* number of ticks to pause during slowdown */ 1303 static int proc_waiting; /* tracks whether we have a timeout posted */ 1304 static int *stat_countp; /* statistic to count in proc_waiting timeout */ 1305 static struct callout softdep_callout; 1306 static int req_clear_inodedeps; /* syncer process flush some inodedeps */ 1307 static int req_clear_remove; /* syncer process flush some freeblks */ 1308 static int softdep_flushcache = 0; /* Should we do BIO_FLUSH? */ 1309 1310 /* 1311 * runtime statistics 1312 */ 1313 static int stat_flush_threads; /* number of softdep flushing threads */ 1314 static int stat_worklist_push; /* number of worklist cleanups */ 1315 static int stat_delayed_inact; /* number of delayed inactivation cleanups */ 1316 static int stat_blk_limit_push; /* number of times block limit neared */ 1317 static int stat_ino_limit_push; /* number of times inode limit neared */ 1318 static int stat_blk_limit_hit; /* number of times block slowdown imposed */ 1319 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */ 1320 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */ 1321 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */ 1322 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */ 1323 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */ 1324 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */ 1325 static int stat_jaddref; /* bufs redirtied as ino bitmap can not write */ 1326 static int stat_jnewblk; /* bufs redirtied as blk bitmap can not write */ 1327 static int stat_journal_min; /* Times hit journal min threshold */ 1328 static int stat_journal_low; /* Times hit journal low threshold */ 1329 static int stat_journal_wait; /* Times blocked in jwait(). */ 1330 static int stat_jwait_filepage; /* Times blocked in jwait() for filepage. */ 1331 static int stat_jwait_freeblks; /* Times blocked in jwait() for freeblks. */ 1332 static int stat_jwait_inode; /* Times blocked in jwait() for inodes. */ 1333 static int stat_jwait_newblk; /* Times blocked in jwait() for newblks. */ 1334 static int stat_cleanup_high_delay; /* Maximum cleanup delay (in ticks) */ 1335 static int stat_cleanup_blkrequests; /* Number of block cleanup requests */ 1336 static int stat_cleanup_inorequests; /* Number of inode cleanup requests */ 1337 static int stat_cleanup_retries; /* Number of cleanups that needed to flush */ 1338 static int stat_cleanup_failures; /* Number of cleanup requests that failed */ 1339 static int stat_emptyjblocks; /* Number of potentially empty journal blocks */ 1340 1341 SYSCTL_INT(_debug_softdep, OID_AUTO, max_softdeps, CTLFLAG_RW, 1342 &max_softdeps, 0, ""); 1343 SYSCTL_INT(_debug_softdep, OID_AUTO, tickdelay, CTLFLAG_RW, 1344 &tickdelay, 0, ""); 1345 SYSCTL_INT(_debug_softdep, OID_AUTO, flush_threads, CTLFLAG_RD, 1346 &stat_flush_threads, 0, ""); 1347 SYSCTL_INT(_debug_softdep, OID_AUTO, worklist_push, 1348 CTLFLAG_RW | CTLFLAG_STATS, &stat_worklist_push, 0,""); 1349 SYSCTL_INT(_debug_softdep, OID_AUTO, delayed_inactivations, CTLFLAG_RD, 1350 &stat_delayed_inact, 0, ""); 1351 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_push, 1352 CTLFLAG_RW | CTLFLAG_STATS, &stat_blk_limit_push, 0,""); 1353 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_push, 1354 CTLFLAG_RW | CTLFLAG_STATS, &stat_ino_limit_push, 0,""); 1355 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_hit, 1356 CTLFLAG_RW | CTLFLAG_STATS, &stat_blk_limit_hit, 0, ""); 1357 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_hit, 1358 CTLFLAG_RW | CTLFLAG_STATS, &stat_ino_limit_hit, 0, ""); 1359 SYSCTL_INT(_debug_softdep, OID_AUTO, sync_limit_hit, 1360 CTLFLAG_RW | CTLFLAG_STATS, &stat_sync_limit_hit, 0, ""); 1361 SYSCTL_INT(_debug_softdep, OID_AUTO, indir_blk_ptrs, 1362 CTLFLAG_RW | CTLFLAG_STATS, &stat_indir_blk_ptrs, 0, ""); 1363 SYSCTL_INT(_debug_softdep, OID_AUTO, inode_bitmap, 1364 CTLFLAG_RW | CTLFLAG_STATS, &stat_inode_bitmap, 0, ""); 1365 SYSCTL_INT(_debug_softdep, OID_AUTO, direct_blk_ptrs, 1366 CTLFLAG_RW | CTLFLAG_STATS, &stat_direct_blk_ptrs, 0, ""); 1367 SYSCTL_INT(_debug_softdep, OID_AUTO, dir_entry, 1368 CTLFLAG_RW | CTLFLAG_STATS, &stat_dir_entry, 0, ""); 1369 SYSCTL_INT(_debug_softdep, OID_AUTO, jaddref_rollback, 1370 CTLFLAG_RW | CTLFLAG_STATS, &stat_jaddref, 0, ""); 1371 SYSCTL_INT(_debug_softdep, OID_AUTO, jnewblk_rollback, 1372 CTLFLAG_RW | CTLFLAG_STATS, &stat_jnewblk, 0, ""); 1373 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_low, 1374 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_low, 0, ""); 1375 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_min, 1376 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_min, 0, ""); 1377 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_wait, 1378 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_wait, 0, ""); 1379 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_filepage, 1380 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_filepage, 0, ""); 1381 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_freeblks, 1382 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_freeblks, 0, ""); 1383 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_inode, 1384 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_inode, 0, ""); 1385 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_newblk, 1386 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_newblk, 0, ""); 1387 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_blkrequests, 1388 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_blkrequests, 0, ""); 1389 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_inorequests, 1390 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_inorequests, 0, ""); 1391 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_high_delay, 1392 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_high_delay, 0, ""); 1393 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_retries, 1394 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_retries, 0, ""); 1395 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_failures, 1396 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_failures, 0, ""); 1397 1398 SYSCTL_INT(_debug_softdep, OID_AUTO, flushcache, CTLFLAG_RW, 1399 &softdep_flushcache, 0, ""); 1400 SYSCTL_INT(_debug_softdep, OID_AUTO, emptyjblocks, CTLFLAG_RD, 1401 &stat_emptyjblocks, 0, ""); 1402 1403 SYSCTL_DECL(_vfs_ffs); 1404 1405 /* Whether to recompute the summary at mount time */ 1406 static int compute_summary_at_mount = 0; 1407 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW, 1408 &compute_summary_at_mount, 0, "Recompute summary at mount"); 1409 static int print_threads = 0; 1410 SYSCTL_INT(_debug_softdep, OID_AUTO, print_threads, CTLFLAG_RW, 1411 &print_threads, 0, "Notify flusher thread start/stop"); 1412 1413 /* List of all filesystems mounted with soft updates */ 1414 static TAILQ_HEAD(, mount_softdeps) softdepmounts; 1415 1416 static void 1417 get_parent_vp_unlock_bp(struct mount *mp, struct buf *bp, 1418 struct diraddhd *diraddhdp, struct diraddhd *unfinishedp) 1419 { 1420 struct diradd *dap; 1421 1422 /* 1423 * Requeue unfinished dependencies before 1424 * unlocking buffer, which could make 1425 * diraddhdp invalid. 1426 */ 1427 ACQUIRE_LOCK(VFSTOUFS(mp)); 1428 while ((dap = LIST_FIRST(unfinishedp)) != NULL) { 1429 LIST_REMOVE(dap, da_pdlist); 1430 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist); 1431 } 1432 FREE_LOCK(VFSTOUFS(mp)); 1433 1434 bp->b_vflags &= ~BV_SCANNED; 1435 BUF_NOREC(bp); 1436 BUF_UNLOCK(bp); 1437 } 1438 1439 /* 1440 * This function fetches inode inum on mount point mp. We already 1441 * hold a locked vnode vp, and might have a locked buffer bp belonging 1442 * to vp. 1443 1444 * We must not block on acquiring the new inode lock as we will get 1445 * into a lock-order reversal with the buffer lock and possibly get a 1446 * deadlock. Thus if we cannot instantiate the requested vnode 1447 * without sleeping on its lock, we must unlock the vnode and the 1448 * buffer before doing a blocking on the vnode lock. We return 1449 * ERELOOKUP if we have had to unlock either the vnode or the buffer so 1450 * that the caller can reassess its state. 1451 * 1452 * Top-level VFS code (for syscalls and other consumers, e.g. callers 1453 * of VOP_FSYNC() in syncer) check for ERELOOKUP and restart at safe 1454 * point. 1455 * 1456 * Since callers expect to operate on fully constructed vnode, we also 1457 * recheck v_data after relock, and return ENOENT if NULL. 1458 * 1459 * If unlocking bp, we must unroll dequeueing its unfinished 1460 * dependencies, and clear scan flag, before unlocking. If unlocking 1461 * vp while it is under deactivation, we re-queue deactivation. 1462 */ 1463 static int 1464 get_parent_vp(struct vnode *vp, struct mount *mp, ino_t inum, struct buf *bp, 1465 struct diraddhd *diraddhdp, struct diraddhd *unfinishedp, 1466 struct vnode **rvp) 1467 { 1468 struct vnode *pvp; 1469 int error; 1470 bool bplocked; 1471 1472 ASSERT_VOP_ELOCKED(vp, "child vnode must be locked"); 1473 for (bplocked = true, pvp = NULL;;) { 1474 error = ffs_vgetf(mp, inum, LK_EXCLUSIVE | LK_NOWAIT, &pvp, 1475 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP); 1476 if (error == 0) { 1477 /* 1478 * Since we could have unlocked vp, the inode 1479 * number could no longer indicate a 1480 * constructed node. In this case, we must 1481 * restart the syscall. 1482 */ 1483 if (VTOI(pvp)->i_mode == 0 || !bplocked) { 1484 if (bp != NULL && bplocked) 1485 get_parent_vp_unlock_bp(mp, bp, 1486 diraddhdp, unfinishedp); 1487 if (VTOI(pvp)->i_mode == 0) 1488 vgone(pvp); 1489 error = ERELOOKUP; 1490 goto out2; 1491 } 1492 goto out1; 1493 } 1494 if (bp != NULL && bplocked) { 1495 get_parent_vp_unlock_bp(mp, bp, diraddhdp, unfinishedp); 1496 bplocked = false; 1497 } 1498 1499 /* 1500 * Do not drop vnode lock while inactivating during 1501 * vunref. This would result in leaks of the VI flags 1502 * and reclaiming of non-truncated vnode. Instead, 1503 * re-schedule inactivation hoping that we would be 1504 * able to sync inode later. 1505 */ 1506 if ((vp->v_iflag & VI_DOINGINACT) != 0 && 1507 (vp->v_vflag & VV_UNREF) != 0) { 1508 VI_LOCK(vp); 1509 vp->v_iflag |= VI_OWEINACT; 1510 VI_UNLOCK(vp); 1511 return (ERELOOKUP); 1512 } 1513 1514 VOP_UNLOCK(vp); 1515 error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &pvp, 1516 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP); 1517 if (error != 0) { 1518 MPASS(error != ERELOOKUP); 1519 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1520 break; 1521 } 1522 if (VTOI(pvp)->i_mode == 0) { 1523 vgone(pvp); 1524 vput(pvp); 1525 pvp = NULL; 1526 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1527 error = ERELOOKUP; 1528 break; 1529 } 1530 error = vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT); 1531 if (error == 0) 1532 break; 1533 vput(pvp); 1534 pvp = NULL; 1535 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1536 if (vp->v_data == NULL) { 1537 error = ENOENT; 1538 break; 1539 } 1540 } 1541 if (bp != NULL) { 1542 MPASS(!bplocked); 1543 error = ERELOOKUP; 1544 } 1545 out2: 1546 if (error != 0 && pvp != NULL) { 1547 vput(pvp); 1548 pvp = NULL; 1549 } 1550 out1: 1551 *rvp = pvp; 1552 ASSERT_VOP_ELOCKED(vp, "child vnode must be locked on return"); 1553 return (error); 1554 } 1555 1556 /* 1557 * This function cleans the worklist for a filesystem. 1558 * Each filesystem running with soft dependencies gets its own 1559 * thread to run in this function. The thread is started up in 1560 * softdep_mount and shutdown in softdep_unmount. They show up 1561 * as part of the kernel "bufdaemon" process whose process 1562 * entry is available in bufdaemonproc. 1563 */ 1564 static int searchfailed; 1565 extern struct proc *bufdaemonproc; 1566 static void 1567 softdep_flush(addr) 1568 void *addr; 1569 { 1570 struct mount *mp; 1571 struct thread *td; 1572 struct ufsmount *ump; 1573 int cleanups; 1574 1575 td = curthread; 1576 td->td_pflags |= TDP_NORUNNINGBUF; 1577 mp = (struct mount *)addr; 1578 ump = VFSTOUFS(mp); 1579 atomic_add_int(&stat_flush_threads, 1); 1580 ACQUIRE_LOCK(ump); 1581 ump->softdep_flags &= ~FLUSH_STARTING; 1582 wakeup(&ump->softdep_flushtd); 1583 FREE_LOCK(ump); 1584 if (print_threads) { 1585 if (stat_flush_threads == 1) 1586 printf("Running %s at pid %d\n", bufdaemonproc->p_comm, 1587 bufdaemonproc->p_pid); 1588 printf("Start thread %s\n", td->td_name); 1589 } 1590 for (;;) { 1591 while (softdep_process_worklist(mp, 0) > 0 || 1592 (MOUNTEDSUJ(mp) && 1593 VFSTOUFS(mp)->softdep_jblocks->jb_suspended)) 1594 kthread_suspend_check(); 1595 ACQUIRE_LOCK(ump); 1596 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1597 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, 1598 "sdflush", hz / 2); 1599 ump->softdep_flags &= ~FLUSH_CLEANUP; 1600 /* 1601 * Check to see if we are done and need to exit. 1602 */ 1603 if ((ump->softdep_flags & FLUSH_EXIT) == 0) { 1604 FREE_LOCK(ump); 1605 continue; 1606 } 1607 ump->softdep_flags &= ~FLUSH_EXIT; 1608 cleanups = ump->um_softdep->sd_cleanups; 1609 FREE_LOCK(ump); 1610 wakeup(&ump->softdep_flags); 1611 if (print_threads) { 1612 printf("Stop thread %s: searchfailed %d, " 1613 "did cleanups %d\n", 1614 td->td_name, searchfailed, cleanups); 1615 } 1616 atomic_subtract_int(&stat_flush_threads, 1); 1617 kthread_exit(); 1618 panic("kthread_exit failed\n"); 1619 } 1620 } 1621 1622 static void 1623 worklist_speedup(mp) 1624 struct mount *mp; 1625 { 1626 struct ufsmount *ump; 1627 1628 ump = VFSTOUFS(mp); 1629 LOCK_OWNED(ump); 1630 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1631 ump->softdep_flags |= FLUSH_CLEANUP; 1632 wakeup(&ump->softdep_flushtd); 1633 } 1634 1635 static void 1636 softdep_send_speedup(struct ufsmount *ump, off_t shortage, u_int flags) 1637 { 1638 struct buf *bp; 1639 1640 if ((ump->um_flags & UM_CANSPEEDUP) == 0) 1641 return; 1642 1643 bp = malloc(sizeof(*bp), M_TRIM, M_WAITOK | M_ZERO); 1644 bp->b_iocmd = BIO_SPEEDUP; 1645 bp->b_ioflags = flags; 1646 bp->b_bcount = omin(shortage, LONG_MAX); 1647 g_vfs_strategy(ump->um_bo, bp); 1648 bufwait(bp); 1649 free(bp, M_TRIM); 1650 } 1651 1652 static int 1653 softdep_speedup(ump) 1654 struct ufsmount *ump; 1655 { 1656 struct ufsmount *altump; 1657 struct mount_softdeps *sdp; 1658 1659 LOCK_OWNED(ump); 1660 worklist_speedup(ump->um_mountp); 1661 bd_speedup(); 1662 /* 1663 * If we have global shortages, then we need other 1664 * filesystems to help with the cleanup. Here we wakeup a 1665 * flusher thread for a filesystem that is over its fair 1666 * share of resources. 1667 */ 1668 if (req_clear_inodedeps || req_clear_remove) { 1669 ACQUIRE_GBLLOCK(&lk); 1670 TAILQ_FOREACH(sdp, &softdepmounts, sd_next) { 1671 if ((altump = sdp->sd_ump) == ump) 1672 continue; 1673 if (((req_clear_inodedeps && 1674 altump->softdep_curdeps[D_INODEDEP] > 1675 max_softdeps / stat_flush_threads) || 1676 (req_clear_remove && 1677 altump->softdep_curdeps[D_DIRREM] > 1678 (max_softdeps / 2) / stat_flush_threads)) && 1679 TRY_ACQUIRE_LOCK(altump)) 1680 break; 1681 } 1682 if (sdp == NULL) { 1683 searchfailed++; 1684 FREE_GBLLOCK(&lk); 1685 } else { 1686 /* 1687 * Move to the end of the list so we pick a 1688 * different one on out next try. 1689 */ 1690 TAILQ_REMOVE(&softdepmounts, sdp, sd_next); 1691 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next); 1692 FREE_GBLLOCK(&lk); 1693 if ((altump->softdep_flags & 1694 (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1695 altump->softdep_flags |= FLUSH_CLEANUP; 1696 altump->um_softdep->sd_cleanups++; 1697 wakeup(&altump->softdep_flushtd); 1698 FREE_LOCK(altump); 1699 } 1700 } 1701 return (speedup_syncer()); 1702 } 1703 1704 /* 1705 * Add an item to the end of the work queue. 1706 * This routine requires that the lock be held. 1707 * This is the only routine that adds items to the list. 1708 * The following routine is the only one that removes items 1709 * and does so in order from first to last. 1710 */ 1711 1712 #define WK_HEAD 0x0001 /* Add to HEAD. */ 1713 #define WK_NODELAY 0x0002 /* Process immediately. */ 1714 1715 static void 1716 add_to_worklist(wk, flags) 1717 struct worklist *wk; 1718 int flags; 1719 { 1720 struct ufsmount *ump; 1721 1722 ump = VFSTOUFS(wk->wk_mp); 1723 LOCK_OWNED(ump); 1724 if (wk->wk_state & ONWORKLIST) 1725 panic("add_to_worklist: %s(0x%X) already on list", 1726 TYPENAME(wk->wk_type), wk->wk_state); 1727 wk->wk_state |= ONWORKLIST; 1728 if (ump->softdep_on_worklist == 0) { 1729 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1730 ump->softdep_worklist_tail = wk; 1731 } else if (flags & WK_HEAD) { 1732 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1733 } else { 1734 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list); 1735 ump->softdep_worklist_tail = wk; 1736 } 1737 ump->softdep_on_worklist += 1; 1738 if (flags & WK_NODELAY) 1739 worklist_speedup(wk->wk_mp); 1740 } 1741 1742 /* 1743 * Remove the item to be processed. If we are removing the last 1744 * item on the list, we need to recalculate the tail pointer. 1745 */ 1746 static void 1747 remove_from_worklist(wk) 1748 struct worklist *wk; 1749 { 1750 struct ufsmount *ump; 1751 1752 ump = VFSTOUFS(wk->wk_mp); 1753 if (ump->softdep_worklist_tail == wk) 1754 ump->softdep_worklist_tail = 1755 (struct worklist *)wk->wk_list.le_prev; 1756 WORKLIST_REMOVE(wk); 1757 ump->softdep_on_worklist -= 1; 1758 } 1759 1760 static void 1761 wake_worklist(wk) 1762 struct worklist *wk; 1763 { 1764 if (wk->wk_state & IOWAITING) { 1765 wk->wk_state &= ~IOWAITING; 1766 wakeup(wk); 1767 } 1768 } 1769 1770 static void 1771 wait_worklist(wk, wmesg) 1772 struct worklist *wk; 1773 char *wmesg; 1774 { 1775 struct ufsmount *ump; 1776 1777 ump = VFSTOUFS(wk->wk_mp); 1778 wk->wk_state |= IOWAITING; 1779 msleep(wk, LOCK_PTR(ump), PVM, wmesg, 0); 1780 } 1781 1782 /* 1783 * Process that runs once per second to handle items in the background queue. 1784 * 1785 * Note that we ensure that everything is done in the order in which they 1786 * appear in the queue. The code below depends on this property to ensure 1787 * that blocks of a file are freed before the inode itself is freed. This 1788 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated 1789 * until all the old ones have been purged from the dependency lists. 1790 */ 1791 static int 1792 softdep_process_worklist(mp, full) 1793 struct mount *mp; 1794 int full; 1795 { 1796 int cnt, matchcnt; 1797 struct ufsmount *ump; 1798 long starttime; 1799 1800 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp")); 1801 ump = VFSTOUFS(mp); 1802 if (ump->um_softdep == NULL) 1803 return (0); 1804 matchcnt = 0; 1805 ACQUIRE_LOCK(ump); 1806 starttime = time_second; 1807 softdep_process_journal(mp, NULL, full ? MNT_WAIT : 0); 1808 check_clear_deps(mp); 1809 while (ump->softdep_on_worklist > 0) { 1810 if ((cnt = process_worklist_item(mp, 10, LK_NOWAIT)) == 0) 1811 break; 1812 else 1813 matchcnt += cnt; 1814 check_clear_deps(mp); 1815 /* 1816 * We do not generally want to stop for buffer space, but if 1817 * we are really being a buffer hog, we will stop and wait. 1818 */ 1819 if (should_yield()) { 1820 FREE_LOCK(ump); 1821 kern_yield(PRI_USER); 1822 bwillwrite(); 1823 ACQUIRE_LOCK(ump); 1824 } 1825 /* 1826 * Never allow processing to run for more than one 1827 * second. This gives the syncer thread the opportunity 1828 * to pause if appropriate. 1829 */ 1830 if (!full && starttime != time_second) 1831 break; 1832 } 1833 if (full == 0) 1834 journal_unsuspend(ump); 1835 FREE_LOCK(ump); 1836 return (matchcnt); 1837 } 1838 1839 /* 1840 * Process all removes associated with a vnode if we are running out of 1841 * journal space. Any other process which attempts to flush these will 1842 * be unable as we have the vnodes locked. 1843 */ 1844 static void 1845 process_removes(vp) 1846 struct vnode *vp; 1847 { 1848 struct inodedep *inodedep; 1849 struct dirrem *dirrem; 1850 struct ufsmount *ump; 1851 struct mount *mp; 1852 ino_t inum; 1853 1854 mp = vp->v_mount; 1855 ump = VFSTOUFS(mp); 1856 LOCK_OWNED(ump); 1857 inum = VTOI(vp)->i_number; 1858 for (;;) { 1859 top: 1860 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1861 return; 1862 LIST_FOREACH(dirrem, &inodedep->id_dirremhd, dm_inonext) { 1863 /* 1864 * If another thread is trying to lock this vnode 1865 * it will fail but we must wait for it to do so 1866 * before we can proceed. 1867 */ 1868 if (dirrem->dm_state & INPROGRESS) { 1869 wait_worklist(&dirrem->dm_list, "pwrwait"); 1870 goto top; 1871 } 1872 if ((dirrem->dm_state & (COMPLETE | ONWORKLIST)) == 1873 (COMPLETE | ONWORKLIST)) 1874 break; 1875 } 1876 if (dirrem == NULL) 1877 return; 1878 remove_from_worklist(&dirrem->dm_list); 1879 FREE_LOCK(ump); 1880 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1881 panic("process_removes: suspended filesystem"); 1882 handle_workitem_remove(dirrem, 0); 1883 vn_finished_secondary_write(mp); 1884 ACQUIRE_LOCK(ump); 1885 } 1886 } 1887 1888 /* 1889 * Process all truncations associated with a vnode if we are running out 1890 * of journal space. This is called when the vnode lock is already held 1891 * and no other process can clear the truncation. This function returns 1892 * a value greater than zero if it did any work. 1893 */ 1894 static void 1895 process_truncates(vp) 1896 struct vnode *vp; 1897 { 1898 struct inodedep *inodedep; 1899 struct freeblks *freeblks; 1900 struct ufsmount *ump; 1901 struct mount *mp; 1902 ino_t inum; 1903 int cgwait; 1904 1905 mp = vp->v_mount; 1906 ump = VFSTOUFS(mp); 1907 LOCK_OWNED(ump); 1908 inum = VTOI(vp)->i_number; 1909 for (;;) { 1910 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1911 return; 1912 cgwait = 0; 1913 TAILQ_FOREACH(freeblks, &inodedep->id_freeblklst, fb_next) { 1914 /* Journal entries not yet written. */ 1915 if (!LIST_EMPTY(&freeblks->fb_jblkdephd)) { 1916 jwait(&LIST_FIRST( 1917 &freeblks->fb_jblkdephd)->jb_list, 1918 MNT_WAIT); 1919 break; 1920 } 1921 /* Another thread is executing this item. */ 1922 if (freeblks->fb_state & INPROGRESS) { 1923 wait_worklist(&freeblks->fb_list, "ptrwait"); 1924 break; 1925 } 1926 /* Freeblks is waiting on a inode write. */ 1927 if ((freeblks->fb_state & COMPLETE) == 0) { 1928 FREE_LOCK(ump); 1929 ffs_update(vp, 1); 1930 ACQUIRE_LOCK(ump); 1931 break; 1932 } 1933 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST)) == 1934 (ALLCOMPLETE | ONWORKLIST)) { 1935 remove_from_worklist(&freeblks->fb_list); 1936 freeblks->fb_state |= INPROGRESS; 1937 FREE_LOCK(ump); 1938 if (vn_start_secondary_write(NULL, &mp, 1939 V_NOWAIT)) 1940 panic("process_truncates: " 1941 "suspended filesystem"); 1942 handle_workitem_freeblocks(freeblks, 0); 1943 vn_finished_secondary_write(mp); 1944 ACQUIRE_LOCK(ump); 1945 break; 1946 } 1947 if (freeblks->fb_cgwait) 1948 cgwait++; 1949 } 1950 if (cgwait) { 1951 FREE_LOCK(ump); 1952 sync_cgs(mp, MNT_WAIT); 1953 ffs_sync_snap(mp, MNT_WAIT); 1954 ACQUIRE_LOCK(ump); 1955 continue; 1956 } 1957 if (freeblks == NULL) 1958 break; 1959 } 1960 return; 1961 } 1962 1963 /* 1964 * Process one item on the worklist. 1965 */ 1966 static int 1967 process_worklist_item(mp, target, flags) 1968 struct mount *mp; 1969 int target; 1970 int flags; 1971 { 1972 struct worklist sentinel; 1973 struct worklist *wk; 1974 struct ufsmount *ump; 1975 int matchcnt; 1976 int error; 1977 1978 KASSERT(mp != NULL, ("process_worklist_item: NULL mp")); 1979 /* 1980 * If we are being called because of a process doing a 1981 * copy-on-write, then it is not safe to write as we may 1982 * recurse into the copy-on-write routine. 1983 */ 1984 if (curthread->td_pflags & TDP_COWINPROGRESS) 1985 return (-1); 1986 PHOLD(curproc); /* Don't let the stack go away. */ 1987 ump = VFSTOUFS(mp); 1988 LOCK_OWNED(ump); 1989 matchcnt = 0; 1990 sentinel.wk_mp = NULL; 1991 sentinel.wk_type = D_SENTINEL; 1992 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, &sentinel, wk_list); 1993 for (wk = LIST_NEXT(&sentinel, wk_list); wk != NULL; 1994 wk = LIST_NEXT(&sentinel, wk_list)) { 1995 if (wk->wk_type == D_SENTINEL) { 1996 LIST_REMOVE(&sentinel, wk_list); 1997 LIST_INSERT_AFTER(wk, &sentinel, wk_list); 1998 continue; 1999 } 2000 if (wk->wk_state & INPROGRESS) 2001 panic("process_worklist_item: %p already in progress.", 2002 wk); 2003 wk->wk_state |= INPROGRESS; 2004 remove_from_worklist(wk); 2005 FREE_LOCK(ump); 2006 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 2007 panic("process_worklist_item: suspended filesystem"); 2008 switch (wk->wk_type) { 2009 case D_DIRREM: 2010 /* removal of a directory entry */ 2011 error = handle_workitem_remove(WK_DIRREM(wk), flags); 2012 break; 2013 2014 case D_FREEBLKS: 2015 /* releasing blocks and/or fragments from a file */ 2016 error = handle_workitem_freeblocks(WK_FREEBLKS(wk), 2017 flags); 2018 break; 2019 2020 case D_FREEFRAG: 2021 /* releasing a fragment when replaced as a file grows */ 2022 handle_workitem_freefrag(WK_FREEFRAG(wk)); 2023 error = 0; 2024 break; 2025 2026 case D_FREEFILE: 2027 /* releasing an inode when its link count drops to 0 */ 2028 handle_workitem_freefile(WK_FREEFILE(wk)); 2029 error = 0; 2030 break; 2031 2032 default: 2033 panic("%s_process_worklist: Unknown type %s", 2034 "softdep", TYPENAME(wk->wk_type)); 2035 /* NOTREACHED */ 2036 } 2037 vn_finished_secondary_write(mp); 2038 ACQUIRE_LOCK(ump); 2039 if (error == 0) { 2040 if (++matchcnt == target) 2041 break; 2042 continue; 2043 } 2044 /* 2045 * We have to retry the worklist item later. Wake up any 2046 * waiters who may be able to complete it immediately and 2047 * add the item back to the head so we don't try to execute 2048 * it again. 2049 */ 2050 wk->wk_state &= ~INPROGRESS; 2051 wake_worklist(wk); 2052 add_to_worklist(wk, WK_HEAD); 2053 } 2054 /* Sentinal could've become the tail from remove_from_worklist. */ 2055 if (ump->softdep_worklist_tail == &sentinel) 2056 ump->softdep_worklist_tail = 2057 (struct worklist *)sentinel.wk_list.le_prev; 2058 LIST_REMOVE(&sentinel, wk_list); 2059 PRELE(curproc); 2060 return (matchcnt); 2061 } 2062 2063 /* 2064 * Move dependencies from one buffer to another. 2065 */ 2066 int 2067 softdep_move_dependencies(oldbp, newbp) 2068 struct buf *oldbp; 2069 struct buf *newbp; 2070 { 2071 struct worklist *wk, *wktail; 2072 struct ufsmount *ump; 2073 int dirty; 2074 2075 if ((wk = LIST_FIRST(&oldbp->b_dep)) == NULL) 2076 return (0); 2077 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 2078 ("softdep_move_dependencies called on non-softdep filesystem")); 2079 dirty = 0; 2080 wktail = NULL; 2081 ump = VFSTOUFS(wk->wk_mp); 2082 ACQUIRE_LOCK(ump); 2083 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) { 2084 LIST_REMOVE(wk, wk_list); 2085 if (wk->wk_type == D_BMSAFEMAP && 2086 bmsafemap_backgroundwrite(WK_BMSAFEMAP(wk), newbp)) 2087 dirty = 1; 2088 if (wktail == NULL) 2089 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list); 2090 else 2091 LIST_INSERT_AFTER(wktail, wk, wk_list); 2092 wktail = wk; 2093 } 2094 FREE_LOCK(ump); 2095 2096 return (dirty); 2097 } 2098 2099 /* 2100 * Purge the work list of all items associated with a particular mount point. 2101 */ 2102 int 2103 softdep_flushworklist(oldmnt, countp, td) 2104 struct mount *oldmnt; 2105 int *countp; 2106 struct thread *td; 2107 { 2108 struct vnode *devvp; 2109 struct ufsmount *ump; 2110 int count, error; 2111 2112 /* 2113 * Alternately flush the block device associated with the mount 2114 * point and process any dependencies that the flushing 2115 * creates. We continue until no more worklist dependencies 2116 * are found. 2117 */ 2118 *countp = 0; 2119 error = 0; 2120 ump = VFSTOUFS(oldmnt); 2121 devvp = ump->um_devvp; 2122 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) { 2123 *countp += count; 2124 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 2125 error = VOP_FSYNC(devvp, MNT_WAIT, td); 2126 VOP_UNLOCK(devvp); 2127 if (error != 0) 2128 break; 2129 } 2130 return (error); 2131 } 2132 2133 #define SU_WAITIDLE_RETRIES 20 2134 static int 2135 softdep_waitidle(struct mount *mp, int flags __unused) 2136 { 2137 struct ufsmount *ump; 2138 struct vnode *devvp; 2139 struct thread *td; 2140 int error, i; 2141 2142 ump = VFSTOUFS(mp); 2143 KASSERT(ump->um_softdep != NULL, 2144 ("softdep_waitidle called on non-softdep filesystem")); 2145 devvp = ump->um_devvp; 2146 td = curthread; 2147 error = 0; 2148 ACQUIRE_LOCK(ump); 2149 for (i = 0; i < SU_WAITIDLE_RETRIES && ump->softdep_deps != 0; i++) { 2150 ump->softdep_req = 1; 2151 KASSERT((flags & FORCECLOSE) == 0 || 2152 ump->softdep_on_worklist == 0, 2153 ("softdep_waitidle: work added after flush")); 2154 msleep(&ump->softdep_deps, LOCK_PTR(ump), PVM | PDROP, 2155 "softdeps", 10 * hz); 2156 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 2157 error = VOP_FSYNC(devvp, MNT_WAIT, td); 2158 VOP_UNLOCK(devvp); 2159 ACQUIRE_LOCK(ump); 2160 if (error != 0) 2161 break; 2162 } 2163 ump->softdep_req = 0; 2164 if (i == SU_WAITIDLE_RETRIES && error == 0 && ump->softdep_deps != 0) { 2165 error = EBUSY; 2166 printf("softdep_waitidle: Failed to flush worklist for %p\n", 2167 mp); 2168 } 2169 FREE_LOCK(ump); 2170 return (error); 2171 } 2172 2173 /* 2174 * Flush all vnodes and worklist items associated with a specified mount point. 2175 */ 2176 int 2177 softdep_flushfiles(oldmnt, flags, td) 2178 struct mount *oldmnt; 2179 int flags; 2180 struct thread *td; 2181 { 2182 struct ufsmount *ump; 2183 #ifdef QUOTA 2184 int i; 2185 #endif 2186 int error, early, depcount, loopcnt, retry_flush_count, retry; 2187 int morework; 2188 2189 ump = VFSTOUFS(oldmnt); 2190 KASSERT(ump->um_softdep != NULL, 2191 ("softdep_flushfiles called on non-softdep filesystem")); 2192 loopcnt = 10; 2193 retry_flush_count = 3; 2194 retry_flush: 2195 error = 0; 2196 2197 /* 2198 * Alternately flush the vnodes associated with the mount 2199 * point and process any dependencies that the flushing 2200 * creates. In theory, this loop can happen at most twice, 2201 * but we give it a few extra just to be sure. 2202 */ 2203 for (; loopcnt > 0; loopcnt--) { 2204 /* 2205 * Do another flush in case any vnodes were brought in 2206 * as part of the cleanup operations. 2207 */ 2208 early = retry_flush_count == 1 || (oldmnt->mnt_kern_flag & 2209 MNTK_UNMOUNT) == 0 ? 0 : EARLYFLUSH; 2210 if ((error = ffs_flushfiles(oldmnt, flags | early, td)) != 0) 2211 break; 2212 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 || 2213 depcount == 0) 2214 break; 2215 } 2216 /* 2217 * If we are unmounting then it is an error to fail. If we 2218 * are simply trying to downgrade to read-only, then filesystem 2219 * activity can keep us busy forever, so we just fail with EBUSY. 2220 */ 2221 if (loopcnt == 0) { 2222 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) 2223 panic("softdep_flushfiles: looping"); 2224 error = EBUSY; 2225 } 2226 if (!error) 2227 error = softdep_waitidle(oldmnt, flags); 2228 if (!error) { 2229 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) { 2230 retry = 0; 2231 MNT_ILOCK(oldmnt); 2232 morework = oldmnt->mnt_nvnodelistsize > 0; 2233 #ifdef QUOTA 2234 UFS_LOCK(ump); 2235 for (i = 0; i < MAXQUOTAS; i++) { 2236 if (ump->um_quotas[i] != NULLVP) 2237 morework = 1; 2238 } 2239 UFS_UNLOCK(ump); 2240 #endif 2241 if (morework) { 2242 if (--retry_flush_count > 0) { 2243 retry = 1; 2244 loopcnt = 3; 2245 } else 2246 error = EBUSY; 2247 } 2248 MNT_IUNLOCK(oldmnt); 2249 if (retry) 2250 goto retry_flush; 2251 } 2252 } 2253 return (error); 2254 } 2255 2256 /* 2257 * Structure hashing. 2258 * 2259 * There are four types of structures that can be looked up: 2260 * 1) pagedep structures identified by mount point, inode number, 2261 * and logical block. 2262 * 2) inodedep structures identified by mount point and inode number. 2263 * 3) newblk structures identified by mount point and 2264 * physical block number. 2265 * 4) bmsafemap structures identified by mount point and 2266 * cylinder group number. 2267 * 2268 * The "pagedep" and "inodedep" dependency structures are hashed 2269 * separately from the file blocks and inodes to which they correspond. 2270 * This separation helps when the in-memory copy of an inode or 2271 * file block must be replaced. It also obviates the need to access 2272 * an inode or file page when simply updating (or de-allocating) 2273 * dependency structures. Lookup of newblk structures is needed to 2274 * find newly allocated blocks when trying to associate them with 2275 * their allocdirect or allocindir structure. 2276 * 2277 * The lookup routines optionally create and hash a new instance when 2278 * an existing entry is not found. The bmsafemap lookup routine always 2279 * allocates a new structure if an existing one is not found. 2280 */ 2281 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */ 2282 2283 /* 2284 * Structures and routines associated with pagedep caching. 2285 */ 2286 #define PAGEDEP_HASH(ump, inum, lbn) \ 2287 (&(ump)->pagedep_hashtbl[((inum) + (lbn)) & (ump)->pagedep_hash_size]) 2288 2289 static int 2290 pagedep_find(pagedephd, ino, lbn, pagedeppp) 2291 struct pagedep_hashhead *pagedephd; 2292 ino_t ino; 2293 ufs_lbn_t lbn; 2294 struct pagedep **pagedeppp; 2295 { 2296 struct pagedep *pagedep; 2297 2298 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 2299 if (ino == pagedep->pd_ino && lbn == pagedep->pd_lbn) { 2300 *pagedeppp = pagedep; 2301 return (1); 2302 } 2303 } 2304 *pagedeppp = NULL; 2305 return (0); 2306 } 2307 /* 2308 * Look up a pagedep. Return 1 if found, 0 otherwise. 2309 * If not found, allocate if DEPALLOC flag is passed. 2310 * Found or allocated entry is returned in pagedeppp. 2311 */ 2312 static int 2313 pagedep_lookup(mp, bp, ino, lbn, flags, pagedeppp) 2314 struct mount *mp; 2315 struct buf *bp; 2316 ino_t ino; 2317 ufs_lbn_t lbn; 2318 int flags; 2319 struct pagedep **pagedeppp; 2320 { 2321 struct pagedep *pagedep; 2322 struct pagedep_hashhead *pagedephd; 2323 struct worklist *wk; 2324 struct ufsmount *ump; 2325 int ret; 2326 int i; 2327 2328 ump = VFSTOUFS(mp); 2329 LOCK_OWNED(ump); 2330 if (bp) { 2331 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 2332 if (wk->wk_type == D_PAGEDEP) { 2333 *pagedeppp = WK_PAGEDEP(wk); 2334 return (1); 2335 } 2336 } 2337 } 2338 pagedephd = PAGEDEP_HASH(ump, ino, lbn); 2339 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp); 2340 if (ret) { 2341 if (((*pagedeppp)->pd_state & ONWORKLIST) == 0 && bp) 2342 WORKLIST_INSERT(&bp->b_dep, &(*pagedeppp)->pd_list); 2343 return (1); 2344 } 2345 if ((flags & DEPALLOC) == 0) 2346 return (0); 2347 FREE_LOCK(ump); 2348 pagedep = malloc(sizeof(struct pagedep), 2349 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO); 2350 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp); 2351 ACQUIRE_LOCK(ump); 2352 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp); 2353 if (*pagedeppp) { 2354 /* 2355 * This should never happen since we only create pagedeps 2356 * with the vnode lock held. Could be an assert. 2357 */ 2358 WORKITEM_FREE(pagedep, D_PAGEDEP); 2359 return (ret); 2360 } 2361 pagedep->pd_ino = ino; 2362 pagedep->pd_lbn = lbn; 2363 LIST_INIT(&pagedep->pd_dirremhd); 2364 LIST_INIT(&pagedep->pd_pendinghd); 2365 for (i = 0; i < DAHASHSZ; i++) 2366 LIST_INIT(&pagedep->pd_diraddhd[i]); 2367 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash); 2368 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list); 2369 *pagedeppp = pagedep; 2370 return (0); 2371 } 2372 2373 /* 2374 * Structures and routines associated with inodedep caching. 2375 */ 2376 #define INODEDEP_HASH(ump, inum) \ 2377 (&(ump)->inodedep_hashtbl[(inum) & (ump)->inodedep_hash_size]) 2378 2379 static int 2380 inodedep_find(inodedephd, inum, inodedeppp) 2381 struct inodedep_hashhead *inodedephd; 2382 ino_t inum; 2383 struct inodedep **inodedeppp; 2384 { 2385 struct inodedep *inodedep; 2386 2387 LIST_FOREACH(inodedep, inodedephd, id_hash) 2388 if (inum == inodedep->id_ino) 2389 break; 2390 if (inodedep) { 2391 *inodedeppp = inodedep; 2392 return (1); 2393 } 2394 *inodedeppp = NULL; 2395 2396 return (0); 2397 } 2398 /* 2399 * Look up an inodedep. Return 1 if found, 0 if not found. 2400 * If not found, allocate if DEPALLOC flag is passed. 2401 * Found or allocated entry is returned in inodedeppp. 2402 */ 2403 static int 2404 inodedep_lookup(mp, inum, flags, inodedeppp) 2405 struct mount *mp; 2406 ino_t inum; 2407 int flags; 2408 struct inodedep **inodedeppp; 2409 { 2410 struct inodedep *inodedep; 2411 struct inodedep_hashhead *inodedephd; 2412 struct ufsmount *ump; 2413 struct fs *fs; 2414 2415 ump = VFSTOUFS(mp); 2416 LOCK_OWNED(ump); 2417 fs = ump->um_fs; 2418 inodedephd = INODEDEP_HASH(ump, inum); 2419 2420 if (inodedep_find(inodedephd, inum, inodedeppp)) 2421 return (1); 2422 if ((flags & DEPALLOC) == 0) 2423 return (0); 2424 /* 2425 * If the system is over its limit and our filesystem is 2426 * responsible for more than our share of that usage and 2427 * we are not in a rush, request some inodedep cleanup. 2428 */ 2429 if (softdep_excess_items(ump, D_INODEDEP)) 2430 schedule_cleanup(mp); 2431 else 2432 FREE_LOCK(ump); 2433 inodedep = malloc(sizeof(struct inodedep), 2434 M_INODEDEP, M_SOFTDEP_FLAGS); 2435 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp); 2436 ACQUIRE_LOCK(ump); 2437 if (inodedep_find(inodedephd, inum, inodedeppp)) { 2438 WORKITEM_FREE(inodedep, D_INODEDEP); 2439 return (1); 2440 } 2441 inodedep->id_fs = fs; 2442 inodedep->id_ino = inum; 2443 inodedep->id_state = ALLCOMPLETE; 2444 inodedep->id_nlinkdelta = 0; 2445 inodedep->id_nlinkwrote = -1; 2446 inodedep->id_savedino1 = NULL; 2447 inodedep->id_savedsize = -1; 2448 inodedep->id_savedextsize = -1; 2449 inodedep->id_savednlink = -1; 2450 inodedep->id_bmsafemap = NULL; 2451 inodedep->id_mkdiradd = NULL; 2452 LIST_INIT(&inodedep->id_dirremhd); 2453 LIST_INIT(&inodedep->id_pendinghd); 2454 LIST_INIT(&inodedep->id_inowait); 2455 LIST_INIT(&inodedep->id_bufwait); 2456 TAILQ_INIT(&inodedep->id_inoreflst); 2457 TAILQ_INIT(&inodedep->id_inoupdt); 2458 TAILQ_INIT(&inodedep->id_newinoupdt); 2459 TAILQ_INIT(&inodedep->id_extupdt); 2460 TAILQ_INIT(&inodedep->id_newextupdt); 2461 TAILQ_INIT(&inodedep->id_freeblklst); 2462 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash); 2463 *inodedeppp = inodedep; 2464 return (0); 2465 } 2466 2467 /* 2468 * Structures and routines associated with newblk caching. 2469 */ 2470 #define NEWBLK_HASH(ump, inum) \ 2471 (&(ump)->newblk_hashtbl[(inum) & (ump)->newblk_hash_size]) 2472 2473 static int 2474 newblk_find(newblkhd, newblkno, flags, newblkpp) 2475 struct newblk_hashhead *newblkhd; 2476 ufs2_daddr_t newblkno; 2477 int flags; 2478 struct newblk **newblkpp; 2479 { 2480 struct newblk *newblk; 2481 2482 LIST_FOREACH(newblk, newblkhd, nb_hash) { 2483 if (newblkno != newblk->nb_newblkno) 2484 continue; 2485 /* 2486 * If we're creating a new dependency don't match those that 2487 * have already been converted to allocdirects. This is for 2488 * a frag extend. 2489 */ 2490 if ((flags & DEPALLOC) && newblk->nb_list.wk_type != D_NEWBLK) 2491 continue; 2492 break; 2493 } 2494 if (newblk) { 2495 *newblkpp = newblk; 2496 return (1); 2497 } 2498 *newblkpp = NULL; 2499 return (0); 2500 } 2501 2502 /* 2503 * Look up a newblk. Return 1 if found, 0 if not found. 2504 * If not found, allocate if DEPALLOC flag is passed. 2505 * Found or allocated entry is returned in newblkpp. 2506 */ 2507 static int 2508 newblk_lookup(mp, newblkno, flags, newblkpp) 2509 struct mount *mp; 2510 ufs2_daddr_t newblkno; 2511 int flags; 2512 struct newblk **newblkpp; 2513 { 2514 struct newblk *newblk; 2515 struct newblk_hashhead *newblkhd; 2516 struct ufsmount *ump; 2517 2518 ump = VFSTOUFS(mp); 2519 LOCK_OWNED(ump); 2520 newblkhd = NEWBLK_HASH(ump, newblkno); 2521 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) 2522 return (1); 2523 if ((flags & DEPALLOC) == 0) 2524 return (0); 2525 if (softdep_excess_items(ump, D_NEWBLK) || 2526 softdep_excess_items(ump, D_ALLOCDIRECT) || 2527 softdep_excess_items(ump, D_ALLOCINDIR)) 2528 schedule_cleanup(mp); 2529 else 2530 FREE_LOCK(ump); 2531 newblk = malloc(sizeof(union allblk), M_NEWBLK, 2532 M_SOFTDEP_FLAGS | M_ZERO); 2533 workitem_alloc(&newblk->nb_list, D_NEWBLK, mp); 2534 ACQUIRE_LOCK(ump); 2535 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) { 2536 WORKITEM_FREE(newblk, D_NEWBLK); 2537 return (1); 2538 } 2539 newblk->nb_freefrag = NULL; 2540 LIST_INIT(&newblk->nb_indirdeps); 2541 LIST_INIT(&newblk->nb_newdirblk); 2542 LIST_INIT(&newblk->nb_jwork); 2543 newblk->nb_state = ATTACHED; 2544 newblk->nb_newblkno = newblkno; 2545 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash); 2546 *newblkpp = newblk; 2547 return (0); 2548 } 2549 2550 /* 2551 * Structures and routines associated with freed indirect block caching. 2552 */ 2553 #define INDIR_HASH(ump, blkno) \ 2554 (&(ump)->indir_hashtbl[(blkno) & (ump)->indir_hash_size]) 2555 2556 /* 2557 * Lookup an indirect block in the indir hash table. The freework is 2558 * removed and potentially freed. The caller must do a blocking journal 2559 * write before writing to the blkno. 2560 */ 2561 static int 2562 indirblk_lookup(mp, blkno) 2563 struct mount *mp; 2564 ufs2_daddr_t blkno; 2565 { 2566 struct freework *freework; 2567 struct indir_hashhead *wkhd; 2568 struct ufsmount *ump; 2569 2570 ump = VFSTOUFS(mp); 2571 wkhd = INDIR_HASH(ump, blkno); 2572 TAILQ_FOREACH(freework, wkhd, fw_next) { 2573 if (freework->fw_blkno != blkno) 2574 continue; 2575 indirblk_remove(freework); 2576 return (1); 2577 } 2578 return (0); 2579 } 2580 2581 /* 2582 * Insert an indirect block represented by freework into the indirblk 2583 * hash table so that it may prevent the block from being re-used prior 2584 * to the journal being written. 2585 */ 2586 static void 2587 indirblk_insert(freework) 2588 struct freework *freework; 2589 { 2590 struct jblocks *jblocks; 2591 struct jseg *jseg; 2592 struct ufsmount *ump; 2593 2594 ump = VFSTOUFS(freework->fw_list.wk_mp); 2595 jblocks = ump->softdep_jblocks; 2596 jseg = TAILQ_LAST(&jblocks->jb_segs, jseglst); 2597 if (jseg == NULL) 2598 return; 2599 2600 LIST_INSERT_HEAD(&jseg->js_indirs, freework, fw_segs); 2601 TAILQ_INSERT_HEAD(INDIR_HASH(ump, freework->fw_blkno), freework, 2602 fw_next); 2603 freework->fw_state &= ~DEPCOMPLETE; 2604 } 2605 2606 static void 2607 indirblk_remove(freework) 2608 struct freework *freework; 2609 { 2610 struct ufsmount *ump; 2611 2612 ump = VFSTOUFS(freework->fw_list.wk_mp); 2613 LIST_REMOVE(freework, fw_segs); 2614 TAILQ_REMOVE(INDIR_HASH(ump, freework->fw_blkno), freework, fw_next); 2615 freework->fw_state |= DEPCOMPLETE; 2616 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 2617 WORKITEM_FREE(freework, D_FREEWORK); 2618 } 2619 2620 /* 2621 * Executed during filesystem system initialization before 2622 * mounting any filesystems. 2623 */ 2624 void 2625 softdep_initialize() 2626 { 2627 2628 TAILQ_INIT(&softdepmounts); 2629 #ifdef __LP64__ 2630 max_softdeps = desiredvnodes * 4; 2631 #else 2632 max_softdeps = desiredvnodes * 2; 2633 #endif 2634 2635 /* initialise bioops hack */ 2636 bioops.io_start = softdep_disk_io_initiation; 2637 bioops.io_complete = softdep_disk_write_complete; 2638 bioops.io_deallocate = softdep_deallocate_dependencies; 2639 bioops.io_countdeps = softdep_count_dependencies; 2640 softdep_ast_cleanup = softdep_ast_cleanup_proc; 2641 2642 /* Initialize the callout with an mtx. */ 2643 callout_init_mtx(&softdep_callout, &lk, 0); 2644 } 2645 2646 /* 2647 * Executed after all filesystems have been unmounted during 2648 * filesystem module unload. 2649 */ 2650 void 2651 softdep_uninitialize() 2652 { 2653 2654 /* clear bioops hack */ 2655 bioops.io_start = NULL; 2656 bioops.io_complete = NULL; 2657 bioops.io_deallocate = NULL; 2658 bioops.io_countdeps = NULL; 2659 softdep_ast_cleanup = NULL; 2660 2661 callout_drain(&softdep_callout); 2662 } 2663 2664 /* 2665 * Called at mount time to notify the dependency code that a 2666 * filesystem wishes to use it. 2667 */ 2668 int 2669 softdep_mount(devvp, mp, fs, cred) 2670 struct vnode *devvp; 2671 struct mount *mp; 2672 struct fs *fs; 2673 struct ucred *cred; 2674 { 2675 struct csum_total cstotal; 2676 struct mount_softdeps *sdp; 2677 struct ufsmount *ump; 2678 struct cg *cgp; 2679 struct buf *bp; 2680 u_int cyl, i; 2681 int error; 2682 2683 ump = VFSTOUFS(mp); 2684 2685 sdp = malloc(sizeof(struct mount_softdeps), M_MOUNTDATA, 2686 M_WAITOK | M_ZERO); 2687 rw_init(&sdp->sd_fslock, "SUrw"); 2688 sdp->sd_ump = ump; 2689 LIST_INIT(&sdp->sd_workitem_pending); 2690 LIST_INIT(&sdp->sd_journal_pending); 2691 TAILQ_INIT(&sdp->sd_unlinked); 2692 LIST_INIT(&sdp->sd_dirtycg); 2693 sdp->sd_worklist_tail = NULL; 2694 sdp->sd_on_worklist = 0; 2695 sdp->sd_deps = 0; 2696 LIST_INIT(&sdp->sd_mkdirlisthd); 2697 sdp->sd_pdhash = hashinit(desiredvnodes / 5, M_PAGEDEP, 2698 &sdp->sd_pdhashsize); 2699 sdp->sd_pdnextclean = 0; 2700 sdp->sd_idhash = hashinit(desiredvnodes, M_INODEDEP, 2701 &sdp->sd_idhashsize); 2702 sdp->sd_idnextclean = 0; 2703 sdp->sd_newblkhash = hashinit(max_softdeps / 2, M_NEWBLK, 2704 &sdp->sd_newblkhashsize); 2705 sdp->sd_bmhash = hashinit(1024, M_BMSAFEMAP, &sdp->sd_bmhashsize); 2706 i = 1 << (ffs(desiredvnodes / 10) - 1); 2707 sdp->sd_indirhash = malloc(i * sizeof(struct indir_hashhead), 2708 M_FREEWORK, M_WAITOK); 2709 sdp->sd_indirhashsize = i - 1; 2710 for (i = 0; i <= sdp->sd_indirhashsize; i++) 2711 TAILQ_INIT(&sdp->sd_indirhash[i]); 2712 #ifdef INVARIANTS 2713 for (i = 0; i <= D_LAST; i++) 2714 LIST_INIT(&sdp->sd_alldeps[i]); 2715 #endif 2716 ACQUIRE_GBLLOCK(&lk); 2717 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next); 2718 FREE_GBLLOCK(&lk); 2719 2720 ump->um_softdep = sdp; 2721 MNT_ILOCK(mp); 2722 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP; 2723 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) { 2724 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) | 2725 MNTK_SOFTDEP | MNTK_NOASYNC; 2726 } 2727 MNT_IUNLOCK(mp); 2728 2729 if ((fs->fs_flags & FS_SUJ) && 2730 (error = journal_mount(mp, fs, cred)) != 0) { 2731 printf("Failed to start journal: %d\n", error); 2732 softdep_unmount(mp); 2733 return (error); 2734 } 2735 /* 2736 * Start our flushing thread in the bufdaemon process. 2737 */ 2738 ACQUIRE_LOCK(ump); 2739 ump->softdep_flags |= FLUSH_STARTING; 2740 FREE_LOCK(ump); 2741 kproc_kthread_add(&softdep_flush, mp, &bufdaemonproc, 2742 &ump->softdep_flushtd, 0, 0, "softdepflush", "%s worker", 2743 mp->mnt_stat.f_mntonname); 2744 ACQUIRE_LOCK(ump); 2745 while ((ump->softdep_flags & FLUSH_STARTING) != 0) { 2746 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, "sdstart", 2747 hz / 2); 2748 } 2749 FREE_LOCK(ump); 2750 /* 2751 * When doing soft updates, the counters in the 2752 * superblock may have gotten out of sync. Recomputation 2753 * can take a long time and can be deferred for background 2754 * fsck. However, the old behavior of scanning the cylinder 2755 * groups and recalculating them at mount time is available 2756 * by setting vfs.ffs.compute_summary_at_mount to one. 2757 */ 2758 if (compute_summary_at_mount == 0 || fs->fs_clean != 0) 2759 return (0); 2760 bzero(&cstotal, sizeof cstotal); 2761 for (cyl = 0; cyl < fs->fs_ncg; cyl++) { 2762 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)), 2763 fs->fs_cgsize, cred, &bp)) != 0) { 2764 brelse(bp); 2765 softdep_unmount(mp); 2766 return (error); 2767 } 2768 cgp = (struct cg *)bp->b_data; 2769 cstotal.cs_nffree += cgp->cg_cs.cs_nffree; 2770 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree; 2771 cstotal.cs_nifree += cgp->cg_cs.cs_nifree; 2772 cstotal.cs_ndir += cgp->cg_cs.cs_ndir; 2773 fs->fs_cs(fs, cyl) = cgp->cg_cs; 2774 brelse(bp); 2775 } 2776 #ifdef INVARIANTS 2777 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal)) 2778 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt); 2779 #endif 2780 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal); 2781 return (0); 2782 } 2783 2784 void 2785 softdep_unmount(mp) 2786 struct mount *mp; 2787 { 2788 struct ufsmount *ump; 2789 struct mount_softdeps *ums; 2790 2791 ump = VFSTOUFS(mp); 2792 KASSERT(ump->um_softdep != NULL, 2793 ("softdep_unmount called on non-softdep filesystem")); 2794 MNT_ILOCK(mp); 2795 mp->mnt_flag &= ~MNT_SOFTDEP; 2796 if ((mp->mnt_flag & MNT_SUJ) == 0) { 2797 MNT_IUNLOCK(mp); 2798 } else { 2799 mp->mnt_flag &= ~MNT_SUJ; 2800 MNT_IUNLOCK(mp); 2801 journal_unmount(ump); 2802 } 2803 /* 2804 * Shut down our flushing thread. Check for NULL is if 2805 * softdep_mount errors out before the thread has been created. 2806 */ 2807 if (ump->softdep_flushtd != NULL) { 2808 ACQUIRE_LOCK(ump); 2809 ump->softdep_flags |= FLUSH_EXIT; 2810 wakeup(&ump->softdep_flushtd); 2811 while ((ump->softdep_flags & FLUSH_EXIT) != 0) { 2812 msleep(&ump->softdep_flags, LOCK_PTR(ump), PVM, 2813 "sdwait", 0); 2814 } 2815 KASSERT((ump->softdep_flags & FLUSH_EXIT) == 0, 2816 ("Thread shutdown failed")); 2817 FREE_LOCK(ump); 2818 } 2819 2820 /* 2821 * We are no longer have softdep structure attached to ump. 2822 */ 2823 ums = ump->um_softdep; 2824 ACQUIRE_GBLLOCK(&lk); 2825 TAILQ_REMOVE(&softdepmounts, ums, sd_next); 2826 FREE_GBLLOCK(&lk); 2827 ump->um_softdep = NULL; 2828 2829 KASSERT(ums->sd_on_journal == 0, 2830 ("ump %p ums %p on_journal %d", ump, ums, ums->sd_on_journal)); 2831 KASSERT(ums->sd_on_worklist == 0, 2832 ("ump %p ums %p on_worklist %d", ump, ums, ums->sd_on_worklist)); 2833 KASSERT(ums->sd_deps == 0, 2834 ("ump %p ums %p deps %d", ump, ums, ums->sd_deps)); 2835 2836 /* 2837 * Free up our resources. 2838 */ 2839 rw_destroy(&ums->sd_fslock); 2840 hashdestroy(ums->sd_pdhash, M_PAGEDEP, ums->sd_pdhashsize); 2841 hashdestroy(ums->sd_idhash, M_INODEDEP, ums->sd_idhashsize); 2842 hashdestroy(ums->sd_newblkhash, M_NEWBLK, ums->sd_newblkhashsize); 2843 hashdestroy(ums->sd_bmhash, M_BMSAFEMAP, ums->sd_bmhashsize); 2844 free(ums->sd_indirhash, M_FREEWORK); 2845 #ifdef INVARIANTS 2846 for (int i = 0; i <= D_LAST; i++) { 2847 KASSERT(ums->sd_curdeps[i] == 0, 2848 ("Unmount %s: Dep type %s != 0 (%ld)", ump->um_fs->fs_fsmnt, 2849 TYPENAME(i), ums->sd_curdeps[i])); 2850 KASSERT(LIST_EMPTY(&ums->sd_alldeps[i]), 2851 ("Unmount %s: Dep type %s not empty (%p)", 2852 ump->um_fs->fs_fsmnt, 2853 TYPENAME(i), LIST_FIRST(&ums->sd_alldeps[i]))); 2854 } 2855 #endif 2856 free(ums, M_MOUNTDATA); 2857 } 2858 2859 static struct jblocks * 2860 jblocks_create(void) 2861 { 2862 struct jblocks *jblocks; 2863 2864 jblocks = malloc(sizeof(*jblocks), M_JBLOCKS, M_WAITOK | M_ZERO); 2865 TAILQ_INIT(&jblocks->jb_segs); 2866 jblocks->jb_avail = 10; 2867 jblocks->jb_extent = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2868 M_JBLOCKS, M_WAITOK | M_ZERO); 2869 2870 return (jblocks); 2871 } 2872 2873 static ufs2_daddr_t 2874 jblocks_alloc(jblocks, bytes, actual) 2875 struct jblocks *jblocks; 2876 int bytes; 2877 int *actual; 2878 { 2879 ufs2_daddr_t daddr; 2880 struct jextent *jext; 2881 int freecnt; 2882 int blocks; 2883 2884 blocks = bytes / DEV_BSIZE; 2885 jext = &jblocks->jb_extent[jblocks->jb_head]; 2886 freecnt = jext->je_blocks - jblocks->jb_off; 2887 if (freecnt == 0) { 2888 jblocks->jb_off = 0; 2889 if (++jblocks->jb_head > jblocks->jb_used) 2890 jblocks->jb_head = 0; 2891 jext = &jblocks->jb_extent[jblocks->jb_head]; 2892 freecnt = jext->je_blocks; 2893 } 2894 if (freecnt > blocks) 2895 freecnt = blocks; 2896 *actual = freecnt * DEV_BSIZE; 2897 daddr = jext->je_daddr + jblocks->jb_off; 2898 jblocks->jb_off += freecnt; 2899 jblocks->jb_free -= freecnt; 2900 2901 return (daddr); 2902 } 2903 2904 static void 2905 jblocks_free(jblocks, mp, bytes) 2906 struct jblocks *jblocks; 2907 struct mount *mp; 2908 int bytes; 2909 { 2910 2911 LOCK_OWNED(VFSTOUFS(mp)); 2912 jblocks->jb_free += bytes / DEV_BSIZE; 2913 if (jblocks->jb_suspended) 2914 worklist_speedup(mp); 2915 wakeup(jblocks); 2916 } 2917 2918 static void 2919 jblocks_destroy(jblocks) 2920 struct jblocks *jblocks; 2921 { 2922 2923 if (jblocks->jb_extent) 2924 free(jblocks->jb_extent, M_JBLOCKS); 2925 free(jblocks, M_JBLOCKS); 2926 } 2927 2928 static void 2929 jblocks_add(jblocks, daddr, blocks) 2930 struct jblocks *jblocks; 2931 ufs2_daddr_t daddr; 2932 int blocks; 2933 { 2934 struct jextent *jext; 2935 2936 jblocks->jb_blocks += blocks; 2937 jblocks->jb_free += blocks; 2938 jext = &jblocks->jb_extent[jblocks->jb_used]; 2939 /* Adding the first block. */ 2940 if (jext->je_daddr == 0) { 2941 jext->je_daddr = daddr; 2942 jext->je_blocks = blocks; 2943 return; 2944 } 2945 /* Extending the last extent. */ 2946 if (jext->je_daddr + jext->je_blocks == daddr) { 2947 jext->je_blocks += blocks; 2948 return; 2949 } 2950 /* Adding a new extent. */ 2951 if (++jblocks->jb_used == jblocks->jb_avail) { 2952 jblocks->jb_avail *= 2; 2953 jext = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2954 M_JBLOCKS, M_WAITOK | M_ZERO); 2955 memcpy(jext, jblocks->jb_extent, 2956 sizeof(struct jextent) * jblocks->jb_used); 2957 free(jblocks->jb_extent, M_JBLOCKS); 2958 jblocks->jb_extent = jext; 2959 } 2960 jext = &jblocks->jb_extent[jblocks->jb_used]; 2961 jext->je_daddr = daddr; 2962 jext->je_blocks = blocks; 2963 return; 2964 } 2965 2966 int 2967 softdep_journal_lookup(mp, vpp) 2968 struct mount *mp; 2969 struct vnode **vpp; 2970 { 2971 struct componentname cnp; 2972 struct vnode *dvp; 2973 ino_t sujournal; 2974 int error; 2975 2976 error = VFS_VGET(mp, UFS_ROOTINO, LK_EXCLUSIVE, &dvp); 2977 if (error) 2978 return (error); 2979 bzero(&cnp, sizeof(cnp)); 2980 cnp.cn_nameiop = LOOKUP; 2981 cnp.cn_flags = ISLASTCN; 2982 cnp.cn_thread = curthread; 2983 cnp.cn_cred = curthread->td_ucred; 2984 cnp.cn_pnbuf = SUJ_FILE; 2985 cnp.cn_nameptr = SUJ_FILE; 2986 cnp.cn_namelen = strlen(SUJ_FILE); 2987 error = ufs_lookup_ino(dvp, NULL, &cnp, &sujournal); 2988 vput(dvp); 2989 if (error != 0) 2990 return (error); 2991 error = VFS_VGET(mp, sujournal, LK_EXCLUSIVE, vpp); 2992 return (error); 2993 } 2994 2995 /* 2996 * Open and verify the journal file. 2997 */ 2998 static int 2999 journal_mount(mp, fs, cred) 3000 struct mount *mp; 3001 struct fs *fs; 3002 struct ucred *cred; 3003 { 3004 struct jblocks *jblocks; 3005 struct ufsmount *ump; 3006 struct vnode *vp; 3007 struct inode *ip; 3008 ufs2_daddr_t blkno; 3009 int bcount; 3010 int error; 3011 int i; 3012 3013 ump = VFSTOUFS(mp); 3014 ump->softdep_journal_tail = NULL; 3015 ump->softdep_on_journal = 0; 3016 ump->softdep_accdeps = 0; 3017 ump->softdep_req = 0; 3018 ump->softdep_jblocks = NULL; 3019 error = softdep_journal_lookup(mp, &vp); 3020 if (error != 0) { 3021 printf("Failed to find journal. Use tunefs to create one\n"); 3022 return (error); 3023 } 3024 ip = VTOI(vp); 3025 if (ip->i_size < SUJ_MIN) { 3026 error = ENOSPC; 3027 goto out; 3028 } 3029 bcount = lblkno(fs, ip->i_size); /* Only use whole blocks. */ 3030 jblocks = jblocks_create(); 3031 for (i = 0; i < bcount; i++) { 3032 error = ufs_bmaparray(vp, i, &blkno, NULL, NULL, NULL); 3033 if (error) 3034 break; 3035 jblocks_add(jblocks, blkno, fsbtodb(fs, fs->fs_frag)); 3036 } 3037 if (error) { 3038 jblocks_destroy(jblocks); 3039 goto out; 3040 } 3041 jblocks->jb_low = jblocks->jb_free / 3; /* Reserve 33%. */ 3042 jblocks->jb_min = jblocks->jb_free / 10; /* Suspend at 10%. */ 3043 ump->softdep_jblocks = jblocks; 3044 3045 MNT_ILOCK(mp); 3046 mp->mnt_flag |= MNT_SUJ; 3047 MNT_IUNLOCK(mp); 3048 3049 /* 3050 * Only validate the journal contents if the 3051 * filesystem is clean, otherwise we write the logs 3052 * but they'll never be used. If the filesystem was 3053 * still dirty when we mounted it the journal is 3054 * invalid and a new journal can only be valid if it 3055 * starts from a clean mount. 3056 */ 3057 if (fs->fs_clean) { 3058 DIP_SET(ip, i_modrev, fs->fs_mtime); 3059 ip->i_flags |= IN_MODIFIED; 3060 ffs_update(vp, 1); 3061 } 3062 out: 3063 vput(vp); 3064 return (error); 3065 } 3066 3067 static void 3068 journal_unmount(ump) 3069 struct ufsmount *ump; 3070 { 3071 3072 if (ump->softdep_jblocks) 3073 jblocks_destroy(ump->softdep_jblocks); 3074 ump->softdep_jblocks = NULL; 3075 } 3076 3077 /* 3078 * Called when a journal record is ready to be written. Space is allocated 3079 * and the journal entry is created when the journal is flushed to stable 3080 * store. 3081 */ 3082 static void 3083 add_to_journal(wk) 3084 struct worklist *wk; 3085 { 3086 struct ufsmount *ump; 3087 3088 ump = VFSTOUFS(wk->wk_mp); 3089 LOCK_OWNED(ump); 3090 if (wk->wk_state & ONWORKLIST) 3091 panic("add_to_journal: %s(0x%X) already on list", 3092 TYPENAME(wk->wk_type), wk->wk_state); 3093 wk->wk_state |= ONWORKLIST | DEPCOMPLETE; 3094 if (LIST_EMPTY(&ump->softdep_journal_pending)) { 3095 ump->softdep_jblocks->jb_age = ticks; 3096 LIST_INSERT_HEAD(&ump->softdep_journal_pending, wk, wk_list); 3097 } else 3098 LIST_INSERT_AFTER(ump->softdep_journal_tail, wk, wk_list); 3099 ump->softdep_journal_tail = wk; 3100 ump->softdep_on_journal += 1; 3101 } 3102 3103 /* 3104 * Remove an arbitrary item for the journal worklist maintain the tail 3105 * pointer. This happens when a new operation obviates the need to 3106 * journal an old operation. 3107 */ 3108 static void 3109 remove_from_journal(wk) 3110 struct worklist *wk; 3111 { 3112 struct ufsmount *ump; 3113 3114 ump = VFSTOUFS(wk->wk_mp); 3115 LOCK_OWNED(ump); 3116 #ifdef INVARIANTS 3117 { 3118 struct worklist *wkn; 3119 3120 LIST_FOREACH(wkn, &ump->softdep_journal_pending, wk_list) 3121 if (wkn == wk) 3122 break; 3123 if (wkn == NULL) 3124 panic("remove_from_journal: %p is not in journal", wk); 3125 } 3126 #endif 3127 /* 3128 * We emulate a TAILQ to save space in most structures which do not 3129 * require TAILQ semantics. Here we must update the tail position 3130 * when removing the tail which is not the final entry. This works 3131 * only if the worklist linkage are at the beginning of the structure. 3132 */ 3133 if (ump->softdep_journal_tail == wk) 3134 ump->softdep_journal_tail = 3135 (struct worklist *)wk->wk_list.le_prev; 3136 WORKLIST_REMOVE(wk); 3137 ump->softdep_on_journal -= 1; 3138 } 3139 3140 /* 3141 * Check for journal space as well as dependency limits so the prelink 3142 * code can throttle both journaled and non-journaled filesystems. 3143 * Threshold is 0 for low and 1 for min. 3144 */ 3145 static int 3146 journal_space(ump, thresh) 3147 struct ufsmount *ump; 3148 int thresh; 3149 { 3150 struct jblocks *jblocks; 3151 int limit, avail; 3152 3153 jblocks = ump->softdep_jblocks; 3154 if (jblocks == NULL) 3155 return (1); 3156 /* 3157 * We use a tighter restriction here to prevent request_cleanup() 3158 * running in threads from running into locks we currently hold. 3159 * We have to be over the limit and our filesystem has to be 3160 * responsible for more than our share of that usage. 3161 */ 3162 limit = (max_softdeps / 10) * 9; 3163 if (dep_current[D_INODEDEP] > limit && 3164 ump->softdep_curdeps[D_INODEDEP] > limit / stat_flush_threads) 3165 return (0); 3166 if (thresh) 3167 thresh = jblocks->jb_min; 3168 else 3169 thresh = jblocks->jb_low; 3170 avail = (ump->softdep_on_journal * JREC_SIZE) / DEV_BSIZE; 3171 avail = jblocks->jb_free - avail; 3172 3173 return (avail > thresh); 3174 } 3175 3176 static void 3177 journal_suspend(ump) 3178 struct ufsmount *ump; 3179 { 3180 struct jblocks *jblocks; 3181 struct mount *mp; 3182 bool set; 3183 3184 mp = UFSTOVFS(ump); 3185 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) 3186 return; 3187 3188 jblocks = ump->softdep_jblocks; 3189 vfs_op_enter(mp); 3190 set = false; 3191 MNT_ILOCK(mp); 3192 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) { 3193 stat_journal_min++; 3194 mp->mnt_kern_flag |= MNTK_SUSPEND; 3195 mp->mnt_susp_owner = ump->softdep_flushtd; 3196 set = true; 3197 } 3198 jblocks->jb_suspended = 1; 3199 MNT_IUNLOCK(mp); 3200 if (!set) 3201 vfs_op_exit(mp); 3202 } 3203 3204 static int 3205 journal_unsuspend(struct ufsmount *ump) 3206 { 3207 struct jblocks *jblocks; 3208 struct mount *mp; 3209 3210 mp = UFSTOVFS(ump); 3211 jblocks = ump->softdep_jblocks; 3212 3213 if (jblocks != NULL && jblocks->jb_suspended && 3214 journal_space(ump, jblocks->jb_min)) { 3215 jblocks->jb_suspended = 0; 3216 FREE_LOCK(ump); 3217 mp->mnt_susp_owner = curthread; 3218 vfs_write_resume(mp, 0); 3219 ACQUIRE_LOCK(ump); 3220 return (1); 3221 } 3222 return (0); 3223 } 3224 3225 static void 3226 journal_check_space(ump) 3227 struct ufsmount *ump; 3228 { 3229 struct mount *mp; 3230 3231 LOCK_OWNED(ump); 3232 3233 if (journal_space(ump, 0) == 0) { 3234 softdep_speedup(ump); 3235 mp = UFSTOVFS(ump); 3236 FREE_LOCK(ump); 3237 VFS_SYNC(mp, MNT_NOWAIT); 3238 ffs_sbupdate(ump, MNT_WAIT, 0); 3239 ACQUIRE_LOCK(ump); 3240 if (journal_space(ump, 1) == 0) 3241 journal_suspend(ump); 3242 } 3243 } 3244 3245 /* 3246 * Called before any allocation function to be certain that there is 3247 * sufficient space in the journal prior to creating any new records. 3248 * Since in the case of block allocation we may have multiple locked 3249 * buffers at the time of the actual allocation we can not block 3250 * when the journal records are created. Doing so would create a deadlock 3251 * if any of these buffers needed to be flushed to reclaim space. Instead 3252 * we require a sufficiently large amount of available space such that 3253 * each thread in the system could have passed this allocation check and 3254 * still have sufficient free space. With 20% of a minimum journal size 3255 * of 1MB we have 6553 records available. 3256 */ 3257 int 3258 softdep_prealloc(vp, waitok) 3259 struct vnode *vp; 3260 int waitok; 3261 { 3262 struct ufsmount *ump; 3263 3264 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 3265 ("softdep_prealloc called on non-softdep filesystem")); 3266 /* 3267 * Nothing to do if we are not running journaled soft updates. 3268 * If we currently hold the snapshot lock, we must avoid 3269 * handling other resources that could cause deadlock. Do not 3270 * touch quotas vnode since it is typically recursed with 3271 * other vnode locks held. 3272 */ 3273 if (DOINGSUJ(vp) == 0 || IS_SNAPSHOT(VTOI(vp)) || 3274 (vp->v_vflag & VV_SYSTEM) != 0) 3275 return (0); 3276 ump = VFSTOUFS(vp->v_mount); 3277 ACQUIRE_LOCK(ump); 3278 if (journal_space(ump, 0)) { 3279 FREE_LOCK(ump); 3280 return (0); 3281 } 3282 stat_journal_low++; 3283 FREE_LOCK(ump); 3284 if (waitok == MNT_NOWAIT) 3285 return (ENOSPC); 3286 /* 3287 * Attempt to sync this vnode once to flush any journal 3288 * work attached to it. 3289 */ 3290 if ((curthread->td_pflags & TDP_COWINPROGRESS) == 0) 3291 ffs_syncvnode(vp, waitok, 0); 3292 ACQUIRE_LOCK(ump); 3293 process_removes(vp); 3294 process_truncates(vp); 3295 journal_check_space(ump); 3296 FREE_LOCK(ump); 3297 3298 return (0); 3299 } 3300 3301 /* 3302 * Try hard to sync all data and metadata for the vnode, and workitems 3303 * flushing which might conflict with the vnode lock. This is a 3304 * helper for softdep_prerename(). 3305 */ 3306 static int 3307 softdep_prerename_vnode(ump, vp) 3308 struct ufsmount *ump; 3309 struct vnode *vp; 3310 { 3311 int error; 3312 3313 ASSERT_VOP_ELOCKED(vp, "prehandle"); 3314 if (vp->v_data == NULL) 3315 return (0); 3316 error = VOP_FSYNC(vp, MNT_WAIT, curthread); 3317 if (error != 0) 3318 return (error); 3319 ACQUIRE_LOCK(ump); 3320 process_removes(vp); 3321 process_truncates(vp); 3322 FREE_LOCK(ump); 3323 return (0); 3324 } 3325 3326 /* 3327 * Must be called from VOP_RENAME() after all vnodes are locked. 3328 * Ensures that there is enough journal space for rename. It is 3329 * sufficiently different from softdep_prelink() by having to handle 3330 * four vnodes. 3331 */ 3332 int 3333 softdep_prerename(fdvp, fvp, tdvp, tvp) 3334 struct vnode *fdvp; 3335 struct vnode *fvp; 3336 struct vnode *tdvp; 3337 struct vnode *tvp; 3338 { 3339 struct ufsmount *ump; 3340 int error; 3341 3342 ump = VFSTOUFS(fdvp->v_mount); 3343 3344 if (journal_space(ump, 0)) 3345 return (0); 3346 3347 VOP_UNLOCK(tdvp); 3348 VOP_UNLOCK(fvp); 3349 if (tvp != NULL && tvp != tdvp) 3350 VOP_UNLOCK(tvp); 3351 3352 error = softdep_prerename_vnode(ump, fdvp); 3353 VOP_UNLOCK(fdvp); 3354 if (error != 0) 3355 return (error); 3356 3357 VOP_LOCK(fvp, LK_EXCLUSIVE | LK_RETRY); 3358 error = softdep_prerename_vnode(ump, fvp); 3359 VOP_UNLOCK(fvp); 3360 if (error != 0) 3361 return (error); 3362 3363 if (tdvp != fdvp) { 3364 VOP_LOCK(tdvp, LK_EXCLUSIVE | LK_RETRY); 3365 error = softdep_prerename_vnode(ump, tdvp); 3366 VOP_UNLOCK(tdvp); 3367 if (error != 0) 3368 return (error); 3369 } 3370 3371 if (tvp != fvp && tvp != NULL) { 3372 VOP_LOCK(tvp, LK_EXCLUSIVE | LK_RETRY); 3373 error = softdep_prerename_vnode(ump, tvp); 3374 VOP_UNLOCK(tvp); 3375 if (error != 0) 3376 return (error); 3377 } 3378 3379 ACQUIRE_LOCK(ump); 3380 softdep_speedup(ump); 3381 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT); 3382 journal_check_space(ump); 3383 FREE_LOCK(ump); 3384 return (ERELOOKUP); 3385 } 3386 3387 /* 3388 * Before adjusting a link count on a vnode verify that we have sufficient 3389 * journal space. If not, process operations that depend on the currently 3390 * locked pair of vnodes to try to flush space as the syncer, buf daemon, 3391 * and softdep flush threads can not acquire these locks to reclaim space. 3392 * 3393 * Returns 0 if all owned locks are still valid and were not dropped 3394 * in the process, in other case it returns either an error from sync, 3395 * or ERELOOKUP if any of the locks were re-acquired. In the later 3396 * case, the state of the vnodes cannot be relied upon and our VFS 3397 * syscall must be restarted at top level from the lookup. 3398 */ 3399 int 3400 softdep_prelink(dvp, vp, cnp) 3401 struct vnode *dvp; 3402 struct vnode *vp; 3403 struct componentname *cnp; 3404 { 3405 struct ufsmount *ump; 3406 struct nameidata *ndp; 3407 3408 ASSERT_VOP_ELOCKED(dvp, "prelink dvp"); 3409 if (vp != NULL) 3410 ASSERT_VOP_ELOCKED(vp, "prelink vp"); 3411 ump = VFSTOUFS(dvp->v_mount); 3412 3413 /* 3414 * Nothing to do if we have sufficient journal space. We skip 3415 * flushing when vp is a snapshot to avoid deadlock where 3416 * another thread is trying to update the inodeblock for dvp 3417 * and is waiting on snaplk that vp holds. 3418 */ 3419 if (journal_space(ump, 0) || (vp != NULL && IS_SNAPSHOT(VTOI(vp)))) 3420 return (0); 3421 3422 /* 3423 * Check if the journal space consumption can in theory be 3424 * accounted on dvp and vp. If the vnodes metadata was not 3425 * changed comparing with the previous round-trip into 3426 * softdep_prelink(), as indicated by the seqc generation 3427 * recorded in the nameidata, then there is no point in 3428 * starting the sync. 3429 */ 3430 ndp = __containerof(cnp, struct nameidata, ni_cnd); 3431 if (!seqc_in_modify(ndp->ni_dvp_seqc) && 3432 vn_seqc_consistent(dvp, ndp->ni_dvp_seqc) && 3433 (vp == NULL || (!seqc_in_modify(ndp->ni_vp_seqc) && 3434 vn_seqc_consistent(vp, ndp->ni_vp_seqc)))) 3435 return (0); 3436 3437 stat_journal_low++; 3438 if (vp != NULL) { 3439 VOP_UNLOCK(dvp); 3440 ffs_syncvnode(vp, MNT_NOWAIT, 0); 3441 vn_lock_pair(dvp, false, vp, true); 3442 if (dvp->v_data == NULL) 3443 goto out; 3444 } 3445 if (vp != NULL) 3446 VOP_UNLOCK(vp); 3447 ffs_syncvnode(dvp, MNT_WAIT, 0); 3448 /* Process vp before dvp as it may create .. removes. */ 3449 if (vp != NULL) { 3450 VOP_UNLOCK(dvp); 3451 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3452 if (vp->v_data == NULL) { 3453 vn_lock_pair(dvp, false, vp, true); 3454 goto out; 3455 } 3456 ACQUIRE_LOCK(ump); 3457 process_removes(vp); 3458 process_truncates(vp); 3459 FREE_LOCK(ump); 3460 VOP_UNLOCK(vp); 3461 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 3462 if (dvp->v_data == NULL) { 3463 vn_lock_pair(dvp, true, vp, false); 3464 goto out; 3465 } 3466 } 3467 3468 ACQUIRE_LOCK(ump); 3469 process_removes(dvp); 3470 process_truncates(dvp); 3471 VOP_UNLOCK(dvp); 3472 softdep_speedup(ump); 3473 3474 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT); 3475 journal_check_space(ump); 3476 FREE_LOCK(ump); 3477 3478 vn_lock_pair(dvp, false, vp, false); 3479 out: 3480 ndp->ni_dvp_seqc = vn_seqc_read_any(dvp); 3481 if (vp != NULL) 3482 ndp->ni_vp_seqc = vn_seqc_read_any(vp); 3483 return (ERELOOKUP); 3484 } 3485 3486 static void 3487 jseg_write(ump, jseg, data) 3488 struct ufsmount *ump; 3489 struct jseg *jseg; 3490 uint8_t *data; 3491 { 3492 struct jsegrec *rec; 3493 3494 rec = (struct jsegrec *)data; 3495 rec->jsr_seq = jseg->js_seq; 3496 rec->jsr_oldest = jseg->js_oldseq; 3497 rec->jsr_cnt = jseg->js_cnt; 3498 rec->jsr_blocks = jseg->js_size / ump->um_devvp->v_bufobj.bo_bsize; 3499 rec->jsr_crc = 0; 3500 rec->jsr_time = ump->um_fs->fs_mtime; 3501 } 3502 3503 static inline void 3504 inoref_write(inoref, jseg, rec) 3505 struct inoref *inoref; 3506 struct jseg *jseg; 3507 struct jrefrec *rec; 3508 { 3509 3510 inoref->if_jsegdep->jd_seg = jseg; 3511 rec->jr_ino = inoref->if_ino; 3512 rec->jr_parent = inoref->if_parent; 3513 rec->jr_nlink = inoref->if_nlink; 3514 rec->jr_mode = inoref->if_mode; 3515 rec->jr_diroff = inoref->if_diroff; 3516 } 3517 3518 static void 3519 jaddref_write(jaddref, jseg, data) 3520 struct jaddref *jaddref; 3521 struct jseg *jseg; 3522 uint8_t *data; 3523 { 3524 struct jrefrec *rec; 3525 3526 rec = (struct jrefrec *)data; 3527 rec->jr_op = JOP_ADDREF; 3528 inoref_write(&jaddref->ja_ref, jseg, rec); 3529 } 3530 3531 static void 3532 jremref_write(jremref, jseg, data) 3533 struct jremref *jremref; 3534 struct jseg *jseg; 3535 uint8_t *data; 3536 { 3537 struct jrefrec *rec; 3538 3539 rec = (struct jrefrec *)data; 3540 rec->jr_op = JOP_REMREF; 3541 inoref_write(&jremref->jr_ref, jseg, rec); 3542 } 3543 3544 static void 3545 jmvref_write(jmvref, jseg, data) 3546 struct jmvref *jmvref; 3547 struct jseg *jseg; 3548 uint8_t *data; 3549 { 3550 struct jmvrec *rec; 3551 3552 rec = (struct jmvrec *)data; 3553 rec->jm_op = JOP_MVREF; 3554 rec->jm_ino = jmvref->jm_ino; 3555 rec->jm_parent = jmvref->jm_parent; 3556 rec->jm_oldoff = jmvref->jm_oldoff; 3557 rec->jm_newoff = jmvref->jm_newoff; 3558 } 3559 3560 static void 3561 jnewblk_write(jnewblk, jseg, data) 3562 struct jnewblk *jnewblk; 3563 struct jseg *jseg; 3564 uint8_t *data; 3565 { 3566 struct jblkrec *rec; 3567 3568 jnewblk->jn_jsegdep->jd_seg = jseg; 3569 rec = (struct jblkrec *)data; 3570 rec->jb_op = JOP_NEWBLK; 3571 rec->jb_ino = jnewblk->jn_ino; 3572 rec->jb_blkno = jnewblk->jn_blkno; 3573 rec->jb_lbn = jnewblk->jn_lbn; 3574 rec->jb_frags = jnewblk->jn_frags; 3575 rec->jb_oldfrags = jnewblk->jn_oldfrags; 3576 } 3577 3578 static void 3579 jfreeblk_write(jfreeblk, jseg, data) 3580 struct jfreeblk *jfreeblk; 3581 struct jseg *jseg; 3582 uint8_t *data; 3583 { 3584 struct jblkrec *rec; 3585 3586 jfreeblk->jf_dep.jb_jsegdep->jd_seg = jseg; 3587 rec = (struct jblkrec *)data; 3588 rec->jb_op = JOP_FREEBLK; 3589 rec->jb_ino = jfreeblk->jf_ino; 3590 rec->jb_blkno = jfreeblk->jf_blkno; 3591 rec->jb_lbn = jfreeblk->jf_lbn; 3592 rec->jb_frags = jfreeblk->jf_frags; 3593 rec->jb_oldfrags = 0; 3594 } 3595 3596 static void 3597 jfreefrag_write(jfreefrag, jseg, data) 3598 struct jfreefrag *jfreefrag; 3599 struct jseg *jseg; 3600 uint8_t *data; 3601 { 3602 struct jblkrec *rec; 3603 3604 jfreefrag->fr_jsegdep->jd_seg = jseg; 3605 rec = (struct jblkrec *)data; 3606 rec->jb_op = JOP_FREEBLK; 3607 rec->jb_ino = jfreefrag->fr_ino; 3608 rec->jb_blkno = jfreefrag->fr_blkno; 3609 rec->jb_lbn = jfreefrag->fr_lbn; 3610 rec->jb_frags = jfreefrag->fr_frags; 3611 rec->jb_oldfrags = 0; 3612 } 3613 3614 static void 3615 jtrunc_write(jtrunc, jseg, data) 3616 struct jtrunc *jtrunc; 3617 struct jseg *jseg; 3618 uint8_t *data; 3619 { 3620 struct jtrncrec *rec; 3621 3622 jtrunc->jt_dep.jb_jsegdep->jd_seg = jseg; 3623 rec = (struct jtrncrec *)data; 3624 rec->jt_op = JOP_TRUNC; 3625 rec->jt_ino = jtrunc->jt_ino; 3626 rec->jt_size = jtrunc->jt_size; 3627 rec->jt_extsize = jtrunc->jt_extsize; 3628 } 3629 3630 static void 3631 jfsync_write(jfsync, jseg, data) 3632 struct jfsync *jfsync; 3633 struct jseg *jseg; 3634 uint8_t *data; 3635 { 3636 struct jtrncrec *rec; 3637 3638 rec = (struct jtrncrec *)data; 3639 rec->jt_op = JOP_SYNC; 3640 rec->jt_ino = jfsync->jfs_ino; 3641 rec->jt_size = jfsync->jfs_size; 3642 rec->jt_extsize = jfsync->jfs_extsize; 3643 } 3644 3645 static void 3646 softdep_flushjournal(mp) 3647 struct mount *mp; 3648 { 3649 struct jblocks *jblocks; 3650 struct ufsmount *ump; 3651 3652 if (MOUNTEDSUJ(mp) == 0) 3653 return; 3654 ump = VFSTOUFS(mp); 3655 jblocks = ump->softdep_jblocks; 3656 ACQUIRE_LOCK(ump); 3657 while (ump->softdep_on_journal) { 3658 jblocks->jb_needseg = 1; 3659 softdep_process_journal(mp, NULL, MNT_WAIT); 3660 } 3661 FREE_LOCK(ump); 3662 } 3663 3664 static void softdep_synchronize_completed(struct bio *); 3665 static void softdep_synchronize(struct bio *, struct ufsmount *, void *); 3666 3667 static void 3668 softdep_synchronize_completed(bp) 3669 struct bio *bp; 3670 { 3671 struct jseg *oldest; 3672 struct jseg *jseg; 3673 struct ufsmount *ump; 3674 3675 /* 3676 * caller1 marks the last segment written before we issued the 3677 * synchronize cache. 3678 */ 3679 jseg = bp->bio_caller1; 3680 if (jseg == NULL) { 3681 g_destroy_bio(bp); 3682 return; 3683 } 3684 ump = VFSTOUFS(jseg->js_list.wk_mp); 3685 ACQUIRE_LOCK(ump); 3686 oldest = NULL; 3687 /* 3688 * Mark all the journal entries waiting on the synchronize cache 3689 * as completed so they may continue on. 3690 */ 3691 while (jseg != NULL && (jseg->js_state & COMPLETE) == 0) { 3692 jseg->js_state |= COMPLETE; 3693 oldest = jseg; 3694 jseg = TAILQ_PREV(jseg, jseglst, js_next); 3695 } 3696 /* 3697 * Restart deferred journal entry processing from the oldest 3698 * completed jseg. 3699 */ 3700 if (oldest) 3701 complete_jsegs(oldest); 3702 3703 FREE_LOCK(ump); 3704 g_destroy_bio(bp); 3705 } 3706 3707 /* 3708 * Send BIO_FLUSH/SYNCHRONIZE CACHE to the device to enforce write ordering 3709 * barriers. The journal must be written prior to any blocks that depend 3710 * on it and the journal can not be released until the blocks have be 3711 * written. This code handles both barriers simultaneously. 3712 */ 3713 static void 3714 softdep_synchronize(bp, ump, caller1) 3715 struct bio *bp; 3716 struct ufsmount *ump; 3717 void *caller1; 3718 { 3719 3720 bp->bio_cmd = BIO_FLUSH; 3721 bp->bio_flags |= BIO_ORDERED; 3722 bp->bio_data = NULL; 3723 bp->bio_offset = ump->um_cp->provider->mediasize; 3724 bp->bio_length = 0; 3725 bp->bio_done = softdep_synchronize_completed; 3726 bp->bio_caller1 = caller1; 3727 g_io_request(bp, ump->um_cp); 3728 } 3729 3730 /* 3731 * Flush some journal records to disk. 3732 */ 3733 static void 3734 softdep_process_journal(mp, needwk, flags) 3735 struct mount *mp; 3736 struct worklist *needwk; 3737 int flags; 3738 { 3739 struct jblocks *jblocks; 3740 struct ufsmount *ump; 3741 struct worklist *wk; 3742 struct jseg *jseg; 3743 struct buf *bp; 3744 struct bio *bio; 3745 uint8_t *data; 3746 struct fs *fs; 3747 int shouldflush; 3748 int segwritten; 3749 int jrecmin; /* Minimum records per block. */ 3750 int jrecmax; /* Maximum records per block. */ 3751 int size; 3752 int cnt; 3753 int off; 3754 int devbsize; 3755 3756 ump = VFSTOUFS(mp); 3757 if (ump->um_softdep == NULL || ump->um_softdep->sd_jblocks == NULL) 3758 return; 3759 shouldflush = softdep_flushcache; 3760 bio = NULL; 3761 jseg = NULL; 3762 LOCK_OWNED(ump); 3763 fs = ump->um_fs; 3764 jblocks = ump->softdep_jblocks; 3765 devbsize = ump->um_devvp->v_bufobj.bo_bsize; 3766 /* 3767 * We write anywhere between a disk block and fs block. The upper 3768 * bound is picked to prevent buffer cache fragmentation and limit 3769 * processing time per I/O. 3770 */ 3771 jrecmin = (devbsize / JREC_SIZE) - 1; /* -1 for seg header */ 3772 jrecmax = (fs->fs_bsize / devbsize) * jrecmin; 3773 segwritten = 0; 3774 for (;;) { 3775 cnt = ump->softdep_on_journal; 3776 /* 3777 * Criteria for writing a segment: 3778 * 1) We have a full block. 3779 * 2) We're called from jwait() and haven't found the 3780 * journal item yet. 3781 * 3) Always write if needseg is set. 3782 * 4) If we are called from process_worklist and have 3783 * not yet written anything we write a partial block 3784 * to enforce a 1 second maximum latency on journal 3785 * entries. 3786 */ 3787 if (cnt < (jrecmax - 1) && needwk == NULL && 3788 jblocks->jb_needseg == 0 && (segwritten || cnt == 0)) 3789 break; 3790 cnt++; 3791 /* 3792 * Verify some free journal space. softdep_prealloc() should 3793 * guarantee that we don't run out so this is indicative of 3794 * a problem with the flow control. Try to recover 3795 * gracefully in any event. 3796 */ 3797 while (jblocks->jb_free == 0) { 3798 if (flags != MNT_WAIT) 3799 break; 3800 printf("softdep: Out of journal space!\n"); 3801 softdep_speedup(ump); 3802 msleep(jblocks, LOCK_PTR(ump), PRIBIO, "jblocks", hz); 3803 } 3804 FREE_LOCK(ump); 3805 jseg = malloc(sizeof(*jseg), M_JSEG, M_SOFTDEP_FLAGS); 3806 workitem_alloc(&jseg->js_list, D_JSEG, mp); 3807 LIST_INIT(&jseg->js_entries); 3808 LIST_INIT(&jseg->js_indirs); 3809 jseg->js_state = ATTACHED; 3810 if (shouldflush == 0) 3811 jseg->js_state |= COMPLETE; 3812 else if (bio == NULL) 3813 bio = g_alloc_bio(); 3814 jseg->js_jblocks = jblocks; 3815 bp = geteblk(fs->fs_bsize, 0); 3816 ACQUIRE_LOCK(ump); 3817 /* 3818 * If there was a race while we were allocating the block 3819 * and jseg the entry we care about was likely written. 3820 * We bail out in both the WAIT and NOWAIT case and assume 3821 * the caller will loop if the entry it cares about is 3822 * not written. 3823 */ 3824 cnt = ump->softdep_on_journal; 3825 if (cnt + jblocks->jb_needseg == 0 || jblocks->jb_free == 0) { 3826 bp->b_flags |= B_INVAL | B_NOCACHE; 3827 WORKITEM_FREE(jseg, D_JSEG); 3828 FREE_LOCK(ump); 3829 brelse(bp); 3830 ACQUIRE_LOCK(ump); 3831 break; 3832 } 3833 /* 3834 * Calculate the disk block size required for the available 3835 * records rounded to the min size. 3836 */ 3837 if (cnt == 0) 3838 size = devbsize; 3839 else if (cnt < jrecmax) 3840 size = howmany(cnt, jrecmin) * devbsize; 3841 else 3842 size = fs->fs_bsize; 3843 /* 3844 * Allocate a disk block for this journal data and account 3845 * for truncation of the requested size if enough contiguous 3846 * space was not available. 3847 */ 3848 bp->b_blkno = jblocks_alloc(jblocks, size, &size); 3849 bp->b_lblkno = bp->b_blkno; 3850 bp->b_offset = bp->b_blkno * DEV_BSIZE; 3851 bp->b_bcount = size; 3852 bp->b_flags &= ~B_INVAL; 3853 bp->b_flags |= B_VALIDSUSPWRT | B_NOCOPY; 3854 /* 3855 * Initialize our jseg with cnt records. Assign the next 3856 * sequence number to it and link it in-order. 3857 */ 3858 cnt = MIN(cnt, (size / devbsize) * jrecmin); 3859 jseg->js_buf = bp; 3860 jseg->js_cnt = cnt; 3861 jseg->js_refs = cnt + 1; /* Self ref. */ 3862 jseg->js_size = size; 3863 jseg->js_seq = jblocks->jb_nextseq++; 3864 if (jblocks->jb_oldestseg == NULL) 3865 jblocks->jb_oldestseg = jseg; 3866 jseg->js_oldseq = jblocks->jb_oldestseg->js_seq; 3867 TAILQ_INSERT_TAIL(&jblocks->jb_segs, jseg, js_next); 3868 if (jblocks->jb_writeseg == NULL) 3869 jblocks->jb_writeseg = jseg; 3870 /* 3871 * Start filling in records from the pending list. 3872 */ 3873 data = bp->b_data; 3874 off = 0; 3875 3876 /* 3877 * Always put a header on the first block. 3878 * XXX As with below, there might not be a chance to get 3879 * into the loop. Ensure that something valid is written. 3880 */ 3881 jseg_write(ump, jseg, data); 3882 off += JREC_SIZE; 3883 data = bp->b_data + off; 3884 3885 /* 3886 * XXX Something is wrong here. There's no work to do, 3887 * but we need to perform and I/O and allow it to complete 3888 * anyways. 3889 */ 3890 if (LIST_EMPTY(&ump->softdep_journal_pending)) 3891 stat_emptyjblocks++; 3892 3893 while ((wk = LIST_FIRST(&ump->softdep_journal_pending)) 3894 != NULL) { 3895 if (cnt == 0) 3896 break; 3897 /* Place a segment header on every device block. */ 3898 if ((off % devbsize) == 0) { 3899 jseg_write(ump, jseg, data); 3900 off += JREC_SIZE; 3901 data = bp->b_data + off; 3902 } 3903 if (wk == needwk) 3904 needwk = NULL; 3905 remove_from_journal(wk); 3906 wk->wk_state |= INPROGRESS; 3907 WORKLIST_INSERT(&jseg->js_entries, wk); 3908 switch (wk->wk_type) { 3909 case D_JADDREF: 3910 jaddref_write(WK_JADDREF(wk), jseg, data); 3911 break; 3912 case D_JREMREF: 3913 jremref_write(WK_JREMREF(wk), jseg, data); 3914 break; 3915 case D_JMVREF: 3916 jmvref_write(WK_JMVREF(wk), jseg, data); 3917 break; 3918 case D_JNEWBLK: 3919 jnewblk_write(WK_JNEWBLK(wk), jseg, data); 3920 break; 3921 case D_JFREEBLK: 3922 jfreeblk_write(WK_JFREEBLK(wk), jseg, data); 3923 break; 3924 case D_JFREEFRAG: 3925 jfreefrag_write(WK_JFREEFRAG(wk), jseg, data); 3926 break; 3927 case D_JTRUNC: 3928 jtrunc_write(WK_JTRUNC(wk), jseg, data); 3929 break; 3930 case D_JFSYNC: 3931 jfsync_write(WK_JFSYNC(wk), jseg, data); 3932 break; 3933 default: 3934 panic("process_journal: Unknown type %s", 3935 TYPENAME(wk->wk_type)); 3936 /* NOTREACHED */ 3937 } 3938 off += JREC_SIZE; 3939 data = bp->b_data + off; 3940 cnt--; 3941 } 3942 3943 /* Clear any remaining space so we don't leak kernel data */ 3944 if (size > off) 3945 bzero(data, size - off); 3946 3947 /* 3948 * Write this one buffer and continue. 3949 */ 3950 segwritten = 1; 3951 jblocks->jb_needseg = 0; 3952 WORKLIST_INSERT(&bp->b_dep, &jseg->js_list); 3953 FREE_LOCK(ump); 3954 bp->b_xflags |= BX_CVTENXIO; 3955 pbgetvp(ump->um_devvp, bp); 3956 /* 3957 * We only do the blocking wait once we find the journal 3958 * entry we're looking for. 3959 */ 3960 if (needwk == NULL && flags == MNT_WAIT) 3961 bwrite(bp); 3962 else 3963 bawrite(bp); 3964 ACQUIRE_LOCK(ump); 3965 } 3966 /* 3967 * If we wrote a segment issue a synchronize cache so the journal 3968 * is reflected on disk before the data is written. Since reclaiming 3969 * journal space also requires writing a journal record this 3970 * process also enforces a barrier before reclamation. 3971 */ 3972 if (segwritten && shouldflush) { 3973 softdep_synchronize(bio, ump, 3974 TAILQ_LAST(&jblocks->jb_segs, jseglst)); 3975 } else if (bio) 3976 g_destroy_bio(bio); 3977 /* 3978 * If we've suspended the filesystem because we ran out of journal 3979 * space either try to sync it here to make some progress or 3980 * unsuspend it if we already have. 3981 */ 3982 if (flags == 0 && jblocks->jb_suspended) { 3983 if (journal_unsuspend(ump)) 3984 return; 3985 FREE_LOCK(ump); 3986 VFS_SYNC(mp, MNT_NOWAIT); 3987 ffs_sbupdate(ump, MNT_WAIT, 0); 3988 ACQUIRE_LOCK(ump); 3989 } 3990 } 3991 3992 /* 3993 * Complete a jseg, allowing all dependencies awaiting journal writes 3994 * to proceed. Each journal dependency also attaches a jsegdep to dependent 3995 * structures so that the journal segment can be freed to reclaim space. 3996 */ 3997 static void 3998 complete_jseg(jseg) 3999 struct jseg *jseg; 4000 { 4001 struct worklist *wk; 4002 struct jmvref *jmvref; 4003 #ifdef INVARIANTS 4004 int i = 0; 4005 #endif 4006 4007 while ((wk = LIST_FIRST(&jseg->js_entries)) != NULL) { 4008 WORKLIST_REMOVE(wk); 4009 wk->wk_state &= ~INPROGRESS; 4010 wk->wk_state |= COMPLETE; 4011 KASSERT(i++ < jseg->js_cnt, 4012 ("handle_written_jseg: overflow %d >= %d", 4013 i - 1, jseg->js_cnt)); 4014 switch (wk->wk_type) { 4015 case D_JADDREF: 4016 handle_written_jaddref(WK_JADDREF(wk)); 4017 break; 4018 case D_JREMREF: 4019 handle_written_jremref(WK_JREMREF(wk)); 4020 break; 4021 case D_JMVREF: 4022 rele_jseg(jseg); /* No jsegdep. */ 4023 jmvref = WK_JMVREF(wk); 4024 LIST_REMOVE(jmvref, jm_deps); 4025 if ((jmvref->jm_pagedep->pd_state & ONWORKLIST) == 0) 4026 free_pagedep(jmvref->jm_pagedep); 4027 WORKITEM_FREE(jmvref, D_JMVREF); 4028 break; 4029 case D_JNEWBLK: 4030 handle_written_jnewblk(WK_JNEWBLK(wk)); 4031 break; 4032 case D_JFREEBLK: 4033 handle_written_jblkdep(&WK_JFREEBLK(wk)->jf_dep); 4034 break; 4035 case D_JTRUNC: 4036 handle_written_jblkdep(&WK_JTRUNC(wk)->jt_dep); 4037 break; 4038 case D_JFSYNC: 4039 rele_jseg(jseg); /* No jsegdep. */ 4040 WORKITEM_FREE(wk, D_JFSYNC); 4041 break; 4042 case D_JFREEFRAG: 4043 handle_written_jfreefrag(WK_JFREEFRAG(wk)); 4044 break; 4045 default: 4046 panic("handle_written_jseg: Unknown type %s", 4047 TYPENAME(wk->wk_type)); 4048 /* NOTREACHED */ 4049 } 4050 } 4051 /* Release the self reference so the structure may be freed. */ 4052 rele_jseg(jseg); 4053 } 4054 4055 /* 4056 * Determine which jsegs are ready for completion processing. Waits for 4057 * synchronize cache to complete as well as forcing in-order completion 4058 * of journal entries. 4059 */ 4060 static void 4061 complete_jsegs(jseg) 4062 struct jseg *jseg; 4063 { 4064 struct jblocks *jblocks; 4065 struct jseg *jsegn; 4066 4067 jblocks = jseg->js_jblocks; 4068 /* 4069 * Don't allow out of order completions. If this isn't the first 4070 * block wait for it to write before we're done. 4071 */ 4072 if (jseg != jblocks->jb_writeseg) 4073 return; 4074 /* Iterate through available jsegs processing their entries. */ 4075 while (jseg && (jseg->js_state & ALLCOMPLETE) == ALLCOMPLETE) { 4076 jblocks->jb_oldestwrseq = jseg->js_oldseq; 4077 jsegn = TAILQ_NEXT(jseg, js_next); 4078 complete_jseg(jseg); 4079 jseg = jsegn; 4080 } 4081 jblocks->jb_writeseg = jseg; 4082 /* 4083 * Attempt to free jsegs now that oldestwrseq may have advanced. 4084 */ 4085 free_jsegs(jblocks); 4086 } 4087 4088 /* 4089 * Mark a jseg as DEPCOMPLETE and throw away the buffer. Attempt to handle 4090 * the final completions. 4091 */ 4092 static void 4093 handle_written_jseg(jseg, bp) 4094 struct jseg *jseg; 4095 struct buf *bp; 4096 { 4097 4098 if (jseg->js_refs == 0) 4099 panic("handle_written_jseg: No self-reference on %p", jseg); 4100 jseg->js_state |= DEPCOMPLETE; 4101 /* 4102 * We'll never need this buffer again, set flags so it will be 4103 * discarded. 4104 */ 4105 bp->b_flags |= B_INVAL | B_NOCACHE; 4106 pbrelvp(bp); 4107 complete_jsegs(jseg); 4108 } 4109 4110 static inline struct jsegdep * 4111 inoref_jseg(inoref) 4112 struct inoref *inoref; 4113 { 4114 struct jsegdep *jsegdep; 4115 4116 jsegdep = inoref->if_jsegdep; 4117 inoref->if_jsegdep = NULL; 4118 4119 return (jsegdep); 4120 } 4121 4122 /* 4123 * Called once a jremref has made it to stable store. The jremref is marked 4124 * complete and we attempt to free it. Any pagedeps writes sleeping waiting 4125 * for the jremref to complete will be awoken by free_jremref. 4126 */ 4127 static void 4128 handle_written_jremref(jremref) 4129 struct jremref *jremref; 4130 { 4131 struct inodedep *inodedep; 4132 struct jsegdep *jsegdep; 4133 struct dirrem *dirrem; 4134 4135 /* Grab the jsegdep. */ 4136 jsegdep = inoref_jseg(&jremref->jr_ref); 4137 /* 4138 * Remove us from the inoref list. 4139 */ 4140 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 4141 0, &inodedep) == 0) 4142 panic("handle_written_jremref: Lost inodedep"); 4143 TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 4144 /* 4145 * Complete the dirrem. 4146 */ 4147 dirrem = jremref->jr_dirrem; 4148 jremref->jr_dirrem = NULL; 4149 LIST_REMOVE(jremref, jr_deps); 4150 jsegdep->jd_state |= jremref->jr_state & MKDIR_PARENT; 4151 jwork_insert(&dirrem->dm_jwork, jsegdep); 4152 if (LIST_EMPTY(&dirrem->dm_jremrefhd) && 4153 (dirrem->dm_state & COMPLETE) != 0) 4154 add_to_worklist(&dirrem->dm_list, 0); 4155 free_jremref(jremref); 4156 } 4157 4158 /* 4159 * Called once a jaddref has made it to stable store. The dependency is 4160 * marked complete and any dependent structures are added to the inode 4161 * bufwait list to be completed as soon as it is written. If a bitmap write 4162 * depends on this entry we move the inode into the inodedephd of the 4163 * bmsafemap dependency and attempt to remove the jaddref from the bmsafemap. 4164 */ 4165 static void 4166 handle_written_jaddref(jaddref) 4167 struct jaddref *jaddref; 4168 { 4169 struct jsegdep *jsegdep; 4170 struct inodedep *inodedep; 4171 struct diradd *diradd; 4172 struct mkdir *mkdir; 4173 4174 /* Grab the jsegdep. */ 4175 jsegdep = inoref_jseg(&jaddref->ja_ref); 4176 mkdir = NULL; 4177 diradd = NULL; 4178 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 4179 0, &inodedep) == 0) 4180 panic("handle_written_jaddref: Lost inodedep."); 4181 if (jaddref->ja_diradd == NULL) 4182 panic("handle_written_jaddref: No dependency"); 4183 if (jaddref->ja_diradd->da_list.wk_type == D_DIRADD) { 4184 diradd = jaddref->ja_diradd; 4185 WORKLIST_INSERT(&inodedep->id_bufwait, &diradd->da_list); 4186 } else if (jaddref->ja_state & MKDIR_PARENT) { 4187 mkdir = jaddref->ja_mkdir; 4188 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir->md_list); 4189 } else if (jaddref->ja_state & MKDIR_BODY) 4190 mkdir = jaddref->ja_mkdir; 4191 else 4192 panic("handle_written_jaddref: Unknown dependency %p", 4193 jaddref->ja_diradd); 4194 jaddref->ja_diradd = NULL; /* also clears ja_mkdir */ 4195 /* 4196 * Remove us from the inode list. 4197 */ 4198 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps); 4199 /* 4200 * The mkdir may be waiting on the jaddref to clear before freeing. 4201 */ 4202 if (mkdir) { 4203 KASSERT(mkdir->md_list.wk_type == D_MKDIR, 4204 ("handle_written_jaddref: Incorrect type for mkdir %s", 4205 TYPENAME(mkdir->md_list.wk_type))); 4206 mkdir->md_jaddref = NULL; 4207 diradd = mkdir->md_diradd; 4208 mkdir->md_state |= DEPCOMPLETE; 4209 complete_mkdir(mkdir); 4210 } 4211 jwork_insert(&diradd->da_jwork, jsegdep); 4212 if (jaddref->ja_state & NEWBLOCK) { 4213 inodedep->id_state |= ONDEPLIST; 4214 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_inodedephd, 4215 inodedep, id_deps); 4216 } 4217 free_jaddref(jaddref); 4218 } 4219 4220 /* 4221 * Called once a jnewblk journal is written. The allocdirect or allocindir 4222 * is placed in the bmsafemap to await notification of a written bitmap. If 4223 * the operation was canceled we add the segdep to the appropriate 4224 * dependency to free the journal space once the canceling operation 4225 * completes. 4226 */ 4227 static void 4228 handle_written_jnewblk(jnewblk) 4229 struct jnewblk *jnewblk; 4230 { 4231 struct bmsafemap *bmsafemap; 4232 struct freefrag *freefrag; 4233 struct freework *freework; 4234 struct jsegdep *jsegdep; 4235 struct newblk *newblk; 4236 4237 /* Grab the jsegdep. */ 4238 jsegdep = jnewblk->jn_jsegdep; 4239 jnewblk->jn_jsegdep = NULL; 4240 if (jnewblk->jn_dep == NULL) 4241 panic("handle_written_jnewblk: No dependency for the segdep."); 4242 switch (jnewblk->jn_dep->wk_type) { 4243 case D_NEWBLK: 4244 case D_ALLOCDIRECT: 4245 case D_ALLOCINDIR: 4246 /* 4247 * Add the written block to the bmsafemap so it can 4248 * be notified when the bitmap is on disk. 4249 */ 4250 newblk = WK_NEWBLK(jnewblk->jn_dep); 4251 newblk->nb_jnewblk = NULL; 4252 if ((newblk->nb_state & GOINGAWAY) == 0) { 4253 bmsafemap = newblk->nb_bmsafemap; 4254 newblk->nb_state |= ONDEPLIST; 4255 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, 4256 nb_deps); 4257 } 4258 jwork_insert(&newblk->nb_jwork, jsegdep); 4259 break; 4260 case D_FREEFRAG: 4261 /* 4262 * A newblock being removed by a freefrag when replaced by 4263 * frag extension. 4264 */ 4265 freefrag = WK_FREEFRAG(jnewblk->jn_dep); 4266 freefrag->ff_jdep = NULL; 4267 jwork_insert(&freefrag->ff_jwork, jsegdep); 4268 break; 4269 case D_FREEWORK: 4270 /* 4271 * A direct block was removed by truncate. 4272 */ 4273 freework = WK_FREEWORK(jnewblk->jn_dep); 4274 freework->fw_jnewblk = NULL; 4275 jwork_insert(&freework->fw_freeblks->fb_jwork, jsegdep); 4276 break; 4277 default: 4278 panic("handle_written_jnewblk: Unknown type %d.", 4279 jnewblk->jn_dep->wk_type); 4280 } 4281 jnewblk->jn_dep = NULL; 4282 free_jnewblk(jnewblk); 4283 } 4284 4285 /* 4286 * Cancel a jfreefrag that won't be needed, probably due to colliding with 4287 * an in-flight allocation that has not yet been committed. Divorce us 4288 * from the freefrag and mark it DEPCOMPLETE so that it may be added 4289 * to the worklist. 4290 */ 4291 static void 4292 cancel_jfreefrag(jfreefrag) 4293 struct jfreefrag *jfreefrag; 4294 { 4295 struct freefrag *freefrag; 4296 4297 if (jfreefrag->fr_jsegdep) { 4298 free_jsegdep(jfreefrag->fr_jsegdep); 4299 jfreefrag->fr_jsegdep = NULL; 4300 } 4301 freefrag = jfreefrag->fr_freefrag; 4302 jfreefrag->fr_freefrag = NULL; 4303 free_jfreefrag(jfreefrag); 4304 freefrag->ff_state |= DEPCOMPLETE; 4305 CTR1(KTR_SUJ, "cancel_jfreefrag: blkno %jd", freefrag->ff_blkno); 4306 } 4307 4308 /* 4309 * Free a jfreefrag when the parent freefrag is rendered obsolete. 4310 */ 4311 static void 4312 free_jfreefrag(jfreefrag) 4313 struct jfreefrag *jfreefrag; 4314 { 4315 4316 if (jfreefrag->fr_state & INPROGRESS) 4317 WORKLIST_REMOVE(&jfreefrag->fr_list); 4318 else if (jfreefrag->fr_state & ONWORKLIST) 4319 remove_from_journal(&jfreefrag->fr_list); 4320 if (jfreefrag->fr_freefrag != NULL) 4321 panic("free_jfreefrag: Still attached to a freefrag."); 4322 WORKITEM_FREE(jfreefrag, D_JFREEFRAG); 4323 } 4324 4325 /* 4326 * Called when the journal write for a jfreefrag completes. The parent 4327 * freefrag is added to the worklist if this completes its dependencies. 4328 */ 4329 static void 4330 handle_written_jfreefrag(jfreefrag) 4331 struct jfreefrag *jfreefrag; 4332 { 4333 struct jsegdep *jsegdep; 4334 struct freefrag *freefrag; 4335 4336 /* Grab the jsegdep. */ 4337 jsegdep = jfreefrag->fr_jsegdep; 4338 jfreefrag->fr_jsegdep = NULL; 4339 freefrag = jfreefrag->fr_freefrag; 4340 if (freefrag == NULL) 4341 panic("handle_written_jfreefrag: No freefrag."); 4342 freefrag->ff_state |= DEPCOMPLETE; 4343 freefrag->ff_jdep = NULL; 4344 jwork_insert(&freefrag->ff_jwork, jsegdep); 4345 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 4346 add_to_worklist(&freefrag->ff_list, 0); 4347 jfreefrag->fr_freefrag = NULL; 4348 free_jfreefrag(jfreefrag); 4349 } 4350 4351 /* 4352 * Called when the journal write for a jfreeblk completes. The jfreeblk 4353 * is removed from the freeblks list of pending journal writes and the 4354 * jsegdep is moved to the freeblks jwork to be completed when all blocks 4355 * have been reclaimed. 4356 */ 4357 static void 4358 handle_written_jblkdep(jblkdep) 4359 struct jblkdep *jblkdep; 4360 { 4361 struct freeblks *freeblks; 4362 struct jsegdep *jsegdep; 4363 4364 /* Grab the jsegdep. */ 4365 jsegdep = jblkdep->jb_jsegdep; 4366 jblkdep->jb_jsegdep = NULL; 4367 freeblks = jblkdep->jb_freeblks; 4368 LIST_REMOVE(jblkdep, jb_deps); 4369 jwork_insert(&freeblks->fb_jwork, jsegdep); 4370 /* 4371 * If the freeblks is all journaled, we can add it to the worklist. 4372 */ 4373 if (LIST_EMPTY(&freeblks->fb_jblkdephd) && 4374 (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 4375 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 4376 4377 free_jblkdep(jblkdep); 4378 } 4379 4380 static struct jsegdep * 4381 newjsegdep(struct worklist *wk) 4382 { 4383 struct jsegdep *jsegdep; 4384 4385 jsegdep = malloc(sizeof(*jsegdep), M_JSEGDEP, M_SOFTDEP_FLAGS); 4386 workitem_alloc(&jsegdep->jd_list, D_JSEGDEP, wk->wk_mp); 4387 jsegdep->jd_seg = NULL; 4388 4389 return (jsegdep); 4390 } 4391 4392 static struct jmvref * 4393 newjmvref(dp, ino, oldoff, newoff) 4394 struct inode *dp; 4395 ino_t ino; 4396 off_t oldoff; 4397 off_t newoff; 4398 { 4399 struct jmvref *jmvref; 4400 4401 jmvref = malloc(sizeof(*jmvref), M_JMVREF, M_SOFTDEP_FLAGS); 4402 workitem_alloc(&jmvref->jm_list, D_JMVREF, ITOVFS(dp)); 4403 jmvref->jm_list.wk_state = ATTACHED | DEPCOMPLETE; 4404 jmvref->jm_parent = dp->i_number; 4405 jmvref->jm_ino = ino; 4406 jmvref->jm_oldoff = oldoff; 4407 jmvref->jm_newoff = newoff; 4408 4409 return (jmvref); 4410 } 4411 4412 /* 4413 * Allocate a new jremref that tracks the removal of ip from dp with the 4414 * directory entry offset of diroff. Mark the entry as ATTACHED and 4415 * DEPCOMPLETE as we have all the information required for the journal write 4416 * and the directory has already been removed from the buffer. The caller 4417 * is responsible for linking the jremref into the pagedep and adding it 4418 * to the journal to write. The MKDIR_PARENT flag is set if we're doing 4419 * a DOTDOT addition so handle_workitem_remove() can properly assign 4420 * the jsegdep when we're done. 4421 */ 4422 static struct jremref * 4423 newjremref(struct dirrem *dirrem, struct inode *dp, struct inode *ip, 4424 off_t diroff, nlink_t nlink) 4425 { 4426 struct jremref *jremref; 4427 4428 jremref = malloc(sizeof(*jremref), M_JREMREF, M_SOFTDEP_FLAGS); 4429 workitem_alloc(&jremref->jr_list, D_JREMREF, ITOVFS(dp)); 4430 jremref->jr_state = ATTACHED; 4431 newinoref(&jremref->jr_ref, ip->i_number, dp->i_number, diroff, 4432 nlink, ip->i_mode); 4433 jremref->jr_dirrem = dirrem; 4434 4435 return (jremref); 4436 } 4437 4438 static inline void 4439 newinoref(struct inoref *inoref, ino_t ino, ino_t parent, off_t diroff, 4440 nlink_t nlink, uint16_t mode) 4441 { 4442 4443 inoref->if_jsegdep = newjsegdep(&inoref->if_list); 4444 inoref->if_diroff = diroff; 4445 inoref->if_ino = ino; 4446 inoref->if_parent = parent; 4447 inoref->if_nlink = nlink; 4448 inoref->if_mode = mode; 4449 } 4450 4451 /* 4452 * Allocate a new jaddref to track the addition of ino to dp at diroff. The 4453 * directory offset may not be known until later. The caller is responsible 4454 * adding the entry to the journal when this information is available. nlink 4455 * should be the link count prior to the addition and mode is only required 4456 * to have the correct FMT. 4457 */ 4458 static struct jaddref * 4459 newjaddref(struct inode *dp, ino_t ino, off_t diroff, int16_t nlink, 4460 uint16_t mode) 4461 { 4462 struct jaddref *jaddref; 4463 4464 jaddref = malloc(sizeof(*jaddref), M_JADDREF, M_SOFTDEP_FLAGS); 4465 workitem_alloc(&jaddref->ja_list, D_JADDREF, ITOVFS(dp)); 4466 jaddref->ja_state = ATTACHED; 4467 jaddref->ja_mkdir = NULL; 4468 newinoref(&jaddref->ja_ref, ino, dp->i_number, diroff, nlink, mode); 4469 4470 return (jaddref); 4471 } 4472 4473 /* 4474 * Create a new free dependency for a freework. The caller is responsible 4475 * for adjusting the reference count when it has the lock held. The freedep 4476 * will track an outstanding bitmap write that will ultimately clear the 4477 * freework to continue. 4478 */ 4479 static struct freedep * 4480 newfreedep(struct freework *freework) 4481 { 4482 struct freedep *freedep; 4483 4484 freedep = malloc(sizeof(*freedep), M_FREEDEP, M_SOFTDEP_FLAGS); 4485 workitem_alloc(&freedep->fd_list, D_FREEDEP, freework->fw_list.wk_mp); 4486 freedep->fd_freework = freework; 4487 4488 return (freedep); 4489 } 4490 4491 /* 4492 * Free a freedep structure once the buffer it is linked to is written. If 4493 * this is the last reference to the freework schedule it for completion. 4494 */ 4495 static void 4496 free_freedep(freedep) 4497 struct freedep *freedep; 4498 { 4499 struct freework *freework; 4500 4501 freework = freedep->fd_freework; 4502 freework->fw_freeblks->fb_cgwait--; 4503 if (--freework->fw_ref == 0) 4504 freework_enqueue(freework); 4505 WORKITEM_FREE(freedep, D_FREEDEP); 4506 } 4507 4508 /* 4509 * Allocate a new freework structure that may be a level in an indirect 4510 * when parent is not NULL or a top level block when it is. The top level 4511 * freework structures are allocated without the per-filesystem lock held 4512 * and before the freeblks is visible outside of softdep_setup_freeblocks(). 4513 */ 4514 static struct freework * 4515 newfreework(ump, freeblks, parent, lbn, nb, frags, off, journal) 4516 struct ufsmount *ump; 4517 struct freeblks *freeblks; 4518 struct freework *parent; 4519 ufs_lbn_t lbn; 4520 ufs2_daddr_t nb; 4521 int frags; 4522 int off; 4523 int journal; 4524 { 4525 struct freework *freework; 4526 4527 freework = malloc(sizeof(*freework), M_FREEWORK, M_SOFTDEP_FLAGS); 4528 workitem_alloc(&freework->fw_list, D_FREEWORK, freeblks->fb_list.wk_mp); 4529 freework->fw_state = ATTACHED; 4530 freework->fw_jnewblk = NULL; 4531 freework->fw_freeblks = freeblks; 4532 freework->fw_parent = parent; 4533 freework->fw_lbn = lbn; 4534 freework->fw_blkno = nb; 4535 freework->fw_frags = frags; 4536 freework->fw_indir = NULL; 4537 freework->fw_ref = (MOUNTEDSUJ(UFSTOVFS(ump)) == 0 || 4538 lbn >= -UFS_NXADDR) ? 0 : NINDIR(ump->um_fs) + 1; 4539 freework->fw_start = freework->fw_off = off; 4540 if (journal) 4541 newjfreeblk(freeblks, lbn, nb, frags); 4542 if (parent == NULL) { 4543 ACQUIRE_LOCK(ump); 4544 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 4545 freeblks->fb_ref++; 4546 FREE_LOCK(ump); 4547 } 4548 4549 return (freework); 4550 } 4551 4552 /* 4553 * Eliminate a jfreeblk for a block that does not need journaling. 4554 */ 4555 static void 4556 cancel_jfreeblk(freeblks, blkno) 4557 struct freeblks *freeblks; 4558 ufs2_daddr_t blkno; 4559 { 4560 struct jfreeblk *jfreeblk; 4561 struct jblkdep *jblkdep; 4562 4563 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) { 4564 if (jblkdep->jb_list.wk_type != D_JFREEBLK) 4565 continue; 4566 jfreeblk = WK_JFREEBLK(&jblkdep->jb_list); 4567 if (jfreeblk->jf_blkno == blkno) 4568 break; 4569 } 4570 if (jblkdep == NULL) 4571 return; 4572 CTR1(KTR_SUJ, "cancel_jfreeblk: blkno %jd", blkno); 4573 free_jsegdep(jblkdep->jb_jsegdep); 4574 LIST_REMOVE(jblkdep, jb_deps); 4575 WORKITEM_FREE(jfreeblk, D_JFREEBLK); 4576 } 4577 4578 /* 4579 * Allocate a new jfreeblk to journal top level block pointer when truncating 4580 * a file. The caller must add this to the worklist when the per-filesystem 4581 * lock is held. 4582 */ 4583 static struct jfreeblk * 4584 newjfreeblk(freeblks, lbn, blkno, frags) 4585 struct freeblks *freeblks; 4586 ufs_lbn_t lbn; 4587 ufs2_daddr_t blkno; 4588 int frags; 4589 { 4590 struct jfreeblk *jfreeblk; 4591 4592 jfreeblk = malloc(sizeof(*jfreeblk), M_JFREEBLK, M_SOFTDEP_FLAGS); 4593 workitem_alloc(&jfreeblk->jf_dep.jb_list, D_JFREEBLK, 4594 freeblks->fb_list.wk_mp); 4595 jfreeblk->jf_dep.jb_jsegdep = newjsegdep(&jfreeblk->jf_dep.jb_list); 4596 jfreeblk->jf_dep.jb_freeblks = freeblks; 4597 jfreeblk->jf_ino = freeblks->fb_inum; 4598 jfreeblk->jf_lbn = lbn; 4599 jfreeblk->jf_blkno = blkno; 4600 jfreeblk->jf_frags = frags; 4601 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jfreeblk->jf_dep, jb_deps); 4602 4603 return (jfreeblk); 4604 } 4605 4606 /* 4607 * The journal is only prepared to handle full-size block numbers, so we 4608 * have to adjust the record to reflect the change to a full-size block. 4609 * For example, suppose we have a block made up of fragments 8-15 and 4610 * want to free its last two fragments. We are given a request that says: 4611 * FREEBLK ino=5, blkno=14, lbn=0, frags=2, oldfrags=0 4612 * where frags are the number of fragments to free and oldfrags are the 4613 * number of fragments to keep. To block align it, we have to change it to 4614 * have a valid full-size blkno, so it becomes: 4615 * FREEBLK ino=5, blkno=8, lbn=0, frags=2, oldfrags=6 4616 */ 4617 static void 4618 adjust_newfreework(freeblks, frag_offset) 4619 struct freeblks *freeblks; 4620 int frag_offset; 4621 { 4622 struct jfreeblk *jfreeblk; 4623 4624 KASSERT((LIST_FIRST(&freeblks->fb_jblkdephd) != NULL && 4625 LIST_FIRST(&freeblks->fb_jblkdephd)->jb_list.wk_type == D_JFREEBLK), 4626 ("adjust_newfreework: Missing freeblks dependency")); 4627 4628 jfreeblk = WK_JFREEBLK(LIST_FIRST(&freeblks->fb_jblkdephd)); 4629 jfreeblk->jf_blkno -= frag_offset; 4630 jfreeblk->jf_frags += frag_offset; 4631 } 4632 4633 /* 4634 * Allocate a new jtrunc to track a partial truncation. 4635 */ 4636 static struct jtrunc * 4637 newjtrunc(freeblks, size, extsize) 4638 struct freeblks *freeblks; 4639 off_t size; 4640 int extsize; 4641 { 4642 struct jtrunc *jtrunc; 4643 4644 jtrunc = malloc(sizeof(*jtrunc), M_JTRUNC, M_SOFTDEP_FLAGS); 4645 workitem_alloc(&jtrunc->jt_dep.jb_list, D_JTRUNC, 4646 freeblks->fb_list.wk_mp); 4647 jtrunc->jt_dep.jb_jsegdep = newjsegdep(&jtrunc->jt_dep.jb_list); 4648 jtrunc->jt_dep.jb_freeblks = freeblks; 4649 jtrunc->jt_ino = freeblks->fb_inum; 4650 jtrunc->jt_size = size; 4651 jtrunc->jt_extsize = extsize; 4652 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jtrunc->jt_dep, jb_deps); 4653 4654 return (jtrunc); 4655 } 4656 4657 /* 4658 * If we're canceling a new bitmap we have to search for another ref 4659 * to move into the bmsafemap dep. This might be better expressed 4660 * with another structure. 4661 */ 4662 static void 4663 move_newblock_dep(jaddref, inodedep) 4664 struct jaddref *jaddref; 4665 struct inodedep *inodedep; 4666 { 4667 struct inoref *inoref; 4668 struct jaddref *jaddrefn; 4669 4670 jaddrefn = NULL; 4671 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4672 inoref = TAILQ_NEXT(inoref, if_deps)) { 4673 if ((jaddref->ja_state & NEWBLOCK) && 4674 inoref->if_list.wk_type == D_JADDREF) { 4675 jaddrefn = (struct jaddref *)inoref; 4676 break; 4677 } 4678 } 4679 if (jaddrefn == NULL) 4680 return; 4681 jaddrefn->ja_state &= ~(ATTACHED | UNDONE); 4682 jaddrefn->ja_state |= jaddref->ja_state & 4683 (ATTACHED | UNDONE | NEWBLOCK); 4684 jaddref->ja_state &= ~(ATTACHED | UNDONE | NEWBLOCK); 4685 jaddref->ja_state |= ATTACHED; 4686 LIST_REMOVE(jaddref, ja_bmdeps); 4687 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_jaddrefhd, jaddrefn, 4688 ja_bmdeps); 4689 } 4690 4691 /* 4692 * Cancel a jaddref either before it has been written or while it is being 4693 * written. This happens when a link is removed before the add reaches 4694 * the disk. The jaddref dependency is kept linked into the bmsafemap 4695 * and inode to prevent the link count or bitmap from reaching the disk 4696 * until handle_workitem_remove() re-adjusts the counts and bitmaps as 4697 * required. 4698 * 4699 * Returns 1 if the canceled addref requires journaling of the remove and 4700 * 0 otherwise. 4701 */ 4702 static int 4703 cancel_jaddref(jaddref, inodedep, wkhd) 4704 struct jaddref *jaddref; 4705 struct inodedep *inodedep; 4706 struct workhead *wkhd; 4707 { 4708 struct inoref *inoref; 4709 struct jsegdep *jsegdep; 4710 int needsj; 4711 4712 KASSERT((jaddref->ja_state & COMPLETE) == 0, 4713 ("cancel_jaddref: Canceling complete jaddref")); 4714 if (jaddref->ja_state & (INPROGRESS | COMPLETE)) 4715 needsj = 1; 4716 else 4717 needsj = 0; 4718 if (inodedep == NULL) 4719 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 4720 0, &inodedep) == 0) 4721 panic("cancel_jaddref: Lost inodedep"); 4722 /* 4723 * We must adjust the nlink of any reference operation that follows 4724 * us so that it is consistent with the in-memory reference. This 4725 * ensures that inode nlink rollbacks always have the correct link. 4726 */ 4727 if (needsj == 0) { 4728 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4729 inoref = TAILQ_NEXT(inoref, if_deps)) { 4730 if (inoref->if_state & GOINGAWAY) 4731 break; 4732 inoref->if_nlink--; 4733 } 4734 } 4735 jsegdep = inoref_jseg(&jaddref->ja_ref); 4736 if (jaddref->ja_state & NEWBLOCK) 4737 move_newblock_dep(jaddref, inodedep); 4738 wake_worklist(&jaddref->ja_list); 4739 jaddref->ja_mkdir = NULL; 4740 if (jaddref->ja_state & INPROGRESS) { 4741 jaddref->ja_state &= ~INPROGRESS; 4742 WORKLIST_REMOVE(&jaddref->ja_list); 4743 jwork_insert(wkhd, jsegdep); 4744 } else { 4745 free_jsegdep(jsegdep); 4746 if (jaddref->ja_state & DEPCOMPLETE) 4747 remove_from_journal(&jaddref->ja_list); 4748 } 4749 jaddref->ja_state |= (GOINGAWAY | DEPCOMPLETE); 4750 /* 4751 * Leave NEWBLOCK jaddrefs on the inodedep so handle_workitem_remove 4752 * can arrange for them to be freed with the bitmap. Otherwise we 4753 * no longer need this addref attached to the inoreflst and it 4754 * will incorrectly adjust nlink if we leave it. 4755 */ 4756 if ((jaddref->ja_state & NEWBLOCK) == 0) { 4757 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 4758 if_deps); 4759 jaddref->ja_state |= COMPLETE; 4760 free_jaddref(jaddref); 4761 return (needsj); 4762 } 4763 /* 4764 * Leave the head of the list for jsegdeps for fast merging. 4765 */ 4766 if (LIST_FIRST(wkhd) != NULL) { 4767 jaddref->ja_state |= ONWORKLIST; 4768 LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list); 4769 } else 4770 WORKLIST_INSERT(wkhd, &jaddref->ja_list); 4771 4772 return (needsj); 4773 } 4774 4775 /* 4776 * Attempt to free a jaddref structure when some work completes. This 4777 * should only succeed once the entry is written and all dependencies have 4778 * been notified. 4779 */ 4780 static void 4781 free_jaddref(jaddref) 4782 struct jaddref *jaddref; 4783 { 4784 4785 if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE) 4786 return; 4787 if (jaddref->ja_ref.if_jsegdep) 4788 panic("free_jaddref: segdep attached to jaddref %p(0x%X)\n", 4789 jaddref, jaddref->ja_state); 4790 if (jaddref->ja_state & NEWBLOCK) 4791 LIST_REMOVE(jaddref, ja_bmdeps); 4792 if (jaddref->ja_state & (INPROGRESS | ONWORKLIST)) 4793 panic("free_jaddref: Bad state %p(0x%X)", 4794 jaddref, jaddref->ja_state); 4795 if (jaddref->ja_mkdir != NULL) 4796 panic("free_jaddref: Work pending, 0x%X\n", jaddref->ja_state); 4797 WORKITEM_FREE(jaddref, D_JADDREF); 4798 } 4799 4800 /* 4801 * Free a jremref structure once it has been written or discarded. 4802 */ 4803 static void 4804 free_jremref(jremref) 4805 struct jremref *jremref; 4806 { 4807 4808 if (jremref->jr_ref.if_jsegdep) 4809 free_jsegdep(jremref->jr_ref.if_jsegdep); 4810 if (jremref->jr_state & INPROGRESS) 4811 panic("free_jremref: IO still pending"); 4812 WORKITEM_FREE(jremref, D_JREMREF); 4813 } 4814 4815 /* 4816 * Free a jnewblk structure. 4817 */ 4818 static void 4819 free_jnewblk(jnewblk) 4820 struct jnewblk *jnewblk; 4821 { 4822 4823 if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE) 4824 return; 4825 LIST_REMOVE(jnewblk, jn_deps); 4826 if (jnewblk->jn_dep != NULL) 4827 panic("free_jnewblk: Dependency still attached."); 4828 WORKITEM_FREE(jnewblk, D_JNEWBLK); 4829 } 4830 4831 /* 4832 * Cancel a jnewblk which has been been made redundant by frag extension. 4833 */ 4834 static void 4835 cancel_jnewblk(jnewblk, wkhd) 4836 struct jnewblk *jnewblk; 4837 struct workhead *wkhd; 4838 { 4839 struct jsegdep *jsegdep; 4840 4841 CTR1(KTR_SUJ, "cancel_jnewblk: blkno %jd", jnewblk->jn_blkno); 4842 jsegdep = jnewblk->jn_jsegdep; 4843 if (jnewblk->jn_jsegdep == NULL || jnewblk->jn_dep == NULL) 4844 panic("cancel_jnewblk: Invalid state"); 4845 jnewblk->jn_jsegdep = NULL; 4846 jnewblk->jn_dep = NULL; 4847 jnewblk->jn_state |= GOINGAWAY; 4848 if (jnewblk->jn_state & INPROGRESS) { 4849 jnewblk->jn_state &= ~INPROGRESS; 4850 WORKLIST_REMOVE(&jnewblk->jn_list); 4851 jwork_insert(wkhd, jsegdep); 4852 } else { 4853 free_jsegdep(jsegdep); 4854 remove_from_journal(&jnewblk->jn_list); 4855 } 4856 wake_worklist(&jnewblk->jn_list); 4857 WORKLIST_INSERT(wkhd, &jnewblk->jn_list); 4858 } 4859 4860 static void 4861 free_jblkdep(jblkdep) 4862 struct jblkdep *jblkdep; 4863 { 4864 4865 if (jblkdep->jb_list.wk_type == D_JFREEBLK) 4866 WORKITEM_FREE(jblkdep, D_JFREEBLK); 4867 else if (jblkdep->jb_list.wk_type == D_JTRUNC) 4868 WORKITEM_FREE(jblkdep, D_JTRUNC); 4869 else 4870 panic("free_jblkdep: Unexpected type %s", 4871 TYPENAME(jblkdep->jb_list.wk_type)); 4872 } 4873 4874 /* 4875 * Free a single jseg once it is no longer referenced in memory or on 4876 * disk. Reclaim journal blocks and dependencies waiting for the segment 4877 * to disappear. 4878 */ 4879 static void 4880 free_jseg(jseg, jblocks) 4881 struct jseg *jseg; 4882 struct jblocks *jblocks; 4883 { 4884 struct freework *freework; 4885 4886 /* 4887 * Free freework structures that were lingering to indicate freed 4888 * indirect blocks that forced journal write ordering on reallocate. 4889 */ 4890 while ((freework = LIST_FIRST(&jseg->js_indirs)) != NULL) 4891 indirblk_remove(freework); 4892 if (jblocks->jb_oldestseg == jseg) 4893 jblocks->jb_oldestseg = TAILQ_NEXT(jseg, js_next); 4894 TAILQ_REMOVE(&jblocks->jb_segs, jseg, js_next); 4895 jblocks_free(jblocks, jseg->js_list.wk_mp, jseg->js_size); 4896 KASSERT(LIST_EMPTY(&jseg->js_entries), 4897 ("free_jseg: Freed jseg has valid entries.")); 4898 WORKITEM_FREE(jseg, D_JSEG); 4899 } 4900 4901 /* 4902 * Free all jsegs that meet the criteria for being reclaimed and update 4903 * oldestseg. 4904 */ 4905 static void 4906 free_jsegs(jblocks) 4907 struct jblocks *jblocks; 4908 { 4909 struct jseg *jseg; 4910 4911 /* 4912 * Free only those jsegs which have none allocated before them to 4913 * preserve the journal space ordering. 4914 */ 4915 while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) { 4916 /* 4917 * Only reclaim space when nothing depends on this journal 4918 * set and another set has written that it is no longer 4919 * valid. 4920 */ 4921 if (jseg->js_refs != 0) { 4922 jblocks->jb_oldestseg = jseg; 4923 return; 4924 } 4925 if ((jseg->js_state & ALLCOMPLETE) != ALLCOMPLETE) 4926 break; 4927 if (jseg->js_seq > jblocks->jb_oldestwrseq) 4928 break; 4929 /* 4930 * We can free jsegs that didn't write entries when 4931 * oldestwrseq == js_seq. 4932 */ 4933 if (jseg->js_seq == jblocks->jb_oldestwrseq && 4934 jseg->js_cnt != 0) 4935 break; 4936 free_jseg(jseg, jblocks); 4937 } 4938 /* 4939 * If we exited the loop above we still must discover the 4940 * oldest valid segment. 4941 */ 4942 if (jseg) 4943 for (jseg = jblocks->jb_oldestseg; jseg != NULL; 4944 jseg = TAILQ_NEXT(jseg, js_next)) 4945 if (jseg->js_refs != 0) 4946 break; 4947 jblocks->jb_oldestseg = jseg; 4948 /* 4949 * The journal has no valid records but some jsegs may still be 4950 * waiting on oldestwrseq to advance. We force a small record 4951 * out to permit these lingering records to be reclaimed. 4952 */ 4953 if (jblocks->jb_oldestseg == NULL && !TAILQ_EMPTY(&jblocks->jb_segs)) 4954 jblocks->jb_needseg = 1; 4955 } 4956 4957 /* 4958 * Release one reference to a jseg and free it if the count reaches 0. This 4959 * should eventually reclaim journal space as well. 4960 */ 4961 static void 4962 rele_jseg(jseg) 4963 struct jseg *jseg; 4964 { 4965 4966 KASSERT(jseg->js_refs > 0, 4967 ("free_jseg: Invalid refcnt %d", jseg->js_refs)); 4968 if (--jseg->js_refs != 0) 4969 return; 4970 free_jsegs(jseg->js_jblocks); 4971 } 4972 4973 /* 4974 * Release a jsegdep and decrement the jseg count. 4975 */ 4976 static void 4977 free_jsegdep(jsegdep) 4978 struct jsegdep *jsegdep; 4979 { 4980 4981 if (jsegdep->jd_seg) 4982 rele_jseg(jsegdep->jd_seg); 4983 WORKITEM_FREE(jsegdep, D_JSEGDEP); 4984 } 4985 4986 /* 4987 * Wait for a journal item to make it to disk. Initiate journal processing 4988 * if required. 4989 */ 4990 static int 4991 jwait(wk, waitfor) 4992 struct worklist *wk; 4993 int waitfor; 4994 { 4995 4996 LOCK_OWNED(VFSTOUFS(wk->wk_mp)); 4997 /* 4998 * Blocking journal waits cause slow synchronous behavior. Record 4999 * stats on the frequency of these blocking operations. 5000 */ 5001 if (waitfor == MNT_WAIT) { 5002 stat_journal_wait++; 5003 switch (wk->wk_type) { 5004 case D_JREMREF: 5005 case D_JMVREF: 5006 stat_jwait_filepage++; 5007 break; 5008 case D_JTRUNC: 5009 case D_JFREEBLK: 5010 stat_jwait_freeblks++; 5011 break; 5012 case D_JNEWBLK: 5013 stat_jwait_newblk++; 5014 break; 5015 case D_JADDREF: 5016 stat_jwait_inode++; 5017 break; 5018 default: 5019 break; 5020 } 5021 } 5022 /* 5023 * If IO has not started we process the journal. We can't mark the 5024 * worklist item as IOWAITING because we drop the lock while 5025 * processing the journal and the worklist entry may be freed after 5026 * this point. The caller may call back in and re-issue the request. 5027 */ 5028 if ((wk->wk_state & INPROGRESS) == 0) { 5029 softdep_process_journal(wk->wk_mp, wk, waitfor); 5030 if (waitfor != MNT_WAIT) 5031 return (EBUSY); 5032 return (0); 5033 } 5034 if (waitfor != MNT_WAIT) 5035 return (EBUSY); 5036 wait_worklist(wk, "jwait"); 5037 return (0); 5038 } 5039 5040 /* 5041 * Lookup an inodedep based on an inode pointer and set the nlinkdelta as 5042 * appropriate. This is a convenience function to reduce duplicate code 5043 * for the setup and revert functions below. 5044 */ 5045 static struct inodedep * 5046 inodedep_lookup_ip(ip) 5047 struct inode *ip; 5048 { 5049 struct inodedep *inodedep; 5050 5051 KASSERT(ip->i_nlink >= ip->i_effnlink, 5052 ("inodedep_lookup_ip: bad delta")); 5053 (void) inodedep_lookup(ITOVFS(ip), ip->i_number, DEPALLOC, 5054 &inodedep); 5055 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 5056 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 5057 5058 return (inodedep); 5059 } 5060 5061 /* 5062 * Called prior to creating a new inode and linking it to a directory. The 5063 * jaddref structure must already be allocated by softdep_setup_inomapdep 5064 * and it is discovered here so we can initialize the mode and update 5065 * nlinkdelta. 5066 */ 5067 void 5068 softdep_setup_create(dp, ip) 5069 struct inode *dp; 5070 struct inode *ip; 5071 { 5072 struct inodedep *inodedep; 5073 struct jaddref *jaddref; 5074 struct vnode *dvp; 5075 5076 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5077 ("softdep_setup_create called on non-softdep filesystem")); 5078 KASSERT(ip->i_nlink == 1, 5079 ("softdep_setup_create: Invalid link count.")); 5080 dvp = ITOV(dp); 5081 ACQUIRE_LOCK(ITOUMP(dp)); 5082 inodedep = inodedep_lookup_ip(ip); 5083 if (DOINGSUJ(dvp)) { 5084 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 5085 inoreflst); 5086 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 5087 ("softdep_setup_create: No addref structure present.")); 5088 } 5089 FREE_LOCK(ITOUMP(dp)); 5090 } 5091 5092 /* 5093 * Create a jaddref structure to track the addition of a DOTDOT link when 5094 * we are reparenting an inode as part of a rename. This jaddref will be 5095 * found by softdep_setup_directory_change. Adjusts nlinkdelta for 5096 * non-journaling softdep. 5097 */ 5098 void 5099 softdep_setup_dotdot_link(dp, ip) 5100 struct inode *dp; 5101 struct inode *ip; 5102 { 5103 struct inodedep *inodedep; 5104 struct jaddref *jaddref; 5105 struct vnode *dvp; 5106 5107 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5108 ("softdep_setup_dotdot_link called on non-softdep filesystem")); 5109 dvp = ITOV(dp); 5110 jaddref = NULL; 5111 /* 5112 * We don't set MKDIR_PARENT as this is not tied to a mkdir and 5113 * is used as a normal link would be. 5114 */ 5115 if (DOINGSUJ(dvp)) 5116 jaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 5117 dp->i_effnlink - 1, dp->i_mode); 5118 ACQUIRE_LOCK(ITOUMP(dp)); 5119 inodedep = inodedep_lookup_ip(dp); 5120 if (jaddref) 5121 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 5122 if_deps); 5123 FREE_LOCK(ITOUMP(dp)); 5124 } 5125 5126 /* 5127 * Create a jaddref structure to track a new link to an inode. The directory 5128 * offset is not known until softdep_setup_directory_add or 5129 * softdep_setup_directory_change. Adjusts nlinkdelta for non-journaling 5130 * softdep. 5131 */ 5132 void 5133 softdep_setup_link(dp, ip) 5134 struct inode *dp; 5135 struct inode *ip; 5136 { 5137 struct inodedep *inodedep; 5138 struct jaddref *jaddref; 5139 struct vnode *dvp; 5140 5141 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5142 ("softdep_setup_link called on non-softdep filesystem")); 5143 dvp = ITOV(dp); 5144 jaddref = NULL; 5145 if (DOINGSUJ(dvp)) 5146 jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1, 5147 ip->i_mode); 5148 ACQUIRE_LOCK(ITOUMP(dp)); 5149 inodedep = inodedep_lookup_ip(ip); 5150 if (jaddref) 5151 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 5152 if_deps); 5153 FREE_LOCK(ITOUMP(dp)); 5154 } 5155 5156 /* 5157 * Called to create the jaddref structures to track . and .. references as 5158 * well as lookup and further initialize the incomplete jaddref created 5159 * by softdep_setup_inomapdep when the inode was allocated. Adjusts 5160 * nlinkdelta for non-journaling softdep. 5161 */ 5162 void 5163 softdep_setup_mkdir(dp, ip) 5164 struct inode *dp; 5165 struct inode *ip; 5166 { 5167 struct inodedep *inodedep; 5168 struct jaddref *dotdotaddref; 5169 struct jaddref *dotaddref; 5170 struct jaddref *jaddref; 5171 struct vnode *dvp; 5172 5173 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5174 ("softdep_setup_mkdir called on non-softdep filesystem")); 5175 dvp = ITOV(dp); 5176 dotaddref = dotdotaddref = NULL; 5177 if (DOINGSUJ(dvp)) { 5178 dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1, 5179 ip->i_mode); 5180 dotaddref->ja_state |= MKDIR_BODY; 5181 dotdotaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 5182 dp->i_effnlink - 1, dp->i_mode); 5183 dotdotaddref->ja_state |= MKDIR_PARENT; 5184 } 5185 ACQUIRE_LOCK(ITOUMP(dp)); 5186 inodedep = inodedep_lookup_ip(ip); 5187 if (DOINGSUJ(dvp)) { 5188 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 5189 inoreflst); 5190 KASSERT(jaddref != NULL, 5191 ("softdep_setup_mkdir: No addref structure present.")); 5192 KASSERT(jaddref->ja_parent == dp->i_number, 5193 ("softdep_setup_mkdir: bad parent %ju", 5194 (uintmax_t)jaddref->ja_parent)); 5195 TAILQ_INSERT_BEFORE(&jaddref->ja_ref, &dotaddref->ja_ref, 5196 if_deps); 5197 } 5198 inodedep = inodedep_lookup_ip(dp); 5199 if (DOINGSUJ(dvp)) 5200 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, 5201 &dotdotaddref->ja_ref, if_deps); 5202 FREE_LOCK(ITOUMP(dp)); 5203 } 5204 5205 /* 5206 * Called to track nlinkdelta of the inode and parent directories prior to 5207 * unlinking a directory. 5208 */ 5209 void 5210 softdep_setup_rmdir(dp, ip) 5211 struct inode *dp; 5212 struct inode *ip; 5213 { 5214 struct vnode *dvp; 5215 5216 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5217 ("softdep_setup_rmdir called on non-softdep filesystem")); 5218 dvp = ITOV(dp); 5219 ACQUIRE_LOCK(ITOUMP(dp)); 5220 (void) inodedep_lookup_ip(ip); 5221 (void) inodedep_lookup_ip(dp); 5222 FREE_LOCK(ITOUMP(dp)); 5223 } 5224 5225 /* 5226 * Called to track nlinkdelta of the inode and parent directories prior to 5227 * unlink. 5228 */ 5229 void 5230 softdep_setup_unlink(dp, ip) 5231 struct inode *dp; 5232 struct inode *ip; 5233 { 5234 struct vnode *dvp; 5235 5236 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5237 ("softdep_setup_unlink called on non-softdep filesystem")); 5238 dvp = ITOV(dp); 5239 ACQUIRE_LOCK(ITOUMP(dp)); 5240 (void) inodedep_lookup_ip(ip); 5241 (void) inodedep_lookup_ip(dp); 5242 FREE_LOCK(ITOUMP(dp)); 5243 } 5244 5245 /* 5246 * Called to release the journal structures created by a failed non-directory 5247 * creation. Adjusts nlinkdelta for non-journaling softdep. 5248 */ 5249 void 5250 softdep_revert_create(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_create 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_create: 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 link 5275 * addition. Adjusts nlinkdelta for non-journaling softdep. 5276 */ 5277 void 5278 softdep_revert_link(dp, ip) 5279 struct inode *dp; 5280 struct inode *ip; 5281 { 5282 struct inodedep *inodedep; 5283 struct jaddref *jaddref; 5284 struct vnode *dvp; 5285 5286 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5287 ("softdep_revert_link called on non-softdep filesystem")); 5288 dvp = ITOV(dp); 5289 ACQUIRE_LOCK(ITOUMP(dp)); 5290 inodedep = inodedep_lookup_ip(ip); 5291 if (DOINGSUJ(dvp)) { 5292 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 5293 inoreflst); 5294 KASSERT(jaddref->ja_parent == dp->i_number, 5295 ("softdep_revert_link: addref parent mismatch")); 5296 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 5297 } 5298 FREE_LOCK(ITOUMP(dp)); 5299 } 5300 5301 /* 5302 * Called to release the journal structures created by a failed mkdir 5303 * attempt. Adjusts nlinkdelta for non-journaling softdep. 5304 */ 5305 void 5306 softdep_revert_mkdir(dp, ip) 5307 struct inode *dp; 5308 struct inode *ip; 5309 { 5310 struct inodedep *inodedep; 5311 struct jaddref *jaddref; 5312 struct jaddref *dotaddref; 5313 struct vnode *dvp; 5314 5315 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5316 ("softdep_revert_mkdir called on non-softdep filesystem")); 5317 dvp = ITOV(dp); 5318 5319 ACQUIRE_LOCK(ITOUMP(dp)); 5320 inodedep = inodedep_lookup_ip(dp); 5321 if (DOINGSUJ(dvp)) { 5322 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 5323 inoreflst); 5324 KASSERT(jaddref->ja_parent == ip->i_number, 5325 ("softdep_revert_mkdir: dotdot addref parent mismatch")); 5326 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 5327 } 5328 inodedep = inodedep_lookup_ip(ip); 5329 if (DOINGSUJ(dvp)) { 5330 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 5331 inoreflst); 5332 KASSERT(jaddref->ja_parent == dp->i_number, 5333 ("softdep_revert_mkdir: addref parent mismatch")); 5334 dotaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 5335 inoreflst, if_deps); 5336 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 5337 KASSERT(dotaddref->ja_parent == ip->i_number, 5338 ("softdep_revert_mkdir: dot addref parent mismatch")); 5339 cancel_jaddref(dotaddref, inodedep, &inodedep->id_inowait); 5340 } 5341 FREE_LOCK(ITOUMP(dp)); 5342 } 5343 5344 /* 5345 * Called to correct nlinkdelta after a failed rmdir. 5346 */ 5347 void 5348 softdep_revert_rmdir(dp, ip) 5349 struct inode *dp; 5350 struct inode *ip; 5351 { 5352 5353 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5354 ("softdep_revert_rmdir called on non-softdep filesystem")); 5355 ACQUIRE_LOCK(ITOUMP(dp)); 5356 (void) inodedep_lookup_ip(ip); 5357 (void) inodedep_lookup_ip(dp); 5358 FREE_LOCK(ITOUMP(dp)); 5359 } 5360 5361 /* 5362 * Protecting the freemaps (or bitmaps). 5363 * 5364 * To eliminate the need to execute fsck before mounting a filesystem 5365 * after a power failure, one must (conservatively) guarantee that the 5366 * on-disk copy of the bitmaps never indicate that a live inode or block is 5367 * free. So, when a block or inode is allocated, the bitmap should be 5368 * updated (on disk) before any new pointers. When a block or inode is 5369 * freed, the bitmap should not be updated until all pointers have been 5370 * reset. The latter dependency is handled by the delayed de-allocation 5371 * approach described below for block and inode de-allocation. The former 5372 * dependency is handled by calling the following procedure when a block or 5373 * inode is allocated. When an inode is allocated an "inodedep" is created 5374 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk. 5375 * Each "inodedep" is also inserted into the hash indexing structure so 5376 * that any additional link additions can be made dependent on the inode 5377 * allocation. 5378 * 5379 * The ufs filesystem maintains a number of free block counts (e.g., per 5380 * cylinder group, per cylinder and per <cylinder, rotational position> pair) 5381 * in addition to the bitmaps. These counts are used to improve efficiency 5382 * during allocation and therefore must be consistent with the bitmaps. 5383 * There is no convenient way to guarantee post-crash consistency of these 5384 * counts with simple update ordering, for two main reasons: (1) The counts 5385 * and bitmaps for a single cylinder group block are not in the same disk 5386 * sector. If a disk write is interrupted (e.g., by power failure), one may 5387 * be written and the other not. (2) Some of the counts are located in the 5388 * superblock rather than the cylinder group block. So, we focus our soft 5389 * updates implementation on protecting the bitmaps. When mounting a 5390 * filesystem, we recompute the auxiliary counts from the bitmaps. 5391 */ 5392 5393 /* 5394 * Called just after updating the cylinder group block to allocate an inode. 5395 */ 5396 void 5397 softdep_setup_inomapdep(bp, ip, newinum, mode) 5398 struct buf *bp; /* buffer for cylgroup block with inode map */ 5399 struct inode *ip; /* inode related to allocation */ 5400 ino_t newinum; /* new inode number being allocated */ 5401 int mode; 5402 { 5403 struct inodedep *inodedep; 5404 struct bmsafemap *bmsafemap; 5405 struct jaddref *jaddref; 5406 struct mount *mp; 5407 struct fs *fs; 5408 5409 mp = ITOVFS(ip); 5410 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5411 ("softdep_setup_inomapdep called on non-softdep filesystem")); 5412 fs = VFSTOUFS(mp)->um_fs; 5413 jaddref = NULL; 5414 5415 /* 5416 * Allocate the journal reference add structure so that the bitmap 5417 * can be dependent on it. 5418 */ 5419 if (MOUNTEDSUJ(mp)) { 5420 jaddref = newjaddref(ip, newinum, 0, 0, mode); 5421 jaddref->ja_state |= NEWBLOCK; 5422 } 5423 5424 /* 5425 * Create a dependency for the newly allocated inode. 5426 * Panic if it already exists as something is seriously wrong. 5427 * Otherwise add it to the dependency list for the buffer holding 5428 * the cylinder group map from which it was allocated. 5429 * 5430 * We have to preallocate a bmsafemap entry in case it is needed 5431 * in bmsafemap_lookup since once we allocate the inodedep, we 5432 * have to finish initializing it before we can FREE_LOCK(). 5433 * By preallocating, we avoid FREE_LOCK() while doing a malloc 5434 * in bmsafemap_lookup. We cannot call bmsafemap_lookup before 5435 * creating the inodedep as it can be freed during the time 5436 * that we FREE_LOCK() while allocating the inodedep. We must 5437 * call workitem_alloc() before entering the locked section as 5438 * it also acquires the lock and we must avoid trying doing so 5439 * recursively. 5440 */ 5441 bmsafemap = malloc(sizeof(struct bmsafemap), 5442 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5443 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5444 ACQUIRE_LOCK(ITOUMP(ip)); 5445 if ((inodedep_lookup(mp, newinum, DEPALLOC, &inodedep))) 5446 panic("softdep_setup_inomapdep: dependency %p for new" 5447 "inode already exists", inodedep); 5448 bmsafemap = bmsafemap_lookup(mp, bp, ino_to_cg(fs, newinum), bmsafemap); 5449 if (jaddref) { 5450 LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps); 5451 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 5452 if_deps); 5453 } else { 5454 inodedep->id_state |= ONDEPLIST; 5455 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps); 5456 } 5457 inodedep->id_bmsafemap = bmsafemap; 5458 inodedep->id_state &= ~DEPCOMPLETE; 5459 FREE_LOCK(ITOUMP(ip)); 5460 } 5461 5462 /* 5463 * Called just after updating the cylinder group block to 5464 * allocate block or fragment. 5465 */ 5466 void 5467 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags) 5468 struct buf *bp; /* buffer for cylgroup block with block map */ 5469 struct mount *mp; /* filesystem doing allocation */ 5470 ufs2_daddr_t newblkno; /* number of newly allocated block */ 5471 int frags; /* Number of fragments. */ 5472 int oldfrags; /* Previous number of fragments for extend. */ 5473 { 5474 struct newblk *newblk; 5475 struct bmsafemap *bmsafemap; 5476 struct jnewblk *jnewblk; 5477 struct ufsmount *ump; 5478 struct fs *fs; 5479 5480 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5481 ("softdep_setup_blkmapdep called on non-softdep filesystem")); 5482 ump = VFSTOUFS(mp); 5483 fs = ump->um_fs; 5484 jnewblk = NULL; 5485 /* 5486 * Create a dependency for the newly allocated block. 5487 * Add it to the dependency list for the buffer holding 5488 * the cylinder group map from which it was allocated. 5489 */ 5490 if (MOUNTEDSUJ(mp)) { 5491 jnewblk = malloc(sizeof(*jnewblk), M_JNEWBLK, M_SOFTDEP_FLAGS); 5492 workitem_alloc(&jnewblk->jn_list, D_JNEWBLK, mp); 5493 jnewblk->jn_jsegdep = newjsegdep(&jnewblk->jn_list); 5494 jnewblk->jn_state = ATTACHED; 5495 jnewblk->jn_blkno = newblkno; 5496 jnewblk->jn_frags = frags; 5497 jnewblk->jn_oldfrags = oldfrags; 5498 #ifdef INVARIANTS 5499 { 5500 struct cg *cgp; 5501 uint8_t *blksfree; 5502 long bno; 5503 int i; 5504 5505 cgp = (struct cg *)bp->b_data; 5506 blksfree = cg_blksfree(cgp); 5507 bno = dtogd(fs, jnewblk->jn_blkno); 5508 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; 5509 i++) { 5510 if (isset(blksfree, bno + i)) 5511 panic("softdep_setup_blkmapdep: " 5512 "free fragment %d from %d-%d " 5513 "state 0x%X dep %p", i, 5514 jnewblk->jn_oldfrags, 5515 jnewblk->jn_frags, 5516 jnewblk->jn_state, 5517 jnewblk->jn_dep); 5518 } 5519 } 5520 #endif 5521 } 5522 5523 CTR3(KTR_SUJ, 5524 "softdep_setup_blkmapdep: blkno %jd frags %d oldfrags %d", 5525 newblkno, frags, oldfrags); 5526 ACQUIRE_LOCK(ump); 5527 if (newblk_lookup(mp, newblkno, DEPALLOC, &newblk) != 0) 5528 panic("softdep_setup_blkmapdep: found block"); 5529 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp, 5530 dtog(fs, newblkno), NULL); 5531 if (jnewblk) { 5532 jnewblk->jn_dep = (struct worklist *)newblk; 5533 LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps); 5534 } else { 5535 newblk->nb_state |= ONDEPLIST; 5536 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps); 5537 } 5538 newblk->nb_bmsafemap = bmsafemap; 5539 newblk->nb_jnewblk = jnewblk; 5540 FREE_LOCK(ump); 5541 } 5542 5543 #define BMSAFEMAP_HASH(ump, cg) \ 5544 (&(ump)->bmsafemap_hashtbl[(cg) & (ump)->bmsafemap_hash_size]) 5545 5546 static int 5547 bmsafemap_find(bmsafemaphd, cg, bmsafemapp) 5548 struct bmsafemap_hashhead *bmsafemaphd; 5549 int cg; 5550 struct bmsafemap **bmsafemapp; 5551 { 5552 struct bmsafemap *bmsafemap; 5553 5554 LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash) 5555 if (bmsafemap->sm_cg == cg) 5556 break; 5557 if (bmsafemap) { 5558 *bmsafemapp = bmsafemap; 5559 return (1); 5560 } 5561 *bmsafemapp = NULL; 5562 5563 return (0); 5564 } 5565 5566 /* 5567 * Find the bmsafemap associated with a cylinder group buffer. 5568 * If none exists, create one. The buffer must be locked when 5569 * this routine is called and this routine must be called with 5570 * the softdep lock held. To avoid giving up the lock while 5571 * allocating a new bmsafemap, a preallocated bmsafemap may be 5572 * provided. If it is provided but not needed, it is freed. 5573 */ 5574 static struct bmsafemap * 5575 bmsafemap_lookup(mp, bp, cg, newbmsafemap) 5576 struct mount *mp; 5577 struct buf *bp; 5578 int cg; 5579 struct bmsafemap *newbmsafemap; 5580 { 5581 struct bmsafemap_hashhead *bmsafemaphd; 5582 struct bmsafemap *bmsafemap, *collision; 5583 struct worklist *wk; 5584 struct ufsmount *ump; 5585 5586 ump = VFSTOUFS(mp); 5587 LOCK_OWNED(ump); 5588 KASSERT(bp != NULL, ("bmsafemap_lookup: missing buffer")); 5589 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 5590 if (wk->wk_type == D_BMSAFEMAP) { 5591 if (newbmsafemap) 5592 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5593 return (WK_BMSAFEMAP(wk)); 5594 } 5595 } 5596 bmsafemaphd = BMSAFEMAP_HASH(ump, cg); 5597 if (bmsafemap_find(bmsafemaphd, cg, &bmsafemap) == 1) { 5598 if (newbmsafemap) 5599 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5600 return (bmsafemap); 5601 } 5602 if (newbmsafemap) { 5603 bmsafemap = newbmsafemap; 5604 } else { 5605 FREE_LOCK(ump); 5606 bmsafemap = malloc(sizeof(struct bmsafemap), 5607 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5608 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5609 ACQUIRE_LOCK(ump); 5610 } 5611 bmsafemap->sm_buf = bp; 5612 LIST_INIT(&bmsafemap->sm_inodedephd); 5613 LIST_INIT(&bmsafemap->sm_inodedepwr); 5614 LIST_INIT(&bmsafemap->sm_newblkhd); 5615 LIST_INIT(&bmsafemap->sm_newblkwr); 5616 LIST_INIT(&bmsafemap->sm_jaddrefhd); 5617 LIST_INIT(&bmsafemap->sm_jnewblkhd); 5618 LIST_INIT(&bmsafemap->sm_freehd); 5619 LIST_INIT(&bmsafemap->sm_freewr); 5620 if (bmsafemap_find(bmsafemaphd, cg, &collision) == 1) { 5621 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 5622 return (collision); 5623 } 5624 bmsafemap->sm_cg = cg; 5625 LIST_INSERT_HEAD(bmsafemaphd, bmsafemap, sm_hash); 5626 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 5627 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list); 5628 return (bmsafemap); 5629 } 5630 5631 /* 5632 * Direct block allocation dependencies. 5633 * 5634 * When a new block is allocated, the corresponding disk locations must be 5635 * initialized (with zeros or new data) before the on-disk inode points to 5636 * them. Also, the freemap from which the block was allocated must be 5637 * updated (on disk) before the inode's pointer. These two dependencies are 5638 * independent of each other and are needed for all file blocks and indirect 5639 * blocks that are pointed to directly by the inode. Just before the 5640 * "in-core" version of the inode is updated with a newly allocated block 5641 * number, a procedure (below) is called to setup allocation dependency 5642 * structures. These structures are removed when the corresponding 5643 * dependencies are satisfied or when the block allocation becomes obsolete 5644 * (i.e., the file is deleted, the block is de-allocated, or the block is a 5645 * fragment that gets upgraded). All of these cases are handled in 5646 * procedures described later. 5647 * 5648 * When a file extension causes a fragment to be upgraded, either to a larger 5649 * fragment or to a full block, the on-disk location may change (if the 5650 * previous fragment could not simply be extended). In this case, the old 5651 * fragment must be de-allocated, but not until after the inode's pointer has 5652 * been updated. In most cases, this is handled by later procedures, which 5653 * will construct a "freefrag" structure to be added to the workitem queue 5654 * when the inode update is complete (or obsolete). The main exception to 5655 * this is when an allocation occurs while a pending allocation dependency 5656 * (for the same block pointer) remains. This case is handled in the main 5657 * allocation dependency setup procedure by immediately freeing the 5658 * unreferenced fragments. 5659 */ 5660 void 5661 softdep_setup_allocdirect(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 5662 struct inode *ip; /* inode to which block is being added */ 5663 ufs_lbn_t off; /* block pointer within inode */ 5664 ufs2_daddr_t newblkno; /* disk block number being added */ 5665 ufs2_daddr_t oldblkno; /* previous block number, 0 unless frag */ 5666 long newsize; /* size of new block */ 5667 long oldsize; /* size of new block */ 5668 struct buf *bp; /* bp for allocated block */ 5669 { 5670 struct allocdirect *adp, *oldadp; 5671 struct allocdirectlst *adphead; 5672 struct freefrag *freefrag; 5673 struct inodedep *inodedep; 5674 struct pagedep *pagedep; 5675 struct jnewblk *jnewblk; 5676 struct newblk *newblk; 5677 struct mount *mp; 5678 ufs_lbn_t lbn; 5679 5680 lbn = bp->b_lblkno; 5681 mp = ITOVFS(ip); 5682 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5683 ("softdep_setup_allocdirect called on non-softdep filesystem")); 5684 if (oldblkno && oldblkno != newblkno) 5685 /* 5686 * The usual case is that a smaller fragment that 5687 * was just allocated has been replaced with a bigger 5688 * fragment or a full-size block. If it is marked as 5689 * B_DELWRI, the current contents have not been written 5690 * to disk. It is possible that the block was written 5691 * earlier, but very uncommon. If the block has never 5692 * been written, there is no need to send a BIO_DELETE 5693 * for it when it is freed. The gain from avoiding the 5694 * TRIMs for the common case of unwritten blocks far 5695 * exceeds the cost of the write amplification for the 5696 * uncommon case of failing to send a TRIM for a block 5697 * that had been written. 5698 */ 5699 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn, 5700 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY); 5701 else 5702 freefrag = NULL; 5703 5704 CTR6(KTR_SUJ, 5705 "softdep_setup_allocdirect: ino %d blkno %jd oldblkno %jd " 5706 "off %jd newsize %ld oldsize %d", 5707 ip->i_number, newblkno, oldblkno, off, newsize, oldsize); 5708 ACQUIRE_LOCK(ITOUMP(ip)); 5709 if (off >= UFS_NDADDR) { 5710 if (lbn > 0) 5711 panic("softdep_setup_allocdirect: bad lbn %jd, off %jd", 5712 lbn, off); 5713 /* allocating an indirect block */ 5714 if (oldblkno != 0) 5715 panic("softdep_setup_allocdirect: non-zero indir"); 5716 } else { 5717 if (off != lbn) 5718 panic("softdep_setup_allocdirect: lbn %jd != off %jd", 5719 lbn, off); 5720 /* 5721 * Allocating a direct block. 5722 * 5723 * If we are allocating a directory block, then we must 5724 * allocate an associated pagedep to track additions and 5725 * deletions. 5726 */ 5727 if ((ip->i_mode & IFMT) == IFDIR) 5728 pagedep_lookup(mp, bp, ip->i_number, off, DEPALLOC, 5729 &pagedep); 5730 } 5731 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5732 panic("softdep_setup_allocdirect: lost block"); 5733 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5734 ("softdep_setup_allocdirect: newblk already initialized")); 5735 /* 5736 * Convert the newblk to an allocdirect. 5737 */ 5738 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT); 5739 adp = (struct allocdirect *)newblk; 5740 newblk->nb_freefrag = freefrag; 5741 adp->ad_offset = off; 5742 adp->ad_oldblkno = oldblkno; 5743 adp->ad_newsize = newsize; 5744 adp->ad_oldsize = oldsize; 5745 5746 /* 5747 * Finish initializing the journal. 5748 */ 5749 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5750 jnewblk->jn_ino = ip->i_number; 5751 jnewblk->jn_lbn = lbn; 5752 add_to_journal(&jnewblk->jn_list); 5753 } 5754 if (freefrag && freefrag->ff_jdep != NULL && 5755 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5756 add_to_journal(freefrag->ff_jdep); 5757 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 5758 adp->ad_inodedep = inodedep; 5759 5760 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5761 /* 5762 * The list of allocdirects must be kept in sorted and ascending 5763 * order so that the rollback routines can quickly determine the 5764 * first uncommitted block (the size of the file stored on disk 5765 * ends at the end of the lowest committed fragment, or if there 5766 * are no fragments, at the end of the highest committed block). 5767 * Since files generally grow, the typical case is that the new 5768 * block is to be added at the end of the list. We speed this 5769 * special case by checking against the last allocdirect in the 5770 * list before laboriously traversing the list looking for the 5771 * insertion point. 5772 */ 5773 adphead = &inodedep->id_newinoupdt; 5774 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5775 if (oldadp == NULL || oldadp->ad_offset <= off) { 5776 /* insert at end of list */ 5777 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5778 if (oldadp != NULL && oldadp->ad_offset == off) 5779 allocdirect_merge(adphead, adp, oldadp); 5780 FREE_LOCK(ITOUMP(ip)); 5781 return; 5782 } 5783 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5784 if (oldadp->ad_offset >= off) 5785 break; 5786 } 5787 if (oldadp == NULL) 5788 panic("softdep_setup_allocdirect: lost entry"); 5789 /* insert in middle of list */ 5790 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5791 if (oldadp->ad_offset == off) 5792 allocdirect_merge(adphead, adp, oldadp); 5793 5794 FREE_LOCK(ITOUMP(ip)); 5795 } 5796 5797 /* 5798 * Merge a newer and older journal record to be stored either in a 5799 * newblock or freefrag. This handles aggregating journal records for 5800 * fragment allocation into a second record as well as replacing a 5801 * journal free with an aborted journal allocation. A segment for the 5802 * oldest record will be placed on wkhd if it has been written. If not 5803 * the segment for the newer record will suffice. 5804 */ 5805 static struct worklist * 5806 jnewblk_merge(new, old, wkhd) 5807 struct worklist *new; 5808 struct worklist *old; 5809 struct workhead *wkhd; 5810 { 5811 struct jnewblk *njnewblk; 5812 struct jnewblk *jnewblk; 5813 5814 /* Handle NULLs to simplify callers. */ 5815 if (new == NULL) 5816 return (old); 5817 if (old == NULL) 5818 return (new); 5819 /* Replace a jfreefrag with a jnewblk. */ 5820 if (new->wk_type == D_JFREEFRAG) { 5821 if (WK_JNEWBLK(old)->jn_blkno != WK_JFREEFRAG(new)->fr_blkno) 5822 panic("jnewblk_merge: blkno mismatch: %p, %p", 5823 old, new); 5824 cancel_jfreefrag(WK_JFREEFRAG(new)); 5825 return (old); 5826 } 5827 if (old->wk_type != D_JNEWBLK || new->wk_type != D_JNEWBLK) 5828 panic("jnewblk_merge: Bad type: old %d new %d\n", 5829 old->wk_type, new->wk_type); 5830 /* 5831 * Handle merging of two jnewblk records that describe 5832 * different sets of fragments in the same block. 5833 */ 5834 jnewblk = WK_JNEWBLK(old); 5835 njnewblk = WK_JNEWBLK(new); 5836 if (jnewblk->jn_blkno != njnewblk->jn_blkno) 5837 panic("jnewblk_merge: Merging disparate blocks."); 5838 /* 5839 * The record may be rolled back in the cg. 5840 */ 5841 if (jnewblk->jn_state & UNDONE) { 5842 jnewblk->jn_state &= ~UNDONE; 5843 njnewblk->jn_state |= UNDONE; 5844 njnewblk->jn_state &= ~ATTACHED; 5845 } 5846 /* 5847 * We modify the newer addref and free the older so that if neither 5848 * has been written the most up-to-date copy will be on disk. If 5849 * both have been written but rolled back we only temporarily need 5850 * one of them to fix the bits when the cg write completes. 5851 */ 5852 jnewblk->jn_state |= ATTACHED | COMPLETE; 5853 njnewblk->jn_oldfrags = jnewblk->jn_oldfrags; 5854 cancel_jnewblk(jnewblk, wkhd); 5855 WORKLIST_REMOVE(&jnewblk->jn_list); 5856 free_jnewblk(jnewblk); 5857 return (new); 5858 } 5859 5860 /* 5861 * Replace an old allocdirect dependency with a newer one. 5862 */ 5863 static void 5864 allocdirect_merge(adphead, newadp, oldadp) 5865 struct allocdirectlst *adphead; /* head of list holding allocdirects */ 5866 struct allocdirect *newadp; /* allocdirect being added */ 5867 struct allocdirect *oldadp; /* existing allocdirect being checked */ 5868 { 5869 struct worklist *wk; 5870 struct freefrag *freefrag; 5871 5872 freefrag = NULL; 5873 LOCK_OWNED(VFSTOUFS(newadp->ad_list.wk_mp)); 5874 if (newadp->ad_oldblkno != oldadp->ad_newblkno || 5875 newadp->ad_oldsize != oldadp->ad_newsize || 5876 newadp->ad_offset >= UFS_NDADDR) 5877 panic("%s %jd != new %jd || old size %ld != new %ld", 5878 "allocdirect_merge: old blkno", 5879 (intmax_t)newadp->ad_oldblkno, 5880 (intmax_t)oldadp->ad_newblkno, 5881 newadp->ad_oldsize, oldadp->ad_newsize); 5882 newadp->ad_oldblkno = oldadp->ad_oldblkno; 5883 newadp->ad_oldsize = oldadp->ad_oldsize; 5884 /* 5885 * If the old dependency had a fragment to free or had never 5886 * previously had a block allocated, then the new dependency 5887 * can immediately post its freefrag and adopt the old freefrag. 5888 * This action is done by swapping the freefrag dependencies. 5889 * The new dependency gains the old one's freefrag, and the 5890 * old one gets the new one and then immediately puts it on 5891 * the worklist when it is freed by free_newblk. It is 5892 * not possible to do this swap when the old dependency had a 5893 * non-zero size but no previous fragment to free. This condition 5894 * arises when the new block is an extension of the old block. 5895 * Here, the first part of the fragment allocated to the new 5896 * dependency is part of the block currently claimed on disk by 5897 * the old dependency, so cannot legitimately be freed until the 5898 * conditions for the new dependency are fulfilled. 5899 */ 5900 freefrag = newadp->ad_freefrag; 5901 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) { 5902 newadp->ad_freefrag = oldadp->ad_freefrag; 5903 oldadp->ad_freefrag = freefrag; 5904 } 5905 /* 5906 * If we are tracking a new directory-block allocation, 5907 * move it from the old allocdirect to the new allocdirect. 5908 */ 5909 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) { 5910 WORKLIST_REMOVE(wk); 5911 if (!LIST_EMPTY(&oldadp->ad_newdirblk)) 5912 panic("allocdirect_merge: extra newdirblk"); 5913 WORKLIST_INSERT(&newadp->ad_newdirblk, wk); 5914 } 5915 TAILQ_REMOVE(adphead, oldadp, ad_next); 5916 /* 5917 * We need to move any journal dependencies over to the freefrag 5918 * that releases this block if it exists. Otherwise we are 5919 * extending an existing block and we'll wait until that is 5920 * complete to release the journal space and extend the 5921 * new journal to cover this old space as well. 5922 */ 5923 if (freefrag == NULL) { 5924 if (oldadp->ad_newblkno != newadp->ad_newblkno) 5925 panic("allocdirect_merge: %jd != %jd", 5926 oldadp->ad_newblkno, newadp->ad_newblkno); 5927 newadp->ad_block.nb_jnewblk = (struct jnewblk *) 5928 jnewblk_merge(&newadp->ad_block.nb_jnewblk->jn_list, 5929 &oldadp->ad_block.nb_jnewblk->jn_list, 5930 &newadp->ad_block.nb_jwork); 5931 oldadp->ad_block.nb_jnewblk = NULL; 5932 cancel_newblk(&oldadp->ad_block, NULL, 5933 &newadp->ad_block.nb_jwork); 5934 } else { 5935 wk = (struct worklist *) cancel_newblk(&oldadp->ad_block, 5936 &freefrag->ff_list, &freefrag->ff_jwork); 5937 freefrag->ff_jdep = jnewblk_merge(freefrag->ff_jdep, wk, 5938 &freefrag->ff_jwork); 5939 } 5940 free_newblk(&oldadp->ad_block); 5941 } 5942 5943 /* 5944 * Allocate a jfreefrag structure to journal a single block free. 5945 */ 5946 static struct jfreefrag * 5947 newjfreefrag(freefrag, ip, blkno, size, lbn) 5948 struct freefrag *freefrag; 5949 struct inode *ip; 5950 ufs2_daddr_t blkno; 5951 long size; 5952 ufs_lbn_t lbn; 5953 { 5954 struct jfreefrag *jfreefrag; 5955 struct fs *fs; 5956 5957 fs = ITOFS(ip); 5958 jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG, 5959 M_SOFTDEP_FLAGS); 5960 workitem_alloc(&jfreefrag->fr_list, D_JFREEFRAG, ITOVFS(ip)); 5961 jfreefrag->fr_jsegdep = newjsegdep(&jfreefrag->fr_list); 5962 jfreefrag->fr_state = ATTACHED | DEPCOMPLETE; 5963 jfreefrag->fr_ino = ip->i_number; 5964 jfreefrag->fr_lbn = lbn; 5965 jfreefrag->fr_blkno = blkno; 5966 jfreefrag->fr_frags = numfrags(fs, size); 5967 jfreefrag->fr_freefrag = freefrag; 5968 5969 return (jfreefrag); 5970 } 5971 5972 /* 5973 * Allocate a new freefrag structure. 5974 */ 5975 static struct freefrag * 5976 newfreefrag(ip, blkno, size, lbn, key) 5977 struct inode *ip; 5978 ufs2_daddr_t blkno; 5979 long size; 5980 ufs_lbn_t lbn; 5981 u_long key; 5982 { 5983 struct freefrag *freefrag; 5984 struct ufsmount *ump; 5985 struct fs *fs; 5986 5987 CTR4(KTR_SUJ, "newfreefrag: ino %d blkno %jd size %ld lbn %jd", 5988 ip->i_number, blkno, size, lbn); 5989 ump = ITOUMP(ip); 5990 fs = ump->um_fs; 5991 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag) 5992 panic("newfreefrag: frag size"); 5993 freefrag = malloc(sizeof(struct freefrag), 5994 M_FREEFRAG, M_SOFTDEP_FLAGS); 5995 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ump)); 5996 freefrag->ff_state = ATTACHED; 5997 LIST_INIT(&freefrag->ff_jwork); 5998 freefrag->ff_inum = ip->i_number; 5999 freefrag->ff_vtype = ITOV(ip)->v_type; 6000 freefrag->ff_blkno = blkno; 6001 freefrag->ff_fragsize = size; 6002 freefrag->ff_key = key; 6003 6004 if (MOUNTEDSUJ(UFSTOVFS(ump))) { 6005 freefrag->ff_jdep = (struct worklist *) 6006 newjfreefrag(freefrag, ip, blkno, size, lbn); 6007 } else { 6008 freefrag->ff_state |= DEPCOMPLETE; 6009 freefrag->ff_jdep = NULL; 6010 } 6011 6012 return (freefrag); 6013 } 6014 6015 /* 6016 * This workitem de-allocates fragments that were replaced during 6017 * file block allocation. 6018 */ 6019 static void 6020 handle_workitem_freefrag(freefrag) 6021 struct freefrag *freefrag; 6022 { 6023 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp); 6024 struct workhead wkhd; 6025 6026 CTR3(KTR_SUJ, 6027 "handle_workitem_freefrag: ino %d blkno %jd size %ld", 6028 freefrag->ff_inum, freefrag->ff_blkno, freefrag->ff_fragsize); 6029 /* 6030 * It would be illegal to add new completion items to the 6031 * freefrag after it was schedule to be done so it must be 6032 * safe to modify the list head here. 6033 */ 6034 LIST_INIT(&wkhd); 6035 ACQUIRE_LOCK(ump); 6036 LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list); 6037 /* 6038 * If the journal has not been written we must cancel it here. 6039 */ 6040 if (freefrag->ff_jdep) { 6041 if (freefrag->ff_jdep->wk_type != D_JNEWBLK) 6042 panic("handle_workitem_freefrag: Unexpected type %d\n", 6043 freefrag->ff_jdep->wk_type); 6044 cancel_jnewblk(WK_JNEWBLK(freefrag->ff_jdep), &wkhd); 6045 } 6046 FREE_LOCK(ump); 6047 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno, 6048 freefrag->ff_fragsize, freefrag->ff_inum, freefrag->ff_vtype, 6049 &wkhd, freefrag->ff_key); 6050 ACQUIRE_LOCK(ump); 6051 WORKITEM_FREE(freefrag, D_FREEFRAG); 6052 FREE_LOCK(ump); 6053 } 6054 6055 /* 6056 * Set up a dependency structure for an external attributes data block. 6057 * This routine follows much of the structure of softdep_setup_allocdirect. 6058 * See the description of softdep_setup_allocdirect above for details. 6059 */ 6060 void 6061 softdep_setup_allocext(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 6062 struct inode *ip; 6063 ufs_lbn_t off; 6064 ufs2_daddr_t newblkno; 6065 ufs2_daddr_t oldblkno; 6066 long newsize; 6067 long oldsize; 6068 struct buf *bp; 6069 { 6070 struct allocdirect *adp, *oldadp; 6071 struct allocdirectlst *adphead; 6072 struct freefrag *freefrag; 6073 struct inodedep *inodedep; 6074 struct jnewblk *jnewblk; 6075 struct newblk *newblk; 6076 struct mount *mp; 6077 struct ufsmount *ump; 6078 ufs_lbn_t lbn; 6079 6080 mp = ITOVFS(ip); 6081 ump = VFSTOUFS(mp); 6082 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6083 ("softdep_setup_allocext called on non-softdep filesystem")); 6084 KASSERT(off < UFS_NXADDR, 6085 ("softdep_setup_allocext: lbn %lld > UFS_NXADDR", (long long)off)); 6086 6087 lbn = bp->b_lblkno; 6088 if (oldblkno && oldblkno != newblkno) 6089 /* 6090 * The usual case is that a smaller fragment that 6091 * was just allocated has been replaced with a bigger 6092 * fragment or a full-size block. If it is marked as 6093 * B_DELWRI, the current contents have not been written 6094 * to disk. It is possible that the block was written 6095 * earlier, but very uncommon. If the block has never 6096 * been written, there is no need to send a BIO_DELETE 6097 * for it when it is freed. The gain from avoiding the 6098 * TRIMs for the common case of unwritten blocks far 6099 * exceeds the cost of the write amplification for the 6100 * uncommon case of failing to send a TRIM for a block 6101 * that had been written. 6102 */ 6103 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn, 6104 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY); 6105 else 6106 freefrag = NULL; 6107 6108 ACQUIRE_LOCK(ump); 6109 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 6110 panic("softdep_setup_allocext: lost block"); 6111 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 6112 ("softdep_setup_allocext: newblk already initialized")); 6113 /* 6114 * Convert the newblk to an allocdirect. 6115 */ 6116 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT); 6117 adp = (struct allocdirect *)newblk; 6118 newblk->nb_freefrag = freefrag; 6119 adp->ad_offset = off; 6120 adp->ad_oldblkno = oldblkno; 6121 adp->ad_newsize = newsize; 6122 adp->ad_oldsize = oldsize; 6123 adp->ad_state |= EXTDATA; 6124 6125 /* 6126 * Finish initializing the journal. 6127 */ 6128 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 6129 jnewblk->jn_ino = ip->i_number; 6130 jnewblk->jn_lbn = lbn; 6131 add_to_journal(&jnewblk->jn_list); 6132 } 6133 if (freefrag && freefrag->ff_jdep != NULL && 6134 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 6135 add_to_journal(freefrag->ff_jdep); 6136 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6137 adp->ad_inodedep = inodedep; 6138 6139 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 6140 /* 6141 * The list of allocdirects must be kept in sorted and ascending 6142 * order so that the rollback routines can quickly determine the 6143 * first uncommitted block (the size of the file stored on disk 6144 * ends at the end of the lowest committed fragment, or if there 6145 * are no fragments, at the end of the highest committed block). 6146 * Since files generally grow, the typical case is that the new 6147 * block is to be added at the end of the list. We speed this 6148 * special case by checking against the last allocdirect in the 6149 * list before laboriously traversing the list looking for the 6150 * insertion point. 6151 */ 6152 adphead = &inodedep->id_newextupdt; 6153 oldadp = TAILQ_LAST(adphead, allocdirectlst); 6154 if (oldadp == NULL || oldadp->ad_offset <= off) { 6155 /* insert at end of list */ 6156 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 6157 if (oldadp != NULL && oldadp->ad_offset == off) 6158 allocdirect_merge(adphead, adp, oldadp); 6159 FREE_LOCK(ump); 6160 return; 6161 } 6162 TAILQ_FOREACH(oldadp, adphead, ad_next) { 6163 if (oldadp->ad_offset >= off) 6164 break; 6165 } 6166 if (oldadp == NULL) 6167 panic("softdep_setup_allocext: lost entry"); 6168 /* insert in middle of list */ 6169 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 6170 if (oldadp->ad_offset == off) 6171 allocdirect_merge(adphead, adp, oldadp); 6172 FREE_LOCK(ump); 6173 } 6174 6175 /* 6176 * Indirect block allocation dependencies. 6177 * 6178 * The same dependencies that exist for a direct block also exist when 6179 * a new block is allocated and pointed to by an entry in a block of 6180 * indirect pointers. The undo/redo states described above are also 6181 * used here. Because an indirect block contains many pointers that 6182 * may have dependencies, a second copy of the entire in-memory indirect 6183 * block is kept. The buffer cache copy is always completely up-to-date. 6184 * The second copy, which is used only as a source for disk writes, 6185 * contains only the safe pointers (i.e., those that have no remaining 6186 * update dependencies). The second copy is freed when all pointers 6187 * are safe. The cache is not allowed to replace indirect blocks with 6188 * pending update dependencies. If a buffer containing an indirect 6189 * block with dependencies is written, these routines will mark it 6190 * dirty again. It can only be successfully written once all the 6191 * dependencies are removed. The ffs_fsync routine in conjunction with 6192 * softdep_sync_metadata work together to get all the dependencies 6193 * removed so that a file can be successfully written to disk. Three 6194 * procedures are used when setting up indirect block pointer 6195 * dependencies. The division is necessary because of the organization 6196 * of the "balloc" routine and because of the distinction between file 6197 * pages and file metadata blocks. 6198 */ 6199 6200 /* 6201 * Allocate a new allocindir structure. 6202 */ 6203 static struct allocindir * 6204 newallocindir(ip, ptrno, newblkno, oldblkno, lbn) 6205 struct inode *ip; /* inode for file being extended */ 6206 int ptrno; /* offset of pointer in indirect block */ 6207 ufs2_daddr_t newblkno; /* disk block number being added */ 6208 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 6209 ufs_lbn_t lbn; 6210 { 6211 struct newblk *newblk; 6212 struct allocindir *aip; 6213 struct freefrag *freefrag; 6214 struct jnewblk *jnewblk; 6215 6216 if (oldblkno) 6217 freefrag = newfreefrag(ip, oldblkno, ITOFS(ip)->fs_bsize, lbn, 6218 SINGLETON_KEY); 6219 else 6220 freefrag = NULL; 6221 ACQUIRE_LOCK(ITOUMP(ip)); 6222 if (newblk_lookup(ITOVFS(ip), newblkno, 0, &newblk) == 0) 6223 panic("new_allocindir: lost block"); 6224 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 6225 ("newallocindir: newblk already initialized")); 6226 WORKITEM_REASSIGN(newblk, D_ALLOCINDIR); 6227 newblk->nb_freefrag = freefrag; 6228 aip = (struct allocindir *)newblk; 6229 aip->ai_offset = ptrno; 6230 aip->ai_oldblkno = oldblkno; 6231 aip->ai_lbn = lbn; 6232 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 6233 jnewblk->jn_ino = ip->i_number; 6234 jnewblk->jn_lbn = lbn; 6235 add_to_journal(&jnewblk->jn_list); 6236 } 6237 if (freefrag && freefrag->ff_jdep != NULL && 6238 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 6239 add_to_journal(freefrag->ff_jdep); 6240 return (aip); 6241 } 6242 6243 /* 6244 * Called just before setting an indirect block pointer 6245 * to a newly allocated file page. 6246 */ 6247 void 6248 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 6249 struct inode *ip; /* inode for file being extended */ 6250 ufs_lbn_t lbn; /* allocated block number within file */ 6251 struct buf *bp; /* buffer with indirect blk referencing page */ 6252 int ptrno; /* offset of pointer in indirect block */ 6253 ufs2_daddr_t newblkno; /* disk block number being added */ 6254 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 6255 struct buf *nbp; /* buffer holding allocated page */ 6256 { 6257 struct inodedep *inodedep; 6258 struct freefrag *freefrag; 6259 struct allocindir *aip; 6260 struct pagedep *pagedep; 6261 struct mount *mp; 6262 struct ufsmount *ump; 6263 6264 mp = ITOVFS(ip); 6265 ump = VFSTOUFS(mp); 6266 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6267 ("softdep_setup_allocindir_page called on non-softdep filesystem")); 6268 KASSERT(lbn == nbp->b_lblkno, 6269 ("softdep_setup_allocindir_page: lbn %jd != lblkno %jd", 6270 lbn, bp->b_lblkno)); 6271 CTR4(KTR_SUJ, 6272 "softdep_setup_allocindir_page: ino %d blkno %jd oldblkno %jd " 6273 "lbn %jd", ip->i_number, newblkno, oldblkno, lbn); 6274 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page"); 6275 aip = newallocindir(ip, ptrno, newblkno, oldblkno, lbn); 6276 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6277 /* 6278 * If we are allocating a directory page, then we must 6279 * allocate an associated pagedep to track additions and 6280 * deletions. 6281 */ 6282 if ((ip->i_mode & IFMT) == IFDIR) 6283 pagedep_lookup(mp, nbp, ip->i_number, lbn, DEPALLOC, &pagedep); 6284 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 6285 freefrag = setup_allocindir_phase2(bp, ip, inodedep, aip, lbn); 6286 FREE_LOCK(ump); 6287 if (freefrag) 6288 handle_workitem_freefrag(freefrag); 6289 } 6290 6291 /* 6292 * Called just before setting an indirect block pointer to a 6293 * newly allocated indirect block. 6294 */ 6295 void 6296 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 6297 struct buf *nbp; /* newly allocated indirect block */ 6298 struct inode *ip; /* inode for file being extended */ 6299 struct buf *bp; /* indirect block referencing allocated block */ 6300 int ptrno; /* offset of pointer in indirect block */ 6301 ufs2_daddr_t newblkno; /* disk block number being added */ 6302 { 6303 struct inodedep *inodedep; 6304 struct allocindir *aip; 6305 struct ufsmount *ump; 6306 ufs_lbn_t lbn; 6307 6308 ump = ITOUMP(ip); 6309 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 6310 ("softdep_setup_allocindir_meta called on non-softdep filesystem")); 6311 CTR3(KTR_SUJ, 6312 "softdep_setup_allocindir_meta: ino %d blkno %jd ptrno %d", 6313 ip->i_number, newblkno, ptrno); 6314 lbn = nbp->b_lblkno; 6315 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta"); 6316 aip = newallocindir(ip, ptrno, newblkno, 0, lbn); 6317 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep); 6318 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 6319 if (setup_allocindir_phase2(bp, ip, inodedep, aip, lbn)) 6320 panic("softdep_setup_allocindir_meta: Block already existed"); 6321 FREE_LOCK(ump); 6322 } 6323 6324 static void 6325 indirdep_complete(indirdep) 6326 struct indirdep *indirdep; 6327 { 6328 struct allocindir *aip; 6329 6330 LIST_REMOVE(indirdep, ir_next); 6331 indirdep->ir_state |= DEPCOMPLETE; 6332 6333 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) { 6334 LIST_REMOVE(aip, ai_next); 6335 free_newblk(&aip->ai_block); 6336 } 6337 /* 6338 * If this indirdep is not attached to a buf it was simply waiting 6339 * on completion to clear completehd. free_indirdep() asserts 6340 * that nothing is dangling. 6341 */ 6342 if ((indirdep->ir_state & ONWORKLIST) == 0) 6343 free_indirdep(indirdep); 6344 } 6345 6346 static struct indirdep * 6347 indirdep_lookup(mp, ip, bp) 6348 struct mount *mp; 6349 struct inode *ip; 6350 struct buf *bp; 6351 { 6352 struct indirdep *indirdep, *newindirdep; 6353 struct newblk *newblk; 6354 struct ufsmount *ump; 6355 struct worklist *wk; 6356 struct fs *fs; 6357 ufs2_daddr_t blkno; 6358 6359 ump = VFSTOUFS(mp); 6360 LOCK_OWNED(ump); 6361 indirdep = NULL; 6362 newindirdep = NULL; 6363 fs = ump->um_fs; 6364 for (;;) { 6365 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 6366 if (wk->wk_type != D_INDIRDEP) 6367 continue; 6368 indirdep = WK_INDIRDEP(wk); 6369 break; 6370 } 6371 /* Found on the buffer worklist, no new structure to free. */ 6372 if (indirdep != NULL && newindirdep == NULL) 6373 return (indirdep); 6374 if (indirdep != NULL && newindirdep != NULL) 6375 panic("indirdep_lookup: simultaneous create"); 6376 /* None found on the buffer and a new structure is ready. */ 6377 if (indirdep == NULL && newindirdep != NULL) 6378 break; 6379 /* None found and no new structure available. */ 6380 FREE_LOCK(ump); 6381 newindirdep = malloc(sizeof(struct indirdep), 6382 M_INDIRDEP, M_SOFTDEP_FLAGS); 6383 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, mp); 6384 newindirdep->ir_state = ATTACHED; 6385 if (I_IS_UFS1(ip)) 6386 newindirdep->ir_state |= UFS1FMT; 6387 TAILQ_INIT(&newindirdep->ir_trunc); 6388 newindirdep->ir_saveddata = NULL; 6389 LIST_INIT(&newindirdep->ir_deplisthd); 6390 LIST_INIT(&newindirdep->ir_donehd); 6391 LIST_INIT(&newindirdep->ir_writehd); 6392 LIST_INIT(&newindirdep->ir_completehd); 6393 if (bp->b_blkno == bp->b_lblkno) { 6394 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp, 6395 NULL, NULL); 6396 bp->b_blkno = blkno; 6397 } 6398 newindirdep->ir_freeblks = NULL; 6399 newindirdep->ir_savebp = 6400 getblk(ump->um_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0); 6401 newindirdep->ir_bp = bp; 6402 BUF_KERNPROC(newindirdep->ir_savebp); 6403 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount); 6404 ACQUIRE_LOCK(ump); 6405 } 6406 indirdep = newindirdep; 6407 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list); 6408 /* 6409 * If the block is not yet allocated we don't set DEPCOMPLETE so 6410 * that we don't free dependencies until the pointers are valid. 6411 * This could search b_dep for D_ALLOCDIRECT/D_ALLOCINDIR rather 6412 * than using the hash. 6413 */ 6414 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk)) 6415 LIST_INSERT_HEAD(&newblk->nb_indirdeps, indirdep, ir_next); 6416 else 6417 indirdep->ir_state |= DEPCOMPLETE; 6418 return (indirdep); 6419 } 6420 6421 /* 6422 * Called to finish the allocation of the "aip" allocated 6423 * by one of the two routines above. 6424 */ 6425 static struct freefrag * 6426 setup_allocindir_phase2(bp, ip, inodedep, aip, lbn) 6427 struct buf *bp; /* in-memory copy of the indirect block */ 6428 struct inode *ip; /* inode for file being extended */ 6429 struct inodedep *inodedep; /* Inodedep for ip */ 6430 struct allocindir *aip; /* allocindir allocated by the above routines */ 6431 ufs_lbn_t lbn; /* Logical block number for this block. */ 6432 { 6433 struct fs *fs; 6434 struct indirdep *indirdep; 6435 struct allocindir *oldaip; 6436 struct freefrag *freefrag; 6437 struct mount *mp; 6438 struct ufsmount *ump; 6439 6440 mp = ITOVFS(ip); 6441 ump = VFSTOUFS(mp); 6442 LOCK_OWNED(ump); 6443 fs = ump->um_fs; 6444 if (bp->b_lblkno >= 0) 6445 panic("setup_allocindir_phase2: not indir blk"); 6446 KASSERT(aip->ai_offset >= 0 && aip->ai_offset < NINDIR(fs), 6447 ("setup_allocindir_phase2: Bad offset %d", aip->ai_offset)); 6448 indirdep = indirdep_lookup(mp, ip, bp); 6449 KASSERT(indirdep->ir_savebp != NULL, 6450 ("setup_allocindir_phase2 NULL ir_savebp")); 6451 aip->ai_indirdep = indirdep; 6452 /* 6453 * Check for an unwritten dependency for this indirect offset. If 6454 * there is, merge the old dependency into the new one. This happens 6455 * as a result of reallocblk only. 6456 */ 6457 freefrag = NULL; 6458 if (aip->ai_oldblkno != 0) { 6459 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) { 6460 if (oldaip->ai_offset == aip->ai_offset) { 6461 freefrag = allocindir_merge(aip, oldaip); 6462 goto done; 6463 } 6464 } 6465 LIST_FOREACH(oldaip, &indirdep->ir_donehd, ai_next) { 6466 if (oldaip->ai_offset == aip->ai_offset) { 6467 freefrag = allocindir_merge(aip, oldaip); 6468 goto done; 6469 } 6470 } 6471 } 6472 done: 6473 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next); 6474 return (freefrag); 6475 } 6476 6477 /* 6478 * Merge two allocindirs which refer to the same block. Move newblock 6479 * dependencies and setup the freefrags appropriately. 6480 */ 6481 static struct freefrag * 6482 allocindir_merge(aip, oldaip) 6483 struct allocindir *aip; 6484 struct allocindir *oldaip; 6485 { 6486 struct freefrag *freefrag; 6487 struct worklist *wk; 6488 6489 if (oldaip->ai_newblkno != aip->ai_oldblkno) 6490 panic("allocindir_merge: blkno"); 6491 aip->ai_oldblkno = oldaip->ai_oldblkno; 6492 freefrag = aip->ai_freefrag; 6493 aip->ai_freefrag = oldaip->ai_freefrag; 6494 oldaip->ai_freefrag = NULL; 6495 KASSERT(freefrag != NULL, ("setup_allocindir_phase2: No freefrag")); 6496 /* 6497 * If we are tracking a new directory-block allocation, 6498 * move it from the old allocindir to the new allocindir. 6499 */ 6500 if ((wk = LIST_FIRST(&oldaip->ai_newdirblk)) != NULL) { 6501 WORKLIST_REMOVE(wk); 6502 if (!LIST_EMPTY(&oldaip->ai_newdirblk)) 6503 panic("allocindir_merge: extra newdirblk"); 6504 WORKLIST_INSERT(&aip->ai_newdirblk, wk); 6505 } 6506 /* 6507 * We can skip journaling for this freefrag and just complete 6508 * any pending journal work for the allocindir that is being 6509 * removed after the freefrag completes. 6510 */ 6511 if (freefrag->ff_jdep) 6512 cancel_jfreefrag(WK_JFREEFRAG(freefrag->ff_jdep)); 6513 LIST_REMOVE(oldaip, ai_next); 6514 freefrag->ff_jdep = (struct worklist *)cancel_newblk(&oldaip->ai_block, 6515 &freefrag->ff_list, &freefrag->ff_jwork); 6516 free_newblk(&oldaip->ai_block); 6517 6518 return (freefrag); 6519 } 6520 6521 static inline void 6522 setup_freedirect(freeblks, ip, i, needj) 6523 struct freeblks *freeblks; 6524 struct inode *ip; 6525 int i; 6526 int needj; 6527 { 6528 struct ufsmount *ump; 6529 ufs2_daddr_t blkno; 6530 int frags; 6531 6532 blkno = DIP(ip, i_db[i]); 6533 if (blkno == 0) 6534 return; 6535 DIP_SET(ip, i_db[i], 0); 6536 ump = ITOUMP(ip); 6537 frags = sblksize(ump->um_fs, ip->i_size, i); 6538 frags = numfrags(ump->um_fs, frags); 6539 newfreework(ump, freeblks, NULL, i, blkno, frags, 0, needj); 6540 } 6541 6542 static inline void 6543 setup_freeext(freeblks, ip, i, needj) 6544 struct freeblks *freeblks; 6545 struct inode *ip; 6546 int i; 6547 int needj; 6548 { 6549 struct ufsmount *ump; 6550 ufs2_daddr_t blkno; 6551 int frags; 6552 6553 blkno = ip->i_din2->di_extb[i]; 6554 if (blkno == 0) 6555 return; 6556 ip->i_din2->di_extb[i] = 0; 6557 ump = ITOUMP(ip); 6558 frags = sblksize(ump->um_fs, ip->i_din2->di_extsize, i); 6559 frags = numfrags(ump->um_fs, frags); 6560 newfreework(ump, freeblks, NULL, -1 - i, blkno, frags, 0, needj); 6561 } 6562 6563 static inline void 6564 setup_freeindir(freeblks, ip, i, lbn, needj) 6565 struct freeblks *freeblks; 6566 struct inode *ip; 6567 int i; 6568 ufs_lbn_t lbn; 6569 int needj; 6570 { 6571 struct ufsmount *ump; 6572 ufs2_daddr_t blkno; 6573 6574 blkno = DIP(ip, i_ib[i]); 6575 if (blkno == 0) 6576 return; 6577 DIP_SET(ip, i_ib[i], 0); 6578 ump = ITOUMP(ip); 6579 newfreework(ump, freeblks, NULL, lbn, blkno, ump->um_fs->fs_frag, 6580 0, needj); 6581 } 6582 6583 static inline struct freeblks * 6584 newfreeblks(mp, ip) 6585 struct mount *mp; 6586 struct inode *ip; 6587 { 6588 struct freeblks *freeblks; 6589 6590 freeblks = malloc(sizeof(struct freeblks), 6591 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO); 6592 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp); 6593 LIST_INIT(&freeblks->fb_jblkdephd); 6594 LIST_INIT(&freeblks->fb_jwork); 6595 freeblks->fb_ref = 0; 6596 freeblks->fb_cgwait = 0; 6597 freeblks->fb_state = ATTACHED; 6598 freeblks->fb_uid = ip->i_uid; 6599 freeblks->fb_inum = ip->i_number; 6600 freeblks->fb_vtype = ITOV(ip)->v_type; 6601 freeblks->fb_modrev = DIP(ip, i_modrev); 6602 freeblks->fb_devvp = ITODEVVP(ip); 6603 freeblks->fb_chkcnt = 0; 6604 freeblks->fb_len = 0; 6605 6606 return (freeblks); 6607 } 6608 6609 static void 6610 trunc_indirdep(indirdep, freeblks, bp, off) 6611 struct indirdep *indirdep; 6612 struct freeblks *freeblks; 6613 struct buf *bp; 6614 int off; 6615 { 6616 struct allocindir *aip, *aipn; 6617 6618 /* 6619 * The first set of allocindirs won't be in savedbp. 6620 */ 6621 LIST_FOREACH_SAFE(aip, &indirdep->ir_deplisthd, ai_next, aipn) 6622 if (aip->ai_offset > off) 6623 cancel_allocindir(aip, bp, freeblks, 1); 6624 LIST_FOREACH_SAFE(aip, &indirdep->ir_donehd, ai_next, aipn) 6625 if (aip->ai_offset > off) 6626 cancel_allocindir(aip, bp, freeblks, 1); 6627 /* 6628 * These will exist in savedbp. 6629 */ 6630 LIST_FOREACH_SAFE(aip, &indirdep->ir_writehd, ai_next, aipn) 6631 if (aip->ai_offset > off) 6632 cancel_allocindir(aip, NULL, freeblks, 0); 6633 LIST_FOREACH_SAFE(aip, &indirdep->ir_completehd, ai_next, aipn) 6634 if (aip->ai_offset > off) 6635 cancel_allocindir(aip, NULL, freeblks, 0); 6636 } 6637 6638 /* 6639 * Follow the chain of indirects down to lastlbn creating a freework 6640 * structure for each. This will be used to start indir_trunc() at 6641 * the right offset and create the journal records for the parrtial 6642 * truncation. A second step will handle the truncated dependencies. 6643 */ 6644 static int 6645 setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno) 6646 struct freeblks *freeblks; 6647 struct inode *ip; 6648 ufs_lbn_t lbn; 6649 ufs_lbn_t lastlbn; 6650 ufs2_daddr_t blkno; 6651 { 6652 struct indirdep *indirdep; 6653 struct indirdep *indirn; 6654 struct freework *freework; 6655 struct newblk *newblk; 6656 struct mount *mp; 6657 struct ufsmount *ump; 6658 struct buf *bp; 6659 uint8_t *start; 6660 uint8_t *end; 6661 ufs_lbn_t lbnadd; 6662 int level; 6663 int error; 6664 int off; 6665 6666 freework = NULL; 6667 if (blkno == 0) 6668 return (0); 6669 mp = freeblks->fb_list.wk_mp; 6670 ump = VFSTOUFS(mp); 6671 /* 6672 * Here, calls to VOP_BMAP() will fail. However, we already have 6673 * the on-disk address, so we just pass it to bread() instead of 6674 * having bread() attempt to calculate it using VOP_BMAP(). 6675 */ 6676 error = ffs_breadz(ump, ITOV(ip), lbn, blkptrtodb(ump, blkno), 6677 (int)mp->mnt_stat.f_iosize, NULL, NULL, 0, NOCRED, 0, NULL, &bp); 6678 if (error) 6679 return (error); 6680 level = lbn_level(lbn); 6681 lbnadd = lbn_offset(ump->um_fs, level); 6682 /* 6683 * Compute the offset of the last block we want to keep. Store 6684 * in the freework the first block we want to completely free. 6685 */ 6686 off = (lastlbn - -(lbn + level)) / lbnadd; 6687 if (off + 1 == NINDIR(ump->um_fs)) 6688 goto nowork; 6689 freework = newfreework(ump, freeblks, NULL, lbn, blkno, 0, off + 1, 0); 6690 /* 6691 * Link the freework into the indirdep. This will prevent any new 6692 * allocations from proceeding until we are finished with the 6693 * truncate and the block is written. 6694 */ 6695 ACQUIRE_LOCK(ump); 6696 indirdep = indirdep_lookup(mp, ip, bp); 6697 if (indirdep->ir_freeblks) 6698 panic("setup_trunc_indir: indirdep already truncated."); 6699 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, freework, fw_next); 6700 freework->fw_indir = indirdep; 6701 /* 6702 * Cancel any allocindirs that will not make it to disk. 6703 * We have to do this for all copies of the indirdep that 6704 * live on this newblk. 6705 */ 6706 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 6707 if (newblk_lookup(mp, dbtofsb(ump->um_fs, bp->b_blkno), 0, 6708 &newblk) == 0) 6709 panic("setup_trunc_indir: lost block"); 6710 LIST_FOREACH(indirn, &newblk->nb_indirdeps, ir_next) 6711 trunc_indirdep(indirn, freeblks, bp, off); 6712 } else 6713 trunc_indirdep(indirdep, freeblks, bp, off); 6714 FREE_LOCK(ump); 6715 /* 6716 * Creation is protected by the buf lock. The saveddata is only 6717 * needed if a full truncation follows a partial truncation but it 6718 * is difficult to allocate in that case so we fetch it anyway. 6719 */ 6720 if (indirdep->ir_saveddata == NULL) 6721 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 6722 M_SOFTDEP_FLAGS); 6723 nowork: 6724 /* Fetch the blkno of the child and the zero start offset. */ 6725 if (I_IS_UFS1(ip)) { 6726 blkno = ((ufs1_daddr_t *)bp->b_data)[off]; 6727 start = (uint8_t *)&((ufs1_daddr_t *)bp->b_data)[off+1]; 6728 } else { 6729 blkno = ((ufs2_daddr_t *)bp->b_data)[off]; 6730 start = (uint8_t *)&((ufs2_daddr_t *)bp->b_data)[off+1]; 6731 } 6732 if (freework) { 6733 /* Zero the truncated pointers. */ 6734 end = bp->b_data + bp->b_bcount; 6735 bzero(start, end - start); 6736 bdwrite(bp); 6737 } else 6738 bqrelse(bp); 6739 if (level == 0) 6740 return (0); 6741 lbn++; /* adjust level */ 6742 lbn -= (off * lbnadd); 6743 return setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno); 6744 } 6745 6746 /* 6747 * Complete the partial truncation of an indirect block setup by 6748 * setup_trunc_indir(). This zeros the truncated pointers in the saved 6749 * copy and writes them to disk before the freeblks is allowed to complete. 6750 */ 6751 static void 6752 complete_trunc_indir(freework) 6753 struct freework *freework; 6754 { 6755 struct freework *fwn; 6756 struct indirdep *indirdep; 6757 struct ufsmount *ump; 6758 struct buf *bp; 6759 uintptr_t start; 6760 int count; 6761 6762 ump = VFSTOUFS(freework->fw_list.wk_mp); 6763 LOCK_OWNED(ump); 6764 indirdep = freework->fw_indir; 6765 for (;;) { 6766 bp = indirdep->ir_bp; 6767 /* See if the block was discarded. */ 6768 if (bp == NULL) 6769 break; 6770 /* Inline part of getdirtybuf(). We dont want bremfree. */ 6771 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) 6772 break; 6773 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 6774 LOCK_PTR(ump)) == 0) 6775 BUF_UNLOCK(bp); 6776 ACQUIRE_LOCK(ump); 6777 } 6778 freework->fw_state |= DEPCOMPLETE; 6779 TAILQ_REMOVE(&indirdep->ir_trunc, freework, fw_next); 6780 /* 6781 * Zero the pointers in the saved copy. 6782 */ 6783 if (indirdep->ir_state & UFS1FMT) 6784 start = sizeof(ufs1_daddr_t); 6785 else 6786 start = sizeof(ufs2_daddr_t); 6787 start *= freework->fw_start; 6788 count = indirdep->ir_savebp->b_bcount - start; 6789 start += (uintptr_t)indirdep->ir_savebp->b_data; 6790 bzero((char *)start, count); 6791 /* 6792 * We need to start the next truncation in the list if it has not 6793 * been started yet. 6794 */ 6795 fwn = TAILQ_FIRST(&indirdep->ir_trunc); 6796 if (fwn != NULL) { 6797 if (fwn->fw_freeblks == indirdep->ir_freeblks) 6798 TAILQ_REMOVE(&indirdep->ir_trunc, fwn, fw_next); 6799 if ((fwn->fw_state & ONWORKLIST) == 0) 6800 freework_enqueue(fwn); 6801 } 6802 /* 6803 * If bp is NULL the block was fully truncated, restore 6804 * the saved block list otherwise free it if it is no 6805 * longer needed. 6806 */ 6807 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 6808 if (bp == NULL) 6809 bcopy(indirdep->ir_saveddata, 6810 indirdep->ir_savebp->b_data, 6811 indirdep->ir_savebp->b_bcount); 6812 free(indirdep->ir_saveddata, M_INDIRDEP); 6813 indirdep->ir_saveddata = NULL; 6814 } 6815 /* 6816 * When bp is NULL there is a full truncation pending. We 6817 * must wait for this full truncation to be journaled before 6818 * we can release this freework because the disk pointers will 6819 * never be written as zero. 6820 */ 6821 if (bp == NULL) { 6822 if (LIST_EMPTY(&indirdep->ir_freeblks->fb_jblkdephd)) 6823 handle_written_freework(freework); 6824 else 6825 WORKLIST_INSERT(&indirdep->ir_freeblks->fb_freeworkhd, 6826 &freework->fw_list); 6827 if (fwn == NULL) { 6828 freework->fw_indir = (void *)0x0000deadbeef0000; 6829 bp = indirdep->ir_savebp; 6830 indirdep->ir_savebp = NULL; 6831 free_indirdep(indirdep); 6832 FREE_LOCK(ump); 6833 brelse(bp); 6834 ACQUIRE_LOCK(ump); 6835 } 6836 } else { 6837 /* Complete when the real copy is written. */ 6838 WORKLIST_INSERT(&bp->b_dep, &freework->fw_list); 6839 BUF_UNLOCK(bp); 6840 } 6841 } 6842 6843 /* 6844 * Calculate the number of blocks we are going to release where datablocks 6845 * is the current total and length is the new file size. 6846 */ 6847 static ufs2_daddr_t 6848 blkcount(fs, datablocks, length) 6849 struct fs *fs; 6850 ufs2_daddr_t datablocks; 6851 off_t length; 6852 { 6853 off_t totblks, numblks; 6854 6855 totblks = 0; 6856 numblks = howmany(length, fs->fs_bsize); 6857 if (numblks <= UFS_NDADDR) { 6858 totblks = howmany(length, fs->fs_fsize); 6859 goto out; 6860 } 6861 totblks = blkstofrags(fs, numblks); 6862 numblks -= UFS_NDADDR; 6863 /* 6864 * Count all single, then double, then triple indirects required. 6865 * Subtracting one indirects worth of blocks for each pass 6866 * acknowledges one of each pointed to by the inode. 6867 */ 6868 for (;;) { 6869 totblks += blkstofrags(fs, howmany(numblks, NINDIR(fs))); 6870 numblks -= NINDIR(fs); 6871 if (numblks <= 0) 6872 break; 6873 numblks = howmany(numblks, NINDIR(fs)); 6874 } 6875 out: 6876 totblks = fsbtodb(fs, totblks); 6877 /* 6878 * Handle sparse files. We can't reclaim more blocks than the inode 6879 * references. We will correct it later in handle_complete_freeblks() 6880 * when we know the real count. 6881 */ 6882 if (totblks > datablocks) 6883 return (0); 6884 return (datablocks - totblks); 6885 } 6886 6887 /* 6888 * Handle freeblocks for journaled softupdate filesystems. 6889 * 6890 * Contrary to normal softupdates, we must preserve the block pointers in 6891 * indirects until their subordinates are free. This is to avoid journaling 6892 * every block that is freed which may consume more space than the journal 6893 * itself. The recovery program will see the free block journals at the 6894 * base of the truncated area and traverse them to reclaim space. The 6895 * pointers in the inode may be cleared immediately after the journal 6896 * records are written because each direct and indirect pointer in the 6897 * inode is recorded in a journal. This permits full truncation to proceed 6898 * asynchronously. The write order is journal -> inode -> cgs -> indirects. 6899 * 6900 * The algorithm is as follows: 6901 * 1) Traverse the in-memory state and create journal entries to release 6902 * the relevant blocks and full indirect trees. 6903 * 2) Traverse the indirect block chain adding partial truncation freework 6904 * records to indirects in the path to lastlbn. The freework will 6905 * prevent new allocation dependencies from being satisfied in this 6906 * indirect until the truncation completes. 6907 * 3) Read and lock the inode block, performing an update with the new size 6908 * and pointers. This prevents truncated data from becoming valid on 6909 * disk through step 4. 6910 * 4) Reap unsatisfied dependencies that are beyond the truncated area, 6911 * eliminate journal work for those records that do not require it. 6912 * 5) Schedule the journal records to be written followed by the inode block. 6913 * 6) Allocate any necessary frags for the end of file. 6914 * 7) Zero any partially truncated blocks. 6915 * 6916 * From this truncation proceeds asynchronously using the freework and 6917 * indir_trunc machinery. The file will not be extended again into a 6918 * partially truncated indirect block until all work is completed but 6919 * the normal dependency mechanism ensures that it is rolled back/forward 6920 * as appropriate. Further truncation may occur without delay and is 6921 * serialized in indir_trunc(). 6922 */ 6923 void 6924 softdep_journal_freeblocks(ip, cred, length, flags) 6925 struct inode *ip; /* The inode whose length is to be reduced */ 6926 struct ucred *cred; 6927 off_t length; /* The new length for the file */ 6928 int flags; /* IO_EXT and/or IO_NORMAL */ 6929 { 6930 struct freeblks *freeblks, *fbn; 6931 struct worklist *wk, *wkn; 6932 struct inodedep *inodedep; 6933 struct jblkdep *jblkdep; 6934 struct allocdirect *adp, *adpn; 6935 struct ufsmount *ump; 6936 struct fs *fs; 6937 struct buf *bp; 6938 struct vnode *vp; 6939 struct mount *mp; 6940 daddr_t dbn; 6941 ufs2_daddr_t extblocks, datablocks; 6942 ufs_lbn_t tmpval, lbn, lastlbn; 6943 int frags, lastoff, iboff, allocblock, needj, error, i; 6944 6945 ump = ITOUMP(ip); 6946 mp = UFSTOVFS(ump); 6947 fs = ump->um_fs; 6948 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6949 ("softdep_journal_freeblocks called on non-softdep filesystem")); 6950 vp = ITOV(ip); 6951 needj = 1; 6952 iboff = -1; 6953 allocblock = 0; 6954 extblocks = 0; 6955 datablocks = 0; 6956 frags = 0; 6957 freeblks = newfreeblks(mp, ip); 6958 ACQUIRE_LOCK(ump); 6959 /* 6960 * If we're truncating a removed file that will never be written 6961 * we don't need to journal the block frees. The canceled journals 6962 * for the allocations will suffice. 6963 */ 6964 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6965 if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED && 6966 length == 0) 6967 needj = 0; 6968 CTR3(KTR_SUJ, "softdep_journal_freeblks: ip %d length %ld needj %d", 6969 ip->i_number, length, needj); 6970 FREE_LOCK(ump); 6971 /* 6972 * Calculate the lbn that we are truncating to. This results in -1 6973 * if we're truncating the 0 bytes. So it is the last lbn we want 6974 * to keep, not the first lbn we want to truncate. 6975 */ 6976 lastlbn = lblkno(fs, length + fs->fs_bsize - 1) - 1; 6977 lastoff = blkoff(fs, length); 6978 /* 6979 * Compute frags we are keeping in lastlbn. 0 means all. 6980 */ 6981 if (lastlbn >= 0 && lastlbn < UFS_NDADDR) { 6982 frags = fragroundup(fs, lastoff); 6983 /* adp offset of last valid allocdirect. */ 6984 iboff = lastlbn; 6985 } else if (lastlbn > 0) 6986 iboff = UFS_NDADDR; 6987 if (fs->fs_magic == FS_UFS2_MAGIC) 6988 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 6989 /* 6990 * Handle normal data blocks and indirects. This section saves 6991 * values used after the inode update to complete frag and indirect 6992 * truncation. 6993 */ 6994 if ((flags & IO_NORMAL) != 0) { 6995 /* 6996 * Handle truncation of whole direct and indirect blocks. 6997 */ 6998 for (i = iboff + 1; i < UFS_NDADDR; i++) 6999 setup_freedirect(freeblks, ip, i, needj); 7000 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR; 7001 i < UFS_NIADDR; 7002 i++, lbn += tmpval, tmpval *= NINDIR(fs)) { 7003 /* Release a whole indirect tree. */ 7004 if (lbn > lastlbn) { 7005 setup_freeindir(freeblks, ip, i, -lbn -i, 7006 needj); 7007 continue; 7008 } 7009 iboff = i + UFS_NDADDR; 7010 /* 7011 * Traverse partially truncated indirect tree. 7012 */ 7013 if (lbn <= lastlbn && lbn + tmpval - 1 > lastlbn) 7014 setup_trunc_indir(freeblks, ip, -lbn - i, 7015 lastlbn, DIP(ip, i_ib[i])); 7016 } 7017 /* 7018 * Handle partial truncation to a frag boundary. 7019 */ 7020 if (frags) { 7021 ufs2_daddr_t blkno; 7022 long oldfrags; 7023 7024 oldfrags = blksize(fs, ip, lastlbn); 7025 blkno = DIP(ip, i_db[lastlbn]); 7026 if (blkno && oldfrags != frags) { 7027 oldfrags -= frags; 7028 oldfrags = numfrags(fs, oldfrags); 7029 blkno += numfrags(fs, frags); 7030 newfreework(ump, freeblks, NULL, lastlbn, 7031 blkno, oldfrags, 0, needj); 7032 if (needj) 7033 adjust_newfreework(freeblks, 7034 numfrags(fs, frags)); 7035 } else if (blkno == 0) 7036 allocblock = 1; 7037 } 7038 /* 7039 * Add a journal record for partial truncate if we are 7040 * handling indirect blocks. Non-indirects need no extra 7041 * journaling. 7042 */ 7043 if (length != 0 && lastlbn >= UFS_NDADDR) { 7044 UFS_INODE_SET_FLAG(ip, IN_TRUNCATED); 7045 newjtrunc(freeblks, length, 0); 7046 } 7047 ip->i_size = length; 7048 DIP_SET(ip, i_size, ip->i_size); 7049 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE); 7050 datablocks = DIP(ip, i_blocks) - extblocks; 7051 if (length != 0) 7052 datablocks = blkcount(fs, datablocks, length); 7053 freeblks->fb_len = length; 7054 } 7055 if ((flags & IO_EXT) != 0) { 7056 for (i = 0; i < UFS_NXADDR; i++) 7057 setup_freeext(freeblks, ip, i, needj); 7058 ip->i_din2->di_extsize = 0; 7059 datablocks += extblocks; 7060 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE); 7061 } 7062 #ifdef QUOTA 7063 /* Reference the quotas in case the block count is wrong in the end. */ 7064 quotaref(vp, freeblks->fb_quota); 7065 (void) chkdq(ip, -datablocks, NOCRED, FORCE); 7066 #endif 7067 freeblks->fb_chkcnt = -datablocks; 7068 UFS_LOCK(ump); 7069 fs->fs_pendingblocks += datablocks; 7070 UFS_UNLOCK(ump); 7071 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 7072 /* 7073 * Handle truncation of incomplete alloc direct dependencies. We 7074 * hold the inode block locked to prevent incomplete dependencies 7075 * from reaching the disk while we are eliminating those that 7076 * have been truncated. This is a partially inlined ffs_update(). 7077 */ 7078 ufs_itimes(vp); 7079 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED); 7080 dbn = fsbtodb(fs, ino_to_fsba(fs, ip->i_number)); 7081 error = ffs_breadz(ump, ump->um_devvp, dbn, dbn, (int)fs->fs_bsize, 7082 NULL, NULL, 0, cred, 0, NULL, &bp); 7083 if (error) { 7084 softdep_error("softdep_journal_freeblocks", error); 7085 return; 7086 } 7087 if (bp->b_bufsize == fs->fs_bsize) 7088 bp->b_flags |= B_CLUSTEROK; 7089 softdep_update_inodeblock(ip, bp, 0); 7090 if (ump->um_fstype == UFS1) { 7091 *((struct ufs1_dinode *)bp->b_data + 7092 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1; 7093 } else { 7094 ffs_update_dinode_ckhash(fs, ip->i_din2); 7095 *((struct ufs2_dinode *)bp->b_data + 7096 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2; 7097 } 7098 ACQUIRE_LOCK(ump); 7099 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 7100 if ((inodedep->id_state & IOSTARTED) != 0) 7101 panic("softdep_setup_freeblocks: inode busy"); 7102 /* 7103 * Add the freeblks structure to the list of operations that 7104 * must await the zero'ed inode being written to disk. If we 7105 * still have a bitmap dependency (needj), then the inode 7106 * has never been written to disk, so we can process the 7107 * freeblks below once we have deleted the dependencies. 7108 */ 7109 if (needj) 7110 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 7111 else 7112 freeblks->fb_state |= COMPLETE; 7113 if ((flags & IO_NORMAL) != 0) { 7114 TAILQ_FOREACH_SAFE(adp, &inodedep->id_inoupdt, ad_next, adpn) { 7115 if (adp->ad_offset > iboff) 7116 cancel_allocdirect(&inodedep->id_inoupdt, adp, 7117 freeblks); 7118 /* 7119 * Truncate the allocdirect. We could eliminate 7120 * or modify journal records as well. 7121 */ 7122 else if (adp->ad_offset == iboff && frags) 7123 adp->ad_newsize = frags; 7124 } 7125 } 7126 if ((flags & IO_EXT) != 0) 7127 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 7128 cancel_allocdirect(&inodedep->id_extupdt, adp, 7129 freeblks); 7130 /* 7131 * Scan the bufwait list for newblock dependencies that will never 7132 * make it to disk. 7133 */ 7134 LIST_FOREACH_SAFE(wk, &inodedep->id_bufwait, wk_list, wkn) { 7135 if (wk->wk_type != D_ALLOCDIRECT) 7136 continue; 7137 adp = WK_ALLOCDIRECT(wk); 7138 if (((flags & IO_NORMAL) != 0 && (adp->ad_offset > iboff)) || 7139 ((flags & IO_EXT) != 0 && (adp->ad_state & EXTDATA))) { 7140 cancel_jfreeblk(freeblks, adp->ad_newblkno); 7141 cancel_newblk(WK_NEWBLK(wk), NULL, &freeblks->fb_jwork); 7142 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 7143 } 7144 } 7145 /* 7146 * Add journal work. 7147 */ 7148 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) 7149 add_to_journal(&jblkdep->jb_list); 7150 FREE_LOCK(ump); 7151 bdwrite(bp); 7152 /* 7153 * Truncate dependency structures beyond length. 7154 */ 7155 trunc_dependencies(ip, freeblks, lastlbn, frags, flags); 7156 /* 7157 * This is only set when we need to allocate a fragment because 7158 * none existed at the end of a frag-sized file. It handles only 7159 * allocating a new, zero filled block. 7160 */ 7161 if (allocblock) { 7162 ip->i_size = length - lastoff; 7163 DIP_SET(ip, i_size, ip->i_size); 7164 error = UFS_BALLOC(vp, length - 1, 1, cred, BA_CLRBUF, &bp); 7165 if (error != 0) { 7166 softdep_error("softdep_journal_freeblks", error); 7167 return; 7168 } 7169 ip->i_size = length; 7170 DIP_SET(ip, i_size, length); 7171 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE); 7172 allocbuf(bp, frags); 7173 ffs_update(vp, 0); 7174 bawrite(bp); 7175 } else if (lastoff != 0 && vp->v_type != VDIR) { 7176 int size; 7177 7178 /* 7179 * Zero the end of a truncated frag or block. 7180 */ 7181 size = sblksize(fs, length, lastlbn); 7182 error = bread(vp, lastlbn, size, cred, &bp); 7183 if (error == 0) { 7184 bzero((char *)bp->b_data + lastoff, size - lastoff); 7185 bawrite(bp); 7186 } else if (!ffs_fsfail_cleanup(ump, error)) { 7187 softdep_error("softdep_journal_freeblks", error); 7188 return; 7189 } 7190 } 7191 ACQUIRE_LOCK(ump); 7192 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 7193 TAILQ_INSERT_TAIL(&inodedep->id_freeblklst, freeblks, fb_next); 7194 freeblks->fb_state |= DEPCOMPLETE | ONDEPLIST; 7195 /* 7196 * We zero earlier truncations so they don't erroneously 7197 * update i_blocks. 7198 */ 7199 if (freeblks->fb_len == 0 && (flags & IO_NORMAL) != 0) 7200 TAILQ_FOREACH(fbn, &inodedep->id_freeblklst, fb_next) 7201 fbn->fb_len = 0; 7202 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE && 7203 LIST_EMPTY(&freeblks->fb_jblkdephd)) 7204 freeblks->fb_state |= INPROGRESS; 7205 else 7206 freeblks = NULL; 7207 FREE_LOCK(ump); 7208 if (freeblks) 7209 handle_workitem_freeblocks(freeblks, 0); 7210 trunc_pages(ip, length, extblocks, flags); 7211 7212 } 7213 7214 /* 7215 * Flush a JOP_SYNC to the journal. 7216 */ 7217 void 7218 softdep_journal_fsync(ip) 7219 struct inode *ip; 7220 { 7221 struct jfsync *jfsync; 7222 struct ufsmount *ump; 7223 7224 ump = ITOUMP(ip); 7225 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 7226 ("softdep_journal_fsync called on non-softdep filesystem")); 7227 if ((ip->i_flag & IN_TRUNCATED) == 0) 7228 return; 7229 ip->i_flag &= ~IN_TRUNCATED; 7230 jfsync = malloc(sizeof(*jfsync), M_JFSYNC, M_SOFTDEP_FLAGS | M_ZERO); 7231 workitem_alloc(&jfsync->jfs_list, D_JFSYNC, UFSTOVFS(ump)); 7232 jfsync->jfs_size = ip->i_size; 7233 jfsync->jfs_ino = ip->i_number; 7234 ACQUIRE_LOCK(ump); 7235 add_to_journal(&jfsync->jfs_list); 7236 jwait(&jfsync->jfs_list, MNT_WAIT); 7237 FREE_LOCK(ump); 7238 } 7239 7240 /* 7241 * Block de-allocation dependencies. 7242 * 7243 * When blocks are de-allocated, the on-disk pointers must be nullified before 7244 * the blocks are made available for use by other files. (The true 7245 * requirement is that old pointers must be nullified before new on-disk 7246 * pointers are set. We chose this slightly more stringent requirement to 7247 * reduce complexity.) Our implementation handles this dependency by updating 7248 * the inode (or indirect block) appropriately but delaying the actual block 7249 * de-allocation (i.e., freemap and free space count manipulation) until 7250 * after the updated versions reach stable storage. After the disk is 7251 * updated, the blocks can be safely de-allocated whenever it is convenient. 7252 * This implementation handles only the common case of reducing a file's 7253 * length to zero. Other cases are handled by the conventional synchronous 7254 * write approach. 7255 * 7256 * The ffs implementation with which we worked double-checks 7257 * the state of the block pointers and file size as it reduces 7258 * a file's length. Some of this code is replicated here in our 7259 * soft updates implementation. The freeblks->fb_chkcnt field is 7260 * used to transfer a part of this information to the procedure 7261 * that eventually de-allocates the blocks. 7262 * 7263 * This routine should be called from the routine that shortens 7264 * a file's length, before the inode's size or block pointers 7265 * are modified. It will save the block pointer information for 7266 * later release and zero the inode so that the calling routine 7267 * can release it. 7268 */ 7269 void 7270 softdep_setup_freeblocks(ip, length, flags) 7271 struct inode *ip; /* The inode whose length is to be reduced */ 7272 off_t length; /* The new length for the file */ 7273 int flags; /* IO_EXT and/or IO_NORMAL */ 7274 { 7275 struct ufs1_dinode *dp1; 7276 struct ufs2_dinode *dp2; 7277 struct freeblks *freeblks; 7278 struct inodedep *inodedep; 7279 struct allocdirect *adp; 7280 struct ufsmount *ump; 7281 struct buf *bp; 7282 struct fs *fs; 7283 ufs2_daddr_t extblocks, datablocks; 7284 struct mount *mp; 7285 int i, delay, error; 7286 ufs_lbn_t tmpval; 7287 ufs_lbn_t lbn; 7288 7289 ump = ITOUMP(ip); 7290 mp = UFSTOVFS(ump); 7291 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 7292 ("softdep_setup_freeblocks called on non-softdep filesystem")); 7293 CTR2(KTR_SUJ, "softdep_setup_freeblks: ip %d length %ld", 7294 ip->i_number, length); 7295 KASSERT(length == 0, ("softdep_setup_freeblocks: non-zero length")); 7296 fs = ump->um_fs; 7297 if ((error = bread(ump->um_devvp, 7298 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 7299 (int)fs->fs_bsize, NOCRED, &bp)) != 0) { 7300 if (!ffs_fsfail_cleanup(ump, error)) 7301 softdep_error("softdep_setup_freeblocks", error); 7302 return; 7303 } 7304 freeblks = newfreeblks(mp, ip); 7305 extblocks = 0; 7306 datablocks = 0; 7307 if (fs->fs_magic == FS_UFS2_MAGIC) 7308 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 7309 if ((flags & IO_NORMAL) != 0) { 7310 for (i = 0; i < UFS_NDADDR; i++) 7311 setup_freedirect(freeblks, ip, i, 0); 7312 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR; 7313 i < UFS_NIADDR; 7314 i++, lbn += tmpval, tmpval *= NINDIR(fs)) 7315 setup_freeindir(freeblks, ip, i, -lbn -i, 0); 7316 ip->i_size = 0; 7317 DIP_SET(ip, i_size, 0); 7318 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE); 7319 datablocks = DIP(ip, i_blocks) - extblocks; 7320 } 7321 if ((flags & IO_EXT) != 0) { 7322 for (i = 0; i < UFS_NXADDR; i++) 7323 setup_freeext(freeblks, ip, i, 0); 7324 ip->i_din2->di_extsize = 0; 7325 datablocks += extblocks; 7326 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE); 7327 } 7328 #ifdef QUOTA 7329 /* Reference the quotas in case the block count is wrong in the end. */ 7330 quotaref(ITOV(ip), freeblks->fb_quota); 7331 (void) chkdq(ip, -datablocks, NOCRED, FORCE); 7332 #endif 7333 freeblks->fb_chkcnt = -datablocks; 7334 UFS_LOCK(ump); 7335 fs->fs_pendingblocks += datablocks; 7336 UFS_UNLOCK(ump); 7337 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 7338 /* 7339 * Push the zero'ed inode to its disk buffer so that we are free 7340 * to delete its dependencies below. Once the dependencies are gone 7341 * the buffer can be safely released. 7342 */ 7343 if (ump->um_fstype == UFS1) { 7344 dp1 = ((struct ufs1_dinode *)bp->b_data + 7345 ino_to_fsbo(fs, ip->i_number)); 7346 ip->i_din1->di_freelink = dp1->di_freelink; 7347 *dp1 = *ip->i_din1; 7348 } else { 7349 dp2 = ((struct ufs2_dinode *)bp->b_data + 7350 ino_to_fsbo(fs, ip->i_number)); 7351 ip->i_din2->di_freelink = dp2->di_freelink; 7352 ffs_update_dinode_ckhash(fs, ip->i_din2); 7353 *dp2 = *ip->i_din2; 7354 } 7355 /* 7356 * Find and eliminate any inode dependencies. 7357 */ 7358 ACQUIRE_LOCK(ump); 7359 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 7360 if ((inodedep->id_state & IOSTARTED) != 0) 7361 panic("softdep_setup_freeblocks: inode busy"); 7362 /* 7363 * Add the freeblks structure to the list of operations that 7364 * must await the zero'ed inode being written to disk. If we 7365 * still have a bitmap dependency (delay == 0), then the inode 7366 * has never been written to disk, so we can process the 7367 * freeblks below once we have deleted the dependencies. 7368 */ 7369 delay = (inodedep->id_state & DEPCOMPLETE); 7370 if (delay) 7371 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 7372 else 7373 freeblks->fb_state |= COMPLETE; 7374 /* 7375 * Because the file length has been truncated to zero, any 7376 * pending block allocation dependency structures associated 7377 * with this inode are obsolete and can simply be de-allocated. 7378 * We must first merge the two dependency lists to get rid of 7379 * any duplicate freefrag structures, then purge the merged list. 7380 * If we still have a bitmap dependency, then the inode has never 7381 * been written to disk, so we can free any fragments without delay. 7382 */ 7383 if (flags & IO_NORMAL) { 7384 merge_inode_lists(&inodedep->id_newinoupdt, 7385 &inodedep->id_inoupdt); 7386 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 7387 cancel_allocdirect(&inodedep->id_inoupdt, adp, 7388 freeblks); 7389 } 7390 if (flags & IO_EXT) { 7391 merge_inode_lists(&inodedep->id_newextupdt, 7392 &inodedep->id_extupdt); 7393 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 7394 cancel_allocdirect(&inodedep->id_extupdt, adp, 7395 freeblks); 7396 } 7397 FREE_LOCK(ump); 7398 bdwrite(bp); 7399 trunc_dependencies(ip, freeblks, -1, 0, flags); 7400 ACQUIRE_LOCK(ump); 7401 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 7402 (void) free_inodedep(inodedep); 7403 freeblks->fb_state |= DEPCOMPLETE; 7404 /* 7405 * If the inode with zeroed block pointers is now on disk 7406 * we can start freeing blocks. 7407 */ 7408 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 7409 freeblks->fb_state |= INPROGRESS; 7410 else 7411 freeblks = NULL; 7412 FREE_LOCK(ump); 7413 if (freeblks) 7414 handle_workitem_freeblocks(freeblks, 0); 7415 trunc_pages(ip, length, extblocks, flags); 7416 } 7417 7418 /* 7419 * Eliminate pages from the page cache that back parts of this inode and 7420 * adjust the vnode pager's idea of our size. This prevents stale data 7421 * from hanging around in the page cache. 7422 */ 7423 static void 7424 trunc_pages(ip, length, extblocks, flags) 7425 struct inode *ip; 7426 off_t length; 7427 ufs2_daddr_t extblocks; 7428 int flags; 7429 { 7430 struct vnode *vp; 7431 struct fs *fs; 7432 ufs_lbn_t lbn; 7433 off_t end, extend; 7434 7435 vp = ITOV(ip); 7436 fs = ITOFS(ip); 7437 extend = OFF_TO_IDX(lblktosize(fs, -extblocks)); 7438 if ((flags & IO_EXT) != 0) 7439 vn_pages_remove(vp, extend, 0); 7440 if ((flags & IO_NORMAL) == 0) 7441 return; 7442 BO_LOCK(&vp->v_bufobj); 7443 drain_output(vp); 7444 BO_UNLOCK(&vp->v_bufobj); 7445 /* 7446 * The vnode pager eliminates file pages we eliminate indirects 7447 * below. 7448 */ 7449 vnode_pager_setsize(vp, length); 7450 /* 7451 * Calculate the end based on the last indirect we want to keep. If 7452 * the block extends into indirects we can just use the negative of 7453 * its lbn. Doubles and triples exist at lower numbers so we must 7454 * be careful not to remove those, if they exist. double and triple 7455 * indirect lbns do not overlap with others so it is not important 7456 * to verify how many levels are required. 7457 */ 7458 lbn = lblkno(fs, length); 7459 if (lbn >= UFS_NDADDR) { 7460 /* Calculate the virtual lbn of the triple indirect. */ 7461 lbn = -lbn - (UFS_NIADDR - 1); 7462 end = OFF_TO_IDX(lblktosize(fs, lbn)); 7463 } else 7464 end = extend; 7465 vn_pages_remove(vp, OFF_TO_IDX(OFF_MAX), end); 7466 } 7467 7468 /* 7469 * See if the buf bp is in the range eliminated by truncation. 7470 */ 7471 static int 7472 trunc_check_buf(bp, blkoffp, lastlbn, lastoff, flags) 7473 struct buf *bp; 7474 int *blkoffp; 7475 ufs_lbn_t lastlbn; 7476 int lastoff; 7477 int flags; 7478 { 7479 ufs_lbn_t lbn; 7480 7481 *blkoffp = 0; 7482 /* Only match ext/normal blocks as appropriate. */ 7483 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) || 7484 ((flags & IO_NORMAL) == 0 && (bp->b_xflags & BX_ALTDATA) == 0)) 7485 return (0); 7486 /* ALTDATA is always a full truncation. */ 7487 if ((bp->b_xflags & BX_ALTDATA) != 0) 7488 return (1); 7489 /* -1 is full truncation. */ 7490 if (lastlbn == -1) 7491 return (1); 7492 /* 7493 * If this is a partial truncate we only want those 7494 * blocks and indirect blocks that cover the range 7495 * we're after. 7496 */ 7497 lbn = bp->b_lblkno; 7498 if (lbn < 0) 7499 lbn = -(lbn + lbn_level(lbn)); 7500 if (lbn < lastlbn) 7501 return (0); 7502 /* Here we only truncate lblkno if it's partial. */ 7503 if (lbn == lastlbn) { 7504 if (lastoff == 0) 7505 return (0); 7506 *blkoffp = lastoff; 7507 } 7508 return (1); 7509 } 7510 7511 /* 7512 * Eliminate any dependencies that exist in memory beyond lblkno:off 7513 */ 7514 static void 7515 trunc_dependencies(ip, freeblks, lastlbn, lastoff, flags) 7516 struct inode *ip; 7517 struct freeblks *freeblks; 7518 ufs_lbn_t lastlbn; 7519 int lastoff; 7520 int flags; 7521 { 7522 struct bufobj *bo; 7523 struct vnode *vp; 7524 struct buf *bp; 7525 int blkoff; 7526 7527 /* 7528 * We must wait for any I/O in progress to finish so that 7529 * all potential buffers on the dirty list will be visible. 7530 * Once they are all there, walk the list and get rid of 7531 * any dependencies. 7532 */ 7533 vp = ITOV(ip); 7534 bo = &vp->v_bufobj; 7535 BO_LOCK(bo); 7536 drain_output(vp); 7537 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) 7538 bp->b_vflags &= ~BV_SCANNED; 7539 restart: 7540 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) { 7541 if (bp->b_vflags & BV_SCANNED) 7542 continue; 7543 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 7544 bp->b_vflags |= BV_SCANNED; 7545 continue; 7546 } 7547 KASSERT(bp->b_bufobj == bo, ("Wrong object in buffer")); 7548 if ((bp = getdirtybuf(bp, BO_LOCKPTR(bo), MNT_WAIT)) == NULL) 7549 goto restart; 7550 BO_UNLOCK(bo); 7551 if (deallocate_dependencies(bp, freeblks, blkoff)) 7552 bqrelse(bp); 7553 else 7554 brelse(bp); 7555 BO_LOCK(bo); 7556 goto restart; 7557 } 7558 /* 7559 * Now do the work of vtruncbuf while also matching indirect blocks. 7560 */ 7561 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) 7562 bp->b_vflags &= ~BV_SCANNED; 7563 cleanrestart: 7564 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) { 7565 if (bp->b_vflags & BV_SCANNED) 7566 continue; 7567 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 7568 bp->b_vflags |= BV_SCANNED; 7569 continue; 7570 } 7571 if (BUF_LOCK(bp, 7572 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 7573 BO_LOCKPTR(bo)) == ENOLCK) { 7574 BO_LOCK(bo); 7575 goto cleanrestart; 7576 } 7577 BO_LOCK(bo); 7578 bp->b_vflags |= BV_SCANNED; 7579 BO_UNLOCK(bo); 7580 bremfree(bp); 7581 if (blkoff != 0) { 7582 allocbuf(bp, blkoff); 7583 bqrelse(bp); 7584 } else { 7585 bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF; 7586 brelse(bp); 7587 } 7588 BO_LOCK(bo); 7589 goto cleanrestart; 7590 } 7591 drain_output(vp); 7592 BO_UNLOCK(bo); 7593 } 7594 7595 static int 7596 cancel_pagedep(pagedep, freeblks, blkoff) 7597 struct pagedep *pagedep; 7598 struct freeblks *freeblks; 7599 int blkoff; 7600 { 7601 struct jremref *jremref; 7602 struct jmvref *jmvref; 7603 struct dirrem *dirrem, *tmp; 7604 int i; 7605 7606 /* 7607 * Copy any directory remove dependencies to the list 7608 * to be processed after the freeblks proceeds. If 7609 * directory entry never made it to disk they 7610 * can be dumped directly onto the work list. 7611 */ 7612 LIST_FOREACH_SAFE(dirrem, &pagedep->pd_dirremhd, dm_next, tmp) { 7613 /* Skip this directory removal if it is intended to remain. */ 7614 if (dirrem->dm_offset < blkoff) 7615 continue; 7616 /* 7617 * If there are any dirrems we wait for the journal write 7618 * to complete and then restart the buf scan as the lock 7619 * has been dropped. 7620 */ 7621 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) { 7622 jwait(&jremref->jr_list, MNT_WAIT); 7623 return (ERESTART); 7624 } 7625 LIST_REMOVE(dirrem, dm_next); 7626 dirrem->dm_dirinum = pagedep->pd_ino; 7627 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list); 7628 } 7629 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) { 7630 jwait(&jmvref->jm_list, MNT_WAIT); 7631 return (ERESTART); 7632 } 7633 /* 7634 * When we're partially truncating a pagedep we just want to flush 7635 * journal entries and return. There can not be any adds in the 7636 * truncated portion of the directory and newblk must remain if 7637 * part of the block remains. 7638 */ 7639 if (blkoff != 0) { 7640 struct diradd *dap; 7641 7642 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 7643 if (dap->da_offset > blkoff) 7644 panic("cancel_pagedep: diradd %p off %d > %d", 7645 dap, dap->da_offset, blkoff); 7646 for (i = 0; i < DAHASHSZ; i++) 7647 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) 7648 if (dap->da_offset > blkoff) 7649 panic("cancel_pagedep: diradd %p off %d > %d", 7650 dap, dap->da_offset, blkoff); 7651 return (0); 7652 } 7653 /* 7654 * There should be no directory add dependencies present 7655 * as the directory could not be truncated until all 7656 * children were removed. 7657 */ 7658 KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL, 7659 ("deallocate_dependencies: pendinghd != NULL")); 7660 for (i = 0; i < DAHASHSZ; i++) 7661 KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL, 7662 ("deallocate_dependencies: diraddhd != NULL")); 7663 if ((pagedep->pd_state & NEWBLOCK) != 0) 7664 free_newdirblk(pagedep->pd_newdirblk); 7665 if (free_pagedep(pagedep) == 0) 7666 panic("Failed to free pagedep %p", pagedep); 7667 return (0); 7668 } 7669 7670 /* 7671 * Reclaim any dependency structures from a buffer that is about to 7672 * be reallocated to a new vnode. The buffer must be locked, thus, 7673 * no I/O completion operations can occur while we are manipulating 7674 * its associated dependencies. The mutex is held so that other I/O's 7675 * associated with related dependencies do not occur. 7676 */ 7677 static int 7678 deallocate_dependencies(bp, freeblks, off) 7679 struct buf *bp; 7680 struct freeblks *freeblks; 7681 int off; 7682 { 7683 struct indirdep *indirdep; 7684 struct pagedep *pagedep; 7685 struct worklist *wk, *wkn; 7686 struct ufsmount *ump; 7687 7688 ump = softdep_bp_to_mp(bp); 7689 if (ump == NULL) 7690 goto done; 7691 ACQUIRE_LOCK(ump); 7692 LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) { 7693 switch (wk->wk_type) { 7694 case D_INDIRDEP: 7695 indirdep = WK_INDIRDEP(wk); 7696 if (bp->b_lblkno >= 0 || 7697 bp->b_blkno != indirdep->ir_savebp->b_lblkno) 7698 panic("deallocate_dependencies: not indir"); 7699 cancel_indirdep(indirdep, bp, freeblks); 7700 continue; 7701 7702 case D_PAGEDEP: 7703 pagedep = WK_PAGEDEP(wk); 7704 if (cancel_pagedep(pagedep, freeblks, off)) { 7705 FREE_LOCK(ump); 7706 return (ERESTART); 7707 } 7708 continue; 7709 7710 case D_ALLOCINDIR: 7711 /* 7712 * Simply remove the allocindir, we'll find it via 7713 * the indirdep where we can clear pointers if 7714 * needed. 7715 */ 7716 WORKLIST_REMOVE(wk); 7717 continue; 7718 7719 case D_FREEWORK: 7720 /* 7721 * A truncation is waiting for the zero'd pointers 7722 * to be written. It can be freed when the freeblks 7723 * is journaled. 7724 */ 7725 WORKLIST_REMOVE(wk); 7726 wk->wk_state |= ONDEPLIST; 7727 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 7728 break; 7729 7730 case D_ALLOCDIRECT: 7731 if (off != 0) 7732 continue; 7733 /* FALLTHROUGH */ 7734 default: 7735 panic("deallocate_dependencies: Unexpected type %s", 7736 TYPENAME(wk->wk_type)); 7737 /* NOTREACHED */ 7738 } 7739 } 7740 FREE_LOCK(ump); 7741 done: 7742 /* 7743 * Don't throw away this buf, we were partially truncating and 7744 * some deps may always remain. 7745 */ 7746 if (off) { 7747 allocbuf(bp, off); 7748 bp->b_vflags |= BV_SCANNED; 7749 return (EBUSY); 7750 } 7751 bp->b_flags |= B_INVAL | B_NOCACHE; 7752 7753 return (0); 7754 } 7755 7756 /* 7757 * An allocdirect is being canceled due to a truncate. We must make sure 7758 * the journal entry is released in concert with the blkfree that releases 7759 * the storage. Completed journal entries must not be released until the 7760 * space is no longer pointed to by the inode or in the bitmap. 7761 */ 7762 static void 7763 cancel_allocdirect(adphead, adp, freeblks) 7764 struct allocdirectlst *adphead; 7765 struct allocdirect *adp; 7766 struct freeblks *freeblks; 7767 { 7768 struct freework *freework; 7769 struct newblk *newblk; 7770 struct worklist *wk; 7771 7772 TAILQ_REMOVE(adphead, adp, ad_next); 7773 newblk = (struct newblk *)adp; 7774 freework = NULL; 7775 /* 7776 * Find the correct freework structure. 7777 */ 7778 LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) { 7779 if (wk->wk_type != D_FREEWORK) 7780 continue; 7781 freework = WK_FREEWORK(wk); 7782 if (freework->fw_blkno == newblk->nb_newblkno) 7783 break; 7784 } 7785 if (freework == NULL) 7786 panic("cancel_allocdirect: Freework not found"); 7787 /* 7788 * If a newblk exists at all we still have the journal entry that 7789 * initiated the allocation so we do not need to journal the free. 7790 */ 7791 cancel_jfreeblk(freeblks, freework->fw_blkno); 7792 /* 7793 * If the journal hasn't been written the jnewblk must be passed 7794 * to the call to ffs_blkfree that reclaims the space. We accomplish 7795 * this by linking the journal dependency into the freework to be 7796 * freed when freework_freeblock() is called. If the journal has 7797 * been written we can simply reclaim the journal space when the 7798 * freeblks work is complete. 7799 */ 7800 freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list, 7801 &freeblks->fb_jwork); 7802 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 7803 } 7804 7805 /* 7806 * Cancel a new block allocation. May be an indirect or direct block. We 7807 * remove it from various lists and return any journal record that needs to 7808 * be resolved by the caller. 7809 * 7810 * A special consideration is made for indirects which were never pointed 7811 * at on disk and will never be found once this block is released. 7812 */ 7813 static struct jnewblk * 7814 cancel_newblk(newblk, wk, wkhd) 7815 struct newblk *newblk; 7816 struct worklist *wk; 7817 struct workhead *wkhd; 7818 { 7819 struct jnewblk *jnewblk; 7820 7821 CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno); 7822 7823 newblk->nb_state |= GOINGAWAY; 7824 /* 7825 * Previously we traversed the completedhd on each indirdep 7826 * attached to this newblk to cancel them and gather journal 7827 * work. Since we need only the oldest journal segment and 7828 * the lowest point on the tree will always have the oldest 7829 * journal segment we are free to release the segments 7830 * of any subordinates and may leave the indirdep list to 7831 * indirdep_complete() when this newblk is freed. 7832 */ 7833 if (newblk->nb_state & ONDEPLIST) { 7834 newblk->nb_state &= ~ONDEPLIST; 7835 LIST_REMOVE(newblk, nb_deps); 7836 } 7837 if (newblk->nb_state & ONWORKLIST) 7838 WORKLIST_REMOVE(&newblk->nb_list); 7839 /* 7840 * If the journal entry hasn't been written we save a pointer to 7841 * the dependency that frees it until it is written or the 7842 * superseding operation completes. 7843 */ 7844 jnewblk = newblk->nb_jnewblk; 7845 if (jnewblk != NULL && wk != NULL) { 7846 newblk->nb_jnewblk = NULL; 7847 jnewblk->jn_dep = wk; 7848 } 7849 if (!LIST_EMPTY(&newblk->nb_jwork)) 7850 jwork_move(wkhd, &newblk->nb_jwork); 7851 /* 7852 * When truncating we must free the newdirblk early to remove 7853 * the pagedep from the hash before returning. 7854 */ 7855 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7856 free_newdirblk(WK_NEWDIRBLK(wk)); 7857 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7858 panic("cancel_newblk: extra newdirblk"); 7859 7860 return (jnewblk); 7861 } 7862 7863 /* 7864 * Schedule the freefrag associated with a newblk to be released once 7865 * the pointers are written and the previous block is no longer needed. 7866 */ 7867 static void 7868 newblk_freefrag(newblk) 7869 struct newblk *newblk; 7870 { 7871 struct freefrag *freefrag; 7872 7873 if (newblk->nb_freefrag == NULL) 7874 return; 7875 freefrag = newblk->nb_freefrag; 7876 newblk->nb_freefrag = NULL; 7877 freefrag->ff_state |= COMPLETE; 7878 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 7879 add_to_worklist(&freefrag->ff_list, 0); 7880 } 7881 7882 /* 7883 * Free a newblk. Generate a new freefrag work request if appropriate. 7884 * This must be called after the inode pointer and any direct block pointers 7885 * are valid or fully removed via truncate or frag extension. 7886 */ 7887 static void 7888 free_newblk(newblk) 7889 struct newblk *newblk; 7890 { 7891 struct indirdep *indirdep; 7892 struct worklist *wk; 7893 7894 KASSERT(newblk->nb_jnewblk == NULL, 7895 ("free_newblk: jnewblk %p still attached", newblk->nb_jnewblk)); 7896 KASSERT(newblk->nb_list.wk_type != D_NEWBLK, 7897 ("free_newblk: unclaimed newblk")); 7898 LOCK_OWNED(VFSTOUFS(newblk->nb_list.wk_mp)); 7899 newblk_freefrag(newblk); 7900 if (newblk->nb_state & ONDEPLIST) 7901 LIST_REMOVE(newblk, nb_deps); 7902 if (newblk->nb_state & ONWORKLIST) 7903 WORKLIST_REMOVE(&newblk->nb_list); 7904 LIST_REMOVE(newblk, nb_hash); 7905 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7906 free_newdirblk(WK_NEWDIRBLK(wk)); 7907 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7908 panic("free_newblk: extra newdirblk"); 7909 while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL) 7910 indirdep_complete(indirdep); 7911 handle_jwork(&newblk->nb_jwork); 7912 WORKITEM_FREE(newblk, D_NEWBLK); 7913 } 7914 7915 /* 7916 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep. 7917 */ 7918 static void 7919 free_newdirblk(newdirblk) 7920 struct newdirblk *newdirblk; 7921 { 7922 struct pagedep *pagedep; 7923 struct diradd *dap; 7924 struct worklist *wk; 7925 7926 LOCK_OWNED(VFSTOUFS(newdirblk->db_list.wk_mp)); 7927 WORKLIST_REMOVE(&newdirblk->db_list); 7928 /* 7929 * If the pagedep is still linked onto the directory buffer 7930 * dependency chain, then some of the entries on the 7931 * pd_pendinghd list may not be committed to disk yet. In 7932 * this case, we will simply clear the NEWBLOCK flag and 7933 * let the pd_pendinghd list be processed when the pagedep 7934 * is next written. If the pagedep is no longer on the buffer 7935 * dependency chain, then all the entries on the pd_pending 7936 * list are committed to disk and we can free them here. 7937 */ 7938 pagedep = newdirblk->db_pagedep; 7939 pagedep->pd_state &= ~NEWBLOCK; 7940 if ((pagedep->pd_state & ONWORKLIST) == 0) { 7941 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 7942 free_diradd(dap, NULL); 7943 /* 7944 * If no dependencies remain, the pagedep will be freed. 7945 */ 7946 free_pagedep(pagedep); 7947 } 7948 /* Should only ever be one item in the list. */ 7949 while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) { 7950 WORKLIST_REMOVE(wk); 7951 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 7952 } 7953 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 7954 } 7955 7956 /* 7957 * Prepare an inode to be freed. The actual free operation is not 7958 * done until the zero'ed inode has been written to disk. 7959 */ 7960 void 7961 softdep_freefile(pvp, ino, mode) 7962 struct vnode *pvp; 7963 ino_t ino; 7964 int mode; 7965 { 7966 struct inode *ip = VTOI(pvp); 7967 struct inodedep *inodedep; 7968 struct freefile *freefile; 7969 struct freeblks *freeblks; 7970 struct ufsmount *ump; 7971 7972 ump = ITOUMP(ip); 7973 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 7974 ("softdep_freefile called on non-softdep filesystem")); 7975 /* 7976 * This sets up the inode de-allocation dependency. 7977 */ 7978 freefile = malloc(sizeof(struct freefile), 7979 M_FREEFILE, M_SOFTDEP_FLAGS); 7980 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount); 7981 freefile->fx_mode = mode; 7982 freefile->fx_oldinum = ino; 7983 freefile->fx_devvp = ump->um_devvp; 7984 LIST_INIT(&freefile->fx_jwork); 7985 UFS_LOCK(ump); 7986 ump->um_fs->fs_pendinginodes += 1; 7987 UFS_UNLOCK(ump); 7988 7989 /* 7990 * If the inodedep does not exist, then the zero'ed inode has 7991 * been written to disk. If the allocated inode has never been 7992 * written to disk, then the on-disk inode is zero'ed. In either 7993 * case we can free the file immediately. If the journal was 7994 * canceled before being written the inode will never make it to 7995 * disk and we must send the canceled journal entrys to 7996 * ffs_freefile() to be cleared in conjunction with the bitmap. 7997 * Any blocks waiting on the inode to write can be safely freed 7998 * here as it will never been written. 7999 */ 8000 ACQUIRE_LOCK(ump); 8001 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 8002 if (inodedep) { 8003 /* 8004 * Clear out freeblks that no longer need to reference 8005 * this inode. 8006 */ 8007 while ((freeblks = 8008 TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) { 8009 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, 8010 fb_next); 8011 freeblks->fb_state &= ~ONDEPLIST; 8012 } 8013 /* 8014 * Remove this inode from the unlinked list. 8015 */ 8016 if (inodedep->id_state & UNLINKED) { 8017 /* 8018 * Save the journal work to be freed with the bitmap 8019 * before we clear UNLINKED. Otherwise it can be lost 8020 * if the inode block is written. 8021 */ 8022 handle_bufwait(inodedep, &freefile->fx_jwork); 8023 clear_unlinked_inodedep(inodedep); 8024 /* 8025 * Re-acquire inodedep as we've dropped the 8026 * per-filesystem lock in clear_unlinked_inodedep(). 8027 */ 8028 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 8029 } 8030 } 8031 if (inodedep == NULL || check_inode_unwritten(inodedep)) { 8032 FREE_LOCK(ump); 8033 handle_workitem_freefile(freefile); 8034 return; 8035 } 8036 if ((inodedep->id_state & DEPCOMPLETE) == 0) 8037 inodedep->id_state |= GOINGAWAY; 8038 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list); 8039 FREE_LOCK(ump); 8040 if (ip->i_number == ino) 8041 UFS_INODE_SET_FLAG(ip, IN_MODIFIED); 8042 } 8043 8044 /* 8045 * Check to see if an inode has never been written to disk. If 8046 * so free the inodedep and return success, otherwise return failure. 8047 * 8048 * If we still have a bitmap dependency, then the inode has never 8049 * been written to disk. Drop the dependency as it is no longer 8050 * necessary since the inode is being deallocated. We set the 8051 * ALLCOMPLETE flags since the bitmap now properly shows that the 8052 * inode is not allocated. Even if the inode is actively being 8053 * written, it has been rolled back to its zero'ed state, so we 8054 * are ensured that a zero inode is what is on the disk. For short 8055 * lived files, this change will usually result in removing all the 8056 * dependencies from the inode so that it can be freed immediately. 8057 */ 8058 static int 8059 check_inode_unwritten(inodedep) 8060 struct inodedep *inodedep; 8061 { 8062 8063 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 8064 8065 if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 || 8066 !LIST_EMPTY(&inodedep->id_dirremhd) || 8067 !LIST_EMPTY(&inodedep->id_pendinghd) || 8068 !LIST_EMPTY(&inodedep->id_bufwait) || 8069 !LIST_EMPTY(&inodedep->id_inowait) || 8070 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 8071 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 8072 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 8073 !TAILQ_EMPTY(&inodedep->id_extupdt) || 8074 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 8075 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 8076 inodedep->id_mkdiradd != NULL || 8077 inodedep->id_nlinkdelta != 0) 8078 return (0); 8079 /* 8080 * Another process might be in initiate_write_inodeblock_ufs[12] 8081 * trying to allocate memory without holding "Softdep Lock". 8082 */ 8083 if ((inodedep->id_state & IOSTARTED) != 0 && 8084 inodedep->id_savedino1 == NULL) 8085 return (0); 8086 8087 if (inodedep->id_state & ONDEPLIST) 8088 LIST_REMOVE(inodedep, id_deps); 8089 inodedep->id_state &= ~ONDEPLIST; 8090 inodedep->id_state |= ALLCOMPLETE; 8091 inodedep->id_bmsafemap = NULL; 8092 if (inodedep->id_state & ONWORKLIST) 8093 WORKLIST_REMOVE(&inodedep->id_list); 8094 if (inodedep->id_savedino1 != NULL) { 8095 free(inodedep->id_savedino1, M_SAVEDINO); 8096 inodedep->id_savedino1 = NULL; 8097 } 8098 if (free_inodedep(inodedep) == 0) 8099 panic("check_inode_unwritten: busy inode"); 8100 return (1); 8101 } 8102 8103 static int 8104 check_inodedep_free(inodedep) 8105 struct inodedep *inodedep; 8106 { 8107 8108 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 8109 if ((inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE || 8110 !LIST_EMPTY(&inodedep->id_dirremhd) || 8111 !LIST_EMPTY(&inodedep->id_pendinghd) || 8112 !LIST_EMPTY(&inodedep->id_bufwait) || 8113 !LIST_EMPTY(&inodedep->id_inowait) || 8114 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 8115 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 8116 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 8117 !TAILQ_EMPTY(&inodedep->id_extupdt) || 8118 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 8119 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 8120 inodedep->id_mkdiradd != NULL || 8121 inodedep->id_nlinkdelta != 0 || 8122 inodedep->id_savedino1 != NULL) 8123 return (0); 8124 return (1); 8125 } 8126 8127 /* 8128 * Try to free an inodedep structure. Return 1 if it could be freed. 8129 */ 8130 static int 8131 free_inodedep(inodedep) 8132 struct inodedep *inodedep; 8133 { 8134 8135 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 8136 if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 || 8137 !check_inodedep_free(inodedep)) 8138 return (0); 8139 if (inodedep->id_state & ONDEPLIST) 8140 LIST_REMOVE(inodedep, id_deps); 8141 LIST_REMOVE(inodedep, id_hash); 8142 WORKITEM_FREE(inodedep, D_INODEDEP); 8143 return (1); 8144 } 8145 8146 /* 8147 * Free the block referenced by a freework structure. The parent freeblks 8148 * structure is released and completed when the final cg bitmap reaches 8149 * the disk. This routine may be freeing a jnewblk which never made it to 8150 * disk in which case we do not have to wait as the operation is undone 8151 * in memory immediately. 8152 */ 8153 static void 8154 freework_freeblock(freework, key) 8155 struct freework *freework; 8156 u_long key; 8157 { 8158 struct freeblks *freeblks; 8159 struct jnewblk *jnewblk; 8160 struct ufsmount *ump; 8161 struct workhead wkhd; 8162 struct fs *fs; 8163 int bsize; 8164 int needj; 8165 8166 ump = VFSTOUFS(freework->fw_list.wk_mp); 8167 LOCK_OWNED(ump); 8168 /* 8169 * Handle partial truncate separately. 8170 */ 8171 if (freework->fw_indir) { 8172 complete_trunc_indir(freework); 8173 return; 8174 } 8175 freeblks = freework->fw_freeblks; 8176 fs = ump->um_fs; 8177 needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0; 8178 bsize = lfragtosize(fs, freework->fw_frags); 8179 LIST_INIT(&wkhd); 8180 /* 8181 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives 8182 * on the indirblk hashtable and prevents premature freeing. 8183 */ 8184 freework->fw_state |= DEPCOMPLETE; 8185 /* 8186 * SUJ needs to wait for the segment referencing freed indirect 8187 * blocks to expire so that we know the checker will not confuse 8188 * a re-allocated indirect block with its old contents. 8189 */ 8190 if (needj && freework->fw_lbn <= -UFS_NDADDR) 8191 indirblk_insert(freework); 8192 /* 8193 * If we are canceling an existing jnewblk pass it to the free 8194 * routine, otherwise pass the freeblk which will ultimately 8195 * release the freeblks. If we're not journaling, we can just 8196 * free the freeblks immediately. 8197 */ 8198 jnewblk = freework->fw_jnewblk; 8199 if (jnewblk != NULL) { 8200 cancel_jnewblk(jnewblk, &wkhd); 8201 needj = 0; 8202 } else if (needj) { 8203 freework->fw_state |= DELAYEDFREE; 8204 freeblks->fb_cgwait++; 8205 WORKLIST_INSERT(&wkhd, &freework->fw_list); 8206 } 8207 FREE_LOCK(ump); 8208 freeblks_free(ump, freeblks, btodb(bsize)); 8209 CTR4(KTR_SUJ, 8210 "freework_freeblock: ino %jd blkno %jd lbn %jd size %d", 8211 freeblks->fb_inum, freework->fw_blkno, freework->fw_lbn, bsize); 8212 ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, bsize, 8213 freeblks->fb_inum, freeblks->fb_vtype, &wkhd, key); 8214 ACQUIRE_LOCK(ump); 8215 /* 8216 * The jnewblk will be discarded and the bits in the map never 8217 * made it to disk. We can immediately free the freeblk. 8218 */ 8219 if (needj == 0) 8220 handle_written_freework(freework); 8221 } 8222 8223 /* 8224 * We enqueue freework items that need processing back on the freeblks and 8225 * add the freeblks to the worklist. This makes it easier to find all work 8226 * required to flush a truncation in process_truncates(). 8227 */ 8228 static void 8229 freework_enqueue(freework) 8230 struct freework *freework; 8231 { 8232 struct freeblks *freeblks; 8233 8234 freeblks = freework->fw_freeblks; 8235 if ((freework->fw_state & INPROGRESS) == 0) 8236 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 8237 if ((freeblks->fb_state & 8238 (ONWORKLIST | INPROGRESS | ALLCOMPLETE)) == ALLCOMPLETE && 8239 LIST_EMPTY(&freeblks->fb_jblkdephd)) 8240 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 8241 } 8242 8243 /* 8244 * Start, continue, or finish the process of freeing an indirect block tree. 8245 * The free operation may be paused at any point with fw_off containing the 8246 * offset to restart from. This enables us to implement some flow control 8247 * for large truncates which may fan out and generate a huge number of 8248 * dependencies. 8249 */ 8250 static void 8251 handle_workitem_indirblk(freework) 8252 struct freework *freework; 8253 { 8254 struct freeblks *freeblks; 8255 struct ufsmount *ump; 8256 struct fs *fs; 8257 8258 freeblks = freework->fw_freeblks; 8259 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8260 fs = ump->um_fs; 8261 if (freework->fw_state & DEPCOMPLETE) { 8262 handle_written_freework(freework); 8263 return; 8264 } 8265 if (freework->fw_off == NINDIR(fs)) { 8266 freework_freeblock(freework, SINGLETON_KEY); 8267 return; 8268 } 8269 freework->fw_state |= INPROGRESS; 8270 FREE_LOCK(ump); 8271 indir_trunc(freework, fsbtodb(fs, freework->fw_blkno), 8272 freework->fw_lbn); 8273 ACQUIRE_LOCK(ump); 8274 } 8275 8276 /* 8277 * Called when a freework structure attached to a cg buf is written. The 8278 * ref on either the parent or the freeblks structure is released and 8279 * the freeblks is added back to the worklist if there is more work to do. 8280 */ 8281 static void 8282 handle_written_freework(freework) 8283 struct freework *freework; 8284 { 8285 struct freeblks *freeblks; 8286 struct freework *parent; 8287 8288 freeblks = freework->fw_freeblks; 8289 parent = freework->fw_parent; 8290 if (freework->fw_state & DELAYEDFREE) 8291 freeblks->fb_cgwait--; 8292 freework->fw_state |= COMPLETE; 8293 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 8294 WORKITEM_FREE(freework, D_FREEWORK); 8295 if (parent) { 8296 if (--parent->fw_ref == 0) 8297 freework_enqueue(parent); 8298 return; 8299 } 8300 if (--freeblks->fb_ref != 0) 8301 return; 8302 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) == 8303 ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd)) 8304 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 8305 } 8306 8307 /* 8308 * This workitem routine performs the block de-allocation. 8309 * The workitem is added to the pending list after the updated 8310 * inode block has been written to disk. As mentioned above, 8311 * checks regarding the number of blocks de-allocated (compared 8312 * to the number of blocks allocated for the file) are also 8313 * performed in this function. 8314 */ 8315 static int 8316 handle_workitem_freeblocks(freeblks, flags) 8317 struct freeblks *freeblks; 8318 int flags; 8319 { 8320 struct freework *freework; 8321 struct newblk *newblk; 8322 struct allocindir *aip; 8323 struct ufsmount *ump; 8324 struct worklist *wk; 8325 u_long key; 8326 8327 KASSERT(LIST_EMPTY(&freeblks->fb_jblkdephd), 8328 ("handle_workitem_freeblocks: Journal entries not written.")); 8329 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8330 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum); 8331 ACQUIRE_LOCK(ump); 8332 while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) { 8333 WORKLIST_REMOVE(wk); 8334 switch (wk->wk_type) { 8335 case D_DIRREM: 8336 wk->wk_state |= COMPLETE; 8337 add_to_worklist(wk, 0); 8338 continue; 8339 8340 case D_ALLOCDIRECT: 8341 free_newblk(WK_NEWBLK(wk)); 8342 continue; 8343 8344 case D_ALLOCINDIR: 8345 aip = WK_ALLOCINDIR(wk); 8346 freework = NULL; 8347 if (aip->ai_state & DELAYEDFREE) { 8348 FREE_LOCK(ump); 8349 freework = newfreework(ump, freeblks, NULL, 8350 aip->ai_lbn, aip->ai_newblkno, 8351 ump->um_fs->fs_frag, 0, 0); 8352 ACQUIRE_LOCK(ump); 8353 } 8354 newblk = WK_NEWBLK(wk); 8355 if (newblk->nb_jnewblk) { 8356 freework->fw_jnewblk = newblk->nb_jnewblk; 8357 newblk->nb_jnewblk->jn_dep = &freework->fw_list; 8358 newblk->nb_jnewblk = NULL; 8359 } 8360 free_newblk(newblk); 8361 continue; 8362 8363 case D_FREEWORK: 8364 freework = WK_FREEWORK(wk); 8365 if (freework->fw_lbn <= -UFS_NDADDR) 8366 handle_workitem_indirblk(freework); 8367 else 8368 freework_freeblock(freework, key); 8369 continue; 8370 default: 8371 panic("handle_workitem_freeblocks: Unknown type %s", 8372 TYPENAME(wk->wk_type)); 8373 } 8374 } 8375 if (freeblks->fb_ref != 0) { 8376 freeblks->fb_state &= ~INPROGRESS; 8377 wake_worklist(&freeblks->fb_list); 8378 freeblks = NULL; 8379 } 8380 FREE_LOCK(ump); 8381 ffs_blkrelease_finish(ump, key); 8382 if (freeblks) 8383 return handle_complete_freeblocks(freeblks, flags); 8384 return (0); 8385 } 8386 8387 /* 8388 * Handle completion of block free via truncate. This allows fs_pending 8389 * to track the actual free block count more closely than if we only updated 8390 * it at the end. We must be careful to handle cases where the block count 8391 * on free was incorrect. 8392 */ 8393 static void 8394 freeblks_free(ump, freeblks, blocks) 8395 struct ufsmount *ump; 8396 struct freeblks *freeblks; 8397 int blocks; 8398 { 8399 struct fs *fs; 8400 ufs2_daddr_t remain; 8401 8402 UFS_LOCK(ump); 8403 remain = -freeblks->fb_chkcnt; 8404 freeblks->fb_chkcnt += blocks; 8405 if (remain > 0) { 8406 if (remain < blocks) 8407 blocks = remain; 8408 fs = ump->um_fs; 8409 fs->fs_pendingblocks -= blocks; 8410 } 8411 UFS_UNLOCK(ump); 8412 } 8413 8414 /* 8415 * Once all of the freework workitems are complete we can retire the 8416 * freeblocks dependency and any journal work awaiting completion. This 8417 * can not be called until all other dependencies are stable on disk. 8418 */ 8419 static int 8420 handle_complete_freeblocks(freeblks, flags) 8421 struct freeblks *freeblks; 8422 int flags; 8423 { 8424 struct inodedep *inodedep; 8425 struct inode *ip; 8426 struct vnode *vp; 8427 struct fs *fs; 8428 struct ufsmount *ump; 8429 ufs2_daddr_t spare; 8430 8431 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8432 fs = ump->um_fs; 8433 flags = LK_EXCLUSIVE | flags; 8434 spare = freeblks->fb_chkcnt; 8435 8436 /* 8437 * If we did not release the expected number of blocks we may have 8438 * to adjust the inode block count here. Only do so if it wasn't 8439 * a truncation to zero and the modrev still matches. 8440 */ 8441 if (spare && freeblks->fb_len != 0) { 8442 if (ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_inum, 8443 flags, &vp, FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP) != 0) 8444 return (EBUSY); 8445 ip = VTOI(vp); 8446 if (ip->i_mode == 0) { 8447 vgone(vp); 8448 } else if (DIP(ip, i_modrev) == freeblks->fb_modrev) { 8449 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare); 8450 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 8451 /* 8452 * We must wait so this happens before the 8453 * journal is reclaimed. 8454 */ 8455 ffs_update(vp, 1); 8456 } 8457 vput(vp); 8458 } 8459 if (spare < 0) { 8460 UFS_LOCK(ump); 8461 fs->fs_pendingblocks += spare; 8462 UFS_UNLOCK(ump); 8463 } 8464 #ifdef QUOTA 8465 /* Handle spare. */ 8466 if (spare) 8467 quotaadj(freeblks->fb_quota, ump, -spare); 8468 quotarele(freeblks->fb_quota); 8469 #endif 8470 ACQUIRE_LOCK(ump); 8471 if (freeblks->fb_state & ONDEPLIST) { 8472 inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum, 8473 0, &inodedep); 8474 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next); 8475 freeblks->fb_state &= ~ONDEPLIST; 8476 if (TAILQ_EMPTY(&inodedep->id_freeblklst)) 8477 free_inodedep(inodedep); 8478 } 8479 /* 8480 * All of the freeblock deps must be complete prior to this call 8481 * so it's now safe to complete earlier outstanding journal entries. 8482 */ 8483 handle_jwork(&freeblks->fb_jwork); 8484 WORKITEM_FREE(freeblks, D_FREEBLKS); 8485 FREE_LOCK(ump); 8486 return (0); 8487 } 8488 8489 /* 8490 * Release blocks associated with the freeblks and stored in the indirect 8491 * block dbn. If level is greater than SINGLE, the block is an indirect block 8492 * and recursive calls to indirtrunc must be used to cleanse other indirect 8493 * blocks. 8494 * 8495 * This handles partial and complete truncation of blocks. Partial is noted 8496 * with goingaway == 0. In this case the freework is completed after the 8497 * zero'd indirects are written to disk. For full truncation the freework 8498 * is completed after the block is freed. 8499 */ 8500 static void 8501 indir_trunc(freework, dbn, lbn) 8502 struct freework *freework; 8503 ufs2_daddr_t dbn; 8504 ufs_lbn_t lbn; 8505 { 8506 struct freework *nfreework; 8507 struct workhead wkhd; 8508 struct freeblks *freeblks; 8509 struct buf *bp; 8510 struct fs *fs; 8511 struct indirdep *indirdep; 8512 struct mount *mp; 8513 struct ufsmount *ump; 8514 ufs1_daddr_t *bap1; 8515 ufs2_daddr_t nb, nnb, *bap2; 8516 ufs_lbn_t lbnadd, nlbn; 8517 u_long key; 8518 int nblocks, ufs1fmt, freedblocks; 8519 int goingaway, freedeps, needj, level, cnt, i, error; 8520 8521 freeblks = freework->fw_freeblks; 8522 mp = freeblks->fb_list.wk_mp; 8523 ump = VFSTOUFS(mp); 8524 fs = ump->um_fs; 8525 /* 8526 * Get buffer of block pointers to be freed. There are three cases: 8527 * 8528 * 1) Partial truncate caches the indirdep pointer in the freework 8529 * which provides us a back copy to the save bp which holds the 8530 * pointers we want to clear. When this completes the zero 8531 * pointers are written to the real copy. 8532 * 2) The indirect is being completely truncated, cancel_indirdep() 8533 * eliminated the real copy and placed the indirdep on the saved 8534 * copy. The indirdep and buf are discarded when this completes. 8535 * 3) The indirect was not in memory, we read a copy off of the disk 8536 * using the devvp and drop and invalidate the buffer when we're 8537 * done. 8538 */ 8539 goingaway = 1; 8540 indirdep = NULL; 8541 if (freework->fw_indir != NULL) { 8542 goingaway = 0; 8543 indirdep = freework->fw_indir; 8544 bp = indirdep->ir_savebp; 8545 if (bp == NULL || bp->b_blkno != dbn) 8546 panic("indir_trunc: Bad saved buf %p blkno %jd", 8547 bp, (intmax_t)dbn); 8548 } else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) { 8549 /* 8550 * The lock prevents the buf dep list from changing and 8551 * indirects on devvp should only ever have one dependency. 8552 */ 8553 indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep)); 8554 if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0) 8555 panic("indir_trunc: Bad indirdep %p from buf %p", 8556 indirdep, bp); 8557 } else { 8558 error = ffs_breadz(ump, freeblks->fb_devvp, dbn, dbn, 8559 (int)fs->fs_bsize, NULL, NULL, 0, NOCRED, 0, NULL, &bp); 8560 if (error) 8561 return; 8562 } 8563 ACQUIRE_LOCK(ump); 8564 /* Protects against a race with complete_trunc_indir(). */ 8565 freework->fw_state &= ~INPROGRESS; 8566 /* 8567 * If we have an indirdep we need to enforce the truncation order 8568 * and discard it when it is complete. 8569 */ 8570 if (indirdep) { 8571 if (freework != TAILQ_FIRST(&indirdep->ir_trunc) && 8572 !TAILQ_EMPTY(&indirdep->ir_trunc)) { 8573 /* 8574 * Add the complete truncate to the list on the 8575 * indirdep to enforce in-order processing. 8576 */ 8577 if (freework->fw_indir == NULL) 8578 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, 8579 freework, fw_next); 8580 FREE_LOCK(ump); 8581 return; 8582 } 8583 /* 8584 * If we're goingaway, free the indirdep. Otherwise it will 8585 * linger until the write completes. 8586 */ 8587 if (goingaway) { 8588 KASSERT(indirdep->ir_savebp == bp, 8589 ("indir_trunc: losing ir_savebp %p", 8590 indirdep->ir_savebp)); 8591 indirdep->ir_savebp = NULL; 8592 free_indirdep(indirdep); 8593 } 8594 } 8595 FREE_LOCK(ump); 8596 /* Initialize pointers depending on block size. */ 8597 if (ump->um_fstype == UFS1) { 8598 bap1 = (ufs1_daddr_t *)bp->b_data; 8599 nb = bap1[freework->fw_off]; 8600 ufs1fmt = 1; 8601 bap2 = NULL; 8602 } else { 8603 bap2 = (ufs2_daddr_t *)bp->b_data; 8604 nb = bap2[freework->fw_off]; 8605 ufs1fmt = 0; 8606 bap1 = NULL; 8607 } 8608 level = lbn_level(lbn); 8609 needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0; 8610 lbnadd = lbn_offset(fs, level); 8611 nblocks = btodb(fs->fs_bsize); 8612 nfreework = freework; 8613 freedeps = 0; 8614 cnt = 0; 8615 /* 8616 * Reclaim blocks. Traverses into nested indirect levels and 8617 * arranges for the current level to be freed when subordinates 8618 * are free when journaling. 8619 */ 8620 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum); 8621 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) { 8622 if (UFS_CHECK_BLKNO(mp, freeblks->fb_inum, nb, 8623 fs->fs_bsize) != 0) 8624 nb = 0; 8625 if (i != NINDIR(fs) - 1) { 8626 if (ufs1fmt) 8627 nnb = bap1[i+1]; 8628 else 8629 nnb = bap2[i+1]; 8630 } else 8631 nnb = 0; 8632 if (nb == 0) 8633 continue; 8634 cnt++; 8635 if (level != 0) { 8636 nlbn = (lbn + 1) - (i * lbnadd); 8637 if (needj != 0) { 8638 nfreework = newfreework(ump, freeblks, freework, 8639 nlbn, nb, fs->fs_frag, 0, 0); 8640 freedeps++; 8641 } 8642 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn); 8643 } else { 8644 struct freedep *freedep; 8645 8646 /* 8647 * Attempt to aggregate freedep dependencies for 8648 * all blocks being released to the same CG. 8649 */ 8650 LIST_INIT(&wkhd); 8651 if (needj != 0 && 8652 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) { 8653 freedep = newfreedep(freework); 8654 WORKLIST_INSERT_UNLOCKED(&wkhd, 8655 &freedep->fd_list); 8656 freedeps++; 8657 } 8658 CTR3(KTR_SUJ, 8659 "indir_trunc: ino %jd blkno %jd size %d", 8660 freeblks->fb_inum, nb, fs->fs_bsize); 8661 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb, 8662 fs->fs_bsize, freeblks->fb_inum, 8663 freeblks->fb_vtype, &wkhd, key); 8664 } 8665 } 8666 ffs_blkrelease_finish(ump, key); 8667 if (goingaway) { 8668 bp->b_flags |= B_INVAL | B_NOCACHE; 8669 brelse(bp); 8670 } 8671 freedblocks = 0; 8672 if (level == 0) 8673 freedblocks = (nblocks * cnt); 8674 if (needj == 0) 8675 freedblocks += nblocks; 8676 freeblks_free(ump, freeblks, freedblocks); 8677 /* 8678 * If we are journaling set up the ref counts and offset so this 8679 * indirect can be completed when its children are free. 8680 */ 8681 if (needj) { 8682 ACQUIRE_LOCK(ump); 8683 freework->fw_off = i; 8684 freework->fw_ref += freedeps; 8685 freework->fw_ref -= NINDIR(fs) + 1; 8686 if (level == 0) 8687 freeblks->fb_cgwait += freedeps; 8688 if (freework->fw_ref == 0) 8689 freework_freeblock(freework, SINGLETON_KEY); 8690 FREE_LOCK(ump); 8691 return; 8692 } 8693 /* 8694 * If we're not journaling we can free the indirect now. 8695 */ 8696 dbn = dbtofsb(fs, dbn); 8697 CTR3(KTR_SUJ, 8698 "indir_trunc 2: ino %jd blkno %jd size %d", 8699 freeblks->fb_inum, dbn, fs->fs_bsize); 8700 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize, 8701 freeblks->fb_inum, freeblks->fb_vtype, NULL, SINGLETON_KEY); 8702 /* Non SUJ softdep does single-threaded truncations. */ 8703 if (freework->fw_blkno == dbn) { 8704 freework->fw_state |= ALLCOMPLETE; 8705 ACQUIRE_LOCK(ump); 8706 handle_written_freework(freework); 8707 FREE_LOCK(ump); 8708 } 8709 return; 8710 } 8711 8712 /* 8713 * Cancel an allocindir when it is removed via truncation. When bp is not 8714 * NULL the indirect never appeared on disk and is scheduled to be freed 8715 * independently of the indir so we can more easily track journal work. 8716 */ 8717 static void 8718 cancel_allocindir(aip, bp, freeblks, trunc) 8719 struct allocindir *aip; 8720 struct buf *bp; 8721 struct freeblks *freeblks; 8722 int trunc; 8723 { 8724 struct indirdep *indirdep; 8725 struct freefrag *freefrag; 8726 struct newblk *newblk; 8727 8728 newblk = (struct newblk *)aip; 8729 LIST_REMOVE(aip, ai_next); 8730 /* 8731 * We must eliminate the pointer in bp if it must be freed on its 8732 * own due to partial truncate or pending journal work. 8733 */ 8734 if (bp && (trunc || newblk->nb_jnewblk)) { 8735 /* 8736 * Clear the pointer and mark the aip to be freed 8737 * directly if it never existed on disk. 8738 */ 8739 aip->ai_state |= DELAYEDFREE; 8740 indirdep = aip->ai_indirdep; 8741 if (indirdep->ir_state & UFS1FMT) 8742 ((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8743 else 8744 ((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8745 } 8746 /* 8747 * When truncating the previous pointer will be freed via 8748 * savedbp. Eliminate the freefrag which would dup free. 8749 */ 8750 if (trunc && (freefrag = newblk->nb_freefrag) != NULL) { 8751 newblk->nb_freefrag = NULL; 8752 if (freefrag->ff_jdep) 8753 cancel_jfreefrag( 8754 WK_JFREEFRAG(freefrag->ff_jdep)); 8755 jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork); 8756 WORKITEM_FREE(freefrag, D_FREEFRAG); 8757 } 8758 /* 8759 * If the journal hasn't been written the jnewblk must be passed 8760 * to the call to ffs_blkfree that reclaims the space. We accomplish 8761 * this by leaving the journal dependency on the newblk to be freed 8762 * when a freework is created in handle_workitem_freeblocks(). 8763 */ 8764 cancel_newblk(newblk, NULL, &freeblks->fb_jwork); 8765 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 8766 } 8767 8768 /* 8769 * Create the mkdir dependencies for . and .. in a new directory. Link them 8770 * in to a newdirblk so any subsequent additions are tracked properly. The 8771 * caller is responsible for adding the mkdir1 dependency to the journal 8772 * and updating id_mkdiradd. This function returns with the per-filesystem 8773 * lock held. 8774 */ 8775 static struct mkdir * 8776 setup_newdir(dap, newinum, dinum, newdirbp, mkdirp) 8777 struct diradd *dap; 8778 ino_t newinum; 8779 ino_t dinum; 8780 struct buf *newdirbp; 8781 struct mkdir **mkdirp; 8782 { 8783 struct newblk *newblk; 8784 struct pagedep *pagedep; 8785 struct inodedep *inodedep; 8786 struct newdirblk *newdirblk; 8787 struct mkdir *mkdir1, *mkdir2; 8788 struct worklist *wk; 8789 struct jaddref *jaddref; 8790 struct ufsmount *ump; 8791 struct mount *mp; 8792 8793 mp = dap->da_list.wk_mp; 8794 ump = VFSTOUFS(mp); 8795 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK, 8796 M_SOFTDEP_FLAGS); 8797 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8798 LIST_INIT(&newdirblk->db_mkdir); 8799 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8800 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp); 8801 mkdir1->md_state = ATTACHED | MKDIR_BODY; 8802 mkdir1->md_diradd = dap; 8803 mkdir1->md_jaddref = NULL; 8804 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8805 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp); 8806 mkdir2->md_state = ATTACHED | MKDIR_PARENT; 8807 mkdir2->md_diradd = dap; 8808 mkdir2->md_jaddref = NULL; 8809 if (MOUNTEDSUJ(mp) == 0) { 8810 mkdir1->md_state |= DEPCOMPLETE; 8811 mkdir2->md_state |= DEPCOMPLETE; 8812 } 8813 /* 8814 * Dependency on "." and ".." being written to disk. 8815 */ 8816 mkdir1->md_buf = newdirbp; 8817 ACQUIRE_LOCK(VFSTOUFS(mp)); 8818 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir1, md_mkdirs); 8819 /* 8820 * We must link the pagedep, allocdirect, and newdirblk for 8821 * the initial file page so the pointer to the new directory 8822 * is not written until the directory contents are live and 8823 * any subsequent additions are not marked live until the 8824 * block is reachable via the inode. 8825 */ 8826 if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0) 8827 panic("setup_newdir: lost pagedep"); 8828 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list) 8829 if (wk->wk_type == D_ALLOCDIRECT) 8830 break; 8831 if (wk == NULL) 8832 panic("setup_newdir: lost allocdirect"); 8833 if (pagedep->pd_state & NEWBLOCK) 8834 panic("setup_newdir: NEWBLOCK already set"); 8835 newblk = WK_NEWBLK(wk); 8836 pagedep->pd_state |= NEWBLOCK; 8837 pagedep->pd_newdirblk = newdirblk; 8838 newdirblk->db_pagedep = pagedep; 8839 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8840 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list); 8841 /* 8842 * Look up the inodedep for the parent directory so that we 8843 * can link mkdir2 into the pending dotdot jaddref or 8844 * the inode write if there is none. If the inode is 8845 * ALLCOMPLETE and no jaddref is present all dependencies have 8846 * been satisfied and mkdir2 can be freed. 8847 */ 8848 inodedep_lookup(mp, dinum, 0, &inodedep); 8849 if (MOUNTEDSUJ(mp)) { 8850 if (inodedep == NULL) 8851 panic("setup_newdir: Lost parent."); 8852 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8853 inoreflst); 8854 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum && 8855 (jaddref->ja_state & MKDIR_PARENT), 8856 ("setup_newdir: bad dotdot jaddref %p", jaddref)); 8857 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8858 mkdir2->md_jaddref = jaddref; 8859 jaddref->ja_mkdir = mkdir2; 8860 } else if (inodedep == NULL || 8861 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 8862 dap->da_state &= ~MKDIR_PARENT; 8863 WORKITEM_FREE(mkdir2, D_MKDIR); 8864 mkdir2 = NULL; 8865 } else { 8866 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8867 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list); 8868 } 8869 *mkdirp = mkdir2; 8870 8871 return (mkdir1); 8872 } 8873 8874 /* 8875 * Directory entry addition dependencies. 8876 * 8877 * When adding a new directory entry, the inode (with its incremented link 8878 * count) must be written to disk before the directory entry's pointer to it. 8879 * Also, if the inode is newly allocated, the corresponding freemap must be 8880 * updated (on disk) before the directory entry's pointer. These requirements 8881 * are met via undo/redo on the directory entry's pointer, which consists 8882 * simply of the inode number. 8883 * 8884 * As directory entries are added and deleted, the free space within a 8885 * directory block can become fragmented. The ufs filesystem will compact 8886 * a fragmented directory block to make space for a new entry. When this 8887 * occurs, the offsets of previously added entries change. Any "diradd" 8888 * dependency structures corresponding to these entries must be updated with 8889 * the new offsets. 8890 */ 8891 8892 /* 8893 * This routine is called after the in-memory inode's link 8894 * count has been incremented, but before the directory entry's 8895 * pointer to the inode has been set. 8896 */ 8897 int 8898 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 8899 struct buf *bp; /* buffer containing directory block */ 8900 struct inode *dp; /* inode for directory */ 8901 off_t diroffset; /* offset of new entry in directory */ 8902 ino_t newinum; /* inode referenced by new directory entry */ 8903 struct buf *newdirbp; /* non-NULL => contents of new mkdir */ 8904 int isnewblk; /* entry is in a newly allocated block */ 8905 { 8906 int offset; /* offset of new entry within directory block */ 8907 ufs_lbn_t lbn; /* block in directory containing new entry */ 8908 struct fs *fs; 8909 struct diradd *dap; 8910 struct newblk *newblk; 8911 struct pagedep *pagedep; 8912 struct inodedep *inodedep; 8913 struct newdirblk *newdirblk; 8914 struct mkdir *mkdir1, *mkdir2; 8915 struct jaddref *jaddref; 8916 struct ufsmount *ump; 8917 struct mount *mp; 8918 int isindir; 8919 8920 mp = ITOVFS(dp); 8921 ump = VFSTOUFS(mp); 8922 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 8923 ("softdep_setup_directory_add called on non-softdep filesystem")); 8924 /* 8925 * Whiteouts have no dependencies. 8926 */ 8927 if (newinum == UFS_WINO) { 8928 if (newdirbp != NULL) 8929 bdwrite(newdirbp); 8930 return (0); 8931 } 8932 jaddref = NULL; 8933 mkdir1 = mkdir2 = NULL; 8934 fs = ump->um_fs; 8935 lbn = lblkno(fs, diroffset); 8936 offset = blkoff(fs, diroffset); 8937 dap = malloc(sizeof(struct diradd), M_DIRADD, 8938 M_SOFTDEP_FLAGS|M_ZERO); 8939 workitem_alloc(&dap->da_list, D_DIRADD, mp); 8940 dap->da_offset = offset; 8941 dap->da_newinum = newinum; 8942 dap->da_state = ATTACHED; 8943 LIST_INIT(&dap->da_jwork); 8944 isindir = bp->b_lblkno >= UFS_NDADDR; 8945 newdirblk = NULL; 8946 if (isnewblk && 8947 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) { 8948 newdirblk = malloc(sizeof(struct newdirblk), 8949 M_NEWDIRBLK, M_SOFTDEP_FLAGS); 8950 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8951 LIST_INIT(&newdirblk->db_mkdir); 8952 } 8953 /* 8954 * If we're creating a new directory setup the dependencies and set 8955 * the dap state to wait for them. Otherwise it's COMPLETE and 8956 * we can move on. 8957 */ 8958 if (newdirbp == NULL) { 8959 dap->da_state |= DEPCOMPLETE; 8960 ACQUIRE_LOCK(ump); 8961 } else { 8962 dap->da_state |= MKDIR_BODY | MKDIR_PARENT; 8963 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp, 8964 &mkdir2); 8965 } 8966 /* 8967 * Link into parent directory pagedep to await its being written. 8968 */ 8969 pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep); 8970 #ifdef INVARIANTS 8971 if (diradd_lookup(pagedep, offset) != NULL) 8972 panic("softdep_setup_directory_add: %p already at off %d\n", 8973 diradd_lookup(pagedep, offset), offset); 8974 #endif 8975 dap->da_pagedep = pagedep; 8976 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap, 8977 da_pdlist); 8978 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 8979 /* 8980 * If we're journaling, link the diradd into the jaddref so it 8981 * may be completed after the journal entry is written. Otherwise, 8982 * link the diradd into its inodedep. If the inode is not yet 8983 * written place it on the bufwait list, otherwise do the post-inode 8984 * write processing to put it on the id_pendinghd list. 8985 */ 8986 if (MOUNTEDSUJ(mp)) { 8987 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8988 inoreflst); 8989 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 8990 ("softdep_setup_directory_add: bad jaddref %p", jaddref)); 8991 jaddref->ja_diroff = diroffset; 8992 jaddref->ja_diradd = dap; 8993 add_to_journal(&jaddref->ja_list); 8994 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) 8995 diradd_inode_written(dap, inodedep); 8996 else 8997 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 8998 /* 8999 * Add the journal entries for . and .. links now that the primary 9000 * link is written. 9001 */ 9002 if (mkdir1 != NULL && MOUNTEDSUJ(mp)) { 9003 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 9004 inoreflst, if_deps); 9005 KASSERT(jaddref != NULL && 9006 jaddref->ja_ino == jaddref->ja_parent && 9007 (jaddref->ja_state & MKDIR_BODY), 9008 ("softdep_setup_directory_add: bad dot jaddref %p", 9009 jaddref)); 9010 mkdir1->md_jaddref = jaddref; 9011 jaddref->ja_mkdir = mkdir1; 9012 /* 9013 * It is important that the dotdot journal entry 9014 * is added prior to the dot entry since dot writes 9015 * both the dot and dotdot links. These both must 9016 * be added after the primary link for the journal 9017 * to remain consistent. 9018 */ 9019 add_to_journal(&mkdir2->md_jaddref->ja_list); 9020 add_to_journal(&jaddref->ja_list); 9021 } 9022 /* 9023 * If we are adding a new directory remember this diradd so that if 9024 * we rename it we can keep the dot and dotdot dependencies. If 9025 * we are adding a new name for an inode that has a mkdiradd we 9026 * must be in rename and we have to move the dot and dotdot 9027 * dependencies to this new name. The old name is being orphaned 9028 * soon. 9029 */ 9030 if (mkdir1 != NULL) { 9031 if (inodedep->id_mkdiradd != NULL) 9032 panic("softdep_setup_directory_add: Existing mkdir"); 9033 inodedep->id_mkdiradd = dap; 9034 } else if (inodedep->id_mkdiradd) 9035 merge_diradd(inodedep, dap); 9036 if (newdirblk != NULL) { 9037 /* 9038 * There is nothing to do if we are already tracking 9039 * this block. 9040 */ 9041 if ((pagedep->pd_state & NEWBLOCK) != 0) { 9042 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 9043 FREE_LOCK(ump); 9044 return (0); 9045 } 9046 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk) 9047 == 0) 9048 panic("softdep_setup_directory_add: lost entry"); 9049 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 9050 pagedep->pd_state |= NEWBLOCK; 9051 pagedep->pd_newdirblk = newdirblk; 9052 newdirblk->db_pagedep = pagedep; 9053 FREE_LOCK(ump); 9054 /* 9055 * If we extended into an indirect signal direnter to sync. 9056 */ 9057 if (isindir) 9058 return (1); 9059 return (0); 9060 } 9061 FREE_LOCK(ump); 9062 return (0); 9063 } 9064 9065 /* 9066 * This procedure is called to change the offset of a directory 9067 * entry when compacting a directory block which must be owned 9068 * exclusively by the caller. Note that the actual entry movement 9069 * must be done in this procedure to ensure that no I/O completions 9070 * occur while the move is in progress. 9071 */ 9072 void 9073 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 9074 struct buf *bp; /* Buffer holding directory block. */ 9075 struct inode *dp; /* inode for directory */ 9076 caddr_t base; /* address of dp->i_offset */ 9077 caddr_t oldloc; /* address of old directory location */ 9078 caddr_t newloc; /* address of new directory location */ 9079 int entrysize; /* size of directory entry */ 9080 { 9081 int offset, oldoffset, newoffset; 9082 struct pagedep *pagedep; 9083 struct jmvref *jmvref; 9084 struct diradd *dap; 9085 struct direct *de; 9086 struct mount *mp; 9087 struct ufsmount *ump; 9088 ufs_lbn_t lbn; 9089 int flags; 9090 9091 mp = ITOVFS(dp); 9092 ump = VFSTOUFS(mp); 9093 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 9094 ("softdep_change_directoryentry_offset called on " 9095 "non-softdep filesystem")); 9096 de = (struct direct *)oldloc; 9097 jmvref = NULL; 9098 flags = 0; 9099 /* 9100 * Moves are always journaled as it would be too complex to 9101 * determine if any affected adds or removes are present in the 9102 * journal. 9103 */ 9104 if (MOUNTEDSUJ(mp)) { 9105 flags = DEPALLOC; 9106 jmvref = newjmvref(dp, de->d_ino, 9107 I_OFFSET(dp) + (oldloc - base), 9108 I_OFFSET(dp) + (newloc - base)); 9109 } 9110 lbn = lblkno(ump->um_fs, I_OFFSET(dp)); 9111 offset = blkoff(ump->um_fs, I_OFFSET(dp)); 9112 oldoffset = offset + (oldloc - base); 9113 newoffset = offset + (newloc - base); 9114 ACQUIRE_LOCK(ump); 9115 if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0) 9116 goto done; 9117 dap = diradd_lookup(pagedep, oldoffset); 9118 if (dap) { 9119 dap->da_offset = newoffset; 9120 newoffset = DIRADDHASH(newoffset); 9121 oldoffset = DIRADDHASH(oldoffset); 9122 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE && 9123 newoffset != oldoffset) { 9124 LIST_REMOVE(dap, da_pdlist); 9125 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset], 9126 dap, da_pdlist); 9127 } 9128 } 9129 done: 9130 if (jmvref) { 9131 jmvref->jm_pagedep = pagedep; 9132 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps); 9133 add_to_journal(&jmvref->jm_list); 9134 } 9135 bcopy(oldloc, newloc, entrysize); 9136 FREE_LOCK(ump); 9137 } 9138 9139 /* 9140 * Move the mkdir dependencies and journal work from one diradd to another 9141 * when renaming a directory. The new name must depend on the mkdir deps 9142 * completing as the old name did. Directories can only have one valid link 9143 * at a time so one must be canonical. 9144 */ 9145 static void 9146 merge_diradd(inodedep, newdap) 9147 struct inodedep *inodedep; 9148 struct diradd *newdap; 9149 { 9150 struct diradd *olddap; 9151 struct mkdir *mkdir, *nextmd; 9152 struct ufsmount *ump; 9153 short state; 9154 9155 olddap = inodedep->id_mkdiradd; 9156 inodedep->id_mkdiradd = newdap; 9157 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 9158 newdap->da_state &= ~DEPCOMPLETE; 9159 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9160 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 9161 mkdir = nextmd) { 9162 nextmd = LIST_NEXT(mkdir, md_mkdirs); 9163 if (mkdir->md_diradd != olddap) 9164 continue; 9165 mkdir->md_diradd = newdap; 9166 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY); 9167 newdap->da_state |= state; 9168 olddap->da_state &= ~state; 9169 if ((olddap->da_state & 9170 (MKDIR_PARENT | MKDIR_BODY)) == 0) 9171 break; 9172 } 9173 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 9174 panic("merge_diradd: unfound ref"); 9175 } 9176 /* 9177 * Any mkdir related journal items are not safe to be freed until 9178 * the new name is stable. 9179 */ 9180 jwork_move(&newdap->da_jwork, &olddap->da_jwork); 9181 olddap->da_state |= DEPCOMPLETE; 9182 complete_diradd(olddap); 9183 } 9184 9185 /* 9186 * Move the diradd to the pending list when all diradd dependencies are 9187 * complete. 9188 */ 9189 static void 9190 complete_diradd(dap) 9191 struct diradd *dap; 9192 { 9193 struct pagedep *pagedep; 9194 9195 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 9196 if (dap->da_state & DIRCHG) 9197 pagedep = dap->da_previous->dm_pagedep; 9198 else 9199 pagedep = dap->da_pagedep; 9200 LIST_REMOVE(dap, da_pdlist); 9201 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 9202 } 9203 } 9204 9205 /* 9206 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal 9207 * add entries and conditonally journal the remove. 9208 */ 9209 static void 9210 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref) 9211 struct diradd *dap; 9212 struct dirrem *dirrem; 9213 struct jremref *jremref; 9214 struct jremref *dotremref; 9215 struct jremref *dotdotremref; 9216 { 9217 struct inodedep *inodedep; 9218 struct jaddref *jaddref; 9219 struct inoref *inoref; 9220 struct ufsmount *ump; 9221 struct mkdir *mkdir; 9222 9223 /* 9224 * If no remove references were allocated we're on a non-journaled 9225 * filesystem and can skip the cancel step. 9226 */ 9227 if (jremref == NULL) { 9228 free_diradd(dap, NULL); 9229 return; 9230 } 9231 /* 9232 * Cancel the primary name an free it if it does not require 9233 * journaling. 9234 */ 9235 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum, 9236 0, &inodedep) != 0) { 9237 /* Abort the addref that reference this diradd. */ 9238 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 9239 if (inoref->if_list.wk_type != D_JADDREF) 9240 continue; 9241 jaddref = (struct jaddref *)inoref; 9242 if (jaddref->ja_diradd != dap) 9243 continue; 9244 if (cancel_jaddref(jaddref, inodedep, 9245 &dirrem->dm_jwork) == 0) { 9246 free_jremref(jremref); 9247 jremref = NULL; 9248 } 9249 break; 9250 } 9251 } 9252 /* 9253 * Cancel subordinate names and free them if they do not require 9254 * journaling. 9255 */ 9256 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 9257 ump = VFSTOUFS(dap->da_list.wk_mp); 9258 LIST_FOREACH(mkdir, &ump->softdep_mkdirlisthd, md_mkdirs) { 9259 if (mkdir->md_diradd != dap) 9260 continue; 9261 if ((jaddref = mkdir->md_jaddref) == NULL) 9262 continue; 9263 mkdir->md_jaddref = NULL; 9264 if (mkdir->md_state & MKDIR_PARENT) { 9265 if (cancel_jaddref(jaddref, NULL, 9266 &dirrem->dm_jwork) == 0) { 9267 free_jremref(dotdotremref); 9268 dotdotremref = NULL; 9269 } 9270 } else { 9271 if (cancel_jaddref(jaddref, inodedep, 9272 &dirrem->dm_jwork) == 0) { 9273 free_jremref(dotremref); 9274 dotremref = NULL; 9275 } 9276 } 9277 } 9278 } 9279 9280 if (jremref) 9281 journal_jremref(dirrem, jremref, inodedep); 9282 if (dotremref) 9283 journal_jremref(dirrem, dotremref, inodedep); 9284 if (dotdotremref) 9285 journal_jremref(dirrem, dotdotremref, NULL); 9286 jwork_move(&dirrem->dm_jwork, &dap->da_jwork); 9287 free_diradd(dap, &dirrem->dm_jwork); 9288 } 9289 9290 /* 9291 * Free a diradd dependency structure. 9292 */ 9293 static void 9294 free_diradd(dap, wkhd) 9295 struct diradd *dap; 9296 struct workhead *wkhd; 9297 { 9298 struct dirrem *dirrem; 9299 struct pagedep *pagedep; 9300 struct inodedep *inodedep; 9301 struct mkdir *mkdir, *nextmd; 9302 struct ufsmount *ump; 9303 9304 ump = VFSTOUFS(dap->da_list.wk_mp); 9305 LOCK_OWNED(ump); 9306 LIST_REMOVE(dap, da_pdlist); 9307 if (dap->da_state & ONWORKLIST) 9308 WORKLIST_REMOVE(&dap->da_list); 9309 if ((dap->da_state & DIRCHG) == 0) { 9310 pagedep = dap->da_pagedep; 9311 } else { 9312 dirrem = dap->da_previous; 9313 pagedep = dirrem->dm_pagedep; 9314 dirrem->dm_dirinum = pagedep->pd_ino; 9315 dirrem->dm_state |= COMPLETE; 9316 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9317 add_to_worklist(&dirrem->dm_list, 0); 9318 } 9319 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum, 9320 0, &inodedep) != 0) 9321 if (inodedep->id_mkdiradd == dap) 9322 inodedep->id_mkdiradd = NULL; 9323 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 9324 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 9325 mkdir = nextmd) { 9326 nextmd = LIST_NEXT(mkdir, md_mkdirs); 9327 if (mkdir->md_diradd != dap) 9328 continue; 9329 dap->da_state &= 9330 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 9331 LIST_REMOVE(mkdir, md_mkdirs); 9332 if (mkdir->md_state & ONWORKLIST) 9333 WORKLIST_REMOVE(&mkdir->md_list); 9334 if (mkdir->md_jaddref != NULL) 9335 panic("free_diradd: Unexpected jaddref"); 9336 WORKITEM_FREE(mkdir, D_MKDIR); 9337 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) 9338 break; 9339 } 9340 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 9341 panic("free_diradd: unfound ref"); 9342 } 9343 if (inodedep) 9344 free_inodedep(inodedep); 9345 /* 9346 * Free any journal segments waiting for the directory write. 9347 */ 9348 handle_jwork(&dap->da_jwork); 9349 WORKITEM_FREE(dap, D_DIRADD); 9350 } 9351 9352 /* 9353 * Directory entry removal dependencies. 9354 * 9355 * When removing a directory entry, the entry's inode pointer must be 9356 * zero'ed on disk before the corresponding inode's link count is decremented 9357 * (possibly freeing the inode for re-use). This dependency is handled by 9358 * updating the directory entry but delaying the inode count reduction until 9359 * after the directory block has been written to disk. After this point, the 9360 * inode count can be decremented whenever it is convenient. 9361 */ 9362 9363 /* 9364 * This routine should be called immediately after removing 9365 * a directory entry. The inode's link count should not be 9366 * decremented by the calling procedure -- the soft updates 9367 * code will do this task when it is safe. 9368 */ 9369 void 9370 softdep_setup_remove(bp, dp, ip, isrmdir) 9371 struct buf *bp; /* buffer containing directory block */ 9372 struct inode *dp; /* inode for the directory being modified */ 9373 struct inode *ip; /* inode for directory entry being removed */ 9374 int isrmdir; /* indicates if doing RMDIR */ 9375 { 9376 struct dirrem *dirrem, *prevdirrem; 9377 struct inodedep *inodedep; 9378 struct ufsmount *ump; 9379 int direct; 9380 9381 ump = ITOUMP(ip); 9382 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9383 ("softdep_setup_remove called on non-softdep filesystem")); 9384 /* 9385 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want 9386 * newdirrem() to setup the full directory remove which requires 9387 * isrmdir > 1. 9388 */ 9389 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 9390 /* 9391 * Add the dirrem to the inodedep's pending remove list for quick 9392 * discovery later. 9393 */ 9394 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) 9395 panic("softdep_setup_remove: Lost inodedep."); 9396 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 9397 dirrem->dm_state |= ONDEPLIST; 9398 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9399 9400 /* 9401 * If the COMPLETE flag is clear, then there were no active 9402 * entries and we want to roll back to a zeroed entry until 9403 * the new inode is committed to disk. If the COMPLETE flag is 9404 * set then we have deleted an entry that never made it to 9405 * disk. If the entry we deleted resulted from a name change, 9406 * then the old name still resides on disk. We cannot delete 9407 * its inode (returned to us in prevdirrem) until the zeroed 9408 * directory entry gets to disk. The new inode has never been 9409 * referenced on the disk, so can be deleted immediately. 9410 */ 9411 if ((dirrem->dm_state & COMPLETE) == 0) { 9412 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem, 9413 dm_next); 9414 FREE_LOCK(ump); 9415 } else { 9416 if (prevdirrem != NULL) 9417 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, 9418 prevdirrem, dm_next); 9419 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino; 9420 direct = LIST_EMPTY(&dirrem->dm_jremrefhd); 9421 FREE_LOCK(ump); 9422 if (direct) 9423 handle_workitem_remove(dirrem, 0); 9424 } 9425 } 9426 9427 /* 9428 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the 9429 * pd_pendinghd list of a pagedep. 9430 */ 9431 static struct diradd * 9432 diradd_lookup(pagedep, offset) 9433 struct pagedep *pagedep; 9434 int offset; 9435 { 9436 struct diradd *dap; 9437 9438 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist) 9439 if (dap->da_offset == offset) 9440 return (dap); 9441 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 9442 if (dap->da_offset == offset) 9443 return (dap); 9444 return (NULL); 9445 } 9446 9447 /* 9448 * Search for a .. diradd dependency in a directory that is being removed. 9449 * If the directory was renamed to a new parent we have a diradd rather 9450 * than a mkdir for the .. entry. We need to cancel it now before 9451 * it is found in truncate(). 9452 */ 9453 static struct jremref * 9454 cancel_diradd_dotdot(ip, dirrem, jremref) 9455 struct inode *ip; 9456 struct dirrem *dirrem; 9457 struct jremref *jremref; 9458 { 9459 struct pagedep *pagedep; 9460 struct diradd *dap; 9461 struct worklist *wk; 9462 9463 if (pagedep_lookup(ITOVFS(ip), NULL, ip->i_number, 0, 0, &pagedep) == 0) 9464 return (jremref); 9465 dap = diradd_lookup(pagedep, DOTDOT_OFFSET); 9466 if (dap == NULL) 9467 return (jremref); 9468 cancel_diradd(dap, dirrem, jremref, NULL, NULL); 9469 /* 9470 * Mark any journal work as belonging to the parent so it is freed 9471 * with the .. reference. 9472 */ 9473 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9474 wk->wk_state |= MKDIR_PARENT; 9475 return (NULL); 9476 } 9477 9478 /* 9479 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to 9480 * replace it with a dirrem/diradd pair as a result of re-parenting a 9481 * directory. This ensures that we don't simultaneously have a mkdir and 9482 * a diradd for the same .. entry. 9483 */ 9484 static struct jremref * 9485 cancel_mkdir_dotdot(ip, dirrem, jremref) 9486 struct inode *ip; 9487 struct dirrem *dirrem; 9488 struct jremref *jremref; 9489 { 9490 struct inodedep *inodedep; 9491 struct jaddref *jaddref; 9492 struct ufsmount *ump; 9493 struct mkdir *mkdir; 9494 struct diradd *dap; 9495 struct mount *mp; 9496 9497 mp = ITOVFS(ip); 9498 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9499 return (jremref); 9500 dap = inodedep->id_mkdiradd; 9501 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0) 9502 return (jremref); 9503 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9504 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 9505 mkdir = LIST_NEXT(mkdir, md_mkdirs)) 9506 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT) 9507 break; 9508 if (mkdir == NULL) 9509 panic("cancel_mkdir_dotdot: Unable to find mkdir\n"); 9510 if ((jaddref = mkdir->md_jaddref) != NULL) { 9511 mkdir->md_jaddref = NULL; 9512 jaddref->ja_state &= ~MKDIR_PARENT; 9513 if (inodedep_lookup(mp, jaddref->ja_ino, 0, &inodedep) == 0) 9514 panic("cancel_mkdir_dotdot: Lost parent inodedep"); 9515 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) { 9516 journal_jremref(dirrem, jremref, inodedep); 9517 jremref = NULL; 9518 } 9519 } 9520 if (mkdir->md_state & ONWORKLIST) 9521 WORKLIST_REMOVE(&mkdir->md_list); 9522 mkdir->md_state |= ALLCOMPLETE; 9523 complete_mkdir(mkdir); 9524 return (jremref); 9525 } 9526 9527 static void 9528 journal_jremref(dirrem, jremref, inodedep) 9529 struct dirrem *dirrem; 9530 struct jremref *jremref; 9531 struct inodedep *inodedep; 9532 { 9533 9534 if (inodedep == NULL) 9535 if (inodedep_lookup(jremref->jr_list.wk_mp, 9536 jremref->jr_ref.if_ino, 0, &inodedep) == 0) 9537 panic("journal_jremref: Lost inodedep"); 9538 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps); 9539 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 9540 add_to_journal(&jremref->jr_list); 9541 } 9542 9543 static void 9544 dirrem_journal(dirrem, jremref, dotremref, dotdotremref) 9545 struct dirrem *dirrem; 9546 struct jremref *jremref; 9547 struct jremref *dotremref; 9548 struct jremref *dotdotremref; 9549 { 9550 struct inodedep *inodedep; 9551 9552 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0, 9553 &inodedep) == 0) 9554 panic("dirrem_journal: Lost inodedep"); 9555 journal_jremref(dirrem, jremref, inodedep); 9556 if (dotremref) 9557 journal_jremref(dirrem, dotremref, inodedep); 9558 if (dotdotremref) 9559 journal_jremref(dirrem, dotdotremref, NULL); 9560 } 9561 9562 /* 9563 * Allocate a new dirrem if appropriate and return it along with 9564 * its associated pagedep. Called without a lock, returns with lock. 9565 */ 9566 static struct dirrem * 9567 newdirrem(bp, dp, ip, isrmdir, prevdirremp) 9568 struct buf *bp; /* buffer containing directory block */ 9569 struct inode *dp; /* inode for the directory being modified */ 9570 struct inode *ip; /* inode for directory entry being removed */ 9571 int isrmdir; /* indicates if doing RMDIR */ 9572 struct dirrem **prevdirremp; /* previously referenced inode, if any */ 9573 { 9574 int offset; 9575 ufs_lbn_t lbn; 9576 struct diradd *dap; 9577 struct dirrem *dirrem; 9578 struct pagedep *pagedep; 9579 struct jremref *jremref; 9580 struct jremref *dotremref; 9581 struct jremref *dotdotremref; 9582 struct vnode *dvp; 9583 struct ufsmount *ump; 9584 9585 /* 9586 * Whiteouts have no deletion dependencies. 9587 */ 9588 if (ip == NULL) 9589 panic("newdirrem: whiteout"); 9590 dvp = ITOV(dp); 9591 ump = ITOUMP(dp); 9592 9593 /* 9594 * If the system is over its limit and our filesystem is 9595 * responsible for more than our share of that usage and 9596 * we are not a snapshot, request some inodedep cleanup. 9597 * Limiting the number of dirrem structures will also limit 9598 * the number of freefile and freeblks structures. 9599 */ 9600 ACQUIRE_LOCK(ump); 9601 if (!IS_SNAPSHOT(ip) && softdep_excess_items(ump, D_DIRREM)) 9602 schedule_cleanup(UFSTOVFS(ump)); 9603 else 9604 FREE_LOCK(ump); 9605 dirrem = malloc(sizeof(struct dirrem), M_DIRREM, M_SOFTDEP_FLAGS | 9606 M_ZERO); 9607 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount); 9608 LIST_INIT(&dirrem->dm_jremrefhd); 9609 LIST_INIT(&dirrem->dm_jwork); 9610 dirrem->dm_state = isrmdir ? RMDIR : 0; 9611 dirrem->dm_oldinum = ip->i_number; 9612 *prevdirremp = NULL; 9613 /* 9614 * Allocate remove reference structures to track journal write 9615 * dependencies. We will always have one for the link and 9616 * when doing directories we will always have one more for dot. 9617 * When renaming a directory we skip the dotdot link change so 9618 * this is not needed. 9619 */ 9620 jremref = dotremref = dotdotremref = NULL; 9621 if (DOINGSUJ(dvp)) { 9622 if (isrmdir) { 9623 jremref = newjremref(dirrem, dp, ip, I_OFFSET(dp), 9624 ip->i_effnlink + 2); 9625 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET, 9626 ip->i_effnlink + 1); 9627 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET, 9628 dp->i_effnlink + 1); 9629 dotdotremref->jr_state |= MKDIR_PARENT; 9630 } else 9631 jremref = newjremref(dirrem, dp, ip, I_OFFSET(dp), 9632 ip->i_effnlink + 1); 9633 } 9634 ACQUIRE_LOCK(ump); 9635 lbn = lblkno(ump->um_fs, I_OFFSET(dp)); 9636 offset = blkoff(ump->um_fs, I_OFFSET(dp)); 9637 pagedep_lookup(UFSTOVFS(ump), bp, dp->i_number, lbn, DEPALLOC, 9638 &pagedep); 9639 dirrem->dm_pagedep = pagedep; 9640 dirrem->dm_offset = offset; 9641 /* 9642 * If we're renaming a .. link to a new directory, cancel any 9643 * existing MKDIR_PARENT mkdir. If it has already been canceled 9644 * the jremref is preserved for any potential diradd in this 9645 * location. This can not coincide with a rmdir. 9646 */ 9647 if (I_OFFSET(dp) == DOTDOT_OFFSET) { 9648 if (isrmdir) 9649 panic("newdirrem: .. directory change during remove?"); 9650 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref); 9651 } 9652 /* 9653 * If we're removing a directory search for the .. dependency now and 9654 * cancel it. Any pending journal work will be added to the dirrem 9655 * to be completed when the workitem remove completes. 9656 */ 9657 if (isrmdir) 9658 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref); 9659 /* 9660 * Check for a diradd dependency for the same directory entry. 9661 * If present, then both dependencies become obsolete and can 9662 * be de-allocated. 9663 */ 9664 dap = diradd_lookup(pagedep, offset); 9665 if (dap == NULL) { 9666 /* 9667 * Link the jremref structures into the dirrem so they are 9668 * written prior to the pagedep. 9669 */ 9670 if (jremref) 9671 dirrem_journal(dirrem, jremref, dotremref, 9672 dotdotremref); 9673 return (dirrem); 9674 } 9675 /* 9676 * Must be ATTACHED at this point. 9677 */ 9678 if ((dap->da_state & ATTACHED) == 0) 9679 panic("newdirrem: not ATTACHED"); 9680 if (dap->da_newinum != ip->i_number) 9681 panic("newdirrem: inum %ju should be %ju", 9682 (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum); 9683 /* 9684 * If we are deleting a changed name that never made it to disk, 9685 * then return the dirrem describing the previous inode (which 9686 * represents the inode currently referenced from this entry on disk). 9687 */ 9688 if ((dap->da_state & DIRCHG) != 0) { 9689 *prevdirremp = dap->da_previous; 9690 dap->da_state &= ~DIRCHG; 9691 dap->da_pagedep = pagedep; 9692 } 9693 /* 9694 * We are deleting an entry that never made it to disk. 9695 * Mark it COMPLETE so we can delete its inode immediately. 9696 */ 9697 dirrem->dm_state |= COMPLETE; 9698 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref); 9699 #ifdef INVARIANTS 9700 if (isrmdir == 0) { 9701 struct worklist *wk; 9702 9703 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9704 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT)) 9705 panic("bad wk %p (0x%X)\n", wk, wk->wk_state); 9706 } 9707 #endif 9708 9709 return (dirrem); 9710 } 9711 9712 /* 9713 * Directory entry change dependencies. 9714 * 9715 * Changing an existing directory entry requires that an add operation 9716 * be completed first followed by a deletion. The semantics for the addition 9717 * are identical to the description of adding a new entry above except 9718 * that the rollback is to the old inode number rather than zero. Once 9719 * the addition dependency is completed, the removal is done as described 9720 * in the removal routine above. 9721 */ 9722 9723 /* 9724 * This routine should be called immediately after changing 9725 * a directory entry. The inode's link count should not be 9726 * decremented by the calling procedure -- the soft updates 9727 * code will perform this task when it is safe. 9728 */ 9729 void 9730 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 9731 struct buf *bp; /* buffer containing directory block */ 9732 struct inode *dp; /* inode for the directory being modified */ 9733 struct inode *ip; /* inode for directory entry being removed */ 9734 ino_t newinum; /* new inode number for changed entry */ 9735 int isrmdir; /* indicates if doing RMDIR */ 9736 { 9737 int offset; 9738 struct diradd *dap = NULL; 9739 struct dirrem *dirrem, *prevdirrem; 9740 struct pagedep *pagedep; 9741 struct inodedep *inodedep; 9742 struct jaddref *jaddref; 9743 struct mount *mp; 9744 struct ufsmount *ump; 9745 9746 mp = ITOVFS(dp); 9747 ump = VFSTOUFS(mp); 9748 offset = blkoff(ump->um_fs, I_OFFSET(dp)); 9749 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 9750 ("softdep_setup_directory_change called on non-softdep filesystem")); 9751 9752 /* 9753 * Whiteouts do not need diradd dependencies. 9754 */ 9755 if (newinum != UFS_WINO) { 9756 dap = malloc(sizeof(struct diradd), 9757 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO); 9758 workitem_alloc(&dap->da_list, D_DIRADD, mp); 9759 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE; 9760 dap->da_offset = offset; 9761 dap->da_newinum = newinum; 9762 LIST_INIT(&dap->da_jwork); 9763 } 9764 9765 /* 9766 * Allocate a new dirrem and ACQUIRE_LOCK. 9767 */ 9768 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 9769 pagedep = dirrem->dm_pagedep; 9770 /* 9771 * The possible values for isrmdir: 9772 * 0 - non-directory file rename 9773 * 1 - directory rename within same directory 9774 * inum - directory rename to new directory of given inode number 9775 * When renaming to a new directory, we are both deleting and 9776 * creating a new directory entry, so the link count on the new 9777 * directory should not change. Thus we do not need the followup 9778 * dirrem which is usually done in handle_workitem_remove. We set 9779 * the DIRCHG flag to tell handle_workitem_remove to skip the 9780 * followup dirrem. 9781 */ 9782 if (isrmdir > 1) 9783 dirrem->dm_state |= DIRCHG; 9784 9785 /* 9786 * Whiteouts have no additional dependencies, 9787 * so just put the dirrem on the correct list. 9788 */ 9789 if (newinum == UFS_WINO) { 9790 if ((dirrem->dm_state & COMPLETE) == 0) { 9791 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem, 9792 dm_next); 9793 } else { 9794 dirrem->dm_dirinum = pagedep->pd_ino; 9795 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9796 add_to_worklist(&dirrem->dm_list, 0); 9797 } 9798 FREE_LOCK(ump); 9799 return; 9800 } 9801 /* 9802 * Add the dirrem to the inodedep's pending remove list for quick 9803 * discovery later. A valid nlinkdelta ensures that this lookup 9804 * will not fail. 9805 */ 9806 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9807 panic("softdep_setup_directory_change: Lost inodedep."); 9808 dirrem->dm_state |= ONDEPLIST; 9809 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9810 9811 /* 9812 * If the COMPLETE flag is clear, then there were no active 9813 * entries and we want to roll back to the previous inode until 9814 * the new inode is committed to disk. If the COMPLETE flag is 9815 * set, then we have deleted an entry that never made it to disk. 9816 * If the entry we deleted resulted from a name change, then the old 9817 * inode reference still resides on disk. Any rollback that we do 9818 * needs to be to that old inode (returned to us in prevdirrem). If 9819 * the entry we deleted resulted from a create, then there is 9820 * no entry on the disk, so we want to roll back to zero rather 9821 * than the uncommitted inode. In either of the COMPLETE cases we 9822 * want to immediately free the unwritten and unreferenced inode. 9823 */ 9824 if ((dirrem->dm_state & COMPLETE) == 0) { 9825 dap->da_previous = dirrem; 9826 } else { 9827 if (prevdirrem != NULL) { 9828 dap->da_previous = prevdirrem; 9829 } else { 9830 dap->da_state &= ~DIRCHG; 9831 dap->da_pagedep = pagedep; 9832 } 9833 dirrem->dm_dirinum = pagedep->pd_ino; 9834 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9835 add_to_worklist(&dirrem->dm_list, 0); 9836 } 9837 /* 9838 * Lookup the jaddref for this journal entry. We must finish 9839 * initializing it and make the diradd write dependent on it. 9840 * If we're not journaling, put it on the id_bufwait list if the 9841 * inode is not yet written. If it is written, do the post-inode 9842 * write processing to put it on the id_pendinghd list. 9843 */ 9844 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 9845 if (MOUNTEDSUJ(mp)) { 9846 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 9847 inoreflst); 9848 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 9849 ("softdep_setup_directory_change: bad jaddref %p", 9850 jaddref)); 9851 jaddref->ja_diroff = I_OFFSET(dp); 9852 jaddref->ja_diradd = dap; 9853 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9854 dap, da_pdlist); 9855 add_to_journal(&jaddref->ja_list); 9856 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 9857 dap->da_state |= COMPLETE; 9858 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 9859 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 9860 } else { 9861 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9862 dap, da_pdlist); 9863 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 9864 } 9865 /* 9866 * If we're making a new name for a directory that has not been 9867 * committed when need to move the dot and dotdot references to 9868 * this new name. 9869 */ 9870 if (inodedep->id_mkdiradd && I_OFFSET(dp) != DOTDOT_OFFSET) 9871 merge_diradd(inodedep, dap); 9872 FREE_LOCK(ump); 9873 } 9874 9875 /* 9876 * Called whenever the link count on an inode is changed. 9877 * It creates an inode dependency so that the new reference(s) 9878 * to the inode cannot be committed to disk until the updated 9879 * inode has been written. 9880 */ 9881 void 9882 softdep_change_linkcnt(ip) 9883 struct inode *ip; /* the inode with the increased link count */ 9884 { 9885 struct inodedep *inodedep; 9886 struct ufsmount *ump; 9887 9888 ump = ITOUMP(ip); 9889 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9890 ("softdep_change_linkcnt called on non-softdep filesystem")); 9891 ACQUIRE_LOCK(ump); 9892 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep); 9893 if (ip->i_nlink < ip->i_effnlink) 9894 panic("softdep_change_linkcnt: bad delta"); 9895 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9896 FREE_LOCK(ump); 9897 } 9898 9899 /* 9900 * Attach a sbdep dependency to the superblock buf so that we can keep 9901 * track of the head of the linked list of referenced but unlinked inodes. 9902 */ 9903 void 9904 softdep_setup_sbupdate(ump, fs, bp) 9905 struct ufsmount *ump; 9906 struct fs *fs; 9907 struct buf *bp; 9908 { 9909 struct sbdep *sbdep; 9910 struct worklist *wk; 9911 9912 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9913 ("softdep_setup_sbupdate called on non-softdep filesystem")); 9914 LIST_FOREACH(wk, &bp->b_dep, wk_list) 9915 if (wk->wk_type == D_SBDEP) 9916 break; 9917 if (wk != NULL) 9918 return; 9919 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS); 9920 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump)); 9921 sbdep->sb_fs = fs; 9922 sbdep->sb_ump = ump; 9923 ACQUIRE_LOCK(ump); 9924 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list); 9925 FREE_LOCK(ump); 9926 } 9927 9928 /* 9929 * Return the first unlinked inodedep which is ready to be the head of the 9930 * list. The inodedep and all those after it must have valid next pointers. 9931 */ 9932 static struct inodedep * 9933 first_unlinked_inodedep(ump) 9934 struct ufsmount *ump; 9935 { 9936 struct inodedep *inodedep; 9937 struct inodedep *idp; 9938 9939 LOCK_OWNED(ump); 9940 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst); 9941 inodedep; inodedep = idp) { 9942 if ((inodedep->id_state & UNLINKNEXT) == 0) 9943 return (NULL); 9944 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9945 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0) 9946 break; 9947 if ((inodedep->id_state & UNLINKPREV) == 0) 9948 break; 9949 } 9950 return (inodedep); 9951 } 9952 9953 /* 9954 * Set the sujfree unlinked head pointer prior to writing a superblock. 9955 */ 9956 static void 9957 initiate_write_sbdep(sbdep) 9958 struct sbdep *sbdep; 9959 { 9960 struct inodedep *inodedep; 9961 struct fs *bpfs; 9962 struct fs *fs; 9963 9964 bpfs = sbdep->sb_fs; 9965 fs = sbdep->sb_ump->um_fs; 9966 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9967 if (inodedep) { 9968 fs->fs_sujfree = inodedep->id_ino; 9969 inodedep->id_state |= UNLINKPREV; 9970 } else 9971 fs->fs_sujfree = 0; 9972 bpfs->fs_sujfree = fs->fs_sujfree; 9973 /* 9974 * Because we have made changes to the superblock, we need to 9975 * recompute its check-hash. 9976 */ 9977 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs); 9978 } 9979 9980 /* 9981 * After a superblock is written determine whether it must be written again 9982 * due to a changing unlinked list head. 9983 */ 9984 static int 9985 handle_written_sbdep(sbdep, bp) 9986 struct sbdep *sbdep; 9987 struct buf *bp; 9988 { 9989 struct inodedep *inodedep; 9990 struct fs *fs; 9991 9992 LOCK_OWNED(sbdep->sb_ump); 9993 fs = sbdep->sb_fs; 9994 /* 9995 * If the superblock doesn't match the in-memory list start over. 9996 */ 9997 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9998 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) || 9999 (inodedep == NULL && fs->fs_sujfree != 0)) { 10000 bdirty(bp); 10001 return (1); 10002 } 10003 WORKITEM_FREE(sbdep, D_SBDEP); 10004 if (fs->fs_sujfree == 0) 10005 return (0); 10006 /* 10007 * Now that we have a record of this inode in stable store allow it 10008 * to be written to free up pending work. Inodes may see a lot of 10009 * write activity after they are unlinked which we must not hold up. 10010 */ 10011 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 10012 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS) 10013 panic("handle_written_sbdep: Bad inodedep %p (0x%X)", 10014 inodedep, inodedep->id_state); 10015 if (inodedep->id_state & UNLINKONLIST) 10016 break; 10017 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST; 10018 } 10019 10020 return (0); 10021 } 10022 10023 /* 10024 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list. 10025 */ 10026 static void 10027 unlinked_inodedep(mp, inodedep) 10028 struct mount *mp; 10029 struct inodedep *inodedep; 10030 { 10031 struct ufsmount *ump; 10032 10033 ump = VFSTOUFS(mp); 10034 LOCK_OWNED(ump); 10035 if (MOUNTEDSUJ(mp) == 0) 10036 return; 10037 ump->um_fs->fs_fmod = 1; 10038 if (inodedep->id_state & UNLINKED) 10039 panic("unlinked_inodedep: %p already unlinked\n", inodedep); 10040 inodedep->id_state |= UNLINKED; 10041 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked); 10042 } 10043 10044 /* 10045 * Remove an inodedep from the unlinked inodedep list. This may require 10046 * disk writes if the inode has made it that far. 10047 */ 10048 static void 10049 clear_unlinked_inodedep(inodedep) 10050 struct inodedep *inodedep; 10051 { 10052 struct ufs2_dinode *dip; 10053 struct ufsmount *ump; 10054 struct inodedep *idp; 10055 struct inodedep *idn; 10056 struct fs *fs, *bpfs; 10057 struct buf *bp; 10058 daddr_t dbn; 10059 ino_t ino; 10060 ino_t nino; 10061 ino_t pino; 10062 int error; 10063 10064 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10065 fs = ump->um_fs; 10066 ino = inodedep->id_ino; 10067 error = 0; 10068 for (;;) { 10069 LOCK_OWNED(ump); 10070 KASSERT((inodedep->id_state & UNLINKED) != 0, 10071 ("clear_unlinked_inodedep: inodedep %p not unlinked", 10072 inodedep)); 10073 /* 10074 * If nothing has yet been written simply remove us from 10075 * the in memory list and return. This is the most common 10076 * case where handle_workitem_remove() loses the final 10077 * reference. 10078 */ 10079 if ((inodedep->id_state & UNLINKLINKS) == 0) 10080 break; 10081 /* 10082 * If we have a NEXT pointer and no PREV pointer we can simply 10083 * clear NEXT's PREV and remove ourselves from the list. Be 10084 * careful not to clear PREV if the superblock points at 10085 * next as well. 10086 */ 10087 idn = TAILQ_NEXT(inodedep, id_unlinked); 10088 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) { 10089 if (idn && fs->fs_sujfree != idn->id_ino) 10090 idn->id_state &= ~UNLINKPREV; 10091 break; 10092 } 10093 /* 10094 * Here we have an inodedep which is actually linked into 10095 * the list. We must remove it by forcing a write to the 10096 * link before us, whether it be the superblock or an inode. 10097 * Unfortunately the list may change while we're waiting 10098 * on the buf lock for either resource so we must loop until 10099 * we lock the right one. If both the superblock and an 10100 * inode point to this inode we must clear the inode first 10101 * followed by the superblock. 10102 */ 10103 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 10104 pino = 0; 10105 if (idp && (idp->id_state & UNLINKNEXT)) 10106 pino = idp->id_ino; 10107 FREE_LOCK(ump); 10108 if (pino == 0) { 10109 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 10110 (int)fs->fs_sbsize, 0, 0, 0); 10111 } else { 10112 dbn = fsbtodb(fs, ino_to_fsba(fs, pino)); 10113 error = ffs_breadz(ump, ump->um_devvp, dbn, dbn, 10114 (int)fs->fs_bsize, NULL, NULL, 0, NOCRED, 0, NULL, 10115 &bp); 10116 } 10117 ACQUIRE_LOCK(ump); 10118 if (error) 10119 break; 10120 /* If the list has changed restart the loop. */ 10121 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 10122 nino = 0; 10123 if (idp && (idp->id_state & UNLINKNEXT)) 10124 nino = idp->id_ino; 10125 if (nino != pino || 10126 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) { 10127 FREE_LOCK(ump); 10128 brelse(bp); 10129 ACQUIRE_LOCK(ump); 10130 continue; 10131 } 10132 nino = 0; 10133 idn = TAILQ_NEXT(inodedep, id_unlinked); 10134 if (idn) 10135 nino = idn->id_ino; 10136 /* 10137 * Remove us from the in memory list. After this we cannot 10138 * access the inodedep. 10139 */ 10140 KASSERT((inodedep->id_state & UNLINKED) != 0, 10141 ("clear_unlinked_inodedep: inodedep %p not unlinked", 10142 inodedep)); 10143 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 10144 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 10145 FREE_LOCK(ump); 10146 /* 10147 * The predecessor's next pointer is manually updated here 10148 * so that the NEXT flag is never cleared for an element 10149 * that is in the list. 10150 */ 10151 if (pino == 0) { 10152 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 10153 bpfs = (struct fs *)bp->b_data; 10154 ffs_oldfscompat_write(bpfs, ump); 10155 softdep_setup_sbupdate(ump, bpfs, bp); 10156 /* 10157 * Because we may have made changes to the superblock, 10158 * we need to recompute its check-hash. 10159 */ 10160 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs); 10161 } else if (fs->fs_magic == FS_UFS1_MAGIC) { 10162 ((struct ufs1_dinode *)bp->b_data + 10163 ino_to_fsbo(fs, pino))->di_freelink = nino; 10164 } else { 10165 dip = (struct ufs2_dinode *)bp->b_data + 10166 ino_to_fsbo(fs, pino); 10167 dip->di_freelink = nino; 10168 ffs_update_dinode_ckhash(fs, dip); 10169 } 10170 /* 10171 * If the bwrite fails we have no recourse to recover. The 10172 * filesystem is corrupted already. 10173 */ 10174 bwrite(bp); 10175 ACQUIRE_LOCK(ump); 10176 /* 10177 * If the superblock pointer still needs to be cleared force 10178 * a write here. 10179 */ 10180 if (fs->fs_sujfree == ino) { 10181 FREE_LOCK(ump); 10182 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 10183 (int)fs->fs_sbsize, 0, 0, 0); 10184 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 10185 bpfs = (struct fs *)bp->b_data; 10186 ffs_oldfscompat_write(bpfs, ump); 10187 softdep_setup_sbupdate(ump, bpfs, bp); 10188 /* 10189 * Because we may have made changes to the superblock, 10190 * we need to recompute its check-hash. 10191 */ 10192 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs); 10193 bwrite(bp); 10194 ACQUIRE_LOCK(ump); 10195 } 10196 10197 if (fs->fs_sujfree != ino) 10198 return; 10199 panic("clear_unlinked_inodedep: Failed to clear free head"); 10200 } 10201 if (inodedep->id_ino == fs->fs_sujfree) 10202 panic("clear_unlinked_inodedep: Freeing head of free list"); 10203 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 10204 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 10205 return; 10206 } 10207 10208 /* 10209 * This workitem decrements the inode's link count. 10210 * If the link count reaches zero, the file is removed. 10211 */ 10212 static int 10213 handle_workitem_remove(dirrem, flags) 10214 struct dirrem *dirrem; 10215 int flags; 10216 { 10217 struct inodedep *inodedep; 10218 struct workhead dotdotwk; 10219 struct worklist *wk; 10220 struct ufsmount *ump; 10221 struct mount *mp; 10222 struct vnode *vp; 10223 struct inode *ip; 10224 ino_t oldinum; 10225 10226 if (dirrem->dm_state & ONWORKLIST) 10227 panic("handle_workitem_remove: dirrem %p still on worklist", 10228 dirrem); 10229 oldinum = dirrem->dm_oldinum; 10230 mp = dirrem->dm_list.wk_mp; 10231 ump = VFSTOUFS(mp); 10232 flags |= LK_EXCLUSIVE; 10233 if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ | 10234 FFSV_FORCEINODEDEP) != 0) 10235 return (EBUSY); 10236 ip = VTOI(vp); 10237 MPASS(ip->i_mode != 0); 10238 ACQUIRE_LOCK(ump); 10239 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0) 10240 panic("handle_workitem_remove: lost inodedep"); 10241 if (dirrem->dm_state & ONDEPLIST) 10242 LIST_REMOVE(dirrem, dm_inonext); 10243 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 10244 ("handle_workitem_remove: Journal entries not written.")); 10245 10246 /* 10247 * Move all dependencies waiting on the remove to complete 10248 * from the dirrem to the inode inowait list to be completed 10249 * after the inode has been updated and written to disk. 10250 * 10251 * Any marked MKDIR_PARENT are saved to be completed when the 10252 * dotdot ref is removed unless DIRCHG is specified. For 10253 * directory change operations there will be no further 10254 * directory writes and the jsegdeps need to be moved along 10255 * with the rest to be completed when the inode is free or 10256 * stable in the inode free list. 10257 */ 10258 LIST_INIT(&dotdotwk); 10259 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) { 10260 WORKLIST_REMOVE(wk); 10261 if ((dirrem->dm_state & DIRCHG) == 0 && 10262 wk->wk_state & MKDIR_PARENT) { 10263 wk->wk_state &= ~MKDIR_PARENT; 10264 WORKLIST_INSERT(&dotdotwk, wk); 10265 continue; 10266 } 10267 WORKLIST_INSERT(&inodedep->id_inowait, wk); 10268 } 10269 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list); 10270 /* 10271 * Normal file deletion. 10272 */ 10273 if ((dirrem->dm_state & RMDIR) == 0) { 10274 ip->i_nlink--; 10275 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: file ino " 10276 "%ju negative i_nlink %d", (intmax_t)ip->i_number, 10277 ip->i_nlink)); 10278 DIP_SET(ip, i_nlink, ip->i_nlink); 10279 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 10280 if (ip->i_nlink < ip->i_effnlink) 10281 panic("handle_workitem_remove: bad file delta"); 10282 if (ip->i_nlink == 0) 10283 unlinked_inodedep(mp, inodedep); 10284 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 10285 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 10286 ("handle_workitem_remove: worklist not empty. %s", 10287 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type))); 10288 WORKITEM_FREE(dirrem, D_DIRREM); 10289 FREE_LOCK(ump); 10290 goto out; 10291 } 10292 /* 10293 * Directory deletion. Decrement reference count for both the 10294 * just deleted parent directory entry and the reference for ".". 10295 * Arrange to have the reference count on the parent decremented 10296 * to account for the loss of "..". 10297 */ 10298 ip->i_nlink -= 2; 10299 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: directory ino " 10300 "%ju negative i_nlink %d", (intmax_t)ip->i_number, ip->i_nlink)); 10301 DIP_SET(ip, i_nlink, ip->i_nlink); 10302 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 10303 if (ip->i_nlink < ip->i_effnlink) 10304 panic("handle_workitem_remove: bad dir delta"); 10305 if (ip->i_nlink == 0) 10306 unlinked_inodedep(mp, inodedep); 10307 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 10308 /* 10309 * Rename a directory to a new parent. Since, we are both deleting 10310 * and creating a new directory entry, the link count on the new 10311 * directory should not change. Thus we skip the followup dirrem. 10312 */ 10313 if (dirrem->dm_state & DIRCHG) { 10314 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 10315 ("handle_workitem_remove: DIRCHG and worklist not empty.")); 10316 WORKITEM_FREE(dirrem, D_DIRREM); 10317 FREE_LOCK(ump); 10318 goto out; 10319 } 10320 dirrem->dm_state = ONDEPLIST; 10321 dirrem->dm_oldinum = dirrem->dm_dirinum; 10322 /* 10323 * Place the dirrem on the parent's diremhd list. 10324 */ 10325 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0) 10326 panic("handle_workitem_remove: lost dir inodedep"); 10327 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 10328 /* 10329 * If the allocated inode has never been written to disk, then 10330 * the on-disk inode is zero'ed and we can remove the file 10331 * immediately. When journaling if the inode has been marked 10332 * unlinked and not DEPCOMPLETE we know it can never be written. 10333 */ 10334 inodedep_lookup(mp, oldinum, 0, &inodedep); 10335 if (inodedep == NULL || 10336 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED || 10337 check_inode_unwritten(inodedep)) { 10338 FREE_LOCK(ump); 10339 vput(vp); 10340 return handle_workitem_remove(dirrem, flags); 10341 } 10342 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list); 10343 FREE_LOCK(ump); 10344 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 10345 out: 10346 ffs_update(vp, 0); 10347 vput(vp); 10348 return (0); 10349 } 10350 10351 /* 10352 * Inode de-allocation dependencies. 10353 * 10354 * When an inode's link count is reduced to zero, it can be de-allocated. We 10355 * found it convenient to postpone de-allocation until after the inode is 10356 * written to disk with its new link count (zero). At this point, all of the 10357 * on-disk inode's block pointers are nullified and, with careful dependency 10358 * list ordering, all dependencies related to the inode will be satisfied and 10359 * the corresponding dependency structures de-allocated. So, if/when the 10360 * inode is reused, there will be no mixing of old dependencies with new 10361 * ones. This artificial dependency is set up by the block de-allocation 10362 * procedure above (softdep_setup_freeblocks) and completed by the 10363 * following procedure. 10364 */ 10365 static void 10366 handle_workitem_freefile(freefile) 10367 struct freefile *freefile; 10368 { 10369 struct workhead wkhd; 10370 struct fs *fs; 10371 struct ufsmount *ump; 10372 int error; 10373 #ifdef INVARIANTS 10374 struct inodedep *idp; 10375 #endif 10376 10377 ump = VFSTOUFS(freefile->fx_list.wk_mp); 10378 fs = ump->um_fs; 10379 #ifdef INVARIANTS 10380 ACQUIRE_LOCK(ump); 10381 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp); 10382 FREE_LOCK(ump); 10383 if (error) 10384 panic("handle_workitem_freefile: inodedep %p survived", idp); 10385 #endif 10386 UFS_LOCK(ump); 10387 fs->fs_pendinginodes -= 1; 10388 UFS_UNLOCK(ump); 10389 LIST_INIT(&wkhd); 10390 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list); 10391 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp, 10392 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0) 10393 softdep_error("handle_workitem_freefile", error); 10394 ACQUIRE_LOCK(ump); 10395 WORKITEM_FREE(freefile, D_FREEFILE); 10396 FREE_LOCK(ump); 10397 } 10398 10399 /* 10400 * Helper function which unlinks marker element from work list and returns 10401 * the next element on the list. 10402 */ 10403 static __inline struct worklist * 10404 markernext(struct worklist *marker) 10405 { 10406 struct worklist *next; 10407 10408 next = LIST_NEXT(marker, wk_list); 10409 LIST_REMOVE(marker, wk_list); 10410 return next; 10411 } 10412 10413 /* 10414 * Disk writes. 10415 * 10416 * The dependency structures constructed above are most actively used when file 10417 * system blocks are written to disk. No constraints are placed on when a 10418 * block can be written, but unsatisfied update dependencies are made safe by 10419 * modifying (or replacing) the source memory for the duration of the disk 10420 * write. When the disk write completes, the memory block is again brought 10421 * up-to-date. 10422 * 10423 * In-core inode structure reclamation. 10424 * 10425 * Because there are a finite number of "in-core" inode structures, they are 10426 * reused regularly. By transferring all inode-related dependencies to the 10427 * in-memory inode block and indexing them separately (via "inodedep"s), we 10428 * can allow "in-core" inode structures to be reused at any time and avoid 10429 * any increase in contention. 10430 * 10431 * Called just before entering the device driver to initiate a new disk I/O. 10432 * The buffer must be locked, thus, no I/O completion operations can occur 10433 * while we are manipulating its associated dependencies. 10434 */ 10435 static void 10436 softdep_disk_io_initiation(bp) 10437 struct buf *bp; /* structure describing disk write to occur */ 10438 { 10439 struct worklist *wk; 10440 struct worklist marker; 10441 struct inodedep *inodedep; 10442 struct freeblks *freeblks; 10443 struct jblkdep *jblkdep; 10444 struct newblk *newblk; 10445 struct ufsmount *ump; 10446 10447 /* 10448 * We only care about write operations. There should never 10449 * be dependencies for reads. 10450 */ 10451 if (bp->b_iocmd != BIO_WRITE) 10452 panic("softdep_disk_io_initiation: not write"); 10453 10454 if (bp->b_vflags & BV_BKGRDINPROG) 10455 panic("softdep_disk_io_initiation: Writing buffer with " 10456 "background write in progress: %p", bp); 10457 10458 ump = softdep_bp_to_mp(bp); 10459 if (ump == NULL) 10460 return; 10461 10462 marker.wk_type = D_LAST + 1; /* Not a normal workitem */ 10463 PHOLD(curproc); /* Don't swap out kernel stack */ 10464 ACQUIRE_LOCK(ump); 10465 /* 10466 * Do any necessary pre-I/O processing. 10467 */ 10468 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL; 10469 wk = markernext(&marker)) { 10470 LIST_INSERT_AFTER(wk, &marker, wk_list); 10471 switch (wk->wk_type) { 10472 case D_PAGEDEP: 10473 initiate_write_filepage(WK_PAGEDEP(wk), bp); 10474 continue; 10475 10476 case D_INODEDEP: 10477 inodedep = WK_INODEDEP(wk); 10478 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) 10479 initiate_write_inodeblock_ufs1(inodedep, bp); 10480 else 10481 initiate_write_inodeblock_ufs2(inodedep, bp); 10482 continue; 10483 10484 case D_INDIRDEP: 10485 initiate_write_indirdep(WK_INDIRDEP(wk), bp); 10486 continue; 10487 10488 case D_BMSAFEMAP: 10489 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp); 10490 continue; 10491 10492 case D_JSEG: 10493 WK_JSEG(wk)->js_buf = NULL; 10494 continue; 10495 10496 case D_FREEBLKS: 10497 freeblks = WK_FREEBLKS(wk); 10498 jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd); 10499 /* 10500 * We have to wait for the freeblks to be journaled 10501 * before we can write an inodeblock with updated 10502 * pointers. Be careful to arrange the marker so 10503 * we revisit the freeblks if it's not removed by 10504 * the first jwait(). 10505 */ 10506 if (jblkdep != NULL) { 10507 LIST_REMOVE(&marker, wk_list); 10508 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10509 jwait(&jblkdep->jb_list, MNT_WAIT); 10510 } 10511 continue; 10512 case D_ALLOCDIRECT: 10513 case D_ALLOCINDIR: 10514 /* 10515 * We have to wait for the jnewblk to be journaled 10516 * before we can write to a block if the contents 10517 * may be confused with an earlier file's indirect 10518 * at recovery time. Handle the marker as described 10519 * above. 10520 */ 10521 newblk = WK_NEWBLK(wk); 10522 if (newblk->nb_jnewblk != NULL && 10523 indirblk_lookup(newblk->nb_list.wk_mp, 10524 newblk->nb_newblkno)) { 10525 LIST_REMOVE(&marker, wk_list); 10526 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10527 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 10528 } 10529 continue; 10530 10531 case D_SBDEP: 10532 initiate_write_sbdep(WK_SBDEP(wk)); 10533 continue; 10534 10535 case D_MKDIR: 10536 case D_FREEWORK: 10537 case D_FREEDEP: 10538 case D_JSEGDEP: 10539 continue; 10540 10541 default: 10542 panic("handle_disk_io_initiation: Unexpected type %s", 10543 TYPENAME(wk->wk_type)); 10544 /* NOTREACHED */ 10545 } 10546 } 10547 FREE_LOCK(ump); 10548 PRELE(curproc); /* Allow swapout of kernel stack */ 10549 } 10550 10551 /* 10552 * Called from within the procedure above to deal with unsatisfied 10553 * allocation dependencies in a directory. The buffer must be locked, 10554 * thus, no I/O completion operations can occur while we are 10555 * manipulating its associated dependencies. 10556 */ 10557 static void 10558 initiate_write_filepage(pagedep, bp) 10559 struct pagedep *pagedep; 10560 struct buf *bp; 10561 { 10562 struct jremref *jremref; 10563 struct jmvref *jmvref; 10564 struct dirrem *dirrem; 10565 struct diradd *dap; 10566 struct direct *ep; 10567 int i; 10568 10569 if (pagedep->pd_state & IOSTARTED) { 10570 /* 10571 * This can only happen if there is a driver that does not 10572 * understand chaining. Here biodone will reissue the call 10573 * to strategy for the incomplete buffers. 10574 */ 10575 printf("initiate_write_filepage: already started\n"); 10576 return; 10577 } 10578 pagedep->pd_state |= IOSTARTED; 10579 /* 10580 * Wait for all journal remove dependencies to hit the disk. 10581 * We can not allow any potentially conflicting directory adds 10582 * to be visible before removes and rollback is too difficult. 10583 * The per-filesystem lock may be dropped and re-acquired, however 10584 * we hold the buf locked so the dependency can not go away. 10585 */ 10586 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) 10587 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) 10588 jwait(&jremref->jr_list, MNT_WAIT); 10589 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) 10590 jwait(&jmvref->jm_list, MNT_WAIT); 10591 for (i = 0; i < DAHASHSZ; i++) { 10592 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 10593 ep = (struct direct *) 10594 ((char *)bp->b_data + dap->da_offset); 10595 if (ep->d_ino != dap->da_newinum) 10596 panic("%s: dir inum %ju != new %ju", 10597 "initiate_write_filepage", 10598 (uintmax_t)ep->d_ino, 10599 (uintmax_t)dap->da_newinum); 10600 if (dap->da_state & DIRCHG) 10601 ep->d_ino = dap->da_previous->dm_oldinum; 10602 else 10603 ep->d_ino = 0; 10604 dap->da_state &= ~ATTACHED; 10605 dap->da_state |= UNDONE; 10606 } 10607 } 10608 } 10609 10610 /* 10611 * Version of initiate_write_inodeblock that handles UFS1 dinodes. 10612 * Note that any bug fixes made to this routine must be done in the 10613 * version found below. 10614 * 10615 * Called from within the procedure above to deal with unsatisfied 10616 * allocation dependencies in an inodeblock. The buffer must be 10617 * locked, thus, no I/O completion operations can occur while we 10618 * are manipulating its associated dependencies. 10619 */ 10620 static void 10621 initiate_write_inodeblock_ufs1(inodedep, bp) 10622 struct inodedep *inodedep; 10623 struct buf *bp; /* The inode block */ 10624 { 10625 struct allocdirect *adp, *lastadp; 10626 struct ufs1_dinode *dp; 10627 struct ufs1_dinode *sip; 10628 struct inoref *inoref; 10629 struct ufsmount *ump; 10630 struct fs *fs; 10631 ufs_lbn_t i; 10632 #ifdef INVARIANTS 10633 ufs_lbn_t prevlbn = 0; 10634 #endif 10635 int deplist; 10636 10637 if (inodedep->id_state & IOSTARTED) 10638 panic("initiate_write_inodeblock_ufs1: already started"); 10639 inodedep->id_state |= IOSTARTED; 10640 fs = inodedep->id_fs; 10641 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10642 LOCK_OWNED(ump); 10643 dp = (struct ufs1_dinode *)bp->b_data + 10644 ino_to_fsbo(fs, inodedep->id_ino); 10645 10646 /* 10647 * If we're on the unlinked list but have not yet written our 10648 * next pointer initialize it here. 10649 */ 10650 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10651 struct inodedep *inon; 10652 10653 inon = TAILQ_NEXT(inodedep, id_unlinked); 10654 dp->di_freelink = inon ? inon->id_ino : 0; 10655 } 10656 /* 10657 * If the bitmap is not yet written, then the allocated 10658 * inode cannot be written to disk. 10659 */ 10660 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10661 if (inodedep->id_savedino1 != NULL) 10662 panic("initiate_write_inodeblock_ufs1: I/O underway"); 10663 FREE_LOCK(ump); 10664 sip = malloc(sizeof(struct ufs1_dinode), 10665 M_SAVEDINO, M_SOFTDEP_FLAGS); 10666 ACQUIRE_LOCK(ump); 10667 inodedep->id_savedino1 = sip; 10668 *inodedep->id_savedino1 = *dp; 10669 bzero((caddr_t)dp, sizeof(struct ufs1_dinode)); 10670 dp->di_gen = inodedep->id_savedino1->di_gen; 10671 dp->di_freelink = inodedep->id_savedino1->di_freelink; 10672 return; 10673 } 10674 /* 10675 * If no dependencies, then there is nothing to roll back. 10676 */ 10677 inodedep->id_savedsize = dp->di_size; 10678 inodedep->id_savedextsize = 0; 10679 inodedep->id_savednlink = dp->di_nlink; 10680 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10681 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10682 return; 10683 /* 10684 * Revert the link count to that of the first unwritten journal entry. 10685 */ 10686 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10687 if (inoref) 10688 dp->di_nlink = inoref->if_nlink; 10689 /* 10690 * Set the dependencies to busy. 10691 */ 10692 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10693 adp = TAILQ_NEXT(adp, ad_next)) { 10694 #ifdef INVARIANTS 10695 if (deplist != 0 && prevlbn >= adp->ad_offset) 10696 panic("softdep_write_inodeblock: lbn order"); 10697 prevlbn = adp->ad_offset; 10698 if (adp->ad_offset < UFS_NDADDR && 10699 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10700 panic("initiate_write_inodeblock_ufs1: " 10701 "direct pointer #%jd mismatch %d != %jd", 10702 (intmax_t)adp->ad_offset, 10703 dp->di_db[adp->ad_offset], 10704 (intmax_t)adp->ad_newblkno); 10705 if (adp->ad_offset >= UFS_NDADDR && 10706 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno) 10707 panic("initiate_write_inodeblock_ufs1: " 10708 "indirect pointer #%jd mismatch %d != %jd", 10709 (intmax_t)adp->ad_offset - UFS_NDADDR, 10710 dp->di_ib[adp->ad_offset - UFS_NDADDR], 10711 (intmax_t)adp->ad_newblkno); 10712 deplist |= 1 << adp->ad_offset; 10713 if ((adp->ad_state & ATTACHED) == 0) 10714 panic("initiate_write_inodeblock_ufs1: " 10715 "Unknown state 0x%x", adp->ad_state); 10716 #endif /* INVARIANTS */ 10717 adp->ad_state &= ~ATTACHED; 10718 adp->ad_state |= UNDONE; 10719 } 10720 /* 10721 * The on-disk inode cannot claim to be any larger than the last 10722 * fragment that has been written. Otherwise, the on-disk inode 10723 * might have fragments that were not the last block in the file 10724 * which would corrupt the filesystem. 10725 */ 10726 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10727 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10728 if (adp->ad_offset >= UFS_NDADDR) 10729 break; 10730 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10731 /* keep going until hitting a rollback to a frag */ 10732 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10733 continue; 10734 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10735 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) { 10736 #ifdef INVARIANTS 10737 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10738 panic("initiate_write_inodeblock_ufs1: " 10739 "lost dep1"); 10740 #endif /* INVARIANTS */ 10741 dp->di_db[i] = 0; 10742 } 10743 for (i = 0; i < UFS_NIADDR; i++) { 10744 #ifdef INVARIANTS 10745 if (dp->di_ib[i] != 0 && 10746 (deplist & ((1 << UFS_NDADDR) << i)) == 0) 10747 panic("initiate_write_inodeblock_ufs1: " 10748 "lost dep2"); 10749 #endif /* INVARIANTS */ 10750 dp->di_ib[i] = 0; 10751 } 10752 return; 10753 } 10754 /* 10755 * If we have zero'ed out the last allocated block of the file, 10756 * roll back the size to the last currently allocated block. 10757 * We know that this last allocated block is a full-sized as 10758 * we already checked for fragments in the loop above. 10759 */ 10760 if (lastadp != NULL && 10761 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10762 for (i = lastadp->ad_offset; i >= 0; i--) 10763 if (dp->di_db[i] != 0) 10764 break; 10765 dp->di_size = (i + 1) * fs->fs_bsize; 10766 } 10767 /* 10768 * The only dependencies are for indirect blocks. 10769 * 10770 * The file size for indirect block additions is not guaranteed. 10771 * Such a guarantee would be non-trivial to achieve. The conventional 10772 * synchronous write implementation also does not make this guarantee. 10773 * Fsck should catch and fix discrepancies. Arguably, the file size 10774 * can be over-estimated without destroying integrity when the file 10775 * moves into the indirect blocks (i.e., is large). If we want to 10776 * postpone fsck, we are stuck with this argument. 10777 */ 10778 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10779 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0; 10780 } 10781 10782 /* 10783 * Version of initiate_write_inodeblock that handles UFS2 dinodes. 10784 * Note that any bug fixes made to this routine must be done in the 10785 * version found above. 10786 * 10787 * Called from within the procedure above to deal with unsatisfied 10788 * allocation dependencies in an inodeblock. The buffer must be 10789 * locked, thus, no I/O completion operations can occur while we 10790 * are manipulating its associated dependencies. 10791 */ 10792 static void 10793 initiate_write_inodeblock_ufs2(inodedep, bp) 10794 struct inodedep *inodedep; 10795 struct buf *bp; /* The inode block */ 10796 { 10797 struct allocdirect *adp, *lastadp; 10798 struct ufs2_dinode *dp; 10799 struct ufs2_dinode *sip; 10800 struct inoref *inoref; 10801 struct ufsmount *ump; 10802 struct fs *fs; 10803 ufs_lbn_t i; 10804 #ifdef INVARIANTS 10805 ufs_lbn_t prevlbn = 0; 10806 #endif 10807 int deplist; 10808 10809 if (inodedep->id_state & IOSTARTED) 10810 panic("initiate_write_inodeblock_ufs2: already started"); 10811 inodedep->id_state |= IOSTARTED; 10812 fs = inodedep->id_fs; 10813 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10814 LOCK_OWNED(ump); 10815 dp = (struct ufs2_dinode *)bp->b_data + 10816 ino_to_fsbo(fs, inodedep->id_ino); 10817 10818 /* 10819 * If we're on the unlinked list but have not yet written our 10820 * next pointer initialize it here. 10821 */ 10822 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10823 struct inodedep *inon; 10824 10825 inon = TAILQ_NEXT(inodedep, id_unlinked); 10826 dp->di_freelink = inon ? inon->id_ino : 0; 10827 ffs_update_dinode_ckhash(fs, dp); 10828 } 10829 /* 10830 * If the bitmap is not yet written, then the allocated 10831 * inode cannot be written to disk. 10832 */ 10833 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10834 if (inodedep->id_savedino2 != NULL) 10835 panic("initiate_write_inodeblock_ufs2: I/O underway"); 10836 FREE_LOCK(ump); 10837 sip = malloc(sizeof(struct ufs2_dinode), 10838 M_SAVEDINO, M_SOFTDEP_FLAGS); 10839 ACQUIRE_LOCK(ump); 10840 inodedep->id_savedino2 = sip; 10841 *inodedep->id_savedino2 = *dp; 10842 bzero((caddr_t)dp, sizeof(struct ufs2_dinode)); 10843 dp->di_gen = inodedep->id_savedino2->di_gen; 10844 dp->di_freelink = inodedep->id_savedino2->di_freelink; 10845 return; 10846 } 10847 /* 10848 * If no dependencies, then there is nothing to roll back. 10849 */ 10850 inodedep->id_savedsize = dp->di_size; 10851 inodedep->id_savedextsize = dp->di_extsize; 10852 inodedep->id_savednlink = dp->di_nlink; 10853 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10854 TAILQ_EMPTY(&inodedep->id_extupdt) && 10855 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10856 return; 10857 /* 10858 * Revert the link count to that of the first unwritten journal entry. 10859 */ 10860 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10861 if (inoref) 10862 dp->di_nlink = inoref->if_nlink; 10863 10864 /* 10865 * Set the ext data dependencies to busy. 10866 */ 10867 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10868 adp = TAILQ_NEXT(adp, ad_next)) { 10869 #ifdef INVARIANTS 10870 if (deplist != 0 && prevlbn >= adp->ad_offset) 10871 panic("initiate_write_inodeblock_ufs2: lbn order"); 10872 prevlbn = adp->ad_offset; 10873 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno) 10874 panic("initiate_write_inodeblock_ufs2: " 10875 "ext pointer #%jd mismatch %jd != %jd", 10876 (intmax_t)adp->ad_offset, 10877 (intmax_t)dp->di_extb[adp->ad_offset], 10878 (intmax_t)adp->ad_newblkno); 10879 deplist |= 1 << adp->ad_offset; 10880 if ((adp->ad_state & ATTACHED) == 0) 10881 panic("initiate_write_inodeblock_ufs2: Unknown " 10882 "state 0x%x", adp->ad_state); 10883 #endif /* INVARIANTS */ 10884 adp->ad_state &= ~ATTACHED; 10885 adp->ad_state |= UNDONE; 10886 } 10887 /* 10888 * The on-disk inode cannot claim to be any larger than the last 10889 * fragment that has been written. Otherwise, the on-disk inode 10890 * might have fragments that were not the last block in the ext 10891 * data which would corrupt the filesystem. 10892 */ 10893 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10894 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10895 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno; 10896 /* keep going until hitting a rollback to a frag */ 10897 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10898 continue; 10899 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10900 for (i = adp->ad_offset + 1; i < UFS_NXADDR; i++) { 10901 #ifdef INVARIANTS 10902 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0) 10903 panic("initiate_write_inodeblock_ufs2: " 10904 "lost dep1"); 10905 #endif /* INVARIANTS */ 10906 dp->di_extb[i] = 0; 10907 } 10908 lastadp = NULL; 10909 break; 10910 } 10911 /* 10912 * If we have zero'ed out the last allocated block of the ext 10913 * data, roll back the size to the last currently allocated block. 10914 * We know that this last allocated block is a full-sized as 10915 * we already checked for fragments in the loop above. 10916 */ 10917 if (lastadp != NULL && 10918 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10919 for (i = lastadp->ad_offset; i >= 0; i--) 10920 if (dp->di_extb[i] != 0) 10921 break; 10922 dp->di_extsize = (i + 1) * fs->fs_bsize; 10923 } 10924 /* 10925 * Set the file data dependencies to busy. 10926 */ 10927 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10928 adp = TAILQ_NEXT(adp, ad_next)) { 10929 #ifdef INVARIANTS 10930 if (deplist != 0 && prevlbn >= adp->ad_offset) 10931 panic("softdep_write_inodeblock: lbn order"); 10932 if ((adp->ad_state & ATTACHED) == 0) 10933 panic("inodedep %p and adp %p not attached", inodedep, adp); 10934 prevlbn = adp->ad_offset; 10935 if (!ffs_fsfail_cleanup(ump, 0) && 10936 adp->ad_offset < UFS_NDADDR && 10937 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10938 panic("initiate_write_inodeblock_ufs2: " 10939 "direct pointer #%jd mismatch %jd != %jd", 10940 (intmax_t)adp->ad_offset, 10941 (intmax_t)dp->di_db[adp->ad_offset], 10942 (intmax_t)adp->ad_newblkno); 10943 if (!ffs_fsfail_cleanup(ump, 0) && 10944 adp->ad_offset >= UFS_NDADDR && 10945 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno) 10946 panic("initiate_write_inodeblock_ufs2: " 10947 "indirect pointer #%jd mismatch %jd != %jd", 10948 (intmax_t)adp->ad_offset - UFS_NDADDR, 10949 (intmax_t)dp->di_ib[adp->ad_offset - UFS_NDADDR], 10950 (intmax_t)adp->ad_newblkno); 10951 deplist |= 1 << adp->ad_offset; 10952 if ((adp->ad_state & ATTACHED) == 0) 10953 panic("initiate_write_inodeblock_ufs2: Unknown " 10954 "state 0x%x", adp->ad_state); 10955 #endif /* INVARIANTS */ 10956 adp->ad_state &= ~ATTACHED; 10957 adp->ad_state |= UNDONE; 10958 } 10959 /* 10960 * The on-disk inode cannot claim to be any larger than the last 10961 * fragment that has been written. Otherwise, the on-disk inode 10962 * might have fragments that were not the last block in the file 10963 * which would corrupt the filesystem. 10964 */ 10965 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10966 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10967 if (adp->ad_offset >= UFS_NDADDR) 10968 break; 10969 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10970 /* keep going until hitting a rollback to a frag */ 10971 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10972 continue; 10973 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10974 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) { 10975 #ifdef INVARIANTS 10976 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10977 panic("initiate_write_inodeblock_ufs2: " 10978 "lost dep2"); 10979 #endif /* INVARIANTS */ 10980 dp->di_db[i] = 0; 10981 } 10982 for (i = 0; i < UFS_NIADDR; i++) { 10983 #ifdef INVARIANTS 10984 if (dp->di_ib[i] != 0 && 10985 (deplist & ((1 << UFS_NDADDR) << i)) == 0) 10986 panic("initiate_write_inodeblock_ufs2: " 10987 "lost dep3"); 10988 #endif /* INVARIANTS */ 10989 dp->di_ib[i] = 0; 10990 } 10991 ffs_update_dinode_ckhash(fs, dp); 10992 return; 10993 } 10994 /* 10995 * If we have zero'ed out the last allocated block of the file, 10996 * roll back the size to the last currently allocated block. 10997 * We know that this last allocated block is a full-sized as 10998 * we already checked for fragments in the loop above. 10999 */ 11000 if (lastadp != NULL && 11001 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 11002 for (i = lastadp->ad_offset; i >= 0; i--) 11003 if (dp->di_db[i] != 0) 11004 break; 11005 dp->di_size = (i + 1) * fs->fs_bsize; 11006 } 11007 /* 11008 * The only dependencies are for indirect blocks. 11009 * 11010 * The file size for indirect block additions is not guaranteed. 11011 * Such a guarantee would be non-trivial to achieve. The conventional 11012 * synchronous write implementation also does not make this guarantee. 11013 * Fsck should catch and fix discrepancies. Arguably, the file size 11014 * can be over-estimated without destroying integrity when the file 11015 * moves into the indirect blocks (i.e., is large). If we want to 11016 * postpone fsck, we are stuck with this argument. 11017 */ 11018 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 11019 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0; 11020 ffs_update_dinode_ckhash(fs, dp); 11021 } 11022 11023 /* 11024 * Cancel an indirdep as a result of truncation. Release all of the 11025 * children allocindirs and place their journal work on the appropriate 11026 * list. 11027 */ 11028 static void 11029 cancel_indirdep(indirdep, bp, freeblks) 11030 struct indirdep *indirdep; 11031 struct buf *bp; 11032 struct freeblks *freeblks; 11033 { 11034 struct allocindir *aip; 11035 11036 /* 11037 * None of the indirect pointers will ever be visible, 11038 * so they can simply be tossed. GOINGAWAY ensures 11039 * that allocated pointers will be saved in the buffer 11040 * cache until they are freed. Note that they will 11041 * only be able to be found by their physical address 11042 * since the inode mapping the logical address will 11043 * be gone. The save buffer used for the safe copy 11044 * was allocated in setup_allocindir_phase2 using 11045 * the physical address so it could be used for this 11046 * purpose. Hence we swap the safe copy with the real 11047 * copy, allowing the safe copy to be freed and holding 11048 * on to the real copy for later use in indir_trunc. 11049 */ 11050 if (indirdep->ir_state & GOINGAWAY) 11051 panic("cancel_indirdep: already gone"); 11052 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 11053 indirdep->ir_state |= DEPCOMPLETE; 11054 LIST_REMOVE(indirdep, ir_next); 11055 } 11056 indirdep->ir_state |= GOINGAWAY; 11057 /* 11058 * Pass in bp for blocks still have journal writes 11059 * pending so we can cancel them on their own. 11060 */ 11061 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != NULL) 11062 cancel_allocindir(aip, bp, freeblks, 0); 11063 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) 11064 cancel_allocindir(aip, NULL, freeblks, 0); 11065 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) 11066 cancel_allocindir(aip, NULL, freeblks, 0); 11067 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) 11068 cancel_allocindir(aip, NULL, freeblks, 0); 11069 /* 11070 * If there are pending partial truncations we need to keep the 11071 * old block copy around until they complete. This is because 11072 * the current b_data is not a perfect superset of the available 11073 * blocks. 11074 */ 11075 if (TAILQ_EMPTY(&indirdep->ir_trunc)) 11076 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount); 11077 else 11078 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 11079 WORKLIST_REMOVE(&indirdep->ir_list); 11080 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list); 11081 indirdep->ir_bp = NULL; 11082 indirdep->ir_freeblks = freeblks; 11083 } 11084 11085 /* 11086 * Free an indirdep once it no longer has new pointers to track. 11087 */ 11088 static void 11089 free_indirdep(indirdep) 11090 struct indirdep *indirdep; 11091 { 11092 11093 KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc), 11094 ("free_indirdep: Indir trunc list not empty.")); 11095 KASSERT(LIST_EMPTY(&indirdep->ir_completehd), 11096 ("free_indirdep: Complete head not empty.")); 11097 KASSERT(LIST_EMPTY(&indirdep->ir_writehd), 11098 ("free_indirdep: write head not empty.")); 11099 KASSERT(LIST_EMPTY(&indirdep->ir_donehd), 11100 ("free_indirdep: done head not empty.")); 11101 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd), 11102 ("free_indirdep: deplist head not empty.")); 11103 KASSERT((indirdep->ir_state & DEPCOMPLETE), 11104 ("free_indirdep: %p still on newblk list.", indirdep)); 11105 KASSERT(indirdep->ir_saveddata == NULL, 11106 ("free_indirdep: %p still has saved data.", indirdep)); 11107 KASSERT(indirdep->ir_savebp == NULL, 11108 ("free_indirdep: %p still has savebp buffer.", indirdep)); 11109 if (indirdep->ir_state & ONWORKLIST) 11110 WORKLIST_REMOVE(&indirdep->ir_list); 11111 WORKITEM_FREE(indirdep, D_INDIRDEP); 11112 } 11113 11114 /* 11115 * Called before a write to an indirdep. This routine is responsible for 11116 * rolling back pointers to a safe state which includes only those 11117 * allocindirs which have been completed. 11118 */ 11119 static void 11120 initiate_write_indirdep(indirdep, bp) 11121 struct indirdep *indirdep; 11122 struct buf *bp; 11123 { 11124 struct ufsmount *ump; 11125 11126 indirdep->ir_state |= IOSTARTED; 11127 if (indirdep->ir_state & GOINGAWAY) 11128 panic("disk_io_initiation: indirdep gone"); 11129 /* 11130 * If there are no remaining dependencies, this will be writing 11131 * the real pointers. 11132 */ 11133 if (LIST_EMPTY(&indirdep->ir_deplisthd) && 11134 TAILQ_EMPTY(&indirdep->ir_trunc)) 11135 return; 11136 /* 11137 * Replace up-to-date version with safe version. 11138 */ 11139 if (indirdep->ir_saveddata == NULL) { 11140 ump = VFSTOUFS(indirdep->ir_list.wk_mp); 11141 LOCK_OWNED(ump); 11142 FREE_LOCK(ump); 11143 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 11144 M_SOFTDEP_FLAGS); 11145 ACQUIRE_LOCK(ump); 11146 } 11147 indirdep->ir_state &= ~ATTACHED; 11148 indirdep->ir_state |= UNDONE; 11149 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 11150 bcopy(indirdep->ir_savebp->b_data, bp->b_data, 11151 bp->b_bcount); 11152 } 11153 11154 /* 11155 * Called when an inode has been cleared in a cg bitmap. This finally 11156 * eliminates any canceled jaddrefs 11157 */ 11158 void 11159 softdep_setup_inofree(mp, bp, ino, wkhd) 11160 struct mount *mp; 11161 struct buf *bp; 11162 ino_t ino; 11163 struct workhead *wkhd; 11164 { 11165 struct worklist *wk, *wkn; 11166 struct inodedep *inodedep; 11167 struct ufsmount *ump; 11168 uint8_t *inosused; 11169 struct cg *cgp; 11170 struct fs *fs; 11171 11172 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 11173 ("softdep_setup_inofree called on non-softdep filesystem")); 11174 ump = VFSTOUFS(mp); 11175 ACQUIRE_LOCK(ump); 11176 if (!ffs_fsfail_cleanup(ump, 0)) { 11177 fs = ump->um_fs; 11178 cgp = (struct cg *)bp->b_data; 11179 inosused = cg_inosused(cgp); 11180 if (isset(inosused, ino % fs->fs_ipg)) 11181 panic("softdep_setup_inofree: inode %ju not freed.", 11182 (uintmax_t)ino); 11183 } 11184 if (inodedep_lookup(mp, ino, 0, &inodedep)) 11185 panic("softdep_setup_inofree: ino %ju has existing inodedep %p", 11186 (uintmax_t)ino, inodedep); 11187 if (wkhd) { 11188 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) { 11189 if (wk->wk_type != D_JADDREF) 11190 continue; 11191 WORKLIST_REMOVE(wk); 11192 /* 11193 * We can free immediately even if the jaddref 11194 * isn't attached in a background write as now 11195 * the bitmaps are reconciled. 11196 */ 11197 wk->wk_state |= COMPLETE | ATTACHED; 11198 free_jaddref(WK_JADDREF(wk)); 11199 } 11200 jwork_move(&bp->b_dep, wkhd); 11201 } 11202 FREE_LOCK(ump); 11203 } 11204 11205 /* 11206 * Called via ffs_blkfree() after a set of frags has been cleared from a cg 11207 * map. Any dependencies waiting for the write to clear are added to the 11208 * buf's list and any jnewblks that are being canceled are discarded 11209 * immediately. 11210 */ 11211 void 11212 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 11213 struct mount *mp; 11214 struct buf *bp; 11215 ufs2_daddr_t blkno; 11216 int frags; 11217 struct workhead *wkhd; 11218 { 11219 struct bmsafemap *bmsafemap; 11220 struct jnewblk *jnewblk; 11221 struct ufsmount *ump; 11222 struct worklist *wk; 11223 struct fs *fs; 11224 #ifdef INVARIANTS 11225 uint8_t *blksfree; 11226 struct cg *cgp; 11227 ufs2_daddr_t jstart; 11228 ufs2_daddr_t jend; 11229 ufs2_daddr_t end; 11230 long bno; 11231 int i; 11232 #endif 11233 11234 CTR3(KTR_SUJ, 11235 "softdep_setup_blkfree: blkno %jd frags %d wk head %p", 11236 blkno, frags, wkhd); 11237 11238 ump = VFSTOUFS(mp); 11239 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 11240 ("softdep_setup_blkfree called on non-softdep filesystem")); 11241 ACQUIRE_LOCK(ump); 11242 /* Lookup the bmsafemap so we track when it is dirty. */ 11243 fs = ump->um_fs; 11244 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 11245 /* 11246 * Detach any jnewblks which have been canceled. They must linger 11247 * until the bitmap is cleared again by ffs_blkfree() to prevent 11248 * an unjournaled allocation from hitting the disk. 11249 */ 11250 if (wkhd) { 11251 while ((wk = LIST_FIRST(wkhd)) != NULL) { 11252 CTR2(KTR_SUJ, 11253 "softdep_setup_blkfree: blkno %jd wk type %d", 11254 blkno, wk->wk_type); 11255 WORKLIST_REMOVE(wk); 11256 if (wk->wk_type != D_JNEWBLK) { 11257 WORKLIST_INSERT(&bmsafemap->sm_freehd, wk); 11258 continue; 11259 } 11260 jnewblk = WK_JNEWBLK(wk); 11261 KASSERT(jnewblk->jn_state & GOINGAWAY, 11262 ("softdep_setup_blkfree: jnewblk not canceled.")); 11263 #ifdef INVARIANTS 11264 /* 11265 * Assert that this block is free in the bitmap 11266 * before we discard the jnewblk. 11267 */ 11268 cgp = (struct cg *)bp->b_data; 11269 blksfree = cg_blksfree(cgp); 11270 bno = dtogd(fs, jnewblk->jn_blkno); 11271 for (i = jnewblk->jn_oldfrags; 11272 i < jnewblk->jn_frags; i++) { 11273 if (isset(blksfree, bno + i)) 11274 continue; 11275 panic("softdep_setup_blkfree: not free"); 11276 } 11277 #endif 11278 /* 11279 * Even if it's not attached we can free immediately 11280 * as the new bitmap is correct. 11281 */ 11282 wk->wk_state |= COMPLETE | ATTACHED; 11283 free_jnewblk(jnewblk); 11284 } 11285 } 11286 11287 #ifdef INVARIANTS 11288 /* 11289 * Assert that we are not freeing a block which has an outstanding 11290 * allocation dependency. 11291 */ 11292 fs = VFSTOUFS(mp)->um_fs; 11293 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 11294 end = blkno + frags; 11295 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 11296 /* 11297 * Don't match against blocks that will be freed when the 11298 * background write is done. 11299 */ 11300 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) == 11301 (COMPLETE | DEPCOMPLETE)) 11302 continue; 11303 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags; 11304 jend = jnewblk->jn_blkno + jnewblk->jn_frags; 11305 if ((blkno >= jstart && blkno < jend) || 11306 (end > jstart && end <= jend)) { 11307 printf("state 0x%X %jd - %d %d dep %p\n", 11308 jnewblk->jn_state, jnewblk->jn_blkno, 11309 jnewblk->jn_oldfrags, jnewblk->jn_frags, 11310 jnewblk->jn_dep); 11311 panic("softdep_setup_blkfree: " 11312 "%jd-%jd(%d) overlaps with %jd-%jd", 11313 blkno, end, frags, jstart, jend); 11314 } 11315 } 11316 #endif 11317 FREE_LOCK(ump); 11318 } 11319 11320 /* 11321 * Revert a block allocation when the journal record that describes it 11322 * is not yet written. 11323 */ 11324 static int 11325 jnewblk_rollback(jnewblk, fs, cgp, blksfree) 11326 struct jnewblk *jnewblk; 11327 struct fs *fs; 11328 struct cg *cgp; 11329 uint8_t *blksfree; 11330 { 11331 ufs1_daddr_t fragno; 11332 long cgbno, bbase; 11333 int frags, blk; 11334 int i; 11335 11336 frags = 0; 11337 cgbno = dtogd(fs, jnewblk->jn_blkno); 11338 /* 11339 * We have to test which frags need to be rolled back. We may 11340 * be operating on a stale copy when doing background writes. 11341 */ 11342 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) 11343 if (isclr(blksfree, cgbno + i)) 11344 frags++; 11345 if (frags == 0) 11346 return (0); 11347 /* 11348 * This is mostly ffs_blkfree() sans some validation and 11349 * superblock updates. 11350 */ 11351 if (frags == fs->fs_frag) { 11352 fragno = fragstoblks(fs, cgbno); 11353 ffs_setblock(fs, blksfree, fragno); 11354 ffs_clusteracct(fs, cgp, fragno, 1); 11355 cgp->cg_cs.cs_nbfree++; 11356 } else { 11357 cgbno += jnewblk->jn_oldfrags; 11358 bbase = cgbno - fragnum(fs, cgbno); 11359 /* Decrement the old frags. */ 11360 blk = blkmap(fs, blksfree, bbase); 11361 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 11362 /* Deallocate the fragment */ 11363 for (i = 0; i < frags; i++) 11364 setbit(blksfree, cgbno + i); 11365 cgp->cg_cs.cs_nffree += frags; 11366 /* Add back in counts associated with the new frags */ 11367 blk = blkmap(fs, blksfree, bbase); 11368 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 11369 /* If a complete block has been reassembled, account for it. */ 11370 fragno = fragstoblks(fs, bbase); 11371 if (ffs_isblock(fs, blksfree, fragno)) { 11372 cgp->cg_cs.cs_nffree -= fs->fs_frag; 11373 ffs_clusteracct(fs, cgp, fragno, 1); 11374 cgp->cg_cs.cs_nbfree++; 11375 } 11376 } 11377 stat_jnewblk++; 11378 jnewblk->jn_state &= ~ATTACHED; 11379 jnewblk->jn_state |= UNDONE; 11380 11381 return (frags); 11382 } 11383 11384 static void 11385 initiate_write_bmsafemap(bmsafemap, bp) 11386 struct bmsafemap *bmsafemap; 11387 struct buf *bp; /* The cg block. */ 11388 { 11389 struct jaddref *jaddref; 11390 struct jnewblk *jnewblk; 11391 uint8_t *inosused; 11392 uint8_t *blksfree; 11393 struct cg *cgp; 11394 struct fs *fs; 11395 ino_t ino; 11396 11397 /* 11398 * If this is a background write, we did this at the time that 11399 * the copy was made, so do not need to do it again. 11400 */ 11401 if (bmsafemap->sm_state & IOSTARTED) 11402 return; 11403 bmsafemap->sm_state |= IOSTARTED; 11404 /* 11405 * Clear any inode allocations which are pending journal writes. 11406 */ 11407 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) { 11408 cgp = (struct cg *)bp->b_data; 11409 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11410 inosused = cg_inosused(cgp); 11411 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) { 11412 ino = jaddref->ja_ino % fs->fs_ipg; 11413 if (isset(inosused, ino)) { 11414 if ((jaddref->ja_mode & IFMT) == IFDIR) 11415 cgp->cg_cs.cs_ndir--; 11416 cgp->cg_cs.cs_nifree++; 11417 clrbit(inosused, ino); 11418 jaddref->ja_state &= ~ATTACHED; 11419 jaddref->ja_state |= UNDONE; 11420 stat_jaddref++; 11421 } else 11422 panic("initiate_write_bmsafemap: inode %ju " 11423 "marked free", (uintmax_t)jaddref->ja_ino); 11424 } 11425 } 11426 /* 11427 * Clear any block allocations which are pending journal writes. 11428 */ 11429 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 11430 cgp = (struct cg *)bp->b_data; 11431 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11432 blksfree = cg_blksfree(cgp); 11433 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 11434 if (jnewblk_rollback(jnewblk, fs, cgp, blksfree)) 11435 continue; 11436 panic("initiate_write_bmsafemap: block %jd " 11437 "marked free", jnewblk->jn_blkno); 11438 } 11439 } 11440 /* 11441 * Move allocation lists to the written lists so they can be 11442 * cleared once the block write is complete. 11443 */ 11444 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr, 11445 inodedep, id_deps); 11446 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 11447 newblk, nb_deps); 11448 LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist, 11449 wk_list); 11450 } 11451 11452 void 11453 softdep_handle_error(struct buf *bp) 11454 { 11455 struct ufsmount *ump; 11456 11457 ump = softdep_bp_to_mp(bp); 11458 if (ump == NULL) 11459 return; 11460 11461 if (ffs_fsfail_cleanup(ump, bp->b_error)) { 11462 /* 11463 * No future writes will succeed, so the on-disk image is safe. 11464 * Pretend that this write succeeded so that the softdep state 11465 * will be cleaned up naturally. 11466 */ 11467 bp->b_ioflags &= ~BIO_ERROR; 11468 bp->b_error = 0; 11469 } 11470 } 11471 11472 /* 11473 * This routine is called during the completion interrupt 11474 * service routine for a disk write (from the procedure called 11475 * by the device driver to inform the filesystem caches of 11476 * a request completion). It should be called early in this 11477 * procedure, before the block is made available to other 11478 * processes or other routines are called. 11479 * 11480 */ 11481 static void 11482 softdep_disk_write_complete(bp) 11483 struct buf *bp; /* describes the completed disk write */ 11484 { 11485 struct worklist *wk; 11486 struct worklist *owk; 11487 struct ufsmount *ump; 11488 struct workhead reattach; 11489 struct freeblks *freeblks; 11490 struct buf *sbp; 11491 11492 ump = softdep_bp_to_mp(bp); 11493 KASSERT(LIST_EMPTY(&bp->b_dep) || ump != NULL, 11494 ("softdep_disk_write_complete: softdep_bp_to_mp returned NULL " 11495 "with outstanding dependencies for buffer %p", bp)); 11496 if (ump == NULL) 11497 return; 11498 if ((bp->b_ioflags & BIO_ERROR) != 0) 11499 softdep_handle_error(bp); 11500 /* 11501 * If an error occurred while doing the write, then the data 11502 * has not hit the disk and the dependencies cannot be processed. 11503 * But we do have to go through and roll forward any dependencies 11504 * that were rolled back before the disk write. 11505 */ 11506 sbp = NULL; 11507 ACQUIRE_LOCK(ump); 11508 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) { 11509 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 11510 switch (wk->wk_type) { 11511 case D_PAGEDEP: 11512 handle_written_filepage(WK_PAGEDEP(wk), bp, 0); 11513 continue; 11514 11515 case D_INODEDEP: 11516 handle_written_inodeblock(WK_INODEDEP(wk), 11517 bp, 0); 11518 continue; 11519 11520 case D_BMSAFEMAP: 11521 handle_written_bmsafemap(WK_BMSAFEMAP(wk), 11522 bp, 0); 11523 continue; 11524 11525 case D_INDIRDEP: 11526 handle_written_indirdep(WK_INDIRDEP(wk), 11527 bp, &sbp, 0); 11528 continue; 11529 default: 11530 /* nothing to roll forward */ 11531 continue; 11532 } 11533 } 11534 FREE_LOCK(ump); 11535 if (sbp) 11536 brelse(sbp); 11537 return; 11538 } 11539 LIST_INIT(&reattach); 11540 11541 /* 11542 * Ump SU lock must not be released anywhere in this code segment. 11543 */ 11544 owk = NULL; 11545 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) { 11546 WORKLIST_REMOVE(wk); 11547 atomic_add_long(&dep_write[wk->wk_type], 1); 11548 if (wk == owk) 11549 panic("duplicate worklist: %p\n", wk); 11550 owk = wk; 11551 switch (wk->wk_type) { 11552 case D_PAGEDEP: 11553 if (handle_written_filepage(WK_PAGEDEP(wk), bp, 11554 WRITESUCCEEDED)) 11555 WORKLIST_INSERT(&reattach, wk); 11556 continue; 11557 11558 case D_INODEDEP: 11559 if (handle_written_inodeblock(WK_INODEDEP(wk), bp, 11560 WRITESUCCEEDED)) 11561 WORKLIST_INSERT(&reattach, wk); 11562 continue; 11563 11564 case D_BMSAFEMAP: 11565 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp, 11566 WRITESUCCEEDED)) 11567 WORKLIST_INSERT(&reattach, wk); 11568 continue; 11569 11570 case D_MKDIR: 11571 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 11572 continue; 11573 11574 case D_ALLOCDIRECT: 11575 wk->wk_state |= COMPLETE; 11576 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL); 11577 continue; 11578 11579 case D_ALLOCINDIR: 11580 wk->wk_state |= COMPLETE; 11581 handle_allocindir_partdone(WK_ALLOCINDIR(wk)); 11582 continue; 11583 11584 case D_INDIRDEP: 11585 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp, 11586 WRITESUCCEEDED)) 11587 WORKLIST_INSERT(&reattach, wk); 11588 continue; 11589 11590 case D_FREEBLKS: 11591 wk->wk_state |= COMPLETE; 11592 freeblks = WK_FREEBLKS(wk); 11593 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE && 11594 LIST_EMPTY(&freeblks->fb_jblkdephd)) 11595 add_to_worklist(wk, WK_NODELAY); 11596 continue; 11597 11598 case D_FREEWORK: 11599 handle_written_freework(WK_FREEWORK(wk)); 11600 break; 11601 11602 case D_JSEGDEP: 11603 free_jsegdep(WK_JSEGDEP(wk)); 11604 continue; 11605 11606 case D_JSEG: 11607 handle_written_jseg(WK_JSEG(wk), bp); 11608 continue; 11609 11610 case D_SBDEP: 11611 if (handle_written_sbdep(WK_SBDEP(wk), bp)) 11612 WORKLIST_INSERT(&reattach, wk); 11613 continue; 11614 11615 case D_FREEDEP: 11616 free_freedep(WK_FREEDEP(wk)); 11617 continue; 11618 11619 default: 11620 panic("handle_disk_write_complete: Unknown type %s", 11621 TYPENAME(wk->wk_type)); 11622 /* NOTREACHED */ 11623 } 11624 } 11625 /* 11626 * Reattach any requests that must be redone. 11627 */ 11628 while ((wk = LIST_FIRST(&reattach)) != NULL) { 11629 WORKLIST_REMOVE(wk); 11630 WORKLIST_INSERT(&bp->b_dep, wk); 11631 } 11632 FREE_LOCK(ump); 11633 if (sbp) 11634 brelse(sbp); 11635 } 11636 11637 /* 11638 * Called from within softdep_disk_write_complete above. 11639 */ 11640 static void 11641 handle_allocdirect_partdone(adp, wkhd) 11642 struct allocdirect *adp; /* the completed allocdirect */ 11643 struct workhead *wkhd; /* Work to do when inode is writtne. */ 11644 { 11645 struct allocdirectlst *listhead; 11646 struct allocdirect *listadp; 11647 struct inodedep *inodedep; 11648 long bsize; 11649 11650 LOCK_OWNED(VFSTOUFS(adp->ad_block.nb_list.wk_mp)); 11651 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11652 return; 11653 /* 11654 * The on-disk inode cannot claim to be any larger than the last 11655 * fragment that has been written. Otherwise, the on-disk inode 11656 * might have fragments that were not the last block in the file 11657 * which would corrupt the filesystem. Thus, we cannot free any 11658 * allocdirects after one whose ad_oldblkno claims a fragment as 11659 * these blocks must be rolled back to zero before writing the inode. 11660 * We check the currently active set of allocdirects in id_inoupdt 11661 * or id_extupdt as appropriate. 11662 */ 11663 inodedep = adp->ad_inodedep; 11664 bsize = inodedep->id_fs->fs_bsize; 11665 if (adp->ad_state & EXTDATA) 11666 listhead = &inodedep->id_extupdt; 11667 else 11668 listhead = &inodedep->id_inoupdt; 11669 TAILQ_FOREACH(listadp, listhead, ad_next) { 11670 /* found our block */ 11671 if (listadp == adp) 11672 break; 11673 /* continue if ad_oldlbn is not a fragment */ 11674 if (listadp->ad_oldsize == 0 || 11675 listadp->ad_oldsize == bsize) 11676 continue; 11677 /* hit a fragment */ 11678 return; 11679 } 11680 /* 11681 * If we have reached the end of the current list without 11682 * finding the just finished dependency, then it must be 11683 * on the future dependency list. Future dependencies cannot 11684 * be freed until they are moved to the current list. 11685 */ 11686 if (listadp == NULL) { 11687 #ifdef INVARIANTS 11688 if (adp->ad_state & EXTDATA) 11689 listhead = &inodedep->id_newextupdt; 11690 else 11691 listhead = &inodedep->id_newinoupdt; 11692 TAILQ_FOREACH(listadp, listhead, ad_next) 11693 /* found our block */ 11694 if (listadp == adp) 11695 break; 11696 if (listadp == NULL) 11697 panic("handle_allocdirect_partdone: lost dep"); 11698 #endif /* INVARIANTS */ 11699 return; 11700 } 11701 /* 11702 * If we have found the just finished dependency, then queue 11703 * it along with anything that follows it that is complete. 11704 * Since the pointer has not yet been written in the inode 11705 * as the dependency prevents it, place the allocdirect on the 11706 * bufwait list where it will be freed once the pointer is 11707 * valid. 11708 */ 11709 if (wkhd == NULL) 11710 wkhd = &inodedep->id_bufwait; 11711 for (; adp; adp = listadp) { 11712 listadp = TAILQ_NEXT(adp, ad_next); 11713 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11714 return; 11715 TAILQ_REMOVE(listhead, adp, ad_next); 11716 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list); 11717 } 11718 } 11719 11720 /* 11721 * Called from within softdep_disk_write_complete above. This routine 11722 * completes successfully written allocindirs. 11723 */ 11724 static void 11725 handle_allocindir_partdone(aip) 11726 struct allocindir *aip; /* the completed allocindir */ 11727 { 11728 struct indirdep *indirdep; 11729 11730 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE) 11731 return; 11732 indirdep = aip->ai_indirdep; 11733 LIST_REMOVE(aip, ai_next); 11734 /* 11735 * Don't set a pointer while the buffer is undergoing IO or while 11736 * we have active truncations. 11737 */ 11738 if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) { 11739 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next); 11740 return; 11741 } 11742 if (indirdep->ir_state & UFS1FMT) 11743 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11744 aip->ai_newblkno; 11745 else 11746 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11747 aip->ai_newblkno; 11748 /* 11749 * Await the pointer write before freeing the allocindir. 11750 */ 11751 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next); 11752 } 11753 11754 /* 11755 * Release segments held on a jwork list. 11756 */ 11757 static void 11758 handle_jwork(wkhd) 11759 struct workhead *wkhd; 11760 { 11761 struct worklist *wk; 11762 11763 while ((wk = LIST_FIRST(wkhd)) != NULL) { 11764 WORKLIST_REMOVE(wk); 11765 switch (wk->wk_type) { 11766 case D_JSEGDEP: 11767 free_jsegdep(WK_JSEGDEP(wk)); 11768 continue; 11769 case D_FREEDEP: 11770 free_freedep(WK_FREEDEP(wk)); 11771 continue; 11772 case D_FREEFRAG: 11773 rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep)); 11774 WORKITEM_FREE(wk, D_FREEFRAG); 11775 continue; 11776 case D_FREEWORK: 11777 handle_written_freework(WK_FREEWORK(wk)); 11778 continue; 11779 default: 11780 panic("handle_jwork: Unknown type %s\n", 11781 TYPENAME(wk->wk_type)); 11782 } 11783 } 11784 } 11785 11786 /* 11787 * Handle the bufwait list on an inode when it is safe to release items 11788 * held there. This normally happens after an inode block is written but 11789 * may be delayed and handled later if there are pending journal items that 11790 * are not yet safe to be released. 11791 */ 11792 static struct freefile * 11793 handle_bufwait(inodedep, refhd) 11794 struct inodedep *inodedep; 11795 struct workhead *refhd; 11796 { 11797 struct jaddref *jaddref; 11798 struct freefile *freefile; 11799 struct worklist *wk; 11800 11801 freefile = NULL; 11802 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) { 11803 WORKLIST_REMOVE(wk); 11804 switch (wk->wk_type) { 11805 case D_FREEFILE: 11806 /* 11807 * We defer adding freefile to the worklist 11808 * until all other additions have been made to 11809 * ensure that it will be done after all the 11810 * old blocks have been freed. 11811 */ 11812 if (freefile != NULL) 11813 panic("handle_bufwait: freefile"); 11814 freefile = WK_FREEFILE(wk); 11815 continue; 11816 11817 case D_MKDIR: 11818 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT); 11819 continue; 11820 11821 case D_DIRADD: 11822 diradd_inode_written(WK_DIRADD(wk), inodedep); 11823 continue; 11824 11825 case D_FREEFRAG: 11826 wk->wk_state |= COMPLETE; 11827 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE) 11828 add_to_worklist(wk, 0); 11829 continue; 11830 11831 case D_DIRREM: 11832 wk->wk_state |= COMPLETE; 11833 add_to_worklist(wk, 0); 11834 continue; 11835 11836 case D_ALLOCDIRECT: 11837 case D_ALLOCINDIR: 11838 free_newblk(WK_NEWBLK(wk)); 11839 continue; 11840 11841 case D_JNEWBLK: 11842 wk->wk_state |= COMPLETE; 11843 free_jnewblk(WK_JNEWBLK(wk)); 11844 continue; 11845 11846 /* 11847 * Save freed journal segments and add references on 11848 * the supplied list which will delay their release 11849 * until the cg bitmap is cleared on disk. 11850 */ 11851 case D_JSEGDEP: 11852 if (refhd == NULL) 11853 free_jsegdep(WK_JSEGDEP(wk)); 11854 else 11855 WORKLIST_INSERT(refhd, wk); 11856 continue; 11857 11858 case D_JADDREF: 11859 jaddref = WK_JADDREF(wk); 11860 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 11861 if_deps); 11862 /* 11863 * Transfer any jaddrefs to the list to be freed with 11864 * the bitmap if we're handling a removed file. 11865 */ 11866 if (refhd == NULL) { 11867 wk->wk_state |= COMPLETE; 11868 free_jaddref(jaddref); 11869 } else 11870 WORKLIST_INSERT(refhd, wk); 11871 continue; 11872 11873 default: 11874 panic("handle_bufwait: Unknown type %p(%s)", 11875 wk, TYPENAME(wk->wk_type)); 11876 /* NOTREACHED */ 11877 } 11878 } 11879 return (freefile); 11880 } 11881 /* 11882 * Called from within softdep_disk_write_complete above to restore 11883 * in-memory inode block contents to their most up-to-date state. Note 11884 * that this routine is always called from interrupt level with further 11885 * interrupts from this device blocked. 11886 * 11887 * If the write did not succeed, we will do all the roll-forward 11888 * operations, but we will not take the actions that will allow its 11889 * dependencies to be processed. 11890 */ 11891 static int 11892 handle_written_inodeblock(inodedep, bp, flags) 11893 struct inodedep *inodedep; 11894 struct buf *bp; /* buffer containing the inode block */ 11895 int flags; 11896 { 11897 struct freefile *freefile; 11898 struct allocdirect *adp, *nextadp; 11899 struct ufs1_dinode *dp1 = NULL; 11900 struct ufs2_dinode *dp2 = NULL; 11901 struct workhead wkhd; 11902 int hadchanges, fstype; 11903 ino_t freelink; 11904 11905 LIST_INIT(&wkhd); 11906 hadchanges = 0; 11907 freefile = NULL; 11908 if ((inodedep->id_state & IOSTARTED) == 0) 11909 panic("handle_written_inodeblock: not started"); 11910 inodedep->id_state &= ~IOSTARTED; 11911 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) { 11912 fstype = UFS1; 11913 dp1 = (struct ufs1_dinode *)bp->b_data + 11914 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11915 freelink = dp1->di_freelink; 11916 } else { 11917 fstype = UFS2; 11918 dp2 = (struct ufs2_dinode *)bp->b_data + 11919 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11920 freelink = dp2->di_freelink; 11921 } 11922 /* 11923 * Leave this inodeblock dirty until it's in the list. 11924 */ 11925 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED && 11926 (flags & WRITESUCCEEDED)) { 11927 struct inodedep *inon; 11928 11929 inon = TAILQ_NEXT(inodedep, id_unlinked); 11930 if ((inon == NULL && freelink == 0) || 11931 (inon && inon->id_ino == freelink)) { 11932 if (inon) 11933 inon->id_state |= UNLINKPREV; 11934 inodedep->id_state |= UNLINKNEXT; 11935 } 11936 hadchanges = 1; 11937 } 11938 /* 11939 * If we had to rollback the inode allocation because of 11940 * bitmaps being incomplete, then simply restore it. 11941 * Keep the block dirty so that it will not be reclaimed until 11942 * all associated dependencies have been cleared and the 11943 * corresponding updates written to disk. 11944 */ 11945 if (inodedep->id_savedino1 != NULL) { 11946 hadchanges = 1; 11947 if (fstype == UFS1) 11948 *dp1 = *inodedep->id_savedino1; 11949 else 11950 *dp2 = *inodedep->id_savedino2; 11951 free(inodedep->id_savedino1, M_SAVEDINO); 11952 inodedep->id_savedino1 = NULL; 11953 if ((bp->b_flags & B_DELWRI) == 0) 11954 stat_inode_bitmap++; 11955 bdirty(bp); 11956 /* 11957 * If the inode is clear here and GOINGAWAY it will never 11958 * be written. Process the bufwait and clear any pending 11959 * work which may include the freefile. 11960 */ 11961 if (inodedep->id_state & GOINGAWAY) 11962 goto bufwait; 11963 return (1); 11964 } 11965 if (flags & WRITESUCCEEDED) 11966 inodedep->id_state |= COMPLETE; 11967 /* 11968 * Roll forward anything that had to be rolled back before 11969 * the inode could be updated. 11970 */ 11971 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) { 11972 nextadp = TAILQ_NEXT(adp, ad_next); 11973 if (adp->ad_state & ATTACHED) 11974 panic("handle_written_inodeblock: new entry"); 11975 if (fstype == UFS1) { 11976 if (adp->ad_offset < UFS_NDADDR) { 11977 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11978 panic("%s %s #%jd mismatch %d != %jd", 11979 "handle_written_inodeblock:", 11980 "direct pointer", 11981 (intmax_t)adp->ad_offset, 11982 dp1->di_db[adp->ad_offset], 11983 (intmax_t)adp->ad_oldblkno); 11984 dp1->di_db[adp->ad_offset] = adp->ad_newblkno; 11985 } else { 11986 if (dp1->di_ib[adp->ad_offset - UFS_NDADDR] != 11987 0) 11988 panic("%s: %s #%jd allocated as %d", 11989 "handle_written_inodeblock", 11990 "indirect pointer", 11991 (intmax_t)adp->ad_offset - 11992 UFS_NDADDR, 11993 dp1->di_ib[adp->ad_offset - 11994 UFS_NDADDR]); 11995 dp1->di_ib[adp->ad_offset - UFS_NDADDR] = 11996 adp->ad_newblkno; 11997 } 11998 } else { 11999 if (adp->ad_offset < UFS_NDADDR) { 12000 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno) 12001 panic("%s: %s #%jd %s %jd != %jd", 12002 "handle_written_inodeblock", 12003 "direct pointer", 12004 (intmax_t)adp->ad_offset, "mismatch", 12005 (intmax_t)dp2->di_db[adp->ad_offset], 12006 (intmax_t)adp->ad_oldblkno); 12007 dp2->di_db[adp->ad_offset] = adp->ad_newblkno; 12008 } else { 12009 if (dp2->di_ib[adp->ad_offset - UFS_NDADDR] != 12010 0) 12011 panic("%s: %s #%jd allocated as %jd", 12012 "handle_written_inodeblock", 12013 "indirect pointer", 12014 (intmax_t)adp->ad_offset - 12015 UFS_NDADDR, 12016 (intmax_t) 12017 dp2->di_ib[adp->ad_offset - 12018 UFS_NDADDR]); 12019 dp2->di_ib[adp->ad_offset - UFS_NDADDR] = 12020 adp->ad_newblkno; 12021 } 12022 } 12023 adp->ad_state &= ~UNDONE; 12024 adp->ad_state |= ATTACHED; 12025 hadchanges = 1; 12026 } 12027 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) { 12028 nextadp = TAILQ_NEXT(adp, ad_next); 12029 if (adp->ad_state & ATTACHED) 12030 panic("handle_written_inodeblock: new entry"); 12031 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno) 12032 panic("%s: direct pointers #%jd %s %jd != %jd", 12033 "handle_written_inodeblock", 12034 (intmax_t)adp->ad_offset, "mismatch", 12035 (intmax_t)dp2->di_extb[adp->ad_offset], 12036 (intmax_t)adp->ad_oldblkno); 12037 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno; 12038 adp->ad_state &= ~UNDONE; 12039 adp->ad_state |= ATTACHED; 12040 hadchanges = 1; 12041 } 12042 if (hadchanges && (bp->b_flags & B_DELWRI) == 0) 12043 stat_direct_blk_ptrs++; 12044 /* 12045 * Reset the file size to its most up-to-date value. 12046 */ 12047 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1) 12048 panic("handle_written_inodeblock: bad size"); 12049 if (inodedep->id_savednlink > UFS_LINK_MAX) 12050 panic("handle_written_inodeblock: Invalid link count " 12051 "%jd for inodedep %p", (uintmax_t)inodedep->id_savednlink, 12052 inodedep); 12053 if (fstype == UFS1) { 12054 if (dp1->di_nlink != inodedep->id_savednlink) { 12055 dp1->di_nlink = inodedep->id_savednlink; 12056 hadchanges = 1; 12057 } 12058 if (dp1->di_size != inodedep->id_savedsize) { 12059 dp1->di_size = inodedep->id_savedsize; 12060 hadchanges = 1; 12061 } 12062 } else { 12063 if (dp2->di_nlink != inodedep->id_savednlink) { 12064 dp2->di_nlink = inodedep->id_savednlink; 12065 hadchanges = 1; 12066 } 12067 if (dp2->di_size != inodedep->id_savedsize) { 12068 dp2->di_size = inodedep->id_savedsize; 12069 hadchanges = 1; 12070 } 12071 if (dp2->di_extsize != inodedep->id_savedextsize) { 12072 dp2->di_extsize = inodedep->id_savedextsize; 12073 hadchanges = 1; 12074 } 12075 } 12076 inodedep->id_savedsize = -1; 12077 inodedep->id_savedextsize = -1; 12078 inodedep->id_savednlink = -1; 12079 /* 12080 * If there were any rollbacks in the inode block, then it must be 12081 * marked dirty so that its will eventually get written back in 12082 * its correct form. 12083 */ 12084 if (hadchanges) { 12085 if (fstype == UFS2) 12086 ffs_update_dinode_ckhash(inodedep->id_fs, dp2); 12087 bdirty(bp); 12088 } 12089 bufwait: 12090 /* 12091 * If the write did not succeed, we have done all the roll-forward 12092 * operations, but we cannot take the actions that will allow its 12093 * dependencies to be processed. 12094 */ 12095 if ((flags & WRITESUCCEEDED) == 0) 12096 return (hadchanges); 12097 /* 12098 * Process any allocdirects that completed during the update. 12099 */ 12100 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 12101 handle_allocdirect_partdone(adp, &wkhd); 12102 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 12103 handle_allocdirect_partdone(adp, &wkhd); 12104 /* 12105 * Process deallocations that were held pending until the 12106 * inode had been written to disk. Freeing of the inode 12107 * is delayed until after all blocks have been freed to 12108 * avoid creation of new <vfsid, inum, lbn> triples 12109 * before the old ones have been deleted. Completely 12110 * unlinked inodes are not processed until the unlinked 12111 * inode list is written or the last reference is removed. 12112 */ 12113 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) { 12114 freefile = handle_bufwait(inodedep, NULL); 12115 if (freefile && !LIST_EMPTY(&wkhd)) { 12116 WORKLIST_INSERT(&wkhd, &freefile->fx_list); 12117 freefile = NULL; 12118 } 12119 } 12120 /* 12121 * Move rolled forward dependency completions to the bufwait list 12122 * now that those that were already written have been processed. 12123 */ 12124 if (!LIST_EMPTY(&wkhd) && hadchanges == 0) 12125 panic("handle_written_inodeblock: bufwait but no changes"); 12126 jwork_move(&inodedep->id_bufwait, &wkhd); 12127 12128 if (freefile != NULL) { 12129 /* 12130 * If the inode is goingaway it was never written. Fake up 12131 * the state here so free_inodedep() can succeed. 12132 */ 12133 if (inodedep->id_state & GOINGAWAY) 12134 inodedep->id_state |= COMPLETE | DEPCOMPLETE; 12135 if (free_inodedep(inodedep) == 0) 12136 panic("handle_written_inodeblock: live inodedep %p", 12137 inodedep); 12138 add_to_worklist(&freefile->fx_list, 0); 12139 return (0); 12140 } 12141 12142 /* 12143 * If no outstanding dependencies, free it. 12144 */ 12145 if (free_inodedep(inodedep) || 12146 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 && 12147 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 && 12148 TAILQ_FIRST(&inodedep->id_extupdt) == 0 && 12149 LIST_FIRST(&inodedep->id_bufwait) == 0)) 12150 return (0); 12151 return (hadchanges); 12152 } 12153 12154 /* 12155 * Perform needed roll-forwards and kick off any dependencies that 12156 * can now be processed. 12157 * 12158 * If the write did not succeed, we will do all the roll-forward 12159 * operations, but we will not take the actions that will allow its 12160 * dependencies to be processed. 12161 */ 12162 static int 12163 handle_written_indirdep(indirdep, bp, bpp, flags) 12164 struct indirdep *indirdep; 12165 struct buf *bp; 12166 struct buf **bpp; 12167 int flags; 12168 { 12169 struct allocindir *aip; 12170 struct buf *sbp; 12171 int chgs; 12172 12173 if (indirdep->ir_state & GOINGAWAY) 12174 panic("handle_written_indirdep: indirdep gone"); 12175 if ((indirdep->ir_state & IOSTARTED) == 0) 12176 panic("handle_written_indirdep: IO not started"); 12177 chgs = 0; 12178 /* 12179 * If there were rollbacks revert them here. 12180 */ 12181 if (indirdep->ir_saveddata) { 12182 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount); 12183 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 12184 free(indirdep->ir_saveddata, M_INDIRDEP); 12185 indirdep->ir_saveddata = NULL; 12186 } 12187 chgs = 1; 12188 } 12189 indirdep->ir_state &= ~(UNDONE | IOSTARTED); 12190 indirdep->ir_state |= ATTACHED; 12191 /* 12192 * If the write did not succeed, we have done all the roll-forward 12193 * operations, but we cannot take the actions that will allow its 12194 * dependencies to be processed. 12195 */ 12196 if ((flags & WRITESUCCEEDED) == 0) { 12197 stat_indir_blk_ptrs++; 12198 bdirty(bp); 12199 return (1); 12200 } 12201 /* 12202 * Move allocindirs with written pointers to the completehd if 12203 * the indirdep's pointer is not yet written. Otherwise 12204 * free them here. 12205 */ 12206 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) { 12207 LIST_REMOVE(aip, ai_next); 12208 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 12209 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip, 12210 ai_next); 12211 newblk_freefrag(&aip->ai_block); 12212 continue; 12213 } 12214 free_newblk(&aip->ai_block); 12215 } 12216 /* 12217 * Move allocindirs that have finished dependency processing from 12218 * the done list to the write list after updating the pointers. 12219 */ 12220 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 12221 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) { 12222 handle_allocindir_partdone(aip); 12223 if (aip == LIST_FIRST(&indirdep->ir_donehd)) 12224 panic("disk_write_complete: not gone"); 12225 chgs = 1; 12226 } 12227 } 12228 /* 12229 * Preserve the indirdep if there were any changes or if it is not 12230 * yet valid on disk. 12231 */ 12232 if (chgs) { 12233 stat_indir_blk_ptrs++; 12234 bdirty(bp); 12235 return (1); 12236 } 12237 /* 12238 * If there were no changes we can discard the savedbp and detach 12239 * ourselves from the buf. We are only carrying completed pointers 12240 * in this case. 12241 */ 12242 sbp = indirdep->ir_savebp; 12243 sbp->b_flags |= B_INVAL | B_NOCACHE; 12244 indirdep->ir_savebp = NULL; 12245 indirdep->ir_bp = NULL; 12246 if (*bpp != NULL) 12247 panic("handle_written_indirdep: bp already exists."); 12248 *bpp = sbp; 12249 /* 12250 * The indirdep may not be freed until its parent points at it. 12251 */ 12252 if (indirdep->ir_state & DEPCOMPLETE) 12253 free_indirdep(indirdep); 12254 12255 return (0); 12256 } 12257 12258 /* 12259 * Process a diradd entry after its dependent inode has been written. 12260 */ 12261 static void 12262 diradd_inode_written(dap, inodedep) 12263 struct diradd *dap; 12264 struct inodedep *inodedep; 12265 { 12266 12267 LOCK_OWNED(VFSTOUFS(dap->da_list.wk_mp)); 12268 dap->da_state |= COMPLETE; 12269 complete_diradd(dap); 12270 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 12271 } 12272 12273 /* 12274 * Returns true if the bmsafemap will have rollbacks when written. Must only 12275 * be called with the per-filesystem lock and the buf lock on the cg held. 12276 */ 12277 static int 12278 bmsafemap_backgroundwrite(bmsafemap, bp) 12279 struct bmsafemap *bmsafemap; 12280 struct buf *bp; 12281 { 12282 int dirty; 12283 12284 LOCK_OWNED(VFSTOUFS(bmsafemap->sm_list.wk_mp)); 12285 dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) | 12286 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd); 12287 /* 12288 * If we're initiating a background write we need to process the 12289 * rollbacks as they exist now, not as they exist when IO starts. 12290 * No other consumers will look at the contents of the shadowed 12291 * buf so this is safe to do here. 12292 */ 12293 if (bp->b_xflags & BX_BKGRDMARKER) 12294 initiate_write_bmsafemap(bmsafemap, bp); 12295 12296 return (dirty); 12297 } 12298 12299 /* 12300 * Re-apply an allocation when a cg write is complete. 12301 */ 12302 static int 12303 jnewblk_rollforward(jnewblk, fs, cgp, blksfree) 12304 struct jnewblk *jnewblk; 12305 struct fs *fs; 12306 struct cg *cgp; 12307 uint8_t *blksfree; 12308 { 12309 ufs1_daddr_t fragno; 12310 ufs2_daddr_t blkno; 12311 long cgbno, bbase; 12312 int frags, blk; 12313 int i; 12314 12315 frags = 0; 12316 cgbno = dtogd(fs, jnewblk->jn_blkno); 12317 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) { 12318 if (isclr(blksfree, cgbno + i)) 12319 panic("jnewblk_rollforward: re-allocated fragment"); 12320 frags++; 12321 } 12322 if (frags == fs->fs_frag) { 12323 blkno = fragstoblks(fs, cgbno); 12324 ffs_clrblock(fs, blksfree, (long)blkno); 12325 ffs_clusteracct(fs, cgp, blkno, -1); 12326 cgp->cg_cs.cs_nbfree--; 12327 } else { 12328 bbase = cgbno - fragnum(fs, cgbno); 12329 cgbno += jnewblk->jn_oldfrags; 12330 /* If a complete block had been reassembled, account for it. */ 12331 fragno = fragstoblks(fs, bbase); 12332 if (ffs_isblock(fs, blksfree, fragno)) { 12333 cgp->cg_cs.cs_nffree += fs->fs_frag; 12334 ffs_clusteracct(fs, cgp, fragno, -1); 12335 cgp->cg_cs.cs_nbfree--; 12336 } 12337 /* Decrement the old frags. */ 12338 blk = blkmap(fs, blksfree, bbase); 12339 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 12340 /* Allocate the fragment */ 12341 for (i = 0; i < frags; i++) 12342 clrbit(blksfree, cgbno + i); 12343 cgp->cg_cs.cs_nffree -= frags; 12344 /* Add back in counts associated with the new frags */ 12345 blk = blkmap(fs, blksfree, bbase); 12346 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 12347 } 12348 return (frags); 12349 } 12350 12351 /* 12352 * Complete a write to a bmsafemap structure. Roll forward any bitmap 12353 * changes if it's not a background write. Set all written dependencies 12354 * to DEPCOMPLETE and free the structure if possible. 12355 * 12356 * If the write did not succeed, we will do all the roll-forward 12357 * operations, but we will not take the actions that will allow its 12358 * dependencies to be processed. 12359 */ 12360 static int 12361 handle_written_bmsafemap(bmsafemap, bp, flags) 12362 struct bmsafemap *bmsafemap; 12363 struct buf *bp; 12364 int flags; 12365 { 12366 struct newblk *newblk; 12367 struct inodedep *inodedep; 12368 struct jaddref *jaddref, *jatmp; 12369 struct jnewblk *jnewblk, *jntmp; 12370 struct ufsmount *ump; 12371 uint8_t *inosused; 12372 uint8_t *blksfree; 12373 struct cg *cgp; 12374 struct fs *fs; 12375 ino_t ino; 12376 int foreground; 12377 int chgs; 12378 12379 if ((bmsafemap->sm_state & IOSTARTED) == 0) 12380 panic("handle_written_bmsafemap: Not started\n"); 12381 ump = VFSTOUFS(bmsafemap->sm_list.wk_mp); 12382 chgs = 0; 12383 bmsafemap->sm_state &= ~IOSTARTED; 12384 foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0; 12385 /* 12386 * If write was successful, release journal work that was waiting 12387 * on the write. Otherwise move the work back. 12388 */ 12389 if (flags & WRITESUCCEEDED) 12390 handle_jwork(&bmsafemap->sm_freewr); 12391 else 12392 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, 12393 worklist, wk_list); 12394 12395 /* 12396 * Restore unwritten inode allocation pending jaddref writes. 12397 */ 12398 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) { 12399 cgp = (struct cg *)bp->b_data; 12400 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 12401 inosused = cg_inosused(cgp); 12402 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd, 12403 ja_bmdeps, jatmp) { 12404 if ((jaddref->ja_state & UNDONE) == 0) 12405 continue; 12406 ino = jaddref->ja_ino % fs->fs_ipg; 12407 if (isset(inosused, ino)) 12408 panic("handle_written_bmsafemap: " 12409 "re-allocated inode"); 12410 /* Do the roll-forward only if it's a real copy. */ 12411 if (foreground) { 12412 if ((jaddref->ja_mode & IFMT) == IFDIR) 12413 cgp->cg_cs.cs_ndir++; 12414 cgp->cg_cs.cs_nifree--; 12415 setbit(inosused, ino); 12416 chgs = 1; 12417 } 12418 jaddref->ja_state &= ~UNDONE; 12419 jaddref->ja_state |= ATTACHED; 12420 free_jaddref(jaddref); 12421 } 12422 } 12423 /* 12424 * Restore any block allocations which are pending journal writes. 12425 */ 12426 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 12427 cgp = (struct cg *)bp->b_data; 12428 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 12429 blksfree = cg_blksfree(cgp); 12430 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps, 12431 jntmp) { 12432 if ((jnewblk->jn_state & UNDONE) == 0) 12433 continue; 12434 /* Do the roll-forward only if it's a real copy. */ 12435 if (foreground && 12436 jnewblk_rollforward(jnewblk, fs, cgp, blksfree)) 12437 chgs = 1; 12438 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK); 12439 jnewblk->jn_state |= ATTACHED; 12440 free_jnewblk(jnewblk); 12441 } 12442 } 12443 /* 12444 * If the write did not succeed, we have done all the roll-forward 12445 * operations, but we cannot take the actions that will allow its 12446 * dependencies to be processed. 12447 */ 12448 if ((flags & WRITESUCCEEDED) == 0) { 12449 LIST_CONCAT(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 12450 newblk, nb_deps); 12451 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, 12452 worklist, wk_list); 12453 if (foreground) 12454 bdirty(bp); 12455 return (1); 12456 } 12457 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) { 12458 newblk->nb_state |= DEPCOMPLETE; 12459 newblk->nb_state &= ~ONDEPLIST; 12460 newblk->nb_bmsafemap = NULL; 12461 LIST_REMOVE(newblk, nb_deps); 12462 if (newblk->nb_list.wk_type == D_ALLOCDIRECT) 12463 handle_allocdirect_partdone( 12464 WK_ALLOCDIRECT(&newblk->nb_list), NULL); 12465 else if (newblk->nb_list.wk_type == D_ALLOCINDIR) 12466 handle_allocindir_partdone( 12467 WK_ALLOCINDIR(&newblk->nb_list)); 12468 else if (newblk->nb_list.wk_type != D_NEWBLK) 12469 panic("handle_written_bmsafemap: Unexpected type: %s", 12470 TYPENAME(newblk->nb_list.wk_type)); 12471 } 12472 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) { 12473 inodedep->id_state |= DEPCOMPLETE; 12474 inodedep->id_state &= ~ONDEPLIST; 12475 LIST_REMOVE(inodedep, id_deps); 12476 inodedep->id_bmsafemap = NULL; 12477 } 12478 LIST_REMOVE(bmsafemap, sm_next); 12479 if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) && 12480 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) && 12481 LIST_EMPTY(&bmsafemap->sm_newblkhd) && 12482 LIST_EMPTY(&bmsafemap->sm_inodedephd) && 12483 LIST_EMPTY(&bmsafemap->sm_freehd)) { 12484 LIST_REMOVE(bmsafemap, sm_hash); 12485 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 12486 return (0); 12487 } 12488 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 12489 if (foreground) 12490 bdirty(bp); 12491 return (1); 12492 } 12493 12494 /* 12495 * Try to free a mkdir dependency. 12496 */ 12497 static void 12498 complete_mkdir(mkdir) 12499 struct mkdir *mkdir; 12500 { 12501 struct diradd *dap; 12502 12503 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE) 12504 return; 12505 LIST_REMOVE(mkdir, md_mkdirs); 12506 dap = mkdir->md_diradd; 12507 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 12508 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) { 12509 dap->da_state |= DEPCOMPLETE; 12510 complete_diradd(dap); 12511 } 12512 WORKITEM_FREE(mkdir, D_MKDIR); 12513 } 12514 12515 /* 12516 * Handle the completion of a mkdir dependency. 12517 */ 12518 static void 12519 handle_written_mkdir(mkdir, type) 12520 struct mkdir *mkdir; 12521 int type; 12522 { 12523 12524 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type) 12525 panic("handle_written_mkdir: bad type"); 12526 mkdir->md_state |= COMPLETE; 12527 complete_mkdir(mkdir); 12528 } 12529 12530 static int 12531 free_pagedep(pagedep) 12532 struct pagedep *pagedep; 12533 { 12534 int i; 12535 12536 if (pagedep->pd_state & NEWBLOCK) 12537 return (0); 12538 if (!LIST_EMPTY(&pagedep->pd_dirremhd)) 12539 return (0); 12540 for (i = 0; i < DAHASHSZ; i++) 12541 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i])) 12542 return (0); 12543 if (!LIST_EMPTY(&pagedep->pd_pendinghd)) 12544 return (0); 12545 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd)) 12546 return (0); 12547 if (pagedep->pd_state & ONWORKLIST) 12548 WORKLIST_REMOVE(&pagedep->pd_list); 12549 LIST_REMOVE(pagedep, pd_hash); 12550 WORKITEM_FREE(pagedep, D_PAGEDEP); 12551 12552 return (1); 12553 } 12554 12555 /* 12556 * Called from within softdep_disk_write_complete above. 12557 * A write operation was just completed. Removed inodes can 12558 * now be freed and associated block pointers may be committed. 12559 * Note that this routine is always called from interrupt level 12560 * with further interrupts from this device blocked. 12561 * 12562 * If the write did not succeed, we will do all the roll-forward 12563 * operations, but we will not take the actions that will allow its 12564 * dependencies to be processed. 12565 */ 12566 static int 12567 handle_written_filepage(pagedep, bp, flags) 12568 struct pagedep *pagedep; 12569 struct buf *bp; /* buffer containing the written page */ 12570 int flags; 12571 { 12572 struct dirrem *dirrem; 12573 struct diradd *dap, *nextdap; 12574 struct direct *ep; 12575 int i, chgs; 12576 12577 if ((pagedep->pd_state & IOSTARTED) == 0) 12578 panic("handle_written_filepage: not started"); 12579 pagedep->pd_state &= ~IOSTARTED; 12580 if ((flags & WRITESUCCEEDED) == 0) 12581 goto rollforward; 12582 /* 12583 * Process any directory removals that have been committed. 12584 */ 12585 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) { 12586 LIST_REMOVE(dirrem, dm_next); 12587 dirrem->dm_state |= COMPLETE; 12588 dirrem->dm_dirinum = pagedep->pd_ino; 12589 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 12590 ("handle_written_filepage: Journal entries not written.")); 12591 add_to_worklist(&dirrem->dm_list, 0); 12592 } 12593 /* 12594 * Free any directory additions that have been committed. 12595 * If it is a newly allocated block, we have to wait until 12596 * the on-disk directory inode claims the new block. 12597 */ 12598 if ((pagedep->pd_state & NEWBLOCK) == 0) 12599 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 12600 free_diradd(dap, NULL); 12601 rollforward: 12602 /* 12603 * Uncommitted directory entries must be restored. 12604 */ 12605 for (chgs = 0, i = 0; i < DAHASHSZ; i++) { 12606 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap; 12607 dap = nextdap) { 12608 nextdap = LIST_NEXT(dap, da_pdlist); 12609 if (dap->da_state & ATTACHED) 12610 panic("handle_written_filepage: attached"); 12611 ep = (struct direct *) 12612 ((char *)bp->b_data + dap->da_offset); 12613 ep->d_ino = dap->da_newinum; 12614 dap->da_state &= ~UNDONE; 12615 dap->da_state |= ATTACHED; 12616 chgs = 1; 12617 /* 12618 * If the inode referenced by the directory has 12619 * been written out, then the dependency can be 12620 * moved to the pending list. 12621 */ 12622 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 12623 LIST_REMOVE(dap, da_pdlist); 12624 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, 12625 da_pdlist); 12626 } 12627 } 12628 } 12629 /* 12630 * If there were any rollbacks in the directory, then it must be 12631 * marked dirty so that its will eventually get written back in 12632 * its correct form. 12633 */ 12634 if (chgs || (flags & WRITESUCCEEDED) == 0) { 12635 if ((bp->b_flags & B_DELWRI) == 0) 12636 stat_dir_entry++; 12637 bdirty(bp); 12638 return (1); 12639 } 12640 /* 12641 * If we are not waiting for a new directory block to be 12642 * claimed by its inode, then the pagedep will be freed. 12643 * Otherwise it will remain to track any new entries on 12644 * the page in case they are fsync'ed. 12645 */ 12646 free_pagedep(pagedep); 12647 return (0); 12648 } 12649 12650 /* 12651 * Writing back in-core inode structures. 12652 * 12653 * The filesystem only accesses an inode's contents when it occupies an 12654 * "in-core" inode structure. These "in-core" structures are separate from 12655 * the page frames used to cache inode blocks. Only the latter are 12656 * transferred to/from the disk. So, when the updated contents of the 12657 * "in-core" inode structure are copied to the corresponding in-memory inode 12658 * block, the dependencies are also transferred. The following procedure is 12659 * called when copying a dirty "in-core" inode to a cached inode block. 12660 */ 12661 12662 /* 12663 * Called when an inode is loaded from disk. If the effective link count 12664 * differed from the actual link count when it was last flushed, then we 12665 * need to ensure that the correct effective link count is put back. 12666 */ 12667 void 12668 softdep_load_inodeblock(ip) 12669 struct inode *ip; /* the "in_core" copy of the inode */ 12670 { 12671 struct inodedep *inodedep; 12672 struct ufsmount *ump; 12673 12674 ump = ITOUMP(ip); 12675 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 12676 ("softdep_load_inodeblock called on non-softdep filesystem")); 12677 /* 12678 * Check for alternate nlink count. 12679 */ 12680 ip->i_effnlink = ip->i_nlink; 12681 ACQUIRE_LOCK(ump); 12682 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) { 12683 FREE_LOCK(ump); 12684 return; 12685 } 12686 if (ip->i_nlink != inodedep->id_nlinkwrote && 12687 inodedep->id_nlinkwrote != -1) { 12688 KASSERT(ip->i_nlink == 0 && 12689 (ump->um_flags & UM_FSFAIL_CLEANUP) != 0, 12690 ("read bad i_nlink value")); 12691 ip->i_effnlink = ip->i_nlink = inodedep->id_nlinkwrote; 12692 } 12693 ip->i_effnlink -= inodedep->id_nlinkdelta; 12694 KASSERT(ip->i_effnlink >= 0, 12695 ("softdep_load_inodeblock: negative i_effnlink")); 12696 FREE_LOCK(ump); 12697 } 12698 12699 /* 12700 * This routine is called just before the "in-core" inode 12701 * information is to be copied to the in-memory inode block. 12702 * Recall that an inode block contains several inodes. If 12703 * the force flag is set, then the dependencies will be 12704 * cleared so that the update can always be made. Note that 12705 * the buffer is locked when this routine is called, so we 12706 * will never be in the middle of writing the inode block 12707 * to disk. 12708 */ 12709 void 12710 softdep_update_inodeblock(ip, bp, waitfor) 12711 struct inode *ip; /* the "in_core" copy of the inode */ 12712 struct buf *bp; /* the buffer containing the inode block */ 12713 int waitfor; /* nonzero => update must be allowed */ 12714 { 12715 struct inodedep *inodedep; 12716 struct inoref *inoref; 12717 struct ufsmount *ump; 12718 struct worklist *wk; 12719 struct mount *mp; 12720 struct buf *ibp; 12721 struct fs *fs; 12722 int error; 12723 12724 ump = ITOUMP(ip); 12725 mp = UFSTOVFS(ump); 12726 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 12727 ("softdep_update_inodeblock called on non-softdep filesystem")); 12728 fs = ump->um_fs; 12729 /* 12730 * Preserve the freelink that is on disk. clear_unlinked_inodedep() 12731 * does not have access to the in-core ip so must write directly into 12732 * the inode block buffer when setting freelink. 12733 */ 12734 if (fs->fs_magic == FS_UFS1_MAGIC) 12735 DIP_SET(ip, i_freelink, ((struct ufs1_dinode *)bp->b_data + 12736 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12737 else 12738 DIP_SET(ip, i_freelink, ((struct ufs2_dinode *)bp->b_data + 12739 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12740 /* 12741 * If the effective link count is not equal to the actual link 12742 * count, then we must track the difference in an inodedep while 12743 * the inode is (potentially) tossed out of the cache. Otherwise, 12744 * if there is no existing inodedep, then there are no dependencies 12745 * to track. 12746 */ 12747 ACQUIRE_LOCK(ump); 12748 again: 12749 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12750 FREE_LOCK(ump); 12751 if (ip->i_effnlink != ip->i_nlink) 12752 panic("softdep_update_inodeblock: bad link count"); 12753 return; 12754 } 12755 KASSERT(ip->i_nlink >= inodedep->id_nlinkdelta, 12756 ("softdep_update_inodeblock inconsistent ip %p i_nlink %d " 12757 "inodedep %p id_nlinkdelta %jd", 12758 ip, ip->i_nlink, inodedep, (intmax_t)inodedep->id_nlinkdelta)); 12759 inodedep->id_nlinkwrote = ip->i_nlink; 12760 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink) 12761 panic("softdep_update_inodeblock: bad delta"); 12762 /* 12763 * If we're flushing all dependencies we must also move any waiting 12764 * for journal writes onto the bufwait list prior to I/O. 12765 */ 12766 if (waitfor) { 12767 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12768 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12769 == DEPCOMPLETE) { 12770 jwait(&inoref->if_list, MNT_WAIT); 12771 goto again; 12772 } 12773 } 12774 } 12775 /* 12776 * Changes have been initiated. Anything depending on these 12777 * changes cannot occur until this inode has been written. 12778 */ 12779 inodedep->id_state &= ~COMPLETE; 12780 if ((inodedep->id_state & ONWORKLIST) == 0) 12781 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list); 12782 /* 12783 * Any new dependencies associated with the incore inode must 12784 * now be moved to the list associated with the buffer holding 12785 * the in-memory copy of the inode. Once merged process any 12786 * allocdirects that are completed by the merger. 12787 */ 12788 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt); 12789 if (!TAILQ_EMPTY(&inodedep->id_inoupdt)) 12790 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt), 12791 NULL); 12792 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt); 12793 if (!TAILQ_EMPTY(&inodedep->id_extupdt)) 12794 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt), 12795 NULL); 12796 /* 12797 * Now that the inode has been pushed into the buffer, the 12798 * operations dependent on the inode being written to disk 12799 * can be moved to the id_bufwait so that they will be 12800 * processed when the buffer I/O completes. 12801 */ 12802 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) { 12803 WORKLIST_REMOVE(wk); 12804 WORKLIST_INSERT(&inodedep->id_bufwait, wk); 12805 } 12806 /* 12807 * Newly allocated inodes cannot be written until the bitmap 12808 * that allocates them have been written (indicated by 12809 * DEPCOMPLETE being set in id_state). If we are doing a 12810 * forced sync (e.g., an fsync on a file), we force the bitmap 12811 * to be written so that the update can be done. 12812 */ 12813 if (waitfor == 0) { 12814 FREE_LOCK(ump); 12815 return; 12816 } 12817 retry: 12818 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) { 12819 FREE_LOCK(ump); 12820 return; 12821 } 12822 ibp = inodedep->id_bmsafemap->sm_buf; 12823 ibp = getdirtybuf(ibp, LOCK_PTR(ump), MNT_WAIT); 12824 if (ibp == NULL) { 12825 /* 12826 * If ibp came back as NULL, the dependency could have been 12827 * freed while we slept. Look it up again, and check to see 12828 * that it has completed. 12829 */ 12830 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 12831 goto retry; 12832 FREE_LOCK(ump); 12833 return; 12834 } 12835 FREE_LOCK(ump); 12836 if ((error = bwrite(ibp)) != 0) 12837 softdep_error("softdep_update_inodeblock: bwrite", error); 12838 } 12839 12840 /* 12841 * Merge the a new inode dependency list (such as id_newinoupdt) into an 12842 * old inode dependency list (such as id_inoupdt). 12843 */ 12844 static void 12845 merge_inode_lists(newlisthead, oldlisthead) 12846 struct allocdirectlst *newlisthead; 12847 struct allocdirectlst *oldlisthead; 12848 { 12849 struct allocdirect *listadp, *newadp; 12850 12851 newadp = TAILQ_FIRST(newlisthead); 12852 if (newadp != NULL) 12853 LOCK_OWNED(VFSTOUFS(newadp->ad_block.nb_list.wk_mp)); 12854 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) { 12855 if (listadp->ad_offset < newadp->ad_offset) { 12856 listadp = TAILQ_NEXT(listadp, ad_next); 12857 continue; 12858 } 12859 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12860 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next); 12861 if (listadp->ad_offset == newadp->ad_offset) { 12862 allocdirect_merge(oldlisthead, newadp, 12863 listadp); 12864 listadp = newadp; 12865 } 12866 newadp = TAILQ_FIRST(newlisthead); 12867 } 12868 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) { 12869 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12870 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next); 12871 } 12872 } 12873 12874 /* 12875 * If we are doing an fsync, then we must ensure that any directory 12876 * entries for the inode have been written after the inode gets to disk. 12877 */ 12878 int 12879 softdep_fsync(vp) 12880 struct vnode *vp; /* the "in_core" copy of the inode */ 12881 { 12882 struct inodedep *inodedep; 12883 struct pagedep *pagedep; 12884 struct inoref *inoref; 12885 struct ufsmount *ump; 12886 struct worklist *wk; 12887 struct diradd *dap; 12888 struct mount *mp; 12889 struct vnode *pvp; 12890 struct inode *ip; 12891 struct buf *bp; 12892 struct fs *fs; 12893 struct thread *td = curthread; 12894 int error, flushparent, pagedep_new_block; 12895 ino_t parentino; 12896 ufs_lbn_t lbn; 12897 12898 ip = VTOI(vp); 12899 mp = vp->v_mount; 12900 ump = VFSTOUFS(mp); 12901 fs = ump->um_fs; 12902 if (MOUNTEDSOFTDEP(mp) == 0) 12903 return (0); 12904 ACQUIRE_LOCK(ump); 12905 restart: 12906 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12907 FREE_LOCK(ump); 12908 return (0); 12909 } 12910 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12911 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12912 == DEPCOMPLETE) { 12913 jwait(&inoref->if_list, MNT_WAIT); 12914 goto restart; 12915 } 12916 } 12917 if (!LIST_EMPTY(&inodedep->id_inowait) || 12918 !TAILQ_EMPTY(&inodedep->id_extupdt) || 12919 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 12920 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 12921 !TAILQ_EMPTY(&inodedep->id_newinoupdt)) 12922 panic("softdep_fsync: pending ops %p", inodedep); 12923 for (error = 0, flushparent = 0; ; ) { 12924 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL) 12925 break; 12926 if (wk->wk_type != D_DIRADD) 12927 panic("softdep_fsync: Unexpected type %s", 12928 TYPENAME(wk->wk_type)); 12929 dap = WK_DIRADD(wk); 12930 /* 12931 * Flush our parent if this directory entry has a MKDIR_PARENT 12932 * dependency or is contained in a newly allocated block. 12933 */ 12934 if (dap->da_state & DIRCHG) 12935 pagedep = dap->da_previous->dm_pagedep; 12936 else 12937 pagedep = dap->da_pagedep; 12938 parentino = pagedep->pd_ino; 12939 lbn = pagedep->pd_lbn; 12940 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) 12941 panic("softdep_fsync: dirty"); 12942 if ((dap->da_state & MKDIR_PARENT) || 12943 (pagedep->pd_state & NEWBLOCK)) 12944 flushparent = 1; 12945 else 12946 flushparent = 0; 12947 /* 12948 * If we are being fsync'ed as part of vgone'ing this vnode, 12949 * then we will not be able to release and recover the 12950 * vnode below, so we just have to give up on writing its 12951 * directory entry out. It will eventually be written, just 12952 * not now, but then the user was not asking to have it 12953 * written, so we are not breaking any promises. 12954 */ 12955 if (VN_IS_DOOMED(vp)) 12956 break; 12957 /* 12958 * We prevent deadlock by always fetching inodes from the 12959 * root, moving down the directory tree. Thus, when fetching 12960 * our parent directory, we first try to get the lock. If 12961 * that fails, we must unlock ourselves before requesting 12962 * the lock on our parent. See the comment in ufs_lookup 12963 * for details on possible races. 12964 */ 12965 FREE_LOCK(ump); 12966 error = get_parent_vp(vp, mp, parentino, NULL, NULL, NULL, 12967 &pvp); 12968 if (error == ERELOOKUP) 12969 error = 0; 12970 if (error != 0) 12971 return (error); 12972 /* 12973 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps 12974 * that are contained in direct blocks will be resolved by 12975 * doing a ffs_update. Pagedeps contained in indirect blocks 12976 * may require a complete sync'ing of the directory. So, we 12977 * try the cheap and fast ffs_update first, and if that fails, 12978 * then we do the slower ffs_syncvnode of the directory. 12979 */ 12980 if (flushparent) { 12981 int locked; 12982 12983 if ((error = ffs_update(pvp, 1)) != 0) { 12984 vput(pvp); 12985 return (error); 12986 } 12987 ACQUIRE_LOCK(ump); 12988 locked = 1; 12989 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) { 12990 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) { 12991 if (wk->wk_type != D_DIRADD) 12992 panic("softdep_fsync: Unexpected type %s", 12993 TYPENAME(wk->wk_type)); 12994 dap = WK_DIRADD(wk); 12995 if (dap->da_state & DIRCHG) 12996 pagedep = dap->da_previous->dm_pagedep; 12997 else 12998 pagedep = dap->da_pagedep; 12999 pagedep_new_block = pagedep->pd_state & NEWBLOCK; 13000 FREE_LOCK(ump); 13001 locked = 0; 13002 if (pagedep_new_block && (error = 13003 ffs_syncvnode(pvp, MNT_WAIT, 0))) { 13004 vput(pvp); 13005 return (error); 13006 } 13007 } 13008 } 13009 if (locked) 13010 FREE_LOCK(ump); 13011 } 13012 /* 13013 * Flush directory page containing the inode's name. 13014 */ 13015 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred, 13016 &bp); 13017 if (error == 0) 13018 error = bwrite(bp); 13019 else 13020 brelse(bp); 13021 vput(pvp); 13022 if (!ffs_fsfail_cleanup(ump, error)) 13023 return (error); 13024 ACQUIRE_LOCK(ump); 13025 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 13026 break; 13027 } 13028 FREE_LOCK(ump); 13029 return (0); 13030 } 13031 13032 /* 13033 * Flush all the dirty bitmaps associated with the block device 13034 * before flushing the rest of the dirty blocks so as to reduce 13035 * the number of dependencies that will have to be rolled back. 13036 * 13037 * XXX Unused? 13038 */ 13039 void 13040 softdep_fsync_mountdev(vp) 13041 struct vnode *vp; 13042 { 13043 struct buf *bp, *nbp; 13044 struct worklist *wk; 13045 struct bufobj *bo; 13046 13047 if (!vn_isdisk(vp)) 13048 panic("softdep_fsync_mountdev: vnode not a disk"); 13049 bo = &vp->v_bufobj; 13050 restart: 13051 BO_LOCK(bo); 13052 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 13053 /* 13054 * If it is already scheduled, skip to the next buffer. 13055 */ 13056 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) 13057 continue; 13058 13059 if ((bp->b_flags & B_DELWRI) == 0) 13060 panic("softdep_fsync_mountdev: not dirty"); 13061 /* 13062 * We are only interested in bitmaps with outstanding 13063 * dependencies. 13064 */ 13065 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL || 13066 wk->wk_type != D_BMSAFEMAP || 13067 (bp->b_vflags & BV_BKGRDINPROG)) { 13068 BUF_UNLOCK(bp); 13069 continue; 13070 } 13071 BO_UNLOCK(bo); 13072 bremfree(bp); 13073 (void) bawrite(bp); 13074 goto restart; 13075 } 13076 drain_output(vp); 13077 BO_UNLOCK(bo); 13078 } 13079 13080 /* 13081 * Sync all cylinder groups that were dirty at the time this function is 13082 * called. Newly dirtied cgs will be inserted before the sentinel. This 13083 * is used to flush freedep activity that may be holding up writes to a 13084 * indirect block. 13085 */ 13086 static int 13087 sync_cgs(mp, waitfor) 13088 struct mount *mp; 13089 int waitfor; 13090 { 13091 struct bmsafemap *bmsafemap; 13092 struct bmsafemap *sentinel; 13093 struct ufsmount *ump; 13094 struct buf *bp; 13095 int error; 13096 13097 sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK); 13098 sentinel->sm_cg = -1; 13099 ump = VFSTOUFS(mp); 13100 error = 0; 13101 ACQUIRE_LOCK(ump); 13102 LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next); 13103 for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL; 13104 bmsafemap = LIST_NEXT(sentinel, sm_next)) { 13105 /* Skip sentinels and cgs with no work to release. */ 13106 if (bmsafemap->sm_cg == -1 || 13107 (LIST_EMPTY(&bmsafemap->sm_freehd) && 13108 LIST_EMPTY(&bmsafemap->sm_freewr))) { 13109 LIST_REMOVE(sentinel, sm_next); 13110 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 13111 continue; 13112 } 13113 /* 13114 * If we don't get the lock and we're waiting try again, if 13115 * not move on to the next buf and try to sync it. 13116 */ 13117 bp = getdirtybuf(bmsafemap->sm_buf, LOCK_PTR(ump), waitfor); 13118 if (bp == NULL && waitfor == MNT_WAIT) 13119 continue; 13120 LIST_REMOVE(sentinel, sm_next); 13121 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 13122 if (bp == NULL) 13123 continue; 13124 FREE_LOCK(ump); 13125 if (waitfor == MNT_NOWAIT) 13126 bawrite(bp); 13127 else 13128 error = bwrite(bp); 13129 ACQUIRE_LOCK(ump); 13130 if (error) 13131 break; 13132 } 13133 LIST_REMOVE(sentinel, sm_next); 13134 FREE_LOCK(ump); 13135 free(sentinel, M_BMSAFEMAP); 13136 return (error); 13137 } 13138 13139 /* 13140 * This routine is called when we are trying to synchronously flush a 13141 * file. This routine must eliminate any filesystem metadata dependencies 13142 * so that the syncing routine can succeed. 13143 */ 13144 int 13145 softdep_sync_metadata(struct vnode *vp) 13146 { 13147 struct inode *ip; 13148 int error; 13149 13150 ip = VTOI(vp); 13151 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 13152 ("softdep_sync_metadata called on non-softdep filesystem")); 13153 /* 13154 * Ensure that any direct block dependencies have been cleared, 13155 * truncations are started, and inode references are journaled. 13156 */ 13157 ACQUIRE_LOCK(VFSTOUFS(vp->v_mount)); 13158 /* 13159 * Write all journal records to prevent rollbacks on devvp. 13160 */ 13161 if (vp->v_type == VCHR) 13162 softdep_flushjournal(vp->v_mount); 13163 error = flush_inodedep_deps(vp, vp->v_mount, ip->i_number); 13164 /* 13165 * Ensure that all truncates are written so we won't find deps on 13166 * indirect blocks. 13167 */ 13168 process_truncates(vp); 13169 FREE_LOCK(VFSTOUFS(vp->v_mount)); 13170 13171 return (error); 13172 } 13173 13174 /* 13175 * This routine is called when we are attempting to sync a buf with 13176 * dependencies. If waitfor is MNT_NOWAIT it attempts to schedule any 13177 * other IO it can but returns EBUSY if the buffer is not yet able to 13178 * be written. Dependencies which will not cause rollbacks will always 13179 * return 0. 13180 */ 13181 int 13182 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 13183 { 13184 struct indirdep *indirdep; 13185 struct pagedep *pagedep; 13186 struct allocindir *aip; 13187 struct newblk *newblk; 13188 struct ufsmount *ump; 13189 struct buf *nbp; 13190 struct worklist *wk; 13191 int i, error; 13192 13193 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 13194 ("softdep_sync_buf called on non-softdep filesystem")); 13195 /* 13196 * For VCHR we just don't want to force flush any dependencies that 13197 * will cause rollbacks. 13198 */ 13199 if (vp->v_type == VCHR) { 13200 if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0)) 13201 return (EBUSY); 13202 return (0); 13203 } 13204 ump = VFSTOUFS(vp->v_mount); 13205 ACQUIRE_LOCK(ump); 13206 /* 13207 * As we hold the buffer locked, none of its dependencies 13208 * will disappear. 13209 */ 13210 error = 0; 13211 top: 13212 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 13213 switch (wk->wk_type) { 13214 case D_ALLOCDIRECT: 13215 case D_ALLOCINDIR: 13216 newblk = WK_NEWBLK(wk); 13217 if (newblk->nb_jnewblk != NULL) { 13218 if (waitfor == MNT_NOWAIT) { 13219 error = EBUSY; 13220 goto out_unlock; 13221 } 13222 jwait(&newblk->nb_jnewblk->jn_list, waitfor); 13223 goto top; 13224 } 13225 if (newblk->nb_state & DEPCOMPLETE || 13226 waitfor == MNT_NOWAIT) 13227 continue; 13228 nbp = newblk->nb_bmsafemap->sm_buf; 13229 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 13230 if (nbp == NULL) 13231 goto top; 13232 FREE_LOCK(ump); 13233 if ((error = bwrite(nbp)) != 0) 13234 goto out; 13235 ACQUIRE_LOCK(ump); 13236 continue; 13237 13238 case D_INDIRDEP: 13239 indirdep = WK_INDIRDEP(wk); 13240 if (waitfor == MNT_NOWAIT) { 13241 if (!TAILQ_EMPTY(&indirdep->ir_trunc) || 13242 !LIST_EMPTY(&indirdep->ir_deplisthd)) { 13243 error = EBUSY; 13244 goto out_unlock; 13245 } 13246 } 13247 if (!TAILQ_EMPTY(&indirdep->ir_trunc)) 13248 panic("softdep_sync_buf: truncation pending."); 13249 restart: 13250 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 13251 newblk = (struct newblk *)aip; 13252 if (newblk->nb_jnewblk != NULL) { 13253 jwait(&newblk->nb_jnewblk->jn_list, 13254 waitfor); 13255 goto restart; 13256 } 13257 if (newblk->nb_state & DEPCOMPLETE) 13258 continue; 13259 nbp = newblk->nb_bmsafemap->sm_buf; 13260 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 13261 if (nbp == NULL) 13262 goto restart; 13263 FREE_LOCK(ump); 13264 if ((error = bwrite(nbp)) != 0) 13265 goto out; 13266 ACQUIRE_LOCK(ump); 13267 goto restart; 13268 } 13269 continue; 13270 13271 case D_PAGEDEP: 13272 /* 13273 * Only flush directory entries in synchronous passes. 13274 */ 13275 if (waitfor != MNT_WAIT) { 13276 error = EBUSY; 13277 goto out_unlock; 13278 } 13279 /* 13280 * While syncing snapshots, we must allow recursive 13281 * lookups. 13282 */ 13283 BUF_AREC(bp); 13284 /* 13285 * We are trying to sync a directory that may 13286 * have dependencies on both its own metadata 13287 * and/or dependencies on the inodes of any 13288 * recently allocated files. We walk its diradd 13289 * lists pushing out the associated inode. 13290 */ 13291 pagedep = WK_PAGEDEP(wk); 13292 for (i = 0; i < DAHASHSZ; i++) { 13293 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0) 13294 continue; 13295 error = flush_pagedep_deps(vp, wk->wk_mp, 13296 &pagedep->pd_diraddhd[i], bp); 13297 if (error != 0) { 13298 if (error != ERELOOKUP) 13299 BUF_NOREC(bp); 13300 goto out_unlock; 13301 } 13302 } 13303 BUF_NOREC(bp); 13304 continue; 13305 13306 case D_FREEWORK: 13307 case D_FREEDEP: 13308 case D_JSEGDEP: 13309 case D_JNEWBLK: 13310 continue; 13311 13312 default: 13313 panic("softdep_sync_buf: Unknown type %s", 13314 TYPENAME(wk->wk_type)); 13315 /* NOTREACHED */ 13316 } 13317 } 13318 out_unlock: 13319 FREE_LOCK(ump); 13320 out: 13321 return (error); 13322 } 13323 13324 /* 13325 * Flush the dependencies associated with an inodedep. 13326 */ 13327 static int 13328 flush_inodedep_deps(vp, mp, ino) 13329 struct vnode *vp; 13330 struct mount *mp; 13331 ino_t ino; 13332 { 13333 struct inodedep *inodedep; 13334 struct inoref *inoref; 13335 struct ufsmount *ump; 13336 int error, waitfor; 13337 13338 /* 13339 * This work is done in two passes. The first pass grabs most 13340 * of the buffers and begins asynchronously writing them. The 13341 * only way to wait for these asynchronous writes is to sleep 13342 * on the filesystem vnode which may stay busy for a long time 13343 * if the filesystem is active. So, instead, we make a second 13344 * pass over the dependencies blocking on each write. In the 13345 * usual case we will be blocking against a write that we 13346 * initiated, so when it is done the dependency will have been 13347 * resolved. Thus the second pass is expected to end quickly. 13348 * We give a brief window at the top of the loop to allow 13349 * any pending I/O to complete. 13350 */ 13351 ump = VFSTOUFS(mp); 13352 LOCK_OWNED(ump); 13353 for (error = 0, waitfor = MNT_NOWAIT; ; ) { 13354 if (error) 13355 return (error); 13356 FREE_LOCK(ump); 13357 ACQUIRE_LOCK(ump); 13358 restart: 13359 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 13360 return (0); 13361 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 13362 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 13363 == DEPCOMPLETE) { 13364 jwait(&inoref->if_list, MNT_WAIT); 13365 goto restart; 13366 } 13367 } 13368 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) || 13369 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) || 13370 flush_deplist(&inodedep->id_extupdt, waitfor, &error) || 13371 flush_deplist(&inodedep->id_newextupdt, waitfor, &error)) 13372 continue; 13373 /* 13374 * If pass2, we are done, otherwise do pass 2. 13375 */ 13376 if (waitfor == MNT_WAIT) 13377 break; 13378 waitfor = MNT_WAIT; 13379 } 13380 /* 13381 * Try freeing inodedep in case all dependencies have been removed. 13382 */ 13383 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0) 13384 (void) free_inodedep(inodedep); 13385 return (0); 13386 } 13387 13388 /* 13389 * Flush an inode dependency list. 13390 */ 13391 static int 13392 flush_deplist(listhead, waitfor, errorp) 13393 struct allocdirectlst *listhead; 13394 int waitfor; 13395 int *errorp; 13396 { 13397 struct allocdirect *adp; 13398 struct newblk *newblk; 13399 struct ufsmount *ump; 13400 struct buf *bp; 13401 13402 if ((adp = TAILQ_FIRST(listhead)) == NULL) 13403 return (0); 13404 ump = VFSTOUFS(adp->ad_list.wk_mp); 13405 LOCK_OWNED(ump); 13406 TAILQ_FOREACH(adp, listhead, ad_next) { 13407 newblk = (struct newblk *)adp; 13408 if (newblk->nb_jnewblk != NULL) { 13409 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 13410 return (1); 13411 } 13412 if (newblk->nb_state & DEPCOMPLETE) 13413 continue; 13414 bp = newblk->nb_bmsafemap->sm_buf; 13415 bp = getdirtybuf(bp, LOCK_PTR(ump), waitfor); 13416 if (bp == NULL) { 13417 if (waitfor == MNT_NOWAIT) 13418 continue; 13419 return (1); 13420 } 13421 FREE_LOCK(ump); 13422 if (waitfor == MNT_NOWAIT) 13423 bawrite(bp); 13424 else 13425 *errorp = bwrite(bp); 13426 ACQUIRE_LOCK(ump); 13427 return (1); 13428 } 13429 return (0); 13430 } 13431 13432 /* 13433 * Flush dependencies associated with an allocdirect block. 13434 */ 13435 static int 13436 flush_newblk_dep(vp, mp, lbn) 13437 struct vnode *vp; 13438 struct mount *mp; 13439 ufs_lbn_t lbn; 13440 { 13441 struct newblk *newblk; 13442 struct ufsmount *ump; 13443 struct bufobj *bo; 13444 struct inode *ip; 13445 struct buf *bp; 13446 ufs2_daddr_t blkno; 13447 int error; 13448 13449 error = 0; 13450 bo = &vp->v_bufobj; 13451 ip = VTOI(vp); 13452 blkno = DIP(ip, i_db[lbn]); 13453 if (blkno == 0) 13454 panic("flush_newblk_dep: Missing block"); 13455 ump = VFSTOUFS(mp); 13456 ACQUIRE_LOCK(ump); 13457 /* 13458 * Loop until all dependencies related to this block are satisfied. 13459 * We must be careful to restart after each sleep in case a write 13460 * completes some part of this process for us. 13461 */ 13462 for (;;) { 13463 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) { 13464 FREE_LOCK(ump); 13465 break; 13466 } 13467 if (newblk->nb_list.wk_type != D_ALLOCDIRECT) 13468 panic("flush_newblk_dep: Bad newblk %p", newblk); 13469 /* 13470 * Flush the journal. 13471 */ 13472 if (newblk->nb_jnewblk != NULL) { 13473 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 13474 continue; 13475 } 13476 /* 13477 * Write the bitmap dependency. 13478 */ 13479 if ((newblk->nb_state & DEPCOMPLETE) == 0) { 13480 bp = newblk->nb_bmsafemap->sm_buf; 13481 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 13482 if (bp == NULL) 13483 continue; 13484 FREE_LOCK(ump); 13485 error = bwrite(bp); 13486 if (error) 13487 break; 13488 ACQUIRE_LOCK(ump); 13489 continue; 13490 } 13491 /* 13492 * Write the buffer. 13493 */ 13494 FREE_LOCK(ump); 13495 BO_LOCK(bo); 13496 bp = gbincore(bo, lbn); 13497 if (bp != NULL) { 13498 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 13499 LK_INTERLOCK, BO_LOCKPTR(bo)); 13500 if (error == ENOLCK) { 13501 ACQUIRE_LOCK(ump); 13502 error = 0; 13503 continue; /* Slept, retry */ 13504 } 13505 if (error != 0) 13506 break; /* Failed */ 13507 if (bp->b_flags & B_DELWRI) { 13508 bremfree(bp); 13509 error = bwrite(bp); 13510 if (error) 13511 break; 13512 } else 13513 BUF_UNLOCK(bp); 13514 } else 13515 BO_UNLOCK(bo); 13516 /* 13517 * We have to wait for the direct pointers to 13518 * point at the newdirblk before the dependency 13519 * will go away. 13520 */ 13521 error = ffs_update(vp, 1); 13522 if (error) 13523 break; 13524 ACQUIRE_LOCK(ump); 13525 } 13526 return (error); 13527 } 13528 13529 /* 13530 * Eliminate a pagedep dependency by flushing out all its diradd dependencies. 13531 */ 13532 static int 13533 flush_pagedep_deps(pvp, mp, diraddhdp, locked_bp) 13534 struct vnode *pvp; 13535 struct mount *mp; 13536 struct diraddhd *diraddhdp; 13537 struct buf *locked_bp; 13538 { 13539 struct inodedep *inodedep; 13540 struct inoref *inoref; 13541 struct ufsmount *ump; 13542 struct diradd *dap; 13543 struct vnode *vp; 13544 int error = 0; 13545 struct buf *bp; 13546 ino_t inum; 13547 struct diraddhd unfinished; 13548 13549 LIST_INIT(&unfinished); 13550 ump = VFSTOUFS(mp); 13551 LOCK_OWNED(ump); 13552 restart: 13553 while ((dap = LIST_FIRST(diraddhdp)) != NULL) { 13554 /* 13555 * Flush ourselves if this directory entry 13556 * has a MKDIR_PARENT dependency. 13557 */ 13558 if (dap->da_state & MKDIR_PARENT) { 13559 FREE_LOCK(ump); 13560 if ((error = ffs_update(pvp, 1)) != 0) 13561 break; 13562 ACQUIRE_LOCK(ump); 13563 /* 13564 * If that cleared dependencies, go on to next. 13565 */ 13566 if (dap != LIST_FIRST(diraddhdp)) 13567 continue; 13568 /* 13569 * All MKDIR_PARENT dependencies and all the 13570 * NEWBLOCK pagedeps that are contained in direct 13571 * blocks were resolved by doing above ffs_update. 13572 * Pagedeps contained in indirect blocks may 13573 * require a complete sync'ing of the directory. 13574 * We are in the midst of doing a complete sync, 13575 * so if they are not resolved in this pass we 13576 * defer them for now as they will be sync'ed by 13577 * our caller shortly. 13578 */ 13579 LIST_REMOVE(dap, da_pdlist); 13580 LIST_INSERT_HEAD(&unfinished, dap, da_pdlist); 13581 continue; 13582 } 13583 /* 13584 * A newly allocated directory must have its "." and 13585 * ".." entries written out before its name can be 13586 * committed in its parent. 13587 */ 13588 inum = dap->da_newinum; 13589 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 13590 panic("flush_pagedep_deps: lost inode1"); 13591 /* 13592 * Wait for any pending journal adds to complete so we don't 13593 * cause rollbacks while syncing. 13594 */ 13595 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 13596 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 13597 == DEPCOMPLETE) { 13598 jwait(&inoref->if_list, MNT_WAIT); 13599 goto restart; 13600 } 13601 } 13602 if (dap->da_state & MKDIR_BODY) { 13603 FREE_LOCK(ump); 13604 error = get_parent_vp(pvp, mp, inum, locked_bp, 13605 diraddhdp, &unfinished, &vp); 13606 if (error != 0) 13607 break; 13608 error = flush_newblk_dep(vp, mp, 0); 13609 /* 13610 * If we still have the dependency we might need to 13611 * update the vnode to sync the new link count to 13612 * disk. 13613 */ 13614 if (error == 0 && dap == LIST_FIRST(diraddhdp)) 13615 error = ffs_update(vp, 1); 13616 vput(vp); 13617 if (error != 0) 13618 break; 13619 ACQUIRE_LOCK(ump); 13620 /* 13621 * If that cleared dependencies, go on to next. 13622 */ 13623 if (dap != LIST_FIRST(diraddhdp)) 13624 continue; 13625 if (dap->da_state & MKDIR_BODY) { 13626 inodedep_lookup(UFSTOVFS(ump), inum, 0, 13627 &inodedep); 13628 panic("flush_pagedep_deps: MKDIR_BODY " 13629 "inodedep %p dap %p vp %p", 13630 inodedep, dap, vp); 13631 } 13632 } 13633 /* 13634 * Flush the inode on which the directory entry depends. 13635 * Having accounted for MKDIR_PARENT and MKDIR_BODY above, 13636 * the only remaining dependency is that the updated inode 13637 * count must get pushed to disk. The inode has already 13638 * been pushed into its inode buffer (via VOP_UPDATE) at 13639 * the time of the reference count change. So we need only 13640 * locate that buffer, ensure that there will be no rollback 13641 * caused by a bitmap dependency, then write the inode buffer. 13642 */ 13643 retry: 13644 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 13645 panic("flush_pagedep_deps: lost inode"); 13646 /* 13647 * If the inode still has bitmap dependencies, 13648 * push them to disk. 13649 */ 13650 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) { 13651 bp = inodedep->id_bmsafemap->sm_buf; 13652 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 13653 if (bp == NULL) 13654 goto retry; 13655 FREE_LOCK(ump); 13656 if ((error = bwrite(bp)) != 0) 13657 break; 13658 ACQUIRE_LOCK(ump); 13659 if (dap != LIST_FIRST(diraddhdp)) 13660 continue; 13661 } 13662 /* 13663 * If the inode is still sitting in a buffer waiting 13664 * to be written or waiting for the link count to be 13665 * adjusted update it here to flush it to disk. 13666 */ 13667 if (dap == LIST_FIRST(diraddhdp)) { 13668 FREE_LOCK(ump); 13669 error = get_parent_vp(pvp, mp, inum, locked_bp, 13670 diraddhdp, &unfinished, &vp); 13671 if (error != 0) 13672 break; 13673 error = ffs_update(vp, 1); 13674 vput(vp); 13675 if (error) 13676 break; 13677 ACQUIRE_LOCK(ump); 13678 } 13679 /* 13680 * If we have failed to get rid of all the dependencies 13681 * then something is seriously wrong. 13682 */ 13683 if (dap == LIST_FIRST(diraddhdp)) { 13684 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep); 13685 panic("flush_pagedep_deps: failed to flush " 13686 "inodedep %p ino %ju dap %p", 13687 inodedep, (uintmax_t)inum, dap); 13688 } 13689 } 13690 if (error) 13691 ACQUIRE_LOCK(ump); 13692 while ((dap = LIST_FIRST(&unfinished)) != NULL) { 13693 LIST_REMOVE(dap, da_pdlist); 13694 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist); 13695 } 13696 return (error); 13697 } 13698 13699 /* 13700 * A large burst of file addition or deletion activity can drive the 13701 * memory load excessively high. First attempt to slow things down 13702 * using the techniques below. If that fails, this routine requests 13703 * the offending operations to fall back to running synchronously 13704 * until the memory load returns to a reasonable level. 13705 */ 13706 int 13707 softdep_slowdown(vp) 13708 struct vnode *vp; 13709 { 13710 struct ufsmount *ump; 13711 int jlow; 13712 int max_softdeps_hard; 13713 13714 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 13715 ("softdep_slowdown called on non-softdep filesystem")); 13716 ump = VFSTOUFS(vp->v_mount); 13717 ACQUIRE_LOCK(ump); 13718 jlow = 0; 13719 /* 13720 * Check for journal space if needed. 13721 */ 13722 if (DOINGSUJ(vp)) { 13723 if (journal_space(ump, 0) == 0) 13724 jlow = 1; 13725 } 13726 /* 13727 * If the system is under its limits and our filesystem is 13728 * not responsible for more than our share of the usage and 13729 * we are not low on journal space, then no need to slow down. 13730 */ 13731 max_softdeps_hard = max_softdeps * 11 / 10; 13732 if (dep_current[D_DIRREM] < max_softdeps_hard / 2 && 13733 dep_current[D_INODEDEP] < max_softdeps_hard && 13734 dep_current[D_INDIRDEP] < max_softdeps_hard / 1000 && 13735 dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0 && 13736 ump->softdep_curdeps[D_DIRREM] < 13737 (max_softdeps_hard / 2) / stat_flush_threads && 13738 ump->softdep_curdeps[D_INODEDEP] < 13739 max_softdeps_hard / stat_flush_threads && 13740 ump->softdep_curdeps[D_INDIRDEP] < 13741 (max_softdeps_hard / 1000) / stat_flush_threads && 13742 ump->softdep_curdeps[D_FREEBLKS] < 13743 max_softdeps_hard / stat_flush_threads) { 13744 FREE_LOCK(ump); 13745 return (0); 13746 } 13747 /* 13748 * If the journal is low or our filesystem is over its limit 13749 * then speedup the cleanup. 13750 */ 13751 if (ump->softdep_curdeps[D_INDIRDEP] < 13752 (max_softdeps_hard / 1000) / stat_flush_threads || jlow) 13753 softdep_speedup(ump); 13754 stat_sync_limit_hit += 1; 13755 FREE_LOCK(ump); 13756 /* 13757 * We only slow down the rate at which new dependencies are 13758 * generated if we are not using journaling. With journaling, 13759 * the cleanup should always be sufficient to keep things 13760 * under control. 13761 */ 13762 if (DOINGSUJ(vp)) 13763 return (0); 13764 return (1); 13765 } 13766 13767 static int 13768 softdep_request_cleanup_filter(struct vnode *vp, void *arg __unused) 13769 { 13770 return ((vp->v_iflag & VI_OWEINACT) != 0 && vp->v_usecount == 0 && 13771 ((vp->v_vflag & VV_NOSYNC) != 0 || VTOI(vp)->i_effnlink == 0)); 13772 } 13773 13774 static void 13775 softdep_request_cleanup_inactivate(struct mount *mp) 13776 { 13777 struct vnode *vp, *mvp; 13778 int error; 13779 13780 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, softdep_request_cleanup_filter, 13781 NULL) { 13782 vholdl(vp); 13783 vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY); 13784 VI_LOCK(vp); 13785 if (vp->v_data != NULL && vp->v_usecount == 0) { 13786 while ((vp->v_iflag & VI_OWEINACT) != 0) { 13787 error = vinactive(vp); 13788 if (error != 0 && error != ERELOOKUP) 13789 break; 13790 } 13791 atomic_add_int(&stat_delayed_inact, 1); 13792 } 13793 VOP_UNLOCK(vp); 13794 vdropl(vp); 13795 } 13796 } 13797 13798 /* 13799 * Called by the allocation routines when they are about to fail 13800 * in the hope that we can free up the requested resource (inodes 13801 * or disk space). 13802 * 13803 * First check to see if the work list has anything on it. If it has, 13804 * clean up entries until we successfully free the requested resource. 13805 * Because this process holds inodes locked, we cannot handle any remove 13806 * requests that might block on a locked inode as that could lead to 13807 * deadlock. If the worklist yields none of the requested resource, 13808 * start syncing out vnodes to free up the needed space. 13809 */ 13810 int 13811 softdep_request_cleanup(fs, vp, cred, resource) 13812 struct fs *fs; 13813 struct vnode *vp; 13814 struct ucred *cred; 13815 int resource; 13816 { 13817 struct ufsmount *ump; 13818 struct mount *mp; 13819 long starttime; 13820 ufs2_daddr_t needed; 13821 int error, failed_vnode; 13822 13823 /* 13824 * If we are being called because of a process doing a 13825 * copy-on-write, then it is not safe to process any 13826 * worklist items as we will recurse into the copyonwrite 13827 * routine. This will result in an incoherent snapshot. 13828 * If the vnode that we hold is a snapshot, we must avoid 13829 * handling other resources that could cause deadlock. 13830 */ 13831 if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp))) 13832 return (0); 13833 13834 if (resource == FLUSH_BLOCKS_WAIT) 13835 stat_cleanup_blkrequests += 1; 13836 else 13837 stat_cleanup_inorequests += 1; 13838 13839 mp = vp->v_mount; 13840 ump = VFSTOUFS(mp); 13841 mtx_assert(UFS_MTX(ump), MA_OWNED); 13842 UFS_UNLOCK(ump); 13843 error = ffs_update(vp, 1); 13844 if (error != 0 || MOUNTEDSOFTDEP(mp) == 0) { 13845 UFS_LOCK(ump); 13846 return (0); 13847 } 13848 /* 13849 * If we are in need of resources, start by cleaning up 13850 * any block removals associated with our inode. 13851 */ 13852 ACQUIRE_LOCK(ump); 13853 process_removes(vp); 13854 process_truncates(vp); 13855 FREE_LOCK(ump); 13856 /* 13857 * Now clean up at least as many resources as we will need. 13858 * 13859 * When requested to clean up inodes, the number that are needed 13860 * is set by the number of simultaneous writers (mnt_writeopcount) 13861 * plus a bit of slop (2) in case some more writers show up while 13862 * we are cleaning. 13863 * 13864 * When requested to free up space, the amount of space that 13865 * we need is enough blocks to allocate a full-sized segment 13866 * (fs_contigsumsize). The number of such segments that will 13867 * be needed is set by the number of simultaneous writers 13868 * (mnt_writeopcount) plus a bit of slop (2) in case some more 13869 * writers show up while we are cleaning. 13870 * 13871 * Additionally, if we are unpriviledged and allocating space, 13872 * we need to ensure that we clean up enough blocks to get the 13873 * needed number of blocks over the threshold of the minimum 13874 * number of blocks required to be kept free by the filesystem 13875 * (fs_minfree). 13876 */ 13877 if (resource == FLUSH_INODES_WAIT) { 13878 needed = vfs_mount_fetch_counter(vp->v_mount, 13879 MNT_COUNT_WRITEOPCOUNT) + 2; 13880 } else if (resource == FLUSH_BLOCKS_WAIT) { 13881 needed = (vfs_mount_fetch_counter(vp->v_mount, 13882 MNT_COUNT_WRITEOPCOUNT) + 2) * fs->fs_contigsumsize; 13883 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE)) 13884 needed += fragstoblks(fs, 13885 roundup((fs->fs_dsize * fs->fs_minfree / 100) - 13886 fs->fs_cstotal.cs_nffree, fs->fs_frag)); 13887 } else { 13888 printf("softdep_request_cleanup: Unknown resource type %d\n", 13889 resource); 13890 UFS_LOCK(ump); 13891 return (0); 13892 } 13893 starttime = time_second; 13894 retry: 13895 if (resource == FLUSH_BLOCKS_WAIT && 13896 fs->fs_cstotal.cs_nbfree <= needed) 13897 softdep_send_speedup(ump, needed * fs->fs_bsize, 13898 BIO_SPEEDUP_TRIM); 13899 if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 && 13900 fs->fs_cstotal.cs_nbfree <= needed) || 13901 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13902 fs->fs_cstotal.cs_nifree <= needed)) { 13903 ACQUIRE_LOCK(ump); 13904 if (ump->softdep_on_worklist > 0 && 13905 process_worklist_item(UFSTOVFS(ump), 13906 ump->softdep_on_worklist, LK_NOWAIT) != 0) 13907 stat_worklist_push += 1; 13908 FREE_LOCK(ump); 13909 } 13910 13911 /* 13912 * Check that there are vnodes pending inactivation. As they 13913 * have been unlinked, inactivating them will free up their 13914 * inodes. 13915 */ 13916 ACQUIRE_LOCK(ump); 13917 if (resource == FLUSH_INODES_WAIT && 13918 fs->fs_cstotal.cs_nifree <= needed && 13919 fs->fs_pendinginodes <= needed) { 13920 if ((ump->um_softdep->sd_flags & FLUSH_DI_ACTIVE) == 0) { 13921 ump->um_softdep->sd_flags |= FLUSH_DI_ACTIVE; 13922 FREE_LOCK(ump); 13923 softdep_request_cleanup_inactivate(mp); 13924 ACQUIRE_LOCK(ump); 13925 ump->um_softdep->sd_flags &= ~FLUSH_DI_ACTIVE; 13926 wakeup(&ump->um_softdep->sd_flags); 13927 } else { 13928 while ((ump->um_softdep->sd_flags & 13929 FLUSH_DI_ACTIVE) != 0) { 13930 msleep(&ump->um_softdep->sd_flags, 13931 LOCK_PTR(ump), PVM, "ffsvina", hz); 13932 } 13933 } 13934 } 13935 FREE_LOCK(ump); 13936 13937 /* 13938 * If we still need resources and there are no more worklist 13939 * entries to process to obtain them, we have to start flushing 13940 * the dirty vnodes to force the release of additional requests 13941 * to the worklist that we can then process to reap addition 13942 * resources. We walk the vnodes associated with the mount point 13943 * until we get the needed worklist requests that we can reap. 13944 * 13945 * If there are several threads all needing to clean the same 13946 * mount point, only one is allowed to walk the mount list. 13947 * When several threads all try to walk the same mount list, 13948 * they end up competing with each other and often end up in 13949 * livelock. This approach ensures that forward progress is 13950 * made at the cost of occational ENOSPC errors being returned 13951 * that might otherwise have been avoided. 13952 */ 13953 error = 1; 13954 if ((resource == FLUSH_BLOCKS_WAIT && 13955 fs->fs_cstotal.cs_nbfree <= needed) || 13956 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13957 fs->fs_cstotal.cs_nifree <= needed)) { 13958 ACQUIRE_LOCK(ump); 13959 if ((ump->um_softdep->sd_flags & FLUSH_RC_ACTIVE) == 0) { 13960 ump->um_softdep->sd_flags |= FLUSH_RC_ACTIVE; 13961 FREE_LOCK(ump); 13962 failed_vnode = softdep_request_cleanup_flush(mp, ump); 13963 ACQUIRE_LOCK(ump); 13964 ump->um_softdep->sd_flags &= ~FLUSH_RC_ACTIVE; 13965 wakeup(&ump->um_softdep->sd_flags); 13966 FREE_LOCK(ump); 13967 if (ump->softdep_on_worklist > 0) { 13968 stat_cleanup_retries += 1; 13969 if (!failed_vnode) 13970 goto retry; 13971 } 13972 } else { 13973 while ((ump->um_softdep->sd_flags & 13974 FLUSH_RC_ACTIVE) != 0) { 13975 msleep(&ump->um_softdep->sd_flags, 13976 LOCK_PTR(ump), PVM, "ffsrca", hz); 13977 } 13978 FREE_LOCK(ump); 13979 error = 0; 13980 } 13981 stat_cleanup_failures += 1; 13982 } 13983 if (time_second - starttime > stat_cleanup_high_delay) 13984 stat_cleanup_high_delay = time_second - starttime; 13985 UFS_LOCK(ump); 13986 return (error); 13987 } 13988 13989 /* 13990 * Scan the vnodes for the specified mount point flushing out any 13991 * vnodes that can be locked without waiting. Finally, try to flush 13992 * the device associated with the mount point if it can be locked 13993 * without waiting. 13994 * 13995 * We return 0 if we were able to lock every vnode in our scan. 13996 * If we had to skip one or more vnodes, we return 1. 13997 */ 13998 static int 13999 softdep_request_cleanup_flush(mp, ump) 14000 struct mount *mp; 14001 struct ufsmount *ump; 14002 { 14003 struct thread *td; 14004 struct vnode *lvp, *mvp; 14005 int failed_vnode; 14006 14007 failed_vnode = 0; 14008 td = curthread; 14009 MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) { 14010 if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) { 14011 VI_UNLOCK(lvp); 14012 continue; 14013 } 14014 if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT) != 0) { 14015 failed_vnode = 1; 14016 continue; 14017 } 14018 if (lvp->v_vflag & VV_NOSYNC) { /* unlinked */ 14019 vput(lvp); 14020 continue; 14021 } 14022 (void) ffs_syncvnode(lvp, MNT_NOWAIT, 0); 14023 vput(lvp); 14024 } 14025 lvp = ump->um_devvp; 14026 if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) { 14027 VOP_FSYNC(lvp, MNT_NOWAIT, td); 14028 VOP_UNLOCK(lvp); 14029 } 14030 return (failed_vnode); 14031 } 14032 14033 static bool 14034 softdep_excess_items(struct ufsmount *ump, int item) 14035 { 14036 14037 KASSERT(item >= 0 && item < D_LAST, ("item %d", item)); 14038 return (dep_current[item] > max_softdeps && 14039 ump->softdep_curdeps[item] > max_softdeps / 14040 stat_flush_threads); 14041 } 14042 14043 static void 14044 schedule_cleanup(struct mount *mp) 14045 { 14046 struct ufsmount *ump; 14047 struct thread *td; 14048 14049 ump = VFSTOUFS(mp); 14050 LOCK_OWNED(ump); 14051 FREE_LOCK(ump); 14052 td = curthread; 14053 if ((td->td_pflags & TDP_KTHREAD) != 0 && 14054 (td->td_proc->p_flag2 & P2_AST_SU) == 0) { 14055 /* 14056 * No ast is delivered to kernel threads, so nobody 14057 * would deref the mp. Some kernel threads 14058 * explicitely check for AST, e.g. NFS daemon does 14059 * this in the serving loop. 14060 */ 14061 return; 14062 } 14063 if (td->td_su != NULL) 14064 vfs_rel(td->td_su); 14065 vfs_ref(mp); 14066 td->td_su = mp; 14067 thread_lock(td); 14068 td->td_flags |= TDF_ASTPENDING; 14069 thread_unlock(td); 14070 } 14071 14072 static void 14073 softdep_ast_cleanup_proc(struct thread *td) 14074 { 14075 struct mount *mp; 14076 struct ufsmount *ump; 14077 int error; 14078 bool req; 14079 14080 while ((mp = td->td_su) != NULL) { 14081 td->td_su = NULL; 14082 error = vfs_busy(mp, MBF_NOWAIT); 14083 vfs_rel(mp); 14084 if (error != 0) 14085 return; 14086 if (ffs_own_mount(mp) && MOUNTEDSOFTDEP(mp)) { 14087 ump = VFSTOUFS(mp); 14088 for (;;) { 14089 req = false; 14090 ACQUIRE_LOCK(ump); 14091 if (softdep_excess_items(ump, D_INODEDEP)) { 14092 req = true; 14093 request_cleanup(mp, FLUSH_INODES); 14094 } 14095 if (softdep_excess_items(ump, D_DIRREM)) { 14096 req = true; 14097 request_cleanup(mp, FLUSH_BLOCKS); 14098 } 14099 FREE_LOCK(ump); 14100 if (softdep_excess_items(ump, D_NEWBLK) || 14101 softdep_excess_items(ump, D_ALLOCDIRECT) || 14102 softdep_excess_items(ump, D_ALLOCINDIR)) { 14103 error = vn_start_write(NULL, &mp, 14104 V_WAIT); 14105 if (error == 0) { 14106 req = true; 14107 VFS_SYNC(mp, MNT_WAIT); 14108 vn_finished_write(mp); 14109 } 14110 } 14111 if ((td->td_pflags & TDP_KTHREAD) != 0 || !req) 14112 break; 14113 } 14114 } 14115 vfs_unbusy(mp); 14116 } 14117 if ((mp = td->td_su) != NULL) { 14118 td->td_su = NULL; 14119 vfs_rel(mp); 14120 } 14121 } 14122 14123 /* 14124 * If memory utilization has gotten too high, deliberately slow things 14125 * down and speed up the I/O processing. 14126 */ 14127 static int 14128 request_cleanup(mp, resource) 14129 struct mount *mp; 14130 int resource; 14131 { 14132 struct thread *td = curthread; 14133 struct ufsmount *ump; 14134 14135 ump = VFSTOUFS(mp); 14136 LOCK_OWNED(ump); 14137 /* 14138 * We never hold up the filesystem syncer or buf daemon. 14139 */ 14140 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF)) 14141 return (0); 14142 /* 14143 * First check to see if the work list has gotten backlogged. 14144 * If it has, co-opt this process to help clean up two entries. 14145 * Because this process may hold inodes locked, we cannot 14146 * handle any remove requests that might block on a locked 14147 * inode as that could lead to deadlock. We set TDP_SOFTDEP 14148 * to avoid recursively processing the worklist. 14149 */ 14150 if (ump->softdep_on_worklist > max_softdeps / 10) { 14151 td->td_pflags |= TDP_SOFTDEP; 14152 process_worklist_item(mp, 2, LK_NOWAIT); 14153 td->td_pflags &= ~TDP_SOFTDEP; 14154 stat_worklist_push += 2; 14155 return(1); 14156 } 14157 /* 14158 * Next, we attempt to speed up the syncer process. If that 14159 * is successful, then we allow the process to continue. 14160 */ 14161 if (softdep_speedup(ump) && 14162 resource != FLUSH_BLOCKS_WAIT && 14163 resource != FLUSH_INODES_WAIT) 14164 return(0); 14165 /* 14166 * If we are resource constrained on inode dependencies, try 14167 * flushing some dirty inodes. Otherwise, we are constrained 14168 * by file deletions, so try accelerating flushes of directories 14169 * with removal dependencies. We would like to do the cleanup 14170 * here, but we probably hold an inode locked at this point and 14171 * that might deadlock against one that we try to clean. So, 14172 * the best that we can do is request the syncer daemon to do 14173 * the cleanup for us. 14174 */ 14175 switch (resource) { 14176 case FLUSH_INODES: 14177 case FLUSH_INODES_WAIT: 14178 ACQUIRE_GBLLOCK(&lk); 14179 stat_ino_limit_push += 1; 14180 req_clear_inodedeps += 1; 14181 FREE_GBLLOCK(&lk); 14182 stat_countp = &stat_ino_limit_hit; 14183 break; 14184 14185 case FLUSH_BLOCKS: 14186 case FLUSH_BLOCKS_WAIT: 14187 ACQUIRE_GBLLOCK(&lk); 14188 stat_blk_limit_push += 1; 14189 req_clear_remove += 1; 14190 FREE_GBLLOCK(&lk); 14191 stat_countp = &stat_blk_limit_hit; 14192 break; 14193 14194 default: 14195 panic("request_cleanup: unknown type"); 14196 } 14197 /* 14198 * Hopefully the syncer daemon will catch up and awaken us. 14199 * We wait at most tickdelay before proceeding in any case. 14200 */ 14201 ACQUIRE_GBLLOCK(&lk); 14202 FREE_LOCK(ump); 14203 proc_waiting += 1; 14204 if (callout_pending(&softdep_callout) == FALSE) 14205 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2, 14206 pause_timer, 0); 14207 14208 if ((td->td_pflags & TDP_KTHREAD) == 0) 14209 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0); 14210 proc_waiting -= 1; 14211 FREE_GBLLOCK(&lk); 14212 ACQUIRE_LOCK(ump); 14213 return (1); 14214 } 14215 14216 /* 14217 * Awaken processes pausing in request_cleanup and clear proc_waiting 14218 * to indicate that there is no longer a timer running. Pause_timer 14219 * will be called with the global softdep mutex (&lk) locked. 14220 */ 14221 static void 14222 pause_timer(arg) 14223 void *arg; 14224 { 14225 14226 GBLLOCK_OWNED(&lk); 14227 /* 14228 * The callout_ API has acquired mtx and will hold it around this 14229 * function call. 14230 */ 14231 *stat_countp += proc_waiting; 14232 wakeup(&proc_waiting); 14233 } 14234 14235 /* 14236 * If requested, try removing inode or removal dependencies. 14237 */ 14238 static void 14239 check_clear_deps(mp) 14240 struct mount *mp; 14241 { 14242 struct ufsmount *ump; 14243 bool suj_susp; 14244 14245 /* 14246 * Tell the lower layers that any TRIM or WRITE transactions that have 14247 * been delayed for performance reasons should proceed to help alleviate 14248 * the shortage faster. The race between checking req_* and the softdep 14249 * mutex (lk) is fine since this is an advisory operation that at most 14250 * causes deferred work to be done sooner. 14251 */ 14252 ump = VFSTOUFS(mp); 14253 suj_susp = ump->um_softdep->sd_jblocks != NULL && 14254 ump->softdep_jblocks->jb_suspended; 14255 if (req_clear_remove || req_clear_inodedeps || suj_susp) { 14256 FREE_LOCK(ump); 14257 softdep_send_speedup(ump, 0, BIO_SPEEDUP_TRIM | BIO_SPEEDUP_WRITE); 14258 ACQUIRE_LOCK(ump); 14259 } 14260 14261 /* 14262 * If we are suspended, it may be because of our using 14263 * too many inodedeps, so help clear them out. 14264 */ 14265 if (suj_susp) 14266 clear_inodedeps(mp); 14267 14268 /* 14269 * General requests for cleanup of backed up dependencies 14270 */ 14271 ACQUIRE_GBLLOCK(&lk); 14272 if (req_clear_inodedeps) { 14273 req_clear_inodedeps -= 1; 14274 FREE_GBLLOCK(&lk); 14275 clear_inodedeps(mp); 14276 ACQUIRE_GBLLOCK(&lk); 14277 wakeup(&proc_waiting); 14278 } 14279 if (req_clear_remove) { 14280 req_clear_remove -= 1; 14281 FREE_GBLLOCK(&lk); 14282 clear_remove(mp); 14283 ACQUIRE_GBLLOCK(&lk); 14284 wakeup(&proc_waiting); 14285 } 14286 FREE_GBLLOCK(&lk); 14287 } 14288 14289 /* 14290 * Flush out a directory with at least one removal dependency in an effort to 14291 * reduce the number of dirrem, freefile, and freeblks dependency structures. 14292 */ 14293 static void 14294 clear_remove(mp) 14295 struct mount *mp; 14296 { 14297 struct pagedep_hashhead *pagedephd; 14298 struct pagedep *pagedep; 14299 struct ufsmount *ump; 14300 struct vnode *vp; 14301 struct bufobj *bo; 14302 int error, cnt; 14303 ino_t ino; 14304 14305 ump = VFSTOUFS(mp); 14306 LOCK_OWNED(ump); 14307 14308 for (cnt = 0; cnt <= ump->pagedep_hash_size; cnt++) { 14309 pagedephd = &ump->pagedep_hashtbl[ump->pagedep_nextclean++]; 14310 if (ump->pagedep_nextclean > ump->pagedep_hash_size) 14311 ump->pagedep_nextclean = 0; 14312 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 14313 if (LIST_EMPTY(&pagedep->pd_dirremhd)) 14314 continue; 14315 ino = pagedep->pd_ino; 14316 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 14317 continue; 14318 FREE_LOCK(ump); 14319 14320 /* 14321 * Let unmount clear deps 14322 */ 14323 error = vfs_busy(mp, MBF_NOWAIT); 14324 if (error != 0) 14325 goto finish_write; 14326 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 14327 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP); 14328 vfs_unbusy(mp); 14329 if (error != 0) { 14330 softdep_error("clear_remove: vget", error); 14331 goto finish_write; 14332 } 14333 MPASS(VTOI(vp)->i_mode != 0); 14334 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 14335 softdep_error("clear_remove: fsync", error); 14336 bo = &vp->v_bufobj; 14337 BO_LOCK(bo); 14338 drain_output(vp); 14339 BO_UNLOCK(bo); 14340 vput(vp); 14341 finish_write: 14342 vn_finished_write(mp); 14343 ACQUIRE_LOCK(ump); 14344 return; 14345 } 14346 } 14347 } 14348 14349 /* 14350 * Clear out a block of dirty inodes in an effort to reduce 14351 * the number of inodedep dependency structures. 14352 */ 14353 static void 14354 clear_inodedeps(mp) 14355 struct mount *mp; 14356 { 14357 struct inodedep_hashhead *inodedephd; 14358 struct inodedep *inodedep; 14359 struct ufsmount *ump; 14360 struct vnode *vp; 14361 struct fs *fs; 14362 int error, cnt; 14363 ino_t firstino, lastino, ino; 14364 14365 ump = VFSTOUFS(mp); 14366 fs = ump->um_fs; 14367 LOCK_OWNED(ump); 14368 /* 14369 * Pick a random inode dependency to be cleared. 14370 * We will then gather up all the inodes in its block 14371 * that have dependencies and flush them out. 14372 */ 14373 for (cnt = 0; cnt <= ump->inodedep_hash_size; cnt++) { 14374 inodedephd = &ump->inodedep_hashtbl[ump->inodedep_nextclean++]; 14375 if (ump->inodedep_nextclean > ump->inodedep_hash_size) 14376 ump->inodedep_nextclean = 0; 14377 if ((inodedep = LIST_FIRST(inodedephd)) != NULL) 14378 break; 14379 } 14380 if (inodedep == NULL) 14381 return; 14382 /* 14383 * Find the last inode in the block with dependencies. 14384 */ 14385 firstino = rounddown2(inodedep->id_ino, INOPB(fs)); 14386 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--) 14387 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0) 14388 break; 14389 /* 14390 * Asynchronously push all but the last inode with dependencies. 14391 * Synchronously push the last inode with dependencies to ensure 14392 * that the inode block gets written to free up the inodedeps. 14393 */ 14394 for (ino = firstino; ino <= lastino; ino++) { 14395 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 14396 continue; 14397 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 14398 continue; 14399 FREE_LOCK(ump); 14400 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */ 14401 if (error != 0) { 14402 vn_finished_write(mp); 14403 ACQUIRE_LOCK(ump); 14404 return; 14405 } 14406 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 14407 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP)) != 0) { 14408 softdep_error("clear_inodedeps: vget", error); 14409 vfs_unbusy(mp); 14410 vn_finished_write(mp); 14411 ACQUIRE_LOCK(ump); 14412 return; 14413 } 14414 vfs_unbusy(mp); 14415 if (VTOI(vp)->i_mode == 0) { 14416 vgone(vp); 14417 } else if (ino == lastino) { 14418 do { 14419 error = ffs_syncvnode(vp, MNT_WAIT, 0); 14420 } while (error == ERELOOKUP); 14421 if (error != 0) 14422 softdep_error("clear_inodedeps: fsync1", error); 14423 } else { 14424 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 14425 softdep_error("clear_inodedeps: fsync2", error); 14426 BO_LOCK(&vp->v_bufobj); 14427 drain_output(vp); 14428 BO_UNLOCK(&vp->v_bufobj); 14429 } 14430 vput(vp); 14431 vn_finished_write(mp); 14432 ACQUIRE_LOCK(ump); 14433 } 14434 } 14435 14436 void 14437 softdep_buf_append(bp, wkhd) 14438 struct buf *bp; 14439 struct workhead *wkhd; 14440 { 14441 struct worklist *wk; 14442 struct ufsmount *ump; 14443 14444 if ((wk = LIST_FIRST(wkhd)) == NULL) 14445 return; 14446 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 14447 ("softdep_buf_append called on non-softdep filesystem")); 14448 ump = VFSTOUFS(wk->wk_mp); 14449 ACQUIRE_LOCK(ump); 14450 while ((wk = LIST_FIRST(wkhd)) != NULL) { 14451 WORKLIST_REMOVE(wk); 14452 WORKLIST_INSERT(&bp->b_dep, wk); 14453 } 14454 FREE_LOCK(ump); 14455 14456 } 14457 14458 void 14459 softdep_inode_append(ip, cred, wkhd) 14460 struct inode *ip; 14461 struct ucred *cred; 14462 struct workhead *wkhd; 14463 { 14464 struct buf *bp; 14465 struct fs *fs; 14466 struct ufsmount *ump; 14467 int error; 14468 14469 ump = ITOUMP(ip); 14470 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 14471 ("softdep_inode_append called on non-softdep filesystem")); 14472 fs = ump->um_fs; 14473 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 14474 (int)fs->fs_bsize, cred, &bp); 14475 if (error) { 14476 bqrelse(bp); 14477 softdep_freework(wkhd); 14478 return; 14479 } 14480 softdep_buf_append(bp, wkhd); 14481 bqrelse(bp); 14482 } 14483 14484 void 14485 softdep_freework(wkhd) 14486 struct workhead *wkhd; 14487 { 14488 struct worklist *wk; 14489 struct ufsmount *ump; 14490 14491 if ((wk = LIST_FIRST(wkhd)) == NULL) 14492 return; 14493 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 14494 ("softdep_freework called on non-softdep filesystem")); 14495 ump = VFSTOUFS(wk->wk_mp); 14496 ACQUIRE_LOCK(ump); 14497 handle_jwork(wkhd); 14498 FREE_LOCK(ump); 14499 } 14500 14501 static struct ufsmount * 14502 softdep_bp_to_mp(bp) 14503 struct buf *bp; 14504 { 14505 struct mount *mp; 14506 struct vnode *vp; 14507 14508 if (LIST_EMPTY(&bp->b_dep)) 14509 return (NULL); 14510 vp = bp->b_vp; 14511 KASSERT(vp != NULL, 14512 ("%s, buffer with dependencies lacks vnode", __func__)); 14513 14514 /* 14515 * The ump mount point is stable after we get a correct 14516 * pointer, since bp is locked and this prevents unmount from 14517 * proceeding. But to get to it, we cannot dereference bp->b_dep 14518 * head wk_mp, because we do not yet own SU ump lock and 14519 * workitem might be freed while dereferenced. 14520 */ 14521 retry: 14522 switch (vp->v_type) { 14523 case VCHR: 14524 VI_LOCK(vp); 14525 mp = vp->v_type == VCHR ? vp->v_rdev->si_mountpt : NULL; 14526 VI_UNLOCK(vp); 14527 if (mp == NULL) 14528 goto retry; 14529 break; 14530 case VREG: 14531 case VDIR: 14532 case VLNK: 14533 case VFIFO: 14534 case VSOCK: 14535 mp = vp->v_mount; 14536 break; 14537 case VBLK: 14538 vn_printf(vp, "softdep_bp_to_mp: unexpected block device\n"); 14539 /* FALLTHROUGH */ 14540 case VNON: 14541 case VBAD: 14542 case VMARKER: 14543 mp = NULL; 14544 break; 14545 default: 14546 vn_printf(vp, "unknown vnode type"); 14547 mp = NULL; 14548 break; 14549 } 14550 return (VFSTOUFS(mp)); 14551 } 14552 14553 /* 14554 * Function to determine if the buffer has outstanding dependencies 14555 * that will cause a roll-back if the buffer is written. If wantcount 14556 * is set, return number of dependencies, otherwise just yes or no. 14557 */ 14558 static int 14559 softdep_count_dependencies(bp, wantcount) 14560 struct buf *bp; 14561 int wantcount; 14562 { 14563 struct worklist *wk; 14564 struct ufsmount *ump; 14565 struct bmsafemap *bmsafemap; 14566 struct freework *freework; 14567 struct inodedep *inodedep; 14568 struct indirdep *indirdep; 14569 struct freeblks *freeblks; 14570 struct allocindir *aip; 14571 struct pagedep *pagedep; 14572 struct dirrem *dirrem; 14573 struct newblk *newblk; 14574 struct mkdir *mkdir; 14575 struct diradd *dap; 14576 int i, retval; 14577 14578 ump = softdep_bp_to_mp(bp); 14579 if (ump == NULL) 14580 return (0); 14581 retval = 0; 14582 ACQUIRE_LOCK(ump); 14583 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 14584 switch (wk->wk_type) { 14585 case D_INODEDEP: 14586 inodedep = WK_INODEDEP(wk); 14587 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 14588 /* bitmap allocation dependency */ 14589 retval += 1; 14590 if (!wantcount) 14591 goto out; 14592 } 14593 if (TAILQ_FIRST(&inodedep->id_inoupdt)) { 14594 /* direct block pointer dependency */ 14595 retval += 1; 14596 if (!wantcount) 14597 goto out; 14598 } 14599 if (TAILQ_FIRST(&inodedep->id_extupdt)) { 14600 /* direct block pointer dependency */ 14601 retval += 1; 14602 if (!wantcount) 14603 goto out; 14604 } 14605 if (TAILQ_FIRST(&inodedep->id_inoreflst)) { 14606 /* Add reference dependency. */ 14607 retval += 1; 14608 if (!wantcount) 14609 goto out; 14610 } 14611 continue; 14612 14613 case D_INDIRDEP: 14614 indirdep = WK_INDIRDEP(wk); 14615 14616 TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) { 14617 /* indirect truncation dependency */ 14618 retval += 1; 14619 if (!wantcount) 14620 goto out; 14621 } 14622 14623 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 14624 /* indirect block pointer dependency */ 14625 retval += 1; 14626 if (!wantcount) 14627 goto out; 14628 } 14629 continue; 14630 14631 case D_PAGEDEP: 14632 pagedep = WK_PAGEDEP(wk); 14633 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) { 14634 if (LIST_FIRST(&dirrem->dm_jremrefhd)) { 14635 /* Journal remove ref dependency. */ 14636 retval += 1; 14637 if (!wantcount) 14638 goto out; 14639 } 14640 } 14641 for (i = 0; i < DAHASHSZ; i++) { 14642 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 14643 /* directory entry dependency */ 14644 retval += 1; 14645 if (!wantcount) 14646 goto out; 14647 } 14648 } 14649 continue; 14650 14651 case D_BMSAFEMAP: 14652 bmsafemap = WK_BMSAFEMAP(wk); 14653 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) { 14654 /* Add reference dependency. */ 14655 retval += 1; 14656 if (!wantcount) 14657 goto out; 14658 } 14659 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) { 14660 /* Allocate block dependency. */ 14661 retval += 1; 14662 if (!wantcount) 14663 goto out; 14664 } 14665 continue; 14666 14667 case D_FREEBLKS: 14668 freeblks = WK_FREEBLKS(wk); 14669 if (LIST_FIRST(&freeblks->fb_jblkdephd)) { 14670 /* Freeblk journal dependency. */ 14671 retval += 1; 14672 if (!wantcount) 14673 goto out; 14674 } 14675 continue; 14676 14677 case D_ALLOCDIRECT: 14678 case D_ALLOCINDIR: 14679 newblk = WK_NEWBLK(wk); 14680 if (newblk->nb_jnewblk) { 14681 /* Journal allocate dependency. */ 14682 retval += 1; 14683 if (!wantcount) 14684 goto out; 14685 } 14686 continue; 14687 14688 case D_MKDIR: 14689 mkdir = WK_MKDIR(wk); 14690 if (mkdir->md_jaddref) { 14691 /* Journal reference dependency. */ 14692 retval += 1; 14693 if (!wantcount) 14694 goto out; 14695 } 14696 continue; 14697 14698 case D_FREEWORK: 14699 case D_FREEDEP: 14700 case D_JSEGDEP: 14701 case D_JSEG: 14702 case D_SBDEP: 14703 /* never a dependency on these blocks */ 14704 continue; 14705 14706 default: 14707 panic("softdep_count_dependencies: Unexpected type %s", 14708 TYPENAME(wk->wk_type)); 14709 /* NOTREACHED */ 14710 } 14711 } 14712 out: 14713 FREE_LOCK(ump); 14714 return (retval); 14715 } 14716 14717 /* 14718 * Acquire exclusive access to a buffer. 14719 * Must be called with a locked mtx parameter. 14720 * Return acquired buffer or NULL on failure. 14721 */ 14722 static struct buf * 14723 getdirtybuf(bp, lock, waitfor) 14724 struct buf *bp; 14725 struct rwlock *lock; 14726 int waitfor; 14727 { 14728 int error; 14729 14730 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) { 14731 if (waitfor != MNT_WAIT) 14732 return (NULL); 14733 error = BUF_LOCK(bp, 14734 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock); 14735 /* 14736 * Even if we successfully acquire bp here, we have dropped 14737 * lock, which may violates our guarantee. 14738 */ 14739 if (error == 0) 14740 BUF_UNLOCK(bp); 14741 else if (error != ENOLCK) 14742 panic("getdirtybuf: inconsistent lock: %d", error); 14743 rw_wlock(lock); 14744 return (NULL); 14745 } 14746 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 14747 if (lock != BO_LOCKPTR(bp->b_bufobj) && waitfor == MNT_WAIT) { 14748 rw_wunlock(lock); 14749 BO_LOCK(bp->b_bufobj); 14750 BUF_UNLOCK(bp); 14751 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 14752 bp->b_vflags |= BV_BKGRDWAIT; 14753 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), 14754 PRIBIO | PDROP, "getbuf", 0); 14755 } else 14756 BO_UNLOCK(bp->b_bufobj); 14757 rw_wlock(lock); 14758 return (NULL); 14759 } 14760 BUF_UNLOCK(bp); 14761 if (waitfor != MNT_WAIT) 14762 return (NULL); 14763 #ifdef DEBUG_VFS_LOCKS 14764 if (bp->b_vp->v_type != VCHR) 14765 ASSERT_BO_WLOCKED(bp->b_bufobj); 14766 #endif 14767 bp->b_vflags |= BV_BKGRDWAIT; 14768 rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0); 14769 return (NULL); 14770 } 14771 if ((bp->b_flags & B_DELWRI) == 0) { 14772 BUF_UNLOCK(bp); 14773 return (NULL); 14774 } 14775 bremfree(bp); 14776 return (bp); 14777 } 14778 14779 /* 14780 * Check if it is safe to suspend the file system now. On entry, 14781 * the vnode interlock for devvp should be held. Return 0 with 14782 * the mount interlock held if the file system can be suspended now, 14783 * otherwise return EAGAIN with the mount interlock held. 14784 */ 14785 int 14786 softdep_check_suspend(struct mount *mp, 14787 struct vnode *devvp, 14788 int softdep_depcnt, 14789 int softdep_accdepcnt, 14790 int secondary_writes, 14791 int secondary_accwrites) 14792 { 14793 struct bufobj *bo; 14794 struct ufsmount *ump; 14795 struct inodedep *inodedep; 14796 int error, unlinked; 14797 14798 bo = &devvp->v_bufobj; 14799 ASSERT_BO_WLOCKED(bo); 14800 14801 /* 14802 * If we are not running with soft updates, then we need only 14803 * deal with secondary writes as we try to suspend. 14804 */ 14805 if (MOUNTEDSOFTDEP(mp) == 0) { 14806 MNT_ILOCK(mp); 14807 while (mp->mnt_secondary_writes != 0) { 14808 BO_UNLOCK(bo); 14809 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 14810 (PUSER - 1) | PDROP, "secwr", 0); 14811 BO_LOCK(bo); 14812 MNT_ILOCK(mp); 14813 } 14814 14815 /* 14816 * Reasons for needing more work before suspend: 14817 * - Dirty buffers on devvp. 14818 * - Secondary writes occurred after start of vnode sync loop 14819 */ 14820 error = 0; 14821 if (bo->bo_numoutput > 0 || 14822 bo->bo_dirty.bv_cnt > 0 || 14823 secondary_writes != 0 || 14824 mp->mnt_secondary_writes != 0 || 14825 secondary_accwrites != mp->mnt_secondary_accwrites) 14826 error = EAGAIN; 14827 BO_UNLOCK(bo); 14828 return (error); 14829 } 14830 14831 /* 14832 * If we are running with soft updates, then we need to coordinate 14833 * with them as we try to suspend. 14834 */ 14835 ump = VFSTOUFS(mp); 14836 for (;;) { 14837 if (!TRY_ACQUIRE_LOCK(ump)) { 14838 BO_UNLOCK(bo); 14839 ACQUIRE_LOCK(ump); 14840 FREE_LOCK(ump); 14841 BO_LOCK(bo); 14842 continue; 14843 } 14844 MNT_ILOCK(mp); 14845 if (mp->mnt_secondary_writes != 0) { 14846 FREE_LOCK(ump); 14847 BO_UNLOCK(bo); 14848 msleep(&mp->mnt_secondary_writes, 14849 MNT_MTX(mp), 14850 (PUSER - 1) | PDROP, "secwr", 0); 14851 BO_LOCK(bo); 14852 continue; 14853 } 14854 break; 14855 } 14856 14857 unlinked = 0; 14858 if (MOUNTEDSUJ(mp)) { 14859 for (inodedep = TAILQ_FIRST(&ump->softdep_unlinked); 14860 inodedep != NULL; 14861 inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 14862 if ((inodedep->id_state & (UNLINKED | UNLINKLINKS | 14863 UNLINKONLIST)) != (UNLINKED | UNLINKLINKS | 14864 UNLINKONLIST) || 14865 !check_inodedep_free(inodedep)) 14866 continue; 14867 unlinked++; 14868 } 14869 } 14870 14871 /* 14872 * Reasons for needing more work before suspend: 14873 * - Dirty buffers on devvp. 14874 * - Softdep activity occurred after start of vnode sync loop 14875 * - Secondary writes occurred after start of vnode sync loop 14876 */ 14877 error = 0; 14878 if (bo->bo_numoutput > 0 || 14879 bo->bo_dirty.bv_cnt > 0 || 14880 softdep_depcnt != unlinked || 14881 ump->softdep_deps != unlinked || 14882 softdep_accdepcnt != ump->softdep_accdeps || 14883 secondary_writes != 0 || 14884 mp->mnt_secondary_writes != 0 || 14885 secondary_accwrites != mp->mnt_secondary_accwrites) 14886 error = EAGAIN; 14887 FREE_LOCK(ump); 14888 BO_UNLOCK(bo); 14889 return (error); 14890 } 14891 14892 /* 14893 * Get the number of dependency structures for the file system, both 14894 * the current number and the total number allocated. These will 14895 * later be used to detect that softdep processing has occurred. 14896 */ 14897 void 14898 softdep_get_depcounts(struct mount *mp, 14899 int *softdep_depsp, 14900 int *softdep_accdepsp) 14901 { 14902 struct ufsmount *ump; 14903 14904 if (MOUNTEDSOFTDEP(mp) == 0) { 14905 *softdep_depsp = 0; 14906 *softdep_accdepsp = 0; 14907 return; 14908 } 14909 ump = VFSTOUFS(mp); 14910 ACQUIRE_LOCK(ump); 14911 *softdep_depsp = ump->softdep_deps; 14912 *softdep_accdepsp = ump->softdep_accdeps; 14913 FREE_LOCK(ump); 14914 } 14915 14916 /* 14917 * Wait for pending output on a vnode to complete. 14918 */ 14919 static void 14920 drain_output(vp) 14921 struct vnode *vp; 14922 { 14923 14924 ASSERT_VOP_LOCKED(vp, "drain_output"); 14925 (void)bufobj_wwait(&vp->v_bufobj, 0, 0); 14926 } 14927 14928 /* 14929 * Called whenever a buffer that is being invalidated or reallocated 14930 * contains dependencies. This should only happen if an I/O error has 14931 * occurred. The routine is called with the buffer locked. 14932 */ 14933 static void 14934 softdep_deallocate_dependencies(bp) 14935 struct buf *bp; 14936 { 14937 14938 if ((bp->b_ioflags & BIO_ERROR) == 0) 14939 panic("softdep_deallocate_dependencies: dangling deps"); 14940 if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL) 14941 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error); 14942 else 14943 printf("softdep_deallocate_dependencies: " 14944 "got error %d while accessing filesystem\n", bp->b_error); 14945 if (bp->b_error != ENXIO) 14946 panic("softdep_deallocate_dependencies: unrecovered I/O error"); 14947 } 14948 14949 /* 14950 * Function to handle asynchronous write errors in the filesystem. 14951 */ 14952 static void 14953 softdep_error(func, error) 14954 char *func; 14955 int error; 14956 { 14957 14958 /* XXX should do something better! */ 14959 printf("%s: got error %d while accessing filesystem\n", func, error); 14960 } 14961 14962 #ifdef DDB 14963 14964 /* exported to ffs_vfsops.c */ 14965 extern void db_print_ffs(struct ufsmount *ump); 14966 void 14967 db_print_ffs(struct ufsmount *ump) 14968 { 14969 db_printf("mp %p (%s) devvp %p\n", ump->um_mountp, 14970 ump->um_mountp->mnt_stat.f_mntonname, ump->um_devvp); 14971 db_printf(" fs %p su_wl %d su_deps %d su_req %d\n", 14972 ump->um_fs, ump->softdep_on_worklist, 14973 ump->softdep_deps, ump->softdep_req); 14974 } 14975 14976 static void 14977 worklist_print(struct worklist *wk, int verbose) 14978 { 14979 14980 if (!verbose) { 14981 db_printf("%s: %p state 0x%b\n", TYPENAME(wk->wk_type), wk, 14982 (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS); 14983 return; 14984 } 14985 db_printf("worklist: %p type %s state 0x%b next %p\n ", wk, 14986 TYPENAME(wk->wk_type), (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS, 14987 LIST_NEXT(wk, wk_list)); 14988 db_print_ffs(VFSTOUFS(wk->wk_mp)); 14989 } 14990 14991 static void 14992 inodedep_print(struct inodedep *inodedep, int verbose) 14993 { 14994 14995 worklist_print(&inodedep->id_list, 0); 14996 db_printf(" fs %p ino %jd inoblk %jd delta %jd nlink %jd\n", 14997 inodedep->id_fs, 14998 (intmax_t)inodedep->id_ino, 14999 (intmax_t)fsbtodb(inodedep->id_fs, 15000 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)), 15001 (intmax_t)inodedep->id_nlinkdelta, 15002 (intmax_t)inodedep->id_savednlink); 15003 15004 if (verbose == 0) 15005 return; 15006 15007 db_printf(" bmsafemap %p, mkdiradd %p, inoreflst %p\n", 15008 inodedep->id_bmsafemap, 15009 inodedep->id_mkdiradd, 15010 TAILQ_FIRST(&inodedep->id_inoreflst)); 15011 db_printf(" dirremhd %p, pendinghd %p, bufwait %p\n", 15012 LIST_FIRST(&inodedep->id_dirremhd), 15013 LIST_FIRST(&inodedep->id_pendinghd), 15014 LIST_FIRST(&inodedep->id_bufwait)); 15015 db_printf(" inowait %p, inoupdt %p, newinoupdt %p\n", 15016 LIST_FIRST(&inodedep->id_inowait), 15017 TAILQ_FIRST(&inodedep->id_inoupdt), 15018 TAILQ_FIRST(&inodedep->id_newinoupdt)); 15019 db_printf(" extupdt %p, newextupdt %p, freeblklst %p\n", 15020 TAILQ_FIRST(&inodedep->id_extupdt), 15021 TAILQ_FIRST(&inodedep->id_newextupdt), 15022 TAILQ_FIRST(&inodedep->id_freeblklst)); 15023 db_printf(" saveino %p, savedsize %jd, savedextsize %jd\n", 15024 inodedep->id_savedino1, 15025 (intmax_t)inodedep->id_savedsize, 15026 (intmax_t)inodedep->id_savedextsize); 15027 } 15028 15029 static void 15030 newblk_print(struct newblk *nbp) 15031 { 15032 15033 worklist_print(&nbp->nb_list, 0); 15034 db_printf(" newblkno %jd\n", (intmax_t)nbp->nb_newblkno); 15035 db_printf(" jnewblk %p, bmsafemap %p, freefrag %p\n", 15036 &nbp->nb_jnewblk, 15037 &nbp->nb_bmsafemap, 15038 &nbp->nb_freefrag); 15039 db_printf(" indirdeps %p, newdirblk %p, jwork %p\n", 15040 LIST_FIRST(&nbp->nb_indirdeps), 15041 LIST_FIRST(&nbp->nb_newdirblk), 15042 LIST_FIRST(&nbp->nb_jwork)); 15043 } 15044 15045 static void 15046 allocdirect_print(struct allocdirect *adp) 15047 { 15048 15049 newblk_print(&adp->ad_block); 15050 db_printf(" oldblkno %jd, oldsize %ld, newsize %ld\n", 15051 adp->ad_oldblkno, adp->ad_oldsize, adp->ad_newsize); 15052 db_printf(" offset %d, inodedep %p\n", 15053 adp->ad_offset, adp->ad_inodedep); 15054 } 15055 15056 static void 15057 allocindir_print(struct allocindir *aip) 15058 { 15059 15060 newblk_print(&aip->ai_block); 15061 db_printf(" oldblkno %jd, lbn %jd\n", 15062 (intmax_t)aip->ai_oldblkno, (intmax_t)aip->ai_lbn); 15063 db_printf(" offset %d, indirdep %p\n", 15064 aip->ai_offset, aip->ai_indirdep); 15065 } 15066 15067 static void 15068 mkdir_print(struct mkdir *mkdir) 15069 { 15070 15071 worklist_print(&mkdir->md_list, 0); 15072 db_printf(" diradd %p, jaddref %p, buf %p\n", 15073 mkdir->md_diradd, mkdir->md_jaddref, mkdir->md_buf); 15074 } 15075 15076 DB_SHOW_COMMAND(sd_inodedep, db_show_sd_inodedep) 15077 { 15078 15079 if (have_addr == 0) { 15080 db_printf("inodedep address required\n"); 15081 return; 15082 } 15083 inodedep_print((struct inodedep*)addr, 1); 15084 } 15085 15086 DB_SHOW_COMMAND(sd_allinodedeps, db_show_sd_allinodedeps) 15087 { 15088 struct inodedep_hashhead *inodedephd; 15089 struct inodedep *inodedep; 15090 struct ufsmount *ump; 15091 int cnt; 15092 15093 if (have_addr == 0) { 15094 db_printf("ufsmount address required\n"); 15095 return; 15096 } 15097 ump = (struct ufsmount *)addr; 15098 for (cnt = 0; cnt < ump->inodedep_hash_size; cnt++) { 15099 inodedephd = &ump->inodedep_hashtbl[cnt]; 15100 LIST_FOREACH(inodedep, inodedephd, id_hash) { 15101 inodedep_print(inodedep, 0); 15102 } 15103 } 15104 } 15105 15106 DB_SHOW_COMMAND(sd_worklist, db_show_sd_worklist) 15107 { 15108 15109 if (have_addr == 0) { 15110 db_printf("worklist address required\n"); 15111 return; 15112 } 15113 worklist_print((struct worklist *)addr, 1); 15114 } 15115 15116 DB_SHOW_COMMAND(sd_workhead, db_show_sd_workhead) 15117 { 15118 struct worklist *wk; 15119 struct workhead *wkhd; 15120 15121 if (have_addr == 0) { 15122 db_printf("worklist address required " 15123 "(for example value in bp->b_dep)\n"); 15124 return; 15125 } 15126 /* 15127 * We often do not have the address of the worklist head but 15128 * instead a pointer to its first entry (e.g., we have the 15129 * contents of bp->b_dep rather than &bp->b_dep). But the back 15130 * pointer of bp->b_dep will point at the head of the list, so 15131 * we cheat and use that instead. If we are in the middle of 15132 * a list we will still get the same result, so nothing 15133 * unexpected will result. 15134 */ 15135 wk = (struct worklist *)addr; 15136 if (wk == NULL) 15137 return; 15138 wkhd = (struct workhead *)wk->wk_list.le_prev; 15139 LIST_FOREACH(wk, wkhd, wk_list) { 15140 switch(wk->wk_type) { 15141 case D_INODEDEP: 15142 inodedep_print(WK_INODEDEP(wk), 0); 15143 continue; 15144 case D_ALLOCDIRECT: 15145 allocdirect_print(WK_ALLOCDIRECT(wk)); 15146 continue; 15147 case D_ALLOCINDIR: 15148 allocindir_print(WK_ALLOCINDIR(wk)); 15149 continue; 15150 case D_MKDIR: 15151 mkdir_print(WK_MKDIR(wk)); 15152 continue; 15153 default: 15154 worklist_print(wk, 0); 15155 continue; 15156 } 15157 } 15158 } 15159 15160 DB_SHOW_COMMAND(sd_mkdir, db_show_sd_mkdir) 15161 { 15162 if (have_addr == 0) { 15163 db_printf("mkdir address required\n"); 15164 return; 15165 } 15166 mkdir_print((struct mkdir *)addr); 15167 } 15168 15169 DB_SHOW_COMMAND(sd_mkdir_list, db_show_sd_mkdir_list) 15170 { 15171 struct mkdirlist *mkdirlisthd; 15172 struct mkdir *mkdir; 15173 15174 if (have_addr == 0) { 15175 db_printf("mkdir listhead address required\n"); 15176 return; 15177 } 15178 mkdirlisthd = (struct mkdirlist *)addr; 15179 LIST_FOREACH(mkdir, mkdirlisthd, md_mkdirs) { 15180 mkdir_print(mkdir); 15181 if (mkdir->md_diradd != NULL) { 15182 db_printf(" "); 15183 worklist_print(&mkdir->md_diradd->da_list, 0); 15184 } 15185 if (mkdir->md_jaddref != NULL) { 15186 db_printf(" "); 15187 worklist_print(&mkdir->md_jaddref->ja_list, 0); 15188 } 15189 } 15190 } 15191 15192 DB_SHOW_COMMAND(sd_allocdirect, db_show_sd_allocdirect) 15193 { 15194 if (have_addr == 0) { 15195 db_printf("allocdirect address required\n"); 15196 return; 15197 } 15198 allocdirect_print((struct allocdirect *)addr); 15199 } 15200 15201 DB_SHOW_COMMAND(sd_allocindir, db_show_sd_allocindir) 15202 { 15203 if (have_addr == 0) { 15204 db_printf("allocindir address required\n"); 15205 return; 15206 } 15207 allocindir_print((struct allocindir *)addr); 15208 } 15209 15210 #endif /* DDB */ 15211 15212 #endif /* SOFTUPDATES */ 15213