1 /*- 2 * Copyright 1998, 2000 Marshall Kirk McKusick. 3 * Copyright 2009, 2010 Jeffrey W. Roberson <jeff@FreeBSD.org> 4 * All rights reserved. 5 * 6 * The soft updates code is derived from the appendix of a University 7 * of Michigan technical report (Gregory R. Ganger and Yale N. Patt, 8 * "Soft Updates: A Solution to the Metadata Update Problem in File 9 * Systems", CSE-TR-254-95, August 1995). 10 * 11 * Further information about soft updates can be obtained from: 12 * 13 * Marshall Kirk McKusick http://www.mckusick.com/softdep/ 14 * 1614 Oxford Street mckusick@mckusick.com 15 * Berkeley, CA 94709-1608 +1-510-843-9542 16 * USA 17 * 18 * Redistribution and use in source and binary forms, with or without 19 * modification, are permitted provided that the following conditions 20 * are met: 21 * 22 * 1. Redistributions of source code must retain the above copyright 23 * notice, this list of conditions and the following disclaimer. 24 * 2. Redistributions in binary form must reproduce the above copyright 25 * notice, this list of conditions and the following disclaimer in the 26 * documentation and/or other materials provided with the distribution. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 31 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 * 39 * from: @(#)ffs_softdep.c 9.59 (McKusick) 6/21/00 40 */ 41 42 #include <sys/cdefs.h> 43 __FBSDID("$FreeBSD$"); 44 45 #include "opt_ffs.h" 46 #include "opt_quota.h" 47 #include "opt_ddb.h" 48 49 /* 50 * For now we want the safety net that the DEBUG flag provides. 51 */ 52 #ifndef DEBUG 53 #define DEBUG 54 #endif 55 56 #include <sys/param.h> 57 #include <sys/kernel.h> 58 #include <sys/systm.h> 59 #include <sys/bio.h> 60 #include <sys/buf.h> 61 #include <sys/kdb.h> 62 #include <sys/kthread.h> 63 #include <sys/ktr.h> 64 #include <sys/limits.h> 65 #include <sys/lock.h> 66 #include <sys/malloc.h> 67 #include <sys/mount.h> 68 #include <sys/mutex.h> 69 #include <sys/namei.h> 70 #include <sys/priv.h> 71 #include <sys/proc.h> 72 #include <sys/racct.h> 73 #include <sys/rwlock.h> 74 #include <sys/stat.h> 75 #include <sys/sysctl.h> 76 #include <sys/syslog.h> 77 #include <sys/vnode.h> 78 #include <sys/conf.h> 79 80 #include <ufs/ufs/dir.h> 81 #include <ufs/ufs/extattr.h> 82 #include <ufs/ufs/quota.h> 83 #include <ufs/ufs/inode.h> 84 #include <ufs/ufs/ufsmount.h> 85 #include <ufs/ffs/fs.h> 86 #include <ufs/ffs/softdep.h> 87 #include <ufs/ffs/ffs_extern.h> 88 #include <ufs/ufs/ufs_extern.h> 89 90 #include <vm/vm.h> 91 #include <vm/vm_extern.h> 92 #include <vm/vm_object.h> 93 94 #include <geom/geom.h> 95 96 #include <ddb/ddb.h> 97 98 #define KTR_SUJ 0 /* Define to KTR_SPARE. */ 99 100 #ifndef SOFTUPDATES 101 102 int 103 softdep_flushfiles(oldmnt, flags, td) 104 struct mount *oldmnt; 105 int flags; 106 struct thread *td; 107 { 108 109 panic("softdep_flushfiles called"); 110 } 111 112 int 113 softdep_mount(devvp, mp, fs, cred) 114 struct vnode *devvp; 115 struct mount *mp; 116 struct fs *fs; 117 struct ucred *cred; 118 { 119 120 return (0); 121 } 122 123 void 124 softdep_initialize() 125 { 126 127 return; 128 } 129 130 void 131 softdep_uninitialize() 132 { 133 134 return; 135 } 136 137 void 138 softdep_unmount(mp) 139 struct mount *mp; 140 { 141 142 panic("softdep_unmount called"); 143 } 144 145 void 146 softdep_setup_sbupdate(ump, fs, bp) 147 struct ufsmount *ump; 148 struct fs *fs; 149 struct buf *bp; 150 { 151 152 panic("softdep_setup_sbupdate called"); 153 } 154 155 void 156 softdep_setup_inomapdep(bp, ip, newinum, mode) 157 struct buf *bp; 158 struct inode *ip; 159 ino_t newinum; 160 int mode; 161 { 162 163 panic("softdep_setup_inomapdep called"); 164 } 165 166 void 167 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags) 168 struct buf *bp; 169 struct mount *mp; 170 ufs2_daddr_t newblkno; 171 int frags; 172 int oldfrags; 173 { 174 175 panic("softdep_setup_blkmapdep called"); 176 } 177 178 void 179 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 180 struct inode *ip; 181 ufs_lbn_t lbn; 182 ufs2_daddr_t newblkno; 183 ufs2_daddr_t oldblkno; 184 long newsize; 185 long oldsize; 186 struct buf *bp; 187 { 188 189 panic("softdep_setup_allocdirect called"); 190 } 191 192 void 193 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 194 struct inode *ip; 195 ufs_lbn_t lbn; 196 ufs2_daddr_t newblkno; 197 ufs2_daddr_t oldblkno; 198 long newsize; 199 long oldsize; 200 struct buf *bp; 201 { 202 203 panic("softdep_setup_allocext called"); 204 } 205 206 void 207 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 208 struct inode *ip; 209 ufs_lbn_t lbn; 210 struct buf *bp; 211 int ptrno; 212 ufs2_daddr_t newblkno; 213 ufs2_daddr_t oldblkno; 214 struct buf *nbp; 215 { 216 217 panic("softdep_setup_allocindir_page called"); 218 } 219 220 void 221 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 222 struct buf *nbp; 223 struct inode *ip; 224 struct buf *bp; 225 int ptrno; 226 ufs2_daddr_t newblkno; 227 { 228 229 panic("softdep_setup_allocindir_meta called"); 230 } 231 232 void 233 softdep_journal_freeblocks(ip, cred, length, flags) 234 struct inode *ip; 235 struct ucred *cred; 236 off_t length; 237 int flags; 238 { 239 240 panic("softdep_journal_freeblocks called"); 241 } 242 243 void 244 softdep_journal_fsync(ip) 245 struct inode *ip; 246 { 247 248 panic("softdep_journal_fsync called"); 249 } 250 251 void 252 softdep_setup_freeblocks(ip, length, flags) 253 struct inode *ip; 254 off_t length; 255 int flags; 256 { 257 258 panic("softdep_setup_freeblocks called"); 259 } 260 261 void 262 softdep_freefile(pvp, ino, mode) 263 struct vnode *pvp; 264 ino_t ino; 265 int mode; 266 { 267 268 panic("softdep_freefile called"); 269 } 270 271 int 272 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 273 struct buf *bp; 274 struct inode *dp; 275 off_t diroffset; 276 ino_t newinum; 277 struct buf *newdirbp; 278 int isnewblk; 279 { 280 281 panic("softdep_setup_directory_add called"); 282 } 283 284 void 285 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 286 struct buf *bp; 287 struct inode *dp; 288 caddr_t base; 289 caddr_t oldloc; 290 caddr_t newloc; 291 int entrysize; 292 { 293 294 panic("softdep_change_directoryentry_offset called"); 295 } 296 297 void 298 softdep_setup_remove(bp, dp, ip, isrmdir) 299 struct buf *bp; 300 struct inode *dp; 301 struct inode *ip; 302 int isrmdir; 303 { 304 305 panic("softdep_setup_remove called"); 306 } 307 308 void 309 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 310 struct buf *bp; 311 struct inode *dp; 312 struct inode *ip; 313 ino_t newinum; 314 int isrmdir; 315 { 316 317 panic("softdep_setup_directory_change called"); 318 } 319 320 void 321 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 322 struct mount *mp; 323 struct buf *bp; 324 ufs2_daddr_t blkno; 325 int frags; 326 struct workhead *wkhd; 327 { 328 329 panic("%s called", __FUNCTION__); 330 } 331 332 void 333 softdep_setup_inofree(mp, bp, ino, wkhd) 334 struct mount *mp; 335 struct buf *bp; 336 ino_t ino; 337 struct workhead *wkhd; 338 { 339 340 panic("%s called", __FUNCTION__); 341 } 342 343 void 344 softdep_setup_unlink(dp, ip) 345 struct inode *dp; 346 struct inode *ip; 347 { 348 349 panic("%s called", __FUNCTION__); 350 } 351 352 void 353 softdep_setup_link(dp, ip) 354 struct inode *dp; 355 struct inode *ip; 356 { 357 358 panic("%s called", __FUNCTION__); 359 } 360 361 void 362 softdep_revert_link(dp, ip) 363 struct inode *dp; 364 struct inode *ip; 365 { 366 367 panic("%s called", __FUNCTION__); 368 } 369 370 void 371 softdep_setup_rmdir(dp, ip) 372 struct inode *dp; 373 struct inode *ip; 374 { 375 376 panic("%s called", __FUNCTION__); 377 } 378 379 void 380 softdep_revert_rmdir(dp, ip) 381 struct inode *dp; 382 struct inode *ip; 383 { 384 385 panic("%s called", __FUNCTION__); 386 } 387 388 void 389 softdep_setup_create(dp, ip) 390 struct inode *dp; 391 struct inode *ip; 392 { 393 394 panic("%s called", __FUNCTION__); 395 } 396 397 void 398 softdep_revert_create(dp, ip) 399 struct inode *dp; 400 struct inode *ip; 401 { 402 403 panic("%s called", __FUNCTION__); 404 } 405 406 void 407 softdep_setup_mkdir(dp, ip) 408 struct inode *dp; 409 struct inode *ip; 410 { 411 412 panic("%s called", __FUNCTION__); 413 } 414 415 void 416 softdep_revert_mkdir(dp, ip) 417 struct inode *dp; 418 struct inode *ip; 419 { 420 421 panic("%s called", __FUNCTION__); 422 } 423 424 void 425 softdep_setup_dotdot_link(dp, ip) 426 struct inode *dp; 427 struct inode *ip; 428 { 429 430 panic("%s called", __FUNCTION__); 431 } 432 433 int 434 softdep_prealloc(vp, waitok) 435 struct vnode *vp; 436 int waitok; 437 { 438 439 panic("%s called", __FUNCTION__); 440 } 441 442 int 443 softdep_journal_lookup(mp, vpp) 444 struct mount *mp; 445 struct vnode **vpp; 446 { 447 448 return (ENOENT); 449 } 450 451 void 452 softdep_change_linkcnt(ip) 453 struct inode *ip; 454 { 455 456 panic("softdep_change_linkcnt called"); 457 } 458 459 void 460 softdep_load_inodeblock(ip) 461 struct inode *ip; 462 { 463 464 panic("softdep_load_inodeblock called"); 465 } 466 467 void 468 softdep_update_inodeblock(ip, bp, waitfor) 469 struct inode *ip; 470 struct buf *bp; 471 int waitfor; 472 { 473 474 panic("softdep_update_inodeblock called"); 475 } 476 477 int 478 softdep_fsync(vp) 479 struct vnode *vp; /* the "in_core" copy of the inode */ 480 { 481 482 return (0); 483 } 484 485 void 486 softdep_fsync_mountdev(vp) 487 struct vnode *vp; 488 { 489 490 return; 491 } 492 493 int 494 softdep_flushworklist(oldmnt, countp, td) 495 struct mount *oldmnt; 496 int *countp; 497 struct thread *td; 498 { 499 500 *countp = 0; 501 return (0); 502 } 503 504 int 505 softdep_sync_metadata(struct vnode *vp) 506 { 507 508 panic("softdep_sync_metadata called"); 509 } 510 511 int 512 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 513 { 514 515 panic("softdep_sync_buf called"); 516 } 517 518 int 519 softdep_slowdown(vp) 520 struct vnode *vp; 521 { 522 523 panic("softdep_slowdown called"); 524 } 525 526 int 527 softdep_request_cleanup(fs, vp, cred, resource) 528 struct fs *fs; 529 struct vnode *vp; 530 struct ucred *cred; 531 int resource; 532 { 533 534 return (0); 535 } 536 537 int 538 softdep_check_suspend(struct mount *mp, 539 struct vnode *devvp, 540 int softdep_depcnt, 541 int softdep_accdepcnt, 542 int secondary_writes, 543 int secondary_accwrites) 544 { 545 struct bufobj *bo; 546 int error; 547 548 (void) softdep_depcnt, 549 (void) softdep_accdepcnt; 550 551 bo = &devvp->v_bufobj; 552 ASSERT_BO_WLOCKED(bo); 553 554 MNT_ILOCK(mp); 555 while (mp->mnt_secondary_writes != 0) { 556 BO_UNLOCK(bo); 557 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 558 (PUSER - 1) | PDROP, "secwr", 0); 559 BO_LOCK(bo); 560 MNT_ILOCK(mp); 561 } 562 563 /* 564 * Reasons for needing more work before suspend: 565 * - Dirty buffers on devvp. 566 * - Secondary writes occurred after start of vnode sync loop 567 */ 568 error = 0; 569 if (bo->bo_numoutput > 0 || 570 bo->bo_dirty.bv_cnt > 0 || 571 secondary_writes != 0 || 572 mp->mnt_secondary_writes != 0 || 573 secondary_accwrites != mp->mnt_secondary_accwrites) 574 error = EAGAIN; 575 BO_UNLOCK(bo); 576 return (error); 577 } 578 579 void 580 softdep_get_depcounts(struct mount *mp, 581 int *softdepactivep, 582 int *softdepactiveaccp) 583 { 584 (void) mp; 585 *softdepactivep = 0; 586 *softdepactiveaccp = 0; 587 } 588 589 void 590 softdep_buf_append(bp, wkhd) 591 struct buf *bp; 592 struct workhead *wkhd; 593 { 594 595 panic("softdep_buf_appendwork called"); 596 } 597 598 void 599 softdep_inode_append(ip, cred, wkhd) 600 struct inode *ip; 601 struct ucred *cred; 602 struct workhead *wkhd; 603 { 604 605 panic("softdep_inode_appendwork called"); 606 } 607 608 void 609 softdep_freework(wkhd) 610 struct workhead *wkhd; 611 { 612 613 panic("softdep_freework called"); 614 } 615 616 #else 617 618 FEATURE(softupdates, "FFS soft-updates support"); 619 620 static SYSCTL_NODE(_debug, OID_AUTO, softdep, CTLFLAG_RW, 0, 621 "soft updates stats"); 622 static SYSCTL_NODE(_debug_softdep, OID_AUTO, total, CTLFLAG_RW, 0, 623 "total dependencies allocated"); 624 static SYSCTL_NODE(_debug_softdep, OID_AUTO, highuse, CTLFLAG_RW, 0, 625 "high use dependencies allocated"); 626 static SYSCTL_NODE(_debug_softdep, OID_AUTO, current, CTLFLAG_RW, 0, 627 "current dependencies allocated"); 628 static SYSCTL_NODE(_debug_softdep, OID_AUTO, write, CTLFLAG_RW, 0, 629 "current dependencies written"); 630 631 unsigned long dep_current[D_LAST + 1]; 632 unsigned long dep_highuse[D_LAST + 1]; 633 unsigned long dep_total[D_LAST + 1]; 634 unsigned long dep_write[D_LAST + 1]; 635 636 #define SOFTDEP_TYPE(type, str, long) \ 637 static MALLOC_DEFINE(M_ ## type, #str, long); \ 638 SYSCTL_ULONG(_debug_softdep_total, OID_AUTO, str, CTLFLAG_RD, \ 639 &dep_total[D_ ## type], 0, ""); \ 640 SYSCTL_ULONG(_debug_softdep_current, OID_AUTO, str, CTLFLAG_RD, \ 641 &dep_current[D_ ## type], 0, ""); \ 642 SYSCTL_ULONG(_debug_softdep_highuse, OID_AUTO, str, CTLFLAG_RD, \ 643 &dep_highuse[D_ ## type], 0, ""); \ 644 SYSCTL_ULONG(_debug_softdep_write, OID_AUTO, str, CTLFLAG_RD, \ 645 &dep_write[D_ ## type], 0, ""); 646 647 SOFTDEP_TYPE(PAGEDEP, pagedep, "File page dependencies"); 648 SOFTDEP_TYPE(INODEDEP, inodedep, "Inode dependencies"); 649 SOFTDEP_TYPE(BMSAFEMAP, bmsafemap, 650 "Block or frag allocated from cyl group map"); 651 SOFTDEP_TYPE(NEWBLK, newblk, "New block or frag allocation dependency"); 652 SOFTDEP_TYPE(ALLOCDIRECT, allocdirect, "Block or frag dependency for an inode"); 653 SOFTDEP_TYPE(INDIRDEP, indirdep, "Indirect block dependencies"); 654 SOFTDEP_TYPE(ALLOCINDIR, allocindir, "Block dependency for an indirect block"); 655 SOFTDEP_TYPE(FREEFRAG, freefrag, "Previously used frag for an inode"); 656 SOFTDEP_TYPE(FREEBLKS, freeblks, "Blocks freed from an inode"); 657 SOFTDEP_TYPE(FREEFILE, freefile, "Inode deallocated"); 658 SOFTDEP_TYPE(DIRADD, diradd, "New directory entry"); 659 SOFTDEP_TYPE(MKDIR, mkdir, "New directory"); 660 SOFTDEP_TYPE(DIRREM, dirrem, "Directory entry deleted"); 661 SOFTDEP_TYPE(NEWDIRBLK, newdirblk, "Unclaimed new directory block"); 662 SOFTDEP_TYPE(FREEWORK, freework, "free an inode block"); 663 SOFTDEP_TYPE(FREEDEP, freedep, "track a block free"); 664 SOFTDEP_TYPE(JADDREF, jaddref, "Journal inode ref add"); 665 SOFTDEP_TYPE(JREMREF, jremref, "Journal inode ref remove"); 666 SOFTDEP_TYPE(JMVREF, jmvref, "Journal inode ref move"); 667 SOFTDEP_TYPE(JNEWBLK, jnewblk, "Journal new block"); 668 SOFTDEP_TYPE(JFREEBLK, jfreeblk, "Journal free block"); 669 SOFTDEP_TYPE(JFREEFRAG, jfreefrag, "Journal free frag"); 670 SOFTDEP_TYPE(JSEG, jseg, "Journal segment"); 671 SOFTDEP_TYPE(JSEGDEP, jsegdep, "Journal segment complete"); 672 SOFTDEP_TYPE(SBDEP, sbdep, "Superblock write dependency"); 673 SOFTDEP_TYPE(JTRUNC, jtrunc, "Journal inode truncation"); 674 SOFTDEP_TYPE(JFSYNC, jfsync, "Journal fsync complete"); 675 676 static MALLOC_DEFINE(M_SENTINEL, "sentinel", "Worklist sentinel"); 677 678 static MALLOC_DEFINE(M_SAVEDINO, "savedino", "Saved inodes"); 679 static MALLOC_DEFINE(M_JBLOCKS, "jblocks", "Journal block locations"); 680 static MALLOC_DEFINE(M_MOUNTDATA, "softdep", "Softdep per-mount data"); 681 682 #define M_SOFTDEP_FLAGS (M_WAITOK) 683 684 /* 685 * translate from workitem type to memory type 686 * MUST match the defines above, such that memtype[D_XXX] == M_XXX 687 */ 688 static struct malloc_type *memtype[] = { 689 M_PAGEDEP, 690 M_INODEDEP, 691 M_BMSAFEMAP, 692 M_NEWBLK, 693 M_ALLOCDIRECT, 694 M_INDIRDEP, 695 M_ALLOCINDIR, 696 M_FREEFRAG, 697 M_FREEBLKS, 698 M_FREEFILE, 699 M_DIRADD, 700 M_MKDIR, 701 M_DIRREM, 702 M_NEWDIRBLK, 703 M_FREEWORK, 704 M_FREEDEP, 705 M_JADDREF, 706 M_JREMREF, 707 M_JMVREF, 708 M_JNEWBLK, 709 M_JFREEBLK, 710 M_JFREEFRAG, 711 M_JSEG, 712 M_JSEGDEP, 713 M_SBDEP, 714 M_JTRUNC, 715 M_JFSYNC, 716 M_SENTINEL 717 }; 718 719 #define DtoM(type) (memtype[type]) 720 721 /* 722 * Names of malloc types. 723 */ 724 #define TYPENAME(type) \ 725 ((unsigned)(type) <= D_LAST ? memtype[type]->ks_shortdesc : "???") 726 /* 727 * End system adaptation definitions. 728 */ 729 730 #define DOTDOT_OFFSET offsetof(struct dirtemplate, dotdot_ino) 731 #define DOT_OFFSET offsetof(struct dirtemplate, dot_ino) 732 733 /* 734 * Internal function prototypes. 735 */ 736 static void check_clear_deps(struct mount *); 737 static void softdep_error(char *, int); 738 static int softdep_process_worklist(struct mount *, int); 739 static int softdep_waitidle(struct mount *, int); 740 static void drain_output(struct vnode *); 741 static struct buf *getdirtybuf(struct buf *, struct rwlock *, int); 742 static int check_inodedep_free(struct inodedep *); 743 static void clear_remove(struct mount *); 744 static void clear_inodedeps(struct mount *); 745 static void unlinked_inodedep(struct mount *, struct inodedep *); 746 static void clear_unlinked_inodedep(struct inodedep *); 747 static struct inodedep *first_unlinked_inodedep(struct ufsmount *); 748 static int flush_pagedep_deps(struct vnode *, struct mount *, 749 struct diraddhd *); 750 static int free_pagedep(struct pagedep *); 751 static int flush_newblk_dep(struct vnode *, struct mount *, ufs_lbn_t); 752 static int flush_inodedep_deps(struct vnode *, struct mount *, ino_t); 753 static int flush_deplist(struct allocdirectlst *, int, int *); 754 static int sync_cgs(struct mount *, int); 755 static int handle_written_filepage(struct pagedep *, struct buf *); 756 static int handle_written_sbdep(struct sbdep *, struct buf *); 757 static void initiate_write_sbdep(struct sbdep *); 758 static void diradd_inode_written(struct diradd *, struct inodedep *); 759 static int handle_written_indirdep(struct indirdep *, struct buf *, 760 struct buf**); 761 static int handle_written_inodeblock(struct inodedep *, struct buf *); 762 static int jnewblk_rollforward(struct jnewblk *, struct fs *, struct cg *, 763 uint8_t *); 764 static int handle_written_bmsafemap(struct bmsafemap *, struct buf *); 765 static void handle_written_jaddref(struct jaddref *); 766 static void handle_written_jremref(struct jremref *); 767 static void handle_written_jseg(struct jseg *, struct buf *); 768 static void handle_written_jnewblk(struct jnewblk *); 769 static void handle_written_jblkdep(struct jblkdep *); 770 static void handle_written_jfreefrag(struct jfreefrag *); 771 static void complete_jseg(struct jseg *); 772 static void complete_jsegs(struct jseg *); 773 static void jseg_write(struct ufsmount *ump, struct jseg *, uint8_t *); 774 static void jaddref_write(struct jaddref *, struct jseg *, uint8_t *); 775 static void jremref_write(struct jremref *, struct jseg *, uint8_t *); 776 static void jmvref_write(struct jmvref *, struct jseg *, uint8_t *); 777 static void jtrunc_write(struct jtrunc *, struct jseg *, uint8_t *); 778 static void jfsync_write(struct jfsync *, struct jseg *, uint8_t *data); 779 static void jnewblk_write(struct jnewblk *, struct jseg *, uint8_t *); 780 static void jfreeblk_write(struct jfreeblk *, struct jseg *, uint8_t *); 781 static void jfreefrag_write(struct jfreefrag *, struct jseg *, uint8_t *); 782 static inline void inoref_write(struct inoref *, struct jseg *, 783 struct jrefrec *); 784 static void handle_allocdirect_partdone(struct allocdirect *, 785 struct workhead *); 786 static struct jnewblk *cancel_newblk(struct newblk *, struct worklist *, 787 struct workhead *); 788 static void indirdep_complete(struct indirdep *); 789 static int indirblk_lookup(struct mount *, ufs2_daddr_t); 790 static void indirblk_insert(struct freework *); 791 static void indirblk_remove(struct freework *); 792 static void handle_allocindir_partdone(struct allocindir *); 793 static void initiate_write_filepage(struct pagedep *, struct buf *); 794 static void initiate_write_indirdep(struct indirdep*, struct buf *); 795 static void handle_written_mkdir(struct mkdir *, int); 796 static int jnewblk_rollback(struct jnewblk *, struct fs *, struct cg *, 797 uint8_t *); 798 static void initiate_write_bmsafemap(struct bmsafemap *, struct buf *); 799 static void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *); 800 static void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *); 801 static void handle_workitem_freefile(struct freefile *); 802 static int handle_workitem_remove(struct dirrem *, int); 803 static struct dirrem *newdirrem(struct buf *, struct inode *, 804 struct inode *, int, struct dirrem **); 805 static struct indirdep *indirdep_lookup(struct mount *, struct inode *, 806 struct buf *); 807 static void cancel_indirdep(struct indirdep *, struct buf *, 808 struct freeblks *); 809 static void free_indirdep(struct indirdep *); 810 static void free_diradd(struct diradd *, struct workhead *); 811 static void merge_diradd(struct inodedep *, struct diradd *); 812 static void complete_diradd(struct diradd *); 813 static struct diradd *diradd_lookup(struct pagedep *, int); 814 static struct jremref *cancel_diradd_dotdot(struct inode *, struct dirrem *, 815 struct jremref *); 816 static struct jremref *cancel_mkdir_dotdot(struct inode *, struct dirrem *, 817 struct jremref *); 818 static void cancel_diradd(struct diradd *, struct dirrem *, struct jremref *, 819 struct jremref *, struct jremref *); 820 static void dirrem_journal(struct dirrem *, struct jremref *, struct jremref *, 821 struct jremref *); 822 static void cancel_allocindir(struct allocindir *, struct buf *bp, 823 struct freeblks *, int); 824 static int setup_trunc_indir(struct freeblks *, struct inode *, 825 ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t); 826 static void complete_trunc_indir(struct freework *); 827 static void trunc_indirdep(struct indirdep *, struct freeblks *, struct buf *, 828 int); 829 static void complete_mkdir(struct mkdir *); 830 static void free_newdirblk(struct newdirblk *); 831 static void free_jremref(struct jremref *); 832 static void free_jaddref(struct jaddref *); 833 static void free_jsegdep(struct jsegdep *); 834 static void free_jsegs(struct jblocks *); 835 static void rele_jseg(struct jseg *); 836 static void free_jseg(struct jseg *, struct jblocks *); 837 static void free_jnewblk(struct jnewblk *); 838 static void free_jblkdep(struct jblkdep *); 839 static void free_jfreefrag(struct jfreefrag *); 840 static void free_freedep(struct freedep *); 841 static void journal_jremref(struct dirrem *, struct jremref *, 842 struct inodedep *); 843 static void cancel_jnewblk(struct jnewblk *, struct workhead *); 844 static int cancel_jaddref(struct jaddref *, struct inodedep *, 845 struct workhead *); 846 static void cancel_jfreefrag(struct jfreefrag *); 847 static inline void setup_freedirect(struct freeblks *, struct inode *, 848 int, int); 849 static inline void setup_freeext(struct freeblks *, struct inode *, int, int); 850 static inline void setup_freeindir(struct freeblks *, struct inode *, int, 851 ufs_lbn_t, int); 852 static inline struct freeblks *newfreeblks(struct mount *, struct inode *); 853 static void freeblks_free(struct ufsmount *, struct freeblks *, int); 854 static void indir_trunc(struct freework *, ufs2_daddr_t, ufs_lbn_t); 855 static ufs2_daddr_t blkcount(struct fs *, ufs2_daddr_t, off_t); 856 static int trunc_check_buf(struct buf *, int *, ufs_lbn_t, int, int); 857 static void trunc_dependencies(struct inode *, struct freeblks *, ufs_lbn_t, 858 int, int); 859 static void trunc_pages(struct inode *, off_t, ufs2_daddr_t, int); 860 static int cancel_pagedep(struct pagedep *, struct freeblks *, int); 861 static int deallocate_dependencies(struct buf *, struct freeblks *, int); 862 static void newblk_freefrag(struct newblk*); 863 static void free_newblk(struct newblk *); 864 static void cancel_allocdirect(struct allocdirectlst *, 865 struct allocdirect *, struct freeblks *); 866 static int check_inode_unwritten(struct inodedep *); 867 static int free_inodedep(struct inodedep *); 868 static void freework_freeblock(struct freework *); 869 static void freework_enqueue(struct freework *); 870 static int handle_workitem_freeblocks(struct freeblks *, int); 871 static int handle_complete_freeblocks(struct freeblks *, int); 872 static void handle_workitem_indirblk(struct freework *); 873 static void handle_written_freework(struct freework *); 874 static void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *); 875 static struct worklist *jnewblk_merge(struct worklist *, struct worklist *, 876 struct workhead *); 877 static struct freefrag *setup_allocindir_phase2(struct buf *, struct inode *, 878 struct inodedep *, struct allocindir *, ufs_lbn_t); 879 static struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t, 880 ufs2_daddr_t, ufs_lbn_t); 881 static void handle_workitem_freefrag(struct freefrag *); 882 static struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long, 883 ufs_lbn_t); 884 static void allocdirect_merge(struct allocdirectlst *, 885 struct allocdirect *, struct allocdirect *); 886 static struct freefrag *allocindir_merge(struct allocindir *, 887 struct allocindir *); 888 static int bmsafemap_find(struct bmsafemap_hashhead *, int, 889 struct bmsafemap **); 890 static struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *, 891 int cg, struct bmsafemap *); 892 static int newblk_find(struct newblk_hashhead *, ufs2_daddr_t, int, 893 struct newblk **); 894 static int newblk_lookup(struct mount *, ufs2_daddr_t, int, struct newblk **); 895 static int inodedep_find(struct inodedep_hashhead *, ino_t, 896 struct inodedep **); 897 static int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **); 898 static int pagedep_lookup(struct mount *, struct buf *bp, ino_t, ufs_lbn_t, 899 int, struct pagedep **); 900 static int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t, 901 struct pagedep **); 902 static void pause_timer(void *); 903 static int request_cleanup(struct mount *, int); 904 static void schedule_cleanup(struct mount *); 905 static void softdep_ast_cleanup_proc(void); 906 static int process_worklist_item(struct mount *, int, int); 907 static void process_removes(struct vnode *); 908 static void process_truncates(struct vnode *); 909 static void jwork_move(struct workhead *, struct workhead *); 910 static void jwork_insert(struct workhead *, struct jsegdep *); 911 static void add_to_worklist(struct worklist *, int); 912 static void wake_worklist(struct worklist *); 913 static void wait_worklist(struct worklist *, char *); 914 static void remove_from_worklist(struct worklist *); 915 static void softdep_flush(void *); 916 static void softdep_flushjournal(struct mount *); 917 static int softdep_speedup(struct ufsmount *); 918 static void worklist_speedup(struct mount *); 919 static int journal_mount(struct mount *, struct fs *, struct ucred *); 920 static void journal_unmount(struct ufsmount *); 921 static int journal_space(struct ufsmount *, int); 922 static void journal_suspend(struct ufsmount *); 923 static int journal_unsuspend(struct ufsmount *ump); 924 static void softdep_prelink(struct vnode *, struct vnode *); 925 static void add_to_journal(struct worklist *); 926 static void remove_from_journal(struct worklist *); 927 static bool softdep_excess_items(struct ufsmount *, int); 928 static void softdep_process_journal(struct mount *, struct worklist *, int); 929 static struct jremref *newjremref(struct dirrem *, struct inode *, 930 struct inode *ip, off_t, nlink_t); 931 static struct jaddref *newjaddref(struct inode *, ino_t, off_t, int16_t, 932 uint16_t); 933 static inline void newinoref(struct inoref *, ino_t, ino_t, off_t, nlink_t, 934 uint16_t); 935 static inline struct jsegdep *inoref_jseg(struct inoref *); 936 static struct jmvref *newjmvref(struct inode *, ino_t, off_t, off_t); 937 static struct jfreeblk *newjfreeblk(struct freeblks *, ufs_lbn_t, 938 ufs2_daddr_t, int); 939 static void adjust_newfreework(struct freeblks *, int); 940 static struct jtrunc *newjtrunc(struct freeblks *, off_t, int); 941 static void move_newblock_dep(struct jaddref *, struct inodedep *); 942 static void cancel_jfreeblk(struct freeblks *, ufs2_daddr_t); 943 static struct jfreefrag *newjfreefrag(struct freefrag *, struct inode *, 944 ufs2_daddr_t, long, ufs_lbn_t); 945 static struct freework *newfreework(struct ufsmount *, struct freeblks *, 946 struct freework *, ufs_lbn_t, ufs2_daddr_t, int, int, int); 947 static int jwait(struct worklist *, int); 948 static struct inodedep *inodedep_lookup_ip(struct inode *); 949 static int bmsafemap_backgroundwrite(struct bmsafemap *, struct buf *); 950 static struct freefile *handle_bufwait(struct inodedep *, struct workhead *); 951 static void handle_jwork(struct workhead *); 952 static struct mkdir *setup_newdir(struct diradd *, ino_t, ino_t, struct buf *, 953 struct mkdir **); 954 static struct jblocks *jblocks_create(void); 955 static ufs2_daddr_t jblocks_alloc(struct jblocks *, int, int *); 956 static void jblocks_free(struct jblocks *, struct mount *, int); 957 static void jblocks_destroy(struct jblocks *); 958 static void jblocks_add(struct jblocks *, ufs2_daddr_t, int); 959 960 /* 961 * Exported softdep operations. 962 */ 963 static void softdep_disk_io_initiation(struct buf *); 964 static void softdep_disk_write_complete(struct buf *); 965 static void softdep_deallocate_dependencies(struct buf *); 966 static int softdep_count_dependencies(struct buf *bp, int); 967 968 /* 969 * Global lock over all of soft updates. 970 */ 971 static struct mtx lk; 972 MTX_SYSINIT(softdep_lock, &lk, "Global Softdep Lock", MTX_DEF); 973 974 #define ACQUIRE_GBLLOCK(lk) mtx_lock(lk) 975 #define FREE_GBLLOCK(lk) mtx_unlock(lk) 976 #define GBLLOCK_OWNED(lk) mtx_assert((lk), MA_OWNED) 977 978 /* 979 * Per-filesystem soft-updates locking. 980 */ 981 #define LOCK_PTR(ump) (&(ump)->um_softdep->sd_fslock) 982 #define TRY_ACQUIRE_LOCK(ump) rw_try_wlock(&(ump)->um_softdep->sd_fslock) 983 #define ACQUIRE_LOCK(ump) rw_wlock(&(ump)->um_softdep->sd_fslock) 984 #define FREE_LOCK(ump) rw_wunlock(&(ump)->um_softdep->sd_fslock) 985 #define LOCK_OWNED(ump) rw_assert(&(ump)->um_softdep->sd_fslock, \ 986 RA_WLOCKED) 987 988 #define BUF_AREC(bp) lockallowrecurse(&(bp)->b_lock) 989 #define BUF_NOREC(bp) lockdisablerecurse(&(bp)->b_lock) 990 991 /* 992 * Worklist queue management. 993 * These routines require that the lock be held. 994 */ 995 #ifndef /* NOT */ DEBUG 996 #define WORKLIST_INSERT(head, item) do { \ 997 (item)->wk_state |= ONWORKLIST; \ 998 LIST_INSERT_HEAD(head, item, wk_list); \ 999 } while (0) 1000 #define WORKLIST_REMOVE(item) do { \ 1001 (item)->wk_state &= ~ONWORKLIST; \ 1002 LIST_REMOVE(item, wk_list); \ 1003 } while (0) 1004 #define WORKLIST_INSERT_UNLOCKED WORKLIST_INSERT 1005 #define WORKLIST_REMOVE_UNLOCKED WORKLIST_REMOVE 1006 1007 #else /* DEBUG */ 1008 static void worklist_insert(struct workhead *, struct worklist *, int); 1009 static void worklist_remove(struct worklist *, int); 1010 1011 #define WORKLIST_INSERT(head, item) worklist_insert(head, item, 1) 1012 #define WORKLIST_INSERT_UNLOCKED(head, item) worklist_insert(head, item, 0) 1013 #define WORKLIST_REMOVE(item) worklist_remove(item, 1) 1014 #define WORKLIST_REMOVE_UNLOCKED(item) worklist_remove(item, 0) 1015 1016 static void 1017 worklist_insert(head, item, locked) 1018 struct workhead *head; 1019 struct worklist *item; 1020 int locked; 1021 { 1022 1023 if (locked) 1024 LOCK_OWNED(VFSTOUFS(item->wk_mp)); 1025 if (item->wk_state & ONWORKLIST) 1026 panic("worklist_insert: %p %s(0x%X) already on list", 1027 item, TYPENAME(item->wk_type), item->wk_state); 1028 item->wk_state |= ONWORKLIST; 1029 LIST_INSERT_HEAD(head, item, wk_list); 1030 } 1031 1032 static void 1033 worklist_remove(item, locked) 1034 struct worklist *item; 1035 int locked; 1036 { 1037 1038 if (locked) 1039 LOCK_OWNED(VFSTOUFS(item->wk_mp)); 1040 if ((item->wk_state & ONWORKLIST) == 0) 1041 panic("worklist_remove: %p %s(0x%X) not on list", 1042 item, TYPENAME(item->wk_type), item->wk_state); 1043 item->wk_state &= ~ONWORKLIST; 1044 LIST_REMOVE(item, wk_list); 1045 } 1046 #endif /* DEBUG */ 1047 1048 /* 1049 * Merge two jsegdeps keeping only the oldest one as newer references 1050 * can't be discarded until after older references. 1051 */ 1052 static inline struct jsegdep * 1053 jsegdep_merge(struct jsegdep *one, struct jsegdep *two) 1054 { 1055 struct jsegdep *swp; 1056 1057 if (two == NULL) 1058 return (one); 1059 1060 if (one->jd_seg->js_seq > two->jd_seg->js_seq) { 1061 swp = one; 1062 one = two; 1063 two = swp; 1064 } 1065 WORKLIST_REMOVE(&two->jd_list); 1066 free_jsegdep(two); 1067 1068 return (one); 1069 } 1070 1071 /* 1072 * If two freedeps are compatible free one to reduce list size. 1073 */ 1074 static inline struct freedep * 1075 freedep_merge(struct freedep *one, struct freedep *two) 1076 { 1077 if (two == NULL) 1078 return (one); 1079 1080 if (one->fd_freework == two->fd_freework) { 1081 WORKLIST_REMOVE(&two->fd_list); 1082 free_freedep(two); 1083 } 1084 return (one); 1085 } 1086 1087 /* 1088 * Move journal work from one list to another. Duplicate freedeps and 1089 * jsegdeps are coalesced to keep the lists as small as possible. 1090 */ 1091 static void 1092 jwork_move(dst, src) 1093 struct workhead *dst; 1094 struct workhead *src; 1095 { 1096 struct freedep *freedep; 1097 struct jsegdep *jsegdep; 1098 struct worklist *wkn; 1099 struct worklist *wk; 1100 1101 KASSERT(dst != src, 1102 ("jwork_move: dst == src")); 1103 freedep = NULL; 1104 jsegdep = NULL; 1105 LIST_FOREACH_SAFE(wk, dst, wk_list, wkn) { 1106 if (wk->wk_type == D_JSEGDEP) 1107 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1108 else if (wk->wk_type == D_FREEDEP) 1109 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1110 } 1111 1112 while ((wk = LIST_FIRST(src)) != NULL) { 1113 WORKLIST_REMOVE(wk); 1114 WORKLIST_INSERT(dst, wk); 1115 if (wk->wk_type == D_JSEGDEP) { 1116 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1117 continue; 1118 } 1119 if (wk->wk_type == D_FREEDEP) 1120 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1121 } 1122 } 1123 1124 static void 1125 jwork_insert(dst, jsegdep) 1126 struct workhead *dst; 1127 struct jsegdep *jsegdep; 1128 { 1129 struct jsegdep *jsegdepn; 1130 struct worklist *wk; 1131 1132 LIST_FOREACH(wk, dst, wk_list) 1133 if (wk->wk_type == D_JSEGDEP) 1134 break; 1135 if (wk == NULL) { 1136 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1137 return; 1138 } 1139 jsegdepn = WK_JSEGDEP(wk); 1140 if (jsegdep->jd_seg->js_seq < jsegdepn->jd_seg->js_seq) { 1141 WORKLIST_REMOVE(wk); 1142 free_jsegdep(jsegdepn); 1143 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1144 } else 1145 free_jsegdep(jsegdep); 1146 } 1147 1148 /* 1149 * Routines for tracking and managing workitems. 1150 */ 1151 static void workitem_free(struct worklist *, int); 1152 static void workitem_alloc(struct worklist *, int, struct mount *); 1153 static void workitem_reassign(struct worklist *, int); 1154 1155 #define WORKITEM_FREE(item, type) \ 1156 workitem_free((struct worklist *)(item), (type)) 1157 #define WORKITEM_REASSIGN(item, type) \ 1158 workitem_reassign((struct worklist *)(item), (type)) 1159 1160 static void 1161 workitem_free(item, type) 1162 struct worklist *item; 1163 int type; 1164 { 1165 struct ufsmount *ump; 1166 1167 #ifdef DEBUG 1168 if (item->wk_state & ONWORKLIST) 1169 panic("workitem_free: %s(0x%X) still on list", 1170 TYPENAME(item->wk_type), item->wk_state); 1171 if (item->wk_type != type && type != D_NEWBLK) 1172 panic("workitem_free: type mismatch %s != %s", 1173 TYPENAME(item->wk_type), TYPENAME(type)); 1174 #endif 1175 if (item->wk_state & IOWAITING) 1176 wakeup(item); 1177 ump = VFSTOUFS(item->wk_mp); 1178 LOCK_OWNED(ump); 1179 KASSERT(ump->softdep_deps > 0, 1180 ("workitem_free: %s: softdep_deps going negative", 1181 ump->um_fs->fs_fsmnt)); 1182 if (--ump->softdep_deps == 0 && ump->softdep_req) 1183 wakeup(&ump->softdep_deps); 1184 KASSERT(dep_current[item->wk_type] > 0, 1185 ("workitem_free: %s: dep_current[%s] going negative", 1186 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1187 KASSERT(ump->softdep_curdeps[item->wk_type] > 0, 1188 ("workitem_free: %s: softdep_curdeps[%s] going negative", 1189 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1190 atomic_subtract_long(&dep_current[item->wk_type], 1); 1191 ump->softdep_curdeps[item->wk_type] -= 1; 1192 free(item, DtoM(type)); 1193 } 1194 1195 static void 1196 workitem_alloc(item, type, mp) 1197 struct worklist *item; 1198 int type; 1199 struct mount *mp; 1200 { 1201 struct ufsmount *ump; 1202 1203 item->wk_type = type; 1204 item->wk_mp = mp; 1205 item->wk_state = 0; 1206 1207 ump = VFSTOUFS(mp); 1208 ACQUIRE_GBLLOCK(&lk); 1209 dep_current[type]++; 1210 if (dep_current[type] > dep_highuse[type]) 1211 dep_highuse[type] = dep_current[type]; 1212 dep_total[type]++; 1213 FREE_GBLLOCK(&lk); 1214 ACQUIRE_LOCK(ump); 1215 ump->softdep_curdeps[type] += 1; 1216 ump->softdep_deps++; 1217 ump->softdep_accdeps++; 1218 FREE_LOCK(ump); 1219 } 1220 1221 static void 1222 workitem_reassign(item, newtype) 1223 struct worklist *item; 1224 int newtype; 1225 { 1226 struct ufsmount *ump; 1227 1228 ump = VFSTOUFS(item->wk_mp); 1229 LOCK_OWNED(ump); 1230 KASSERT(ump->softdep_curdeps[item->wk_type] > 0, 1231 ("workitem_reassign: %s: softdep_curdeps[%s] going negative", 1232 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1233 ump->softdep_curdeps[item->wk_type] -= 1; 1234 ump->softdep_curdeps[newtype] += 1; 1235 KASSERT(dep_current[item->wk_type] > 0, 1236 ("workitem_reassign: %s: dep_current[%s] going negative", 1237 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1238 ACQUIRE_GBLLOCK(&lk); 1239 dep_current[newtype]++; 1240 dep_current[item->wk_type]--; 1241 if (dep_current[newtype] > dep_highuse[newtype]) 1242 dep_highuse[newtype] = dep_current[newtype]; 1243 dep_total[newtype]++; 1244 FREE_GBLLOCK(&lk); 1245 item->wk_type = newtype; 1246 } 1247 1248 /* 1249 * Workitem queue management 1250 */ 1251 static int max_softdeps; /* maximum number of structs before slowdown */ 1252 static int tickdelay = 2; /* number of ticks to pause during slowdown */ 1253 static int proc_waiting; /* tracks whether we have a timeout posted */ 1254 static int *stat_countp; /* statistic to count in proc_waiting timeout */ 1255 static struct callout softdep_callout; 1256 static int req_clear_inodedeps; /* syncer process flush some inodedeps */ 1257 static int req_clear_remove; /* syncer process flush some freeblks */ 1258 static int softdep_flushcache = 0; /* Should we do BIO_FLUSH? */ 1259 1260 /* 1261 * runtime statistics 1262 */ 1263 static int stat_flush_threads; /* number of softdep flushing threads */ 1264 static int stat_worklist_push; /* number of worklist cleanups */ 1265 static int stat_blk_limit_push; /* number of times block limit neared */ 1266 static int stat_ino_limit_push; /* number of times inode limit neared */ 1267 static int stat_blk_limit_hit; /* number of times block slowdown imposed */ 1268 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */ 1269 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */ 1270 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */ 1271 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */ 1272 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */ 1273 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */ 1274 static int stat_jaddref; /* bufs redirtied as ino bitmap can not write */ 1275 static int stat_jnewblk; /* bufs redirtied as blk bitmap can not write */ 1276 static int stat_journal_min; /* Times hit journal min threshold */ 1277 static int stat_journal_low; /* Times hit journal low threshold */ 1278 static int stat_journal_wait; /* Times blocked in jwait(). */ 1279 static int stat_jwait_filepage; /* Times blocked in jwait() for filepage. */ 1280 static int stat_jwait_freeblks; /* Times blocked in jwait() for freeblks. */ 1281 static int stat_jwait_inode; /* Times blocked in jwait() for inodes. */ 1282 static int stat_jwait_newblk; /* Times blocked in jwait() for newblks. */ 1283 static int stat_cleanup_high_delay; /* Maximum cleanup delay (in ticks) */ 1284 static int stat_cleanup_blkrequests; /* Number of block cleanup requests */ 1285 static int stat_cleanup_inorequests; /* Number of inode cleanup requests */ 1286 static int stat_cleanup_retries; /* Number of cleanups that needed to flush */ 1287 static int stat_cleanup_failures; /* Number of cleanup requests that failed */ 1288 static int stat_emptyjblocks; /* Number of potentially empty journal blocks */ 1289 1290 SYSCTL_INT(_debug_softdep, OID_AUTO, max_softdeps, CTLFLAG_RW, 1291 &max_softdeps, 0, ""); 1292 SYSCTL_INT(_debug_softdep, OID_AUTO, tickdelay, CTLFLAG_RW, 1293 &tickdelay, 0, ""); 1294 SYSCTL_INT(_debug_softdep, OID_AUTO, flush_threads, CTLFLAG_RD, 1295 &stat_flush_threads, 0, ""); 1296 SYSCTL_INT(_debug_softdep, OID_AUTO, worklist_push, CTLFLAG_RW, 1297 &stat_worklist_push, 0,""); 1298 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_push, CTLFLAG_RW, 1299 &stat_blk_limit_push, 0,""); 1300 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_push, CTLFLAG_RW, 1301 &stat_ino_limit_push, 0,""); 1302 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_hit, CTLFLAG_RW, 1303 &stat_blk_limit_hit, 0, ""); 1304 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_hit, CTLFLAG_RW, 1305 &stat_ino_limit_hit, 0, ""); 1306 SYSCTL_INT(_debug_softdep, OID_AUTO, sync_limit_hit, CTLFLAG_RW, 1307 &stat_sync_limit_hit, 0, ""); 1308 SYSCTL_INT(_debug_softdep, OID_AUTO, indir_blk_ptrs, CTLFLAG_RW, 1309 &stat_indir_blk_ptrs, 0, ""); 1310 SYSCTL_INT(_debug_softdep, OID_AUTO, inode_bitmap, CTLFLAG_RW, 1311 &stat_inode_bitmap, 0, ""); 1312 SYSCTL_INT(_debug_softdep, OID_AUTO, direct_blk_ptrs, CTLFLAG_RW, 1313 &stat_direct_blk_ptrs, 0, ""); 1314 SYSCTL_INT(_debug_softdep, OID_AUTO, dir_entry, CTLFLAG_RW, 1315 &stat_dir_entry, 0, ""); 1316 SYSCTL_INT(_debug_softdep, OID_AUTO, jaddref_rollback, CTLFLAG_RW, 1317 &stat_jaddref, 0, ""); 1318 SYSCTL_INT(_debug_softdep, OID_AUTO, jnewblk_rollback, CTLFLAG_RW, 1319 &stat_jnewblk, 0, ""); 1320 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_low, CTLFLAG_RW, 1321 &stat_journal_low, 0, ""); 1322 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_min, CTLFLAG_RW, 1323 &stat_journal_min, 0, ""); 1324 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_wait, CTLFLAG_RW, 1325 &stat_journal_wait, 0, ""); 1326 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_filepage, CTLFLAG_RW, 1327 &stat_jwait_filepage, 0, ""); 1328 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_freeblks, CTLFLAG_RW, 1329 &stat_jwait_freeblks, 0, ""); 1330 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_inode, CTLFLAG_RW, 1331 &stat_jwait_inode, 0, ""); 1332 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_newblk, CTLFLAG_RW, 1333 &stat_jwait_newblk, 0, ""); 1334 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_blkrequests, CTLFLAG_RW, 1335 &stat_cleanup_blkrequests, 0, ""); 1336 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_inorequests, CTLFLAG_RW, 1337 &stat_cleanup_inorequests, 0, ""); 1338 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_high_delay, CTLFLAG_RW, 1339 &stat_cleanup_high_delay, 0, ""); 1340 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_retries, CTLFLAG_RW, 1341 &stat_cleanup_retries, 0, ""); 1342 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_failures, CTLFLAG_RW, 1343 &stat_cleanup_failures, 0, ""); 1344 SYSCTL_INT(_debug_softdep, OID_AUTO, flushcache, CTLFLAG_RW, 1345 &softdep_flushcache, 0, ""); 1346 SYSCTL_INT(_debug_softdep, OID_AUTO, emptyjblocks, CTLFLAG_RD, 1347 &stat_emptyjblocks, 0, ""); 1348 1349 SYSCTL_DECL(_vfs_ffs); 1350 1351 /* Whether to recompute the summary at mount time */ 1352 static int compute_summary_at_mount = 0; 1353 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW, 1354 &compute_summary_at_mount, 0, "Recompute summary at mount"); 1355 static int print_threads = 0; 1356 SYSCTL_INT(_debug_softdep, OID_AUTO, print_threads, CTLFLAG_RW, 1357 &print_threads, 0, "Notify flusher thread start/stop"); 1358 1359 /* List of all filesystems mounted with soft updates */ 1360 static TAILQ_HEAD(, mount_softdeps) softdepmounts; 1361 1362 /* 1363 * This function cleans the worklist for a filesystem. 1364 * Each filesystem running with soft dependencies gets its own 1365 * thread to run in this function. The thread is started up in 1366 * softdep_mount and shutdown in softdep_unmount. They show up 1367 * as part of the kernel "bufdaemon" process whose process 1368 * entry is available in bufdaemonproc. 1369 */ 1370 static int searchfailed; 1371 extern struct proc *bufdaemonproc; 1372 static void 1373 softdep_flush(addr) 1374 void *addr; 1375 { 1376 struct mount *mp; 1377 struct thread *td; 1378 struct ufsmount *ump; 1379 1380 td = curthread; 1381 td->td_pflags |= TDP_NORUNNINGBUF; 1382 mp = (struct mount *)addr; 1383 ump = VFSTOUFS(mp); 1384 atomic_add_int(&stat_flush_threads, 1); 1385 ACQUIRE_LOCK(ump); 1386 ump->softdep_flags &= ~FLUSH_STARTING; 1387 wakeup(&ump->softdep_flushtd); 1388 FREE_LOCK(ump); 1389 if (print_threads) { 1390 if (stat_flush_threads == 1) 1391 printf("Running %s at pid %d\n", bufdaemonproc->p_comm, 1392 bufdaemonproc->p_pid); 1393 printf("Start thread %s\n", td->td_name); 1394 } 1395 for (;;) { 1396 while (softdep_process_worklist(mp, 0) > 0 || 1397 (MOUNTEDSUJ(mp) && 1398 VFSTOUFS(mp)->softdep_jblocks->jb_suspended)) 1399 kthread_suspend_check(); 1400 ACQUIRE_LOCK(ump); 1401 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1402 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, 1403 "sdflush", hz / 2); 1404 ump->softdep_flags &= ~FLUSH_CLEANUP; 1405 /* 1406 * Check to see if we are done and need to exit. 1407 */ 1408 if ((ump->softdep_flags & FLUSH_EXIT) == 0) { 1409 FREE_LOCK(ump); 1410 continue; 1411 } 1412 ump->softdep_flags &= ~FLUSH_EXIT; 1413 FREE_LOCK(ump); 1414 wakeup(&ump->softdep_flags); 1415 if (print_threads) 1416 printf("Stop thread %s: searchfailed %d, did cleanups %d\n", td->td_name, searchfailed, ump->um_softdep->sd_cleanups); 1417 atomic_subtract_int(&stat_flush_threads, 1); 1418 kthread_exit(); 1419 panic("kthread_exit failed\n"); 1420 } 1421 } 1422 1423 static void 1424 worklist_speedup(mp) 1425 struct mount *mp; 1426 { 1427 struct ufsmount *ump; 1428 1429 ump = VFSTOUFS(mp); 1430 LOCK_OWNED(ump); 1431 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1432 ump->softdep_flags |= FLUSH_CLEANUP; 1433 wakeup(&ump->softdep_flushtd); 1434 } 1435 1436 static int 1437 softdep_speedup(ump) 1438 struct ufsmount *ump; 1439 { 1440 struct ufsmount *altump; 1441 struct mount_softdeps *sdp; 1442 1443 LOCK_OWNED(ump); 1444 worklist_speedup(ump->um_mountp); 1445 bd_speedup(); 1446 /* 1447 * If we have global shortages, then we need other 1448 * filesystems to help with the cleanup. Here we wakeup a 1449 * flusher thread for a filesystem that is over its fair 1450 * share of resources. 1451 */ 1452 if (req_clear_inodedeps || req_clear_remove) { 1453 ACQUIRE_GBLLOCK(&lk); 1454 TAILQ_FOREACH(sdp, &softdepmounts, sd_next) { 1455 if ((altump = sdp->sd_ump) == ump) 1456 continue; 1457 if (((req_clear_inodedeps && 1458 altump->softdep_curdeps[D_INODEDEP] > 1459 max_softdeps / stat_flush_threads) || 1460 (req_clear_remove && 1461 altump->softdep_curdeps[D_DIRREM] > 1462 (max_softdeps / 2) / stat_flush_threads)) && 1463 TRY_ACQUIRE_LOCK(altump)) 1464 break; 1465 } 1466 if (sdp == NULL) { 1467 searchfailed++; 1468 FREE_GBLLOCK(&lk); 1469 } else { 1470 /* 1471 * Move to the end of the list so we pick a 1472 * different one on out next try. 1473 */ 1474 TAILQ_REMOVE(&softdepmounts, sdp, sd_next); 1475 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next); 1476 FREE_GBLLOCK(&lk); 1477 if ((altump->softdep_flags & 1478 (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1479 altump->softdep_flags |= FLUSH_CLEANUP; 1480 altump->um_softdep->sd_cleanups++; 1481 wakeup(&altump->softdep_flushtd); 1482 FREE_LOCK(altump); 1483 } 1484 } 1485 return (speedup_syncer()); 1486 } 1487 1488 /* 1489 * Add an item to the end of the work queue. 1490 * This routine requires that the lock be held. 1491 * This is the only routine that adds items to the list. 1492 * The following routine is the only one that removes items 1493 * and does so in order from first to last. 1494 */ 1495 1496 #define WK_HEAD 0x0001 /* Add to HEAD. */ 1497 #define WK_NODELAY 0x0002 /* Process immediately. */ 1498 1499 static void 1500 add_to_worklist(wk, flags) 1501 struct worklist *wk; 1502 int flags; 1503 { 1504 struct ufsmount *ump; 1505 1506 ump = VFSTOUFS(wk->wk_mp); 1507 LOCK_OWNED(ump); 1508 if (wk->wk_state & ONWORKLIST) 1509 panic("add_to_worklist: %s(0x%X) already on list", 1510 TYPENAME(wk->wk_type), wk->wk_state); 1511 wk->wk_state |= ONWORKLIST; 1512 if (ump->softdep_on_worklist == 0) { 1513 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1514 ump->softdep_worklist_tail = wk; 1515 } else if (flags & WK_HEAD) { 1516 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1517 } else { 1518 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list); 1519 ump->softdep_worklist_tail = wk; 1520 } 1521 ump->softdep_on_worklist += 1; 1522 if (flags & WK_NODELAY) 1523 worklist_speedup(wk->wk_mp); 1524 } 1525 1526 /* 1527 * Remove the item to be processed. If we are removing the last 1528 * item on the list, we need to recalculate the tail pointer. 1529 */ 1530 static void 1531 remove_from_worklist(wk) 1532 struct worklist *wk; 1533 { 1534 struct ufsmount *ump; 1535 1536 ump = VFSTOUFS(wk->wk_mp); 1537 WORKLIST_REMOVE(wk); 1538 if (ump->softdep_worklist_tail == wk) 1539 ump->softdep_worklist_tail = 1540 (struct worklist *)wk->wk_list.le_prev; 1541 ump->softdep_on_worklist -= 1; 1542 } 1543 1544 static void 1545 wake_worklist(wk) 1546 struct worklist *wk; 1547 { 1548 if (wk->wk_state & IOWAITING) { 1549 wk->wk_state &= ~IOWAITING; 1550 wakeup(wk); 1551 } 1552 } 1553 1554 static void 1555 wait_worklist(wk, wmesg) 1556 struct worklist *wk; 1557 char *wmesg; 1558 { 1559 struct ufsmount *ump; 1560 1561 ump = VFSTOUFS(wk->wk_mp); 1562 wk->wk_state |= IOWAITING; 1563 msleep(wk, LOCK_PTR(ump), PVM, wmesg, 0); 1564 } 1565 1566 /* 1567 * Process that runs once per second to handle items in the background queue. 1568 * 1569 * Note that we ensure that everything is done in the order in which they 1570 * appear in the queue. The code below depends on this property to ensure 1571 * that blocks of a file are freed before the inode itself is freed. This 1572 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated 1573 * until all the old ones have been purged from the dependency lists. 1574 */ 1575 static int 1576 softdep_process_worklist(mp, full) 1577 struct mount *mp; 1578 int full; 1579 { 1580 int cnt, matchcnt; 1581 struct ufsmount *ump; 1582 long starttime; 1583 1584 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp")); 1585 if (MOUNTEDSOFTDEP(mp) == 0) 1586 return (0); 1587 matchcnt = 0; 1588 ump = VFSTOUFS(mp); 1589 ACQUIRE_LOCK(ump); 1590 starttime = time_second; 1591 softdep_process_journal(mp, NULL, full ? MNT_WAIT : 0); 1592 check_clear_deps(mp); 1593 while (ump->softdep_on_worklist > 0) { 1594 if ((cnt = process_worklist_item(mp, 10, LK_NOWAIT)) == 0) 1595 break; 1596 else 1597 matchcnt += cnt; 1598 check_clear_deps(mp); 1599 /* 1600 * We do not generally want to stop for buffer space, but if 1601 * we are really being a buffer hog, we will stop and wait. 1602 */ 1603 if (should_yield()) { 1604 FREE_LOCK(ump); 1605 kern_yield(PRI_USER); 1606 bwillwrite(); 1607 ACQUIRE_LOCK(ump); 1608 } 1609 /* 1610 * Never allow processing to run for more than one 1611 * second. This gives the syncer thread the opportunity 1612 * to pause if appropriate. 1613 */ 1614 if (!full && starttime != time_second) 1615 break; 1616 } 1617 if (full == 0) 1618 journal_unsuspend(ump); 1619 FREE_LOCK(ump); 1620 return (matchcnt); 1621 } 1622 1623 /* 1624 * Process all removes associated with a vnode if we are running out of 1625 * journal space. Any other process which attempts to flush these will 1626 * be unable as we have the vnodes locked. 1627 */ 1628 static void 1629 process_removes(vp) 1630 struct vnode *vp; 1631 { 1632 struct inodedep *inodedep; 1633 struct dirrem *dirrem; 1634 struct ufsmount *ump; 1635 struct mount *mp; 1636 ino_t inum; 1637 1638 mp = vp->v_mount; 1639 ump = VFSTOUFS(mp); 1640 LOCK_OWNED(ump); 1641 inum = VTOI(vp)->i_number; 1642 for (;;) { 1643 top: 1644 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1645 return; 1646 LIST_FOREACH(dirrem, &inodedep->id_dirremhd, dm_inonext) { 1647 /* 1648 * If another thread is trying to lock this vnode 1649 * it will fail but we must wait for it to do so 1650 * before we can proceed. 1651 */ 1652 if (dirrem->dm_state & INPROGRESS) { 1653 wait_worklist(&dirrem->dm_list, "pwrwait"); 1654 goto top; 1655 } 1656 if ((dirrem->dm_state & (COMPLETE | ONWORKLIST)) == 1657 (COMPLETE | ONWORKLIST)) 1658 break; 1659 } 1660 if (dirrem == NULL) 1661 return; 1662 remove_from_worklist(&dirrem->dm_list); 1663 FREE_LOCK(ump); 1664 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1665 panic("process_removes: suspended filesystem"); 1666 handle_workitem_remove(dirrem, 0); 1667 vn_finished_secondary_write(mp); 1668 ACQUIRE_LOCK(ump); 1669 } 1670 } 1671 1672 /* 1673 * Process all truncations associated with a vnode if we are running out 1674 * of journal space. This is called when the vnode lock is already held 1675 * and no other process can clear the truncation. This function returns 1676 * a value greater than zero if it did any work. 1677 */ 1678 static void 1679 process_truncates(vp) 1680 struct vnode *vp; 1681 { 1682 struct inodedep *inodedep; 1683 struct freeblks *freeblks; 1684 struct ufsmount *ump; 1685 struct mount *mp; 1686 ino_t inum; 1687 int cgwait; 1688 1689 mp = vp->v_mount; 1690 ump = VFSTOUFS(mp); 1691 LOCK_OWNED(ump); 1692 inum = VTOI(vp)->i_number; 1693 for (;;) { 1694 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1695 return; 1696 cgwait = 0; 1697 TAILQ_FOREACH(freeblks, &inodedep->id_freeblklst, fb_next) { 1698 /* Journal entries not yet written. */ 1699 if (!LIST_EMPTY(&freeblks->fb_jblkdephd)) { 1700 jwait(&LIST_FIRST( 1701 &freeblks->fb_jblkdephd)->jb_list, 1702 MNT_WAIT); 1703 break; 1704 } 1705 /* Another thread is executing this item. */ 1706 if (freeblks->fb_state & INPROGRESS) { 1707 wait_worklist(&freeblks->fb_list, "ptrwait"); 1708 break; 1709 } 1710 /* Freeblks is waiting on a inode write. */ 1711 if ((freeblks->fb_state & COMPLETE) == 0) { 1712 FREE_LOCK(ump); 1713 ffs_update(vp, 1); 1714 ACQUIRE_LOCK(ump); 1715 break; 1716 } 1717 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST)) == 1718 (ALLCOMPLETE | ONWORKLIST)) { 1719 remove_from_worklist(&freeblks->fb_list); 1720 freeblks->fb_state |= INPROGRESS; 1721 FREE_LOCK(ump); 1722 if (vn_start_secondary_write(NULL, &mp, 1723 V_NOWAIT)) 1724 panic("process_truncates: " 1725 "suspended filesystem"); 1726 handle_workitem_freeblocks(freeblks, 0); 1727 vn_finished_secondary_write(mp); 1728 ACQUIRE_LOCK(ump); 1729 break; 1730 } 1731 if (freeblks->fb_cgwait) 1732 cgwait++; 1733 } 1734 if (cgwait) { 1735 FREE_LOCK(ump); 1736 sync_cgs(mp, MNT_WAIT); 1737 ffs_sync_snap(mp, MNT_WAIT); 1738 ACQUIRE_LOCK(ump); 1739 continue; 1740 } 1741 if (freeblks == NULL) 1742 break; 1743 } 1744 return; 1745 } 1746 1747 /* 1748 * Process one item on the worklist. 1749 */ 1750 static int 1751 process_worklist_item(mp, target, flags) 1752 struct mount *mp; 1753 int target; 1754 int flags; 1755 { 1756 struct worklist sentinel; 1757 struct worklist *wk; 1758 struct ufsmount *ump; 1759 int matchcnt; 1760 int error; 1761 1762 KASSERT(mp != NULL, ("process_worklist_item: NULL mp")); 1763 /* 1764 * If we are being called because of a process doing a 1765 * copy-on-write, then it is not safe to write as we may 1766 * recurse into the copy-on-write routine. 1767 */ 1768 if (curthread->td_pflags & TDP_COWINPROGRESS) 1769 return (-1); 1770 PHOLD(curproc); /* Don't let the stack go away. */ 1771 ump = VFSTOUFS(mp); 1772 LOCK_OWNED(ump); 1773 matchcnt = 0; 1774 sentinel.wk_mp = NULL; 1775 sentinel.wk_type = D_SENTINEL; 1776 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, &sentinel, wk_list); 1777 for (wk = LIST_NEXT(&sentinel, wk_list); wk != NULL; 1778 wk = LIST_NEXT(&sentinel, wk_list)) { 1779 if (wk->wk_type == D_SENTINEL) { 1780 LIST_REMOVE(&sentinel, wk_list); 1781 LIST_INSERT_AFTER(wk, &sentinel, wk_list); 1782 continue; 1783 } 1784 if (wk->wk_state & INPROGRESS) 1785 panic("process_worklist_item: %p already in progress.", 1786 wk); 1787 wk->wk_state |= INPROGRESS; 1788 remove_from_worklist(wk); 1789 FREE_LOCK(ump); 1790 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1791 panic("process_worklist_item: suspended filesystem"); 1792 switch (wk->wk_type) { 1793 case D_DIRREM: 1794 /* removal of a directory entry */ 1795 error = handle_workitem_remove(WK_DIRREM(wk), flags); 1796 break; 1797 1798 case D_FREEBLKS: 1799 /* releasing blocks and/or fragments from a file */ 1800 error = handle_workitem_freeblocks(WK_FREEBLKS(wk), 1801 flags); 1802 break; 1803 1804 case D_FREEFRAG: 1805 /* releasing a fragment when replaced as a file grows */ 1806 handle_workitem_freefrag(WK_FREEFRAG(wk)); 1807 error = 0; 1808 break; 1809 1810 case D_FREEFILE: 1811 /* releasing an inode when its link count drops to 0 */ 1812 handle_workitem_freefile(WK_FREEFILE(wk)); 1813 error = 0; 1814 break; 1815 1816 default: 1817 panic("%s_process_worklist: Unknown type %s", 1818 "softdep", TYPENAME(wk->wk_type)); 1819 /* NOTREACHED */ 1820 } 1821 vn_finished_secondary_write(mp); 1822 ACQUIRE_LOCK(ump); 1823 if (error == 0) { 1824 if (++matchcnt == target) 1825 break; 1826 continue; 1827 } 1828 /* 1829 * We have to retry the worklist item later. Wake up any 1830 * waiters who may be able to complete it immediately and 1831 * add the item back to the head so we don't try to execute 1832 * it again. 1833 */ 1834 wk->wk_state &= ~INPROGRESS; 1835 wake_worklist(wk); 1836 add_to_worklist(wk, WK_HEAD); 1837 } 1838 LIST_REMOVE(&sentinel, wk_list); 1839 /* Sentinal could've become the tail from remove_from_worklist. */ 1840 if (ump->softdep_worklist_tail == &sentinel) 1841 ump->softdep_worklist_tail = 1842 (struct worklist *)sentinel.wk_list.le_prev; 1843 PRELE(curproc); 1844 return (matchcnt); 1845 } 1846 1847 /* 1848 * Move dependencies from one buffer to another. 1849 */ 1850 int 1851 softdep_move_dependencies(oldbp, newbp) 1852 struct buf *oldbp; 1853 struct buf *newbp; 1854 { 1855 struct worklist *wk, *wktail; 1856 struct ufsmount *ump; 1857 int dirty; 1858 1859 if ((wk = LIST_FIRST(&oldbp->b_dep)) == NULL) 1860 return (0); 1861 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 1862 ("softdep_move_dependencies called on non-softdep filesystem")); 1863 dirty = 0; 1864 wktail = NULL; 1865 ump = VFSTOUFS(wk->wk_mp); 1866 ACQUIRE_LOCK(ump); 1867 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) { 1868 LIST_REMOVE(wk, wk_list); 1869 if (wk->wk_type == D_BMSAFEMAP && 1870 bmsafemap_backgroundwrite(WK_BMSAFEMAP(wk), newbp)) 1871 dirty = 1; 1872 if (wktail == NULL) 1873 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list); 1874 else 1875 LIST_INSERT_AFTER(wktail, wk, wk_list); 1876 wktail = wk; 1877 } 1878 FREE_LOCK(ump); 1879 1880 return (dirty); 1881 } 1882 1883 /* 1884 * Purge the work list of all items associated with a particular mount point. 1885 */ 1886 int 1887 softdep_flushworklist(oldmnt, countp, td) 1888 struct mount *oldmnt; 1889 int *countp; 1890 struct thread *td; 1891 { 1892 struct vnode *devvp; 1893 struct ufsmount *ump; 1894 int count, error; 1895 1896 /* 1897 * Alternately flush the block device associated with the mount 1898 * point and process any dependencies that the flushing 1899 * creates. We continue until no more worklist dependencies 1900 * are found. 1901 */ 1902 *countp = 0; 1903 error = 0; 1904 ump = VFSTOUFS(oldmnt); 1905 devvp = ump->um_devvp; 1906 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) { 1907 *countp += count; 1908 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1909 error = VOP_FSYNC(devvp, MNT_WAIT, td); 1910 VOP_UNLOCK(devvp, 0); 1911 if (error != 0) 1912 break; 1913 } 1914 return (error); 1915 } 1916 1917 #define SU_WAITIDLE_RETRIES 20 1918 static int 1919 softdep_waitidle(struct mount *mp, int flags __unused) 1920 { 1921 struct ufsmount *ump; 1922 struct vnode *devvp; 1923 struct thread *td; 1924 int error, i; 1925 1926 ump = VFSTOUFS(mp); 1927 devvp = ump->um_devvp; 1928 td = curthread; 1929 error = 0; 1930 ACQUIRE_LOCK(ump); 1931 for (i = 0; i < SU_WAITIDLE_RETRIES && ump->softdep_deps != 0; i++) { 1932 ump->softdep_req = 1; 1933 KASSERT((flags & FORCECLOSE) == 0 || 1934 ump->softdep_on_worklist == 0, 1935 ("softdep_waitidle: work added after flush")); 1936 msleep(&ump->softdep_deps, LOCK_PTR(ump), PVM | PDROP, 1937 "softdeps", 10 * hz); 1938 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1939 error = VOP_FSYNC(devvp, MNT_WAIT, td); 1940 VOP_UNLOCK(devvp, 0); 1941 ACQUIRE_LOCK(ump); 1942 if (error != 0) 1943 break; 1944 } 1945 ump->softdep_req = 0; 1946 if (i == SU_WAITIDLE_RETRIES && error == 0 && ump->softdep_deps != 0) { 1947 error = EBUSY; 1948 printf("softdep_waitidle: Failed to flush worklist for %p\n", 1949 mp); 1950 } 1951 FREE_LOCK(ump); 1952 return (error); 1953 } 1954 1955 /* 1956 * Flush all vnodes and worklist items associated with a specified mount point. 1957 */ 1958 int 1959 softdep_flushfiles(oldmnt, flags, td) 1960 struct mount *oldmnt; 1961 int flags; 1962 struct thread *td; 1963 { 1964 #ifdef QUOTA 1965 struct ufsmount *ump; 1966 int i; 1967 #endif 1968 int error, early, depcount, loopcnt, retry_flush_count, retry; 1969 int morework; 1970 1971 KASSERT(MOUNTEDSOFTDEP(oldmnt) != 0, 1972 ("softdep_flushfiles called on non-softdep filesystem")); 1973 loopcnt = 10; 1974 retry_flush_count = 3; 1975 retry_flush: 1976 error = 0; 1977 1978 /* 1979 * Alternately flush the vnodes associated with the mount 1980 * point and process any dependencies that the flushing 1981 * creates. In theory, this loop can happen at most twice, 1982 * but we give it a few extra just to be sure. 1983 */ 1984 for (; loopcnt > 0; loopcnt--) { 1985 /* 1986 * Do another flush in case any vnodes were brought in 1987 * as part of the cleanup operations. 1988 */ 1989 early = retry_flush_count == 1 || (oldmnt->mnt_kern_flag & 1990 MNTK_UNMOUNT) == 0 ? 0 : EARLYFLUSH; 1991 if ((error = ffs_flushfiles(oldmnt, flags | early, td)) != 0) 1992 break; 1993 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 || 1994 depcount == 0) 1995 break; 1996 } 1997 /* 1998 * If we are unmounting then it is an error to fail. If we 1999 * are simply trying to downgrade to read-only, then filesystem 2000 * activity can keep us busy forever, so we just fail with EBUSY. 2001 */ 2002 if (loopcnt == 0) { 2003 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) 2004 panic("softdep_flushfiles: looping"); 2005 error = EBUSY; 2006 } 2007 if (!error) 2008 error = softdep_waitidle(oldmnt, flags); 2009 if (!error) { 2010 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) { 2011 retry = 0; 2012 MNT_ILOCK(oldmnt); 2013 KASSERT((oldmnt->mnt_kern_flag & MNTK_NOINSMNTQ) != 0, 2014 ("softdep_flushfiles: !MNTK_NOINSMNTQ")); 2015 morework = oldmnt->mnt_nvnodelistsize > 0; 2016 #ifdef QUOTA 2017 ump = VFSTOUFS(oldmnt); 2018 UFS_LOCK(ump); 2019 for (i = 0; i < MAXQUOTAS; i++) { 2020 if (ump->um_quotas[i] != NULLVP) 2021 morework = 1; 2022 } 2023 UFS_UNLOCK(ump); 2024 #endif 2025 if (morework) { 2026 if (--retry_flush_count > 0) { 2027 retry = 1; 2028 loopcnt = 3; 2029 } else 2030 error = EBUSY; 2031 } 2032 MNT_IUNLOCK(oldmnt); 2033 if (retry) 2034 goto retry_flush; 2035 } 2036 } 2037 return (error); 2038 } 2039 2040 /* 2041 * Structure hashing. 2042 * 2043 * There are four types of structures that can be looked up: 2044 * 1) pagedep structures identified by mount point, inode number, 2045 * and logical block. 2046 * 2) inodedep structures identified by mount point and inode number. 2047 * 3) newblk structures identified by mount point and 2048 * physical block number. 2049 * 4) bmsafemap structures identified by mount point and 2050 * cylinder group number. 2051 * 2052 * The "pagedep" and "inodedep" dependency structures are hashed 2053 * separately from the file blocks and inodes to which they correspond. 2054 * This separation helps when the in-memory copy of an inode or 2055 * file block must be replaced. It also obviates the need to access 2056 * an inode or file page when simply updating (or de-allocating) 2057 * dependency structures. Lookup of newblk structures is needed to 2058 * find newly allocated blocks when trying to associate them with 2059 * their allocdirect or allocindir structure. 2060 * 2061 * The lookup routines optionally create and hash a new instance when 2062 * an existing entry is not found. The bmsafemap lookup routine always 2063 * allocates a new structure if an existing one is not found. 2064 */ 2065 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */ 2066 2067 /* 2068 * Structures and routines associated with pagedep caching. 2069 */ 2070 #define PAGEDEP_HASH(ump, inum, lbn) \ 2071 (&(ump)->pagedep_hashtbl[((inum) + (lbn)) & (ump)->pagedep_hash_size]) 2072 2073 static int 2074 pagedep_find(pagedephd, ino, lbn, pagedeppp) 2075 struct pagedep_hashhead *pagedephd; 2076 ino_t ino; 2077 ufs_lbn_t lbn; 2078 struct pagedep **pagedeppp; 2079 { 2080 struct pagedep *pagedep; 2081 2082 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 2083 if (ino == pagedep->pd_ino && lbn == pagedep->pd_lbn) { 2084 *pagedeppp = pagedep; 2085 return (1); 2086 } 2087 } 2088 *pagedeppp = NULL; 2089 return (0); 2090 } 2091 /* 2092 * Look up a pagedep. Return 1 if found, 0 otherwise. 2093 * If not found, allocate if DEPALLOC flag is passed. 2094 * Found or allocated entry is returned in pagedeppp. 2095 * This routine must be called with splbio interrupts blocked. 2096 */ 2097 static int 2098 pagedep_lookup(mp, bp, ino, lbn, flags, pagedeppp) 2099 struct mount *mp; 2100 struct buf *bp; 2101 ino_t ino; 2102 ufs_lbn_t lbn; 2103 int flags; 2104 struct pagedep **pagedeppp; 2105 { 2106 struct pagedep *pagedep; 2107 struct pagedep_hashhead *pagedephd; 2108 struct worklist *wk; 2109 struct ufsmount *ump; 2110 int ret; 2111 int i; 2112 2113 ump = VFSTOUFS(mp); 2114 LOCK_OWNED(ump); 2115 if (bp) { 2116 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 2117 if (wk->wk_type == D_PAGEDEP) { 2118 *pagedeppp = WK_PAGEDEP(wk); 2119 return (1); 2120 } 2121 } 2122 } 2123 pagedephd = PAGEDEP_HASH(ump, ino, lbn); 2124 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp); 2125 if (ret) { 2126 if (((*pagedeppp)->pd_state & ONWORKLIST) == 0 && bp) 2127 WORKLIST_INSERT(&bp->b_dep, &(*pagedeppp)->pd_list); 2128 return (1); 2129 } 2130 if ((flags & DEPALLOC) == 0) 2131 return (0); 2132 FREE_LOCK(ump); 2133 pagedep = malloc(sizeof(struct pagedep), 2134 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO); 2135 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp); 2136 ACQUIRE_LOCK(ump); 2137 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp); 2138 if (*pagedeppp) { 2139 /* 2140 * This should never happen since we only create pagedeps 2141 * with the vnode lock held. Could be an assert. 2142 */ 2143 WORKITEM_FREE(pagedep, D_PAGEDEP); 2144 return (ret); 2145 } 2146 pagedep->pd_ino = ino; 2147 pagedep->pd_lbn = lbn; 2148 LIST_INIT(&pagedep->pd_dirremhd); 2149 LIST_INIT(&pagedep->pd_pendinghd); 2150 for (i = 0; i < DAHASHSZ; i++) 2151 LIST_INIT(&pagedep->pd_diraddhd[i]); 2152 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash); 2153 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list); 2154 *pagedeppp = pagedep; 2155 return (0); 2156 } 2157 2158 /* 2159 * Structures and routines associated with inodedep caching. 2160 */ 2161 #define INODEDEP_HASH(ump, inum) \ 2162 (&(ump)->inodedep_hashtbl[(inum) & (ump)->inodedep_hash_size]) 2163 2164 static int 2165 inodedep_find(inodedephd, inum, inodedeppp) 2166 struct inodedep_hashhead *inodedephd; 2167 ino_t inum; 2168 struct inodedep **inodedeppp; 2169 { 2170 struct inodedep *inodedep; 2171 2172 LIST_FOREACH(inodedep, inodedephd, id_hash) 2173 if (inum == inodedep->id_ino) 2174 break; 2175 if (inodedep) { 2176 *inodedeppp = inodedep; 2177 return (1); 2178 } 2179 *inodedeppp = NULL; 2180 2181 return (0); 2182 } 2183 /* 2184 * Look up an inodedep. Return 1 if found, 0 if not found. 2185 * If not found, allocate if DEPALLOC flag is passed. 2186 * Found or allocated entry is returned in inodedeppp. 2187 * This routine must be called with splbio interrupts blocked. 2188 */ 2189 static int 2190 inodedep_lookup(mp, inum, flags, inodedeppp) 2191 struct mount *mp; 2192 ino_t inum; 2193 int flags; 2194 struct inodedep **inodedeppp; 2195 { 2196 struct inodedep *inodedep; 2197 struct inodedep_hashhead *inodedephd; 2198 struct ufsmount *ump; 2199 struct fs *fs; 2200 2201 ump = VFSTOUFS(mp); 2202 LOCK_OWNED(ump); 2203 fs = ump->um_fs; 2204 inodedephd = INODEDEP_HASH(ump, inum); 2205 2206 if (inodedep_find(inodedephd, inum, inodedeppp)) 2207 return (1); 2208 if ((flags & DEPALLOC) == 0) 2209 return (0); 2210 /* 2211 * If the system is over its limit and our filesystem is 2212 * responsible for more than our share of that usage and 2213 * we are not in a rush, request some inodedep cleanup. 2214 */ 2215 if (softdep_excess_items(ump, D_INODEDEP)) 2216 schedule_cleanup(mp); 2217 else 2218 FREE_LOCK(ump); 2219 inodedep = malloc(sizeof(struct inodedep), 2220 M_INODEDEP, M_SOFTDEP_FLAGS); 2221 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp); 2222 ACQUIRE_LOCK(ump); 2223 if (inodedep_find(inodedephd, inum, inodedeppp)) { 2224 WORKITEM_FREE(inodedep, D_INODEDEP); 2225 return (1); 2226 } 2227 inodedep->id_fs = fs; 2228 inodedep->id_ino = inum; 2229 inodedep->id_state = ALLCOMPLETE; 2230 inodedep->id_nlinkdelta = 0; 2231 inodedep->id_savedino1 = NULL; 2232 inodedep->id_savedsize = -1; 2233 inodedep->id_savedextsize = -1; 2234 inodedep->id_savednlink = -1; 2235 inodedep->id_bmsafemap = NULL; 2236 inodedep->id_mkdiradd = NULL; 2237 LIST_INIT(&inodedep->id_dirremhd); 2238 LIST_INIT(&inodedep->id_pendinghd); 2239 LIST_INIT(&inodedep->id_inowait); 2240 LIST_INIT(&inodedep->id_bufwait); 2241 TAILQ_INIT(&inodedep->id_inoreflst); 2242 TAILQ_INIT(&inodedep->id_inoupdt); 2243 TAILQ_INIT(&inodedep->id_newinoupdt); 2244 TAILQ_INIT(&inodedep->id_extupdt); 2245 TAILQ_INIT(&inodedep->id_newextupdt); 2246 TAILQ_INIT(&inodedep->id_freeblklst); 2247 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash); 2248 *inodedeppp = inodedep; 2249 return (0); 2250 } 2251 2252 /* 2253 * Structures and routines associated with newblk caching. 2254 */ 2255 #define NEWBLK_HASH(ump, inum) \ 2256 (&(ump)->newblk_hashtbl[(inum) & (ump)->newblk_hash_size]) 2257 2258 static int 2259 newblk_find(newblkhd, newblkno, flags, newblkpp) 2260 struct newblk_hashhead *newblkhd; 2261 ufs2_daddr_t newblkno; 2262 int flags; 2263 struct newblk **newblkpp; 2264 { 2265 struct newblk *newblk; 2266 2267 LIST_FOREACH(newblk, newblkhd, nb_hash) { 2268 if (newblkno != newblk->nb_newblkno) 2269 continue; 2270 /* 2271 * If we're creating a new dependency don't match those that 2272 * have already been converted to allocdirects. This is for 2273 * a frag extend. 2274 */ 2275 if ((flags & DEPALLOC) && newblk->nb_list.wk_type != D_NEWBLK) 2276 continue; 2277 break; 2278 } 2279 if (newblk) { 2280 *newblkpp = newblk; 2281 return (1); 2282 } 2283 *newblkpp = NULL; 2284 return (0); 2285 } 2286 2287 /* 2288 * Look up a newblk. Return 1 if found, 0 if not found. 2289 * If not found, allocate if DEPALLOC flag is passed. 2290 * Found or allocated entry is returned in newblkpp. 2291 */ 2292 static int 2293 newblk_lookup(mp, newblkno, flags, newblkpp) 2294 struct mount *mp; 2295 ufs2_daddr_t newblkno; 2296 int flags; 2297 struct newblk **newblkpp; 2298 { 2299 struct newblk *newblk; 2300 struct newblk_hashhead *newblkhd; 2301 struct ufsmount *ump; 2302 2303 ump = VFSTOUFS(mp); 2304 LOCK_OWNED(ump); 2305 newblkhd = NEWBLK_HASH(ump, newblkno); 2306 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) 2307 return (1); 2308 if ((flags & DEPALLOC) == 0) 2309 return (0); 2310 if (softdep_excess_items(ump, D_NEWBLK) || 2311 softdep_excess_items(ump, D_ALLOCDIRECT) || 2312 softdep_excess_items(ump, D_ALLOCINDIR)) 2313 schedule_cleanup(mp); 2314 else 2315 FREE_LOCK(ump); 2316 newblk = malloc(sizeof(union allblk), M_NEWBLK, 2317 M_SOFTDEP_FLAGS | M_ZERO); 2318 workitem_alloc(&newblk->nb_list, D_NEWBLK, mp); 2319 ACQUIRE_LOCK(ump); 2320 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) { 2321 WORKITEM_FREE(newblk, D_NEWBLK); 2322 return (1); 2323 } 2324 newblk->nb_freefrag = NULL; 2325 LIST_INIT(&newblk->nb_indirdeps); 2326 LIST_INIT(&newblk->nb_newdirblk); 2327 LIST_INIT(&newblk->nb_jwork); 2328 newblk->nb_state = ATTACHED; 2329 newblk->nb_newblkno = newblkno; 2330 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash); 2331 *newblkpp = newblk; 2332 return (0); 2333 } 2334 2335 /* 2336 * Structures and routines associated with freed indirect block caching. 2337 */ 2338 #define INDIR_HASH(ump, blkno) \ 2339 (&(ump)->indir_hashtbl[(blkno) & (ump)->indir_hash_size]) 2340 2341 /* 2342 * Lookup an indirect block in the indir hash table. The freework is 2343 * removed and potentially freed. The caller must do a blocking journal 2344 * write before writing to the blkno. 2345 */ 2346 static int 2347 indirblk_lookup(mp, blkno) 2348 struct mount *mp; 2349 ufs2_daddr_t blkno; 2350 { 2351 struct freework *freework; 2352 struct indir_hashhead *wkhd; 2353 struct ufsmount *ump; 2354 2355 ump = VFSTOUFS(mp); 2356 wkhd = INDIR_HASH(ump, blkno); 2357 TAILQ_FOREACH(freework, wkhd, fw_next) { 2358 if (freework->fw_blkno != blkno) 2359 continue; 2360 indirblk_remove(freework); 2361 return (1); 2362 } 2363 return (0); 2364 } 2365 2366 /* 2367 * Insert an indirect block represented by freework into the indirblk 2368 * hash table so that it may prevent the block from being re-used prior 2369 * to the journal being written. 2370 */ 2371 static void 2372 indirblk_insert(freework) 2373 struct freework *freework; 2374 { 2375 struct jblocks *jblocks; 2376 struct jseg *jseg; 2377 struct ufsmount *ump; 2378 2379 ump = VFSTOUFS(freework->fw_list.wk_mp); 2380 jblocks = ump->softdep_jblocks; 2381 jseg = TAILQ_LAST(&jblocks->jb_segs, jseglst); 2382 if (jseg == NULL) 2383 return; 2384 2385 LIST_INSERT_HEAD(&jseg->js_indirs, freework, fw_segs); 2386 TAILQ_INSERT_HEAD(INDIR_HASH(ump, freework->fw_blkno), freework, 2387 fw_next); 2388 freework->fw_state &= ~DEPCOMPLETE; 2389 } 2390 2391 static void 2392 indirblk_remove(freework) 2393 struct freework *freework; 2394 { 2395 struct ufsmount *ump; 2396 2397 ump = VFSTOUFS(freework->fw_list.wk_mp); 2398 LIST_REMOVE(freework, fw_segs); 2399 TAILQ_REMOVE(INDIR_HASH(ump, freework->fw_blkno), freework, fw_next); 2400 freework->fw_state |= DEPCOMPLETE; 2401 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 2402 WORKITEM_FREE(freework, D_FREEWORK); 2403 } 2404 2405 /* 2406 * Executed during filesystem system initialization before 2407 * mounting any filesystems. 2408 */ 2409 void 2410 softdep_initialize() 2411 { 2412 2413 TAILQ_INIT(&softdepmounts); 2414 #ifdef __LP64__ 2415 max_softdeps = desiredvnodes * 4; 2416 #else 2417 max_softdeps = desiredvnodes * 2; 2418 #endif 2419 2420 /* initialise bioops hack */ 2421 bioops.io_start = softdep_disk_io_initiation; 2422 bioops.io_complete = softdep_disk_write_complete; 2423 bioops.io_deallocate = softdep_deallocate_dependencies; 2424 bioops.io_countdeps = softdep_count_dependencies; 2425 softdep_ast_cleanup = softdep_ast_cleanup_proc; 2426 2427 /* Initialize the callout with an mtx. */ 2428 callout_init_mtx(&softdep_callout, &lk, 0); 2429 } 2430 2431 /* 2432 * Executed after all filesystems have been unmounted during 2433 * filesystem module unload. 2434 */ 2435 void 2436 softdep_uninitialize() 2437 { 2438 2439 /* clear bioops hack */ 2440 bioops.io_start = NULL; 2441 bioops.io_complete = NULL; 2442 bioops.io_deallocate = NULL; 2443 bioops.io_countdeps = NULL; 2444 softdep_ast_cleanup = NULL; 2445 2446 callout_drain(&softdep_callout); 2447 } 2448 2449 /* 2450 * Called at mount time to notify the dependency code that a 2451 * filesystem wishes to use it. 2452 */ 2453 int 2454 softdep_mount(devvp, mp, fs, cred) 2455 struct vnode *devvp; 2456 struct mount *mp; 2457 struct fs *fs; 2458 struct ucred *cred; 2459 { 2460 struct csum_total cstotal; 2461 struct mount_softdeps *sdp; 2462 struct ufsmount *ump; 2463 struct cg *cgp; 2464 struct buf *bp; 2465 int i, error, cyl; 2466 2467 sdp = malloc(sizeof(struct mount_softdeps), M_MOUNTDATA, 2468 M_WAITOK | M_ZERO); 2469 MNT_ILOCK(mp); 2470 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP; 2471 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) { 2472 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) | 2473 MNTK_SOFTDEP | MNTK_NOASYNC; 2474 } 2475 ump = VFSTOUFS(mp); 2476 ump->um_softdep = sdp; 2477 MNT_IUNLOCK(mp); 2478 rw_init(LOCK_PTR(ump), "Per-Filesystem Softdep Lock"); 2479 sdp->sd_ump = ump; 2480 LIST_INIT(&ump->softdep_workitem_pending); 2481 LIST_INIT(&ump->softdep_journal_pending); 2482 TAILQ_INIT(&ump->softdep_unlinked); 2483 LIST_INIT(&ump->softdep_dirtycg); 2484 ump->softdep_worklist_tail = NULL; 2485 ump->softdep_on_worklist = 0; 2486 ump->softdep_deps = 0; 2487 LIST_INIT(&ump->softdep_mkdirlisthd); 2488 ump->pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP, 2489 &ump->pagedep_hash_size); 2490 ump->pagedep_nextclean = 0; 2491 ump->inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP, 2492 &ump->inodedep_hash_size); 2493 ump->inodedep_nextclean = 0; 2494 ump->newblk_hashtbl = hashinit(max_softdeps / 2, M_NEWBLK, 2495 &ump->newblk_hash_size); 2496 ump->bmsafemap_hashtbl = hashinit(1024, M_BMSAFEMAP, 2497 &ump->bmsafemap_hash_size); 2498 i = 1 << (ffs(desiredvnodes / 10) - 1); 2499 ump->indir_hashtbl = malloc(i * sizeof(struct indir_hashhead), 2500 M_FREEWORK, M_WAITOK); 2501 ump->indir_hash_size = i - 1; 2502 for (i = 0; i <= ump->indir_hash_size; i++) 2503 TAILQ_INIT(&ump->indir_hashtbl[i]); 2504 ACQUIRE_GBLLOCK(&lk); 2505 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next); 2506 FREE_GBLLOCK(&lk); 2507 if ((fs->fs_flags & FS_SUJ) && 2508 (error = journal_mount(mp, fs, cred)) != 0) { 2509 printf("Failed to start journal: %d\n", error); 2510 softdep_unmount(mp); 2511 return (error); 2512 } 2513 /* 2514 * Start our flushing thread in the bufdaemon process. 2515 */ 2516 ACQUIRE_LOCK(ump); 2517 ump->softdep_flags |= FLUSH_STARTING; 2518 FREE_LOCK(ump); 2519 kproc_kthread_add(&softdep_flush, mp, &bufdaemonproc, 2520 &ump->softdep_flushtd, 0, 0, "softdepflush", "%s worker", 2521 mp->mnt_stat.f_mntonname); 2522 ACQUIRE_LOCK(ump); 2523 while ((ump->softdep_flags & FLUSH_STARTING) != 0) { 2524 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, "sdstart", 2525 hz / 2); 2526 } 2527 FREE_LOCK(ump); 2528 /* 2529 * When doing soft updates, the counters in the 2530 * superblock may have gotten out of sync. Recomputation 2531 * can take a long time and can be deferred for background 2532 * fsck. However, the old behavior of scanning the cylinder 2533 * groups and recalculating them at mount time is available 2534 * by setting vfs.ffs.compute_summary_at_mount to one. 2535 */ 2536 if (compute_summary_at_mount == 0 || fs->fs_clean != 0) 2537 return (0); 2538 bzero(&cstotal, sizeof cstotal); 2539 for (cyl = 0; cyl < fs->fs_ncg; cyl++) { 2540 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)), 2541 fs->fs_cgsize, cred, &bp)) != 0) { 2542 brelse(bp); 2543 softdep_unmount(mp); 2544 return (error); 2545 } 2546 cgp = (struct cg *)bp->b_data; 2547 cstotal.cs_nffree += cgp->cg_cs.cs_nffree; 2548 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree; 2549 cstotal.cs_nifree += cgp->cg_cs.cs_nifree; 2550 cstotal.cs_ndir += cgp->cg_cs.cs_ndir; 2551 fs->fs_cs(fs, cyl) = cgp->cg_cs; 2552 brelse(bp); 2553 } 2554 #ifdef DEBUG 2555 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal)) 2556 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt); 2557 #endif 2558 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal); 2559 return (0); 2560 } 2561 2562 void 2563 softdep_unmount(mp) 2564 struct mount *mp; 2565 { 2566 struct ufsmount *ump; 2567 #ifdef INVARIANTS 2568 int i; 2569 #endif 2570 2571 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 2572 ("softdep_unmount called on non-softdep filesystem")); 2573 ump = VFSTOUFS(mp); 2574 MNT_ILOCK(mp); 2575 mp->mnt_flag &= ~MNT_SOFTDEP; 2576 if (MOUNTEDSUJ(mp) == 0) { 2577 MNT_IUNLOCK(mp); 2578 } else { 2579 mp->mnt_flag &= ~MNT_SUJ; 2580 MNT_IUNLOCK(mp); 2581 journal_unmount(ump); 2582 } 2583 /* 2584 * Shut down our flushing thread. Check for NULL is if 2585 * softdep_mount errors out before the thread has been created. 2586 */ 2587 if (ump->softdep_flushtd != NULL) { 2588 ACQUIRE_LOCK(ump); 2589 ump->softdep_flags |= FLUSH_EXIT; 2590 wakeup(&ump->softdep_flushtd); 2591 msleep(&ump->softdep_flags, LOCK_PTR(ump), PVM | PDROP, 2592 "sdwait", 0); 2593 KASSERT((ump->softdep_flags & FLUSH_EXIT) == 0, 2594 ("Thread shutdown failed")); 2595 } 2596 /* 2597 * Free up our resources. 2598 */ 2599 ACQUIRE_GBLLOCK(&lk); 2600 TAILQ_REMOVE(&softdepmounts, ump->um_softdep, sd_next); 2601 FREE_GBLLOCK(&lk); 2602 rw_destroy(LOCK_PTR(ump)); 2603 hashdestroy(ump->pagedep_hashtbl, M_PAGEDEP, ump->pagedep_hash_size); 2604 hashdestroy(ump->inodedep_hashtbl, M_INODEDEP, ump->inodedep_hash_size); 2605 hashdestroy(ump->newblk_hashtbl, M_NEWBLK, ump->newblk_hash_size); 2606 hashdestroy(ump->bmsafemap_hashtbl, M_BMSAFEMAP, 2607 ump->bmsafemap_hash_size); 2608 free(ump->indir_hashtbl, M_FREEWORK); 2609 #ifdef INVARIANTS 2610 for (i = 0; i <= D_LAST; i++) 2611 KASSERT(ump->softdep_curdeps[i] == 0, 2612 ("Unmount %s: Dep type %s != 0 (%ld)", ump->um_fs->fs_fsmnt, 2613 TYPENAME(i), ump->softdep_curdeps[i])); 2614 #endif 2615 free(ump->um_softdep, M_MOUNTDATA); 2616 } 2617 2618 static struct jblocks * 2619 jblocks_create(void) 2620 { 2621 struct jblocks *jblocks; 2622 2623 jblocks = malloc(sizeof(*jblocks), M_JBLOCKS, M_WAITOK | M_ZERO); 2624 TAILQ_INIT(&jblocks->jb_segs); 2625 jblocks->jb_avail = 10; 2626 jblocks->jb_extent = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2627 M_JBLOCKS, M_WAITOK | M_ZERO); 2628 2629 return (jblocks); 2630 } 2631 2632 static ufs2_daddr_t 2633 jblocks_alloc(jblocks, bytes, actual) 2634 struct jblocks *jblocks; 2635 int bytes; 2636 int *actual; 2637 { 2638 ufs2_daddr_t daddr; 2639 struct jextent *jext; 2640 int freecnt; 2641 int blocks; 2642 2643 blocks = bytes / DEV_BSIZE; 2644 jext = &jblocks->jb_extent[jblocks->jb_head]; 2645 freecnt = jext->je_blocks - jblocks->jb_off; 2646 if (freecnt == 0) { 2647 jblocks->jb_off = 0; 2648 if (++jblocks->jb_head > jblocks->jb_used) 2649 jblocks->jb_head = 0; 2650 jext = &jblocks->jb_extent[jblocks->jb_head]; 2651 freecnt = jext->je_blocks; 2652 } 2653 if (freecnt > blocks) 2654 freecnt = blocks; 2655 *actual = freecnt * DEV_BSIZE; 2656 daddr = jext->je_daddr + jblocks->jb_off; 2657 jblocks->jb_off += freecnt; 2658 jblocks->jb_free -= freecnt; 2659 2660 return (daddr); 2661 } 2662 2663 static void 2664 jblocks_free(jblocks, mp, bytes) 2665 struct jblocks *jblocks; 2666 struct mount *mp; 2667 int bytes; 2668 { 2669 2670 LOCK_OWNED(VFSTOUFS(mp)); 2671 jblocks->jb_free += bytes / DEV_BSIZE; 2672 if (jblocks->jb_suspended) 2673 worklist_speedup(mp); 2674 wakeup(jblocks); 2675 } 2676 2677 static void 2678 jblocks_destroy(jblocks) 2679 struct jblocks *jblocks; 2680 { 2681 2682 if (jblocks->jb_extent) 2683 free(jblocks->jb_extent, M_JBLOCKS); 2684 free(jblocks, M_JBLOCKS); 2685 } 2686 2687 static void 2688 jblocks_add(jblocks, daddr, blocks) 2689 struct jblocks *jblocks; 2690 ufs2_daddr_t daddr; 2691 int blocks; 2692 { 2693 struct jextent *jext; 2694 2695 jblocks->jb_blocks += blocks; 2696 jblocks->jb_free += blocks; 2697 jext = &jblocks->jb_extent[jblocks->jb_used]; 2698 /* Adding the first block. */ 2699 if (jext->je_daddr == 0) { 2700 jext->je_daddr = daddr; 2701 jext->je_blocks = blocks; 2702 return; 2703 } 2704 /* Extending the last extent. */ 2705 if (jext->je_daddr + jext->je_blocks == daddr) { 2706 jext->je_blocks += blocks; 2707 return; 2708 } 2709 /* Adding a new extent. */ 2710 if (++jblocks->jb_used == jblocks->jb_avail) { 2711 jblocks->jb_avail *= 2; 2712 jext = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2713 M_JBLOCKS, M_WAITOK | M_ZERO); 2714 memcpy(jext, jblocks->jb_extent, 2715 sizeof(struct jextent) * jblocks->jb_used); 2716 free(jblocks->jb_extent, M_JBLOCKS); 2717 jblocks->jb_extent = jext; 2718 } 2719 jext = &jblocks->jb_extent[jblocks->jb_used]; 2720 jext->je_daddr = daddr; 2721 jext->je_blocks = blocks; 2722 return; 2723 } 2724 2725 int 2726 softdep_journal_lookup(mp, vpp) 2727 struct mount *mp; 2728 struct vnode **vpp; 2729 { 2730 struct componentname cnp; 2731 struct vnode *dvp; 2732 ino_t sujournal; 2733 int error; 2734 2735 error = VFS_VGET(mp, ROOTINO, LK_EXCLUSIVE, &dvp); 2736 if (error) 2737 return (error); 2738 bzero(&cnp, sizeof(cnp)); 2739 cnp.cn_nameiop = LOOKUP; 2740 cnp.cn_flags = ISLASTCN; 2741 cnp.cn_thread = curthread; 2742 cnp.cn_cred = curthread->td_ucred; 2743 cnp.cn_pnbuf = SUJ_FILE; 2744 cnp.cn_nameptr = SUJ_FILE; 2745 cnp.cn_namelen = strlen(SUJ_FILE); 2746 error = ufs_lookup_ino(dvp, NULL, &cnp, &sujournal); 2747 vput(dvp); 2748 if (error != 0) 2749 return (error); 2750 error = VFS_VGET(mp, sujournal, LK_EXCLUSIVE, vpp); 2751 return (error); 2752 } 2753 2754 /* 2755 * Open and verify the journal file. 2756 */ 2757 static int 2758 journal_mount(mp, fs, cred) 2759 struct mount *mp; 2760 struct fs *fs; 2761 struct ucred *cred; 2762 { 2763 struct jblocks *jblocks; 2764 struct ufsmount *ump; 2765 struct vnode *vp; 2766 struct inode *ip; 2767 ufs2_daddr_t blkno; 2768 int bcount; 2769 int error; 2770 int i; 2771 2772 ump = VFSTOUFS(mp); 2773 ump->softdep_journal_tail = NULL; 2774 ump->softdep_on_journal = 0; 2775 ump->softdep_accdeps = 0; 2776 ump->softdep_req = 0; 2777 ump->softdep_jblocks = NULL; 2778 error = softdep_journal_lookup(mp, &vp); 2779 if (error != 0) { 2780 printf("Failed to find journal. Use tunefs to create one\n"); 2781 return (error); 2782 } 2783 ip = VTOI(vp); 2784 if (ip->i_size < SUJ_MIN) { 2785 error = ENOSPC; 2786 goto out; 2787 } 2788 bcount = lblkno(fs, ip->i_size); /* Only use whole blocks. */ 2789 jblocks = jblocks_create(); 2790 for (i = 0; i < bcount; i++) { 2791 error = ufs_bmaparray(vp, i, &blkno, NULL, NULL, NULL); 2792 if (error) 2793 break; 2794 jblocks_add(jblocks, blkno, fsbtodb(fs, fs->fs_frag)); 2795 } 2796 if (error) { 2797 jblocks_destroy(jblocks); 2798 goto out; 2799 } 2800 jblocks->jb_low = jblocks->jb_free / 3; /* Reserve 33%. */ 2801 jblocks->jb_min = jblocks->jb_free / 10; /* Suspend at 10%. */ 2802 ump->softdep_jblocks = jblocks; 2803 out: 2804 if (error == 0) { 2805 MNT_ILOCK(mp); 2806 mp->mnt_flag |= MNT_SUJ; 2807 mp->mnt_flag &= ~MNT_SOFTDEP; 2808 MNT_IUNLOCK(mp); 2809 /* 2810 * Only validate the journal contents if the 2811 * filesystem is clean, otherwise we write the logs 2812 * but they'll never be used. If the filesystem was 2813 * still dirty when we mounted it the journal is 2814 * invalid and a new journal can only be valid if it 2815 * starts from a clean mount. 2816 */ 2817 if (fs->fs_clean) { 2818 DIP_SET(ip, i_modrev, fs->fs_mtime); 2819 ip->i_flags |= IN_MODIFIED; 2820 ffs_update(vp, 1); 2821 } 2822 } 2823 vput(vp); 2824 return (error); 2825 } 2826 2827 static void 2828 journal_unmount(ump) 2829 struct ufsmount *ump; 2830 { 2831 2832 if (ump->softdep_jblocks) 2833 jblocks_destroy(ump->softdep_jblocks); 2834 ump->softdep_jblocks = NULL; 2835 } 2836 2837 /* 2838 * Called when a journal record is ready to be written. Space is allocated 2839 * and the journal entry is created when the journal is flushed to stable 2840 * store. 2841 */ 2842 static void 2843 add_to_journal(wk) 2844 struct worklist *wk; 2845 { 2846 struct ufsmount *ump; 2847 2848 ump = VFSTOUFS(wk->wk_mp); 2849 LOCK_OWNED(ump); 2850 if (wk->wk_state & ONWORKLIST) 2851 panic("add_to_journal: %s(0x%X) already on list", 2852 TYPENAME(wk->wk_type), wk->wk_state); 2853 wk->wk_state |= ONWORKLIST | DEPCOMPLETE; 2854 if (LIST_EMPTY(&ump->softdep_journal_pending)) { 2855 ump->softdep_jblocks->jb_age = ticks; 2856 LIST_INSERT_HEAD(&ump->softdep_journal_pending, wk, wk_list); 2857 } else 2858 LIST_INSERT_AFTER(ump->softdep_journal_tail, wk, wk_list); 2859 ump->softdep_journal_tail = wk; 2860 ump->softdep_on_journal += 1; 2861 } 2862 2863 /* 2864 * Remove an arbitrary item for the journal worklist maintain the tail 2865 * pointer. This happens when a new operation obviates the need to 2866 * journal an old operation. 2867 */ 2868 static void 2869 remove_from_journal(wk) 2870 struct worklist *wk; 2871 { 2872 struct ufsmount *ump; 2873 2874 ump = VFSTOUFS(wk->wk_mp); 2875 LOCK_OWNED(ump); 2876 #ifdef SUJ_DEBUG 2877 { 2878 struct worklist *wkn; 2879 2880 LIST_FOREACH(wkn, &ump->softdep_journal_pending, wk_list) 2881 if (wkn == wk) 2882 break; 2883 if (wkn == NULL) 2884 panic("remove_from_journal: %p is not in journal", wk); 2885 } 2886 #endif 2887 /* 2888 * We emulate a TAILQ to save space in most structures which do not 2889 * require TAILQ semantics. Here we must update the tail position 2890 * when removing the tail which is not the final entry. This works 2891 * only if the worklist linkage are at the beginning of the structure. 2892 */ 2893 if (ump->softdep_journal_tail == wk) 2894 ump->softdep_journal_tail = 2895 (struct worklist *)wk->wk_list.le_prev; 2896 2897 WORKLIST_REMOVE(wk); 2898 ump->softdep_on_journal -= 1; 2899 } 2900 2901 /* 2902 * Check for journal space as well as dependency limits so the prelink 2903 * code can throttle both journaled and non-journaled filesystems. 2904 * Threshold is 0 for low and 1 for min. 2905 */ 2906 static int 2907 journal_space(ump, thresh) 2908 struct ufsmount *ump; 2909 int thresh; 2910 { 2911 struct jblocks *jblocks; 2912 int limit, avail; 2913 2914 jblocks = ump->softdep_jblocks; 2915 if (jblocks == NULL) 2916 return (1); 2917 /* 2918 * We use a tighter restriction here to prevent request_cleanup() 2919 * running in threads from running into locks we currently hold. 2920 * We have to be over the limit and our filesystem has to be 2921 * responsible for more than our share of that usage. 2922 */ 2923 limit = (max_softdeps / 10) * 9; 2924 if (dep_current[D_INODEDEP] > limit && 2925 ump->softdep_curdeps[D_INODEDEP] > limit / stat_flush_threads) 2926 return (0); 2927 if (thresh) 2928 thresh = jblocks->jb_min; 2929 else 2930 thresh = jblocks->jb_low; 2931 avail = (ump->softdep_on_journal * JREC_SIZE) / DEV_BSIZE; 2932 avail = jblocks->jb_free - avail; 2933 2934 return (avail > thresh); 2935 } 2936 2937 static void 2938 journal_suspend(ump) 2939 struct ufsmount *ump; 2940 { 2941 struct jblocks *jblocks; 2942 struct mount *mp; 2943 2944 mp = UFSTOVFS(ump); 2945 jblocks = ump->softdep_jblocks; 2946 MNT_ILOCK(mp); 2947 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) { 2948 stat_journal_min++; 2949 mp->mnt_kern_flag |= MNTK_SUSPEND; 2950 mp->mnt_susp_owner = ump->softdep_flushtd; 2951 } 2952 jblocks->jb_suspended = 1; 2953 MNT_IUNLOCK(mp); 2954 } 2955 2956 static int 2957 journal_unsuspend(struct ufsmount *ump) 2958 { 2959 struct jblocks *jblocks; 2960 struct mount *mp; 2961 2962 mp = UFSTOVFS(ump); 2963 jblocks = ump->softdep_jblocks; 2964 2965 if (jblocks != NULL && jblocks->jb_suspended && 2966 journal_space(ump, jblocks->jb_min)) { 2967 jblocks->jb_suspended = 0; 2968 FREE_LOCK(ump); 2969 mp->mnt_susp_owner = curthread; 2970 vfs_write_resume(mp, 0); 2971 ACQUIRE_LOCK(ump); 2972 return (1); 2973 } 2974 return (0); 2975 } 2976 2977 /* 2978 * Called before any allocation function to be certain that there is 2979 * sufficient space in the journal prior to creating any new records. 2980 * Since in the case of block allocation we may have multiple locked 2981 * buffers at the time of the actual allocation we can not block 2982 * when the journal records are created. Doing so would create a deadlock 2983 * if any of these buffers needed to be flushed to reclaim space. Instead 2984 * we require a sufficiently large amount of available space such that 2985 * each thread in the system could have passed this allocation check and 2986 * still have sufficient free space. With 20% of a minimum journal size 2987 * of 1MB we have 6553 records available. 2988 */ 2989 int 2990 softdep_prealloc(vp, waitok) 2991 struct vnode *vp; 2992 int waitok; 2993 { 2994 struct ufsmount *ump; 2995 2996 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 2997 ("softdep_prealloc called on non-softdep filesystem")); 2998 /* 2999 * Nothing to do if we are not running journaled soft updates. 3000 * If we currently hold the snapshot lock, we must avoid handling 3001 * other resources that could cause deadlock. 3002 */ 3003 if (DOINGSUJ(vp) == 0 || IS_SNAPSHOT(VTOI(vp))) 3004 return (0); 3005 ump = VFSTOUFS(vp->v_mount); 3006 ACQUIRE_LOCK(ump); 3007 if (journal_space(ump, 0)) { 3008 FREE_LOCK(ump); 3009 return (0); 3010 } 3011 stat_journal_low++; 3012 FREE_LOCK(ump); 3013 if (waitok == MNT_NOWAIT) 3014 return (ENOSPC); 3015 /* 3016 * Attempt to sync this vnode once to flush any journal 3017 * work attached to it. 3018 */ 3019 if ((curthread->td_pflags & TDP_COWINPROGRESS) == 0) 3020 ffs_syncvnode(vp, waitok, 0); 3021 ACQUIRE_LOCK(ump); 3022 process_removes(vp); 3023 process_truncates(vp); 3024 if (journal_space(ump, 0) == 0) { 3025 softdep_speedup(ump); 3026 if (journal_space(ump, 1) == 0) 3027 journal_suspend(ump); 3028 } 3029 FREE_LOCK(ump); 3030 3031 return (0); 3032 } 3033 3034 /* 3035 * Before adjusting a link count on a vnode verify that we have sufficient 3036 * journal space. If not, process operations that depend on the currently 3037 * locked pair of vnodes to try to flush space as the syncer, buf daemon, 3038 * and softdep flush threads can not acquire these locks to reclaim space. 3039 */ 3040 static void 3041 softdep_prelink(dvp, vp) 3042 struct vnode *dvp; 3043 struct vnode *vp; 3044 { 3045 struct ufsmount *ump; 3046 3047 ump = VFSTOUFS(dvp->v_mount); 3048 LOCK_OWNED(ump); 3049 /* 3050 * Nothing to do if we have sufficient journal space. 3051 * If we currently hold the snapshot lock, we must avoid 3052 * handling other resources that could cause deadlock. 3053 */ 3054 if (journal_space(ump, 0) || (vp && IS_SNAPSHOT(VTOI(vp)))) 3055 return; 3056 stat_journal_low++; 3057 FREE_LOCK(ump); 3058 if (vp) 3059 ffs_syncvnode(vp, MNT_NOWAIT, 0); 3060 ffs_syncvnode(dvp, MNT_WAIT, 0); 3061 ACQUIRE_LOCK(ump); 3062 /* Process vp before dvp as it may create .. removes. */ 3063 if (vp) { 3064 process_removes(vp); 3065 process_truncates(vp); 3066 } 3067 process_removes(dvp); 3068 process_truncates(dvp); 3069 softdep_speedup(ump); 3070 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT); 3071 if (journal_space(ump, 0) == 0) { 3072 softdep_speedup(ump); 3073 if (journal_space(ump, 1) == 0) 3074 journal_suspend(ump); 3075 } 3076 } 3077 3078 static void 3079 jseg_write(ump, jseg, data) 3080 struct ufsmount *ump; 3081 struct jseg *jseg; 3082 uint8_t *data; 3083 { 3084 struct jsegrec *rec; 3085 3086 rec = (struct jsegrec *)data; 3087 rec->jsr_seq = jseg->js_seq; 3088 rec->jsr_oldest = jseg->js_oldseq; 3089 rec->jsr_cnt = jseg->js_cnt; 3090 rec->jsr_blocks = jseg->js_size / ump->um_devvp->v_bufobj.bo_bsize; 3091 rec->jsr_crc = 0; 3092 rec->jsr_time = ump->um_fs->fs_mtime; 3093 } 3094 3095 static inline void 3096 inoref_write(inoref, jseg, rec) 3097 struct inoref *inoref; 3098 struct jseg *jseg; 3099 struct jrefrec *rec; 3100 { 3101 3102 inoref->if_jsegdep->jd_seg = jseg; 3103 rec->jr_ino = inoref->if_ino; 3104 rec->jr_parent = inoref->if_parent; 3105 rec->jr_nlink = inoref->if_nlink; 3106 rec->jr_mode = inoref->if_mode; 3107 rec->jr_diroff = inoref->if_diroff; 3108 } 3109 3110 static void 3111 jaddref_write(jaddref, jseg, data) 3112 struct jaddref *jaddref; 3113 struct jseg *jseg; 3114 uint8_t *data; 3115 { 3116 struct jrefrec *rec; 3117 3118 rec = (struct jrefrec *)data; 3119 rec->jr_op = JOP_ADDREF; 3120 inoref_write(&jaddref->ja_ref, jseg, rec); 3121 } 3122 3123 static void 3124 jremref_write(jremref, jseg, data) 3125 struct jremref *jremref; 3126 struct jseg *jseg; 3127 uint8_t *data; 3128 { 3129 struct jrefrec *rec; 3130 3131 rec = (struct jrefrec *)data; 3132 rec->jr_op = JOP_REMREF; 3133 inoref_write(&jremref->jr_ref, jseg, rec); 3134 } 3135 3136 static void 3137 jmvref_write(jmvref, jseg, data) 3138 struct jmvref *jmvref; 3139 struct jseg *jseg; 3140 uint8_t *data; 3141 { 3142 struct jmvrec *rec; 3143 3144 rec = (struct jmvrec *)data; 3145 rec->jm_op = JOP_MVREF; 3146 rec->jm_ino = jmvref->jm_ino; 3147 rec->jm_parent = jmvref->jm_parent; 3148 rec->jm_oldoff = jmvref->jm_oldoff; 3149 rec->jm_newoff = jmvref->jm_newoff; 3150 } 3151 3152 static void 3153 jnewblk_write(jnewblk, jseg, data) 3154 struct jnewblk *jnewblk; 3155 struct jseg *jseg; 3156 uint8_t *data; 3157 { 3158 struct jblkrec *rec; 3159 3160 jnewblk->jn_jsegdep->jd_seg = jseg; 3161 rec = (struct jblkrec *)data; 3162 rec->jb_op = JOP_NEWBLK; 3163 rec->jb_ino = jnewblk->jn_ino; 3164 rec->jb_blkno = jnewblk->jn_blkno; 3165 rec->jb_lbn = jnewblk->jn_lbn; 3166 rec->jb_frags = jnewblk->jn_frags; 3167 rec->jb_oldfrags = jnewblk->jn_oldfrags; 3168 } 3169 3170 static void 3171 jfreeblk_write(jfreeblk, jseg, data) 3172 struct jfreeblk *jfreeblk; 3173 struct jseg *jseg; 3174 uint8_t *data; 3175 { 3176 struct jblkrec *rec; 3177 3178 jfreeblk->jf_dep.jb_jsegdep->jd_seg = jseg; 3179 rec = (struct jblkrec *)data; 3180 rec->jb_op = JOP_FREEBLK; 3181 rec->jb_ino = jfreeblk->jf_ino; 3182 rec->jb_blkno = jfreeblk->jf_blkno; 3183 rec->jb_lbn = jfreeblk->jf_lbn; 3184 rec->jb_frags = jfreeblk->jf_frags; 3185 rec->jb_oldfrags = 0; 3186 } 3187 3188 static void 3189 jfreefrag_write(jfreefrag, jseg, data) 3190 struct jfreefrag *jfreefrag; 3191 struct jseg *jseg; 3192 uint8_t *data; 3193 { 3194 struct jblkrec *rec; 3195 3196 jfreefrag->fr_jsegdep->jd_seg = jseg; 3197 rec = (struct jblkrec *)data; 3198 rec->jb_op = JOP_FREEBLK; 3199 rec->jb_ino = jfreefrag->fr_ino; 3200 rec->jb_blkno = jfreefrag->fr_blkno; 3201 rec->jb_lbn = jfreefrag->fr_lbn; 3202 rec->jb_frags = jfreefrag->fr_frags; 3203 rec->jb_oldfrags = 0; 3204 } 3205 3206 static void 3207 jtrunc_write(jtrunc, jseg, data) 3208 struct jtrunc *jtrunc; 3209 struct jseg *jseg; 3210 uint8_t *data; 3211 { 3212 struct jtrncrec *rec; 3213 3214 jtrunc->jt_dep.jb_jsegdep->jd_seg = jseg; 3215 rec = (struct jtrncrec *)data; 3216 rec->jt_op = JOP_TRUNC; 3217 rec->jt_ino = jtrunc->jt_ino; 3218 rec->jt_size = jtrunc->jt_size; 3219 rec->jt_extsize = jtrunc->jt_extsize; 3220 } 3221 3222 static void 3223 jfsync_write(jfsync, jseg, data) 3224 struct jfsync *jfsync; 3225 struct jseg *jseg; 3226 uint8_t *data; 3227 { 3228 struct jtrncrec *rec; 3229 3230 rec = (struct jtrncrec *)data; 3231 rec->jt_op = JOP_SYNC; 3232 rec->jt_ino = jfsync->jfs_ino; 3233 rec->jt_size = jfsync->jfs_size; 3234 rec->jt_extsize = jfsync->jfs_extsize; 3235 } 3236 3237 static void 3238 softdep_flushjournal(mp) 3239 struct mount *mp; 3240 { 3241 struct jblocks *jblocks; 3242 struct ufsmount *ump; 3243 3244 if (MOUNTEDSUJ(mp) == 0) 3245 return; 3246 ump = VFSTOUFS(mp); 3247 jblocks = ump->softdep_jblocks; 3248 ACQUIRE_LOCK(ump); 3249 while (ump->softdep_on_journal) { 3250 jblocks->jb_needseg = 1; 3251 softdep_process_journal(mp, NULL, MNT_WAIT); 3252 } 3253 FREE_LOCK(ump); 3254 } 3255 3256 static void softdep_synchronize_completed(struct bio *); 3257 static void softdep_synchronize(struct bio *, struct ufsmount *, void *); 3258 3259 static void 3260 softdep_synchronize_completed(bp) 3261 struct bio *bp; 3262 { 3263 struct jseg *oldest; 3264 struct jseg *jseg; 3265 struct ufsmount *ump; 3266 3267 /* 3268 * caller1 marks the last segment written before we issued the 3269 * synchronize cache. 3270 */ 3271 jseg = bp->bio_caller1; 3272 if (jseg == NULL) { 3273 g_destroy_bio(bp); 3274 return; 3275 } 3276 ump = VFSTOUFS(jseg->js_list.wk_mp); 3277 ACQUIRE_LOCK(ump); 3278 oldest = NULL; 3279 /* 3280 * Mark all the journal entries waiting on the synchronize cache 3281 * as completed so they may continue on. 3282 */ 3283 while (jseg != NULL && (jseg->js_state & COMPLETE) == 0) { 3284 jseg->js_state |= COMPLETE; 3285 oldest = jseg; 3286 jseg = TAILQ_PREV(jseg, jseglst, js_next); 3287 } 3288 /* 3289 * Restart deferred journal entry processing from the oldest 3290 * completed jseg. 3291 */ 3292 if (oldest) 3293 complete_jsegs(oldest); 3294 3295 FREE_LOCK(ump); 3296 g_destroy_bio(bp); 3297 } 3298 3299 /* 3300 * Send BIO_FLUSH/SYNCHRONIZE CACHE to the device to enforce write ordering 3301 * barriers. The journal must be written prior to any blocks that depend 3302 * on it and the journal can not be released until the blocks have be 3303 * written. This code handles both barriers simultaneously. 3304 */ 3305 static void 3306 softdep_synchronize(bp, ump, caller1) 3307 struct bio *bp; 3308 struct ufsmount *ump; 3309 void *caller1; 3310 { 3311 3312 bp->bio_cmd = BIO_FLUSH; 3313 bp->bio_flags |= BIO_ORDERED; 3314 bp->bio_data = NULL; 3315 bp->bio_offset = ump->um_cp->provider->mediasize; 3316 bp->bio_length = 0; 3317 bp->bio_done = softdep_synchronize_completed; 3318 bp->bio_caller1 = caller1; 3319 g_io_request(bp, 3320 (struct g_consumer *)ump->um_devvp->v_bufobj.bo_private); 3321 } 3322 3323 /* 3324 * Flush some journal records to disk. 3325 */ 3326 static void 3327 softdep_process_journal(mp, needwk, flags) 3328 struct mount *mp; 3329 struct worklist *needwk; 3330 int flags; 3331 { 3332 struct jblocks *jblocks; 3333 struct ufsmount *ump; 3334 struct worklist *wk; 3335 struct jseg *jseg; 3336 struct buf *bp; 3337 struct bio *bio; 3338 uint8_t *data; 3339 struct fs *fs; 3340 int shouldflush; 3341 int segwritten; 3342 int jrecmin; /* Minimum records per block. */ 3343 int jrecmax; /* Maximum records per block. */ 3344 int size; 3345 int cnt; 3346 int off; 3347 int devbsize; 3348 3349 if (MOUNTEDSUJ(mp) == 0) 3350 return; 3351 shouldflush = softdep_flushcache; 3352 bio = NULL; 3353 jseg = NULL; 3354 ump = VFSTOUFS(mp); 3355 LOCK_OWNED(ump); 3356 fs = ump->um_fs; 3357 jblocks = ump->softdep_jblocks; 3358 devbsize = ump->um_devvp->v_bufobj.bo_bsize; 3359 /* 3360 * We write anywhere between a disk block and fs block. The upper 3361 * bound is picked to prevent buffer cache fragmentation and limit 3362 * processing time per I/O. 3363 */ 3364 jrecmin = (devbsize / JREC_SIZE) - 1; /* -1 for seg header */ 3365 jrecmax = (fs->fs_bsize / devbsize) * jrecmin; 3366 segwritten = 0; 3367 for (;;) { 3368 cnt = ump->softdep_on_journal; 3369 /* 3370 * Criteria for writing a segment: 3371 * 1) We have a full block. 3372 * 2) We're called from jwait() and haven't found the 3373 * journal item yet. 3374 * 3) Always write if needseg is set. 3375 * 4) If we are called from process_worklist and have 3376 * not yet written anything we write a partial block 3377 * to enforce a 1 second maximum latency on journal 3378 * entries. 3379 */ 3380 if (cnt < (jrecmax - 1) && needwk == NULL && 3381 jblocks->jb_needseg == 0 && (segwritten || cnt == 0)) 3382 break; 3383 cnt++; 3384 /* 3385 * Verify some free journal space. softdep_prealloc() should 3386 * guarantee that we don't run out so this is indicative of 3387 * a problem with the flow control. Try to recover 3388 * gracefully in any event. 3389 */ 3390 while (jblocks->jb_free == 0) { 3391 if (flags != MNT_WAIT) 3392 break; 3393 printf("softdep: Out of journal space!\n"); 3394 softdep_speedup(ump); 3395 msleep(jblocks, LOCK_PTR(ump), PRIBIO, "jblocks", hz); 3396 } 3397 FREE_LOCK(ump); 3398 jseg = malloc(sizeof(*jseg), M_JSEG, M_SOFTDEP_FLAGS); 3399 workitem_alloc(&jseg->js_list, D_JSEG, mp); 3400 LIST_INIT(&jseg->js_entries); 3401 LIST_INIT(&jseg->js_indirs); 3402 jseg->js_state = ATTACHED; 3403 if (shouldflush == 0) 3404 jseg->js_state |= COMPLETE; 3405 else if (bio == NULL) 3406 bio = g_alloc_bio(); 3407 jseg->js_jblocks = jblocks; 3408 bp = geteblk(fs->fs_bsize, 0); 3409 ACQUIRE_LOCK(ump); 3410 /* 3411 * If there was a race while we were allocating the block 3412 * and jseg the entry we care about was likely written. 3413 * We bail out in both the WAIT and NOWAIT case and assume 3414 * the caller will loop if the entry it cares about is 3415 * not written. 3416 */ 3417 cnt = ump->softdep_on_journal; 3418 if (cnt + jblocks->jb_needseg == 0 || jblocks->jb_free == 0) { 3419 bp->b_flags |= B_INVAL | B_NOCACHE; 3420 WORKITEM_FREE(jseg, D_JSEG); 3421 FREE_LOCK(ump); 3422 brelse(bp); 3423 ACQUIRE_LOCK(ump); 3424 break; 3425 } 3426 /* 3427 * Calculate the disk block size required for the available 3428 * records rounded to the min size. 3429 */ 3430 if (cnt == 0) 3431 size = devbsize; 3432 else if (cnt < jrecmax) 3433 size = howmany(cnt, jrecmin) * devbsize; 3434 else 3435 size = fs->fs_bsize; 3436 /* 3437 * Allocate a disk block for this journal data and account 3438 * for truncation of the requested size if enough contiguous 3439 * space was not available. 3440 */ 3441 bp->b_blkno = jblocks_alloc(jblocks, size, &size); 3442 bp->b_lblkno = bp->b_blkno; 3443 bp->b_offset = bp->b_blkno * DEV_BSIZE; 3444 bp->b_bcount = size; 3445 bp->b_flags &= ~B_INVAL; 3446 bp->b_flags |= B_VALIDSUSPWRT | B_NOCOPY; 3447 /* 3448 * Initialize our jseg with cnt records. Assign the next 3449 * sequence number to it and link it in-order. 3450 */ 3451 cnt = MIN(cnt, (size / devbsize) * jrecmin); 3452 jseg->js_buf = bp; 3453 jseg->js_cnt = cnt; 3454 jseg->js_refs = cnt + 1; /* Self ref. */ 3455 jseg->js_size = size; 3456 jseg->js_seq = jblocks->jb_nextseq++; 3457 if (jblocks->jb_oldestseg == NULL) 3458 jblocks->jb_oldestseg = jseg; 3459 jseg->js_oldseq = jblocks->jb_oldestseg->js_seq; 3460 TAILQ_INSERT_TAIL(&jblocks->jb_segs, jseg, js_next); 3461 if (jblocks->jb_writeseg == NULL) 3462 jblocks->jb_writeseg = jseg; 3463 /* 3464 * Start filling in records from the pending list. 3465 */ 3466 data = bp->b_data; 3467 off = 0; 3468 3469 /* 3470 * Always put a header on the first block. 3471 * XXX As with below, there might not be a chance to get 3472 * into the loop. Ensure that something valid is written. 3473 */ 3474 jseg_write(ump, jseg, data); 3475 off += JREC_SIZE; 3476 data = bp->b_data + off; 3477 3478 /* 3479 * XXX Something is wrong here. There's no work to do, 3480 * but we need to perform and I/O and allow it to complete 3481 * anyways. 3482 */ 3483 if (LIST_EMPTY(&ump->softdep_journal_pending)) 3484 stat_emptyjblocks++; 3485 3486 while ((wk = LIST_FIRST(&ump->softdep_journal_pending)) 3487 != NULL) { 3488 if (cnt == 0) 3489 break; 3490 /* Place a segment header on every device block. */ 3491 if ((off % devbsize) == 0) { 3492 jseg_write(ump, jseg, data); 3493 off += JREC_SIZE; 3494 data = bp->b_data + off; 3495 } 3496 if (wk == needwk) 3497 needwk = NULL; 3498 remove_from_journal(wk); 3499 wk->wk_state |= INPROGRESS; 3500 WORKLIST_INSERT(&jseg->js_entries, wk); 3501 switch (wk->wk_type) { 3502 case D_JADDREF: 3503 jaddref_write(WK_JADDREF(wk), jseg, data); 3504 break; 3505 case D_JREMREF: 3506 jremref_write(WK_JREMREF(wk), jseg, data); 3507 break; 3508 case D_JMVREF: 3509 jmvref_write(WK_JMVREF(wk), jseg, data); 3510 break; 3511 case D_JNEWBLK: 3512 jnewblk_write(WK_JNEWBLK(wk), jseg, data); 3513 break; 3514 case D_JFREEBLK: 3515 jfreeblk_write(WK_JFREEBLK(wk), jseg, data); 3516 break; 3517 case D_JFREEFRAG: 3518 jfreefrag_write(WK_JFREEFRAG(wk), jseg, data); 3519 break; 3520 case D_JTRUNC: 3521 jtrunc_write(WK_JTRUNC(wk), jseg, data); 3522 break; 3523 case D_JFSYNC: 3524 jfsync_write(WK_JFSYNC(wk), jseg, data); 3525 break; 3526 default: 3527 panic("process_journal: Unknown type %s", 3528 TYPENAME(wk->wk_type)); 3529 /* NOTREACHED */ 3530 } 3531 off += JREC_SIZE; 3532 data = bp->b_data + off; 3533 cnt--; 3534 } 3535 3536 /* Clear any remaining space so we don't leak kernel data */ 3537 if (size > off) 3538 bzero(data, size - off); 3539 3540 /* 3541 * Write this one buffer and continue. 3542 */ 3543 segwritten = 1; 3544 jblocks->jb_needseg = 0; 3545 WORKLIST_INSERT(&bp->b_dep, &jseg->js_list); 3546 FREE_LOCK(ump); 3547 pbgetvp(ump->um_devvp, bp); 3548 /* 3549 * We only do the blocking wait once we find the journal 3550 * entry we're looking for. 3551 */ 3552 if (needwk == NULL && flags == MNT_WAIT) 3553 bwrite(bp); 3554 else 3555 bawrite(bp); 3556 ACQUIRE_LOCK(ump); 3557 } 3558 /* 3559 * If we wrote a segment issue a synchronize cache so the journal 3560 * is reflected on disk before the data is written. Since reclaiming 3561 * journal space also requires writing a journal record this 3562 * process also enforces a barrier before reclamation. 3563 */ 3564 if (segwritten && shouldflush) { 3565 softdep_synchronize(bio, ump, 3566 TAILQ_LAST(&jblocks->jb_segs, jseglst)); 3567 } else if (bio) 3568 g_destroy_bio(bio); 3569 /* 3570 * If we've suspended the filesystem because we ran out of journal 3571 * space either try to sync it here to make some progress or 3572 * unsuspend it if we already have. 3573 */ 3574 if (flags == 0 && jblocks->jb_suspended) { 3575 if (journal_unsuspend(ump)) 3576 return; 3577 FREE_LOCK(ump); 3578 VFS_SYNC(mp, MNT_NOWAIT); 3579 ffs_sbupdate(ump, MNT_WAIT, 0); 3580 ACQUIRE_LOCK(ump); 3581 } 3582 } 3583 3584 /* 3585 * Complete a jseg, allowing all dependencies awaiting journal writes 3586 * to proceed. Each journal dependency also attaches a jsegdep to dependent 3587 * structures so that the journal segment can be freed to reclaim space. 3588 */ 3589 static void 3590 complete_jseg(jseg) 3591 struct jseg *jseg; 3592 { 3593 struct worklist *wk; 3594 struct jmvref *jmvref; 3595 int waiting; 3596 #ifdef INVARIANTS 3597 int i = 0; 3598 #endif 3599 3600 while ((wk = LIST_FIRST(&jseg->js_entries)) != NULL) { 3601 WORKLIST_REMOVE(wk); 3602 waiting = wk->wk_state & IOWAITING; 3603 wk->wk_state &= ~(INPROGRESS | IOWAITING); 3604 wk->wk_state |= COMPLETE; 3605 KASSERT(i++ < jseg->js_cnt, 3606 ("handle_written_jseg: overflow %d >= %d", 3607 i - 1, jseg->js_cnt)); 3608 switch (wk->wk_type) { 3609 case D_JADDREF: 3610 handle_written_jaddref(WK_JADDREF(wk)); 3611 break; 3612 case D_JREMREF: 3613 handle_written_jremref(WK_JREMREF(wk)); 3614 break; 3615 case D_JMVREF: 3616 rele_jseg(jseg); /* No jsegdep. */ 3617 jmvref = WK_JMVREF(wk); 3618 LIST_REMOVE(jmvref, jm_deps); 3619 if ((jmvref->jm_pagedep->pd_state & ONWORKLIST) == 0) 3620 free_pagedep(jmvref->jm_pagedep); 3621 WORKITEM_FREE(jmvref, D_JMVREF); 3622 break; 3623 case D_JNEWBLK: 3624 handle_written_jnewblk(WK_JNEWBLK(wk)); 3625 break; 3626 case D_JFREEBLK: 3627 handle_written_jblkdep(&WK_JFREEBLK(wk)->jf_dep); 3628 break; 3629 case D_JTRUNC: 3630 handle_written_jblkdep(&WK_JTRUNC(wk)->jt_dep); 3631 break; 3632 case D_JFSYNC: 3633 rele_jseg(jseg); /* No jsegdep. */ 3634 WORKITEM_FREE(wk, D_JFSYNC); 3635 break; 3636 case D_JFREEFRAG: 3637 handle_written_jfreefrag(WK_JFREEFRAG(wk)); 3638 break; 3639 default: 3640 panic("handle_written_jseg: Unknown type %s", 3641 TYPENAME(wk->wk_type)); 3642 /* NOTREACHED */ 3643 } 3644 if (waiting) 3645 wakeup(wk); 3646 } 3647 /* Release the self reference so the structure may be freed. */ 3648 rele_jseg(jseg); 3649 } 3650 3651 /* 3652 * Determine which jsegs are ready for completion processing. Waits for 3653 * synchronize cache to complete as well as forcing in-order completion 3654 * of journal entries. 3655 */ 3656 static void 3657 complete_jsegs(jseg) 3658 struct jseg *jseg; 3659 { 3660 struct jblocks *jblocks; 3661 struct jseg *jsegn; 3662 3663 jblocks = jseg->js_jblocks; 3664 /* 3665 * Don't allow out of order completions. If this isn't the first 3666 * block wait for it to write before we're done. 3667 */ 3668 if (jseg != jblocks->jb_writeseg) 3669 return; 3670 /* Iterate through available jsegs processing their entries. */ 3671 while (jseg && (jseg->js_state & ALLCOMPLETE) == ALLCOMPLETE) { 3672 jblocks->jb_oldestwrseq = jseg->js_oldseq; 3673 jsegn = TAILQ_NEXT(jseg, js_next); 3674 complete_jseg(jseg); 3675 jseg = jsegn; 3676 } 3677 jblocks->jb_writeseg = jseg; 3678 /* 3679 * Attempt to free jsegs now that oldestwrseq may have advanced. 3680 */ 3681 free_jsegs(jblocks); 3682 } 3683 3684 /* 3685 * Mark a jseg as DEPCOMPLETE and throw away the buffer. Attempt to handle 3686 * the final completions. 3687 */ 3688 static void 3689 handle_written_jseg(jseg, bp) 3690 struct jseg *jseg; 3691 struct buf *bp; 3692 { 3693 3694 if (jseg->js_refs == 0) 3695 panic("handle_written_jseg: No self-reference on %p", jseg); 3696 jseg->js_state |= DEPCOMPLETE; 3697 /* 3698 * We'll never need this buffer again, set flags so it will be 3699 * discarded. 3700 */ 3701 bp->b_flags |= B_INVAL | B_NOCACHE; 3702 pbrelvp(bp); 3703 complete_jsegs(jseg); 3704 } 3705 3706 static inline struct jsegdep * 3707 inoref_jseg(inoref) 3708 struct inoref *inoref; 3709 { 3710 struct jsegdep *jsegdep; 3711 3712 jsegdep = inoref->if_jsegdep; 3713 inoref->if_jsegdep = NULL; 3714 3715 return (jsegdep); 3716 } 3717 3718 /* 3719 * Called once a jremref has made it to stable store. The jremref is marked 3720 * complete and we attempt to free it. Any pagedeps writes sleeping waiting 3721 * for the jremref to complete will be awoken by free_jremref. 3722 */ 3723 static void 3724 handle_written_jremref(jremref) 3725 struct jremref *jremref; 3726 { 3727 struct inodedep *inodedep; 3728 struct jsegdep *jsegdep; 3729 struct dirrem *dirrem; 3730 3731 /* Grab the jsegdep. */ 3732 jsegdep = inoref_jseg(&jremref->jr_ref); 3733 /* 3734 * Remove us from the inoref list. 3735 */ 3736 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 3737 0, &inodedep) == 0) 3738 panic("handle_written_jremref: Lost inodedep"); 3739 TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 3740 /* 3741 * Complete the dirrem. 3742 */ 3743 dirrem = jremref->jr_dirrem; 3744 jremref->jr_dirrem = NULL; 3745 LIST_REMOVE(jremref, jr_deps); 3746 jsegdep->jd_state |= jremref->jr_state & MKDIR_PARENT; 3747 jwork_insert(&dirrem->dm_jwork, jsegdep); 3748 if (LIST_EMPTY(&dirrem->dm_jremrefhd) && 3749 (dirrem->dm_state & COMPLETE) != 0) 3750 add_to_worklist(&dirrem->dm_list, 0); 3751 free_jremref(jremref); 3752 } 3753 3754 /* 3755 * Called once a jaddref has made it to stable store. The dependency is 3756 * marked complete and any dependent structures are added to the inode 3757 * bufwait list to be completed as soon as it is written. If a bitmap write 3758 * depends on this entry we move the inode into the inodedephd of the 3759 * bmsafemap dependency and attempt to remove the jaddref from the bmsafemap. 3760 */ 3761 static void 3762 handle_written_jaddref(jaddref) 3763 struct jaddref *jaddref; 3764 { 3765 struct jsegdep *jsegdep; 3766 struct inodedep *inodedep; 3767 struct diradd *diradd; 3768 struct mkdir *mkdir; 3769 3770 /* Grab the jsegdep. */ 3771 jsegdep = inoref_jseg(&jaddref->ja_ref); 3772 mkdir = NULL; 3773 diradd = NULL; 3774 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 3775 0, &inodedep) == 0) 3776 panic("handle_written_jaddref: Lost inodedep."); 3777 if (jaddref->ja_diradd == NULL) 3778 panic("handle_written_jaddref: No dependency"); 3779 if (jaddref->ja_diradd->da_list.wk_type == D_DIRADD) { 3780 diradd = jaddref->ja_diradd; 3781 WORKLIST_INSERT(&inodedep->id_bufwait, &diradd->da_list); 3782 } else if (jaddref->ja_state & MKDIR_PARENT) { 3783 mkdir = jaddref->ja_mkdir; 3784 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir->md_list); 3785 } else if (jaddref->ja_state & MKDIR_BODY) 3786 mkdir = jaddref->ja_mkdir; 3787 else 3788 panic("handle_written_jaddref: Unknown dependency %p", 3789 jaddref->ja_diradd); 3790 jaddref->ja_diradd = NULL; /* also clears ja_mkdir */ 3791 /* 3792 * Remove us from the inode list. 3793 */ 3794 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps); 3795 /* 3796 * The mkdir may be waiting on the jaddref to clear before freeing. 3797 */ 3798 if (mkdir) { 3799 KASSERT(mkdir->md_list.wk_type == D_MKDIR, 3800 ("handle_written_jaddref: Incorrect type for mkdir %s", 3801 TYPENAME(mkdir->md_list.wk_type))); 3802 mkdir->md_jaddref = NULL; 3803 diradd = mkdir->md_diradd; 3804 mkdir->md_state |= DEPCOMPLETE; 3805 complete_mkdir(mkdir); 3806 } 3807 jwork_insert(&diradd->da_jwork, jsegdep); 3808 if (jaddref->ja_state & NEWBLOCK) { 3809 inodedep->id_state |= ONDEPLIST; 3810 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_inodedephd, 3811 inodedep, id_deps); 3812 } 3813 free_jaddref(jaddref); 3814 } 3815 3816 /* 3817 * Called once a jnewblk journal is written. The allocdirect or allocindir 3818 * is placed in the bmsafemap to await notification of a written bitmap. If 3819 * the operation was canceled we add the segdep to the appropriate 3820 * dependency to free the journal space once the canceling operation 3821 * completes. 3822 */ 3823 static void 3824 handle_written_jnewblk(jnewblk) 3825 struct jnewblk *jnewblk; 3826 { 3827 struct bmsafemap *bmsafemap; 3828 struct freefrag *freefrag; 3829 struct freework *freework; 3830 struct jsegdep *jsegdep; 3831 struct newblk *newblk; 3832 3833 /* Grab the jsegdep. */ 3834 jsegdep = jnewblk->jn_jsegdep; 3835 jnewblk->jn_jsegdep = NULL; 3836 if (jnewblk->jn_dep == NULL) 3837 panic("handle_written_jnewblk: No dependency for the segdep."); 3838 switch (jnewblk->jn_dep->wk_type) { 3839 case D_NEWBLK: 3840 case D_ALLOCDIRECT: 3841 case D_ALLOCINDIR: 3842 /* 3843 * Add the written block to the bmsafemap so it can 3844 * be notified when the bitmap is on disk. 3845 */ 3846 newblk = WK_NEWBLK(jnewblk->jn_dep); 3847 newblk->nb_jnewblk = NULL; 3848 if ((newblk->nb_state & GOINGAWAY) == 0) { 3849 bmsafemap = newblk->nb_bmsafemap; 3850 newblk->nb_state |= ONDEPLIST; 3851 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, 3852 nb_deps); 3853 } 3854 jwork_insert(&newblk->nb_jwork, jsegdep); 3855 break; 3856 case D_FREEFRAG: 3857 /* 3858 * A newblock being removed by a freefrag when replaced by 3859 * frag extension. 3860 */ 3861 freefrag = WK_FREEFRAG(jnewblk->jn_dep); 3862 freefrag->ff_jdep = NULL; 3863 jwork_insert(&freefrag->ff_jwork, jsegdep); 3864 break; 3865 case D_FREEWORK: 3866 /* 3867 * A direct block was removed by truncate. 3868 */ 3869 freework = WK_FREEWORK(jnewblk->jn_dep); 3870 freework->fw_jnewblk = NULL; 3871 jwork_insert(&freework->fw_freeblks->fb_jwork, jsegdep); 3872 break; 3873 default: 3874 panic("handle_written_jnewblk: Unknown type %d.", 3875 jnewblk->jn_dep->wk_type); 3876 } 3877 jnewblk->jn_dep = NULL; 3878 free_jnewblk(jnewblk); 3879 } 3880 3881 /* 3882 * Cancel a jfreefrag that won't be needed, probably due to colliding with 3883 * an in-flight allocation that has not yet been committed. Divorce us 3884 * from the freefrag and mark it DEPCOMPLETE so that it may be added 3885 * to the worklist. 3886 */ 3887 static void 3888 cancel_jfreefrag(jfreefrag) 3889 struct jfreefrag *jfreefrag; 3890 { 3891 struct freefrag *freefrag; 3892 3893 if (jfreefrag->fr_jsegdep) { 3894 free_jsegdep(jfreefrag->fr_jsegdep); 3895 jfreefrag->fr_jsegdep = NULL; 3896 } 3897 freefrag = jfreefrag->fr_freefrag; 3898 jfreefrag->fr_freefrag = NULL; 3899 free_jfreefrag(jfreefrag); 3900 freefrag->ff_state |= DEPCOMPLETE; 3901 CTR1(KTR_SUJ, "cancel_jfreefrag: blkno %jd", freefrag->ff_blkno); 3902 } 3903 3904 /* 3905 * Free a jfreefrag when the parent freefrag is rendered obsolete. 3906 */ 3907 static void 3908 free_jfreefrag(jfreefrag) 3909 struct jfreefrag *jfreefrag; 3910 { 3911 3912 if (jfreefrag->fr_state & INPROGRESS) 3913 WORKLIST_REMOVE(&jfreefrag->fr_list); 3914 else if (jfreefrag->fr_state & ONWORKLIST) 3915 remove_from_journal(&jfreefrag->fr_list); 3916 if (jfreefrag->fr_freefrag != NULL) 3917 panic("free_jfreefrag: Still attached to a freefrag."); 3918 WORKITEM_FREE(jfreefrag, D_JFREEFRAG); 3919 } 3920 3921 /* 3922 * Called when the journal write for a jfreefrag completes. The parent 3923 * freefrag is added to the worklist if this completes its dependencies. 3924 */ 3925 static void 3926 handle_written_jfreefrag(jfreefrag) 3927 struct jfreefrag *jfreefrag; 3928 { 3929 struct jsegdep *jsegdep; 3930 struct freefrag *freefrag; 3931 3932 /* Grab the jsegdep. */ 3933 jsegdep = jfreefrag->fr_jsegdep; 3934 jfreefrag->fr_jsegdep = NULL; 3935 freefrag = jfreefrag->fr_freefrag; 3936 if (freefrag == NULL) 3937 panic("handle_written_jfreefrag: No freefrag."); 3938 freefrag->ff_state |= DEPCOMPLETE; 3939 freefrag->ff_jdep = NULL; 3940 jwork_insert(&freefrag->ff_jwork, jsegdep); 3941 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 3942 add_to_worklist(&freefrag->ff_list, 0); 3943 jfreefrag->fr_freefrag = NULL; 3944 free_jfreefrag(jfreefrag); 3945 } 3946 3947 /* 3948 * Called when the journal write for a jfreeblk completes. The jfreeblk 3949 * is removed from the freeblks list of pending journal writes and the 3950 * jsegdep is moved to the freeblks jwork to be completed when all blocks 3951 * have been reclaimed. 3952 */ 3953 static void 3954 handle_written_jblkdep(jblkdep) 3955 struct jblkdep *jblkdep; 3956 { 3957 struct freeblks *freeblks; 3958 struct jsegdep *jsegdep; 3959 3960 /* Grab the jsegdep. */ 3961 jsegdep = jblkdep->jb_jsegdep; 3962 jblkdep->jb_jsegdep = NULL; 3963 freeblks = jblkdep->jb_freeblks; 3964 LIST_REMOVE(jblkdep, jb_deps); 3965 jwork_insert(&freeblks->fb_jwork, jsegdep); 3966 /* 3967 * If the freeblks is all journaled, we can add it to the worklist. 3968 */ 3969 if (LIST_EMPTY(&freeblks->fb_jblkdephd) && 3970 (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 3971 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 3972 3973 free_jblkdep(jblkdep); 3974 } 3975 3976 static struct jsegdep * 3977 newjsegdep(struct worklist *wk) 3978 { 3979 struct jsegdep *jsegdep; 3980 3981 jsegdep = malloc(sizeof(*jsegdep), M_JSEGDEP, M_SOFTDEP_FLAGS); 3982 workitem_alloc(&jsegdep->jd_list, D_JSEGDEP, wk->wk_mp); 3983 jsegdep->jd_seg = NULL; 3984 3985 return (jsegdep); 3986 } 3987 3988 static struct jmvref * 3989 newjmvref(dp, ino, oldoff, newoff) 3990 struct inode *dp; 3991 ino_t ino; 3992 off_t oldoff; 3993 off_t newoff; 3994 { 3995 struct jmvref *jmvref; 3996 3997 jmvref = malloc(sizeof(*jmvref), M_JMVREF, M_SOFTDEP_FLAGS); 3998 workitem_alloc(&jmvref->jm_list, D_JMVREF, UFSTOVFS(dp->i_ump)); 3999 jmvref->jm_list.wk_state = ATTACHED | DEPCOMPLETE; 4000 jmvref->jm_parent = dp->i_number; 4001 jmvref->jm_ino = ino; 4002 jmvref->jm_oldoff = oldoff; 4003 jmvref->jm_newoff = newoff; 4004 4005 return (jmvref); 4006 } 4007 4008 /* 4009 * Allocate a new jremref that tracks the removal of ip from dp with the 4010 * directory entry offset of diroff. Mark the entry as ATTACHED and 4011 * DEPCOMPLETE as we have all the information required for the journal write 4012 * and the directory has already been removed from the buffer. The caller 4013 * is responsible for linking the jremref into the pagedep and adding it 4014 * to the journal to write. The MKDIR_PARENT flag is set if we're doing 4015 * a DOTDOT addition so handle_workitem_remove() can properly assign 4016 * the jsegdep when we're done. 4017 */ 4018 static struct jremref * 4019 newjremref(struct dirrem *dirrem, struct inode *dp, struct inode *ip, 4020 off_t diroff, nlink_t nlink) 4021 { 4022 struct jremref *jremref; 4023 4024 jremref = malloc(sizeof(*jremref), M_JREMREF, M_SOFTDEP_FLAGS); 4025 workitem_alloc(&jremref->jr_list, D_JREMREF, UFSTOVFS(dp->i_ump)); 4026 jremref->jr_state = ATTACHED; 4027 newinoref(&jremref->jr_ref, ip->i_number, dp->i_number, diroff, 4028 nlink, ip->i_mode); 4029 jremref->jr_dirrem = dirrem; 4030 4031 return (jremref); 4032 } 4033 4034 static inline void 4035 newinoref(struct inoref *inoref, ino_t ino, ino_t parent, off_t diroff, 4036 nlink_t nlink, uint16_t mode) 4037 { 4038 4039 inoref->if_jsegdep = newjsegdep(&inoref->if_list); 4040 inoref->if_diroff = diroff; 4041 inoref->if_ino = ino; 4042 inoref->if_parent = parent; 4043 inoref->if_nlink = nlink; 4044 inoref->if_mode = mode; 4045 } 4046 4047 /* 4048 * Allocate a new jaddref to track the addition of ino to dp at diroff. The 4049 * directory offset may not be known until later. The caller is responsible 4050 * adding the entry to the journal when this information is available. nlink 4051 * should be the link count prior to the addition and mode is only required 4052 * to have the correct FMT. 4053 */ 4054 static struct jaddref * 4055 newjaddref(struct inode *dp, ino_t ino, off_t diroff, int16_t nlink, 4056 uint16_t mode) 4057 { 4058 struct jaddref *jaddref; 4059 4060 jaddref = malloc(sizeof(*jaddref), M_JADDREF, M_SOFTDEP_FLAGS); 4061 workitem_alloc(&jaddref->ja_list, D_JADDREF, UFSTOVFS(dp->i_ump)); 4062 jaddref->ja_state = ATTACHED; 4063 jaddref->ja_mkdir = NULL; 4064 newinoref(&jaddref->ja_ref, ino, dp->i_number, diroff, nlink, mode); 4065 4066 return (jaddref); 4067 } 4068 4069 /* 4070 * Create a new free dependency for a freework. The caller is responsible 4071 * for adjusting the reference count when it has the lock held. The freedep 4072 * will track an outstanding bitmap write that will ultimately clear the 4073 * freework to continue. 4074 */ 4075 static struct freedep * 4076 newfreedep(struct freework *freework) 4077 { 4078 struct freedep *freedep; 4079 4080 freedep = malloc(sizeof(*freedep), M_FREEDEP, M_SOFTDEP_FLAGS); 4081 workitem_alloc(&freedep->fd_list, D_FREEDEP, freework->fw_list.wk_mp); 4082 freedep->fd_freework = freework; 4083 4084 return (freedep); 4085 } 4086 4087 /* 4088 * Free a freedep structure once the buffer it is linked to is written. If 4089 * this is the last reference to the freework schedule it for completion. 4090 */ 4091 static void 4092 free_freedep(freedep) 4093 struct freedep *freedep; 4094 { 4095 struct freework *freework; 4096 4097 freework = freedep->fd_freework; 4098 freework->fw_freeblks->fb_cgwait--; 4099 if (--freework->fw_ref == 0) 4100 freework_enqueue(freework); 4101 WORKITEM_FREE(freedep, D_FREEDEP); 4102 } 4103 4104 /* 4105 * Allocate a new freework structure that may be a level in an indirect 4106 * when parent is not NULL or a top level block when it is. The top level 4107 * freework structures are allocated without the per-filesystem lock held 4108 * and before the freeblks is visible outside of softdep_setup_freeblocks(). 4109 */ 4110 static struct freework * 4111 newfreework(ump, freeblks, parent, lbn, nb, frags, off, journal) 4112 struct ufsmount *ump; 4113 struct freeblks *freeblks; 4114 struct freework *parent; 4115 ufs_lbn_t lbn; 4116 ufs2_daddr_t nb; 4117 int frags; 4118 int off; 4119 int journal; 4120 { 4121 struct freework *freework; 4122 4123 freework = malloc(sizeof(*freework), M_FREEWORK, M_SOFTDEP_FLAGS); 4124 workitem_alloc(&freework->fw_list, D_FREEWORK, freeblks->fb_list.wk_mp); 4125 freework->fw_state = ATTACHED; 4126 freework->fw_jnewblk = NULL; 4127 freework->fw_freeblks = freeblks; 4128 freework->fw_parent = parent; 4129 freework->fw_lbn = lbn; 4130 freework->fw_blkno = nb; 4131 freework->fw_frags = frags; 4132 freework->fw_indir = NULL; 4133 freework->fw_ref = (MOUNTEDSUJ(UFSTOVFS(ump)) == 0 || lbn >= -NXADDR) 4134 ? 0 : NINDIR(ump->um_fs) + 1; 4135 freework->fw_start = freework->fw_off = off; 4136 if (journal) 4137 newjfreeblk(freeblks, lbn, nb, frags); 4138 if (parent == NULL) { 4139 ACQUIRE_LOCK(ump); 4140 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 4141 freeblks->fb_ref++; 4142 FREE_LOCK(ump); 4143 } 4144 4145 return (freework); 4146 } 4147 4148 /* 4149 * Eliminate a jfreeblk for a block that does not need journaling. 4150 */ 4151 static void 4152 cancel_jfreeblk(freeblks, blkno) 4153 struct freeblks *freeblks; 4154 ufs2_daddr_t blkno; 4155 { 4156 struct jfreeblk *jfreeblk; 4157 struct jblkdep *jblkdep; 4158 4159 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) { 4160 if (jblkdep->jb_list.wk_type != D_JFREEBLK) 4161 continue; 4162 jfreeblk = WK_JFREEBLK(&jblkdep->jb_list); 4163 if (jfreeblk->jf_blkno == blkno) 4164 break; 4165 } 4166 if (jblkdep == NULL) 4167 return; 4168 CTR1(KTR_SUJ, "cancel_jfreeblk: blkno %jd", blkno); 4169 free_jsegdep(jblkdep->jb_jsegdep); 4170 LIST_REMOVE(jblkdep, jb_deps); 4171 WORKITEM_FREE(jfreeblk, D_JFREEBLK); 4172 } 4173 4174 /* 4175 * Allocate a new jfreeblk to journal top level block pointer when truncating 4176 * a file. The caller must add this to the worklist when the per-filesystem 4177 * lock is held. 4178 */ 4179 static struct jfreeblk * 4180 newjfreeblk(freeblks, lbn, blkno, frags) 4181 struct freeblks *freeblks; 4182 ufs_lbn_t lbn; 4183 ufs2_daddr_t blkno; 4184 int frags; 4185 { 4186 struct jfreeblk *jfreeblk; 4187 4188 jfreeblk = malloc(sizeof(*jfreeblk), M_JFREEBLK, M_SOFTDEP_FLAGS); 4189 workitem_alloc(&jfreeblk->jf_dep.jb_list, D_JFREEBLK, 4190 freeblks->fb_list.wk_mp); 4191 jfreeblk->jf_dep.jb_jsegdep = newjsegdep(&jfreeblk->jf_dep.jb_list); 4192 jfreeblk->jf_dep.jb_freeblks = freeblks; 4193 jfreeblk->jf_ino = freeblks->fb_inum; 4194 jfreeblk->jf_lbn = lbn; 4195 jfreeblk->jf_blkno = blkno; 4196 jfreeblk->jf_frags = frags; 4197 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jfreeblk->jf_dep, jb_deps); 4198 4199 return (jfreeblk); 4200 } 4201 4202 /* 4203 * The journal is only prepared to handle full-size block numbers, so we 4204 * have to adjust the record to reflect the change to a full-size block. 4205 * For example, suppose we have a block made up of fragments 8-15 and 4206 * want to free its last two fragments. We are given a request that says: 4207 * FREEBLK ino=5, blkno=14, lbn=0, frags=2, oldfrags=0 4208 * where frags are the number of fragments to free and oldfrags are the 4209 * number of fragments to keep. To block align it, we have to change it to 4210 * have a valid full-size blkno, so it becomes: 4211 * FREEBLK ino=5, blkno=8, lbn=0, frags=2, oldfrags=6 4212 */ 4213 static void 4214 adjust_newfreework(freeblks, frag_offset) 4215 struct freeblks *freeblks; 4216 int frag_offset; 4217 { 4218 struct jfreeblk *jfreeblk; 4219 4220 KASSERT((LIST_FIRST(&freeblks->fb_jblkdephd) != NULL && 4221 LIST_FIRST(&freeblks->fb_jblkdephd)->jb_list.wk_type == D_JFREEBLK), 4222 ("adjust_newfreework: Missing freeblks dependency")); 4223 4224 jfreeblk = WK_JFREEBLK(LIST_FIRST(&freeblks->fb_jblkdephd)); 4225 jfreeblk->jf_blkno -= frag_offset; 4226 jfreeblk->jf_frags += frag_offset; 4227 } 4228 4229 /* 4230 * Allocate a new jtrunc to track a partial truncation. 4231 */ 4232 static struct jtrunc * 4233 newjtrunc(freeblks, size, extsize) 4234 struct freeblks *freeblks; 4235 off_t size; 4236 int extsize; 4237 { 4238 struct jtrunc *jtrunc; 4239 4240 jtrunc = malloc(sizeof(*jtrunc), M_JTRUNC, M_SOFTDEP_FLAGS); 4241 workitem_alloc(&jtrunc->jt_dep.jb_list, D_JTRUNC, 4242 freeblks->fb_list.wk_mp); 4243 jtrunc->jt_dep.jb_jsegdep = newjsegdep(&jtrunc->jt_dep.jb_list); 4244 jtrunc->jt_dep.jb_freeblks = freeblks; 4245 jtrunc->jt_ino = freeblks->fb_inum; 4246 jtrunc->jt_size = size; 4247 jtrunc->jt_extsize = extsize; 4248 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jtrunc->jt_dep, jb_deps); 4249 4250 return (jtrunc); 4251 } 4252 4253 /* 4254 * If we're canceling a new bitmap we have to search for another ref 4255 * to move into the bmsafemap dep. This might be better expressed 4256 * with another structure. 4257 */ 4258 static void 4259 move_newblock_dep(jaddref, inodedep) 4260 struct jaddref *jaddref; 4261 struct inodedep *inodedep; 4262 { 4263 struct inoref *inoref; 4264 struct jaddref *jaddrefn; 4265 4266 jaddrefn = NULL; 4267 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4268 inoref = TAILQ_NEXT(inoref, if_deps)) { 4269 if ((jaddref->ja_state & NEWBLOCK) && 4270 inoref->if_list.wk_type == D_JADDREF) { 4271 jaddrefn = (struct jaddref *)inoref; 4272 break; 4273 } 4274 } 4275 if (jaddrefn == NULL) 4276 return; 4277 jaddrefn->ja_state &= ~(ATTACHED | UNDONE); 4278 jaddrefn->ja_state |= jaddref->ja_state & 4279 (ATTACHED | UNDONE | NEWBLOCK); 4280 jaddref->ja_state &= ~(ATTACHED | UNDONE | NEWBLOCK); 4281 jaddref->ja_state |= ATTACHED; 4282 LIST_REMOVE(jaddref, ja_bmdeps); 4283 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_jaddrefhd, jaddrefn, 4284 ja_bmdeps); 4285 } 4286 4287 /* 4288 * Cancel a jaddref either before it has been written or while it is being 4289 * written. This happens when a link is removed before the add reaches 4290 * the disk. The jaddref dependency is kept linked into the bmsafemap 4291 * and inode to prevent the link count or bitmap from reaching the disk 4292 * until handle_workitem_remove() re-adjusts the counts and bitmaps as 4293 * required. 4294 * 4295 * Returns 1 if the canceled addref requires journaling of the remove and 4296 * 0 otherwise. 4297 */ 4298 static int 4299 cancel_jaddref(jaddref, inodedep, wkhd) 4300 struct jaddref *jaddref; 4301 struct inodedep *inodedep; 4302 struct workhead *wkhd; 4303 { 4304 struct inoref *inoref; 4305 struct jsegdep *jsegdep; 4306 int needsj; 4307 4308 KASSERT((jaddref->ja_state & COMPLETE) == 0, 4309 ("cancel_jaddref: Canceling complete jaddref")); 4310 if (jaddref->ja_state & (INPROGRESS | COMPLETE)) 4311 needsj = 1; 4312 else 4313 needsj = 0; 4314 if (inodedep == NULL) 4315 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 4316 0, &inodedep) == 0) 4317 panic("cancel_jaddref: Lost inodedep"); 4318 /* 4319 * We must adjust the nlink of any reference operation that follows 4320 * us so that it is consistent with the in-memory reference. This 4321 * ensures that inode nlink rollbacks always have the correct link. 4322 */ 4323 if (needsj == 0) { 4324 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4325 inoref = TAILQ_NEXT(inoref, if_deps)) { 4326 if (inoref->if_state & GOINGAWAY) 4327 break; 4328 inoref->if_nlink--; 4329 } 4330 } 4331 jsegdep = inoref_jseg(&jaddref->ja_ref); 4332 if (jaddref->ja_state & NEWBLOCK) 4333 move_newblock_dep(jaddref, inodedep); 4334 wake_worklist(&jaddref->ja_list); 4335 jaddref->ja_mkdir = NULL; 4336 if (jaddref->ja_state & INPROGRESS) { 4337 jaddref->ja_state &= ~INPROGRESS; 4338 WORKLIST_REMOVE(&jaddref->ja_list); 4339 jwork_insert(wkhd, jsegdep); 4340 } else { 4341 free_jsegdep(jsegdep); 4342 if (jaddref->ja_state & DEPCOMPLETE) 4343 remove_from_journal(&jaddref->ja_list); 4344 } 4345 jaddref->ja_state |= (GOINGAWAY | DEPCOMPLETE); 4346 /* 4347 * Leave NEWBLOCK jaddrefs on the inodedep so handle_workitem_remove 4348 * can arrange for them to be freed with the bitmap. Otherwise we 4349 * no longer need this addref attached to the inoreflst and it 4350 * will incorrectly adjust nlink if we leave it. 4351 */ 4352 if ((jaddref->ja_state & NEWBLOCK) == 0) { 4353 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 4354 if_deps); 4355 jaddref->ja_state |= COMPLETE; 4356 free_jaddref(jaddref); 4357 return (needsj); 4358 } 4359 /* 4360 * Leave the head of the list for jsegdeps for fast merging. 4361 */ 4362 if (LIST_FIRST(wkhd) != NULL) { 4363 jaddref->ja_state |= ONWORKLIST; 4364 LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list); 4365 } else 4366 WORKLIST_INSERT(wkhd, &jaddref->ja_list); 4367 4368 return (needsj); 4369 } 4370 4371 /* 4372 * Attempt to free a jaddref structure when some work completes. This 4373 * should only succeed once the entry is written and all dependencies have 4374 * been notified. 4375 */ 4376 static void 4377 free_jaddref(jaddref) 4378 struct jaddref *jaddref; 4379 { 4380 4381 if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE) 4382 return; 4383 if (jaddref->ja_ref.if_jsegdep) 4384 panic("free_jaddref: segdep attached to jaddref %p(0x%X)\n", 4385 jaddref, jaddref->ja_state); 4386 if (jaddref->ja_state & NEWBLOCK) 4387 LIST_REMOVE(jaddref, ja_bmdeps); 4388 if (jaddref->ja_state & (INPROGRESS | ONWORKLIST)) 4389 panic("free_jaddref: Bad state %p(0x%X)", 4390 jaddref, jaddref->ja_state); 4391 if (jaddref->ja_mkdir != NULL) 4392 panic("free_jaddref: Work pending, 0x%X\n", jaddref->ja_state); 4393 WORKITEM_FREE(jaddref, D_JADDREF); 4394 } 4395 4396 /* 4397 * Free a jremref structure once it has been written or discarded. 4398 */ 4399 static void 4400 free_jremref(jremref) 4401 struct jremref *jremref; 4402 { 4403 4404 if (jremref->jr_ref.if_jsegdep) 4405 free_jsegdep(jremref->jr_ref.if_jsegdep); 4406 if (jremref->jr_state & INPROGRESS) 4407 panic("free_jremref: IO still pending"); 4408 WORKITEM_FREE(jremref, D_JREMREF); 4409 } 4410 4411 /* 4412 * Free a jnewblk structure. 4413 */ 4414 static void 4415 free_jnewblk(jnewblk) 4416 struct jnewblk *jnewblk; 4417 { 4418 4419 if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE) 4420 return; 4421 LIST_REMOVE(jnewblk, jn_deps); 4422 if (jnewblk->jn_dep != NULL) 4423 panic("free_jnewblk: Dependency still attached."); 4424 WORKITEM_FREE(jnewblk, D_JNEWBLK); 4425 } 4426 4427 /* 4428 * Cancel a jnewblk which has been been made redundant by frag extension. 4429 */ 4430 static void 4431 cancel_jnewblk(jnewblk, wkhd) 4432 struct jnewblk *jnewblk; 4433 struct workhead *wkhd; 4434 { 4435 struct jsegdep *jsegdep; 4436 4437 CTR1(KTR_SUJ, "cancel_jnewblk: blkno %jd", jnewblk->jn_blkno); 4438 jsegdep = jnewblk->jn_jsegdep; 4439 if (jnewblk->jn_jsegdep == NULL || jnewblk->jn_dep == NULL) 4440 panic("cancel_jnewblk: Invalid state"); 4441 jnewblk->jn_jsegdep = NULL; 4442 jnewblk->jn_dep = NULL; 4443 jnewblk->jn_state |= GOINGAWAY; 4444 if (jnewblk->jn_state & INPROGRESS) { 4445 jnewblk->jn_state &= ~INPROGRESS; 4446 WORKLIST_REMOVE(&jnewblk->jn_list); 4447 jwork_insert(wkhd, jsegdep); 4448 } else { 4449 free_jsegdep(jsegdep); 4450 remove_from_journal(&jnewblk->jn_list); 4451 } 4452 wake_worklist(&jnewblk->jn_list); 4453 WORKLIST_INSERT(wkhd, &jnewblk->jn_list); 4454 } 4455 4456 static void 4457 free_jblkdep(jblkdep) 4458 struct jblkdep *jblkdep; 4459 { 4460 4461 if (jblkdep->jb_list.wk_type == D_JFREEBLK) 4462 WORKITEM_FREE(jblkdep, D_JFREEBLK); 4463 else if (jblkdep->jb_list.wk_type == D_JTRUNC) 4464 WORKITEM_FREE(jblkdep, D_JTRUNC); 4465 else 4466 panic("free_jblkdep: Unexpected type %s", 4467 TYPENAME(jblkdep->jb_list.wk_type)); 4468 } 4469 4470 /* 4471 * Free a single jseg once it is no longer referenced in memory or on 4472 * disk. Reclaim journal blocks and dependencies waiting for the segment 4473 * to disappear. 4474 */ 4475 static void 4476 free_jseg(jseg, jblocks) 4477 struct jseg *jseg; 4478 struct jblocks *jblocks; 4479 { 4480 struct freework *freework; 4481 4482 /* 4483 * Free freework structures that were lingering to indicate freed 4484 * indirect blocks that forced journal write ordering on reallocate. 4485 */ 4486 while ((freework = LIST_FIRST(&jseg->js_indirs)) != NULL) 4487 indirblk_remove(freework); 4488 if (jblocks->jb_oldestseg == jseg) 4489 jblocks->jb_oldestseg = TAILQ_NEXT(jseg, js_next); 4490 TAILQ_REMOVE(&jblocks->jb_segs, jseg, js_next); 4491 jblocks_free(jblocks, jseg->js_list.wk_mp, jseg->js_size); 4492 KASSERT(LIST_EMPTY(&jseg->js_entries), 4493 ("free_jseg: Freed jseg has valid entries.")); 4494 WORKITEM_FREE(jseg, D_JSEG); 4495 } 4496 4497 /* 4498 * Free all jsegs that meet the criteria for being reclaimed and update 4499 * oldestseg. 4500 */ 4501 static void 4502 free_jsegs(jblocks) 4503 struct jblocks *jblocks; 4504 { 4505 struct jseg *jseg; 4506 4507 /* 4508 * Free only those jsegs which have none allocated before them to 4509 * preserve the journal space ordering. 4510 */ 4511 while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) { 4512 /* 4513 * Only reclaim space when nothing depends on this journal 4514 * set and another set has written that it is no longer 4515 * valid. 4516 */ 4517 if (jseg->js_refs != 0) { 4518 jblocks->jb_oldestseg = jseg; 4519 return; 4520 } 4521 if ((jseg->js_state & ALLCOMPLETE) != ALLCOMPLETE) 4522 break; 4523 if (jseg->js_seq > jblocks->jb_oldestwrseq) 4524 break; 4525 /* 4526 * We can free jsegs that didn't write entries when 4527 * oldestwrseq == js_seq. 4528 */ 4529 if (jseg->js_seq == jblocks->jb_oldestwrseq && 4530 jseg->js_cnt != 0) 4531 break; 4532 free_jseg(jseg, jblocks); 4533 } 4534 /* 4535 * If we exited the loop above we still must discover the 4536 * oldest valid segment. 4537 */ 4538 if (jseg) 4539 for (jseg = jblocks->jb_oldestseg; jseg != NULL; 4540 jseg = TAILQ_NEXT(jseg, js_next)) 4541 if (jseg->js_refs != 0) 4542 break; 4543 jblocks->jb_oldestseg = jseg; 4544 /* 4545 * The journal has no valid records but some jsegs may still be 4546 * waiting on oldestwrseq to advance. We force a small record 4547 * out to permit these lingering records to be reclaimed. 4548 */ 4549 if (jblocks->jb_oldestseg == NULL && !TAILQ_EMPTY(&jblocks->jb_segs)) 4550 jblocks->jb_needseg = 1; 4551 } 4552 4553 /* 4554 * Release one reference to a jseg and free it if the count reaches 0. This 4555 * should eventually reclaim journal space as well. 4556 */ 4557 static void 4558 rele_jseg(jseg) 4559 struct jseg *jseg; 4560 { 4561 4562 KASSERT(jseg->js_refs > 0, 4563 ("free_jseg: Invalid refcnt %d", jseg->js_refs)); 4564 if (--jseg->js_refs != 0) 4565 return; 4566 free_jsegs(jseg->js_jblocks); 4567 } 4568 4569 /* 4570 * Release a jsegdep and decrement the jseg count. 4571 */ 4572 static void 4573 free_jsegdep(jsegdep) 4574 struct jsegdep *jsegdep; 4575 { 4576 4577 if (jsegdep->jd_seg) 4578 rele_jseg(jsegdep->jd_seg); 4579 WORKITEM_FREE(jsegdep, D_JSEGDEP); 4580 } 4581 4582 /* 4583 * Wait for a journal item to make it to disk. Initiate journal processing 4584 * if required. 4585 */ 4586 static int 4587 jwait(wk, waitfor) 4588 struct worklist *wk; 4589 int waitfor; 4590 { 4591 4592 LOCK_OWNED(VFSTOUFS(wk->wk_mp)); 4593 /* 4594 * Blocking journal waits cause slow synchronous behavior. Record 4595 * stats on the frequency of these blocking operations. 4596 */ 4597 if (waitfor == MNT_WAIT) { 4598 stat_journal_wait++; 4599 switch (wk->wk_type) { 4600 case D_JREMREF: 4601 case D_JMVREF: 4602 stat_jwait_filepage++; 4603 break; 4604 case D_JTRUNC: 4605 case D_JFREEBLK: 4606 stat_jwait_freeblks++; 4607 break; 4608 case D_JNEWBLK: 4609 stat_jwait_newblk++; 4610 break; 4611 case D_JADDREF: 4612 stat_jwait_inode++; 4613 break; 4614 default: 4615 break; 4616 } 4617 } 4618 /* 4619 * If IO has not started we process the journal. We can't mark the 4620 * worklist item as IOWAITING because we drop the lock while 4621 * processing the journal and the worklist entry may be freed after 4622 * this point. The caller may call back in and re-issue the request. 4623 */ 4624 if ((wk->wk_state & INPROGRESS) == 0) { 4625 softdep_process_journal(wk->wk_mp, wk, waitfor); 4626 if (waitfor != MNT_WAIT) 4627 return (EBUSY); 4628 return (0); 4629 } 4630 if (waitfor != MNT_WAIT) 4631 return (EBUSY); 4632 wait_worklist(wk, "jwait"); 4633 return (0); 4634 } 4635 4636 /* 4637 * Lookup an inodedep based on an inode pointer and set the nlinkdelta as 4638 * appropriate. This is a convenience function to reduce duplicate code 4639 * for the setup and revert functions below. 4640 */ 4641 static struct inodedep * 4642 inodedep_lookup_ip(ip) 4643 struct inode *ip; 4644 { 4645 struct inodedep *inodedep; 4646 4647 KASSERT(ip->i_nlink >= ip->i_effnlink, 4648 ("inodedep_lookup_ip: bad delta")); 4649 (void) inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, DEPALLOC, 4650 &inodedep); 4651 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 4652 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 4653 4654 return (inodedep); 4655 } 4656 4657 /* 4658 * Called prior to creating a new inode and linking it to a directory. The 4659 * jaddref structure must already be allocated by softdep_setup_inomapdep 4660 * and it is discovered here so we can initialize the mode and update 4661 * nlinkdelta. 4662 */ 4663 void 4664 softdep_setup_create(dp, ip) 4665 struct inode *dp; 4666 struct inode *ip; 4667 { 4668 struct inodedep *inodedep; 4669 struct jaddref *jaddref; 4670 struct vnode *dvp; 4671 4672 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4673 ("softdep_setup_create called on non-softdep filesystem")); 4674 KASSERT(ip->i_nlink == 1, 4675 ("softdep_setup_create: Invalid link count.")); 4676 dvp = ITOV(dp); 4677 ACQUIRE_LOCK(dp->i_ump); 4678 inodedep = inodedep_lookup_ip(ip); 4679 if (DOINGSUJ(dvp)) { 4680 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4681 inoreflst); 4682 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 4683 ("softdep_setup_create: No addref structure present.")); 4684 } 4685 softdep_prelink(dvp, NULL); 4686 FREE_LOCK(dp->i_ump); 4687 } 4688 4689 /* 4690 * Create a jaddref structure to track the addition of a DOTDOT link when 4691 * we are reparenting an inode as part of a rename. This jaddref will be 4692 * found by softdep_setup_directory_change. Adjusts nlinkdelta for 4693 * non-journaling softdep. 4694 */ 4695 void 4696 softdep_setup_dotdot_link(dp, ip) 4697 struct inode *dp; 4698 struct inode *ip; 4699 { 4700 struct inodedep *inodedep; 4701 struct jaddref *jaddref; 4702 struct vnode *dvp; 4703 4704 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4705 ("softdep_setup_dotdot_link called on non-softdep filesystem")); 4706 dvp = ITOV(dp); 4707 jaddref = NULL; 4708 /* 4709 * We don't set MKDIR_PARENT as this is not tied to a mkdir and 4710 * is used as a normal link would be. 4711 */ 4712 if (DOINGSUJ(dvp)) 4713 jaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 4714 dp->i_effnlink - 1, dp->i_mode); 4715 ACQUIRE_LOCK(dp->i_ump); 4716 inodedep = inodedep_lookup_ip(dp); 4717 if (jaddref) 4718 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4719 if_deps); 4720 softdep_prelink(dvp, ITOV(ip)); 4721 FREE_LOCK(dp->i_ump); 4722 } 4723 4724 /* 4725 * Create a jaddref structure to track a new link to an inode. The directory 4726 * offset is not known until softdep_setup_directory_add or 4727 * softdep_setup_directory_change. Adjusts nlinkdelta for non-journaling 4728 * softdep. 4729 */ 4730 void 4731 softdep_setup_link(dp, ip) 4732 struct inode *dp; 4733 struct inode *ip; 4734 { 4735 struct inodedep *inodedep; 4736 struct jaddref *jaddref; 4737 struct vnode *dvp; 4738 4739 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4740 ("softdep_setup_link called on non-softdep filesystem")); 4741 dvp = ITOV(dp); 4742 jaddref = NULL; 4743 if (DOINGSUJ(dvp)) 4744 jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1, 4745 ip->i_mode); 4746 ACQUIRE_LOCK(dp->i_ump); 4747 inodedep = inodedep_lookup_ip(ip); 4748 if (jaddref) 4749 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4750 if_deps); 4751 softdep_prelink(dvp, ITOV(ip)); 4752 FREE_LOCK(dp->i_ump); 4753 } 4754 4755 /* 4756 * Called to create the jaddref structures to track . and .. references as 4757 * well as lookup and further initialize the incomplete jaddref created 4758 * by softdep_setup_inomapdep when the inode was allocated. Adjusts 4759 * nlinkdelta for non-journaling softdep. 4760 */ 4761 void 4762 softdep_setup_mkdir(dp, ip) 4763 struct inode *dp; 4764 struct inode *ip; 4765 { 4766 struct inodedep *inodedep; 4767 struct jaddref *dotdotaddref; 4768 struct jaddref *dotaddref; 4769 struct jaddref *jaddref; 4770 struct vnode *dvp; 4771 4772 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4773 ("softdep_setup_mkdir called on non-softdep filesystem")); 4774 dvp = ITOV(dp); 4775 dotaddref = dotdotaddref = NULL; 4776 if (DOINGSUJ(dvp)) { 4777 dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1, 4778 ip->i_mode); 4779 dotaddref->ja_state |= MKDIR_BODY; 4780 dotdotaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 4781 dp->i_effnlink - 1, dp->i_mode); 4782 dotdotaddref->ja_state |= MKDIR_PARENT; 4783 } 4784 ACQUIRE_LOCK(dp->i_ump); 4785 inodedep = inodedep_lookup_ip(ip); 4786 if (DOINGSUJ(dvp)) { 4787 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4788 inoreflst); 4789 KASSERT(jaddref != NULL, 4790 ("softdep_setup_mkdir: No addref structure present.")); 4791 KASSERT(jaddref->ja_parent == dp->i_number, 4792 ("softdep_setup_mkdir: bad parent %ju", 4793 (uintmax_t)jaddref->ja_parent)); 4794 TAILQ_INSERT_BEFORE(&jaddref->ja_ref, &dotaddref->ja_ref, 4795 if_deps); 4796 } 4797 inodedep = inodedep_lookup_ip(dp); 4798 if (DOINGSUJ(dvp)) 4799 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, 4800 &dotdotaddref->ja_ref, if_deps); 4801 softdep_prelink(ITOV(dp), NULL); 4802 FREE_LOCK(dp->i_ump); 4803 } 4804 4805 /* 4806 * Called to track nlinkdelta of the inode and parent directories prior to 4807 * unlinking a directory. 4808 */ 4809 void 4810 softdep_setup_rmdir(dp, ip) 4811 struct inode *dp; 4812 struct inode *ip; 4813 { 4814 struct vnode *dvp; 4815 4816 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4817 ("softdep_setup_rmdir called on non-softdep filesystem")); 4818 dvp = ITOV(dp); 4819 ACQUIRE_LOCK(dp->i_ump); 4820 (void) inodedep_lookup_ip(ip); 4821 (void) inodedep_lookup_ip(dp); 4822 softdep_prelink(dvp, ITOV(ip)); 4823 FREE_LOCK(dp->i_ump); 4824 } 4825 4826 /* 4827 * Called to track nlinkdelta of the inode and parent directories prior to 4828 * unlink. 4829 */ 4830 void 4831 softdep_setup_unlink(dp, ip) 4832 struct inode *dp; 4833 struct inode *ip; 4834 { 4835 struct vnode *dvp; 4836 4837 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4838 ("softdep_setup_unlink called on non-softdep filesystem")); 4839 dvp = ITOV(dp); 4840 ACQUIRE_LOCK(dp->i_ump); 4841 (void) inodedep_lookup_ip(ip); 4842 (void) inodedep_lookup_ip(dp); 4843 softdep_prelink(dvp, ITOV(ip)); 4844 FREE_LOCK(dp->i_ump); 4845 } 4846 4847 /* 4848 * Called to release the journal structures created by a failed non-directory 4849 * creation. Adjusts nlinkdelta for non-journaling softdep. 4850 */ 4851 void 4852 softdep_revert_create(dp, ip) 4853 struct inode *dp; 4854 struct inode *ip; 4855 { 4856 struct inodedep *inodedep; 4857 struct jaddref *jaddref; 4858 struct vnode *dvp; 4859 4860 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4861 ("softdep_revert_create called on non-softdep filesystem")); 4862 dvp = ITOV(dp); 4863 ACQUIRE_LOCK(dp->i_ump); 4864 inodedep = inodedep_lookup_ip(ip); 4865 if (DOINGSUJ(dvp)) { 4866 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4867 inoreflst); 4868 KASSERT(jaddref->ja_parent == dp->i_number, 4869 ("softdep_revert_create: addref parent mismatch")); 4870 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4871 } 4872 FREE_LOCK(dp->i_ump); 4873 } 4874 4875 /* 4876 * Called to release the journal structures created by a failed link 4877 * addition. Adjusts nlinkdelta for non-journaling softdep. 4878 */ 4879 void 4880 softdep_revert_link(dp, ip) 4881 struct inode *dp; 4882 struct inode *ip; 4883 { 4884 struct inodedep *inodedep; 4885 struct jaddref *jaddref; 4886 struct vnode *dvp; 4887 4888 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4889 ("softdep_revert_link called on non-softdep filesystem")); 4890 dvp = ITOV(dp); 4891 ACQUIRE_LOCK(dp->i_ump); 4892 inodedep = inodedep_lookup_ip(ip); 4893 if (DOINGSUJ(dvp)) { 4894 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4895 inoreflst); 4896 KASSERT(jaddref->ja_parent == dp->i_number, 4897 ("softdep_revert_link: addref parent mismatch")); 4898 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4899 } 4900 FREE_LOCK(dp->i_ump); 4901 } 4902 4903 /* 4904 * Called to release the journal structures created by a failed mkdir 4905 * attempt. Adjusts nlinkdelta for non-journaling softdep. 4906 */ 4907 void 4908 softdep_revert_mkdir(dp, ip) 4909 struct inode *dp; 4910 struct inode *ip; 4911 { 4912 struct inodedep *inodedep; 4913 struct jaddref *jaddref; 4914 struct jaddref *dotaddref; 4915 struct vnode *dvp; 4916 4917 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4918 ("softdep_revert_mkdir called on non-softdep filesystem")); 4919 dvp = ITOV(dp); 4920 4921 ACQUIRE_LOCK(dp->i_ump); 4922 inodedep = inodedep_lookup_ip(dp); 4923 if (DOINGSUJ(dvp)) { 4924 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4925 inoreflst); 4926 KASSERT(jaddref->ja_parent == ip->i_number, 4927 ("softdep_revert_mkdir: dotdot addref parent mismatch")); 4928 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4929 } 4930 inodedep = inodedep_lookup_ip(ip); 4931 if (DOINGSUJ(dvp)) { 4932 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4933 inoreflst); 4934 KASSERT(jaddref->ja_parent == dp->i_number, 4935 ("softdep_revert_mkdir: addref parent mismatch")); 4936 dotaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 4937 inoreflst, if_deps); 4938 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4939 KASSERT(dotaddref->ja_parent == ip->i_number, 4940 ("softdep_revert_mkdir: dot addref parent mismatch")); 4941 cancel_jaddref(dotaddref, inodedep, &inodedep->id_inowait); 4942 } 4943 FREE_LOCK(dp->i_ump); 4944 } 4945 4946 /* 4947 * Called to correct nlinkdelta after a failed rmdir. 4948 */ 4949 void 4950 softdep_revert_rmdir(dp, ip) 4951 struct inode *dp; 4952 struct inode *ip; 4953 { 4954 4955 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4956 ("softdep_revert_rmdir called on non-softdep filesystem")); 4957 ACQUIRE_LOCK(dp->i_ump); 4958 (void) inodedep_lookup_ip(ip); 4959 (void) inodedep_lookup_ip(dp); 4960 FREE_LOCK(dp->i_ump); 4961 } 4962 4963 /* 4964 * Protecting the freemaps (or bitmaps). 4965 * 4966 * To eliminate the need to execute fsck before mounting a filesystem 4967 * after a power failure, one must (conservatively) guarantee that the 4968 * on-disk copy of the bitmaps never indicate that a live inode or block is 4969 * free. So, when a block or inode is allocated, the bitmap should be 4970 * updated (on disk) before any new pointers. When a block or inode is 4971 * freed, the bitmap should not be updated until all pointers have been 4972 * reset. The latter dependency is handled by the delayed de-allocation 4973 * approach described below for block and inode de-allocation. The former 4974 * dependency is handled by calling the following procedure when a block or 4975 * inode is allocated. When an inode is allocated an "inodedep" is created 4976 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk. 4977 * Each "inodedep" is also inserted into the hash indexing structure so 4978 * that any additional link additions can be made dependent on the inode 4979 * allocation. 4980 * 4981 * The ufs filesystem maintains a number of free block counts (e.g., per 4982 * cylinder group, per cylinder and per <cylinder, rotational position> pair) 4983 * in addition to the bitmaps. These counts are used to improve efficiency 4984 * during allocation and therefore must be consistent with the bitmaps. 4985 * There is no convenient way to guarantee post-crash consistency of these 4986 * counts with simple update ordering, for two main reasons: (1) The counts 4987 * and bitmaps for a single cylinder group block are not in the same disk 4988 * sector. If a disk write is interrupted (e.g., by power failure), one may 4989 * be written and the other not. (2) Some of the counts are located in the 4990 * superblock rather than the cylinder group block. So, we focus our soft 4991 * updates implementation on protecting the bitmaps. When mounting a 4992 * filesystem, we recompute the auxiliary counts from the bitmaps. 4993 */ 4994 4995 /* 4996 * Called just after updating the cylinder group block to allocate an inode. 4997 */ 4998 void 4999 softdep_setup_inomapdep(bp, ip, newinum, mode) 5000 struct buf *bp; /* buffer for cylgroup block with inode map */ 5001 struct inode *ip; /* inode related to allocation */ 5002 ino_t newinum; /* new inode number being allocated */ 5003 int mode; 5004 { 5005 struct inodedep *inodedep; 5006 struct bmsafemap *bmsafemap; 5007 struct jaddref *jaddref; 5008 struct mount *mp; 5009 struct fs *fs; 5010 5011 mp = UFSTOVFS(ip->i_ump); 5012 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5013 ("softdep_setup_inomapdep called on non-softdep filesystem")); 5014 fs = ip->i_ump->um_fs; 5015 jaddref = NULL; 5016 5017 /* 5018 * Allocate the journal reference add structure so that the bitmap 5019 * can be dependent on it. 5020 */ 5021 if (MOUNTEDSUJ(mp)) { 5022 jaddref = newjaddref(ip, newinum, 0, 0, mode); 5023 jaddref->ja_state |= NEWBLOCK; 5024 } 5025 5026 /* 5027 * Create a dependency for the newly allocated inode. 5028 * Panic if it already exists as something is seriously wrong. 5029 * Otherwise add it to the dependency list for the buffer holding 5030 * the cylinder group map from which it was allocated. 5031 * 5032 * We have to preallocate a bmsafemap entry in case it is needed 5033 * in bmsafemap_lookup since once we allocate the inodedep, we 5034 * have to finish initializing it before we can FREE_LOCK(). 5035 * By preallocating, we avoid FREE_LOCK() while doing a malloc 5036 * in bmsafemap_lookup. We cannot call bmsafemap_lookup before 5037 * creating the inodedep as it can be freed during the time 5038 * that we FREE_LOCK() while allocating the inodedep. We must 5039 * call workitem_alloc() before entering the locked section as 5040 * it also acquires the lock and we must avoid trying doing so 5041 * recursively. 5042 */ 5043 bmsafemap = malloc(sizeof(struct bmsafemap), 5044 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5045 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5046 ACQUIRE_LOCK(ip->i_ump); 5047 if ((inodedep_lookup(mp, newinum, DEPALLOC, &inodedep))) 5048 panic("softdep_setup_inomapdep: dependency %p for new" 5049 "inode already exists", inodedep); 5050 bmsafemap = bmsafemap_lookup(mp, bp, ino_to_cg(fs, newinum), bmsafemap); 5051 if (jaddref) { 5052 LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps); 5053 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 5054 if_deps); 5055 } else { 5056 inodedep->id_state |= ONDEPLIST; 5057 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps); 5058 } 5059 inodedep->id_bmsafemap = bmsafemap; 5060 inodedep->id_state &= ~DEPCOMPLETE; 5061 FREE_LOCK(ip->i_ump); 5062 } 5063 5064 /* 5065 * Called just after updating the cylinder group block to 5066 * allocate block or fragment. 5067 */ 5068 void 5069 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags) 5070 struct buf *bp; /* buffer for cylgroup block with block map */ 5071 struct mount *mp; /* filesystem doing allocation */ 5072 ufs2_daddr_t newblkno; /* number of newly allocated block */ 5073 int frags; /* Number of fragments. */ 5074 int oldfrags; /* Previous number of fragments for extend. */ 5075 { 5076 struct newblk *newblk; 5077 struct bmsafemap *bmsafemap; 5078 struct jnewblk *jnewblk; 5079 struct ufsmount *ump; 5080 struct fs *fs; 5081 5082 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5083 ("softdep_setup_blkmapdep called on non-softdep filesystem")); 5084 ump = VFSTOUFS(mp); 5085 fs = ump->um_fs; 5086 jnewblk = NULL; 5087 /* 5088 * Create a dependency for the newly allocated block. 5089 * Add it to the dependency list for the buffer holding 5090 * the cylinder group map from which it was allocated. 5091 */ 5092 if (MOUNTEDSUJ(mp)) { 5093 jnewblk = malloc(sizeof(*jnewblk), M_JNEWBLK, M_SOFTDEP_FLAGS); 5094 workitem_alloc(&jnewblk->jn_list, D_JNEWBLK, mp); 5095 jnewblk->jn_jsegdep = newjsegdep(&jnewblk->jn_list); 5096 jnewblk->jn_state = ATTACHED; 5097 jnewblk->jn_blkno = newblkno; 5098 jnewblk->jn_frags = frags; 5099 jnewblk->jn_oldfrags = oldfrags; 5100 #ifdef SUJ_DEBUG 5101 { 5102 struct cg *cgp; 5103 uint8_t *blksfree; 5104 long bno; 5105 int i; 5106 5107 cgp = (struct cg *)bp->b_data; 5108 blksfree = cg_blksfree(cgp); 5109 bno = dtogd(fs, jnewblk->jn_blkno); 5110 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; 5111 i++) { 5112 if (isset(blksfree, bno + i)) 5113 panic("softdep_setup_blkmapdep: " 5114 "free fragment %d from %d-%d " 5115 "state 0x%X dep %p", i, 5116 jnewblk->jn_oldfrags, 5117 jnewblk->jn_frags, 5118 jnewblk->jn_state, 5119 jnewblk->jn_dep); 5120 } 5121 } 5122 #endif 5123 } 5124 5125 CTR3(KTR_SUJ, 5126 "softdep_setup_blkmapdep: blkno %jd frags %d oldfrags %d", 5127 newblkno, frags, oldfrags); 5128 ACQUIRE_LOCK(ump); 5129 if (newblk_lookup(mp, newblkno, DEPALLOC, &newblk) != 0) 5130 panic("softdep_setup_blkmapdep: found block"); 5131 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp, 5132 dtog(fs, newblkno), NULL); 5133 if (jnewblk) { 5134 jnewblk->jn_dep = (struct worklist *)newblk; 5135 LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps); 5136 } else { 5137 newblk->nb_state |= ONDEPLIST; 5138 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps); 5139 } 5140 newblk->nb_bmsafemap = bmsafemap; 5141 newblk->nb_jnewblk = jnewblk; 5142 FREE_LOCK(ump); 5143 } 5144 5145 #define BMSAFEMAP_HASH(ump, cg) \ 5146 (&(ump)->bmsafemap_hashtbl[(cg) & (ump)->bmsafemap_hash_size]) 5147 5148 static int 5149 bmsafemap_find(bmsafemaphd, cg, bmsafemapp) 5150 struct bmsafemap_hashhead *bmsafemaphd; 5151 int cg; 5152 struct bmsafemap **bmsafemapp; 5153 { 5154 struct bmsafemap *bmsafemap; 5155 5156 LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash) 5157 if (bmsafemap->sm_cg == cg) 5158 break; 5159 if (bmsafemap) { 5160 *bmsafemapp = bmsafemap; 5161 return (1); 5162 } 5163 *bmsafemapp = NULL; 5164 5165 return (0); 5166 } 5167 5168 /* 5169 * Find the bmsafemap associated with a cylinder group buffer. 5170 * If none exists, create one. The buffer must be locked when 5171 * this routine is called and this routine must be called with 5172 * the softdep lock held. To avoid giving up the lock while 5173 * allocating a new bmsafemap, a preallocated bmsafemap may be 5174 * provided. If it is provided but not needed, it is freed. 5175 */ 5176 static struct bmsafemap * 5177 bmsafemap_lookup(mp, bp, cg, newbmsafemap) 5178 struct mount *mp; 5179 struct buf *bp; 5180 int cg; 5181 struct bmsafemap *newbmsafemap; 5182 { 5183 struct bmsafemap_hashhead *bmsafemaphd; 5184 struct bmsafemap *bmsafemap, *collision; 5185 struct worklist *wk; 5186 struct ufsmount *ump; 5187 5188 ump = VFSTOUFS(mp); 5189 LOCK_OWNED(ump); 5190 KASSERT(bp != NULL, ("bmsafemap_lookup: missing buffer")); 5191 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 5192 if (wk->wk_type == D_BMSAFEMAP) { 5193 if (newbmsafemap) 5194 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5195 return (WK_BMSAFEMAP(wk)); 5196 } 5197 } 5198 bmsafemaphd = BMSAFEMAP_HASH(ump, cg); 5199 if (bmsafemap_find(bmsafemaphd, cg, &bmsafemap) == 1) { 5200 if (newbmsafemap) 5201 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5202 return (bmsafemap); 5203 } 5204 if (newbmsafemap) { 5205 bmsafemap = newbmsafemap; 5206 } else { 5207 FREE_LOCK(ump); 5208 bmsafemap = malloc(sizeof(struct bmsafemap), 5209 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5210 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5211 ACQUIRE_LOCK(ump); 5212 } 5213 bmsafemap->sm_buf = bp; 5214 LIST_INIT(&bmsafemap->sm_inodedephd); 5215 LIST_INIT(&bmsafemap->sm_inodedepwr); 5216 LIST_INIT(&bmsafemap->sm_newblkhd); 5217 LIST_INIT(&bmsafemap->sm_newblkwr); 5218 LIST_INIT(&bmsafemap->sm_jaddrefhd); 5219 LIST_INIT(&bmsafemap->sm_jnewblkhd); 5220 LIST_INIT(&bmsafemap->sm_freehd); 5221 LIST_INIT(&bmsafemap->sm_freewr); 5222 if (bmsafemap_find(bmsafemaphd, cg, &collision) == 1) { 5223 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 5224 return (collision); 5225 } 5226 bmsafemap->sm_cg = cg; 5227 LIST_INSERT_HEAD(bmsafemaphd, bmsafemap, sm_hash); 5228 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 5229 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list); 5230 return (bmsafemap); 5231 } 5232 5233 /* 5234 * Direct block allocation dependencies. 5235 * 5236 * When a new block is allocated, the corresponding disk locations must be 5237 * initialized (with zeros or new data) before the on-disk inode points to 5238 * them. Also, the freemap from which the block was allocated must be 5239 * updated (on disk) before the inode's pointer. These two dependencies are 5240 * independent of each other and are needed for all file blocks and indirect 5241 * blocks that are pointed to directly by the inode. Just before the 5242 * "in-core" version of the inode is updated with a newly allocated block 5243 * number, a procedure (below) is called to setup allocation dependency 5244 * structures. These structures are removed when the corresponding 5245 * dependencies are satisfied or when the block allocation becomes obsolete 5246 * (i.e., the file is deleted, the block is de-allocated, or the block is a 5247 * fragment that gets upgraded). All of these cases are handled in 5248 * procedures described later. 5249 * 5250 * When a file extension causes a fragment to be upgraded, either to a larger 5251 * fragment or to a full block, the on-disk location may change (if the 5252 * previous fragment could not simply be extended). In this case, the old 5253 * fragment must be de-allocated, but not until after the inode's pointer has 5254 * been updated. In most cases, this is handled by later procedures, which 5255 * will construct a "freefrag" structure to be added to the workitem queue 5256 * when the inode update is complete (or obsolete). The main exception to 5257 * this is when an allocation occurs while a pending allocation dependency 5258 * (for the same block pointer) remains. This case is handled in the main 5259 * allocation dependency setup procedure by immediately freeing the 5260 * unreferenced fragments. 5261 */ 5262 void 5263 softdep_setup_allocdirect(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 5264 struct inode *ip; /* inode to which block is being added */ 5265 ufs_lbn_t off; /* block pointer within inode */ 5266 ufs2_daddr_t newblkno; /* disk block number being added */ 5267 ufs2_daddr_t oldblkno; /* previous block number, 0 unless frag */ 5268 long newsize; /* size of new block */ 5269 long oldsize; /* size of new block */ 5270 struct buf *bp; /* bp for allocated block */ 5271 { 5272 struct allocdirect *adp, *oldadp; 5273 struct allocdirectlst *adphead; 5274 struct freefrag *freefrag; 5275 struct inodedep *inodedep; 5276 struct pagedep *pagedep; 5277 struct jnewblk *jnewblk; 5278 struct newblk *newblk; 5279 struct mount *mp; 5280 ufs_lbn_t lbn; 5281 5282 lbn = bp->b_lblkno; 5283 mp = UFSTOVFS(ip->i_ump); 5284 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5285 ("softdep_setup_allocdirect called on non-softdep filesystem")); 5286 if (oldblkno && oldblkno != newblkno) 5287 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn); 5288 else 5289 freefrag = NULL; 5290 5291 CTR6(KTR_SUJ, 5292 "softdep_setup_allocdirect: ino %d blkno %jd oldblkno %jd " 5293 "off %jd newsize %ld oldsize %d", 5294 ip->i_number, newblkno, oldblkno, off, newsize, oldsize); 5295 ACQUIRE_LOCK(ip->i_ump); 5296 if (off >= NDADDR) { 5297 if (lbn > 0) 5298 panic("softdep_setup_allocdirect: bad lbn %jd, off %jd", 5299 lbn, off); 5300 /* allocating an indirect block */ 5301 if (oldblkno != 0) 5302 panic("softdep_setup_allocdirect: non-zero indir"); 5303 } else { 5304 if (off != lbn) 5305 panic("softdep_setup_allocdirect: lbn %jd != off %jd", 5306 lbn, off); 5307 /* 5308 * Allocating a direct block. 5309 * 5310 * If we are allocating a directory block, then we must 5311 * allocate an associated pagedep to track additions and 5312 * deletions. 5313 */ 5314 if ((ip->i_mode & IFMT) == IFDIR) 5315 pagedep_lookup(mp, bp, ip->i_number, off, DEPALLOC, 5316 &pagedep); 5317 } 5318 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5319 panic("softdep_setup_allocdirect: lost block"); 5320 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5321 ("softdep_setup_allocdirect: newblk already initialized")); 5322 /* 5323 * Convert the newblk to an allocdirect. 5324 */ 5325 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT); 5326 adp = (struct allocdirect *)newblk; 5327 newblk->nb_freefrag = freefrag; 5328 adp->ad_offset = off; 5329 adp->ad_oldblkno = oldblkno; 5330 adp->ad_newsize = newsize; 5331 adp->ad_oldsize = oldsize; 5332 5333 /* 5334 * Finish initializing the journal. 5335 */ 5336 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5337 jnewblk->jn_ino = ip->i_number; 5338 jnewblk->jn_lbn = lbn; 5339 add_to_journal(&jnewblk->jn_list); 5340 } 5341 if (freefrag && freefrag->ff_jdep != NULL && 5342 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5343 add_to_journal(freefrag->ff_jdep); 5344 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 5345 adp->ad_inodedep = inodedep; 5346 5347 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5348 /* 5349 * The list of allocdirects must be kept in sorted and ascending 5350 * order so that the rollback routines can quickly determine the 5351 * first uncommitted block (the size of the file stored on disk 5352 * ends at the end of the lowest committed fragment, or if there 5353 * are no fragments, at the end of the highest committed block). 5354 * Since files generally grow, the typical case is that the new 5355 * block is to be added at the end of the list. We speed this 5356 * special case by checking against the last allocdirect in the 5357 * list before laboriously traversing the list looking for the 5358 * insertion point. 5359 */ 5360 adphead = &inodedep->id_newinoupdt; 5361 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5362 if (oldadp == NULL || oldadp->ad_offset <= off) { 5363 /* insert at end of list */ 5364 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5365 if (oldadp != NULL && oldadp->ad_offset == off) 5366 allocdirect_merge(adphead, adp, oldadp); 5367 FREE_LOCK(ip->i_ump); 5368 return; 5369 } 5370 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5371 if (oldadp->ad_offset >= off) 5372 break; 5373 } 5374 if (oldadp == NULL) 5375 panic("softdep_setup_allocdirect: lost entry"); 5376 /* insert in middle of list */ 5377 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5378 if (oldadp->ad_offset == off) 5379 allocdirect_merge(adphead, adp, oldadp); 5380 5381 FREE_LOCK(ip->i_ump); 5382 } 5383 5384 /* 5385 * Merge a newer and older journal record to be stored either in a 5386 * newblock or freefrag. This handles aggregating journal records for 5387 * fragment allocation into a second record as well as replacing a 5388 * journal free with an aborted journal allocation. A segment for the 5389 * oldest record will be placed on wkhd if it has been written. If not 5390 * the segment for the newer record will suffice. 5391 */ 5392 static struct worklist * 5393 jnewblk_merge(new, old, wkhd) 5394 struct worklist *new; 5395 struct worklist *old; 5396 struct workhead *wkhd; 5397 { 5398 struct jnewblk *njnewblk; 5399 struct jnewblk *jnewblk; 5400 5401 /* Handle NULLs to simplify callers. */ 5402 if (new == NULL) 5403 return (old); 5404 if (old == NULL) 5405 return (new); 5406 /* Replace a jfreefrag with a jnewblk. */ 5407 if (new->wk_type == D_JFREEFRAG) { 5408 if (WK_JNEWBLK(old)->jn_blkno != WK_JFREEFRAG(new)->fr_blkno) 5409 panic("jnewblk_merge: blkno mismatch: %p, %p", 5410 old, new); 5411 cancel_jfreefrag(WK_JFREEFRAG(new)); 5412 return (old); 5413 } 5414 if (old->wk_type != D_JNEWBLK || new->wk_type != D_JNEWBLK) 5415 panic("jnewblk_merge: Bad type: old %d new %d\n", 5416 old->wk_type, new->wk_type); 5417 /* 5418 * Handle merging of two jnewblk records that describe 5419 * different sets of fragments in the same block. 5420 */ 5421 jnewblk = WK_JNEWBLK(old); 5422 njnewblk = WK_JNEWBLK(new); 5423 if (jnewblk->jn_blkno != njnewblk->jn_blkno) 5424 panic("jnewblk_merge: Merging disparate blocks."); 5425 /* 5426 * The record may be rolled back in the cg. 5427 */ 5428 if (jnewblk->jn_state & UNDONE) { 5429 jnewblk->jn_state &= ~UNDONE; 5430 njnewblk->jn_state |= UNDONE; 5431 njnewblk->jn_state &= ~ATTACHED; 5432 } 5433 /* 5434 * We modify the newer addref and free the older so that if neither 5435 * has been written the most up-to-date copy will be on disk. If 5436 * both have been written but rolled back we only temporarily need 5437 * one of them to fix the bits when the cg write completes. 5438 */ 5439 jnewblk->jn_state |= ATTACHED | COMPLETE; 5440 njnewblk->jn_oldfrags = jnewblk->jn_oldfrags; 5441 cancel_jnewblk(jnewblk, wkhd); 5442 WORKLIST_REMOVE(&jnewblk->jn_list); 5443 free_jnewblk(jnewblk); 5444 return (new); 5445 } 5446 5447 /* 5448 * Replace an old allocdirect dependency with a newer one. 5449 * This routine must be called with splbio interrupts blocked. 5450 */ 5451 static void 5452 allocdirect_merge(adphead, newadp, oldadp) 5453 struct allocdirectlst *adphead; /* head of list holding allocdirects */ 5454 struct allocdirect *newadp; /* allocdirect being added */ 5455 struct allocdirect *oldadp; /* existing allocdirect being checked */ 5456 { 5457 struct worklist *wk; 5458 struct freefrag *freefrag; 5459 5460 freefrag = NULL; 5461 LOCK_OWNED(VFSTOUFS(newadp->ad_list.wk_mp)); 5462 if (newadp->ad_oldblkno != oldadp->ad_newblkno || 5463 newadp->ad_oldsize != oldadp->ad_newsize || 5464 newadp->ad_offset >= NDADDR) 5465 panic("%s %jd != new %jd || old size %ld != new %ld", 5466 "allocdirect_merge: old blkno", 5467 (intmax_t)newadp->ad_oldblkno, 5468 (intmax_t)oldadp->ad_newblkno, 5469 newadp->ad_oldsize, oldadp->ad_newsize); 5470 newadp->ad_oldblkno = oldadp->ad_oldblkno; 5471 newadp->ad_oldsize = oldadp->ad_oldsize; 5472 /* 5473 * If the old dependency had a fragment to free or had never 5474 * previously had a block allocated, then the new dependency 5475 * can immediately post its freefrag and adopt the old freefrag. 5476 * This action is done by swapping the freefrag dependencies. 5477 * The new dependency gains the old one's freefrag, and the 5478 * old one gets the new one and then immediately puts it on 5479 * the worklist when it is freed by free_newblk. It is 5480 * not possible to do this swap when the old dependency had a 5481 * non-zero size but no previous fragment to free. This condition 5482 * arises when the new block is an extension of the old block. 5483 * Here, the first part of the fragment allocated to the new 5484 * dependency is part of the block currently claimed on disk by 5485 * the old dependency, so cannot legitimately be freed until the 5486 * conditions for the new dependency are fulfilled. 5487 */ 5488 freefrag = newadp->ad_freefrag; 5489 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) { 5490 newadp->ad_freefrag = oldadp->ad_freefrag; 5491 oldadp->ad_freefrag = freefrag; 5492 } 5493 /* 5494 * If we are tracking a new directory-block allocation, 5495 * move it from the old allocdirect to the new allocdirect. 5496 */ 5497 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) { 5498 WORKLIST_REMOVE(wk); 5499 if (!LIST_EMPTY(&oldadp->ad_newdirblk)) 5500 panic("allocdirect_merge: extra newdirblk"); 5501 WORKLIST_INSERT(&newadp->ad_newdirblk, wk); 5502 } 5503 TAILQ_REMOVE(adphead, oldadp, ad_next); 5504 /* 5505 * We need to move any journal dependencies over to the freefrag 5506 * that releases this block if it exists. Otherwise we are 5507 * extending an existing block and we'll wait until that is 5508 * complete to release the journal space and extend the 5509 * new journal to cover this old space as well. 5510 */ 5511 if (freefrag == NULL) { 5512 if (oldadp->ad_newblkno != newadp->ad_newblkno) 5513 panic("allocdirect_merge: %jd != %jd", 5514 oldadp->ad_newblkno, newadp->ad_newblkno); 5515 newadp->ad_block.nb_jnewblk = (struct jnewblk *) 5516 jnewblk_merge(&newadp->ad_block.nb_jnewblk->jn_list, 5517 &oldadp->ad_block.nb_jnewblk->jn_list, 5518 &newadp->ad_block.nb_jwork); 5519 oldadp->ad_block.nb_jnewblk = NULL; 5520 cancel_newblk(&oldadp->ad_block, NULL, 5521 &newadp->ad_block.nb_jwork); 5522 } else { 5523 wk = (struct worklist *) cancel_newblk(&oldadp->ad_block, 5524 &freefrag->ff_list, &freefrag->ff_jwork); 5525 freefrag->ff_jdep = jnewblk_merge(freefrag->ff_jdep, wk, 5526 &freefrag->ff_jwork); 5527 } 5528 free_newblk(&oldadp->ad_block); 5529 } 5530 5531 /* 5532 * Allocate a jfreefrag structure to journal a single block free. 5533 */ 5534 static struct jfreefrag * 5535 newjfreefrag(freefrag, ip, blkno, size, lbn) 5536 struct freefrag *freefrag; 5537 struct inode *ip; 5538 ufs2_daddr_t blkno; 5539 long size; 5540 ufs_lbn_t lbn; 5541 { 5542 struct jfreefrag *jfreefrag; 5543 struct fs *fs; 5544 5545 fs = ip->i_fs; 5546 jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG, 5547 M_SOFTDEP_FLAGS); 5548 workitem_alloc(&jfreefrag->fr_list, D_JFREEFRAG, UFSTOVFS(ip->i_ump)); 5549 jfreefrag->fr_jsegdep = newjsegdep(&jfreefrag->fr_list); 5550 jfreefrag->fr_state = ATTACHED | DEPCOMPLETE; 5551 jfreefrag->fr_ino = ip->i_number; 5552 jfreefrag->fr_lbn = lbn; 5553 jfreefrag->fr_blkno = blkno; 5554 jfreefrag->fr_frags = numfrags(fs, size); 5555 jfreefrag->fr_freefrag = freefrag; 5556 5557 return (jfreefrag); 5558 } 5559 5560 /* 5561 * Allocate a new freefrag structure. 5562 */ 5563 static struct freefrag * 5564 newfreefrag(ip, blkno, size, lbn) 5565 struct inode *ip; 5566 ufs2_daddr_t blkno; 5567 long size; 5568 ufs_lbn_t lbn; 5569 { 5570 struct freefrag *freefrag; 5571 struct fs *fs; 5572 5573 CTR4(KTR_SUJ, "newfreefrag: ino %d blkno %jd size %ld lbn %jd", 5574 ip->i_number, blkno, size, lbn); 5575 fs = ip->i_fs; 5576 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag) 5577 panic("newfreefrag: frag size"); 5578 freefrag = malloc(sizeof(struct freefrag), 5579 M_FREEFRAG, M_SOFTDEP_FLAGS); 5580 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ip->i_ump)); 5581 freefrag->ff_state = ATTACHED; 5582 LIST_INIT(&freefrag->ff_jwork); 5583 freefrag->ff_inum = ip->i_number; 5584 freefrag->ff_vtype = ITOV(ip)->v_type; 5585 freefrag->ff_blkno = blkno; 5586 freefrag->ff_fragsize = size; 5587 5588 if (MOUNTEDSUJ(UFSTOVFS(ip->i_ump))) { 5589 freefrag->ff_jdep = (struct worklist *) 5590 newjfreefrag(freefrag, ip, blkno, size, lbn); 5591 } else { 5592 freefrag->ff_state |= DEPCOMPLETE; 5593 freefrag->ff_jdep = NULL; 5594 } 5595 5596 return (freefrag); 5597 } 5598 5599 /* 5600 * This workitem de-allocates fragments that were replaced during 5601 * file block allocation. 5602 */ 5603 static void 5604 handle_workitem_freefrag(freefrag) 5605 struct freefrag *freefrag; 5606 { 5607 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp); 5608 struct workhead wkhd; 5609 5610 CTR3(KTR_SUJ, 5611 "handle_workitem_freefrag: ino %d blkno %jd size %ld", 5612 freefrag->ff_inum, freefrag->ff_blkno, freefrag->ff_fragsize); 5613 /* 5614 * It would be illegal to add new completion items to the 5615 * freefrag after it was schedule to be done so it must be 5616 * safe to modify the list head here. 5617 */ 5618 LIST_INIT(&wkhd); 5619 ACQUIRE_LOCK(ump); 5620 LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list); 5621 /* 5622 * If the journal has not been written we must cancel it here. 5623 */ 5624 if (freefrag->ff_jdep) { 5625 if (freefrag->ff_jdep->wk_type != D_JNEWBLK) 5626 panic("handle_workitem_freefrag: Unexpected type %d\n", 5627 freefrag->ff_jdep->wk_type); 5628 cancel_jnewblk(WK_JNEWBLK(freefrag->ff_jdep), &wkhd); 5629 } 5630 FREE_LOCK(ump); 5631 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno, 5632 freefrag->ff_fragsize, freefrag->ff_inum, freefrag->ff_vtype, &wkhd); 5633 ACQUIRE_LOCK(ump); 5634 WORKITEM_FREE(freefrag, D_FREEFRAG); 5635 FREE_LOCK(ump); 5636 } 5637 5638 /* 5639 * Set up a dependency structure for an external attributes data block. 5640 * This routine follows much of the structure of softdep_setup_allocdirect. 5641 * See the description of softdep_setup_allocdirect above for details. 5642 */ 5643 void 5644 softdep_setup_allocext(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 5645 struct inode *ip; 5646 ufs_lbn_t off; 5647 ufs2_daddr_t newblkno; 5648 ufs2_daddr_t oldblkno; 5649 long newsize; 5650 long oldsize; 5651 struct buf *bp; 5652 { 5653 struct allocdirect *adp, *oldadp; 5654 struct allocdirectlst *adphead; 5655 struct freefrag *freefrag; 5656 struct inodedep *inodedep; 5657 struct jnewblk *jnewblk; 5658 struct newblk *newblk; 5659 struct mount *mp; 5660 ufs_lbn_t lbn; 5661 5662 mp = UFSTOVFS(ip->i_ump); 5663 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5664 ("softdep_setup_allocext called on non-softdep filesystem")); 5665 KASSERT(off < NXADDR, ("softdep_setup_allocext: lbn %lld > NXADDR", 5666 (long long)off)); 5667 5668 lbn = bp->b_lblkno; 5669 if (oldblkno && oldblkno != newblkno) 5670 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn); 5671 else 5672 freefrag = NULL; 5673 5674 ACQUIRE_LOCK(ip->i_ump); 5675 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5676 panic("softdep_setup_allocext: lost block"); 5677 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5678 ("softdep_setup_allocext: newblk already initialized")); 5679 /* 5680 * Convert the newblk to an allocdirect. 5681 */ 5682 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT); 5683 adp = (struct allocdirect *)newblk; 5684 newblk->nb_freefrag = freefrag; 5685 adp->ad_offset = off; 5686 adp->ad_oldblkno = oldblkno; 5687 adp->ad_newsize = newsize; 5688 adp->ad_oldsize = oldsize; 5689 adp->ad_state |= EXTDATA; 5690 5691 /* 5692 * Finish initializing the journal. 5693 */ 5694 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5695 jnewblk->jn_ino = ip->i_number; 5696 jnewblk->jn_lbn = lbn; 5697 add_to_journal(&jnewblk->jn_list); 5698 } 5699 if (freefrag && freefrag->ff_jdep != NULL && 5700 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5701 add_to_journal(freefrag->ff_jdep); 5702 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 5703 adp->ad_inodedep = inodedep; 5704 5705 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5706 /* 5707 * The list of allocdirects must be kept in sorted and ascending 5708 * order so that the rollback routines can quickly determine the 5709 * first uncommitted block (the size of the file stored on disk 5710 * ends at the end of the lowest committed fragment, or if there 5711 * are no fragments, at the end of the highest committed block). 5712 * Since files generally grow, the typical case is that the new 5713 * block is to be added at the end of the list. We speed this 5714 * special case by checking against the last allocdirect in the 5715 * list before laboriously traversing the list looking for the 5716 * insertion point. 5717 */ 5718 adphead = &inodedep->id_newextupdt; 5719 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5720 if (oldadp == NULL || oldadp->ad_offset <= off) { 5721 /* insert at end of list */ 5722 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5723 if (oldadp != NULL && oldadp->ad_offset == off) 5724 allocdirect_merge(adphead, adp, oldadp); 5725 FREE_LOCK(ip->i_ump); 5726 return; 5727 } 5728 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5729 if (oldadp->ad_offset >= off) 5730 break; 5731 } 5732 if (oldadp == NULL) 5733 panic("softdep_setup_allocext: lost entry"); 5734 /* insert in middle of list */ 5735 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5736 if (oldadp->ad_offset == off) 5737 allocdirect_merge(adphead, adp, oldadp); 5738 FREE_LOCK(ip->i_ump); 5739 } 5740 5741 /* 5742 * Indirect block allocation dependencies. 5743 * 5744 * The same dependencies that exist for a direct block also exist when 5745 * a new block is allocated and pointed to by an entry in a block of 5746 * indirect pointers. The undo/redo states described above are also 5747 * used here. Because an indirect block contains many pointers that 5748 * may have dependencies, a second copy of the entire in-memory indirect 5749 * block is kept. The buffer cache copy is always completely up-to-date. 5750 * The second copy, which is used only as a source for disk writes, 5751 * contains only the safe pointers (i.e., those that have no remaining 5752 * update dependencies). The second copy is freed when all pointers 5753 * are safe. The cache is not allowed to replace indirect blocks with 5754 * pending update dependencies. If a buffer containing an indirect 5755 * block with dependencies is written, these routines will mark it 5756 * dirty again. It can only be successfully written once all the 5757 * dependencies are removed. The ffs_fsync routine in conjunction with 5758 * softdep_sync_metadata work together to get all the dependencies 5759 * removed so that a file can be successfully written to disk. Three 5760 * procedures are used when setting up indirect block pointer 5761 * dependencies. The division is necessary because of the organization 5762 * of the "balloc" routine and because of the distinction between file 5763 * pages and file metadata blocks. 5764 */ 5765 5766 /* 5767 * Allocate a new allocindir structure. 5768 */ 5769 static struct allocindir * 5770 newallocindir(ip, ptrno, newblkno, oldblkno, lbn) 5771 struct inode *ip; /* inode for file being extended */ 5772 int ptrno; /* offset of pointer in indirect block */ 5773 ufs2_daddr_t newblkno; /* disk block number being added */ 5774 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 5775 ufs_lbn_t lbn; 5776 { 5777 struct newblk *newblk; 5778 struct allocindir *aip; 5779 struct freefrag *freefrag; 5780 struct jnewblk *jnewblk; 5781 5782 if (oldblkno) 5783 freefrag = newfreefrag(ip, oldblkno, ip->i_fs->fs_bsize, lbn); 5784 else 5785 freefrag = NULL; 5786 ACQUIRE_LOCK(ip->i_ump); 5787 if (newblk_lookup(UFSTOVFS(ip->i_ump), newblkno, 0, &newblk) == 0) 5788 panic("new_allocindir: lost block"); 5789 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5790 ("newallocindir: newblk already initialized")); 5791 WORKITEM_REASSIGN(newblk, D_ALLOCINDIR); 5792 newblk->nb_freefrag = freefrag; 5793 aip = (struct allocindir *)newblk; 5794 aip->ai_offset = ptrno; 5795 aip->ai_oldblkno = oldblkno; 5796 aip->ai_lbn = lbn; 5797 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5798 jnewblk->jn_ino = ip->i_number; 5799 jnewblk->jn_lbn = lbn; 5800 add_to_journal(&jnewblk->jn_list); 5801 } 5802 if (freefrag && freefrag->ff_jdep != NULL && 5803 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5804 add_to_journal(freefrag->ff_jdep); 5805 return (aip); 5806 } 5807 5808 /* 5809 * Called just before setting an indirect block pointer 5810 * to a newly allocated file page. 5811 */ 5812 void 5813 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 5814 struct inode *ip; /* inode for file being extended */ 5815 ufs_lbn_t lbn; /* allocated block number within file */ 5816 struct buf *bp; /* buffer with indirect blk referencing page */ 5817 int ptrno; /* offset of pointer in indirect block */ 5818 ufs2_daddr_t newblkno; /* disk block number being added */ 5819 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 5820 struct buf *nbp; /* buffer holding allocated page */ 5821 { 5822 struct inodedep *inodedep; 5823 struct freefrag *freefrag; 5824 struct allocindir *aip; 5825 struct pagedep *pagedep; 5826 struct mount *mp; 5827 5828 mp = UFSTOVFS(ip->i_ump); 5829 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5830 ("softdep_setup_allocindir_page called on non-softdep filesystem")); 5831 KASSERT(lbn == nbp->b_lblkno, 5832 ("softdep_setup_allocindir_page: lbn %jd != lblkno %jd", 5833 lbn, bp->b_lblkno)); 5834 CTR4(KTR_SUJ, 5835 "softdep_setup_allocindir_page: ino %d blkno %jd oldblkno %jd " 5836 "lbn %jd", ip->i_number, newblkno, oldblkno, lbn); 5837 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page"); 5838 aip = newallocindir(ip, ptrno, newblkno, oldblkno, lbn); 5839 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 5840 /* 5841 * If we are allocating a directory page, then we must 5842 * allocate an associated pagedep to track additions and 5843 * deletions. 5844 */ 5845 if ((ip->i_mode & IFMT) == IFDIR) 5846 pagedep_lookup(mp, nbp, ip->i_number, lbn, DEPALLOC, &pagedep); 5847 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 5848 freefrag = setup_allocindir_phase2(bp, ip, inodedep, aip, lbn); 5849 FREE_LOCK(ip->i_ump); 5850 if (freefrag) 5851 handle_workitem_freefrag(freefrag); 5852 } 5853 5854 /* 5855 * Called just before setting an indirect block pointer to a 5856 * newly allocated indirect block. 5857 */ 5858 void 5859 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 5860 struct buf *nbp; /* newly allocated indirect block */ 5861 struct inode *ip; /* inode for file being extended */ 5862 struct buf *bp; /* indirect block referencing allocated block */ 5863 int ptrno; /* offset of pointer in indirect block */ 5864 ufs2_daddr_t newblkno; /* disk block number being added */ 5865 { 5866 struct inodedep *inodedep; 5867 struct allocindir *aip; 5868 ufs_lbn_t lbn; 5869 5870 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0, 5871 ("softdep_setup_allocindir_meta called on non-softdep filesystem")); 5872 CTR3(KTR_SUJ, 5873 "softdep_setup_allocindir_meta: ino %d blkno %jd ptrno %d", 5874 ip->i_number, newblkno, ptrno); 5875 lbn = nbp->b_lblkno; 5876 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta"); 5877 aip = newallocindir(ip, ptrno, newblkno, 0, lbn); 5878 inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, DEPALLOC, 5879 &inodedep); 5880 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 5881 if (setup_allocindir_phase2(bp, ip, inodedep, aip, lbn)) 5882 panic("softdep_setup_allocindir_meta: Block already existed"); 5883 FREE_LOCK(ip->i_ump); 5884 } 5885 5886 static void 5887 indirdep_complete(indirdep) 5888 struct indirdep *indirdep; 5889 { 5890 struct allocindir *aip; 5891 5892 LIST_REMOVE(indirdep, ir_next); 5893 indirdep->ir_state |= DEPCOMPLETE; 5894 5895 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) { 5896 LIST_REMOVE(aip, ai_next); 5897 free_newblk(&aip->ai_block); 5898 } 5899 /* 5900 * If this indirdep is not attached to a buf it was simply waiting 5901 * on completion to clear completehd. free_indirdep() asserts 5902 * that nothing is dangling. 5903 */ 5904 if ((indirdep->ir_state & ONWORKLIST) == 0) 5905 free_indirdep(indirdep); 5906 } 5907 5908 static struct indirdep * 5909 indirdep_lookup(mp, ip, bp) 5910 struct mount *mp; 5911 struct inode *ip; 5912 struct buf *bp; 5913 { 5914 struct indirdep *indirdep, *newindirdep; 5915 struct newblk *newblk; 5916 struct ufsmount *ump; 5917 struct worklist *wk; 5918 struct fs *fs; 5919 ufs2_daddr_t blkno; 5920 5921 ump = VFSTOUFS(mp); 5922 LOCK_OWNED(ump); 5923 indirdep = NULL; 5924 newindirdep = NULL; 5925 fs = ip->i_fs; 5926 for (;;) { 5927 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 5928 if (wk->wk_type != D_INDIRDEP) 5929 continue; 5930 indirdep = WK_INDIRDEP(wk); 5931 break; 5932 } 5933 /* Found on the buffer worklist, no new structure to free. */ 5934 if (indirdep != NULL && newindirdep == NULL) 5935 return (indirdep); 5936 if (indirdep != NULL && newindirdep != NULL) 5937 panic("indirdep_lookup: simultaneous create"); 5938 /* None found on the buffer and a new structure is ready. */ 5939 if (indirdep == NULL && newindirdep != NULL) 5940 break; 5941 /* None found and no new structure available. */ 5942 FREE_LOCK(ump); 5943 newindirdep = malloc(sizeof(struct indirdep), 5944 M_INDIRDEP, M_SOFTDEP_FLAGS); 5945 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, mp); 5946 newindirdep->ir_state = ATTACHED; 5947 if (ip->i_ump->um_fstype == UFS1) 5948 newindirdep->ir_state |= UFS1FMT; 5949 TAILQ_INIT(&newindirdep->ir_trunc); 5950 newindirdep->ir_saveddata = NULL; 5951 LIST_INIT(&newindirdep->ir_deplisthd); 5952 LIST_INIT(&newindirdep->ir_donehd); 5953 LIST_INIT(&newindirdep->ir_writehd); 5954 LIST_INIT(&newindirdep->ir_completehd); 5955 if (bp->b_blkno == bp->b_lblkno) { 5956 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp, 5957 NULL, NULL); 5958 bp->b_blkno = blkno; 5959 } 5960 newindirdep->ir_freeblks = NULL; 5961 newindirdep->ir_savebp = 5962 getblk(ip->i_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0); 5963 newindirdep->ir_bp = bp; 5964 BUF_KERNPROC(newindirdep->ir_savebp); 5965 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount); 5966 ACQUIRE_LOCK(ump); 5967 } 5968 indirdep = newindirdep; 5969 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list); 5970 /* 5971 * If the block is not yet allocated we don't set DEPCOMPLETE so 5972 * that we don't free dependencies until the pointers are valid. 5973 * This could search b_dep for D_ALLOCDIRECT/D_ALLOCINDIR rather 5974 * than using the hash. 5975 */ 5976 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk)) 5977 LIST_INSERT_HEAD(&newblk->nb_indirdeps, indirdep, ir_next); 5978 else 5979 indirdep->ir_state |= DEPCOMPLETE; 5980 return (indirdep); 5981 } 5982 5983 /* 5984 * Called to finish the allocation of the "aip" allocated 5985 * by one of the two routines above. 5986 */ 5987 static struct freefrag * 5988 setup_allocindir_phase2(bp, ip, inodedep, aip, lbn) 5989 struct buf *bp; /* in-memory copy of the indirect block */ 5990 struct inode *ip; /* inode for file being extended */ 5991 struct inodedep *inodedep; /* Inodedep for ip */ 5992 struct allocindir *aip; /* allocindir allocated by the above routines */ 5993 ufs_lbn_t lbn; /* Logical block number for this block. */ 5994 { 5995 struct fs *fs; 5996 struct indirdep *indirdep; 5997 struct allocindir *oldaip; 5998 struct freefrag *freefrag; 5999 struct mount *mp; 6000 6001 LOCK_OWNED(ip->i_ump); 6002 mp = UFSTOVFS(ip->i_ump); 6003 fs = ip->i_fs; 6004 if (bp->b_lblkno >= 0) 6005 panic("setup_allocindir_phase2: not indir blk"); 6006 KASSERT(aip->ai_offset >= 0 && aip->ai_offset < NINDIR(fs), 6007 ("setup_allocindir_phase2: Bad offset %d", aip->ai_offset)); 6008 indirdep = indirdep_lookup(mp, ip, bp); 6009 KASSERT(indirdep->ir_savebp != NULL, 6010 ("setup_allocindir_phase2 NULL ir_savebp")); 6011 aip->ai_indirdep = indirdep; 6012 /* 6013 * Check for an unwritten dependency for this indirect offset. If 6014 * there is, merge the old dependency into the new one. This happens 6015 * as a result of reallocblk only. 6016 */ 6017 freefrag = NULL; 6018 if (aip->ai_oldblkno != 0) { 6019 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) { 6020 if (oldaip->ai_offset == aip->ai_offset) { 6021 freefrag = allocindir_merge(aip, oldaip); 6022 goto done; 6023 } 6024 } 6025 LIST_FOREACH(oldaip, &indirdep->ir_donehd, ai_next) { 6026 if (oldaip->ai_offset == aip->ai_offset) { 6027 freefrag = allocindir_merge(aip, oldaip); 6028 goto done; 6029 } 6030 } 6031 } 6032 done: 6033 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next); 6034 return (freefrag); 6035 } 6036 6037 /* 6038 * Merge two allocindirs which refer to the same block. Move newblock 6039 * dependencies and setup the freefrags appropriately. 6040 */ 6041 static struct freefrag * 6042 allocindir_merge(aip, oldaip) 6043 struct allocindir *aip; 6044 struct allocindir *oldaip; 6045 { 6046 struct freefrag *freefrag; 6047 struct worklist *wk; 6048 6049 if (oldaip->ai_newblkno != aip->ai_oldblkno) 6050 panic("allocindir_merge: blkno"); 6051 aip->ai_oldblkno = oldaip->ai_oldblkno; 6052 freefrag = aip->ai_freefrag; 6053 aip->ai_freefrag = oldaip->ai_freefrag; 6054 oldaip->ai_freefrag = NULL; 6055 KASSERT(freefrag != NULL, ("setup_allocindir_phase2: No freefrag")); 6056 /* 6057 * If we are tracking a new directory-block allocation, 6058 * move it from the old allocindir to the new allocindir. 6059 */ 6060 if ((wk = LIST_FIRST(&oldaip->ai_newdirblk)) != NULL) { 6061 WORKLIST_REMOVE(wk); 6062 if (!LIST_EMPTY(&oldaip->ai_newdirblk)) 6063 panic("allocindir_merge: extra newdirblk"); 6064 WORKLIST_INSERT(&aip->ai_newdirblk, wk); 6065 } 6066 /* 6067 * We can skip journaling for this freefrag and just complete 6068 * any pending journal work for the allocindir that is being 6069 * removed after the freefrag completes. 6070 */ 6071 if (freefrag->ff_jdep) 6072 cancel_jfreefrag(WK_JFREEFRAG(freefrag->ff_jdep)); 6073 LIST_REMOVE(oldaip, ai_next); 6074 freefrag->ff_jdep = (struct worklist *)cancel_newblk(&oldaip->ai_block, 6075 &freefrag->ff_list, &freefrag->ff_jwork); 6076 free_newblk(&oldaip->ai_block); 6077 6078 return (freefrag); 6079 } 6080 6081 static inline void 6082 setup_freedirect(freeblks, ip, i, needj) 6083 struct freeblks *freeblks; 6084 struct inode *ip; 6085 int i; 6086 int needj; 6087 { 6088 ufs2_daddr_t blkno; 6089 int frags; 6090 6091 blkno = DIP(ip, i_db[i]); 6092 if (blkno == 0) 6093 return; 6094 DIP_SET(ip, i_db[i], 0); 6095 frags = sblksize(ip->i_fs, ip->i_size, i); 6096 frags = numfrags(ip->i_fs, frags); 6097 newfreework(ip->i_ump, freeblks, NULL, i, blkno, frags, 0, needj); 6098 } 6099 6100 static inline void 6101 setup_freeext(freeblks, ip, i, needj) 6102 struct freeblks *freeblks; 6103 struct inode *ip; 6104 int i; 6105 int needj; 6106 { 6107 ufs2_daddr_t blkno; 6108 int frags; 6109 6110 blkno = ip->i_din2->di_extb[i]; 6111 if (blkno == 0) 6112 return; 6113 ip->i_din2->di_extb[i] = 0; 6114 frags = sblksize(ip->i_fs, ip->i_din2->di_extsize, i); 6115 frags = numfrags(ip->i_fs, frags); 6116 newfreework(ip->i_ump, freeblks, NULL, -1 - i, blkno, frags, 0, needj); 6117 } 6118 6119 static inline void 6120 setup_freeindir(freeblks, ip, i, lbn, needj) 6121 struct freeblks *freeblks; 6122 struct inode *ip; 6123 int i; 6124 ufs_lbn_t lbn; 6125 int needj; 6126 { 6127 ufs2_daddr_t blkno; 6128 6129 blkno = DIP(ip, i_ib[i]); 6130 if (blkno == 0) 6131 return; 6132 DIP_SET(ip, i_ib[i], 0); 6133 newfreework(ip->i_ump, freeblks, NULL, lbn, blkno, ip->i_fs->fs_frag, 6134 0, needj); 6135 } 6136 6137 static inline struct freeblks * 6138 newfreeblks(mp, ip) 6139 struct mount *mp; 6140 struct inode *ip; 6141 { 6142 struct freeblks *freeblks; 6143 6144 freeblks = malloc(sizeof(struct freeblks), 6145 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO); 6146 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp); 6147 LIST_INIT(&freeblks->fb_jblkdephd); 6148 LIST_INIT(&freeblks->fb_jwork); 6149 freeblks->fb_ref = 0; 6150 freeblks->fb_cgwait = 0; 6151 freeblks->fb_state = ATTACHED; 6152 freeblks->fb_uid = ip->i_uid; 6153 freeblks->fb_inum = ip->i_number; 6154 freeblks->fb_vtype = ITOV(ip)->v_type; 6155 freeblks->fb_modrev = DIP(ip, i_modrev); 6156 freeblks->fb_devvp = ip->i_devvp; 6157 freeblks->fb_chkcnt = 0; 6158 freeblks->fb_len = 0; 6159 6160 return (freeblks); 6161 } 6162 6163 static void 6164 trunc_indirdep(indirdep, freeblks, bp, off) 6165 struct indirdep *indirdep; 6166 struct freeblks *freeblks; 6167 struct buf *bp; 6168 int off; 6169 { 6170 struct allocindir *aip, *aipn; 6171 6172 /* 6173 * The first set of allocindirs won't be in savedbp. 6174 */ 6175 LIST_FOREACH_SAFE(aip, &indirdep->ir_deplisthd, ai_next, aipn) 6176 if (aip->ai_offset > off) 6177 cancel_allocindir(aip, bp, freeblks, 1); 6178 LIST_FOREACH_SAFE(aip, &indirdep->ir_donehd, ai_next, aipn) 6179 if (aip->ai_offset > off) 6180 cancel_allocindir(aip, bp, freeblks, 1); 6181 /* 6182 * These will exist in savedbp. 6183 */ 6184 LIST_FOREACH_SAFE(aip, &indirdep->ir_writehd, ai_next, aipn) 6185 if (aip->ai_offset > off) 6186 cancel_allocindir(aip, NULL, freeblks, 0); 6187 LIST_FOREACH_SAFE(aip, &indirdep->ir_completehd, ai_next, aipn) 6188 if (aip->ai_offset > off) 6189 cancel_allocindir(aip, NULL, freeblks, 0); 6190 } 6191 6192 /* 6193 * Follow the chain of indirects down to lastlbn creating a freework 6194 * structure for each. This will be used to start indir_trunc() at 6195 * the right offset and create the journal records for the parrtial 6196 * truncation. A second step will handle the truncated dependencies. 6197 */ 6198 static int 6199 setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno) 6200 struct freeblks *freeblks; 6201 struct inode *ip; 6202 ufs_lbn_t lbn; 6203 ufs_lbn_t lastlbn; 6204 ufs2_daddr_t blkno; 6205 { 6206 struct indirdep *indirdep; 6207 struct indirdep *indirn; 6208 struct freework *freework; 6209 struct newblk *newblk; 6210 struct mount *mp; 6211 struct buf *bp; 6212 uint8_t *start; 6213 uint8_t *end; 6214 ufs_lbn_t lbnadd; 6215 int level; 6216 int error; 6217 int off; 6218 6219 6220 freework = NULL; 6221 if (blkno == 0) 6222 return (0); 6223 mp = freeblks->fb_list.wk_mp; 6224 bp = getblk(ITOV(ip), lbn, mp->mnt_stat.f_iosize, 0, 0, 0); 6225 if ((bp->b_flags & B_CACHE) == 0) { 6226 bp->b_blkno = blkptrtodb(VFSTOUFS(mp), blkno); 6227 bp->b_iocmd = BIO_READ; 6228 bp->b_flags &= ~B_INVAL; 6229 bp->b_ioflags &= ~BIO_ERROR; 6230 vfs_busy_pages(bp, 0); 6231 bp->b_iooffset = dbtob(bp->b_blkno); 6232 bstrategy(bp); 6233 #ifdef RACCT 6234 if (racct_enable) { 6235 PROC_LOCK(curproc); 6236 racct_add_buf(curproc, bp, 0); 6237 PROC_UNLOCK(curproc); 6238 } 6239 #endif /* RACCT */ 6240 curthread->td_ru.ru_inblock++; 6241 error = bufwait(bp); 6242 if (error) { 6243 brelse(bp); 6244 return (error); 6245 } 6246 } 6247 level = lbn_level(lbn); 6248 lbnadd = lbn_offset(ip->i_fs, level); 6249 /* 6250 * Compute the offset of the last block we want to keep. Store 6251 * in the freework the first block we want to completely free. 6252 */ 6253 off = (lastlbn - -(lbn + level)) / lbnadd; 6254 if (off + 1 == NINDIR(ip->i_fs)) 6255 goto nowork; 6256 freework = newfreework(ip->i_ump, freeblks, NULL, lbn, blkno, 0, off+1, 6257 0); 6258 /* 6259 * Link the freework into the indirdep. This will prevent any new 6260 * allocations from proceeding until we are finished with the 6261 * truncate and the block is written. 6262 */ 6263 ACQUIRE_LOCK(ip->i_ump); 6264 indirdep = indirdep_lookup(mp, ip, bp); 6265 if (indirdep->ir_freeblks) 6266 panic("setup_trunc_indir: indirdep already truncated."); 6267 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, freework, fw_next); 6268 freework->fw_indir = indirdep; 6269 /* 6270 * Cancel any allocindirs that will not make it to disk. 6271 * We have to do this for all copies of the indirdep that 6272 * live on this newblk. 6273 */ 6274 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 6275 newblk_lookup(mp, dbtofsb(ip->i_fs, bp->b_blkno), 0, &newblk); 6276 LIST_FOREACH(indirn, &newblk->nb_indirdeps, ir_next) 6277 trunc_indirdep(indirn, freeblks, bp, off); 6278 } else 6279 trunc_indirdep(indirdep, freeblks, bp, off); 6280 FREE_LOCK(ip->i_ump); 6281 /* 6282 * Creation is protected by the buf lock. The saveddata is only 6283 * needed if a full truncation follows a partial truncation but it 6284 * is difficult to allocate in that case so we fetch it anyway. 6285 */ 6286 if (indirdep->ir_saveddata == NULL) 6287 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 6288 M_SOFTDEP_FLAGS); 6289 nowork: 6290 /* Fetch the blkno of the child and the zero start offset. */ 6291 if (ip->i_ump->um_fstype == UFS1) { 6292 blkno = ((ufs1_daddr_t *)bp->b_data)[off]; 6293 start = (uint8_t *)&((ufs1_daddr_t *)bp->b_data)[off+1]; 6294 } else { 6295 blkno = ((ufs2_daddr_t *)bp->b_data)[off]; 6296 start = (uint8_t *)&((ufs2_daddr_t *)bp->b_data)[off+1]; 6297 } 6298 if (freework) { 6299 /* Zero the truncated pointers. */ 6300 end = bp->b_data + bp->b_bcount; 6301 bzero(start, end - start); 6302 bdwrite(bp); 6303 } else 6304 bqrelse(bp); 6305 if (level == 0) 6306 return (0); 6307 lbn++; /* adjust level */ 6308 lbn -= (off * lbnadd); 6309 return setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno); 6310 } 6311 6312 /* 6313 * Complete the partial truncation of an indirect block setup by 6314 * setup_trunc_indir(). This zeros the truncated pointers in the saved 6315 * copy and writes them to disk before the freeblks is allowed to complete. 6316 */ 6317 static void 6318 complete_trunc_indir(freework) 6319 struct freework *freework; 6320 { 6321 struct freework *fwn; 6322 struct indirdep *indirdep; 6323 struct ufsmount *ump; 6324 struct buf *bp; 6325 uintptr_t start; 6326 int count; 6327 6328 ump = VFSTOUFS(freework->fw_list.wk_mp); 6329 LOCK_OWNED(ump); 6330 indirdep = freework->fw_indir; 6331 for (;;) { 6332 bp = indirdep->ir_bp; 6333 /* See if the block was discarded. */ 6334 if (bp == NULL) 6335 break; 6336 /* Inline part of getdirtybuf(). We dont want bremfree. */ 6337 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) 6338 break; 6339 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 6340 LOCK_PTR(ump)) == 0) 6341 BUF_UNLOCK(bp); 6342 ACQUIRE_LOCK(ump); 6343 } 6344 freework->fw_state |= DEPCOMPLETE; 6345 TAILQ_REMOVE(&indirdep->ir_trunc, freework, fw_next); 6346 /* 6347 * Zero the pointers in the saved copy. 6348 */ 6349 if (indirdep->ir_state & UFS1FMT) 6350 start = sizeof(ufs1_daddr_t); 6351 else 6352 start = sizeof(ufs2_daddr_t); 6353 start *= freework->fw_start; 6354 count = indirdep->ir_savebp->b_bcount - start; 6355 start += (uintptr_t)indirdep->ir_savebp->b_data; 6356 bzero((char *)start, count); 6357 /* 6358 * We need to start the next truncation in the list if it has not 6359 * been started yet. 6360 */ 6361 fwn = TAILQ_FIRST(&indirdep->ir_trunc); 6362 if (fwn != NULL) { 6363 if (fwn->fw_freeblks == indirdep->ir_freeblks) 6364 TAILQ_REMOVE(&indirdep->ir_trunc, fwn, fw_next); 6365 if ((fwn->fw_state & ONWORKLIST) == 0) 6366 freework_enqueue(fwn); 6367 } 6368 /* 6369 * If bp is NULL the block was fully truncated, restore 6370 * the saved block list otherwise free it if it is no 6371 * longer needed. 6372 */ 6373 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 6374 if (bp == NULL) 6375 bcopy(indirdep->ir_saveddata, 6376 indirdep->ir_savebp->b_data, 6377 indirdep->ir_savebp->b_bcount); 6378 free(indirdep->ir_saveddata, M_INDIRDEP); 6379 indirdep->ir_saveddata = NULL; 6380 } 6381 /* 6382 * When bp is NULL there is a full truncation pending. We 6383 * must wait for this full truncation to be journaled before 6384 * we can release this freework because the disk pointers will 6385 * never be written as zero. 6386 */ 6387 if (bp == NULL) { 6388 if (LIST_EMPTY(&indirdep->ir_freeblks->fb_jblkdephd)) 6389 handle_written_freework(freework); 6390 else 6391 WORKLIST_INSERT(&indirdep->ir_freeblks->fb_freeworkhd, 6392 &freework->fw_list); 6393 } else { 6394 /* Complete when the real copy is written. */ 6395 WORKLIST_INSERT(&bp->b_dep, &freework->fw_list); 6396 BUF_UNLOCK(bp); 6397 } 6398 } 6399 6400 /* 6401 * Calculate the number of blocks we are going to release where datablocks 6402 * is the current total and length is the new file size. 6403 */ 6404 static ufs2_daddr_t 6405 blkcount(fs, datablocks, length) 6406 struct fs *fs; 6407 ufs2_daddr_t datablocks; 6408 off_t length; 6409 { 6410 off_t totblks, numblks; 6411 6412 totblks = 0; 6413 numblks = howmany(length, fs->fs_bsize); 6414 if (numblks <= NDADDR) { 6415 totblks = howmany(length, fs->fs_fsize); 6416 goto out; 6417 } 6418 totblks = blkstofrags(fs, numblks); 6419 numblks -= NDADDR; 6420 /* 6421 * Count all single, then double, then triple indirects required. 6422 * Subtracting one indirects worth of blocks for each pass 6423 * acknowledges one of each pointed to by the inode. 6424 */ 6425 for (;;) { 6426 totblks += blkstofrags(fs, howmany(numblks, NINDIR(fs))); 6427 numblks -= NINDIR(fs); 6428 if (numblks <= 0) 6429 break; 6430 numblks = howmany(numblks, NINDIR(fs)); 6431 } 6432 out: 6433 totblks = fsbtodb(fs, totblks); 6434 /* 6435 * Handle sparse files. We can't reclaim more blocks than the inode 6436 * references. We will correct it later in handle_complete_freeblks() 6437 * when we know the real count. 6438 */ 6439 if (totblks > datablocks) 6440 return (0); 6441 return (datablocks - totblks); 6442 } 6443 6444 /* 6445 * Handle freeblocks for journaled softupdate filesystems. 6446 * 6447 * Contrary to normal softupdates, we must preserve the block pointers in 6448 * indirects until their subordinates are free. This is to avoid journaling 6449 * every block that is freed which may consume more space than the journal 6450 * itself. The recovery program will see the free block journals at the 6451 * base of the truncated area and traverse them to reclaim space. The 6452 * pointers in the inode may be cleared immediately after the journal 6453 * records are written because each direct and indirect pointer in the 6454 * inode is recorded in a journal. This permits full truncation to proceed 6455 * asynchronously. The write order is journal -> inode -> cgs -> indirects. 6456 * 6457 * The algorithm is as follows: 6458 * 1) Traverse the in-memory state and create journal entries to release 6459 * the relevant blocks and full indirect trees. 6460 * 2) Traverse the indirect block chain adding partial truncation freework 6461 * records to indirects in the path to lastlbn. The freework will 6462 * prevent new allocation dependencies from being satisfied in this 6463 * indirect until the truncation completes. 6464 * 3) Read and lock the inode block, performing an update with the new size 6465 * and pointers. This prevents truncated data from becoming valid on 6466 * disk through step 4. 6467 * 4) Reap unsatisfied dependencies that are beyond the truncated area, 6468 * eliminate journal work for those records that do not require it. 6469 * 5) Schedule the journal records to be written followed by the inode block. 6470 * 6) Allocate any necessary frags for the end of file. 6471 * 7) Zero any partially truncated blocks. 6472 * 6473 * From this truncation proceeds asynchronously using the freework and 6474 * indir_trunc machinery. The file will not be extended again into a 6475 * partially truncated indirect block until all work is completed but 6476 * the normal dependency mechanism ensures that it is rolled back/forward 6477 * as appropriate. Further truncation may occur without delay and is 6478 * serialized in indir_trunc(). 6479 */ 6480 void 6481 softdep_journal_freeblocks(ip, cred, length, flags) 6482 struct inode *ip; /* The inode whose length is to be reduced */ 6483 struct ucred *cred; 6484 off_t length; /* The new length for the file */ 6485 int flags; /* IO_EXT and/or IO_NORMAL */ 6486 { 6487 struct freeblks *freeblks, *fbn; 6488 struct worklist *wk, *wkn; 6489 struct inodedep *inodedep; 6490 struct jblkdep *jblkdep; 6491 struct allocdirect *adp, *adpn; 6492 struct ufsmount *ump; 6493 struct fs *fs; 6494 struct buf *bp; 6495 struct vnode *vp; 6496 struct mount *mp; 6497 ufs2_daddr_t extblocks, datablocks; 6498 ufs_lbn_t tmpval, lbn, lastlbn; 6499 int frags, lastoff, iboff, allocblock, needj, error, i; 6500 6501 fs = ip->i_fs; 6502 ump = ip->i_ump; 6503 mp = UFSTOVFS(ump); 6504 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6505 ("softdep_journal_freeblocks called on non-softdep filesystem")); 6506 vp = ITOV(ip); 6507 needj = 1; 6508 iboff = -1; 6509 allocblock = 0; 6510 extblocks = 0; 6511 datablocks = 0; 6512 frags = 0; 6513 freeblks = newfreeblks(mp, ip); 6514 ACQUIRE_LOCK(ump); 6515 /* 6516 * If we're truncating a removed file that will never be written 6517 * we don't need to journal the block frees. The canceled journals 6518 * for the allocations will suffice. 6519 */ 6520 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6521 if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED && 6522 length == 0) 6523 needj = 0; 6524 CTR3(KTR_SUJ, "softdep_journal_freeblks: ip %d length %ld needj %d", 6525 ip->i_number, length, needj); 6526 FREE_LOCK(ump); 6527 /* 6528 * Calculate the lbn that we are truncating to. This results in -1 6529 * if we're truncating the 0 bytes. So it is the last lbn we want 6530 * to keep, not the first lbn we want to truncate. 6531 */ 6532 lastlbn = lblkno(fs, length + fs->fs_bsize - 1) - 1; 6533 lastoff = blkoff(fs, length); 6534 /* 6535 * Compute frags we are keeping in lastlbn. 0 means all. 6536 */ 6537 if (lastlbn >= 0 && lastlbn < NDADDR) { 6538 frags = fragroundup(fs, lastoff); 6539 /* adp offset of last valid allocdirect. */ 6540 iboff = lastlbn; 6541 } else if (lastlbn > 0) 6542 iboff = NDADDR; 6543 if (fs->fs_magic == FS_UFS2_MAGIC) 6544 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 6545 /* 6546 * Handle normal data blocks and indirects. This section saves 6547 * values used after the inode update to complete frag and indirect 6548 * truncation. 6549 */ 6550 if ((flags & IO_NORMAL) != 0) { 6551 /* 6552 * Handle truncation of whole direct and indirect blocks. 6553 */ 6554 for (i = iboff + 1; i < NDADDR; i++) 6555 setup_freedirect(freeblks, ip, i, needj); 6556 for (i = 0, tmpval = NINDIR(fs), lbn = NDADDR; i < NIADDR; 6557 i++, lbn += tmpval, tmpval *= NINDIR(fs)) { 6558 /* Release a whole indirect tree. */ 6559 if (lbn > lastlbn) { 6560 setup_freeindir(freeblks, ip, i, -lbn -i, 6561 needj); 6562 continue; 6563 } 6564 iboff = i + NDADDR; 6565 /* 6566 * Traverse partially truncated indirect tree. 6567 */ 6568 if (lbn <= lastlbn && lbn + tmpval - 1 > lastlbn) 6569 setup_trunc_indir(freeblks, ip, -lbn - i, 6570 lastlbn, DIP(ip, i_ib[i])); 6571 } 6572 /* 6573 * Handle partial truncation to a frag boundary. 6574 */ 6575 if (frags) { 6576 ufs2_daddr_t blkno; 6577 long oldfrags; 6578 6579 oldfrags = blksize(fs, ip, lastlbn); 6580 blkno = DIP(ip, i_db[lastlbn]); 6581 if (blkno && oldfrags != frags) { 6582 oldfrags -= frags; 6583 oldfrags = numfrags(ip->i_fs, oldfrags); 6584 blkno += numfrags(ip->i_fs, frags); 6585 newfreework(ump, freeblks, NULL, lastlbn, 6586 blkno, oldfrags, 0, needj); 6587 if (needj) 6588 adjust_newfreework(freeblks, 6589 numfrags(ip->i_fs, frags)); 6590 } else if (blkno == 0) 6591 allocblock = 1; 6592 } 6593 /* 6594 * Add a journal record for partial truncate if we are 6595 * handling indirect blocks. Non-indirects need no extra 6596 * journaling. 6597 */ 6598 if (length != 0 && lastlbn >= NDADDR) { 6599 ip->i_flag |= IN_TRUNCATED; 6600 newjtrunc(freeblks, length, 0); 6601 } 6602 ip->i_size = length; 6603 DIP_SET(ip, i_size, ip->i_size); 6604 datablocks = DIP(ip, i_blocks) - extblocks; 6605 if (length != 0) 6606 datablocks = blkcount(ip->i_fs, datablocks, length); 6607 freeblks->fb_len = length; 6608 } 6609 if ((flags & IO_EXT) != 0) { 6610 for (i = 0; i < NXADDR; i++) 6611 setup_freeext(freeblks, ip, i, needj); 6612 ip->i_din2->di_extsize = 0; 6613 datablocks += extblocks; 6614 } 6615 #ifdef QUOTA 6616 /* Reference the quotas in case the block count is wrong in the end. */ 6617 quotaref(vp, freeblks->fb_quota); 6618 (void) chkdq(ip, -datablocks, NOCRED, 0); 6619 #endif 6620 freeblks->fb_chkcnt = -datablocks; 6621 UFS_LOCK(ump); 6622 fs->fs_pendingblocks += datablocks; 6623 UFS_UNLOCK(ump); 6624 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 6625 /* 6626 * Handle truncation of incomplete alloc direct dependencies. We 6627 * hold the inode block locked to prevent incomplete dependencies 6628 * from reaching the disk while we are eliminating those that 6629 * have been truncated. This is a partially inlined ffs_update(). 6630 */ 6631 ufs_itimes(vp); 6632 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED); 6633 error = bread(ip->i_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 6634 (int)fs->fs_bsize, cred, &bp); 6635 if (error) { 6636 brelse(bp); 6637 softdep_error("softdep_journal_freeblocks", error); 6638 return; 6639 } 6640 if (bp->b_bufsize == fs->fs_bsize) 6641 bp->b_flags |= B_CLUSTEROK; 6642 softdep_update_inodeblock(ip, bp, 0); 6643 if (ump->um_fstype == UFS1) 6644 *((struct ufs1_dinode *)bp->b_data + 6645 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1; 6646 else 6647 *((struct ufs2_dinode *)bp->b_data + 6648 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2; 6649 ACQUIRE_LOCK(ump); 6650 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6651 if ((inodedep->id_state & IOSTARTED) != 0) 6652 panic("softdep_setup_freeblocks: inode busy"); 6653 /* 6654 * Add the freeblks structure to the list of operations that 6655 * must await the zero'ed inode being written to disk. If we 6656 * still have a bitmap dependency (needj), then the inode 6657 * has never been written to disk, so we can process the 6658 * freeblks below once we have deleted the dependencies. 6659 */ 6660 if (needj) 6661 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 6662 else 6663 freeblks->fb_state |= COMPLETE; 6664 if ((flags & IO_NORMAL) != 0) { 6665 TAILQ_FOREACH_SAFE(adp, &inodedep->id_inoupdt, ad_next, adpn) { 6666 if (adp->ad_offset > iboff) 6667 cancel_allocdirect(&inodedep->id_inoupdt, adp, 6668 freeblks); 6669 /* 6670 * Truncate the allocdirect. We could eliminate 6671 * or modify journal records as well. 6672 */ 6673 else if (adp->ad_offset == iboff && frags) 6674 adp->ad_newsize = frags; 6675 } 6676 } 6677 if ((flags & IO_EXT) != 0) 6678 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 6679 cancel_allocdirect(&inodedep->id_extupdt, adp, 6680 freeblks); 6681 /* 6682 * Scan the bufwait list for newblock dependencies that will never 6683 * make it to disk. 6684 */ 6685 LIST_FOREACH_SAFE(wk, &inodedep->id_bufwait, wk_list, wkn) { 6686 if (wk->wk_type != D_ALLOCDIRECT) 6687 continue; 6688 adp = WK_ALLOCDIRECT(wk); 6689 if (((flags & IO_NORMAL) != 0 && (adp->ad_offset > iboff)) || 6690 ((flags & IO_EXT) != 0 && (adp->ad_state & EXTDATA))) { 6691 cancel_jfreeblk(freeblks, adp->ad_newblkno); 6692 cancel_newblk(WK_NEWBLK(wk), NULL, &freeblks->fb_jwork); 6693 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 6694 } 6695 } 6696 /* 6697 * Add journal work. 6698 */ 6699 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) 6700 add_to_journal(&jblkdep->jb_list); 6701 FREE_LOCK(ump); 6702 bdwrite(bp); 6703 /* 6704 * Truncate dependency structures beyond length. 6705 */ 6706 trunc_dependencies(ip, freeblks, lastlbn, frags, flags); 6707 /* 6708 * This is only set when we need to allocate a fragment because 6709 * none existed at the end of a frag-sized file. It handles only 6710 * allocating a new, zero filled block. 6711 */ 6712 if (allocblock) { 6713 ip->i_size = length - lastoff; 6714 DIP_SET(ip, i_size, ip->i_size); 6715 error = UFS_BALLOC(vp, length - 1, 1, cred, BA_CLRBUF, &bp); 6716 if (error != 0) { 6717 softdep_error("softdep_journal_freeblks", error); 6718 return; 6719 } 6720 ip->i_size = length; 6721 DIP_SET(ip, i_size, length); 6722 ip->i_flag |= IN_CHANGE | IN_UPDATE; 6723 allocbuf(bp, frags); 6724 ffs_update(vp, 0); 6725 bawrite(bp); 6726 } else if (lastoff != 0 && vp->v_type != VDIR) { 6727 int size; 6728 6729 /* 6730 * Zero the end of a truncated frag or block. 6731 */ 6732 size = sblksize(fs, length, lastlbn); 6733 error = bread(vp, lastlbn, size, cred, &bp); 6734 if (error) { 6735 softdep_error("softdep_journal_freeblks", error); 6736 return; 6737 } 6738 bzero((char *)bp->b_data + lastoff, size - lastoff); 6739 bawrite(bp); 6740 6741 } 6742 ACQUIRE_LOCK(ump); 6743 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6744 TAILQ_INSERT_TAIL(&inodedep->id_freeblklst, freeblks, fb_next); 6745 freeblks->fb_state |= DEPCOMPLETE | ONDEPLIST; 6746 /* 6747 * We zero earlier truncations so they don't erroneously 6748 * update i_blocks. 6749 */ 6750 if (freeblks->fb_len == 0 && (flags & IO_NORMAL) != 0) 6751 TAILQ_FOREACH(fbn, &inodedep->id_freeblklst, fb_next) 6752 fbn->fb_len = 0; 6753 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE && 6754 LIST_EMPTY(&freeblks->fb_jblkdephd)) 6755 freeblks->fb_state |= INPROGRESS; 6756 else 6757 freeblks = NULL; 6758 FREE_LOCK(ump); 6759 if (freeblks) 6760 handle_workitem_freeblocks(freeblks, 0); 6761 trunc_pages(ip, length, extblocks, flags); 6762 6763 } 6764 6765 /* 6766 * Flush a JOP_SYNC to the journal. 6767 */ 6768 void 6769 softdep_journal_fsync(ip) 6770 struct inode *ip; 6771 { 6772 struct jfsync *jfsync; 6773 6774 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0, 6775 ("softdep_journal_fsync called on non-softdep filesystem")); 6776 if ((ip->i_flag & IN_TRUNCATED) == 0) 6777 return; 6778 ip->i_flag &= ~IN_TRUNCATED; 6779 jfsync = malloc(sizeof(*jfsync), M_JFSYNC, M_SOFTDEP_FLAGS | M_ZERO); 6780 workitem_alloc(&jfsync->jfs_list, D_JFSYNC, UFSTOVFS(ip->i_ump)); 6781 jfsync->jfs_size = ip->i_size; 6782 jfsync->jfs_ino = ip->i_number; 6783 ACQUIRE_LOCK(ip->i_ump); 6784 add_to_journal(&jfsync->jfs_list); 6785 jwait(&jfsync->jfs_list, MNT_WAIT); 6786 FREE_LOCK(ip->i_ump); 6787 } 6788 6789 /* 6790 * Block de-allocation dependencies. 6791 * 6792 * When blocks are de-allocated, the on-disk pointers must be nullified before 6793 * the blocks are made available for use by other files. (The true 6794 * requirement is that old pointers must be nullified before new on-disk 6795 * pointers are set. We chose this slightly more stringent requirement to 6796 * reduce complexity.) Our implementation handles this dependency by updating 6797 * the inode (or indirect block) appropriately but delaying the actual block 6798 * de-allocation (i.e., freemap and free space count manipulation) until 6799 * after the updated versions reach stable storage. After the disk is 6800 * updated, the blocks can be safely de-allocated whenever it is convenient. 6801 * This implementation handles only the common case of reducing a file's 6802 * length to zero. Other cases are handled by the conventional synchronous 6803 * write approach. 6804 * 6805 * The ffs implementation with which we worked double-checks 6806 * the state of the block pointers and file size as it reduces 6807 * a file's length. Some of this code is replicated here in our 6808 * soft updates implementation. The freeblks->fb_chkcnt field is 6809 * used to transfer a part of this information to the procedure 6810 * that eventually de-allocates the blocks. 6811 * 6812 * This routine should be called from the routine that shortens 6813 * a file's length, before the inode's size or block pointers 6814 * are modified. It will save the block pointer information for 6815 * later release and zero the inode so that the calling routine 6816 * can release it. 6817 */ 6818 void 6819 softdep_setup_freeblocks(ip, length, flags) 6820 struct inode *ip; /* The inode whose length is to be reduced */ 6821 off_t length; /* The new length for the file */ 6822 int flags; /* IO_EXT and/or IO_NORMAL */ 6823 { 6824 struct ufs1_dinode *dp1; 6825 struct ufs2_dinode *dp2; 6826 struct freeblks *freeblks; 6827 struct inodedep *inodedep; 6828 struct allocdirect *adp; 6829 struct ufsmount *ump; 6830 struct buf *bp; 6831 struct fs *fs; 6832 ufs2_daddr_t extblocks, datablocks; 6833 struct mount *mp; 6834 int i, delay, error; 6835 ufs_lbn_t tmpval; 6836 ufs_lbn_t lbn; 6837 6838 ump = ip->i_ump; 6839 mp = UFSTOVFS(ump); 6840 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6841 ("softdep_setup_freeblocks called on non-softdep filesystem")); 6842 CTR2(KTR_SUJ, "softdep_setup_freeblks: ip %d length %ld", 6843 ip->i_number, length); 6844 KASSERT(length == 0, ("softdep_setup_freeblocks: non-zero length")); 6845 fs = ip->i_fs; 6846 if ((error = bread(ip->i_devvp, 6847 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 6848 (int)fs->fs_bsize, NOCRED, &bp)) != 0) { 6849 brelse(bp); 6850 softdep_error("softdep_setup_freeblocks", error); 6851 return; 6852 } 6853 freeblks = newfreeblks(mp, ip); 6854 extblocks = 0; 6855 datablocks = 0; 6856 if (fs->fs_magic == FS_UFS2_MAGIC) 6857 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 6858 if ((flags & IO_NORMAL) != 0) { 6859 for (i = 0; i < NDADDR; i++) 6860 setup_freedirect(freeblks, ip, i, 0); 6861 for (i = 0, tmpval = NINDIR(fs), lbn = NDADDR; i < NIADDR; 6862 i++, lbn += tmpval, tmpval *= NINDIR(fs)) 6863 setup_freeindir(freeblks, ip, i, -lbn -i, 0); 6864 ip->i_size = 0; 6865 DIP_SET(ip, i_size, 0); 6866 datablocks = DIP(ip, i_blocks) - extblocks; 6867 } 6868 if ((flags & IO_EXT) != 0) { 6869 for (i = 0; i < NXADDR; i++) 6870 setup_freeext(freeblks, ip, i, 0); 6871 ip->i_din2->di_extsize = 0; 6872 datablocks += extblocks; 6873 } 6874 #ifdef QUOTA 6875 /* Reference the quotas in case the block count is wrong in the end. */ 6876 quotaref(ITOV(ip), freeblks->fb_quota); 6877 (void) chkdq(ip, -datablocks, NOCRED, 0); 6878 #endif 6879 freeblks->fb_chkcnt = -datablocks; 6880 UFS_LOCK(ump); 6881 fs->fs_pendingblocks += datablocks; 6882 UFS_UNLOCK(ump); 6883 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 6884 /* 6885 * Push the zero'ed inode to to its disk buffer so that we are free 6886 * to delete its dependencies below. Once the dependencies are gone 6887 * the buffer can be safely released. 6888 */ 6889 if (ump->um_fstype == UFS1) { 6890 dp1 = ((struct ufs1_dinode *)bp->b_data + 6891 ino_to_fsbo(fs, ip->i_number)); 6892 ip->i_din1->di_freelink = dp1->di_freelink; 6893 *dp1 = *ip->i_din1; 6894 } else { 6895 dp2 = ((struct ufs2_dinode *)bp->b_data + 6896 ino_to_fsbo(fs, ip->i_number)); 6897 ip->i_din2->di_freelink = dp2->di_freelink; 6898 *dp2 = *ip->i_din2; 6899 } 6900 /* 6901 * Find and eliminate any inode dependencies. 6902 */ 6903 ACQUIRE_LOCK(ump); 6904 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6905 if ((inodedep->id_state & IOSTARTED) != 0) 6906 panic("softdep_setup_freeblocks: inode busy"); 6907 /* 6908 * Add the freeblks structure to the list of operations that 6909 * must await the zero'ed inode being written to disk. If we 6910 * still have a bitmap dependency (delay == 0), then the inode 6911 * has never been written to disk, so we can process the 6912 * freeblks below once we have deleted the dependencies. 6913 */ 6914 delay = (inodedep->id_state & DEPCOMPLETE); 6915 if (delay) 6916 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 6917 else 6918 freeblks->fb_state |= COMPLETE; 6919 /* 6920 * Because the file length has been truncated to zero, any 6921 * pending block allocation dependency structures associated 6922 * with this inode are obsolete and can simply be de-allocated. 6923 * We must first merge the two dependency lists to get rid of 6924 * any duplicate freefrag structures, then purge the merged list. 6925 * If we still have a bitmap dependency, then the inode has never 6926 * been written to disk, so we can free any fragments without delay. 6927 */ 6928 if (flags & IO_NORMAL) { 6929 merge_inode_lists(&inodedep->id_newinoupdt, 6930 &inodedep->id_inoupdt); 6931 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 6932 cancel_allocdirect(&inodedep->id_inoupdt, adp, 6933 freeblks); 6934 } 6935 if (flags & IO_EXT) { 6936 merge_inode_lists(&inodedep->id_newextupdt, 6937 &inodedep->id_extupdt); 6938 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 6939 cancel_allocdirect(&inodedep->id_extupdt, adp, 6940 freeblks); 6941 } 6942 FREE_LOCK(ump); 6943 bdwrite(bp); 6944 trunc_dependencies(ip, freeblks, -1, 0, flags); 6945 ACQUIRE_LOCK(ump); 6946 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 6947 (void) free_inodedep(inodedep); 6948 freeblks->fb_state |= DEPCOMPLETE; 6949 /* 6950 * If the inode with zeroed block pointers is now on disk 6951 * we can start freeing blocks. 6952 */ 6953 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 6954 freeblks->fb_state |= INPROGRESS; 6955 else 6956 freeblks = NULL; 6957 FREE_LOCK(ump); 6958 if (freeblks) 6959 handle_workitem_freeblocks(freeblks, 0); 6960 trunc_pages(ip, length, extblocks, flags); 6961 } 6962 6963 /* 6964 * Eliminate pages from the page cache that back parts of this inode and 6965 * adjust the vnode pager's idea of our size. This prevents stale data 6966 * from hanging around in the page cache. 6967 */ 6968 static void 6969 trunc_pages(ip, length, extblocks, flags) 6970 struct inode *ip; 6971 off_t length; 6972 ufs2_daddr_t extblocks; 6973 int flags; 6974 { 6975 struct vnode *vp; 6976 struct fs *fs; 6977 ufs_lbn_t lbn; 6978 off_t end, extend; 6979 6980 vp = ITOV(ip); 6981 fs = ip->i_fs; 6982 extend = OFF_TO_IDX(lblktosize(fs, -extblocks)); 6983 if ((flags & IO_EXT) != 0) 6984 vn_pages_remove(vp, extend, 0); 6985 if ((flags & IO_NORMAL) == 0) 6986 return; 6987 BO_LOCK(&vp->v_bufobj); 6988 drain_output(vp); 6989 BO_UNLOCK(&vp->v_bufobj); 6990 /* 6991 * The vnode pager eliminates file pages we eliminate indirects 6992 * below. 6993 */ 6994 vnode_pager_setsize(vp, length); 6995 /* 6996 * Calculate the end based on the last indirect we want to keep. If 6997 * the block extends into indirects we can just use the negative of 6998 * its lbn. Doubles and triples exist at lower numbers so we must 6999 * be careful not to remove those, if they exist. double and triple 7000 * indirect lbns do not overlap with others so it is not important 7001 * to verify how many levels are required. 7002 */ 7003 lbn = lblkno(fs, length); 7004 if (lbn >= NDADDR) { 7005 /* Calculate the virtual lbn of the triple indirect. */ 7006 lbn = -lbn - (NIADDR - 1); 7007 end = OFF_TO_IDX(lblktosize(fs, lbn)); 7008 } else 7009 end = extend; 7010 vn_pages_remove(vp, OFF_TO_IDX(OFF_MAX), end); 7011 } 7012 7013 /* 7014 * See if the buf bp is in the range eliminated by truncation. 7015 */ 7016 static int 7017 trunc_check_buf(bp, blkoffp, lastlbn, lastoff, flags) 7018 struct buf *bp; 7019 int *blkoffp; 7020 ufs_lbn_t lastlbn; 7021 int lastoff; 7022 int flags; 7023 { 7024 ufs_lbn_t lbn; 7025 7026 *blkoffp = 0; 7027 /* Only match ext/normal blocks as appropriate. */ 7028 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) || 7029 ((flags & IO_NORMAL) == 0 && (bp->b_xflags & BX_ALTDATA) == 0)) 7030 return (0); 7031 /* ALTDATA is always a full truncation. */ 7032 if ((bp->b_xflags & BX_ALTDATA) != 0) 7033 return (1); 7034 /* -1 is full truncation. */ 7035 if (lastlbn == -1) 7036 return (1); 7037 /* 7038 * If this is a partial truncate we only want those 7039 * blocks and indirect blocks that cover the range 7040 * we're after. 7041 */ 7042 lbn = bp->b_lblkno; 7043 if (lbn < 0) 7044 lbn = -(lbn + lbn_level(lbn)); 7045 if (lbn < lastlbn) 7046 return (0); 7047 /* Here we only truncate lblkno if it's partial. */ 7048 if (lbn == lastlbn) { 7049 if (lastoff == 0) 7050 return (0); 7051 *blkoffp = lastoff; 7052 } 7053 return (1); 7054 } 7055 7056 /* 7057 * Eliminate any dependencies that exist in memory beyond lblkno:off 7058 */ 7059 static void 7060 trunc_dependencies(ip, freeblks, lastlbn, lastoff, flags) 7061 struct inode *ip; 7062 struct freeblks *freeblks; 7063 ufs_lbn_t lastlbn; 7064 int lastoff; 7065 int flags; 7066 { 7067 struct bufobj *bo; 7068 struct vnode *vp; 7069 struct buf *bp; 7070 int blkoff; 7071 7072 /* 7073 * We must wait for any I/O in progress to finish so that 7074 * all potential buffers on the dirty list will be visible. 7075 * Once they are all there, walk the list and get rid of 7076 * any dependencies. 7077 */ 7078 vp = ITOV(ip); 7079 bo = &vp->v_bufobj; 7080 BO_LOCK(bo); 7081 drain_output(vp); 7082 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) 7083 bp->b_vflags &= ~BV_SCANNED; 7084 restart: 7085 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) { 7086 if (bp->b_vflags & BV_SCANNED) 7087 continue; 7088 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 7089 bp->b_vflags |= BV_SCANNED; 7090 continue; 7091 } 7092 KASSERT(bp->b_bufobj == bo, ("Wrong object in buffer")); 7093 if ((bp = getdirtybuf(bp, BO_LOCKPTR(bo), MNT_WAIT)) == NULL) 7094 goto restart; 7095 BO_UNLOCK(bo); 7096 if (deallocate_dependencies(bp, freeblks, blkoff)) 7097 bqrelse(bp); 7098 else 7099 brelse(bp); 7100 BO_LOCK(bo); 7101 goto restart; 7102 } 7103 /* 7104 * Now do the work of vtruncbuf while also matching indirect blocks. 7105 */ 7106 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) 7107 bp->b_vflags &= ~BV_SCANNED; 7108 cleanrestart: 7109 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) { 7110 if (bp->b_vflags & BV_SCANNED) 7111 continue; 7112 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 7113 bp->b_vflags |= BV_SCANNED; 7114 continue; 7115 } 7116 if (BUF_LOCK(bp, 7117 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 7118 BO_LOCKPTR(bo)) == ENOLCK) { 7119 BO_LOCK(bo); 7120 goto cleanrestart; 7121 } 7122 bp->b_vflags |= BV_SCANNED; 7123 bremfree(bp); 7124 if (blkoff != 0) { 7125 allocbuf(bp, blkoff); 7126 bqrelse(bp); 7127 } else { 7128 bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF; 7129 brelse(bp); 7130 } 7131 BO_LOCK(bo); 7132 goto cleanrestart; 7133 } 7134 drain_output(vp); 7135 BO_UNLOCK(bo); 7136 } 7137 7138 static int 7139 cancel_pagedep(pagedep, freeblks, blkoff) 7140 struct pagedep *pagedep; 7141 struct freeblks *freeblks; 7142 int blkoff; 7143 { 7144 struct jremref *jremref; 7145 struct jmvref *jmvref; 7146 struct dirrem *dirrem, *tmp; 7147 int i; 7148 7149 /* 7150 * Copy any directory remove dependencies to the list 7151 * to be processed after the freeblks proceeds. If 7152 * directory entry never made it to disk they 7153 * can be dumped directly onto the work list. 7154 */ 7155 LIST_FOREACH_SAFE(dirrem, &pagedep->pd_dirremhd, dm_next, tmp) { 7156 /* Skip this directory removal if it is intended to remain. */ 7157 if (dirrem->dm_offset < blkoff) 7158 continue; 7159 /* 7160 * If there are any dirrems we wait for the journal write 7161 * to complete and then restart the buf scan as the lock 7162 * has been dropped. 7163 */ 7164 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) { 7165 jwait(&jremref->jr_list, MNT_WAIT); 7166 return (ERESTART); 7167 } 7168 LIST_REMOVE(dirrem, dm_next); 7169 dirrem->dm_dirinum = pagedep->pd_ino; 7170 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list); 7171 } 7172 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) { 7173 jwait(&jmvref->jm_list, MNT_WAIT); 7174 return (ERESTART); 7175 } 7176 /* 7177 * When we're partially truncating a pagedep we just want to flush 7178 * journal entries and return. There can not be any adds in the 7179 * truncated portion of the directory and newblk must remain if 7180 * part of the block remains. 7181 */ 7182 if (blkoff != 0) { 7183 struct diradd *dap; 7184 7185 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 7186 if (dap->da_offset > blkoff) 7187 panic("cancel_pagedep: diradd %p off %d > %d", 7188 dap, dap->da_offset, blkoff); 7189 for (i = 0; i < DAHASHSZ; i++) 7190 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) 7191 if (dap->da_offset > blkoff) 7192 panic("cancel_pagedep: diradd %p off %d > %d", 7193 dap, dap->da_offset, blkoff); 7194 return (0); 7195 } 7196 /* 7197 * There should be no directory add dependencies present 7198 * as the directory could not be truncated until all 7199 * children were removed. 7200 */ 7201 KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL, 7202 ("deallocate_dependencies: pendinghd != NULL")); 7203 for (i = 0; i < DAHASHSZ; i++) 7204 KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL, 7205 ("deallocate_dependencies: diraddhd != NULL")); 7206 if ((pagedep->pd_state & NEWBLOCK) != 0) 7207 free_newdirblk(pagedep->pd_newdirblk); 7208 if (free_pagedep(pagedep) == 0) 7209 panic("Failed to free pagedep %p", pagedep); 7210 return (0); 7211 } 7212 7213 /* 7214 * Reclaim any dependency structures from a buffer that is about to 7215 * be reallocated to a new vnode. The buffer must be locked, thus, 7216 * no I/O completion operations can occur while we are manipulating 7217 * its associated dependencies. The mutex is held so that other I/O's 7218 * associated with related dependencies do not occur. 7219 */ 7220 static int 7221 deallocate_dependencies(bp, freeblks, off) 7222 struct buf *bp; 7223 struct freeblks *freeblks; 7224 int off; 7225 { 7226 struct indirdep *indirdep; 7227 struct pagedep *pagedep; 7228 struct worklist *wk, *wkn; 7229 struct ufsmount *ump; 7230 7231 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL) 7232 goto done; 7233 ump = VFSTOUFS(wk->wk_mp); 7234 ACQUIRE_LOCK(ump); 7235 LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) { 7236 switch (wk->wk_type) { 7237 case D_INDIRDEP: 7238 indirdep = WK_INDIRDEP(wk); 7239 if (bp->b_lblkno >= 0 || 7240 bp->b_blkno != indirdep->ir_savebp->b_lblkno) 7241 panic("deallocate_dependencies: not indir"); 7242 cancel_indirdep(indirdep, bp, freeblks); 7243 continue; 7244 7245 case D_PAGEDEP: 7246 pagedep = WK_PAGEDEP(wk); 7247 if (cancel_pagedep(pagedep, freeblks, off)) { 7248 FREE_LOCK(ump); 7249 return (ERESTART); 7250 } 7251 continue; 7252 7253 case D_ALLOCINDIR: 7254 /* 7255 * Simply remove the allocindir, we'll find it via 7256 * the indirdep where we can clear pointers if 7257 * needed. 7258 */ 7259 WORKLIST_REMOVE(wk); 7260 continue; 7261 7262 case D_FREEWORK: 7263 /* 7264 * A truncation is waiting for the zero'd pointers 7265 * to be written. It can be freed when the freeblks 7266 * is journaled. 7267 */ 7268 WORKLIST_REMOVE(wk); 7269 wk->wk_state |= ONDEPLIST; 7270 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 7271 break; 7272 7273 case D_ALLOCDIRECT: 7274 if (off != 0) 7275 continue; 7276 /* FALLTHROUGH */ 7277 default: 7278 panic("deallocate_dependencies: Unexpected type %s", 7279 TYPENAME(wk->wk_type)); 7280 /* NOTREACHED */ 7281 } 7282 } 7283 FREE_LOCK(ump); 7284 done: 7285 /* 7286 * Don't throw away this buf, we were partially truncating and 7287 * some deps may always remain. 7288 */ 7289 if (off) { 7290 allocbuf(bp, off); 7291 bp->b_vflags |= BV_SCANNED; 7292 return (EBUSY); 7293 } 7294 bp->b_flags |= B_INVAL | B_NOCACHE; 7295 7296 return (0); 7297 } 7298 7299 /* 7300 * An allocdirect is being canceled due to a truncate. We must make sure 7301 * the journal entry is released in concert with the blkfree that releases 7302 * the storage. Completed journal entries must not be released until the 7303 * space is no longer pointed to by the inode or in the bitmap. 7304 */ 7305 static void 7306 cancel_allocdirect(adphead, adp, freeblks) 7307 struct allocdirectlst *adphead; 7308 struct allocdirect *adp; 7309 struct freeblks *freeblks; 7310 { 7311 struct freework *freework; 7312 struct newblk *newblk; 7313 struct worklist *wk; 7314 7315 TAILQ_REMOVE(adphead, adp, ad_next); 7316 newblk = (struct newblk *)adp; 7317 freework = NULL; 7318 /* 7319 * Find the correct freework structure. 7320 */ 7321 LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) { 7322 if (wk->wk_type != D_FREEWORK) 7323 continue; 7324 freework = WK_FREEWORK(wk); 7325 if (freework->fw_blkno == newblk->nb_newblkno) 7326 break; 7327 } 7328 if (freework == NULL) 7329 panic("cancel_allocdirect: Freework not found"); 7330 /* 7331 * If a newblk exists at all we still have the journal entry that 7332 * initiated the allocation so we do not need to journal the free. 7333 */ 7334 cancel_jfreeblk(freeblks, freework->fw_blkno); 7335 /* 7336 * If the journal hasn't been written the jnewblk must be passed 7337 * to the call to ffs_blkfree that reclaims the space. We accomplish 7338 * this by linking the journal dependency into the freework to be 7339 * freed when freework_freeblock() is called. If the journal has 7340 * been written we can simply reclaim the journal space when the 7341 * freeblks work is complete. 7342 */ 7343 freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list, 7344 &freeblks->fb_jwork); 7345 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 7346 } 7347 7348 7349 /* 7350 * Cancel a new block allocation. May be an indirect or direct block. We 7351 * remove it from various lists and return any journal record that needs to 7352 * be resolved by the caller. 7353 * 7354 * A special consideration is made for indirects which were never pointed 7355 * at on disk and will never be found once this block is released. 7356 */ 7357 static struct jnewblk * 7358 cancel_newblk(newblk, wk, wkhd) 7359 struct newblk *newblk; 7360 struct worklist *wk; 7361 struct workhead *wkhd; 7362 { 7363 struct jnewblk *jnewblk; 7364 7365 CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno); 7366 7367 newblk->nb_state |= GOINGAWAY; 7368 /* 7369 * Previously we traversed the completedhd on each indirdep 7370 * attached to this newblk to cancel them and gather journal 7371 * work. Since we need only the oldest journal segment and 7372 * the lowest point on the tree will always have the oldest 7373 * journal segment we are free to release the segments 7374 * of any subordinates and may leave the indirdep list to 7375 * indirdep_complete() when this newblk is freed. 7376 */ 7377 if (newblk->nb_state & ONDEPLIST) { 7378 newblk->nb_state &= ~ONDEPLIST; 7379 LIST_REMOVE(newblk, nb_deps); 7380 } 7381 if (newblk->nb_state & ONWORKLIST) 7382 WORKLIST_REMOVE(&newblk->nb_list); 7383 /* 7384 * If the journal entry hasn't been written we save a pointer to 7385 * the dependency that frees it until it is written or the 7386 * superseding operation completes. 7387 */ 7388 jnewblk = newblk->nb_jnewblk; 7389 if (jnewblk != NULL && wk != NULL) { 7390 newblk->nb_jnewblk = NULL; 7391 jnewblk->jn_dep = wk; 7392 } 7393 if (!LIST_EMPTY(&newblk->nb_jwork)) 7394 jwork_move(wkhd, &newblk->nb_jwork); 7395 /* 7396 * When truncating we must free the newdirblk early to remove 7397 * the pagedep from the hash before returning. 7398 */ 7399 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7400 free_newdirblk(WK_NEWDIRBLK(wk)); 7401 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7402 panic("cancel_newblk: extra newdirblk"); 7403 7404 return (jnewblk); 7405 } 7406 7407 /* 7408 * Schedule the freefrag associated with a newblk to be released once 7409 * the pointers are written and the previous block is no longer needed. 7410 */ 7411 static void 7412 newblk_freefrag(newblk) 7413 struct newblk *newblk; 7414 { 7415 struct freefrag *freefrag; 7416 7417 if (newblk->nb_freefrag == NULL) 7418 return; 7419 freefrag = newblk->nb_freefrag; 7420 newblk->nb_freefrag = NULL; 7421 freefrag->ff_state |= COMPLETE; 7422 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 7423 add_to_worklist(&freefrag->ff_list, 0); 7424 } 7425 7426 /* 7427 * Free a newblk. Generate a new freefrag work request if appropriate. 7428 * This must be called after the inode pointer and any direct block pointers 7429 * are valid or fully removed via truncate or frag extension. 7430 */ 7431 static void 7432 free_newblk(newblk) 7433 struct newblk *newblk; 7434 { 7435 struct indirdep *indirdep; 7436 struct worklist *wk; 7437 7438 KASSERT(newblk->nb_jnewblk == NULL, 7439 ("free_newblk: jnewblk %p still attached", newblk->nb_jnewblk)); 7440 KASSERT(newblk->nb_list.wk_type != D_NEWBLK, 7441 ("free_newblk: unclaimed newblk")); 7442 LOCK_OWNED(VFSTOUFS(newblk->nb_list.wk_mp)); 7443 newblk_freefrag(newblk); 7444 if (newblk->nb_state & ONDEPLIST) 7445 LIST_REMOVE(newblk, nb_deps); 7446 if (newblk->nb_state & ONWORKLIST) 7447 WORKLIST_REMOVE(&newblk->nb_list); 7448 LIST_REMOVE(newblk, nb_hash); 7449 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7450 free_newdirblk(WK_NEWDIRBLK(wk)); 7451 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7452 panic("free_newblk: extra newdirblk"); 7453 while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL) 7454 indirdep_complete(indirdep); 7455 handle_jwork(&newblk->nb_jwork); 7456 WORKITEM_FREE(newblk, D_NEWBLK); 7457 } 7458 7459 /* 7460 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep. 7461 * This routine must be called with splbio interrupts blocked. 7462 */ 7463 static void 7464 free_newdirblk(newdirblk) 7465 struct newdirblk *newdirblk; 7466 { 7467 struct pagedep *pagedep; 7468 struct diradd *dap; 7469 struct worklist *wk; 7470 7471 LOCK_OWNED(VFSTOUFS(newdirblk->db_list.wk_mp)); 7472 WORKLIST_REMOVE(&newdirblk->db_list); 7473 /* 7474 * If the pagedep is still linked onto the directory buffer 7475 * dependency chain, then some of the entries on the 7476 * pd_pendinghd list may not be committed to disk yet. In 7477 * this case, we will simply clear the NEWBLOCK flag and 7478 * let the pd_pendinghd list be processed when the pagedep 7479 * is next written. If the pagedep is no longer on the buffer 7480 * dependency chain, then all the entries on the pd_pending 7481 * list are committed to disk and we can free them here. 7482 */ 7483 pagedep = newdirblk->db_pagedep; 7484 pagedep->pd_state &= ~NEWBLOCK; 7485 if ((pagedep->pd_state & ONWORKLIST) == 0) { 7486 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 7487 free_diradd(dap, NULL); 7488 /* 7489 * If no dependencies remain, the pagedep will be freed. 7490 */ 7491 free_pagedep(pagedep); 7492 } 7493 /* Should only ever be one item in the list. */ 7494 while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) { 7495 WORKLIST_REMOVE(wk); 7496 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 7497 } 7498 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 7499 } 7500 7501 /* 7502 * Prepare an inode to be freed. The actual free operation is not 7503 * done until the zero'ed inode has been written to disk. 7504 */ 7505 void 7506 softdep_freefile(pvp, ino, mode) 7507 struct vnode *pvp; 7508 ino_t ino; 7509 int mode; 7510 { 7511 struct inode *ip = VTOI(pvp); 7512 struct inodedep *inodedep; 7513 struct freefile *freefile; 7514 struct freeblks *freeblks; 7515 struct ufsmount *ump; 7516 7517 ump = ip->i_ump; 7518 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 7519 ("softdep_freefile called on non-softdep filesystem")); 7520 /* 7521 * This sets up the inode de-allocation dependency. 7522 */ 7523 freefile = malloc(sizeof(struct freefile), 7524 M_FREEFILE, M_SOFTDEP_FLAGS); 7525 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount); 7526 freefile->fx_mode = mode; 7527 freefile->fx_oldinum = ino; 7528 freefile->fx_devvp = ip->i_devvp; 7529 LIST_INIT(&freefile->fx_jwork); 7530 UFS_LOCK(ump); 7531 ip->i_fs->fs_pendinginodes += 1; 7532 UFS_UNLOCK(ump); 7533 7534 /* 7535 * If the inodedep does not exist, then the zero'ed inode has 7536 * been written to disk. If the allocated inode has never been 7537 * written to disk, then the on-disk inode is zero'ed. In either 7538 * case we can free the file immediately. If the journal was 7539 * canceled before being written the inode will never make it to 7540 * disk and we must send the canceled journal entrys to 7541 * ffs_freefile() to be cleared in conjunction with the bitmap. 7542 * Any blocks waiting on the inode to write can be safely freed 7543 * here as it will never been written. 7544 */ 7545 ACQUIRE_LOCK(ump); 7546 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7547 if (inodedep) { 7548 /* 7549 * Clear out freeblks that no longer need to reference 7550 * this inode. 7551 */ 7552 while ((freeblks = 7553 TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) { 7554 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, 7555 fb_next); 7556 freeblks->fb_state &= ~ONDEPLIST; 7557 } 7558 /* 7559 * Remove this inode from the unlinked list. 7560 */ 7561 if (inodedep->id_state & UNLINKED) { 7562 /* 7563 * Save the journal work to be freed with the bitmap 7564 * before we clear UNLINKED. Otherwise it can be lost 7565 * if the inode block is written. 7566 */ 7567 handle_bufwait(inodedep, &freefile->fx_jwork); 7568 clear_unlinked_inodedep(inodedep); 7569 /* 7570 * Re-acquire inodedep as we've dropped the 7571 * per-filesystem lock in clear_unlinked_inodedep(). 7572 */ 7573 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7574 } 7575 } 7576 if (inodedep == NULL || check_inode_unwritten(inodedep)) { 7577 FREE_LOCK(ump); 7578 handle_workitem_freefile(freefile); 7579 return; 7580 } 7581 if ((inodedep->id_state & DEPCOMPLETE) == 0) 7582 inodedep->id_state |= GOINGAWAY; 7583 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list); 7584 FREE_LOCK(ump); 7585 if (ip->i_number == ino) 7586 ip->i_flag |= IN_MODIFIED; 7587 } 7588 7589 /* 7590 * Check to see if an inode has never been written to disk. If 7591 * so free the inodedep and return success, otherwise return failure. 7592 * This routine must be called with splbio interrupts blocked. 7593 * 7594 * If we still have a bitmap dependency, then the inode has never 7595 * been written to disk. Drop the dependency as it is no longer 7596 * necessary since the inode is being deallocated. We set the 7597 * ALLCOMPLETE flags since the bitmap now properly shows that the 7598 * inode is not allocated. Even if the inode is actively being 7599 * written, it has been rolled back to its zero'ed state, so we 7600 * are ensured that a zero inode is what is on the disk. For short 7601 * lived files, this change will usually result in removing all the 7602 * dependencies from the inode so that it can be freed immediately. 7603 */ 7604 static int 7605 check_inode_unwritten(inodedep) 7606 struct inodedep *inodedep; 7607 { 7608 7609 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7610 7611 if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 || 7612 !LIST_EMPTY(&inodedep->id_dirremhd) || 7613 !LIST_EMPTY(&inodedep->id_pendinghd) || 7614 !LIST_EMPTY(&inodedep->id_bufwait) || 7615 !LIST_EMPTY(&inodedep->id_inowait) || 7616 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 7617 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 7618 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 7619 !TAILQ_EMPTY(&inodedep->id_extupdt) || 7620 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 7621 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 7622 inodedep->id_mkdiradd != NULL || 7623 inodedep->id_nlinkdelta != 0) 7624 return (0); 7625 /* 7626 * Another process might be in initiate_write_inodeblock_ufs[12] 7627 * trying to allocate memory without holding "Softdep Lock". 7628 */ 7629 if ((inodedep->id_state & IOSTARTED) != 0 && 7630 inodedep->id_savedino1 == NULL) 7631 return (0); 7632 7633 if (inodedep->id_state & ONDEPLIST) 7634 LIST_REMOVE(inodedep, id_deps); 7635 inodedep->id_state &= ~ONDEPLIST; 7636 inodedep->id_state |= ALLCOMPLETE; 7637 inodedep->id_bmsafemap = NULL; 7638 if (inodedep->id_state & ONWORKLIST) 7639 WORKLIST_REMOVE(&inodedep->id_list); 7640 if (inodedep->id_savedino1 != NULL) { 7641 free(inodedep->id_savedino1, M_SAVEDINO); 7642 inodedep->id_savedino1 = NULL; 7643 } 7644 if (free_inodedep(inodedep) == 0) 7645 panic("check_inode_unwritten: busy inode"); 7646 return (1); 7647 } 7648 7649 static int 7650 check_inodedep_free(inodedep) 7651 struct inodedep *inodedep; 7652 { 7653 7654 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7655 if ((inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE || 7656 !LIST_EMPTY(&inodedep->id_dirremhd) || 7657 !LIST_EMPTY(&inodedep->id_pendinghd) || 7658 !LIST_EMPTY(&inodedep->id_bufwait) || 7659 !LIST_EMPTY(&inodedep->id_inowait) || 7660 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 7661 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 7662 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 7663 !TAILQ_EMPTY(&inodedep->id_extupdt) || 7664 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 7665 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 7666 inodedep->id_mkdiradd != NULL || 7667 inodedep->id_nlinkdelta != 0 || 7668 inodedep->id_savedino1 != NULL) 7669 return (0); 7670 return (1); 7671 } 7672 7673 /* 7674 * Try to free an inodedep structure. Return 1 if it could be freed. 7675 */ 7676 static int 7677 free_inodedep(inodedep) 7678 struct inodedep *inodedep; 7679 { 7680 7681 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7682 if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 || 7683 !check_inodedep_free(inodedep)) 7684 return (0); 7685 if (inodedep->id_state & ONDEPLIST) 7686 LIST_REMOVE(inodedep, id_deps); 7687 LIST_REMOVE(inodedep, id_hash); 7688 WORKITEM_FREE(inodedep, D_INODEDEP); 7689 return (1); 7690 } 7691 7692 /* 7693 * Free the block referenced by a freework structure. The parent freeblks 7694 * structure is released and completed when the final cg bitmap reaches 7695 * the disk. This routine may be freeing a jnewblk which never made it to 7696 * disk in which case we do not have to wait as the operation is undone 7697 * in memory immediately. 7698 */ 7699 static void 7700 freework_freeblock(freework) 7701 struct freework *freework; 7702 { 7703 struct freeblks *freeblks; 7704 struct jnewblk *jnewblk; 7705 struct ufsmount *ump; 7706 struct workhead wkhd; 7707 struct fs *fs; 7708 int bsize; 7709 int needj; 7710 7711 ump = VFSTOUFS(freework->fw_list.wk_mp); 7712 LOCK_OWNED(ump); 7713 /* 7714 * Handle partial truncate separately. 7715 */ 7716 if (freework->fw_indir) { 7717 complete_trunc_indir(freework); 7718 return; 7719 } 7720 freeblks = freework->fw_freeblks; 7721 fs = ump->um_fs; 7722 needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0; 7723 bsize = lfragtosize(fs, freework->fw_frags); 7724 LIST_INIT(&wkhd); 7725 /* 7726 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives 7727 * on the indirblk hashtable and prevents premature freeing. 7728 */ 7729 freework->fw_state |= DEPCOMPLETE; 7730 /* 7731 * SUJ needs to wait for the segment referencing freed indirect 7732 * blocks to expire so that we know the checker will not confuse 7733 * a re-allocated indirect block with its old contents. 7734 */ 7735 if (needj && freework->fw_lbn <= -NDADDR) 7736 indirblk_insert(freework); 7737 /* 7738 * If we are canceling an existing jnewblk pass it to the free 7739 * routine, otherwise pass the freeblk which will ultimately 7740 * release the freeblks. If we're not journaling, we can just 7741 * free the freeblks immediately. 7742 */ 7743 jnewblk = freework->fw_jnewblk; 7744 if (jnewblk != NULL) { 7745 cancel_jnewblk(jnewblk, &wkhd); 7746 needj = 0; 7747 } else if (needj) { 7748 freework->fw_state |= DELAYEDFREE; 7749 freeblks->fb_cgwait++; 7750 WORKLIST_INSERT(&wkhd, &freework->fw_list); 7751 } 7752 FREE_LOCK(ump); 7753 freeblks_free(ump, freeblks, btodb(bsize)); 7754 CTR4(KTR_SUJ, 7755 "freework_freeblock: ino %d blkno %jd lbn %jd size %ld", 7756 freeblks->fb_inum, freework->fw_blkno, freework->fw_lbn, bsize); 7757 ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, bsize, 7758 freeblks->fb_inum, freeblks->fb_vtype, &wkhd); 7759 ACQUIRE_LOCK(ump); 7760 /* 7761 * The jnewblk will be discarded and the bits in the map never 7762 * made it to disk. We can immediately free the freeblk. 7763 */ 7764 if (needj == 0) 7765 handle_written_freework(freework); 7766 } 7767 7768 /* 7769 * We enqueue freework items that need processing back on the freeblks and 7770 * add the freeblks to the worklist. This makes it easier to find all work 7771 * required to flush a truncation in process_truncates(). 7772 */ 7773 static void 7774 freework_enqueue(freework) 7775 struct freework *freework; 7776 { 7777 struct freeblks *freeblks; 7778 7779 freeblks = freework->fw_freeblks; 7780 if ((freework->fw_state & INPROGRESS) == 0) 7781 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 7782 if ((freeblks->fb_state & 7783 (ONWORKLIST | INPROGRESS | ALLCOMPLETE)) == ALLCOMPLETE && 7784 LIST_EMPTY(&freeblks->fb_jblkdephd)) 7785 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 7786 } 7787 7788 /* 7789 * Start, continue, or finish the process of freeing an indirect block tree. 7790 * The free operation may be paused at any point with fw_off containing the 7791 * offset to restart from. This enables us to implement some flow control 7792 * for large truncates which may fan out and generate a huge number of 7793 * dependencies. 7794 */ 7795 static void 7796 handle_workitem_indirblk(freework) 7797 struct freework *freework; 7798 { 7799 struct freeblks *freeblks; 7800 struct ufsmount *ump; 7801 struct fs *fs; 7802 7803 freeblks = freework->fw_freeblks; 7804 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7805 fs = ump->um_fs; 7806 if (freework->fw_state & DEPCOMPLETE) { 7807 handle_written_freework(freework); 7808 return; 7809 } 7810 if (freework->fw_off == NINDIR(fs)) { 7811 freework_freeblock(freework); 7812 return; 7813 } 7814 freework->fw_state |= INPROGRESS; 7815 FREE_LOCK(ump); 7816 indir_trunc(freework, fsbtodb(fs, freework->fw_blkno), 7817 freework->fw_lbn); 7818 ACQUIRE_LOCK(ump); 7819 } 7820 7821 /* 7822 * Called when a freework structure attached to a cg buf is written. The 7823 * ref on either the parent or the freeblks structure is released and 7824 * the freeblks is added back to the worklist if there is more work to do. 7825 */ 7826 static void 7827 handle_written_freework(freework) 7828 struct freework *freework; 7829 { 7830 struct freeblks *freeblks; 7831 struct freework *parent; 7832 7833 freeblks = freework->fw_freeblks; 7834 parent = freework->fw_parent; 7835 if (freework->fw_state & DELAYEDFREE) 7836 freeblks->fb_cgwait--; 7837 freework->fw_state |= COMPLETE; 7838 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 7839 WORKITEM_FREE(freework, D_FREEWORK); 7840 if (parent) { 7841 if (--parent->fw_ref == 0) 7842 freework_enqueue(parent); 7843 return; 7844 } 7845 if (--freeblks->fb_ref != 0) 7846 return; 7847 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) == 7848 ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd)) 7849 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 7850 } 7851 7852 /* 7853 * This workitem routine performs the block de-allocation. 7854 * The workitem is added to the pending list after the updated 7855 * inode block has been written to disk. As mentioned above, 7856 * checks regarding the number of blocks de-allocated (compared 7857 * to the number of blocks allocated for the file) are also 7858 * performed in this function. 7859 */ 7860 static int 7861 handle_workitem_freeblocks(freeblks, flags) 7862 struct freeblks *freeblks; 7863 int flags; 7864 { 7865 struct freework *freework; 7866 struct newblk *newblk; 7867 struct allocindir *aip; 7868 struct ufsmount *ump; 7869 struct worklist *wk; 7870 7871 KASSERT(LIST_EMPTY(&freeblks->fb_jblkdephd), 7872 ("handle_workitem_freeblocks: Journal entries not written.")); 7873 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7874 ACQUIRE_LOCK(ump); 7875 while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) { 7876 WORKLIST_REMOVE(wk); 7877 switch (wk->wk_type) { 7878 case D_DIRREM: 7879 wk->wk_state |= COMPLETE; 7880 add_to_worklist(wk, 0); 7881 continue; 7882 7883 case D_ALLOCDIRECT: 7884 free_newblk(WK_NEWBLK(wk)); 7885 continue; 7886 7887 case D_ALLOCINDIR: 7888 aip = WK_ALLOCINDIR(wk); 7889 freework = NULL; 7890 if (aip->ai_state & DELAYEDFREE) { 7891 FREE_LOCK(ump); 7892 freework = newfreework(ump, freeblks, NULL, 7893 aip->ai_lbn, aip->ai_newblkno, 7894 ump->um_fs->fs_frag, 0, 0); 7895 ACQUIRE_LOCK(ump); 7896 } 7897 newblk = WK_NEWBLK(wk); 7898 if (newblk->nb_jnewblk) { 7899 freework->fw_jnewblk = newblk->nb_jnewblk; 7900 newblk->nb_jnewblk->jn_dep = &freework->fw_list; 7901 newblk->nb_jnewblk = NULL; 7902 } 7903 free_newblk(newblk); 7904 continue; 7905 7906 case D_FREEWORK: 7907 freework = WK_FREEWORK(wk); 7908 if (freework->fw_lbn <= -NDADDR) 7909 handle_workitem_indirblk(freework); 7910 else 7911 freework_freeblock(freework); 7912 continue; 7913 default: 7914 panic("handle_workitem_freeblocks: Unknown type %s", 7915 TYPENAME(wk->wk_type)); 7916 } 7917 } 7918 if (freeblks->fb_ref != 0) { 7919 freeblks->fb_state &= ~INPROGRESS; 7920 wake_worklist(&freeblks->fb_list); 7921 freeblks = NULL; 7922 } 7923 FREE_LOCK(ump); 7924 if (freeblks) 7925 return handle_complete_freeblocks(freeblks, flags); 7926 return (0); 7927 } 7928 7929 /* 7930 * Handle completion of block free via truncate. This allows fs_pending 7931 * to track the actual free block count more closely than if we only updated 7932 * it at the end. We must be careful to handle cases where the block count 7933 * on free was incorrect. 7934 */ 7935 static void 7936 freeblks_free(ump, freeblks, blocks) 7937 struct ufsmount *ump; 7938 struct freeblks *freeblks; 7939 int blocks; 7940 { 7941 struct fs *fs; 7942 ufs2_daddr_t remain; 7943 7944 UFS_LOCK(ump); 7945 remain = -freeblks->fb_chkcnt; 7946 freeblks->fb_chkcnt += blocks; 7947 if (remain > 0) { 7948 if (remain < blocks) 7949 blocks = remain; 7950 fs = ump->um_fs; 7951 fs->fs_pendingblocks -= blocks; 7952 } 7953 UFS_UNLOCK(ump); 7954 } 7955 7956 /* 7957 * Once all of the freework workitems are complete we can retire the 7958 * freeblocks dependency and any journal work awaiting completion. This 7959 * can not be called until all other dependencies are stable on disk. 7960 */ 7961 static int 7962 handle_complete_freeblocks(freeblks, flags) 7963 struct freeblks *freeblks; 7964 int flags; 7965 { 7966 struct inodedep *inodedep; 7967 struct inode *ip; 7968 struct vnode *vp; 7969 struct fs *fs; 7970 struct ufsmount *ump; 7971 ufs2_daddr_t spare; 7972 7973 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7974 fs = ump->um_fs; 7975 flags = LK_EXCLUSIVE | flags; 7976 spare = freeblks->fb_chkcnt; 7977 7978 /* 7979 * If we did not release the expected number of blocks we may have 7980 * to adjust the inode block count here. Only do so if it wasn't 7981 * a truncation to zero and the modrev still matches. 7982 */ 7983 if (spare && freeblks->fb_len != 0) { 7984 if (ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_inum, 7985 flags, &vp, FFSV_FORCEINSMQ) != 0) 7986 return (EBUSY); 7987 ip = VTOI(vp); 7988 if (DIP(ip, i_modrev) == freeblks->fb_modrev) { 7989 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare); 7990 ip->i_flag |= IN_CHANGE; 7991 /* 7992 * We must wait so this happens before the 7993 * journal is reclaimed. 7994 */ 7995 ffs_update(vp, 1); 7996 } 7997 vput(vp); 7998 } 7999 if (spare < 0) { 8000 UFS_LOCK(ump); 8001 fs->fs_pendingblocks += spare; 8002 UFS_UNLOCK(ump); 8003 } 8004 #ifdef QUOTA 8005 /* Handle spare. */ 8006 if (spare) 8007 quotaadj(freeblks->fb_quota, ump, -spare); 8008 quotarele(freeblks->fb_quota); 8009 #endif 8010 ACQUIRE_LOCK(ump); 8011 if (freeblks->fb_state & ONDEPLIST) { 8012 inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum, 8013 0, &inodedep); 8014 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next); 8015 freeblks->fb_state &= ~ONDEPLIST; 8016 if (TAILQ_EMPTY(&inodedep->id_freeblklst)) 8017 free_inodedep(inodedep); 8018 } 8019 /* 8020 * All of the freeblock deps must be complete prior to this call 8021 * so it's now safe to complete earlier outstanding journal entries. 8022 */ 8023 handle_jwork(&freeblks->fb_jwork); 8024 WORKITEM_FREE(freeblks, D_FREEBLKS); 8025 FREE_LOCK(ump); 8026 return (0); 8027 } 8028 8029 /* 8030 * Release blocks associated with the freeblks and stored in the indirect 8031 * block dbn. If level is greater than SINGLE, the block is an indirect block 8032 * and recursive calls to indirtrunc must be used to cleanse other indirect 8033 * blocks. 8034 * 8035 * This handles partial and complete truncation of blocks. Partial is noted 8036 * with goingaway == 0. In this case the freework is completed after the 8037 * zero'd indirects are written to disk. For full truncation the freework 8038 * is completed after the block is freed. 8039 */ 8040 static void 8041 indir_trunc(freework, dbn, lbn) 8042 struct freework *freework; 8043 ufs2_daddr_t dbn; 8044 ufs_lbn_t lbn; 8045 { 8046 struct freework *nfreework; 8047 struct workhead wkhd; 8048 struct freeblks *freeblks; 8049 struct buf *bp; 8050 struct fs *fs; 8051 struct indirdep *indirdep; 8052 struct ufsmount *ump; 8053 ufs1_daddr_t *bap1; 8054 ufs2_daddr_t nb, nnb, *bap2; 8055 ufs_lbn_t lbnadd, nlbn; 8056 int i, nblocks, ufs1fmt; 8057 int freedblocks; 8058 int goingaway; 8059 int freedeps; 8060 int needj; 8061 int level; 8062 int cnt; 8063 8064 freeblks = freework->fw_freeblks; 8065 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8066 fs = ump->um_fs; 8067 /* 8068 * Get buffer of block pointers to be freed. There are three cases: 8069 * 8070 * 1) Partial truncate caches the indirdep pointer in the freework 8071 * which provides us a back copy to the save bp which holds the 8072 * pointers we want to clear. When this completes the zero 8073 * pointers are written to the real copy. 8074 * 2) The indirect is being completely truncated, cancel_indirdep() 8075 * eliminated the real copy and placed the indirdep on the saved 8076 * copy. The indirdep and buf are discarded when this completes. 8077 * 3) The indirect was not in memory, we read a copy off of the disk 8078 * using the devvp and drop and invalidate the buffer when we're 8079 * done. 8080 */ 8081 goingaway = 1; 8082 indirdep = NULL; 8083 if (freework->fw_indir != NULL) { 8084 goingaway = 0; 8085 indirdep = freework->fw_indir; 8086 bp = indirdep->ir_savebp; 8087 if (bp == NULL || bp->b_blkno != dbn) 8088 panic("indir_trunc: Bad saved buf %p blkno %jd", 8089 bp, (intmax_t)dbn); 8090 } else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) { 8091 /* 8092 * The lock prevents the buf dep list from changing and 8093 * indirects on devvp should only ever have one dependency. 8094 */ 8095 indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep)); 8096 if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0) 8097 panic("indir_trunc: Bad indirdep %p from buf %p", 8098 indirdep, bp); 8099 } else if (bread(freeblks->fb_devvp, dbn, (int)fs->fs_bsize, 8100 NOCRED, &bp) != 0) { 8101 brelse(bp); 8102 return; 8103 } 8104 ACQUIRE_LOCK(ump); 8105 /* Protects against a race with complete_trunc_indir(). */ 8106 freework->fw_state &= ~INPROGRESS; 8107 /* 8108 * If we have an indirdep we need to enforce the truncation order 8109 * and discard it when it is complete. 8110 */ 8111 if (indirdep) { 8112 if (freework != TAILQ_FIRST(&indirdep->ir_trunc) && 8113 !TAILQ_EMPTY(&indirdep->ir_trunc)) { 8114 /* 8115 * Add the complete truncate to the list on the 8116 * indirdep to enforce in-order processing. 8117 */ 8118 if (freework->fw_indir == NULL) 8119 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, 8120 freework, fw_next); 8121 FREE_LOCK(ump); 8122 return; 8123 } 8124 /* 8125 * If we're goingaway, free the indirdep. Otherwise it will 8126 * linger until the write completes. 8127 */ 8128 if (goingaway) 8129 free_indirdep(indirdep); 8130 } 8131 FREE_LOCK(ump); 8132 /* Initialize pointers depending on block size. */ 8133 if (ump->um_fstype == UFS1) { 8134 bap1 = (ufs1_daddr_t *)bp->b_data; 8135 nb = bap1[freework->fw_off]; 8136 ufs1fmt = 1; 8137 bap2 = NULL; 8138 } else { 8139 bap2 = (ufs2_daddr_t *)bp->b_data; 8140 nb = bap2[freework->fw_off]; 8141 ufs1fmt = 0; 8142 bap1 = NULL; 8143 } 8144 level = lbn_level(lbn); 8145 needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0; 8146 lbnadd = lbn_offset(fs, level); 8147 nblocks = btodb(fs->fs_bsize); 8148 nfreework = freework; 8149 freedeps = 0; 8150 cnt = 0; 8151 /* 8152 * Reclaim blocks. Traverses into nested indirect levels and 8153 * arranges for the current level to be freed when subordinates 8154 * are free when journaling. 8155 */ 8156 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) { 8157 if (i != NINDIR(fs) - 1) { 8158 if (ufs1fmt) 8159 nnb = bap1[i+1]; 8160 else 8161 nnb = bap2[i+1]; 8162 } else 8163 nnb = 0; 8164 if (nb == 0) 8165 continue; 8166 cnt++; 8167 if (level != 0) { 8168 nlbn = (lbn + 1) - (i * lbnadd); 8169 if (needj != 0) { 8170 nfreework = newfreework(ump, freeblks, freework, 8171 nlbn, nb, fs->fs_frag, 0, 0); 8172 freedeps++; 8173 } 8174 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn); 8175 } else { 8176 struct freedep *freedep; 8177 8178 /* 8179 * Attempt to aggregate freedep dependencies for 8180 * all blocks being released to the same CG. 8181 */ 8182 LIST_INIT(&wkhd); 8183 if (needj != 0 && 8184 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) { 8185 freedep = newfreedep(freework); 8186 WORKLIST_INSERT_UNLOCKED(&wkhd, 8187 &freedep->fd_list); 8188 freedeps++; 8189 } 8190 CTR3(KTR_SUJ, 8191 "indir_trunc: ino %d blkno %jd size %ld", 8192 freeblks->fb_inum, nb, fs->fs_bsize); 8193 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb, 8194 fs->fs_bsize, freeblks->fb_inum, 8195 freeblks->fb_vtype, &wkhd); 8196 } 8197 } 8198 if (goingaway) { 8199 bp->b_flags |= B_INVAL | B_NOCACHE; 8200 brelse(bp); 8201 } 8202 freedblocks = 0; 8203 if (level == 0) 8204 freedblocks = (nblocks * cnt); 8205 if (needj == 0) 8206 freedblocks += nblocks; 8207 freeblks_free(ump, freeblks, freedblocks); 8208 /* 8209 * If we are journaling set up the ref counts and offset so this 8210 * indirect can be completed when its children are free. 8211 */ 8212 if (needj) { 8213 ACQUIRE_LOCK(ump); 8214 freework->fw_off = i; 8215 freework->fw_ref += freedeps; 8216 freework->fw_ref -= NINDIR(fs) + 1; 8217 if (level == 0) 8218 freeblks->fb_cgwait += freedeps; 8219 if (freework->fw_ref == 0) 8220 freework_freeblock(freework); 8221 FREE_LOCK(ump); 8222 return; 8223 } 8224 /* 8225 * If we're not journaling we can free the indirect now. 8226 */ 8227 dbn = dbtofsb(fs, dbn); 8228 CTR3(KTR_SUJ, 8229 "indir_trunc 2: ino %d blkno %jd size %ld", 8230 freeblks->fb_inum, dbn, fs->fs_bsize); 8231 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize, 8232 freeblks->fb_inum, freeblks->fb_vtype, NULL); 8233 /* Non SUJ softdep does single-threaded truncations. */ 8234 if (freework->fw_blkno == dbn) { 8235 freework->fw_state |= ALLCOMPLETE; 8236 ACQUIRE_LOCK(ump); 8237 handle_written_freework(freework); 8238 FREE_LOCK(ump); 8239 } 8240 return; 8241 } 8242 8243 /* 8244 * Cancel an allocindir when it is removed via truncation. When bp is not 8245 * NULL the indirect never appeared on disk and is scheduled to be freed 8246 * independently of the indir so we can more easily track journal work. 8247 */ 8248 static void 8249 cancel_allocindir(aip, bp, freeblks, trunc) 8250 struct allocindir *aip; 8251 struct buf *bp; 8252 struct freeblks *freeblks; 8253 int trunc; 8254 { 8255 struct indirdep *indirdep; 8256 struct freefrag *freefrag; 8257 struct newblk *newblk; 8258 8259 newblk = (struct newblk *)aip; 8260 LIST_REMOVE(aip, ai_next); 8261 /* 8262 * We must eliminate the pointer in bp if it must be freed on its 8263 * own due to partial truncate or pending journal work. 8264 */ 8265 if (bp && (trunc || newblk->nb_jnewblk)) { 8266 /* 8267 * Clear the pointer and mark the aip to be freed 8268 * directly if it never existed on disk. 8269 */ 8270 aip->ai_state |= DELAYEDFREE; 8271 indirdep = aip->ai_indirdep; 8272 if (indirdep->ir_state & UFS1FMT) 8273 ((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8274 else 8275 ((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8276 } 8277 /* 8278 * When truncating the previous pointer will be freed via 8279 * savedbp. Eliminate the freefrag which would dup free. 8280 */ 8281 if (trunc && (freefrag = newblk->nb_freefrag) != NULL) { 8282 newblk->nb_freefrag = NULL; 8283 if (freefrag->ff_jdep) 8284 cancel_jfreefrag( 8285 WK_JFREEFRAG(freefrag->ff_jdep)); 8286 jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork); 8287 WORKITEM_FREE(freefrag, D_FREEFRAG); 8288 } 8289 /* 8290 * If the journal hasn't been written the jnewblk must be passed 8291 * to the call to ffs_blkfree that reclaims the space. We accomplish 8292 * this by leaving the journal dependency on the newblk to be freed 8293 * when a freework is created in handle_workitem_freeblocks(). 8294 */ 8295 cancel_newblk(newblk, NULL, &freeblks->fb_jwork); 8296 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 8297 } 8298 8299 /* 8300 * Create the mkdir dependencies for . and .. in a new directory. Link them 8301 * in to a newdirblk so any subsequent additions are tracked properly. The 8302 * caller is responsible for adding the mkdir1 dependency to the journal 8303 * and updating id_mkdiradd. This function returns with the per-filesystem 8304 * lock held. 8305 */ 8306 static struct mkdir * 8307 setup_newdir(dap, newinum, dinum, newdirbp, mkdirp) 8308 struct diradd *dap; 8309 ino_t newinum; 8310 ino_t dinum; 8311 struct buf *newdirbp; 8312 struct mkdir **mkdirp; 8313 { 8314 struct newblk *newblk; 8315 struct pagedep *pagedep; 8316 struct inodedep *inodedep; 8317 struct newdirblk *newdirblk; 8318 struct mkdir *mkdir1, *mkdir2; 8319 struct worklist *wk; 8320 struct jaddref *jaddref; 8321 struct ufsmount *ump; 8322 struct mount *mp; 8323 8324 mp = dap->da_list.wk_mp; 8325 ump = VFSTOUFS(mp); 8326 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK, 8327 M_SOFTDEP_FLAGS); 8328 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8329 LIST_INIT(&newdirblk->db_mkdir); 8330 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8331 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp); 8332 mkdir1->md_state = ATTACHED | MKDIR_BODY; 8333 mkdir1->md_diradd = dap; 8334 mkdir1->md_jaddref = NULL; 8335 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8336 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp); 8337 mkdir2->md_state = ATTACHED | MKDIR_PARENT; 8338 mkdir2->md_diradd = dap; 8339 mkdir2->md_jaddref = NULL; 8340 if (MOUNTEDSUJ(mp) == 0) { 8341 mkdir1->md_state |= DEPCOMPLETE; 8342 mkdir2->md_state |= DEPCOMPLETE; 8343 } 8344 /* 8345 * Dependency on "." and ".." being written to disk. 8346 */ 8347 mkdir1->md_buf = newdirbp; 8348 ACQUIRE_LOCK(VFSTOUFS(mp)); 8349 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir1, md_mkdirs); 8350 /* 8351 * We must link the pagedep, allocdirect, and newdirblk for 8352 * the initial file page so the pointer to the new directory 8353 * is not written until the directory contents are live and 8354 * any subsequent additions are not marked live until the 8355 * block is reachable via the inode. 8356 */ 8357 if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0) 8358 panic("setup_newdir: lost pagedep"); 8359 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list) 8360 if (wk->wk_type == D_ALLOCDIRECT) 8361 break; 8362 if (wk == NULL) 8363 panic("setup_newdir: lost allocdirect"); 8364 if (pagedep->pd_state & NEWBLOCK) 8365 panic("setup_newdir: NEWBLOCK already set"); 8366 newblk = WK_NEWBLK(wk); 8367 pagedep->pd_state |= NEWBLOCK; 8368 pagedep->pd_newdirblk = newdirblk; 8369 newdirblk->db_pagedep = pagedep; 8370 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8371 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list); 8372 /* 8373 * Look up the inodedep for the parent directory so that we 8374 * can link mkdir2 into the pending dotdot jaddref or 8375 * the inode write if there is none. If the inode is 8376 * ALLCOMPLETE and no jaddref is present all dependencies have 8377 * been satisfied and mkdir2 can be freed. 8378 */ 8379 inodedep_lookup(mp, dinum, 0, &inodedep); 8380 if (MOUNTEDSUJ(mp)) { 8381 if (inodedep == NULL) 8382 panic("setup_newdir: Lost parent."); 8383 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8384 inoreflst); 8385 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum && 8386 (jaddref->ja_state & MKDIR_PARENT), 8387 ("setup_newdir: bad dotdot jaddref %p", jaddref)); 8388 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8389 mkdir2->md_jaddref = jaddref; 8390 jaddref->ja_mkdir = mkdir2; 8391 } else if (inodedep == NULL || 8392 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 8393 dap->da_state &= ~MKDIR_PARENT; 8394 WORKITEM_FREE(mkdir2, D_MKDIR); 8395 mkdir2 = NULL; 8396 } else { 8397 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8398 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list); 8399 } 8400 *mkdirp = mkdir2; 8401 8402 return (mkdir1); 8403 } 8404 8405 /* 8406 * Directory entry addition dependencies. 8407 * 8408 * When adding a new directory entry, the inode (with its incremented link 8409 * count) must be written to disk before the directory entry's pointer to it. 8410 * Also, if the inode is newly allocated, the corresponding freemap must be 8411 * updated (on disk) before the directory entry's pointer. These requirements 8412 * are met via undo/redo on the directory entry's pointer, which consists 8413 * simply of the inode number. 8414 * 8415 * As directory entries are added and deleted, the free space within a 8416 * directory block can become fragmented. The ufs filesystem will compact 8417 * a fragmented directory block to make space for a new entry. When this 8418 * occurs, the offsets of previously added entries change. Any "diradd" 8419 * dependency structures corresponding to these entries must be updated with 8420 * the new offsets. 8421 */ 8422 8423 /* 8424 * This routine is called after the in-memory inode's link 8425 * count has been incremented, but before the directory entry's 8426 * pointer to the inode has been set. 8427 */ 8428 int 8429 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 8430 struct buf *bp; /* buffer containing directory block */ 8431 struct inode *dp; /* inode for directory */ 8432 off_t diroffset; /* offset of new entry in directory */ 8433 ino_t newinum; /* inode referenced by new directory entry */ 8434 struct buf *newdirbp; /* non-NULL => contents of new mkdir */ 8435 int isnewblk; /* entry is in a newly allocated block */ 8436 { 8437 int offset; /* offset of new entry within directory block */ 8438 ufs_lbn_t lbn; /* block in directory containing new entry */ 8439 struct fs *fs; 8440 struct diradd *dap; 8441 struct newblk *newblk; 8442 struct pagedep *pagedep; 8443 struct inodedep *inodedep; 8444 struct newdirblk *newdirblk; 8445 struct mkdir *mkdir1, *mkdir2; 8446 struct jaddref *jaddref; 8447 struct ufsmount *ump; 8448 struct mount *mp; 8449 int isindir; 8450 8451 ump = dp->i_ump; 8452 mp = UFSTOVFS(ump); 8453 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 8454 ("softdep_setup_directory_add called on non-softdep filesystem")); 8455 /* 8456 * Whiteouts have no dependencies. 8457 */ 8458 if (newinum == WINO) { 8459 if (newdirbp != NULL) 8460 bdwrite(newdirbp); 8461 return (0); 8462 } 8463 jaddref = NULL; 8464 mkdir1 = mkdir2 = NULL; 8465 fs = dp->i_fs; 8466 lbn = lblkno(fs, diroffset); 8467 offset = blkoff(fs, diroffset); 8468 dap = malloc(sizeof(struct diradd), M_DIRADD, 8469 M_SOFTDEP_FLAGS|M_ZERO); 8470 workitem_alloc(&dap->da_list, D_DIRADD, mp); 8471 dap->da_offset = offset; 8472 dap->da_newinum = newinum; 8473 dap->da_state = ATTACHED; 8474 LIST_INIT(&dap->da_jwork); 8475 isindir = bp->b_lblkno >= NDADDR; 8476 newdirblk = NULL; 8477 if (isnewblk && 8478 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) { 8479 newdirblk = malloc(sizeof(struct newdirblk), 8480 M_NEWDIRBLK, M_SOFTDEP_FLAGS); 8481 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8482 LIST_INIT(&newdirblk->db_mkdir); 8483 } 8484 /* 8485 * If we're creating a new directory setup the dependencies and set 8486 * the dap state to wait for them. Otherwise it's COMPLETE and 8487 * we can move on. 8488 */ 8489 if (newdirbp == NULL) { 8490 dap->da_state |= DEPCOMPLETE; 8491 ACQUIRE_LOCK(ump); 8492 } else { 8493 dap->da_state |= MKDIR_BODY | MKDIR_PARENT; 8494 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp, 8495 &mkdir2); 8496 } 8497 /* 8498 * Link into parent directory pagedep to await its being written. 8499 */ 8500 pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep); 8501 #ifdef DEBUG 8502 if (diradd_lookup(pagedep, offset) != NULL) 8503 panic("softdep_setup_directory_add: %p already at off %d\n", 8504 diradd_lookup(pagedep, offset), offset); 8505 #endif 8506 dap->da_pagedep = pagedep; 8507 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap, 8508 da_pdlist); 8509 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 8510 /* 8511 * If we're journaling, link the diradd into the jaddref so it 8512 * may be completed after the journal entry is written. Otherwise, 8513 * link the diradd into its inodedep. If the inode is not yet 8514 * written place it on the bufwait list, otherwise do the post-inode 8515 * write processing to put it on the id_pendinghd list. 8516 */ 8517 if (MOUNTEDSUJ(mp)) { 8518 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8519 inoreflst); 8520 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 8521 ("softdep_setup_directory_add: bad jaddref %p", jaddref)); 8522 jaddref->ja_diroff = diroffset; 8523 jaddref->ja_diradd = dap; 8524 add_to_journal(&jaddref->ja_list); 8525 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) 8526 diradd_inode_written(dap, inodedep); 8527 else 8528 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 8529 /* 8530 * Add the journal entries for . and .. links now that the primary 8531 * link is written. 8532 */ 8533 if (mkdir1 != NULL && MOUNTEDSUJ(mp)) { 8534 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 8535 inoreflst, if_deps); 8536 KASSERT(jaddref != NULL && 8537 jaddref->ja_ino == jaddref->ja_parent && 8538 (jaddref->ja_state & MKDIR_BODY), 8539 ("softdep_setup_directory_add: bad dot jaddref %p", 8540 jaddref)); 8541 mkdir1->md_jaddref = jaddref; 8542 jaddref->ja_mkdir = mkdir1; 8543 /* 8544 * It is important that the dotdot journal entry 8545 * is added prior to the dot entry since dot writes 8546 * both the dot and dotdot links. These both must 8547 * be added after the primary link for the journal 8548 * to remain consistent. 8549 */ 8550 add_to_journal(&mkdir2->md_jaddref->ja_list); 8551 add_to_journal(&jaddref->ja_list); 8552 } 8553 /* 8554 * If we are adding a new directory remember this diradd so that if 8555 * we rename it we can keep the dot and dotdot dependencies. If 8556 * we are adding a new name for an inode that has a mkdiradd we 8557 * must be in rename and we have to move the dot and dotdot 8558 * dependencies to this new name. The old name is being orphaned 8559 * soon. 8560 */ 8561 if (mkdir1 != NULL) { 8562 if (inodedep->id_mkdiradd != NULL) 8563 panic("softdep_setup_directory_add: Existing mkdir"); 8564 inodedep->id_mkdiradd = dap; 8565 } else if (inodedep->id_mkdiradd) 8566 merge_diradd(inodedep, dap); 8567 if (newdirblk != NULL) { 8568 /* 8569 * There is nothing to do if we are already tracking 8570 * this block. 8571 */ 8572 if ((pagedep->pd_state & NEWBLOCK) != 0) { 8573 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 8574 FREE_LOCK(ump); 8575 return (0); 8576 } 8577 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk) 8578 == 0) 8579 panic("softdep_setup_directory_add: lost entry"); 8580 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8581 pagedep->pd_state |= NEWBLOCK; 8582 pagedep->pd_newdirblk = newdirblk; 8583 newdirblk->db_pagedep = pagedep; 8584 FREE_LOCK(ump); 8585 /* 8586 * If we extended into an indirect signal direnter to sync. 8587 */ 8588 if (isindir) 8589 return (1); 8590 return (0); 8591 } 8592 FREE_LOCK(ump); 8593 return (0); 8594 } 8595 8596 /* 8597 * This procedure is called to change the offset of a directory 8598 * entry when compacting a directory block which must be owned 8599 * exclusively by the caller. Note that the actual entry movement 8600 * must be done in this procedure to ensure that no I/O completions 8601 * occur while the move is in progress. 8602 */ 8603 void 8604 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 8605 struct buf *bp; /* Buffer holding directory block. */ 8606 struct inode *dp; /* inode for directory */ 8607 caddr_t base; /* address of dp->i_offset */ 8608 caddr_t oldloc; /* address of old directory location */ 8609 caddr_t newloc; /* address of new directory location */ 8610 int entrysize; /* size of directory entry */ 8611 { 8612 int offset, oldoffset, newoffset; 8613 struct pagedep *pagedep; 8614 struct jmvref *jmvref; 8615 struct diradd *dap; 8616 struct direct *de; 8617 struct mount *mp; 8618 ufs_lbn_t lbn; 8619 int flags; 8620 8621 mp = UFSTOVFS(dp->i_ump); 8622 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 8623 ("softdep_change_directoryentry_offset called on " 8624 "non-softdep filesystem")); 8625 de = (struct direct *)oldloc; 8626 jmvref = NULL; 8627 flags = 0; 8628 /* 8629 * Moves are always journaled as it would be too complex to 8630 * determine if any affected adds or removes are present in the 8631 * journal. 8632 */ 8633 if (MOUNTEDSUJ(mp)) { 8634 flags = DEPALLOC; 8635 jmvref = newjmvref(dp, de->d_ino, 8636 dp->i_offset + (oldloc - base), 8637 dp->i_offset + (newloc - base)); 8638 } 8639 lbn = lblkno(dp->i_fs, dp->i_offset); 8640 offset = blkoff(dp->i_fs, dp->i_offset); 8641 oldoffset = offset + (oldloc - base); 8642 newoffset = offset + (newloc - base); 8643 ACQUIRE_LOCK(dp->i_ump); 8644 if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0) 8645 goto done; 8646 dap = diradd_lookup(pagedep, oldoffset); 8647 if (dap) { 8648 dap->da_offset = newoffset; 8649 newoffset = DIRADDHASH(newoffset); 8650 oldoffset = DIRADDHASH(oldoffset); 8651 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE && 8652 newoffset != oldoffset) { 8653 LIST_REMOVE(dap, da_pdlist); 8654 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset], 8655 dap, da_pdlist); 8656 } 8657 } 8658 done: 8659 if (jmvref) { 8660 jmvref->jm_pagedep = pagedep; 8661 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps); 8662 add_to_journal(&jmvref->jm_list); 8663 } 8664 bcopy(oldloc, newloc, entrysize); 8665 FREE_LOCK(dp->i_ump); 8666 } 8667 8668 /* 8669 * Move the mkdir dependencies and journal work from one diradd to another 8670 * when renaming a directory. The new name must depend on the mkdir deps 8671 * completing as the old name did. Directories can only have one valid link 8672 * at a time so one must be canonical. 8673 */ 8674 static void 8675 merge_diradd(inodedep, newdap) 8676 struct inodedep *inodedep; 8677 struct diradd *newdap; 8678 { 8679 struct diradd *olddap; 8680 struct mkdir *mkdir, *nextmd; 8681 struct ufsmount *ump; 8682 short state; 8683 8684 olddap = inodedep->id_mkdiradd; 8685 inodedep->id_mkdiradd = newdap; 8686 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8687 newdap->da_state &= ~DEPCOMPLETE; 8688 ump = VFSTOUFS(inodedep->id_list.wk_mp); 8689 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 8690 mkdir = nextmd) { 8691 nextmd = LIST_NEXT(mkdir, md_mkdirs); 8692 if (mkdir->md_diradd != olddap) 8693 continue; 8694 mkdir->md_diradd = newdap; 8695 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY); 8696 newdap->da_state |= state; 8697 olddap->da_state &= ~state; 8698 if ((olddap->da_state & 8699 (MKDIR_PARENT | MKDIR_BODY)) == 0) 8700 break; 8701 } 8702 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 8703 panic("merge_diradd: unfound ref"); 8704 } 8705 /* 8706 * Any mkdir related journal items are not safe to be freed until 8707 * the new name is stable. 8708 */ 8709 jwork_move(&newdap->da_jwork, &olddap->da_jwork); 8710 olddap->da_state |= DEPCOMPLETE; 8711 complete_diradd(olddap); 8712 } 8713 8714 /* 8715 * Move the diradd to the pending list when all diradd dependencies are 8716 * complete. 8717 */ 8718 static void 8719 complete_diradd(dap) 8720 struct diradd *dap; 8721 { 8722 struct pagedep *pagedep; 8723 8724 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 8725 if (dap->da_state & DIRCHG) 8726 pagedep = dap->da_previous->dm_pagedep; 8727 else 8728 pagedep = dap->da_pagedep; 8729 LIST_REMOVE(dap, da_pdlist); 8730 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 8731 } 8732 } 8733 8734 /* 8735 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal 8736 * add entries and conditonally journal the remove. 8737 */ 8738 static void 8739 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref) 8740 struct diradd *dap; 8741 struct dirrem *dirrem; 8742 struct jremref *jremref; 8743 struct jremref *dotremref; 8744 struct jremref *dotdotremref; 8745 { 8746 struct inodedep *inodedep; 8747 struct jaddref *jaddref; 8748 struct inoref *inoref; 8749 struct ufsmount *ump; 8750 struct mkdir *mkdir; 8751 8752 /* 8753 * If no remove references were allocated we're on a non-journaled 8754 * filesystem and can skip the cancel step. 8755 */ 8756 if (jremref == NULL) { 8757 free_diradd(dap, NULL); 8758 return; 8759 } 8760 /* 8761 * Cancel the primary name an free it if it does not require 8762 * journaling. 8763 */ 8764 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum, 8765 0, &inodedep) != 0) { 8766 /* Abort the addref that reference this diradd. */ 8767 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 8768 if (inoref->if_list.wk_type != D_JADDREF) 8769 continue; 8770 jaddref = (struct jaddref *)inoref; 8771 if (jaddref->ja_diradd != dap) 8772 continue; 8773 if (cancel_jaddref(jaddref, inodedep, 8774 &dirrem->dm_jwork) == 0) { 8775 free_jremref(jremref); 8776 jremref = NULL; 8777 } 8778 break; 8779 } 8780 } 8781 /* 8782 * Cancel subordinate names and free them if they do not require 8783 * journaling. 8784 */ 8785 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8786 ump = VFSTOUFS(dap->da_list.wk_mp); 8787 LIST_FOREACH(mkdir, &ump->softdep_mkdirlisthd, md_mkdirs) { 8788 if (mkdir->md_diradd != dap) 8789 continue; 8790 if ((jaddref = mkdir->md_jaddref) == NULL) 8791 continue; 8792 mkdir->md_jaddref = NULL; 8793 if (mkdir->md_state & MKDIR_PARENT) { 8794 if (cancel_jaddref(jaddref, NULL, 8795 &dirrem->dm_jwork) == 0) { 8796 free_jremref(dotdotremref); 8797 dotdotremref = NULL; 8798 } 8799 } else { 8800 if (cancel_jaddref(jaddref, inodedep, 8801 &dirrem->dm_jwork) == 0) { 8802 free_jremref(dotremref); 8803 dotremref = NULL; 8804 } 8805 } 8806 } 8807 } 8808 8809 if (jremref) 8810 journal_jremref(dirrem, jremref, inodedep); 8811 if (dotremref) 8812 journal_jremref(dirrem, dotremref, inodedep); 8813 if (dotdotremref) 8814 journal_jremref(dirrem, dotdotremref, NULL); 8815 jwork_move(&dirrem->dm_jwork, &dap->da_jwork); 8816 free_diradd(dap, &dirrem->dm_jwork); 8817 } 8818 8819 /* 8820 * Free a diradd dependency structure. This routine must be called 8821 * with splbio interrupts blocked. 8822 */ 8823 static void 8824 free_diradd(dap, wkhd) 8825 struct diradd *dap; 8826 struct workhead *wkhd; 8827 { 8828 struct dirrem *dirrem; 8829 struct pagedep *pagedep; 8830 struct inodedep *inodedep; 8831 struct mkdir *mkdir, *nextmd; 8832 struct ufsmount *ump; 8833 8834 ump = VFSTOUFS(dap->da_list.wk_mp); 8835 LOCK_OWNED(ump); 8836 LIST_REMOVE(dap, da_pdlist); 8837 if (dap->da_state & ONWORKLIST) 8838 WORKLIST_REMOVE(&dap->da_list); 8839 if ((dap->da_state & DIRCHG) == 0) { 8840 pagedep = dap->da_pagedep; 8841 } else { 8842 dirrem = dap->da_previous; 8843 pagedep = dirrem->dm_pagedep; 8844 dirrem->dm_dirinum = pagedep->pd_ino; 8845 dirrem->dm_state |= COMPLETE; 8846 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 8847 add_to_worklist(&dirrem->dm_list, 0); 8848 } 8849 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum, 8850 0, &inodedep) != 0) 8851 if (inodedep->id_mkdiradd == dap) 8852 inodedep->id_mkdiradd = NULL; 8853 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8854 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 8855 mkdir = nextmd) { 8856 nextmd = LIST_NEXT(mkdir, md_mkdirs); 8857 if (mkdir->md_diradd != dap) 8858 continue; 8859 dap->da_state &= 8860 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 8861 LIST_REMOVE(mkdir, md_mkdirs); 8862 if (mkdir->md_state & ONWORKLIST) 8863 WORKLIST_REMOVE(&mkdir->md_list); 8864 if (mkdir->md_jaddref != NULL) 8865 panic("free_diradd: Unexpected jaddref"); 8866 WORKITEM_FREE(mkdir, D_MKDIR); 8867 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) 8868 break; 8869 } 8870 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 8871 panic("free_diradd: unfound ref"); 8872 } 8873 if (inodedep) 8874 free_inodedep(inodedep); 8875 /* 8876 * Free any journal segments waiting for the directory write. 8877 */ 8878 handle_jwork(&dap->da_jwork); 8879 WORKITEM_FREE(dap, D_DIRADD); 8880 } 8881 8882 /* 8883 * Directory entry removal dependencies. 8884 * 8885 * When removing a directory entry, the entry's inode pointer must be 8886 * zero'ed on disk before the corresponding inode's link count is decremented 8887 * (possibly freeing the inode for re-use). This dependency is handled by 8888 * updating the directory entry but delaying the inode count reduction until 8889 * after the directory block has been written to disk. After this point, the 8890 * inode count can be decremented whenever it is convenient. 8891 */ 8892 8893 /* 8894 * This routine should be called immediately after removing 8895 * a directory entry. The inode's link count should not be 8896 * decremented by the calling procedure -- the soft updates 8897 * code will do this task when it is safe. 8898 */ 8899 void 8900 softdep_setup_remove(bp, dp, ip, isrmdir) 8901 struct buf *bp; /* buffer containing directory block */ 8902 struct inode *dp; /* inode for the directory being modified */ 8903 struct inode *ip; /* inode for directory entry being removed */ 8904 int isrmdir; /* indicates if doing RMDIR */ 8905 { 8906 struct dirrem *dirrem, *prevdirrem; 8907 struct inodedep *inodedep; 8908 int direct; 8909 8910 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0, 8911 ("softdep_setup_remove called on non-softdep filesystem")); 8912 /* 8913 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want 8914 * newdirrem() to setup the full directory remove which requires 8915 * isrmdir > 1. 8916 */ 8917 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 8918 /* 8919 * Add the dirrem to the inodedep's pending remove list for quick 8920 * discovery later. 8921 */ 8922 if (inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0, 8923 &inodedep) == 0) 8924 panic("softdep_setup_remove: Lost inodedep."); 8925 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 8926 dirrem->dm_state |= ONDEPLIST; 8927 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 8928 8929 /* 8930 * If the COMPLETE flag is clear, then there were no active 8931 * entries and we want to roll back to a zeroed entry until 8932 * the new inode is committed to disk. If the COMPLETE flag is 8933 * set then we have deleted an entry that never made it to 8934 * disk. If the entry we deleted resulted from a name change, 8935 * then the old name still resides on disk. We cannot delete 8936 * its inode (returned to us in prevdirrem) until the zeroed 8937 * directory entry gets to disk. The new inode has never been 8938 * referenced on the disk, so can be deleted immediately. 8939 */ 8940 if ((dirrem->dm_state & COMPLETE) == 0) { 8941 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem, 8942 dm_next); 8943 FREE_LOCK(ip->i_ump); 8944 } else { 8945 if (prevdirrem != NULL) 8946 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, 8947 prevdirrem, dm_next); 8948 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino; 8949 direct = LIST_EMPTY(&dirrem->dm_jremrefhd); 8950 FREE_LOCK(ip->i_ump); 8951 if (direct) 8952 handle_workitem_remove(dirrem, 0); 8953 } 8954 } 8955 8956 /* 8957 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the 8958 * pd_pendinghd list of a pagedep. 8959 */ 8960 static struct diradd * 8961 diradd_lookup(pagedep, offset) 8962 struct pagedep *pagedep; 8963 int offset; 8964 { 8965 struct diradd *dap; 8966 8967 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist) 8968 if (dap->da_offset == offset) 8969 return (dap); 8970 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 8971 if (dap->da_offset == offset) 8972 return (dap); 8973 return (NULL); 8974 } 8975 8976 /* 8977 * Search for a .. diradd dependency in a directory that is being removed. 8978 * If the directory was renamed to a new parent we have a diradd rather 8979 * than a mkdir for the .. entry. We need to cancel it now before 8980 * it is found in truncate(). 8981 */ 8982 static struct jremref * 8983 cancel_diradd_dotdot(ip, dirrem, jremref) 8984 struct inode *ip; 8985 struct dirrem *dirrem; 8986 struct jremref *jremref; 8987 { 8988 struct pagedep *pagedep; 8989 struct diradd *dap; 8990 struct worklist *wk; 8991 8992 if (pagedep_lookup(UFSTOVFS(ip->i_ump), NULL, ip->i_number, 0, 0, 8993 &pagedep) == 0) 8994 return (jremref); 8995 dap = diradd_lookup(pagedep, DOTDOT_OFFSET); 8996 if (dap == NULL) 8997 return (jremref); 8998 cancel_diradd(dap, dirrem, jremref, NULL, NULL); 8999 /* 9000 * Mark any journal work as belonging to the parent so it is freed 9001 * with the .. reference. 9002 */ 9003 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9004 wk->wk_state |= MKDIR_PARENT; 9005 return (NULL); 9006 } 9007 9008 /* 9009 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to 9010 * replace it with a dirrem/diradd pair as a result of re-parenting a 9011 * directory. This ensures that we don't simultaneously have a mkdir and 9012 * a diradd for the same .. entry. 9013 */ 9014 static struct jremref * 9015 cancel_mkdir_dotdot(ip, dirrem, jremref) 9016 struct inode *ip; 9017 struct dirrem *dirrem; 9018 struct jremref *jremref; 9019 { 9020 struct inodedep *inodedep; 9021 struct jaddref *jaddref; 9022 struct ufsmount *ump; 9023 struct mkdir *mkdir; 9024 struct diradd *dap; 9025 9026 if (inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0, 9027 &inodedep) == 0) 9028 return (jremref); 9029 dap = inodedep->id_mkdiradd; 9030 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0) 9031 return (jremref); 9032 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9033 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 9034 mkdir = LIST_NEXT(mkdir, md_mkdirs)) 9035 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT) 9036 break; 9037 if (mkdir == NULL) 9038 panic("cancel_mkdir_dotdot: Unable to find mkdir\n"); 9039 if ((jaddref = mkdir->md_jaddref) != NULL) { 9040 mkdir->md_jaddref = NULL; 9041 jaddref->ja_state &= ~MKDIR_PARENT; 9042 if (inodedep_lookup(UFSTOVFS(ip->i_ump), jaddref->ja_ino, 0, 9043 &inodedep) == 0) 9044 panic("cancel_mkdir_dotdot: Lost parent inodedep"); 9045 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) { 9046 journal_jremref(dirrem, jremref, inodedep); 9047 jremref = NULL; 9048 } 9049 } 9050 if (mkdir->md_state & ONWORKLIST) 9051 WORKLIST_REMOVE(&mkdir->md_list); 9052 mkdir->md_state |= ALLCOMPLETE; 9053 complete_mkdir(mkdir); 9054 return (jremref); 9055 } 9056 9057 static void 9058 journal_jremref(dirrem, jremref, inodedep) 9059 struct dirrem *dirrem; 9060 struct jremref *jremref; 9061 struct inodedep *inodedep; 9062 { 9063 9064 if (inodedep == NULL) 9065 if (inodedep_lookup(jremref->jr_list.wk_mp, 9066 jremref->jr_ref.if_ino, 0, &inodedep) == 0) 9067 panic("journal_jremref: Lost inodedep"); 9068 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps); 9069 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 9070 add_to_journal(&jremref->jr_list); 9071 } 9072 9073 static void 9074 dirrem_journal(dirrem, jremref, dotremref, dotdotremref) 9075 struct dirrem *dirrem; 9076 struct jremref *jremref; 9077 struct jremref *dotremref; 9078 struct jremref *dotdotremref; 9079 { 9080 struct inodedep *inodedep; 9081 9082 9083 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0, 9084 &inodedep) == 0) 9085 panic("dirrem_journal: Lost inodedep"); 9086 journal_jremref(dirrem, jremref, inodedep); 9087 if (dotremref) 9088 journal_jremref(dirrem, dotremref, inodedep); 9089 if (dotdotremref) 9090 journal_jremref(dirrem, dotdotremref, NULL); 9091 } 9092 9093 /* 9094 * Allocate a new dirrem if appropriate and return it along with 9095 * its associated pagedep. Called without a lock, returns with lock. 9096 */ 9097 static struct dirrem * 9098 newdirrem(bp, dp, ip, isrmdir, prevdirremp) 9099 struct buf *bp; /* buffer containing directory block */ 9100 struct inode *dp; /* inode for the directory being modified */ 9101 struct inode *ip; /* inode for directory entry being removed */ 9102 int isrmdir; /* indicates if doing RMDIR */ 9103 struct dirrem **prevdirremp; /* previously referenced inode, if any */ 9104 { 9105 int offset; 9106 ufs_lbn_t lbn; 9107 struct diradd *dap; 9108 struct dirrem *dirrem; 9109 struct pagedep *pagedep; 9110 struct jremref *jremref; 9111 struct jremref *dotremref; 9112 struct jremref *dotdotremref; 9113 struct vnode *dvp; 9114 9115 /* 9116 * Whiteouts have no deletion dependencies. 9117 */ 9118 if (ip == NULL) 9119 panic("newdirrem: whiteout"); 9120 dvp = ITOV(dp); 9121 /* 9122 * If the system is over its limit and our filesystem is 9123 * responsible for more than our share of that usage and 9124 * we are not a snapshot, request some inodedep cleanup. 9125 * Limiting the number of dirrem structures will also limit 9126 * the number of freefile and freeblks structures. 9127 */ 9128 ACQUIRE_LOCK(ip->i_ump); 9129 if (!IS_SNAPSHOT(ip) && softdep_excess_items(ip->i_ump, D_DIRREM)) 9130 schedule_cleanup(ITOV(dp)->v_mount); 9131 else 9132 FREE_LOCK(ip->i_ump); 9133 dirrem = malloc(sizeof(struct dirrem), M_DIRREM, M_SOFTDEP_FLAGS | 9134 M_ZERO); 9135 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount); 9136 LIST_INIT(&dirrem->dm_jremrefhd); 9137 LIST_INIT(&dirrem->dm_jwork); 9138 dirrem->dm_state = isrmdir ? RMDIR : 0; 9139 dirrem->dm_oldinum = ip->i_number; 9140 *prevdirremp = NULL; 9141 /* 9142 * Allocate remove reference structures to track journal write 9143 * dependencies. We will always have one for the link and 9144 * when doing directories we will always have one more for dot. 9145 * When renaming a directory we skip the dotdot link change so 9146 * this is not needed. 9147 */ 9148 jremref = dotremref = dotdotremref = NULL; 9149 if (DOINGSUJ(dvp)) { 9150 if (isrmdir) { 9151 jremref = newjremref(dirrem, dp, ip, dp->i_offset, 9152 ip->i_effnlink + 2); 9153 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET, 9154 ip->i_effnlink + 1); 9155 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET, 9156 dp->i_effnlink + 1); 9157 dotdotremref->jr_state |= MKDIR_PARENT; 9158 } else 9159 jremref = newjremref(dirrem, dp, ip, dp->i_offset, 9160 ip->i_effnlink + 1); 9161 } 9162 ACQUIRE_LOCK(ip->i_ump); 9163 lbn = lblkno(dp->i_fs, dp->i_offset); 9164 offset = blkoff(dp->i_fs, dp->i_offset); 9165 pagedep_lookup(UFSTOVFS(dp->i_ump), bp, dp->i_number, lbn, DEPALLOC, 9166 &pagedep); 9167 dirrem->dm_pagedep = pagedep; 9168 dirrem->dm_offset = offset; 9169 /* 9170 * If we're renaming a .. link to a new directory, cancel any 9171 * existing MKDIR_PARENT mkdir. If it has already been canceled 9172 * the jremref is preserved for any potential diradd in this 9173 * location. This can not coincide with a rmdir. 9174 */ 9175 if (dp->i_offset == DOTDOT_OFFSET) { 9176 if (isrmdir) 9177 panic("newdirrem: .. directory change during remove?"); 9178 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref); 9179 } 9180 /* 9181 * If we're removing a directory search for the .. dependency now and 9182 * cancel it. Any pending journal work will be added to the dirrem 9183 * to be completed when the workitem remove completes. 9184 */ 9185 if (isrmdir) 9186 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref); 9187 /* 9188 * Check for a diradd dependency for the same directory entry. 9189 * If present, then both dependencies become obsolete and can 9190 * be de-allocated. 9191 */ 9192 dap = diradd_lookup(pagedep, offset); 9193 if (dap == NULL) { 9194 /* 9195 * Link the jremref structures into the dirrem so they are 9196 * written prior to the pagedep. 9197 */ 9198 if (jremref) 9199 dirrem_journal(dirrem, jremref, dotremref, 9200 dotdotremref); 9201 return (dirrem); 9202 } 9203 /* 9204 * Must be ATTACHED at this point. 9205 */ 9206 if ((dap->da_state & ATTACHED) == 0) 9207 panic("newdirrem: not ATTACHED"); 9208 if (dap->da_newinum != ip->i_number) 9209 panic("newdirrem: inum %ju should be %ju", 9210 (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum); 9211 /* 9212 * If we are deleting a changed name that never made it to disk, 9213 * then return the dirrem describing the previous inode (which 9214 * represents the inode currently referenced from this entry on disk). 9215 */ 9216 if ((dap->da_state & DIRCHG) != 0) { 9217 *prevdirremp = dap->da_previous; 9218 dap->da_state &= ~DIRCHG; 9219 dap->da_pagedep = pagedep; 9220 } 9221 /* 9222 * We are deleting an entry that never made it to disk. 9223 * Mark it COMPLETE so we can delete its inode immediately. 9224 */ 9225 dirrem->dm_state |= COMPLETE; 9226 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref); 9227 #ifdef SUJ_DEBUG 9228 if (isrmdir == 0) { 9229 struct worklist *wk; 9230 9231 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9232 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT)) 9233 panic("bad wk %p (0x%X)\n", wk, wk->wk_state); 9234 } 9235 #endif 9236 9237 return (dirrem); 9238 } 9239 9240 /* 9241 * Directory entry change dependencies. 9242 * 9243 * Changing an existing directory entry requires that an add operation 9244 * be completed first followed by a deletion. The semantics for the addition 9245 * are identical to the description of adding a new entry above except 9246 * that the rollback is to the old inode number rather than zero. Once 9247 * the addition dependency is completed, the removal is done as described 9248 * in the removal routine above. 9249 */ 9250 9251 /* 9252 * This routine should be called immediately after changing 9253 * a directory entry. The inode's link count should not be 9254 * decremented by the calling procedure -- the soft updates 9255 * code will perform this task when it is safe. 9256 */ 9257 void 9258 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 9259 struct buf *bp; /* buffer containing directory block */ 9260 struct inode *dp; /* inode for the directory being modified */ 9261 struct inode *ip; /* inode for directory entry being removed */ 9262 ino_t newinum; /* new inode number for changed entry */ 9263 int isrmdir; /* indicates if doing RMDIR */ 9264 { 9265 int offset; 9266 struct diradd *dap = NULL; 9267 struct dirrem *dirrem, *prevdirrem; 9268 struct pagedep *pagedep; 9269 struct inodedep *inodedep; 9270 struct jaddref *jaddref; 9271 struct mount *mp; 9272 9273 offset = blkoff(dp->i_fs, dp->i_offset); 9274 mp = UFSTOVFS(dp->i_ump); 9275 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 9276 ("softdep_setup_directory_change called on non-softdep filesystem")); 9277 9278 /* 9279 * Whiteouts do not need diradd dependencies. 9280 */ 9281 if (newinum != WINO) { 9282 dap = malloc(sizeof(struct diradd), 9283 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO); 9284 workitem_alloc(&dap->da_list, D_DIRADD, mp); 9285 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE; 9286 dap->da_offset = offset; 9287 dap->da_newinum = newinum; 9288 LIST_INIT(&dap->da_jwork); 9289 } 9290 9291 /* 9292 * Allocate a new dirrem and ACQUIRE_LOCK. 9293 */ 9294 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 9295 pagedep = dirrem->dm_pagedep; 9296 /* 9297 * The possible values for isrmdir: 9298 * 0 - non-directory file rename 9299 * 1 - directory rename within same directory 9300 * inum - directory rename to new directory of given inode number 9301 * When renaming to a new directory, we are both deleting and 9302 * creating a new directory entry, so the link count on the new 9303 * directory should not change. Thus we do not need the followup 9304 * dirrem which is usually done in handle_workitem_remove. We set 9305 * the DIRCHG flag to tell handle_workitem_remove to skip the 9306 * followup dirrem. 9307 */ 9308 if (isrmdir > 1) 9309 dirrem->dm_state |= DIRCHG; 9310 9311 /* 9312 * Whiteouts have no additional dependencies, 9313 * so just put the dirrem on the correct list. 9314 */ 9315 if (newinum == WINO) { 9316 if ((dirrem->dm_state & COMPLETE) == 0) { 9317 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem, 9318 dm_next); 9319 } else { 9320 dirrem->dm_dirinum = pagedep->pd_ino; 9321 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9322 add_to_worklist(&dirrem->dm_list, 0); 9323 } 9324 FREE_LOCK(dp->i_ump); 9325 return; 9326 } 9327 /* 9328 * Add the dirrem to the inodedep's pending remove list for quick 9329 * discovery later. A valid nlinkdelta ensures that this lookup 9330 * will not fail. 9331 */ 9332 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9333 panic("softdep_setup_directory_change: Lost inodedep."); 9334 dirrem->dm_state |= ONDEPLIST; 9335 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9336 9337 /* 9338 * If the COMPLETE flag is clear, then there were no active 9339 * entries and we want to roll back to the previous inode until 9340 * the new inode is committed to disk. If the COMPLETE flag is 9341 * set, then we have deleted an entry that never made it to disk. 9342 * If the entry we deleted resulted from a name change, then the old 9343 * inode reference still resides on disk. Any rollback that we do 9344 * needs to be to that old inode (returned to us in prevdirrem). If 9345 * the entry we deleted resulted from a create, then there is 9346 * no entry on the disk, so we want to roll back to zero rather 9347 * than the uncommitted inode. In either of the COMPLETE cases we 9348 * want to immediately free the unwritten and unreferenced inode. 9349 */ 9350 if ((dirrem->dm_state & COMPLETE) == 0) { 9351 dap->da_previous = dirrem; 9352 } else { 9353 if (prevdirrem != NULL) { 9354 dap->da_previous = prevdirrem; 9355 } else { 9356 dap->da_state &= ~DIRCHG; 9357 dap->da_pagedep = pagedep; 9358 } 9359 dirrem->dm_dirinum = pagedep->pd_ino; 9360 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9361 add_to_worklist(&dirrem->dm_list, 0); 9362 } 9363 /* 9364 * Lookup the jaddref for this journal entry. We must finish 9365 * initializing it and make the diradd write dependent on it. 9366 * If we're not journaling, put it on the id_bufwait list if the 9367 * inode is not yet written. If it is written, do the post-inode 9368 * write processing to put it on the id_pendinghd list. 9369 */ 9370 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 9371 if (MOUNTEDSUJ(mp)) { 9372 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 9373 inoreflst); 9374 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 9375 ("softdep_setup_directory_change: bad jaddref %p", 9376 jaddref)); 9377 jaddref->ja_diroff = dp->i_offset; 9378 jaddref->ja_diradd = dap; 9379 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9380 dap, da_pdlist); 9381 add_to_journal(&jaddref->ja_list); 9382 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 9383 dap->da_state |= COMPLETE; 9384 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 9385 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 9386 } else { 9387 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9388 dap, da_pdlist); 9389 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 9390 } 9391 /* 9392 * If we're making a new name for a directory that has not been 9393 * committed when need to move the dot and dotdot references to 9394 * this new name. 9395 */ 9396 if (inodedep->id_mkdiradd && dp->i_offset != DOTDOT_OFFSET) 9397 merge_diradd(inodedep, dap); 9398 FREE_LOCK(dp->i_ump); 9399 } 9400 9401 /* 9402 * Called whenever the link count on an inode is changed. 9403 * It creates an inode dependency so that the new reference(s) 9404 * to the inode cannot be committed to disk until the updated 9405 * inode has been written. 9406 */ 9407 void 9408 softdep_change_linkcnt(ip) 9409 struct inode *ip; /* the inode with the increased link count */ 9410 { 9411 struct inodedep *inodedep; 9412 9413 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0, 9414 ("softdep_change_linkcnt called on non-softdep filesystem")); 9415 ACQUIRE_LOCK(ip->i_ump); 9416 inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, DEPALLOC, 9417 &inodedep); 9418 if (ip->i_nlink < ip->i_effnlink) 9419 panic("softdep_change_linkcnt: bad delta"); 9420 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9421 FREE_LOCK(ip->i_ump); 9422 } 9423 9424 /* 9425 * Attach a sbdep dependency to the superblock buf so that we can keep 9426 * track of the head of the linked list of referenced but unlinked inodes. 9427 */ 9428 void 9429 softdep_setup_sbupdate(ump, fs, bp) 9430 struct ufsmount *ump; 9431 struct fs *fs; 9432 struct buf *bp; 9433 { 9434 struct sbdep *sbdep; 9435 struct worklist *wk; 9436 9437 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9438 ("softdep_setup_sbupdate called on non-softdep filesystem")); 9439 LIST_FOREACH(wk, &bp->b_dep, wk_list) 9440 if (wk->wk_type == D_SBDEP) 9441 break; 9442 if (wk != NULL) 9443 return; 9444 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS); 9445 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump)); 9446 sbdep->sb_fs = fs; 9447 sbdep->sb_ump = ump; 9448 ACQUIRE_LOCK(ump); 9449 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list); 9450 FREE_LOCK(ump); 9451 } 9452 9453 /* 9454 * Return the first unlinked inodedep which is ready to be the head of the 9455 * list. The inodedep and all those after it must have valid next pointers. 9456 */ 9457 static struct inodedep * 9458 first_unlinked_inodedep(ump) 9459 struct ufsmount *ump; 9460 { 9461 struct inodedep *inodedep; 9462 struct inodedep *idp; 9463 9464 LOCK_OWNED(ump); 9465 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst); 9466 inodedep; inodedep = idp) { 9467 if ((inodedep->id_state & UNLINKNEXT) == 0) 9468 return (NULL); 9469 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9470 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0) 9471 break; 9472 if ((inodedep->id_state & UNLINKPREV) == 0) 9473 break; 9474 } 9475 return (inodedep); 9476 } 9477 9478 /* 9479 * Set the sujfree unlinked head pointer prior to writing a superblock. 9480 */ 9481 static void 9482 initiate_write_sbdep(sbdep) 9483 struct sbdep *sbdep; 9484 { 9485 struct inodedep *inodedep; 9486 struct fs *bpfs; 9487 struct fs *fs; 9488 9489 bpfs = sbdep->sb_fs; 9490 fs = sbdep->sb_ump->um_fs; 9491 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9492 if (inodedep) { 9493 fs->fs_sujfree = inodedep->id_ino; 9494 inodedep->id_state |= UNLINKPREV; 9495 } else 9496 fs->fs_sujfree = 0; 9497 bpfs->fs_sujfree = fs->fs_sujfree; 9498 } 9499 9500 /* 9501 * After a superblock is written determine whether it must be written again 9502 * due to a changing unlinked list head. 9503 */ 9504 static int 9505 handle_written_sbdep(sbdep, bp) 9506 struct sbdep *sbdep; 9507 struct buf *bp; 9508 { 9509 struct inodedep *inodedep; 9510 struct fs *fs; 9511 9512 LOCK_OWNED(sbdep->sb_ump); 9513 fs = sbdep->sb_fs; 9514 /* 9515 * If the superblock doesn't match the in-memory list start over. 9516 */ 9517 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9518 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) || 9519 (inodedep == NULL && fs->fs_sujfree != 0)) { 9520 bdirty(bp); 9521 return (1); 9522 } 9523 WORKITEM_FREE(sbdep, D_SBDEP); 9524 if (fs->fs_sujfree == 0) 9525 return (0); 9526 /* 9527 * Now that we have a record of this inode in stable store allow it 9528 * to be written to free up pending work. Inodes may see a lot of 9529 * write activity after they are unlinked which we must not hold up. 9530 */ 9531 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 9532 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS) 9533 panic("handle_written_sbdep: Bad inodedep %p (0x%X)", 9534 inodedep, inodedep->id_state); 9535 if (inodedep->id_state & UNLINKONLIST) 9536 break; 9537 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST; 9538 } 9539 9540 return (0); 9541 } 9542 9543 /* 9544 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list. 9545 */ 9546 static void 9547 unlinked_inodedep(mp, inodedep) 9548 struct mount *mp; 9549 struct inodedep *inodedep; 9550 { 9551 struct ufsmount *ump; 9552 9553 ump = VFSTOUFS(mp); 9554 LOCK_OWNED(ump); 9555 if (MOUNTEDSUJ(mp) == 0) 9556 return; 9557 ump->um_fs->fs_fmod = 1; 9558 if (inodedep->id_state & UNLINKED) 9559 panic("unlinked_inodedep: %p already unlinked\n", inodedep); 9560 inodedep->id_state |= UNLINKED; 9561 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked); 9562 } 9563 9564 /* 9565 * Remove an inodedep from the unlinked inodedep list. This may require 9566 * disk writes if the inode has made it that far. 9567 */ 9568 static void 9569 clear_unlinked_inodedep(inodedep) 9570 struct inodedep *inodedep; 9571 { 9572 struct ufsmount *ump; 9573 struct inodedep *idp; 9574 struct inodedep *idn; 9575 struct fs *fs; 9576 struct buf *bp; 9577 ino_t ino; 9578 ino_t nino; 9579 ino_t pino; 9580 int error; 9581 9582 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9583 fs = ump->um_fs; 9584 ino = inodedep->id_ino; 9585 error = 0; 9586 for (;;) { 9587 LOCK_OWNED(ump); 9588 KASSERT((inodedep->id_state & UNLINKED) != 0, 9589 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9590 inodedep)); 9591 /* 9592 * If nothing has yet been written simply remove us from 9593 * the in memory list and return. This is the most common 9594 * case where handle_workitem_remove() loses the final 9595 * reference. 9596 */ 9597 if ((inodedep->id_state & UNLINKLINKS) == 0) 9598 break; 9599 /* 9600 * If we have a NEXT pointer and no PREV pointer we can simply 9601 * clear NEXT's PREV and remove ourselves from the list. Be 9602 * careful not to clear PREV if the superblock points at 9603 * next as well. 9604 */ 9605 idn = TAILQ_NEXT(inodedep, id_unlinked); 9606 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) { 9607 if (idn && fs->fs_sujfree != idn->id_ino) 9608 idn->id_state &= ~UNLINKPREV; 9609 break; 9610 } 9611 /* 9612 * Here we have an inodedep which is actually linked into 9613 * the list. We must remove it by forcing a write to the 9614 * link before us, whether it be the superblock or an inode. 9615 * Unfortunately the list may change while we're waiting 9616 * on the buf lock for either resource so we must loop until 9617 * we lock the right one. If both the superblock and an 9618 * inode point to this inode we must clear the inode first 9619 * followed by the superblock. 9620 */ 9621 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9622 pino = 0; 9623 if (idp && (idp->id_state & UNLINKNEXT)) 9624 pino = idp->id_ino; 9625 FREE_LOCK(ump); 9626 if (pino == 0) { 9627 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9628 (int)fs->fs_sbsize, 0, 0, 0); 9629 } else { 9630 error = bread(ump->um_devvp, 9631 fsbtodb(fs, ino_to_fsba(fs, pino)), 9632 (int)fs->fs_bsize, NOCRED, &bp); 9633 if (error) 9634 brelse(bp); 9635 } 9636 ACQUIRE_LOCK(ump); 9637 if (error) 9638 break; 9639 /* If the list has changed restart the loop. */ 9640 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9641 nino = 0; 9642 if (idp && (idp->id_state & UNLINKNEXT)) 9643 nino = idp->id_ino; 9644 if (nino != pino || 9645 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) { 9646 FREE_LOCK(ump); 9647 brelse(bp); 9648 ACQUIRE_LOCK(ump); 9649 continue; 9650 } 9651 nino = 0; 9652 idn = TAILQ_NEXT(inodedep, id_unlinked); 9653 if (idn) 9654 nino = idn->id_ino; 9655 /* 9656 * Remove us from the in memory list. After this we cannot 9657 * access the inodedep. 9658 */ 9659 KASSERT((inodedep->id_state & UNLINKED) != 0, 9660 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9661 inodedep)); 9662 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9663 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9664 FREE_LOCK(ump); 9665 /* 9666 * The predecessor's next pointer is manually updated here 9667 * so that the NEXT flag is never cleared for an element 9668 * that is in the list. 9669 */ 9670 if (pino == 0) { 9671 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 9672 ffs_oldfscompat_write((struct fs *)bp->b_data, ump); 9673 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, 9674 bp); 9675 } else if (fs->fs_magic == FS_UFS1_MAGIC) 9676 ((struct ufs1_dinode *)bp->b_data + 9677 ino_to_fsbo(fs, pino))->di_freelink = nino; 9678 else 9679 ((struct ufs2_dinode *)bp->b_data + 9680 ino_to_fsbo(fs, pino))->di_freelink = nino; 9681 /* 9682 * If the bwrite fails we have no recourse to recover. The 9683 * filesystem is corrupted already. 9684 */ 9685 bwrite(bp); 9686 ACQUIRE_LOCK(ump); 9687 /* 9688 * If the superblock pointer still needs to be cleared force 9689 * a write here. 9690 */ 9691 if (fs->fs_sujfree == ino) { 9692 FREE_LOCK(ump); 9693 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9694 (int)fs->fs_sbsize, 0, 0, 0); 9695 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 9696 ffs_oldfscompat_write((struct fs *)bp->b_data, ump); 9697 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, 9698 bp); 9699 bwrite(bp); 9700 ACQUIRE_LOCK(ump); 9701 } 9702 9703 if (fs->fs_sujfree != ino) 9704 return; 9705 panic("clear_unlinked_inodedep: Failed to clear free head"); 9706 } 9707 if (inodedep->id_ino == fs->fs_sujfree) 9708 panic("clear_unlinked_inodedep: Freeing head of free list"); 9709 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9710 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9711 return; 9712 } 9713 9714 /* 9715 * This workitem decrements the inode's link count. 9716 * If the link count reaches zero, the file is removed. 9717 */ 9718 static int 9719 handle_workitem_remove(dirrem, flags) 9720 struct dirrem *dirrem; 9721 int flags; 9722 { 9723 struct inodedep *inodedep; 9724 struct workhead dotdotwk; 9725 struct worklist *wk; 9726 struct ufsmount *ump; 9727 struct mount *mp; 9728 struct vnode *vp; 9729 struct inode *ip; 9730 ino_t oldinum; 9731 9732 if (dirrem->dm_state & ONWORKLIST) 9733 panic("handle_workitem_remove: dirrem %p still on worklist", 9734 dirrem); 9735 oldinum = dirrem->dm_oldinum; 9736 mp = dirrem->dm_list.wk_mp; 9737 ump = VFSTOUFS(mp); 9738 flags |= LK_EXCLUSIVE; 9739 if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ) != 0) 9740 return (EBUSY); 9741 ip = VTOI(vp); 9742 ACQUIRE_LOCK(ump); 9743 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0) 9744 panic("handle_workitem_remove: lost inodedep"); 9745 if (dirrem->dm_state & ONDEPLIST) 9746 LIST_REMOVE(dirrem, dm_inonext); 9747 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 9748 ("handle_workitem_remove: Journal entries not written.")); 9749 9750 /* 9751 * Move all dependencies waiting on the remove to complete 9752 * from the dirrem to the inode inowait list to be completed 9753 * after the inode has been updated and written to disk. Any 9754 * marked MKDIR_PARENT are saved to be completed when the .. ref 9755 * is removed. 9756 */ 9757 LIST_INIT(&dotdotwk); 9758 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) { 9759 WORKLIST_REMOVE(wk); 9760 if (wk->wk_state & MKDIR_PARENT) { 9761 wk->wk_state &= ~MKDIR_PARENT; 9762 WORKLIST_INSERT(&dotdotwk, wk); 9763 continue; 9764 } 9765 WORKLIST_INSERT(&inodedep->id_inowait, wk); 9766 } 9767 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list); 9768 /* 9769 * Normal file deletion. 9770 */ 9771 if ((dirrem->dm_state & RMDIR) == 0) { 9772 ip->i_nlink--; 9773 DIP_SET(ip, i_nlink, ip->i_nlink); 9774 ip->i_flag |= IN_CHANGE; 9775 if (ip->i_nlink < ip->i_effnlink) 9776 panic("handle_workitem_remove: bad file delta"); 9777 if (ip->i_nlink == 0) 9778 unlinked_inodedep(mp, inodedep); 9779 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9780 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 9781 ("handle_workitem_remove: worklist not empty. %s", 9782 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type))); 9783 WORKITEM_FREE(dirrem, D_DIRREM); 9784 FREE_LOCK(ump); 9785 goto out; 9786 } 9787 /* 9788 * Directory deletion. Decrement reference count for both the 9789 * just deleted parent directory entry and the reference for ".". 9790 * Arrange to have the reference count on the parent decremented 9791 * to account for the loss of "..". 9792 */ 9793 ip->i_nlink -= 2; 9794 DIP_SET(ip, i_nlink, ip->i_nlink); 9795 ip->i_flag |= IN_CHANGE; 9796 if (ip->i_nlink < ip->i_effnlink) 9797 panic("handle_workitem_remove: bad dir delta"); 9798 if (ip->i_nlink == 0) 9799 unlinked_inodedep(mp, inodedep); 9800 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9801 /* 9802 * Rename a directory to a new parent. Since, we are both deleting 9803 * and creating a new directory entry, the link count on the new 9804 * directory should not change. Thus we skip the followup dirrem. 9805 */ 9806 if (dirrem->dm_state & DIRCHG) { 9807 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 9808 ("handle_workitem_remove: DIRCHG and worklist not empty.")); 9809 WORKITEM_FREE(dirrem, D_DIRREM); 9810 FREE_LOCK(ump); 9811 goto out; 9812 } 9813 dirrem->dm_state = ONDEPLIST; 9814 dirrem->dm_oldinum = dirrem->dm_dirinum; 9815 /* 9816 * Place the dirrem on the parent's diremhd list. 9817 */ 9818 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0) 9819 panic("handle_workitem_remove: lost dir inodedep"); 9820 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9821 /* 9822 * If the allocated inode has never been written to disk, then 9823 * the on-disk inode is zero'ed and we can remove the file 9824 * immediately. When journaling if the inode has been marked 9825 * unlinked and not DEPCOMPLETE we know it can never be written. 9826 */ 9827 inodedep_lookup(mp, oldinum, 0, &inodedep); 9828 if (inodedep == NULL || 9829 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED || 9830 check_inode_unwritten(inodedep)) { 9831 FREE_LOCK(ump); 9832 vput(vp); 9833 return handle_workitem_remove(dirrem, flags); 9834 } 9835 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list); 9836 FREE_LOCK(ump); 9837 ip->i_flag |= IN_CHANGE; 9838 out: 9839 ffs_update(vp, 0); 9840 vput(vp); 9841 return (0); 9842 } 9843 9844 /* 9845 * Inode de-allocation dependencies. 9846 * 9847 * When an inode's link count is reduced to zero, it can be de-allocated. We 9848 * found it convenient to postpone de-allocation until after the inode is 9849 * written to disk with its new link count (zero). At this point, all of the 9850 * on-disk inode's block pointers are nullified and, with careful dependency 9851 * list ordering, all dependencies related to the inode will be satisfied and 9852 * the corresponding dependency structures de-allocated. So, if/when the 9853 * inode is reused, there will be no mixing of old dependencies with new 9854 * ones. This artificial dependency is set up by the block de-allocation 9855 * procedure above (softdep_setup_freeblocks) and completed by the 9856 * following procedure. 9857 */ 9858 static void 9859 handle_workitem_freefile(freefile) 9860 struct freefile *freefile; 9861 { 9862 struct workhead wkhd; 9863 struct fs *fs; 9864 struct inodedep *idp; 9865 struct ufsmount *ump; 9866 int error; 9867 9868 ump = VFSTOUFS(freefile->fx_list.wk_mp); 9869 fs = ump->um_fs; 9870 #ifdef DEBUG 9871 ACQUIRE_LOCK(ump); 9872 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp); 9873 FREE_LOCK(ump); 9874 if (error) 9875 panic("handle_workitem_freefile: inodedep %p survived", idp); 9876 #endif 9877 UFS_LOCK(ump); 9878 fs->fs_pendinginodes -= 1; 9879 UFS_UNLOCK(ump); 9880 LIST_INIT(&wkhd); 9881 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list); 9882 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp, 9883 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0) 9884 softdep_error("handle_workitem_freefile", error); 9885 ACQUIRE_LOCK(ump); 9886 WORKITEM_FREE(freefile, D_FREEFILE); 9887 FREE_LOCK(ump); 9888 } 9889 9890 9891 /* 9892 * Helper function which unlinks marker element from work list and returns 9893 * the next element on the list. 9894 */ 9895 static __inline struct worklist * 9896 markernext(struct worklist *marker) 9897 { 9898 struct worklist *next; 9899 9900 next = LIST_NEXT(marker, wk_list); 9901 LIST_REMOVE(marker, wk_list); 9902 return next; 9903 } 9904 9905 /* 9906 * Disk writes. 9907 * 9908 * The dependency structures constructed above are most actively used when file 9909 * system blocks are written to disk. No constraints are placed on when a 9910 * block can be written, but unsatisfied update dependencies are made safe by 9911 * modifying (or replacing) the source memory for the duration of the disk 9912 * write. When the disk write completes, the memory block is again brought 9913 * up-to-date. 9914 * 9915 * In-core inode structure reclamation. 9916 * 9917 * Because there are a finite number of "in-core" inode structures, they are 9918 * reused regularly. By transferring all inode-related dependencies to the 9919 * in-memory inode block and indexing them separately (via "inodedep"s), we 9920 * can allow "in-core" inode structures to be reused at any time and avoid 9921 * any increase in contention. 9922 * 9923 * Called just before entering the device driver to initiate a new disk I/O. 9924 * The buffer must be locked, thus, no I/O completion operations can occur 9925 * while we are manipulating its associated dependencies. 9926 */ 9927 static void 9928 softdep_disk_io_initiation(bp) 9929 struct buf *bp; /* structure describing disk write to occur */ 9930 { 9931 struct worklist *wk; 9932 struct worklist marker; 9933 struct inodedep *inodedep; 9934 struct freeblks *freeblks; 9935 struct jblkdep *jblkdep; 9936 struct newblk *newblk; 9937 struct ufsmount *ump; 9938 9939 /* 9940 * We only care about write operations. There should never 9941 * be dependencies for reads. 9942 */ 9943 if (bp->b_iocmd != BIO_WRITE) 9944 panic("softdep_disk_io_initiation: not write"); 9945 9946 if (bp->b_vflags & BV_BKGRDINPROG) 9947 panic("softdep_disk_io_initiation: Writing buffer with " 9948 "background write in progress: %p", bp); 9949 9950 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL) 9951 return; 9952 ump = VFSTOUFS(wk->wk_mp); 9953 9954 marker.wk_type = D_LAST + 1; /* Not a normal workitem */ 9955 PHOLD(curproc); /* Don't swap out kernel stack */ 9956 ACQUIRE_LOCK(ump); 9957 /* 9958 * Do any necessary pre-I/O processing. 9959 */ 9960 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL; 9961 wk = markernext(&marker)) { 9962 LIST_INSERT_AFTER(wk, &marker, wk_list); 9963 switch (wk->wk_type) { 9964 9965 case D_PAGEDEP: 9966 initiate_write_filepage(WK_PAGEDEP(wk), bp); 9967 continue; 9968 9969 case D_INODEDEP: 9970 inodedep = WK_INODEDEP(wk); 9971 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) 9972 initiate_write_inodeblock_ufs1(inodedep, bp); 9973 else 9974 initiate_write_inodeblock_ufs2(inodedep, bp); 9975 continue; 9976 9977 case D_INDIRDEP: 9978 initiate_write_indirdep(WK_INDIRDEP(wk), bp); 9979 continue; 9980 9981 case D_BMSAFEMAP: 9982 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp); 9983 continue; 9984 9985 case D_JSEG: 9986 WK_JSEG(wk)->js_buf = NULL; 9987 continue; 9988 9989 case D_FREEBLKS: 9990 freeblks = WK_FREEBLKS(wk); 9991 jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd); 9992 /* 9993 * We have to wait for the freeblks to be journaled 9994 * before we can write an inodeblock with updated 9995 * pointers. Be careful to arrange the marker so 9996 * we revisit the freeblks if it's not removed by 9997 * the first jwait(). 9998 */ 9999 if (jblkdep != NULL) { 10000 LIST_REMOVE(&marker, wk_list); 10001 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10002 jwait(&jblkdep->jb_list, MNT_WAIT); 10003 } 10004 continue; 10005 case D_ALLOCDIRECT: 10006 case D_ALLOCINDIR: 10007 /* 10008 * We have to wait for the jnewblk to be journaled 10009 * before we can write to a block if the contents 10010 * may be confused with an earlier file's indirect 10011 * at recovery time. Handle the marker as described 10012 * above. 10013 */ 10014 newblk = WK_NEWBLK(wk); 10015 if (newblk->nb_jnewblk != NULL && 10016 indirblk_lookup(newblk->nb_list.wk_mp, 10017 newblk->nb_newblkno)) { 10018 LIST_REMOVE(&marker, wk_list); 10019 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10020 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 10021 } 10022 continue; 10023 10024 case D_SBDEP: 10025 initiate_write_sbdep(WK_SBDEP(wk)); 10026 continue; 10027 10028 case D_MKDIR: 10029 case D_FREEWORK: 10030 case D_FREEDEP: 10031 case D_JSEGDEP: 10032 continue; 10033 10034 default: 10035 panic("handle_disk_io_initiation: Unexpected type %s", 10036 TYPENAME(wk->wk_type)); 10037 /* NOTREACHED */ 10038 } 10039 } 10040 FREE_LOCK(ump); 10041 PRELE(curproc); /* Allow swapout of kernel stack */ 10042 } 10043 10044 /* 10045 * Called from within the procedure above to deal with unsatisfied 10046 * allocation dependencies in a directory. The buffer must be locked, 10047 * thus, no I/O completion operations can occur while we are 10048 * manipulating its associated dependencies. 10049 */ 10050 static void 10051 initiate_write_filepage(pagedep, bp) 10052 struct pagedep *pagedep; 10053 struct buf *bp; 10054 { 10055 struct jremref *jremref; 10056 struct jmvref *jmvref; 10057 struct dirrem *dirrem; 10058 struct diradd *dap; 10059 struct direct *ep; 10060 int i; 10061 10062 if (pagedep->pd_state & IOSTARTED) { 10063 /* 10064 * This can only happen if there is a driver that does not 10065 * understand chaining. Here biodone will reissue the call 10066 * to strategy for the incomplete buffers. 10067 */ 10068 printf("initiate_write_filepage: already started\n"); 10069 return; 10070 } 10071 pagedep->pd_state |= IOSTARTED; 10072 /* 10073 * Wait for all journal remove dependencies to hit the disk. 10074 * We can not allow any potentially conflicting directory adds 10075 * to be visible before removes and rollback is too difficult. 10076 * The per-filesystem lock may be dropped and re-acquired, however 10077 * we hold the buf locked so the dependency can not go away. 10078 */ 10079 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) 10080 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) 10081 jwait(&jremref->jr_list, MNT_WAIT); 10082 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) 10083 jwait(&jmvref->jm_list, MNT_WAIT); 10084 for (i = 0; i < DAHASHSZ; i++) { 10085 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 10086 ep = (struct direct *) 10087 ((char *)bp->b_data + dap->da_offset); 10088 if (ep->d_ino != dap->da_newinum) 10089 panic("%s: dir inum %ju != new %ju", 10090 "initiate_write_filepage", 10091 (uintmax_t)ep->d_ino, 10092 (uintmax_t)dap->da_newinum); 10093 if (dap->da_state & DIRCHG) 10094 ep->d_ino = dap->da_previous->dm_oldinum; 10095 else 10096 ep->d_ino = 0; 10097 dap->da_state &= ~ATTACHED; 10098 dap->da_state |= UNDONE; 10099 } 10100 } 10101 } 10102 10103 /* 10104 * Version of initiate_write_inodeblock that handles UFS1 dinodes. 10105 * Note that any bug fixes made to this routine must be done in the 10106 * version found below. 10107 * 10108 * Called from within the procedure above to deal with unsatisfied 10109 * allocation dependencies in an inodeblock. The buffer must be 10110 * locked, thus, no I/O completion operations can occur while we 10111 * are manipulating its associated dependencies. 10112 */ 10113 static void 10114 initiate_write_inodeblock_ufs1(inodedep, bp) 10115 struct inodedep *inodedep; 10116 struct buf *bp; /* The inode block */ 10117 { 10118 struct allocdirect *adp, *lastadp; 10119 struct ufs1_dinode *dp; 10120 struct ufs1_dinode *sip; 10121 struct inoref *inoref; 10122 struct ufsmount *ump; 10123 struct fs *fs; 10124 ufs_lbn_t i; 10125 #ifdef INVARIANTS 10126 ufs_lbn_t prevlbn = 0; 10127 #endif 10128 int deplist; 10129 10130 if (inodedep->id_state & IOSTARTED) 10131 panic("initiate_write_inodeblock_ufs1: already started"); 10132 inodedep->id_state |= IOSTARTED; 10133 fs = inodedep->id_fs; 10134 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10135 LOCK_OWNED(ump); 10136 dp = (struct ufs1_dinode *)bp->b_data + 10137 ino_to_fsbo(fs, inodedep->id_ino); 10138 10139 /* 10140 * If we're on the unlinked list but have not yet written our 10141 * next pointer initialize it here. 10142 */ 10143 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10144 struct inodedep *inon; 10145 10146 inon = TAILQ_NEXT(inodedep, id_unlinked); 10147 dp->di_freelink = inon ? inon->id_ino : 0; 10148 } 10149 /* 10150 * If the bitmap is not yet written, then the allocated 10151 * inode cannot be written to disk. 10152 */ 10153 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10154 if (inodedep->id_savedino1 != NULL) 10155 panic("initiate_write_inodeblock_ufs1: I/O underway"); 10156 FREE_LOCK(ump); 10157 sip = malloc(sizeof(struct ufs1_dinode), 10158 M_SAVEDINO, M_SOFTDEP_FLAGS); 10159 ACQUIRE_LOCK(ump); 10160 inodedep->id_savedino1 = sip; 10161 *inodedep->id_savedino1 = *dp; 10162 bzero((caddr_t)dp, sizeof(struct ufs1_dinode)); 10163 dp->di_gen = inodedep->id_savedino1->di_gen; 10164 dp->di_freelink = inodedep->id_savedino1->di_freelink; 10165 return; 10166 } 10167 /* 10168 * If no dependencies, then there is nothing to roll back. 10169 */ 10170 inodedep->id_savedsize = dp->di_size; 10171 inodedep->id_savedextsize = 0; 10172 inodedep->id_savednlink = dp->di_nlink; 10173 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10174 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10175 return; 10176 /* 10177 * Revert the link count to that of the first unwritten journal entry. 10178 */ 10179 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10180 if (inoref) 10181 dp->di_nlink = inoref->if_nlink; 10182 /* 10183 * Set the dependencies to busy. 10184 */ 10185 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10186 adp = TAILQ_NEXT(adp, ad_next)) { 10187 #ifdef INVARIANTS 10188 if (deplist != 0 && prevlbn >= adp->ad_offset) 10189 panic("softdep_write_inodeblock: lbn order"); 10190 prevlbn = adp->ad_offset; 10191 if (adp->ad_offset < NDADDR && 10192 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10193 panic("%s: direct pointer #%jd mismatch %d != %jd", 10194 "softdep_write_inodeblock", 10195 (intmax_t)adp->ad_offset, 10196 dp->di_db[adp->ad_offset], 10197 (intmax_t)adp->ad_newblkno); 10198 if (adp->ad_offset >= NDADDR && 10199 dp->di_ib[adp->ad_offset - NDADDR] != adp->ad_newblkno) 10200 panic("%s: indirect pointer #%jd mismatch %d != %jd", 10201 "softdep_write_inodeblock", 10202 (intmax_t)adp->ad_offset - NDADDR, 10203 dp->di_ib[adp->ad_offset - NDADDR], 10204 (intmax_t)adp->ad_newblkno); 10205 deplist |= 1 << adp->ad_offset; 10206 if ((adp->ad_state & ATTACHED) == 0) 10207 panic("softdep_write_inodeblock: Unknown state 0x%x", 10208 adp->ad_state); 10209 #endif /* INVARIANTS */ 10210 adp->ad_state &= ~ATTACHED; 10211 adp->ad_state |= UNDONE; 10212 } 10213 /* 10214 * The on-disk inode cannot claim to be any larger than the last 10215 * fragment that has been written. Otherwise, the on-disk inode 10216 * might have fragments that were not the last block in the file 10217 * which would corrupt the filesystem. 10218 */ 10219 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10220 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10221 if (adp->ad_offset >= NDADDR) 10222 break; 10223 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10224 /* keep going until hitting a rollback to a frag */ 10225 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10226 continue; 10227 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10228 for (i = adp->ad_offset + 1; i < NDADDR; i++) { 10229 #ifdef INVARIANTS 10230 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10231 panic("softdep_write_inodeblock: lost dep1"); 10232 #endif /* INVARIANTS */ 10233 dp->di_db[i] = 0; 10234 } 10235 for (i = 0; i < NIADDR; i++) { 10236 #ifdef INVARIANTS 10237 if (dp->di_ib[i] != 0 && 10238 (deplist & ((1 << NDADDR) << i)) == 0) 10239 panic("softdep_write_inodeblock: lost dep2"); 10240 #endif /* INVARIANTS */ 10241 dp->di_ib[i] = 0; 10242 } 10243 return; 10244 } 10245 /* 10246 * If we have zero'ed out the last allocated block of the file, 10247 * roll back the size to the last currently allocated block. 10248 * We know that this last allocated block is a full-sized as 10249 * we already checked for fragments in the loop above. 10250 */ 10251 if (lastadp != NULL && 10252 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10253 for (i = lastadp->ad_offset; i >= 0; i--) 10254 if (dp->di_db[i] != 0) 10255 break; 10256 dp->di_size = (i + 1) * fs->fs_bsize; 10257 } 10258 /* 10259 * The only dependencies are for indirect blocks. 10260 * 10261 * The file size for indirect block additions is not guaranteed. 10262 * Such a guarantee would be non-trivial to achieve. The conventional 10263 * synchronous write implementation also does not make this guarantee. 10264 * Fsck should catch and fix discrepancies. Arguably, the file size 10265 * can be over-estimated without destroying integrity when the file 10266 * moves into the indirect blocks (i.e., is large). If we want to 10267 * postpone fsck, we are stuck with this argument. 10268 */ 10269 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10270 dp->di_ib[adp->ad_offset - NDADDR] = 0; 10271 } 10272 10273 /* 10274 * Version of initiate_write_inodeblock that handles UFS2 dinodes. 10275 * Note that any bug fixes made to this routine must be done in the 10276 * version found above. 10277 * 10278 * Called from within the procedure above to deal with unsatisfied 10279 * allocation dependencies in an inodeblock. The buffer must be 10280 * locked, thus, no I/O completion operations can occur while we 10281 * are manipulating its associated dependencies. 10282 */ 10283 static void 10284 initiate_write_inodeblock_ufs2(inodedep, bp) 10285 struct inodedep *inodedep; 10286 struct buf *bp; /* The inode block */ 10287 { 10288 struct allocdirect *adp, *lastadp; 10289 struct ufs2_dinode *dp; 10290 struct ufs2_dinode *sip; 10291 struct inoref *inoref; 10292 struct ufsmount *ump; 10293 struct fs *fs; 10294 ufs_lbn_t i; 10295 #ifdef INVARIANTS 10296 ufs_lbn_t prevlbn = 0; 10297 #endif 10298 int deplist; 10299 10300 if (inodedep->id_state & IOSTARTED) 10301 panic("initiate_write_inodeblock_ufs2: already started"); 10302 inodedep->id_state |= IOSTARTED; 10303 fs = inodedep->id_fs; 10304 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10305 LOCK_OWNED(ump); 10306 dp = (struct ufs2_dinode *)bp->b_data + 10307 ino_to_fsbo(fs, inodedep->id_ino); 10308 10309 /* 10310 * If we're on the unlinked list but have not yet written our 10311 * next pointer initialize it here. 10312 */ 10313 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10314 struct inodedep *inon; 10315 10316 inon = TAILQ_NEXT(inodedep, id_unlinked); 10317 dp->di_freelink = inon ? inon->id_ino : 0; 10318 } 10319 /* 10320 * If the bitmap is not yet written, then the allocated 10321 * inode cannot be written to disk. 10322 */ 10323 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10324 if (inodedep->id_savedino2 != NULL) 10325 panic("initiate_write_inodeblock_ufs2: I/O underway"); 10326 FREE_LOCK(ump); 10327 sip = malloc(sizeof(struct ufs2_dinode), 10328 M_SAVEDINO, M_SOFTDEP_FLAGS); 10329 ACQUIRE_LOCK(ump); 10330 inodedep->id_savedino2 = sip; 10331 *inodedep->id_savedino2 = *dp; 10332 bzero((caddr_t)dp, sizeof(struct ufs2_dinode)); 10333 dp->di_gen = inodedep->id_savedino2->di_gen; 10334 dp->di_freelink = inodedep->id_savedino2->di_freelink; 10335 return; 10336 } 10337 /* 10338 * If no dependencies, then there is nothing to roll back. 10339 */ 10340 inodedep->id_savedsize = dp->di_size; 10341 inodedep->id_savedextsize = dp->di_extsize; 10342 inodedep->id_savednlink = dp->di_nlink; 10343 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10344 TAILQ_EMPTY(&inodedep->id_extupdt) && 10345 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10346 return; 10347 /* 10348 * Revert the link count to that of the first unwritten journal entry. 10349 */ 10350 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10351 if (inoref) 10352 dp->di_nlink = inoref->if_nlink; 10353 10354 /* 10355 * Set the ext data dependencies to busy. 10356 */ 10357 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10358 adp = TAILQ_NEXT(adp, ad_next)) { 10359 #ifdef INVARIANTS 10360 if (deplist != 0 && prevlbn >= adp->ad_offset) 10361 panic("softdep_write_inodeblock: lbn order"); 10362 prevlbn = adp->ad_offset; 10363 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno) 10364 panic("%s: direct pointer #%jd mismatch %jd != %jd", 10365 "softdep_write_inodeblock", 10366 (intmax_t)adp->ad_offset, 10367 (intmax_t)dp->di_extb[adp->ad_offset], 10368 (intmax_t)adp->ad_newblkno); 10369 deplist |= 1 << adp->ad_offset; 10370 if ((adp->ad_state & ATTACHED) == 0) 10371 panic("softdep_write_inodeblock: Unknown state 0x%x", 10372 adp->ad_state); 10373 #endif /* INVARIANTS */ 10374 adp->ad_state &= ~ATTACHED; 10375 adp->ad_state |= UNDONE; 10376 } 10377 /* 10378 * The on-disk inode cannot claim to be any larger than the last 10379 * fragment that has been written. Otherwise, the on-disk inode 10380 * might have fragments that were not the last block in the ext 10381 * data which would corrupt the filesystem. 10382 */ 10383 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10384 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10385 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno; 10386 /* keep going until hitting a rollback to a frag */ 10387 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10388 continue; 10389 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10390 for (i = adp->ad_offset + 1; i < NXADDR; i++) { 10391 #ifdef INVARIANTS 10392 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0) 10393 panic("softdep_write_inodeblock: lost dep1"); 10394 #endif /* INVARIANTS */ 10395 dp->di_extb[i] = 0; 10396 } 10397 lastadp = NULL; 10398 break; 10399 } 10400 /* 10401 * If we have zero'ed out the last allocated block of the ext 10402 * data, roll back the size to the last currently allocated block. 10403 * We know that this last allocated block is a full-sized as 10404 * we already checked for fragments in the loop above. 10405 */ 10406 if (lastadp != NULL && 10407 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10408 for (i = lastadp->ad_offset; i >= 0; i--) 10409 if (dp->di_extb[i] != 0) 10410 break; 10411 dp->di_extsize = (i + 1) * fs->fs_bsize; 10412 } 10413 /* 10414 * Set the file data dependencies to busy. 10415 */ 10416 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10417 adp = TAILQ_NEXT(adp, ad_next)) { 10418 #ifdef INVARIANTS 10419 if (deplist != 0 && prevlbn >= adp->ad_offset) 10420 panic("softdep_write_inodeblock: lbn order"); 10421 if ((adp->ad_state & ATTACHED) == 0) 10422 panic("inodedep %p and adp %p not attached", inodedep, adp); 10423 prevlbn = adp->ad_offset; 10424 if (adp->ad_offset < NDADDR && 10425 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10426 panic("%s: direct pointer #%jd mismatch %jd != %jd", 10427 "softdep_write_inodeblock", 10428 (intmax_t)adp->ad_offset, 10429 (intmax_t)dp->di_db[adp->ad_offset], 10430 (intmax_t)adp->ad_newblkno); 10431 if (adp->ad_offset >= NDADDR && 10432 dp->di_ib[adp->ad_offset - NDADDR] != adp->ad_newblkno) 10433 panic("%s indirect pointer #%jd mismatch %jd != %jd", 10434 "softdep_write_inodeblock:", 10435 (intmax_t)adp->ad_offset - NDADDR, 10436 (intmax_t)dp->di_ib[adp->ad_offset - NDADDR], 10437 (intmax_t)adp->ad_newblkno); 10438 deplist |= 1 << adp->ad_offset; 10439 if ((adp->ad_state & ATTACHED) == 0) 10440 panic("softdep_write_inodeblock: Unknown state 0x%x", 10441 adp->ad_state); 10442 #endif /* INVARIANTS */ 10443 adp->ad_state &= ~ATTACHED; 10444 adp->ad_state |= UNDONE; 10445 } 10446 /* 10447 * The on-disk inode cannot claim to be any larger than the last 10448 * fragment that has been written. Otherwise, the on-disk inode 10449 * might have fragments that were not the last block in the file 10450 * which would corrupt the filesystem. 10451 */ 10452 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10453 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10454 if (adp->ad_offset >= NDADDR) 10455 break; 10456 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10457 /* keep going until hitting a rollback to a frag */ 10458 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10459 continue; 10460 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10461 for (i = adp->ad_offset + 1; i < NDADDR; i++) { 10462 #ifdef INVARIANTS 10463 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10464 panic("softdep_write_inodeblock: lost dep2"); 10465 #endif /* INVARIANTS */ 10466 dp->di_db[i] = 0; 10467 } 10468 for (i = 0; i < NIADDR; i++) { 10469 #ifdef INVARIANTS 10470 if (dp->di_ib[i] != 0 && 10471 (deplist & ((1 << NDADDR) << i)) == 0) 10472 panic("softdep_write_inodeblock: lost dep3"); 10473 #endif /* INVARIANTS */ 10474 dp->di_ib[i] = 0; 10475 } 10476 return; 10477 } 10478 /* 10479 * If we have zero'ed out the last allocated block of the file, 10480 * roll back the size to the last currently allocated block. 10481 * We know that this last allocated block is a full-sized as 10482 * we already checked for fragments in the loop above. 10483 */ 10484 if (lastadp != NULL && 10485 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10486 for (i = lastadp->ad_offset; i >= 0; i--) 10487 if (dp->di_db[i] != 0) 10488 break; 10489 dp->di_size = (i + 1) * fs->fs_bsize; 10490 } 10491 /* 10492 * The only dependencies are for indirect blocks. 10493 * 10494 * The file size for indirect block additions is not guaranteed. 10495 * Such a guarantee would be non-trivial to achieve. The conventional 10496 * synchronous write implementation also does not make this guarantee. 10497 * Fsck should catch and fix discrepancies. Arguably, the file size 10498 * can be over-estimated without destroying integrity when the file 10499 * moves into the indirect blocks (i.e., is large). If we want to 10500 * postpone fsck, we are stuck with this argument. 10501 */ 10502 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10503 dp->di_ib[adp->ad_offset - NDADDR] = 0; 10504 } 10505 10506 /* 10507 * Cancel an indirdep as a result of truncation. Release all of the 10508 * children allocindirs and place their journal work on the appropriate 10509 * list. 10510 */ 10511 static void 10512 cancel_indirdep(indirdep, bp, freeblks) 10513 struct indirdep *indirdep; 10514 struct buf *bp; 10515 struct freeblks *freeblks; 10516 { 10517 struct allocindir *aip; 10518 10519 /* 10520 * None of the indirect pointers will ever be visible, 10521 * so they can simply be tossed. GOINGAWAY ensures 10522 * that allocated pointers will be saved in the buffer 10523 * cache until they are freed. Note that they will 10524 * only be able to be found by their physical address 10525 * since the inode mapping the logical address will 10526 * be gone. The save buffer used for the safe copy 10527 * was allocated in setup_allocindir_phase2 using 10528 * the physical address so it could be used for this 10529 * purpose. Hence we swap the safe copy with the real 10530 * copy, allowing the safe copy to be freed and holding 10531 * on to the real copy for later use in indir_trunc. 10532 */ 10533 if (indirdep->ir_state & GOINGAWAY) 10534 panic("cancel_indirdep: already gone"); 10535 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 10536 indirdep->ir_state |= DEPCOMPLETE; 10537 LIST_REMOVE(indirdep, ir_next); 10538 } 10539 indirdep->ir_state |= GOINGAWAY; 10540 /* 10541 * Pass in bp for blocks still have journal writes 10542 * pending so we can cancel them on their own. 10543 */ 10544 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != NULL) 10545 cancel_allocindir(aip, bp, freeblks, 0); 10546 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) 10547 cancel_allocindir(aip, NULL, freeblks, 0); 10548 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) 10549 cancel_allocindir(aip, NULL, freeblks, 0); 10550 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) 10551 cancel_allocindir(aip, NULL, freeblks, 0); 10552 /* 10553 * If there are pending partial truncations we need to keep the 10554 * old block copy around until they complete. This is because 10555 * the current b_data is not a perfect superset of the available 10556 * blocks. 10557 */ 10558 if (TAILQ_EMPTY(&indirdep->ir_trunc)) 10559 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount); 10560 else 10561 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10562 WORKLIST_REMOVE(&indirdep->ir_list); 10563 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list); 10564 indirdep->ir_bp = NULL; 10565 indirdep->ir_freeblks = freeblks; 10566 } 10567 10568 /* 10569 * Free an indirdep once it no longer has new pointers to track. 10570 */ 10571 static void 10572 free_indirdep(indirdep) 10573 struct indirdep *indirdep; 10574 { 10575 10576 KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc), 10577 ("free_indirdep: Indir trunc list not empty.")); 10578 KASSERT(LIST_EMPTY(&indirdep->ir_completehd), 10579 ("free_indirdep: Complete head not empty.")); 10580 KASSERT(LIST_EMPTY(&indirdep->ir_writehd), 10581 ("free_indirdep: write head not empty.")); 10582 KASSERT(LIST_EMPTY(&indirdep->ir_donehd), 10583 ("free_indirdep: done head not empty.")); 10584 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd), 10585 ("free_indirdep: deplist head not empty.")); 10586 KASSERT((indirdep->ir_state & DEPCOMPLETE), 10587 ("free_indirdep: %p still on newblk list.", indirdep)); 10588 KASSERT(indirdep->ir_saveddata == NULL, 10589 ("free_indirdep: %p still has saved data.", indirdep)); 10590 if (indirdep->ir_state & ONWORKLIST) 10591 WORKLIST_REMOVE(&indirdep->ir_list); 10592 WORKITEM_FREE(indirdep, D_INDIRDEP); 10593 } 10594 10595 /* 10596 * Called before a write to an indirdep. This routine is responsible for 10597 * rolling back pointers to a safe state which includes only those 10598 * allocindirs which have been completed. 10599 */ 10600 static void 10601 initiate_write_indirdep(indirdep, bp) 10602 struct indirdep *indirdep; 10603 struct buf *bp; 10604 { 10605 struct ufsmount *ump; 10606 10607 indirdep->ir_state |= IOSTARTED; 10608 if (indirdep->ir_state & GOINGAWAY) 10609 panic("disk_io_initiation: indirdep gone"); 10610 /* 10611 * If there are no remaining dependencies, this will be writing 10612 * the real pointers. 10613 */ 10614 if (LIST_EMPTY(&indirdep->ir_deplisthd) && 10615 TAILQ_EMPTY(&indirdep->ir_trunc)) 10616 return; 10617 /* 10618 * Replace up-to-date version with safe version. 10619 */ 10620 if (indirdep->ir_saveddata == NULL) { 10621 ump = VFSTOUFS(indirdep->ir_list.wk_mp); 10622 LOCK_OWNED(ump); 10623 FREE_LOCK(ump); 10624 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 10625 M_SOFTDEP_FLAGS); 10626 ACQUIRE_LOCK(ump); 10627 } 10628 indirdep->ir_state &= ~ATTACHED; 10629 indirdep->ir_state |= UNDONE; 10630 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10631 bcopy(indirdep->ir_savebp->b_data, bp->b_data, 10632 bp->b_bcount); 10633 } 10634 10635 /* 10636 * Called when an inode has been cleared in a cg bitmap. This finally 10637 * eliminates any canceled jaddrefs 10638 */ 10639 void 10640 softdep_setup_inofree(mp, bp, ino, wkhd) 10641 struct mount *mp; 10642 struct buf *bp; 10643 ino_t ino; 10644 struct workhead *wkhd; 10645 { 10646 struct worklist *wk, *wkn; 10647 struct inodedep *inodedep; 10648 struct ufsmount *ump; 10649 uint8_t *inosused; 10650 struct cg *cgp; 10651 struct fs *fs; 10652 10653 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 10654 ("softdep_setup_inofree called on non-softdep filesystem")); 10655 ump = VFSTOUFS(mp); 10656 ACQUIRE_LOCK(ump); 10657 fs = ump->um_fs; 10658 cgp = (struct cg *)bp->b_data; 10659 inosused = cg_inosused(cgp); 10660 if (isset(inosused, ino % fs->fs_ipg)) 10661 panic("softdep_setup_inofree: inode %ju not freed.", 10662 (uintmax_t)ino); 10663 if (inodedep_lookup(mp, ino, 0, &inodedep)) 10664 panic("softdep_setup_inofree: ino %ju has existing inodedep %p", 10665 (uintmax_t)ino, inodedep); 10666 if (wkhd) { 10667 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) { 10668 if (wk->wk_type != D_JADDREF) 10669 continue; 10670 WORKLIST_REMOVE(wk); 10671 /* 10672 * We can free immediately even if the jaddref 10673 * isn't attached in a background write as now 10674 * the bitmaps are reconciled. 10675 */ 10676 wk->wk_state |= COMPLETE | ATTACHED; 10677 free_jaddref(WK_JADDREF(wk)); 10678 } 10679 jwork_move(&bp->b_dep, wkhd); 10680 } 10681 FREE_LOCK(ump); 10682 } 10683 10684 10685 /* 10686 * Called via ffs_blkfree() after a set of frags has been cleared from a cg 10687 * map. Any dependencies waiting for the write to clear are added to the 10688 * buf's list and any jnewblks that are being canceled are discarded 10689 * immediately. 10690 */ 10691 void 10692 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 10693 struct mount *mp; 10694 struct buf *bp; 10695 ufs2_daddr_t blkno; 10696 int frags; 10697 struct workhead *wkhd; 10698 { 10699 struct bmsafemap *bmsafemap; 10700 struct jnewblk *jnewblk; 10701 struct ufsmount *ump; 10702 struct worklist *wk; 10703 struct fs *fs; 10704 #ifdef SUJ_DEBUG 10705 uint8_t *blksfree; 10706 struct cg *cgp; 10707 ufs2_daddr_t jstart; 10708 ufs2_daddr_t jend; 10709 ufs2_daddr_t end; 10710 long bno; 10711 int i; 10712 #endif 10713 10714 CTR3(KTR_SUJ, 10715 "softdep_setup_blkfree: blkno %jd frags %d wk head %p", 10716 blkno, frags, wkhd); 10717 10718 ump = VFSTOUFS(mp); 10719 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 10720 ("softdep_setup_blkfree called on non-softdep filesystem")); 10721 ACQUIRE_LOCK(ump); 10722 /* Lookup the bmsafemap so we track when it is dirty. */ 10723 fs = ump->um_fs; 10724 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 10725 /* 10726 * Detach any jnewblks which have been canceled. They must linger 10727 * until the bitmap is cleared again by ffs_blkfree() to prevent 10728 * an unjournaled allocation from hitting the disk. 10729 */ 10730 if (wkhd) { 10731 while ((wk = LIST_FIRST(wkhd)) != NULL) { 10732 CTR2(KTR_SUJ, 10733 "softdep_setup_blkfree: blkno %jd wk type %d", 10734 blkno, wk->wk_type); 10735 WORKLIST_REMOVE(wk); 10736 if (wk->wk_type != D_JNEWBLK) { 10737 WORKLIST_INSERT(&bmsafemap->sm_freehd, wk); 10738 continue; 10739 } 10740 jnewblk = WK_JNEWBLK(wk); 10741 KASSERT(jnewblk->jn_state & GOINGAWAY, 10742 ("softdep_setup_blkfree: jnewblk not canceled.")); 10743 #ifdef SUJ_DEBUG 10744 /* 10745 * Assert that this block is free in the bitmap 10746 * before we discard the jnewblk. 10747 */ 10748 cgp = (struct cg *)bp->b_data; 10749 blksfree = cg_blksfree(cgp); 10750 bno = dtogd(fs, jnewblk->jn_blkno); 10751 for (i = jnewblk->jn_oldfrags; 10752 i < jnewblk->jn_frags; i++) { 10753 if (isset(blksfree, bno + i)) 10754 continue; 10755 panic("softdep_setup_blkfree: not free"); 10756 } 10757 #endif 10758 /* 10759 * Even if it's not attached we can free immediately 10760 * as the new bitmap is correct. 10761 */ 10762 wk->wk_state |= COMPLETE | ATTACHED; 10763 free_jnewblk(jnewblk); 10764 } 10765 } 10766 10767 #ifdef SUJ_DEBUG 10768 /* 10769 * Assert that we are not freeing a block which has an outstanding 10770 * allocation dependency. 10771 */ 10772 fs = VFSTOUFS(mp)->um_fs; 10773 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 10774 end = blkno + frags; 10775 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 10776 /* 10777 * Don't match against blocks that will be freed when the 10778 * background write is done. 10779 */ 10780 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) == 10781 (COMPLETE | DEPCOMPLETE)) 10782 continue; 10783 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags; 10784 jend = jnewblk->jn_blkno + jnewblk->jn_frags; 10785 if ((blkno >= jstart && blkno < jend) || 10786 (end > jstart && end <= jend)) { 10787 printf("state 0x%X %jd - %d %d dep %p\n", 10788 jnewblk->jn_state, jnewblk->jn_blkno, 10789 jnewblk->jn_oldfrags, jnewblk->jn_frags, 10790 jnewblk->jn_dep); 10791 panic("softdep_setup_blkfree: " 10792 "%jd-%jd(%d) overlaps with %jd-%jd", 10793 blkno, end, frags, jstart, jend); 10794 } 10795 } 10796 #endif 10797 FREE_LOCK(ump); 10798 } 10799 10800 /* 10801 * Revert a block allocation when the journal record that describes it 10802 * is not yet written. 10803 */ 10804 static int 10805 jnewblk_rollback(jnewblk, fs, cgp, blksfree) 10806 struct jnewblk *jnewblk; 10807 struct fs *fs; 10808 struct cg *cgp; 10809 uint8_t *blksfree; 10810 { 10811 ufs1_daddr_t fragno; 10812 long cgbno, bbase; 10813 int frags, blk; 10814 int i; 10815 10816 frags = 0; 10817 cgbno = dtogd(fs, jnewblk->jn_blkno); 10818 /* 10819 * We have to test which frags need to be rolled back. We may 10820 * be operating on a stale copy when doing background writes. 10821 */ 10822 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) 10823 if (isclr(blksfree, cgbno + i)) 10824 frags++; 10825 if (frags == 0) 10826 return (0); 10827 /* 10828 * This is mostly ffs_blkfree() sans some validation and 10829 * superblock updates. 10830 */ 10831 if (frags == fs->fs_frag) { 10832 fragno = fragstoblks(fs, cgbno); 10833 ffs_setblock(fs, blksfree, fragno); 10834 ffs_clusteracct(fs, cgp, fragno, 1); 10835 cgp->cg_cs.cs_nbfree++; 10836 } else { 10837 cgbno += jnewblk->jn_oldfrags; 10838 bbase = cgbno - fragnum(fs, cgbno); 10839 /* Decrement the old frags. */ 10840 blk = blkmap(fs, blksfree, bbase); 10841 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 10842 /* Deallocate the fragment */ 10843 for (i = 0; i < frags; i++) 10844 setbit(blksfree, cgbno + i); 10845 cgp->cg_cs.cs_nffree += frags; 10846 /* Add back in counts associated with the new frags */ 10847 blk = blkmap(fs, blksfree, bbase); 10848 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 10849 /* If a complete block has been reassembled, account for it. */ 10850 fragno = fragstoblks(fs, bbase); 10851 if (ffs_isblock(fs, blksfree, fragno)) { 10852 cgp->cg_cs.cs_nffree -= fs->fs_frag; 10853 ffs_clusteracct(fs, cgp, fragno, 1); 10854 cgp->cg_cs.cs_nbfree++; 10855 } 10856 } 10857 stat_jnewblk++; 10858 jnewblk->jn_state &= ~ATTACHED; 10859 jnewblk->jn_state |= UNDONE; 10860 10861 return (frags); 10862 } 10863 10864 static void 10865 initiate_write_bmsafemap(bmsafemap, bp) 10866 struct bmsafemap *bmsafemap; 10867 struct buf *bp; /* The cg block. */ 10868 { 10869 struct jaddref *jaddref; 10870 struct jnewblk *jnewblk; 10871 uint8_t *inosused; 10872 uint8_t *blksfree; 10873 struct cg *cgp; 10874 struct fs *fs; 10875 ino_t ino; 10876 10877 if (bmsafemap->sm_state & IOSTARTED) 10878 return; 10879 bmsafemap->sm_state |= IOSTARTED; 10880 /* 10881 * Clear any inode allocations which are pending journal writes. 10882 */ 10883 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) { 10884 cgp = (struct cg *)bp->b_data; 10885 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 10886 inosused = cg_inosused(cgp); 10887 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) { 10888 ino = jaddref->ja_ino % fs->fs_ipg; 10889 if (isset(inosused, ino)) { 10890 if ((jaddref->ja_mode & IFMT) == IFDIR) 10891 cgp->cg_cs.cs_ndir--; 10892 cgp->cg_cs.cs_nifree++; 10893 clrbit(inosused, ino); 10894 jaddref->ja_state &= ~ATTACHED; 10895 jaddref->ja_state |= UNDONE; 10896 stat_jaddref++; 10897 } else 10898 panic("initiate_write_bmsafemap: inode %ju " 10899 "marked free", (uintmax_t)jaddref->ja_ino); 10900 } 10901 } 10902 /* 10903 * Clear any block allocations which are pending journal writes. 10904 */ 10905 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 10906 cgp = (struct cg *)bp->b_data; 10907 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 10908 blksfree = cg_blksfree(cgp); 10909 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 10910 if (jnewblk_rollback(jnewblk, fs, cgp, blksfree)) 10911 continue; 10912 panic("initiate_write_bmsafemap: block %jd " 10913 "marked free", jnewblk->jn_blkno); 10914 } 10915 } 10916 /* 10917 * Move allocation lists to the written lists so they can be 10918 * cleared once the block write is complete. 10919 */ 10920 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr, 10921 inodedep, id_deps); 10922 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 10923 newblk, nb_deps); 10924 LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist, 10925 wk_list); 10926 } 10927 10928 /* 10929 * This routine is called during the completion interrupt 10930 * service routine for a disk write (from the procedure called 10931 * by the device driver to inform the filesystem caches of 10932 * a request completion). It should be called early in this 10933 * procedure, before the block is made available to other 10934 * processes or other routines are called. 10935 * 10936 */ 10937 static void 10938 softdep_disk_write_complete(bp) 10939 struct buf *bp; /* describes the completed disk write */ 10940 { 10941 struct worklist *wk; 10942 struct worklist *owk; 10943 struct ufsmount *ump; 10944 struct workhead reattach; 10945 struct freeblks *freeblks; 10946 struct buf *sbp; 10947 10948 /* 10949 * If an error occurred while doing the write, then the data 10950 * has not hit the disk and the dependencies cannot be unrolled. 10951 */ 10952 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) 10953 return; 10954 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL) 10955 return; 10956 ump = VFSTOUFS(wk->wk_mp); 10957 LIST_INIT(&reattach); 10958 /* 10959 * This lock must not be released anywhere in this code segment. 10960 */ 10961 sbp = NULL; 10962 owk = NULL; 10963 ACQUIRE_LOCK(ump); 10964 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) { 10965 WORKLIST_REMOVE(wk); 10966 atomic_add_long(&dep_write[wk->wk_type], 1); 10967 if (wk == owk) 10968 panic("duplicate worklist: %p\n", wk); 10969 owk = wk; 10970 switch (wk->wk_type) { 10971 10972 case D_PAGEDEP: 10973 if (handle_written_filepage(WK_PAGEDEP(wk), bp)) 10974 WORKLIST_INSERT(&reattach, wk); 10975 continue; 10976 10977 case D_INODEDEP: 10978 if (handle_written_inodeblock(WK_INODEDEP(wk), bp)) 10979 WORKLIST_INSERT(&reattach, wk); 10980 continue; 10981 10982 case D_BMSAFEMAP: 10983 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp)) 10984 WORKLIST_INSERT(&reattach, wk); 10985 continue; 10986 10987 case D_MKDIR: 10988 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 10989 continue; 10990 10991 case D_ALLOCDIRECT: 10992 wk->wk_state |= COMPLETE; 10993 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL); 10994 continue; 10995 10996 case D_ALLOCINDIR: 10997 wk->wk_state |= COMPLETE; 10998 handle_allocindir_partdone(WK_ALLOCINDIR(wk)); 10999 continue; 11000 11001 case D_INDIRDEP: 11002 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp)) 11003 WORKLIST_INSERT(&reattach, wk); 11004 continue; 11005 11006 case D_FREEBLKS: 11007 wk->wk_state |= COMPLETE; 11008 freeblks = WK_FREEBLKS(wk); 11009 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE && 11010 LIST_EMPTY(&freeblks->fb_jblkdephd)) 11011 add_to_worklist(wk, WK_NODELAY); 11012 continue; 11013 11014 case D_FREEWORK: 11015 handle_written_freework(WK_FREEWORK(wk)); 11016 break; 11017 11018 case D_JSEGDEP: 11019 free_jsegdep(WK_JSEGDEP(wk)); 11020 continue; 11021 11022 case D_JSEG: 11023 handle_written_jseg(WK_JSEG(wk), bp); 11024 continue; 11025 11026 case D_SBDEP: 11027 if (handle_written_sbdep(WK_SBDEP(wk), bp)) 11028 WORKLIST_INSERT(&reattach, wk); 11029 continue; 11030 11031 case D_FREEDEP: 11032 free_freedep(WK_FREEDEP(wk)); 11033 continue; 11034 11035 default: 11036 panic("handle_disk_write_complete: Unknown type %s", 11037 TYPENAME(wk->wk_type)); 11038 /* NOTREACHED */ 11039 } 11040 } 11041 /* 11042 * Reattach any requests that must be redone. 11043 */ 11044 while ((wk = LIST_FIRST(&reattach)) != NULL) { 11045 WORKLIST_REMOVE(wk); 11046 WORKLIST_INSERT(&bp->b_dep, wk); 11047 } 11048 FREE_LOCK(ump); 11049 if (sbp) 11050 brelse(sbp); 11051 } 11052 11053 /* 11054 * Called from within softdep_disk_write_complete above. Note that 11055 * this routine is always called from interrupt level with further 11056 * splbio interrupts blocked. 11057 */ 11058 static void 11059 handle_allocdirect_partdone(adp, wkhd) 11060 struct allocdirect *adp; /* the completed allocdirect */ 11061 struct workhead *wkhd; /* Work to do when inode is writtne. */ 11062 { 11063 struct allocdirectlst *listhead; 11064 struct allocdirect *listadp; 11065 struct inodedep *inodedep; 11066 long bsize; 11067 11068 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11069 return; 11070 /* 11071 * The on-disk inode cannot claim to be any larger than the last 11072 * fragment that has been written. Otherwise, the on-disk inode 11073 * might have fragments that were not the last block in the file 11074 * which would corrupt the filesystem. Thus, we cannot free any 11075 * allocdirects after one whose ad_oldblkno claims a fragment as 11076 * these blocks must be rolled back to zero before writing the inode. 11077 * We check the currently active set of allocdirects in id_inoupdt 11078 * or id_extupdt as appropriate. 11079 */ 11080 inodedep = adp->ad_inodedep; 11081 bsize = inodedep->id_fs->fs_bsize; 11082 if (adp->ad_state & EXTDATA) 11083 listhead = &inodedep->id_extupdt; 11084 else 11085 listhead = &inodedep->id_inoupdt; 11086 TAILQ_FOREACH(listadp, listhead, ad_next) { 11087 /* found our block */ 11088 if (listadp == adp) 11089 break; 11090 /* continue if ad_oldlbn is not a fragment */ 11091 if (listadp->ad_oldsize == 0 || 11092 listadp->ad_oldsize == bsize) 11093 continue; 11094 /* hit a fragment */ 11095 return; 11096 } 11097 /* 11098 * If we have reached the end of the current list without 11099 * finding the just finished dependency, then it must be 11100 * on the future dependency list. Future dependencies cannot 11101 * be freed until they are moved to the current list. 11102 */ 11103 if (listadp == NULL) { 11104 #ifdef DEBUG 11105 if (adp->ad_state & EXTDATA) 11106 listhead = &inodedep->id_newextupdt; 11107 else 11108 listhead = &inodedep->id_newinoupdt; 11109 TAILQ_FOREACH(listadp, listhead, ad_next) 11110 /* found our block */ 11111 if (listadp == adp) 11112 break; 11113 if (listadp == NULL) 11114 panic("handle_allocdirect_partdone: lost dep"); 11115 #endif /* DEBUG */ 11116 return; 11117 } 11118 /* 11119 * If we have found the just finished dependency, then queue 11120 * it along with anything that follows it that is complete. 11121 * Since the pointer has not yet been written in the inode 11122 * as the dependency prevents it, place the allocdirect on the 11123 * bufwait list where it will be freed once the pointer is 11124 * valid. 11125 */ 11126 if (wkhd == NULL) 11127 wkhd = &inodedep->id_bufwait; 11128 for (; adp; adp = listadp) { 11129 listadp = TAILQ_NEXT(adp, ad_next); 11130 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11131 return; 11132 TAILQ_REMOVE(listhead, adp, ad_next); 11133 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list); 11134 } 11135 } 11136 11137 /* 11138 * Called from within softdep_disk_write_complete above. This routine 11139 * completes successfully written allocindirs. 11140 */ 11141 static void 11142 handle_allocindir_partdone(aip) 11143 struct allocindir *aip; /* the completed allocindir */ 11144 { 11145 struct indirdep *indirdep; 11146 11147 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE) 11148 return; 11149 indirdep = aip->ai_indirdep; 11150 LIST_REMOVE(aip, ai_next); 11151 /* 11152 * Don't set a pointer while the buffer is undergoing IO or while 11153 * we have active truncations. 11154 */ 11155 if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) { 11156 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next); 11157 return; 11158 } 11159 if (indirdep->ir_state & UFS1FMT) 11160 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11161 aip->ai_newblkno; 11162 else 11163 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11164 aip->ai_newblkno; 11165 /* 11166 * Await the pointer write before freeing the allocindir. 11167 */ 11168 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next); 11169 } 11170 11171 /* 11172 * Release segments held on a jwork list. 11173 */ 11174 static void 11175 handle_jwork(wkhd) 11176 struct workhead *wkhd; 11177 { 11178 struct worklist *wk; 11179 11180 while ((wk = LIST_FIRST(wkhd)) != NULL) { 11181 WORKLIST_REMOVE(wk); 11182 switch (wk->wk_type) { 11183 case D_JSEGDEP: 11184 free_jsegdep(WK_JSEGDEP(wk)); 11185 continue; 11186 case D_FREEDEP: 11187 free_freedep(WK_FREEDEP(wk)); 11188 continue; 11189 case D_FREEFRAG: 11190 rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep)); 11191 WORKITEM_FREE(wk, D_FREEFRAG); 11192 continue; 11193 case D_FREEWORK: 11194 handle_written_freework(WK_FREEWORK(wk)); 11195 continue; 11196 default: 11197 panic("handle_jwork: Unknown type %s\n", 11198 TYPENAME(wk->wk_type)); 11199 } 11200 } 11201 } 11202 11203 /* 11204 * Handle the bufwait list on an inode when it is safe to release items 11205 * held there. This normally happens after an inode block is written but 11206 * may be delayed and handled later if there are pending journal items that 11207 * are not yet safe to be released. 11208 */ 11209 static struct freefile * 11210 handle_bufwait(inodedep, refhd) 11211 struct inodedep *inodedep; 11212 struct workhead *refhd; 11213 { 11214 struct jaddref *jaddref; 11215 struct freefile *freefile; 11216 struct worklist *wk; 11217 11218 freefile = NULL; 11219 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) { 11220 WORKLIST_REMOVE(wk); 11221 switch (wk->wk_type) { 11222 case D_FREEFILE: 11223 /* 11224 * We defer adding freefile to the worklist 11225 * until all other additions have been made to 11226 * ensure that it will be done after all the 11227 * old blocks have been freed. 11228 */ 11229 if (freefile != NULL) 11230 panic("handle_bufwait: freefile"); 11231 freefile = WK_FREEFILE(wk); 11232 continue; 11233 11234 case D_MKDIR: 11235 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT); 11236 continue; 11237 11238 case D_DIRADD: 11239 diradd_inode_written(WK_DIRADD(wk), inodedep); 11240 continue; 11241 11242 case D_FREEFRAG: 11243 wk->wk_state |= COMPLETE; 11244 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE) 11245 add_to_worklist(wk, 0); 11246 continue; 11247 11248 case D_DIRREM: 11249 wk->wk_state |= COMPLETE; 11250 add_to_worklist(wk, 0); 11251 continue; 11252 11253 case D_ALLOCDIRECT: 11254 case D_ALLOCINDIR: 11255 free_newblk(WK_NEWBLK(wk)); 11256 continue; 11257 11258 case D_JNEWBLK: 11259 wk->wk_state |= COMPLETE; 11260 free_jnewblk(WK_JNEWBLK(wk)); 11261 continue; 11262 11263 /* 11264 * Save freed journal segments and add references on 11265 * the supplied list which will delay their release 11266 * until the cg bitmap is cleared on disk. 11267 */ 11268 case D_JSEGDEP: 11269 if (refhd == NULL) 11270 free_jsegdep(WK_JSEGDEP(wk)); 11271 else 11272 WORKLIST_INSERT(refhd, wk); 11273 continue; 11274 11275 case D_JADDREF: 11276 jaddref = WK_JADDREF(wk); 11277 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 11278 if_deps); 11279 /* 11280 * Transfer any jaddrefs to the list to be freed with 11281 * the bitmap if we're handling a removed file. 11282 */ 11283 if (refhd == NULL) { 11284 wk->wk_state |= COMPLETE; 11285 free_jaddref(jaddref); 11286 } else 11287 WORKLIST_INSERT(refhd, wk); 11288 continue; 11289 11290 default: 11291 panic("handle_bufwait: Unknown type %p(%s)", 11292 wk, TYPENAME(wk->wk_type)); 11293 /* NOTREACHED */ 11294 } 11295 } 11296 return (freefile); 11297 } 11298 /* 11299 * Called from within softdep_disk_write_complete above to restore 11300 * in-memory inode block contents to their most up-to-date state. Note 11301 * that this routine is always called from interrupt level with further 11302 * splbio interrupts blocked. 11303 */ 11304 static int 11305 handle_written_inodeblock(inodedep, bp) 11306 struct inodedep *inodedep; 11307 struct buf *bp; /* buffer containing the inode block */ 11308 { 11309 struct freefile *freefile; 11310 struct allocdirect *adp, *nextadp; 11311 struct ufs1_dinode *dp1 = NULL; 11312 struct ufs2_dinode *dp2 = NULL; 11313 struct workhead wkhd; 11314 int hadchanges, fstype; 11315 ino_t freelink; 11316 11317 LIST_INIT(&wkhd); 11318 hadchanges = 0; 11319 freefile = NULL; 11320 if ((inodedep->id_state & IOSTARTED) == 0) 11321 panic("handle_written_inodeblock: not started"); 11322 inodedep->id_state &= ~IOSTARTED; 11323 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) { 11324 fstype = UFS1; 11325 dp1 = (struct ufs1_dinode *)bp->b_data + 11326 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11327 freelink = dp1->di_freelink; 11328 } else { 11329 fstype = UFS2; 11330 dp2 = (struct ufs2_dinode *)bp->b_data + 11331 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11332 freelink = dp2->di_freelink; 11333 } 11334 /* 11335 * Leave this inodeblock dirty until it's in the list. 11336 */ 11337 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED) { 11338 struct inodedep *inon; 11339 11340 inon = TAILQ_NEXT(inodedep, id_unlinked); 11341 if ((inon == NULL && freelink == 0) || 11342 (inon && inon->id_ino == freelink)) { 11343 if (inon) 11344 inon->id_state |= UNLINKPREV; 11345 inodedep->id_state |= UNLINKNEXT; 11346 } 11347 hadchanges = 1; 11348 } 11349 /* 11350 * If we had to rollback the inode allocation because of 11351 * bitmaps being incomplete, then simply restore it. 11352 * Keep the block dirty so that it will not be reclaimed until 11353 * all associated dependencies have been cleared and the 11354 * corresponding updates written to disk. 11355 */ 11356 if (inodedep->id_savedino1 != NULL) { 11357 hadchanges = 1; 11358 if (fstype == UFS1) 11359 *dp1 = *inodedep->id_savedino1; 11360 else 11361 *dp2 = *inodedep->id_savedino2; 11362 free(inodedep->id_savedino1, M_SAVEDINO); 11363 inodedep->id_savedino1 = NULL; 11364 if ((bp->b_flags & B_DELWRI) == 0) 11365 stat_inode_bitmap++; 11366 bdirty(bp); 11367 /* 11368 * If the inode is clear here and GOINGAWAY it will never 11369 * be written. Process the bufwait and clear any pending 11370 * work which may include the freefile. 11371 */ 11372 if (inodedep->id_state & GOINGAWAY) 11373 goto bufwait; 11374 return (1); 11375 } 11376 inodedep->id_state |= COMPLETE; 11377 /* 11378 * Roll forward anything that had to be rolled back before 11379 * the inode could be updated. 11380 */ 11381 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) { 11382 nextadp = TAILQ_NEXT(adp, ad_next); 11383 if (adp->ad_state & ATTACHED) 11384 panic("handle_written_inodeblock: new entry"); 11385 if (fstype == UFS1) { 11386 if (adp->ad_offset < NDADDR) { 11387 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11388 panic("%s %s #%jd mismatch %d != %jd", 11389 "handle_written_inodeblock:", 11390 "direct pointer", 11391 (intmax_t)adp->ad_offset, 11392 dp1->di_db[adp->ad_offset], 11393 (intmax_t)adp->ad_oldblkno); 11394 dp1->di_db[adp->ad_offset] = adp->ad_newblkno; 11395 } else { 11396 if (dp1->di_ib[adp->ad_offset - NDADDR] != 0) 11397 panic("%s: %s #%jd allocated as %d", 11398 "handle_written_inodeblock", 11399 "indirect pointer", 11400 (intmax_t)adp->ad_offset - NDADDR, 11401 dp1->di_ib[adp->ad_offset - NDADDR]); 11402 dp1->di_ib[adp->ad_offset - NDADDR] = 11403 adp->ad_newblkno; 11404 } 11405 } else { 11406 if (adp->ad_offset < NDADDR) { 11407 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11408 panic("%s: %s #%jd %s %jd != %jd", 11409 "handle_written_inodeblock", 11410 "direct pointer", 11411 (intmax_t)adp->ad_offset, "mismatch", 11412 (intmax_t)dp2->di_db[adp->ad_offset], 11413 (intmax_t)adp->ad_oldblkno); 11414 dp2->di_db[adp->ad_offset] = adp->ad_newblkno; 11415 } else { 11416 if (dp2->di_ib[adp->ad_offset - NDADDR] != 0) 11417 panic("%s: %s #%jd allocated as %jd", 11418 "handle_written_inodeblock", 11419 "indirect pointer", 11420 (intmax_t)adp->ad_offset - NDADDR, 11421 (intmax_t) 11422 dp2->di_ib[adp->ad_offset - NDADDR]); 11423 dp2->di_ib[adp->ad_offset - NDADDR] = 11424 adp->ad_newblkno; 11425 } 11426 } 11427 adp->ad_state &= ~UNDONE; 11428 adp->ad_state |= ATTACHED; 11429 hadchanges = 1; 11430 } 11431 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) { 11432 nextadp = TAILQ_NEXT(adp, ad_next); 11433 if (adp->ad_state & ATTACHED) 11434 panic("handle_written_inodeblock: new entry"); 11435 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno) 11436 panic("%s: direct pointers #%jd %s %jd != %jd", 11437 "handle_written_inodeblock", 11438 (intmax_t)adp->ad_offset, "mismatch", 11439 (intmax_t)dp2->di_extb[adp->ad_offset], 11440 (intmax_t)adp->ad_oldblkno); 11441 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno; 11442 adp->ad_state &= ~UNDONE; 11443 adp->ad_state |= ATTACHED; 11444 hadchanges = 1; 11445 } 11446 if (hadchanges && (bp->b_flags & B_DELWRI) == 0) 11447 stat_direct_blk_ptrs++; 11448 /* 11449 * Reset the file size to its most up-to-date value. 11450 */ 11451 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1) 11452 panic("handle_written_inodeblock: bad size"); 11453 if (inodedep->id_savednlink > LINK_MAX) 11454 panic("handle_written_inodeblock: Invalid link count " 11455 "%d for inodedep %p", inodedep->id_savednlink, inodedep); 11456 if (fstype == UFS1) { 11457 if (dp1->di_nlink != inodedep->id_savednlink) { 11458 dp1->di_nlink = inodedep->id_savednlink; 11459 hadchanges = 1; 11460 } 11461 if (dp1->di_size != inodedep->id_savedsize) { 11462 dp1->di_size = inodedep->id_savedsize; 11463 hadchanges = 1; 11464 } 11465 } else { 11466 if (dp2->di_nlink != inodedep->id_savednlink) { 11467 dp2->di_nlink = inodedep->id_savednlink; 11468 hadchanges = 1; 11469 } 11470 if (dp2->di_size != inodedep->id_savedsize) { 11471 dp2->di_size = inodedep->id_savedsize; 11472 hadchanges = 1; 11473 } 11474 if (dp2->di_extsize != inodedep->id_savedextsize) { 11475 dp2->di_extsize = inodedep->id_savedextsize; 11476 hadchanges = 1; 11477 } 11478 } 11479 inodedep->id_savedsize = -1; 11480 inodedep->id_savedextsize = -1; 11481 inodedep->id_savednlink = -1; 11482 /* 11483 * If there were any rollbacks in the inode block, then it must be 11484 * marked dirty so that its will eventually get written back in 11485 * its correct form. 11486 */ 11487 if (hadchanges) 11488 bdirty(bp); 11489 bufwait: 11490 /* 11491 * Process any allocdirects that completed during the update. 11492 */ 11493 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 11494 handle_allocdirect_partdone(adp, &wkhd); 11495 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 11496 handle_allocdirect_partdone(adp, &wkhd); 11497 /* 11498 * Process deallocations that were held pending until the 11499 * inode had been written to disk. Freeing of the inode 11500 * is delayed until after all blocks have been freed to 11501 * avoid creation of new <vfsid, inum, lbn> triples 11502 * before the old ones have been deleted. Completely 11503 * unlinked inodes are not processed until the unlinked 11504 * inode list is written or the last reference is removed. 11505 */ 11506 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) { 11507 freefile = handle_bufwait(inodedep, NULL); 11508 if (freefile && !LIST_EMPTY(&wkhd)) { 11509 WORKLIST_INSERT(&wkhd, &freefile->fx_list); 11510 freefile = NULL; 11511 } 11512 } 11513 /* 11514 * Move rolled forward dependency completions to the bufwait list 11515 * now that those that were already written have been processed. 11516 */ 11517 if (!LIST_EMPTY(&wkhd) && hadchanges == 0) 11518 panic("handle_written_inodeblock: bufwait but no changes"); 11519 jwork_move(&inodedep->id_bufwait, &wkhd); 11520 11521 if (freefile != NULL) { 11522 /* 11523 * If the inode is goingaway it was never written. Fake up 11524 * the state here so free_inodedep() can succeed. 11525 */ 11526 if (inodedep->id_state & GOINGAWAY) 11527 inodedep->id_state |= COMPLETE | DEPCOMPLETE; 11528 if (free_inodedep(inodedep) == 0) 11529 panic("handle_written_inodeblock: live inodedep %p", 11530 inodedep); 11531 add_to_worklist(&freefile->fx_list, 0); 11532 return (0); 11533 } 11534 11535 /* 11536 * If no outstanding dependencies, free it. 11537 */ 11538 if (free_inodedep(inodedep) || 11539 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 && 11540 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 && 11541 TAILQ_FIRST(&inodedep->id_extupdt) == 0 && 11542 LIST_FIRST(&inodedep->id_bufwait) == 0)) 11543 return (0); 11544 return (hadchanges); 11545 } 11546 11547 static int 11548 handle_written_indirdep(indirdep, bp, bpp) 11549 struct indirdep *indirdep; 11550 struct buf *bp; 11551 struct buf **bpp; 11552 { 11553 struct allocindir *aip; 11554 struct buf *sbp; 11555 int chgs; 11556 11557 if (indirdep->ir_state & GOINGAWAY) 11558 panic("handle_written_indirdep: indirdep gone"); 11559 if ((indirdep->ir_state & IOSTARTED) == 0) 11560 panic("handle_written_indirdep: IO not started"); 11561 chgs = 0; 11562 /* 11563 * If there were rollbacks revert them here. 11564 */ 11565 if (indirdep->ir_saveddata) { 11566 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount); 11567 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11568 free(indirdep->ir_saveddata, M_INDIRDEP); 11569 indirdep->ir_saveddata = NULL; 11570 } 11571 chgs = 1; 11572 } 11573 indirdep->ir_state &= ~(UNDONE | IOSTARTED); 11574 indirdep->ir_state |= ATTACHED; 11575 /* 11576 * Move allocindirs with written pointers to the completehd if 11577 * the indirdep's pointer is not yet written. Otherwise 11578 * free them here. 11579 */ 11580 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) { 11581 LIST_REMOVE(aip, ai_next); 11582 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 11583 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip, 11584 ai_next); 11585 newblk_freefrag(&aip->ai_block); 11586 continue; 11587 } 11588 free_newblk(&aip->ai_block); 11589 } 11590 /* 11591 * Move allocindirs that have finished dependency processing from 11592 * the done list to the write list after updating the pointers. 11593 */ 11594 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11595 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) { 11596 handle_allocindir_partdone(aip); 11597 if (aip == LIST_FIRST(&indirdep->ir_donehd)) 11598 panic("disk_write_complete: not gone"); 11599 chgs = 1; 11600 } 11601 } 11602 /* 11603 * Preserve the indirdep if there were any changes or if it is not 11604 * yet valid on disk. 11605 */ 11606 if (chgs) { 11607 stat_indir_blk_ptrs++; 11608 bdirty(bp); 11609 return (1); 11610 } 11611 /* 11612 * If there were no changes we can discard the savedbp and detach 11613 * ourselves from the buf. We are only carrying completed pointers 11614 * in this case. 11615 */ 11616 sbp = indirdep->ir_savebp; 11617 sbp->b_flags |= B_INVAL | B_NOCACHE; 11618 indirdep->ir_savebp = NULL; 11619 indirdep->ir_bp = NULL; 11620 if (*bpp != NULL) 11621 panic("handle_written_indirdep: bp already exists."); 11622 *bpp = sbp; 11623 /* 11624 * The indirdep may not be freed until its parent points at it. 11625 */ 11626 if (indirdep->ir_state & DEPCOMPLETE) 11627 free_indirdep(indirdep); 11628 11629 return (0); 11630 } 11631 11632 /* 11633 * Process a diradd entry after its dependent inode has been written. 11634 * This routine must be called with splbio interrupts blocked. 11635 */ 11636 static void 11637 diradd_inode_written(dap, inodedep) 11638 struct diradd *dap; 11639 struct inodedep *inodedep; 11640 { 11641 11642 dap->da_state |= COMPLETE; 11643 complete_diradd(dap); 11644 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 11645 } 11646 11647 /* 11648 * Returns true if the bmsafemap will have rollbacks when written. Must only 11649 * be called with the per-filesystem lock and the buf lock on the cg held. 11650 */ 11651 static int 11652 bmsafemap_backgroundwrite(bmsafemap, bp) 11653 struct bmsafemap *bmsafemap; 11654 struct buf *bp; 11655 { 11656 int dirty; 11657 11658 LOCK_OWNED(VFSTOUFS(bmsafemap->sm_list.wk_mp)); 11659 dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) | 11660 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd); 11661 /* 11662 * If we're initiating a background write we need to process the 11663 * rollbacks as they exist now, not as they exist when IO starts. 11664 * No other consumers will look at the contents of the shadowed 11665 * buf so this is safe to do here. 11666 */ 11667 if (bp->b_xflags & BX_BKGRDMARKER) 11668 initiate_write_bmsafemap(bmsafemap, bp); 11669 11670 return (dirty); 11671 } 11672 11673 /* 11674 * Re-apply an allocation when a cg write is complete. 11675 */ 11676 static int 11677 jnewblk_rollforward(jnewblk, fs, cgp, blksfree) 11678 struct jnewblk *jnewblk; 11679 struct fs *fs; 11680 struct cg *cgp; 11681 uint8_t *blksfree; 11682 { 11683 ufs1_daddr_t fragno; 11684 ufs2_daddr_t blkno; 11685 long cgbno, bbase; 11686 int frags, blk; 11687 int i; 11688 11689 frags = 0; 11690 cgbno = dtogd(fs, jnewblk->jn_blkno); 11691 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) { 11692 if (isclr(blksfree, cgbno + i)) 11693 panic("jnewblk_rollforward: re-allocated fragment"); 11694 frags++; 11695 } 11696 if (frags == fs->fs_frag) { 11697 blkno = fragstoblks(fs, cgbno); 11698 ffs_clrblock(fs, blksfree, (long)blkno); 11699 ffs_clusteracct(fs, cgp, blkno, -1); 11700 cgp->cg_cs.cs_nbfree--; 11701 } else { 11702 bbase = cgbno - fragnum(fs, cgbno); 11703 cgbno += jnewblk->jn_oldfrags; 11704 /* If a complete block had been reassembled, account for it. */ 11705 fragno = fragstoblks(fs, bbase); 11706 if (ffs_isblock(fs, blksfree, fragno)) { 11707 cgp->cg_cs.cs_nffree += fs->fs_frag; 11708 ffs_clusteracct(fs, cgp, fragno, -1); 11709 cgp->cg_cs.cs_nbfree--; 11710 } 11711 /* Decrement the old frags. */ 11712 blk = blkmap(fs, blksfree, bbase); 11713 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 11714 /* Allocate the fragment */ 11715 for (i = 0; i < frags; i++) 11716 clrbit(blksfree, cgbno + i); 11717 cgp->cg_cs.cs_nffree -= frags; 11718 /* Add back in counts associated with the new frags */ 11719 blk = blkmap(fs, blksfree, bbase); 11720 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 11721 } 11722 return (frags); 11723 } 11724 11725 /* 11726 * Complete a write to a bmsafemap structure. Roll forward any bitmap 11727 * changes if it's not a background write. Set all written dependencies 11728 * to DEPCOMPLETE and free the structure if possible. 11729 */ 11730 static int 11731 handle_written_bmsafemap(bmsafemap, bp) 11732 struct bmsafemap *bmsafemap; 11733 struct buf *bp; 11734 { 11735 struct newblk *newblk; 11736 struct inodedep *inodedep; 11737 struct jaddref *jaddref, *jatmp; 11738 struct jnewblk *jnewblk, *jntmp; 11739 struct ufsmount *ump; 11740 uint8_t *inosused; 11741 uint8_t *blksfree; 11742 struct cg *cgp; 11743 struct fs *fs; 11744 ino_t ino; 11745 int foreground; 11746 int chgs; 11747 11748 if ((bmsafemap->sm_state & IOSTARTED) == 0) 11749 panic("initiate_write_bmsafemap: Not started\n"); 11750 ump = VFSTOUFS(bmsafemap->sm_list.wk_mp); 11751 chgs = 0; 11752 bmsafemap->sm_state &= ~IOSTARTED; 11753 foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0; 11754 /* 11755 * Release journal work that was waiting on the write. 11756 */ 11757 handle_jwork(&bmsafemap->sm_freewr); 11758 11759 /* 11760 * Restore unwritten inode allocation pending jaddref writes. 11761 */ 11762 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) { 11763 cgp = (struct cg *)bp->b_data; 11764 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11765 inosused = cg_inosused(cgp); 11766 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd, 11767 ja_bmdeps, jatmp) { 11768 if ((jaddref->ja_state & UNDONE) == 0) 11769 continue; 11770 ino = jaddref->ja_ino % fs->fs_ipg; 11771 if (isset(inosused, ino)) 11772 panic("handle_written_bmsafemap: " 11773 "re-allocated inode"); 11774 /* Do the roll-forward only if it's a real copy. */ 11775 if (foreground) { 11776 if ((jaddref->ja_mode & IFMT) == IFDIR) 11777 cgp->cg_cs.cs_ndir++; 11778 cgp->cg_cs.cs_nifree--; 11779 setbit(inosused, ino); 11780 chgs = 1; 11781 } 11782 jaddref->ja_state &= ~UNDONE; 11783 jaddref->ja_state |= ATTACHED; 11784 free_jaddref(jaddref); 11785 } 11786 } 11787 /* 11788 * Restore any block allocations which are pending journal writes. 11789 */ 11790 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 11791 cgp = (struct cg *)bp->b_data; 11792 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11793 blksfree = cg_blksfree(cgp); 11794 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps, 11795 jntmp) { 11796 if ((jnewblk->jn_state & UNDONE) == 0) 11797 continue; 11798 /* Do the roll-forward only if it's a real copy. */ 11799 if (foreground && 11800 jnewblk_rollforward(jnewblk, fs, cgp, blksfree)) 11801 chgs = 1; 11802 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK); 11803 jnewblk->jn_state |= ATTACHED; 11804 free_jnewblk(jnewblk); 11805 } 11806 } 11807 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) { 11808 newblk->nb_state |= DEPCOMPLETE; 11809 newblk->nb_state &= ~ONDEPLIST; 11810 newblk->nb_bmsafemap = NULL; 11811 LIST_REMOVE(newblk, nb_deps); 11812 if (newblk->nb_list.wk_type == D_ALLOCDIRECT) 11813 handle_allocdirect_partdone( 11814 WK_ALLOCDIRECT(&newblk->nb_list), NULL); 11815 else if (newblk->nb_list.wk_type == D_ALLOCINDIR) 11816 handle_allocindir_partdone( 11817 WK_ALLOCINDIR(&newblk->nb_list)); 11818 else if (newblk->nb_list.wk_type != D_NEWBLK) 11819 panic("handle_written_bmsafemap: Unexpected type: %s", 11820 TYPENAME(newblk->nb_list.wk_type)); 11821 } 11822 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) { 11823 inodedep->id_state |= DEPCOMPLETE; 11824 inodedep->id_state &= ~ONDEPLIST; 11825 LIST_REMOVE(inodedep, id_deps); 11826 inodedep->id_bmsafemap = NULL; 11827 } 11828 LIST_REMOVE(bmsafemap, sm_next); 11829 if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) && 11830 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) && 11831 LIST_EMPTY(&bmsafemap->sm_newblkhd) && 11832 LIST_EMPTY(&bmsafemap->sm_inodedephd) && 11833 LIST_EMPTY(&bmsafemap->sm_freehd)) { 11834 LIST_REMOVE(bmsafemap, sm_hash); 11835 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 11836 return (0); 11837 } 11838 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 11839 if (foreground) 11840 bdirty(bp); 11841 return (1); 11842 } 11843 11844 /* 11845 * Try to free a mkdir dependency. 11846 */ 11847 static void 11848 complete_mkdir(mkdir) 11849 struct mkdir *mkdir; 11850 { 11851 struct diradd *dap; 11852 11853 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE) 11854 return; 11855 LIST_REMOVE(mkdir, md_mkdirs); 11856 dap = mkdir->md_diradd; 11857 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 11858 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) { 11859 dap->da_state |= DEPCOMPLETE; 11860 complete_diradd(dap); 11861 } 11862 WORKITEM_FREE(mkdir, D_MKDIR); 11863 } 11864 11865 /* 11866 * Handle the completion of a mkdir dependency. 11867 */ 11868 static void 11869 handle_written_mkdir(mkdir, type) 11870 struct mkdir *mkdir; 11871 int type; 11872 { 11873 11874 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type) 11875 panic("handle_written_mkdir: bad type"); 11876 mkdir->md_state |= COMPLETE; 11877 complete_mkdir(mkdir); 11878 } 11879 11880 static int 11881 free_pagedep(pagedep) 11882 struct pagedep *pagedep; 11883 { 11884 int i; 11885 11886 if (pagedep->pd_state & NEWBLOCK) 11887 return (0); 11888 if (!LIST_EMPTY(&pagedep->pd_dirremhd)) 11889 return (0); 11890 for (i = 0; i < DAHASHSZ; i++) 11891 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i])) 11892 return (0); 11893 if (!LIST_EMPTY(&pagedep->pd_pendinghd)) 11894 return (0); 11895 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd)) 11896 return (0); 11897 if (pagedep->pd_state & ONWORKLIST) 11898 WORKLIST_REMOVE(&pagedep->pd_list); 11899 LIST_REMOVE(pagedep, pd_hash); 11900 WORKITEM_FREE(pagedep, D_PAGEDEP); 11901 11902 return (1); 11903 } 11904 11905 /* 11906 * Called from within softdep_disk_write_complete above. 11907 * A write operation was just completed. Removed inodes can 11908 * now be freed and associated block pointers may be committed. 11909 * Note that this routine is always called from interrupt level 11910 * with further splbio interrupts blocked. 11911 */ 11912 static int 11913 handle_written_filepage(pagedep, bp) 11914 struct pagedep *pagedep; 11915 struct buf *bp; /* buffer containing the written page */ 11916 { 11917 struct dirrem *dirrem; 11918 struct diradd *dap, *nextdap; 11919 struct direct *ep; 11920 int i, chgs; 11921 11922 if ((pagedep->pd_state & IOSTARTED) == 0) 11923 panic("handle_written_filepage: not started"); 11924 pagedep->pd_state &= ~IOSTARTED; 11925 /* 11926 * Process any directory removals that have been committed. 11927 */ 11928 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) { 11929 LIST_REMOVE(dirrem, dm_next); 11930 dirrem->dm_state |= COMPLETE; 11931 dirrem->dm_dirinum = pagedep->pd_ino; 11932 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 11933 ("handle_written_filepage: Journal entries not written.")); 11934 add_to_worklist(&dirrem->dm_list, 0); 11935 } 11936 /* 11937 * Free any directory additions that have been committed. 11938 * If it is a newly allocated block, we have to wait until 11939 * the on-disk directory inode claims the new block. 11940 */ 11941 if ((pagedep->pd_state & NEWBLOCK) == 0) 11942 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 11943 free_diradd(dap, NULL); 11944 /* 11945 * Uncommitted directory entries must be restored. 11946 */ 11947 for (chgs = 0, i = 0; i < DAHASHSZ; i++) { 11948 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap; 11949 dap = nextdap) { 11950 nextdap = LIST_NEXT(dap, da_pdlist); 11951 if (dap->da_state & ATTACHED) 11952 panic("handle_written_filepage: attached"); 11953 ep = (struct direct *) 11954 ((char *)bp->b_data + dap->da_offset); 11955 ep->d_ino = dap->da_newinum; 11956 dap->da_state &= ~UNDONE; 11957 dap->da_state |= ATTACHED; 11958 chgs = 1; 11959 /* 11960 * If the inode referenced by the directory has 11961 * been written out, then the dependency can be 11962 * moved to the pending list. 11963 */ 11964 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 11965 LIST_REMOVE(dap, da_pdlist); 11966 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, 11967 da_pdlist); 11968 } 11969 } 11970 } 11971 /* 11972 * If there were any rollbacks in the directory, then it must be 11973 * marked dirty so that its will eventually get written back in 11974 * its correct form. 11975 */ 11976 if (chgs) { 11977 if ((bp->b_flags & B_DELWRI) == 0) 11978 stat_dir_entry++; 11979 bdirty(bp); 11980 return (1); 11981 } 11982 /* 11983 * If we are not waiting for a new directory block to be 11984 * claimed by its inode, then the pagedep will be freed. 11985 * Otherwise it will remain to track any new entries on 11986 * the page in case they are fsync'ed. 11987 */ 11988 free_pagedep(pagedep); 11989 return (0); 11990 } 11991 11992 /* 11993 * Writing back in-core inode structures. 11994 * 11995 * The filesystem only accesses an inode's contents when it occupies an 11996 * "in-core" inode structure. These "in-core" structures are separate from 11997 * the page frames used to cache inode blocks. Only the latter are 11998 * transferred to/from the disk. So, when the updated contents of the 11999 * "in-core" inode structure are copied to the corresponding in-memory inode 12000 * block, the dependencies are also transferred. The following procedure is 12001 * called when copying a dirty "in-core" inode to a cached inode block. 12002 */ 12003 12004 /* 12005 * Called when an inode is loaded from disk. If the effective link count 12006 * differed from the actual link count when it was last flushed, then we 12007 * need to ensure that the correct effective link count is put back. 12008 */ 12009 void 12010 softdep_load_inodeblock(ip) 12011 struct inode *ip; /* the "in_core" copy of the inode */ 12012 { 12013 struct inodedep *inodedep; 12014 12015 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0, 12016 ("softdep_load_inodeblock called on non-softdep filesystem")); 12017 /* 12018 * Check for alternate nlink count. 12019 */ 12020 ip->i_effnlink = ip->i_nlink; 12021 ACQUIRE_LOCK(ip->i_ump); 12022 if (inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0, 12023 &inodedep) == 0) { 12024 FREE_LOCK(ip->i_ump); 12025 return; 12026 } 12027 ip->i_effnlink -= inodedep->id_nlinkdelta; 12028 FREE_LOCK(ip->i_ump); 12029 } 12030 12031 /* 12032 * This routine is called just before the "in-core" inode 12033 * information is to be copied to the in-memory inode block. 12034 * Recall that an inode block contains several inodes. If 12035 * the force flag is set, then the dependencies will be 12036 * cleared so that the update can always be made. Note that 12037 * the buffer is locked when this routine is called, so we 12038 * will never be in the middle of writing the inode block 12039 * to disk. 12040 */ 12041 void 12042 softdep_update_inodeblock(ip, bp, waitfor) 12043 struct inode *ip; /* the "in_core" copy of the inode */ 12044 struct buf *bp; /* the buffer containing the inode block */ 12045 int waitfor; /* nonzero => update must be allowed */ 12046 { 12047 struct inodedep *inodedep; 12048 struct inoref *inoref; 12049 struct ufsmount *ump; 12050 struct worklist *wk; 12051 struct mount *mp; 12052 struct buf *ibp; 12053 struct fs *fs; 12054 int error; 12055 12056 ump = ip->i_ump; 12057 mp = UFSTOVFS(ump); 12058 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 12059 ("softdep_update_inodeblock called on non-softdep filesystem")); 12060 fs = ip->i_fs; 12061 /* 12062 * Preserve the freelink that is on disk. clear_unlinked_inodedep() 12063 * does not have access to the in-core ip so must write directly into 12064 * the inode block buffer when setting freelink. 12065 */ 12066 if (fs->fs_magic == FS_UFS1_MAGIC) 12067 DIP_SET(ip, i_freelink, ((struct ufs1_dinode *)bp->b_data + 12068 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12069 else 12070 DIP_SET(ip, i_freelink, ((struct ufs2_dinode *)bp->b_data + 12071 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12072 /* 12073 * If the effective link count is not equal to the actual link 12074 * count, then we must track the difference in an inodedep while 12075 * the inode is (potentially) tossed out of the cache. Otherwise, 12076 * if there is no existing inodedep, then there are no dependencies 12077 * to track. 12078 */ 12079 ACQUIRE_LOCK(ump); 12080 again: 12081 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12082 FREE_LOCK(ump); 12083 if (ip->i_effnlink != ip->i_nlink) 12084 panic("softdep_update_inodeblock: bad link count"); 12085 return; 12086 } 12087 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink) 12088 panic("softdep_update_inodeblock: bad delta"); 12089 /* 12090 * If we're flushing all dependencies we must also move any waiting 12091 * for journal writes onto the bufwait list prior to I/O. 12092 */ 12093 if (waitfor) { 12094 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12095 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12096 == DEPCOMPLETE) { 12097 jwait(&inoref->if_list, MNT_WAIT); 12098 goto again; 12099 } 12100 } 12101 } 12102 /* 12103 * Changes have been initiated. Anything depending on these 12104 * changes cannot occur until this inode has been written. 12105 */ 12106 inodedep->id_state &= ~COMPLETE; 12107 if ((inodedep->id_state & ONWORKLIST) == 0) 12108 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list); 12109 /* 12110 * Any new dependencies associated with the incore inode must 12111 * now be moved to the list associated with the buffer holding 12112 * the in-memory copy of the inode. Once merged process any 12113 * allocdirects that are completed by the merger. 12114 */ 12115 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt); 12116 if (!TAILQ_EMPTY(&inodedep->id_inoupdt)) 12117 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt), 12118 NULL); 12119 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt); 12120 if (!TAILQ_EMPTY(&inodedep->id_extupdt)) 12121 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt), 12122 NULL); 12123 /* 12124 * Now that the inode has been pushed into the buffer, the 12125 * operations dependent on the inode being written to disk 12126 * can be moved to the id_bufwait so that they will be 12127 * processed when the buffer I/O completes. 12128 */ 12129 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) { 12130 WORKLIST_REMOVE(wk); 12131 WORKLIST_INSERT(&inodedep->id_bufwait, wk); 12132 } 12133 /* 12134 * Newly allocated inodes cannot be written until the bitmap 12135 * that allocates them have been written (indicated by 12136 * DEPCOMPLETE being set in id_state). If we are doing a 12137 * forced sync (e.g., an fsync on a file), we force the bitmap 12138 * to be written so that the update can be done. 12139 */ 12140 if (waitfor == 0) { 12141 FREE_LOCK(ump); 12142 return; 12143 } 12144 retry: 12145 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) { 12146 FREE_LOCK(ump); 12147 return; 12148 } 12149 ibp = inodedep->id_bmsafemap->sm_buf; 12150 ibp = getdirtybuf(ibp, LOCK_PTR(ump), MNT_WAIT); 12151 if (ibp == NULL) { 12152 /* 12153 * If ibp came back as NULL, the dependency could have been 12154 * freed while we slept. Look it up again, and check to see 12155 * that it has completed. 12156 */ 12157 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 12158 goto retry; 12159 FREE_LOCK(ump); 12160 return; 12161 } 12162 FREE_LOCK(ump); 12163 if ((error = bwrite(ibp)) != 0) 12164 softdep_error("softdep_update_inodeblock: bwrite", error); 12165 } 12166 12167 /* 12168 * Merge the a new inode dependency list (such as id_newinoupdt) into an 12169 * old inode dependency list (such as id_inoupdt). This routine must be 12170 * called with splbio interrupts blocked. 12171 */ 12172 static void 12173 merge_inode_lists(newlisthead, oldlisthead) 12174 struct allocdirectlst *newlisthead; 12175 struct allocdirectlst *oldlisthead; 12176 { 12177 struct allocdirect *listadp, *newadp; 12178 12179 newadp = TAILQ_FIRST(newlisthead); 12180 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) { 12181 if (listadp->ad_offset < newadp->ad_offset) { 12182 listadp = TAILQ_NEXT(listadp, ad_next); 12183 continue; 12184 } 12185 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12186 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next); 12187 if (listadp->ad_offset == newadp->ad_offset) { 12188 allocdirect_merge(oldlisthead, newadp, 12189 listadp); 12190 listadp = newadp; 12191 } 12192 newadp = TAILQ_FIRST(newlisthead); 12193 } 12194 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) { 12195 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12196 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next); 12197 } 12198 } 12199 12200 /* 12201 * If we are doing an fsync, then we must ensure that any directory 12202 * entries for the inode have been written after the inode gets to disk. 12203 */ 12204 int 12205 softdep_fsync(vp) 12206 struct vnode *vp; /* the "in_core" copy of the inode */ 12207 { 12208 struct inodedep *inodedep; 12209 struct pagedep *pagedep; 12210 struct inoref *inoref; 12211 struct ufsmount *ump; 12212 struct worklist *wk; 12213 struct diradd *dap; 12214 struct mount *mp; 12215 struct vnode *pvp; 12216 struct inode *ip; 12217 struct buf *bp; 12218 struct fs *fs; 12219 struct thread *td = curthread; 12220 int error, flushparent, pagedep_new_block; 12221 ino_t parentino; 12222 ufs_lbn_t lbn; 12223 12224 ip = VTOI(vp); 12225 fs = ip->i_fs; 12226 ump = ip->i_ump; 12227 mp = vp->v_mount; 12228 if (MOUNTEDSOFTDEP(mp) == 0) 12229 return (0); 12230 ACQUIRE_LOCK(ump); 12231 restart: 12232 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12233 FREE_LOCK(ump); 12234 return (0); 12235 } 12236 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12237 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12238 == DEPCOMPLETE) { 12239 jwait(&inoref->if_list, MNT_WAIT); 12240 goto restart; 12241 } 12242 } 12243 if (!LIST_EMPTY(&inodedep->id_inowait) || 12244 !TAILQ_EMPTY(&inodedep->id_extupdt) || 12245 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 12246 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 12247 !TAILQ_EMPTY(&inodedep->id_newinoupdt)) 12248 panic("softdep_fsync: pending ops %p", inodedep); 12249 for (error = 0, flushparent = 0; ; ) { 12250 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL) 12251 break; 12252 if (wk->wk_type != D_DIRADD) 12253 panic("softdep_fsync: Unexpected type %s", 12254 TYPENAME(wk->wk_type)); 12255 dap = WK_DIRADD(wk); 12256 /* 12257 * Flush our parent if this directory entry has a MKDIR_PARENT 12258 * dependency or is contained in a newly allocated block. 12259 */ 12260 if (dap->da_state & DIRCHG) 12261 pagedep = dap->da_previous->dm_pagedep; 12262 else 12263 pagedep = dap->da_pagedep; 12264 parentino = pagedep->pd_ino; 12265 lbn = pagedep->pd_lbn; 12266 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) 12267 panic("softdep_fsync: dirty"); 12268 if ((dap->da_state & MKDIR_PARENT) || 12269 (pagedep->pd_state & NEWBLOCK)) 12270 flushparent = 1; 12271 else 12272 flushparent = 0; 12273 /* 12274 * If we are being fsync'ed as part of vgone'ing this vnode, 12275 * then we will not be able to release and recover the 12276 * vnode below, so we just have to give up on writing its 12277 * directory entry out. It will eventually be written, just 12278 * not now, but then the user was not asking to have it 12279 * written, so we are not breaking any promises. 12280 */ 12281 if (vp->v_iflag & VI_DOOMED) 12282 break; 12283 /* 12284 * We prevent deadlock by always fetching inodes from the 12285 * root, moving down the directory tree. Thus, when fetching 12286 * our parent directory, we first try to get the lock. If 12287 * that fails, we must unlock ourselves before requesting 12288 * the lock on our parent. See the comment in ufs_lookup 12289 * for details on possible races. 12290 */ 12291 FREE_LOCK(ump); 12292 if (ffs_vgetf(mp, parentino, LK_NOWAIT | LK_EXCLUSIVE, &pvp, 12293 FFSV_FORCEINSMQ)) { 12294 error = vfs_busy(mp, MBF_NOWAIT); 12295 if (error != 0) { 12296 vfs_ref(mp); 12297 VOP_UNLOCK(vp, 0); 12298 error = vfs_busy(mp, 0); 12299 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 12300 vfs_rel(mp); 12301 if (error != 0) 12302 return (ENOENT); 12303 if (vp->v_iflag & VI_DOOMED) { 12304 vfs_unbusy(mp); 12305 return (ENOENT); 12306 } 12307 } 12308 VOP_UNLOCK(vp, 0); 12309 error = ffs_vgetf(mp, parentino, LK_EXCLUSIVE, 12310 &pvp, FFSV_FORCEINSMQ); 12311 vfs_unbusy(mp); 12312 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 12313 if (vp->v_iflag & VI_DOOMED) { 12314 if (error == 0) 12315 vput(pvp); 12316 error = ENOENT; 12317 } 12318 if (error != 0) 12319 return (error); 12320 } 12321 /* 12322 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps 12323 * that are contained in direct blocks will be resolved by 12324 * doing a ffs_update. Pagedeps contained in indirect blocks 12325 * may require a complete sync'ing of the directory. So, we 12326 * try the cheap and fast ffs_update first, and if that fails, 12327 * then we do the slower ffs_syncvnode of the directory. 12328 */ 12329 if (flushparent) { 12330 int locked; 12331 12332 if ((error = ffs_update(pvp, 1)) != 0) { 12333 vput(pvp); 12334 return (error); 12335 } 12336 ACQUIRE_LOCK(ump); 12337 locked = 1; 12338 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) { 12339 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) { 12340 if (wk->wk_type != D_DIRADD) 12341 panic("softdep_fsync: Unexpected type %s", 12342 TYPENAME(wk->wk_type)); 12343 dap = WK_DIRADD(wk); 12344 if (dap->da_state & DIRCHG) 12345 pagedep = dap->da_previous->dm_pagedep; 12346 else 12347 pagedep = dap->da_pagedep; 12348 pagedep_new_block = pagedep->pd_state & NEWBLOCK; 12349 FREE_LOCK(ump); 12350 locked = 0; 12351 if (pagedep_new_block && (error = 12352 ffs_syncvnode(pvp, MNT_WAIT, 0))) { 12353 vput(pvp); 12354 return (error); 12355 } 12356 } 12357 } 12358 if (locked) 12359 FREE_LOCK(ump); 12360 } 12361 /* 12362 * Flush directory page containing the inode's name. 12363 */ 12364 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred, 12365 &bp); 12366 if (error == 0) 12367 error = bwrite(bp); 12368 else 12369 brelse(bp); 12370 vput(pvp); 12371 if (error != 0) 12372 return (error); 12373 ACQUIRE_LOCK(ump); 12374 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 12375 break; 12376 } 12377 FREE_LOCK(ump); 12378 return (0); 12379 } 12380 12381 /* 12382 * Flush all the dirty bitmaps associated with the block device 12383 * before flushing the rest of the dirty blocks so as to reduce 12384 * the number of dependencies that will have to be rolled back. 12385 * 12386 * XXX Unused? 12387 */ 12388 void 12389 softdep_fsync_mountdev(vp) 12390 struct vnode *vp; 12391 { 12392 struct buf *bp, *nbp; 12393 struct worklist *wk; 12394 struct bufobj *bo; 12395 12396 if (!vn_isdisk(vp, NULL)) 12397 panic("softdep_fsync_mountdev: vnode not a disk"); 12398 bo = &vp->v_bufobj; 12399 restart: 12400 BO_LOCK(bo); 12401 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 12402 /* 12403 * If it is already scheduled, skip to the next buffer. 12404 */ 12405 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) 12406 continue; 12407 12408 if ((bp->b_flags & B_DELWRI) == 0) 12409 panic("softdep_fsync_mountdev: not dirty"); 12410 /* 12411 * We are only interested in bitmaps with outstanding 12412 * dependencies. 12413 */ 12414 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL || 12415 wk->wk_type != D_BMSAFEMAP || 12416 (bp->b_vflags & BV_BKGRDINPROG)) { 12417 BUF_UNLOCK(bp); 12418 continue; 12419 } 12420 BO_UNLOCK(bo); 12421 bremfree(bp); 12422 (void) bawrite(bp); 12423 goto restart; 12424 } 12425 drain_output(vp); 12426 BO_UNLOCK(bo); 12427 } 12428 12429 /* 12430 * Sync all cylinder groups that were dirty at the time this function is 12431 * called. Newly dirtied cgs will be inserted before the sentinel. This 12432 * is used to flush freedep activity that may be holding up writes to a 12433 * indirect block. 12434 */ 12435 static int 12436 sync_cgs(mp, waitfor) 12437 struct mount *mp; 12438 int waitfor; 12439 { 12440 struct bmsafemap *bmsafemap; 12441 struct bmsafemap *sentinel; 12442 struct ufsmount *ump; 12443 struct buf *bp; 12444 int error; 12445 12446 sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK); 12447 sentinel->sm_cg = -1; 12448 ump = VFSTOUFS(mp); 12449 error = 0; 12450 ACQUIRE_LOCK(ump); 12451 LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next); 12452 for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL; 12453 bmsafemap = LIST_NEXT(sentinel, sm_next)) { 12454 /* Skip sentinels and cgs with no work to release. */ 12455 if (bmsafemap->sm_cg == -1 || 12456 (LIST_EMPTY(&bmsafemap->sm_freehd) && 12457 LIST_EMPTY(&bmsafemap->sm_freewr))) { 12458 LIST_REMOVE(sentinel, sm_next); 12459 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 12460 continue; 12461 } 12462 /* 12463 * If we don't get the lock and we're waiting try again, if 12464 * not move on to the next buf and try to sync it. 12465 */ 12466 bp = getdirtybuf(bmsafemap->sm_buf, LOCK_PTR(ump), waitfor); 12467 if (bp == NULL && waitfor == MNT_WAIT) 12468 continue; 12469 LIST_REMOVE(sentinel, sm_next); 12470 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 12471 if (bp == NULL) 12472 continue; 12473 FREE_LOCK(ump); 12474 if (waitfor == MNT_NOWAIT) 12475 bawrite(bp); 12476 else 12477 error = bwrite(bp); 12478 ACQUIRE_LOCK(ump); 12479 if (error) 12480 break; 12481 } 12482 LIST_REMOVE(sentinel, sm_next); 12483 FREE_LOCK(ump); 12484 free(sentinel, M_BMSAFEMAP); 12485 return (error); 12486 } 12487 12488 /* 12489 * This routine is called when we are trying to synchronously flush a 12490 * file. This routine must eliminate any filesystem metadata dependencies 12491 * so that the syncing routine can succeed. 12492 */ 12493 int 12494 softdep_sync_metadata(struct vnode *vp) 12495 { 12496 struct inode *ip; 12497 int error; 12498 12499 ip = VTOI(vp); 12500 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0, 12501 ("softdep_sync_metadata called on non-softdep filesystem")); 12502 /* 12503 * Ensure that any direct block dependencies have been cleared, 12504 * truncations are started, and inode references are journaled. 12505 */ 12506 ACQUIRE_LOCK(ip->i_ump); 12507 /* 12508 * Write all journal records to prevent rollbacks on devvp. 12509 */ 12510 if (vp->v_type == VCHR) 12511 softdep_flushjournal(vp->v_mount); 12512 error = flush_inodedep_deps(vp, vp->v_mount, ip->i_number); 12513 /* 12514 * Ensure that all truncates are written so we won't find deps on 12515 * indirect blocks. 12516 */ 12517 process_truncates(vp); 12518 FREE_LOCK(ip->i_ump); 12519 12520 return (error); 12521 } 12522 12523 /* 12524 * This routine is called when we are attempting to sync a buf with 12525 * dependencies. If waitfor is MNT_NOWAIT it attempts to schedule any 12526 * other IO it can but returns EBUSY if the buffer is not yet able to 12527 * be written. Dependencies which will not cause rollbacks will always 12528 * return 0. 12529 */ 12530 int 12531 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 12532 { 12533 struct indirdep *indirdep; 12534 struct pagedep *pagedep; 12535 struct allocindir *aip; 12536 struct newblk *newblk; 12537 struct ufsmount *ump; 12538 struct buf *nbp; 12539 struct worklist *wk; 12540 int i, error; 12541 12542 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 12543 ("softdep_sync_buf called on non-softdep filesystem")); 12544 /* 12545 * For VCHR we just don't want to force flush any dependencies that 12546 * will cause rollbacks. 12547 */ 12548 if (vp->v_type == VCHR) { 12549 if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0)) 12550 return (EBUSY); 12551 return (0); 12552 } 12553 ump = VTOI(vp)->i_ump; 12554 ACQUIRE_LOCK(ump); 12555 /* 12556 * As we hold the buffer locked, none of its dependencies 12557 * will disappear. 12558 */ 12559 error = 0; 12560 top: 12561 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 12562 switch (wk->wk_type) { 12563 12564 case D_ALLOCDIRECT: 12565 case D_ALLOCINDIR: 12566 newblk = WK_NEWBLK(wk); 12567 if (newblk->nb_jnewblk != NULL) { 12568 if (waitfor == MNT_NOWAIT) { 12569 error = EBUSY; 12570 goto out_unlock; 12571 } 12572 jwait(&newblk->nb_jnewblk->jn_list, waitfor); 12573 goto top; 12574 } 12575 if (newblk->nb_state & DEPCOMPLETE || 12576 waitfor == MNT_NOWAIT) 12577 continue; 12578 nbp = newblk->nb_bmsafemap->sm_buf; 12579 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 12580 if (nbp == NULL) 12581 goto top; 12582 FREE_LOCK(ump); 12583 if ((error = bwrite(nbp)) != 0) 12584 goto out; 12585 ACQUIRE_LOCK(ump); 12586 continue; 12587 12588 case D_INDIRDEP: 12589 indirdep = WK_INDIRDEP(wk); 12590 if (waitfor == MNT_NOWAIT) { 12591 if (!TAILQ_EMPTY(&indirdep->ir_trunc) || 12592 !LIST_EMPTY(&indirdep->ir_deplisthd)) { 12593 error = EBUSY; 12594 goto out_unlock; 12595 } 12596 } 12597 if (!TAILQ_EMPTY(&indirdep->ir_trunc)) 12598 panic("softdep_sync_buf: truncation pending."); 12599 restart: 12600 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 12601 newblk = (struct newblk *)aip; 12602 if (newblk->nb_jnewblk != NULL) { 12603 jwait(&newblk->nb_jnewblk->jn_list, 12604 waitfor); 12605 goto restart; 12606 } 12607 if (newblk->nb_state & DEPCOMPLETE) 12608 continue; 12609 nbp = newblk->nb_bmsafemap->sm_buf; 12610 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 12611 if (nbp == NULL) 12612 goto restart; 12613 FREE_LOCK(ump); 12614 if ((error = bwrite(nbp)) != 0) 12615 goto out; 12616 ACQUIRE_LOCK(ump); 12617 goto restart; 12618 } 12619 continue; 12620 12621 case D_PAGEDEP: 12622 /* 12623 * Only flush directory entries in synchronous passes. 12624 */ 12625 if (waitfor != MNT_WAIT) { 12626 error = EBUSY; 12627 goto out_unlock; 12628 } 12629 /* 12630 * While syncing snapshots, we must allow recursive 12631 * lookups. 12632 */ 12633 BUF_AREC(bp); 12634 /* 12635 * We are trying to sync a directory that may 12636 * have dependencies on both its own metadata 12637 * and/or dependencies on the inodes of any 12638 * recently allocated files. We walk its diradd 12639 * lists pushing out the associated inode. 12640 */ 12641 pagedep = WK_PAGEDEP(wk); 12642 for (i = 0; i < DAHASHSZ; i++) { 12643 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0) 12644 continue; 12645 if ((error = flush_pagedep_deps(vp, wk->wk_mp, 12646 &pagedep->pd_diraddhd[i]))) { 12647 BUF_NOREC(bp); 12648 goto out_unlock; 12649 } 12650 } 12651 BUF_NOREC(bp); 12652 continue; 12653 12654 case D_FREEWORK: 12655 case D_FREEDEP: 12656 case D_JSEGDEP: 12657 case D_JNEWBLK: 12658 continue; 12659 12660 default: 12661 panic("softdep_sync_buf: Unknown type %s", 12662 TYPENAME(wk->wk_type)); 12663 /* NOTREACHED */ 12664 } 12665 } 12666 out_unlock: 12667 FREE_LOCK(ump); 12668 out: 12669 return (error); 12670 } 12671 12672 /* 12673 * Flush the dependencies associated with an inodedep. 12674 * Called with splbio blocked. 12675 */ 12676 static int 12677 flush_inodedep_deps(vp, mp, ino) 12678 struct vnode *vp; 12679 struct mount *mp; 12680 ino_t ino; 12681 { 12682 struct inodedep *inodedep; 12683 struct inoref *inoref; 12684 struct ufsmount *ump; 12685 int error, waitfor; 12686 12687 /* 12688 * This work is done in two passes. The first pass grabs most 12689 * of the buffers and begins asynchronously writing them. The 12690 * only way to wait for these asynchronous writes is to sleep 12691 * on the filesystem vnode which may stay busy for a long time 12692 * if the filesystem is active. So, instead, we make a second 12693 * pass over the dependencies blocking on each write. In the 12694 * usual case we will be blocking against a write that we 12695 * initiated, so when it is done the dependency will have been 12696 * resolved. Thus the second pass is expected to end quickly. 12697 * We give a brief window at the top of the loop to allow 12698 * any pending I/O to complete. 12699 */ 12700 ump = VFSTOUFS(mp); 12701 LOCK_OWNED(ump); 12702 for (error = 0, waitfor = MNT_NOWAIT; ; ) { 12703 if (error) 12704 return (error); 12705 FREE_LOCK(ump); 12706 ACQUIRE_LOCK(ump); 12707 restart: 12708 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 12709 return (0); 12710 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12711 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12712 == DEPCOMPLETE) { 12713 jwait(&inoref->if_list, MNT_WAIT); 12714 goto restart; 12715 } 12716 } 12717 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) || 12718 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) || 12719 flush_deplist(&inodedep->id_extupdt, waitfor, &error) || 12720 flush_deplist(&inodedep->id_newextupdt, waitfor, &error)) 12721 continue; 12722 /* 12723 * If pass2, we are done, otherwise do pass 2. 12724 */ 12725 if (waitfor == MNT_WAIT) 12726 break; 12727 waitfor = MNT_WAIT; 12728 } 12729 /* 12730 * Try freeing inodedep in case all dependencies have been removed. 12731 */ 12732 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0) 12733 (void) free_inodedep(inodedep); 12734 return (0); 12735 } 12736 12737 /* 12738 * Flush an inode dependency list. 12739 * Called with splbio blocked. 12740 */ 12741 static int 12742 flush_deplist(listhead, waitfor, errorp) 12743 struct allocdirectlst *listhead; 12744 int waitfor; 12745 int *errorp; 12746 { 12747 struct allocdirect *adp; 12748 struct newblk *newblk; 12749 struct ufsmount *ump; 12750 struct buf *bp; 12751 12752 if ((adp = TAILQ_FIRST(listhead)) == NULL) 12753 return (0); 12754 ump = VFSTOUFS(adp->ad_list.wk_mp); 12755 LOCK_OWNED(ump); 12756 TAILQ_FOREACH(adp, listhead, ad_next) { 12757 newblk = (struct newblk *)adp; 12758 if (newblk->nb_jnewblk != NULL) { 12759 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 12760 return (1); 12761 } 12762 if (newblk->nb_state & DEPCOMPLETE) 12763 continue; 12764 bp = newblk->nb_bmsafemap->sm_buf; 12765 bp = getdirtybuf(bp, LOCK_PTR(ump), waitfor); 12766 if (bp == NULL) { 12767 if (waitfor == MNT_NOWAIT) 12768 continue; 12769 return (1); 12770 } 12771 FREE_LOCK(ump); 12772 if (waitfor == MNT_NOWAIT) 12773 bawrite(bp); 12774 else 12775 *errorp = bwrite(bp); 12776 ACQUIRE_LOCK(ump); 12777 return (1); 12778 } 12779 return (0); 12780 } 12781 12782 /* 12783 * Flush dependencies associated with an allocdirect block. 12784 */ 12785 static int 12786 flush_newblk_dep(vp, mp, lbn) 12787 struct vnode *vp; 12788 struct mount *mp; 12789 ufs_lbn_t lbn; 12790 { 12791 struct newblk *newblk; 12792 struct ufsmount *ump; 12793 struct bufobj *bo; 12794 struct inode *ip; 12795 struct buf *bp; 12796 ufs2_daddr_t blkno; 12797 int error; 12798 12799 error = 0; 12800 bo = &vp->v_bufobj; 12801 ip = VTOI(vp); 12802 blkno = DIP(ip, i_db[lbn]); 12803 if (blkno == 0) 12804 panic("flush_newblk_dep: Missing block"); 12805 ump = VFSTOUFS(mp); 12806 ACQUIRE_LOCK(ump); 12807 /* 12808 * Loop until all dependencies related to this block are satisfied. 12809 * We must be careful to restart after each sleep in case a write 12810 * completes some part of this process for us. 12811 */ 12812 for (;;) { 12813 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) { 12814 FREE_LOCK(ump); 12815 break; 12816 } 12817 if (newblk->nb_list.wk_type != D_ALLOCDIRECT) 12818 panic("flush_newblk_deps: Bad newblk %p", newblk); 12819 /* 12820 * Flush the journal. 12821 */ 12822 if (newblk->nb_jnewblk != NULL) { 12823 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 12824 continue; 12825 } 12826 /* 12827 * Write the bitmap dependency. 12828 */ 12829 if ((newblk->nb_state & DEPCOMPLETE) == 0) { 12830 bp = newblk->nb_bmsafemap->sm_buf; 12831 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 12832 if (bp == NULL) 12833 continue; 12834 FREE_LOCK(ump); 12835 error = bwrite(bp); 12836 if (error) 12837 break; 12838 ACQUIRE_LOCK(ump); 12839 continue; 12840 } 12841 /* 12842 * Write the buffer. 12843 */ 12844 FREE_LOCK(ump); 12845 BO_LOCK(bo); 12846 bp = gbincore(bo, lbn); 12847 if (bp != NULL) { 12848 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 12849 LK_INTERLOCK, BO_LOCKPTR(bo)); 12850 if (error == ENOLCK) { 12851 ACQUIRE_LOCK(ump); 12852 continue; /* Slept, retry */ 12853 } 12854 if (error != 0) 12855 break; /* Failed */ 12856 if (bp->b_flags & B_DELWRI) { 12857 bremfree(bp); 12858 error = bwrite(bp); 12859 if (error) 12860 break; 12861 } else 12862 BUF_UNLOCK(bp); 12863 } else 12864 BO_UNLOCK(bo); 12865 /* 12866 * We have to wait for the direct pointers to 12867 * point at the newdirblk before the dependency 12868 * will go away. 12869 */ 12870 error = ffs_update(vp, 1); 12871 if (error) 12872 break; 12873 ACQUIRE_LOCK(ump); 12874 } 12875 return (error); 12876 } 12877 12878 /* 12879 * Eliminate a pagedep dependency by flushing out all its diradd dependencies. 12880 * Called with splbio blocked. 12881 */ 12882 static int 12883 flush_pagedep_deps(pvp, mp, diraddhdp) 12884 struct vnode *pvp; 12885 struct mount *mp; 12886 struct diraddhd *diraddhdp; 12887 { 12888 struct inodedep *inodedep; 12889 struct inoref *inoref; 12890 struct ufsmount *ump; 12891 struct diradd *dap; 12892 struct vnode *vp; 12893 int error = 0; 12894 struct buf *bp; 12895 ino_t inum; 12896 struct diraddhd unfinished; 12897 12898 LIST_INIT(&unfinished); 12899 ump = VFSTOUFS(mp); 12900 LOCK_OWNED(ump); 12901 restart: 12902 while ((dap = LIST_FIRST(diraddhdp)) != NULL) { 12903 /* 12904 * Flush ourselves if this directory entry 12905 * has a MKDIR_PARENT dependency. 12906 */ 12907 if (dap->da_state & MKDIR_PARENT) { 12908 FREE_LOCK(ump); 12909 if ((error = ffs_update(pvp, 1)) != 0) 12910 break; 12911 ACQUIRE_LOCK(ump); 12912 /* 12913 * If that cleared dependencies, go on to next. 12914 */ 12915 if (dap != LIST_FIRST(diraddhdp)) 12916 continue; 12917 /* 12918 * All MKDIR_PARENT dependencies and all the 12919 * NEWBLOCK pagedeps that are contained in direct 12920 * blocks were resolved by doing above ffs_update. 12921 * Pagedeps contained in indirect blocks may 12922 * require a complete sync'ing of the directory. 12923 * We are in the midst of doing a complete sync, 12924 * so if they are not resolved in this pass we 12925 * defer them for now as they will be sync'ed by 12926 * our caller shortly. 12927 */ 12928 LIST_REMOVE(dap, da_pdlist); 12929 LIST_INSERT_HEAD(&unfinished, dap, da_pdlist); 12930 continue; 12931 } 12932 /* 12933 * A newly allocated directory must have its "." and 12934 * ".." entries written out before its name can be 12935 * committed in its parent. 12936 */ 12937 inum = dap->da_newinum; 12938 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 12939 panic("flush_pagedep_deps: lost inode1"); 12940 /* 12941 * Wait for any pending journal adds to complete so we don't 12942 * cause rollbacks while syncing. 12943 */ 12944 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12945 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12946 == DEPCOMPLETE) { 12947 jwait(&inoref->if_list, MNT_WAIT); 12948 goto restart; 12949 } 12950 } 12951 if (dap->da_state & MKDIR_BODY) { 12952 FREE_LOCK(ump); 12953 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp, 12954 FFSV_FORCEINSMQ))) 12955 break; 12956 error = flush_newblk_dep(vp, mp, 0); 12957 /* 12958 * If we still have the dependency we might need to 12959 * update the vnode to sync the new link count to 12960 * disk. 12961 */ 12962 if (error == 0 && dap == LIST_FIRST(diraddhdp)) 12963 error = ffs_update(vp, 1); 12964 vput(vp); 12965 if (error != 0) 12966 break; 12967 ACQUIRE_LOCK(ump); 12968 /* 12969 * If that cleared dependencies, go on to next. 12970 */ 12971 if (dap != LIST_FIRST(diraddhdp)) 12972 continue; 12973 if (dap->da_state & MKDIR_BODY) { 12974 inodedep_lookup(UFSTOVFS(ump), inum, 0, 12975 &inodedep); 12976 panic("flush_pagedep_deps: MKDIR_BODY " 12977 "inodedep %p dap %p vp %p", 12978 inodedep, dap, vp); 12979 } 12980 } 12981 /* 12982 * Flush the inode on which the directory entry depends. 12983 * Having accounted for MKDIR_PARENT and MKDIR_BODY above, 12984 * the only remaining dependency is that the updated inode 12985 * count must get pushed to disk. The inode has already 12986 * been pushed into its inode buffer (via VOP_UPDATE) at 12987 * the time of the reference count change. So we need only 12988 * locate that buffer, ensure that there will be no rollback 12989 * caused by a bitmap dependency, then write the inode buffer. 12990 */ 12991 retry: 12992 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 12993 panic("flush_pagedep_deps: lost inode"); 12994 /* 12995 * If the inode still has bitmap dependencies, 12996 * push them to disk. 12997 */ 12998 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) { 12999 bp = inodedep->id_bmsafemap->sm_buf; 13000 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 13001 if (bp == NULL) 13002 goto retry; 13003 FREE_LOCK(ump); 13004 if ((error = bwrite(bp)) != 0) 13005 break; 13006 ACQUIRE_LOCK(ump); 13007 if (dap != LIST_FIRST(diraddhdp)) 13008 continue; 13009 } 13010 /* 13011 * If the inode is still sitting in a buffer waiting 13012 * to be written or waiting for the link count to be 13013 * adjusted update it here to flush it to disk. 13014 */ 13015 if (dap == LIST_FIRST(diraddhdp)) { 13016 FREE_LOCK(ump); 13017 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp, 13018 FFSV_FORCEINSMQ))) 13019 break; 13020 error = ffs_update(vp, 1); 13021 vput(vp); 13022 if (error) 13023 break; 13024 ACQUIRE_LOCK(ump); 13025 } 13026 /* 13027 * If we have failed to get rid of all the dependencies 13028 * then something is seriously wrong. 13029 */ 13030 if (dap == LIST_FIRST(diraddhdp)) { 13031 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep); 13032 panic("flush_pagedep_deps: failed to flush " 13033 "inodedep %p ino %ju dap %p", 13034 inodedep, (uintmax_t)inum, dap); 13035 } 13036 } 13037 if (error) 13038 ACQUIRE_LOCK(ump); 13039 while ((dap = LIST_FIRST(&unfinished)) != NULL) { 13040 LIST_REMOVE(dap, da_pdlist); 13041 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist); 13042 } 13043 return (error); 13044 } 13045 13046 /* 13047 * A large burst of file addition or deletion activity can drive the 13048 * memory load excessively high. First attempt to slow things down 13049 * using the techniques below. If that fails, this routine requests 13050 * the offending operations to fall back to running synchronously 13051 * until the memory load returns to a reasonable level. 13052 */ 13053 int 13054 softdep_slowdown(vp) 13055 struct vnode *vp; 13056 { 13057 struct ufsmount *ump; 13058 int jlow; 13059 int max_softdeps_hard; 13060 13061 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 13062 ("softdep_slowdown called on non-softdep filesystem")); 13063 ump = VFSTOUFS(vp->v_mount); 13064 ACQUIRE_LOCK(ump); 13065 jlow = 0; 13066 /* 13067 * Check for journal space if needed. 13068 */ 13069 if (DOINGSUJ(vp)) { 13070 if (journal_space(ump, 0) == 0) 13071 jlow = 1; 13072 } 13073 /* 13074 * If the system is under its limits and our filesystem is 13075 * not responsible for more than our share of the usage and 13076 * we are not low on journal space, then no need to slow down. 13077 */ 13078 max_softdeps_hard = max_softdeps * 11 / 10; 13079 if (dep_current[D_DIRREM] < max_softdeps_hard / 2 && 13080 dep_current[D_INODEDEP] < max_softdeps_hard && 13081 dep_current[D_INDIRDEP] < max_softdeps_hard / 1000 && 13082 dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0 && 13083 ump->softdep_curdeps[D_DIRREM] < 13084 (max_softdeps_hard / 2) / stat_flush_threads && 13085 ump->softdep_curdeps[D_INODEDEP] < 13086 max_softdeps_hard / stat_flush_threads && 13087 ump->softdep_curdeps[D_INDIRDEP] < 13088 (max_softdeps_hard / 1000) / stat_flush_threads && 13089 ump->softdep_curdeps[D_FREEBLKS] < 13090 max_softdeps_hard / stat_flush_threads) { 13091 FREE_LOCK(ump); 13092 return (0); 13093 } 13094 /* 13095 * If the journal is low or our filesystem is over its limit 13096 * then speedup the cleanup. 13097 */ 13098 if (ump->softdep_curdeps[D_INDIRDEP] < 13099 (max_softdeps_hard / 1000) / stat_flush_threads || jlow) 13100 softdep_speedup(ump); 13101 stat_sync_limit_hit += 1; 13102 FREE_LOCK(ump); 13103 /* 13104 * We only slow down the rate at which new dependencies are 13105 * generated if we are not using journaling. With journaling, 13106 * the cleanup should always be sufficient to keep things 13107 * under control. 13108 */ 13109 if (DOINGSUJ(vp)) 13110 return (0); 13111 return (1); 13112 } 13113 13114 /* 13115 * Called by the allocation routines when they are about to fail 13116 * in the hope that we can free up the requested resource (inodes 13117 * or disk space). 13118 * 13119 * First check to see if the work list has anything on it. If it has, 13120 * clean up entries until we successfully free the requested resource. 13121 * Because this process holds inodes locked, we cannot handle any remove 13122 * requests that might block on a locked inode as that could lead to 13123 * deadlock. If the worklist yields none of the requested resource, 13124 * start syncing out vnodes to free up the needed space. 13125 */ 13126 int 13127 softdep_request_cleanup(fs, vp, cred, resource) 13128 struct fs *fs; 13129 struct vnode *vp; 13130 struct ucred *cred; 13131 int resource; 13132 { 13133 struct ufsmount *ump; 13134 struct mount *mp; 13135 struct vnode *lvp, *mvp; 13136 long starttime; 13137 ufs2_daddr_t needed; 13138 int error; 13139 13140 /* 13141 * If we are being called because of a process doing a 13142 * copy-on-write, then it is not safe to process any 13143 * worklist items as we will recurse into the copyonwrite 13144 * routine. This will result in an incoherent snapshot. 13145 * If the vnode that we hold is a snapshot, we must avoid 13146 * handling other resources that could cause deadlock. 13147 */ 13148 if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp))) 13149 return (0); 13150 13151 if (resource == FLUSH_BLOCKS_WAIT) 13152 stat_cleanup_blkrequests += 1; 13153 else 13154 stat_cleanup_inorequests += 1; 13155 13156 mp = vp->v_mount; 13157 ump = VFSTOUFS(mp); 13158 mtx_assert(UFS_MTX(ump), MA_OWNED); 13159 UFS_UNLOCK(ump); 13160 error = ffs_update(vp, 1); 13161 if (error != 0 || MOUNTEDSOFTDEP(mp) == 0) { 13162 UFS_LOCK(ump); 13163 return (0); 13164 } 13165 /* 13166 * If we are in need of resources, start by cleaning up 13167 * any block removals associated with our inode. 13168 */ 13169 ACQUIRE_LOCK(ump); 13170 process_removes(vp); 13171 process_truncates(vp); 13172 FREE_LOCK(ump); 13173 /* 13174 * Now clean up at least as many resources as we will need. 13175 * 13176 * When requested to clean up inodes, the number that are needed 13177 * is set by the number of simultaneous writers (mnt_writeopcount) 13178 * plus a bit of slop (2) in case some more writers show up while 13179 * we are cleaning. 13180 * 13181 * When requested to free up space, the amount of space that 13182 * we need is enough blocks to allocate a full-sized segment 13183 * (fs_contigsumsize). The number of such segments that will 13184 * be needed is set by the number of simultaneous writers 13185 * (mnt_writeopcount) plus a bit of slop (2) in case some more 13186 * writers show up while we are cleaning. 13187 * 13188 * Additionally, if we are unpriviledged and allocating space, 13189 * we need to ensure that we clean up enough blocks to get the 13190 * needed number of blocks over the threshold of the minimum 13191 * number of blocks required to be kept free by the filesystem 13192 * (fs_minfree). 13193 */ 13194 if (resource == FLUSH_INODES_WAIT) { 13195 needed = vp->v_mount->mnt_writeopcount + 2; 13196 } else if (resource == FLUSH_BLOCKS_WAIT) { 13197 needed = (vp->v_mount->mnt_writeopcount + 2) * 13198 fs->fs_contigsumsize; 13199 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE, 0)) 13200 needed += fragstoblks(fs, 13201 roundup((fs->fs_dsize * fs->fs_minfree / 100) - 13202 fs->fs_cstotal.cs_nffree, fs->fs_frag)); 13203 } else { 13204 UFS_LOCK(ump); 13205 printf("softdep_request_cleanup: Unknown resource type %d\n", 13206 resource); 13207 return (0); 13208 } 13209 starttime = time_second; 13210 retry: 13211 if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 && 13212 fs->fs_cstotal.cs_nbfree <= needed) || 13213 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13214 fs->fs_cstotal.cs_nifree <= needed)) { 13215 ACQUIRE_LOCK(ump); 13216 if (ump->softdep_on_worklist > 0 && 13217 process_worklist_item(UFSTOVFS(ump), 13218 ump->softdep_on_worklist, LK_NOWAIT) != 0) 13219 stat_worklist_push += 1; 13220 FREE_LOCK(ump); 13221 } 13222 /* 13223 * If we still need resources and there are no more worklist 13224 * entries to process to obtain them, we have to start flushing 13225 * the dirty vnodes to force the release of additional requests 13226 * to the worklist that we can then process to reap addition 13227 * resources. We walk the vnodes associated with the mount point 13228 * until we get the needed worklist requests that we can reap. 13229 */ 13230 if ((resource == FLUSH_BLOCKS_WAIT && 13231 fs->fs_cstotal.cs_nbfree <= needed) || 13232 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13233 fs->fs_cstotal.cs_nifree <= needed)) { 13234 MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) { 13235 if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) { 13236 VI_UNLOCK(lvp); 13237 continue; 13238 } 13239 if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT, 13240 curthread)) 13241 continue; 13242 if (lvp->v_vflag & VV_NOSYNC) { /* unlinked */ 13243 vput(lvp); 13244 continue; 13245 } 13246 (void) ffs_syncvnode(lvp, MNT_NOWAIT, 0); 13247 vput(lvp); 13248 } 13249 lvp = ump->um_devvp; 13250 if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) { 13251 VOP_FSYNC(lvp, MNT_NOWAIT, curthread); 13252 VOP_UNLOCK(lvp, 0); 13253 } 13254 if (ump->softdep_on_worklist > 0) { 13255 stat_cleanup_retries += 1; 13256 goto retry; 13257 } 13258 stat_cleanup_failures += 1; 13259 } 13260 if (time_second - starttime > stat_cleanup_high_delay) 13261 stat_cleanup_high_delay = time_second - starttime; 13262 UFS_LOCK(ump); 13263 return (1); 13264 } 13265 13266 static bool 13267 softdep_excess_items(struct ufsmount *ump, int item) 13268 { 13269 13270 KASSERT(item >= 0 && item < D_LAST, ("item %d", item)); 13271 return (dep_current[item] > max_softdeps && 13272 ump->softdep_curdeps[item] > max_softdeps / 13273 stat_flush_threads); 13274 } 13275 13276 static void 13277 schedule_cleanup(struct mount *mp) 13278 { 13279 struct ufsmount *ump; 13280 struct thread *td; 13281 13282 ump = VFSTOUFS(mp); 13283 LOCK_OWNED(ump); 13284 FREE_LOCK(ump); 13285 td = curthread; 13286 if ((td->td_pflags & TDP_KTHREAD) != 0 && 13287 (td->td_proc->p_flag2 & P2_AST_SU) == 0) { 13288 /* 13289 * No ast is delivered to kernel threads, so nobody 13290 * would deref the mp. Some kernel threads 13291 * explicitely check for AST, e.g. NFS daemon does 13292 * this in the serving loop. 13293 */ 13294 return; 13295 } 13296 if (td->td_su != NULL) 13297 vfs_rel(td->td_su); 13298 vfs_ref(mp); 13299 td->td_su = mp; 13300 thread_lock(td); 13301 td->td_flags |= TDF_ASTPENDING; 13302 thread_unlock(td); 13303 } 13304 13305 static void 13306 softdep_ast_cleanup_proc(void) 13307 { 13308 struct thread *td; 13309 struct mount *mp; 13310 struct ufsmount *ump; 13311 int error; 13312 bool req; 13313 13314 td = curthread; 13315 while ((mp = td->td_su) != NULL) { 13316 td->td_su = NULL; 13317 error = vfs_busy(mp, MBF_NOWAIT); 13318 vfs_rel(mp); 13319 if (error != 0) 13320 return; 13321 if (ffs_own_mount(mp) && MOUNTEDSOFTDEP(mp)) { 13322 ump = VFSTOUFS(mp); 13323 for (;;) { 13324 req = false; 13325 ACQUIRE_LOCK(ump); 13326 if (softdep_excess_items(ump, D_INODEDEP)) { 13327 req = true; 13328 request_cleanup(mp, FLUSH_INODES); 13329 } 13330 if (softdep_excess_items(ump, D_DIRREM)) { 13331 req = true; 13332 request_cleanup(mp, FLUSH_BLOCKS); 13333 } 13334 FREE_LOCK(ump); 13335 if (softdep_excess_items(ump, D_NEWBLK) || 13336 softdep_excess_items(ump, D_ALLOCDIRECT) || 13337 softdep_excess_items(ump, D_ALLOCINDIR)) { 13338 error = vn_start_write(NULL, &mp, 13339 V_WAIT); 13340 if (error == 0) { 13341 req = true; 13342 VFS_SYNC(mp, MNT_WAIT); 13343 vn_finished_write(mp); 13344 } 13345 } 13346 if ((td->td_pflags & TDP_KTHREAD) != 0 || !req) 13347 break; 13348 } 13349 } 13350 vfs_unbusy(mp); 13351 } 13352 } 13353 13354 /* 13355 * If memory utilization has gotten too high, deliberately slow things 13356 * down and speed up the I/O processing. 13357 */ 13358 static int 13359 request_cleanup(mp, resource) 13360 struct mount *mp; 13361 int resource; 13362 { 13363 struct thread *td = curthread; 13364 struct ufsmount *ump; 13365 13366 ump = VFSTOUFS(mp); 13367 LOCK_OWNED(ump); 13368 /* 13369 * We never hold up the filesystem syncer or buf daemon. 13370 */ 13371 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF)) 13372 return (0); 13373 /* 13374 * First check to see if the work list has gotten backlogged. 13375 * If it has, co-opt this process to help clean up two entries. 13376 * Because this process may hold inodes locked, we cannot 13377 * handle any remove requests that might block on a locked 13378 * inode as that could lead to deadlock. We set TDP_SOFTDEP 13379 * to avoid recursively processing the worklist. 13380 */ 13381 if (ump->softdep_on_worklist > max_softdeps / 10) { 13382 td->td_pflags |= TDP_SOFTDEP; 13383 process_worklist_item(mp, 2, LK_NOWAIT); 13384 td->td_pflags &= ~TDP_SOFTDEP; 13385 stat_worklist_push += 2; 13386 return(1); 13387 } 13388 /* 13389 * Next, we attempt to speed up the syncer process. If that 13390 * is successful, then we allow the process to continue. 13391 */ 13392 if (softdep_speedup(ump) && 13393 resource != FLUSH_BLOCKS_WAIT && 13394 resource != FLUSH_INODES_WAIT) 13395 return(0); 13396 /* 13397 * If we are resource constrained on inode dependencies, try 13398 * flushing some dirty inodes. Otherwise, we are constrained 13399 * by file deletions, so try accelerating flushes of directories 13400 * with removal dependencies. We would like to do the cleanup 13401 * here, but we probably hold an inode locked at this point and 13402 * that might deadlock against one that we try to clean. So, 13403 * the best that we can do is request the syncer daemon to do 13404 * the cleanup for us. 13405 */ 13406 switch (resource) { 13407 13408 case FLUSH_INODES: 13409 case FLUSH_INODES_WAIT: 13410 ACQUIRE_GBLLOCK(&lk); 13411 stat_ino_limit_push += 1; 13412 req_clear_inodedeps += 1; 13413 FREE_GBLLOCK(&lk); 13414 stat_countp = &stat_ino_limit_hit; 13415 break; 13416 13417 case FLUSH_BLOCKS: 13418 case FLUSH_BLOCKS_WAIT: 13419 ACQUIRE_GBLLOCK(&lk); 13420 stat_blk_limit_push += 1; 13421 req_clear_remove += 1; 13422 FREE_GBLLOCK(&lk); 13423 stat_countp = &stat_blk_limit_hit; 13424 break; 13425 13426 default: 13427 panic("request_cleanup: unknown type"); 13428 } 13429 /* 13430 * Hopefully the syncer daemon will catch up and awaken us. 13431 * We wait at most tickdelay before proceeding in any case. 13432 */ 13433 ACQUIRE_GBLLOCK(&lk); 13434 FREE_LOCK(ump); 13435 proc_waiting += 1; 13436 if (callout_pending(&softdep_callout) == FALSE) 13437 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2, 13438 pause_timer, 0); 13439 13440 if ((td->td_pflags & TDP_KTHREAD) == 0) 13441 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0); 13442 proc_waiting -= 1; 13443 FREE_GBLLOCK(&lk); 13444 ACQUIRE_LOCK(ump); 13445 return (1); 13446 } 13447 13448 /* 13449 * Awaken processes pausing in request_cleanup and clear proc_waiting 13450 * to indicate that there is no longer a timer running. Pause_timer 13451 * will be called with the global softdep mutex (&lk) locked. 13452 */ 13453 static void 13454 pause_timer(arg) 13455 void *arg; 13456 { 13457 13458 GBLLOCK_OWNED(&lk); 13459 /* 13460 * The callout_ API has acquired mtx and will hold it around this 13461 * function call. 13462 */ 13463 *stat_countp += proc_waiting; 13464 wakeup(&proc_waiting); 13465 } 13466 13467 /* 13468 * If requested, try removing inode or removal dependencies. 13469 */ 13470 static void 13471 check_clear_deps(mp) 13472 struct mount *mp; 13473 { 13474 13475 /* 13476 * If we are suspended, it may be because of our using 13477 * too many inodedeps, so help clear them out. 13478 */ 13479 if (MOUNTEDSUJ(mp) && VFSTOUFS(mp)->softdep_jblocks->jb_suspended) 13480 clear_inodedeps(mp); 13481 /* 13482 * General requests for cleanup of backed up dependencies 13483 */ 13484 ACQUIRE_GBLLOCK(&lk); 13485 if (req_clear_inodedeps) { 13486 req_clear_inodedeps -= 1; 13487 FREE_GBLLOCK(&lk); 13488 clear_inodedeps(mp); 13489 ACQUIRE_GBLLOCK(&lk); 13490 wakeup(&proc_waiting); 13491 } 13492 if (req_clear_remove) { 13493 req_clear_remove -= 1; 13494 FREE_GBLLOCK(&lk); 13495 clear_remove(mp); 13496 ACQUIRE_GBLLOCK(&lk); 13497 wakeup(&proc_waiting); 13498 } 13499 FREE_GBLLOCK(&lk); 13500 } 13501 13502 /* 13503 * Flush out a directory with at least one removal dependency in an effort to 13504 * reduce the number of dirrem, freefile, and freeblks dependency structures. 13505 */ 13506 static void 13507 clear_remove(mp) 13508 struct mount *mp; 13509 { 13510 struct pagedep_hashhead *pagedephd; 13511 struct pagedep *pagedep; 13512 struct ufsmount *ump; 13513 struct vnode *vp; 13514 struct bufobj *bo; 13515 int error, cnt; 13516 ino_t ino; 13517 13518 ump = VFSTOUFS(mp); 13519 LOCK_OWNED(ump); 13520 13521 for (cnt = 0; cnt <= ump->pagedep_hash_size; cnt++) { 13522 pagedephd = &ump->pagedep_hashtbl[ump->pagedep_nextclean++]; 13523 if (ump->pagedep_nextclean > ump->pagedep_hash_size) 13524 ump->pagedep_nextclean = 0; 13525 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 13526 if (LIST_EMPTY(&pagedep->pd_dirremhd)) 13527 continue; 13528 ino = pagedep->pd_ino; 13529 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 13530 continue; 13531 FREE_LOCK(ump); 13532 13533 /* 13534 * Let unmount clear deps 13535 */ 13536 error = vfs_busy(mp, MBF_NOWAIT); 13537 if (error != 0) 13538 goto finish_write; 13539 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 13540 FFSV_FORCEINSMQ); 13541 vfs_unbusy(mp); 13542 if (error != 0) { 13543 softdep_error("clear_remove: vget", error); 13544 goto finish_write; 13545 } 13546 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 13547 softdep_error("clear_remove: fsync", error); 13548 bo = &vp->v_bufobj; 13549 BO_LOCK(bo); 13550 drain_output(vp); 13551 BO_UNLOCK(bo); 13552 vput(vp); 13553 finish_write: 13554 vn_finished_write(mp); 13555 ACQUIRE_LOCK(ump); 13556 return; 13557 } 13558 } 13559 } 13560 13561 /* 13562 * Clear out a block of dirty inodes in an effort to reduce 13563 * the number of inodedep dependency structures. 13564 */ 13565 static void 13566 clear_inodedeps(mp) 13567 struct mount *mp; 13568 { 13569 struct inodedep_hashhead *inodedephd; 13570 struct inodedep *inodedep; 13571 struct ufsmount *ump; 13572 struct vnode *vp; 13573 struct fs *fs; 13574 int error, cnt; 13575 ino_t firstino, lastino, ino; 13576 13577 ump = VFSTOUFS(mp); 13578 fs = ump->um_fs; 13579 LOCK_OWNED(ump); 13580 /* 13581 * Pick a random inode dependency to be cleared. 13582 * We will then gather up all the inodes in its block 13583 * that have dependencies and flush them out. 13584 */ 13585 for (cnt = 0; cnt <= ump->inodedep_hash_size; cnt++) { 13586 inodedephd = &ump->inodedep_hashtbl[ump->inodedep_nextclean++]; 13587 if (ump->inodedep_nextclean > ump->inodedep_hash_size) 13588 ump->inodedep_nextclean = 0; 13589 if ((inodedep = LIST_FIRST(inodedephd)) != NULL) 13590 break; 13591 } 13592 if (inodedep == NULL) 13593 return; 13594 /* 13595 * Find the last inode in the block with dependencies. 13596 */ 13597 firstino = rounddown2(inodedep->id_ino, INOPB(fs)); 13598 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--) 13599 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0) 13600 break; 13601 /* 13602 * Asynchronously push all but the last inode with dependencies. 13603 * Synchronously push the last inode with dependencies to ensure 13604 * that the inode block gets written to free up the inodedeps. 13605 */ 13606 for (ino = firstino; ino <= lastino; ino++) { 13607 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 13608 continue; 13609 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 13610 continue; 13611 FREE_LOCK(ump); 13612 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */ 13613 if (error != 0) { 13614 vn_finished_write(mp); 13615 ACQUIRE_LOCK(ump); 13616 return; 13617 } 13618 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 13619 FFSV_FORCEINSMQ)) != 0) { 13620 softdep_error("clear_inodedeps: vget", error); 13621 vfs_unbusy(mp); 13622 vn_finished_write(mp); 13623 ACQUIRE_LOCK(ump); 13624 return; 13625 } 13626 vfs_unbusy(mp); 13627 if (ino == lastino) { 13628 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0))) 13629 softdep_error("clear_inodedeps: fsync1", error); 13630 } else { 13631 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 13632 softdep_error("clear_inodedeps: fsync2", error); 13633 BO_LOCK(&vp->v_bufobj); 13634 drain_output(vp); 13635 BO_UNLOCK(&vp->v_bufobj); 13636 } 13637 vput(vp); 13638 vn_finished_write(mp); 13639 ACQUIRE_LOCK(ump); 13640 } 13641 } 13642 13643 void 13644 softdep_buf_append(bp, wkhd) 13645 struct buf *bp; 13646 struct workhead *wkhd; 13647 { 13648 struct worklist *wk; 13649 struct ufsmount *ump; 13650 13651 if ((wk = LIST_FIRST(wkhd)) == NULL) 13652 return; 13653 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 13654 ("softdep_buf_append called on non-softdep filesystem")); 13655 ump = VFSTOUFS(wk->wk_mp); 13656 ACQUIRE_LOCK(ump); 13657 while ((wk = LIST_FIRST(wkhd)) != NULL) { 13658 WORKLIST_REMOVE(wk); 13659 WORKLIST_INSERT(&bp->b_dep, wk); 13660 } 13661 FREE_LOCK(ump); 13662 13663 } 13664 13665 void 13666 softdep_inode_append(ip, cred, wkhd) 13667 struct inode *ip; 13668 struct ucred *cred; 13669 struct workhead *wkhd; 13670 { 13671 struct buf *bp; 13672 struct fs *fs; 13673 int error; 13674 13675 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0, 13676 ("softdep_inode_append called on non-softdep filesystem")); 13677 fs = ip->i_fs; 13678 error = bread(ip->i_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 13679 (int)fs->fs_bsize, cred, &bp); 13680 if (error) { 13681 bqrelse(bp); 13682 softdep_freework(wkhd); 13683 return; 13684 } 13685 softdep_buf_append(bp, wkhd); 13686 bqrelse(bp); 13687 } 13688 13689 void 13690 softdep_freework(wkhd) 13691 struct workhead *wkhd; 13692 { 13693 struct worklist *wk; 13694 struct ufsmount *ump; 13695 13696 if ((wk = LIST_FIRST(wkhd)) == NULL) 13697 return; 13698 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 13699 ("softdep_freework called on non-softdep filesystem")); 13700 ump = VFSTOUFS(wk->wk_mp); 13701 ACQUIRE_LOCK(ump); 13702 handle_jwork(wkhd); 13703 FREE_LOCK(ump); 13704 } 13705 13706 /* 13707 * Function to determine if the buffer has outstanding dependencies 13708 * that will cause a roll-back if the buffer is written. If wantcount 13709 * is set, return number of dependencies, otherwise just yes or no. 13710 */ 13711 static int 13712 softdep_count_dependencies(bp, wantcount) 13713 struct buf *bp; 13714 int wantcount; 13715 { 13716 struct worklist *wk; 13717 struct ufsmount *ump; 13718 struct bmsafemap *bmsafemap; 13719 struct freework *freework; 13720 struct inodedep *inodedep; 13721 struct indirdep *indirdep; 13722 struct freeblks *freeblks; 13723 struct allocindir *aip; 13724 struct pagedep *pagedep; 13725 struct dirrem *dirrem; 13726 struct newblk *newblk; 13727 struct mkdir *mkdir; 13728 struct diradd *dap; 13729 int i, retval; 13730 13731 retval = 0; 13732 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL) 13733 return (0); 13734 ump = VFSTOUFS(wk->wk_mp); 13735 ACQUIRE_LOCK(ump); 13736 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 13737 switch (wk->wk_type) { 13738 13739 case D_INODEDEP: 13740 inodedep = WK_INODEDEP(wk); 13741 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 13742 /* bitmap allocation dependency */ 13743 retval += 1; 13744 if (!wantcount) 13745 goto out; 13746 } 13747 if (TAILQ_FIRST(&inodedep->id_inoupdt)) { 13748 /* direct block pointer dependency */ 13749 retval += 1; 13750 if (!wantcount) 13751 goto out; 13752 } 13753 if (TAILQ_FIRST(&inodedep->id_extupdt)) { 13754 /* direct block pointer dependency */ 13755 retval += 1; 13756 if (!wantcount) 13757 goto out; 13758 } 13759 if (TAILQ_FIRST(&inodedep->id_inoreflst)) { 13760 /* Add reference dependency. */ 13761 retval += 1; 13762 if (!wantcount) 13763 goto out; 13764 } 13765 continue; 13766 13767 case D_INDIRDEP: 13768 indirdep = WK_INDIRDEP(wk); 13769 13770 TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) { 13771 /* indirect truncation dependency */ 13772 retval += 1; 13773 if (!wantcount) 13774 goto out; 13775 } 13776 13777 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 13778 /* indirect block pointer dependency */ 13779 retval += 1; 13780 if (!wantcount) 13781 goto out; 13782 } 13783 continue; 13784 13785 case D_PAGEDEP: 13786 pagedep = WK_PAGEDEP(wk); 13787 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) { 13788 if (LIST_FIRST(&dirrem->dm_jremrefhd)) { 13789 /* Journal remove ref dependency. */ 13790 retval += 1; 13791 if (!wantcount) 13792 goto out; 13793 } 13794 } 13795 for (i = 0; i < DAHASHSZ; i++) { 13796 13797 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 13798 /* directory entry dependency */ 13799 retval += 1; 13800 if (!wantcount) 13801 goto out; 13802 } 13803 } 13804 continue; 13805 13806 case D_BMSAFEMAP: 13807 bmsafemap = WK_BMSAFEMAP(wk); 13808 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) { 13809 /* Add reference dependency. */ 13810 retval += 1; 13811 if (!wantcount) 13812 goto out; 13813 } 13814 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) { 13815 /* Allocate block dependency. */ 13816 retval += 1; 13817 if (!wantcount) 13818 goto out; 13819 } 13820 continue; 13821 13822 case D_FREEBLKS: 13823 freeblks = WK_FREEBLKS(wk); 13824 if (LIST_FIRST(&freeblks->fb_jblkdephd)) { 13825 /* Freeblk journal dependency. */ 13826 retval += 1; 13827 if (!wantcount) 13828 goto out; 13829 } 13830 continue; 13831 13832 case D_ALLOCDIRECT: 13833 case D_ALLOCINDIR: 13834 newblk = WK_NEWBLK(wk); 13835 if (newblk->nb_jnewblk) { 13836 /* Journal allocate dependency. */ 13837 retval += 1; 13838 if (!wantcount) 13839 goto out; 13840 } 13841 continue; 13842 13843 case D_MKDIR: 13844 mkdir = WK_MKDIR(wk); 13845 if (mkdir->md_jaddref) { 13846 /* Journal reference dependency. */ 13847 retval += 1; 13848 if (!wantcount) 13849 goto out; 13850 } 13851 continue; 13852 13853 case D_FREEWORK: 13854 case D_FREEDEP: 13855 case D_JSEGDEP: 13856 case D_JSEG: 13857 case D_SBDEP: 13858 /* never a dependency on these blocks */ 13859 continue; 13860 13861 default: 13862 panic("softdep_count_dependencies: Unexpected type %s", 13863 TYPENAME(wk->wk_type)); 13864 /* NOTREACHED */ 13865 } 13866 } 13867 out: 13868 FREE_LOCK(ump); 13869 return retval; 13870 } 13871 13872 /* 13873 * Acquire exclusive access to a buffer. 13874 * Must be called with a locked mtx parameter. 13875 * Return acquired buffer or NULL on failure. 13876 */ 13877 static struct buf * 13878 getdirtybuf(bp, lock, waitfor) 13879 struct buf *bp; 13880 struct rwlock *lock; 13881 int waitfor; 13882 { 13883 int error; 13884 13885 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) { 13886 if (waitfor != MNT_WAIT) 13887 return (NULL); 13888 error = BUF_LOCK(bp, 13889 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock); 13890 /* 13891 * Even if we successfully acquire bp here, we have dropped 13892 * lock, which may violates our guarantee. 13893 */ 13894 if (error == 0) 13895 BUF_UNLOCK(bp); 13896 else if (error != ENOLCK) 13897 panic("getdirtybuf: inconsistent lock: %d", error); 13898 rw_wlock(lock); 13899 return (NULL); 13900 } 13901 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 13902 if (lock != BO_LOCKPTR(bp->b_bufobj) && waitfor == MNT_WAIT) { 13903 rw_wunlock(lock); 13904 BO_LOCK(bp->b_bufobj); 13905 BUF_UNLOCK(bp); 13906 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 13907 bp->b_vflags |= BV_BKGRDWAIT; 13908 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), 13909 PRIBIO | PDROP, "getbuf", 0); 13910 } else 13911 BO_UNLOCK(bp->b_bufobj); 13912 rw_wlock(lock); 13913 return (NULL); 13914 } 13915 BUF_UNLOCK(bp); 13916 if (waitfor != MNT_WAIT) 13917 return (NULL); 13918 /* 13919 * The lock argument must be bp->b_vp's mutex in 13920 * this case. 13921 */ 13922 #ifdef DEBUG_VFS_LOCKS 13923 if (bp->b_vp->v_type != VCHR) 13924 ASSERT_BO_WLOCKED(bp->b_bufobj); 13925 #endif 13926 bp->b_vflags |= BV_BKGRDWAIT; 13927 rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0); 13928 return (NULL); 13929 } 13930 if ((bp->b_flags & B_DELWRI) == 0) { 13931 BUF_UNLOCK(bp); 13932 return (NULL); 13933 } 13934 bremfree(bp); 13935 return (bp); 13936 } 13937 13938 13939 /* 13940 * Check if it is safe to suspend the file system now. On entry, 13941 * the vnode interlock for devvp should be held. Return 0 with 13942 * the mount interlock held if the file system can be suspended now, 13943 * otherwise return EAGAIN with the mount interlock held. 13944 */ 13945 int 13946 softdep_check_suspend(struct mount *mp, 13947 struct vnode *devvp, 13948 int softdep_depcnt, 13949 int softdep_accdepcnt, 13950 int secondary_writes, 13951 int secondary_accwrites) 13952 { 13953 struct bufobj *bo; 13954 struct ufsmount *ump; 13955 struct inodedep *inodedep; 13956 int error, unlinked; 13957 13958 bo = &devvp->v_bufobj; 13959 ASSERT_BO_WLOCKED(bo); 13960 13961 /* 13962 * If we are not running with soft updates, then we need only 13963 * deal with secondary writes as we try to suspend. 13964 */ 13965 if (MOUNTEDSOFTDEP(mp) == 0) { 13966 MNT_ILOCK(mp); 13967 while (mp->mnt_secondary_writes != 0) { 13968 BO_UNLOCK(bo); 13969 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 13970 (PUSER - 1) | PDROP, "secwr", 0); 13971 BO_LOCK(bo); 13972 MNT_ILOCK(mp); 13973 } 13974 13975 /* 13976 * Reasons for needing more work before suspend: 13977 * - Dirty buffers on devvp. 13978 * - Secondary writes occurred after start of vnode sync loop 13979 */ 13980 error = 0; 13981 if (bo->bo_numoutput > 0 || 13982 bo->bo_dirty.bv_cnt > 0 || 13983 secondary_writes != 0 || 13984 mp->mnt_secondary_writes != 0 || 13985 secondary_accwrites != mp->mnt_secondary_accwrites) 13986 error = EAGAIN; 13987 BO_UNLOCK(bo); 13988 return (error); 13989 } 13990 13991 /* 13992 * If we are running with soft updates, then we need to coordinate 13993 * with them as we try to suspend. 13994 */ 13995 ump = VFSTOUFS(mp); 13996 for (;;) { 13997 if (!TRY_ACQUIRE_LOCK(ump)) { 13998 BO_UNLOCK(bo); 13999 ACQUIRE_LOCK(ump); 14000 FREE_LOCK(ump); 14001 BO_LOCK(bo); 14002 continue; 14003 } 14004 MNT_ILOCK(mp); 14005 if (mp->mnt_secondary_writes != 0) { 14006 FREE_LOCK(ump); 14007 BO_UNLOCK(bo); 14008 msleep(&mp->mnt_secondary_writes, 14009 MNT_MTX(mp), 14010 (PUSER - 1) | PDROP, "secwr", 0); 14011 BO_LOCK(bo); 14012 continue; 14013 } 14014 break; 14015 } 14016 14017 unlinked = 0; 14018 if (MOUNTEDSUJ(mp)) { 14019 for (inodedep = TAILQ_FIRST(&ump->softdep_unlinked); 14020 inodedep != NULL; 14021 inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 14022 if ((inodedep->id_state & (UNLINKED | UNLINKLINKS | 14023 UNLINKONLIST)) != (UNLINKED | UNLINKLINKS | 14024 UNLINKONLIST) || 14025 !check_inodedep_free(inodedep)) 14026 continue; 14027 unlinked++; 14028 } 14029 } 14030 14031 /* 14032 * Reasons for needing more work before suspend: 14033 * - Dirty buffers on devvp. 14034 * - Softdep activity occurred after start of vnode sync loop 14035 * - Secondary writes occurred after start of vnode sync loop 14036 */ 14037 error = 0; 14038 if (bo->bo_numoutput > 0 || 14039 bo->bo_dirty.bv_cnt > 0 || 14040 softdep_depcnt != unlinked || 14041 ump->softdep_deps != unlinked || 14042 softdep_accdepcnt != ump->softdep_accdeps || 14043 secondary_writes != 0 || 14044 mp->mnt_secondary_writes != 0 || 14045 secondary_accwrites != mp->mnt_secondary_accwrites) 14046 error = EAGAIN; 14047 FREE_LOCK(ump); 14048 BO_UNLOCK(bo); 14049 return (error); 14050 } 14051 14052 14053 /* 14054 * Get the number of dependency structures for the file system, both 14055 * the current number and the total number allocated. These will 14056 * later be used to detect that softdep processing has occurred. 14057 */ 14058 void 14059 softdep_get_depcounts(struct mount *mp, 14060 int *softdep_depsp, 14061 int *softdep_accdepsp) 14062 { 14063 struct ufsmount *ump; 14064 14065 if (MOUNTEDSOFTDEP(mp) == 0) { 14066 *softdep_depsp = 0; 14067 *softdep_accdepsp = 0; 14068 return; 14069 } 14070 ump = VFSTOUFS(mp); 14071 ACQUIRE_LOCK(ump); 14072 *softdep_depsp = ump->softdep_deps; 14073 *softdep_accdepsp = ump->softdep_accdeps; 14074 FREE_LOCK(ump); 14075 } 14076 14077 /* 14078 * Wait for pending output on a vnode to complete. 14079 * Must be called with vnode lock and interlock locked. 14080 * 14081 * XXX: Should just be a call to bufobj_wwait(). 14082 */ 14083 static void 14084 drain_output(vp) 14085 struct vnode *vp; 14086 { 14087 struct bufobj *bo; 14088 14089 bo = &vp->v_bufobj; 14090 ASSERT_VOP_LOCKED(vp, "drain_output"); 14091 ASSERT_BO_WLOCKED(bo); 14092 14093 while (bo->bo_numoutput) { 14094 bo->bo_flag |= BO_WWAIT; 14095 msleep((caddr_t)&bo->bo_numoutput, 14096 BO_LOCKPTR(bo), PRIBIO + 1, "drainvp", 0); 14097 } 14098 } 14099 14100 /* 14101 * Called whenever a buffer that is being invalidated or reallocated 14102 * contains dependencies. This should only happen if an I/O error has 14103 * occurred. The routine is called with the buffer locked. 14104 */ 14105 static void 14106 softdep_deallocate_dependencies(bp) 14107 struct buf *bp; 14108 { 14109 14110 if ((bp->b_ioflags & BIO_ERROR) == 0) 14111 panic("softdep_deallocate_dependencies: dangling deps"); 14112 if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL) 14113 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error); 14114 else 14115 printf("softdep_deallocate_dependencies: " 14116 "got error %d while accessing filesystem\n", bp->b_error); 14117 if (bp->b_error != ENXIO) 14118 panic("softdep_deallocate_dependencies: unrecovered I/O error"); 14119 } 14120 14121 /* 14122 * Function to handle asynchronous write errors in the filesystem. 14123 */ 14124 static void 14125 softdep_error(func, error) 14126 char *func; 14127 int error; 14128 { 14129 14130 /* XXX should do something better! */ 14131 printf("%s: got error %d while accessing filesystem\n", func, error); 14132 } 14133 14134 #ifdef DDB 14135 14136 static void 14137 inodedep_print(struct inodedep *inodedep, int verbose) 14138 { 14139 db_printf("%p fs %p st %x ino %jd inoblk %jd delta %d nlink %d" 14140 " saveino %p\n", 14141 inodedep, inodedep->id_fs, inodedep->id_state, 14142 (intmax_t)inodedep->id_ino, 14143 (intmax_t)fsbtodb(inodedep->id_fs, 14144 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)), 14145 inodedep->id_nlinkdelta, inodedep->id_savednlink, 14146 inodedep->id_savedino1); 14147 14148 if (verbose == 0) 14149 return; 14150 14151 db_printf("\tpendinghd %p, bufwait %p, inowait %p, inoreflst %p, " 14152 "mkdiradd %p\n", 14153 LIST_FIRST(&inodedep->id_pendinghd), 14154 LIST_FIRST(&inodedep->id_bufwait), 14155 LIST_FIRST(&inodedep->id_inowait), 14156 TAILQ_FIRST(&inodedep->id_inoreflst), 14157 inodedep->id_mkdiradd); 14158 db_printf("\tinoupdt %p, newinoupdt %p, extupdt %p, newextupdt %p\n", 14159 TAILQ_FIRST(&inodedep->id_inoupdt), 14160 TAILQ_FIRST(&inodedep->id_newinoupdt), 14161 TAILQ_FIRST(&inodedep->id_extupdt), 14162 TAILQ_FIRST(&inodedep->id_newextupdt)); 14163 } 14164 14165 DB_SHOW_COMMAND(inodedep, db_show_inodedep) 14166 { 14167 14168 if (have_addr == 0) { 14169 db_printf("Address required\n"); 14170 return; 14171 } 14172 inodedep_print((struct inodedep*)addr, 1); 14173 } 14174 14175 DB_SHOW_COMMAND(inodedeps, db_show_inodedeps) 14176 { 14177 struct inodedep_hashhead *inodedephd; 14178 struct inodedep *inodedep; 14179 struct ufsmount *ump; 14180 int cnt; 14181 14182 if (have_addr == 0) { 14183 db_printf("Address required\n"); 14184 return; 14185 } 14186 ump = (struct ufsmount *)addr; 14187 for (cnt = 0; cnt < ump->inodedep_hash_size; cnt++) { 14188 inodedephd = &ump->inodedep_hashtbl[cnt]; 14189 LIST_FOREACH(inodedep, inodedephd, id_hash) { 14190 inodedep_print(inodedep, 0); 14191 } 14192 } 14193 } 14194 14195 DB_SHOW_COMMAND(worklist, db_show_worklist) 14196 { 14197 struct worklist *wk; 14198 14199 if (have_addr == 0) { 14200 db_printf("Address required\n"); 14201 return; 14202 } 14203 wk = (struct worklist *)addr; 14204 printf("worklist: %p type %s state 0x%X\n", 14205 wk, TYPENAME(wk->wk_type), wk->wk_state); 14206 } 14207 14208 DB_SHOW_COMMAND(workhead, db_show_workhead) 14209 { 14210 struct workhead *wkhd; 14211 struct worklist *wk; 14212 int i; 14213 14214 if (have_addr == 0) { 14215 db_printf("Address required\n"); 14216 return; 14217 } 14218 wkhd = (struct workhead *)addr; 14219 wk = LIST_FIRST(wkhd); 14220 for (i = 0; i < 100 && wk != NULL; i++, wk = LIST_NEXT(wk, wk_list)) 14221 db_printf("worklist: %p type %s state 0x%X", 14222 wk, TYPENAME(wk->wk_type), wk->wk_state); 14223 if (i == 100) 14224 db_printf("workhead overflow"); 14225 printf("\n"); 14226 } 14227 14228 14229 DB_SHOW_COMMAND(mkdirs, db_show_mkdirs) 14230 { 14231 struct mkdirlist *mkdirlisthd; 14232 struct jaddref *jaddref; 14233 struct diradd *diradd; 14234 struct mkdir *mkdir; 14235 14236 if (have_addr == 0) { 14237 db_printf("Address required\n"); 14238 return; 14239 } 14240 mkdirlisthd = (struct mkdirlist *)addr; 14241 LIST_FOREACH(mkdir, mkdirlisthd, md_mkdirs) { 14242 diradd = mkdir->md_diradd; 14243 db_printf("mkdir: %p state 0x%X dap %p state 0x%X", 14244 mkdir, mkdir->md_state, diradd, diradd->da_state); 14245 if ((jaddref = mkdir->md_jaddref) != NULL) 14246 db_printf(" jaddref %p jaddref state 0x%X", 14247 jaddref, jaddref->ja_state); 14248 db_printf("\n"); 14249 } 14250 } 14251 14252 /* exported to ffs_vfsops.c */ 14253 extern void db_print_ffs(struct ufsmount *ump); 14254 void 14255 db_print_ffs(struct ufsmount *ump) 14256 { 14257 db_printf("mp %p %s devvp %p fs %p su_wl %d su_deps %d su_req %d\n", 14258 ump->um_mountp, ump->um_mountp->mnt_stat.f_mntonname, 14259 ump->um_devvp, ump->um_fs, ump->softdep_on_worklist, 14260 ump->softdep_deps, ump->softdep_req); 14261 } 14262 14263 #endif /* DDB */ 14264 14265 #endif /* SOFTUPDATES */ 14266