1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright 1998, 2000 Marshall Kirk McKusick. 5 * Copyright 2009, 2010 Jeffrey W. Roberson <jeff@FreeBSD.org> 6 * All rights reserved. 7 * 8 * The soft updates code is derived from the appendix of a University 9 * of Michigan technical report (Gregory R. Ganger and Yale N. Patt, 10 * "Soft Updates: A Solution to the Metadata Update Problem in File 11 * Systems", CSE-TR-254-95, August 1995). 12 * 13 * Further information about soft updates can be obtained from: 14 * 15 * Marshall Kirk McKusick http://www.mckusick.com/softdep/ 16 * 1614 Oxford Street mckusick@mckusick.com 17 * Berkeley, CA 94709-1608 +1-510-843-9542 18 * USA 19 * 20 * Redistribution and use in source and binary forms, with or without 21 * modification, are permitted provided that the following conditions 22 * are met: 23 * 24 * 1. Redistributions of source code must retain the above copyright 25 * notice, this list of conditions and the following disclaimer. 26 * 2. Redistributions in binary form must reproduce the above copyright 27 * notice, this list of conditions and the following disclaimer in the 28 * documentation and/or other materials provided with the distribution. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 33 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 35 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 36 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 37 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 38 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 39 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * from: @(#)ffs_softdep.c 9.59 (McKusick) 6/21/00 42 */ 43 44 #include <sys/cdefs.h> 45 __FBSDID("$FreeBSD$"); 46 47 #include "opt_ffs.h" 48 #include "opt_quota.h" 49 #include "opt_ddb.h" 50 51 /* 52 * For now we want the safety net that the DEBUG flag provides. 53 */ 54 #ifndef DEBUG 55 #define DEBUG 56 #endif 57 58 #include <sys/param.h> 59 #include <sys/kernel.h> 60 #include <sys/systm.h> 61 #include <sys/bio.h> 62 #include <sys/buf.h> 63 #include <sys/kdb.h> 64 #include <sys/kthread.h> 65 #include <sys/ktr.h> 66 #include <sys/limits.h> 67 #include <sys/lock.h> 68 #include <sys/malloc.h> 69 #include <sys/mount.h> 70 #include <sys/mutex.h> 71 #include <sys/namei.h> 72 #include <sys/priv.h> 73 #include <sys/proc.h> 74 #include <sys/racct.h> 75 #include <sys/rwlock.h> 76 #include <sys/stat.h> 77 #include <sys/sysctl.h> 78 #include <sys/syslog.h> 79 #include <sys/vnode.h> 80 #include <sys/conf.h> 81 82 #include <ufs/ufs/dir.h> 83 #include <ufs/ufs/extattr.h> 84 #include <ufs/ufs/quota.h> 85 #include <ufs/ufs/inode.h> 86 #include <ufs/ufs/ufsmount.h> 87 #include <ufs/ffs/fs.h> 88 #include <ufs/ffs/softdep.h> 89 #include <ufs/ffs/ffs_extern.h> 90 #include <ufs/ufs/ufs_extern.h> 91 92 #include <vm/vm.h> 93 #include <vm/vm_extern.h> 94 #include <vm/vm_object.h> 95 96 #include <geom/geom.h> 97 98 #include <ddb/ddb.h> 99 100 #define KTR_SUJ 0 /* Define to KTR_SPARE. */ 101 102 #ifndef SOFTUPDATES 103 104 int 105 softdep_flushfiles(oldmnt, flags, td) 106 struct mount *oldmnt; 107 int flags; 108 struct thread *td; 109 { 110 111 panic("softdep_flushfiles called"); 112 } 113 114 int 115 softdep_mount(devvp, mp, fs, cred) 116 struct vnode *devvp; 117 struct mount *mp; 118 struct fs *fs; 119 struct ucred *cred; 120 { 121 122 return (0); 123 } 124 125 void 126 softdep_initialize() 127 { 128 129 return; 130 } 131 132 void 133 softdep_uninitialize() 134 { 135 136 return; 137 } 138 139 void 140 softdep_unmount(mp) 141 struct mount *mp; 142 { 143 144 panic("softdep_unmount called"); 145 } 146 147 void 148 softdep_setup_sbupdate(ump, fs, bp) 149 struct ufsmount *ump; 150 struct fs *fs; 151 struct buf *bp; 152 { 153 154 panic("softdep_setup_sbupdate called"); 155 } 156 157 void 158 softdep_setup_inomapdep(bp, ip, newinum, mode) 159 struct buf *bp; 160 struct inode *ip; 161 ino_t newinum; 162 int mode; 163 { 164 165 panic("softdep_setup_inomapdep called"); 166 } 167 168 void 169 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags) 170 struct buf *bp; 171 struct mount *mp; 172 ufs2_daddr_t newblkno; 173 int frags; 174 int oldfrags; 175 { 176 177 panic("softdep_setup_blkmapdep called"); 178 } 179 180 void 181 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 182 struct inode *ip; 183 ufs_lbn_t lbn; 184 ufs2_daddr_t newblkno; 185 ufs2_daddr_t oldblkno; 186 long newsize; 187 long oldsize; 188 struct buf *bp; 189 { 190 191 panic("softdep_setup_allocdirect called"); 192 } 193 194 void 195 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 196 struct inode *ip; 197 ufs_lbn_t lbn; 198 ufs2_daddr_t newblkno; 199 ufs2_daddr_t oldblkno; 200 long newsize; 201 long oldsize; 202 struct buf *bp; 203 { 204 205 panic("softdep_setup_allocext called"); 206 } 207 208 void 209 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 210 struct inode *ip; 211 ufs_lbn_t lbn; 212 struct buf *bp; 213 int ptrno; 214 ufs2_daddr_t newblkno; 215 ufs2_daddr_t oldblkno; 216 struct buf *nbp; 217 { 218 219 panic("softdep_setup_allocindir_page called"); 220 } 221 222 void 223 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 224 struct buf *nbp; 225 struct inode *ip; 226 struct buf *bp; 227 int ptrno; 228 ufs2_daddr_t newblkno; 229 { 230 231 panic("softdep_setup_allocindir_meta called"); 232 } 233 234 void 235 softdep_journal_freeblocks(ip, cred, length, flags) 236 struct inode *ip; 237 struct ucred *cred; 238 off_t length; 239 int flags; 240 { 241 242 panic("softdep_journal_freeblocks called"); 243 } 244 245 void 246 softdep_journal_fsync(ip) 247 struct inode *ip; 248 { 249 250 panic("softdep_journal_fsync called"); 251 } 252 253 void 254 softdep_setup_freeblocks(ip, length, flags) 255 struct inode *ip; 256 off_t length; 257 int flags; 258 { 259 260 panic("softdep_setup_freeblocks called"); 261 } 262 263 void 264 softdep_freefile(pvp, ino, mode) 265 struct vnode *pvp; 266 ino_t ino; 267 int mode; 268 { 269 270 panic("softdep_freefile called"); 271 } 272 273 int 274 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 275 struct buf *bp; 276 struct inode *dp; 277 off_t diroffset; 278 ino_t newinum; 279 struct buf *newdirbp; 280 int isnewblk; 281 { 282 283 panic("softdep_setup_directory_add called"); 284 } 285 286 void 287 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 288 struct buf *bp; 289 struct inode *dp; 290 caddr_t base; 291 caddr_t oldloc; 292 caddr_t newloc; 293 int entrysize; 294 { 295 296 panic("softdep_change_directoryentry_offset called"); 297 } 298 299 void 300 softdep_setup_remove(bp, dp, ip, isrmdir) 301 struct buf *bp; 302 struct inode *dp; 303 struct inode *ip; 304 int isrmdir; 305 { 306 307 panic("softdep_setup_remove called"); 308 } 309 310 void 311 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 312 struct buf *bp; 313 struct inode *dp; 314 struct inode *ip; 315 ino_t newinum; 316 int isrmdir; 317 { 318 319 panic("softdep_setup_directory_change called"); 320 } 321 322 void 323 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 324 struct mount *mp; 325 struct buf *bp; 326 ufs2_daddr_t blkno; 327 int frags; 328 struct workhead *wkhd; 329 { 330 331 panic("%s called", __FUNCTION__); 332 } 333 334 void 335 softdep_setup_inofree(mp, bp, ino, wkhd) 336 struct mount *mp; 337 struct buf *bp; 338 ino_t ino; 339 struct workhead *wkhd; 340 { 341 342 panic("%s called", __FUNCTION__); 343 } 344 345 void 346 softdep_setup_unlink(dp, ip) 347 struct inode *dp; 348 struct inode *ip; 349 { 350 351 panic("%s called", __FUNCTION__); 352 } 353 354 void 355 softdep_setup_link(dp, ip) 356 struct inode *dp; 357 struct inode *ip; 358 { 359 360 panic("%s called", __FUNCTION__); 361 } 362 363 void 364 softdep_revert_link(dp, ip) 365 struct inode *dp; 366 struct inode *ip; 367 { 368 369 panic("%s called", __FUNCTION__); 370 } 371 372 void 373 softdep_setup_rmdir(dp, ip) 374 struct inode *dp; 375 struct inode *ip; 376 { 377 378 panic("%s called", __FUNCTION__); 379 } 380 381 void 382 softdep_revert_rmdir(dp, ip) 383 struct inode *dp; 384 struct inode *ip; 385 { 386 387 panic("%s called", __FUNCTION__); 388 } 389 390 void 391 softdep_setup_create(dp, ip) 392 struct inode *dp; 393 struct inode *ip; 394 { 395 396 panic("%s called", __FUNCTION__); 397 } 398 399 void 400 softdep_revert_create(dp, ip) 401 struct inode *dp; 402 struct inode *ip; 403 { 404 405 panic("%s called", __FUNCTION__); 406 } 407 408 void 409 softdep_setup_mkdir(dp, ip) 410 struct inode *dp; 411 struct inode *ip; 412 { 413 414 panic("%s called", __FUNCTION__); 415 } 416 417 void 418 softdep_revert_mkdir(dp, ip) 419 struct inode *dp; 420 struct inode *ip; 421 { 422 423 panic("%s called", __FUNCTION__); 424 } 425 426 void 427 softdep_setup_dotdot_link(dp, ip) 428 struct inode *dp; 429 struct inode *ip; 430 { 431 432 panic("%s called", __FUNCTION__); 433 } 434 435 int 436 softdep_prealloc(vp, waitok) 437 struct vnode *vp; 438 int waitok; 439 { 440 441 panic("%s called", __FUNCTION__); 442 } 443 444 int 445 softdep_journal_lookup(mp, vpp) 446 struct mount *mp; 447 struct vnode **vpp; 448 { 449 450 return (ENOENT); 451 } 452 453 void 454 softdep_change_linkcnt(ip) 455 struct inode *ip; 456 { 457 458 panic("softdep_change_linkcnt called"); 459 } 460 461 void 462 softdep_load_inodeblock(ip) 463 struct inode *ip; 464 { 465 466 panic("softdep_load_inodeblock called"); 467 } 468 469 void 470 softdep_update_inodeblock(ip, bp, waitfor) 471 struct inode *ip; 472 struct buf *bp; 473 int waitfor; 474 { 475 476 panic("softdep_update_inodeblock called"); 477 } 478 479 int 480 softdep_fsync(vp) 481 struct vnode *vp; /* the "in_core" copy of the inode */ 482 { 483 484 return (0); 485 } 486 487 void 488 softdep_fsync_mountdev(vp) 489 struct vnode *vp; 490 { 491 492 return; 493 } 494 495 int 496 softdep_flushworklist(oldmnt, countp, td) 497 struct mount *oldmnt; 498 int *countp; 499 struct thread *td; 500 { 501 502 *countp = 0; 503 return (0); 504 } 505 506 int 507 softdep_sync_metadata(struct vnode *vp) 508 { 509 510 panic("softdep_sync_metadata called"); 511 } 512 513 int 514 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 515 { 516 517 panic("softdep_sync_buf called"); 518 } 519 520 int 521 softdep_slowdown(vp) 522 struct vnode *vp; 523 { 524 525 panic("softdep_slowdown called"); 526 } 527 528 int 529 softdep_request_cleanup(fs, vp, cred, resource) 530 struct fs *fs; 531 struct vnode *vp; 532 struct ucred *cred; 533 int resource; 534 { 535 536 return (0); 537 } 538 539 int 540 softdep_check_suspend(struct mount *mp, 541 struct vnode *devvp, 542 int softdep_depcnt, 543 int softdep_accdepcnt, 544 int secondary_writes, 545 int secondary_accwrites) 546 { 547 struct bufobj *bo; 548 int error; 549 550 (void) softdep_depcnt, 551 (void) softdep_accdepcnt; 552 553 bo = &devvp->v_bufobj; 554 ASSERT_BO_WLOCKED(bo); 555 556 MNT_ILOCK(mp); 557 while (mp->mnt_secondary_writes != 0) { 558 BO_UNLOCK(bo); 559 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 560 (PUSER - 1) | PDROP, "secwr", 0); 561 BO_LOCK(bo); 562 MNT_ILOCK(mp); 563 } 564 565 /* 566 * Reasons for needing more work before suspend: 567 * - Dirty buffers on devvp. 568 * - Secondary writes occurred after start of vnode sync loop 569 */ 570 error = 0; 571 if (bo->bo_numoutput > 0 || 572 bo->bo_dirty.bv_cnt > 0 || 573 secondary_writes != 0 || 574 mp->mnt_secondary_writes != 0 || 575 secondary_accwrites != mp->mnt_secondary_accwrites) 576 error = EAGAIN; 577 BO_UNLOCK(bo); 578 return (error); 579 } 580 581 void 582 softdep_get_depcounts(struct mount *mp, 583 int *softdepactivep, 584 int *softdepactiveaccp) 585 { 586 (void) mp; 587 *softdepactivep = 0; 588 *softdepactiveaccp = 0; 589 } 590 591 void 592 softdep_buf_append(bp, wkhd) 593 struct buf *bp; 594 struct workhead *wkhd; 595 { 596 597 panic("softdep_buf_appendwork called"); 598 } 599 600 void 601 softdep_inode_append(ip, cred, wkhd) 602 struct inode *ip; 603 struct ucred *cred; 604 struct workhead *wkhd; 605 { 606 607 panic("softdep_inode_appendwork called"); 608 } 609 610 void 611 softdep_freework(wkhd) 612 struct workhead *wkhd; 613 { 614 615 panic("softdep_freework called"); 616 } 617 618 #else 619 620 FEATURE(softupdates, "FFS soft-updates support"); 621 622 static SYSCTL_NODE(_debug, OID_AUTO, softdep, CTLFLAG_RW, 0, 623 "soft updates stats"); 624 static SYSCTL_NODE(_debug_softdep, OID_AUTO, total, CTLFLAG_RW, 0, 625 "total dependencies allocated"); 626 static SYSCTL_NODE(_debug_softdep, OID_AUTO, highuse, CTLFLAG_RW, 0, 627 "high use dependencies allocated"); 628 static SYSCTL_NODE(_debug_softdep, OID_AUTO, current, CTLFLAG_RW, 0, 629 "current dependencies allocated"); 630 static SYSCTL_NODE(_debug_softdep, OID_AUTO, write, CTLFLAG_RW, 0, 631 "current dependencies written"); 632 633 unsigned long dep_current[D_LAST + 1]; 634 unsigned long dep_highuse[D_LAST + 1]; 635 unsigned long dep_total[D_LAST + 1]; 636 unsigned long dep_write[D_LAST + 1]; 637 638 #define SOFTDEP_TYPE(type, str, long) \ 639 static MALLOC_DEFINE(M_ ## type, #str, long); \ 640 SYSCTL_ULONG(_debug_softdep_total, OID_AUTO, str, CTLFLAG_RD, \ 641 &dep_total[D_ ## type], 0, ""); \ 642 SYSCTL_ULONG(_debug_softdep_current, OID_AUTO, str, CTLFLAG_RD, \ 643 &dep_current[D_ ## type], 0, ""); \ 644 SYSCTL_ULONG(_debug_softdep_highuse, OID_AUTO, str, CTLFLAG_RD, \ 645 &dep_highuse[D_ ## type], 0, ""); \ 646 SYSCTL_ULONG(_debug_softdep_write, OID_AUTO, str, CTLFLAG_RD, \ 647 &dep_write[D_ ## type], 0, ""); 648 649 SOFTDEP_TYPE(PAGEDEP, pagedep, "File page dependencies"); 650 SOFTDEP_TYPE(INODEDEP, inodedep, "Inode dependencies"); 651 SOFTDEP_TYPE(BMSAFEMAP, bmsafemap, 652 "Block or frag allocated from cyl group map"); 653 SOFTDEP_TYPE(NEWBLK, newblk, "New block or frag allocation dependency"); 654 SOFTDEP_TYPE(ALLOCDIRECT, allocdirect, "Block or frag dependency for an inode"); 655 SOFTDEP_TYPE(INDIRDEP, indirdep, "Indirect block dependencies"); 656 SOFTDEP_TYPE(ALLOCINDIR, allocindir, "Block dependency for an indirect block"); 657 SOFTDEP_TYPE(FREEFRAG, freefrag, "Previously used frag for an inode"); 658 SOFTDEP_TYPE(FREEBLKS, freeblks, "Blocks freed from an inode"); 659 SOFTDEP_TYPE(FREEFILE, freefile, "Inode deallocated"); 660 SOFTDEP_TYPE(DIRADD, diradd, "New directory entry"); 661 SOFTDEP_TYPE(MKDIR, mkdir, "New directory"); 662 SOFTDEP_TYPE(DIRREM, dirrem, "Directory entry deleted"); 663 SOFTDEP_TYPE(NEWDIRBLK, newdirblk, "Unclaimed new directory block"); 664 SOFTDEP_TYPE(FREEWORK, freework, "free an inode block"); 665 SOFTDEP_TYPE(FREEDEP, freedep, "track a block free"); 666 SOFTDEP_TYPE(JADDREF, jaddref, "Journal inode ref add"); 667 SOFTDEP_TYPE(JREMREF, jremref, "Journal inode ref remove"); 668 SOFTDEP_TYPE(JMVREF, jmvref, "Journal inode ref move"); 669 SOFTDEP_TYPE(JNEWBLK, jnewblk, "Journal new block"); 670 SOFTDEP_TYPE(JFREEBLK, jfreeblk, "Journal free block"); 671 SOFTDEP_TYPE(JFREEFRAG, jfreefrag, "Journal free frag"); 672 SOFTDEP_TYPE(JSEG, jseg, "Journal segment"); 673 SOFTDEP_TYPE(JSEGDEP, jsegdep, "Journal segment complete"); 674 SOFTDEP_TYPE(SBDEP, sbdep, "Superblock write dependency"); 675 SOFTDEP_TYPE(JTRUNC, jtrunc, "Journal inode truncation"); 676 SOFTDEP_TYPE(JFSYNC, jfsync, "Journal fsync complete"); 677 678 static MALLOC_DEFINE(M_SENTINEL, "sentinel", "Worklist sentinel"); 679 680 static MALLOC_DEFINE(M_SAVEDINO, "savedino", "Saved inodes"); 681 static MALLOC_DEFINE(M_JBLOCKS, "jblocks", "Journal block locations"); 682 static MALLOC_DEFINE(M_MOUNTDATA, "softdep", "Softdep per-mount data"); 683 684 #define M_SOFTDEP_FLAGS (M_WAITOK) 685 686 /* 687 * translate from workitem type to memory type 688 * MUST match the defines above, such that memtype[D_XXX] == M_XXX 689 */ 690 static struct malloc_type *memtype[] = { 691 M_PAGEDEP, 692 M_INODEDEP, 693 M_BMSAFEMAP, 694 M_NEWBLK, 695 M_ALLOCDIRECT, 696 M_INDIRDEP, 697 M_ALLOCINDIR, 698 M_FREEFRAG, 699 M_FREEBLKS, 700 M_FREEFILE, 701 M_DIRADD, 702 M_MKDIR, 703 M_DIRREM, 704 M_NEWDIRBLK, 705 M_FREEWORK, 706 M_FREEDEP, 707 M_JADDREF, 708 M_JREMREF, 709 M_JMVREF, 710 M_JNEWBLK, 711 M_JFREEBLK, 712 M_JFREEFRAG, 713 M_JSEG, 714 M_JSEGDEP, 715 M_SBDEP, 716 M_JTRUNC, 717 M_JFSYNC, 718 M_SENTINEL 719 }; 720 721 #define DtoM(type) (memtype[type]) 722 723 /* 724 * Names of malloc types. 725 */ 726 #define TYPENAME(type) \ 727 ((unsigned)(type) <= D_LAST ? memtype[type]->ks_shortdesc : "???") 728 /* 729 * End system adaptation definitions. 730 */ 731 732 #define DOTDOT_OFFSET offsetof(struct dirtemplate, dotdot_ino) 733 #define DOT_OFFSET offsetof(struct dirtemplate, dot_ino) 734 735 /* 736 * Internal function prototypes. 737 */ 738 static void check_clear_deps(struct mount *); 739 static void softdep_error(char *, int); 740 static int softdep_process_worklist(struct mount *, int); 741 static int softdep_waitidle(struct mount *, int); 742 static void drain_output(struct vnode *); 743 static struct buf *getdirtybuf(struct buf *, struct rwlock *, int); 744 static int check_inodedep_free(struct inodedep *); 745 static void clear_remove(struct mount *); 746 static void clear_inodedeps(struct mount *); 747 static void unlinked_inodedep(struct mount *, struct inodedep *); 748 static void clear_unlinked_inodedep(struct inodedep *); 749 static struct inodedep *first_unlinked_inodedep(struct ufsmount *); 750 static int flush_pagedep_deps(struct vnode *, struct mount *, 751 struct diraddhd *); 752 static int free_pagedep(struct pagedep *); 753 static int flush_newblk_dep(struct vnode *, struct mount *, ufs_lbn_t); 754 static int flush_inodedep_deps(struct vnode *, struct mount *, ino_t); 755 static int flush_deplist(struct allocdirectlst *, int, int *); 756 static int sync_cgs(struct mount *, int); 757 static int handle_written_filepage(struct pagedep *, struct buf *, int); 758 static int handle_written_sbdep(struct sbdep *, struct buf *); 759 static void initiate_write_sbdep(struct sbdep *); 760 static void diradd_inode_written(struct diradd *, struct inodedep *); 761 static int handle_written_indirdep(struct indirdep *, struct buf *, 762 struct buf**, int); 763 static int handle_written_inodeblock(struct inodedep *, struct buf *, int); 764 static int jnewblk_rollforward(struct jnewblk *, struct fs *, struct cg *, 765 uint8_t *); 766 static int handle_written_bmsafemap(struct bmsafemap *, struct buf *, int); 767 static void handle_written_jaddref(struct jaddref *); 768 static void handle_written_jremref(struct jremref *); 769 static void handle_written_jseg(struct jseg *, struct buf *); 770 static void handle_written_jnewblk(struct jnewblk *); 771 static void handle_written_jblkdep(struct jblkdep *); 772 static void handle_written_jfreefrag(struct jfreefrag *); 773 static void complete_jseg(struct jseg *); 774 static void complete_jsegs(struct jseg *); 775 static void jseg_write(struct ufsmount *ump, struct jseg *, uint8_t *); 776 static void jaddref_write(struct jaddref *, struct jseg *, uint8_t *); 777 static void jremref_write(struct jremref *, struct jseg *, uint8_t *); 778 static void jmvref_write(struct jmvref *, struct jseg *, uint8_t *); 779 static void jtrunc_write(struct jtrunc *, struct jseg *, uint8_t *); 780 static void jfsync_write(struct jfsync *, struct jseg *, uint8_t *data); 781 static void jnewblk_write(struct jnewblk *, struct jseg *, uint8_t *); 782 static void jfreeblk_write(struct jfreeblk *, struct jseg *, uint8_t *); 783 static void jfreefrag_write(struct jfreefrag *, struct jseg *, uint8_t *); 784 static inline void inoref_write(struct inoref *, struct jseg *, 785 struct jrefrec *); 786 static void handle_allocdirect_partdone(struct allocdirect *, 787 struct workhead *); 788 static struct jnewblk *cancel_newblk(struct newblk *, struct worklist *, 789 struct workhead *); 790 static void indirdep_complete(struct indirdep *); 791 static int indirblk_lookup(struct mount *, ufs2_daddr_t); 792 static void indirblk_insert(struct freework *); 793 static void indirblk_remove(struct freework *); 794 static void handle_allocindir_partdone(struct allocindir *); 795 static void initiate_write_filepage(struct pagedep *, struct buf *); 796 static void initiate_write_indirdep(struct indirdep*, struct buf *); 797 static void handle_written_mkdir(struct mkdir *, int); 798 static int jnewblk_rollback(struct jnewblk *, struct fs *, struct cg *, 799 uint8_t *); 800 static void initiate_write_bmsafemap(struct bmsafemap *, struct buf *); 801 static void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *); 802 static void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *); 803 static void handle_workitem_freefile(struct freefile *); 804 static int handle_workitem_remove(struct dirrem *, int); 805 static struct dirrem *newdirrem(struct buf *, struct inode *, 806 struct inode *, int, struct dirrem **); 807 static struct indirdep *indirdep_lookup(struct mount *, struct inode *, 808 struct buf *); 809 static void cancel_indirdep(struct indirdep *, struct buf *, 810 struct freeblks *); 811 static void free_indirdep(struct indirdep *); 812 static void free_diradd(struct diradd *, struct workhead *); 813 static void merge_diradd(struct inodedep *, struct diradd *); 814 static void complete_diradd(struct diradd *); 815 static struct diradd *diradd_lookup(struct pagedep *, int); 816 static struct jremref *cancel_diradd_dotdot(struct inode *, struct dirrem *, 817 struct jremref *); 818 static struct jremref *cancel_mkdir_dotdot(struct inode *, struct dirrem *, 819 struct jremref *); 820 static void cancel_diradd(struct diradd *, struct dirrem *, struct jremref *, 821 struct jremref *, struct jremref *); 822 static void dirrem_journal(struct dirrem *, struct jremref *, struct jremref *, 823 struct jremref *); 824 static void cancel_allocindir(struct allocindir *, struct buf *bp, 825 struct freeblks *, int); 826 static int setup_trunc_indir(struct freeblks *, struct inode *, 827 ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t); 828 static void complete_trunc_indir(struct freework *); 829 static void trunc_indirdep(struct indirdep *, struct freeblks *, struct buf *, 830 int); 831 static void complete_mkdir(struct mkdir *); 832 static void free_newdirblk(struct newdirblk *); 833 static void free_jremref(struct jremref *); 834 static void free_jaddref(struct jaddref *); 835 static void free_jsegdep(struct jsegdep *); 836 static void free_jsegs(struct jblocks *); 837 static void rele_jseg(struct jseg *); 838 static void free_jseg(struct jseg *, struct jblocks *); 839 static void free_jnewblk(struct jnewblk *); 840 static void free_jblkdep(struct jblkdep *); 841 static void free_jfreefrag(struct jfreefrag *); 842 static void free_freedep(struct freedep *); 843 static void journal_jremref(struct dirrem *, struct jremref *, 844 struct inodedep *); 845 static void cancel_jnewblk(struct jnewblk *, struct workhead *); 846 static int cancel_jaddref(struct jaddref *, struct inodedep *, 847 struct workhead *); 848 static void cancel_jfreefrag(struct jfreefrag *); 849 static inline void setup_freedirect(struct freeblks *, struct inode *, 850 int, int); 851 static inline void setup_freeext(struct freeblks *, struct inode *, int, int); 852 static inline void setup_freeindir(struct freeblks *, struct inode *, int, 853 ufs_lbn_t, int); 854 static inline struct freeblks *newfreeblks(struct mount *, struct inode *); 855 static void freeblks_free(struct ufsmount *, struct freeblks *, int); 856 static void indir_trunc(struct freework *, ufs2_daddr_t, ufs_lbn_t); 857 static ufs2_daddr_t blkcount(struct fs *, ufs2_daddr_t, off_t); 858 static int trunc_check_buf(struct buf *, int *, ufs_lbn_t, int, int); 859 static void trunc_dependencies(struct inode *, struct freeblks *, ufs_lbn_t, 860 int, int); 861 static void trunc_pages(struct inode *, off_t, ufs2_daddr_t, int); 862 static int cancel_pagedep(struct pagedep *, struct freeblks *, int); 863 static int deallocate_dependencies(struct buf *, struct freeblks *, int); 864 static void newblk_freefrag(struct newblk*); 865 static void free_newblk(struct newblk *); 866 static void cancel_allocdirect(struct allocdirectlst *, 867 struct allocdirect *, struct freeblks *); 868 static int check_inode_unwritten(struct inodedep *); 869 static int free_inodedep(struct inodedep *); 870 static void freework_freeblock(struct freework *); 871 static void freework_enqueue(struct freework *); 872 static int handle_workitem_freeblocks(struct freeblks *, int); 873 static int handle_complete_freeblocks(struct freeblks *, int); 874 static void handle_workitem_indirblk(struct freework *); 875 static void handle_written_freework(struct freework *); 876 static void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *); 877 static struct worklist *jnewblk_merge(struct worklist *, struct worklist *, 878 struct workhead *); 879 static struct freefrag *setup_allocindir_phase2(struct buf *, struct inode *, 880 struct inodedep *, struct allocindir *, ufs_lbn_t); 881 static struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t, 882 ufs2_daddr_t, ufs_lbn_t); 883 static void handle_workitem_freefrag(struct freefrag *); 884 static struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long, 885 ufs_lbn_t); 886 static void allocdirect_merge(struct allocdirectlst *, 887 struct allocdirect *, struct allocdirect *); 888 static struct freefrag *allocindir_merge(struct allocindir *, 889 struct allocindir *); 890 static int bmsafemap_find(struct bmsafemap_hashhead *, int, 891 struct bmsafemap **); 892 static struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *, 893 int cg, struct bmsafemap *); 894 static int newblk_find(struct newblk_hashhead *, ufs2_daddr_t, int, 895 struct newblk **); 896 static int newblk_lookup(struct mount *, ufs2_daddr_t, int, struct newblk **); 897 static int inodedep_find(struct inodedep_hashhead *, ino_t, 898 struct inodedep **); 899 static int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **); 900 static int pagedep_lookup(struct mount *, struct buf *bp, ino_t, ufs_lbn_t, 901 int, struct pagedep **); 902 static int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t, 903 struct pagedep **); 904 static void pause_timer(void *); 905 static int request_cleanup(struct mount *, int); 906 static int softdep_request_cleanup_flush(struct mount *, struct ufsmount *); 907 static void schedule_cleanup(struct mount *); 908 static void softdep_ast_cleanup_proc(struct thread *); 909 static int process_worklist_item(struct mount *, int, int); 910 static void process_removes(struct vnode *); 911 static void process_truncates(struct vnode *); 912 static void jwork_move(struct workhead *, struct workhead *); 913 static void jwork_insert(struct workhead *, struct jsegdep *); 914 static void add_to_worklist(struct worklist *, int); 915 static void wake_worklist(struct worklist *); 916 static void wait_worklist(struct worklist *, char *); 917 static void remove_from_worklist(struct worklist *); 918 static void softdep_flush(void *); 919 static void softdep_flushjournal(struct mount *); 920 static int softdep_speedup(struct ufsmount *); 921 static void worklist_speedup(struct mount *); 922 static int journal_mount(struct mount *, struct fs *, struct ucred *); 923 static void journal_unmount(struct ufsmount *); 924 static int journal_space(struct ufsmount *, int); 925 static void journal_suspend(struct ufsmount *); 926 static int journal_unsuspend(struct ufsmount *ump); 927 static void softdep_prelink(struct vnode *, struct vnode *); 928 static void add_to_journal(struct worklist *); 929 static void remove_from_journal(struct worklist *); 930 static bool softdep_excess_items(struct ufsmount *, int); 931 static void softdep_process_journal(struct mount *, struct worklist *, int); 932 static struct jremref *newjremref(struct dirrem *, struct inode *, 933 struct inode *ip, off_t, nlink_t); 934 static struct jaddref *newjaddref(struct inode *, ino_t, off_t, int16_t, 935 uint16_t); 936 static inline void newinoref(struct inoref *, ino_t, ino_t, off_t, nlink_t, 937 uint16_t); 938 static inline struct jsegdep *inoref_jseg(struct inoref *); 939 static struct jmvref *newjmvref(struct inode *, ino_t, off_t, off_t); 940 static struct jfreeblk *newjfreeblk(struct freeblks *, ufs_lbn_t, 941 ufs2_daddr_t, int); 942 static void adjust_newfreework(struct freeblks *, int); 943 static struct jtrunc *newjtrunc(struct freeblks *, off_t, int); 944 static void move_newblock_dep(struct jaddref *, struct inodedep *); 945 static void cancel_jfreeblk(struct freeblks *, ufs2_daddr_t); 946 static struct jfreefrag *newjfreefrag(struct freefrag *, struct inode *, 947 ufs2_daddr_t, long, ufs_lbn_t); 948 static struct freework *newfreework(struct ufsmount *, struct freeblks *, 949 struct freework *, ufs_lbn_t, ufs2_daddr_t, int, int, int); 950 static int jwait(struct worklist *, int); 951 static struct inodedep *inodedep_lookup_ip(struct inode *); 952 static int bmsafemap_backgroundwrite(struct bmsafemap *, struct buf *); 953 static struct freefile *handle_bufwait(struct inodedep *, struct workhead *); 954 static void handle_jwork(struct workhead *); 955 static struct mkdir *setup_newdir(struct diradd *, ino_t, ino_t, struct buf *, 956 struct mkdir **); 957 static struct jblocks *jblocks_create(void); 958 static ufs2_daddr_t jblocks_alloc(struct jblocks *, int, int *); 959 static void jblocks_free(struct jblocks *, struct mount *, int); 960 static void jblocks_destroy(struct jblocks *); 961 static void jblocks_add(struct jblocks *, ufs2_daddr_t, int); 962 963 /* 964 * Exported softdep operations. 965 */ 966 static void softdep_disk_io_initiation(struct buf *); 967 static void softdep_disk_write_complete(struct buf *); 968 static void softdep_deallocate_dependencies(struct buf *); 969 static int softdep_count_dependencies(struct buf *bp, int); 970 971 /* 972 * Global lock over all of soft updates. 973 */ 974 static struct mtx lk; 975 MTX_SYSINIT(softdep_lock, &lk, "Global Softdep Lock", MTX_DEF); 976 977 #define ACQUIRE_GBLLOCK(lk) mtx_lock(lk) 978 #define FREE_GBLLOCK(lk) mtx_unlock(lk) 979 #define GBLLOCK_OWNED(lk) mtx_assert((lk), MA_OWNED) 980 981 /* 982 * Per-filesystem soft-updates locking. 983 */ 984 #define LOCK_PTR(ump) (&(ump)->um_softdep->sd_fslock) 985 #define TRY_ACQUIRE_LOCK(ump) rw_try_wlock(&(ump)->um_softdep->sd_fslock) 986 #define ACQUIRE_LOCK(ump) rw_wlock(&(ump)->um_softdep->sd_fslock) 987 #define FREE_LOCK(ump) rw_wunlock(&(ump)->um_softdep->sd_fslock) 988 #define LOCK_OWNED(ump) rw_assert(&(ump)->um_softdep->sd_fslock, \ 989 RA_WLOCKED) 990 991 #define BUF_AREC(bp) lockallowrecurse(&(bp)->b_lock) 992 #define BUF_NOREC(bp) lockdisablerecurse(&(bp)->b_lock) 993 994 /* 995 * Worklist queue management. 996 * These routines require that the lock be held. 997 */ 998 #ifndef /* NOT */ DEBUG 999 #define WORKLIST_INSERT(head, item) do { \ 1000 (item)->wk_state |= ONWORKLIST; \ 1001 LIST_INSERT_HEAD(head, item, wk_list); \ 1002 } while (0) 1003 #define WORKLIST_REMOVE(item) do { \ 1004 (item)->wk_state &= ~ONWORKLIST; \ 1005 LIST_REMOVE(item, wk_list); \ 1006 } while (0) 1007 #define WORKLIST_INSERT_UNLOCKED WORKLIST_INSERT 1008 #define WORKLIST_REMOVE_UNLOCKED WORKLIST_REMOVE 1009 1010 #else /* DEBUG */ 1011 static void worklist_insert(struct workhead *, struct worklist *, int); 1012 static void worklist_remove(struct worklist *, int); 1013 1014 #define WORKLIST_INSERT(head, item) worklist_insert(head, item, 1) 1015 #define WORKLIST_INSERT_UNLOCKED(head, item) worklist_insert(head, item, 0) 1016 #define WORKLIST_REMOVE(item) worklist_remove(item, 1) 1017 #define WORKLIST_REMOVE_UNLOCKED(item) worklist_remove(item, 0) 1018 1019 static void 1020 worklist_insert(head, item, locked) 1021 struct workhead *head; 1022 struct worklist *item; 1023 int locked; 1024 { 1025 1026 if (locked) 1027 LOCK_OWNED(VFSTOUFS(item->wk_mp)); 1028 if (item->wk_state & ONWORKLIST) 1029 panic("worklist_insert: %p %s(0x%X) already on list", 1030 item, TYPENAME(item->wk_type), item->wk_state); 1031 item->wk_state |= ONWORKLIST; 1032 LIST_INSERT_HEAD(head, item, wk_list); 1033 } 1034 1035 static void 1036 worklist_remove(item, locked) 1037 struct worklist *item; 1038 int locked; 1039 { 1040 1041 if (locked) 1042 LOCK_OWNED(VFSTOUFS(item->wk_mp)); 1043 if ((item->wk_state & ONWORKLIST) == 0) 1044 panic("worklist_remove: %p %s(0x%X) not on list", 1045 item, TYPENAME(item->wk_type), item->wk_state); 1046 item->wk_state &= ~ONWORKLIST; 1047 LIST_REMOVE(item, wk_list); 1048 } 1049 #endif /* DEBUG */ 1050 1051 /* 1052 * Merge two jsegdeps keeping only the oldest one as newer references 1053 * can't be discarded until after older references. 1054 */ 1055 static inline struct jsegdep * 1056 jsegdep_merge(struct jsegdep *one, struct jsegdep *two) 1057 { 1058 struct jsegdep *swp; 1059 1060 if (two == NULL) 1061 return (one); 1062 1063 if (one->jd_seg->js_seq > two->jd_seg->js_seq) { 1064 swp = one; 1065 one = two; 1066 two = swp; 1067 } 1068 WORKLIST_REMOVE(&two->jd_list); 1069 free_jsegdep(two); 1070 1071 return (one); 1072 } 1073 1074 /* 1075 * If two freedeps are compatible free one to reduce list size. 1076 */ 1077 static inline struct freedep * 1078 freedep_merge(struct freedep *one, struct freedep *two) 1079 { 1080 if (two == NULL) 1081 return (one); 1082 1083 if (one->fd_freework == two->fd_freework) { 1084 WORKLIST_REMOVE(&two->fd_list); 1085 free_freedep(two); 1086 } 1087 return (one); 1088 } 1089 1090 /* 1091 * Move journal work from one list to another. Duplicate freedeps and 1092 * jsegdeps are coalesced to keep the lists as small as possible. 1093 */ 1094 static void 1095 jwork_move(dst, src) 1096 struct workhead *dst; 1097 struct workhead *src; 1098 { 1099 struct freedep *freedep; 1100 struct jsegdep *jsegdep; 1101 struct worklist *wkn; 1102 struct worklist *wk; 1103 1104 KASSERT(dst != src, 1105 ("jwork_move: dst == src")); 1106 freedep = NULL; 1107 jsegdep = NULL; 1108 LIST_FOREACH_SAFE(wk, dst, wk_list, wkn) { 1109 if (wk->wk_type == D_JSEGDEP) 1110 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1111 else if (wk->wk_type == D_FREEDEP) 1112 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1113 } 1114 1115 while ((wk = LIST_FIRST(src)) != NULL) { 1116 WORKLIST_REMOVE(wk); 1117 WORKLIST_INSERT(dst, wk); 1118 if (wk->wk_type == D_JSEGDEP) { 1119 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1120 continue; 1121 } 1122 if (wk->wk_type == D_FREEDEP) 1123 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1124 } 1125 } 1126 1127 static void 1128 jwork_insert(dst, jsegdep) 1129 struct workhead *dst; 1130 struct jsegdep *jsegdep; 1131 { 1132 struct jsegdep *jsegdepn; 1133 struct worklist *wk; 1134 1135 LIST_FOREACH(wk, dst, wk_list) 1136 if (wk->wk_type == D_JSEGDEP) 1137 break; 1138 if (wk == NULL) { 1139 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1140 return; 1141 } 1142 jsegdepn = WK_JSEGDEP(wk); 1143 if (jsegdep->jd_seg->js_seq < jsegdepn->jd_seg->js_seq) { 1144 WORKLIST_REMOVE(wk); 1145 free_jsegdep(jsegdepn); 1146 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1147 } else 1148 free_jsegdep(jsegdep); 1149 } 1150 1151 /* 1152 * Routines for tracking and managing workitems. 1153 */ 1154 static void workitem_free(struct worklist *, int); 1155 static void workitem_alloc(struct worklist *, int, struct mount *); 1156 static void workitem_reassign(struct worklist *, int); 1157 1158 #define WORKITEM_FREE(item, type) \ 1159 workitem_free((struct worklist *)(item), (type)) 1160 #define WORKITEM_REASSIGN(item, type) \ 1161 workitem_reassign((struct worklist *)(item), (type)) 1162 1163 static void 1164 workitem_free(item, type) 1165 struct worklist *item; 1166 int type; 1167 { 1168 struct ufsmount *ump; 1169 1170 #ifdef DEBUG 1171 if (item->wk_state & ONWORKLIST) 1172 panic("workitem_free: %s(0x%X) still on list", 1173 TYPENAME(item->wk_type), item->wk_state); 1174 if (item->wk_type != type && type != D_NEWBLK) 1175 panic("workitem_free: type mismatch %s != %s", 1176 TYPENAME(item->wk_type), TYPENAME(type)); 1177 #endif 1178 if (item->wk_state & IOWAITING) 1179 wakeup(item); 1180 ump = VFSTOUFS(item->wk_mp); 1181 LOCK_OWNED(ump); 1182 KASSERT(ump->softdep_deps > 0, 1183 ("workitem_free: %s: softdep_deps going negative", 1184 ump->um_fs->fs_fsmnt)); 1185 if (--ump->softdep_deps == 0 && ump->softdep_req) 1186 wakeup(&ump->softdep_deps); 1187 KASSERT(dep_current[item->wk_type] > 0, 1188 ("workitem_free: %s: dep_current[%s] going negative", 1189 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1190 KASSERT(ump->softdep_curdeps[item->wk_type] > 0, 1191 ("workitem_free: %s: softdep_curdeps[%s] going negative", 1192 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1193 atomic_subtract_long(&dep_current[item->wk_type], 1); 1194 ump->softdep_curdeps[item->wk_type] -= 1; 1195 free(item, DtoM(type)); 1196 } 1197 1198 static void 1199 workitem_alloc(item, type, mp) 1200 struct worklist *item; 1201 int type; 1202 struct mount *mp; 1203 { 1204 struct ufsmount *ump; 1205 1206 item->wk_type = type; 1207 item->wk_mp = mp; 1208 item->wk_state = 0; 1209 1210 ump = VFSTOUFS(mp); 1211 ACQUIRE_GBLLOCK(&lk); 1212 dep_current[type]++; 1213 if (dep_current[type] > dep_highuse[type]) 1214 dep_highuse[type] = dep_current[type]; 1215 dep_total[type]++; 1216 FREE_GBLLOCK(&lk); 1217 ACQUIRE_LOCK(ump); 1218 ump->softdep_curdeps[type] += 1; 1219 ump->softdep_deps++; 1220 ump->softdep_accdeps++; 1221 FREE_LOCK(ump); 1222 } 1223 1224 static void 1225 workitem_reassign(item, newtype) 1226 struct worklist *item; 1227 int newtype; 1228 { 1229 struct ufsmount *ump; 1230 1231 ump = VFSTOUFS(item->wk_mp); 1232 LOCK_OWNED(ump); 1233 KASSERT(ump->softdep_curdeps[item->wk_type] > 0, 1234 ("workitem_reassign: %s: softdep_curdeps[%s] going negative", 1235 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1236 ump->softdep_curdeps[item->wk_type] -= 1; 1237 ump->softdep_curdeps[newtype] += 1; 1238 KASSERT(dep_current[item->wk_type] > 0, 1239 ("workitem_reassign: %s: dep_current[%s] going negative", 1240 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1241 ACQUIRE_GBLLOCK(&lk); 1242 dep_current[newtype]++; 1243 dep_current[item->wk_type]--; 1244 if (dep_current[newtype] > dep_highuse[newtype]) 1245 dep_highuse[newtype] = dep_current[newtype]; 1246 dep_total[newtype]++; 1247 FREE_GBLLOCK(&lk); 1248 item->wk_type = newtype; 1249 } 1250 1251 /* 1252 * Workitem queue management 1253 */ 1254 static int max_softdeps; /* maximum number of structs before slowdown */ 1255 static int tickdelay = 2; /* number of ticks to pause during slowdown */ 1256 static int proc_waiting; /* tracks whether we have a timeout posted */ 1257 static int *stat_countp; /* statistic to count in proc_waiting timeout */ 1258 static struct callout softdep_callout; 1259 static int req_clear_inodedeps; /* syncer process flush some inodedeps */ 1260 static int req_clear_remove; /* syncer process flush some freeblks */ 1261 static int softdep_flushcache = 0; /* Should we do BIO_FLUSH? */ 1262 1263 /* 1264 * runtime statistics 1265 */ 1266 static int stat_flush_threads; /* number of softdep flushing threads */ 1267 static int stat_worklist_push; /* number of worklist cleanups */ 1268 static int stat_blk_limit_push; /* number of times block limit neared */ 1269 static int stat_ino_limit_push; /* number of times inode limit neared */ 1270 static int stat_blk_limit_hit; /* number of times block slowdown imposed */ 1271 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */ 1272 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */ 1273 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */ 1274 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */ 1275 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */ 1276 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */ 1277 static int stat_jaddref; /* bufs redirtied as ino bitmap can not write */ 1278 static int stat_jnewblk; /* bufs redirtied as blk bitmap can not write */ 1279 static int stat_journal_min; /* Times hit journal min threshold */ 1280 static int stat_journal_low; /* Times hit journal low threshold */ 1281 static int stat_journal_wait; /* Times blocked in jwait(). */ 1282 static int stat_jwait_filepage; /* Times blocked in jwait() for filepage. */ 1283 static int stat_jwait_freeblks; /* Times blocked in jwait() for freeblks. */ 1284 static int stat_jwait_inode; /* Times blocked in jwait() for inodes. */ 1285 static int stat_jwait_newblk; /* Times blocked in jwait() for newblks. */ 1286 static int stat_cleanup_high_delay; /* Maximum cleanup delay (in ticks) */ 1287 static int stat_cleanup_blkrequests; /* Number of block cleanup requests */ 1288 static int stat_cleanup_inorequests; /* Number of inode cleanup requests */ 1289 static int stat_cleanup_retries; /* Number of cleanups that needed to flush */ 1290 static int stat_cleanup_failures; /* Number of cleanup requests that failed */ 1291 static int stat_emptyjblocks; /* Number of potentially empty journal blocks */ 1292 1293 SYSCTL_INT(_debug_softdep, OID_AUTO, max_softdeps, CTLFLAG_RW, 1294 &max_softdeps, 0, ""); 1295 SYSCTL_INT(_debug_softdep, OID_AUTO, tickdelay, CTLFLAG_RW, 1296 &tickdelay, 0, ""); 1297 SYSCTL_INT(_debug_softdep, OID_AUTO, flush_threads, CTLFLAG_RD, 1298 &stat_flush_threads, 0, ""); 1299 SYSCTL_INT(_debug_softdep, OID_AUTO, worklist_push, CTLFLAG_RW, 1300 &stat_worklist_push, 0,""); 1301 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_push, CTLFLAG_RW, 1302 &stat_blk_limit_push, 0,""); 1303 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_push, CTLFLAG_RW, 1304 &stat_ino_limit_push, 0,""); 1305 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_hit, CTLFLAG_RW, 1306 &stat_blk_limit_hit, 0, ""); 1307 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_hit, CTLFLAG_RW, 1308 &stat_ino_limit_hit, 0, ""); 1309 SYSCTL_INT(_debug_softdep, OID_AUTO, sync_limit_hit, CTLFLAG_RW, 1310 &stat_sync_limit_hit, 0, ""); 1311 SYSCTL_INT(_debug_softdep, OID_AUTO, indir_blk_ptrs, CTLFLAG_RW, 1312 &stat_indir_blk_ptrs, 0, ""); 1313 SYSCTL_INT(_debug_softdep, OID_AUTO, inode_bitmap, CTLFLAG_RW, 1314 &stat_inode_bitmap, 0, ""); 1315 SYSCTL_INT(_debug_softdep, OID_AUTO, direct_blk_ptrs, CTLFLAG_RW, 1316 &stat_direct_blk_ptrs, 0, ""); 1317 SYSCTL_INT(_debug_softdep, OID_AUTO, dir_entry, CTLFLAG_RW, 1318 &stat_dir_entry, 0, ""); 1319 SYSCTL_INT(_debug_softdep, OID_AUTO, jaddref_rollback, CTLFLAG_RW, 1320 &stat_jaddref, 0, ""); 1321 SYSCTL_INT(_debug_softdep, OID_AUTO, jnewblk_rollback, CTLFLAG_RW, 1322 &stat_jnewblk, 0, ""); 1323 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_low, CTLFLAG_RW, 1324 &stat_journal_low, 0, ""); 1325 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_min, CTLFLAG_RW, 1326 &stat_journal_min, 0, ""); 1327 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_wait, CTLFLAG_RW, 1328 &stat_journal_wait, 0, ""); 1329 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_filepage, CTLFLAG_RW, 1330 &stat_jwait_filepage, 0, ""); 1331 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_freeblks, CTLFLAG_RW, 1332 &stat_jwait_freeblks, 0, ""); 1333 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_inode, CTLFLAG_RW, 1334 &stat_jwait_inode, 0, ""); 1335 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_newblk, CTLFLAG_RW, 1336 &stat_jwait_newblk, 0, ""); 1337 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_blkrequests, CTLFLAG_RW, 1338 &stat_cleanup_blkrequests, 0, ""); 1339 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_inorequests, CTLFLAG_RW, 1340 &stat_cleanup_inorequests, 0, ""); 1341 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_high_delay, CTLFLAG_RW, 1342 &stat_cleanup_high_delay, 0, ""); 1343 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_retries, CTLFLAG_RW, 1344 &stat_cleanup_retries, 0, ""); 1345 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_failures, CTLFLAG_RW, 1346 &stat_cleanup_failures, 0, ""); 1347 SYSCTL_INT(_debug_softdep, OID_AUTO, flushcache, CTLFLAG_RW, 1348 &softdep_flushcache, 0, ""); 1349 SYSCTL_INT(_debug_softdep, OID_AUTO, emptyjblocks, CTLFLAG_RD, 1350 &stat_emptyjblocks, 0, ""); 1351 1352 SYSCTL_DECL(_vfs_ffs); 1353 1354 /* Whether to recompute the summary at mount time */ 1355 static int compute_summary_at_mount = 0; 1356 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW, 1357 &compute_summary_at_mount, 0, "Recompute summary at mount"); 1358 static int print_threads = 0; 1359 SYSCTL_INT(_debug_softdep, OID_AUTO, print_threads, CTLFLAG_RW, 1360 &print_threads, 0, "Notify flusher thread start/stop"); 1361 1362 /* List of all filesystems mounted with soft updates */ 1363 static TAILQ_HEAD(, mount_softdeps) softdepmounts; 1364 1365 /* 1366 * This function cleans the worklist for a filesystem. 1367 * Each filesystem running with soft dependencies gets its own 1368 * thread to run in this function. The thread is started up in 1369 * softdep_mount and shutdown in softdep_unmount. They show up 1370 * as part of the kernel "bufdaemon" process whose process 1371 * entry is available in bufdaemonproc. 1372 */ 1373 static int searchfailed; 1374 extern struct proc *bufdaemonproc; 1375 static void 1376 softdep_flush(addr) 1377 void *addr; 1378 { 1379 struct mount *mp; 1380 struct thread *td; 1381 struct ufsmount *ump; 1382 1383 td = curthread; 1384 td->td_pflags |= TDP_NORUNNINGBUF; 1385 mp = (struct mount *)addr; 1386 ump = VFSTOUFS(mp); 1387 atomic_add_int(&stat_flush_threads, 1); 1388 ACQUIRE_LOCK(ump); 1389 ump->softdep_flags &= ~FLUSH_STARTING; 1390 wakeup(&ump->softdep_flushtd); 1391 FREE_LOCK(ump); 1392 if (print_threads) { 1393 if (stat_flush_threads == 1) 1394 printf("Running %s at pid %d\n", bufdaemonproc->p_comm, 1395 bufdaemonproc->p_pid); 1396 printf("Start thread %s\n", td->td_name); 1397 } 1398 for (;;) { 1399 while (softdep_process_worklist(mp, 0) > 0 || 1400 (MOUNTEDSUJ(mp) && 1401 VFSTOUFS(mp)->softdep_jblocks->jb_suspended)) 1402 kthread_suspend_check(); 1403 ACQUIRE_LOCK(ump); 1404 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1405 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, 1406 "sdflush", hz / 2); 1407 ump->softdep_flags &= ~FLUSH_CLEANUP; 1408 /* 1409 * Check to see if we are done and need to exit. 1410 */ 1411 if ((ump->softdep_flags & FLUSH_EXIT) == 0) { 1412 FREE_LOCK(ump); 1413 continue; 1414 } 1415 ump->softdep_flags &= ~FLUSH_EXIT; 1416 FREE_LOCK(ump); 1417 wakeup(&ump->softdep_flags); 1418 if (print_threads) 1419 printf("Stop thread %s: searchfailed %d, did cleanups %d\n", td->td_name, searchfailed, ump->um_softdep->sd_cleanups); 1420 atomic_subtract_int(&stat_flush_threads, 1); 1421 kthread_exit(); 1422 panic("kthread_exit failed\n"); 1423 } 1424 } 1425 1426 static void 1427 worklist_speedup(mp) 1428 struct mount *mp; 1429 { 1430 struct ufsmount *ump; 1431 1432 ump = VFSTOUFS(mp); 1433 LOCK_OWNED(ump); 1434 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1435 ump->softdep_flags |= FLUSH_CLEANUP; 1436 wakeup(&ump->softdep_flushtd); 1437 } 1438 1439 static int 1440 softdep_speedup(ump) 1441 struct ufsmount *ump; 1442 { 1443 struct ufsmount *altump; 1444 struct mount_softdeps *sdp; 1445 1446 LOCK_OWNED(ump); 1447 worklist_speedup(ump->um_mountp); 1448 bd_speedup(); 1449 /* 1450 * If we have global shortages, then we need other 1451 * filesystems to help with the cleanup. Here we wakeup a 1452 * flusher thread for a filesystem that is over its fair 1453 * share of resources. 1454 */ 1455 if (req_clear_inodedeps || req_clear_remove) { 1456 ACQUIRE_GBLLOCK(&lk); 1457 TAILQ_FOREACH(sdp, &softdepmounts, sd_next) { 1458 if ((altump = sdp->sd_ump) == ump) 1459 continue; 1460 if (((req_clear_inodedeps && 1461 altump->softdep_curdeps[D_INODEDEP] > 1462 max_softdeps / stat_flush_threads) || 1463 (req_clear_remove && 1464 altump->softdep_curdeps[D_DIRREM] > 1465 (max_softdeps / 2) / stat_flush_threads)) && 1466 TRY_ACQUIRE_LOCK(altump)) 1467 break; 1468 } 1469 if (sdp == NULL) { 1470 searchfailed++; 1471 FREE_GBLLOCK(&lk); 1472 } else { 1473 /* 1474 * Move to the end of the list so we pick a 1475 * different one on out next try. 1476 */ 1477 TAILQ_REMOVE(&softdepmounts, sdp, sd_next); 1478 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next); 1479 FREE_GBLLOCK(&lk); 1480 if ((altump->softdep_flags & 1481 (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1482 altump->softdep_flags |= FLUSH_CLEANUP; 1483 altump->um_softdep->sd_cleanups++; 1484 wakeup(&altump->softdep_flushtd); 1485 FREE_LOCK(altump); 1486 } 1487 } 1488 return (speedup_syncer()); 1489 } 1490 1491 /* 1492 * Add an item to the end of the work queue. 1493 * This routine requires that the lock be held. 1494 * This is the only routine that adds items to the list. 1495 * The following routine is the only one that removes items 1496 * and does so in order from first to last. 1497 */ 1498 1499 #define WK_HEAD 0x0001 /* Add to HEAD. */ 1500 #define WK_NODELAY 0x0002 /* Process immediately. */ 1501 1502 static void 1503 add_to_worklist(wk, flags) 1504 struct worklist *wk; 1505 int flags; 1506 { 1507 struct ufsmount *ump; 1508 1509 ump = VFSTOUFS(wk->wk_mp); 1510 LOCK_OWNED(ump); 1511 if (wk->wk_state & ONWORKLIST) 1512 panic("add_to_worklist: %s(0x%X) already on list", 1513 TYPENAME(wk->wk_type), wk->wk_state); 1514 wk->wk_state |= ONWORKLIST; 1515 if (ump->softdep_on_worklist == 0) { 1516 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1517 ump->softdep_worklist_tail = wk; 1518 } else if (flags & WK_HEAD) { 1519 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1520 } else { 1521 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list); 1522 ump->softdep_worklist_tail = wk; 1523 } 1524 ump->softdep_on_worklist += 1; 1525 if (flags & WK_NODELAY) 1526 worklist_speedup(wk->wk_mp); 1527 } 1528 1529 /* 1530 * Remove the item to be processed. If we are removing the last 1531 * item on the list, we need to recalculate the tail pointer. 1532 */ 1533 static void 1534 remove_from_worklist(wk) 1535 struct worklist *wk; 1536 { 1537 struct ufsmount *ump; 1538 1539 ump = VFSTOUFS(wk->wk_mp); 1540 if (ump->softdep_worklist_tail == wk) 1541 ump->softdep_worklist_tail = 1542 (struct worklist *)wk->wk_list.le_prev; 1543 WORKLIST_REMOVE(wk); 1544 ump->softdep_on_worklist -= 1; 1545 } 1546 1547 static void 1548 wake_worklist(wk) 1549 struct worklist *wk; 1550 { 1551 if (wk->wk_state & IOWAITING) { 1552 wk->wk_state &= ~IOWAITING; 1553 wakeup(wk); 1554 } 1555 } 1556 1557 static void 1558 wait_worklist(wk, wmesg) 1559 struct worklist *wk; 1560 char *wmesg; 1561 { 1562 struct ufsmount *ump; 1563 1564 ump = VFSTOUFS(wk->wk_mp); 1565 wk->wk_state |= IOWAITING; 1566 msleep(wk, LOCK_PTR(ump), PVM, wmesg, 0); 1567 } 1568 1569 /* 1570 * Process that runs once per second to handle items in the background queue. 1571 * 1572 * Note that we ensure that everything is done in the order in which they 1573 * appear in the queue. The code below depends on this property to ensure 1574 * that blocks of a file are freed before the inode itself is freed. This 1575 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated 1576 * until all the old ones have been purged from the dependency lists. 1577 */ 1578 static int 1579 softdep_process_worklist(mp, full) 1580 struct mount *mp; 1581 int full; 1582 { 1583 int cnt, matchcnt; 1584 struct ufsmount *ump; 1585 long starttime; 1586 1587 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp")); 1588 if (MOUNTEDSOFTDEP(mp) == 0) 1589 return (0); 1590 matchcnt = 0; 1591 ump = VFSTOUFS(mp); 1592 ACQUIRE_LOCK(ump); 1593 starttime = time_second; 1594 softdep_process_journal(mp, NULL, full ? MNT_WAIT : 0); 1595 check_clear_deps(mp); 1596 while (ump->softdep_on_worklist > 0) { 1597 if ((cnt = process_worklist_item(mp, 10, LK_NOWAIT)) == 0) 1598 break; 1599 else 1600 matchcnt += cnt; 1601 check_clear_deps(mp); 1602 /* 1603 * We do not generally want to stop for buffer space, but if 1604 * we are really being a buffer hog, we will stop and wait. 1605 */ 1606 if (should_yield()) { 1607 FREE_LOCK(ump); 1608 kern_yield(PRI_USER); 1609 bwillwrite(); 1610 ACQUIRE_LOCK(ump); 1611 } 1612 /* 1613 * Never allow processing to run for more than one 1614 * second. This gives the syncer thread the opportunity 1615 * to pause if appropriate. 1616 */ 1617 if (!full && starttime != time_second) 1618 break; 1619 } 1620 if (full == 0) 1621 journal_unsuspend(ump); 1622 FREE_LOCK(ump); 1623 return (matchcnt); 1624 } 1625 1626 /* 1627 * Process all removes associated with a vnode if we are running out of 1628 * journal space. Any other process which attempts to flush these will 1629 * be unable as we have the vnodes locked. 1630 */ 1631 static void 1632 process_removes(vp) 1633 struct vnode *vp; 1634 { 1635 struct inodedep *inodedep; 1636 struct dirrem *dirrem; 1637 struct ufsmount *ump; 1638 struct mount *mp; 1639 ino_t inum; 1640 1641 mp = vp->v_mount; 1642 ump = VFSTOUFS(mp); 1643 LOCK_OWNED(ump); 1644 inum = VTOI(vp)->i_number; 1645 for (;;) { 1646 top: 1647 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1648 return; 1649 LIST_FOREACH(dirrem, &inodedep->id_dirremhd, dm_inonext) { 1650 /* 1651 * If another thread is trying to lock this vnode 1652 * it will fail but we must wait for it to do so 1653 * before we can proceed. 1654 */ 1655 if (dirrem->dm_state & INPROGRESS) { 1656 wait_worklist(&dirrem->dm_list, "pwrwait"); 1657 goto top; 1658 } 1659 if ((dirrem->dm_state & (COMPLETE | ONWORKLIST)) == 1660 (COMPLETE | ONWORKLIST)) 1661 break; 1662 } 1663 if (dirrem == NULL) 1664 return; 1665 remove_from_worklist(&dirrem->dm_list); 1666 FREE_LOCK(ump); 1667 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1668 panic("process_removes: suspended filesystem"); 1669 handle_workitem_remove(dirrem, 0); 1670 vn_finished_secondary_write(mp); 1671 ACQUIRE_LOCK(ump); 1672 } 1673 } 1674 1675 /* 1676 * Process all truncations associated with a vnode if we are running out 1677 * of journal space. This is called when the vnode lock is already held 1678 * and no other process can clear the truncation. This function returns 1679 * a value greater than zero if it did any work. 1680 */ 1681 static void 1682 process_truncates(vp) 1683 struct vnode *vp; 1684 { 1685 struct inodedep *inodedep; 1686 struct freeblks *freeblks; 1687 struct ufsmount *ump; 1688 struct mount *mp; 1689 ino_t inum; 1690 int cgwait; 1691 1692 mp = vp->v_mount; 1693 ump = VFSTOUFS(mp); 1694 LOCK_OWNED(ump); 1695 inum = VTOI(vp)->i_number; 1696 for (;;) { 1697 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1698 return; 1699 cgwait = 0; 1700 TAILQ_FOREACH(freeblks, &inodedep->id_freeblklst, fb_next) { 1701 /* Journal entries not yet written. */ 1702 if (!LIST_EMPTY(&freeblks->fb_jblkdephd)) { 1703 jwait(&LIST_FIRST( 1704 &freeblks->fb_jblkdephd)->jb_list, 1705 MNT_WAIT); 1706 break; 1707 } 1708 /* Another thread is executing this item. */ 1709 if (freeblks->fb_state & INPROGRESS) { 1710 wait_worklist(&freeblks->fb_list, "ptrwait"); 1711 break; 1712 } 1713 /* Freeblks is waiting on a inode write. */ 1714 if ((freeblks->fb_state & COMPLETE) == 0) { 1715 FREE_LOCK(ump); 1716 ffs_update(vp, 1); 1717 ACQUIRE_LOCK(ump); 1718 break; 1719 } 1720 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST)) == 1721 (ALLCOMPLETE | ONWORKLIST)) { 1722 remove_from_worklist(&freeblks->fb_list); 1723 freeblks->fb_state |= INPROGRESS; 1724 FREE_LOCK(ump); 1725 if (vn_start_secondary_write(NULL, &mp, 1726 V_NOWAIT)) 1727 panic("process_truncates: " 1728 "suspended filesystem"); 1729 handle_workitem_freeblocks(freeblks, 0); 1730 vn_finished_secondary_write(mp); 1731 ACQUIRE_LOCK(ump); 1732 break; 1733 } 1734 if (freeblks->fb_cgwait) 1735 cgwait++; 1736 } 1737 if (cgwait) { 1738 FREE_LOCK(ump); 1739 sync_cgs(mp, MNT_WAIT); 1740 ffs_sync_snap(mp, MNT_WAIT); 1741 ACQUIRE_LOCK(ump); 1742 continue; 1743 } 1744 if (freeblks == NULL) 1745 break; 1746 } 1747 return; 1748 } 1749 1750 /* 1751 * Process one item on the worklist. 1752 */ 1753 static int 1754 process_worklist_item(mp, target, flags) 1755 struct mount *mp; 1756 int target; 1757 int flags; 1758 { 1759 struct worklist sentinel; 1760 struct worklist *wk; 1761 struct ufsmount *ump; 1762 int matchcnt; 1763 int error; 1764 1765 KASSERT(mp != NULL, ("process_worklist_item: NULL mp")); 1766 /* 1767 * If we are being called because of a process doing a 1768 * copy-on-write, then it is not safe to write as we may 1769 * recurse into the copy-on-write routine. 1770 */ 1771 if (curthread->td_pflags & TDP_COWINPROGRESS) 1772 return (-1); 1773 PHOLD(curproc); /* Don't let the stack go away. */ 1774 ump = VFSTOUFS(mp); 1775 LOCK_OWNED(ump); 1776 matchcnt = 0; 1777 sentinel.wk_mp = NULL; 1778 sentinel.wk_type = D_SENTINEL; 1779 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, &sentinel, wk_list); 1780 for (wk = LIST_NEXT(&sentinel, wk_list); wk != NULL; 1781 wk = LIST_NEXT(&sentinel, wk_list)) { 1782 if (wk->wk_type == D_SENTINEL) { 1783 LIST_REMOVE(&sentinel, wk_list); 1784 LIST_INSERT_AFTER(wk, &sentinel, wk_list); 1785 continue; 1786 } 1787 if (wk->wk_state & INPROGRESS) 1788 panic("process_worklist_item: %p already in progress.", 1789 wk); 1790 wk->wk_state |= INPROGRESS; 1791 remove_from_worklist(wk); 1792 FREE_LOCK(ump); 1793 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1794 panic("process_worklist_item: suspended filesystem"); 1795 switch (wk->wk_type) { 1796 case D_DIRREM: 1797 /* removal of a directory entry */ 1798 error = handle_workitem_remove(WK_DIRREM(wk), flags); 1799 break; 1800 1801 case D_FREEBLKS: 1802 /* releasing blocks and/or fragments from a file */ 1803 error = handle_workitem_freeblocks(WK_FREEBLKS(wk), 1804 flags); 1805 break; 1806 1807 case D_FREEFRAG: 1808 /* releasing a fragment when replaced as a file grows */ 1809 handle_workitem_freefrag(WK_FREEFRAG(wk)); 1810 error = 0; 1811 break; 1812 1813 case D_FREEFILE: 1814 /* releasing an inode when its link count drops to 0 */ 1815 handle_workitem_freefile(WK_FREEFILE(wk)); 1816 error = 0; 1817 break; 1818 1819 default: 1820 panic("%s_process_worklist: Unknown type %s", 1821 "softdep", TYPENAME(wk->wk_type)); 1822 /* NOTREACHED */ 1823 } 1824 vn_finished_secondary_write(mp); 1825 ACQUIRE_LOCK(ump); 1826 if (error == 0) { 1827 if (++matchcnt == target) 1828 break; 1829 continue; 1830 } 1831 /* 1832 * We have to retry the worklist item later. Wake up any 1833 * waiters who may be able to complete it immediately and 1834 * add the item back to the head so we don't try to execute 1835 * it again. 1836 */ 1837 wk->wk_state &= ~INPROGRESS; 1838 wake_worklist(wk); 1839 add_to_worklist(wk, WK_HEAD); 1840 } 1841 /* Sentinal could've become the tail from remove_from_worklist. */ 1842 if (ump->softdep_worklist_tail == &sentinel) 1843 ump->softdep_worklist_tail = 1844 (struct worklist *)sentinel.wk_list.le_prev; 1845 LIST_REMOVE(&sentinel, wk_list); 1846 PRELE(curproc); 1847 return (matchcnt); 1848 } 1849 1850 /* 1851 * Move dependencies from one buffer to another. 1852 */ 1853 int 1854 softdep_move_dependencies(oldbp, newbp) 1855 struct buf *oldbp; 1856 struct buf *newbp; 1857 { 1858 struct worklist *wk, *wktail; 1859 struct ufsmount *ump; 1860 int dirty; 1861 1862 if ((wk = LIST_FIRST(&oldbp->b_dep)) == NULL) 1863 return (0); 1864 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 1865 ("softdep_move_dependencies called on non-softdep filesystem")); 1866 dirty = 0; 1867 wktail = NULL; 1868 ump = VFSTOUFS(wk->wk_mp); 1869 ACQUIRE_LOCK(ump); 1870 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) { 1871 LIST_REMOVE(wk, wk_list); 1872 if (wk->wk_type == D_BMSAFEMAP && 1873 bmsafemap_backgroundwrite(WK_BMSAFEMAP(wk), newbp)) 1874 dirty = 1; 1875 if (wktail == NULL) 1876 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list); 1877 else 1878 LIST_INSERT_AFTER(wktail, wk, wk_list); 1879 wktail = wk; 1880 } 1881 FREE_LOCK(ump); 1882 1883 return (dirty); 1884 } 1885 1886 /* 1887 * Purge the work list of all items associated with a particular mount point. 1888 */ 1889 int 1890 softdep_flushworklist(oldmnt, countp, td) 1891 struct mount *oldmnt; 1892 int *countp; 1893 struct thread *td; 1894 { 1895 struct vnode *devvp; 1896 struct ufsmount *ump; 1897 int count, error; 1898 1899 /* 1900 * Alternately flush the block device associated with the mount 1901 * point and process any dependencies that the flushing 1902 * creates. We continue until no more worklist dependencies 1903 * are found. 1904 */ 1905 *countp = 0; 1906 error = 0; 1907 ump = VFSTOUFS(oldmnt); 1908 devvp = ump->um_devvp; 1909 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) { 1910 *countp += count; 1911 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1912 error = VOP_FSYNC(devvp, MNT_WAIT, td); 1913 VOP_UNLOCK(devvp, 0); 1914 if (error != 0) 1915 break; 1916 } 1917 return (error); 1918 } 1919 1920 #define SU_WAITIDLE_RETRIES 20 1921 static int 1922 softdep_waitidle(struct mount *mp, int flags __unused) 1923 { 1924 struct ufsmount *ump; 1925 struct vnode *devvp; 1926 struct thread *td; 1927 int error, i; 1928 1929 ump = VFSTOUFS(mp); 1930 devvp = ump->um_devvp; 1931 td = curthread; 1932 error = 0; 1933 ACQUIRE_LOCK(ump); 1934 for (i = 0; i < SU_WAITIDLE_RETRIES && ump->softdep_deps != 0; i++) { 1935 ump->softdep_req = 1; 1936 KASSERT((flags & FORCECLOSE) == 0 || 1937 ump->softdep_on_worklist == 0, 1938 ("softdep_waitidle: work added after flush")); 1939 msleep(&ump->softdep_deps, LOCK_PTR(ump), PVM | PDROP, 1940 "softdeps", 10 * hz); 1941 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1942 error = VOP_FSYNC(devvp, MNT_WAIT, td); 1943 VOP_UNLOCK(devvp, 0); 1944 ACQUIRE_LOCK(ump); 1945 if (error != 0) 1946 break; 1947 } 1948 ump->softdep_req = 0; 1949 if (i == SU_WAITIDLE_RETRIES && error == 0 && ump->softdep_deps != 0) { 1950 error = EBUSY; 1951 printf("softdep_waitidle: Failed to flush worklist for %p\n", 1952 mp); 1953 } 1954 FREE_LOCK(ump); 1955 return (error); 1956 } 1957 1958 /* 1959 * Flush all vnodes and worklist items associated with a specified mount point. 1960 */ 1961 int 1962 softdep_flushfiles(oldmnt, flags, td) 1963 struct mount *oldmnt; 1964 int flags; 1965 struct thread *td; 1966 { 1967 #ifdef QUOTA 1968 struct ufsmount *ump; 1969 int i; 1970 #endif 1971 int error, early, depcount, loopcnt, retry_flush_count, retry; 1972 int morework; 1973 1974 KASSERT(MOUNTEDSOFTDEP(oldmnt) != 0, 1975 ("softdep_flushfiles called on non-softdep filesystem")); 1976 loopcnt = 10; 1977 retry_flush_count = 3; 1978 retry_flush: 1979 error = 0; 1980 1981 /* 1982 * Alternately flush the vnodes associated with the mount 1983 * point and process any dependencies that the flushing 1984 * creates. In theory, this loop can happen at most twice, 1985 * but we give it a few extra just to be sure. 1986 */ 1987 for (; loopcnt > 0; loopcnt--) { 1988 /* 1989 * Do another flush in case any vnodes were brought in 1990 * as part of the cleanup operations. 1991 */ 1992 early = retry_flush_count == 1 || (oldmnt->mnt_kern_flag & 1993 MNTK_UNMOUNT) == 0 ? 0 : EARLYFLUSH; 1994 if ((error = ffs_flushfiles(oldmnt, flags | early, td)) != 0) 1995 break; 1996 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 || 1997 depcount == 0) 1998 break; 1999 } 2000 /* 2001 * If we are unmounting then it is an error to fail. If we 2002 * are simply trying to downgrade to read-only, then filesystem 2003 * activity can keep us busy forever, so we just fail with EBUSY. 2004 */ 2005 if (loopcnt == 0) { 2006 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) 2007 panic("softdep_flushfiles: looping"); 2008 error = EBUSY; 2009 } 2010 if (!error) 2011 error = softdep_waitidle(oldmnt, flags); 2012 if (!error) { 2013 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) { 2014 retry = 0; 2015 MNT_ILOCK(oldmnt); 2016 KASSERT((oldmnt->mnt_kern_flag & MNTK_NOINSMNTQ) != 0, 2017 ("softdep_flushfiles: !MNTK_NOINSMNTQ")); 2018 morework = oldmnt->mnt_nvnodelistsize > 0; 2019 #ifdef QUOTA 2020 ump = VFSTOUFS(oldmnt); 2021 UFS_LOCK(ump); 2022 for (i = 0; i < MAXQUOTAS; i++) { 2023 if (ump->um_quotas[i] != NULLVP) 2024 morework = 1; 2025 } 2026 UFS_UNLOCK(ump); 2027 #endif 2028 if (morework) { 2029 if (--retry_flush_count > 0) { 2030 retry = 1; 2031 loopcnt = 3; 2032 } else 2033 error = EBUSY; 2034 } 2035 MNT_IUNLOCK(oldmnt); 2036 if (retry) 2037 goto retry_flush; 2038 } 2039 } 2040 return (error); 2041 } 2042 2043 /* 2044 * Structure hashing. 2045 * 2046 * There are four types of structures that can be looked up: 2047 * 1) pagedep structures identified by mount point, inode number, 2048 * and logical block. 2049 * 2) inodedep structures identified by mount point and inode number. 2050 * 3) newblk structures identified by mount point and 2051 * physical block number. 2052 * 4) bmsafemap structures identified by mount point and 2053 * cylinder group number. 2054 * 2055 * The "pagedep" and "inodedep" dependency structures are hashed 2056 * separately from the file blocks and inodes to which they correspond. 2057 * This separation helps when the in-memory copy of an inode or 2058 * file block must be replaced. It also obviates the need to access 2059 * an inode or file page when simply updating (or de-allocating) 2060 * dependency structures. Lookup of newblk structures is needed to 2061 * find newly allocated blocks when trying to associate them with 2062 * their allocdirect or allocindir structure. 2063 * 2064 * The lookup routines optionally create and hash a new instance when 2065 * an existing entry is not found. The bmsafemap lookup routine always 2066 * allocates a new structure if an existing one is not found. 2067 */ 2068 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */ 2069 2070 /* 2071 * Structures and routines associated with pagedep caching. 2072 */ 2073 #define PAGEDEP_HASH(ump, inum, lbn) \ 2074 (&(ump)->pagedep_hashtbl[((inum) + (lbn)) & (ump)->pagedep_hash_size]) 2075 2076 static int 2077 pagedep_find(pagedephd, ino, lbn, pagedeppp) 2078 struct pagedep_hashhead *pagedephd; 2079 ino_t ino; 2080 ufs_lbn_t lbn; 2081 struct pagedep **pagedeppp; 2082 { 2083 struct pagedep *pagedep; 2084 2085 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 2086 if (ino == pagedep->pd_ino && lbn == pagedep->pd_lbn) { 2087 *pagedeppp = pagedep; 2088 return (1); 2089 } 2090 } 2091 *pagedeppp = NULL; 2092 return (0); 2093 } 2094 /* 2095 * Look up a pagedep. Return 1 if found, 0 otherwise. 2096 * If not found, allocate if DEPALLOC flag is passed. 2097 * Found or allocated entry is returned in pagedeppp. 2098 * This routine must be called with splbio interrupts blocked. 2099 */ 2100 static int 2101 pagedep_lookup(mp, bp, ino, lbn, flags, pagedeppp) 2102 struct mount *mp; 2103 struct buf *bp; 2104 ino_t ino; 2105 ufs_lbn_t lbn; 2106 int flags; 2107 struct pagedep **pagedeppp; 2108 { 2109 struct pagedep *pagedep; 2110 struct pagedep_hashhead *pagedephd; 2111 struct worklist *wk; 2112 struct ufsmount *ump; 2113 int ret; 2114 int i; 2115 2116 ump = VFSTOUFS(mp); 2117 LOCK_OWNED(ump); 2118 if (bp) { 2119 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 2120 if (wk->wk_type == D_PAGEDEP) { 2121 *pagedeppp = WK_PAGEDEP(wk); 2122 return (1); 2123 } 2124 } 2125 } 2126 pagedephd = PAGEDEP_HASH(ump, ino, lbn); 2127 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp); 2128 if (ret) { 2129 if (((*pagedeppp)->pd_state & ONWORKLIST) == 0 && bp) 2130 WORKLIST_INSERT(&bp->b_dep, &(*pagedeppp)->pd_list); 2131 return (1); 2132 } 2133 if ((flags & DEPALLOC) == 0) 2134 return (0); 2135 FREE_LOCK(ump); 2136 pagedep = malloc(sizeof(struct pagedep), 2137 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO); 2138 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp); 2139 ACQUIRE_LOCK(ump); 2140 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp); 2141 if (*pagedeppp) { 2142 /* 2143 * This should never happen since we only create pagedeps 2144 * with the vnode lock held. Could be an assert. 2145 */ 2146 WORKITEM_FREE(pagedep, D_PAGEDEP); 2147 return (ret); 2148 } 2149 pagedep->pd_ino = ino; 2150 pagedep->pd_lbn = lbn; 2151 LIST_INIT(&pagedep->pd_dirremhd); 2152 LIST_INIT(&pagedep->pd_pendinghd); 2153 for (i = 0; i < DAHASHSZ; i++) 2154 LIST_INIT(&pagedep->pd_diraddhd[i]); 2155 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash); 2156 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list); 2157 *pagedeppp = pagedep; 2158 return (0); 2159 } 2160 2161 /* 2162 * Structures and routines associated with inodedep caching. 2163 */ 2164 #define INODEDEP_HASH(ump, inum) \ 2165 (&(ump)->inodedep_hashtbl[(inum) & (ump)->inodedep_hash_size]) 2166 2167 static int 2168 inodedep_find(inodedephd, inum, inodedeppp) 2169 struct inodedep_hashhead *inodedephd; 2170 ino_t inum; 2171 struct inodedep **inodedeppp; 2172 { 2173 struct inodedep *inodedep; 2174 2175 LIST_FOREACH(inodedep, inodedephd, id_hash) 2176 if (inum == inodedep->id_ino) 2177 break; 2178 if (inodedep) { 2179 *inodedeppp = inodedep; 2180 return (1); 2181 } 2182 *inodedeppp = NULL; 2183 2184 return (0); 2185 } 2186 /* 2187 * Look up an inodedep. Return 1 if found, 0 if not found. 2188 * If not found, allocate if DEPALLOC flag is passed. 2189 * Found or allocated entry is returned in inodedeppp. 2190 * This routine must be called with splbio interrupts blocked. 2191 */ 2192 static int 2193 inodedep_lookup(mp, inum, flags, inodedeppp) 2194 struct mount *mp; 2195 ino_t inum; 2196 int flags; 2197 struct inodedep **inodedeppp; 2198 { 2199 struct inodedep *inodedep; 2200 struct inodedep_hashhead *inodedephd; 2201 struct ufsmount *ump; 2202 struct fs *fs; 2203 2204 ump = VFSTOUFS(mp); 2205 LOCK_OWNED(ump); 2206 fs = ump->um_fs; 2207 inodedephd = INODEDEP_HASH(ump, inum); 2208 2209 if (inodedep_find(inodedephd, inum, inodedeppp)) 2210 return (1); 2211 if ((flags & DEPALLOC) == 0) 2212 return (0); 2213 /* 2214 * If the system is over its limit and our filesystem is 2215 * responsible for more than our share of that usage and 2216 * we are not in a rush, request some inodedep cleanup. 2217 */ 2218 if (softdep_excess_items(ump, D_INODEDEP)) 2219 schedule_cleanup(mp); 2220 else 2221 FREE_LOCK(ump); 2222 inodedep = malloc(sizeof(struct inodedep), 2223 M_INODEDEP, M_SOFTDEP_FLAGS); 2224 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp); 2225 ACQUIRE_LOCK(ump); 2226 if (inodedep_find(inodedephd, inum, inodedeppp)) { 2227 WORKITEM_FREE(inodedep, D_INODEDEP); 2228 return (1); 2229 } 2230 inodedep->id_fs = fs; 2231 inodedep->id_ino = inum; 2232 inodedep->id_state = ALLCOMPLETE; 2233 inodedep->id_nlinkdelta = 0; 2234 inodedep->id_savedino1 = NULL; 2235 inodedep->id_savedsize = -1; 2236 inodedep->id_savedextsize = -1; 2237 inodedep->id_savednlink = -1; 2238 inodedep->id_bmsafemap = NULL; 2239 inodedep->id_mkdiradd = NULL; 2240 LIST_INIT(&inodedep->id_dirremhd); 2241 LIST_INIT(&inodedep->id_pendinghd); 2242 LIST_INIT(&inodedep->id_inowait); 2243 LIST_INIT(&inodedep->id_bufwait); 2244 TAILQ_INIT(&inodedep->id_inoreflst); 2245 TAILQ_INIT(&inodedep->id_inoupdt); 2246 TAILQ_INIT(&inodedep->id_newinoupdt); 2247 TAILQ_INIT(&inodedep->id_extupdt); 2248 TAILQ_INIT(&inodedep->id_newextupdt); 2249 TAILQ_INIT(&inodedep->id_freeblklst); 2250 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash); 2251 *inodedeppp = inodedep; 2252 return (0); 2253 } 2254 2255 /* 2256 * Structures and routines associated with newblk caching. 2257 */ 2258 #define NEWBLK_HASH(ump, inum) \ 2259 (&(ump)->newblk_hashtbl[(inum) & (ump)->newblk_hash_size]) 2260 2261 static int 2262 newblk_find(newblkhd, newblkno, flags, newblkpp) 2263 struct newblk_hashhead *newblkhd; 2264 ufs2_daddr_t newblkno; 2265 int flags; 2266 struct newblk **newblkpp; 2267 { 2268 struct newblk *newblk; 2269 2270 LIST_FOREACH(newblk, newblkhd, nb_hash) { 2271 if (newblkno != newblk->nb_newblkno) 2272 continue; 2273 /* 2274 * If we're creating a new dependency don't match those that 2275 * have already been converted to allocdirects. This is for 2276 * a frag extend. 2277 */ 2278 if ((flags & DEPALLOC) && newblk->nb_list.wk_type != D_NEWBLK) 2279 continue; 2280 break; 2281 } 2282 if (newblk) { 2283 *newblkpp = newblk; 2284 return (1); 2285 } 2286 *newblkpp = NULL; 2287 return (0); 2288 } 2289 2290 /* 2291 * Look up a newblk. Return 1 if found, 0 if not found. 2292 * If not found, allocate if DEPALLOC flag is passed. 2293 * Found or allocated entry is returned in newblkpp. 2294 */ 2295 static int 2296 newblk_lookup(mp, newblkno, flags, newblkpp) 2297 struct mount *mp; 2298 ufs2_daddr_t newblkno; 2299 int flags; 2300 struct newblk **newblkpp; 2301 { 2302 struct newblk *newblk; 2303 struct newblk_hashhead *newblkhd; 2304 struct ufsmount *ump; 2305 2306 ump = VFSTOUFS(mp); 2307 LOCK_OWNED(ump); 2308 newblkhd = NEWBLK_HASH(ump, newblkno); 2309 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) 2310 return (1); 2311 if ((flags & DEPALLOC) == 0) 2312 return (0); 2313 if (softdep_excess_items(ump, D_NEWBLK) || 2314 softdep_excess_items(ump, D_ALLOCDIRECT) || 2315 softdep_excess_items(ump, D_ALLOCINDIR)) 2316 schedule_cleanup(mp); 2317 else 2318 FREE_LOCK(ump); 2319 newblk = malloc(sizeof(union allblk), M_NEWBLK, 2320 M_SOFTDEP_FLAGS | M_ZERO); 2321 workitem_alloc(&newblk->nb_list, D_NEWBLK, mp); 2322 ACQUIRE_LOCK(ump); 2323 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) { 2324 WORKITEM_FREE(newblk, D_NEWBLK); 2325 return (1); 2326 } 2327 newblk->nb_freefrag = NULL; 2328 LIST_INIT(&newblk->nb_indirdeps); 2329 LIST_INIT(&newblk->nb_newdirblk); 2330 LIST_INIT(&newblk->nb_jwork); 2331 newblk->nb_state = ATTACHED; 2332 newblk->nb_newblkno = newblkno; 2333 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash); 2334 *newblkpp = newblk; 2335 return (0); 2336 } 2337 2338 /* 2339 * Structures and routines associated with freed indirect block caching. 2340 */ 2341 #define INDIR_HASH(ump, blkno) \ 2342 (&(ump)->indir_hashtbl[(blkno) & (ump)->indir_hash_size]) 2343 2344 /* 2345 * Lookup an indirect block in the indir hash table. The freework is 2346 * removed and potentially freed. The caller must do a blocking journal 2347 * write before writing to the blkno. 2348 */ 2349 static int 2350 indirblk_lookup(mp, blkno) 2351 struct mount *mp; 2352 ufs2_daddr_t blkno; 2353 { 2354 struct freework *freework; 2355 struct indir_hashhead *wkhd; 2356 struct ufsmount *ump; 2357 2358 ump = VFSTOUFS(mp); 2359 wkhd = INDIR_HASH(ump, blkno); 2360 TAILQ_FOREACH(freework, wkhd, fw_next) { 2361 if (freework->fw_blkno != blkno) 2362 continue; 2363 indirblk_remove(freework); 2364 return (1); 2365 } 2366 return (0); 2367 } 2368 2369 /* 2370 * Insert an indirect block represented by freework into the indirblk 2371 * hash table so that it may prevent the block from being re-used prior 2372 * to the journal being written. 2373 */ 2374 static void 2375 indirblk_insert(freework) 2376 struct freework *freework; 2377 { 2378 struct jblocks *jblocks; 2379 struct jseg *jseg; 2380 struct ufsmount *ump; 2381 2382 ump = VFSTOUFS(freework->fw_list.wk_mp); 2383 jblocks = ump->softdep_jblocks; 2384 jseg = TAILQ_LAST(&jblocks->jb_segs, jseglst); 2385 if (jseg == NULL) 2386 return; 2387 2388 LIST_INSERT_HEAD(&jseg->js_indirs, freework, fw_segs); 2389 TAILQ_INSERT_HEAD(INDIR_HASH(ump, freework->fw_blkno), freework, 2390 fw_next); 2391 freework->fw_state &= ~DEPCOMPLETE; 2392 } 2393 2394 static void 2395 indirblk_remove(freework) 2396 struct freework *freework; 2397 { 2398 struct ufsmount *ump; 2399 2400 ump = VFSTOUFS(freework->fw_list.wk_mp); 2401 LIST_REMOVE(freework, fw_segs); 2402 TAILQ_REMOVE(INDIR_HASH(ump, freework->fw_blkno), freework, fw_next); 2403 freework->fw_state |= DEPCOMPLETE; 2404 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 2405 WORKITEM_FREE(freework, D_FREEWORK); 2406 } 2407 2408 /* 2409 * Executed during filesystem system initialization before 2410 * mounting any filesystems. 2411 */ 2412 void 2413 softdep_initialize() 2414 { 2415 2416 TAILQ_INIT(&softdepmounts); 2417 #ifdef __LP64__ 2418 max_softdeps = desiredvnodes * 4; 2419 #else 2420 max_softdeps = desiredvnodes * 2; 2421 #endif 2422 2423 /* initialise bioops hack */ 2424 bioops.io_start = softdep_disk_io_initiation; 2425 bioops.io_complete = softdep_disk_write_complete; 2426 bioops.io_deallocate = softdep_deallocate_dependencies; 2427 bioops.io_countdeps = softdep_count_dependencies; 2428 softdep_ast_cleanup = softdep_ast_cleanup_proc; 2429 2430 /* Initialize the callout with an mtx. */ 2431 callout_init_mtx(&softdep_callout, &lk, 0); 2432 } 2433 2434 /* 2435 * Executed after all filesystems have been unmounted during 2436 * filesystem module unload. 2437 */ 2438 void 2439 softdep_uninitialize() 2440 { 2441 2442 /* clear bioops hack */ 2443 bioops.io_start = NULL; 2444 bioops.io_complete = NULL; 2445 bioops.io_deallocate = NULL; 2446 bioops.io_countdeps = NULL; 2447 softdep_ast_cleanup = NULL; 2448 2449 callout_drain(&softdep_callout); 2450 } 2451 2452 /* 2453 * Called at mount time to notify the dependency code that a 2454 * filesystem wishes to use it. 2455 */ 2456 int 2457 softdep_mount(devvp, mp, fs, cred) 2458 struct vnode *devvp; 2459 struct mount *mp; 2460 struct fs *fs; 2461 struct ucred *cred; 2462 { 2463 struct csum_total cstotal; 2464 struct mount_softdeps *sdp; 2465 struct ufsmount *ump; 2466 struct cg *cgp; 2467 struct buf *bp; 2468 int i, error, cyl; 2469 2470 sdp = malloc(sizeof(struct mount_softdeps), M_MOUNTDATA, 2471 M_WAITOK | M_ZERO); 2472 MNT_ILOCK(mp); 2473 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP; 2474 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) { 2475 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) | 2476 MNTK_SOFTDEP | MNTK_NOASYNC; 2477 } 2478 ump = VFSTOUFS(mp); 2479 ump->um_softdep = sdp; 2480 MNT_IUNLOCK(mp); 2481 rw_init(LOCK_PTR(ump), "Per-Filesystem Softdep Lock"); 2482 sdp->sd_ump = ump; 2483 LIST_INIT(&ump->softdep_workitem_pending); 2484 LIST_INIT(&ump->softdep_journal_pending); 2485 TAILQ_INIT(&ump->softdep_unlinked); 2486 LIST_INIT(&ump->softdep_dirtycg); 2487 ump->softdep_worklist_tail = NULL; 2488 ump->softdep_on_worklist = 0; 2489 ump->softdep_deps = 0; 2490 LIST_INIT(&ump->softdep_mkdirlisthd); 2491 ump->pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP, 2492 &ump->pagedep_hash_size); 2493 ump->pagedep_nextclean = 0; 2494 ump->inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP, 2495 &ump->inodedep_hash_size); 2496 ump->inodedep_nextclean = 0; 2497 ump->newblk_hashtbl = hashinit(max_softdeps / 2, M_NEWBLK, 2498 &ump->newblk_hash_size); 2499 ump->bmsafemap_hashtbl = hashinit(1024, M_BMSAFEMAP, 2500 &ump->bmsafemap_hash_size); 2501 i = 1 << (ffs(desiredvnodes / 10) - 1); 2502 ump->indir_hashtbl = malloc(i * sizeof(struct indir_hashhead), 2503 M_FREEWORK, M_WAITOK); 2504 ump->indir_hash_size = i - 1; 2505 for (i = 0; i <= ump->indir_hash_size; i++) 2506 TAILQ_INIT(&ump->indir_hashtbl[i]); 2507 ACQUIRE_GBLLOCK(&lk); 2508 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next); 2509 FREE_GBLLOCK(&lk); 2510 if ((fs->fs_flags & FS_SUJ) && 2511 (error = journal_mount(mp, fs, cred)) != 0) { 2512 printf("Failed to start journal: %d\n", error); 2513 softdep_unmount(mp); 2514 return (error); 2515 } 2516 /* 2517 * Start our flushing thread in the bufdaemon process. 2518 */ 2519 ACQUIRE_LOCK(ump); 2520 ump->softdep_flags |= FLUSH_STARTING; 2521 FREE_LOCK(ump); 2522 kproc_kthread_add(&softdep_flush, mp, &bufdaemonproc, 2523 &ump->softdep_flushtd, 0, 0, "softdepflush", "%s worker", 2524 mp->mnt_stat.f_mntonname); 2525 ACQUIRE_LOCK(ump); 2526 while ((ump->softdep_flags & FLUSH_STARTING) != 0) { 2527 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, "sdstart", 2528 hz / 2); 2529 } 2530 FREE_LOCK(ump); 2531 /* 2532 * When doing soft updates, the counters in the 2533 * superblock may have gotten out of sync. Recomputation 2534 * can take a long time and can be deferred for background 2535 * fsck. However, the old behavior of scanning the cylinder 2536 * groups and recalculating them at mount time is available 2537 * by setting vfs.ffs.compute_summary_at_mount to one. 2538 */ 2539 if (compute_summary_at_mount == 0 || fs->fs_clean != 0) 2540 return (0); 2541 bzero(&cstotal, sizeof cstotal); 2542 for (cyl = 0; cyl < fs->fs_ncg; cyl++) { 2543 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)), 2544 fs->fs_cgsize, cred, &bp)) != 0) { 2545 brelse(bp); 2546 softdep_unmount(mp); 2547 return (error); 2548 } 2549 cgp = (struct cg *)bp->b_data; 2550 cstotal.cs_nffree += cgp->cg_cs.cs_nffree; 2551 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree; 2552 cstotal.cs_nifree += cgp->cg_cs.cs_nifree; 2553 cstotal.cs_ndir += cgp->cg_cs.cs_ndir; 2554 fs->fs_cs(fs, cyl) = cgp->cg_cs; 2555 brelse(bp); 2556 } 2557 #ifdef DEBUG 2558 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal)) 2559 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt); 2560 #endif 2561 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal); 2562 return (0); 2563 } 2564 2565 void 2566 softdep_unmount(mp) 2567 struct mount *mp; 2568 { 2569 struct ufsmount *ump; 2570 #ifdef INVARIANTS 2571 int i; 2572 #endif 2573 2574 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 2575 ("softdep_unmount called on non-softdep filesystem")); 2576 ump = VFSTOUFS(mp); 2577 MNT_ILOCK(mp); 2578 mp->mnt_flag &= ~MNT_SOFTDEP; 2579 if (MOUNTEDSUJ(mp) == 0) { 2580 MNT_IUNLOCK(mp); 2581 } else { 2582 mp->mnt_flag &= ~MNT_SUJ; 2583 MNT_IUNLOCK(mp); 2584 journal_unmount(ump); 2585 } 2586 /* 2587 * Shut down our flushing thread. Check for NULL is if 2588 * softdep_mount errors out before the thread has been created. 2589 */ 2590 if (ump->softdep_flushtd != NULL) { 2591 ACQUIRE_LOCK(ump); 2592 ump->softdep_flags |= FLUSH_EXIT; 2593 wakeup(&ump->softdep_flushtd); 2594 msleep(&ump->softdep_flags, LOCK_PTR(ump), PVM | PDROP, 2595 "sdwait", 0); 2596 KASSERT((ump->softdep_flags & FLUSH_EXIT) == 0, 2597 ("Thread shutdown failed")); 2598 } 2599 /* 2600 * Free up our resources. 2601 */ 2602 ACQUIRE_GBLLOCK(&lk); 2603 TAILQ_REMOVE(&softdepmounts, ump->um_softdep, sd_next); 2604 FREE_GBLLOCK(&lk); 2605 rw_destroy(LOCK_PTR(ump)); 2606 hashdestroy(ump->pagedep_hashtbl, M_PAGEDEP, ump->pagedep_hash_size); 2607 hashdestroy(ump->inodedep_hashtbl, M_INODEDEP, ump->inodedep_hash_size); 2608 hashdestroy(ump->newblk_hashtbl, M_NEWBLK, ump->newblk_hash_size); 2609 hashdestroy(ump->bmsafemap_hashtbl, M_BMSAFEMAP, 2610 ump->bmsafemap_hash_size); 2611 free(ump->indir_hashtbl, M_FREEWORK); 2612 #ifdef INVARIANTS 2613 for (i = 0; i <= D_LAST; i++) 2614 KASSERT(ump->softdep_curdeps[i] == 0, 2615 ("Unmount %s: Dep type %s != 0 (%ld)", ump->um_fs->fs_fsmnt, 2616 TYPENAME(i), ump->softdep_curdeps[i])); 2617 #endif 2618 free(ump->um_softdep, M_MOUNTDATA); 2619 } 2620 2621 static struct jblocks * 2622 jblocks_create(void) 2623 { 2624 struct jblocks *jblocks; 2625 2626 jblocks = malloc(sizeof(*jblocks), M_JBLOCKS, M_WAITOK | M_ZERO); 2627 TAILQ_INIT(&jblocks->jb_segs); 2628 jblocks->jb_avail = 10; 2629 jblocks->jb_extent = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2630 M_JBLOCKS, M_WAITOK | M_ZERO); 2631 2632 return (jblocks); 2633 } 2634 2635 static ufs2_daddr_t 2636 jblocks_alloc(jblocks, bytes, actual) 2637 struct jblocks *jblocks; 2638 int bytes; 2639 int *actual; 2640 { 2641 ufs2_daddr_t daddr; 2642 struct jextent *jext; 2643 int freecnt; 2644 int blocks; 2645 2646 blocks = bytes / DEV_BSIZE; 2647 jext = &jblocks->jb_extent[jblocks->jb_head]; 2648 freecnt = jext->je_blocks - jblocks->jb_off; 2649 if (freecnt == 0) { 2650 jblocks->jb_off = 0; 2651 if (++jblocks->jb_head > jblocks->jb_used) 2652 jblocks->jb_head = 0; 2653 jext = &jblocks->jb_extent[jblocks->jb_head]; 2654 freecnt = jext->je_blocks; 2655 } 2656 if (freecnt > blocks) 2657 freecnt = blocks; 2658 *actual = freecnt * DEV_BSIZE; 2659 daddr = jext->je_daddr + jblocks->jb_off; 2660 jblocks->jb_off += freecnt; 2661 jblocks->jb_free -= freecnt; 2662 2663 return (daddr); 2664 } 2665 2666 static void 2667 jblocks_free(jblocks, mp, bytes) 2668 struct jblocks *jblocks; 2669 struct mount *mp; 2670 int bytes; 2671 { 2672 2673 LOCK_OWNED(VFSTOUFS(mp)); 2674 jblocks->jb_free += bytes / DEV_BSIZE; 2675 if (jblocks->jb_suspended) 2676 worklist_speedup(mp); 2677 wakeup(jblocks); 2678 } 2679 2680 static void 2681 jblocks_destroy(jblocks) 2682 struct jblocks *jblocks; 2683 { 2684 2685 if (jblocks->jb_extent) 2686 free(jblocks->jb_extent, M_JBLOCKS); 2687 free(jblocks, M_JBLOCKS); 2688 } 2689 2690 static void 2691 jblocks_add(jblocks, daddr, blocks) 2692 struct jblocks *jblocks; 2693 ufs2_daddr_t daddr; 2694 int blocks; 2695 { 2696 struct jextent *jext; 2697 2698 jblocks->jb_blocks += blocks; 2699 jblocks->jb_free += blocks; 2700 jext = &jblocks->jb_extent[jblocks->jb_used]; 2701 /* Adding the first block. */ 2702 if (jext->je_daddr == 0) { 2703 jext->je_daddr = daddr; 2704 jext->je_blocks = blocks; 2705 return; 2706 } 2707 /* Extending the last extent. */ 2708 if (jext->je_daddr + jext->je_blocks == daddr) { 2709 jext->je_blocks += blocks; 2710 return; 2711 } 2712 /* Adding a new extent. */ 2713 if (++jblocks->jb_used == jblocks->jb_avail) { 2714 jblocks->jb_avail *= 2; 2715 jext = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2716 M_JBLOCKS, M_WAITOK | M_ZERO); 2717 memcpy(jext, jblocks->jb_extent, 2718 sizeof(struct jextent) * jblocks->jb_used); 2719 free(jblocks->jb_extent, M_JBLOCKS); 2720 jblocks->jb_extent = jext; 2721 } 2722 jext = &jblocks->jb_extent[jblocks->jb_used]; 2723 jext->je_daddr = daddr; 2724 jext->je_blocks = blocks; 2725 return; 2726 } 2727 2728 int 2729 softdep_journal_lookup(mp, vpp) 2730 struct mount *mp; 2731 struct vnode **vpp; 2732 { 2733 struct componentname cnp; 2734 struct vnode *dvp; 2735 ino_t sujournal; 2736 int error; 2737 2738 error = VFS_VGET(mp, UFS_ROOTINO, LK_EXCLUSIVE, &dvp); 2739 if (error) 2740 return (error); 2741 bzero(&cnp, sizeof(cnp)); 2742 cnp.cn_nameiop = LOOKUP; 2743 cnp.cn_flags = ISLASTCN; 2744 cnp.cn_thread = curthread; 2745 cnp.cn_cred = curthread->td_ucred; 2746 cnp.cn_pnbuf = SUJ_FILE; 2747 cnp.cn_nameptr = SUJ_FILE; 2748 cnp.cn_namelen = strlen(SUJ_FILE); 2749 error = ufs_lookup_ino(dvp, NULL, &cnp, &sujournal); 2750 vput(dvp); 2751 if (error != 0) 2752 return (error); 2753 error = VFS_VGET(mp, sujournal, LK_EXCLUSIVE, vpp); 2754 return (error); 2755 } 2756 2757 /* 2758 * Open and verify the journal file. 2759 */ 2760 static int 2761 journal_mount(mp, fs, cred) 2762 struct mount *mp; 2763 struct fs *fs; 2764 struct ucred *cred; 2765 { 2766 struct jblocks *jblocks; 2767 struct ufsmount *ump; 2768 struct vnode *vp; 2769 struct inode *ip; 2770 ufs2_daddr_t blkno; 2771 int bcount; 2772 int error; 2773 int i; 2774 2775 ump = VFSTOUFS(mp); 2776 ump->softdep_journal_tail = NULL; 2777 ump->softdep_on_journal = 0; 2778 ump->softdep_accdeps = 0; 2779 ump->softdep_req = 0; 2780 ump->softdep_jblocks = NULL; 2781 error = softdep_journal_lookup(mp, &vp); 2782 if (error != 0) { 2783 printf("Failed to find journal. Use tunefs to create one\n"); 2784 return (error); 2785 } 2786 ip = VTOI(vp); 2787 if (ip->i_size < SUJ_MIN) { 2788 error = ENOSPC; 2789 goto out; 2790 } 2791 bcount = lblkno(fs, ip->i_size); /* Only use whole blocks. */ 2792 jblocks = jblocks_create(); 2793 for (i = 0; i < bcount; i++) { 2794 error = ufs_bmaparray(vp, i, &blkno, NULL, NULL, NULL); 2795 if (error) 2796 break; 2797 jblocks_add(jblocks, blkno, fsbtodb(fs, fs->fs_frag)); 2798 } 2799 if (error) { 2800 jblocks_destroy(jblocks); 2801 goto out; 2802 } 2803 jblocks->jb_low = jblocks->jb_free / 3; /* Reserve 33%. */ 2804 jblocks->jb_min = jblocks->jb_free / 10; /* Suspend at 10%. */ 2805 ump->softdep_jblocks = jblocks; 2806 out: 2807 if (error == 0) { 2808 MNT_ILOCK(mp); 2809 mp->mnt_flag |= MNT_SUJ; 2810 mp->mnt_flag &= ~MNT_SOFTDEP; 2811 MNT_IUNLOCK(mp); 2812 /* 2813 * Only validate the journal contents if the 2814 * filesystem is clean, otherwise we write the logs 2815 * but they'll never be used. If the filesystem was 2816 * still dirty when we mounted it the journal is 2817 * invalid and a new journal can only be valid if it 2818 * starts from a clean mount. 2819 */ 2820 if (fs->fs_clean) { 2821 DIP_SET(ip, i_modrev, fs->fs_mtime); 2822 ip->i_flags |= IN_MODIFIED; 2823 ffs_update(vp, 1); 2824 } 2825 } 2826 vput(vp); 2827 return (error); 2828 } 2829 2830 static void 2831 journal_unmount(ump) 2832 struct ufsmount *ump; 2833 { 2834 2835 if (ump->softdep_jblocks) 2836 jblocks_destroy(ump->softdep_jblocks); 2837 ump->softdep_jblocks = NULL; 2838 } 2839 2840 /* 2841 * Called when a journal record is ready to be written. Space is allocated 2842 * and the journal entry is created when the journal is flushed to stable 2843 * store. 2844 */ 2845 static void 2846 add_to_journal(wk) 2847 struct worklist *wk; 2848 { 2849 struct ufsmount *ump; 2850 2851 ump = VFSTOUFS(wk->wk_mp); 2852 LOCK_OWNED(ump); 2853 if (wk->wk_state & ONWORKLIST) 2854 panic("add_to_journal: %s(0x%X) already on list", 2855 TYPENAME(wk->wk_type), wk->wk_state); 2856 wk->wk_state |= ONWORKLIST | DEPCOMPLETE; 2857 if (LIST_EMPTY(&ump->softdep_journal_pending)) { 2858 ump->softdep_jblocks->jb_age = ticks; 2859 LIST_INSERT_HEAD(&ump->softdep_journal_pending, wk, wk_list); 2860 } else 2861 LIST_INSERT_AFTER(ump->softdep_journal_tail, wk, wk_list); 2862 ump->softdep_journal_tail = wk; 2863 ump->softdep_on_journal += 1; 2864 } 2865 2866 /* 2867 * Remove an arbitrary item for the journal worklist maintain the tail 2868 * pointer. This happens when a new operation obviates the need to 2869 * journal an old operation. 2870 */ 2871 static void 2872 remove_from_journal(wk) 2873 struct worklist *wk; 2874 { 2875 struct ufsmount *ump; 2876 2877 ump = VFSTOUFS(wk->wk_mp); 2878 LOCK_OWNED(ump); 2879 #ifdef SUJ_DEBUG 2880 { 2881 struct worklist *wkn; 2882 2883 LIST_FOREACH(wkn, &ump->softdep_journal_pending, wk_list) 2884 if (wkn == wk) 2885 break; 2886 if (wkn == NULL) 2887 panic("remove_from_journal: %p is not in journal", wk); 2888 } 2889 #endif 2890 /* 2891 * We emulate a TAILQ to save space in most structures which do not 2892 * require TAILQ semantics. Here we must update the tail position 2893 * when removing the tail which is not the final entry. This works 2894 * only if the worklist linkage are at the beginning of the structure. 2895 */ 2896 if (ump->softdep_journal_tail == wk) 2897 ump->softdep_journal_tail = 2898 (struct worklist *)wk->wk_list.le_prev; 2899 WORKLIST_REMOVE(wk); 2900 ump->softdep_on_journal -= 1; 2901 } 2902 2903 /* 2904 * Check for journal space as well as dependency limits so the prelink 2905 * code can throttle both journaled and non-journaled filesystems. 2906 * Threshold is 0 for low and 1 for min. 2907 */ 2908 static int 2909 journal_space(ump, thresh) 2910 struct ufsmount *ump; 2911 int thresh; 2912 { 2913 struct jblocks *jblocks; 2914 int limit, avail; 2915 2916 jblocks = ump->softdep_jblocks; 2917 if (jblocks == NULL) 2918 return (1); 2919 /* 2920 * We use a tighter restriction here to prevent request_cleanup() 2921 * running in threads from running into locks we currently hold. 2922 * We have to be over the limit and our filesystem has to be 2923 * responsible for more than our share of that usage. 2924 */ 2925 limit = (max_softdeps / 10) * 9; 2926 if (dep_current[D_INODEDEP] > limit && 2927 ump->softdep_curdeps[D_INODEDEP] > limit / stat_flush_threads) 2928 return (0); 2929 if (thresh) 2930 thresh = jblocks->jb_min; 2931 else 2932 thresh = jblocks->jb_low; 2933 avail = (ump->softdep_on_journal * JREC_SIZE) / DEV_BSIZE; 2934 avail = jblocks->jb_free - avail; 2935 2936 return (avail > thresh); 2937 } 2938 2939 static void 2940 journal_suspend(ump) 2941 struct ufsmount *ump; 2942 { 2943 struct jblocks *jblocks; 2944 struct mount *mp; 2945 2946 mp = UFSTOVFS(ump); 2947 jblocks = ump->softdep_jblocks; 2948 MNT_ILOCK(mp); 2949 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) { 2950 stat_journal_min++; 2951 mp->mnt_kern_flag |= MNTK_SUSPEND; 2952 mp->mnt_susp_owner = ump->softdep_flushtd; 2953 } 2954 jblocks->jb_suspended = 1; 2955 MNT_IUNLOCK(mp); 2956 } 2957 2958 static int 2959 journal_unsuspend(struct ufsmount *ump) 2960 { 2961 struct jblocks *jblocks; 2962 struct mount *mp; 2963 2964 mp = UFSTOVFS(ump); 2965 jblocks = ump->softdep_jblocks; 2966 2967 if (jblocks != NULL && jblocks->jb_suspended && 2968 journal_space(ump, jblocks->jb_min)) { 2969 jblocks->jb_suspended = 0; 2970 FREE_LOCK(ump); 2971 mp->mnt_susp_owner = curthread; 2972 vfs_write_resume(mp, 0); 2973 ACQUIRE_LOCK(ump); 2974 return (1); 2975 } 2976 return (0); 2977 } 2978 2979 /* 2980 * Called before any allocation function to be certain that there is 2981 * sufficient space in the journal prior to creating any new records. 2982 * Since in the case of block allocation we may have multiple locked 2983 * buffers at the time of the actual allocation we can not block 2984 * when the journal records are created. Doing so would create a deadlock 2985 * if any of these buffers needed to be flushed to reclaim space. Instead 2986 * we require a sufficiently large amount of available space such that 2987 * each thread in the system could have passed this allocation check and 2988 * still have sufficient free space. With 20% of a minimum journal size 2989 * of 1MB we have 6553 records available. 2990 */ 2991 int 2992 softdep_prealloc(vp, waitok) 2993 struct vnode *vp; 2994 int waitok; 2995 { 2996 struct ufsmount *ump; 2997 2998 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 2999 ("softdep_prealloc called on non-softdep filesystem")); 3000 /* 3001 * Nothing to do if we are not running journaled soft updates. 3002 * If we currently hold the snapshot lock, we must avoid 3003 * handling other resources that could cause deadlock. Do not 3004 * touch quotas vnode since it is typically recursed with 3005 * other vnode locks held. 3006 */ 3007 if (DOINGSUJ(vp) == 0 || IS_SNAPSHOT(VTOI(vp)) || 3008 (vp->v_vflag & VV_SYSTEM) != 0) 3009 return (0); 3010 ump = VFSTOUFS(vp->v_mount); 3011 ACQUIRE_LOCK(ump); 3012 if (journal_space(ump, 0)) { 3013 FREE_LOCK(ump); 3014 return (0); 3015 } 3016 stat_journal_low++; 3017 FREE_LOCK(ump); 3018 if (waitok == MNT_NOWAIT) 3019 return (ENOSPC); 3020 /* 3021 * Attempt to sync this vnode once to flush any journal 3022 * work attached to it. 3023 */ 3024 if ((curthread->td_pflags & TDP_COWINPROGRESS) == 0) 3025 ffs_syncvnode(vp, waitok, 0); 3026 ACQUIRE_LOCK(ump); 3027 process_removes(vp); 3028 process_truncates(vp); 3029 if (journal_space(ump, 0) == 0) { 3030 softdep_speedup(ump); 3031 if (journal_space(ump, 1) == 0) 3032 journal_suspend(ump); 3033 } 3034 FREE_LOCK(ump); 3035 3036 return (0); 3037 } 3038 3039 /* 3040 * Before adjusting a link count on a vnode verify that we have sufficient 3041 * journal space. If not, process operations that depend on the currently 3042 * locked pair of vnodes to try to flush space as the syncer, buf daemon, 3043 * and softdep flush threads can not acquire these locks to reclaim space. 3044 */ 3045 static void 3046 softdep_prelink(dvp, vp) 3047 struct vnode *dvp; 3048 struct vnode *vp; 3049 { 3050 struct ufsmount *ump; 3051 3052 ump = VFSTOUFS(dvp->v_mount); 3053 LOCK_OWNED(ump); 3054 /* 3055 * Nothing to do if we have sufficient journal space. 3056 * If we currently hold the snapshot lock, we must avoid 3057 * handling other resources that could cause deadlock. 3058 */ 3059 if (journal_space(ump, 0) || (vp && IS_SNAPSHOT(VTOI(vp)))) 3060 return; 3061 stat_journal_low++; 3062 FREE_LOCK(ump); 3063 if (vp) 3064 ffs_syncvnode(vp, MNT_NOWAIT, 0); 3065 ffs_syncvnode(dvp, MNT_WAIT, 0); 3066 ACQUIRE_LOCK(ump); 3067 /* Process vp before dvp as it may create .. removes. */ 3068 if (vp) { 3069 process_removes(vp); 3070 process_truncates(vp); 3071 } 3072 process_removes(dvp); 3073 process_truncates(dvp); 3074 softdep_speedup(ump); 3075 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT); 3076 if (journal_space(ump, 0) == 0) { 3077 softdep_speedup(ump); 3078 if (journal_space(ump, 1) == 0) 3079 journal_suspend(ump); 3080 } 3081 } 3082 3083 static void 3084 jseg_write(ump, jseg, data) 3085 struct ufsmount *ump; 3086 struct jseg *jseg; 3087 uint8_t *data; 3088 { 3089 struct jsegrec *rec; 3090 3091 rec = (struct jsegrec *)data; 3092 rec->jsr_seq = jseg->js_seq; 3093 rec->jsr_oldest = jseg->js_oldseq; 3094 rec->jsr_cnt = jseg->js_cnt; 3095 rec->jsr_blocks = jseg->js_size / ump->um_devvp->v_bufobj.bo_bsize; 3096 rec->jsr_crc = 0; 3097 rec->jsr_time = ump->um_fs->fs_mtime; 3098 } 3099 3100 static inline void 3101 inoref_write(inoref, jseg, rec) 3102 struct inoref *inoref; 3103 struct jseg *jseg; 3104 struct jrefrec *rec; 3105 { 3106 3107 inoref->if_jsegdep->jd_seg = jseg; 3108 rec->jr_ino = inoref->if_ino; 3109 rec->jr_parent = inoref->if_parent; 3110 rec->jr_nlink = inoref->if_nlink; 3111 rec->jr_mode = inoref->if_mode; 3112 rec->jr_diroff = inoref->if_diroff; 3113 } 3114 3115 static void 3116 jaddref_write(jaddref, jseg, data) 3117 struct jaddref *jaddref; 3118 struct jseg *jseg; 3119 uint8_t *data; 3120 { 3121 struct jrefrec *rec; 3122 3123 rec = (struct jrefrec *)data; 3124 rec->jr_op = JOP_ADDREF; 3125 inoref_write(&jaddref->ja_ref, jseg, rec); 3126 } 3127 3128 static void 3129 jremref_write(jremref, jseg, data) 3130 struct jremref *jremref; 3131 struct jseg *jseg; 3132 uint8_t *data; 3133 { 3134 struct jrefrec *rec; 3135 3136 rec = (struct jrefrec *)data; 3137 rec->jr_op = JOP_REMREF; 3138 inoref_write(&jremref->jr_ref, jseg, rec); 3139 } 3140 3141 static void 3142 jmvref_write(jmvref, jseg, data) 3143 struct jmvref *jmvref; 3144 struct jseg *jseg; 3145 uint8_t *data; 3146 { 3147 struct jmvrec *rec; 3148 3149 rec = (struct jmvrec *)data; 3150 rec->jm_op = JOP_MVREF; 3151 rec->jm_ino = jmvref->jm_ino; 3152 rec->jm_parent = jmvref->jm_parent; 3153 rec->jm_oldoff = jmvref->jm_oldoff; 3154 rec->jm_newoff = jmvref->jm_newoff; 3155 } 3156 3157 static void 3158 jnewblk_write(jnewblk, jseg, data) 3159 struct jnewblk *jnewblk; 3160 struct jseg *jseg; 3161 uint8_t *data; 3162 { 3163 struct jblkrec *rec; 3164 3165 jnewblk->jn_jsegdep->jd_seg = jseg; 3166 rec = (struct jblkrec *)data; 3167 rec->jb_op = JOP_NEWBLK; 3168 rec->jb_ino = jnewblk->jn_ino; 3169 rec->jb_blkno = jnewblk->jn_blkno; 3170 rec->jb_lbn = jnewblk->jn_lbn; 3171 rec->jb_frags = jnewblk->jn_frags; 3172 rec->jb_oldfrags = jnewblk->jn_oldfrags; 3173 } 3174 3175 static void 3176 jfreeblk_write(jfreeblk, jseg, data) 3177 struct jfreeblk *jfreeblk; 3178 struct jseg *jseg; 3179 uint8_t *data; 3180 { 3181 struct jblkrec *rec; 3182 3183 jfreeblk->jf_dep.jb_jsegdep->jd_seg = jseg; 3184 rec = (struct jblkrec *)data; 3185 rec->jb_op = JOP_FREEBLK; 3186 rec->jb_ino = jfreeblk->jf_ino; 3187 rec->jb_blkno = jfreeblk->jf_blkno; 3188 rec->jb_lbn = jfreeblk->jf_lbn; 3189 rec->jb_frags = jfreeblk->jf_frags; 3190 rec->jb_oldfrags = 0; 3191 } 3192 3193 static void 3194 jfreefrag_write(jfreefrag, jseg, data) 3195 struct jfreefrag *jfreefrag; 3196 struct jseg *jseg; 3197 uint8_t *data; 3198 { 3199 struct jblkrec *rec; 3200 3201 jfreefrag->fr_jsegdep->jd_seg = jseg; 3202 rec = (struct jblkrec *)data; 3203 rec->jb_op = JOP_FREEBLK; 3204 rec->jb_ino = jfreefrag->fr_ino; 3205 rec->jb_blkno = jfreefrag->fr_blkno; 3206 rec->jb_lbn = jfreefrag->fr_lbn; 3207 rec->jb_frags = jfreefrag->fr_frags; 3208 rec->jb_oldfrags = 0; 3209 } 3210 3211 static void 3212 jtrunc_write(jtrunc, jseg, data) 3213 struct jtrunc *jtrunc; 3214 struct jseg *jseg; 3215 uint8_t *data; 3216 { 3217 struct jtrncrec *rec; 3218 3219 jtrunc->jt_dep.jb_jsegdep->jd_seg = jseg; 3220 rec = (struct jtrncrec *)data; 3221 rec->jt_op = JOP_TRUNC; 3222 rec->jt_ino = jtrunc->jt_ino; 3223 rec->jt_size = jtrunc->jt_size; 3224 rec->jt_extsize = jtrunc->jt_extsize; 3225 } 3226 3227 static void 3228 jfsync_write(jfsync, jseg, data) 3229 struct jfsync *jfsync; 3230 struct jseg *jseg; 3231 uint8_t *data; 3232 { 3233 struct jtrncrec *rec; 3234 3235 rec = (struct jtrncrec *)data; 3236 rec->jt_op = JOP_SYNC; 3237 rec->jt_ino = jfsync->jfs_ino; 3238 rec->jt_size = jfsync->jfs_size; 3239 rec->jt_extsize = jfsync->jfs_extsize; 3240 } 3241 3242 static void 3243 softdep_flushjournal(mp) 3244 struct mount *mp; 3245 { 3246 struct jblocks *jblocks; 3247 struct ufsmount *ump; 3248 3249 if (MOUNTEDSUJ(mp) == 0) 3250 return; 3251 ump = VFSTOUFS(mp); 3252 jblocks = ump->softdep_jblocks; 3253 ACQUIRE_LOCK(ump); 3254 while (ump->softdep_on_journal) { 3255 jblocks->jb_needseg = 1; 3256 softdep_process_journal(mp, NULL, MNT_WAIT); 3257 } 3258 FREE_LOCK(ump); 3259 } 3260 3261 static void softdep_synchronize_completed(struct bio *); 3262 static void softdep_synchronize(struct bio *, struct ufsmount *, void *); 3263 3264 static void 3265 softdep_synchronize_completed(bp) 3266 struct bio *bp; 3267 { 3268 struct jseg *oldest; 3269 struct jseg *jseg; 3270 struct ufsmount *ump; 3271 3272 /* 3273 * caller1 marks the last segment written before we issued the 3274 * synchronize cache. 3275 */ 3276 jseg = bp->bio_caller1; 3277 if (jseg == NULL) { 3278 g_destroy_bio(bp); 3279 return; 3280 } 3281 ump = VFSTOUFS(jseg->js_list.wk_mp); 3282 ACQUIRE_LOCK(ump); 3283 oldest = NULL; 3284 /* 3285 * Mark all the journal entries waiting on the synchronize cache 3286 * as completed so they may continue on. 3287 */ 3288 while (jseg != NULL && (jseg->js_state & COMPLETE) == 0) { 3289 jseg->js_state |= COMPLETE; 3290 oldest = jseg; 3291 jseg = TAILQ_PREV(jseg, jseglst, js_next); 3292 } 3293 /* 3294 * Restart deferred journal entry processing from the oldest 3295 * completed jseg. 3296 */ 3297 if (oldest) 3298 complete_jsegs(oldest); 3299 3300 FREE_LOCK(ump); 3301 g_destroy_bio(bp); 3302 } 3303 3304 /* 3305 * Send BIO_FLUSH/SYNCHRONIZE CACHE to the device to enforce write ordering 3306 * barriers. The journal must be written prior to any blocks that depend 3307 * on it and the journal can not be released until the blocks have be 3308 * written. This code handles both barriers simultaneously. 3309 */ 3310 static void 3311 softdep_synchronize(bp, ump, caller1) 3312 struct bio *bp; 3313 struct ufsmount *ump; 3314 void *caller1; 3315 { 3316 3317 bp->bio_cmd = BIO_FLUSH; 3318 bp->bio_flags |= BIO_ORDERED; 3319 bp->bio_data = NULL; 3320 bp->bio_offset = ump->um_cp->provider->mediasize; 3321 bp->bio_length = 0; 3322 bp->bio_done = softdep_synchronize_completed; 3323 bp->bio_caller1 = caller1; 3324 g_io_request(bp, 3325 (struct g_consumer *)ump->um_devvp->v_bufobj.bo_private); 3326 } 3327 3328 /* 3329 * Flush some journal records to disk. 3330 */ 3331 static void 3332 softdep_process_journal(mp, needwk, flags) 3333 struct mount *mp; 3334 struct worklist *needwk; 3335 int flags; 3336 { 3337 struct jblocks *jblocks; 3338 struct ufsmount *ump; 3339 struct worklist *wk; 3340 struct jseg *jseg; 3341 struct buf *bp; 3342 struct bio *bio; 3343 uint8_t *data; 3344 struct fs *fs; 3345 int shouldflush; 3346 int segwritten; 3347 int jrecmin; /* Minimum records per block. */ 3348 int jrecmax; /* Maximum records per block. */ 3349 int size; 3350 int cnt; 3351 int off; 3352 int devbsize; 3353 3354 if (MOUNTEDSUJ(mp) == 0) 3355 return; 3356 shouldflush = softdep_flushcache; 3357 bio = NULL; 3358 jseg = NULL; 3359 ump = VFSTOUFS(mp); 3360 LOCK_OWNED(ump); 3361 fs = ump->um_fs; 3362 jblocks = ump->softdep_jblocks; 3363 devbsize = ump->um_devvp->v_bufobj.bo_bsize; 3364 /* 3365 * We write anywhere between a disk block and fs block. The upper 3366 * bound is picked to prevent buffer cache fragmentation and limit 3367 * processing time per I/O. 3368 */ 3369 jrecmin = (devbsize / JREC_SIZE) - 1; /* -1 for seg header */ 3370 jrecmax = (fs->fs_bsize / devbsize) * jrecmin; 3371 segwritten = 0; 3372 for (;;) { 3373 cnt = ump->softdep_on_journal; 3374 /* 3375 * Criteria for writing a segment: 3376 * 1) We have a full block. 3377 * 2) We're called from jwait() and haven't found the 3378 * journal item yet. 3379 * 3) Always write if needseg is set. 3380 * 4) If we are called from process_worklist and have 3381 * not yet written anything we write a partial block 3382 * to enforce a 1 second maximum latency on journal 3383 * entries. 3384 */ 3385 if (cnt < (jrecmax - 1) && needwk == NULL && 3386 jblocks->jb_needseg == 0 && (segwritten || cnt == 0)) 3387 break; 3388 cnt++; 3389 /* 3390 * Verify some free journal space. softdep_prealloc() should 3391 * guarantee that we don't run out so this is indicative of 3392 * a problem with the flow control. Try to recover 3393 * gracefully in any event. 3394 */ 3395 while (jblocks->jb_free == 0) { 3396 if (flags != MNT_WAIT) 3397 break; 3398 printf("softdep: Out of journal space!\n"); 3399 softdep_speedup(ump); 3400 msleep(jblocks, LOCK_PTR(ump), PRIBIO, "jblocks", hz); 3401 } 3402 FREE_LOCK(ump); 3403 jseg = malloc(sizeof(*jseg), M_JSEG, M_SOFTDEP_FLAGS); 3404 workitem_alloc(&jseg->js_list, D_JSEG, mp); 3405 LIST_INIT(&jseg->js_entries); 3406 LIST_INIT(&jseg->js_indirs); 3407 jseg->js_state = ATTACHED; 3408 if (shouldflush == 0) 3409 jseg->js_state |= COMPLETE; 3410 else if (bio == NULL) 3411 bio = g_alloc_bio(); 3412 jseg->js_jblocks = jblocks; 3413 bp = geteblk(fs->fs_bsize, 0); 3414 ACQUIRE_LOCK(ump); 3415 /* 3416 * If there was a race while we were allocating the block 3417 * and jseg the entry we care about was likely written. 3418 * We bail out in both the WAIT and NOWAIT case and assume 3419 * the caller will loop if the entry it cares about is 3420 * not written. 3421 */ 3422 cnt = ump->softdep_on_journal; 3423 if (cnt + jblocks->jb_needseg == 0 || jblocks->jb_free == 0) { 3424 bp->b_flags |= B_INVAL | B_NOCACHE; 3425 WORKITEM_FREE(jseg, D_JSEG); 3426 FREE_LOCK(ump); 3427 brelse(bp); 3428 ACQUIRE_LOCK(ump); 3429 break; 3430 } 3431 /* 3432 * Calculate the disk block size required for the available 3433 * records rounded to the min size. 3434 */ 3435 if (cnt == 0) 3436 size = devbsize; 3437 else if (cnt < jrecmax) 3438 size = howmany(cnt, jrecmin) * devbsize; 3439 else 3440 size = fs->fs_bsize; 3441 /* 3442 * Allocate a disk block for this journal data and account 3443 * for truncation of the requested size if enough contiguous 3444 * space was not available. 3445 */ 3446 bp->b_blkno = jblocks_alloc(jblocks, size, &size); 3447 bp->b_lblkno = bp->b_blkno; 3448 bp->b_offset = bp->b_blkno * DEV_BSIZE; 3449 bp->b_bcount = size; 3450 bp->b_flags &= ~B_INVAL; 3451 bp->b_flags |= B_VALIDSUSPWRT | B_NOCOPY; 3452 /* 3453 * Initialize our jseg with cnt records. Assign the next 3454 * sequence number to it and link it in-order. 3455 */ 3456 cnt = MIN(cnt, (size / devbsize) * jrecmin); 3457 jseg->js_buf = bp; 3458 jseg->js_cnt = cnt; 3459 jseg->js_refs = cnt + 1; /* Self ref. */ 3460 jseg->js_size = size; 3461 jseg->js_seq = jblocks->jb_nextseq++; 3462 if (jblocks->jb_oldestseg == NULL) 3463 jblocks->jb_oldestseg = jseg; 3464 jseg->js_oldseq = jblocks->jb_oldestseg->js_seq; 3465 TAILQ_INSERT_TAIL(&jblocks->jb_segs, jseg, js_next); 3466 if (jblocks->jb_writeseg == NULL) 3467 jblocks->jb_writeseg = jseg; 3468 /* 3469 * Start filling in records from the pending list. 3470 */ 3471 data = bp->b_data; 3472 off = 0; 3473 3474 /* 3475 * Always put a header on the first block. 3476 * XXX As with below, there might not be a chance to get 3477 * into the loop. Ensure that something valid is written. 3478 */ 3479 jseg_write(ump, jseg, data); 3480 off += JREC_SIZE; 3481 data = bp->b_data + off; 3482 3483 /* 3484 * XXX Something is wrong here. There's no work to do, 3485 * but we need to perform and I/O and allow it to complete 3486 * anyways. 3487 */ 3488 if (LIST_EMPTY(&ump->softdep_journal_pending)) 3489 stat_emptyjblocks++; 3490 3491 while ((wk = LIST_FIRST(&ump->softdep_journal_pending)) 3492 != NULL) { 3493 if (cnt == 0) 3494 break; 3495 /* Place a segment header on every device block. */ 3496 if ((off % devbsize) == 0) { 3497 jseg_write(ump, jseg, data); 3498 off += JREC_SIZE; 3499 data = bp->b_data + off; 3500 } 3501 if (wk == needwk) 3502 needwk = NULL; 3503 remove_from_journal(wk); 3504 wk->wk_state |= INPROGRESS; 3505 WORKLIST_INSERT(&jseg->js_entries, wk); 3506 switch (wk->wk_type) { 3507 case D_JADDREF: 3508 jaddref_write(WK_JADDREF(wk), jseg, data); 3509 break; 3510 case D_JREMREF: 3511 jremref_write(WK_JREMREF(wk), jseg, data); 3512 break; 3513 case D_JMVREF: 3514 jmvref_write(WK_JMVREF(wk), jseg, data); 3515 break; 3516 case D_JNEWBLK: 3517 jnewblk_write(WK_JNEWBLK(wk), jseg, data); 3518 break; 3519 case D_JFREEBLK: 3520 jfreeblk_write(WK_JFREEBLK(wk), jseg, data); 3521 break; 3522 case D_JFREEFRAG: 3523 jfreefrag_write(WK_JFREEFRAG(wk), jseg, data); 3524 break; 3525 case D_JTRUNC: 3526 jtrunc_write(WK_JTRUNC(wk), jseg, data); 3527 break; 3528 case D_JFSYNC: 3529 jfsync_write(WK_JFSYNC(wk), jseg, data); 3530 break; 3531 default: 3532 panic("process_journal: Unknown type %s", 3533 TYPENAME(wk->wk_type)); 3534 /* NOTREACHED */ 3535 } 3536 off += JREC_SIZE; 3537 data = bp->b_data + off; 3538 cnt--; 3539 } 3540 3541 /* Clear any remaining space so we don't leak kernel data */ 3542 if (size > off) 3543 bzero(data, size - off); 3544 3545 /* 3546 * Write this one buffer and continue. 3547 */ 3548 segwritten = 1; 3549 jblocks->jb_needseg = 0; 3550 WORKLIST_INSERT(&bp->b_dep, &jseg->js_list); 3551 FREE_LOCK(ump); 3552 pbgetvp(ump->um_devvp, bp); 3553 /* 3554 * We only do the blocking wait once we find the journal 3555 * entry we're looking for. 3556 */ 3557 if (needwk == NULL && flags == MNT_WAIT) 3558 bwrite(bp); 3559 else 3560 bawrite(bp); 3561 ACQUIRE_LOCK(ump); 3562 } 3563 /* 3564 * If we wrote a segment issue a synchronize cache so the journal 3565 * is reflected on disk before the data is written. Since reclaiming 3566 * journal space also requires writing a journal record this 3567 * process also enforces a barrier before reclamation. 3568 */ 3569 if (segwritten && shouldflush) { 3570 softdep_synchronize(bio, ump, 3571 TAILQ_LAST(&jblocks->jb_segs, jseglst)); 3572 } else if (bio) 3573 g_destroy_bio(bio); 3574 /* 3575 * If we've suspended the filesystem because we ran out of journal 3576 * space either try to sync it here to make some progress or 3577 * unsuspend it if we already have. 3578 */ 3579 if (flags == 0 && jblocks->jb_suspended) { 3580 if (journal_unsuspend(ump)) 3581 return; 3582 FREE_LOCK(ump); 3583 VFS_SYNC(mp, MNT_NOWAIT); 3584 ffs_sbupdate(ump, MNT_WAIT, 0); 3585 ACQUIRE_LOCK(ump); 3586 } 3587 } 3588 3589 /* 3590 * Complete a jseg, allowing all dependencies awaiting journal writes 3591 * to proceed. Each journal dependency also attaches a jsegdep to dependent 3592 * structures so that the journal segment can be freed to reclaim space. 3593 */ 3594 static void 3595 complete_jseg(jseg) 3596 struct jseg *jseg; 3597 { 3598 struct worklist *wk; 3599 struct jmvref *jmvref; 3600 #ifdef INVARIANTS 3601 int i = 0; 3602 #endif 3603 3604 while ((wk = LIST_FIRST(&jseg->js_entries)) != NULL) { 3605 WORKLIST_REMOVE(wk); 3606 wk->wk_state &= ~INPROGRESS; 3607 wk->wk_state |= COMPLETE; 3608 KASSERT(i++ < jseg->js_cnt, 3609 ("handle_written_jseg: overflow %d >= %d", 3610 i - 1, jseg->js_cnt)); 3611 switch (wk->wk_type) { 3612 case D_JADDREF: 3613 handle_written_jaddref(WK_JADDREF(wk)); 3614 break; 3615 case D_JREMREF: 3616 handle_written_jremref(WK_JREMREF(wk)); 3617 break; 3618 case D_JMVREF: 3619 rele_jseg(jseg); /* No jsegdep. */ 3620 jmvref = WK_JMVREF(wk); 3621 LIST_REMOVE(jmvref, jm_deps); 3622 if ((jmvref->jm_pagedep->pd_state & ONWORKLIST) == 0) 3623 free_pagedep(jmvref->jm_pagedep); 3624 WORKITEM_FREE(jmvref, D_JMVREF); 3625 break; 3626 case D_JNEWBLK: 3627 handle_written_jnewblk(WK_JNEWBLK(wk)); 3628 break; 3629 case D_JFREEBLK: 3630 handle_written_jblkdep(&WK_JFREEBLK(wk)->jf_dep); 3631 break; 3632 case D_JTRUNC: 3633 handle_written_jblkdep(&WK_JTRUNC(wk)->jt_dep); 3634 break; 3635 case D_JFSYNC: 3636 rele_jseg(jseg); /* No jsegdep. */ 3637 WORKITEM_FREE(wk, D_JFSYNC); 3638 break; 3639 case D_JFREEFRAG: 3640 handle_written_jfreefrag(WK_JFREEFRAG(wk)); 3641 break; 3642 default: 3643 panic("handle_written_jseg: Unknown type %s", 3644 TYPENAME(wk->wk_type)); 3645 /* NOTREACHED */ 3646 } 3647 } 3648 /* Release the self reference so the structure may be freed. */ 3649 rele_jseg(jseg); 3650 } 3651 3652 /* 3653 * Determine which jsegs are ready for completion processing. Waits for 3654 * synchronize cache to complete as well as forcing in-order completion 3655 * of journal entries. 3656 */ 3657 static void 3658 complete_jsegs(jseg) 3659 struct jseg *jseg; 3660 { 3661 struct jblocks *jblocks; 3662 struct jseg *jsegn; 3663 3664 jblocks = jseg->js_jblocks; 3665 /* 3666 * Don't allow out of order completions. If this isn't the first 3667 * block wait for it to write before we're done. 3668 */ 3669 if (jseg != jblocks->jb_writeseg) 3670 return; 3671 /* Iterate through available jsegs processing their entries. */ 3672 while (jseg && (jseg->js_state & ALLCOMPLETE) == ALLCOMPLETE) { 3673 jblocks->jb_oldestwrseq = jseg->js_oldseq; 3674 jsegn = TAILQ_NEXT(jseg, js_next); 3675 complete_jseg(jseg); 3676 jseg = jsegn; 3677 } 3678 jblocks->jb_writeseg = jseg; 3679 /* 3680 * Attempt to free jsegs now that oldestwrseq may have advanced. 3681 */ 3682 free_jsegs(jblocks); 3683 } 3684 3685 /* 3686 * Mark a jseg as DEPCOMPLETE and throw away the buffer. Attempt to handle 3687 * the final completions. 3688 */ 3689 static void 3690 handle_written_jseg(jseg, bp) 3691 struct jseg *jseg; 3692 struct buf *bp; 3693 { 3694 3695 if (jseg->js_refs == 0) 3696 panic("handle_written_jseg: No self-reference on %p", jseg); 3697 jseg->js_state |= DEPCOMPLETE; 3698 /* 3699 * We'll never need this buffer again, set flags so it will be 3700 * discarded. 3701 */ 3702 bp->b_flags |= B_INVAL | B_NOCACHE; 3703 pbrelvp(bp); 3704 complete_jsegs(jseg); 3705 } 3706 3707 static inline struct jsegdep * 3708 inoref_jseg(inoref) 3709 struct inoref *inoref; 3710 { 3711 struct jsegdep *jsegdep; 3712 3713 jsegdep = inoref->if_jsegdep; 3714 inoref->if_jsegdep = NULL; 3715 3716 return (jsegdep); 3717 } 3718 3719 /* 3720 * Called once a jremref has made it to stable store. The jremref is marked 3721 * complete and we attempt to free it. Any pagedeps writes sleeping waiting 3722 * for the jremref to complete will be awoken by free_jremref. 3723 */ 3724 static void 3725 handle_written_jremref(jremref) 3726 struct jremref *jremref; 3727 { 3728 struct inodedep *inodedep; 3729 struct jsegdep *jsegdep; 3730 struct dirrem *dirrem; 3731 3732 /* Grab the jsegdep. */ 3733 jsegdep = inoref_jseg(&jremref->jr_ref); 3734 /* 3735 * Remove us from the inoref list. 3736 */ 3737 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 3738 0, &inodedep) == 0) 3739 panic("handle_written_jremref: Lost inodedep"); 3740 TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 3741 /* 3742 * Complete the dirrem. 3743 */ 3744 dirrem = jremref->jr_dirrem; 3745 jremref->jr_dirrem = NULL; 3746 LIST_REMOVE(jremref, jr_deps); 3747 jsegdep->jd_state |= jremref->jr_state & MKDIR_PARENT; 3748 jwork_insert(&dirrem->dm_jwork, jsegdep); 3749 if (LIST_EMPTY(&dirrem->dm_jremrefhd) && 3750 (dirrem->dm_state & COMPLETE) != 0) 3751 add_to_worklist(&dirrem->dm_list, 0); 3752 free_jremref(jremref); 3753 } 3754 3755 /* 3756 * Called once a jaddref has made it to stable store. The dependency is 3757 * marked complete and any dependent structures are added to the inode 3758 * bufwait list to be completed as soon as it is written. If a bitmap write 3759 * depends on this entry we move the inode into the inodedephd of the 3760 * bmsafemap dependency and attempt to remove the jaddref from the bmsafemap. 3761 */ 3762 static void 3763 handle_written_jaddref(jaddref) 3764 struct jaddref *jaddref; 3765 { 3766 struct jsegdep *jsegdep; 3767 struct inodedep *inodedep; 3768 struct diradd *diradd; 3769 struct mkdir *mkdir; 3770 3771 /* Grab the jsegdep. */ 3772 jsegdep = inoref_jseg(&jaddref->ja_ref); 3773 mkdir = NULL; 3774 diradd = NULL; 3775 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 3776 0, &inodedep) == 0) 3777 panic("handle_written_jaddref: Lost inodedep."); 3778 if (jaddref->ja_diradd == NULL) 3779 panic("handle_written_jaddref: No dependency"); 3780 if (jaddref->ja_diradd->da_list.wk_type == D_DIRADD) { 3781 diradd = jaddref->ja_diradd; 3782 WORKLIST_INSERT(&inodedep->id_bufwait, &diradd->da_list); 3783 } else if (jaddref->ja_state & MKDIR_PARENT) { 3784 mkdir = jaddref->ja_mkdir; 3785 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir->md_list); 3786 } else if (jaddref->ja_state & MKDIR_BODY) 3787 mkdir = jaddref->ja_mkdir; 3788 else 3789 panic("handle_written_jaddref: Unknown dependency %p", 3790 jaddref->ja_diradd); 3791 jaddref->ja_diradd = NULL; /* also clears ja_mkdir */ 3792 /* 3793 * Remove us from the inode list. 3794 */ 3795 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps); 3796 /* 3797 * The mkdir may be waiting on the jaddref to clear before freeing. 3798 */ 3799 if (mkdir) { 3800 KASSERT(mkdir->md_list.wk_type == D_MKDIR, 3801 ("handle_written_jaddref: Incorrect type for mkdir %s", 3802 TYPENAME(mkdir->md_list.wk_type))); 3803 mkdir->md_jaddref = NULL; 3804 diradd = mkdir->md_diradd; 3805 mkdir->md_state |= DEPCOMPLETE; 3806 complete_mkdir(mkdir); 3807 } 3808 jwork_insert(&diradd->da_jwork, jsegdep); 3809 if (jaddref->ja_state & NEWBLOCK) { 3810 inodedep->id_state |= ONDEPLIST; 3811 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_inodedephd, 3812 inodedep, id_deps); 3813 } 3814 free_jaddref(jaddref); 3815 } 3816 3817 /* 3818 * Called once a jnewblk journal is written. The allocdirect or allocindir 3819 * is placed in the bmsafemap to await notification of a written bitmap. If 3820 * the operation was canceled we add the segdep to the appropriate 3821 * dependency to free the journal space once the canceling operation 3822 * completes. 3823 */ 3824 static void 3825 handle_written_jnewblk(jnewblk) 3826 struct jnewblk *jnewblk; 3827 { 3828 struct bmsafemap *bmsafemap; 3829 struct freefrag *freefrag; 3830 struct freework *freework; 3831 struct jsegdep *jsegdep; 3832 struct newblk *newblk; 3833 3834 /* Grab the jsegdep. */ 3835 jsegdep = jnewblk->jn_jsegdep; 3836 jnewblk->jn_jsegdep = NULL; 3837 if (jnewblk->jn_dep == NULL) 3838 panic("handle_written_jnewblk: No dependency for the segdep."); 3839 switch (jnewblk->jn_dep->wk_type) { 3840 case D_NEWBLK: 3841 case D_ALLOCDIRECT: 3842 case D_ALLOCINDIR: 3843 /* 3844 * Add the written block to the bmsafemap so it can 3845 * be notified when the bitmap is on disk. 3846 */ 3847 newblk = WK_NEWBLK(jnewblk->jn_dep); 3848 newblk->nb_jnewblk = NULL; 3849 if ((newblk->nb_state & GOINGAWAY) == 0) { 3850 bmsafemap = newblk->nb_bmsafemap; 3851 newblk->nb_state |= ONDEPLIST; 3852 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, 3853 nb_deps); 3854 } 3855 jwork_insert(&newblk->nb_jwork, jsegdep); 3856 break; 3857 case D_FREEFRAG: 3858 /* 3859 * A newblock being removed by a freefrag when replaced by 3860 * frag extension. 3861 */ 3862 freefrag = WK_FREEFRAG(jnewblk->jn_dep); 3863 freefrag->ff_jdep = NULL; 3864 jwork_insert(&freefrag->ff_jwork, jsegdep); 3865 break; 3866 case D_FREEWORK: 3867 /* 3868 * A direct block was removed by truncate. 3869 */ 3870 freework = WK_FREEWORK(jnewblk->jn_dep); 3871 freework->fw_jnewblk = NULL; 3872 jwork_insert(&freework->fw_freeblks->fb_jwork, jsegdep); 3873 break; 3874 default: 3875 panic("handle_written_jnewblk: Unknown type %d.", 3876 jnewblk->jn_dep->wk_type); 3877 } 3878 jnewblk->jn_dep = NULL; 3879 free_jnewblk(jnewblk); 3880 } 3881 3882 /* 3883 * Cancel a jfreefrag that won't be needed, probably due to colliding with 3884 * an in-flight allocation that has not yet been committed. Divorce us 3885 * from the freefrag and mark it DEPCOMPLETE so that it may be added 3886 * to the worklist. 3887 */ 3888 static void 3889 cancel_jfreefrag(jfreefrag) 3890 struct jfreefrag *jfreefrag; 3891 { 3892 struct freefrag *freefrag; 3893 3894 if (jfreefrag->fr_jsegdep) { 3895 free_jsegdep(jfreefrag->fr_jsegdep); 3896 jfreefrag->fr_jsegdep = NULL; 3897 } 3898 freefrag = jfreefrag->fr_freefrag; 3899 jfreefrag->fr_freefrag = NULL; 3900 free_jfreefrag(jfreefrag); 3901 freefrag->ff_state |= DEPCOMPLETE; 3902 CTR1(KTR_SUJ, "cancel_jfreefrag: blkno %jd", freefrag->ff_blkno); 3903 } 3904 3905 /* 3906 * Free a jfreefrag when the parent freefrag is rendered obsolete. 3907 */ 3908 static void 3909 free_jfreefrag(jfreefrag) 3910 struct jfreefrag *jfreefrag; 3911 { 3912 3913 if (jfreefrag->fr_state & INPROGRESS) 3914 WORKLIST_REMOVE(&jfreefrag->fr_list); 3915 else if (jfreefrag->fr_state & ONWORKLIST) 3916 remove_from_journal(&jfreefrag->fr_list); 3917 if (jfreefrag->fr_freefrag != NULL) 3918 panic("free_jfreefrag: Still attached to a freefrag."); 3919 WORKITEM_FREE(jfreefrag, D_JFREEFRAG); 3920 } 3921 3922 /* 3923 * Called when the journal write for a jfreefrag completes. The parent 3924 * freefrag is added to the worklist if this completes its dependencies. 3925 */ 3926 static void 3927 handle_written_jfreefrag(jfreefrag) 3928 struct jfreefrag *jfreefrag; 3929 { 3930 struct jsegdep *jsegdep; 3931 struct freefrag *freefrag; 3932 3933 /* Grab the jsegdep. */ 3934 jsegdep = jfreefrag->fr_jsegdep; 3935 jfreefrag->fr_jsegdep = NULL; 3936 freefrag = jfreefrag->fr_freefrag; 3937 if (freefrag == NULL) 3938 panic("handle_written_jfreefrag: No freefrag."); 3939 freefrag->ff_state |= DEPCOMPLETE; 3940 freefrag->ff_jdep = NULL; 3941 jwork_insert(&freefrag->ff_jwork, jsegdep); 3942 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 3943 add_to_worklist(&freefrag->ff_list, 0); 3944 jfreefrag->fr_freefrag = NULL; 3945 free_jfreefrag(jfreefrag); 3946 } 3947 3948 /* 3949 * Called when the journal write for a jfreeblk completes. The jfreeblk 3950 * is removed from the freeblks list of pending journal writes and the 3951 * jsegdep is moved to the freeblks jwork to be completed when all blocks 3952 * have been reclaimed. 3953 */ 3954 static void 3955 handle_written_jblkdep(jblkdep) 3956 struct jblkdep *jblkdep; 3957 { 3958 struct freeblks *freeblks; 3959 struct jsegdep *jsegdep; 3960 3961 /* Grab the jsegdep. */ 3962 jsegdep = jblkdep->jb_jsegdep; 3963 jblkdep->jb_jsegdep = NULL; 3964 freeblks = jblkdep->jb_freeblks; 3965 LIST_REMOVE(jblkdep, jb_deps); 3966 jwork_insert(&freeblks->fb_jwork, jsegdep); 3967 /* 3968 * If the freeblks is all journaled, we can add it to the worklist. 3969 */ 3970 if (LIST_EMPTY(&freeblks->fb_jblkdephd) && 3971 (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 3972 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 3973 3974 free_jblkdep(jblkdep); 3975 } 3976 3977 static struct jsegdep * 3978 newjsegdep(struct worklist *wk) 3979 { 3980 struct jsegdep *jsegdep; 3981 3982 jsegdep = malloc(sizeof(*jsegdep), M_JSEGDEP, M_SOFTDEP_FLAGS); 3983 workitem_alloc(&jsegdep->jd_list, D_JSEGDEP, wk->wk_mp); 3984 jsegdep->jd_seg = NULL; 3985 3986 return (jsegdep); 3987 } 3988 3989 static struct jmvref * 3990 newjmvref(dp, ino, oldoff, newoff) 3991 struct inode *dp; 3992 ino_t ino; 3993 off_t oldoff; 3994 off_t newoff; 3995 { 3996 struct jmvref *jmvref; 3997 3998 jmvref = malloc(sizeof(*jmvref), M_JMVREF, M_SOFTDEP_FLAGS); 3999 workitem_alloc(&jmvref->jm_list, D_JMVREF, ITOVFS(dp)); 4000 jmvref->jm_list.wk_state = ATTACHED | DEPCOMPLETE; 4001 jmvref->jm_parent = dp->i_number; 4002 jmvref->jm_ino = ino; 4003 jmvref->jm_oldoff = oldoff; 4004 jmvref->jm_newoff = newoff; 4005 4006 return (jmvref); 4007 } 4008 4009 /* 4010 * Allocate a new jremref that tracks the removal of ip from dp with the 4011 * directory entry offset of diroff. Mark the entry as ATTACHED and 4012 * DEPCOMPLETE as we have all the information required for the journal write 4013 * and the directory has already been removed from the buffer. The caller 4014 * is responsible for linking the jremref into the pagedep and adding it 4015 * to the journal to write. The MKDIR_PARENT flag is set if we're doing 4016 * a DOTDOT addition so handle_workitem_remove() can properly assign 4017 * the jsegdep when we're done. 4018 */ 4019 static struct jremref * 4020 newjremref(struct dirrem *dirrem, struct inode *dp, struct inode *ip, 4021 off_t diroff, nlink_t nlink) 4022 { 4023 struct jremref *jremref; 4024 4025 jremref = malloc(sizeof(*jremref), M_JREMREF, M_SOFTDEP_FLAGS); 4026 workitem_alloc(&jremref->jr_list, D_JREMREF, ITOVFS(dp)); 4027 jremref->jr_state = ATTACHED; 4028 newinoref(&jremref->jr_ref, ip->i_number, dp->i_number, diroff, 4029 nlink, ip->i_mode); 4030 jremref->jr_dirrem = dirrem; 4031 4032 return (jremref); 4033 } 4034 4035 static inline void 4036 newinoref(struct inoref *inoref, ino_t ino, ino_t parent, off_t diroff, 4037 nlink_t nlink, uint16_t mode) 4038 { 4039 4040 inoref->if_jsegdep = newjsegdep(&inoref->if_list); 4041 inoref->if_diroff = diroff; 4042 inoref->if_ino = ino; 4043 inoref->if_parent = parent; 4044 inoref->if_nlink = nlink; 4045 inoref->if_mode = mode; 4046 } 4047 4048 /* 4049 * Allocate a new jaddref to track the addition of ino to dp at diroff. The 4050 * directory offset may not be known until later. The caller is responsible 4051 * adding the entry to the journal when this information is available. nlink 4052 * should be the link count prior to the addition and mode is only required 4053 * to have the correct FMT. 4054 */ 4055 static struct jaddref * 4056 newjaddref(struct inode *dp, ino_t ino, off_t diroff, int16_t nlink, 4057 uint16_t mode) 4058 { 4059 struct jaddref *jaddref; 4060 4061 jaddref = malloc(sizeof(*jaddref), M_JADDREF, M_SOFTDEP_FLAGS); 4062 workitem_alloc(&jaddref->ja_list, D_JADDREF, ITOVFS(dp)); 4063 jaddref->ja_state = ATTACHED; 4064 jaddref->ja_mkdir = NULL; 4065 newinoref(&jaddref->ja_ref, ino, dp->i_number, diroff, nlink, mode); 4066 4067 return (jaddref); 4068 } 4069 4070 /* 4071 * Create a new free dependency for a freework. The caller is responsible 4072 * for adjusting the reference count when it has the lock held. The freedep 4073 * will track an outstanding bitmap write that will ultimately clear the 4074 * freework to continue. 4075 */ 4076 static struct freedep * 4077 newfreedep(struct freework *freework) 4078 { 4079 struct freedep *freedep; 4080 4081 freedep = malloc(sizeof(*freedep), M_FREEDEP, M_SOFTDEP_FLAGS); 4082 workitem_alloc(&freedep->fd_list, D_FREEDEP, freework->fw_list.wk_mp); 4083 freedep->fd_freework = freework; 4084 4085 return (freedep); 4086 } 4087 4088 /* 4089 * Free a freedep structure once the buffer it is linked to is written. If 4090 * this is the last reference to the freework schedule it for completion. 4091 */ 4092 static void 4093 free_freedep(freedep) 4094 struct freedep *freedep; 4095 { 4096 struct freework *freework; 4097 4098 freework = freedep->fd_freework; 4099 freework->fw_freeblks->fb_cgwait--; 4100 if (--freework->fw_ref == 0) 4101 freework_enqueue(freework); 4102 WORKITEM_FREE(freedep, D_FREEDEP); 4103 } 4104 4105 /* 4106 * Allocate a new freework structure that may be a level in an indirect 4107 * when parent is not NULL or a top level block when it is. The top level 4108 * freework structures are allocated without the per-filesystem lock held 4109 * and before the freeblks is visible outside of softdep_setup_freeblocks(). 4110 */ 4111 static struct freework * 4112 newfreework(ump, freeblks, parent, lbn, nb, frags, off, journal) 4113 struct ufsmount *ump; 4114 struct freeblks *freeblks; 4115 struct freework *parent; 4116 ufs_lbn_t lbn; 4117 ufs2_daddr_t nb; 4118 int frags; 4119 int off; 4120 int journal; 4121 { 4122 struct freework *freework; 4123 4124 freework = malloc(sizeof(*freework), M_FREEWORK, M_SOFTDEP_FLAGS); 4125 workitem_alloc(&freework->fw_list, D_FREEWORK, freeblks->fb_list.wk_mp); 4126 freework->fw_state = ATTACHED; 4127 freework->fw_jnewblk = NULL; 4128 freework->fw_freeblks = freeblks; 4129 freework->fw_parent = parent; 4130 freework->fw_lbn = lbn; 4131 freework->fw_blkno = nb; 4132 freework->fw_frags = frags; 4133 freework->fw_indir = NULL; 4134 freework->fw_ref = (MOUNTEDSUJ(UFSTOVFS(ump)) == 0 || 4135 lbn >= -UFS_NXADDR) ? 0 : NINDIR(ump->um_fs) + 1; 4136 freework->fw_start = freework->fw_off = off; 4137 if (journal) 4138 newjfreeblk(freeblks, lbn, nb, frags); 4139 if (parent == NULL) { 4140 ACQUIRE_LOCK(ump); 4141 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 4142 freeblks->fb_ref++; 4143 FREE_LOCK(ump); 4144 } 4145 4146 return (freework); 4147 } 4148 4149 /* 4150 * Eliminate a jfreeblk for a block that does not need journaling. 4151 */ 4152 static void 4153 cancel_jfreeblk(freeblks, blkno) 4154 struct freeblks *freeblks; 4155 ufs2_daddr_t blkno; 4156 { 4157 struct jfreeblk *jfreeblk; 4158 struct jblkdep *jblkdep; 4159 4160 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) { 4161 if (jblkdep->jb_list.wk_type != D_JFREEBLK) 4162 continue; 4163 jfreeblk = WK_JFREEBLK(&jblkdep->jb_list); 4164 if (jfreeblk->jf_blkno == blkno) 4165 break; 4166 } 4167 if (jblkdep == NULL) 4168 return; 4169 CTR1(KTR_SUJ, "cancel_jfreeblk: blkno %jd", blkno); 4170 free_jsegdep(jblkdep->jb_jsegdep); 4171 LIST_REMOVE(jblkdep, jb_deps); 4172 WORKITEM_FREE(jfreeblk, D_JFREEBLK); 4173 } 4174 4175 /* 4176 * Allocate a new jfreeblk to journal top level block pointer when truncating 4177 * a file. The caller must add this to the worklist when the per-filesystem 4178 * lock is held. 4179 */ 4180 static struct jfreeblk * 4181 newjfreeblk(freeblks, lbn, blkno, frags) 4182 struct freeblks *freeblks; 4183 ufs_lbn_t lbn; 4184 ufs2_daddr_t blkno; 4185 int frags; 4186 { 4187 struct jfreeblk *jfreeblk; 4188 4189 jfreeblk = malloc(sizeof(*jfreeblk), M_JFREEBLK, M_SOFTDEP_FLAGS); 4190 workitem_alloc(&jfreeblk->jf_dep.jb_list, D_JFREEBLK, 4191 freeblks->fb_list.wk_mp); 4192 jfreeblk->jf_dep.jb_jsegdep = newjsegdep(&jfreeblk->jf_dep.jb_list); 4193 jfreeblk->jf_dep.jb_freeblks = freeblks; 4194 jfreeblk->jf_ino = freeblks->fb_inum; 4195 jfreeblk->jf_lbn = lbn; 4196 jfreeblk->jf_blkno = blkno; 4197 jfreeblk->jf_frags = frags; 4198 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jfreeblk->jf_dep, jb_deps); 4199 4200 return (jfreeblk); 4201 } 4202 4203 /* 4204 * The journal is only prepared to handle full-size block numbers, so we 4205 * have to adjust the record to reflect the change to a full-size block. 4206 * For example, suppose we have a block made up of fragments 8-15 and 4207 * want to free its last two fragments. We are given a request that says: 4208 * FREEBLK ino=5, blkno=14, lbn=0, frags=2, oldfrags=0 4209 * where frags are the number of fragments to free and oldfrags are the 4210 * number of fragments to keep. To block align it, we have to change it to 4211 * have a valid full-size blkno, so it becomes: 4212 * FREEBLK ino=5, blkno=8, lbn=0, frags=2, oldfrags=6 4213 */ 4214 static void 4215 adjust_newfreework(freeblks, frag_offset) 4216 struct freeblks *freeblks; 4217 int frag_offset; 4218 { 4219 struct jfreeblk *jfreeblk; 4220 4221 KASSERT((LIST_FIRST(&freeblks->fb_jblkdephd) != NULL && 4222 LIST_FIRST(&freeblks->fb_jblkdephd)->jb_list.wk_type == D_JFREEBLK), 4223 ("adjust_newfreework: Missing freeblks dependency")); 4224 4225 jfreeblk = WK_JFREEBLK(LIST_FIRST(&freeblks->fb_jblkdephd)); 4226 jfreeblk->jf_blkno -= frag_offset; 4227 jfreeblk->jf_frags += frag_offset; 4228 } 4229 4230 /* 4231 * Allocate a new jtrunc to track a partial truncation. 4232 */ 4233 static struct jtrunc * 4234 newjtrunc(freeblks, size, extsize) 4235 struct freeblks *freeblks; 4236 off_t size; 4237 int extsize; 4238 { 4239 struct jtrunc *jtrunc; 4240 4241 jtrunc = malloc(sizeof(*jtrunc), M_JTRUNC, M_SOFTDEP_FLAGS); 4242 workitem_alloc(&jtrunc->jt_dep.jb_list, D_JTRUNC, 4243 freeblks->fb_list.wk_mp); 4244 jtrunc->jt_dep.jb_jsegdep = newjsegdep(&jtrunc->jt_dep.jb_list); 4245 jtrunc->jt_dep.jb_freeblks = freeblks; 4246 jtrunc->jt_ino = freeblks->fb_inum; 4247 jtrunc->jt_size = size; 4248 jtrunc->jt_extsize = extsize; 4249 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jtrunc->jt_dep, jb_deps); 4250 4251 return (jtrunc); 4252 } 4253 4254 /* 4255 * If we're canceling a new bitmap we have to search for another ref 4256 * to move into the bmsafemap dep. This might be better expressed 4257 * with another structure. 4258 */ 4259 static void 4260 move_newblock_dep(jaddref, inodedep) 4261 struct jaddref *jaddref; 4262 struct inodedep *inodedep; 4263 { 4264 struct inoref *inoref; 4265 struct jaddref *jaddrefn; 4266 4267 jaddrefn = NULL; 4268 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4269 inoref = TAILQ_NEXT(inoref, if_deps)) { 4270 if ((jaddref->ja_state & NEWBLOCK) && 4271 inoref->if_list.wk_type == D_JADDREF) { 4272 jaddrefn = (struct jaddref *)inoref; 4273 break; 4274 } 4275 } 4276 if (jaddrefn == NULL) 4277 return; 4278 jaddrefn->ja_state &= ~(ATTACHED | UNDONE); 4279 jaddrefn->ja_state |= jaddref->ja_state & 4280 (ATTACHED | UNDONE | NEWBLOCK); 4281 jaddref->ja_state &= ~(ATTACHED | UNDONE | NEWBLOCK); 4282 jaddref->ja_state |= ATTACHED; 4283 LIST_REMOVE(jaddref, ja_bmdeps); 4284 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_jaddrefhd, jaddrefn, 4285 ja_bmdeps); 4286 } 4287 4288 /* 4289 * Cancel a jaddref either before it has been written or while it is being 4290 * written. This happens when a link is removed before the add reaches 4291 * the disk. The jaddref dependency is kept linked into the bmsafemap 4292 * and inode to prevent the link count or bitmap from reaching the disk 4293 * until handle_workitem_remove() re-adjusts the counts and bitmaps as 4294 * required. 4295 * 4296 * Returns 1 if the canceled addref requires journaling of the remove and 4297 * 0 otherwise. 4298 */ 4299 static int 4300 cancel_jaddref(jaddref, inodedep, wkhd) 4301 struct jaddref *jaddref; 4302 struct inodedep *inodedep; 4303 struct workhead *wkhd; 4304 { 4305 struct inoref *inoref; 4306 struct jsegdep *jsegdep; 4307 int needsj; 4308 4309 KASSERT((jaddref->ja_state & COMPLETE) == 0, 4310 ("cancel_jaddref: Canceling complete jaddref")); 4311 if (jaddref->ja_state & (INPROGRESS | COMPLETE)) 4312 needsj = 1; 4313 else 4314 needsj = 0; 4315 if (inodedep == NULL) 4316 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 4317 0, &inodedep) == 0) 4318 panic("cancel_jaddref: Lost inodedep"); 4319 /* 4320 * We must adjust the nlink of any reference operation that follows 4321 * us so that it is consistent with the in-memory reference. This 4322 * ensures that inode nlink rollbacks always have the correct link. 4323 */ 4324 if (needsj == 0) { 4325 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4326 inoref = TAILQ_NEXT(inoref, if_deps)) { 4327 if (inoref->if_state & GOINGAWAY) 4328 break; 4329 inoref->if_nlink--; 4330 } 4331 } 4332 jsegdep = inoref_jseg(&jaddref->ja_ref); 4333 if (jaddref->ja_state & NEWBLOCK) 4334 move_newblock_dep(jaddref, inodedep); 4335 wake_worklist(&jaddref->ja_list); 4336 jaddref->ja_mkdir = NULL; 4337 if (jaddref->ja_state & INPROGRESS) { 4338 jaddref->ja_state &= ~INPROGRESS; 4339 WORKLIST_REMOVE(&jaddref->ja_list); 4340 jwork_insert(wkhd, jsegdep); 4341 } else { 4342 free_jsegdep(jsegdep); 4343 if (jaddref->ja_state & DEPCOMPLETE) 4344 remove_from_journal(&jaddref->ja_list); 4345 } 4346 jaddref->ja_state |= (GOINGAWAY | DEPCOMPLETE); 4347 /* 4348 * Leave NEWBLOCK jaddrefs on the inodedep so handle_workitem_remove 4349 * can arrange for them to be freed with the bitmap. Otherwise we 4350 * no longer need this addref attached to the inoreflst and it 4351 * will incorrectly adjust nlink if we leave it. 4352 */ 4353 if ((jaddref->ja_state & NEWBLOCK) == 0) { 4354 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 4355 if_deps); 4356 jaddref->ja_state |= COMPLETE; 4357 free_jaddref(jaddref); 4358 return (needsj); 4359 } 4360 /* 4361 * Leave the head of the list for jsegdeps for fast merging. 4362 */ 4363 if (LIST_FIRST(wkhd) != NULL) { 4364 jaddref->ja_state |= ONWORKLIST; 4365 LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list); 4366 } else 4367 WORKLIST_INSERT(wkhd, &jaddref->ja_list); 4368 4369 return (needsj); 4370 } 4371 4372 /* 4373 * Attempt to free a jaddref structure when some work completes. This 4374 * should only succeed once the entry is written and all dependencies have 4375 * been notified. 4376 */ 4377 static void 4378 free_jaddref(jaddref) 4379 struct jaddref *jaddref; 4380 { 4381 4382 if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE) 4383 return; 4384 if (jaddref->ja_ref.if_jsegdep) 4385 panic("free_jaddref: segdep attached to jaddref %p(0x%X)\n", 4386 jaddref, jaddref->ja_state); 4387 if (jaddref->ja_state & NEWBLOCK) 4388 LIST_REMOVE(jaddref, ja_bmdeps); 4389 if (jaddref->ja_state & (INPROGRESS | ONWORKLIST)) 4390 panic("free_jaddref: Bad state %p(0x%X)", 4391 jaddref, jaddref->ja_state); 4392 if (jaddref->ja_mkdir != NULL) 4393 panic("free_jaddref: Work pending, 0x%X\n", jaddref->ja_state); 4394 WORKITEM_FREE(jaddref, D_JADDREF); 4395 } 4396 4397 /* 4398 * Free a jremref structure once it has been written or discarded. 4399 */ 4400 static void 4401 free_jremref(jremref) 4402 struct jremref *jremref; 4403 { 4404 4405 if (jremref->jr_ref.if_jsegdep) 4406 free_jsegdep(jremref->jr_ref.if_jsegdep); 4407 if (jremref->jr_state & INPROGRESS) 4408 panic("free_jremref: IO still pending"); 4409 WORKITEM_FREE(jremref, D_JREMREF); 4410 } 4411 4412 /* 4413 * Free a jnewblk structure. 4414 */ 4415 static void 4416 free_jnewblk(jnewblk) 4417 struct jnewblk *jnewblk; 4418 { 4419 4420 if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE) 4421 return; 4422 LIST_REMOVE(jnewblk, jn_deps); 4423 if (jnewblk->jn_dep != NULL) 4424 panic("free_jnewblk: Dependency still attached."); 4425 WORKITEM_FREE(jnewblk, D_JNEWBLK); 4426 } 4427 4428 /* 4429 * Cancel a jnewblk which has been been made redundant by frag extension. 4430 */ 4431 static void 4432 cancel_jnewblk(jnewblk, wkhd) 4433 struct jnewblk *jnewblk; 4434 struct workhead *wkhd; 4435 { 4436 struct jsegdep *jsegdep; 4437 4438 CTR1(KTR_SUJ, "cancel_jnewblk: blkno %jd", jnewblk->jn_blkno); 4439 jsegdep = jnewblk->jn_jsegdep; 4440 if (jnewblk->jn_jsegdep == NULL || jnewblk->jn_dep == NULL) 4441 panic("cancel_jnewblk: Invalid state"); 4442 jnewblk->jn_jsegdep = NULL; 4443 jnewblk->jn_dep = NULL; 4444 jnewblk->jn_state |= GOINGAWAY; 4445 if (jnewblk->jn_state & INPROGRESS) { 4446 jnewblk->jn_state &= ~INPROGRESS; 4447 WORKLIST_REMOVE(&jnewblk->jn_list); 4448 jwork_insert(wkhd, jsegdep); 4449 } else { 4450 free_jsegdep(jsegdep); 4451 remove_from_journal(&jnewblk->jn_list); 4452 } 4453 wake_worklist(&jnewblk->jn_list); 4454 WORKLIST_INSERT(wkhd, &jnewblk->jn_list); 4455 } 4456 4457 static void 4458 free_jblkdep(jblkdep) 4459 struct jblkdep *jblkdep; 4460 { 4461 4462 if (jblkdep->jb_list.wk_type == D_JFREEBLK) 4463 WORKITEM_FREE(jblkdep, D_JFREEBLK); 4464 else if (jblkdep->jb_list.wk_type == D_JTRUNC) 4465 WORKITEM_FREE(jblkdep, D_JTRUNC); 4466 else 4467 panic("free_jblkdep: Unexpected type %s", 4468 TYPENAME(jblkdep->jb_list.wk_type)); 4469 } 4470 4471 /* 4472 * Free a single jseg once it is no longer referenced in memory or on 4473 * disk. Reclaim journal blocks and dependencies waiting for the segment 4474 * to disappear. 4475 */ 4476 static void 4477 free_jseg(jseg, jblocks) 4478 struct jseg *jseg; 4479 struct jblocks *jblocks; 4480 { 4481 struct freework *freework; 4482 4483 /* 4484 * Free freework structures that were lingering to indicate freed 4485 * indirect blocks that forced journal write ordering on reallocate. 4486 */ 4487 while ((freework = LIST_FIRST(&jseg->js_indirs)) != NULL) 4488 indirblk_remove(freework); 4489 if (jblocks->jb_oldestseg == jseg) 4490 jblocks->jb_oldestseg = TAILQ_NEXT(jseg, js_next); 4491 TAILQ_REMOVE(&jblocks->jb_segs, jseg, js_next); 4492 jblocks_free(jblocks, jseg->js_list.wk_mp, jseg->js_size); 4493 KASSERT(LIST_EMPTY(&jseg->js_entries), 4494 ("free_jseg: Freed jseg has valid entries.")); 4495 WORKITEM_FREE(jseg, D_JSEG); 4496 } 4497 4498 /* 4499 * Free all jsegs that meet the criteria for being reclaimed and update 4500 * oldestseg. 4501 */ 4502 static void 4503 free_jsegs(jblocks) 4504 struct jblocks *jblocks; 4505 { 4506 struct jseg *jseg; 4507 4508 /* 4509 * Free only those jsegs which have none allocated before them to 4510 * preserve the journal space ordering. 4511 */ 4512 while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) { 4513 /* 4514 * Only reclaim space when nothing depends on this journal 4515 * set and another set has written that it is no longer 4516 * valid. 4517 */ 4518 if (jseg->js_refs != 0) { 4519 jblocks->jb_oldestseg = jseg; 4520 return; 4521 } 4522 if ((jseg->js_state & ALLCOMPLETE) != ALLCOMPLETE) 4523 break; 4524 if (jseg->js_seq > jblocks->jb_oldestwrseq) 4525 break; 4526 /* 4527 * We can free jsegs that didn't write entries when 4528 * oldestwrseq == js_seq. 4529 */ 4530 if (jseg->js_seq == jblocks->jb_oldestwrseq && 4531 jseg->js_cnt != 0) 4532 break; 4533 free_jseg(jseg, jblocks); 4534 } 4535 /* 4536 * If we exited the loop above we still must discover the 4537 * oldest valid segment. 4538 */ 4539 if (jseg) 4540 for (jseg = jblocks->jb_oldestseg; jseg != NULL; 4541 jseg = TAILQ_NEXT(jseg, js_next)) 4542 if (jseg->js_refs != 0) 4543 break; 4544 jblocks->jb_oldestseg = jseg; 4545 /* 4546 * The journal has no valid records but some jsegs may still be 4547 * waiting on oldestwrseq to advance. We force a small record 4548 * out to permit these lingering records to be reclaimed. 4549 */ 4550 if (jblocks->jb_oldestseg == NULL && !TAILQ_EMPTY(&jblocks->jb_segs)) 4551 jblocks->jb_needseg = 1; 4552 } 4553 4554 /* 4555 * Release one reference to a jseg and free it if the count reaches 0. This 4556 * should eventually reclaim journal space as well. 4557 */ 4558 static void 4559 rele_jseg(jseg) 4560 struct jseg *jseg; 4561 { 4562 4563 KASSERT(jseg->js_refs > 0, 4564 ("free_jseg: Invalid refcnt %d", jseg->js_refs)); 4565 if (--jseg->js_refs != 0) 4566 return; 4567 free_jsegs(jseg->js_jblocks); 4568 } 4569 4570 /* 4571 * Release a jsegdep and decrement the jseg count. 4572 */ 4573 static void 4574 free_jsegdep(jsegdep) 4575 struct jsegdep *jsegdep; 4576 { 4577 4578 if (jsegdep->jd_seg) 4579 rele_jseg(jsegdep->jd_seg); 4580 WORKITEM_FREE(jsegdep, D_JSEGDEP); 4581 } 4582 4583 /* 4584 * Wait for a journal item to make it to disk. Initiate journal processing 4585 * if required. 4586 */ 4587 static int 4588 jwait(wk, waitfor) 4589 struct worklist *wk; 4590 int waitfor; 4591 { 4592 4593 LOCK_OWNED(VFSTOUFS(wk->wk_mp)); 4594 /* 4595 * Blocking journal waits cause slow synchronous behavior. Record 4596 * stats on the frequency of these blocking operations. 4597 */ 4598 if (waitfor == MNT_WAIT) { 4599 stat_journal_wait++; 4600 switch (wk->wk_type) { 4601 case D_JREMREF: 4602 case D_JMVREF: 4603 stat_jwait_filepage++; 4604 break; 4605 case D_JTRUNC: 4606 case D_JFREEBLK: 4607 stat_jwait_freeblks++; 4608 break; 4609 case D_JNEWBLK: 4610 stat_jwait_newblk++; 4611 break; 4612 case D_JADDREF: 4613 stat_jwait_inode++; 4614 break; 4615 default: 4616 break; 4617 } 4618 } 4619 /* 4620 * If IO has not started we process the journal. We can't mark the 4621 * worklist item as IOWAITING because we drop the lock while 4622 * processing the journal and the worklist entry may be freed after 4623 * this point. The caller may call back in and re-issue the request. 4624 */ 4625 if ((wk->wk_state & INPROGRESS) == 0) { 4626 softdep_process_journal(wk->wk_mp, wk, waitfor); 4627 if (waitfor != MNT_WAIT) 4628 return (EBUSY); 4629 return (0); 4630 } 4631 if (waitfor != MNT_WAIT) 4632 return (EBUSY); 4633 wait_worklist(wk, "jwait"); 4634 return (0); 4635 } 4636 4637 /* 4638 * Lookup an inodedep based on an inode pointer and set the nlinkdelta as 4639 * appropriate. This is a convenience function to reduce duplicate code 4640 * for the setup and revert functions below. 4641 */ 4642 static struct inodedep * 4643 inodedep_lookup_ip(ip) 4644 struct inode *ip; 4645 { 4646 struct inodedep *inodedep; 4647 4648 KASSERT(ip->i_nlink >= ip->i_effnlink, 4649 ("inodedep_lookup_ip: bad delta")); 4650 (void) inodedep_lookup(ITOVFS(ip), ip->i_number, DEPALLOC, 4651 &inodedep); 4652 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 4653 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 4654 4655 return (inodedep); 4656 } 4657 4658 /* 4659 * Called prior to creating a new inode and linking it to a directory. The 4660 * jaddref structure must already be allocated by softdep_setup_inomapdep 4661 * and it is discovered here so we can initialize the mode and update 4662 * nlinkdelta. 4663 */ 4664 void 4665 softdep_setup_create(dp, ip) 4666 struct inode *dp; 4667 struct inode *ip; 4668 { 4669 struct inodedep *inodedep; 4670 struct jaddref *jaddref; 4671 struct vnode *dvp; 4672 4673 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4674 ("softdep_setup_create called on non-softdep filesystem")); 4675 KASSERT(ip->i_nlink == 1, 4676 ("softdep_setup_create: Invalid link count.")); 4677 dvp = ITOV(dp); 4678 ACQUIRE_LOCK(ITOUMP(dp)); 4679 inodedep = inodedep_lookup_ip(ip); 4680 if (DOINGSUJ(dvp)) { 4681 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4682 inoreflst); 4683 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 4684 ("softdep_setup_create: No addref structure present.")); 4685 } 4686 softdep_prelink(dvp, NULL); 4687 FREE_LOCK(ITOUMP(dp)); 4688 } 4689 4690 /* 4691 * Create a jaddref structure to track the addition of a DOTDOT link when 4692 * we are reparenting an inode as part of a rename. This jaddref will be 4693 * found by softdep_setup_directory_change. Adjusts nlinkdelta for 4694 * non-journaling softdep. 4695 */ 4696 void 4697 softdep_setup_dotdot_link(dp, ip) 4698 struct inode *dp; 4699 struct inode *ip; 4700 { 4701 struct inodedep *inodedep; 4702 struct jaddref *jaddref; 4703 struct vnode *dvp; 4704 4705 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4706 ("softdep_setup_dotdot_link called on non-softdep filesystem")); 4707 dvp = ITOV(dp); 4708 jaddref = NULL; 4709 /* 4710 * We don't set MKDIR_PARENT as this is not tied to a mkdir and 4711 * is used as a normal link would be. 4712 */ 4713 if (DOINGSUJ(dvp)) 4714 jaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 4715 dp->i_effnlink - 1, dp->i_mode); 4716 ACQUIRE_LOCK(ITOUMP(dp)); 4717 inodedep = inodedep_lookup_ip(dp); 4718 if (jaddref) 4719 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4720 if_deps); 4721 softdep_prelink(dvp, ITOV(ip)); 4722 FREE_LOCK(ITOUMP(dp)); 4723 } 4724 4725 /* 4726 * Create a jaddref structure to track a new link to an inode. The directory 4727 * offset is not known until softdep_setup_directory_add or 4728 * softdep_setup_directory_change. Adjusts nlinkdelta for non-journaling 4729 * softdep. 4730 */ 4731 void 4732 softdep_setup_link(dp, ip) 4733 struct inode *dp; 4734 struct inode *ip; 4735 { 4736 struct inodedep *inodedep; 4737 struct jaddref *jaddref; 4738 struct vnode *dvp; 4739 4740 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4741 ("softdep_setup_link called on non-softdep filesystem")); 4742 dvp = ITOV(dp); 4743 jaddref = NULL; 4744 if (DOINGSUJ(dvp)) 4745 jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1, 4746 ip->i_mode); 4747 ACQUIRE_LOCK(ITOUMP(dp)); 4748 inodedep = inodedep_lookup_ip(ip); 4749 if (jaddref) 4750 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4751 if_deps); 4752 softdep_prelink(dvp, ITOV(ip)); 4753 FREE_LOCK(ITOUMP(dp)); 4754 } 4755 4756 /* 4757 * Called to create the jaddref structures to track . and .. references as 4758 * well as lookup and further initialize the incomplete jaddref created 4759 * by softdep_setup_inomapdep when the inode was allocated. Adjusts 4760 * nlinkdelta for non-journaling softdep. 4761 */ 4762 void 4763 softdep_setup_mkdir(dp, ip) 4764 struct inode *dp; 4765 struct inode *ip; 4766 { 4767 struct inodedep *inodedep; 4768 struct jaddref *dotdotaddref; 4769 struct jaddref *dotaddref; 4770 struct jaddref *jaddref; 4771 struct vnode *dvp; 4772 4773 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4774 ("softdep_setup_mkdir called on non-softdep filesystem")); 4775 dvp = ITOV(dp); 4776 dotaddref = dotdotaddref = NULL; 4777 if (DOINGSUJ(dvp)) { 4778 dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1, 4779 ip->i_mode); 4780 dotaddref->ja_state |= MKDIR_BODY; 4781 dotdotaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 4782 dp->i_effnlink - 1, dp->i_mode); 4783 dotdotaddref->ja_state |= MKDIR_PARENT; 4784 } 4785 ACQUIRE_LOCK(ITOUMP(dp)); 4786 inodedep = inodedep_lookup_ip(ip); 4787 if (DOINGSUJ(dvp)) { 4788 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4789 inoreflst); 4790 KASSERT(jaddref != NULL, 4791 ("softdep_setup_mkdir: No addref structure present.")); 4792 KASSERT(jaddref->ja_parent == dp->i_number, 4793 ("softdep_setup_mkdir: bad parent %ju", 4794 (uintmax_t)jaddref->ja_parent)); 4795 TAILQ_INSERT_BEFORE(&jaddref->ja_ref, &dotaddref->ja_ref, 4796 if_deps); 4797 } 4798 inodedep = inodedep_lookup_ip(dp); 4799 if (DOINGSUJ(dvp)) 4800 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, 4801 &dotdotaddref->ja_ref, if_deps); 4802 softdep_prelink(ITOV(dp), NULL); 4803 FREE_LOCK(ITOUMP(dp)); 4804 } 4805 4806 /* 4807 * Called to track nlinkdelta of the inode and parent directories prior to 4808 * unlinking a directory. 4809 */ 4810 void 4811 softdep_setup_rmdir(dp, ip) 4812 struct inode *dp; 4813 struct inode *ip; 4814 { 4815 struct vnode *dvp; 4816 4817 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4818 ("softdep_setup_rmdir called on non-softdep filesystem")); 4819 dvp = ITOV(dp); 4820 ACQUIRE_LOCK(ITOUMP(dp)); 4821 (void) inodedep_lookup_ip(ip); 4822 (void) inodedep_lookup_ip(dp); 4823 softdep_prelink(dvp, ITOV(ip)); 4824 FREE_LOCK(ITOUMP(dp)); 4825 } 4826 4827 /* 4828 * Called to track nlinkdelta of the inode and parent directories prior to 4829 * unlink. 4830 */ 4831 void 4832 softdep_setup_unlink(dp, ip) 4833 struct inode *dp; 4834 struct inode *ip; 4835 { 4836 struct vnode *dvp; 4837 4838 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4839 ("softdep_setup_unlink called on non-softdep filesystem")); 4840 dvp = ITOV(dp); 4841 ACQUIRE_LOCK(ITOUMP(dp)); 4842 (void) inodedep_lookup_ip(ip); 4843 (void) inodedep_lookup_ip(dp); 4844 softdep_prelink(dvp, ITOV(ip)); 4845 FREE_LOCK(ITOUMP(dp)); 4846 } 4847 4848 /* 4849 * Called to release the journal structures created by a failed non-directory 4850 * creation. Adjusts nlinkdelta for non-journaling softdep. 4851 */ 4852 void 4853 softdep_revert_create(dp, ip) 4854 struct inode *dp; 4855 struct inode *ip; 4856 { 4857 struct inodedep *inodedep; 4858 struct jaddref *jaddref; 4859 struct vnode *dvp; 4860 4861 KASSERT(MOUNTEDSOFTDEP(ITOVFS((dp))) != 0, 4862 ("softdep_revert_create called on non-softdep filesystem")); 4863 dvp = ITOV(dp); 4864 ACQUIRE_LOCK(ITOUMP(dp)); 4865 inodedep = inodedep_lookup_ip(ip); 4866 if (DOINGSUJ(dvp)) { 4867 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4868 inoreflst); 4869 KASSERT(jaddref->ja_parent == dp->i_number, 4870 ("softdep_revert_create: addref parent mismatch")); 4871 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4872 } 4873 FREE_LOCK(ITOUMP(dp)); 4874 } 4875 4876 /* 4877 * Called to release the journal structures created by a failed link 4878 * addition. Adjusts nlinkdelta for non-journaling softdep. 4879 */ 4880 void 4881 softdep_revert_link(dp, ip) 4882 struct inode *dp; 4883 struct inode *ip; 4884 { 4885 struct inodedep *inodedep; 4886 struct jaddref *jaddref; 4887 struct vnode *dvp; 4888 4889 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4890 ("softdep_revert_link called on non-softdep filesystem")); 4891 dvp = ITOV(dp); 4892 ACQUIRE_LOCK(ITOUMP(dp)); 4893 inodedep = inodedep_lookup_ip(ip); 4894 if (DOINGSUJ(dvp)) { 4895 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4896 inoreflst); 4897 KASSERT(jaddref->ja_parent == dp->i_number, 4898 ("softdep_revert_link: addref parent mismatch")); 4899 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4900 } 4901 FREE_LOCK(ITOUMP(dp)); 4902 } 4903 4904 /* 4905 * Called to release the journal structures created by a failed mkdir 4906 * attempt. Adjusts nlinkdelta for non-journaling softdep. 4907 */ 4908 void 4909 softdep_revert_mkdir(dp, ip) 4910 struct inode *dp; 4911 struct inode *ip; 4912 { 4913 struct inodedep *inodedep; 4914 struct jaddref *jaddref; 4915 struct jaddref *dotaddref; 4916 struct vnode *dvp; 4917 4918 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4919 ("softdep_revert_mkdir called on non-softdep filesystem")); 4920 dvp = ITOV(dp); 4921 4922 ACQUIRE_LOCK(ITOUMP(dp)); 4923 inodedep = inodedep_lookup_ip(dp); 4924 if (DOINGSUJ(dvp)) { 4925 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4926 inoreflst); 4927 KASSERT(jaddref->ja_parent == ip->i_number, 4928 ("softdep_revert_mkdir: dotdot addref parent mismatch")); 4929 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4930 } 4931 inodedep = inodedep_lookup_ip(ip); 4932 if (DOINGSUJ(dvp)) { 4933 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4934 inoreflst); 4935 KASSERT(jaddref->ja_parent == dp->i_number, 4936 ("softdep_revert_mkdir: addref parent mismatch")); 4937 dotaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 4938 inoreflst, if_deps); 4939 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4940 KASSERT(dotaddref->ja_parent == ip->i_number, 4941 ("softdep_revert_mkdir: dot addref parent mismatch")); 4942 cancel_jaddref(dotaddref, inodedep, &inodedep->id_inowait); 4943 } 4944 FREE_LOCK(ITOUMP(dp)); 4945 } 4946 4947 /* 4948 * Called to correct nlinkdelta after a failed rmdir. 4949 */ 4950 void 4951 softdep_revert_rmdir(dp, ip) 4952 struct inode *dp; 4953 struct inode *ip; 4954 { 4955 4956 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4957 ("softdep_revert_rmdir called on non-softdep filesystem")); 4958 ACQUIRE_LOCK(ITOUMP(dp)); 4959 (void) inodedep_lookup_ip(ip); 4960 (void) inodedep_lookup_ip(dp); 4961 FREE_LOCK(ITOUMP(dp)); 4962 } 4963 4964 /* 4965 * Protecting the freemaps (or bitmaps). 4966 * 4967 * To eliminate the need to execute fsck before mounting a filesystem 4968 * after a power failure, one must (conservatively) guarantee that the 4969 * on-disk copy of the bitmaps never indicate that a live inode or block is 4970 * free. So, when a block or inode is allocated, the bitmap should be 4971 * updated (on disk) before any new pointers. When a block or inode is 4972 * freed, the bitmap should not be updated until all pointers have been 4973 * reset. The latter dependency is handled by the delayed de-allocation 4974 * approach described below for block and inode de-allocation. The former 4975 * dependency is handled by calling the following procedure when a block or 4976 * inode is allocated. When an inode is allocated an "inodedep" is created 4977 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk. 4978 * Each "inodedep" is also inserted into the hash indexing structure so 4979 * that any additional link additions can be made dependent on the inode 4980 * allocation. 4981 * 4982 * The ufs filesystem maintains a number of free block counts (e.g., per 4983 * cylinder group, per cylinder and per <cylinder, rotational position> pair) 4984 * in addition to the bitmaps. These counts are used to improve efficiency 4985 * during allocation and therefore must be consistent with the bitmaps. 4986 * There is no convenient way to guarantee post-crash consistency of these 4987 * counts with simple update ordering, for two main reasons: (1) The counts 4988 * and bitmaps for a single cylinder group block are not in the same disk 4989 * sector. If a disk write is interrupted (e.g., by power failure), one may 4990 * be written and the other not. (2) Some of the counts are located in the 4991 * superblock rather than the cylinder group block. So, we focus our soft 4992 * updates implementation on protecting the bitmaps. When mounting a 4993 * filesystem, we recompute the auxiliary counts from the bitmaps. 4994 */ 4995 4996 /* 4997 * Called just after updating the cylinder group block to allocate an inode. 4998 */ 4999 void 5000 softdep_setup_inomapdep(bp, ip, newinum, mode) 5001 struct buf *bp; /* buffer for cylgroup block with inode map */ 5002 struct inode *ip; /* inode related to allocation */ 5003 ino_t newinum; /* new inode number being allocated */ 5004 int mode; 5005 { 5006 struct inodedep *inodedep; 5007 struct bmsafemap *bmsafemap; 5008 struct jaddref *jaddref; 5009 struct mount *mp; 5010 struct fs *fs; 5011 5012 mp = ITOVFS(ip); 5013 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5014 ("softdep_setup_inomapdep called on non-softdep filesystem")); 5015 fs = VFSTOUFS(mp)->um_fs; 5016 jaddref = NULL; 5017 5018 /* 5019 * Allocate the journal reference add structure so that the bitmap 5020 * can be dependent on it. 5021 */ 5022 if (MOUNTEDSUJ(mp)) { 5023 jaddref = newjaddref(ip, newinum, 0, 0, mode); 5024 jaddref->ja_state |= NEWBLOCK; 5025 } 5026 5027 /* 5028 * Create a dependency for the newly allocated inode. 5029 * Panic if it already exists as something is seriously wrong. 5030 * Otherwise add it to the dependency list for the buffer holding 5031 * the cylinder group map from which it was allocated. 5032 * 5033 * We have to preallocate a bmsafemap entry in case it is needed 5034 * in bmsafemap_lookup since once we allocate the inodedep, we 5035 * have to finish initializing it before we can FREE_LOCK(). 5036 * By preallocating, we avoid FREE_LOCK() while doing a malloc 5037 * in bmsafemap_lookup. We cannot call bmsafemap_lookup before 5038 * creating the inodedep as it can be freed during the time 5039 * that we FREE_LOCK() while allocating the inodedep. We must 5040 * call workitem_alloc() before entering the locked section as 5041 * it also acquires the lock and we must avoid trying doing so 5042 * recursively. 5043 */ 5044 bmsafemap = malloc(sizeof(struct bmsafemap), 5045 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5046 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5047 ACQUIRE_LOCK(ITOUMP(ip)); 5048 if ((inodedep_lookup(mp, newinum, DEPALLOC, &inodedep))) 5049 panic("softdep_setup_inomapdep: dependency %p for new" 5050 "inode already exists", inodedep); 5051 bmsafemap = bmsafemap_lookup(mp, bp, ino_to_cg(fs, newinum), bmsafemap); 5052 if (jaddref) { 5053 LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps); 5054 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 5055 if_deps); 5056 } else { 5057 inodedep->id_state |= ONDEPLIST; 5058 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps); 5059 } 5060 inodedep->id_bmsafemap = bmsafemap; 5061 inodedep->id_state &= ~DEPCOMPLETE; 5062 FREE_LOCK(ITOUMP(ip)); 5063 } 5064 5065 /* 5066 * Called just after updating the cylinder group block to 5067 * allocate block or fragment. 5068 */ 5069 void 5070 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags) 5071 struct buf *bp; /* buffer for cylgroup block with block map */ 5072 struct mount *mp; /* filesystem doing allocation */ 5073 ufs2_daddr_t newblkno; /* number of newly allocated block */ 5074 int frags; /* Number of fragments. */ 5075 int oldfrags; /* Previous number of fragments for extend. */ 5076 { 5077 struct newblk *newblk; 5078 struct bmsafemap *bmsafemap; 5079 struct jnewblk *jnewblk; 5080 struct ufsmount *ump; 5081 struct fs *fs; 5082 5083 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5084 ("softdep_setup_blkmapdep called on non-softdep filesystem")); 5085 ump = VFSTOUFS(mp); 5086 fs = ump->um_fs; 5087 jnewblk = NULL; 5088 /* 5089 * Create a dependency for the newly allocated block. 5090 * Add it to the dependency list for the buffer holding 5091 * the cylinder group map from which it was allocated. 5092 */ 5093 if (MOUNTEDSUJ(mp)) { 5094 jnewblk = malloc(sizeof(*jnewblk), M_JNEWBLK, M_SOFTDEP_FLAGS); 5095 workitem_alloc(&jnewblk->jn_list, D_JNEWBLK, mp); 5096 jnewblk->jn_jsegdep = newjsegdep(&jnewblk->jn_list); 5097 jnewblk->jn_state = ATTACHED; 5098 jnewblk->jn_blkno = newblkno; 5099 jnewblk->jn_frags = frags; 5100 jnewblk->jn_oldfrags = oldfrags; 5101 #ifdef SUJ_DEBUG 5102 { 5103 struct cg *cgp; 5104 uint8_t *blksfree; 5105 long bno; 5106 int i; 5107 5108 cgp = (struct cg *)bp->b_data; 5109 blksfree = cg_blksfree(cgp); 5110 bno = dtogd(fs, jnewblk->jn_blkno); 5111 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; 5112 i++) { 5113 if (isset(blksfree, bno + i)) 5114 panic("softdep_setup_blkmapdep: " 5115 "free fragment %d from %d-%d " 5116 "state 0x%X dep %p", i, 5117 jnewblk->jn_oldfrags, 5118 jnewblk->jn_frags, 5119 jnewblk->jn_state, 5120 jnewblk->jn_dep); 5121 } 5122 } 5123 #endif 5124 } 5125 5126 CTR3(KTR_SUJ, 5127 "softdep_setup_blkmapdep: blkno %jd frags %d oldfrags %d", 5128 newblkno, frags, oldfrags); 5129 ACQUIRE_LOCK(ump); 5130 if (newblk_lookup(mp, newblkno, DEPALLOC, &newblk) != 0) 5131 panic("softdep_setup_blkmapdep: found block"); 5132 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp, 5133 dtog(fs, newblkno), NULL); 5134 if (jnewblk) { 5135 jnewblk->jn_dep = (struct worklist *)newblk; 5136 LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps); 5137 } else { 5138 newblk->nb_state |= ONDEPLIST; 5139 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps); 5140 } 5141 newblk->nb_bmsafemap = bmsafemap; 5142 newblk->nb_jnewblk = jnewblk; 5143 FREE_LOCK(ump); 5144 } 5145 5146 #define BMSAFEMAP_HASH(ump, cg) \ 5147 (&(ump)->bmsafemap_hashtbl[(cg) & (ump)->bmsafemap_hash_size]) 5148 5149 static int 5150 bmsafemap_find(bmsafemaphd, cg, bmsafemapp) 5151 struct bmsafemap_hashhead *bmsafemaphd; 5152 int cg; 5153 struct bmsafemap **bmsafemapp; 5154 { 5155 struct bmsafemap *bmsafemap; 5156 5157 LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash) 5158 if (bmsafemap->sm_cg == cg) 5159 break; 5160 if (bmsafemap) { 5161 *bmsafemapp = bmsafemap; 5162 return (1); 5163 } 5164 *bmsafemapp = NULL; 5165 5166 return (0); 5167 } 5168 5169 /* 5170 * Find the bmsafemap associated with a cylinder group buffer. 5171 * If none exists, create one. The buffer must be locked when 5172 * this routine is called and this routine must be called with 5173 * the softdep lock held. To avoid giving up the lock while 5174 * allocating a new bmsafemap, a preallocated bmsafemap may be 5175 * provided. If it is provided but not needed, it is freed. 5176 */ 5177 static struct bmsafemap * 5178 bmsafemap_lookup(mp, bp, cg, newbmsafemap) 5179 struct mount *mp; 5180 struct buf *bp; 5181 int cg; 5182 struct bmsafemap *newbmsafemap; 5183 { 5184 struct bmsafemap_hashhead *bmsafemaphd; 5185 struct bmsafemap *bmsafemap, *collision; 5186 struct worklist *wk; 5187 struct ufsmount *ump; 5188 5189 ump = VFSTOUFS(mp); 5190 LOCK_OWNED(ump); 5191 KASSERT(bp != NULL, ("bmsafemap_lookup: missing buffer")); 5192 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 5193 if (wk->wk_type == D_BMSAFEMAP) { 5194 if (newbmsafemap) 5195 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5196 return (WK_BMSAFEMAP(wk)); 5197 } 5198 } 5199 bmsafemaphd = BMSAFEMAP_HASH(ump, cg); 5200 if (bmsafemap_find(bmsafemaphd, cg, &bmsafemap) == 1) { 5201 if (newbmsafemap) 5202 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5203 return (bmsafemap); 5204 } 5205 if (newbmsafemap) { 5206 bmsafemap = newbmsafemap; 5207 } else { 5208 FREE_LOCK(ump); 5209 bmsafemap = malloc(sizeof(struct bmsafemap), 5210 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5211 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5212 ACQUIRE_LOCK(ump); 5213 } 5214 bmsafemap->sm_buf = bp; 5215 LIST_INIT(&bmsafemap->sm_inodedephd); 5216 LIST_INIT(&bmsafemap->sm_inodedepwr); 5217 LIST_INIT(&bmsafemap->sm_newblkhd); 5218 LIST_INIT(&bmsafemap->sm_newblkwr); 5219 LIST_INIT(&bmsafemap->sm_jaddrefhd); 5220 LIST_INIT(&bmsafemap->sm_jnewblkhd); 5221 LIST_INIT(&bmsafemap->sm_freehd); 5222 LIST_INIT(&bmsafemap->sm_freewr); 5223 if (bmsafemap_find(bmsafemaphd, cg, &collision) == 1) { 5224 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 5225 return (collision); 5226 } 5227 bmsafemap->sm_cg = cg; 5228 LIST_INSERT_HEAD(bmsafemaphd, bmsafemap, sm_hash); 5229 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 5230 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list); 5231 return (bmsafemap); 5232 } 5233 5234 /* 5235 * Direct block allocation dependencies. 5236 * 5237 * When a new block is allocated, the corresponding disk locations must be 5238 * initialized (with zeros or new data) before the on-disk inode points to 5239 * them. Also, the freemap from which the block was allocated must be 5240 * updated (on disk) before the inode's pointer. These two dependencies are 5241 * independent of each other and are needed for all file blocks and indirect 5242 * blocks that are pointed to directly by the inode. Just before the 5243 * "in-core" version of the inode is updated with a newly allocated block 5244 * number, a procedure (below) is called to setup allocation dependency 5245 * structures. These structures are removed when the corresponding 5246 * dependencies are satisfied or when the block allocation becomes obsolete 5247 * (i.e., the file is deleted, the block is de-allocated, or the block is a 5248 * fragment that gets upgraded). All of these cases are handled in 5249 * procedures described later. 5250 * 5251 * When a file extension causes a fragment to be upgraded, either to a larger 5252 * fragment or to a full block, the on-disk location may change (if the 5253 * previous fragment could not simply be extended). In this case, the old 5254 * fragment must be de-allocated, but not until after the inode's pointer has 5255 * been updated. In most cases, this is handled by later procedures, which 5256 * will construct a "freefrag" structure to be added to the workitem queue 5257 * when the inode update is complete (or obsolete). The main exception to 5258 * this is when an allocation occurs while a pending allocation dependency 5259 * (for the same block pointer) remains. This case is handled in the main 5260 * allocation dependency setup procedure by immediately freeing the 5261 * unreferenced fragments. 5262 */ 5263 void 5264 softdep_setup_allocdirect(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 5265 struct inode *ip; /* inode to which block is being added */ 5266 ufs_lbn_t off; /* block pointer within inode */ 5267 ufs2_daddr_t newblkno; /* disk block number being added */ 5268 ufs2_daddr_t oldblkno; /* previous block number, 0 unless frag */ 5269 long newsize; /* size of new block */ 5270 long oldsize; /* size of new block */ 5271 struct buf *bp; /* bp for allocated block */ 5272 { 5273 struct allocdirect *adp, *oldadp; 5274 struct allocdirectlst *adphead; 5275 struct freefrag *freefrag; 5276 struct inodedep *inodedep; 5277 struct pagedep *pagedep; 5278 struct jnewblk *jnewblk; 5279 struct newblk *newblk; 5280 struct mount *mp; 5281 ufs_lbn_t lbn; 5282 5283 lbn = bp->b_lblkno; 5284 mp = ITOVFS(ip); 5285 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5286 ("softdep_setup_allocdirect called on non-softdep filesystem")); 5287 if (oldblkno && oldblkno != newblkno) 5288 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn); 5289 else 5290 freefrag = NULL; 5291 5292 CTR6(KTR_SUJ, 5293 "softdep_setup_allocdirect: ino %d blkno %jd oldblkno %jd " 5294 "off %jd newsize %ld oldsize %d", 5295 ip->i_number, newblkno, oldblkno, off, newsize, oldsize); 5296 ACQUIRE_LOCK(ITOUMP(ip)); 5297 if (off >= UFS_NDADDR) { 5298 if (lbn > 0) 5299 panic("softdep_setup_allocdirect: bad lbn %jd, off %jd", 5300 lbn, off); 5301 /* allocating an indirect block */ 5302 if (oldblkno != 0) 5303 panic("softdep_setup_allocdirect: non-zero indir"); 5304 } else { 5305 if (off != lbn) 5306 panic("softdep_setup_allocdirect: lbn %jd != off %jd", 5307 lbn, off); 5308 /* 5309 * Allocating a direct block. 5310 * 5311 * If we are allocating a directory block, then we must 5312 * allocate an associated pagedep to track additions and 5313 * deletions. 5314 */ 5315 if ((ip->i_mode & IFMT) == IFDIR) 5316 pagedep_lookup(mp, bp, ip->i_number, off, DEPALLOC, 5317 &pagedep); 5318 } 5319 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5320 panic("softdep_setup_allocdirect: lost block"); 5321 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5322 ("softdep_setup_allocdirect: newblk already initialized")); 5323 /* 5324 * Convert the newblk to an allocdirect. 5325 */ 5326 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT); 5327 adp = (struct allocdirect *)newblk; 5328 newblk->nb_freefrag = freefrag; 5329 adp->ad_offset = off; 5330 adp->ad_oldblkno = oldblkno; 5331 adp->ad_newsize = newsize; 5332 adp->ad_oldsize = oldsize; 5333 5334 /* 5335 * Finish initializing the journal. 5336 */ 5337 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5338 jnewblk->jn_ino = ip->i_number; 5339 jnewblk->jn_lbn = lbn; 5340 add_to_journal(&jnewblk->jn_list); 5341 } 5342 if (freefrag && freefrag->ff_jdep != NULL && 5343 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5344 add_to_journal(freefrag->ff_jdep); 5345 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 5346 adp->ad_inodedep = inodedep; 5347 5348 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5349 /* 5350 * The list of allocdirects must be kept in sorted and ascending 5351 * order so that the rollback routines can quickly determine the 5352 * first uncommitted block (the size of the file stored on disk 5353 * ends at the end of the lowest committed fragment, or if there 5354 * are no fragments, at the end of the highest committed block). 5355 * Since files generally grow, the typical case is that the new 5356 * block is to be added at the end of the list. We speed this 5357 * special case by checking against the last allocdirect in the 5358 * list before laboriously traversing the list looking for the 5359 * insertion point. 5360 */ 5361 adphead = &inodedep->id_newinoupdt; 5362 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5363 if (oldadp == NULL || oldadp->ad_offset <= off) { 5364 /* insert at end of list */ 5365 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5366 if (oldadp != NULL && oldadp->ad_offset == off) 5367 allocdirect_merge(adphead, adp, oldadp); 5368 FREE_LOCK(ITOUMP(ip)); 5369 return; 5370 } 5371 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5372 if (oldadp->ad_offset >= off) 5373 break; 5374 } 5375 if (oldadp == NULL) 5376 panic("softdep_setup_allocdirect: lost entry"); 5377 /* insert in middle of list */ 5378 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5379 if (oldadp->ad_offset == off) 5380 allocdirect_merge(adphead, adp, oldadp); 5381 5382 FREE_LOCK(ITOUMP(ip)); 5383 } 5384 5385 /* 5386 * Merge a newer and older journal record to be stored either in a 5387 * newblock or freefrag. This handles aggregating journal records for 5388 * fragment allocation into a second record as well as replacing a 5389 * journal free with an aborted journal allocation. A segment for the 5390 * oldest record will be placed on wkhd if it has been written. If not 5391 * the segment for the newer record will suffice. 5392 */ 5393 static struct worklist * 5394 jnewblk_merge(new, old, wkhd) 5395 struct worklist *new; 5396 struct worklist *old; 5397 struct workhead *wkhd; 5398 { 5399 struct jnewblk *njnewblk; 5400 struct jnewblk *jnewblk; 5401 5402 /* Handle NULLs to simplify callers. */ 5403 if (new == NULL) 5404 return (old); 5405 if (old == NULL) 5406 return (new); 5407 /* Replace a jfreefrag with a jnewblk. */ 5408 if (new->wk_type == D_JFREEFRAG) { 5409 if (WK_JNEWBLK(old)->jn_blkno != WK_JFREEFRAG(new)->fr_blkno) 5410 panic("jnewblk_merge: blkno mismatch: %p, %p", 5411 old, new); 5412 cancel_jfreefrag(WK_JFREEFRAG(new)); 5413 return (old); 5414 } 5415 if (old->wk_type != D_JNEWBLK || new->wk_type != D_JNEWBLK) 5416 panic("jnewblk_merge: Bad type: old %d new %d\n", 5417 old->wk_type, new->wk_type); 5418 /* 5419 * Handle merging of two jnewblk records that describe 5420 * different sets of fragments in the same block. 5421 */ 5422 jnewblk = WK_JNEWBLK(old); 5423 njnewblk = WK_JNEWBLK(new); 5424 if (jnewblk->jn_blkno != njnewblk->jn_blkno) 5425 panic("jnewblk_merge: Merging disparate blocks."); 5426 /* 5427 * The record may be rolled back in the cg. 5428 */ 5429 if (jnewblk->jn_state & UNDONE) { 5430 jnewblk->jn_state &= ~UNDONE; 5431 njnewblk->jn_state |= UNDONE; 5432 njnewblk->jn_state &= ~ATTACHED; 5433 } 5434 /* 5435 * We modify the newer addref and free the older so that if neither 5436 * has been written the most up-to-date copy will be on disk. If 5437 * both have been written but rolled back we only temporarily need 5438 * one of them to fix the bits when the cg write completes. 5439 */ 5440 jnewblk->jn_state |= ATTACHED | COMPLETE; 5441 njnewblk->jn_oldfrags = jnewblk->jn_oldfrags; 5442 cancel_jnewblk(jnewblk, wkhd); 5443 WORKLIST_REMOVE(&jnewblk->jn_list); 5444 free_jnewblk(jnewblk); 5445 return (new); 5446 } 5447 5448 /* 5449 * Replace an old allocdirect dependency with a newer one. 5450 * This routine must be called with splbio interrupts blocked. 5451 */ 5452 static void 5453 allocdirect_merge(adphead, newadp, oldadp) 5454 struct allocdirectlst *adphead; /* head of list holding allocdirects */ 5455 struct allocdirect *newadp; /* allocdirect being added */ 5456 struct allocdirect *oldadp; /* existing allocdirect being checked */ 5457 { 5458 struct worklist *wk; 5459 struct freefrag *freefrag; 5460 5461 freefrag = NULL; 5462 LOCK_OWNED(VFSTOUFS(newadp->ad_list.wk_mp)); 5463 if (newadp->ad_oldblkno != oldadp->ad_newblkno || 5464 newadp->ad_oldsize != oldadp->ad_newsize || 5465 newadp->ad_offset >= UFS_NDADDR) 5466 panic("%s %jd != new %jd || old size %ld != new %ld", 5467 "allocdirect_merge: old blkno", 5468 (intmax_t)newadp->ad_oldblkno, 5469 (intmax_t)oldadp->ad_newblkno, 5470 newadp->ad_oldsize, oldadp->ad_newsize); 5471 newadp->ad_oldblkno = oldadp->ad_oldblkno; 5472 newadp->ad_oldsize = oldadp->ad_oldsize; 5473 /* 5474 * If the old dependency had a fragment to free or had never 5475 * previously had a block allocated, then the new dependency 5476 * can immediately post its freefrag and adopt the old freefrag. 5477 * This action is done by swapping the freefrag dependencies. 5478 * The new dependency gains the old one's freefrag, and the 5479 * old one gets the new one and then immediately puts it on 5480 * the worklist when it is freed by free_newblk. It is 5481 * not possible to do this swap when the old dependency had a 5482 * non-zero size but no previous fragment to free. This condition 5483 * arises when the new block is an extension of the old block. 5484 * Here, the first part of the fragment allocated to the new 5485 * dependency is part of the block currently claimed on disk by 5486 * the old dependency, so cannot legitimately be freed until the 5487 * conditions for the new dependency are fulfilled. 5488 */ 5489 freefrag = newadp->ad_freefrag; 5490 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) { 5491 newadp->ad_freefrag = oldadp->ad_freefrag; 5492 oldadp->ad_freefrag = freefrag; 5493 } 5494 /* 5495 * If we are tracking a new directory-block allocation, 5496 * move it from the old allocdirect to the new allocdirect. 5497 */ 5498 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) { 5499 WORKLIST_REMOVE(wk); 5500 if (!LIST_EMPTY(&oldadp->ad_newdirblk)) 5501 panic("allocdirect_merge: extra newdirblk"); 5502 WORKLIST_INSERT(&newadp->ad_newdirblk, wk); 5503 } 5504 TAILQ_REMOVE(adphead, oldadp, ad_next); 5505 /* 5506 * We need to move any journal dependencies over to the freefrag 5507 * that releases this block if it exists. Otherwise we are 5508 * extending an existing block and we'll wait until that is 5509 * complete to release the journal space and extend the 5510 * new journal to cover this old space as well. 5511 */ 5512 if (freefrag == NULL) { 5513 if (oldadp->ad_newblkno != newadp->ad_newblkno) 5514 panic("allocdirect_merge: %jd != %jd", 5515 oldadp->ad_newblkno, newadp->ad_newblkno); 5516 newadp->ad_block.nb_jnewblk = (struct jnewblk *) 5517 jnewblk_merge(&newadp->ad_block.nb_jnewblk->jn_list, 5518 &oldadp->ad_block.nb_jnewblk->jn_list, 5519 &newadp->ad_block.nb_jwork); 5520 oldadp->ad_block.nb_jnewblk = NULL; 5521 cancel_newblk(&oldadp->ad_block, NULL, 5522 &newadp->ad_block.nb_jwork); 5523 } else { 5524 wk = (struct worklist *) cancel_newblk(&oldadp->ad_block, 5525 &freefrag->ff_list, &freefrag->ff_jwork); 5526 freefrag->ff_jdep = jnewblk_merge(freefrag->ff_jdep, wk, 5527 &freefrag->ff_jwork); 5528 } 5529 free_newblk(&oldadp->ad_block); 5530 } 5531 5532 /* 5533 * Allocate a jfreefrag structure to journal a single block free. 5534 */ 5535 static struct jfreefrag * 5536 newjfreefrag(freefrag, ip, blkno, size, lbn) 5537 struct freefrag *freefrag; 5538 struct inode *ip; 5539 ufs2_daddr_t blkno; 5540 long size; 5541 ufs_lbn_t lbn; 5542 { 5543 struct jfreefrag *jfreefrag; 5544 struct fs *fs; 5545 5546 fs = ITOFS(ip); 5547 jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG, 5548 M_SOFTDEP_FLAGS); 5549 workitem_alloc(&jfreefrag->fr_list, D_JFREEFRAG, ITOVFS(ip)); 5550 jfreefrag->fr_jsegdep = newjsegdep(&jfreefrag->fr_list); 5551 jfreefrag->fr_state = ATTACHED | DEPCOMPLETE; 5552 jfreefrag->fr_ino = ip->i_number; 5553 jfreefrag->fr_lbn = lbn; 5554 jfreefrag->fr_blkno = blkno; 5555 jfreefrag->fr_frags = numfrags(fs, size); 5556 jfreefrag->fr_freefrag = freefrag; 5557 5558 return (jfreefrag); 5559 } 5560 5561 /* 5562 * Allocate a new freefrag structure. 5563 */ 5564 static struct freefrag * 5565 newfreefrag(ip, blkno, size, lbn) 5566 struct inode *ip; 5567 ufs2_daddr_t blkno; 5568 long size; 5569 ufs_lbn_t lbn; 5570 { 5571 struct freefrag *freefrag; 5572 struct ufsmount *ump; 5573 struct fs *fs; 5574 5575 CTR4(KTR_SUJ, "newfreefrag: ino %d blkno %jd size %ld lbn %jd", 5576 ip->i_number, blkno, size, lbn); 5577 ump = ITOUMP(ip); 5578 fs = ump->um_fs; 5579 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag) 5580 panic("newfreefrag: frag size"); 5581 freefrag = malloc(sizeof(struct freefrag), 5582 M_FREEFRAG, M_SOFTDEP_FLAGS); 5583 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ump)); 5584 freefrag->ff_state = ATTACHED; 5585 LIST_INIT(&freefrag->ff_jwork); 5586 freefrag->ff_inum = ip->i_number; 5587 freefrag->ff_vtype = ITOV(ip)->v_type; 5588 freefrag->ff_blkno = blkno; 5589 freefrag->ff_fragsize = size; 5590 5591 if (MOUNTEDSUJ(UFSTOVFS(ump))) { 5592 freefrag->ff_jdep = (struct worklist *) 5593 newjfreefrag(freefrag, ip, blkno, size, lbn); 5594 } else { 5595 freefrag->ff_state |= DEPCOMPLETE; 5596 freefrag->ff_jdep = NULL; 5597 } 5598 5599 return (freefrag); 5600 } 5601 5602 /* 5603 * This workitem de-allocates fragments that were replaced during 5604 * file block allocation. 5605 */ 5606 static void 5607 handle_workitem_freefrag(freefrag) 5608 struct freefrag *freefrag; 5609 { 5610 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp); 5611 struct workhead wkhd; 5612 5613 CTR3(KTR_SUJ, 5614 "handle_workitem_freefrag: ino %d blkno %jd size %ld", 5615 freefrag->ff_inum, freefrag->ff_blkno, freefrag->ff_fragsize); 5616 /* 5617 * It would be illegal to add new completion items to the 5618 * freefrag after it was schedule to be done so it must be 5619 * safe to modify the list head here. 5620 */ 5621 LIST_INIT(&wkhd); 5622 ACQUIRE_LOCK(ump); 5623 LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list); 5624 /* 5625 * If the journal has not been written we must cancel it here. 5626 */ 5627 if (freefrag->ff_jdep) { 5628 if (freefrag->ff_jdep->wk_type != D_JNEWBLK) 5629 panic("handle_workitem_freefrag: Unexpected type %d\n", 5630 freefrag->ff_jdep->wk_type); 5631 cancel_jnewblk(WK_JNEWBLK(freefrag->ff_jdep), &wkhd); 5632 } 5633 FREE_LOCK(ump); 5634 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno, 5635 freefrag->ff_fragsize, freefrag->ff_inum, freefrag->ff_vtype, &wkhd); 5636 ACQUIRE_LOCK(ump); 5637 WORKITEM_FREE(freefrag, D_FREEFRAG); 5638 FREE_LOCK(ump); 5639 } 5640 5641 /* 5642 * Set up a dependency structure for an external attributes data block. 5643 * This routine follows much of the structure of softdep_setup_allocdirect. 5644 * See the description of softdep_setup_allocdirect above for details. 5645 */ 5646 void 5647 softdep_setup_allocext(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 5648 struct inode *ip; 5649 ufs_lbn_t off; 5650 ufs2_daddr_t newblkno; 5651 ufs2_daddr_t oldblkno; 5652 long newsize; 5653 long oldsize; 5654 struct buf *bp; 5655 { 5656 struct allocdirect *adp, *oldadp; 5657 struct allocdirectlst *adphead; 5658 struct freefrag *freefrag; 5659 struct inodedep *inodedep; 5660 struct jnewblk *jnewblk; 5661 struct newblk *newblk; 5662 struct mount *mp; 5663 struct ufsmount *ump; 5664 ufs_lbn_t lbn; 5665 5666 mp = ITOVFS(ip); 5667 ump = VFSTOUFS(mp); 5668 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5669 ("softdep_setup_allocext called on non-softdep filesystem")); 5670 KASSERT(off < UFS_NXADDR, 5671 ("softdep_setup_allocext: lbn %lld > UFS_NXADDR", (long long)off)); 5672 5673 lbn = bp->b_lblkno; 5674 if (oldblkno && oldblkno != newblkno) 5675 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn); 5676 else 5677 freefrag = NULL; 5678 5679 ACQUIRE_LOCK(ump); 5680 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5681 panic("softdep_setup_allocext: lost block"); 5682 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5683 ("softdep_setup_allocext: newblk already initialized")); 5684 /* 5685 * Convert the newblk to an allocdirect. 5686 */ 5687 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT); 5688 adp = (struct allocdirect *)newblk; 5689 newblk->nb_freefrag = freefrag; 5690 adp->ad_offset = off; 5691 adp->ad_oldblkno = oldblkno; 5692 adp->ad_newsize = newsize; 5693 adp->ad_oldsize = oldsize; 5694 adp->ad_state |= EXTDATA; 5695 5696 /* 5697 * Finish initializing the journal. 5698 */ 5699 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5700 jnewblk->jn_ino = ip->i_number; 5701 jnewblk->jn_lbn = lbn; 5702 add_to_journal(&jnewblk->jn_list); 5703 } 5704 if (freefrag && freefrag->ff_jdep != NULL && 5705 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5706 add_to_journal(freefrag->ff_jdep); 5707 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 5708 adp->ad_inodedep = inodedep; 5709 5710 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5711 /* 5712 * The list of allocdirects must be kept in sorted and ascending 5713 * order so that the rollback routines can quickly determine the 5714 * first uncommitted block (the size of the file stored on disk 5715 * ends at the end of the lowest committed fragment, or if there 5716 * are no fragments, at the end of the highest committed block). 5717 * Since files generally grow, the typical case is that the new 5718 * block is to be added at the end of the list. We speed this 5719 * special case by checking against the last allocdirect in the 5720 * list before laboriously traversing the list looking for the 5721 * insertion point. 5722 */ 5723 adphead = &inodedep->id_newextupdt; 5724 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5725 if (oldadp == NULL || oldadp->ad_offset <= off) { 5726 /* insert at end of list */ 5727 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5728 if (oldadp != NULL && oldadp->ad_offset == off) 5729 allocdirect_merge(adphead, adp, oldadp); 5730 FREE_LOCK(ump); 5731 return; 5732 } 5733 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5734 if (oldadp->ad_offset >= off) 5735 break; 5736 } 5737 if (oldadp == NULL) 5738 panic("softdep_setup_allocext: lost entry"); 5739 /* insert in middle of list */ 5740 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5741 if (oldadp->ad_offset == off) 5742 allocdirect_merge(adphead, adp, oldadp); 5743 FREE_LOCK(ump); 5744 } 5745 5746 /* 5747 * Indirect block allocation dependencies. 5748 * 5749 * The same dependencies that exist for a direct block also exist when 5750 * a new block is allocated and pointed to by an entry in a block of 5751 * indirect pointers. The undo/redo states described above are also 5752 * used here. Because an indirect block contains many pointers that 5753 * may have dependencies, a second copy of the entire in-memory indirect 5754 * block is kept. The buffer cache copy is always completely up-to-date. 5755 * The second copy, which is used only as a source for disk writes, 5756 * contains only the safe pointers (i.e., those that have no remaining 5757 * update dependencies). The second copy is freed when all pointers 5758 * are safe. The cache is not allowed to replace indirect blocks with 5759 * pending update dependencies. If a buffer containing an indirect 5760 * block with dependencies is written, these routines will mark it 5761 * dirty again. It can only be successfully written once all the 5762 * dependencies are removed. The ffs_fsync routine in conjunction with 5763 * softdep_sync_metadata work together to get all the dependencies 5764 * removed so that a file can be successfully written to disk. Three 5765 * procedures are used when setting up indirect block pointer 5766 * dependencies. The division is necessary because of the organization 5767 * of the "balloc" routine and because of the distinction between file 5768 * pages and file metadata blocks. 5769 */ 5770 5771 /* 5772 * Allocate a new allocindir structure. 5773 */ 5774 static struct allocindir * 5775 newallocindir(ip, ptrno, newblkno, oldblkno, lbn) 5776 struct inode *ip; /* inode for file being extended */ 5777 int ptrno; /* offset of pointer in indirect block */ 5778 ufs2_daddr_t newblkno; /* disk block number being added */ 5779 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 5780 ufs_lbn_t lbn; 5781 { 5782 struct newblk *newblk; 5783 struct allocindir *aip; 5784 struct freefrag *freefrag; 5785 struct jnewblk *jnewblk; 5786 5787 if (oldblkno) 5788 freefrag = newfreefrag(ip, oldblkno, ITOFS(ip)->fs_bsize, lbn); 5789 else 5790 freefrag = NULL; 5791 ACQUIRE_LOCK(ITOUMP(ip)); 5792 if (newblk_lookup(ITOVFS(ip), newblkno, 0, &newblk) == 0) 5793 panic("new_allocindir: lost block"); 5794 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5795 ("newallocindir: newblk already initialized")); 5796 WORKITEM_REASSIGN(newblk, D_ALLOCINDIR); 5797 newblk->nb_freefrag = freefrag; 5798 aip = (struct allocindir *)newblk; 5799 aip->ai_offset = ptrno; 5800 aip->ai_oldblkno = oldblkno; 5801 aip->ai_lbn = lbn; 5802 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5803 jnewblk->jn_ino = ip->i_number; 5804 jnewblk->jn_lbn = lbn; 5805 add_to_journal(&jnewblk->jn_list); 5806 } 5807 if (freefrag && freefrag->ff_jdep != NULL && 5808 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5809 add_to_journal(freefrag->ff_jdep); 5810 return (aip); 5811 } 5812 5813 /* 5814 * Called just before setting an indirect block pointer 5815 * to a newly allocated file page. 5816 */ 5817 void 5818 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 5819 struct inode *ip; /* inode for file being extended */ 5820 ufs_lbn_t lbn; /* allocated block number within file */ 5821 struct buf *bp; /* buffer with indirect blk referencing page */ 5822 int ptrno; /* offset of pointer in indirect block */ 5823 ufs2_daddr_t newblkno; /* disk block number being added */ 5824 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 5825 struct buf *nbp; /* buffer holding allocated page */ 5826 { 5827 struct inodedep *inodedep; 5828 struct freefrag *freefrag; 5829 struct allocindir *aip; 5830 struct pagedep *pagedep; 5831 struct mount *mp; 5832 struct ufsmount *ump; 5833 5834 mp = ITOVFS(ip); 5835 ump = VFSTOUFS(mp); 5836 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5837 ("softdep_setup_allocindir_page called on non-softdep filesystem")); 5838 KASSERT(lbn == nbp->b_lblkno, 5839 ("softdep_setup_allocindir_page: lbn %jd != lblkno %jd", 5840 lbn, bp->b_lblkno)); 5841 CTR4(KTR_SUJ, 5842 "softdep_setup_allocindir_page: ino %d blkno %jd oldblkno %jd " 5843 "lbn %jd", ip->i_number, newblkno, oldblkno, lbn); 5844 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page"); 5845 aip = newallocindir(ip, ptrno, newblkno, oldblkno, lbn); 5846 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 5847 /* 5848 * If we are allocating a directory page, then we must 5849 * allocate an associated pagedep to track additions and 5850 * deletions. 5851 */ 5852 if ((ip->i_mode & IFMT) == IFDIR) 5853 pagedep_lookup(mp, nbp, ip->i_number, lbn, DEPALLOC, &pagedep); 5854 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 5855 freefrag = setup_allocindir_phase2(bp, ip, inodedep, aip, lbn); 5856 FREE_LOCK(ump); 5857 if (freefrag) 5858 handle_workitem_freefrag(freefrag); 5859 } 5860 5861 /* 5862 * Called just before setting an indirect block pointer to a 5863 * newly allocated indirect block. 5864 */ 5865 void 5866 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 5867 struct buf *nbp; /* newly allocated indirect block */ 5868 struct inode *ip; /* inode for file being extended */ 5869 struct buf *bp; /* indirect block referencing allocated block */ 5870 int ptrno; /* offset of pointer in indirect block */ 5871 ufs2_daddr_t newblkno; /* disk block number being added */ 5872 { 5873 struct inodedep *inodedep; 5874 struct allocindir *aip; 5875 struct ufsmount *ump; 5876 ufs_lbn_t lbn; 5877 5878 ump = ITOUMP(ip); 5879 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 5880 ("softdep_setup_allocindir_meta called on non-softdep filesystem")); 5881 CTR3(KTR_SUJ, 5882 "softdep_setup_allocindir_meta: ino %d blkno %jd ptrno %d", 5883 ip->i_number, newblkno, ptrno); 5884 lbn = nbp->b_lblkno; 5885 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta"); 5886 aip = newallocindir(ip, ptrno, newblkno, 0, lbn); 5887 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep); 5888 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 5889 if (setup_allocindir_phase2(bp, ip, inodedep, aip, lbn)) 5890 panic("softdep_setup_allocindir_meta: Block already existed"); 5891 FREE_LOCK(ump); 5892 } 5893 5894 static void 5895 indirdep_complete(indirdep) 5896 struct indirdep *indirdep; 5897 { 5898 struct allocindir *aip; 5899 5900 LIST_REMOVE(indirdep, ir_next); 5901 indirdep->ir_state |= DEPCOMPLETE; 5902 5903 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) { 5904 LIST_REMOVE(aip, ai_next); 5905 free_newblk(&aip->ai_block); 5906 } 5907 /* 5908 * If this indirdep is not attached to a buf it was simply waiting 5909 * on completion to clear completehd. free_indirdep() asserts 5910 * that nothing is dangling. 5911 */ 5912 if ((indirdep->ir_state & ONWORKLIST) == 0) 5913 free_indirdep(indirdep); 5914 } 5915 5916 static struct indirdep * 5917 indirdep_lookup(mp, ip, bp) 5918 struct mount *mp; 5919 struct inode *ip; 5920 struct buf *bp; 5921 { 5922 struct indirdep *indirdep, *newindirdep; 5923 struct newblk *newblk; 5924 struct ufsmount *ump; 5925 struct worklist *wk; 5926 struct fs *fs; 5927 ufs2_daddr_t blkno; 5928 5929 ump = VFSTOUFS(mp); 5930 LOCK_OWNED(ump); 5931 indirdep = NULL; 5932 newindirdep = NULL; 5933 fs = ump->um_fs; 5934 for (;;) { 5935 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 5936 if (wk->wk_type != D_INDIRDEP) 5937 continue; 5938 indirdep = WK_INDIRDEP(wk); 5939 break; 5940 } 5941 /* Found on the buffer worklist, no new structure to free. */ 5942 if (indirdep != NULL && newindirdep == NULL) 5943 return (indirdep); 5944 if (indirdep != NULL && newindirdep != NULL) 5945 panic("indirdep_lookup: simultaneous create"); 5946 /* None found on the buffer and a new structure is ready. */ 5947 if (indirdep == NULL && newindirdep != NULL) 5948 break; 5949 /* None found and no new structure available. */ 5950 FREE_LOCK(ump); 5951 newindirdep = malloc(sizeof(struct indirdep), 5952 M_INDIRDEP, M_SOFTDEP_FLAGS); 5953 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, mp); 5954 newindirdep->ir_state = ATTACHED; 5955 if (I_IS_UFS1(ip)) 5956 newindirdep->ir_state |= UFS1FMT; 5957 TAILQ_INIT(&newindirdep->ir_trunc); 5958 newindirdep->ir_saveddata = NULL; 5959 LIST_INIT(&newindirdep->ir_deplisthd); 5960 LIST_INIT(&newindirdep->ir_donehd); 5961 LIST_INIT(&newindirdep->ir_writehd); 5962 LIST_INIT(&newindirdep->ir_completehd); 5963 if (bp->b_blkno == bp->b_lblkno) { 5964 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp, 5965 NULL, NULL); 5966 bp->b_blkno = blkno; 5967 } 5968 newindirdep->ir_freeblks = NULL; 5969 newindirdep->ir_savebp = 5970 getblk(ump->um_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0); 5971 newindirdep->ir_bp = bp; 5972 BUF_KERNPROC(newindirdep->ir_savebp); 5973 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount); 5974 ACQUIRE_LOCK(ump); 5975 } 5976 indirdep = newindirdep; 5977 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list); 5978 /* 5979 * If the block is not yet allocated we don't set DEPCOMPLETE so 5980 * that we don't free dependencies until the pointers are valid. 5981 * This could search b_dep for D_ALLOCDIRECT/D_ALLOCINDIR rather 5982 * than using the hash. 5983 */ 5984 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk)) 5985 LIST_INSERT_HEAD(&newblk->nb_indirdeps, indirdep, ir_next); 5986 else 5987 indirdep->ir_state |= DEPCOMPLETE; 5988 return (indirdep); 5989 } 5990 5991 /* 5992 * Called to finish the allocation of the "aip" allocated 5993 * by one of the two routines above. 5994 */ 5995 static struct freefrag * 5996 setup_allocindir_phase2(bp, ip, inodedep, aip, lbn) 5997 struct buf *bp; /* in-memory copy of the indirect block */ 5998 struct inode *ip; /* inode for file being extended */ 5999 struct inodedep *inodedep; /* Inodedep for ip */ 6000 struct allocindir *aip; /* allocindir allocated by the above routines */ 6001 ufs_lbn_t lbn; /* Logical block number for this block. */ 6002 { 6003 struct fs *fs; 6004 struct indirdep *indirdep; 6005 struct allocindir *oldaip; 6006 struct freefrag *freefrag; 6007 struct mount *mp; 6008 struct ufsmount *ump; 6009 6010 mp = ITOVFS(ip); 6011 ump = VFSTOUFS(mp); 6012 LOCK_OWNED(ump); 6013 fs = ump->um_fs; 6014 if (bp->b_lblkno >= 0) 6015 panic("setup_allocindir_phase2: not indir blk"); 6016 KASSERT(aip->ai_offset >= 0 && aip->ai_offset < NINDIR(fs), 6017 ("setup_allocindir_phase2: Bad offset %d", aip->ai_offset)); 6018 indirdep = indirdep_lookup(mp, ip, bp); 6019 KASSERT(indirdep->ir_savebp != NULL, 6020 ("setup_allocindir_phase2 NULL ir_savebp")); 6021 aip->ai_indirdep = indirdep; 6022 /* 6023 * Check for an unwritten dependency for this indirect offset. If 6024 * there is, merge the old dependency into the new one. This happens 6025 * as a result of reallocblk only. 6026 */ 6027 freefrag = NULL; 6028 if (aip->ai_oldblkno != 0) { 6029 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) { 6030 if (oldaip->ai_offset == aip->ai_offset) { 6031 freefrag = allocindir_merge(aip, oldaip); 6032 goto done; 6033 } 6034 } 6035 LIST_FOREACH(oldaip, &indirdep->ir_donehd, ai_next) { 6036 if (oldaip->ai_offset == aip->ai_offset) { 6037 freefrag = allocindir_merge(aip, oldaip); 6038 goto done; 6039 } 6040 } 6041 } 6042 done: 6043 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next); 6044 return (freefrag); 6045 } 6046 6047 /* 6048 * Merge two allocindirs which refer to the same block. Move newblock 6049 * dependencies and setup the freefrags appropriately. 6050 */ 6051 static struct freefrag * 6052 allocindir_merge(aip, oldaip) 6053 struct allocindir *aip; 6054 struct allocindir *oldaip; 6055 { 6056 struct freefrag *freefrag; 6057 struct worklist *wk; 6058 6059 if (oldaip->ai_newblkno != aip->ai_oldblkno) 6060 panic("allocindir_merge: blkno"); 6061 aip->ai_oldblkno = oldaip->ai_oldblkno; 6062 freefrag = aip->ai_freefrag; 6063 aip->ai_freefrag = oldaip->ai_freefrag; 6064 oldaip->ai_freefrag = NULL; 6065 KASSERT(freefrag != NULL, ("setup_allocindir_phase2: No freefrag")); 6066 /* 6067 * If we are tracking a new directory-block allocation, 6068 * move it from the old allocindir to the new allocindir. 6069 */ 6070 if ((wk = LIST_FIRST(&oldaip->ai_newdirblk)) != NULL) { 6071 WORKLIST_REMOVE(wk); 6072 if (!LIST_EMPTY(&oldaip->ai_newdirblk)) 6073 panic("allocindir_merge: extra newdirblk"); 6074 WORKLIST_INSERT(&aip->ai_newdirblk, wk); 6075 } 6076 /* 6077 * We can skip journaling for this freefrag and just complete 6078 * any pending journal work for the allocindir that is being 6079 * removed after the freefrag completes. 6080 */ 6081 if (freefrag->ff_jdep) 6082 cancel_jfreefrag(WK_JFREEFRAG(freefrag->ff_jdep)); 6083 LIST_REMOVE(oldaip, ai_next); 6084 freefrag->ff_jdep = (struct worklist *)cancel_newblk(&oldaip->ai_block, 6085 &freefrag->ff_list, &freefrag->ff_jwork); 6086 free_newblk(&oldaip->ai_block); 6087 6088 return (freefrag); 6089 } 6090 6091 static inline void 6092 setup_freedirect(freeblks, ip, i, needj) 6093 struct freeblks *freeblks; 6094 struct inode *ip; 6095 int i; 6096 int needj; 6097 { 6098 struct ufsmount *ump; 6099 ufs2_daddr_t blkno; 6100 int frags; 6101 6102 blkno = DIP(ip, i_db[i]); 6103 if (blkno == 0) 6104 return; 6105 DIP_SET(ip, i_db[i], 0); 6106 ump = ITOUMP(ip); 6107 frags = sblksize(ump->um_fs, ip->i_size, i); 6108 frags = numfrags(ump->um_fs, frags); 6109 newfreework(ump, freeblks, NULL, i, blkno, frags, 0, needj); 6110 } 6111 6112 static inline void 6113 setup_freeext(freeblks, ip, i, needj) 6114 struct freeblks *freeblks; 6115 struct inode *ip; 6116 int i; 6117 int needj; 6118 { 6119 struct ufsmount *ump; 6120 ufs2_daddr_t blkno; 6121 int frags; 6122 6123 blkno = ip->i_din2->di_extb[i]; 6124 if (blkno == 0) 6125 return; 6126 ip->i_din2->di_extb[i] = 0; 6127 ump = ITOUMP(ip); 6128 frags = sblksize(ump->um_fs, ip->i_din2->di_extsize, i); 6129 frags = numfrags(ump->um_fs, frags); 6130 newfreework(ump, freeblks, NULL, -1 - i, blkno, frags, 0, needj); 6131 } 6132 6133 static inline void 6134 setup_freeindir(freeblks, ip, i, lbn, needj) 6135 struct freeblks *freeblks; 6136 struct inode *ip; 6137 int i; 6138 ufs_lbn_t lbn; 6139 int needj; 6140 { 6141 struct ufsmount *ump; 6142 ufs2_daddr_t blkno; 6143 6144 blkno = DIP(ip, i_ib[i]); 6145 if (blkno == 0) 6146 return; 6147 DIP_SET(ip, i_ib[i], 0); 6148 ump = ITOUMP(ip); 6149 newfreework(ump, freeblks, NULL, lbn, blkno, ump->um_fs->fs_frag, 6150 0, needj); 6151 } 6152 6153 static inline struct freeblks * 6154 newfreeblks(mp, ip) 6155 struct mount *mp; 6156 struct inode *ip; 6157 { 6158 struct freeblks *freeblks; 6159 6160 freeblks = malloc(sizeof(struct freeblks), 6161 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO); 6162 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp); 6163 LIST_INIT(&freeblks->fb_jblkdephd); 6164 LIST_INIT(&freeblks->fb_jwork); 6165 freeblks->fb_ref = 0; 6166 freeblks->fb_cgwait = 0; 6167 freeblks->fb_state = ATTACHED; 6168 freeblks->fb_uid = ip->i_uid; 6169 freeblks->fb_inum = ip->i_number; 6170 freeblks->fb_vtype = ITOV(ip)->v_type; 6171 freeblks->fb_modrev = DIP(ip, i_modrev); 6172 freeblks->fb_devvp = ITODEVVP(ip); 6173 freeblks->fb_chkcnt = 0; 6174 freeblks->fb_len = 0; 6175 6176 return (freeblks); 6177 } 6178 6179 static void 6180 trunc_indirdep(indirdep, freeblks, bp, off) 6181 struct indirdep *indirdep; 6182 struct freeblks *freeblks; 6183 struct buf *bp; 6184 int off; 6185 { 6186 struct allocindir *aip, *aipn; 6187 6188 /* 6189 * The first set of allocindirs won't be in savedbp. 6190 */ 6191 LIST_FOREACH_SAFE(aip, &indirdep->ir_deplisthd, ai_next, aipn) 6192 if (aip->ai_offset > off) 6193 cancel_allocindir(aip, bp, freeblks, 1); 6194 LIST_FOREACH_SAFE(aip, &indirdep->ir_donehd, ai_next, aipn) 6195 if (aip->ai_offset > off) 6196 cancel_allocindir(aip, bp, freeblks, 1); 6197 /* 6198 * These will exist in savedbp. 6199 */ 6200 LIST_FOREACH_SAFE(aip, &indirdep->ir_writehd, ai_next, aipn) 6201 if (aip->ai_offset > off) 6202 cancel_allocindir(aip, NULL, freeblks, 0); 6203 LIST_FOREACH_SAFE(aip, &indirdep->ir_completehd, ai_next, aipn) 6204 if (aip->ai_offset > off) 6205 cancel_allocindir(aip, NULL, freeblks, 0); 6206 } 6207 6208 /* 6209 * Follow the chain of indirects down to lastlbn creating a freework 6210 * structure for each. This will be used to start indir_trunc() at 6211 * the right offset and create the journal records for the parrtial 6212 * truncation. A second step will handle the truncated dependencies. 6213 */ 6214 static int 6215 setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno) 6216 struct freeblks *freeblks; 6217 struct inode *ip; 6218 ufs_lbn_t lbn; 6219 ufs_lbn_t lastlbn; 6220 ufs2_daddr_t blkno; 6221 { 6222 struct indirdep *indirdep; 6223 struct indirdep *indirn; 6224 struct freework *freework; 6225 struct newblk *newblk; 6226 struct mount *mp; 6227 struct ufsmount *ump; 6228 struct buf *bp; 6229 uint8_t *start; 6230 uint8_t *end; 6231 ufs_lbn_t lbnadd; 6232 int level; 6233 int error; 6234 int off; 6235 6236 6237 freework = NULL; 6238 if (blkno == 0) 6239 return (0); 6240 mp = freeblks->fb_list.wk_mp; 6241 ump = VFSTOUFS(mp); 6242 bp = getblk(ITOV(ip), lbn, mp->mnt_stat.f_iosize, 0, 0, 0); 6243 if ((bp->b_flags & B_CACHE) == 0) { 6244 bp->b_blkno = blkptrtodb(VFSTOUFS(mp), blkno); 6245 bp->b_iocmd = BIO_READ; 6246 bp->b_flags &= ~B_INVAL; 6247 bp->b_ioflags &= ~BIO_ERROR; 6248 vfs_busy_pages(bp, 0); 6249 bp->b_iooffset = dbtob(bp->b_blkno); 6250 bstrategy(bp); 6251 #ifdef RACCT 6252 if (racct_enable) { 6253 PROC_LOCK(curproc); 6254 racct_add_buf(curproc, bp, 0); 6255 PROC_UNLOCK(curproc); 6256 } 6257 #endif /* RACCT */ 6258 curthread->td_ru.ru_inblock++; 6259 error = bufwait(bp); 6260 if (error) { 6261 brelse(bp); 6262 return (error); 6263 } 6264 } 6265 level = lbn_level(lbn); 6266 lbnadd = lbn_offset(ump->um_fs, level); 6267 /* 6268 * Compute the offset of the last block we want to keep. Store 6269 * in the freework the first block we want to completely free. 6270 */ 6271 off = (lastlbn - -(lbn + level)) / lbnadd; 6272 if (off + 1 == NINDIR(ump->um_fs)) 6273 goto nowork; 6274 freework = newfreework(ump, freeblks, NULL, lbn, blkno, 0, off + 1, 0); 6275 /* 6276 * Link the freework into the indirdep. This will prevent any new 6277 * allocations from proceeding until we are finished with the 6278 * truncate and the block is written. 6279 */ 6280 ACQUIRE_LOCK(ump); 6281 indirdep = indirdep_lookup(mp, ip, bp); 6282 if (indirdep->ir_freeblks) 6283 panic("setup_trunc_indir: indirdep already truncated."); 6284 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, freework, fw_next); 6285 freework->fw_indir = indirdep; 6286 /* 6287 * Cancel any allocindirs that will not make it to disk. 6288 * We have to do this for all copies of the indirdep that 6289 * live on this newblk. 6290 */ 6291 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 6292 newblk_lookup(mp, dbtofsb(ump->um_fs, bp->b_blkno), 0, &newblk); 6293 LIST_FOREACH(indirn, &newblk->nb_indirdeps, ir_next) 6294 trunc_indirdep(indirn, freeblks, bp, off); 6295 } else 6296 trunc_indirdep(indirdep, freeblks, bp, off); 6297 FREE_LOCK(ump); 6298 /* 6299 * Creation is protected by the buf lock. The saveddata is only 6300 * needed if a full truncation follows a partial truncation but it 6301 * is difficult to allocate in that case so we fetch it anyway. 6302 */ 6303 if (indirdep->ir_saveddata == NULL) 6304 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 6305 M_SOFTDEP_FLAGS); 6306 nowork: 6307 /* Fetch the blkno of the child and the zero start offset. */ 6308 if (I_IS_UFS1(ip)) { 6309 blkno = ((ufs1_daddr_t *)bp->b_data)[off]; 6310 start = (uint8_t *)&((ufs1_daddr_t *)bp->b_data)[off+1]; 6311 } else { 6312 blkno = ((ufs2_daddr_t *)bp->b_data)[off]; 6313 start = (uint8_t *)&((ufs2_daddr_t *)bp->b_data)[off+1]; 6314 } 6315 if (freework) { 6316 /* Zero the truncated pointers. */ 6317 end = bp->b_data + bp->b_bcount; 6318 bzero(start, end - start); 6319 bdwrite(bp); 6320 } else 6321 bqrelse(bp); 6322 if (level == 0) 6323 return (0); 6324 lbn++; /* adjust level */ 6325 lbn -= (off * lbnadd); 6326 return setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno); 6327 } 6328 6329 /* 6330 * Complete the partial truncation of an indirect block setup by 6331 * setup_trunc_indir(). This zeros the truncated pointers in the saved 6332 * copy and writes them to disk before the freeblks is allowed to complete. 6333 */ 6334 static void 6335 complete_trunc_indir(freework) 6336 struct freework *freework; 6337 { 6338 struct freework *fwn; 6339 struct indirdep *indirdep; 6340 struct ufsmount *ump; 6341 struct buf *bp; 6342 uintptr_t start; 6343 int count; 6344 6345 ump = VFSTOUFS(freework->fw_list.wk_mp); 6346 LOCK_OWNED(ump); 6347 indirdep = freework->fw_indir; 6348 for (;;) { 6349 bp = indirdep->ir_bp; 6350 /* See if the block was discarded. */ 6351 if (bp == NULL) 6352 break; 6353 /* Inline part of getdirtybuf(). We dont want bremfree. */ 6354 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) 6355 break; 6356 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 6357 LOCK_PTR(ump)) == 0) 6358 BUF_UNLOCK(bp); 6359 ACQUIRE_LOCK(ump); 6360 } 6361 freework->fw_state |= DEPCOMPLETE; 6362 TAILQ_REMOVE(&indirdep->ir_trunc, freework, fw_next); 6363 /* 6364 * Zero the pointers in the saved copy. 6365 */ 6366 if (indirdep->ir_state & UFS1FMT) 6367 start = sizeof(ufs1_daddr_t); 6368 else 6369 start = sizeof(ufs2_daddr_t); 6370 start *= freework->fw_start; 6371 count = indirdep->ir_savebp->b_bcount - start; 6372 start += (uintptr_t)indirdep->ir_savebp->b_data; 6373 bzero((char *)start, count); 6374 /* 6375 * We need to start the next truncation in the list if it has not 6376 * been started yet. 6377 */ 6378 fwn = TAILQ_FIRST(&indirdep->ir_trunc); 6379 if (fwn != NULL) { 6380 if (fwn->fw_freeblks == indirdep->ir_freeblks) 6381 TAILQ_REMOVE(&indirdep->ir_trunc, fwn, fw_next); 6382 if ((fwn->fw_state & ONWORKLIST) == 0) 6383 freework_enqueue(fwn); 6384 } 6385 /* 6386 * If bp is NULL the block was fully truncated, restore 6387 * the saved block list otherwise free it if it is no 6388 * longer needed. 6389 */ 6390 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 6391 if (bp == NULL) 6392 bcopy(indirdep->ir_saveddata, 6393 indirdep->ir_savebp->b_data, 6394 indirdep->ir_savebp->b_bcount); 6395 free(indirdep->ir_saveddata, M_INDIRDEP); 6396 indirdep->ir_saveddata = NULL; 6397 } 6398 /* 6399 * When bp is NULL there is a full truncation pending. We 6400 * must wait for this full truncation to be journaled before 6401 * we can release this freework because the disk pointers will 6402 * never be written as zero. 6403 */ 6404 if (bp == NULL) { 6405 if (LIST_EMPTY(&indirdep->ir_freeblks->fb_jblkdephd)) 6406 handle_written_freework(freework); 6407 else 6408 WORKLIST_INSERT(&indirdep->ir_freeblks->fb_freeworkhd, 6409 &freework->fw_list); 6410 } else { 6411 /* Complete when the real copy is written. */ 6412 WORKLIST_INSERT(&bp->b_dep, &freework->fw_list); 6413 BUF_UNLOCK(bp); 6414 } 6415 } 6416 6417 /* 6418 * Calculate the number of blocks we are going to release where datablocks 6419 * is the current total and length is the new file size. 6420 */ 6421 static ufs2_daddr_t 6422 blkcount(fs, datablocks, length) 6423 struct fs *fs; 6424 ufs2_daddr_t datablocks; 6425 off_t length; 6426 { 6427 off_t totblks, numblks; 6428 6429 totblks = 0; 6430 numblks = howmany(length, fs->fs_bsize); 6431 if (numblks <= UFS_NDADDR) { 6432 totblks = howmany(length, fs->fs_fsize); 6433 goto out; 6434 } 6435 totblks = blkstofrags(fs, numblks); 6436 numblks -= UFS_NDADDR; 6437 /* 6438 * Count all single, then double, then triple indirects required. 6439 * Subtracting one indirects worth of blocks for each pass 6440 * acknowledges one of each pointed to by the inode. 6441 */ 6442 for (;;) { 6443 totblks += blkstofrags(fs, howmany(numblks, NINDIR(fs))); 6444 numblks -= NINDIR(fs); 6445 if (numblks <= 0) 6446 break; 6447 numblks = howmany(numblks, NINDIR(fs)); 6448 } 6449 out: 6450 totblks = fsbtodb(fs, totblks); 6451 /* 6452 * Handle sparse files. We can't reclaim more blocks than the inode 6453 * references. We will correct it later in handle_complete_freeblks() 6454 * when we know the real count. 6455 */ 6456 if (totblks > datablocks) 6457 return (0); 6458 return (datablocks - totblks); 6459 } 6460 6461 /* 6462 * Handle freeblocks for journaled softupdate filesystems. 6463 * 6464 * Contrary to normal softupdates, we must preserve the block pointers in 6465 * indirects until their subordinates are free. This is to avoid journaling 6466 * every block that is freed which may consume more space than the journal 6467 * itself. The recovery program will see the free block journals at the 6468 * base of the truncated area and traverse them to reclaim space. The 6469 * pointers in the inode may be cleared immediately after the journal 6470 * records are written because each direct and indirect pointer in the 6471 * inode is recorded in a journal. This permits full truncation to proceed 6472 * asynchronously. The write order is journal -> inode -> cgs -> indirects. 6473 * 6474 * The algorithm is as follows: 6475 * 1) Traverse the in-memory state and create journal entries to release 6476 * the relevant blocks and full indirect trees. 6477 * 2) Traverse the indirect block chain adding partial truncation freework 6478 * records to indirects in the path to lastlbn. The freework will 6479 * prevent new allocation dependencies from being satisfied in this 6480 * indirect until the truncation completes. 6481 * 3) Read and lock the inode block, performing an update with the new size 6482 * and pointers. This prevents truncated data from becoming valid on 6483 * disk through step 4. 6484 * 4) Reap unsatisfied dependencies that are beyond the truncated area, 6485 * eliminate journal work for those records that do not require it. 6486 * 5) Schedule the journal records to be written followed by the inode block. 6487 * 6) Allocate any necessary frags for the end of file. 6488 * 7) Zero any partially truncated blocks. 6489 * 6490 * From this truncation proceeds asynchronously using the freework and 6491 * indir_trunc machinery. The file will not be extended again into a 6492 * partially truncated indirect block until all work is completed but 6493 * the normal dependency mechanism ensures that it is rolled back/forward 6494 * as appropriate. Further truncation may occur without delay and is 6495 * serialized in indir_trunc(). 6496 */ 6497 void 6498 softdep_journal_freeblocks(ip, cred, length, flags) 6499 struct inode *ip; /* The inode whose length is to be reduced */ 6500 struct ucred *cred; 6501 off_t length; /* The new length for the file */ 6502 int flags; /* IO_EXT and/or IO_NORMAL */ 6503 { 6504 struct freeblks *freeblks, *fbn; 6505 struct worklist *wk, *wkn; 6506 struct inodedep *inodedep; 6507 struct jblkdep *jblkdep; 6508 struct allocdirect *adp, *adpn; 6509 struct ufsmount *ump; 6510 struct fs *fs; 6511 struct buf *bp; 6512 struct vnode *vp; 6513 struct mount *mp; 6514 ufs2_daddr_t extblocks, datablocks; 6515 ufs_lbn_t tmpval, lbn, lastlbn; 6516 int frags, lastoff, iboff, allocblock, needj, error, i; 6517 6518 ump = ITOUMP(ip); 6519 mp = UFSTOVFS(ump); 6520 fs = ump->um_fs; 6521 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6522 ("softdep_journal_freeblocks called on non-softdep filesystem")); 6523 vp = ITOV(ip); 6524 needj = 1; 6525 iboff = -1; 6526 allocblock = 0; 6527 extblocks = 0; 6528 datablocks = 0; 6529 frags = 0; 6530 freeblks = newfreeblks(mp, ip); 6531 ACQUIRE_LOCK(ump); 6532 /* 6533 * If we're truncating a removed file that will never be written 6534 * we don't need to journal the block frees. The canceled journals 6535 * for the allocations will suffice. 6536 */ 6537 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6538 if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED && 6539 length == 0) 6540 needj = 0; 6541 CTR3(KTR_SUJ, "softdep_journal_freeblks: ip %d length %ld needj %d", 6542 ip->i_number, length, needj); 6543 FREE_LOCK(ump); 6544 /* 6545 * Calculate the lbn that we are truncating to. This results in -1 6546 * if we're truncating the 0 bytes. So it is the last lbn we want 6547 * to keep, not the first lbn we want to truncate. 6548 */ 6549 lastlbn = lblkno(fs, length + fs->fs_bsize - 1) - 1; 6550 lastoff = blkoff(fs, length); 6551 /* 6552 * Compute frags we are keeping in lastlbn. 0 means all. 6553 */ 6554 if (lastlbn >= 0 && lastlbn < UFS_NDADDR) { 6555 frags = fragroundup(fs, lastoff); 6556 /* adp offset of last valid allocdirect. */ 6557 iboff = lastlbn; 6558 } else if (lastlbn > 0) 6559 iboff = UFS_NDADDR; 6560 if (fs->fs_magic == FS_UFS2_MAGIC) 6561 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 6562 /* 6563 * Handle normal data blocks and indirects. This section saves 6564 * values used after the inode update to complete frag and indirect 6565 * truncation. 6566 */ 6567 if ((flags & IO_NORMAL) != 0) { 6568 /* 6569 * Handle truncation of whole direct and indirect blocks. 6570 */ 6571 for (i = iboff + 1; i < UFS_NDADDR; i++) 6572 setup_freedirect(freeblks, ip, i, needj); 6573 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR; 6574 i < UFS_NIADDR; 6575 i++, lbn += tmpval, tmpval *= NINDIR(fs)) { 6576 /* Release a whole indirect tree. */ 6577 if (lbn > lastlbn) { 6578 setup_freeindir(freeblks, ip, i, -lbn -i, 6579 needj); 6580 continue; 6581 } 6582 iboff = i + UFS_NDADDR; 6583 /* 6584 * Traverse partially truncated indirect tree. 6585 */ 6586 if (lbn <= lastlbn && lbn + tmpval - 1 > lastlbn) 6587 setup_trunc_indir(freeblks, ip, -lbn - i, 6588 lastlbn, DIP(ip, i_ib[i])); 6589 } 6590 /* 6591 * Handle partial truncation to a frag boundary. 6592 */ 6593 if (frags) { 6594 ufs2_daddr_t blkno; 6595 long oldfrags; 6596 6597 oldfrags = blksize(fs, ip, lastlbn); 6598 blkno = DIP(ip, i_db[lastlbn]); 6599 if (blkno && oldfrags != frags) { 6600 oldfrags -= frags; 6601 oldfrags = numfrags(fs, oldfrags); 6602 blkno += numfrags(fs, frags); 6603 newfreework(ump, freeblks, NULL, lastlbn, 6604 blkno, oldfrags, 0, needj); 6605 if (needj) 6606 adjust_newfreework(freeblks, 6607 numfrags(fs, frags)); 6608 } else if (blkno == 0) 6609 allocblock = 1; 6610 } 6611 /* 6612 * Add a journal record for partial truncate if we are 6613 * handling indirect blocks. Non-indirects need no extra 6614 * journaling. 6615 */ 6616 if (length != 0 && lastlbn >= UFS_NDADDR) { 6617 ip->i_flag |= IN_TRUNCATED; 6618 newjtrunc(freeblks, length, 0); 6619 } 6620 ip->i_size = length; 6621 DIP_SET(ip, i_size, ip->i_size); 6622 datablocks = DIP(ip, i_blocks) - extblocks; 6623 if (length != 0) 6624 datablocks = blkcount(fs, datablocks, length); 6625 freeblks->fb_len = length; 6626 } 6627 if ((flags & IO_EXT) != 0) { 6628 for (i = 0; i < UFS_NXADDR; i++) 6629 setup_freeext(freeblks, ip, i, needj); 6630 ip->i_din2->di_extsize = 0; 6631 datablocks += extblocks; 6632 } 6633 #ifdef QUOTA 6634 /* Reference the quotas in case the block count is wrong in the end. */ 6635 quotaref(vp, freeblks->fb_quota); 6636 (void) chkdq(ip, -datablocks, NOCRED, 0); 6637 #endif 6638 freeblks->fb_chkcnt = -datablocks; 6639 UFS_LOCK(ump); 6640 fs->fs_pendingblocks += datablocks; 6641 UFS_UNLOCK(ump); 6642 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 6643 /* 6644 * Handle truncation of incomplete alloc direct dependencies. We 6645 * hold the inode block locked to prevent incomplete dependencies 6646 * from reaching the disk while we are eliminating those that 6647 * have been truncated. This is a partially inlined ffs_update(). 6648 */ 6649 ufs_itimes(vp); 6650 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED); 6651 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 6652 (int)fs->fs_bsize, cred, &bp); 6653 if (error) { 6654 brelse(bp); 6655 softdep_error("softdep_journal_freeblocks", error); 6656 return; 6657 } 6658 if (bp->b_bufsize == fs->fs_bsize) 6659 bp->b_flags |= B_CLUSTEROK; 6660 softdep_update_inodeblock(ip, bp, 0); 6661 if (ump->um_fstype == UFS1) 6662 *((struct ufs1_dinode *)bp->b_data + 6663 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1; 6664 else 6665 *((struct ufs2_dinode *)bp->b_data + 6666 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2; 6667 ACQUIRE_LOCK(ump); 6668 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6669 if ((inodedep->id_state & IOSTARTED) != 0) 6670 panic("softdep_setup_freeblocks: inode busy"); 6671 /* 6672 * Add the freeblks structure to the list of operations that 6673 * must await the zero'ed inode being written to disk. If we 6674 * still have a bitmap dependency (needj), then the inode 6675 * has never been written to disk, so we can process the 6676 * freeblks below once we have deleted the dependencies. 6677 */ 6678 if (needj) 6679 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 6680 else 6681 freeblks->fb_state |= COMPLETE; 6682 if ((flags & IO_NORMAL) != 0) { 6683 TAILQ_FOREACH_SAFE(adp, &inodedep->id_inoupdt, ad_next, adpn) { 6684 if (adp->ad_offset > iboff) 6685 cancel_allocdirect(&inodedep->id_inoupdt, adp, 6686 freeblks); 6687 /* 6688 * Truncate the allocdirect. We could eliminate 6689 * or modify journal records as well. 6690 */ 6691 else if (adp->ad_offset == iboff && frags) 6692 adp->ad_newsize = frags; 6693 } 6694 } 6695 if ((flags & IO_EXT) != 0) 6696 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 6697 cancel_allocdirect(&inodedep->id_extupdt, adp, 6698 freeblks); 6699 /* 6700 * Scan the bufwait list for newblock dependencies that will never 6701 * make it to disk. 6702 */ 6703 LIST_FOREACH_SAFE(wk, &inodedep->id_bufwait, wk_list, wkn) { 6704 if (wk->wk_type != D_ALLOCDIRECT) 6705 continue; 6706 adp = WK_ALLOCDIRECT(wk); 6707 if (((flags & IO_NORMAL) != 0 && (adp->ad_offset > iboff)) || 6708 ((flags & IO_EXT) != 0 && (adp->ad_state & EXTDATA))) { 6709 cancel_jfreeblk(freeblks, adp->ad_newblkno); 6710 cancel_newblk(WK_NEWBLK(wk), NULL, &freeblks->fb_jwork); 6711 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 6712 } 6713 } 6714 /* 6715 * Add journal work. 6716 */ 6717 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) 6718 add_to_journal(&jblkdep->jb_list); 6719 FREE_LOCK(ump); 6720 bdwrite(bp); 6721 /* 6722 * Truncate dependency structures beyond length. 6723 */ 6724 trunc_dependencies(ip, freeblks, lastlbn, frags, flags); 6725 /* 6726 * This is only set when we need to allocate a fragment because 6727 * none existed at the end of a frag-sized file. It handles only 6728 * allocating a new, zero filled block. 6729 */ 6730 if (allocblock) { 6731 ip->i_size = length - lastoff; 6732 DIP_SET(ip, i_size, ip->i_size); 6733 error = UFS_BALLOC(vp, length - 1, 1, cred, BA_CLRBUF, &bp); 6734 if (error != 0) { 6735 softdep_error("softdep_journal_freeblks", error); 6736 return; 6737 } 6738 ip->i_size = length; 6739 DIP_SET(ip, i_size, length); 6740 ip->i_flag |= IN_CHANGE | IN_UPDATE; 6741 allocbuf(bp, frags); 6742 ffs_update(vp, 0); 6743 bawrite(bp); 6744 } else if (lastoff != 0 && vp->v_type != VDIR) { 6745 int size; 6746 6747 /* 6748 * Zero the end of a truncated frag or block. 6749 */ 6750 size = sblksize(fs, length, lastlbn); 6751 error = bread(vp, lastlbn, size, cred, &bp); 6752 if (error) { 6753 softdep_error("softdep_journal_freeblks", error); 6754 return; 6755 } 6756 bzero((char *)bp->b_data + lastoff, size - lastoff); 6757 bawrite(bp); 6758 6759 } 6760 ACQUIRE_LOCK(ump); 6761 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6762 TAILQ_INSERT_TAIL(&inodedep->id_freeblklst, freeblks, fb_next); 6763 freeblks->fb_state |= DEPCOMPLETE | ONDEPLIST; 6764 /* 6765 * We zero earlier truncations so they don't erroneously 6766 * update i_blocks. 6767 */ 6768 if (freeblks->fb_len == 0 && (flags & IO_NORMAL) != 0) 6769 TAILQ_FOREACH(fbn, &inodedep->id_freeblklst, fb_next) 6770 fbn->fb_len = 0; 6771 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE && 6772 LIST_EMPTY(&freeblks->fb_jblkdephd)) 6773 freeblks->fb_state |= INPROGRESS; 6774 else 6775 freeblks = NULL; 6776 FREE_LOCK(ump); 6777 if (freeblks) 6778 handle_workitem_freeblocks(freeblks, 0); 6779 trunc_pages(ip, length, extblocks, flags); 6780 6781 } 6782 6783 /* 6784 * Flush a JOP_SYNC to the journal. 6785 */ 6786 void 6787 softdep_journal_fsync(ip) 6788 struct inode *ip; 6789 { 6790 struct jfsync *jfsync; 6791 struct ufsmount *ump; 6792 6793 ump = ITOUMP(ip); 6794 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 6795 ("softdep_journal_fsync called on non-softdep filesystem")); 6796 if ((ip->i_flag & IN_TRUNCATED) == 0) 6797 return; 6798 ip->i_flag &= ~IN_TRUNCATED; 6799 jfsync = malloc(sizeof(*jfsync), M_JFSYNC, M_SOFTDEP_FLAGS | M_ZERO); 6800 workitem_alloc(&jfsync->jfs_list, D_JFSYNC, UFSTOVFS(ump)); 6801 jfsync->jfs_size = ip->i_size; 6802 jfsync->jfs_ino = ip->i_number; 6803 ACQUIRE_LOCK(ump); 6804 add_to_journal(&jfsync->jfs_list); 6805 jwait(&jfsync->jfs_list, MNT_WAIT); 6806 FREE_LOCK(ump); 6807 } 6808 6809 /* 6810 * Block de-allocation dependencies. 6811 * 6812 * When blocks are de-allocated, the on-disk pointers must be nullified before 6813 * the blocks are made available for use by other files. (The true 6814 * requirement is that old pointers must be nullified before new on-disk 6815 * pointers are set. We chose this slightly more stringent requirement to 6816 * reduce complexity.) Our implementation handles this dependency by updating 6817 * the inode (or indirect block) appropriately but delaying the actual block 6818 * de-allocation (i.e., freemap and free space count manipulation) until 6819 * after the updated versions reach stable storage. After the disk is 6820 * updated, the blocks can be safely de-allocated whenever it is convenient. 6821 * This implementation handles only the common case of reducing a file's 6822 * length to zero. Other cases are handled by the conventional synchronous 6823 * write approach. 6824 * 6825 * The ffs implementation with which we worked double-checks 6826 * the state of the block pointers and file size as it reduces 6827 * a file's length. Some of this code is replicated here in our 6828 * soft updates implementation. The freeblks->fb_chkcnt field is 6829 * used to transfer a part of this information to the procedure 6830 * that eventually de-allocates the blocks. 6831 * 6832 * This routine should be called from the routine that shortens 6833 * a file's length, before the inode's size or block pointers 6834 * are modified. It will save the block pointer information for 6835 * later release and zero the inode so that the calling routine 6836 * can release it. 6837 */ 6838 void 6839 softdep_setup_freeblocks(ip, length, flags) 6840 struct inode *ip; /* The inode whose length is to be reduced */ 6841 off_t length; /* The new length for the file */ 6842 int flags; /* IO_EXT and/or IO_NORMAL */ 6843 { 6844 struct ufs1_dinode *dp1; 6845 struct ufs2_dinode *dp2; 6846 struct freeblks *freeblks; 6847 struct inodedep *inodedep; 6848 struct allocdirect *adp; 6849 struct ufsmount *ump; 6850 struct buf *bp; 6851 struct fs *fs; 6852 ufs2_daddr_t extblocks, datablocks; 6853 struct mount *mp; 6854 int i, delay, error; 6855 ufs_lbn_t tmpval; 6856 ufs_lbn_t lbn; 6857 6858 ump = ITOUMP(ip); 6859 mp = UFSTOVFS(ump); 6860 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6861 ("softdep_setup_freeblocks called on non-softdep filesystem")); 6862 CTR2(KTR_SUJ, "softdep_setup_freeblks: ip %d length %ld", 6863 ip->i_number, length); 6864 KASSERT(length == 0, ("softdep_setup_freeblocks: non-zero length")); 6865 fs = ump->um_fs; 6866 if ((error = bread(ump->um_devvp, 6867 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 6868 (int)fs->fs_bsize, NOCRED, &bp)) != 0) { 6869 brelse(bp); 6870 softdep_error("softdep_setup_freeblocks", error); 6871 return; 6872 } 6873 freeblks = newfreeblks(mp, ip); 6874 extblocks = 0; 6875 datablocks = 0; 6876 if (fs->fs_magic == FS_UFS2_MAGIC) 6877 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 6878 if ((flags & IO_NORMAL) != 0) { 6879 for (i = 0; i < UFS_NDADDR; i++) 6880 setup_freedirect(freeblks, ip, i, 0); 6881 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR; 6882 i < UFS_NIADDR; 6883 i++, lbn += tmpval, tmpval *= NINDIR(fs)) 6884 setup_freeindir(freeblks, ip, i, -lbn -i, 0); 6885 ip->i_size = 0; 6886 DIP_SET(ip, i_size, 0); 6887 datablocks = DIP(ip, i_blocks) - extblocks; 6888 } 6889 if ((flags & IO_EXT) != 0) { 6890 for (i = 0; i < UFS_NXADDR; i++) 6891 setup_freeext(freeblks, ip, i, 0); 6892 ip->i_din2->di_extsize = 0; 6893 datablocks += extblocks; 6894 } 6895 #ifdef QUOTA 6896 /* Reference the quotas in case the block count is wrong in the end. */ 6897 quotaref(ITOV(ip), freeblks->fb_quota); 6898 (void) chkdq(ip, -datablocks, NOCRED, 0); 6899 #endif 6900 freeblks->fb_chkcnt = -datablocks; 6901 UFS_LOCK(ump); 6902 fs->fs_pendingblocks += datablocks; 6903 UFS_UNLOCK(ump); 6904 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 6905 /* 6906 * Push the zero'ed inode to to its disk buffer so that we are free 6907 * to delete its dependencies below. Once the dependencies are gone 6908 * the buffer can be safely released. 6909 */ 6910 if (ump->um_fstype == UFS1) { 6911 dp1 = ((struct ufs1_dinode *)bp->b_data + 6912 ino_to_fsbo(fs, ip->i_number)); 6913 ip->i_din1->di_freelink = dp1->di_freelink; 6914 *dp1 = *ip->i_din1; 6915 } else { 6916 dp2 = ((struct ufs2_dinode *)bp->b_data + 6917 ino_to_fsbo(fs, ip->i_number)); 6918 ip->i_din2->di_freelink = dp2->di_freelink; 6919 *dp2 = *ip->i_din2; 6920 } 6921 /* 6922 * Find and eliminate any inode dependencies. 6923 */ 6924 ACQUIRE_LOCK(ump); 6925 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6926 if ((inodedep->id_state & IOSTARTED) != 0) 6927 panic("softdep_setup_freeblocks: inode busy"); 6928 /* 6929 * Add the freeblks structure to the list of operations that 6930 * must await the zero'ed inode being written to disk. If we 6931 * still have a bitmap dependency (delay == 0), then the inode 6932 * has never been written to disk, so we can process the 6933 * freeblks below once we have deleted the dependencies. 6934 */ 6935 delay = (inodedep->id_state & DEPCOMPLETE); 6936 if (delay) 6937 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 6938 else 6939 freeblks->fb_state |= COMPLETE; 6940 /* 6941 * Because the file length has been truncated to zero, any 6942 * pending block allocation dependency structures associated 6943 * with this inode are obsolete and can simply be de-allocated. 6944 * We must first merge the two dependency lists to get rid of 6945 * any duplicate freefrag structures, then purge the merged list. 6946 * If we still have a bitmap dependency, then the inode has never 6947 * been written to disk, so we can free any fragments without delay. 6948 */ 6949 if (flags & IO_NORMAL) { 6950 merge_inode_lists(&inodedep->id_newinoupdt, 6951 &inodedep->id_inoupdt); 6952 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 6953 cancel_allocdirect(&inodedep->id_inoupdt, adp, 6954 freeblks); 6955 } 6956 if (flags & IO_EXT) { 6957 merge_inode_lists(&inodedep->id_newextupdt, 6958 &inodedep->id_extupdt); 6959 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 6960 cancel_allocdirect(&inodedep->id_extupdt, adp, 6961 freeblks); 6962 } 6963 FREE_LOCK(ump); 6964 bdwrite(bp); 6965 trunc_dependencies(ip, freeblks, -1, 0, flags); 6966 ACQUIRE_LOCK(ump); 6967 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 6968 (void) free_inodedep(inodedep); 6969 freeblks->fb_state |= DEPCOMPLETE; 6970 /* 6971 * If the inode with zeroed block pointers is now on disk 6972 * we can start freeing blocks. 6973 */ 6974 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 6975 freeblks->fb_state |= INPROGRESS; 6976 else 6977 freeblks = NULL; 6978 FREE_LOCK(ump); 6979 if (freeblks) 6980 handle_workitem_freeblocks(freeblks, 0); 6981 trunc_pages(ip, length, extblocks, flags); 6982 } 6983 6984 /* 6985 * Eliminate pages from the page cache that back parts of this inode and 6986 * adjust the vnode pager's idea of our size. This prevents stale data 6987 * from hanging around in the page cache. 6988 */ 6989 static void 6990 trunc_pages(ip, length, extblocks, flags) 6991 struct inode *ip; 6992 off_t length; 6993 ufs2_daddr_t extblocks; 6994 int flags; 6995 { 6996 struct vnode *vp; 6997 struct fs *fs; 6998 ufs_lbn_t lbn; 6999 off_t end, extend; 7000 7001 vp = ITOV(ip); 7002 fs = ITOFS(ip); 7003 extend = OFF_TO_IDX(lblktosize(fs, -extblocks)); 7004 if ((flags & IO_EXT) != 0) 7005 vn_pages_remove(vp, extend, 0); 7006 if ((flags & IO_NORMAL) == 0) 7007 return; 7008 BO_LOCK(&vp->v_bufobj); 7009 drain_output(vp); 7010 BO_UNLOCK(&vp->v_bufobj); 7011 /* 7012 * The vnode pager eliminates file pages we eliminate indirects 7013 * below. 7014 */ 7015 vnode_pager_setsize(vp, length); 7016 /* 7017 * Calculate the end based on the last indirect we want to keep. If 7018 * the block extends into indirects we can just use the negative of 7019 * its lbn. Doubles and triples exist at lower numbers so we must 7020 * be careful not to remove those, if they exist. double and triple 7021 * indirect lbns do not overlap with others so it is not important 7022 * to verify how many levels are required. 7023 */ 7024 lbn = lblkno(fs, length); 7025 if (lbn >= UFS_NDADDR) { 7026 /* Calculate the virtual lbn of the triple indirect. */ 7027 lbn = -lbn - (UFS_NIADDR - 1); 7028 end = OFF_TO_IDX(lblktosize(fs, lbn)); 7029 } else 7030 end = extend; 7031 vn_pages_remove(vp, OFF_TO_IDX(OFF_MAX), end); 7032 } 7033 7034 /* 7035 * See if the buf bp is in the range eliminated by truncation. 7036 */ 7037 static int 7038 trunc_check_buf(bp, blkoffp, lastlbn, lastoff, flags) 7039 struct buf *bp; 7040 int *blkoffp; 7041 ufs_lbn_t lastlbn; 7042 int lastoff; 7043 int flags; 7044 { 7045 ufs_lbn_t lbn; 7046 7047 *blkoffp = 0; 7048 /* Only match ext/normal blocks as appropriate. */ 7049 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) || 7050 ((flags & IO_NORMAL) == 0 && (bp->b_xflags & BX_ALTDATA) == 0)) 7051 return (0); 7052 /* ALTDATA is always a full truncation. */ 7053 if ((bp->b_xflags & BX_ALTDATA) != 0) 7054 return (1); 7055 /* -1 is full truncation. */ 7056 if (lastlbn == -1) 7057 return (1); 7058 /* 7059 * If this is a partial truncate we only want those 7060 * blocks and indirect blocks that cover the range 7061 * we're after. 7062 */ 7063 lbn = bp->b_lblkno; 7064 if (lbn < 0) 7065 lbn = -(lbn + lbn_level(lbn)); 7066 if (lbn < lastlbn) 7067 return (0); 7068 /* Here we only truncate lblkno if it's partial. */ 7069 if (lbn == lastlbn) { 7070 if (lastoff == 0) 7071 return (0); 7072 *blkoffp = lastoff; 7073 } 7074 return (1); 7075 } 7076 7077 /* 7078 * Eliminate any dependencies that exist in memory beyond lblkno:off 7079 */ 7080 static void 7081 trunc_dependencies(ip, freeblks, lastlbn, lastoff, flags) 7082 struct inode *ip; 7083 struct freeblks *freeblks; 7084 ufs_lbn_t lastlbn; 7085 int lastoff; 7086 int flags; 7087 { 7088 struct bufobj *bo; 7089 struct vnode *vp; 7090 struct buf *bp; 7091 int blkoff; 7092 7093 /* 7094 * We must wait for any I/O in progress to finish so that 7095 * all potential buffers on the dirty list will be visible. 7096 * Once they are all there, walk the list and get rid of 7097 * any dependencies. 7098 */ 7099 vp = ITOV(ip); 7100 bo = &vp->v_bufobj; 7101 BO_LOCK(bo); 7102 drain_output(vp); 7103 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) 7104 bp->b_vflags &= ~BV_SCANNED; 7105 restart: 7106 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) { 7107 if (bp->b_vflags & BV_SCANNED) 7108 continue; 7109 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 7110 bp->b_vflags |= BV_SCANNED; 7111 continue; 7112 } 7113 KASSERT(bp->b_bufobj == bo, ("Wrong object in buffer")); 7114 if ((bp = getdirtybuf(bp, BO_LOCKPTR(bo), MNT_WAIT)) == NULL) 7115 goto restart; 7116 BO_UNLOCK(bo); 7117 if (deallocate_dependencies(bp, freeblks, blkoff)) 7118 bqrelse(bp); 7119 else 7120 brelse(bp); 7121 BO_LOCK(bo); 7122 goto restart; 7123 } 7124 /* 7125 * Now do the work of vtruncbuf while also matching indirect blocks. 7126 */ 7127 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) 7128 bp->b_vflags &= ~BV_SCANNED; 7129 cleanrestart: 7130 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) { 7131 if (bp->b_vflags & BV_SCANNED) 7132 continue; 7133 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 7134 bp->b_vflags |= BV_SCANNED; 7135 continue; 7136 } 7137 if (BUF_LOCK(bp, 7138 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 7139 BO_LOCKPTR(bo)) == ENOLCK) { 7140 BO_LOCK(bo); 7141 goto cleanrestart; 7142 } 7143 bp->b_vflags |= BV_SCANNED; 7144 bremfree(bp); 7145 if (blkoff != 0) { 7146 allocbuf(bp, blkoff); 7147 bqrelse(bp); 7148 } else { 7149 bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF; 7150 brelse(bp); 7151 } 7152 BO_LOCK(bo); 7153 goto cleanrestart; 7154 } 7155 drain_output(vp); 7156 BO_UNLOCK(bo); 7157 } 7158 7159 static int 7160 cancel_pagedep(pagedep, freeblks, blkoff) 7161 struct pagedep *pagedep; 7162 struct freeblks *freeblks; 7163 int blkoff; 7164 { 7165 struct jremref *jremref; 7166 struct jmvref *jmvref; 7167 struct dirrem *dirrem, *tmp; 7168 int i; 7169 7170 /* 7171 * Copy any directory remove dependencies to the list 7172 * to be processed after the freeblks proceeds. If 7173 * directory entry never made it to disk they 7174 * can be dumped directly onto the work list. 7175 */ 7176 LIST_FOREACH_SAFE(dirrem, &pagedep->pd_dirremhd, dm_next, tmp) { 7177 /* Skip this directory removal if it is intended to remain. */ 7178 if (dirrem->dm_offset < blkoff) 7179 continue; 7180 /* 7181 * If there are any dirrems we wait for the journal write 7182 * to complete and then restart the buf scan as the lock 7183 * has been dropped. 7184 */ 7185 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) { 7186 jwait(&jremref->jr_list, MNT_WAIT); 7187 return (ERESTART); 7188 } 7189 LIST_REMOVE(dirrem, dm_next); 7190 dirrem->dm_dirinum = pagedep->pd_ino; 7191 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list); 7192 } 7193 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) { 7194 jwait(&jmvref->jm_list, MNT_WAIT); 7195 return (ERESTART); 7196 } 7197 /* 7198 * When we're partially truncating a pagedep we just want to flush 7199 * journal entries and return. There can not be any adds in the 7200 * truncated portion of the directory and newblk must remain if 7201 * part of the block remains. 7202 */ 7203 if (blkoff != 0) { 7204 struct diradd *dap; 7205 7206 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 7207 if (dap->da_offset > blkoff) 7208 panic("cancel_pagedep: diradd %p off %d > %d", 7209 dap, dap->da_offset, blkoff); 7210 for (i = 0; i < DAHASHSZ; i++) 7211 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) 7212 if (dap->da_offset > blkoff) 7213 panic("cancel_pagedep: diradd %p off %d > %d", 7214 dap, dap->da_offset, blkoff); 7215 return (0); 7216 } 7217 /* 7218 * There should be no directory add dependencies present 7219 * as the directory could not be truncated until all 7220 * children were removed. 7221 */ 7222 KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL, 7223 ("deallocate_dependencies: pendinghd != NULL")); 7224 for (i = 0; i < DAHASHSZ; i++) 7225 KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL, 7226 ("deallocate_dependencies: diraddhd != NULL")); 7227 if ((pagedep->pd_state & NEWBLOCK) != 0) 7228 free_newdirblk(pagedep->pd_newdirblk); 7229 if (free_pagedep(pagedep) == 0) 7230 panic("Failed to free pagedep %p", pagedep); 7231 return (0); 7232 } 7233 7234 /* 7235 * Reclaim any dependency structures from a buffer that is about to 7236 * be reallocated to a new vnode. The buffer must be locked, thus, 7237 * no I/O completion operations can occur while we are manipulating 7238 * its associated dependencies. The mutex is held so that other I/O's 7239 * associated with related dependencies do not occur. 7240 */ 7241 static int 7242 deallocate_dependencies(bp, freeblks, off) 7243 struct buf *bp; 7244 struct freeblks *freeblks; 7245 int off; 7246 { 7247 struct indirdep *indirdep; 7248 struct pagedep *pagedep; 7249 struct worklist *wk, *wkn; 7250 struct ufsmount *ump; 7251 7252 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL) 7253 goto done; 7254 ump = VFSTOUFS(wk->wk_mp); 7255 ACQUIRE_LOCK(ump); 7256 LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) { 7257 switch (wk->wk_type) { 7258 case D_INDIRDEP: 7259 indirdep = WK_INDIRDEP(wk); 7260 if (bp->b_lblkno >= 0 || 7261 bp->b_blkno != indirdep->ir_savebp->b_lblkno) 7262 panic("deallocate_dependencies: not indir"); 7263 cancel_indirdep(indirdep, bp, freeblks); 7264 continue; 7265 7266 case D_PAGEDEP: 7267 pagedep = WK_PAGEDEP(wk); 7268 if (cancel_pagedep(pagedep, freeblks, off)) { 7269 FREE_LOCK(ump); 7270 return (ERESTART); 7271 } 7272 continue; 7273 7274 case D_ALLOCINDIR: 7275 /* 7276 * Simply remove the allocindir, we'll find it via 7277 * the indirdep where we can clear pointers if 7278 * needed. 7279 */ 7280 WORKLIST_REMOVE(wk); 7281 continue; 7282 7283 case D_FREEWORK: 7284 /* 7285 * A truncation is waiting for the zero'd pointers 7286 * to be written. It can be freed when the freeblks 7287 * is journaled. 7288 */ 7289 WORKLIST_REMOVE(wk); 7290 wk->wk_state |= ONDEPLIST; 7291 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 7292 break; 7293 7294 case D_ALLOCDIRECT: 7295 if (off != 0) 7296 continue; 7297 /* FALLTHROUGH */ 7298 default: 7299 panic("deallocate_dependencies: Unexpected type %s", 7300 TYPENAME(wk->wk_type)); 7301 /* NOTREACHED */ 7302 } 7303 } 7304 FREE_LOCK(ump); 7305 done: 7306 /* 7307 * Don't throw away this buf, we were partially truncating and 7308 * some deps may always remain. 7309 */ 7310 if (off) { 7311 allocbuf(bp, off); 7312 bp->b_vflags |= BV_SCANNED; 7313 return (EBUSY); 7314 } 7315 bp->b_flags |= B_INVAL | B_NOCACHE; 7316 7317 return (0); 7318 } 7319 7320 /* 7321 * An allocdirect is being canceled due to a truncate. We must make sure 7322 * the journal entry is released in concert with the blkfree that releases 7323 * the storage. Completed journal entries must not be released until the 7324 * space is no longer pointed to by the inode or in the bitmap. 7325 */ 7326 static void 7327 cancel_allocdirect(adphead, adp, freeblks) 7328 struct allocdirectlst *adphead; 7329 struct allocdirect *adp; 7330 struct freeblks *freeblks; 7331 { 7332 struct freework *freework; 7333 struct newblk *newblk; 7334 struct worklist *wk; 7335 7336 TAILQ_REMOVE(adphead, adp, ad_next); 7337 newblk = (struct newblk *)adp; 7338 freework = NULL; 7339 /* 7340 * Find the correct freework structure. 7341 */ 7342 LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) { 7343 if (wk->wk_type != D_FREEWORK) 7344 continue; 7345 freework = WK_FREEWORK(wk); 7346 if (freework->fw_blkno == newblk->nb_newblkno) 7347 break; 7348 } 7349 if (freework == NULL) 7350 panic("cancel_allocdirect: Freework not found"); 7351 /* 7352 * If a newblk exists at all we still have the journal entry that 7353 * initiated the allocation so we do not need to journal the free. 7354 */ 7355 cancel_jfreeblk(freeblks, freework->fw_blkno); 7356 /* 7357 * If the journal hasn't been written the jnewblk must be passed 7358 * to the call to ffs_blkfree that reclaims the space. We accomplish 7359 * this by linking the journal dependency into the freework to be 7360 * freed when freework_freeblock() is called. If the journal has 7361 * been written we can simply reclaim the journal space when the 7362 * freeblks work is complete. 7363 */ 7364 freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list, 7365 &freeblks->fb_jwork); 7366 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 7367 } 7368 7369 7370 /* 7371 * Cancel a new block allocation. May be an indirect or direct block. We 7372 * remove it from various lists and return any journal record that needs to 7373 * be resolved by the caller. 7374 * 7375 * A special consideration is made for indirects which were never pointed 7376 * at on disk and will never be found once this block is released. 7377 */ 7378 static struct jnewblk * 7379 cancel_newblk(newblk, wk, wkhd) 7380 struct newblk *newblk; 7381 struct worklist *wk; 7382 struct workhead *wkhd; 7383 { 7384 struct jnewblk *jnewblk; 7385 7386 CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno); 7387 7388 newblk->nb_state |= GOINGAWAY; 7389 /* 7390 * Previously we traversed the completedhd on each indirdep 7391 * attached to this newblk to cancel them and gather journal 7392 * work. Since we need only the oldest journal segment and 7393 * the lowest point on the tree will always have the oldest 7394 * journal segment we are free to release the segments 7395 * of any subordinates and may leave the indirdep list to 7396 * indirdep_complete() when this newblk is freed. 7397 */ 7398 if (newblk->nb_state & ONDEPLIST) { 7399 newblk->nb_state &= ~ONDEPLIST; 7400 LIST_REMOVE(newblk, nb_deps); 7401 } 7402 if (newblk->nb_state & ONWORKLIST) 7403 WORKLIST_REMOVE(&newblk->nb_list); 7404 /* 7405 * If the journal entry hasn't been written we save a pointer to 7406 * the dependency that frees it until it is written or the 7407 * superseding operation completes. 7408 */ 7409 jnewblk = newblk->nb_jnewblk; 7410 if (jnewblk != NULL && wk != NULL) { 7411 newblk->nb_jnewblk = NULL; 7412 jnewblk->jn_dep = wk; 7413 } 7414 if (!LIST_EMPTY(&newblk->nb_jwork)) 7415 jwork_move(wkhd, &newblk->nb_jwork); 7416 /* 7417 * When truncating we must free the newdirblk early to remove 7418 * the pagedep from the hash before returning. 7419 */ 7420 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7421 free_newdirblk(WK_NEWDIRBLK(wk)); 7422 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7423 panic("cancel_newblk: extra newdirblk"); 7424 7425 return (jnewblk); 7426 } 7427 7428 /* 7429 * Schedule the freefrag associated with a newblk to be released once 7430 * the pointers are written and the previous block is no longer needed. 7431 */ 7432 static void 7433 newblk_freefrag(newblk) 7434 struct newblk *newblk; 7435 { 7436 struct freefrag *freefrag; 7437 7438 if (newblk->nb_freefrag == NULL) 7439 return; 7440 freefrag = newblk->nb_freefrag; 7441 newblk->nb_freefrag = NULL; 7442 freefrag->ff_state |= COMPLETE; 7443 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 7444 add_to_worklist(&freefrag->ff_list, 0); 7445 } 7446 7447 /* 7448 * Free a newblk. Generate a new freefrag work request if appropriate. 7449 * This must be called after the inode pointer and any direct block pointers 7450 * are valid or fully removed via truncate or frag extension. 7451 */ 7452 static void 7453 free_newblk(newblk) 7454 struct newblk *newblk; 7455 { 7456 struct indirdep *indirdep; 7457 struct worklist *wk; 7458 7459 KASSERT(newblk->nb_jnewblk == NULL, 7460 ("free_newblk: jnewblk %p still attached", newblk->nb_jnewblk)); 7461 KASSERT(newblk->nb_list.wk_type != D_NEWBLK, 7462 ("free_newblk: unclaimed newblk")); 7463 LOCK_OWNED(VFSTOUFS(newblk->nb_list.wk_mp)); 7464 newblk_freefrag(newblk); 7465 if (newblk->nb_state & ONDEPLIST) 7466 LIST_REMOVE(newblk, nb_deps); 7467 if (newblk->nb_state & ONWORKLIST) 7468 WORKLIST_REMOVE(&newblk->nb_list); 7469 LIST_REMOVE(newblk, nb_hash); 7470 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7471 free_newdirblk(WK_NEWDIRBLK(wk)); 7472 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7473 panic("free_newblk: extra newdirblk"); 7474 while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL) 7475 indirdep_complete(indirdep); 7476 handle_jwork(&newblk->nb_jwork); 7477 WORKITEM_FREE(newblk, D_NEWBLK); 7478 } 7479 7480 /* 7481 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep. 7482 * This routine must be called with splbio interrupts blocked. 7483 */ 7484 static void 7485 free_newdirblk(newdirblk) 7486 struct newdirblk *newdirblk; 7487 { 7488 struct pagedep *pagedep; 7489 struct diradd *dap; 7490 struct worklist *wk; 7491 7492 LOCK_OWNED(VFSTOUFS(newdirblk->db_list.wk_mp)); 7493 WORKLIST_REMOVE(&newdirblk->db_list); 7494 /* 7495 * If the pagedep is still linked onto the directory buffer 7496 * dependency chain, then some of the entries on the 7497 * pd_pendinghd list may not be committed to disk yet. In 7498 * this case, we will simply clear the NEWBLOCK flag and 7499 * let the pd_pendinghd list be processed when the pagedep 7500 * is next written. If the pagedep is no longer on the buffer 7501 * dependency chain, then all the entries on the pd_pending 7502 * list are committed to disk and we can free them here. 7503 */ 7504 pagedep = newdirblk->db_pagedep; 7505 pagedep->pd_state &= ~NEWBLOCK; 7506 if ((pagedep->pd_state & ONWORKLIST) == 0) { 7507 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 7508 free_diradd(dap, NULL); 7509 /* 7510 * If no dependencies remain, the pagedep will be freed. 7511 */ 7512 free_pagedep(pagedep); 7513 } 7514 /* Should only ever be one item in the list. */ 7515 while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) { 7516 WORKLIST_REMOVE(wk); 7517 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 7518 } 7519 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 7520 } 7521 7522 /* 7523 * Prepare an inode to be freed. The actual free operation is not 7524 * done until the zero'ed inode has been written to disk. 7525 */ 7526 void 7527 softdep_freefile(pvp, ino, mode) 7528 struct vnode *pvp; 7529 ino_t ino; 7530 int mode; 7531 { 7532 struct inode *ip = VTOI(pvp); 7533 struct inodedep *inodedep; 7534 struct freefile *freefile; 7535 struct freeblks *freeblks; 7536 struct ufsmount *ump; 7537 7538 ump = ITOUMP(ip); 7539 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 7540 ("softdep_freefile called on non-softdep filesystem")); 7541 /* 7542 * This sets up the inode de-allocation dependency. 7543 */ 7544 freefile = malloc(sizeof(struct freefile), 7545 M_FREEFILE, M_SOFTDEP_FLAGS); 7546 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount); 7547 freefile->fx_mode = mode; 7548 freefile->fx_oldinum = ino; 7549 freefile->fx_devvp = ump->um_devvp; 7550 LIST_INIT(&freefile->fx_jwork); 7551 UFS_LOCK(ump); 7552 ump->um_fs->fs_pendinginodes += 1; 7553 UFS_UNLOCK(ump); 7554 7555 /* 7556 * If the inodedep does not exist, then the zero'ed inode has 7557 * been written to disk. If the allocated inode has never been 7558 * written to disk, then the on-disk inode is zero'ed. In either 7559 * case we can free the file immediately. If the journal was 7560 * canceled before being written the inode will never make it to 7561 * disk and we must send the canceled journal entrys to 7562 * ffs_freefile() to be cleared in conjunction with the bitmap. 7563 * Any blocks waiting on the inode to write can be safely freed 7564 * here as it will never been written. 7565 */ 7566 ACQUIRE_LOCK(ump); 7567 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7568 if (inodedep) { 7569 /* 7570 * Clear out freeblks that no longer need to reference 7571 * this inode. 7572 */ 7573 while ((freeblks = 7574 TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) { 7575 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, 7576 fb_next); 7577 freeblks->fb_state &= ~ONDEPLIST; 7578 } 7579 /* 7580 * Remove this inode from the unlinked list. 7581 */ 7582 if (inodedep->id_state & UNLINKED) { 7583 /* 7584 * Save the journal work to be freed with the bitmap 7585 * before we clear UNLINKED. Otherwise it can be lost 7586 * if the inode block is written. 7587 */ 7588 handle_bufwait(inodedep, &freefile->fx_jwork); 7589 clear_unlinked_inodedep(inodedep); 7590 /* 7591 * Re-acquire inodedep as we've dropped the 7592 * per-filesystem lock in clear_unlinked_inodedep(). 7593 */ 7594 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7595 } 7596 } 7597 if (inodedep == NULL || check_inode_unwritten(inodedep)) { 7598 FREE_LOCK(ump); 7599 handle_workitem_freefile(freefile); 7600 return; 7601 } 7602 if ((inodedep->id_state & DEPCOMPLETE) == 0) 7603 inodedep->id_state |= GOINGAWAY; 7604 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list); 7605 FREE_LOCK(ump); 7606 if (ip->i_number == ino) 7607 ip->i_flag |= IN_MODIFIED; 7608 } 7609 7610 /* 7611 * Check to see if an inode has never been written to disk. If 7612 * so free the inodedep and return success, otherwise return failure. 7613 * This routine must be called with splbio interrupts blocked. 7614 * 7615 * If we still have a bitmap dependency, then the inode has never 7616 * been written to disk. Drop the dependency as it is no longer 7617 * necessary since the inode is being deallocated. We set the 7618 * ALLCOMPLETE flags since the bitmap now properly shows that the 7619 * inode is not allocated. Even if the inode is actively being 7620 * written, it has been rolled back to its zero'ed state, so we 7621 * are ensured that a zero inode is what is on the disk. For short 7622 * lived files, this change will usually result in removing all the 7623 * dependencies from the inode so that it can be freed immediately. 7624 */ 7625 static int 7626 check_inode_unwritten(inodedep) 7627 struct inodedep *inodedep; 7628 { 7629 7630 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7631 7632 if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 || 7633 !LIST_EMPTY(&inodedep->id_dirremhd) || 7634 !LIST_EMPTY(&inodedep->id_pendinghd) || 7635 !LIST_EMPTY(&inodedep->id_bufwait) || 7636 !LIST_EMPTY(&inodedep->id_inowait) || 7637 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 7638 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 7639 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 7640 !TAILQ_EMPTY(&inodedep->id_extupdt) || 7641 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 7642 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 7643 inodedep->id_mkdiradd != NULL || 7644 inodedep->id_nlinkdelta != 0) 7645 return (0); 7646 /* 7647 * Another process might be in initiate_write_inodeblock_ufs[12] 7648 * trying to allocate memory without holding "Softdep Lock". 7649 */ 7650 if ((inodedep->id_state & IOSTARTED) != 0 && 7651 inodedep->id_savedino1 == NULL) 7652 return (0); 7653 7654 if (inodedep->id_state & ONDEPLIST) 7655 LIST_REMOVE(inodedep, id_deps); 7656 inodedep->id_state &= ~ONDEPLIST; 7657 inodedep->id_state |= ALLCOMPLETE; 7658 inodedep->id_bmsafemap = NULL; 7659 if (inodedep->id_state & ONWORKLIST) 7660 WORKLIST_REMOVE(&inodedep->id_list); 7661 if (inodedep->id_savedino1 != NULL) { 7662 free(inodedep->id_savedino1, M_SAVEDINO); 7663 inodedep->id_savedino1 = NULL; 7664 } 7665 if (free_inodedep(inodedep) == 0) 7666 panic("check_inode_unwritten: busy inode"); 7667 return (1); 7668 } 7669 7670 static int 7671 check_inodedep_free(inodedep) 7672 struct inodedep *inodedep; 7673 { 7674 7675 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7676 if ((inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE || 7677 !LIST_EMPTY(&inodedep->id_dirremhd) || 7678 !LIST_EMPTY(&inodedep->id_pendinghd) || 7679 !LIST_EMPTY(&inodedep->id_bufwait) || 7680 !LIST_EMPTY(&inodedep->id_inowait) || 7681 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 7682 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 7683 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 7684 !TAILQ_EMPTY(&inodedep->id_extupdt) || 7685 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 7686 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 7687 inodedep->id_mkdiradd != NULL || 7688 inodedep->id_nlinkdelta != 0 || 7689 inodedep->id_savedino1 != NULL) 7690 return (0); 7691 return (1); 7692 } 7693 7694 /* 7695 * Try to free an inodedep structure. Return 1 if it could be freed. 7696 */ 7697 static int 7698 free_inodedep(inodedep) 7699 struct inodedep *inodedep; 7700 { 7701 7702 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7703 if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 || 7704 !check_inodedep_free(inodedep)) 7705 return (0); 7706 if (inodedep->id_state & ONDEPLIST) 7707 LIST_REMOVE(inodedep, id_deps); 7708 LIST_REMOVE(inodedep, id_hash); 7709 WORKITEM_FREE(inodedep, D_INODEDEP); 7710 return (1); 7711 } 7712 7713 /* 7714 * Free the block referenced by a freework structure. The parent freeblks 7715 * structure is released and completed when the final cg bitmap reaches 7716 * the disk. This routine may be freeing a jnewblk which never made it to 7717 * disk in which case we do not have to wait as the operation is undone 7718 * in memory immediately. 7719 */ 7720 static void 7721 freework_freeblock(freework) 7722 struct freework *freework; 7723 { 7724 struct freeblks *freeblks; 7725 struct jnewblk *jnewblk; 7726 struct ufsmount *ump; 7727 struct workhead wkhd; 7728 struct fs *fs; 7729 int bsize; 7730 int needj; 7731 7732 ump = VFSTOUFS(freework->fw_list.wk_mp); 7733 LOCK_OWNED(ump); 7734 /* 7735 * Handle partial truncate separately. 7736 */ 7737 if (freework->fw_indir) { 7738 complete_trunc_indir(freework); 7739 return; 7740 } 7741 freeblks = freework->fw_freeblks; 7742 fs = ump->um_fs; 7743 needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0; 7744 bsize = lfragtosize(fs, freework->fw_frags); 7745 LIST_INIT(&wkhd); 7746 /* 7747 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives 7748 * on the indirblk hashtable and prevents premature freeing. 7749 */ 7750 freework->fw_state |= DEPCOMPLETE; 7751 /* 7752 * SUJ needs to wait for the segment referencing freed indirect 7753 * blocks to expire so that we know the checker will not confuse 7754 * a re-allocated indirect block with its old contents. 7755 */ 7756 if (needj && freework->fw_lbn <= -UFS_NDADDR) 7757 indirblk_insert(freework); 7758 /* 7759 * If we are canceling an existing jnewblk pass it to the free 7760 * routine, otherwise pass the freeblk which will ultimately 7761 * release the freeblks. If we're not journaling, we can just 7762 * free the freeblks immediately. 7763 */ 7764 jnewblk = freework->fw_jnewblk; 7765 if (jnewblk != NULL) { 7766 cancel_jnewblk(jnewblk, &wkhd); 7767 needj = 0; 7768 } else if (needj) { 7769 freework->fw_state |= DELAYEDFREE; 7770 freeblks->fb_cgwait++; 7771 WORKLIST_INSERT(&wkhd, &freework->fw_list); 7772 } 7773 FREE_LOCK(ump); 7774 freeblks_free(ump, freeblks, btodb(bsize)); 7775 CTR4(KTR_SUJ, 7776 "freework_freeblock: ino %d blkno %jd lbn %jd size %ld", 7777 freeblks->fb_inum, freework->fw_blkno, freework->fw_lbn, bsize); 7778 ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, bsize, 7779 freeblks->fb_inum, freeblks->fb_vtype, &wkhd); 7780 ACQUIRE_LOCK(ump); 7781 /* 7782 * The jnewblk will be discarded and the bits in the map never 7783 * made it to disk. We can immediately free the freeblk. 7784 */ 7785 if (needj == 0) 7786 handle_written_freework(freework); 7787 } 7788 7789 /* 7790 * We enqueue freework items that need processing back on the freeblks and 7791 * add the freeblks to the worklist. This makes it easier to find all work 7792 * required to flush a truncation in process_truncates(). 7793 */ 7794 static void 7795 freework_enqueue(freework) 7796 struct freework *freework; 7797 { 7798 struct freeblks *freeblks; 7799 7800 freeblks = freework->fw_freeblks; 7801 if ((freework->fw_state & INPROGRESS) == 0) 7802 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 7803 if ((freeblks->fb_state & 7804 (ONWORKLIST | INPROGRESS | ALLCOMPLETE)) == ALLCOMPLETE && 7805 LIST_EMPTY(&freeblks->fb_jblkdephd)) 7806 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 7807 } 7808 7809 /* 7810 * Start, continue, or finish the process of freeing an indirect block tree. 7811 * The free operation may be paused at any point with fw_off containing the 7812 * offset to restart from. This enables us to implement some flow control 7813 * for large truncates which may fan out and generate a huge number of 7814 * dependencies. 7815 */ 7816 static void 7817 handle_workitem_indirblk(freework) 7818 struct freework *freework; 7819 { 7820 struct freeblks *freeblks; 7821 struct ufsmount *ump; 7822 struct fs *fs; 7823 7824 freeblks = freework->fw_freeblks; 7825 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7826 fs = ump->um_fs; 7827 if (freework->fw_state & DEPCOMPLETE) { 7828 handle_written_freework(freework); 7829 return; 7830 } 7831 if (freework->fw_off == NINDIR(fs)) { 7832 freework_freeblock(freework); 7833 return; 7834 } 7835 freework->fw_state |= INPROGRESS; 7836 FREE_LOCK(ump); 7837 indir_trunc(freework, fsbtodb(fs, freework->fw_blkno), 7838 freework->fw_lbn); 7839 ACQUIRE_LOCK(ump); 7840 } 7841 7842 /* 7843 * Called when a freework structure attached to a cg buf is written. The 7844 * ref on either the parent or the freeblks structure is released and 7845 * the freeblks is added back to the worklist if there is more work to do. 7846 */ 7847 static void 7848 handle_written_freework(freework) 7849 struct freework *freework; 7850 { 7851 struct freeblks *freeblks; 7852 struct freework *parent; 7853 7854 freeblks = freework->fw_freeblks; 7855 parent = freework->fw_parent; 7856 if (freework->fw_state & DELAYEDFREE) 7857 freeblks->fb_cgwait--; 7858 freework->fw_state |= COMPLETE; 7859 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 7860 WORKITEM_FREE(freework, D_FREEWORK); 7861 if (parent) { 7862 if (--parent->fw_ref == 0) 7863 freework_enqueue(parent); 7864 return; 7865 } 7866 if (--freeblks->fb_ref != 0) 7867 return; 7868 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) == 7869 ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd)) 7870 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 7871 } 7872 7873 /* 7874 * This workitem routine performs the block de-allocation. 7875 * The workitem is added to the pending list after the updated 7876 * inode block has been written to disk. As mentioned above, 7877 * checks regarding the number of blocks de-allocated (compared 7878 * to the number of blocks allocated for the file) are also 7879 * performed in this function. 7880 */ 7881 static int 7882 handle_workitem_freeblocks(freeblks, flags) 7883 struct freeblks *freeblks; 7884 int flags; 7885 { 7886 struct freework *freework; 7887 struct newblk *newblk; 7888 struct allocindir *aip; 7889 struct ufsmount *ump; 7890 struct worklist *wk; 7891 7892 KASSERT(LIST_EMPTY(&freeblks->fb_jblkdephd), 7893 ("handle_workitem_freeblocks: Journal entries not written.")); 7894 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7895 ACQUIRE_LOCK(ump); 7896 while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) { 7897 WORKLIST_REMOVE(wk); 7898 switch (wk->wk_type) { 7899 case D_DIRREM: 7900 wk->wk_state |= COMPLETE; 7901 add_to_worklist(wk, 0); 7902 continue; 7903 7904 case D_ALLOCDIRECT: 7905 free_newblk(WK_NEWBLK(wk)); 7906 continue; 7907 7908 case D_ALLOCINDIR: 7909 aip = WK_ALLOCINDIR(wk); 7910 freework = NULL; 7911 if (aip->ai_state & DELAYEDFREE) { 7912 FREE_LOCK(ump); 7913 freework = newfreework(ump, freeblks, NULL, 7914 aip->ai_lbn, aip->ai_newblkno, 7915 ump->um_fs->fs_frag, 0, 0); 7916 ACQUIRE_LOCK(ump); 7917 } 7918 newblk = WK_NEWBLK(wk); 7919 if (newblk->nb_jnewblk) { 7920 freework->fw_jnewblk = newblk->nb_jnewblk; 7921 newblk->nb_jnewblk->jn_dep = &freework->fw_list; 7922 newblk->nb_jnewblk = NULL; 7923 } 7924 free_newblk(newblk); 7925 continue; 7926 7927 case D_FREEWORK: 7928 freework = WK_FREEWORK(wk); 7929 if (freework->fw_lbn <= -UFS_NDADDR) 7930 handle_workitem_indirblk(freework); 7931 else 7932 freework_freeblock(freework); 7933 continue; 7934 default: 7935 panic("handle_workitem_freeblocks: Unknown type %s", 7936 TYPENAME(wk->wk_type)); 7937 } 7938 } 7939 if (freeblks->fb_ref != 0) { 7940 freeblks->fb_state &= ~INPROGRESS; 7941 wake_worklist(&freeblks->fb_list); 7942 freeblks = NULL; 7943 } 7944 FREE_LOCK(ump); 7945 if (freeblks) 7946 return handle_complete_freeblocks(freeblks, flags); 7947 return (0); 7948 } 7949 7950 /* 7951 * Handle completion of block free via truncate. This allows fs_pending 7952 * to track the actual free block count more closely than if we only updated 7953 * it at the end. We must be careful to handle cases where the block count 7954 * on free was incorrect. 7955 */ 7956 static void 7957 freeblks_free(ump, freeblks, blocks) 7958 struct ufsmount *ump; 7959 struct freeblks *freeblks; 7960 int blocks; 7961 { 7962 struct fs *fs; 7963 ufs2_daddr_t remain; 7964 7965 UFS_LOCK(ump); 7966 remain = -freeblks->fb_chkcnt; 7967 freeblks->fb_chkcnt += blocks; 7968 if (remain > 0) { 7969 if (remain < blocks) 7970 blocks = remain; 7971 fs = ump->um_fs; 7972 fs->fs_pendingblocks -= blocks; 7973 } 7974 UFS_UNLOCK(ump); 7975 } 7976 7977 /* 7978 * Once all of the freework workitems are complete we can retire the 7979 * freeblocks dependency and any journal work awaiting completion. This 7980 * can not be called until all other dependencies are stable on disk. 7981 */ 7982 static int 7983 handle_complete_freeblocks(freeblks, flags) 7984 struct freeblks *freeblks; 7985 int flags; 7986 { 7987 struct inodedep *inodedep; 7988 struct inode *ip; 7989 struct vnode *vp; 7990 struct fs *fs; 7991 struct ufsmount *ump; 7992 ufs2_daddr_t spare; 7993 7994 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7995 fs = ump->um_fs; 7996 flags = LK_EXCLUSIVE | flags; 7997 spare = freeblks->fb_chkcnt; 7998 7999 /* 8000 * If we did not release the expected number of blocks we may have 8001 * to adjust the inode block count here. Only do so if it wasn't 8002 * a truncation to zero and the modrev still matches. 8003 */ 8004 if (spare && freeblks->fb_len != 0) { 8005 if (ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_inum, 8006 flags, &vp, FFSV_FORCEINSMQ) != 0) 8007 return (EBUSY); 8008 ip = VTOI(vp); 8009 if (DIP(ip, i_modrev) == freeblks->fb_modrev) { 8010 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare); 8011 ip->i_flag |= IN_CHANGE; 8012 /* 8013 * We must wait so this happens before the 8014 * journal is reclaimed. 8015 */ 8016 ffs_update(vp, 1); 8017 } 8018 vput(vp); 8019 } 8020 if (spare < 0) { 8021 UFS_LOCK(ump); 8022 fs->fs_pendingblocks += spare; 8023 UFS_UNLOCK(ump); 8024 } 8025 #ifdef QUOTA 8026 /* Handle spare. */ 8027 if (spare) 8028 quotaadj(freeblks->fb_quota, ump, -spare); 8029 quotarele(freeblks->fb_quota); 8030 #endif 8031 ACQUIRE_LOCK(ump); 8032 if (freeblks->fb_state & ONDEPLIST) { 8033 inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum, 8034 0, &inodedep); 8035 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next); 8036 freeblks->fb_state &= ~ONDEPLIST; 8037 if (TAILQ_EMPTY(&inodedep->id_freeblklst)) 8038 free_inodedep(inodedep); 8039 } 8040 /* 8041 * All of the freeblock deps must be complete prior to this call 8042 * so it's now safe to complete earlier outstanding journal entries. 8043 */ 8044 handle_jwork(&freeblks->fb_jwork); 8045 WORKITEM_FREE(freeblks, D_FREEBLKS); 8046 FREE_LOCK(ump); 8047 return (0); 8048 } 8049 8050 /* 8051 * Release blocks associated with the freeblks and stored in the indirect 8052 * block dbn. If level is greater than SINGLE, the block is an indirect block 8053 * and recursive calls to indirtrunc must be used to cleanse other indirect 8054 * blocks. 8055 * 8056 * This handles partial and complete truncation of blocks. Partial is noted 8057 * with goingaway == 0. In this case the freework is completed after the 8058 * zero'd indirects are written to disk. For full truncation the freework 8059 * is completed after the block is freed. 8060 */ 8061 static void 8062 indir_trunc(freework, dbn, lbn) 8063 struct freework *freework; 8064 ufs2_daddr_t dbn; 8065 ufs_lbn_t lbn; 8066 { 8067 struct freework *nfreework; 8068 struct workhead wkhd; 8069 struct freeblks *freeblks; 8070 struct buf *bp; 8071 struct fs *fs; 8072 struct indirdep *indirdep; 8073 struct ufsmount *ump; 8074 ufs1_daddr_t *bap1; 8075 ufs2_daddr_t nb, nnb, *bap2; 8076 ufs_lbn_t lbnadd, nlbn; 8077 int i, nblocks, ufs1fmt; 8078 int freedblocks; 8079 int goingaway; 8080 int freedeps; 8081 int needj; 8082 int level; 8083 int cnt; 8084 8085 freeblks = freework->fw_freeblks; 8086 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8087 fs = ump->um_fs; 8088 /* 8089 * Get buffer of block pointers to be freed. There are three cases: 8090 * 8091 * 1) Partial truncate caches the indirdep pointer in the freework 8092 * which provides us a back copy to the save bp which holds the 8093 * pointers we want to clear. When this completes the zero 8094 * pointers are written to the real copy. 8095 * 2) The indirect is being completely truncated, cancel_indirdep() 8096 * eliminated the real copy and placed the indirdep on the saved 8097 * copy. The indirdep and buf are discarded when this completes. 8098 * 3) The indirect was not in memory, we read a copy off of the disk 8099 * using the devvp and drop and invalidate the buffer when we're 8100 * done. 8101 */ 8102 goingaway = 1; 8103 indirdep = NULL; 8104 if (freework->fw_indir != NULL) { 8105 goingaway = 0; 8106 indirdep = freework->fw_indir; 8107 bp = indirdep->ir_savebp; 8108 if (bp == NULL || bp->b_blkno != dbn) 8109 panic("indir_trunc: Bad saved buf %p blkno %jd", 8110 bp, (intmax_t)dbn); 8111 } else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) { 8112 /* 8113 * The lock prevents the buf dep list from changing and 8114 * indirects on devvp should only ever have one dependency. 8115 */ 8116 indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep)); 8117 if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0) 8118 panic("indir_trunc: Bad indirdep %p from buf %p", 8119 indirdep, bp); 8120 } else if (bread(freeblks->fb_devvp, dbn, (int)fs->fs_bsize, 8121 NOCRED, &bp) != 0) { 8122 brelse(bp); 8123 return; 8124 } 8125 ACQUIRE_LOCK(ump); 8126 /* Protects against a race with complete_trunc_indir(). */ 8127 freework->fw_state &= ~INPROGRESS; 8128 /* 8129 * If we have an indirdep we need to enforce the truncation order 8130 * and discard it when it is complete. 8131 */ 8132 if (indirdep) { 8133 if (freework != TAILQ_FIRST(&indirdep->ir_trunc) && 8134 !TAILQ_EMPTY(&indirdep->ir_trunc)) { 8135 /* 8136 * Add the complete truncate to the list on the 8137 * indirdep to enforce in-order processing. 8138 */ 8139 if (freework->fw_indir == NULL) 8140 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, 8141 freework, fw_next); 8142 FREE_LOCK(ump); 8143 return; 8144 } 8145 /* 8146 * If we're goingaway, free the indirdep. Otherwise it will 8147 * linger until the write completes. 8148 */ 8149 if (goingaway) 8150 free_indirdep(indirdep); 8151 } 8152 FREE_LOCK(ump); 8153 /* Initialize pointers depending on block size. */ 8154 if (ump->um_fstype == UFS1) { 8155 bap1 = (ufs1_daddr_t *)bp->b_data; 8156 nb = bap1[freework->fw_off]; 8157 ufs1fmt = 1; 8158 bap2 = NULL; 8159 } else { 8160 bap2 = (ufs2_daddr_t *)bp->b_data; 8161 nb = bap2[freework->fw_off]; 8162 ufs1fmt = 0; 8163 bap1 = NULL; 8164 } 8165 level = lbn_level(lbn); 8166 needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0; 8167 lbnadd = lbn_offset(fs, level); 8168 nblocks = btodb(fs->fs_bsize); 8169 nfreework = freework; 8170 freedeps = 0; 8171 cnt = 0; 8172 /* 8173 * Reclaim blocks. Traverses into nested indirect levels and 8174 * arranges for the current level to be freed when subordinates 8175 * are free when journaling. 8176 */ 8177 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) { 8178 if (i != NINDIR(fs) - 1) { 8179 if (ufs1fmt) 8180 nnb = bap1[i+1]; 8181 else 8182 nnb = bap2[i+1]; 8183 } else 8184 nnb = 0; 8185 if (nb == 0) 8186 continue; 8187 cnt++; 8188 if (level != 0) { 8189 nlbn = (lbn + 1) - (i * lbnadd); 8190 if (needj != 0) { 8191 nfreework = newfreework(ump, freeblks, freework, 8192 nlbn, nb, fs->fs_frag, 0, 0); 8193 freedeps++; 8194 } 8195 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn); 8196 } else { 8197 struct freedep *freedep; 8198 8199 /* 8200 * Attempt to aggregate freedep dependencies for 8201 * all blocks being released to the same CG. 8202 */ 8203 LIST_INIT(&wkhd); 8204 if (needj != 0 && 8205 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) { 8206 freedep = newfreedep(freework); 8207 WORKLIST_INSERT_UNLOCKED(&wkhd, 8208 &freedep->fd_list); 8209 freedeps++; 8210 } 8211 CTR3(KTR_SUJ, 8212 "indir_trunc: ino %d blkno %jd size %ld", 8213 freeblks->fb_inum, nb, fs->fs_bsize); 8214 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb, 8215 fs->fs_bsize, freeblks->fb_inum, 8216 freeblks->fb_vtype, &wkhd); 8217 } 8218 } 8219 if (goingaway) { 8220 bp->b_flags |= B_INVAL | B_NOCACHE; 8221 brelse(bp); 8222 } 8223 freedblocks = 0; 8224 if (level == 0) 8225 freedblocks = (nblocks * cnt); 8226 if (needj == 0) 8227 freedblocks += nblocks; 8228 freeblks_free(ump, freeblks, freedblocks); 8229 /* 8230 * If we are journaling set up the ref counts and offset so this 8231 * indirect can be completed when its children are free. 8232 */ 8233 if (needj) { 8234 ACQUIRE_LOCK(ump); 8235 freework->fw_off = i; 8236 freework->fw_ref += freedeps; 8237 freework->fw_ref -= NINDIR(fs) + 1; 8238 if (level == 0) 8239 freeblks->fb_cgwait += freedeps; 8240 if (freework->fw_ref == 0) 8241 freework_freeblock(freework); 8242 FREE_LOCK(ump); 8243 return; 8244 } 8245 /* 8246 * If we're not journaling we can free the indirect now. 8247 */ 8248 dbn = dbtofsb(fs, dbn); 8249 CTR3(KTR_SUJ, 8250 "indir_trunc 2: ino %d blkno %jd size %ld", 8251 freeblks->fb_inum, dbn, fs->fs_bsize); 8252 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize, 8253 freeblks->fb_inum, freeblks->fb_vtype, NULL); 8254 /* Non SUJ softdep does single-threaded truncations. */ 8255 if (freework->fw_blkno == dbn) { 8256 freework->fw_state |= ALLCOMPLETE; 8257 ACQUIRE_LOCK(ump); 8258 handle_written_freework(freework); 8259 FREE_LOCK(ump); 8260 } 8261 return; 8262 } 8263 8264 /* 8265 * Cancel an allocindir when it is removed via truncation. When bp is not 8266 * NULL the indirect never appeared on disk and is scheduled to be freed 8267 * independently of the indir so we can more easily track journal work. 8268 */ 8269 static void 8270 cancel_allocindir(aip, bp, freeblks, trunc) 8271 struct allocindir *aip; 8272 struct buf *bp; 8273 struct freeblks *freeblks; 8274 int trunc; 8275 { 8276 struct indirdep *indirdep; 8277 struct freefrag *freefrag; 8278 struct newblk *newblk; 8279 8280 newblk = (struct newblk *)aip; 8281 LIST_REMOVE(aip, ai_next); 8282 /* 8283 * We must eliminate the pointer in bp if it must be freed on its 8284 * own due to partial truncate or pending journal work. 8285 */ 8286 if (bp && (trunc || newblk->nb_jnewblk)) { 8287 /* 8288 * Clear the pointer and mark the aip to be freed 8289 * directly if it never existed on disk. 8290 */ 8291 aip->ai_state |= DELAYEDFREE; 8292 indirdep = aip->ai_indirdep; 8293 if (indirdep->ir_state & UFS1FMT) 8294 ((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8295 else 8296 ((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8297 } 8298 /* 8299 * When truncating the previous pointer will be freed via 8300 * savedbp. Eliminate the freefrag which would dup free. 8301 */ 8302 if (trunc && (freefrag = newblk->nb_freefrag) != NULL) { 8303 newblk->nb_freefrag = NULL; 8304 if (freefrag->ff_jdep) 8305 cancel_jfreefrag( 8306 WK_JFREEFRAG(freefrag->ff_jdep)); 8307 jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork); 8308 WORKITEM_FREE(freefrag, D_FREEFRAG); 8309 } 8310 /* 8311 * If the journal hasn't been written the jnewblk must be passed 8312 * to the call to ffs_blkfree that reclaims the space. We accomplish 8313 * this by leaving the journal dependency on the newblk to be freed 8314 * when a freework is created in handle_workitem_freeblocks(). 8315 */ 8316 cancel_newblk(newblk, NULL, &freeblks->fb_jwork); 8317 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 8318 } 8319 8320 /* 8321 * Create the mkdir dependencies for . and .. in a new directory. Link them 8322 * in to a newdirblk so any subsequent additions are tracked properly. The 8323 * caller is responsible for adding the mkdir1 dependency to the journal 8324 * and updating id_mkdiradd. This function returns with the per-filesystem 8325 * lock held. 8326 */ 8327 static struct mkdir * 8328 setup_newdir(dap, newinum, dinum, newdirbp, mkdirp) 8329 struct diradd *dap; 8330 ino_t newinum; 8331 ino_t dinum; 8332 struct buf *newdirbp; 8333 struct mkdir **mkdirp; 8334 { 8335 struct newblk *newblk; 8336 struct pagedep *pagedep; 8337 struct inodedep *inodedep; 8338 struct newdirblk *newdirblk; 8339 struct mkdir *mkdir1, *mkdir2; 8340 struct worklist *wk; 8341 struct jaddref *jaddref; 8342 struct ufsmount *ump; 8343 struct mount *mp; 8344 8345 mp = dap->da_list.wk_mp; 8346 ump = VFSTOUFS(mp); 8347 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK, 8348 M_SOFTDEP_FLAGS); 8349 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8350 LIST_INIT(&newdirblk->db_mkdir); 8351 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8352 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp); 8353 mkdir1->md_state = ATTACHED | MKDIR_BODY; 8354 mkdir1->md_diradd = dap; 8355 mkdir1->md_jaddref = NULL; 8356 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8357 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp); 8358 mkdir2->md_state = ATTACHED | MKDIR_PARENT; 8359 mkdir2->md_diradd = dap; 8360 mkdir2->md_jaddref = NULL; 8361 if (MOUNTEDSUJ(mp) == 0) { 8362 mkdir1->md_state |= DEPCOMPLETE; 8363 mkdir2->md_state |= DEPCOMPLETE; 8364 } 8365 /* 8366 * Dependency on "." and ".." being written to disk. 8367 */ 8368 mkdir1->md_buf = newdirbp; 8369 ACQUIRE_LOCK(VFSTOUFS(mp)); 8370 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir1, md_mkdirs); 8371 /* 8372 * We must link the pagedep, allocdirect, and newdirblk for 8373 * the initial file page so the pointer to the new directory 8374 * is not written until the directory contents are live and 8375 * any subsequent additions are not marked live until the 8376 * block is reachable via the inode. 8377 */ 8378 if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0) 8379 panic("setup_newdir: lost pagedep"); 8380 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list) 8381 if (wk->wk_type == D_ALLOCDIRECT) 8382 break; 8383 if (wk == NULL) 8384 panic("setup_newdir: lost allocdirect"); 8385 if (pagedep->pd_state & NEWBLOCK) 8386 panic("setup_newdir: NEWBLOCK already set"); 8387 newblk = WK_NEWBLK(wk); 8388 pagedep->pd_state |= NEWBLOCK; 8389 pagedep->pd_newdirblk = newdirblk; 8390 newdirblk->db_pagedep = pagedep; 8391 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8392 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list); 8393 /* 8394 * Look up the inodedep for the parent directory so that we 8395 * can link mkdir2 into the pending dotdot jaddref or 8396 * the inode write if there is none. If the inode is 8397 * ALLCOMPLETE and no jaddref is present all dependencies have 8398 * been satisfied and mkdir2 can be freed. 8399 */ 8400 inodedep_lookup(mp, dinum, 0, &inodedep); 8401 if (MOUNTEDSUJ(mp)) { 8402 if (inodedep == NULL) 8403 panic("setup_newdir: Lost parent."); 8404 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8405 inoreflst); 8406 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum && 8407 (jaddref->ja_state & MKDIR_PARENT), 8408 ("setup_newdir: bad dotdot jaddref %p", jaddref)); 8409 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8410 mkdir2->md_jaddref = jaddref; 8411 jaddref->ja_mkdir = mkdir2; 8412 } else if (inodedep == NULL || 8413 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 8414 dap->da_state &= ~MKDIR_PARENT; 8415 WORKITEM_FREE(mkdir2, D_MKDIR); 8416 mkdir2 = NULL; 8417 } else { 8418 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8419 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list); 8420 } 8421 *mkdirp = mkdir2; 8422 8423 return (mkdir1); 8424 } 8425 8426 /* 8427 * Directory entry addition dependencies. 8428 * 8429 * When adding a new directory entry, the inode (with its incremented link 8430 * count) must be written to disk before the directory entry's pointer to it. 8431 * Also, if the inode is newly allocated, the corresponding freemap must be 8432 * updated (on disk) before the directory entry's pointer. These requirements 8433 * are met via undo/redo on the directory entry's pointer, which consists 8434 * simply of the inode number. 8435 * 8436 * As directory entries are added and deleted, the free space within a 8437 * directory block can become fragmented. The ufs filesystem will compact 8438 * a fragmented directory block to make space for a new entry. When this 8439 * occurs, the offsets of previously added entries change. Any "diradd" 8440 * dependency structures corresponding to these entries must be updated with 8441 * the new offsets. 8442 */ 8443 8444 /* 8445 * This routine is called after the in-memory inode's link 8446 * count has been incremented, but before the directory entry's 8447 * pointer to the inode has been set. 8448 */ 8449 int 8450 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 8451 struct buf *bp; /* buffer containing directory block */ 8452 struct inode *dp; /* inode for directory */ 8453 off_t diroffset; /* offset of new entry in directory */ 8454 ino_t newinum; /* inode referenced by new directory entry */ 8455 struct buf *newdirbp; /* non-NULL => contents of new mkdir */ 8456 int isnewblk; /* entry is in a newly allocated block */ 8457 { 8458 int offset; /* offset of new entry within directory block */ 8459 ufs_lbn_t lbn; /* block in directory containing new entry */ 8460 struct fs *fs; 8461 struct diradd *dap; 8462 struct newblk *newblk; 8463 struct pagedep *pagedep; 8464 struct inodedep *inodedep; 8465 struct newdirblk *newdirblk; 8466 struct mkdir *mkdir1, *mkdir2; 8467 struct jaddref *jaddref; 8468 struct ufsmount *ump; 8469 struct mount *mp; 8470 int isindir; 8471 8472 mp = ITOVFS(dp); 8473 ump = VFSTOUFS(mp); 8474 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 8475 ("softdep_setup_directory_add called on non-softdep filesystem")); 8476 /* 8477 * Whiteouts have no dependencies. 8478 */ 8479 if (newinum == UFS_WINO) { 8480 if (newdirbp != NULL) 8481 bdwrite(newdirbp); 8482 return (0); 8483 } 8484 jaddref = NULL; 8485 mkdir1 = mkdir2 = NULL; 8486 fs = ump->um_fs; 8487 lbn = lblkno(fs, diroffset); 8488 offset = blkoff(fs, diroffset); 8489 dap = malloc(sizeof(struct diradd), M_DIRADD, 8490 M_SOFTDEP_FLAGS|M_ZERO); 8491 workitem_alloc(&dap->da_list, D_DIRADD, mp); 8492 dap->da_offset = offset; 8493 dap->da_newinum = newinum; 8494 dap->da_state = ATTACHED; 8495 LIST_INIT(&dap->da_jwork); 8496 isindir = bp->b_lblkno >= UFS_NDADDR; 8497 newdirblk = NULL; 8498 if (isnewblk && 8499 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) { 8500 newdirblk = malloc(sizeof(struct newdirblk), 8501 M_NEWDIRBLK, M_SOFTDEP_FLAGS); 8502 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8503 LIST_INIT(&newdirblk->db_mkdir); 8504 } 8505 /* 8506 * If we're creating a new directory setup the dependencies and set 8507 * the dap state to wait for them. Otherwise it's COMPLETE and 8508 * we can move on. 8509 */ 8510 if (newdirbp == NULL) { 8511 dap->da_state |= DEPCOMPLETE; 8512 ACQUIRE_LOCK(ump); 8513 } else { 8514 dap->da_state |= MKDIR_BODY | MKDIR_PARENT; 8515 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp, 8516 &mkdir2); 8517 } 8518 /* 8519 * Link into parent directory pagedep to await its being written. 8520 */ 8521 pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep); 8522 #ifdef DEBUG 8523 if (diradd_lookup(pagedep, offset) != NULL) 8524 panic("softdep_setup_directory_add: %p already at off %d\n", 8525 diradd_lookup(pagedep, offset), offset); 8526 #endif 8527 dap->da_pagedep = pagedep; 8528 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap, 8529 da_pdlist); 8530 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 8531 /* 8532 * If we're journaling, link the diradd into the jaddref so it 8533 * may be completed after the journal entry is written. Otherwise, 8534 * link the diradd into its inodedep. If the inode is not yet 8535 * written place it on the bufwait list, otherwise do the post-inode 8536 * write processing to put it on the id_pendinghd list. 8537 */ 8538 if (MOUNTEDSUJ(mp)) { 8539 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8540 inoreflst); 8541 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 8542 ("softdep_setup_directory_add: bad jaddref %p", jaddref)); 8543 jaddref->ja_diroff = diroffset; 8544 jaddref->ja_diradd = dap; 8545 add_to_journal(&jaddref->ja_list); 8546 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) 8547 diradd_inode_written(dap, inodedep); 8548 else 8549 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 8550 /* 8551 * Add the journal entries for . and .. links now that the primary 8552 * link is written. 8553 */ 8554 if (mkdir1 != NULL && MOUNTEDSUJ(mp)) { 8555 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 8556 inoreflst, if_deps); 8557 KASSERT(jaddref != NULL && 8558 jaddref->ja_ino == jaddref->ja_parent && 8559 (jaddref->ja_state & MKDIR_BODY), 8560 ("softdep_setup_directory_add: bad dot jaddref %p", 8561 jaddref)); 8562 mkdir1->md_jaddref = jaddref; 8563 jaddref->ja_mkdir = mkdir1; 8564 /* 8565 * It is important that the dotdot journal entry 8566 * is added prior to the dot entry since dot writes 8567 * both the dot and dotdot links. These both must 8568 * be added after the primary link for the journal 8569 * to remain consistent. 8570 */ 8571 add_to_journal(&mkdir2->md_jaddref->ja_list); 8572 add_to_journal(&jaddref->ja_list); 8573 } 8574 /* 8575 * If we are adding a new directory remember this diradd so that if 8576 * we rename it we can keep the dot and dotdot dependencies. If 8577 * we are adding a new name for an inode that has a mkdiradd we 8578 * must be in rename and we have to move the dot and dotdot 8579 * dependencies to this new name. The old name is being orphaned 8580 * soon. 8581 */ 8582 if (mkdir1 != NULL) { 8583 if (inodedep->id_mkdiradd != NULL) 8584 panic("softdep_setup_directory_add: Existing mkdir"); 8585 inodedep->id_mkdiradd = dap; 8586 } else if (inodedep->id_mkdiradd) 8587 merge_diradd(inodedep, dap); 8588 if (newdirblk != NULL) { 8589 /* 8590 * There is nothing to do if we are already tracking 8591 * this block. 8592 */ 8593 if ((pagedep->pd_state & NEWBLOCK) != 0) { 8594 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 8595 FREE_LOCK(ump); 8596 return (0); 8597 } 8598 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk) 8599 == 0) 8600 panic("softdep_setup_directory_add: lost entry"); 8601 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8602 pagedep->pd_state |= NEWBLOCK; 8603 pagedep->pd_newdirblk = newdirblk; 8604 newdirblk->db_pagedep = pagedep; 8605 FREE_LOCK(ump); 8606 /* 8607 * If we extended into an indirect signal direnter to sync. 8608 */ 8609 if (isindir) 8610 return (1); 8611 return (0); 8612 } 8613 FREE_LOCK(ump); 8614 return (0); 8615 } 8616 8617 /* 8618 * This procedure is called to change the offset of a directory 8619 * entry when compacting a directory block which must be owned 8620 * exclusively by the caller. Note that the actual entry movement 8621 * must be done in this procedure to ensure that no I/O completions 8622 * occur while the move is in progress. 8623 */ 8624 void 8625 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 8626 struct buf *bp; /* Buffer holding directory block. */ 8627 struct inode *dp; /* inode for directory */ 8628 caddr_t base; /* address of dp->i_offset */ 8629 caddr_t oldloc; /* address of old directory location */ 8630 caddr_t newloc; /* address of new directory location */ 8631 int entrysize; /* size of directory entry */ 8632 { 8633 int offset, oldoffset, newoffset; 8634 struct pagedep *pagedep; 8635 struct jmvref *jmvref; 8636 struct diradd *dap; 8637 struct direct *de; 8638 struct mount *mp; 8639 struct ufsmount *ump; 8640 ufs_lbn_t lbn; 8641 int flags; 8642 8643 mp = ITOVFS(dp); 8644 ump = VFSTOUFS(mp); 8645 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 8646 ("softdep_change_directoryentry_offset called on " 8647 "non-softdep filesystem")); 8648 de = (struct direct *)oldloc; 8649 jmvref = NULL; 8650 flags = 0; 8651 /* 8652 * Moves are always journaled as it would be too complex to 8653 * determine if any affected adds or removes are present in the 8654 * journal. 8655 */ 8656 if (MOUNTEDSUJ(mp)) { 8657 flags = DEPALLOC; 8658 jmvref = newjmvref(dp, de->d_ino, 8659 dp->i_offset + (oldloc - base), 8660 dp->i_offset + (newloc - base)); 8661 } 8662 lbn = lblkno(ump->um_fs, dp->i_offset); 8663 offset = blkoff(ump->um_fs, dp->i_offset); 8664 oldoffset = offset + (oldloc - base); 8665 newoffset = offset + (newloc - base); 8666 ACQUIRE_LOCK(ump); 8667 if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0) 8668 goto done; 8669 dap = diradd_lookup(pagedep, oldoffset); 8670 if (dap) { 8671 dap->da_offset = newoffset; 8672 newoffset = DIRADDHASH(newoffset); 8673 oldoffset = DIRADDHASH(oldoffset); 8674 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE && 8675 newoffset != oldoffset) { 8676 LIST_REMOVE(dap, da_pdlist); 8677 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset], 8678 dap, da_pdlist); 8679 } 8680 } 8681 done: 8682 if (jmvref) { 8683 jmvref->jm_pagedep = pagedep; 8684 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps); 8685 add_to_journal(&jmvref->jm_list); 8686 } 8687 bcopy(oldloc, newloc, entrysize); 8688 FREE_LOCK(ump); 8689 } 8690 8691 /* 8692 * Move the mkdir dependencies and journal work from one diradd to another 8693 * when renaming a directory. The new name must depend on the mkdir deps 8694 * completing as the old name did. Directories can only have one valid link 8695 * at a time so one must be canonical. 8696 */ 8697 static void 8698 merge_diradd(inodedep, newdap) 8699 struct inodedep *inodedep; 8700 struct diradd *newdap; 8701 { 8702 struct diradd *olddap; 8703 struct mkdir *mkdir, *nextmd; 8704 struct ufsmount *ump; 8705 short state; 8706 8707 olddap = inodedep->id_mkdiradd; 8708 inodedep->id_mkdiradd = newdap; 8709 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8710 newdap->da_state &= ~DEPCOMPLETE; 8711 ump = VFSTOUFS(inodedep->id_list.wk_mp); 8712 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 8713 mkdir = nextmd) { 8714 nextmd = LIST_NEXT(mkdir, md_mkdirs); 8715 if (mkdir->md_diradd != olddap) 8716 continue; 8717 mkdir->md_diradd = newdap; 8718 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY); 8719 newdap->da_state |= state; 8720 olddap->da_state &= ~state; 8721 if ((olddap->da_state & 8722 (MKDIR_PARENT | MKDIR_BODY)) == 0) 8723 break; 8724 } 8725 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 8726 panic("merge_diradd: unfound ref"); 8727 } 8728 /* 8729 * Any mkdir related journal items are not safe to be freed until 8730 * the new name is stable. 8731 */ 8732 jwork_move(&newdap->da_jwork, &olddap->da_jwork); 8733 olddap->da_state |= DEPCOMPLETE; 8734 complete_diradd(olddap); 8735 } 8736 8737 /* 8738 * Move the diradd to the pending list when all diradd dependencies are 8739 * complete. 8740 */ 8741 static void 8742 complete_diradd(dap) 8743 struct diradd *dap; 8744 { 8745 struct pagedep *pagedep; 8746 8747 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 8748 if (dap->da_state & DIRCHG) 8749 pagedep = dap->da_previous->dm_pagedep; 8750 else 8751 pagedep = dap->da_pagedep; 8752 LIST_REMOVE(dap, da_pdlist); 8753 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 8754 } 8755 } 8756 8757 /* 8758 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal 8759 * add entries and conditonally journal the remove. 8760 */ 8761 static void 8762 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref) 8763 struct diradd *dap; 8764 struct dirrem *dirrem; 8765 struct jremref *jremref; 8766 struct jremref *dotremref; 8767 struct jremref *dotdotremref; 8768 { 8769 struct inodedep *inodedep; 8770 struct jaddref *jaddref; 8771 struct inoref *inoref; 8772 struct ufsmount *ump; 8773 struct mkdir *mkdir; 8774 8775 /* 8776 * If no remove references were allocated we're on a non-journaled 8777 * filesystem and can skip the cancel step. 8778 */ 8779 if (jremref == NULL) { 8780 free_diradd(dap, NULL); 8781 return; 8782 } 8783 /* 8784 * Cancel the primary name an free it if it does not require 8785 * journaling. 8786 */ 8787 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum, 8788 0, &inodedep) != 0) { 8789 /* Abort the addref that reference this diradd. */ 8790 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 8791 if (inoref->if_list.wk_type != D_JADDREF) 8792 continue; 8793 jaddref = (struct jaddref *)inoref; 8794 if (jaddref->ja_diradd != dap) 8795 continue; 8796 if (cancel_jaddref(jaddref, inodedep, 8797 &dirrem->dm_jwork) == 0) { 8798 free_jremref(jremref); 8799 jremref = NULL; 8800 } 8801 break; 8802 } 8803 } 8804 /* 8805 * Cancel subordinate names and free them if they do not require 8806 * journaling. 8807 */ 8808 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8809 ump = VFSTOUFS(dap->da_list.wk_mp); 8810 LIST_FOREACH(mkdir, &ump->softdep_mkdirlisthd, md_mkdirs) { 8811 if (mkdir->md_diradd != dap) 8812 continue; 8813 if ((jaddref = mkdir->md_jaddref) == NULL) 8814 continue; 8815 mkdir->md_jaddref = NULL; 8816 if (mkdir->md_state & MKDIR_PARENT) { 8817 if (cancel_jaddref(jaddref, NULL, 8818 &dirrem->dm_jwork) == 0) { 8819 free_jremref(dotdotremref); 8820 dotdotremref = NULL; 8821 } 8822 } else { 8823 if (cancel_jaddref(jaddref, inodedep, 8824 &dirrem->dm_jwork) == 0) { 8825 free_jremref(dotremref); 8826 dotremref = NULL; 8827 } 8828 } 8829 } 8830 } 8831 8832 if (jremref) 8833 journal_jremref(dirrem, jremref, inodedep); 8834 if (dotremref) 8835 journal_jremref(dirrem, dotremref, inodedep); 8836 if (dotdotremref) 8837 journal_jremref(dirrem, dotdotremref, NULL); 8838 jwork_move(&dirrem->dm_jwork, &dap->da_jwork); 8839 free_diradd(dap, &dirrem->dm_jwork); 8840 } 8841 8842 /* 8843 * Free a diradd dependency structure. This routine must be called 8844 * with splbio interrupts blocked. 8845 */ 8846 static void 8847 free_diradd(dap, wkhd) 8848 struct diradd *dap; 8849 struct workhead *wkhd; 8850 { 8851 struct dirrem *dirrem; 8852 struct pagedep *pagedep; 8853 struct inodedep *inodedep; 8854 struct mkdir *mkdir, *nextmd; 8855 struct ufsmount *ump; 8856 8857 ump = VFSTOUFS(dap->da_list.wk_mp); 8858 LOCK_OWNED(ump); 8859 LIST_REMOVE(dap, da_pdlist); 8860 if (dap->da_state & ONWORKLIST) 8861 WORKLIST_REMOVE(&dap->da_list); 8862 if ((dap->da_state & DIRCHG) == 0) { 8863 pagedep = dap->da_pagedep; 8864 } else { 8865 dirrem = dap->da_previous; 8866 pagedep = dirrem->dm_pagedep; 8867 dirrem->dm_dirinum = pagedep->pd_ino; 8868 dirrem->dm_state |= COMPLETE; 8869 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 8870 add_to_worklist(&dirrem->dm_list, 0); 8871 } 8872 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum, 8873 0, &inodedep) != 0) 8874 if (inodedep->id_mkdiradd == dap) 8875 inodedep->id_mkdiradd = NULL; 8876 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8877 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 8878 mkdir = nextmd) { 8879 nextmd = LIST_NEXT(mkdir, md_mkdirs); 8880 if (mkdir->md_diradd != dap) 8881 continue; 8882 dap->da_state &= 8883 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 8884 LIST_REMOVE(mkdir, md_mkdirs); 8885 if (mkdir->md_state & ONWORKLIST) 8886 WORKLIST_REMOVE(&mkdir->md_list); 8887 if (mkdir->md_jaddref != NULL) 8888 panic("free_diradd: Unexpected jaddref"); 8889 WORKITEM_FREE(mkdir, D_MKDIR); 8890 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) 8891 break; 8892 } 8893 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 8894 panic("free_diradd: unfound ref"); 8895 } 8896 if (inodedep) 8897 free_inodedep(inodedep); 8898 /* 8899 * Free any journal segments waiting for the directory write. 8900 */ 8901 handle_jwork(&dap->da_jwork); 8902 WORKITEM_FREE(dap, D_DIRADD); 8903 } 8904 8905 /* 8906 * Directory entry removal dependencies. 8907 * 8908 * When removing a directory entry, the entry's inode pointer must be 8909 * zero'ed on disk before the corresponding inode's link count is decremented 8910 * (possibly freeing the inode for re-use). This dependency is handled by 8911 * updating the directory entry but delaying the inode count reduction until 8912 * after the directory block has been written to disk. After this point, the 8913 * inode count can be decremented whenever it is convenient. 8914 */ 8915 8916 /* 8917 * This routine should be called immediately after removing 8918 * a directory entry. The inode's link count should not be 8919 * decremented by the calling procedure -- the soft updates 8920 * code will do this task when it is safe. 8921 */ 8922 void 8923 softdep_setup_remove(bp, dp, ip, isrmdir) 8924 struct buf *bp; /* buffer containing directory block */ 8925 struct inode *dp; /* inode for the directory being modified */ 8926 struct inode *ip; /* inode for directory entry being removed */ 8927 int isrmdir; /* indicates if doing RMDIR */ 8928 { 8929 struct dirrem *dirrem, *prevdirrem; 8930 struct inodedep *inodedep; 8931 struct ufsmount *ump; 8932 int direct; 8933 8934 ump = ITOUMP(ip); 8935 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 8936 ("softdep_setup_remove called on non-softdep filesystem")); 8937 /* 8938 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want 8939 * newdirrem() to setup the full directory remove which requires 8940 * isrmdir > 1. 8941 */ 8942 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 8943 /* 8944 * Add the dirrem to the inodedep's pending remove list for quick 8945 * discovery later. 8946 */ 8947 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) 8948 panic("softdep_setup_remove: Lost inodedep."); 8949 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 8950 dirrem->dm_state |= ONDEPLIST; 8951 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 8952 8953 /* 8954 * If the COMPLETE flag is clear, then there were no active 8955 * entries and we want to roll back to a zeroed entry until 8956 * the new inode is committed to disk. If the COMPLETE flag is 8957 * set then we have deleted an entry that never made it to 8958 * disk. If the entry we deleted resulted from a name change, 8959 * then the old name still resides on disk. We cannot delete 8960 * its inode (returned to us in prevdirrem) until the zeroed 8961 * directory entry gets to disk. The new inode has never been 8962 * referenced on the disk, so can be deleted immediately. 8963 */ 8964 if ((dirrem->dm_state & COMPLETE) == 0) { 8965 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem, 8966 dm_next); 8967 FREE_LOCK(ump); 8968 } else { 8969 if (prevdirrem != NULL) 8970 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, 8971 prevdirrem, dm_next); 8972 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino; 8973 direct = LIST_EMPTY(&dirrem->dm_jremrefhd); 8974 FREE_LOCK(ump); 8975 if (direct) 8976 handle_workitem_remove(dirrem, 0); 8977 } 8978 } 8979 8980 /* 8981 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the 8982 * pd_pendinghd list of a pagedep. 8983 */ 8984 static struct diradd * 8985 diradd_lookup(pagedep, offset) 8986 struct pagedep *pagedep; 8987 int offset; 8988 { 8989 struct diradd *dap; 8990 8991 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist) 8992 if (dap->da_offset == offset) 8993 return (dap); 8994 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 8995 if (dap->da_offset == offset) 8996 return (dap); 8997 return (NULL); 8998 } 8999 9000 /* 9001 * Search for a .. diradd dependency in a directory that is being removed. 9002 * If the directory was renamed to a new parent we have a diradd rather 9003 * than a mkdir for the .. entry. We need to cancel it now before 9004 * it is found in truncate(). 9005 */ 9006 static struct jremref * 9007 cancel_diradd_dotdot(ip, dirrem, jremref) 9008 struct inode *ip; 9009 struct dirrem *dirrem; 9010 struct jremref *jremref; 9011 { 9012 struct pagedep *pagedep; 9013 struct diradd *dap; 9014 struct worklist *wk; 9015 9016 if (pagedep_lookup(ITOVFS(ip), NULL, ip->i_number, 0, 0, &pagedep) == 0) 9017 return (jremref); 9018 dap = diradd_lookup(pagedep, DOTDOT_OFFSET); 9019 if (dap == NULL) 9020 return (jremref); 9021 cancel_diradd(dap, dirrem, jremref, NULL, NULL); 9022 /* 9023 * Mark any journal work as belonging to the parent so it is freed 9024 * with the .. reference. 9025 */ 9026 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9027 wk->wk_state |= MKDIR_PARENT; 9028 return (NULL); 9029 } 9030 9031 /* 9032 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to 9033 * replace it with a dirrem/diradd pair as a result of re-parenting a 9034 * directory. This ensures that we don't simultaneously have a mkdir and 9035 * a diradd for the same .. entry. 9036 */ 9037 static struct jremref * 9038 cancel_mkdir_dotdot(ip, dirrem, jremref) 9039 struct inode *ip; 9040 struct dirrem *dirrem; 9041 struct jremref *jremref; 9042 { 9043 struct inodedep *inodedep; 9044 struct jaddref *jaddref; 9045 struct ufsmount *ump; 9046 struct mkdir *mkdir; 9047 struct diradd *dap; 9048 struct mount *mp; 9049 9050 mp = ITOVFS(ip); 9051 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9052 return (jremref); 9053 dap = inodedep->id_mkdiradd; 9054 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0) 9055 return (jremref); 9056 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9057 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 9058 mkdir = LIST_NEXT(mkdir, md_mkdirs)) 9059 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT) 9060 break; 9061 if (mkdir == NULL) 9062 panic("cancel_mkdir_dotdot: Unable to find mkdir\n"); 9063 if ((jaddref = mkdir->md_jaddref) != NULL) { 9064 mkdir->md_jaddref = NULL; 9065 jaddref->ja_state &= ~MKDIR_PARENT; 9066 if (inodedep_lookup(mp, jaddref->ja_ino, 0, &inodedep) == 0) 9067 panic("cancel_mkdir_dotdot: Lost parent inodedep"); 9068 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) { 9069 journal_jremref(dirrem, jremref, inodedep); 9070 jremref = NULL; 9071 } 9072 } 9073 if (mkdir->md_state & ONWORKLIST) 9074 WORKLIST_REMOVE(&mkdir->md_list); 9075 mkdir->md_state |= ALLCOMPLETE; 9076 complete_mkdir(mkdir); 9077 return (jremref); 9078 } 9079 9080 static void 9081 journal_jremref(dirrem, jremref, inodedep) 9082 struct dirrem *dirrem; 9083 struct jremref *jremref; 9084 struct inodedep *inodedep; 9085 { 9086 9087 if (inodedep == NULL) 9088 if (inodedep_lookup(jremref->jr_list.wk_mp, 9089 jremref->jr_ref.if_ino, 0, &inodedep) == 0) 9090 panic("journal_jremref: Lost inodedep"); 9091 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps); 9092 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 9093 add_to_journal(&jremref->jr_list); 9094 } 9095 9096 static void 9097 dirrem_journal(dirrem, jremref, dotremref, dotdotremref) 9098 struct dirrem *dirrem; 9099 struct jremref *jremref; 9100 struct jremref *dotremref; 9101 struct jremref *dotdotremref; 9102 { 9103 struct inodedep *inodedep; 9104 9105 9106 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0, 9107 &inodedep) == 0) 9108 panic("dirrem_journal: Lost inodedep"); 9109 journal_jremref(dirrem, jremref, inodedep); 9110 if (dotremref) 9111 journal_jremref(dirrem, dotremref, inodedep); 9112 if (dotdotremref) 9113 journal_jremref(dirrem, dotdotremref, NULL); 9114 } 9115 9116 /* 9117 * Allocate a new dirrem if appropriate and return it along with 9118 * its associated pagedep. Called without a lock, returns with lock. 9119 */ 9120 static struct dirrem * 9121 newdirrem(bp, dp, ip, isrmdir, prevdirremp) 9122 struct buf *bp; /* buffer containing directory block */ 9123 struct inode *dp; /* inode for the directory being modified */ 9124 struct inode *ip; /* inode for directory entry being removed */ 9125 int isrmdir; /* indicates if doing RMDIR */ 9126 struct dirrem **prevdirremp; /* previously referenced inode, if any */ 9127 { 9128 int offset; 9129 ufs_lbn_t lbn; 9130 struct diradd *dap; 9131 struct dirrem *dirrem; 9132 struct pagedep *pagedep; 9133 struct jremref *jremref; 9134 struct jremref *dotremref; 9135 struct jremref *dotdotremref; 9136 struct vnode *dvp; 9137 struct ufsmount *ump; 9138 9139 /* 9140 * Whiteouts have no deletion dependencies. 9141 */ 9142 if (ip == NULL) 9143 panic("newdirrem: whiteout"); 9144 dvp = ITOV(dp); 9145 ump = ITOUMP(dp); 9146 9147 /* 9148 * If the system is over its limit and our filesystem is 9149 * responsible for more than our share of that usage and 9150 * we are not a snapshot, request some inodedep cleanup. 9151 * Limiting the number of dirrem structures will also limit 9152 * the number of freefile and freeblks structures. 9153 */ 9154 ACQUIRE_LOCK(ump); 9155 if (!IS_SNAPSHOT(ip) && softdep_excess_items(ump, D_DIRREM)) 9156 schedule_cleanup(UFSTOVFS(ump)); 9157 else 9158 FREE_LOCK(ump); 9159 dirrem = malloc(sizeof(struct dirrem), M_DIRREM, M_SOFTDEP_FLAGS | 9160 M_ZERO); 9161 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount); 9162 LIST_INIT(&dirrem->dm_jremrefhd); 9163 LIST_INIT(&dirrem->dm_jwork); 9164 dirrem->dm_state = isrmdir ? RMDIR : 0; 9165 dirrem->dm_oldinum = ip->i_number; 9166 *prevdirremp = NULL; 9167 /* 9168 * Allocate remove reference structures to track journal write 9169 * dependencies. We will always have one for the link and 9170 * when doing directories we will always have one more for dot. 9171 * When renaming a directory we skip the dotdot link change so 9172 * this is not needed. 9173 */ 9174 jremref = dotremref = dotdotremref = NULL; 9175 if (DOINGSUJ(dvp)) { 9176 if (isrmdir) { 9177 jremref = newjremref(dirrem, dp, ip, dp->i_offset, 9178 ip->i_effnlink + 2); 9179 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET, 9180 ip->i_effnlink + 1); 9181 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET, 9182 dp->i_effnlink + 1); 9183 dotdotremref->jr_state |= MKDIR_PARENT; 9184 } else 9185 jremref = newjremref(dirrem, dp, ip, dp->i_offset, 9186 ip->i_effnlink + 1); 9187 } 9188 ACQUIRE_LOCK(ump); 9189 lbn = lblkno(ump->um_fs, dp->i_offset); 9190 offset = blkoff(ump->um_fs, dp->i_offset); 9191 pagedep_lookup(UFSTOVFS(ump), bp, dp->i_number, lbn, DEPALLOC, 9192 &pagedep); 9193 dirrem->dm_pagedep = pagedep; 9194 dirrem->dm_offset = offset; 9195 /* 9196 * If we're renaming a .. link to a new directory, cancel any 9197 * existing MKDIR_PARENT mkdir. If it has already been canceled 9198 * the jremref is preserved for any potential diradd in this 9199 * location. This can not coincide with a rmdir. 9200 */ 9201 if (dp->i_offset == DOTDOT_OFFSET) { 9202 if (isrmdir) 9203 panic("newdirrem: .. directory change during remove?"); 9204 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref); 9205 } 9206 /* 9207 * If we're removing a directory search for the .. dependency now and 9208 * cancel it. Any pending journal work will be added to the dirrem 9209 * to be completed when the workitem remove completes. 9210 */ 9211 if (isrmdir) 9212 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref); 9213 /* 9214 * Check for a diradd dependency for the same directory entry. 9215 * If present, then both dependencies become obsolete and can 9216 * be de-allocated. 9217 */ 9218 dap = diradd_lookup(pagedep, offset); 9219 if (dap == NULL) { 9220 /* 9221 * Link the jremref structures into the dirrem so they are 9222 * written prior to the pagedep. 9223 */ 9224 if (jremref) 9225 dirrem_journal(dirrem, jremref, dotremref, 9226 dotdotremref); 9227 return (dirrem); 9228 } 9229 /* 9230 * Must be ATTACHED at this point. 9231 */ 9232 if ((dap->da_state & ATTACHED) == 0) 9233 panic("newdirrem: not ATTACHED"); 9234 if (dap->da_newinum != ip->i_number) 9235 panic("newdirrem: inum %ju should be %ju", 9236 (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum); 9237 /* 9238 * If we are deleting a changed name that never made it to disk, 9239 * then return the dirrem describing the previous inode (which 9240 * represents the inode currently referenced from this entry on disk). 9241 */ 9242 if ((dap->da_state & DIRCHG) != 0) { 9243 *prevdirremp = dap->da_previous; 9244 dap->da_state &= ~DIRCHG; 9245 dap->da_pagedep = pagedep; 9246 } 9247 /* 9248 * We are deleting an entry that never made it to disk. 9249 * Mark it COMPLETE so we can delete its inode immediately. 9250 */ 9251 dirrem->dm_state |= COMPLETE; 9252 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref); 9253 #ifdef SUJ_DEBUG 9254 if (isrmdir == 0) { 9255 struct worklist *wk; 9256 9257 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9258 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT)) 9259 panic("bad wk %p (0x%X)\n", wk, wk->wk_state); 9260 } 9261 #endif 9262 9263 return (dirrem); 9264 } 9265 9266 /* 9267 * Directory entry change dependencies. 9268 * 9269 * Changing an existing directory entry requires that an add operation 9270 * be completed first followed by a deletion. The semantics for the addition 9271 * are identical to the description of adding a new entry above except 9272 * that the rollback is to the old inode number rather than zero. Once 9273 * the addition dependency is completed, the removal is done as described 9274 * in the removal routine above. 9275 */ 9276 9277 /* 9278 * This routine should be called immediately after changing 9279 * a directory entry. The inode's link count should not be 9280 * decremented by the calling procedure -- the soft updates 9281 * code will perform this task when it is safe. 9282 */ 9283 void 9284 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 9285 struct buf *bp; /* buffer containing directory block */ 9286 struct inode *dp; /* inode for the directory being modified */ 9287 struct inode *ip; /* inode for directory entry being removed */ 9288 ino_t newinum; /* new inode number for changed entry */ 9289 int isrmdir; /* indicates if doing RMDIR */ 9290 { 9291 int offset; 9292 struct diradd *dap = NULL; 9293 struct dirrem *dirrem, *prevdirrem; 9294 struct pagedep *pagedep; 9295 struct inodedep *inodedep; 9296 struct jaddref *jaddref; 9297 struct mount *mp; 9298 struct ufsmount *ump; 9299 9300 mp = ITOVFS(dp); 9301 ump = VFSTOUFS(mp); 9302 offset = blkoff(ump->um_fs, dp->i_offset); 9303 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 9304 ("softdep_setup_directory_change called on non-softdep filesystem")); 9305 9306 /* 9307 * Whiteouts do not need diradd dependencies. 9308 */ 9309 if (newinum != UFS_WINO) { 9310 dap = malloc(sizeof(struct diradd), 9311 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO); 9312 workitem_alloc(&dap->da_list, D_DIRADD, mp); 9313 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE; 9314 dap->da_offset = offset; 9315 dap->da_newinum = newinum; 9316 LIST_INIT(&dap->da_jwork); 9317 } 9318 9319 /* 9320 * Allocate a new dirrem and ACQUIRE_LOCK. 9321 */ 9322 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 9323 pagedep = dirrem->dm_pagedep; 9324 /* 9325 * The possible values for isrmdir: 9326 * 0 - non-directory file rename 9327 * 1 - directory rename within same directory 9328 * inum - directory rename to new directory of given inode number 9329 * When renaming to a new directory, we are both deleting and 9330 * creating a new directory entry, so the link count on the new 9331 * directory should not change. Thus we do not need the followup 9332 * dirrem which is usually done in handle_workitem_remove. We set 9333 * the DIRCHG flag to tell handle_workitem_remove to skip the 9334 * followup dirrem. 9335 */ 9336 if (isrmdir > 1) 9337 dirrem->dm_state |= DIRCHG; 9338 9339 /* 9340 * Whiteouts have no additional dependencies, 9341 * so just put the dirrem on the correct list. 9342 */ 9343 if (newinum == UFS_WINO) { 9344 if ((dirrem->dm_state & COMPLETE) == 0) { 9345 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem, 9346 dm_next); 9347 } else { 9348 dirrem->dm_dirinum = pagedep->pd_ino; 9349 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9350 add_to_worklist(&dirrem->dm_list, 0); 9351 } 9352 FREE_LOCK(ump); 9353 return; 9354 } 9355 /* 9356 * Add the dirrem to the inodedep's pending remove list for quick 9357 * discovery later. A valid nlinkdelta ensures that this lookup 9358 * will not fail. 9359 */ 9360 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9361 panic("softdep_setup_directory_change: Lost inodedep."); 9362 dirrem->dm_state |= ONDEPLIST; 9363 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9364 9365 /* 9366 * If the COMPLETE flag is clear, then there were no active 9367 * entries and we want to roll back to the previous inode until 9368 * the new inode is committed to disk. If the COMPLETE flag is 9369 * set, then we have deleted an entry that never made it to disk. 9370 * If the entry we deleted resulted from a name change, then the old 9371 * inode reference still resides on disk. Any rollback that we do 9372 * needs to be to that old inode (returned to us in prevdirrem). If 9373 * the entry we deleted resulted from a create, then there is 9374 * no entry on the disk, so we want to roll back to zero rather 9375 * than the uncommitted inode. In either of the COMPLETE cases we 9376 * want to immediately free the unwritten and unreferenced inode. 9377 */ 9378 if ((dirrem->dm_state & COMPLETE) == 0) { 9379 dap->da_previous = dirrem; 9380 } else { 9381 if (prevdirrem != NULL) { 9382 dap->da_previous = prevdirrem; 9383 } else { 9384 dap->da_state &= ~DIRCHG; 9385 dap->da_pagedep = pagedep; 9386 } 9387 dirrem->dm_dirinum = pagedep->pd_ino; 9388 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9389 add_to_worklist(&dirrem->dm_list, 0); 9390 } 9391 /* 9392 * Lookup the jaddref for this journal entry. We must finish 9393 * initializing it and make the diradd write dependent on it. 9394 * If we're not journaling, put it on the id_bufwait list if the 9395 * inode is not yet written. If it is written, do the post-inode 9396 * write processing to put it on the id_pendinghd list. 9397 */ 9398 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 9399 if (MOUNTEDSUJ(mp)) { 9400 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 9401 inoreflst); 9402 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 9403 ("softdep_setup_directory_change: bad jaddref %p", 9404 jaddref)); 9405 jaddref->ja_diroff = dp->i_offset; 9406 jaddref->ja_diradd = dap; 9407 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9408 dap, da_pdlist); 9409 add_to_journal(&jaddref->ja_list); 9410 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 9411 dap->da_state |= COMPLETE; 9412 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 9413 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 9414 } else { 9415 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9416 dap, da_pdlist); 9417 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 9418 } 9419 /* 9420 * If we're making a new name for a directory that has not been 9421 * committed when need to move the dot and dotdot references to 9422 * this new name. 9423 */ 9424 if (inodedep->id_mkdiradd && dp->i_offset != DOTDOT_OFFSET) 9425 merge_diradd(inodedep, dap); 9426 FREE_LOCK(ump); 9427 } 9428 9429 /* 9430 * Called whenever the link count on an inode is changed. 9431 * It creates an inode dependency so that the new reference(s) 9432 * to the inode cannot be committed to disk until the updated 9433 * inode has been written. 9434 */ 9435 void 9436 softdep_change_linkcnt(ip) 9437 struct inode *ip; /* the inode with the increased link count */ 9438 { 9439 struct inodedep *inodedep; 9440 struct ufsmount *ump; 9441 9442 ump = ITOUMP(ip); 9443 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9444 ("softdep_change_linkcnt called on non-softdep filesystem")); 9445 ACQUIRE_LOCK(ump); 9446 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep); 9447 if (ip->i_nlink < ip->i_effnlink) 9448 panic("softdep_change_linkcnt: bad delta"); 9449 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9450 FREE_LOCK(ump); 9451 } 9452 9453 /* 9454 * Attach a sbdep dependency to the superblock buf so that we can keep 9455 * track of the head of the linked list of referenced but unlinked inodes. 9456 */ 9457 void 9458 softdep_setup_sbupdate(ump, fs, bp) 9459 struct ufsmount *ump; 9460 struct fs *fs; 9461 struct buf *bp; 9462 { 9463 struct sbdep *sbdep; 9464 struct worklist *wk; 9465 9466 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9467 ("softdep_setup_sbupdate called on non-softdep filesystem")); 9468 LIST_FOREACH(wk, &bp->b_dep, wk_list) 9469 if (wk->wk_type == D_SBDEP) 9470 break; 9471 if (wk != NULL) 9472 return; 9473 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS); 9474 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump)); 9475 sbdep->sb_fs = fs; 9476 sbdep->sb_ump = ump; 9477 ACQUIRE_LOCK(ump); 9478 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list); 9479 FREE_LOCK(ump); 9480 } 9481 9482 /* 9483 * Return the first unlinked inodedep which is ready to be the head of the 9484 * list. The inodedep and all those after it must have valid next pointers. 9485 */ 9486 static struct inodedep * 9487 first_unlinked_inodedep(ump) 9488 struct ufsmount *ump; 9489 { 9490 struct inodedep *inodedep; 9491 struct inodedep *idp; 9492 9493 LOCK_OWNED(ump); 9494 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst); 9495 inodedep; inodedep = idp) { 9496 if ((inodedep->id_state & UNLINKNEXT) == 0) 9497 return (NULL); 9498 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9499 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0) 9500 break; 9501 if ((inodedep->id_state & UNLINKPREV) == 0) 9502 break; 9503 } 9504 return (inodedep); 9505 } 9506 9507 /* 9508 * Set the sujfree unlinked head pointer prior to writing a superblock. 9509 */ 9510 static void 9511 initiate_write_sbdep(sbdep) 9512 struct sbdep *sbdep; 9513 { 9514 struct inodedep *inodedep; 9515 struct fs *bpfs; 9516 struct fs *fs; 9517 9518 bpfs = sbdep->sb_fs; 9519 fs = sbdep->sb_ump->um_fs; 9520 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9521 if (inodedep) { 9522 fs->fs_sujfree = inodedep->id_ino; 9523 inodedep->id_state |= UNLINKPREV; 9524 } else 9525 fs->fs_sujfree = 0; 9526 bpfs->fs_sujfree = fs->fs_sujfree; 9527 } 9528 9529 /* 9530 * After a superblock is written determine whether it must be written again 9531 * due to a changing unlinked list head. 9532 */ 9533 static int 9534 handle_written_sbdep(sbdep, bp) 9535 struct sbdep *sbdep; 9536 struct buf *bp; 9537 { 9538 struct inodedep *inodedep; 9539 struct fs *fs; 9540 9541 LOCK_OWNED(sbdep->sb_ump); 9542 fs = sbdep->sb_fs; 9543 /* 9544 * If the superblock doesn't match the in-memory list start over. 9545 */ 9546 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9547 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) || 9548 (inodedep == NULL && fs->fs_sujfree != 0)) { 9549 bdirty(bp); 9550 return (1); 9551 } 9552 WORKITEM_FREE(sbdep, D_SBDEP); 9553 if (fs->fs_sujfree == 0) 9554 return (0); 9555 /* 9556 * Now that we have a record of this inode in stable store allow it 9557 * to be written to free up pending work. Inodes may see a lot of 9558 * write activity after they are unlinked which we must not hold up. 9559 */ 9560 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 9561 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS) 9562 panic("handle_written_sbdep: Bad inodedep %p (0x%X)", 9563 inodedep, inodedep->id_state); 9564 if (inodedep->id_state & UNLINKONLIST) 9565 break; 9566 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST; 9567 } 9568 9569 return (0); 9570 } 9571 9572 /* 9573 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list. 9574 */ 9575 static void 9576 unlinked_inodedep(mp, inodedep) 9577 struct mount *mp; 9578 struct inodedep *inodedep; 9579 { 9580 struct ufsmount *ump; 9581 9582 ump = VFSTOUFS(mp); 9583 LOCK_OWNED(ump); 9584 if (MOUNTEDSUJ(mp) == 0) 9585 return; 9586 ump->um_fs->fs_fmod = 1; 9587 if (inodedep->id_state & UNLINKED) 9588 panic("unlinked_inodedep: %p already unlinked\n", inodedep); 9589 inodedep->id_state |= UNLINKED; 9590 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked); 9591 } 9592 9593 /* 9594 * Remove an inodedep from the unlinked inodedep list. This may require 9595 * disk writes if the inode has made it that far. 9596 */ 9597 static void 9598 clear_unlinked_inodedep(inodedep) 9599 struct inodedep *inodedep; 9600 { 9601 struct ufsmount *ump; 9602 struct inodedep *idp; 9603 struct inodedep *idn; 9604 struct fs *fs; 9605 struct buf *bp; 9606 ino_t ino; 9607 ino_t nino; 9608 ino_t pino; 9609 int error; 9610 9611 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9612 fs = ump->um_fs; 9613 ino = inodedep->id_ino; 9614 error = 0; 9615 for (;;) { 9616 LOCK_OWNED(ump); 9617 KASSERT((inodedep->id_state & UNLINKED) != 0, 9618 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9619 inodedep)); 9620 /* 9621 * If nothing has yet been written simply remove us from 9622 * the in memory list and return. This is the most common 9623 * case where handle_workitem_remove() loses the final 9624 * reference. 9625 */ 9626 if ((inodedep->id_state & UNLINKLINKS) == 0) 9627 break; 9628 /* 9629 * If we have a NEXT pointer and no PREV pointer we can simply 9630 * clear NEXT's PREV and remove ourselves from the list. Be 9631 * careful not to clear PREV if the superblock points at 9632 * next as well. 9633 */ 9634 idn = TAILQ_NEXT(inodedep, id_unlinked); 9635 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) { 9636 if (idn && fs->fs_sujfree != idn->id_ino) 9637 idn->id_state &= ~UNLINKPREV; 9638 break; 9639 } 9640 /* 9641 * Here we have an inodedep which is actually linked into 9642 * the list. We must remove it by forcing a write to the 9643 * link before us, whether it be the superblock or an inode. 9644 * Unfortunately the list may change while we're waiting 9645 * on the buf lock for either resource so we must loop until 9646 * we lock the right one. If both the superblock and an 9647 * inode point to this inode we must clear the inode first 9648 * followed by the superblock. 9649 */ 9650 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9651 pino = 0; 9652 if (idp && (idp->id_state & UNLINKNEXT)) 9653 pino = idp->id_ino; 9654 FREE_LOCK(ump); 9655 if (pino == 0) { 9656 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9657 (int)fs->fs_sbsize, 0, 0, 0); 9658 } else { 9659 error = bread(ump->um_devvp, 9660 fsbtodb(fs, ino_to_fsba(fs, pino)), 9661 (int)fs->fs_bsize, NOCRED, &bp); 9662 if (error) 9663 brelse(bp); 9664 } 9665 ACQUIRE_LOCK(ump); 9666 if (error) 9667 break; 9668 /* If the list has changed restart the loop. */ 9669 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9670 nino = 0; 9671 if (idp && (idp->id_state & UNLINKNEXT)) 9672 nino = idp->id_ino; 9673 if (nino != pino || 9674 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) { 9675 FREE_LOCK(ump); 9676 brelse(bp); 9677 ACQUIRE_LOCK(ump); 9678 continue; 9679 } 9680 nino = 0; 9681 idn = TAILQ_NEXT(inodedep, id_unlinked); 9682 if (idn) 9683 nino = idn->id_ino; 9684 /* 9685 * Remove us from the in memory list. After this we cannot 9686 * access the inodedep. 9687 */ 9688 KASSERT((inodedep->id_state & UNLINKED) != 0, 9689 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9690 inodedep)); 9691 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9692 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9693 FREE_LOCK(ump); 9694 /* 9695 * The predecessor's next pointer is manually updated here 9696 * so that the NEXT flag is never cleared for an element 9697 * that is in the list. 9698 */ 9699 if (pino == 0) { 9700 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 9701 ffs_oldfscompat_write((struct fs *)bp->b_data, ump); 9702 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, 9703 bp); 9704 } else if (fs->fs_magic == FS_UFS1_MAGIC) 9705 ((struct ufs1_dinode *)bp->b_data + 9706 ino_to_fsbo(fs, pino))->di_freelink = nino; 9707 else 9708 ((struct ufs2_dinode *)bp->b_data + 9709 ino_to_fsbo(fs, pino))->di_freelink = nino; 9710 /* 9711 * If the bwrite fails we have no recourse to recover. The 9712 * filesystem is corrupted already. 9713 */ 9714 bwrite(bp); 9715 ACQUIRE_LOCK(ump); 9716 /* 9717 * If the superblock pointer still needs to be cleared force 9718 * a write here. 9719 */ 9720 if (fs->fs_sujfree == ino) { 9721 FREE_LOCK(ump); 9722 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9723 (int)fs->fs_sbsize, 0, 0, 0); 9724 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 9725 ffs_oldfscompat_write((struct fs *)bp->b_data, ump); 9726 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, 9727 bp); 9728 bwrite(bp); 9729 ACQUIRE_LOCK(ump); 9730 } 9731 9732 if (fs->fs_sujfree != ino) 9733 return; 9734 panic("clear_unlinked_inodedep: Failed to clear free head"); 9735 } 9736 if (inodedep->id_ino == fs->fs_sujfree) 9737 panic("clear_unlinked_inodedep: Freeing head of free list"); 9738 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9739 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9740 return; 9741 } 9742 9743 /* 9744 * This workitem decrements the inode's link count. 9745 * If the link count reaches zero, the file is removed. 9746 */ 9747 static int 9748 handle_workitem_remove(dirrem, flags) 9749 struct dirrem *dirrem; 9750 int flags; 9751 { 9752 struct inodedep *inodedep; 9753 struct workhead dotdotwk; 9754 struct worklist *wk; 9755 struct ufsmount *ump; 9756 struct mount *mp; 9757 struct vnode *vp; 9758 struct inode *ip; 9759 ino_t oldinum; 9760 9761 if (dirrem->dm_state & ONWORKLIST) 9762 panic("handle_workitem_remove: dirrem %p still on worklist", 9763 dirrem); 9764 oldinum = dirrem->dm_oldinum; 9765 mp = dirrem->dm_list.wk_mp; 9766 ump = VFSTOUFS(mp); 9767 flags |= LK_EXCLUSIVE; 9768 if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ) != 0) 9769 return (EBUSY); 9770 ip = VTOI(vp); 9771 ACQUIRE_LOCK(ump); 9772 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0) 9773 panic("handle_workitem_remove: lost inodedep"); 9774 if (dirrem->dm_state & ONDEPLIST) 9775 LIST_REMOVE(dirrem, dm_inonext); 9776 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 9777 ("handle_workitem_remove: Journal entries not written.")); 9778 9779 /* 9780 * Move all dependencies waiting on the remove to complete 9781 * from the dirrem to the inode inowait list to be completed 9782 * after the inode has been updated and written to disk. Any 9783 * marked MKDIR_PARENT are saved to be completed when the .. ref 9784 * is removed. 9785 */ 9786 LIST_INIT(&dotdotwk); 9787 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) { 9788 WORKLIST_REMOVE(wk); 9789 if (wk->wk_state & MKDIR_PARENT) { 9790 wk->wk_state &= ~MKDIR_PARENT; 9791 WORKLIST_INSERT(&dotdotwk, wk); 9792 continue; 9793 } 9794 WORKLIST_INSERT(&inodedep->id_inowait, wk); 9795 } 9796 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list); 9797 /* 9798 * Normal file deletion. 9799 */ 9800 if ((dirrem->dm_state & RMDIR) == 0) { 9801 ip->i_nlink--; 9802 DIP_SET(ip, i_nlink, ip->i_nlink); 9803 ip->i_flag |= IN_CHANGE; 9804 if (ip->i_nlink < ip->i_effnlink) 9805 panic("handle_workitem_remove: bad file delta"); 9806 if (ip->i_nlink == 0) 9807 unlinked_inodedep(mp, inodedep); 9808 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9809 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 9810 ("handle_workitem_remove: worklist not empty. %s", 9811 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type))); 9812 WORKITEM_FREE(dirrem, D_DIRREM); 9813 FREE_LOCK(ump); 9814 goto out; 9815 } 9816 /* 9817 * Directory deletion. Decrement reference count for both the 9818 * just deleted parent directory entry and the reference for ".". 9819 * Arrange to have the reference count on the parent decremented 9820 * to account for the loss of "..". 9821 */ 9822 ip->i_nlink -= 2; 9823 DIP_SET(ip, i_nlink, ip->i_nlink); 9824 ip->i_flag |= IN_CHANGE; 9825 if (ip->i_nlink < ip->i_effnlink) 9826 panic("handle_workitem_remove: bad dir delta"); 9827 if (ip->i_nlink == 0) 9828 unlinked_inodedep(mp, inodedep); 9829 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9830 /* 9831 * Rename a directory to a new parent. Since, we are both deleting 9832 * and creating a new directory entry, the link count on the new 9833 * directory should not change. Thus we skip the followup dirrem. 9834 */ 9835 if (dirrem->dm_state & DIRCHG) { 9836 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 9837 ("handle_workitem_remove: DIRCHG and worklist not empty.")); 9838 WORKITEM_FREE(dirrem, D_DIRREM); 9839 FREE_LOCK(ump); 9840 goto out; 9841 } 9842 dirrem->dm_state = ONDEPLIST; 9843 dirrem->dm_oldinum = dirrem->dm_dirinum; 9844 /* 9845 * Place the dirrem on the parent's diremhd list. 9846 */ 9847 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0) 9848 panic("handle_workitem_remove: lost dir inodedep"); 9849 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9850 /* 9851 * If the allocated inode has never been written to disk, then 9852 * the on-disk inode is zero'ed and we can remove the file 9853 * immediately. When journaling if the inode has been marked 9854 * unlinked and not DEPCOMPLETE we know it can never be written. 9855 */ 9856 inodedep_lookup(mp, oldinum, 0, &inodedep); 9857 if (inodedep == NULL || 9858 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED || 9859 check_inode_unwritten(inodedep)) { 9860 FREE_LOCK(ump); 9861 vput(vp); 9862 return handle_workitem_remove(dirrem, flags); 9863 } 9864 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list); 9865 FREE_LOCK(ump); 9866 ip->i_flag |= IN_CHANGE; 9867 out: 9868 ffs_update(vp, 0); 9869 vput(vp); 9870 return (0); 9871 } 9872 9873 /* 9874 * Inode de-allocation dependencies. 9875 * 9876 * When an inode's link count is reduced to zero, it can be de-allocated. We 9877 * found it convenient to postpone de-allocation until after the inode is 9878 * written to disk with its new link count (zero). At this point, all of the 9879 * on-disk inode's block pointers are nullified and, with careful dependency 9880 * list ordering, all dependencies related to the inode will be satisfied and 9881 * the corresponding dependency structures de-allocated. So, if/when the 9882 * inode is reused, there will be no mixing of old dependencies with new 9883 * ones. This artificial dependency is set up by the block de-allocation 9884 * procedure above (softdep_setup_freeblocks) and completed by the 9885 * following procedure. 9886 */ 9887 static void 9888 handle_workitem_freefile(freefile) 9889 struct freefile *freefile; 9890 { 9891 struct workhead wkhd; 9892 struct fs *fs; 9893 struct inodedep *idp; 9894 struct ufsmount *ump; 9895 int error; 9896 9897 ump = VFSTOUFS(freefile->fx_list.wk_mp); 9898 fs = ump->um_fs; 9899 #ifdef DEBUG 9900 ACQUIRE_LOCK(ump); 9901 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp); 9902 FREE_LOCK(ump); 9903 if (error) 9904 panic("handle_workitem_freefile: inodedep %p survived", idp); 9905 #endif 9906 UFS_LOCK(ump); 9907 fs->fs_pendinginodes -= 1; 9908 UFS_UNLOCK(ump); 9909 LIST_INIT(&wkhd); 9910 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list); 9911 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp, 9912 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0) 9913 softdep_error("handle_workitem_freefile", error); 9914 ACQUIRE_LOCK(ump); 9915 WORKITEM_FREE(freefile, D_FREEFILE); 9916 FREE_LOCK(ump); 9917 } 9918 9919 9920 /* 9921 * Helper function which unlinks marker element from work list and returns 9922 * the next element on the list. 9923 */ 9924 static __inline struct worklist * 9925 markernext(struct worklist *marker) 9926 { 9927 struct worklist *next; 9928 9929 next = LIST_NEXT(marker, wk_list); 9930 LIST_REMOVE(marker, wk_list); 9931 return next; 9932 } 9933 9934 /* 9935 * Disk writes. 9936 * 9937 * The dependency structures constructed above are most actively used when file 9938 * system blocks are written to disk. No constraints are placed on when a 9939 * block can be written, but unsatisfied update dependencies are made safe by 9940 * modifying (or replacing) the source memory for the duration of the disk 9941 * write. When the disk write completes, the memory block is again brought 9942 * up-to-date. 9943 * 9944 * In-core inode structure reclamation. 9945 * 9946 * Because there are a finite number of "in-core" inode structures, they are 9947 * reused regularly. By transferring all inode-related dependencies to the 9948 * in-memory inode block and indexing them separately (via "inodedep"s), we 9949 * can allow "in-core" inode structures to be reused at any time and avoid 9950 * any increase in contention. 9951 * 9952 * Called just before entering the device driver to initiate a new disk I/O. 9953 * The buffer must be locked, thus, no I/O completion operations can occur 9954 * while we are manipulating its associated dependencies. 9955 */ 9956 static void 9957 softdep_disk_io_initiation(bp) 9958 struct buf *bp; /* structure describing disk write to occur */ 9959 { 9960 struct worklist *wk; 9961 struct worklist marker; 9962 struct inodedep *inodedep; 9963 struct freeblks *freeblks; 9964 struct jblkdep *jblkdep; 9965 struct newblk *newblk; 9966 struct ufsmount *ump; 9967 9968 /* 9969 * We only care about write operations. There should never 9970 * be dependencies for reads. 9971 */ 9972 if (bp->b_iocmd != BIO_WRITE) 9973 panic("softdep_disk_io_initiation: not write"); 9974 9975 if (bp->b_vflags & BV_BKGRDINPROG) 9976 panic("softdep_disk_io_initiation: Writing buffer with " 9977 "background write in progress: %p", bp); 9978 9979 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL) 9980 return; 9981 ump = VFSTOUFS(wk->wk_mp); 9982 9983 marker.wk_type = D_LAST + 1; /* Not a normal workitem */ 9984 PHOLD(curproc); /* Don't swap out kernel stack */ 9985 ACQUIRE_LOCK(ump); 9986 /* 9987 * Do any necessary pre-I/O processing. 9988 */ 9989 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL; 9990 wk = markernext(&marker)) { 9991 LIST_INSERT_AFTER(wk, &marker, wk_list); 9992 switch (wk->wk_type) { 9993 9994 case D_PAGEDEP: 9995 initiate_write_filepage(WK_PAGEDEP(wk), bp); 9996 continue; 9997 9998 case D_INODEDEP: 9999 inodedep = WK_INODEDEP(wk); 10000 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) 10001 initiate_write_inodeblock_ufs1(inodedep, bp); 10002 else 10003 initiate_write_inodeblock_ufs2(inodedep, bp); 10004 continue; 10005 10006 case D_INDIRDEP: 10007 initiate_write_indirdep(WK_INDIRDEP(wk), bp); 10008 continue; 10009 10010 case D_BMSAFEMAP: 10011 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp); 10012 continue; 10013 10014 case D_JSEG: 10015 WK_JSEG(wk)->js_buf = NULL; 10016 continue; 10017 10018 case D_FREEBLKS: 10019 freeblks = WK_FREEBLKS(wk); 10020 jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd); 10021 /* 10022 * We have to wait for the freeblks to be journaled 10023 * before we can write an inodeblock with updated 10024 * pointers. Be careful to arrange the marker so 10025 * we revisit the freeblks if it's not removed by 10026 * the first jwait(). 10027 */ 10028 if (jblkdep != NULL) { 10029 LIST_REMOVE(&marker, wk_list); 10030 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10031 jwait(&jblkdep->jb_list, MNT_WAIT); 10032 } 10033 continue; 10034 case D_ALLOCDIRECT: 10035 case D_ALLOCINDIR: 10036 /* 10037 * We have to wait for the jnewblk to be journaled 10038 * before we can write to a block if the contents 10039 * may be confused with an earlier file's indirect 10040 * at recovery time. Handle the marker as described 10041 * above. 10042 */ 10043 newblk = WK_NEWBLK(wk); 10044 if (newblk->nb_jnewblk != NULL && 10045 indirblk_lookup(newblk->nb_list.wk_mp, 10046 newblk->nb_newblkno)) { 10047 LIST_REMOVE(&marker, wk_list); 10048 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10049 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 10050 } 10051 continue; 10052 10053 case D_SBDEP: 10054 initiate_write_sbdep(WK_SBDEP(wk)); 10055 continue; 10056 10057 case D_MKDIR: 10058 case D_FREEWORK: 10059 case D_FREEDEP: 10060 case D_JSEGDEP: 10061 continue; 10062 10063 default: 10064 panic("handle_disk_io_initiation: Unexpected type %s", 10065 TYPENAME(wk->wk_type)); 10066 /* NOTREACHED */ 10067 } 10068 } 10069 FREE_LOCK(ump); 10070 PRELE(curproc); /* Allow swapout of kernel stack */ 10071 } 10072 10073 /* 10074 * Called from within the procedure above to deal with unsatisfied 10075 * allocation dependencies in a directory. The buffer must be locked, 10076 * thus, no I/O completion operations can occur while we are 10077 * manipulating its associated dependencies. 10078 */ 10079 static void 10080 initiate_write_filepage(pagedep, bp) 10081 struct pagedep *pagedep; 10082 struct buf *bp; 10083 { 10084 struct jremref *jremref; 10085 struct jmvref *jmvref; 10086 struct dirrem *dirrem; 10087 struct diradd *dap; 10088 struct direct *ep; 10089 int i; 10090 10091 if (pagedep->pd_state & IOSTARTED) { 10092 /* 10093 * This can only happen if there is a driver that does not 10094 * understand chaining. Here biodone will reissue the call 10095 * to strategy for the incomplete buffers. 10096 */ 10097 printf("initiate_write_filepage: already started\n"); 10098 return; 10099 } 10100 pagedep->pd_state |= IOSTARTED; 10101 /* 10102 * Wait for all journal remove dependencies to hit the disk. 10103 * We can not allow any potentially conflicting directory adds 10104 * to be visible before removes and rollback is too difficult. 10105 * The per-filesystem lock may be dropped and re-acquired, however 10106 * we hold the buf locked so the dependency can not go away. 10107 */ 10108 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) 10109 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) 10110 jwait(&jremref->jr_list, MNT_WAIT); 10111 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) 10112 jwait(&jmvref->jm_list, MNT_WAIT); 10113 for (i = 0; i < DAHASHSZ; i++) { 10114 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 10115 ep = (struct direct *) 10116 ((char *)bp->b_data + dap->da_offset); 10117 if (ep->d_ino != dap->da_newinum) 10118 panic("%s: dir inum %ju != new %ju", 10119 "initiate_write_filepage", 10120 (uintmax_t)ep->d_ino, 10121 (uintmax_t)dap->da_newinum); 10122 if (dap->da_state & DIRCHG) 10123 ep->d_ino = dap->da_previous->dm_oldinum; 10124 else 10125 ep->d_ino = 0; 10126 dap->da_state &= ~ATTACHED; 10127 dap->da_state |= UNDONE; 10128 } 10129 } 10130 } 10131 10132 /* 10133 * Version of initiate_write_inodeblock that handles UFS1 dinodes. 10134 * Note that any bug fixes made to this routine must be done in the 10135 * version found below. 10136 * 10137 * Called from within the procedure above to deal with unsatisfied 10138 * allocation dependencies in an inodeblock. The buffer must be 10139 * locked, thus, no I/O completion operations can occur while we 10140 * are manipulating its associated dependencies. 10141 */ 10142 static void 10143 initiate_write_inodeblock_ufs1(inodedep, bp) 10144 struct inodedep *inodedep; 10145 struct buf *bp; /* The inode block */ 10146 { 10147 struct allocdirect *adp, *lastadp; 10148 struct ufs1_dinode *dp; 10149 struct ufs1_dinode *sip; 10150 struct inoref *inoref; 10151 struct ufsmount *ump; 10152 struct fs *fs; 10153 ufs_lbn_t i; 10154 #ifdef INVARIANTS 10155 ufs_lbn_t prevlbn = 0; 10156 #endif 10157 int deplist; 10158 10159 if (inodedep->id_state & IOSTARTED) 10160 panic("initiate_write_inodeblock_ufs1: already started"); 10161 inodedep->id_state |= IOSTARTED; 10162 fs = inodedep->id_fs; 10163 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10164 LOCK_OWNED(ump); 10165 dp = (struct ufs1_dinode *)bp->b_data + 10166 ino_to_fsbo(fs, inodedep->id_ino); 10167 10168 /* 10169 * If we're on the unlinked list but have not yet written our 10170 * next pointer initialize it here. 10171 */ 10172 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10173 struct inodedep *inon; 10174 10175 inon = TAILQ_NEXT(inodedep, id_unlinked); 10176 dp->di_freelink = inon ? inon->id_ino : 0; 10177 } 10178 /* 10179 * If the bitmap is not yet written, then the allocated 10180 * inode cannot be written to disk. 10181 */ 10182 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10183 if (inodedep->id_savedino1 != NULL) 10184 panic("initiate_write_inodeblock_ufs1: I/O underway"); 10185 FREE_LOCK(ump); 10186 sip = malloc(sizeof(struct ufs1_dinode), 10187 M_SAVEDINO, M_SOFTDEP_FLAGS); 10188 ACQUIRE_LOCK(ump); 10189 inodedep->id_savedino1 = sip; 10190 *inodedep->id_savedino1 = *dp; 10191 bzero((caddr_t)dp, sizeof(struct ufs1_dinode)); 10192 dp->di_gen = inodedep->id_savedino1->di_gen; 10193 dp->di_freelink = inodedep->id_savedino1->di_freelink; 10194 return; 10195 } 10196 /* 10197 * If no dependencies, then there is nothing to roll back. 10198 */ 10199 inodedep->id_savedsize = dp->di_size; 10200 inodedep->id_savedextsize = 0; 10201 inodedep->id_savednlink = dp->di_nlink; 10202 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10203 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10204 return; 10205 /* 10206 * Revert the link count to that of the first unwritten journal entry. 10207 */ 10208 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10209 if (inoref) 10210 dp->di_nlink = inoref->if_nlink; 10211 /* 10212 * Set the dependencies to busy. 10213 */ 10214 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10215 adp = TAILQ_NEXT(adp, ad_next)) { 10216 #ifdef INVARIANTS 10217 if (deplist != 0 && prevlbn >= adp->ad_offset) 10218 panic("softdep_write_inodeblock: lbn order"); 10219 prevlbn = adp->ad_offset; 10220 if (adp->ad_offset < UFS_NDADDR && 10221 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10222 panic("%s: direct pointer #%jd mismatch %d != %jd", 10223 "softdep_write_inodeblock", 10224 (intmax_t)adp->ad_offset, 10225 dp->di_db[adp->ad_offset], 10226 (intmax_t)adp->ad_newblkno); 10227 if (adp->ad_offset >= UFS_NDADDR && 10228 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno) 10229 panic("%s: indirect pointer #%jd mismatch %d != %jd", 10230 "softdep_write_inodeblock", 10231 (intmax_t)adp->ad_offset - UFS_NDADDR, 10232 dp->di_ib[adp->ad_offset - UFS_NDADDR], 10233 (intmax_t)adp->ad_newblkno); 10234 deplist |= 1 << adp->ad_offset; 10235 if ((adp->ad_state & ATTACHED) == 0) 10236 panic("softdep_write_inodeblock: Unknown state 0x%x", 10237 adp->ad_state); 10238 #endif /* INVARIANTS */ 10239 adp->ad_state &= ~ATTACHED; 10240 adp->ad_state |= UNDONE; 10241 } 10242 /* 10243 * The on-disk inode cannot claim to be any larger than the last 10244 * fragment that has been written. Otherwise, the on-disk inode 10245 * might have fragments that were not the last block in the file 10246 * which would corrupt the filesystem. 10247 */ 10248 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10249 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10250 if (adp->ad_offset >= UFS_NDADDR) 10251 break; 10252 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10253 /* keep going until hitting a rollback to a frag */ 10254 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10255 continue; 10256 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10257 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) { 10258 #ifdef INVARIANTS 10259 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10260 panic("softdep_write_inodeblock: lost dep1"); 10261 #endif /* INVARIANTS */ 10262 dp->di_db[i] = 0; 10263 } 10264 for (i = 0; i < UFS_NIADDR; i++) { 10265 #ifdef INVARIANTS 10266 if (dp->di_ib[i] != 0 && 10267 (deplist & ((1 << UFS_NDADDR) << i)) == 0) 10268 panic("softdep_write_inodeblock: lost dep2"); 10269 #endif /* INVARIANTS */ 10270 dp->di_ib[i] = 0; 10271 } 10272 return; 10273 } 10274 /* 10275 * If we have zero'ed out the last allocated block of the file, 10276 * roll back the size to the last currently allocated block. 10277 * We know that this last allocated block is a full-sized as 10278 * we already checked for fragments in the loop above. 10279 */ 10280 if (lastadp != NULL && 10281 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10282 for (i = lastadp->ad_offset; i >= 0; i--) 10283 if (dp->di_db[i] != 0) 10284 break; 10285 dp->di_size = (i + 1) * fs->fs_bsize; 10286 } 10287 /* 10288 * The only dependencies are for indirect blocks. 10289 * 10290 * The file size for indirect block additions is not guaranteed. 10291 * Such a guarantee would be non-trivial to achieve. The conventional 10292 * synchronous write implementation also does not make this guarantee. 10293 * Fsck should catch and fix discrepancies. Arguably, the file size 10294 * can be over-estimated without destroying integrity when the file 10295 * moves into the indirect blocks (i.e., is large). If we want to 10296 * postpone fsck, we are stuck with this argument. 10297 */ 10298 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10299 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0; 10300 } 10301 10302 /* 10303 * Version of initiate_write_inodeblock that handles UFS2 dinodes. 10304 * Note that any bug fixes made to this routine must be done in the 10305 * version found above. 10306 * 10307 * Called from within the procedure above to deal with unsatisfied 10308 * allocation dependencies in an inodeblock. The buffer must be 10309 * locked, thus, no I/O completion operations can occur while we 10310 * are manipulating its associated dependencies. 10311 */ 10312 static void 10313 initiate_write_inodeblock_ufs2(inodedep, bp) 10314 struct inodedep *inodedep; 10315 struct buf *bp; /* The inode block */ 10316 { 10317 struct allocdirect *adp, *lastadp; 10318 struct ufs2_dinode *dp; 10319 struct ufs2_dinode *sip; 10320 struct inoref *inoref; 10321 struct ufsmount *ump; 10322 struct fs *fs; 10323 ufs_lbn_t i; 10324 #ifdef INVARIANTS 10325 ufs_lbn_t prevlbn = 0; 10326 #endif 10327 int deplist; 10328 10329 if (inodedep->id_state & IOSTARTED) 10330 panic("initiate_write_inodeblock_ufs2: already started"); 10331 inodedep->id_state |= IOSTARTED; 10332 fs = inodedep->id_fs; 10333 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10334 LOCK_OWNED(ump); 10335 dp = (struct ufs2_dinode *)bp->b_data + 10336 ino_to_fsbo(fs, inodedep->id_ino); 10337 10338 /* 10339 * If we're on the unlinked list but have not yet written our 10340 * next pointer initialize it here. 10341 */ 10342 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10343 struct inodedep *inon; 10344 10345 inon = TAILQ_NEXT(inodedep, id_unlinked); 10346 dp->di_freelink = inon ? inon->id_ino : 0; 10347 } 10348 /* 10349 * If the bitmap is not yet written, then the allocated 10350 * inode cannot be written to disk. 10351 */ 10352 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10353 if (inodedep->id_savedino2 != NULL) 10354 panic("initiate_write_inodeblock_ufs2: I/O underway"); 10355 FREE_LOCK(ump); 10356 sip = malloc(sizeof(struct ufs2_dinode), 10357 M_SAVEDINO, M_SOFTDEP_FLAGS); 10358 ACQUIRE_LOCK(ump); 10359 inodedep->id_savedino2 = sip; 10360 *inodedep->id_savedino2 = *dp; 10361 bzero((caddr_t)dp, sizeof(struct ufs2_dinode)); 10362 dp->di_gen = inodedep->id_savedino2->di_gen; 10363 dp->di_freelink = inodedep->id_savedino2->di_freelink; 10364 return; 10365 } 10366 /* 10367 * If no dependencies, then there is nothing to roll back. 10368 */ 10369 inodedep->id_savedsize = dp->di_size; 10370 inodedep->id_savedextsize = dp->di_extsize; 10371 inodedep->id_savednlink = dp->di_nlink; 10372 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10373 TAILQ_EMPTY(&inodedep->id_extupdt) && 10374 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10375 return; 10376 /* 10377 * Revert the link count to that of the first unwritten journal entry. 10378 */ 10379 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10380 if (inoref) 10381 dp->di_nlink = inoref->if_nlink; 10382 10383 /* 10384 * Set the ext data dependencies to busy. 10385 */ 10386 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10387 adp = TAILQ_NEXT(adp, ad_next)) { 10388 #ifdef INVARIANTS 10389 if (deplist != 0 && prevlbn >= adp->ad_offset) 10390 panic("softdep_write_inodeblock: lbn order"); 10391 prevlbn = adp->ad_offset; 10392 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno) 10393 panic("%s: direct pointer #%jd mismatch %jd != %jd", 10394 "softdep_write_inodeblock", 10395 (intmax_t)adp->ad_offset, 10396 (intmax_t)dp->di_extb[adp->ad_offset], 10397 (intmax_t)adp->ad_newblkno); 10398 deplist |= 1 << adp->ad_offset; 10399 if ((adp->ad_state & ATTACHED) == 0) 10400 panic("softdep_write_inodeblock: Unknown state 0x%x", 10401 adp->ad_state); 10402 #endif /* INVARIANTS */ 10403 adp->ad_state &= ~ATTACHED; 10404 adp->ad_state |= UNDONE; 10405 } 10406 /* 10407 * The on-disk inode cannot claim to be any larger than the last 10408 * fragment that has been written. Otherwise, the on-disk inode 10409 * might have fragments that were not the last block in the ext 10410 * data which would corrupt the filesystem. 10411 */ 10412 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10413 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10414 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno; 10415 /* keep going until hitting a rollback to a frag */ 10416 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10417 continue; 10418 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10419 for (i = adp->ad_offset + 1; i < UFS_NXADDR; i++) { 10420 #ifdef INVARIANTS 10421 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0) 10422 panic("softdep_write_inodeblock: lost dep1"); 10423 #endif /* INVARIANTS */ 10424 dp->di_extb[i] = 0; 10425 } 10426 lastadp = NULL; 10427 break; 10428 } 10429 /* 10430 * If we have zero'ed out the last allocated block of the ext 10431 * data, roll back the size to the last currently allocated block. 10432 * We know that this last allocated block is a full-sized as 10433 * we already checked for fragments in the loop above. 10434 */ 10435 if (lastadp != NULL && 10436 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10437 for (i = lastadp->ad_offset; i >= 0; i--) 10438 if (dp->di_extb[i] != 0) 10439 break; 10440 dp->di_extsize = (i + 1) * fs->fs_bsize; 10441 } 10442 /* 10443 * Set the file data dependencies to busy. 10444 */ 10445 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10446 adp = TAILQ_NEXT(adp, ad_next)) { 10447 #ifdef INVARIANTS 10448 if (deplist != 0 && prevlbn >= adp->ad_offset) 10449 panic("softdep_write_inodeblock: lbn order"); 10450 if ((adp->ad_state & ATTACHED) == 0) 10451 panic("inodedep %p and adp %p not attached", inodedep, adp); 10452 prevlbn = adp->ad_offset; 10453 if (adp->ad_offset < UFS_NDADDR && 10454 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10455 panic("%s: direct pointer #%jd mismatch %jd != %jd", 10456 "softdep_write_inodeblock", 10457 (intmax_t)adp->ad_offset, 10458 (intmax_t)dp->di_db[adp->ad_offset], 10459 (intmax_t)adp->ad_newblkno); 10460 if (adp->ad_offset >= UFS_NDADDR && 10461 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno) 10462 panic("%s indirect pointer #%jd mismatch %jd != %jd", 10463 "softdep_write_inodeblock:", 10464 (intmax_t)adp->ad_offset - UFS_NDADDR, 10465 (intmax_t)dp->di_ib[adp->ad_offset - UFS_NDADDR], 10466 (intmax_t)adp->ad_newblkno); 10467 deplist |= 1 << adp->ad_offset; 10468 if ((adp->ad_state & ATTACHED) == 0) 10469 panic("softdep_write_inodeblock: Unknown state 0x%x", 10470 adp->ad_state); 10471 #endif /* INVARIANTS */ 10472 adp->ad_state &= ~ATTACHED; 10473 adp->ad_state |= UNDONE; 10474 } 10475 /* 10476 * The on-disk inode cannot claim to be any larger than the last 10477 * fragment that has been written. Otherwise, the on-disk inode 10478 * might have fragments that were not the last block in the file 10479 * which would corrupt the filesystem. 10480 */ 10481 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10482 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10483 if (adp->ad_offset >= UFS_NDADDR) 10484 break; 10485 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10486 /* keep going until hitting a rollback to a frag */ 10487 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10488 continue; 10489 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10490 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) { 10491 #ifdef INVARIANTS 10492 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10493 panic("softdep_write_inodeblock: lost dep2"); 10494 #endif /* INVARIANTS */ 10495 dp->di_db[i] = 0; 10496 } 10497 for (i = 0; i < UFS_NIADDR; i++) { 10498 #ifdef INVARIANTS 10499 if (dp->di_ib[i] != 0 && 10500 (deplist & ((1 << UFS_NDADDR) << i)) == 0) 10501 panic("softdep_write_inodeblock: lost dep3"); 10502 #endif /* INVARIANTS */ 10503 dp->di_ib[i] = 0; 10504 } 10505 return; 10506 } 10507 /* 10508 * If we have zero'ed out the last allocated block of the file, 10509 * roll back the size to the last currently allocated block. 10510 * We know that this last allocated block is a full-sized as 10511 * we already checked for fragments in the loop above. 10512 */ 10513 if (lastadp != NULL && 10514 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10515 for (i = lastadp->ad_offset; i >= 0; i--) 10516 if (dp->di_db[i] != 0) 10517 break; 10518 dp->di_size = (i + 1) * fs->fs_bsize; 10519 } 10520 /* 10521 * The only dependencies are for indirect blocks. 10522 * 10523 * The file size for indirect block additions is not guaranteed. 10524 * Such a guarantee would be non-trivial to achieve. The conventional 10525 * synchronous write implementation also does not make this guarantee. 10526 * Fsck should catch and fix discrepancies. Arguably, the file size 10527 * can be over-estimated without destroying integrity when the file 10528 * moves into the indirect blocks (i.e., is large). If we want to 10529 * postpone fsck, we are stuck with this argument. 10530 */ 10531 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10532 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0; 10533 } 10534 10535 /* 10536 * Cancel an indirdep as a result of truncation. Release all of the 10537 * children allocindirs and place their journal work on the appropriate 10538 * list. 10539 */ 10540 static void 10541 cancel_indirdep(indirdep, bp, freeblks) 10542 struct indirdep *indirdep; 10543 struct buf *bp; 10544 struct freeblks *freeblks; 10545 { 10546 struct allocindir *aip; 10547 10548 /* 10549 * None of the indirect pointers will ever be visible, 10550 * so they can simply be tossed. GOINGAWAY ensures 10551 * that allocated pointers will be saved in the buffer 10552 * cache until they are freed. Note that they will 10553 * only be able to be found by their physical address 10554 * since the inode mapping the logical address will 10555 * be gone. The save buffer used for the safe copy 10556 * was allocated in setup_allocindir_phase2 using 10557 * the physical address so it could be used for this 10558 * purpose. Hence we swap the safe copy with the real 10559 * copy, allowing the safe copy to be freed and holding 10560 * on to the real copy for later use in indir_trunc. 10561 */ 10562 if (indirdep->ir_state & GOINGAWAY) 10563 panic("cancel_indirdep: already gone"); 10564 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 10565 indirdep->ir_state |= DEPCOMPLETE; 10566 LIST_REMOVE(indirdep, ir_next); 10567 } 10568 indirdep->ir_state |= GOINGAWAY; 10569 /* 10570 * Pass in bp for blocks still have journal writes 10571 * pending so we can cancel them on their own. 10572 */ 10573 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != NULL) 10574 cancel_allocindir(aip, bp, freeblks, 0); 10575 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) 10576 cancel_allocindir(aip, NULL, freeblks, 0); 10577 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) 10578 cancel_allocindir(aip, NULL, freeblks, 0); 10579 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) 10580 cancel_allocindir(aip, NULL, freeblks, 0); 10581 /* 10582 * If there are pending partial truncations we need to keep the 10583 * old block copy around until they complete. This is because 10584 * the current b_data is not a perfect superset of the available 10585 * blocks. 10586 */ 10587 if (TAILQ_EMPTY(&indirdep->ir_trunc)) 10588 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount); 10589 else 10590 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10591 WORKLIST_REMOVE(&indirdep->ir_list); 10592 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list); 10593 indirdep->ir_bp = NULL; 10594 indirdep->ir_freeblks = freeblks; 10595 } 10596 10597 /* 10598 * Free an indirdep once it no longer has new pointers to track. 10599 */ 10600 static void 10601 free_indirdep(indirdep) 10602 struct indirdep *indirdep; 10603 { 10604 10605 KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc), 10606 ("free_indirdep: Indir trunc list not empty.")); 10607 KASSERT(LIST_EMPTY(&indirdep->ir_completehd), 10608 ("free_indirdep: Complete head not empty.")); 10609 KASSERT(LIST_EMPTY(&indirdep->ir_writehd), 10610 ("free_indirdep: write head not empty.")); 10611 KASSERT(LIST_EMPTY(&indirdep->ir_donehd), 10612 ("free_indirdep: done head not empty.")); 10613 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd), 10614 ("free_indirdep: deplist head not empty.")); 10615 KASSERT((indirdep->ir_state & DEPCOMPLETE), 10616 ("free_indirdep: %p still on newblk list.", indirdep)); 10617 KASSERT(indirdep->ir_saveddata == NULL, 10618 ("free_indirdep: %p still has saved data.", indirdep)); 10619 if (indirdep->ir_state & ONWORKLIST) 10620 WORKLIST_REMOVE(&indirdep->ir_list); 10621 WORKITEM_FREE(indirdep, D_INDIRDEP); 10622 } 10623 10624 /* 10625 * Called before a write to an indirdep. This routine is responsible for 10626 * rolling back pointers to a safe state which includes only those 10627 * allocindirs which have been completed. 10628 */ 10629 static void 10630 initiate_write_indirdep(indirdep, bp) 10631 struct indirdep *indirdep; 10632 struct buf *bp; 10633 { 10634 struct ufsmount *ump; 10635 10636 indirdep->ir_state |= IOSTARTED; 10637 if (indirdep->ir_state & GOINGAWAY) 10638 panic("disk_io_initiation: indirdep gone"); 10639 /* 10640 * If there are no remaining dependencies, this will be writing 10641 * the real pointers. 10642 */ 10643 if (LIST_EMPTY(&indirdep->ir_deplisthd) && 10644 TAILQ_EMPTY(&indirdep->ir_trunc)) 10645 return; 10646 /* 10647 * Replace up-to-date version with safe version. 10648 */ 10649 if (indirdep->ir_saveddata == NULL) { 10650 ump = VFSTOUFS(indirdep->ir_list.wk_mp); 10651 LOCK_OWNED(ump); 10652 FREE_LOCK(ump); 10653 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 10654 M_SOFTDEP_FLAGS); 10655 ACQUIRE_LOCK(ump); 10656 } 10657 indirdep->ir_state &= ~ATTACHED; 10658 indirdep->ir_state |= UNDONE; 10659 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10660 bcopy(indirdep->ir_savebp->b_data, bp->b_data, 10661 bp->b_bcount); 10662 } 10663 10664 /* 10665 * Called when an inode has been cleared in a cg bitmap. This finally 10666 * eliminates any canceled jaddrefs 10667 */ 10668 void 10669 softdep_setup_inofree(mp, bp, ino, wkhd) 10670 struct mount *mp; 10671 struct buf *bp; 10672 ino_t ino; 10673 struct workhead *wkhd; 10674 { 10675 struct worklist *wk, *wkn; 10676 struct inodedep *inodedep; 10677 struct ufsmount *ump; 10678 uint8_t *inosused; 10679 struct cg *cgp; 10680 struct fs *fs; 10681 10682 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 10683 ("softdep_setup_inofree called on non-softdep filesystem")); 10684 ump = VFSTOUFS(mp); 10685 ACQUIRE_LOCK(ump); 10686 fs = ump->um_fs; 10687 cgp = (struct cg *)bp->b_data; 10688 inosused = cg_inosused(cgp); 10689 if (isset(inosused, ino % fs->fs_ipg)) 10690 panic("softdep_setup_inofree: inode %ju not freed.", 10691 (uintmax_t)ino); 10692 if (inodedep_lookup(mp, ino, 0, &inodedep)) 10693 panic("softdep_setup_inofree: ino %ju has existing inodedep %p", 10694 (uintmax_t)ino, inodedep); 10695 if (wkhd) { 10696 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) { 10697 if (wk->wk_type != D_JADDREF) 10698 continue; 10699 WORKLIST_REMOVE(wk); 10700 /* 10701 * We can free immediately even if the jaddref 10702 * isn't attached in a background write as now 10703 * the bitmaps are reconciled. 10704 */ 10705 wk->wk_state |= COMPLETE | ATTACHED; 10706 free_jaddref(WK_JADDREF(wk)); 10707 } 10708 jwork_move(&bp->b_dep, wkhd); 10709 } 10710 FREE_LOCK(ump); 10711 } 10712 10713 10714 /* 10715 * Called via ffs_blkfree() after a set of frags has been cleared from a cg 10716 * map. Any dependencies waiting for the write to clear are added to the 10717 * buf's list and any jnewblks that are being canceled are discarded 10718 * immediately. 10719 */ 10720 void 10721 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 10722 struct mount *mp; 10723 struct buf *bp; 10724 ufs2_daddr_t blkno; 10725 int frags; 10726 struct workhead *wkhd; 10727 { 10728 struct bmsafemap *bmsafemap; 10729 struct jnewblk *jnewblk; 10730 struct ufsmount *ump; 10731 struct worklist *wk; 10732 struct fs *fs; 10733 #ifdef SUJ_DEBUG 10734 uint8_t *blksfree; 10735 struct cg *cgp; 10736 ufs2_daddr_t jstart; 10737 ufs2_daddr_t jend; 10738 ufs2_daddr_t end; 10739 long bno; 10740 int i; 10741 #endif 10742 10743 CTR3(KTR_SUJ, 10744 "softdep_setup_blkfree: blkno %jd frags %d wk head %p", 10745 blkno, frags, wkhd); 10746 10747 ump = VFSTOUFS(mp); 10748 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 10749 ("softdep_setup_blkfree called on non-softdep filesystem")); 10750 ACQUIRE_LOCK(ump); 10751 /* Lookup the bmsafemap so we track when it is dirty. */ 10752 fs = ump->um_fs; 10753 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 10754 /* 10755 * Detach any jnewblks which have been canceled. They must linger 10756 * until the bitmap is cleared again by ffs_blkfree() to prevent 10757 * an unjournaled allocation from hitting the disk. 10758 */ 10759 if (wkhd) { 10760 while ((wk = LIST_FIRST(wkhd)) != NULL) { 10761 CTR2(KTR_SUJ, 10762 "softdep_setup_blkfree: blkno %jd wk type %d", 10763 blkno, wk->wk_type); 10764 WORKLIST_REMOVE(wk); 10765 if (wk->wk_type != D_JNEWBLK) { 10766 WORKLIST_INSERT(&bmsafemap->sm_freehd, wk); 10767 continue; 10768 } 10769 jnewblk = WK_JNEWBLK(wk); 10770 KASSERT(jnewblk->jn_state & GOINGAWAY, 10771 ("softdep_setup_blkfree: jnewblk not canceled.")); 10772 #ifdef SUJ_DEBUG 10773 /* 10774 * Assert that this block is free in the bitmap 10775 * before we discard the jnewblk. 10776 */ 10777 cgp = (struct cg *)bp->b_data; 10778 blksfree = cg_blksfree(cgp); 10779 bno = dtogd(fs, jnewblk->jn_blkno); 10780 for (i = jnewblk->jn_oldfrags; 10781 i < jnewblk->jn_frags; i++) { 10782 if (isset(blksfree, bno + i)) 10783 continue; 10784 panic("softdep_setup_blkfree: not free"); 10785 } 10786 #endif 10787 /* 10788 * Even if it's not attached we can free immediately 10789 * as the new bitmap is correct. 10790 */ 10791 wk->wk_state |= COMPLETE | ATTACHED; 10792 free_jnewblk(jnewblk); 10793 } 10794 } 10795 10796 #ifdef SUJ_DEBUG 10797 /* 10798 * Assert that we are not freeing a block which has an outstanding 10799 * allocation dependency. 10800 */ 10801 fs = VFSTOUFS(mp)->um_fs; 10802 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 10803 end = blkno + frags; 10804 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 10805 /* 10806 * Don't match against blocks that will be freed when the 10807 * background write is done. 10808 */ 10809 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) == 10810 (COMPLETE | DEPCOMPLETE)) 10811 continue; 10812 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags; 10813 jend = jnewblk->jn_blkno + jnewblk->jn_frags; 10814 if ((blkno >= jstart && blkno < jend) || 10815 (end > jstart && end <= jend)) { 10816 printf("state 0x%X %jd - %d %d dep %p\n", 10817 jnewblk->jn_state, jnewblk->jn_blkno, 10818 jnewblk->jn_oldfrags, jnewblk->jn_frags, 10819 jnewblk->jn_dep); 10820 panic("softdep_setup_blkfree: " 10821 "%jd-%jd(%d) overlaps with %jd-%jd", 10822 blkno, end, frags, jstart, jend); 10823 } 10824 } 10825 #endif 10826 FREE_LOCK(ump); 10827 } 10828 10829 /* 10830 * Revert a block allocation when the journal record that describes it 10831 * is not yet written. 10832 */ 10833 static int 10834 jnewblk_rollback(jnewblk, fs, cgp, blksfree) 10835 struct jnewblk *jnewblk; 10836 struct fs *fs; 10837 struct cg *cgp; 10838 uint8_t *blksfree; 10839 { 10840 ufs1_daddr_t fragno; 10841 long cgbno, bbase; 10842 int frags, blk; 10843 int i; 10844 10845 frags = 0; 10846 cgbno = dtogd(fs, jnewblk->jn_blkno); 10847 /* 10848 * We have to test which frags need to be rolled back. We may 10849 * be operating on a stale copy when doing background writes. 10850 */ 10851 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) 10852 if (isclr(blksfree, cgbno + i)) 10853 frags++; 10854 if (frags == 0) 10855 return (0); 10856 /* 10857 * This is mostly ffs_blkfree() sans some validation and 10858 * superblock updates. 10859 */ 10860 if (frags == fs->fs_frag) { 10861 fragno = fragstoblks(fs, cgbno); 10862 ffs_setblock(fs, blksfree, fragno); 10863 ffs_clusteracct(fs, cgp, fragno, 1); 10864 cgp->cg_cs.cs_nbfree++; 10865 } else { 10866 cgbno += jnewblk->jn_oldfrags; 10867 bbase = cgbno - fragnum(fs, cgbno); 10868 /* Decrement the old frags. */ 10869 blk = blkmap(fs, blksfree, bbase); 10870 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 10871 /* Deallocate the fragment */ 10872 for (i = 0; i < frags; i++) 10873 setbit(blksfree, cgbno + i); 10874 cgp->cg_cs.cs_nffree += frags; 10875 /* Add back in counts associated with the new frags */ 10876 blk = blkmap(fs, blksfree, bbase); 10877 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 10878 /* If a complete block has been reassembled, account for it. */ 10879 fragno = fragstoblks(fs, bbase); 10880 if (ffs_isblock(fs, blksfree, fragno)) { 10881 cgp->cg_cs.cs_nffree -= fs->fs_frag; 10882 ffs_clusteracct(fs, cgp, fragno, 1); 10883 cgp->cg_cs.cs_nbfree++; 10884 } 10885 } 10886 stat_jnewblk++; 10887 jnewblk->jn_state &= ~ATTACHED; 10888 jnewblk->jn_state |= UNDONE; 10889 10890 return (frags); 10891 } 10892 10893 static void 10894 initiate_write_bmsafemap(bmsafemap, bp) 10895 struct bmsafemap *bmsafemap; 10896 struct buf *bp; /* The cg block. */ 10897 { 10898 struct jaddref *jaddref; 10899 struct jnewblk *jnewblk; 10900 uint8_t *inosused; 10901 uint8_t *blksfree; 10902 struct cg *cgp; 10903 struct fs *fs; 10904 ino_t ino; 10905 10906 /* 10907 * If this is a background write, we did this at the time that 10908 * the copy was made, so do not need to do it again. 10909 */ 10910 if (bmsafemap->sm_state & IOSTARTED) 10911 return; 10912 bmsafemap->sm_state |= IOSTARTED; 10913 /* 10914 * Clear any inode allocations which are pending journal writes. 10915 */ 10916 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) { 10917 cgp = (struct cg *)bp->b_data; 10918 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 10919 inosused = cg_inosused(cgp); 10920 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) { 10921 ino = jaddref->ja_ino % fs->fs_ipg; 10922 if (isset(inosused, ino)) { 10923 if ((jaddref->ja_mode & IFMT) == IFDIR) 10924 cgp->cg_cs.cs_ndir--; 10925 cgp->cg_cs.cs_nifree++; 10926 clrbit(inosused, ino); 10927 jaddref->ja_state &= ~ATTACHED; 10928 jaddref->ja_state |= UNDONE; 10929 stat_jaddref++; 10930 } else 10931 panic("initiate_write_bmsafemap: inode %ju " 10932 "marked free", (uintmax_t)jaddref->ja_ino); 10933 } 10934 } 10935 /* 10936 * Clear any block allocations which are pending journal writes. 10937 */ 10938 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 10939 cgp = (struct cg *)bp->b_data; 10940 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 10941 blksfree = cg_blksfree(cgp); 10942 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 10943 if (jnewblk_rollback(jnewblk, fs, cgp, blksfree)) 10944 continue; 10945 panic("initiate_write_bmsafemap: block %jd " 10946 "marked free", jnewblk->jn_blkno); 10947 } 10948 } 10949 /* 10950 * Move allocation lists to the written lists so they can be 10951 * cleared once the block write is complete. 10952 */ 10953 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr, 10954 inodedep, id_deps); 10955 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 10956 newblk, nb_deps); 10957 LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist, 10958 wk_list); 10959 } 10960 10961 /* 10962 * This routine is called during the completion interrupt 10963 * service routine for a disk write (from the procedure called 10964 * by the device driver to inform the filesystem caches of 10965 * a request completion). It should be called early in this 10966 * procedure, before the block is made available to other 10967 * processes or other routines are called. 10968 * 10969 */ 10970 static void 10971 softdep_disk_write_complete(bp) 10972 struct buf *bp; /* describes the completed disk write */ 10973 { 10974 struct worklist *wk; 10975 struct worklist *owk; 10976 struct ufsmount *ump; 10977 struct workhead reattach; 10978 struct freeblks *freeblks; 10979 struct buf *sbp; 10980 10981 /* 10982 * If an error occurred while doing the write, then the data 10983 * has not hit the disk and the dependencies cannot be processed. 10984 * But we do have to go through and roll forward any dependencies 10985 * that were rolled back before the disk write. 10986 */ 10987 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) { 10988 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 10989 switch (wk->wk_type) { 10990 10991 case D_PAGEDEP: 10992 handle_written_filepage(WK_PAGEDEP(wk), bp, 0); 10993 continue; 10994 10995 case D_INODEDEP: 10996 handle_written_inodeblock(WK_INODEDEP(wk), 10997 bp, 0); 10998 continue; 10999 11000 case D_BMSAFEMAP: 11001 handle_written_bmsafemap(WK_BMSAFEMAP(wk), 11002 bp, 0); 11003 continue; 11004 11005 case D_INDIRDEP: 11006 handle_written_indirdep(WK_INDIRDEP(wk), 11007 bp, &sbp, 0); 11008 continue; 11009 default: 11010 /* nothing to roll forward */ 11011 continue; 11012 } 11013 } 11014 return; 11015 } 11016 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL) 11017 return; 11018 ump = VFSTOUFS(wk->wk_mp); 11019 LIST_INIT(&reattach); 11020 /* 11021 * This lock must not be released anywhere in this code segment. 11022 */ 11023 sbp = NULL; 11024 owk = NULL; 11025 ACQUIRE_LOCK(ump); 11026 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) { 11027 WORKLIST_REMOVE(wk); 11028 atomic_add_long(&dep_write[wk->wk_type], 1); 11029 if (wk == owk) 11030 panic("duplicate worklist: %p\n", wk); 11031 owk = wk; 11032 switch (wk->wk_type) { 11033 11034 case D_PAGEDEP: 11035 if (handle_written_filepage(WK_PAGEDEP(wk), bp, 11036 WRITESUCCEEDED)) 11037 WORKLIST_INSERT(&reattach, wk); 11038 continue; 11039 11040 case D_INODEDEP: 11041 if (handle_written_inodeblock(WK_INODEDEP(wk), bp, 11042 WRITESUCCEEDED)) 11043 WORKLIST_INSERT(&reattach, wk); 11044 continue; 11045 11046 case D_BMSAFEMAP: 11047 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp, 11048 WRITESUCCEEDED)) 11049 WORKLIST_INSERT(&reattach, wk); 11050 continue; 11051 11052 case D_MKDIR: 11053 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 11054 continue; 11055 11056 case D_ALLOCDIRECT: 11057 wk->wk_state |= COMPLETE; 11058 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL); 11059 continue; 11060 11061 case D_ALLOCINDIR: 11062 wk->wk_state |= COMPLETE; 11063 handle_allocindir_partdone(WK_ALLOCINDIR(wk)); 11064 continue; 11065 11066 case D_INDIRDEP: 11067 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp, 11068 WRITESUCCEEDED)) 11069 WORKLIST_INSERT(&reattach, wk); 11070 continue; 11071 11072 case D_FREEBLKS: 11073 wk->wk_state |= COMPLETE; 11074 freeblks = WK_FREEBLKS(wk); 11075 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE && 11076 LIST_EMPTY(&freeblks->fb_jblkdephd)) 11077 add_to_worklist(wk, WK_NODELAY); 11078 continue; 11079 11080 case D_FREEWORK: 11081 handle_written_freework(WK_FREEWORK(wk)); 11082 break; 11083 11084 case D_JSEGDEP: 11085 free_jsegdep(WK_JSEGDEP(wk)); 11086 continue; 11087 11088 case D_JSEG: 11089 handle_written_jseg(WK_JSEG(wk), bp); 11090 continue; 11091 11092 case D_SBDEP: 11093 if (handle_written_sbdep(WK_SBDEP(wk), bp)) 11094 WORKLIST_INSERT(&reattach, wk); 11095 continue; 11096 11097 case D_FREEDEP: 11098 free_freedep(WK_FREEDEP(wk)); 11099 continue; 11100 11101 default: 11102 panic("handle_disk_write_complete: Unknown type %s", 11103 TYPENAME(wk->wk_type)); 11104 /* NOTREACHED */ 11105 } 11106 } 11107 /* 11108 * Reattach any requests that must be redone. 11109 */ 11110 while ((wk = LIST_FIRST(&reattach)) != NULL) { 11111 WORKLIST_REMOVE(wk); 11112 WORKLIST_INSERT(&bp->b_dep, wk); 11113 } 11114 FREE_LOCK(ump); 11115 if (sbp) 11116 brelse(sbp); 11117 } 11118 11119 /* 11120 * Called from within softdep_disk_write_complete above. Note that 11121 * this routine is always called from interrupt level with further 11122 * splbio interrupts blocked. 11123 */ 11124 static void 11125 handle_allocdirect_partdone(adp, wkhd) 11126 struct allocdirect *adp; /* the completed allocdirect */ 11127 struct workhead *wkhd; /* Work to do when inode is writtne. */ 11128 { 11129 struct allocdirectlst *listhead; 11130 struct allocdirect *listadp; 11131 struct inodedep *inodedep; 11132 long bsize; 11133 11134 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11135 return; 11136 /* 11137 * The on-disk inode cannot claim to be any larger than the last 11138 * fragment that has been written. Otherwise, the on-disk inode 11139 * might have fragments that were not the last block in the file 11140 * which would corrupt the filesystem. Thus, we cannot free any 11141 * allocdirects after one whose ad_oldblkno claims a fragment as 11142 * these blocks must be rolled back to zero before writing the inode. 11143 * We check the currently active set of allocdirects in id_inoupdt 11144 * or id_extupdt as appropriate. 11145 */ 11146 inodedep = adp->ad_inodedep; 11147 bsize = inodedep->id_fs->fs_bsize; 11148 if (adp->ad_state & EXTDATA) 11149 listhead = &inodedep->id_extupdt; 11150 else 11151 listhead = &inodedep->id_inoupdt; 11152 TAILQ_FOREACH(listadp, listhead, ad_next) { 11153 /* found our block */ 11154 if (listadp == adp) 11155 break; 11156 /* continue if ad_oldlbn is not a fragment */ 11157 if (listadp->ad_oldsize == 0 || 11158 listadp->ad_oldsize == bsize) 11159 continue; 11160 /* hit a fragment */ 11161 return; 11162 } 11163 /* 11164 * If we have reached the end of the current list without 11165 * finding the just finished dependency, then it must be 11166 * on the future dependency list. Future dependencies cannot 11167 * be freed until they are moved to the current list. 11168 */ 11169 if (listadp == NULL) { 11170 #ifdef DEBUG 11171 if (adp->ad_state & EXTDATA) 11172 listhead = &inodedep->id_newextupdt; 11173 else 11174 listhead = &inodedep->id_newinoupdt; 11175 TAILQ_FOREACH(listadp, listhead, ad_next) 11176 /* found our block */ 11177 if (listadp == adp) 11178 break; 11179 if (listadp == NULL) 11180 panic("handle_allocdirect_partdone: lost dep"); 11181 #endif /* DEBUG */ 11182 return; 11183 } 11184 /* 11185 * If we have found the just finished dependency, then queue 11186 * it along with anything that follows it that is complete. 11187 * Since the pointer has not yet been written in the inode 11188 * as the dependency prevents it, place the allocdirect on the 11189 * bufwait list where it will be freed once the pointer is 11190 * valid. 11191 */ 11192 if (wkhd == NULL) 11193 wkhd = &inodedep->id_bufwait; 11194 for (; adp; adp = listadp) { 11195 listadp = TAILQ_NEXT(adp, ad_next); 11196 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11197 return; 11198 TAILQ_REMOVE(listhead, adp, ad_next); 11199 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list); 11200 } 11201 } 11202 11203 /* 11204 * Called from within softdep_disk_write_complete above. This routine 11205 * completes successfully written allocindirs. 11206 */ 11207 static void 11208 handle_allocindir_partdone(aip) 11209 struct allocindir *aip; /* the completed allocindir */ 11210 { 11211 struct indirdep *indirdep; 11212 11213 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE) 11214 return; 11215 indirdep = aip->ai_indirdep; 11216 LIST_REMOVE(aip, ai_next); 11217 /* 11218 * Don't set a pointer while the buffer is undergoing IO or while 11219 * we have active truncations. 11220 */ 11221 if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) { 11222 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next); 11223 return; 11224 } 11225 if (indirdep->ir_state & UFS1FMT) 11226 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11227 aip->ai_newblkno; 11228 else 11229 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11230 aip->ai_newblkno; 11231 /* 11232 * Await the pointer write before freeing the allocindir. 11233 */ 11234 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next); 11235 } 11236 11237 /* 11238 * Release segments held on a jwork list. 11239 */ 11240 static void 11241 handle_jwork(wkhd) 11242 struct workhead *wkhd; 11243 { 11244 struct worklist *wk; 11245 11246 while ((wk = LIST_FIRST(wkhd)) != NULL) { 11247 WORKLIST_REMOVE(wk); 11248 switch (wk->wk_type) { 11249 case D_JSEGDEP: 11250 free_jsegdep(WK_JSEGDEP(wk)); 11251 continue; 11252 case D_FREEDEP: 11253 free_freedep(WK_FREEDEP(wk)); 11254 continue; 11255 case D_FREEFRAG: 11256 rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep)); 11257 WORKITEM_FREE(wk, D_FREEFRAG); 11258 continue; 11259 case D_FREEWORK: 11260 handle_written_freework(WK_FREEWORK(wk)); 11261 continue; 11262 default: 11263 panic("handle_jwork: Unknown type %s\n", 11264 TYPENAME(wk->wk_type)); 11265 } 11266 } 11267 } 11268 11269 /* 11270 * Handle the bufwait list on an inode when it is safe to release items 11271 * held there. This normally happens after an inode block is written but 11272 * may be delayed and handled later if there are pending journal items that 11273 * are not yet safe to be released. 11274 */ 11275 static struct freefile * 11276 handle_bufwait(inodedep, refhd) 11277 struct inodedep *inodedep; 11278 struct workhead *refhd; 11279 { 11280 struct jaddref *jaddref; 11281 struct freefile *freefile; 11282 struct worklist *wk; 11283 11284 freefile = NULL; 11285 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) { 11286 WORKLIST_REMOVE(wk); 11287 switch (wk->wk_type) { 11288 case D_FREEFILE: 11289 /* 11290 * We defer adding freefile to the worklist 11291 * until all other additions have been made to 11292 * ensure that it will be done after all the 11293 * old blocks have been freed. 11294 */ 11295 if (freefile != NULL) 11296 panic("handle_bufwait: freefile"); 11297 freefile = WK_FREEFILE(wk); 11298 continue; 11299 11300 case D_MKDIR: 11301 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT); 11302 continue; 11303 11304 case D_DIRADD: 11305 diradd_inode_written(WK_DIRADD(wk), inodedep); 11306 continue; 11307 11308 case D_FREEFRAG: 11309 wk->wk_state |= COMPLETE; 11310 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE) 11311 add_to_worklist(wk, 0); 11312 continue; 11313 11314 case D_DIRREM: 11315 wk->wk_state |= COMPLETE; 11316 add_to_worklist(wk, 0); 11317 continue; 11318 11319 case D_ALLOCDIRECT: 11320 case D_ALLOCINDIR: 11321 free_newblk(WK_NEWBLK(wk)); 11322 continue; 11323 11324 case D_JNEWBLK: 11325 wk->wk_state |= COMPLETE; 11326 free_jnewblk(WK_JNEWBLK(wk)); 11327 continue; 11328 11329 /* 11330 * Save freed journal segments and add references on 11331 * the supplied list which will delay their release 11332 * until the cg bitmap is cleared on disk. 11333 */ 11334 case D_JSEGDEP: 11335 if (refhd == NULL) 11336 free_jsegdep(WK_JSEGDEP(wk)); 11337 else 11338 WORKLIST_INSERT(refhd, wk); 11339 continue; 11340 11341 case D_JADDREF: 11342 jaddref = WK_JADDREF(wk); 11343 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 11344 if_deps); 11345 /* 11346 * Transfer any jaddrefs to the list to be freed with 11347 * the bitmap if we're handling a removed file. 11348 */ 11349 if (refhd == NULL) { 11350 wk->wk_state |= COMPLETE; 11351 free_jaddref(jaddref); 11352 } else 11353 WORKLIST_INSERT(refhd, wk); 11354 continue; 11355 11356 default: 11357 panic("handle_bufwait: Unknown type %p(%s)", 11358 wk, TYPENAME(wk->wk_type)); 11359 /* NOTREACHED */ 11360 } 11361 } 11362 return (freefile); 11363 } 11364 /* 11365 * Called from within softdep_disk_write_complete above to restore 11366 * in-memory inode block contents to their most up-to-date state. Note 11367 * that this routine is always called from interrupt level with further 11368 * interrupts from this device blocked. 11369 * 11370 * If the write did not succeed, we will do all the roll-forward 11371 * operations, but we will not take the actions that will allow its 11372 * dependencies to be processed. 11373 */ 11374 static int 11375 handle_written_inodeblock(inodedep, bp, flags) 11376 struct inodedep *inodedep; 11377 struct buf *bp; /* buffer containing the inode block */ 11378 int flags; 11379 { 11380 struct freefile *freefile; 11381 struct allocdirect *adp, *nextadp; 11382 struct ufs1_dinode *dp1 = NULL; 11383 struct ufs2_dinode *dp2 = NULL; 11384 struct workhead wkhd; 11385 int hadchanges, fstype; 11386 ino_t freelink; 11387 11388 LIST_INIT(&wkhd); 11389 hadchanges = 0; 11390 freefile = NULL; 11391 if ((inodedep->id_state & IOSTARTED) == 0) 11392 panic("handle_written_inodeblock: not started"); 11393 inodedep->id_state &= ~IOSTARTED; 11394 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) { 11395 fstype = UFS1; 11396 dp1 = (struct ufs1_dinode *)bp->b_data + 11397 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11398 freelink = dp1->di_freelink; 11399 } else { 11400 fstype = UFS2; 11401 dp2 = (struct ufs2_dinode *)bp->b_data + 11402 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11403 freelink = dp2->di_freelink; 11404 } 11405 /* 11406 * Leave this inodeblock dirty until it's in the list. 11407 */ 11408 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED && 11409 (flags & WRITESUCCEEDED)) { 11410 struct inodedep *inon; 11411 11412 inon = TAILQ_NEXT(inodedep, id_unlinked); 11413 if ((inon == NULL && freelink == 0) || 11414 (inon && inon->id_ino == freelink)) { 11415 if (inon) 11416 inon->id_state |= UNLINKPREV; 11417 inodedep->id_state |= UNLINKNEXT; 11418 } 11419 hadchanges = 1; 11420 } 11421 /* 11422 * If we had to rollback the inode allocation because of 11423 * bitmaps being incomplete, then simply restore it. 11424 * Keep the block dirty so that it will not be reclaimed until 11425 * all associated dependencies have been cleared and the 11426 * corresponding updates written to disk. 11427 */ 11428 if (inodedep->id_savedino1 != NULL) { 11429 hadchanges = 1; 11430 if (fstype == UFS1) 11431 *dp1 = *inodedep->id_savedino1; 11432 else 11433 *dp2 = *inodedep->id_savedino2; 11434 free(inodedep->id_savedino1, M_SAVEDINO); 11435 inodedep->id_savedino1 = NULL; 11436 if ((bp->b_flags & B_DELWRI) == 0) 11437 stat_inode_bitmap++; 11438 bdirty(bp); 11439 /* 11440 * If the inode is clear here and GOINGAWAY it will never 11441 * be written. Process the bufwait and clear any pending 11442 * work which may include the freefile. 11443 */ 11444 if (inodedep->id_state & GOINGAWAY) 11445 goto bufwait; 11446 return (1); 11447 } 11448 if (flags & WRITESUCCEEDED) 11449 inodedep->id_state |= COMPLETE; 11450 /* 11451 * Roll forward anything that had to be rolled back before 11452 * the inode could be updated. 11453 */ 11454 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) { 11455 nextadp = TAILQ_NEXT(adp, ad_next); 11456 if (adp->ad_state & ATTACHED) 11457 panic("handle_written_inodeblock: new entry"); 11458 if (fstype == UFS1) { 11459 if (adp->ad_offset < UFS_NDADDR) { 11460 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11461 panic("%s %s #%jd mismatch %d != %jd", 11462 "handle_written_inodeblock:", 11463 "direct pointer", 11464 (intmax_t)adp->ad_offset, 11465 dp1->di_db[adp->ad_offset], 11466 (intmax_t)adp->ad_oldblkno); 11467 dp1->di_db[adp->ad_offset] = adp->ad_newblkno; 11468 } else { 11469 if (dp1->di_ib[adp->ad_offset - UFS_NDADDR] != 11470 0) 11471 panic("%s: %s #%jd allocated as %d", 11472 "handle_written_inodeblock", 11473 "indirect pointer", 11474 (intmax_t)adp->ad_offset - 11475 UFS_NDADDR, 11476 dp1->di_ib[adp->ad_offset - 11477 UFS_NDADDR]); 11478 dp1->di_ib[adp->ad_offset - UFS_NDADDR] = 11479 adp->ad_newblkno; 11480 } 11481 } else { 11482 if (adp->ad_offset < UFS_NDADDR) { 11483 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11484 panic("%s: %s #%jd %s %jd != %jd", 11485 "handle_written_inodeblock", 11486 "direct pointer", 11487 (intmax_t)adp->ad_offset, "mismatch", 11488 (intmax_t)dp2->di_db[adp->ad_offset], 11489 (intmax_t)adp->ad_oldblkno); 11490 dp2->di_db[adp->ad_offset] = adp->ad_newblkno; 11491 } else { 11492 if (dp2->di_ib[adp->ad_offset - UFS_NDADDR] != 11493 0) 11494 panic("%s: %s #%jd allocated as %jd", 11495 "handle_written_inodeblock", 11496 "indirect pointer", 11497 (intmax_t)adp->ad_offset - 11498 UFS_NDADDR, 11499 (intmax_t) 11500 dp2->di_ib[adp->ad_offset - 11501 UFS_NDADDR]); 11502 dp2->di_ib[adp->ad_offset - UFS_NDADDR] = 11503 adp->ad_newblkno; 11504 } 11505 } 11506 adp->ad_state &= ~UNDONE; 11507 adp->ad_state |= ATTACHED; 11508 hadchanges = 1; 11509 } 11510 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) { 11511 nextadp = TAILQ_NEXT(adp, ad_next); 11512 if (adp->ad_state & ATTACHED) 11513 panic("handle_written_inodeblock: new entry"); 11514 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno) 11515 panic("%s: direct pointers #%jd %s %jd != %jd", 11516 "handle_written_inodeblock", 11517 (intmax_t)adp->ad_offset, "mismatch", 11518 (intmax_t)dp2->di_extb[adp->ad_offset], 11519 (intmax_t)adp->ad_oldblkno); 11520 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno; 11521 adp->ad_state &= ~UNDONE; 11522 adp->ad_state |= ATTACHED; 11523 hadchanges = 1; 11524 } 11525 if (hadchanges && (bp->b_flags & B_DELWRI) == 0) 11526 stat_direct_blk_ptrs++; 11527 /* 11528 * Reset the file size to its most up-to-date value. 11529 */ 11530 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1) 11531 panic("handle_written_inodeblock: bad size"); 11532 if (inodedep->id_savednlink > UFS_LINK_MAX) 11533 panic("handle_written_inodeblock: Invalid link count " 11534 "%jd for inodedep %p", (uintmax_t)inodedep->id_savednlink, 11535 inodedep); 11536 if (fstype == UFS1) { 11537 if (dp1->di_nlink != inodedep->id_savednlink) { 11538 dp1->di_nlink = inodedep->id_savednlink; 11539 hadchanges = 1; 11540 } 11541 if (dp1->di_size != inodedep->id_savedsize) { 11542 dp1->di_size = inodedep->id_savedsize; 11543 hadchanges = 1; 11544 } 11545 } else { 11546 if (dp2->di_nlink != inodedep->id_savednlink) { 11547 dp2->di_nlink = inodedep->id_savednlink; 11548 hadchanges = 1; 11549 } 11550 if (dp2->di_size != inodedep->id_savedsize) { 11551 dp2->di_size = inodedep->id_savedsize; 11552 hadchanges = 1; 11553 } 11554 if (dp2->di_extsize != inodedep->id_savedextsize) { 11555 dp2->di_extsize = inodedep->id_savedextsize; 11556 hadchanges = 1; 11557 } 11558 } 11559 inodedep->id_savedsize = -1; 11560 inodedep->id_savedextsize = -1; 11561 inodedep->id_savednlink = -1; 11562 /* 11563 * If there were any rollbacks in the inode block, then it must be 11564 * marked dirty so that its will eventually get written back in 11565 * its correct form. 11566 */ 11567 if (hadchanges) 11568 bdirty(bp); 11569 bufwait: 11570 /* 11571 * If the write did not succeed, we have done all the roll-forward 11572 * operations, but we cannot take the actions that will allow its 11573 * dependencies to be processed. 11574 */ 11575 if ((flags & WRITESUCCEEDED) == 0) 11576 return (hadchanges); 11577 /* 11578 * Process any allocdirects that completed during the update. 11579 */ 11580 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 11581 handle_allocdirect_partdone(adp, &wkhd); 11582 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 11583 handle_allocdirect_partdone(adp, &wkhd); 11584 /* 11585 * Process deallocations that were held pending until the 11586 * inode had been written to disk. Freeing of the inode 11587 * is delayed until after all blocks have been freed to 11588 * avoid creation of new <vfsid, inum, lbn> triples 11589 * before the old ones have been deleted. Completely 11590 * unlinked inodes are not processed until the unlinked 11591 * inode list is written or the last reference is removed. 11592 */ 11593 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) { 11594 freefile = handle_bufwait(inodedep, NULL); 11595 if (freefile && !LIST_EMPTY(&wkhd)) { 11596 WORKLIST_INSERT(&wkhd, &freefile->fx_list); 11597 freefile = NULL; 11598 } 11599 } 11600 /* 11601 * Move rolled forward dependency completions to the bufwait list 11602 * now that those that were already written have been processed. 11603 */ 11604 if (!LIST_EMPTY(&wkhd) && hadchanges == 0) 11605 panic("handle_written_inodeblock: bufwait but no changes"); 11606 jwork_move(&inodedep->id_bufwait, &wkhd); 11607 11608 if (freefile != NULL) { 11609 /* 11610 * If the inode is goingaway it was never written. Fake up 11611 * the state here so free_inodedep() can succeed. 11612 */ 11613 if (inodedep->id_state & GOINGAWAY) 11614 inodedep->id_state |= COMPLETE | DEPCOMPLETE; 11615 if (free_inodedep(inodedep) == 0) 11616 panic("handle_written_inodeblock: live inodedep %p", 11617 inodedep); 11618 add_to_worklist(&freefile->fx_list, 0); 11619 return (0); 11620 } 11621 11622 /* 11623 * If no outstanding dependencies, free it. 11624 */ 11625 if (free_inodedep(inodedep) || 11626 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 && 11627 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 && 11628 TAILQ_FIRST(&inodedep->id_extupdt) == 0 && 11629 LIST_FIRST(&inodedep->id_bufwait) == 0)) 11630 return (0); 11631 return (hadchanges); 11632 } 11633 11634 /* 11635 * Perform needed roll-forwards and kick off any dependencies that 11636 * can now be processed. 11637 * 11638 * If the write did not succeed, we will do all the roll-forward 11639 * operations, but we will not take the actions that will allow its 11640 * dependencies to be processed. 11641 */ 11642 static int 11643 handle_written_indirdep(indirdep, bp, bpp, flags) 11644 struct indirdep *indirdep; 11645 struct buf *bp; 11646 struct buf **bpp; 11647 int flags; 11648 { 11649 struct allocindir *aip; 11650 struct buf *sbp; 11651 int chgs; 11652 11653 if (indirdep->ir_state & GOINGAWAY) 11654 panic("handle_written_indirdep: indirdep gone"); 11655 if ((indirdep->ir_state & IOSTARTED) == 0) 11656 panic("handle_written_indirdep: IO not started"); 11657 chgs = 0; 11658 /* 11659 * If there were rollbacks revert them here. 11660 */ 11661 if (indirdep->ir_saveddata) { 11662 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount); 11663 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11664 free(indirdep->ir_saveddata, M_INDIRDEP); 11665 indirdep->ir_saveddata = NULL; 11666 } 11667 chgs = 1; 11668 } 11669 indirdep->ir_state &= ~(UNDONE | IOSTARTED); 11670 indirdep->ir_state |= ATTACHED; 11671 /* 11672 * If the write did not succeed, we have done all the roll-forward 11673 * operations, but we cannot take the actions that will allow its 11674 * dependencies to be processed. 11675 */ 11676 if ((flags & WRITESUCCEEDED) == 0) { 11677 stat_indir_blk_ptrs++; 11678 bdirty(bp); 11679 return (1); 11680 } 11681 /* 11682 * Move allocindirs with written pointers to the completehd if 11683 * the indirdep's pointer is not yet written. Otherwise 11684 * free them here. 11685 */ 11686 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) { 11687 LIST_REMOVE(aip, ai_next); 11688 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 11689 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip, 11690 ai_next); 11691 newblk_freefrag(&aip->ai_block); 11692 continue; 11693 } 11694 free_newblk(&aip->ai_block); 11695 } 11696 /* 11697 * Move allocindirs that have finished dependency processing from 11698 * the done list to the write list after updating the pointers. 11699 */ 11700 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11701 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) { 11702 handle_allocindir_partdone(aip); 11703 if (aip == LIST_FIRST(&indirdep->ir_donehd)) 11704 panic("disk_write_complete: not gone"); 11705 chgs = 1; 11706 } 11707 } 11708 /* 11709 * Preserve the indirdep if there were any changes or if it is not 11710 * yet valid on disk. 11711 */ 11712 if (chgs) { 11713 stat_indir_blk_ptrs++; 11714 bdirty(bp); 11715 return (1); 11716 } 11717 /* 11718 * If there were no changes we can discard the savedbp and detach 11719 * ourselves from the buf. We are only carrying completed pointers 11720 * in this case. 11721 */ 11722 sbp = indirdep->ir_savebp; 11723 sbp->b_flags |= B_INVAL | B_NOCACHE; 11724 indirdep->ir_savebp = NULL; 11725 indirdep->ir_bp = NULL; 11726 if (*bpp != NULL) 11727 panic("handle_written_indirdep: bp already exists."); 11728 *bpp = sbp; 11729 /* 11730 * The indirdep may not be freed until its parent points at it. 11731 */ 11732 if (indirdep->ir_state & DEPCOMPLETE) 11733 free_indirdep(indirdep); 11734 11735 return (0); 11736 } 11737 11738 /* 11739 * Process a diradd entry after its dependent inode has been written. 11740 * This routine must be called with splbio interrupts blocked. 11741 */ 11742 static void 11743 diradd_inode_written(dap, inodedep) 11744 struct diradd *dap; 11745 struct inodedep *inodedep; 11746 { 11747 11748 dap->da_state |= COMPLETE; 11749 complete_diradd(dap); 11750 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 11751 } 11752 11753 /* 11754 * Returns true if the bmsafemap will have rollbacks when written. Must only 11755 * be called with the per-filesystem lock and the buf lock on the cg held. 11756 */ 11757 static int 11758 bmsafemap_backgroundwrite(bmsafemap, bp) 11759 struct bmsafemap *bmsafemap; 11760 struct buf *bp; 11761 { 11762 int dirty; 11763 11764 LOCK_OWNED(VFSTOUFS(bmsafemap->sm_list.wk_mp)); 11765 dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) | 11766 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd); 11767 /* 11768 * If we're initiating a background write we need to process the 11769 * rollbacks as they exist now, not as they exist when IO starts. 11770 * No other consumers will look at the contents of the shadowed 11771 * buf so this is safe to do here. 11772 */ 11773 if (bp->b_xflags & BX_BKGRDMARKER) 11774 initiate_write_bmsafemap(bmsafemap, bp); 11775 11776 return (dirty); 11777 } 11778 11779 /* 11780 * Re-apply an allocation when a cg write is complete. 11781 */ 11782 static int 11783 jnewblk_rollforward(jnewblk, fs, cgp, blksfree) 11784 struct jnewblk *jnewblk; 11785 struct fs *fs; 11786 struct cg *cgp; 11787 uint8_t *blksfree; 11788 { 11789 ufs1_daddr_t fragno; 11790 ufs2_daddr_t blkno; 11791 long cgbno, bbase; 11792 int frags, blk; 11793 int i; 11794 11795 frags = 0; 11796 cgbno = dtogd(fs, jnewblk->jn_blkno); 11797 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) { 11798 if (isclr(blksfree, cgbno + i)) 11799 panic("jnewblk_rollforward: re-allocated fragment"); 11800 frags++; 11801 } 11802 if (frags == fs->fs_frag) { 11803 blkno = fragstoblks(fs, cgbno); 11804 ffs_clrblock(fs, blksfree, (long)blkno); 11805 ffs_clusteracct(fs, cgp, blkno, -1); 11806 cgp->cg_cs.cs_nbfree--; 11807 } else { 11808 bbase = cgbno - fragnum(fs, cgbno); 11809 cgbno += jnewblk->jn_oldfrags; 11810 /* If a complete block had been reassembled, account for it. */ 11811 fragno = fragstoblks(fs, bbase); 11812 if (ffs_isblock(fs, blksfree, fragno)) { 11813 cgp->cg_cs.cs_nffree += fs->fs_frag; 11814 ffs_clusteracct(fs, cgp, fragno, -1); 11815 cgp->cg_cs.cs_nbfree--; 11816 } 11817 /* Decrement the old frags. */ 11818 blk = blkmap(fs, blksfree, bbase); 11819 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 11820 /* Allocate the fragment */ 11821 for (i = 0; i < frags; i++) 11822 clrbit(blksfree, cgbno + i); 11823 cgp->cg_cs.cs_nffree -= frags; 11824 /* Add back in counts associated with the new frags */ 11825 blk = blkmap(fs, blksfree, bbase); 11826 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 11827 } 11828 return (frags); 11829 } 11830 11831 /* 11832 * Complete a write to a bmsafemap structure. Roll forward any bitmap 11833 * changes if it's not a background write. Set all written dependencies 11834 * to DEPCOMPLETE and free the structure if possible. 11835 * 11836 * If the write did not succeed, we will do all the roll-forward 11837 * operations, but we will not take the actions that will allow its 11838 * dependencies to be processed. 11839 */ 11840 static int 11841 handle_written_bmsafemap(bmsafemap, bp, flags) 11842 struct bmsafemap *bmsafemap; 11843 struct buf *bp; 11844 int flags; 11845 { 11846 struct newblk *newblk; 11847 struct inodedep *inodedep; 11848 struct jaddref *jaddref, *jatmp; 11849 struct jnewblk *jnewblk, *jntmp; 11850 struct ufsmount *ump; 11851 uint8_t *inosused; 11852 uint8_t *blksfree; 11853 struct cg *cgp; 11854 struct fs *fs; 11855 ino_t ino; 11856 int foreground; 11857 int chgs; 11858 11859 if ((bmsafemap->sm_state & IOSTARTED) == 0) 11860 panic("handle_written_bmsafemap: Not started\n"); 11861 ump = VFSTOUFS(bmsafemap->sm_list.wk_mp); 11862 chgs = 0; 11863 bmsafemap->sm_state &= ~IOSTARTED; 11864 foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0; 11865 /* 11866 * If write was successful, release journal work that was waiting 11867 * on the write. Otherwise move the work back. 11868 */ 11869 if (flags & WRITESUCCEEDED) 11870 handle_jwork(&bmsafemap->sm_freewr); 11871 else 11872 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, 11873 worklist, wk_list); 11874 11875 /* 11876 * Restore unwritten inode allocation pending jaddref writes. 11877 */ 11878 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) { 11879 cgp = (struct cg *)bp->b_data; 11880 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11881 inosused = cg_inosused(cgp); 11882 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd, 11883 ja_bmdeps, jatmp) { 11884 if ((jaddref->ja_state & UNDONE) == 0) 11885 continue; 11886 ino = jaddref->ja_ino % fs->fs_ipg; 11887 if (isset(inosused, ino)) 11888 panic("handle_written_bmsafemap: " 11889 "re-allocated inode"); 11890 /* Do the roll-forward only if it's a real copy. */ 11891 if (foreground) { 11892 if ((jaddref->ja_mode & IFMT) == IFDIR) 11893 cgp->cg_cs.cs_ndir++; 11894 cgp->cg_cs.cs_nifree--; 11895 setbit(inosused, ino); 11896 chgs = 1; 11897 } 11898 jaddref->ja_state &= ~UNDONE; 11899 jaddref->ja_state |= ATTACHED; 11900 free_jaddref(jaddref); 11901 } 11902 } 11903 /* 11904 * Restore any block allocations which are pending journal writes. 11905 */ 11906 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 11907 cgp = (struct cg *)bp->b_data; 11908 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11909 blksfree = cg_blksfree(cgp); 11910 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps, 11911 jntmp) { 11912 if ((jnewblk->jn_state & UNDONE) == 0) 11913 continue; 11914 /* Do the roll-forward only if it's a real copy. */ 11915 if (foreground && 11916 jnewblk_rollforward(jnewblk, fs, cgp, blksfree)) 11917 chgs = 1; 11918 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK); 11919 jnewblk->jn_state |= ATTACHED; 11920 free_jnewblk(jnewblk); 11921 } 11922 } 11923 /* 11924 * If the write did not succeed, we have done all the roll-forward 11925 * operations, but we cannot take the actions that will allow its 11926 * dependencies to be processed. 11927 */ 11928 if ((flags & WRITESUCCEEDED) == 0) { 11929 LIST_CONCAT(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 11930 newblk, nb_deps); 11931 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, 11932 worklist, wk_list); 11933 if (foreground) 11934 bdirty(bp); 11935 return (1); 11936 } 11937 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) { 11938 newblk->nb_state |= DEPCOMPLETE; 11939 newblk->nb_state &= ~ONDEPLIST; 11940 newblk->nb_bmsafemap = NULL; 11941 LIST_REMOVE(newblk, nb_deps); 11942 if (newblk->nb_list.wk_type == D_ALLOCDIRECT) 11943 handle_allocdirect_partdone( 11944 WK_ALLOCDIRECT(&newblk->nb_list), NULL); 11945 else if (newblk->nb_list.wk_type == D_ALLOCINDIR) 11946 handle_allocindir_partdone( 11947 WK_ALLOCINDIR(&newblk->nb_list)); 11948 else if (newblk->nb_list.wk_type != D_NEWBLK) 11949 panic("handle_written_bmsafemap: Unexpected type: %s", 11950 TYPENAME(newblk->nb_list.wk_type)); 11951 } 11952 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) { 11953 inodedep->id_state |= DEPCOMPLETE; 11954 inodedep->id_state &= ~ONDEPLIST; 11955 LIST_REMOVE(inodedep, id_deps); 11956 inodedep->id_bmsafemap = NULL; 11957 } 11958 LIST_REMOVE(bmsafemap, sm_next); 11959 if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) && 11960 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) && 11961 LIST_EMPTY(&bmsafemap->sm_newblkhd) && 11962 LIST_EMPTY(&bmsafemap->sm_inodedephd) && 11963 LIST_EMPTY(&bmsafemap->sm_freehd)) { 11964 LIST_REMOVE(bmsafemap, sm_hash); 11965 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 11966 return (0); 11967 } 11968 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 11969 if (foreground) 11970 bdirty(bp); 11971 return (1); 11972 } 11973 11974 /* 11975 * Try to free a mkdir dependency. 11976 */ 11977 static void 11978 complete_mkdir(mkdir) 11979 struct mkdir *mkdir; 11980 { 11981 struct diradd *dap; 11982 11983 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE) 11984 return; 11985 LIST_REMOVE(mkdir, md_mkdirs); 11986 dap = mkdir->md_diradd; 11987 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 11988 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) { 11989 dap->da_state |= DEPCOMPLETE; 11990 complete_diradd(dap); 11991 } 11992 WORKITEM_FREE(mkdir, D_MKDIR); 11993 } 11994 11995 /* 11996 * Handle the completion of a mkdir dependency. 11997 */ 11998 static void 11999 handle_written_mkdir(mkdir, type) 12000 struct mkdir *mkdir; 12001 int type; 12002 { 12003 12004 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type) 12005 panic("handle_written_mkdir: bad type"); 12006 mkdir->md_state |= COMPLETE; 12007 complete_mkdir(mkdir); 12008 } 12009 12010 static int 12011 free_pagedep(pagedep) 12012 struct pagedep *pagedep; 12013 { 12014 int i; 12015 12016 if (pagedep->pd_state & NEWBLOCK) 12017 return (0); 12018 if (!LIST_EMPTY(&pagedep->pd_dirremhd)) 12019 return (0); 12020 for (i = 0; i < DAHASHSZ; i++) 12021 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i])) 12022 return (0); 12023 if (!LIST_EMPTY(&pagedep->pd_pendinghd)) 12024 return (0); 12025 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd)) 12026 return (0); 12027 if (pagedep->pd_state & ONWORKLIST) 12028 WORKLIST_REMOVE(&pagedep->pd_list); 12029 LIST_REMOVE(pagedep, pd_hash); 12030 WORKITEM_FREE(pagedep, D_PAGEDEP); 12031 12032 return (1); 12033 } 12034 12035 /* 12036 * Called from within softdep_disk_write_complete above. 12037 * A write operation was just completed. Removed inodes can 12038 * now be freed and associated block pointers may be committed. 12039 * Note that this routine is always called from interrupt level 12040 * with further interrupts from this device blocked. 12041 * 12042 * If the write did not succeed, we will do all the roll-forward 12043 * operations, but we will not take the actions that will allow its 12044 * dependencies to be processed. 12045 */ 12046 static int 12047 handle_written_filepage(pagedep, bp, flags) 12048 struct pagedep *pagedep; 12049 struct buf *bp; /* buffer containing the written page */ 12050 int flags; 12051 { 12052 struct dirrem *dirrem; 12053 struct diradd *dap, *nextdap; 12054 struct direct *ep; 12055 int i, chgs; 12056 12057 if ((pagedep->pd_state & IOSTARTED) == 0) 12058 panic("handle_written_filepage: not started"); 12059 pagedep->pd_state &= ~IOSTARTED; 12060 if ((flags & WRITESUCCEEDED) == 0) 12061 goto rollforward; 12062 /* 12063 * Process any directory removals that have been committed. 12064 */ 12065 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) { 12066 LIST_REMOVE(dirrem, dm_next); 12067 dirrem->dm_state |= COMPLETE; 12068 dirrem->dm_dirinum = pagedep->pd_ino; 12069 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 12070 ("handle_written_filepage: Journal entries not written.")); 12071 add_to_worklist(&dirrem->dm_list, 0); 12072 } 12073 /* 12074 * Free any directory additions that have been committed. 12075 * If it is a newly allocated block, we have to wait until 12076 * the on-disk directory inode claims the new block. 12077 */ 12078 if ((pagedep->pd_state & NEWBLOCK) == 0) 12079 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 12080 free_diradd(dap, NULL); 12081 rollforward: 12082 /* 12083 * Uncommitted directory entries must be restored. 12084 */ 12085 for (chgs = 0, i = 0; i < DAHASHSZ; i++) { 12086 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap; 12087 dap = nextdap) { 12088 nextdap = LIST_NEXT(dap, da_pdlist); 12089 if (dap->da_state & ATTACHED) 12090 panic("handle_written_filepage: attached"); 12091 ep = (struct direct *) 12092 ((char *)bp->b_data + dap->da_offset); 12093 ep->d_ino = dap->da_newinum; 12094 dap->da_state &= ~UNDONE; 12095 dap->da_state |= ATTACHED; 12096 chgs = 1; 12097 /* 12098 * If the inode referenced by the directory has 12099 * been written out, then the dependency can be 12100 * moved to the pending list. 12101 */ 12102 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 12103 LIST_REMOVE(dap, da_pdlist); 12104 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, 12105 da_pdlist); 12106 } 12107 } 12108 } 12109 /* 12110 * If there were any rollbacks in the directory, then it must be 12111 * marked dirty so that its will eventually get written back in 12112 * its correct form. 12113 */ 12114 if (chgs || (flags & WRITESUCCEEDED) == 0) { 12115 if ((bp->b_flags & B_DELWRI) == 0) 12116 stat_dir_entry++; 12117 bdirty(bp); 12118 return (1); 12119 } 12120 /* 12121 * If we are not waiting for a new directory block to be 12122 * claimed by its inode, then the pagedep will be freed. 12123 * Otherwise it will remain to track any new entries on 12124 * the page in case they are fsync'ed. 12125 */ 12126 free_pagedep(pagedep); 12127 return (0); 12128 } 12129 12130 /* 12131 * Writing back in-core inode structures. 12132 * 12133 * The filesystem only accesses an inode's contents when it occupies an 12134 * "in-core" inode structure. These "in-core" structures are separate from 12135 * the page frames used to cache inode blocks. Only the latter are 12136 * transferred to/from the disk. So, when the updated contents of the 12137 * "in-core" inode structure are copied to the corresponding in-memory inode 12138 * block, the dependencies are also transferred. The following procedure is 12139 * called when copying a dirty "in-core" inode to a cached inode block. 12140 */ 12141 12142 /* 12143 * Called when an inode is loaded from disk. If the effective link count 12144 * differed from the actual link count when it was last flushed, then we 12145 * need to ensure that the correct effective link count is put back. 12146 */ 12147 void 12148 softdep_load_inodeblock(ip) 12149 struct inode *ip; /* the "in_core" copy of the inode */ 12150 { 12151 struct inodedep *inodedep; 12152 struct ufsmount *ump; 12153 12154 ump = ITOUMP(ip); 12155 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 12156 ("softdep_load_inodeblock called on non-softdep filesystem")); 12157 /* 12158 * Check for alternate nlink count. 12159 */ 12160 ip->i_effnlink = ip->i_nlink; 12161 ACQUIRE_LOCK(ump); 12162 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) { 12163 FREE_LOCK(ump); 12164 return; 12165 } 12166 ip->i_effnlink -= inodedep->id_nlinkdelta; 12167 FREE_LOCK(ump); 12168 } 12169 12170 /* 12171 * This routine is called just before the "in-core" inode 12172 * information is to be copied to the in-memory inode block. 12173 * Recall that an inode block contains several inodes. If 12174 * the force flag is set, then the dependencies will be 12175 * cleared so that the update can always be made. Note that 12176 * the buffer is locked when this routine is called, so we 12177 * will never be in the middle of writing the inode block 12178 * to disk. 12179 */ 12180 void 12181 softdep_update_inodeblock(ip, bp, waitfor) 12182 struct inode *ip; /* the "in_core" copy of the inode */ 12183 struct buf *bp; /* the buffer containing the inode block */ 12184 int waitfor; /* nonzero => update must be allowed */ 12185 { 12186 struct inodedep *inodedep; 12187 struct inoref *inoref; 12188 struct ufsmount *ump; 12189 struct worklist *wk; 12190 struct mount *mp; 12191 struct buf *ibp; 12192 struct fs *fs; 12193 int error; 12194 12195 ump = ITOUMP(ip); 12196 mp = UFSTOVFS(ump); 12197 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 12198 ("softdep_update_inodeblock called on non-softdep filesystem")); 12199 fs = ump->um_fs; 12200 /* 12201 * Preserve the freelink that is on disk. clear_unlinked_inodedep() 12202 * does not have access to the in-core ip so must write directly into 12203 * the inode block buffer when setting freelink. 12204 */ 12205 if (fs->fs_magic == FS_UFS1_MAGIC) 12206 DIP_SET(ip, i_freelink, ((struct ufs1_dinode *)bp->b_data + 12207 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12208 else 12209 DIP_SET(ip, i_freelink, ((struct ufs2_dinode *)bp->b_data + 12210 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12211 /* 12212 * If the effective link count is not equal to the actual link 12213 * count, then we must track the difference in an inodedep while 12214 * the inode is (potentially) tossed out of the cache. Otherwise, 12215 * if there is no existing inodedep, then there are no dependencies 12216 * to track. 12217 */ 12218 ACQUIRE_LOCK(ump); 12219 again: 12220 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12221 FREE_LOCK(ump); 12222 if (ip->i_effnlink != ip->i_nlink) 12223 panic("softdep_update_inodeblock: bad link count"); 12224 return; 12225 } 12226 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink) 12227 panic("softdep_update_inodeblock: bad delta"); 12228 /* 12229 * If we're flushing all dependencies we must also move any waiting 12230 * for journal writes onto the bufwait list prior to I/O. 12231 */ 12232 if (waitfor) { 12233 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12234 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12235 == DEPCOMPLETE) { 12236 jwait(&inoref->if_list, MNT_WAIT); 12237 goto again; 12238 } 12239 } 12240 } 12241 /* 12242 * Changes have been initiated. Anything depending on these 12243 * changes cannot occur until this inode has been written. 12244 */ 12245 inodedep->id_state &= ~COMPLETE; 12246 if ((inodedep->id_state & ONWORKLIST) == 0) 12247 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list); 12248 /* 12249 * Any new dependencies associated with the incore inode must 12250 * now be moved to the list associated with the buffer holding 12251 * the in-memory copy of the inode. Once merged process any 12252 * allocdirects that are completed by the merger. 12253 */ 12254 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt); 12255 if (!TAILQ_EMPTY(&inodedep->id_inoupdt)) 12256 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt), 12257 NULL); 12258 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt); 12259 if (!TAILQ_EMPTY(&inodedep->id_extupdt)) 12260 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt), 12261 NULL); 12262 /* 12263 * Now that the inode has been pushed into the buffer, the 12264 * operations dependent on the inode being written to disk 12265 * can be moved to the id_bufwait so that they will be 12266 * processed when the buffer I/O completes. 12267 */ 12268 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) { 12269 WORKLIST_REMOVE(wk); 12270 WORKLIST_INSERT(&inodedep->id_bufwait, wk); 12271 } 12272 /* 12273 * Newly allocated inodes cannot be written until the bitmap 12274 * that allocates them have been written (indicated by 12275 * DEPCOMPLETE being set in id_state). If we are doing a 12276 * forced sync (e.g., an fsync on a file), we force the bitmap 12277 * to be written so that the update can be done. 12278 */ 12279 if (waitfor == 0) { 12280 FREE_LOCK(ump); 12281 return; 12282 } 12283 retry: 12284 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) { 12285 FREE_LOCK(ump); 12286 return; 12287 } 12288 ibp = inodedep->id_bmsafemap->sm_buf; 12289 ibp = getdirtybuf(ibp, LOCK_PTR(ump), MNT_WAIT); 12290 if (ibp == NULL) { 12291 /* 12292 * If ibp came back as NULL, the dependency could have been 12293 * freed while we slept. Look it up again, and check to see 12294 * that it has completed. 12295 */ 12296 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 12297 goto retry; 12298 FREE_LOCK(ump); 12299 return; 12300 } 12301 FREE_LOCK(ump); 12302 if ((error = bwrite(ibp)) != 0) 12303 softdep_error("softdep_update_inodeblock: bwrite", error); 12304 } 12305 12306 /* 12307 * Merge the a new inode dependency list (such as id_newinoupdt) into an 12308 * old inode dependency list (such as id_inoupdt). This routine must be 12309 * called with splbio interrupts blocked. 12310 */ 12311 static void 12312 merge_inode_lists(newlisthead, oldlisthead) 12313 struct allocdirectlst *newlisthead; 12314 struct allocdirectlst *oldlisthead; 12315 { 12316 struct allocdirect *listadp, *newadp; 12317 12318 newadp = TAILQ_FIRST(newlisthead); 12319 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) { 12320 if (listadp->ad_offset < newadp->ad_offset) { 12321 listadp = TAILQ_NEXT(listadp, ad_next); 12322 continue; 12323 } 12324 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12325 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next); 12326 if (listadp->ad_offset == newadp->ad_offset) { 12327 allocdirect_merge(oldlisthead, newadp, 12328 listadp); 12329 listadp = newadp; 12330 } 12331 newadp = TAILQ_FIRST(newlisthead); 12332 } 12333 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) { 12334 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12335 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next); 12336 } 12337 } 12338 12339 /* 12340 * If we are doing an fsync, then we must ensure that any directory 12341 * entries for the inode have been written after the inode gets to disk. 12342 */ 12343 int 12344 softdep_fsync(vp) 12345 struct vnode *vp; /* the "in_core" copy of the inode */ 12346 { 12347 struct inodedep *inodedep; 12348 struct pagedep *pagedep; 12349 struct inoref *inoref; 12350 struct ufsmount *ump; 12351 struct worklist *wk; 12352 struct diradd *dap; 12353 struct mount *mp; 12354 struct vnode *pvp; 12355 struct inode *ip; 12356 struct buf *bp; 12357 struct fs *fs; 12358 struct thread *td = curthread; 12359 int error, flushparent, pagedep_new_block; 12360 ino_t parentino; 12361 ufs_lbn_t lbn; 12362 12363 ip = VTOI(vp); 12364 mp = vp->v_mount; 12365 ump = VFSTOUFS(mp); 12366 fs = ump->um_fs; 12367 if (MOUNTEDSOFTDEP(mp) == 0) 12368 return (0); 12369 ACQUIRE_LOCK(ump); 12370 restart: 12371 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12372 FREE_LOCK(ump); 12373 return (0); 12374 } 12375 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12376 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12377 == DEPCOMPLETE) { 12378 jwait(&inoref->if_list, MNT_WAIT); 12379 goto restart; 12380 } 12381 } 12382 if (!LIST_EMPTY(&inodedep->id_inowait) || 12383 !TAILQ_EMPTY(&inodedep->id_extupdt) || 12384 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 12385 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 12386 !TAILQ_EMPTY(&inodedep->id_newinoupdt)) 12387 panic("softdep_fsync: pending ops %p", inodedep); 12388 for (error = 0, flushparent = 0; ; ) { 12389 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL) 12390 break; 12391 if (wk->wk_type != D_DIRADD) 12392 panic("softdep_fsync: Unexpected type %s", 12393 TYPENAME(wk->wk_type)); 12394 dap = WK_DIRADD(wk); 12395 /* 12396 * Flush our parent if this directory entry has a MKDIR_PARENT 12397 * dependency or is contained in a newly allocated block. 12398 */ 12399 if (dap->da_state & DIRCHG) 12400 pagedep = dap->da_previous->dm_pagedep; 12401 else 12402 pagedep = dap->da_pagedep; 12403 parentino = pagedep->pd_ino; 12404 lbn = pagedep->pd_lbn; 12405 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) 12406 panic("softdep_fsync: dirty"); 12407 if ((dap->da_state & MKDIR_PARENT) || 12408 (pagedep->pd_state & NEWBLOCK)) 12409 flushparent = 1; 12410 else 12411 flushparent = 0; 12412 /* 12413 * If we are being fsync'ed as part of vgone'ing this vnode, 12414 * then we will not be able to release and recover the 12415 * vnode below, so we just have to give up on writing its 12416 * directory entry out. It will eventually be written, just 12417 * not now, but then the user was not asking to have it 12418 * written, so we are not breaking any promises. 12419 */ 12420 if (vp->v_iflag & VI_DOOMED) 12421 break; 12422 /* 12423 * We prevent deadlock by always fetching inodes from the 12424 * root, moving down the directory tree. Thus, when fetching 12425 * our parent directory, we first try to get the lock. If 12426 * that fails, we must unlock ourselves before requesting 12427 * the lock on our parent. See the comment in ufs_lookup 12428 * for details on possible races. 12429 */ 12430 FREE_LOCK(ump); 12431 if (ffs_vgetf(mp, parentino, LK_NOWAIT | LK_EXCLUSIVE, &pvp, 12432 FFSV_FORCEINSMQ)) { 12433 error = vfs_busy(mp, MBF_NOWAIT); 12434 if (error != 0) { 12435 vfs_ref(mp); 12436 VOP_UNLOCK(vp, 0); 12437 error = vfs_busy(mp, 0); 12438 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 12439 vfs_rel(mp); 12440 if (error != 0) 12441 return (ENOENT); 12442 if (vp->v_iflag & VI_DOOMED) { 12443 vfs_unbusy(mp); 12444 return (ENOENT); 12445 } 12446 } 12447 VOP_UNLOCK(vp, 0); 12448 error = ffs_vgetf(mp, parentino, LK_EXCLUSIVE, 12449 &pvp, FFSV_FORCEINSMQ); 12450 vfs_unbusy(mp); 12451 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 12452 if (vp->v_iflag & VI_DOOMED) { 12453 if (error == 0) 12454 vput(pvp); 12455 error = ENOENT; 12456 } 12457 if (error != 0) 12458 return (error); 12459 } 12460 /* 12461 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps 12462 * that are contained in direct blocks will be resolved by 12463 * doing a ffs_update. Pagedeps contained in indirect blocks 12464 * may require a complete sync'ing of the directory. So, we 12465 * try the cheap and fast ffs_update first, and if that fails, 12466 * then we do the slower ffs_syncvnode of the directory. 12467 */ 12468 if (flushparent) { 12469 int locked; 12470 12471 if ((error = ffs_update(pvp, 1)) != 0) { 12472 vput(pvp); 12473 return (error); 12474 } 12475 ACQUIRE_LOCK(ump); 12476 locked = 1; 12477 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) { 12478 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) { 12479 if (wk->wk_type != D_DIRADD) 12480 panic("softdep_fsync: Unexpected type %s", 12481 TYPENAME(wk->wk_type)); 12482 dap = WK_DIRADD(wk); 12483 if (dap->da_state & DIRCHG) 12484 pagedep = dap->da_previous->dm_pagedep; 12485 else 12486 pagedep = dap->da_pagedep; 12487 pagedep_new_block = pagedep->pd_state & NEWBLOCK; 12488 FREE_LOCK(ump); 12489 locked = 0; 12490 if (pagedep_new_block && (error = 12491 ffs_syncvnode(pvp, MNT_WAIT, 0))) { 12492 vput(pvp); 12493 return (error); 12494 } 12495 } 12496 } 12497 if (locked) 12498 FREE_LOCK(ump); 12499 } 12500 /* 12501 * Flush directory page containing the inode's name. 12502 */ 12503 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred, 12504 &bp); 12505 if (error == 0) 12506 error = bwrite(bp); 12507 else 12508 brelse(bp); 12509 vput(pvp); 12510 if (error != 0) 12511 return (error); 12512 ACQUIRE_LOCK(ump); 12513 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 12514 break; 12515 } 12516 FREE_LOCK(ump); 12517 return (0); 12518 } 12519 12520 /* 12521 * Flush all the dirty bitmaps associated with the block device 12522 * before flushing the rest of the dirty blocks so as to reduce 12523 * the number of dependencies that will have to be rolled back. 12524 * 12525 * XXX Unused? 12526 */ 12527 void 12528 softdep_fsync_mountdev(vp) 12529 struct vnode *vp; 12530 { 12531 struct buf *bp, *nbp; 12532 struct worklist *wk; 12533 struct bufobj *bo; 12534 12535 if (!vn_isdisk(vp, NULL)) 12536 panic("softdep_fsync_mountdev: vnode not a disk"); 12537 bo = &vp->v_bufobj; 12538 restart: 12539 BO_LOCK(bo); 12540 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 12541 /* 12542 * If it is already scheduled, skip to the next buffer. 12543 */ 12544 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) 12545 continue; 12546 12547 if ((bp->b_flags & B_DELWRI) == 0) 12548 panic("softdep_fsync_mountdev: not dirty"); 12549 /* 12550 * We are only interested in bitmaps with outstanding 12551 * dependencies. 12552 */ 12553 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL || 12554 wk->wk_type != D_BMSAFEMAP || 12555 (bp->b_vflags & BV_BKGRDINPROG)) { 12556 BUF_UNLOCK(bp); 12557 continue; 12558 } 12559 BO_UNLOCK(bo); 12560 bremfree(bp); 12561 (void) bawrite(bp); 12562 goto restart; 12563 } 12564 drain_output(vp); 12565 BO_UNLOCK(bo); 12566 } 12567 12568 /* 12569 * Sync all cylinder groups that were dirty at the time this function is 12570 * called. Newly dirtied cgs will be inserted before the sentinel. This 12571 * is used to flush freedep activity that may be holding up writes to a 12572 * indirect block. 12573 */ 12574 static int 12575 sync_cgs(mp, waitfor) 12576 struct mount *mp; 12577 int waitfor; 12578 { 12579 struct bmsafemap *bmsafemap; 12580 struct bmsafemap *sentinel; 12581 struct ufsmount *ump; 12582 struct buf *bp; 12583 int error; 12584 12585 sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK); 12586 sentinel->sm_cg = -1; 12587 ump = VFSTOUFS(mp); 12588 error = 0; 12589 ACQUIRE_LOCK(ump); 12590 LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next); 12591 for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL; 12592 bmsafemap = LIST_NEXT(sentinel, sm_next)) { 12593 /* Skip sentinels and cgs with no work to release. */ 12594 if (bmsafemap->sm_cg == -1 || 12595 (LIST_EMPTY(&bmsafemap->sm_freehd) && 12596 LIST_EMPTY(&bmsafemap->sm_freewr))) { 12597 LIST_REMOVE(sentinel, sm_next); 12598 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 12599 continue; 12600 } 12601 /* 12602 * If we don't get the lock and we're waiting try again, if 12603 * not move on to the next buf and try to sync it. 12604 */ 12605 bp = getdirtybuf(bmsafemap->sm_buf, LOCK_PTR(ump), waitfor); 12606 if (bp == NULL && waitfor == MNT_WAIT) 12607 continue; 12608 LIST_REMOVE(sentinel, sm_next); 12609 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 12610 if (bp == NULL) 12611 continue; 12612 FREE_LOCK(ump); 12613 if (waitfor == MNT_NOWAIT) 12614 bawrite(bp); 12615 else 12616 error = bwrite(bp); 12617 ACQUIRE_LOCK(ump); 12618 if (error) 12619 break; 12620 } 12621 LIST_REMOVE(sentinel, sm_next); 12622 FREE_LOCK(ump); 12623 free(sentinel, M_BMSAFEMAP); 12624 return (error); 12625 } 12626 12627 /* 12628 * This routine is called when we are trying to synchronously flush a 12629 * file. This routine must eliminate any filesystem metadata dependencies 12630 * so that the syncing routine can succeed. 12631 */ 12632 int 12633 softdep_sync_metadata(struct vnode *vp) 12634 { 12635 struct inode *ip; 12636 int error; 12637 12638 ip = VTOI(vp); 12639 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 12640 ("softdep_sync_metadata called on non-softdep filesystem")); 12641 /* 12642 * Ensure that any direct block dependencies have been cleared, 12643 * truncations are started, and inode references are journaled. 12644 */ 12645 ACQUIRE_LOCK(VFSTOUFS(vp->v_mount)); 12646 /* 12647 * Write all journal records to prevent rollbacks on devvp. 12648 */ 12649 if (vp->v_type == VCHR) 12650 softdep_flushjournal(vp->v_mount); 12651 error = flush_inodedep_deps(vp, vp->v_mount, ip->i_number); 12652 /* 12653 * Ensure that all truncates are written so we won't find deps on 12654 * indirect blocks. 12655 */ 12656 process_truncates(vp); 12657 FREE_LOCK(VFSTOUFS(vp->v_mount)); 12658 12659 return (error); 12660 } 12661 12662 /* 12663 * This routine is called when we are attempting to sync a buf with 12664 * dependencies. If waitfor is MNT_NOWAIT it attempts to schedule any 12665 * other IO it can but returns EBUSY if the buffer is not yet able to 12666 * be written. Dependencies which will not cause rollbacks will always 12667 * return 0. 12668 */ 12669 int 12670 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 12671 { 12672 struct indirdep *indirdep; 12673 struct pagedep *pagedep; 12674 struct allocindir *aip; 12675 struct newblk *newblk; 12676 struct ufsmount *ump; 12677 struct buf *nbp; 12678 struct worklist *wk; 12679 int i, error; 12680 12681 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 12682 ("softdep_sync_buf called on non-softdep filesystem")); 12683 /* 12684 * For VCHR we just don't want to force flush any dependencies that 12685 * will cause rollbacks. 12686 */ 12687 if (vp->v_type == VCHR) { 12688 if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0)) 12689 return (EBUSY); 12690 return (0); 12691 } 12692 ump = VFSTOUFS(vp->v_mount); 12693 ACQUIRE_LOCK(ump); 12694 /* 12695 * As we hold the buffer locked, none of its dependencies 12696 * will disappear. 12697 */ 12698 error = 0; 12699 top: 12700 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 12701 switch (wk->wk_type) { 12702 12703 case D_ALLOCDIRECT: 12704 case D_ALLOCINDIR: 12705 newblk = WK_NEWBLK(wk); 12706 if (newblk->nb_jnewblk != NULL) { 12707 if (waitfor == MNT_NOWAIT) { 12708 error = EBUSY; 12709 goto out_unlock; 12710 } 12711 jwait(&newblk->nb_jnewblk->jn_list, waitfor); 12712 goto top; 12713 } 12714 if (newblk->nb_state & DEPCOMPLETE || 12715 waitfor == MNT_NOWAIT) 12716 continue; 12717 nbp = newblk->nb_bmsafemap->sm_buf; 12718 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 12719 if (nbp == NULL) 12720 goto top; 12721 FREE_LOCK(ump); 12722 if ((error = bwrite(nbp)) != 0) 12723 goto out; 12724 ACQUIRE_LOCK(ump); 12725 continue; 12726 12727 case D_INDIRDEP: 12728 indirdep = WK_INDIRDEP(wk); 12729 if (waitfor == MNT_NOWAIT) { 12730 if (!TAILQ_EMPTY(&indirdep->ir_trunc) || 12731 !LIST_EMPTY(&indirdep->ir_deplisthd)) { 12732 error = EBUSY; 12733 goto out_unlock; 12734 } 12735 } 12736 if (!TAILQ_EMPTY(&indirdep->ir_trunc)) 12737 panic("softdep_sync_buf: truncation pending."); 12738 restart: 12739 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 12740 newblk = (struct newblk *)aip; 12741 if (newblk->nb_jnewblk != NULL) { 12742 jwait(&newblk->nb_jnewblk->jn_list, 12743 waitfor); 12744 goto restart; 12745 } 12746 if (newblk->nb_state & DEPCOMPLETE) 12747 continue; 12748 nbp = newblk->nb_bmsafemap->sm_buf; 12749 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 12750 if (nbp == NULL) 12751 goto restart; 12752 FREE_LOCK(ump); 12753 if ((error = bwrite(nbp)) != 0) 12754 goto out; 12755 ACQUIRE_LOCK(ump); 12756 goto restart; 12757 } 12758 continue; 12759 12760 case D_PAGEDEP: 12761 /* 12762 * Only flush directory entries in synchronous passes. 12763 */ 12764 if (waitfor != MNT_WAIT) { 12765 error = EBUSY; 12766 goto out_unlock; 12767 } 12768 /* 12769 * While syncing snapshots, we must allow recursive 12770 * lookups. 12771 */ 12772 BUF_AREC(bp); 12773 /* 12774 * We are trying to sync a directory that may 12775 * have dependencies on both its own metadata 12776 * and/or dependencies on the inodes of any 12777 * recently allocated files. We walk its diradd 12778 * lists pushing out the associated inode. 12779 */ 12780 pagedep = WK_PAGEDEP(wk); 12781 for (i = 0; i < DAHASHSZ; i++) { 12782 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0) 12783 continue; 12784 if ((error = flush_pagedep_deps(vp, wk->wk_mp, 12785 &pagedep->pd_diraddhd[i]))) { 12786 BUF_NOREC(bp); 12787 goto out_unlock; 12788 } 12789 } 12790 BUF_NOREC(bp); 12791 continue; 12792 12793 case D_FREEWORK: 12794 case D_FREEDEP: 12795 case D_JSEGDEP: 12796 case D_JNEWBLK: 12797 continue; 12798 12799 default: 12800 panic("softdep_sync_buf: Unknown type %s", 12801 TYPENAME(wk->wk_type)); 12802 /* NOTREACHED */ 12803 } 12804 } 12805 out_unlock: 12806 FREE_LOCK(ump); 12807 out: 12808 return (error); 12809 } 12810 12811 /* 12812 * Flush the dependencies associated with an inodedep. 12813 * Called with splbio blocked. 12814 */ 12815 static int 12816 flush_inodedep_deps(vp, mp, ino) 12817 struct vnode *vp; 12818 struct mount *mp; 12819 ino_t ino; 12820 { 12821 struct inodedep *inodedep; 12822 struct inoref *inoref; 12823 struct ufsmount *ump; 12824 int error, waitfor; 12825 12826 /* 12827 * This work is done in two passes. The first pass grabs most 12828 * of the buffers and begins asynchronously writing them. The 12829 * only way to wait for these asynchronous writes is to sleep 12830 * on the filesystem vnode which may stay busy for a long time 12831 * if the filesystem is active. So, instead, we make a second 12832 * pass over the dependencies blocking on each write. In the 12833 * usual case we will be blocking against a write that we 12834 * initiated, so when it is done the dependency will have been 12835 * resolved. Thus the second pass is expected to end quickly. 12836 * We give a brief window at the top of the loop to allow 12837 * any pending I/O to complete. 12838 */ 12839 ump = VFSTOUFS(mp); 12840 LOCK_OWNED(ump); 12841 for (error = 0, waitfor = MNT_NOWAIT; ; ) { 12842 if (error) 12843 return (error); 12844 FREE_LOCK(ump); 12845 ACQUIRE_LOCK(ump); 12846 restart: 12847 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 12848 return (0); 12849 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12850 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12851 == DEPCOMPLETE) { 12852 jwait(&inoref->if_list, MNT_WAIT); 12853 goto restart; 12854 } 12855 } 12856 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) || 12857 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) || 12858 flush_deplist(&inodedep->id_extupdt, waitfor, &error) || 12859 flush_deplist(&inodedep->id_newextupdt, waitfor, &error)) 12860 continue; 12861 /* 12862 * If pass2, we are done, otherwise do pass 2. 12863 */ 12864 if (waitfor == MNT_WAIT) 12865 break; 12866 waitfor = MNT_WAIT; 12867 } 12868 /* 12869 * Try freeing inodedep in case all dependencies have been removed. 12870 */ 12871 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0) 12872 (void) free_inodedep(inodedep); 12873 return (0); 12874 } 12875 12876 /* 12877 * Flush an inode dependency list. 12878 * Called with splbio blocked. 12879 */ 12880 static int 12881 flush_deplist(listhead, waitfor, errorp) 12882 struct allocdirectlst *listhead; 12883 int waitfor; 12884 int *errorp; 12885 { 12886 struct allocdirect *adp; 12887 struct newblk *newblk; 12888 struct ufsmount *ump; 12889 struct buf *bp; 12890 12891 if ((adp = TAILQ_FIRST(listhead)) == NULL) 12892 return (0); 12893 ump = VFSTOUFS(adp->ad_list.wk_mp); 12894 LOCK_OWNED(ump); 12895 TAILQ_FOREACH(adp, listhead, ad_next) { 12896 newblk = (struct newblk *)adp; 12897 if (newblk->nb_jnewblk != NULL) { 12898 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 12899 return (1); 12900 } 12901 if (newblk->nb_state & DEPCOMPLETE) 12902 continue; 12903 bp = newblk->nb_bmsafemap->sm_buf; 12904 bp = getdirtybuf(bp, LOCK_PTR(ump), waitfor); 12905 if (bp == NULL) { 12906 if (waitfor == MNT_NOWAIT) 12907 continue; 12908 return (1); 12909 } 12910 FREE_LOCK(ump); 12911 if (waitfor == MNT_NOWAIT) 12912 bawrite(bp); 12913 else 12914 *errorp = bwrite(bp); 12915 ACQUIRE_LOCK(ump); 12916 return (1); 12917 } 12918 return (0); 12919 } 12920 12921 /* 12922 * Flush dependencies associated with an allocdirect block. 12923 */ 12924 static int 12925 flush_newblk_dep(vp, mp, lbn) 12926 struct vnode *vp; 12927 struct mount *mp; 12928 ufs_lbn_t lbn; 12929 { 12930 struct newblk *newblk; 12931 struct ufsmount *ump; 12932 struct bufobj *bo; 12933 struct inode *ip; 12934 struct buf *bp; 12935 ufs2_daddr_t blkno; 12936 int error; 12937 12938 error = 0; 12939 bo = &vp->v_bufobj; 12940 ip = VTOI(vp); 12941 blkno = DIP(ip, i_db[lbn]); 12942 if (blkno == 0) 12943 panic("flush_newblk_dep: Missing block"); 12944 ump = VFSTOUFS(mp); 12945 ACQUIRE_LOCK(ump); 12946 /* 12947 * Loop until all dependencies related to this block are satisfied. 12948 * We must be careful to restart after each sleep in case a write 12949 * completes some part of this process for us. 12950 */ 12951 for (;;) { 12952 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) { 12953 FREE_LOCK(ump); 12954 break; 12955 } 12956 if (newblk->nb_list.wk_type != D_ALLOCDIRECT) 12957 panic("flush_newblk_deps: Bad newblk %p", newblk); 12958 /* 12959 * Flush the journal. 12960 */ 12961 if (newblk->nb_jnewblk != NULL) { 12962 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 12963 continue; 12964 } 12965 /* 12966 * Write the bitmap dependency. 12967 */ 12968 if ((newblk->nb_state & DEPCOMPLETE) == 0) { 12969 bp = newblk->nb_bmsafemap->sm_buf; 12970 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 12971 if (bp == NULL) 12972 continue; 12973 FREE_LOCK(ump); 12974 error = bwrite(bp); 12975 if (error) 12976 break; 12977 ACQUIRE_LOCK(ump); 12978 continue; 12979 } 12980 /* 12981 * Write the buffer. 12982 */ 12983 FREE_LOCK(ump); 12984 BO_LOCK(bo); 12985 bp = gbincore(bo, lbn); 12986 if (bp != NULL) { 12987 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 12988 LK_INTERLOCK, BO_LOCKPTR(bo)); 12989 if (error == ENOLCK) { 12990 ACQUIRE_LOCK(ump); 12991 error = 0; 12992 continue; /* Slept, retry */ 12993 } 12994 if (error != 0) 12995 break; /* Failed */ 12996 if (bp->b_flags & B_DELWRI) { 12997 bremfree(bp); 12998 error = bwrite(bp); 12999 if (error) 13000 break; 13001 } else 13002 BUF_UNLOCK(bp); 13003 } else 13004 BO_UNLOCK(bo); 13005 /* 13006 * We have to wait for the direct pointers to 13007 * point at the newdirblk before the dependency 13008 * will go away. 13009 */ 13010 error = ffs_update(vp, 1); 13011 if (error) 13012 break; 13013 ACQUIRE_LOCK(ump); 13014 } 13015 return (error); 13016 } 13017 13018 /* 13019 * Eliminate a pagedep dependency by flushing out all its diradd dependencies. 13020 * Called with splbio blocked. 13021 */ 13022 static int 13023 flush_pagedep_deps(pvp, mp, diraddhdp) 13024 struct vnode *pvp; 13025 struct mount *mp; 13026 struct diraddhd *diraddhdp; 13027 { 13028 struct inodedep *inodedep; 13029 struct inoref *inoref; 13030 struct ufsmount *ump; 13031 struct diradd *dap; 13032 struct vnode *vp; 13033 int error = 0; 13034 struct buf *bp; 13035 ino_t inum; 13036 struct diraddhd unfinished; 13037 13038 LIST_INIT(&unfinished); 13039 ump = VFSTOUFS(mp); 13040 LOCK_OWNED(ump); 13041 restart: 13042 while ((dap = LIST_FIRST(diraddhdp)) != NULL) { 13043 /* 13044 * Flush ourselves if this directory entry 13045 * has a MKDIR_PARENT dependency. 13046 */ 13047 if (dap->da_state & MKDIR_PARENT) { 13048 FREE_LOCK(ump); 13049 if ((error = ffs_update(pvp, 1)) != 0) 13050 break; 13051 ACQUIRE_LOCK(ump); 13052 /* 13053 * If that cleared dependencies, go on to next. 13054 */ 13055 if (dap != LIST_FIRST(diraddhdp)) 13056 continue; 13057 /* 13058 * All MKDIR_PARENT dependencies and all the 13059 * NEWBLOCK pagedeps that are contained in direct 13060 * blocks were resolved by doing above ffs_update. 13061 * Pagedeps contained in indirect blocks may 13062 * require a complete sync'ing of the directory. 13063 * We are in the midst of doing a complete sync, 13064 * so if they are not resolved in this pass we 13065 * defer them for now as they will be sync'ed by 13066 * our caller shortly. 13067 */ 13068 LIST_REMOVE(dap, da_pdlist); 13069 LIST_INSERT_HEAD(&unfinished, dap, da_pdlist); 13070 continue; 13071 } 13072 /* 13073 * A newly allocated directory must have its "." and 13074 * ".." entries written out before its name can be 13075 * committed in its parent. 13076 */ 13077 inum = dap->da_newinum; 13078 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 13079 panic("flush_pagedep_deps: lost inode1"); 13080 /* 13081 * Wait for any pending journal adds to complete so we don't 13082 * cause rollbacks while syncing. 13083 */ 13084 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 13085 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 13086 == DEPCOMPLETE) { 13087 jwait(&inoref->if_list, MNT_WAIT); 13088 goto restart; 13089 } 13090 } 13091 if (dap->da_state & MKDIR_BODY) { 13092 FREE_LOCK(ump); 13093 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp, 13094 FFSV_FORCEINSMQ))) 13095 break; 13096 error = flush_newblk_dep(vp, mp, 0); 13097 /* 13098 * If we still have the dependency we might need to 13099 * update the vnode to sync the new link count to 13100 * disk. 13101 */ 13102 if (error == 0 && dap == LIST_FIRST(diraddhdp)) 13103 error = ffs_update(vp, 1); 13104 vput(vp); 13105 if (error != 0) 13106 break; 13107 ACQUIRE_LOCK(ump); 13108 /* 13109 * If that cleared dependencies, go on to next. 13110 */ 13111 if (dap != LIST_FIRST(diraddhdp)) 13112 continue; 13113 if (dap->da_state & MKDIR_BODY) { 13114 inodedep_lookup(UFSTOVFS(ump), inum, 0, 13115 &inodedep); 13116 panic("flush_pagedep_deps: MKDIR_BODY " 13117 "inodedep %p dap %p vp %p", 13118 inodedep, dap, vp); 13119 } 13120 } 13121 /* 13122 * Flush the inode on which the directory entry depends. 13123 * Having accounted for MKDIR_PARENT and MKDIR_BODY above, 13124 * the only remaining dependency is that the updated inode 13125 * count must get pushed to disk. The inode has already 13126 * been pushed into its inode buffer (via VOP_UPDATE) at 13127 * the time of the reference count change. So we need only 13128 * locate that buffer, ensure that there will be no rollback 13129 * caused by a bitmap dependency, then write the inode buffer. 13130 */ 13131 retry: 13132 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 13133 panic("flush_pagedep_deps: lost inode"); 13134 /* 13135 * If the inode still has bitmap dependencies, 13136 * push them to disk. 13137 */ 13138 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) { 13139 bp = inodedep->id_bmsafemap->sm_buf; 13140 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 13141 if (bp == NULL) 13142 goto retry; 13143 FREE_LOCK(ump); 13144 if ((error = bwrite(bp)) != 0) 13145 break; 13146 ACQUIRE_LOCK(ump); 13147 if (dap != LIST_FIRST(diraddhdp)) 13148 continue; 13149 } 13150 /* 13151 * If the inode is still sitting in a buffer waiting 13152 * to be written or waiting for the link count to be 13153 * adjusted update it here to flush it to disk. 13154 */ 13155 if (dap == LIST_FIRST(diraddhdp)) { 13156 FREE_LOCK(ump); 13157 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp, 13158 FFSV_FORCEINSMQ))) 13159 break; 13160 error = ffs_update(vp, 1); 13161 vput(vp); 13162 if (error) 13163 break; 13164 ACQUIRE_LOCK(ump); 13165 } 13166 /* 13167 * If we have failed to get rid of all the dependencies 13168 * then something is seriously wrong. 13169 */ 13170 if (dap == LIST_FIRST(diraddhdp)) { 13171 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep); 13172 panic("flush_pagedep_deps: failed to flush " 13173 "inodedep %p ino %ju dap %p", 13174 inodedep, (uintmax_t)inum, dap); 13175 } 13176 } 13177 if (error) 13178 ACQUIRE_LOCK(ump); 13179 while ((dap = LIST_FIRST(&unfinished)) != NULL) { 13180 LIST_REMOVE(dap, da_pdlist); 13181 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist); 13182 } 13183 return (error); 13184 } 13185 13186 /* 13187 * A large burst of file addition or deletion activity can drive the 13188 * memory load excessively high. First attempt to slow things down 13189 * using the techniques below. If that fails, this routine requests 13190 * the offending operations to fall back to running synchronously 13191 * until the memory load returns to a reasonable level. 13192 */ 13193 int 13194 softdep_slowdown(vp) 13195 struct vnode *vp; 13196 { 13197 struct ufsmount *ump; 13198 int jlow; 13199 int max_softdeps_hard; 13200 13201 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 13202 ("softdep_slowdown called on non-softdep filesystem")); 13203 ump = VFSTOUFS(vp->v_mount); 13204 ACQUIRE_LOCK(ump); 13205 jlow = 0; 13206 /* 13207 * Check for journal space if needed. 13208 */ 13209 if (DOINGSUJ(vp)) { 13210 if (journal_space(ump, 0) == 0) 13211 jlow = 1; 13212 } 13213 /* 13214 * If the system is under its limits and our filesystem is 13215 * not responsible for more than our share of the usage and 13216 * we are not low on journal space, then no need to slow down. 13217 */ 13218 max_softdeps_hard = max_softdeps * 11 / 10; 13219 if (dep_current[D_DIRREM] < max_softdeps_hard / 2 && 13220 dep_current[D_INODEDEP] < max_softdeps_hard && 13221 dep_current[D_INDIRDEP] < max_softdeps_hard / 1000 && 13222 dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0 && 13223 ump->softdep_curdeps[D_DIRREM] < 13224 (max_softdeps_hard / 2) / stat_flush_threads && 13225 ump->softdep_curdeps[D_INODEDEP] < 13226 max_softdeps_hard / stat_flush_threads && 13227 ump->softdep_curdeps[D_INDIRDEP] < 13228 (max_softdeps_hard / 1000) / stat_flush_threads && 13229 ump->softdep_curdeps[D_FREEBLKS] < 13230 max_softdeps_hard / stat_flush_threads) { 13231 FREE_LOCK(ump); 13232 return (0); 13233 } 13234 /* 13235 * If the journal is low or our filesystem is over its limit 13236 * then speedup the cleanup. 13237 */ 13238 if (ump->softdep_curdeps[D_INDIRDEP] < 13239 (max_softdeps_hard / 1000) / stat_flush_threads || jlow) 13240 softdep_speedup(ump); 13241 stat_sync_limit_hit += 1; 13242 FREE_LOCK(ump); 13243 /* 13244 * We only slow down the rate at which new dependencies are 13245 * generated if we are not using journaling. With journaling, 13246 * the cleanup should always be sufficient to keep things 13247 * under control. 13248 */ 13249 if (DOINGSUJ(vp)) 13250 return (0); 13251 return (1); 13252 } 13253 13254 /* 13255 * Called by the allocation routines when they are about to fail 13256 * in the hope that we can free up the requested resource (inodes 13257 * or disk space). 13258 * 13259 * First check to see if the work list has anything on it. If it has, 13260 * clean up entries until we successfully free the requested resource. 13261 * Because this process holds inodes locked, we cannot handle any remove 13262 * requests that might block on a locked inode as that could lead to 13263 * deadlock. If the worklist yields none of the requested resource, 13264 * start syncing out vnodes to free up the needed space. 13265 */ 13266 int 13267 softdep_request_cleanup(fs, vp, cred, resource) 13268 struct fs *fs; 13269 struct vnode *vp; 13270 struct ucred *cred; 13271 int resource; 13272 { 13273 struct ufsmount *ump; 13274 struct mount *mp; 13275 long starttime; 13276 ufs2_daddr_t needed; 13277 int error, failed_vnode; 13278 13279 /* 13280 * If we are being called because of a process doing a 13281 * copy-on-write, then it is not safe to process any 13282 * worklist items as we will recurse into the copyonwrite 13283 * routine. This will result in an incoherent snapshot. 13284 * If the vnode that we hold is a snapshot, we must avoid 13285 * handling other resources that could cause deadlock. 13286 */ 13287 if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp))) 13288 return (0); 13289 13290 if (resource == FLUSH_BLOCKS_WAIT) 13291 stat_cleanup_blkrequests += 1; 13292 else 13293 stat_cleanup_inorequests += 1; 13294 13295 mp = vp->v_mount; 13296 ump = VFSTOUFS(mp); 13297 mtx_assert(UFS_MTX(ump), MA_OWNED); 13298 UFS_UNLOCK(ump); 13299 error = ffs_update(vp, 1); 13300 if (error != 0 || MOUNTEDSOFTDEP(mp) == 0) { 13301 UFS_LOCK(ump); 13302 return (0); 13303 } 13304 /* 13305 * If we are in need of resources, start by cleaning up 13306 * any block removals associated with our inode. 13307 */ 13308 ACQUIRE_LOCK(ump); 13309 process_removes(vp); 13310 process_truncates(vp); 13311 FREE_LOCK(ump); 13312 /* 13313 * Now clean up at least as many resources as we will need. 13314 * 13315 * When requested to clean up inodes, the number that are needed 13316 * is set by the number of simultaneous writers (mnt_writeopcount) 13317 * plus a bit of slop (2) in case some more writers show up while 13318 * we are cleaning. 13319 * 13320 * When requested to free up space, the amount of space that 13321 * we need is enough blocks to allocate a full-sized segment 13322 * (fs_contigsumsize). The number of such segments that will 13323 * be needed is set by the number of simultaneous writers 13324 * (mnt_writeopcount) plus a bit of slop (2) in case some more 13325 * writers show up while we are cleaning. 13326 * 13327 * Additionally, if we are unpriviledged and allocating space, 13328 * we need to ensure that we clean up enough blocks to get the 13329 * needed number of blocks over the threshold of the minimum 13330 * number of blocks required to be kept free by the filesystem 13331 * (fs_minfree). 13332 */ 13333 if (resource == FLUSH_INODES_WAIT) { 13334 needed = vp->v_mount->mnt_writeopcount + 2; 13335 } else if (resource == FLUSH_BLOCKS_WAIT) { 13336 needed = (vp->v_mount->mnt_writeopcount + 2) * 13337 fs->fs_contigsumsize; 13338 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE, 0)) 13339 needed += fragstoblks(fs, 13340 roundup((fs->fs_dsize * fs->fs_minfree / 100) - 13341 fs->fs_cstotal.cs_nffree, fs->fs_frag)); 13342 } else { 13343 UFS_LOCK(ump); 13344 printf("softdep_request_cleanup: Unknown resource type %d\n", 13345 resource); 13346 return (0); 13347 } 13348 starttime = time_second; 13349 retry: 13350 if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 && 13351 fs->fs_cstotal.cs_nbfree <= needed) || 13352 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13353 fs->fs_cstotal.cs_nifree <= needed)) { 13354 ACQUIRE_LOCK(ump); 13355 if (ump->softdep_on_worklist > 0 && 13356 process_worklist_item(UFSTOVFS(ump), 13357 ump->softdep_on_worklist, LK_NOWAIT) != 0) 13358 stat_worklist_push += 1; 13359 FREE_LOCK(ump); 13360 } 13361 /* 13362 * If we still need resources and there are no more worklist 13363 * entries to process to obtain them, we have to start flushing 13364 * the dirty vnodes to force the release of additional requests 13365 * to the worklist that we can then process to reap addition 13366 * resources. We walk the vnodes associated with the mount point 13367 * until we get the needed worklist requests that we can reap. 13368 * 13369 * If there are several threads all needing to clean the same 13370 * mount point, only one is allowed to walk the mount list. 13371 * When several threads all try to walk the same mount list, 13372 * they end up competing with each other and often end up in 13373 * livelock. This approach ensures that forward progress is 13374 * made at the cost of occational ENOSPC errors being returned 13375 * that might otherwise have been avoided. 13376 */ 13377 error = 1; 13378 if ((resource == FLUSH_BLOCKS_WAIT && 13379 fs->fs_cstotal.cs_nbfree <= needed) || 13380 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13381 fs->fs_cstotal.cs_nifree <= needed)) { 13382 ACQUIRE_LOCK(ump); 13383 if ((ump->um_softdep->sd_flags & FLUSH_RC_ACTIVE) == 0) { 13384 ump->um_softdep->sd_flags |= FLUSH_RC_ACTIVE; 13385 FREE_LOCK(ump); 13386 failed_vnode = softdep_request_cleanup_flush(mp, ump); 13387 ACQUIRE_LOCK(ump); 13388 ump->um_softdep->sd_flags &= ~FLUSH_RC_ACTIVE; 13389 FREE_LOCK(ump); 13390 if (ump->softdep_on_worklist > 0) { 13391 stat_cleanup_retries += 1; 13392 if (!failed_vnode) 13393 goto retry; 13394 } 13395 } else { 13396 FREE_LOCK(ump); 13397 error = 0; 13398 } 13399 stat_cleanup_failures += 1; 13400 } 13401 if (time_second - starttime > stat_cleanup_high_delay) 13402 stat_cleanup_high_delay = time_second - starttime; 13403 UFS_LOCK(ump); 13404 return (error); 13405 } 13406 13407 /* 13408 * Scan the vnodes for the specified mount point flushing out any 13409 * vnodes that can be locked without waiting. Finally, try to flush 13410 * the device associated with the mount point if it can be locked 13411 * without waiting. 13412 * 13413 * We return 0 if we were able to lock every vnode in our scan. 13414 * If we had to skip one or more vnodes, we return 1. 13415 */ 13416 static int 13417 softdep_request_cleanup_flush(mp, ump) 13418 struct mount *mp; 13419 struct ufsmount *ump; 13420 { 13421 struct thread *td; 13422 struct vnode *lvp, *mvp; 13423 int failed_vnode; 13424 13425 failed_vnode = 0; 13426 td = curthread; 13427 MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) { 13428 if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) { 13429 VI_UNLOCK(lvp); 13430 continue; 13431 } 13432 if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT, 13433 td) != 0) { 13434 failed_vnode = 1; 13435 continue; 13436 } 13437 if (lvp->v_vflag & VV_NOSYNC) { /* unlinked */ 13438 vput(lvp); 13439 continue; 13440 } 13441 (void) ffs_syncvnode(lvp, MNT_NOWAIT, 0); 13442 vput(lvp); 13443 } 13444 lvp = ump->um_devvp; 13445 if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) { 13446 VOP_FSYNC(lvp, MNT_NOWAIT, td); 13447 VOP_UNLOCK(lvp, 0); 13448 } 13449 return (failed_vnode); 13450 } 13451 13452 static bool 13453 softdep_excess_items(struct ufsmount *ump, int item) 13454 { 13455 13456 KASSERT(item >= 0 && item < D_LAST, ("item %d", item)); 13457 return (dep_current[item] > max_softdeps && 13458 ump->softdep_curdeps[item] > max_softdeps / 13459 stat_flush_threads); 13460 } 13461 13462 static void 13463 schedule_cleanup(struct mount *mp) 13464 { 13465 struct ufsmount *ump; 13466 struct thread *td; 13467 13468 ump = VFSTOUFS(mp); 13469 LOCK_OWNED(ump); 13470 FREE_LOCK(ump); 13471 td = curthread; 13472 if ((td->td_pflags & TDP_KTHREAD) != 0 && 13473 (td->td_proc->p_flag2 & P2_AST_SU) == 0) { 13474 /* 13475 * No ast is delivered to kernel threads, so nobody 13476 * would deref the mp. Some kernel threads 13477 * explicitely check for AST, e.g. NFS daemon does 13478 * this in the serving loop. 13479 */ 13480 return; 13481 } 13482 if (td->td_su != NULL) 13483 vfs_rel(td->td_su); 13484 vfs_ref(mp); 13485 td->td_su = mp; 13486 thread_lock(td); 13487 td->td_flags |= TDF_ASTPENDING; 13488 thread_unlock(td); 13489 } 13490 13491 static void 13492 softdep_ast_cleanup_proc(struct thread *td) 13493 { 13494 struct mount *mp; 13495 struct ufsmount *ump; 13496 int error; 13497 bool req; 13498 13499 while ((mp = td->td_su) != NULL) { 13500 td->td_su = NULL; 13501 error = vfs_busy(mp, MBF_NOWAIT); 13502 vfs_rel(mp); 13503 if (error != 0) 13504 return; 13505 if (ffs_own_mount(mp) && MOUNTEDSOFTDEP(mp)) { 13506 ump = VFSTOUFS(mp); 13507 for (;;) { 13508 req = false; 13509 ACQUIRE_LOCK(ump); 13510 if (softdep_excess_items(ump, D_INODEDEP)) { 13511 req = true; 13512 request_cleanup(mp, FLUSH_INODES); 13513 } 13514 if (softdep_excess_items(ump, D_DIRREM)) { 13515 req = true; 13516 request_cleanup(mp, FLUSH_BLOCKS); 13517 } 13518 FREE_LOCK(ump); 13519 if (softdep_excess_items(ump, D_NEWBLK) || 13520 softdep_excess_items(ump, D_ALLOCDIRECT) || 13521 softdep_excess_items(ump, D_ALLOCINDIR)) { 13522 error = vn_start_write(NULL, &mp, 13523 V_WAIT); 13524 if (error == 0) { 13525 req = true; 13526 VFS_SYNC(mp, MNT_WAIT); 13527 vn_finished_write(mp); 13528 } 13529 } 13530 if ((td->td_pflags & TDP_KTHREAD) != 0 || !req) 13531 break; 13532 } 13533 } 13534 vfs_unbusy(mp); 13535 } 13536 if ((mp = td->td_su) != NULL) { 13537 td->td_su = NULL; 13538 vfs_rel(mp); 13539 } 13540 } 13541 13542 /* 13543 * If memory utilization has gotten too high, deliberately slow things 13544 * down and speed up the I/O processing. 13545 */ 13546 static int 13547 request_cleanup(mp, resource) 13548 struct mount *mp; 13549 int resource; 13550 { 13551 struct thread *td = curthread; 13552 struct ufsmount *ump; 13553 13554 ump = VFSTOUFS(mp); 13555 LOCK_OWNED(ump); 13556 /* 13557 * We never hold up the filesystem syncer or buf daemon. 13558 */ 13559 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF)) 13560 return (0); 13561 /* 13562 * First check to see if the work list has gotten backlogged. 13563 * If it has, co-opt this process to help clean up two entries. 13564 * Because this process may hold inodes locked, we cannot 13565 * handle any remove requests that might block on a locked 13566 * inode as that could lead to deadlock. We set TDP_SOFTDEP 13567 * to avoid recursively processing the worklist. 13568 */ 13569 if (ump->softdep_on_worklist > max_softdeps / 10) { 13570 td->td_pflags |= TDP_SOFTDEP; 13571 process_worklist_item(mp, 2, LK_NOWAIT); 13572 td->td_pflags &= ~TDP_SOFTDEP; 13573 stat_worklist_push += 2; 13574 return(1); 13575 } 13576 /* 13577 * Next, we attempt to speed up the syncer process. If that 13578 * is successful, then we allow the process to continue. 13579 */ 13580 if (softdep_speedup(ump) && 13581 resource != FLUSH_BLOCKS_WAIT && 13582 resource != FLUSH_INODES_WAIT) 13583 return(0); 13584 /* 13585 * If we are resource constrained on inode dependencies, try 13586 * flushing some dirty inodes. Otherwise, we are constrained 13587 * by file deletions, so try accelerating flushes of directories 13588 * with removal dependencies. We would like to do the cleanup 13589 * here, but we probably hold an inode locked at this point and 13590 * that might deadlock against one that we try to clean. So, 13591 * the best that we can do is request the syncer daemon to do 13592 * the cleanup for us. 13593 */ 13594 switch (resource) { 13595 13596 case FLUSH_INODES: 13597 case FLUSH_INODES_WAIT: 13598 ACQUIRE_GBLLOCK(&lk); 13599 stat_ino_limit_push += 1; 13600 req_clear_inodedeps += 1; 13601 FREE_GBLLOCK(&lk); 13602 stat_countp = &stat_ino_limit_hit; 13603 break; 13604 13605 case FLUSH_BLOCKS: 13606 case FLUSH_BLOCKS_WAIT: 13607 ACQUIRE_GBLLOCK(&lk); 13608 stat_blk_limit_push += 1; 13609 req_clear_remove += 1; 13610 FREE_GBLLOCK(&lk); 13611 stat_countp = &stat_blk_limit_hit; 13612 break; 13613 13614 default: 13615 panic("request_cleanup: unknown type"); 13616 } 13617 /* 13618 * Hopefully the syncer daemon will catch up and awaken us. 13619 * We wait at most tickdelay before proceeding in any case. 13620 */ 13621 ACQUIRE_GBLLOCK(&lk); 13622 FREE_LOCK(ump); 13623 proc_waiting += 1; 13624 if (callout_pending(&softdep_callout) == FALSE) 13625 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2, 13626 pause_timer, 0); 13627 13628 if ((td->td_pflags & TDP_KTHREAD) == 0) 13629 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0); 13630 proc_waiting -= 1; 13631 FREE_GBLLOCK(&lk); 13632 ACQUIRE_LOCK(ump); 13633 return (1); 13634 } 13635 13636 /* 13637 * Awaken processes pausing in request_cleanup and clear proc_waiting 13638 * to indicate that there is no longer a timer running. Pause_timer 13639 * will be called with the global softdep mutex (&lk) locked. 13640 */ 13641 static void 13642 pause_timer(arg) 13643 void *arg; 13644 { 13645 13646 GBLLOCK_OWNED(&lk); 13647 /* 13648 * The callout_ API has acquired mtx and will hold it around this 13649 * function call. 13650 */ 13651 *stat_countp += proc_waiting; 13652 wakeup(&proc_waiting); 13653 } 13654 13655 /* 13656 * If requested, try removing inode or removal dependencies. 13657 */ 13658 static void 13659 check_clear_deps(mp) 13660 struct mount *mp; 13661 { 13662 13663 /* 13664 * If we are suspended, it may be because of our using 13665 * too many inodedeps, so help clear them out. 13666 */ 13667 if (MOUNTEDSUJ(mp) && VFSTOUFS(mp)->softdep_jblocks->jb_suspended) 13668 clear_inodedeps(mp); 13669 /* 13670 * General requests for cleanup of backed up dependencies 13671 */ 13672 ACQUIRE_GBLLOCK(&lk); 13673 if (req_clear_inodedeps) { 13674 req_clear_inodedeps -= 1; 13675 FREE_GBLLOCK(&lk); 13676 clear_inodedeps(mp); 13677 ACQUIRE_GBLLOCK(&lk); 13678 wakeup(&proc_waiting); 13679 } 13680 if (req_clear_remove) { 13681 req_clear_remove -= 1; 13682 FREE_GBLLOCK(&lk); 13683 clear_remove(mp); 13684 ACQUIRE_GBLLOCK(&lk); 13685 wakeup(&proc_waiting); 13686 } 13687 FREE_GBLLOCK(&lk); 13688 } 13689 13690 /* 13691 * Flush out a directory with at least one removal dependency in an effort to 13692 * reduce the number of dirrem, freefile, and freeblks dependency structures. 13693 */ 13694 static void 13695 clear_remove(mp) 13696 struct mount *mp; 13697 { 13698 struct pagedep_hashhead *pagedephd; 13699 struct pagedep *pagedep; 13700 struct ufsmount *ump; 13701 struct vnode *vp; 13702 struct bufobj *bo; 13703 int error, cnt; 13704 ino_t ino; 13705 13706 ump = VFSTOUFS(mp); 13707 LOCK_OWNED(ump); 13708 13709 for (cnt = 0; cnt <= ump->pagedep_hash_size; cnt++) { 13710 pagedephd = &ump->pagedep_hashtbl[ump->pagedep_nextclean++]; 13711 if (ump->pagedep_nextclean > ump->pagedep_hash_size) 13712 ump->pagedep_nextclean = 0; 13713 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 13714 if (LIST_EMPTY(&pagedep->pd_dirremhd)) 13715 continue; 13716 ino = pagedep->pd_ino; 13717 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 13718 continue; 13719 FREE_LOCK(ump); 13720 13721 /* 13722 * Let unmount clear deps 13723 */ 13724 error = vfs_busy(mp, MBF_NOWAIT); 13725 if (error != 0) 13726 goto finish_write; 13727 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 13728 FFSV_FORCEINSMQ); 13729 vfs_unbusy(mp); 13730 if (error != 0) { 13731 softdep_error("clear_remove: vget", error); 13732 goto finish_write; 13733 } 13734 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 13735 softdep_error("clear_remove: fsync", error); 13736 bo = &vp->v_bufobj; 13737 BO_LOCK(bo); 13738 drain_output(vp); 13739 BO_UNLOCK(bo); 13740 vput(vp); 13741 finish_write: 13742 vn_finished_write(mp); 13743 ACQUIRE_LOCK(ump); 13744 return; 13745 } 13746 } 13747 } 13748 13749 /* 13750 * Clear out a block of dirty inodes in an effort to reduce 13751 * the number of inodedep dependency structures. 13752 */ 13753 static void 13754 clear_inodedeps(mp) 13755 struct mount *mp; 13756 { 13757 struct inodedep_hashhead *inodedephd; 13758 struct inodedep *inodedep; 13759 struct ufsmount *ump; 13760 struct vnode *vp; 13761 struct fs *fs; 13762 int error, cnt; 13763 ino_t firstino, lastino, ino; 13764 13765 ump = VFSTOUFS(mp); 13766 fs = ump->um_fs; 13767 LOCK_OWNED(ump); 13768 /* 13769 * Pick a random inode dependency to be cleared. 13770 * We will then gather up all the inodes in its block 13771 * that have dependencies and flush them out. 13772 */ 13773 for (cnt = 0; cnt <= ump->inodedep_hash_size; cnt++) { 13774 inodedephd = &ump->inodedep_hashtbl[ump->inodedep_nextclean++]; 13775 if (ump->inodedep_nextclean > ump->inodedep_hash_size) 13776 ump->inodedep_nextclean = 0; 13777 if ((inodedep = LIST_FIRST(inodedephd)) != NULL) 13778 break; 13779 } 13780 if (inodedep == NULL) 13781 return; 13782 /* 13783 * Find the last inode in the block with dependencies. 13784 */ 13785 firstino = rounddown2(inodedep->id_ino, INOPB(fs)); 13786 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--) 13787 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0) 13788 break; 13789 /* 13790 * Asynchronously push all but the last inode with dependencies. 13791 * Synchronously push the last inode with dependencies to ensure 13792 * that the inode block gets written to free up the inodedeps. 13793 */ 13794 for (ino = firstino; ino <= lastino; ino++) { 13795 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 13796 continue; 13797 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 13798 continue; 13799 FREE_LOCK(ump); 13800 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */ 13801 if (error != 0) { 13802 vn_finished_write(mp); 13803 ACQUIRE_LOCK(ump); 13804 return; 13805 } 13806 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 13807 FFSV_FORCEINSMQ)) != 0) { 13808 softdep_error("clear_inodedeps: vget", error); 13809 vfs_unbusy(mp); 13810 vn_finished_write(mp); 13811 ACQUIRE_LOCK(ump); 13812 return; 13813 } 13814 vfs_unbusy(mp); 13815 if (ino == lastino) { 13816 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0))) 13817 softdep_error("clear_inodedeps: fsync1", error); 13818 } else { 13819 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 13820 softdep_error("clear_inodedeps: fsync2", error); 13821 BO_LOCK(&vp->v_bufobj); 13822 drain_output(vp); 13823 BO_UNLOCK(&vp->v_bufobj); 13824 } 13825 vput(vp); 13826 vn_finished_write(mp); 13827 ACQUIRE_LOCK(ump); 13828 } 13829 } 13830 13831 void 13832 softdep_buf_append(bp, wkhd) 13833 struct buf *bp; 13834 struct workhead *wkhd; 13835 { 13836 struct worklist *wk; 13837 struct ufsmount *ump; 13838 13839 if ((wk = LIST_FIRST(wkhd)) == NULL) 13840 return; 13841 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 13842 ("softdep_buf_append called on non-softdep filesystem")); 13843 ump = VFSTOUFS(wk->wk_mp); 13844 ACQUIRE_LOCK(ump); 13845 while ((wk = LIST_FIRST(wkhd)) != NULL) { 13846 WORKLIST_REMOVE(wk); 13847 WORKLIST_INSERT(&bp->b_dep, wk); 13848 } 13849 FREE_LOCK(ump); 13850 13851 } 13852 13853 void 13854 softdep_inode_append(ip, cred, wkhd) 13855 struct inode *ip; 13856 struct ucred *cred; 13857 struct workhead *wkhd; 13858 { 13859 struct buf *bp; 13860 struct fs *fs; 13861 struct ufsmount *ump; 13862 int error; 13863 13864 ump = ITOUMP(ip); 13865 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 13866 ("softdep_inode_append called on non-softdep filesystem")); 13867 fs = ump->um_fs; 13868 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 13869 (int)fs->fs_bsize, cred, &bp); 13870 if (error) { 13871 bqrelse(bp); 13872 softdep_freework(wkhd); 13873 return; 13874 } 13875 softdep_buf_append(bp, wkhd); 13876 bqrelse(bp); 13877 } 13878 13879 void 13880 softdep_freework(wkhd) 13881 struct workhead *wkhd; 13882 { 13883 struct worklist *wk; 13884 struct ufsmount *ump; 13885 13886 if ((wk = LIST_FIRST(wkhd)) == NULL) 13887 return; 13888 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 13889 ("softdep_freework called on non-softdep filesystem")); 13890 ump = VFSTOUFS(wk->wk_mp); 13891 ACQUIRE_LOCK(ump); 13892 handle_jwork(wkhd); 13893 FREE_LOCK(ump); 13894 } 13895 13896 /* 13897 * Function to determine if the buffer has outstanding dependencies 13898 * that will cause a roll-back if the buffer is written. If wantcount 13899 * is set, return number of dependencies, otherwise just yes or no. 13900 */ 13901 static int 13902 softdep_count_dependencies(bp, wantcount) 13903 struct buf *bp; 13904 int wantcount; 13905 { 13906 struct worklist *wk; 13907 struct ufsmount *ump; 13908 struct bmsafemap *bmsafemap; 13909 struct freework *freework; 13910 struct inodedep *inodedep; 13911 struct indirdep *indirdep; 13912 struct freeblks *freeblks; 13913 struct allocindir *aip; 13914 struct pagedep *pagedep; 13915 struct dirrem *dirrem; 13916 struct newblk *newblk; 13917 struct mkdir *mkdir; 13918 struct diradd *dap; 13919 struct vnode *vp; 13920 struct mount *mp; 13921 int i, retval; 13922 13923 retval = 0; 13924 if (LIST_EMPTY(&bp->b_dep)) 13925 return (0); 13926 vp = bp->b_vp; 13927 13928 /* 13929 * The ump mount point is stable after we get a correct 13930 * pointer, since bp is locked and this prevents unmount from 13931 * proceed. But to get to it, we cannot dereference bp->b_dep 13932 * head wk_mp, because we do not yet own SU ump lock and 13933 * workitem might be freed while dereferenced. 13934 */ 13935 retry: 13936 if (vp->v_type == VCHR) { 13937 VI_LOCK(vp); 13938 mp = vp->v_type == VCHR ? vp->v_rdev->si_mountpt : NULL; 13939 VI_UNLOCK(vp); 13940 if (mp == NULL) 13941 goto retry; 13942 } else if (vp->v_type == VREG) { 13943 mp = vp->v_mount; 13944 } else { 13945 return (0); 13946 } 13947 ump = VFSTOUFS(mp); 13948 13949 ACQUIRE_LOCK(ump); 13950 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 13951 switch (wk->wk_type) { 13952 13953 case D_INODEDEP: 13954 inodedep = WK_INODEDEP(wk); 13955 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 13956 /* bitmap allocation dependency */ 13957 retval += 1; 13958 if (!wantcount) 13959 goto out; 13960 } 13961 if (TAILQ_FIRST(&inodedep->id_inoupdt)) { 13962 /* direct block pointer dependency */ 13963 retval += 1; 13964 if (!wantcount) 13965 goto out; 13966 } 13967 if (TAILQ_FIRST(&inodedep->id_extupdt)) { 13968 /* direct block pointer dependency */ 13969 retval += 1; 13970 if (!wantcount) 13971 goto out; 13972 } 13973 if (TAILQ_FIRST(&inodedep->id_inoreflst)) { 13974 /* Add reference dependency. */ 13975 retval += 1; 13976 if (!wantcount) 13977 goto out; 13978 } 13979 continue; 13980 13981 case D_INDIRDEP: 13982 indirdep = WK_INDIRDEP(wk); 13983 13984 TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) { 13985 /* indirect truncation dependency */ 13986 retval += 1; 13987 if (!wantcount) 13988 goto out; 13989 } 13990 13991 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 13992 /* indirect block pointer dependency */ 13993 retval += 1; 13994 if (!wantcount) 13995 goto out; 13996 } 13997 continue; 13998 13999 case D_PAGEDEP: 14000 pagedep = WK_PAGEDEP(wk); 14001 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) { 14002 if (LIST_FIRST(&dirrem->dm_jremrefhd)) { 14003 /* Journal remove ref dependency. */ 14004 retval += 1; 14005 if (!wantcount) 14006 goto out; 14007 } 14008 } 14009 for (i = 0; i < DAHASHSZ; i++) { 14010 14011 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 14012 /* directory entry dependency */ 14013 retval += 1; 14014 if (!wantcount) 14015 goto out; 14016 } 14017 } 14018 continue; 14019 14020 case D_BMSAFEMAP: 14021 bmsafemap = WK_BMSAFEMAP(wk); 14022 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) { 14023 /* Add reference dependency. */ 14024 retval += 1; 14025 if (!wantcount) 14026 goto out; 14027 } 14028 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) { 14029 /* Allocate block dependency. */ 14030 retval += 1; 14031 if (!wantcount) 14032 goto out; 14033 } 14034 continue; 14035 14036 case D_FREEBLKS: 14037 freeblks = WK_FREEBLKS(wk); 14038 if (LIST_FIRST(&freeblks->fb_jblkdephd)) { 14039 /* Freeblk journal dependency. */ 14040 retval += 1; 14041 if (!wantcount) 14042 goto out; 14043 } 14044 continue; 14045 14046 case D_ALLOCDIRECT: 14047 case D_ALLOCINDIR: 14048 newblk = WK_NEWBLK(wk); 14049 if (newblk->nb_jnewblk) { 14050 /* Journal allocate dependency. */ 14051 retval += 1; 14052 if (!wantcount) 14053 goto out; 14054 } 14055 continue; 14056 14057 case D_MKDIR: 14058 mkdir = WK_MKDIR(wk); 14059 if (mkdir->md_jaddref) { 14060 /* Journal reference dependency. */ 14061 retval += 1; 14062 if (!wantcount) 14063 goto out; 14064 } 14065 continue; 14066 14067 case D_FREEWORK: 14068 case D_FREEDEP: 14069 case D_JSEGDEP: 14070 case D_JSEG: 14071 case D_SBDEP: 14072 /* never a dependency on these blocks */ 14073 continue; 14074 14075 default: 14076 panic("softdep_count_dependencies: Unexpected type %s", 14077 TYPENAME(wk->wk_type)); 14078 /* NOTREACHED */ 14079 } 14080 } 14081 out: 14082 FREE_LOCK(ump); 14083 return (retval); 14084 } 14085 14086 /* 14087 * Acquire exclusive access to a buffer. 14088 * Must be called with a locked mtx parameter. 14089 * Return acquired buffer or NULL on failure. 14090 */ 14091 static struct buf * 14092 getdirtybuf(bp, lock, waitfor) 14093 struct buf *bp; 14094 struct rwlock *lock; 14095 int waitfor; 14096 { 14097 int error; 14098 14099 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) { 14100 if (waitfor != MNT_WAIT) 14101 return (NULL); 14102 error = BUF_LOCK(bp, 14103 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock); 14104 /* 14105 * Even if we successfully acquire bp here, we have dropped 14106 * lock, which may violates our guarantee. 14107 */ 14108 if (error == 0) 14109 BUF_UNLOCK(bp); 14110 else if (error != ENOLCK) 14111 panic("getdirtybuf: inconsistent lock: %d", error); 14112 rw_wlock(lock); 14113 return (NULL); 14114 } 14115 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 14116 if (lock != BO_LOCKPTR(bp->b_bufobj) && waitfor == MNT_WAIT) { 14117 rw_wunlock(lock); 14118 BO_LOCK(bp->b_bufobj); 14119 BUF_UNLOCK(bp); 14120 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 14121 bp->b_vflags |= BV_BKGRDWAIT; 14122 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), 14123 PRIBIO | PDROP, "getbuf", 0); 14124 } else 14125 BO_UNLOCK(bp->b_bufobj); 14126 rw_wlock(lock); 14127 return (NULL); 14128 } 14129 BUF_UNLOCK(bp); 14130 if (waitfor != MNT_WAIT) 14131 return (NULL); 14132 #ifdef DEBUG_VFS_LOCKS 14133 if (bp->b_vp->v_type != VCHR) 14134 ASSERT_BO_WLOCKED(bp->b_bufobj); 14135 #endif 14136 bp->b_vflags |= BV_BKGRDWAIT; 14137 rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0); 14138 return (NULL); 14139 } 14140 if ((bp->b_flags & B_DELWRI) == 0) { 14141 BUF_UNLOCK(bp); 14142 return (NULL); 14143 } 14144 bremfree(bp); 14145 return (bp); 14146 } 14147 14148 14149 /* 14150 * Check if it is safe to suspend the file system now. On entry, 14151 * the vnode interlock for devvp should be held. Return 0 with 14152 * the mount interlock held if the file system can be suspended now, 14153 * otherwise return EAGAIN with the mount interlock held. 14154 */ 14155 int 14156 softdep_check_suspend(struct mount *mp, 14157 struct vnode *devvp, 14158 int softdep_depcnt, 14159 int softdep_accdepcnt, 14160 int secondary_writes, 14161 int secondary_accwrites) 14162 { 14163 struct bufobj *bo; 14164 struct ufsmount *ump; 14165 struct inodedep *inodedep; 14166 int error, unlinked; 14167 14168 bo = &devvp->v_bufobj; 14169 ASSERT_BO_WLOCKED(bo); 14170 14171 /* 14172 * If we are not running with soft updates, then we need only 14173 * deal with secondary writes as we try to suspend. 14174 */ 14175 if (MOUNTEDSOFTDEP(mp) == 0) { 14176 MNT_ILOCK(mp); 14177 while (mp->mnt_secondary_writes != 0) { 14178 BO_UNLOCK(bo); 14179 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 14180 (PUSER - 1) | PDROP, "secwr", 0); 14181 BO_LOCK(bo); 14182 MNT_ILOCK(mp); 14183 } 14184 14185 /* 14186 * Reasons for needing more work before suspend: 14187 * - Dirty buffers on devvp. 14188 * - Secondary writes occurred after start of vnode sync loop 14189 */ 14190 error = 0; 14191 if (bo->bo_numoutput > 0 || 14192 bo->bo_dirty.bv_cnt > 0 || 14193 secondary_writes != 0 || 14194 mp->mnt_secondary_writes != 0 || 14195 secondary_accwrites != mp->mnt_secondary_accwrites) 14196 error = EAGAIN; 14197 BO_UNLOCK(bo); 14198 return (error); 14199 } 14200 14201 /* 14202 * If we are running with soft updates, then we need to coordinate 14203 * with them as we try to suspend. 14204 */ 14205 ump = VFSTOUFS(mp); 14206 for (;;) { 14207 if (!TRY_ACQUIRE_LOCK(ump)) { 14208 BO_UNLOCK(bo); 14209 ACQUIRE_LOCK(ump); 14210 FREE_LOCK(ump); 14211 BO_LOCK(bo); 14212 continue; 14213 } 14214 MNT_ILOCK(mp); 14215 if (mp->mnt_secondary_writes != 0) { 14216 FREE_LOCK(ump); 14217 BO_UNLOCK(bo); 14218 msleep(&mp->mnt_secondary_writes, 14219 MNT_MTX(mp), 14220 (PUSER - 1) | PDROP, "secwr", 0); 14221 BO_LOCK(bo); 14222 continue; 14223 } 14224 break; 14225 } 14226 14227 unlinked = 0; 14228 if (MOUNTEDSUJ(mp)) { 14229 for (inodedep = TAILQ_FIRST(&ump->softdep_unlinked); 14230 inodedep != NULL; 14231 inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 14232 if ((inodedep->id_state & (UNLINKED | UNLINKLINKS | 14233 UNLINKONLIST)) != (UNLINKED | UNLINKLINKS | 14234 UNLINKONLIST) || 14235 !check_inodedep_free(inodedep)) 14236 continue; 14237 unlinked++; 14238 } 14239 } 14240 14241 /* 14242 * Reasons for needing more work before suspend: 14243 * - Dirty buffers on devvp. 14244 * - Softdep activity occurred after start of vnode sync loop 14245 * - Secondary writes occurred after start of vnode sync loop 14246 */ 14247 error = 0; 14248 if (bo->bo_numoutput > 0 || 14249 bo->bo_dirty.bv_cnt > 0 || 14250 softdep_depcnt != unlinked || 14251 ump->softdep_deps != unlinked || 14252 softdep_accdepcnt != ump->softdep_accdeps || 14253 secondary_writes != 0 || 14254 mp->mnt_secondary_writes != 0 || 14255 secondary_accwrites != mp->mnt_secondary_accwrites) 14256 error = EAGAIN; 14257 FREE_LOCK(ump); 14258 BO_UNLOCK(bo); 14259 return (error); 14260 } 14261 14262 14263 /* 14264 * Get the number of dependency structures for the file system, both 14265 * the current number and the total number allocated. These will 14266 * later be used to detect that softdep processing has occurred. 14267 */ 14268 void 14269 softdep_get_depcounts(struct mount *mp, 14270 int *softdep_depsp, 14271 int *softdep_accdepsp) 14272 { 14273 struct ufsmount *ump; 14274 14275 if (MOUNTEDSOFTDEP(mp) == 0) { 14276 *softdep_depsp = 0; 14277 *softdep_accdepsp = 0; 14278 return; 14279 } 14280 ump = VFSTOUFS(mp); 14281 ACQUIRE_LOCK(ump); 14282 *softdep_depsp = ump->softdep_deps; 14283 *softdep_accdepsp = ump->softdep_accdeps; 14284 FREE_LOCK(ump); 14285 } 14286 14287 /* 14288 * Wait for pending output on a vnode to complete. 14289 */ 14290 static void 14291 drain_output(vp) 14292 struct vnode *vp; 14293 { 14294 14295 ASSERT_VOP_LOCKED(vp, "drain_output"); 14296 (void)bufobj_wwait(&vp->v_bufobj, 0, 0); 14297 } 14298 14299 /* 14300 * Called whenever a buffer that is being invalidated or reallocated 14301 * contains dependencies. This should only happen if an I/O error has 14302 * occurred. The routine is called with the buffer locked. 14303 */ 14304 static void 14305 softdep_deallocate_dependencies(bp) 14306 struct buf *bp; 14307 { 14308 14309 if ((bp->b_ioflags & BIO_ERROR) == 0) 14310 panic("softdep_deallocate_dependencies: dangling deps"); 14311 if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL) 14312 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error); 14313 else 14314 printf("softdep_deallocate_dependencies: " 14315 "got error %d while accessing filesystem\n", bp->b_error); 14316 if (bp->b_error != ENXIO) 14317 panic("softdep_deallocate_dependencies: unrecovered I/O error"); 14318 } 14319 14320 /* 14321 * Function to handle asynchronous write errors in the filesystem. 14322 */ 14323 static void 14324 softdep_error(func, error) 14325 char *func; 14326 int error; 14327 { 14328 14329 /* XXX should do something better! */ 14330 printf("%s: got error %d while accessing filesystem\n", func, error); 14331 } 14332 14333 #ifdef DDB 14334 14335 static void 14336 inodedep_print(struct inodedep *inodedep, int verbose) 14337 { 14338 db_printf("%p fs %p st %x ino %jd inoblk %jd delta %jd nlink %jd" 14339 " saveino %p\n", 14340 inodedep, inodedep->id_fs, inodedep->id_state, 14341 (intmax_t)inodedep->id_ino, 14342 (intmax_t)fsbtodb(inodedep->id_fs, 14343 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)), 14344 (intmax_t)inodedep->id_nlinkdelta, 14345 (intmax_t)inodedep->id_savednlink, 14346 inodedep->id_savedino1); 14347 14348 if (verbose == 0) 14349 return; 14350 14351 db_printf("\tpendinghd %p, bufwait %p, inowait %p, inoreflst %p, " 14352 "mkdiradd %p\n", 14353 LIST_FIRST(&inodedep->id_pendinghd), 14354 LIST_FIRST(&inodedep->id_bufwait), 14355 LIST_FIRST(&inodedep->id_inowait), 14356 TAILQ_FIRST(&inodedep->id_inoreflst), 14357 inodedep->id_mkdiradd); 14358 db_printf("\tinoupdt %p, newinoupdt %p, extupdt %p, newextupdt %p\n", 14359 TAILQ_FIRST(&inodedep->id_inoupdt), 14360 TAILQ_FIRST(&inodedep->id_newinoupdt), 14361 TAILQ_FIRST(&inodedep->id_extupdt), 14362 TAILQ_FIRST(&inodedep->id_newextupdt)); 14363 } 14364 14365 DB_SHOW_COMMAND(inodedep, db_show_inodedep) 14366 { 14367 14368 if (have_addr == 0) { 14369 db_printf("Address required\n"); 14370 return; 14371 } 14372 inodedep_print((struct inodedep*)addr, 1); 14373 } 14374 14375 DB_SHOW_COMMAND(inodedeps, db_show_inodedeps) 14376 { 14377 struct inodedep_hashhead *inodedephd; 14378 struct inodedep *inodedep; 14379 struct ufsmount *ump; 14380 int cnt; 14381 14382 if (have_addr == 0) { 14383 db_printf("Address required\n"); 14384 return; 14385 } 14386 ump = (struct ufsmount *)addr; 14387 for (cnt = 0; cnt < ump->inodedep_hash_size; cnt++) { 14388 inodedephd = &ump->inodedep_hashtbl[cnt]; 14389 LIST_FOREACH(inodedep, inodedephd, id_hash) { 14390 inodedep_print(inodedep, 0); 14391 } 14392 } 14393 } 14394 14395 DB_SHOW_COMMAND(worklist, db_show_worklist) 14396 { 14397 struct worklist *wk; 14398 14399 if (have_addr == 0) { 14400 db_printf("Address required\n"); 14401 return; 14402 } 14403 wk = (struct worklist *)addr; 14404 printf("worklist: %p type %s state 0x%X\n", 14405 wk, TYPENAME(wk->wk_type), wk->wk_state); 14406 } 14407 14408 DB_SHOW_COMMAND(workhead, db_show_workhead) 14409 { 14410 struct workhead *wkhd; 14411 struct worklist *wk; 14412 int i; 14413 14414 if (have_addr == 0) { 14415 db_printf("Address required\n"); 14416 return; 14417 } 14418 wkhd = (struct workhead *)addr; 14419 wk = LIST_FIRST(wkhd); 14420 for (i = 0; i < 100 && wk != NULL; i++, wk = LIST_NEXT(wk, wk_list)) 14421 db_printf("worklist: %p type %s state 0x%X", 14422 wk, TYPENAME(wk->wk_type), wk->wk_state); 14423 if (i == 100) 14424 db_printf("workhead overflow"); 14425 printf("\n"); 14426 } 14427 14428 14429 DB_SHOW_COMMAND(mkdirs, db_show_mkdirs) 14430 { 14431 struct mkdirlist *mkdirlisthd; 14432 struct jaddref *jaddref; 14433 struct diradd *diradd; 14434 struct mkdir *mkdir; 14435 14436 if (have_addr == 0) { 14437 db_printf("Address required\n"); 14438 return; 14439 } 14440 mkdirlisthd = (struct mkdirlist *)addr; 14441 LIST_FOREACH(mkdir, mkdirlisthd, md_mkdirs) { 14442 diradd = mkdir->md_diradd; 14443 db_printf("mkdir: %p state 0x%X dap %p state 0x%X", 14444 mkdir, mkdir->md_state, diradd, diradd->da_state); 14445 if ((jaddref = mkdir->md_jaddref) != NULL) 14446 db_printf(" jaddref %p jaddref state 0x%X", 14447 jaddref, jaddref->ja_state); 14448 db_printf("\n"); 14449 } 14450 } 14451 14452 /* exported to ffs_vfsops.c */ 14453 extern void db_print_ffs(struct ufsmount *ump); 14454 void 14455 db_print_ffs(struct ufsmount *ump) 14456 { 14457 db_printf("mp %p %s devvp %p fs %p su_wl %d su_deps %d su_req %d\n", 14458 ump->um_mountp, ump->um_mountp->mnt_stat.f_mntonname, 14459 ump->um_devvp, ump->um_fs, ump->softdep_on_worklist, 14460 ump->softdep_deps, ump->softdep_req); 14461 } 14462 14463 #endif /* DDB */ 14464 14465 #endif /* SOFTUPDATES */ 14466