1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright 1998, 2000 Marshall Kirk McKusick. 5 * Copyright 2009, 2010 Jeffrey W. Roberson <jeff@FreeBSD.org> 6 * All rights reserved. 7 * 8 * The soft updates code is derived from the appendix of a University 9 * of Michigan technical report (Gregory R. Ganger and Yale N. Patt, 10 * "Soft Updates: A Solution to the Metadata Update Problem in File 11 * Systems", CSE-TR-254-95, August 1995). 12 * 13 * Further information about soft updates can be obtained from: 14 * 15 * Marshall Kirk McKusick http://www.mckusick.com/softdep/ 16 * 1614 Oxford Street mckusick@mckusick.com 17 * Berkeley, CA 94709-1608 +1-510-843-9542 18 * USA 19 * 20 * Redistribution and use in source and binary forms, with or without 21 * modification, are permitted provided that the following conditions 22 * are met: 23 * 24 * 1. Redistributions of source code must retain the above copyright 25 * notice, this list of conditions and the following disclaimer. 26 * 2. Redistributions in binary form must reproduce the above copyright 27 * notice, this list of conditions and the following disclaimer in the 28 * documentation and/or other materials provided with the distribution. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 33 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 35 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 36 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 37 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 38 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 39 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * from: @(#)ffs_softdep.c 9.59 (McKusick) 6/21/00 42 */ 43 44 #include <sys/cdefs.h> 45 __FBSDID("$FreeBSD$"); 46 47 #include "opt_ffs.h" 48 #include "opt_quota.h" 49 #include "opt_ddb.h" 50 51 #include <sys/param.h> 52 #include <sys/kernel.h> 53 #include <sys/systm.h> 54 #include <sys/bio.h> 55 #include <sys/buf.h> 56 #include <sys/kdb.h> 57 #include <sys/kthread.h> 58 #include <sys/ktr.h> 59 #include <sys/limits.h> 60 #include <sys/lock.h> 61 #include <sys/malloc.h> 62 #include <sys/mount.h> 63 #include <sys/mutex.h> 64 #include <sys/namei.h> 65 #include <sys/priv.h> 66 #include <sys/proc.h> 67 #include <sys/racct.h> 68 #include <sys/rwlock.h> 69 #include <sys/stat.h> 70 #include <sys/sysctl.h> 71 #include <sys/syslog.h> 72 #include <sys/vnode.h> 73 #include <sys/conf.h> 74 75 #include <ufs/ufs/dir.h> 76 #include <ufs/ufs/extattr.h> 77 #include <ufs/ufs/quota.h> 78 #include <ufs/ufs/inode.h> 79 #include <ufs/ufs/ufsmount.h> 80 #include <ufs/ffs/fs.h> 81 #include <ufs/ffs/softdep.h> 82 #include <ufs/ffs/ffs_extern.h> 83 #include <ufs/ufs/ufs_extern.h> 84 85 #include <vm/vm.h> 86 #include <vm/vm_extern.h> 87 #include <vm/vm_object.h> 88 89 #include <geom/geom.h> 90 91 #include <ddb/ddb.h> 92 93 #define KTR_SUJ 0 /* Define to KTR_SPARE. */ 94 95 #ifndef SOFTUPDATES 96 97 int 98 softdep_flushfiles(oldmnt, flags, td) 99 struct mount *oldmnt; 100 int flags; 101 struct thread *td; 102 { 103 104 panic("softdep_flushfiles called"); 105 } 106 107 int 108 softdep_mount(devvp, mp, fs, cred) 109 struct vnode *devvp; 110 struct mount *mp; 111 struct fs *fs; 112 struct ucred *cred; 113 { 114 115 return (0); 116 } 117 118 void 119 softdep_initialize() 120 { 121 122 return; 123 } 124 125 void 126 softdep_uninitialize() 127 { 128 129 return; 130 } 131 132 void 133 softdep_unmount(mp) 134 struct mount *mp; 135 { 136 137 panic("softdep_unmount called"); 138 } 139 140 void 141 softdep_setup_sbupdate(ump, fs, bp) 142 struct ufsmount *ump; 143 struct fs *fs; 144 struct buf *bp; 145 { 146 147 panic("softdep_setup_sbupdate called"); 148 } 149 150 void 151 softdep_setup_inomapdep(bp, ip, newinum, mode) 152 struct buf *bp; 153 struct inode *ip; 154 ino_t newinum; 155 int mode; 156 { 157 158 panic("softdep_setup_inomapdep called"); 159 } 160 161 void 162 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags) 163 struct buf *bp; 164 struct mount *mp; 165 ufs2_daddr_t newblkno; 166 int frags; 167 int oldfrags; 168 { 169 170 panic("softdep_setup_blkmapdep called"); 171 } 172 173 void 174 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 175 struct inode *ip; 176 ufs_lbn_t lbn; 177 ufs2_daddr_t newblkno; 178 ufs2_daddr_t oldblkno; 179 long newsize; 180 long oldsize; 181 struct buf *bp; 182 { 183 184 panic("softdep_setup_allocdirect called"); 185 } 186 187 void 188 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 189 struct inode *ip; 190 ufs_lbn_t lbn; 191 ufs2_daddr_t newblkno; 192 ufs2_daddr_t oldblkno; 193 long newsize; 194 long oldsize; 195 struct buf *bp; 196 { 197 198 panic("softdep_setup_allocext called"); 199 } 200 201 void 202 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 203 struct inode *ip; 204 ufs_lbn_t lbn; 205 struct buf *bp; 206 int ptrno; 207 ufs2_daddr_t newblkno; 208 ufs2_daddr_t oldblkno; 209 struct buf *nbp; 210 { 211 212 panic("softdep_setup_allocindir_page called"); 213 } 214 215 void 216 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 217 struct buf *nbp; 218 struct inode *ip; 219 struct buf *bp; 220 int ptrno; 221 ufs2_daddr_t newblkno; 222 { 223 224 panic("softdep_setup_allocindir_meta called"); 225 } 226 227 void 228 softdep_journal_freeblocks(ip, cred, length, flags) 229 struct inode *ip; 230 struct ucred *cred; 231 off_t length; 232 int flags; 233 { 234 235 panic("softdep_journal_freeblocks called"); 236 } 237 238 void 239 softdep_journal_fsync(ip) 240 struct inode *ip; 241 { 242 243 panic("softdep_journal_fsync called"); 244 } 245 246 void 247 softdep_setup_freeblocks(ip, length, flags) 248 struct inode *ip; 249 off_t length; 250 int flags; 251 { 252 253 panic("softdep_setup_freeblocks called"); 254 } 255 256 void 257 softdep_freefile(pvp, ino, mode) 258 struct vnode *pvp; 259 ino_t ino; 260 int mode; 261 { 262 263 panic("softdep_freefile called"); 264 } 265 266 int 267 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 268 struct buf *bp; 269 struct inode *dp; 270 off_t diroffset; 271 ino_t newinum; 272 struct buf *newdirbp; 273 int isnewblk; 274 { 275 276 panic("softdep_setup_directory_add called"); 277 } 278 279 void 280 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 281 struct buf *bp; 282 struct inode *dp; 283 caddr_t base; 284 caddr_t oldloc; 285 caddr_t newloc; 286 int entrysize; 287 { 288 289 panic("softdep_change_directoryentry_offset called"); 290 } 291 292 void 293 softdep_setup_remove(bp, dp, ip, isrmdir) 294 struct buf *bp; 295 struct inode *dp; 296 struct inode *ip; 297 int isrmdir; 298 { 299 300 panic("softdep_setup_remove called"); 301 } 302 303 void 304 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 305 struct buf *bp; 306 struct inode *dp; 307 struct inode *ip; 308 ino_t newinum; 309 int isrmdir; 310 { 311 312 panic("softdep_setup_directory_change called"); 313 } 314 315 void 316 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 317 struct mount *mp; 318 struct buf *bp; 319 ufs2_daddr_t blkno; 320 int frags; 321 struct workhead *wkhd; 322 { 323 324 panic("%s called", __FUNCTION__); 325 } 326 327 void 328 softdep_setup_inofree(mp, bp, ino, wkhd) 329 struct mount *mp; 330 struct buf *bp; 331 ino_t ino; 332 struct workhead *wkhd; 333 { 334 335 panic("%s called", __FUNCTION__); 336 } 337 338 void 339 softdep_setup_unlink(dp, ip) 340 struct inode *dp; 341 struct inode *ip; 342 { 343 344 panic("%s called", __FUNCTION__); 345 } 346 347 void 348 softdep_setup_link(dp, ip) 349 struct inode *dp; 350 struct inode *ip; 351 { 352 353 panic("%s called", __FUNCTION__); 354 } 355 356 void 357 softdep_revert_link(dp, ip) 358 struct inode *dp; 359 struct inode *ip; 360 { 361 362 panic("%s called", __FUNCTION__); 363 } 364 365 void 366 softdep_setup_rmdir(dp, ip) 367 struct inode *dp; 368 struct inode *ip; 369 { 370 371 panic("%s called", __FUNCTION__); 372 } 373 374 void 375 softdep_revert_rmdir(dp, ip) 376 struct inode *dp; 377 struct inode *ip; 378 { 379 380 panic("%s called", __FUNCTION__); 381 } 382 383 void 384 softdep_setup_create(dp, ip) 385 struct inode *dp; 386 struct inode *ip; 387 { 388 389 panic("%s called", __FUNCTION__); 390 } 391 392 void 393 softdep_revert_create(dp, ip) 394 struct inode *dp; 395 struct inode *ip; 396 { 397 398 panic("%s called", __FUNCTION__); 399 } 400 401 void 402 softdep_setup_mkdir(dp, ip) 403 struct inode *dp; 404 struct inode *ip; 405 { 406 407 panic("%s called", __FUNCTION__); 408 } 409 410 void 411 softdep_revert_mkdir(dp, ip) 412 struct inode *dp; 413 struct inode *ip; 414 { 415 416 panic("%s called", __FUNCTION__); 417 } 418 419 void 420 softdep_setup_dotdot_link(dp, ip) 421 struct inode *dp; 422 struct inode *ip; 423 { 424 425 panic("%s called", __FUNCTION__); 426 } 427 428 int 429 softdep_prealloc(vp, waitok) 430 struct vnode *vp; 431 int waitok; 432 { 433 434 panic("%s called", __FUNCTION__); 435 } 436 437 int 438 softdep_journal_lookup(mp, vpp) 439 struct mount *mp; 440 struct vnode **vpp; 441 { 442 443 return (ENOENT); 444 } 445 446 void 447 softdep_change_linkcnt(ip) 448 struct inode *ip; 449 { 450 451 panic("softdep_change_linkcnt called"); 452 } 453 454 void 455 softdep_load_inodeblock(ip) 456 struct inode *ip; 457 { 458 459 panic("softdep_load_inodeblock called"); 460 } 461 462 void 463 softdep_update_inodeblock(ip, bp, waitfor) 464 struct inode *ip; 465 struct buf *bp; 466 int waitfor; 467 { 468 469 panic("softdep_update_inodeblock called"); 470 } 471 472 int 473 softdep_fsync(vp) 474 struct vnode *vp; /* the "in_core" copy of the inode */ 475 { 476 477 return (0); 478 } 479 480 void 481 softdep_fsync_mountdev(vp) 482 struct vnode *vp; 483 { 484 485 return; 486 } 487 488 int 489 softdep_flushworklist(oldmnt, countp, td) 490 struct mount *oldmnt; 491 int *countp; 492 struct thread *td; 493 { 494 495 *countp = 0; 496 return (0); 497 } 498 499 int 500 softdep_sync_metadata(struct vnode *vp) 501 { 502 503 panic("softdep_sync_metadata called"); 504 } 505 506 int 507 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 508 { 509 510 panic("softdep_sync_buf called"); 511 } 512 513 int 514 softdep_slowdown(vp) 515 struct vnode *vp; 516 { 517 518 panic("softdep_slowdown called"); 519 } 520 521 int 522 softdep_request_cleanup(fs, vp, cred, resource) 523 struct fs *fs; 524 struct vnode *vp; 525 struct ucred *cred; 526 int resource; 527 { 528 529 return (0); 530 } 531 532 int 533 softdep_check_suspend(struct mount *mp, 534 struct vnode *devvp, 535 int softdep_depcnt, 536 int softdep_accdepcnt, 537 int secondary_writes, 538 int secondary_accwrites) 539 { 540 struct bufobj *bo; 541 int error; 542 543 (void) softdep_depcnt, 544 (void) softdep_accdepcnt; 545 546 bo = &devvp->v_bufobj; 547 ASSERT_BO_WLOCKED(bo); 548 549 MNT_ILOCK(mp); 550 while (mp->mnt_secondary_writes != 0) { 551 BO_UNLOCK(bo); 552 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 553 (PUSER - 1) | PDROP, "secwr", 0); 554 BO_LOCK(bo); 555 MNT_ILOCK(mp); 556 } 557 558 /* 559 * Reasons for needing more work before suspend: 560 * - Dirty buffers on devvp. 561 * - Secondary writes occurred after start of vnode sync loop 562 */ 563 error = 0; 564 if (bo->bo_numoutput > 0 || 565 bo->bo_dirty.bv_cnt > 0 || 566 secondary_writes != 0 || 567 mp->mnt_secondary_writes != 0 || 568 secondary_accwrites != mp->mnt_secondary_accwrites) 569 error = EAGAIN; 570 BO_UNLOCK(bo); 571 return (error); 572 } 573 574 void 575 softdep_get_depcounts(struct mount *mp, 576 int *softdepactivep, 577 int *softdepactiveaccp) 578 { 579 (void) mp; 580 *softdepactivep = 0; 581 *softdepactiveaccp = 0; 582 } 583 584 void 585 softdep_buf_append(bp, wkhd) 586 struct buf *bp; 587 struct workhead *wkhd; 588 { 589 590 panic("softdep_buf_appendwork called"); 591 } 592 593 void 594 softdep_inode_append(ip, cred, wkhd) 595 struct inode *ip; 596 struct ucred *cred; 597 struct workhead *wkhd; 598 { 599 600 panic("softdep_inode_appendwork called"); 601 } 602 603 void 604 softdep_freework(wkhd) 605 struct workhead *wkhd; 606 { 607 608 panic("softdep_freework called"); 609 } 610 611 #else 612 613 FEATURE(softupdates, "FFS soft-updates support"); 614 615 static SYSCTL_NODE(_debug, OID_AUTO, softdep, CTLFLAG_RW, 0, 616 "soft updates stats"); 617 static SYSCTL_NODE(_debug_softdep, OID_AUTO, total, CTLFLAG_RW, 0, 618 "total dependencies allocated"); 619 static SYSCTL_NODE(_debug_softdep, OID_AUTO, highuse, CTLFLAG_RW, 0, 620 "high use dependencies allocated"); 621 static SYSCTL_NODE(_debug_softdep, OID_AUTO, current, CTLFLAG_RW, 0, 622 "current dependencies allocated"); 623 static SYSCTL_NODE(_debug_softdep, OID_AUTO, write, CTLFLAG_RW, 0, 624 "current dependencies written"); 625 626 unsigned long dep_current[D_LAST + 1]; 627 unsigned long dep_highuse[D_LAST + 1]; 628 unsigned long dep_total[D_LAST + 1]; 629 unsigned long dep_write[D_LAST + 1]; 630 631 #define SOFTDEP_TYPE(type, str, long) \ 632 static MALLOC_DEFINE(M_ ## type, #str, long); \ 633 SYSCTL_ULONG(_debug_softdep_total, OID_AUTO, str, CTLFLAG_RD, \ 634 &dep_total[D_ ## type], 0, ""); \ 635 SYSCTL_ULONG(_debug_softdep_current, OID_AUTO, str, CTLFLAG_RD, \ 636 &dep_current[D_ ## type], 0, ""); \ 637 SYSCTL_ULONG(_debug_softdep_highuse, OID_AUTO, str, CTLFLAG_RD, \ 638 &dep_highuse[D_ ## type], 0, ""); \ 639 SYSCTL_ULONG(_debug_softdep_write, OID_AUTO, str, CTLFLAG_RD, \ 640 &dep_write[D_ ## type], 0, ""); 641 642 SOFTDEP_TYPE(PAGEDEP, pagedep, "File page dependencies"); 643 SOFTDEP_TYPE(INODEDEP, inodedep, "Inode dependencies"); 644 SOFTDEP_TYPE(BMSAFEMAP, bmsafemap, 645 "Block or frag allocated from cyl group map"); 646 SOFTDEP_TYPE(NEWBLK, newblk, "New block or frag allocation dependency"); 647 SOFTDEP_TYPE(ALLOCDIRECT, allocdirect, "Block or frag dependency for an inode"); 648 SOFTDEP_TYPE(INDIRDEP, indirdep, "Indirect block dependencies"); 649 SOFTDEP_TYPE(ALLOCINDIR, allocindir, "Block dependency for an indirect block"); 650 SOFTDEP_TYPE(FREEFRAG, freefrag, "Previously used frag for an inode"); 651 SOFTDEP_TYPE(FREEBLKS, freeblks, "Blocks freed from an inode"); 652 SOFTDEP_TYPE(FREEFILE, freefile, "Inode deallocated"); 653 SOFTDEP_TYPE(DIRADD, diradd, "New directory entry"); 654 SOFTDEP_TYPE(MKDIR, mkdir, "New directory"); 655 SOFTDEP_TYPE(DIRREM, dirrem, "Directory entry deleted"); 656 SOFTDEP_TYPE(NEWDIRBLK, newdirblk, "Unclaimed new directory block"); 657 SOFTDEP_TYPE(FREEWORK, freework, "free an inode block"); 658 SOFTDEP_TYPE(FREEDEP, freedep, "track a block free"); 659 SOFTDEP_TYPE(JADDREF, jaddref, "Journal inode ref add"); 660 SOFTDEP_TYPE(JREMREF, jremref, "Journal inode ref remove"); 661 SOFTDEP_TYPE(JMVREF, jmvref, "Journal inode ref move"); 662 SOFTDEP_TYPE(JNEWBLK, jnewblk, "Journal new block"); 663 SOFTDEP_TYPE(JFREEBLK, jfreeblk, "Journal free block"); 664 SOFTDEP_TYPE(JFREEFRAG, jfreefrag, "Journal free frag"); 665 SOFTDEP_TYPE(JSEG, jseg, "Journal segment"); 666 SOFTDEP_TYPE(JSEGDEP, jsegdep, "Journal segment complete"); 667 SOFTDEP_TYPE(SBDEP, sbdep, "Superblock write dependency"); 668 SOFTDEP_TYPE(JTRUNC, jtrunc, "Journal inode truncation"); 669 SOFTDEP_TYPE(JFSYNC, jfsync, "Journal fsync complete"); 670 671 static MALLOC_DEFINE(M_SENTINEL, "sentinel", "Worklist sentinel"); 672 673 static MALLOC_DEFINE(M_SAVEDINO, "savedino", "Saved inodes"); 674 static MALLOC_DEFINE(M_JBLOCKS, "jblocks", "Journal block locations"); 675 static MALLOC_DEFINE(M_MOUNTDATA, "softdep", "Softdep per-mount data"); 676 677 #define M_SOFTDEP_FLAGS (M_WAITOK) 678 679 /* 680 * translate from workitem type to memory type 681 * MUST match the defines above, such that memtype[D_XXX] == M_XXX 682 */ 683 static struct malloc_type *memtype[] = { 684 NULL, 685 M_PAGEDEP, 686 M_INODEDEP, 687 M_BMSAFEMAP, 688 M_NEWBLK, 689 M_ALLOCDIRECT, 690 M_INDIRDEP, 691 M_ALLOCINDIR, 692 M_FREEFRAG, 693 M_FREEBLKS, 694 M_FREEFILE, 695 M_DIRADD, 696 M_MKDIR, 697 M_DIRREM, 698 M_NEWDIRBLK, 699 M_FREEWORK, 700 M_FREEDEP, 701 M_JADDREF, 702 M_JREMREF, 703 M_JMVREF, 704 M_JNEWBLK, 705 M_JFREEBLK, 706 M_JFREEFRAG, 707 M_JSEG, 708 M_JSEGDEP, 709 M_SBDEP, 710 M_JTRUNC, 711 M_JFSYNC, 712 M_SENTINEL 713 }; 714 715 #define DtoM(type) (memtype[type]) 716 717 /* 718 * Names of malloc types. 719 */ 720 #define TYPENAME(type) \ 721 ((unsigned)(type) <= D_LAST && (unsigned)(type) >= D_FIRST ? \ 722 memtype[type]->ks_shortdesc : "???") 723 /* 724 * End system adaptation definitions. 725 */ 726 727 #define DOTDOT_OFFSET offsetof(struct dirtemplate, dotdot_ino) 728 #define DOT_OFFSET offsetof(struct dirtemplate, dot_ino) 729 730 /* 731 * Internal function prototypes. 732 */ 733 static void check_clear_deps(struct mount *); 734 static void softdep_error(char *, int); 735 static int softdep_process_worklist(struct mount *, int); 736 static int softdep_waitidle(struct mount *, int); 737 static void drain_output(struct vnode *); 738 static struct buf *getdirtybuf(struct buf *, struct rwlock *, int); 739 static int check_inodedep_free(struct inodedep *); 740 static void clear_remove(struct mount *); 741 static void clear_inodedeps(struct mount *); 742 static void unlinked_inodedep(struct mount *, struct inodedep *); 743 static void clear_unlinked_inodedep(struct inodedep *); 744 static struct inodedep *first_unlinked_inodedep(struct ufsmount *); 745 static int flush_pagedep_deps(struct vnode *, struct mount *, 746 struct diraddhd *); 747 static int free_pagedep(struct pagedep *); 748 static int flush_newblk_dep(struct vnode *, struct mount *, ufs_lbn_t); 749 static int flush_inodedep_deps(struct vnode *, struct mount *, ino_t); 750 static int flush_deplist(struct allocdirectlst *, int, int *); 751 static int sync_cgs(struct mount *, int); 752 static int handle_written_filepage(struct pagedep *, struct buf *, int); 753 static int handle_written_sbdep(struct sbdep *, struct buf *); 754 static void initiate_write_sbdep(struct sbdep *); 755 static void diradd_inode_written(struct diradd *, struct inodedep *); 756 static int handle_written_indirdep(struct indirdep *, struct buf *, 757 struct buf**, int); 758 static int handle_written_inodeblock(struct inodedep *, struct buf *, int); 759 static int jnewblk_rollforward(struct jnewblk *, struct fs *, struct cg *, 760 uint8_t *); 761 static int handle_written_bmsafemap(struct bmsafemap *, struct buf *, int); 762 static void handle_written_jaddref(struct jaddref *); 763 static void handle_written_jremref(struct jremref *); 764 static void handle_written_jseg(struct jseg *, struct buf *); 765 static void handle_written_jnewblk(struct jnewblk *); 766 static void handle_written_jblkdep(struct jblkdep *); 767 static void handle_written_jfreefrag(struct jfreefrag *); 768 static void complete_jseg(struct jseg *); 769 static void complete_jsegs(struct jseg *); 770 static void jseg_write(struct ufsmount *ump, struct jseg *, uint8_t *); 771 static void jaddref_write(struct jaddref *, struct jseg *, uint8_t *); 772 static void jremref_write(struct jremref *, struct jseg *, uint8_t *); 773 static void jmvref_write(struct jmvref *, struct jseg *, uint8_t *); 774 static void jtrunc_write(struct jtrunc *, struct jseg *, uint8_t *); 775 static void jfsync_write(struct jfsync *, struct jseg *, uint8_t *data); 776 static void jnewblk_write(struct jnewblk *, struct jseg *, uint8_t *); 777 static void jfreeblk_write(struct jfreeblk *, struct jseg *, uint8_t *); 778 static void jfreefrag_write(struct jfreefrag *, struct jseg *, uint8_t *); 779 static inline void inoref_write(struct inoref *, struct jseg *, 780 struct jrefrec *); 781 static void handle_allocdirect_partdone(struct allocdirect *, 782 struct workhead *); 783 static struct jnewblk *cancel_newblk(struct newblk *, struct worklist *, 784 struct workhead *); 785 static void indirdep_complete(struct indirdep *); 786 static int indirblk_lookup(struct mount *, ufs2_daddr_t); 787 static void indirblk_insert(struct freework *); 788 static void indirblk_remove(struct freework *); 789 static void handle_allocindir_partdone(struct allocindir *); 790 static void initiate_write_filepage(struct pagedep *, struct buf *); 791 static void initiate_write_indirdep(struct indirdep*, struct buf *); 792 static void handle_written_mkdir(struct mkdir *, int); 793 static int jnewblk_rollback(struct jnewblk *, struct fs *, struct cg *, 794 uint8_t *); 795 static void initiate_write_bmsafemap(struct bmsafemap *, struct buf *); 796 static void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *); 797 static void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *); 798 static void handle_workitem_freefile(struct freefile *); 799 static int handle_workitem_remove(struct dirrem *, int); 800 static struct dirrem *newdirrem(struct buf *, struct inode *, 801 struct inode *, int, struct dirrem **); 802 static struct indirdep *indirdep_lookup(struct mount *, struct inode *, 803 struct buf *); 804 static void cancel_indirdep(struct indirdep *, struct buf *, 805 struct freeblks *); 806 static void free_indirdep(struct indirdep *); 807 static void free_diradd(struct diradd *, struct workhead *); 808 static void merge_diradd(struct inodedep *, struct diradd *); 809 static void complete_diradd(struct diradd *); 810 static struct diradd *diradd_lookup(struct pagedep *, int); 811 static struct jremref *cancel_diradd_dotdot(struct inode *, struct dirrem *, 812 struct jremref *); 813 static struct jremref *cancel_mkdir_dotdot(struct inode *, struct dirrem *, 814 struct jremref *); 815 static void cancel_diradd(struct diradd *, struct dirrem *, struct jremref *, 816 struct jremref *, struct jremref *); 817 static void dirrem_journal(struct dirrem *, struct jremref *, struct jremref *, 818 struct jremref *); 819 static void cancel_allocindir(struct allocindir *, struct buf *bp, 820 struct freeblks *, int); 821 static int setup_trunc_indir(struct freeblks *, struct inode *, 822 ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t); 823 static void complete_trunc_indir(struct freework *); 824 static void trunc_indirdep(struct indirdep *, struct freeblks *, struct buf *, 825 int); 826 static void complete_mkdir(struct mkdir *); 827 static void free_newdirblk(struct newdirblk *); 828 static void free_jremref(struct jremref *); 829 static void free_jaddref(struct jaddref *); 830 static void free_jsegdep(struct jsegdep *); 831 static void free_jsegs(struct jblocks *); 832 static void rele_jseg(struct jseg *); 833 static void free_jseg(struct jseg *, struct jblocks *); 834 static void free_jnewblk(struct jnewblk *); 835 static void free_jblkdep(struct jblkdep *); 836 static void free_jfreefrag(struct jfreefrag *); 837 static void free_freedep(struct freedep *); 838 static void journal_jremref(struct dirrem *, struct jremref *, 839 struct inodedep *); 840 static void cancel_jnewblk(struct jnewblk *, struct workhead *); 841 static int cancel_jaddref(struct jaddref *, struct inodedep *, 842 struct workhead *); 843 static void cancel_jfreefrag(struct jfreefrag *); 844 static inline void setup_freedirect(struct freeblks *, struct inode *, 845 int, int); 846 static inline void setup_freeext(struct freeblks *, struct inode *, int, int); 847 static inline void setup_freeindir(struct freeblks *, struct inode *, int, 848 ufs_lbn_t, int); 849 static inline struct freeblks *newfreeblks(struct mount *, struct inode *); 850 static void freeblks_free(struct ufsmount *, struct freeblks *, int); 851 static void indir_trunc(struct freework *, ufs2_daddr_t, ufs_lbn_t); 852 static ufs2_daddr_t blkcount(struct fs *, ufs2_daddr_t, off_t); 853 static int trunc_check_buf(struct buf *, int *, ufs_lbn_t, int, int); 854 static void trunc_dependencies(struct inode *, struct freeblks *, ufs_lbn_t, 855 int, int); 856 static void trunc_pages(struct inode *, off_t, ufs2_daddr_t, int); 857 static int cancel_pagedep(struct pagedep *, struct freeblks *, int); 858 static int deallocate_dependencies(struct buf *, struct freeblks *, int); 859 static void newblk_freefrag(struct newblk*); 860 static void free_newblk(struct newblk *); 861 static void cancel_allocdirect(struct allocdirectlst *, 862 struct allocdirect *, struct freeblks *); 863 static int check_inode_unwritten(struct inodedep *); 864 static int free_inodedep(struct inodedep *); 865 static void freework_freeblock(struct freework *, u_long); 866 static void freework_enqueue(struct freework *); 867 static int handle_workitem_freeblocks(struct freeblks *, int); 868 static int handle_complete_freeblocks(struct freeblks *, int); 869 static void handle_workitem_indirblk(struct freework *); 870 static void handle_written_freework(struct freework *); 871 static void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *); 872 static struct worklist *jnewblk_merge(struct worklist *, struct worklist *, 873 struct workhead *); 874 static struct freefrag *setup_allocindir_phase2(struct buf *, struct inode *, 875 struct inodedep *, struct allocindir *, ufs_lbn_t); 876 static struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t, 877 ufs2_daddr_t, ufs_lbn_t); 878 static void handle_workitem_freefrag(struct freefrag *); 879 static struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long, 880 ufs_lbn_t, u_long); 881 static void allocdirect_merge(struct allocdirectlst *, 882 struct allocdirect *, struct allocdirect *); 883 static struct freefrag *allocindir_merge(struct allocindir *, 884 struct allocindir *); 885 static int bmsafemap_find(struct bmsafemap_hashhead *, int, 886 struct bmsafemap **); 887 static struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *, 888 int cg, struct bmsafemap *); 889 static int newblk_find(struct newblk_hashhead *, ufs2_daddr_t, int, 890 struct newblk **); 891 static int newblk_lookup(struct mount *, ufs2_daddr_t, int, struct newblk **); 892 static int inodedep_find(struct inodedep_hashhead *, ino_t, 893 struct inodedep **); 894 static int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **); 895 static int pagedep_lookup(struct mount *, struct buf *bp, ino_t, ufs_lbn_t, 896 int, struct pagedep **); 897 static int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t, 898 struct pagedep **); 899 static void pause_timer(void *); 900 static int request_cleanup(struct mount *, int); 901 static int softdep_request_cleanup_flush(struct mount *, struct ufsmount *); 902 static void schedule_cleanup(struct mount *); 903 static void softdep_ast_cleanup_proc(struct thread *); 904 static struct ufsmount *softdep_bp_to_mp(struct buf *bp); 905 static int process_worklist_item(struct mount *, int, int); 906 static void process_removes(struct vnode *); 907 static void process_truncates(struct vnode *); 908 static void jwork_move(struct workhead *, struct workhead *); 909 static void jwork_insert(struct workhead *, struct jsegdep *); 910 static void add_to_worklist(struct worklist *, int); 911 static void wake_worklist(struct worklist *); 912 static void wait_worklist(struct worklist *, char *); 913 static void remove_from_worklist(struct worklist *); 914 static void softdep_flush(void *); 915 static void softdep_flushjournal(struct mount *); 916 static int softdep_speedup(struct ufsmount *); 917 static void worklist_speedup(struct mount *); 918 static int journal_mount(struct mount *, struct fs *, struct ucred *); 919 static void journal_unmount(struct ufsmount *); 920 static int journal_space(struct ufsmount *, int); 921 static void journal_suspend(struct ufsmount *); 922 static int journal_unsuspend(struct ufsmount *ump); 923 static void softdep_prelink(struct vnode *, struct vnode *); 924 static void add_to_journal(struct worklist *); 925 static void remove_from_journal(struct worklist *); 926 static bool softdep_excess_items(struct ufsmount *, int); 927 static void softdep_process_journal(struct mount *, struct worklist *, int); 928 static struct jremref *newjremref(struct dirrem *, struct inode *, 929 struct inode *ip, off_t, nlink_t); 930 static struct jaddref *newjaddref(struct inode *, ino_t, off_t, int16_t, 931 uint16_t); 932 static inline void newinoref(struct inoref *, ino_t, ino_t, off_t, nlink_t, 933 uint16_t); 934 static inline struct jsegdep *inoref_jseg(struct inoref *); 935 static struct jmvref *newjmvref(struct inode *, ino_t, off_t, off_t); 936 static struct jfreeblk *newjfreeblk(struct freeblks *, ufs_lbn_t, 937 ufs2_daddr_t, int); 938 static void adjust_newfreework(struct freeblks *, int); 939 static struct jtrunc *newjtrunc(struct freeblks *, off_t, int); 940 static void move_newblock_dep(struct jaddref *, struct inodedep *); 941 static void cancel_jfreeblk(struct freeblks *, ufs2_daddr_t); 942 static struct jfreefrag *newjfreefrag(struct freefrag *, struct inode *, 943 ufs2_daddr_t, long, ufs_lbn_t); 944 static struct freework *newfreework(struct ufsmount *, struct freeblks *, 945 struct freework *, ufs_lbn_t, ufs2_daddr_t, int, int, int); 946 static int jwait(struct worklist *, int); 947 static struct inodedep *inodedep_lookup_ip(struct inode *); 948 static int bmsafemap_backgroundwrite(struct bmsafemap *, struct buf *); 949 static struct freefile *handle_bufwait(struct inodedep *, struct workhead *); 950 static void handle_jwork(struct workhead *); 951 static struct mkdir *setup_newdir(struct diradd *, ino_t, ino_t, struct buf *, 952 struct mkdir **); 953 static struct jblocks *jblocks_create(void); 954 static ufs2_daddr_t jblocks_alloc(struct jblocks *, int, int *); 955 static void jblocks_free(struct jblocks *, struct mount *, int); 956 static void jblocks_destroy(struct jblocks *); 957 static void jblocks_add(struct jblocks *, ufs2_daddr_t, int); 958 959 /* 960 * Exported softdep operations. 961 */ 962 static void softdep_disk_io_initiation(struct buf *); 963 static void softdep_disk_write_complete(struct buf *); 964 static void softdep_deallocate_dependencies(struct buf *); 965 static int softdep_count_dependencies(struct buf *bp, int); 966 967 /* 968 * Global lock over all of soft updates. 969 */ 970 static struct mtx lk; 971 MTX_SYSINIT(softdep_lock, &lk, "Global Softdep Lock", MTX_DEF); 972 973 #define ACQUIRE_GBLLOCK(lk) mtx_lock(lk) 974 #define FREE_GBLLOCK(lk) mtx_unlock(lk) 975 #define GBLLOCK_OWNED(lk) mtx_assert((lk), MA_OWNED) 976 977 /* 978 * Per-filesystem soft-updates locking. 979 */ 980 #define LOCK_PTR(ump) (&(ump)->um_softdep->sd_fslock) 981 #define TRY_ACQUIRE_LOCK(ump) rw_try_wlock(&(ump)->um_softdep->sd_fslock) 982 #define ACQUIRE_LOCK(ump) rw_wlock(&(ump)->um_softdep->sd_fslock) 983 #define FREE_LOCK(ump) rw_wunlock(&(ump)->um_softdep->sd_fslock) 984 #define LOCK_OWNED(ump) rw_assert(&(ump)->um_softdep->sd_fslock, \ 985 RA_WLOCKED) 986 987 #define BUF_AREC(bp) lockallowrecurse(&(bp)->b_lock) 988 #define BUF_NOREC(bp) lockdisablerecurse(&(bp)->b_lock) 989 990 /* 991 * Worklist queue management. 992 * These routines require that the lock be held. 993 */ 994 #ifndef /* NOT */ INVARIANTS 995 #define WORKLIST_INSERT(head, item) do { \ 996 (item)->wk_state |= ONWORKLIST; \ 997 LIST_INSERT_HEAD(head, item, wk_list); \ 998 } while (0) 999 #define WORKLIST_REMOVE(item) do { \ 1000 (item)->wk_state &= ~ONWORKLIST; \ 1001 LIST_REMOVE(item, wk_list); \ 1002 } while (0) 1003 #define WORKLIST_INSERT_UNLOCKED WORKLIST_INSERT 1004 #define WORKLIST_REMOVE_UNLOCKED WORKLIST_REMOVE 1005 1006 #else /* INVARIANTS */ 1007 static void worklist_insert(struct workhead *, struct worklist *, int, 1008 const char *, int); 1009 static void worklist_remove(struct worklist *, int, const char *, int); 1010 1011 #define WORKLIST_INSERT(head, item) \ 1012 worklist_insert(head, item, 1, __func__, __LINE__) 1013 #define WORKLIST_INSERT_UNLOCKED(head, item)\ 1014 worklist_insert(head, item, 0, __func__, __LINE__) 1015 #define WORKLIST_REMOVE(item)\ 1016 worklist_remove(item, 1, __func__, __LINE__) 1017 #define WORKLIST_REMOVE_UNLOCKED(item)\ 1018 worklist_remove(item, 0, __func__, __LINE__) 1019 1020 static void 1021 worklist_insert(head, item, locked, func, line) 1022 struct workhead *head; 1023 struct worklist *item; 1024 int locked; 1025 const char *func; 1026 int line; 1027 { 1028 1029 if (locked) 1030 LOCK_OWNED(VFSTOUFS(item->wk_mp)); 1031 if (item->wk_state & ONWORKLIST) 1032 panic("worklist_insert: %p %s(0x%X) already on list, " 1033 "added in function %s at line %d", 1034 item, TYPENAME(item->wk_type), item->wk_state, 1035 item->wk_func, item->wk_line); 1036 item->wk_state |= ONWORKLIST; 1037 item->wk_func = func; 1038 item->wk_line = line; 1039 LIST_INSERT_HEAD(head, item, wk_list); 1040 } 1041 1042 static void 1043 worklist_remove(item, locked, func, line) 1044 struct worklist *item; 1045 int locked; 1046 const char *func; 1047 int line; 1048 { 1049 1050 if (locked) 1051 LOCK_OWNED(VFSTOUFS(item->wk_mp)); 1052 if ((item->wk_state & ONWORKLIST) == 0) 1053 panic("worklist_remove: %p %s(0x%X) not on list, " 1054 "removed in function %s at line %d", 1055 item, TYPENAME(item->wk_type), item->wk_state, 1056 item->wk_func, item->wk_line); 1057 item->wk_state &= ~ONWORKLIST; 1058 item->wk_func = func; 1059 item->wk_line = line; 1060 LIST_REMOVE(item, wk_list); 1061 } 1062 #endif /* INVARIANTS */ 1063 1064 /* 1065 * Merge two jsegdeps keeping only the oldest one as newer references 1066 * can't be discarded until after older references. 1067 */ 1068 static inline struct jsegdep * 1069 jsegdep_merge(struct jsegdep *one, struct jsegdep *two) 1070 { 1071 struct jsegdep *swp; 1072 1073 if (two == NULL) 1074 return (one); 1075 1076 if (one->jd_seg->js_seq > two->jd_seg->js_seq) { 1077 swp = one; 1078 one = two; 1079 two = swp; 1080 } 1081 WORKLIST_REMOVE(&two->jd_list); 1082 free_jsegdep(two); 1083 1084 return (one); 1085 } 1086 1087 /* 1088 * If two freedeps are compatible free one to reduce list size. 1089 */ 1090 static inline struct freedep * 1091 freedep_merge(struct freedep *one, struct freedep *two) 1092 { 1093 if (two == NULL) 1094 return (one); 1095 1096 if (one->fd_freework == two->fd_freework) { 1097 WORKLIST_REMOVE(&two->fd_list); 1098 free_freedep(two); 1099 } 1100 return (one); 1101 } 1102 1103 /* 1104 * Move journal work from one list to another. Duplicate freedeps and 1105 * jsegdeps are coalesced to keep the lists as small as possible. 1106 */ 1107 static void 1108 jwork_move(dst, src) 1109 struct workhead *dst; 1110 struct workhead *src; 1111 { 1112 struct freedep *freedep; 1113 struct jsegdep *jsegdep; 1114 struct worklist *wkn; 1115 struct worklist *wk; 1116 1117 KASSERT(dst != src, 1118 ("jwork_move: dst == src")); 1119 freedep = NULL; 1120 jsegdep = NULL; 1121 LIST_FOREACH_SAFE(wk, dst, wk_list, wkn) { 1122 if (wk->wk_type == D_JSEGDEP) 1123 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1124 else if (wk->wk_type == D_FREEDEP) 1125 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1126 } 1127 1128 while ((wk = LIST_FIRST(src)) != NULL) { 1129 WORKLIST_REMOVE(wk); 1130 WORKLIST_INSERT(dst, wk); 1131 if (wk->wk_type == D_JSEGDEP) { 1132 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1133 continue; 1134 } 1135 if (wk->wk_type == D_FREEDEP) 1136 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1137 } 1138 } 1139 1140 static void 1141 jwork_insert(dst, jsegdep) 1142 struct workhead *dst; 1143 struct jsegdep *jsegdep; 1144 { 1145 struct jsegdep *jsegdepn; 1146 struct worklist *wk; 1147 1148 LIST_FOREACH(wk, dst, wk_list) 1149 if (wk->wk_type == D_JSEGDEP) 1150 break; 1151 if (wk == NULL) { 1152 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1153 return; 1154 } 1155 jsegdepn = WK_JSEGDEP(wk); 1156 if (jsegdep->jd_seg->js_seq < jsegdepn->jd_seg->js_seq) { 1157 WORKLIST_REMOVE(wk); 1158 free_jsegdep(jsegdepn); 1159 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1160 } else 1161 free_jsegdep(jsegdep); 1162 } 1163 1164 /* 1165 * Routines for tracking and managing workitems. 1166 */ 1167 static void workitem_free(struct worklist *, int); 1168 static void workitem_alloc(struct worklist *, int, struct mount *); 1169 static void workitem_reassign(struct worklist *, int); 1170 1171 #define WORKITEM_FREE(item, type) \ 1172 workitem_free((struct worklist *)(item), (type)) 1173 #define WORKITEM_REASSIGN(item, type) \ 1174 workitem_reassign((struct worklist *)(item), (type)) 1175 1176 static void 1177 workitem_free(item, type) 1178 struct worklist *item; 1179 int type; 1180 { 1181 struct ufsmount *ump; 1182 1183 #ifdef INVARIANTS 1184 if (item->wk_state & ONWORKLIST) 1185 panic("workitem_free: %s(0x%X) still on list, " 1186 "added in function %s at line %d", 1187 TYPENAME(item->wk_type), item->wk_state, 1188 item->wk_func, item->wk_line); 1189 if (item->wk_type != type && type != D_NEWBLK) 1190 panic("workitem_free: type mismatch %s != %s", 1191 TYPENAME(item->wk_type), TYPENAME(type)); 1192 #endif 1193 if (item->wk_state & IOWAITING) 1194 wakeup(item); 1195 ump = VFSTOUFS(item->wk_mp); 1196 LOCK_OWNED(ump); 1197 KASSERT(ump->softdep_deps > 0, 1198 ("workitem_free: %s: softdep_deps going negative", 1199 ump->um_fs->fs_fsmnt)); 1200 if (--ump->softdep_deps == 0 && ump->softdep_req) 1201 wakeup(&ump->softdep_deps); 1202 KASSERT(dep_current[item->wk_type] > 0, 1203 ("workitem_free: %s: dep_current[%s] going negative", 1204 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1205 KASSERT(ump->softdep_curdeps[item->wk_type] > 0, 1206 ("workitem_free: %s: softdep_curdeps[%s] going negative", 1207 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1208 atomic_subtract_long(&dep_current[item->wk_type], 1); 1209 ump->softdep_curdeps[item->wk_type] -= 1; 1210 free(item, DtoM(type)); 1211 } 1212 1213 static void 1214 workitem_alloc(item, type, mp) 1215 struct worklist *item; 1216 int type; 1217 struct mount *mp; 1218 { 1219 struct ufsmount *ump; 1220 1221 item->wk_type = type; 1222 item->wk_mp = mp; 1223 item->wk_state = 0; 1224 1225 ump = VFSTOUFS(mp); 1226 ACQUIRE_GBLLOCK(&lk); 1227 dep_current[type]++; 1228 if (dep_current[type] > dep_highuse[type]) 1229 dep_highuse[type] = dep_current[type]; 1230 dep_total[type]++; 1231 FREE_GBLLOCK(&lk); 1232 ACQUIRE_LOCK(ump); 1233 ump->softdep_curdeps[type] += 1; 1234 ump->softdep_deps++; 1235 ump->softdep_accdeps++; 1236 FREE_LOCK(ump); 1237 } 1238 1239 static void 1240 workitem_reassign(item, newtype) 1241 struct worklist *item; 1242 int newtype; 1243 { 1244 struct ufsmount *ump; 1245 1246 ump = VFSTOUFS(item->wk_mp); 1247 LOCK_OWNED(ump); 1248 KASSERT(ump->softdep_curdeps[item->wk_type] > 0, 1249 ("workitem_reassign: %s: softdep_curdeps[%s] going negative", 1250 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1251 ump->softdep_curdeps[item->wk_type] -= 1; 1252 ump->softdep_curdeps[newtype] += 1; 1253 KASSERT(dep_current[item->wk_type] > 0, 1254 ("workitem_reassign: %s: dep_current[%s] going negative", 1255 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1256 ACQUIRE_GBLLOCK(&lk); 1257 dep_current[newtype]++; 1258 dep_current[item->wk_type]--; 1259 if (dep_current[newtype] > dep_highuse[newtype]) 1260 dep_highuse[newtype] = dep_current[newtype]; 1261 dep_total[newtype]++; 1262 FREE_GBLLOCK(&lk); 1263 item->wk_type = newtype; 1264 } 1265 1266 /* 1267 * Workitem queue management 1268 */ 1269 static int max_softdeps; /* maximum number of structs before slowdown */ 1270 static int tickdelay = 2; /* number of ticks to pause during slowdown */ 1271 static int proc_waiting; /* tracks whether we have a timeout posted */ 1272 static int *stat_countp; /* statistic to count in proc_waiting timeout */ 1273 static struct callout softdep_callout; 1274 static int req_clear_inodedeps; /* syncer process flush some inodedeps */ 1275 static int req_clear_remove; /* syncer process flush some freeblks */ 1276 static int softdep_flushcache = 0; /* Should we do BIO_FLUSH? */ 1277 1278 /* 1279 * runtime statistics 1280 */ 1281 static int stat_flush_threads; /* number of softdep flushing threads */ 1282 static int stat_worklist_push; /* number of worklist cleanups */ 1283 static int stat_blk_limit_push; /* number of times block limit neared */ 1284 static int stat_ino_limit_push; /* number of times inode limit neared */ 1285 static int stat_blk_limit_hit; /* number of times block slowdown imposed */ 1286 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */ 1287 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */ 1288 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */ 1289 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */ 1290 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */ 1291 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */ 1292 static int stat_jaddref; /* bufs redirtied as ino bitmap can not write */ 1293 static int stat_jnewblk; /* bufs redirtied as blk bitmap can not write */ 1294 static int stat_journal_min; /* Times hit journal min threshold */ 1295 static int stat_journal_low; /* Times hit journal low threshold */ 1296 static int stat_journal_wait; /* Times blocked in jwait(). */ 1297 static int stat_jwait_filepage; /* Times blocked in jwait() for filepage. */ 1298 static int stat_jwait_freeblks; /* Times blocked in jwait() for freeblks. */ 1299 static int stat_jwait_inode; /* Times blocked in jwait() for inodes. */ 1300 static int stat_jwait_newblk; /* Times blocked in jwait() for newblks. */ 1301 static int stat_cleanup_high_delay; /* Maximum cleanup delay (in ticks) */ 1302 static int stat_cleanup_blkrequests; /* Number of block cleanup requests */ 1303 static int stat_cleanup_inorequests; /* Number of inode cleanup requests */ 1304 static int stat_cleanup_retries; /* Number of cleanups that needed to flush */ 1305 static int stat_cleanup_failures; /* Number of cleanup requests that failed */ 1306 static int stat_emptyjblocks; /* Number of potentially empty journal blocks */ 1307 1308 SYSCTL_INT(_debug_softdep, OID_AUTO, max_softdeps, CTLFLAG_RW, 1309 &max_softdeps, 0, ""); 1310 SYSCTL_INT(_debug_softdep, OID_AUTO, tickdelay, CTLFLAG_RW, 1311 &tickdelay, 0, ""); 1312 SYSCTL_INT(_debug_softdep, OID_AUTO, flush_threads, CTLFLAG_RD, 1313 &stat_flush_threads, 0, ""); 1314 SYSCTL_INT(_debug_softdep, OID_AUTO, worklist_push, CTLFLAG_RW, 1315 &stat_worklist_push, 0,""); 1316 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_push, CTLFLAG_RW, 1317 &stat_blk_limit_push, 0,""); 1318 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_push, CTLFLAG_RW, 1319 &stat_ino_limit_push, 0,""); 1320 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_hit, CTLFLAG_RW, 1321 &stat_blk_limit_hit, 0, ""); 1322 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_hit, CTLFLAG_RW, 1323 &stat_ino_limit_hit, 0, ""); 1324 SYSCTL_INT(_debug_softdep, OID_AUTO, sync_limit_hit, CTLFLAG_RW, 1325 &stat_sync_limit_hit, 0, ""); 1326 SYSCTL_INT(_debug_softdep, OID_AUTO, indir_blk_ptrs, CTLFLAG_RW, 1327 &stat_indir_blk_ptrs, 0, ""); 1328 SYSCTL_INT(_debug_softdep, OID_AUTO, inode_bitmap, CTLFLAG_RW, 1329 &stat_inode_bitmap, 0, ""); 1330 SYSCTL_INT(_debug_softdep, OID_AUTO, direct_blk_ptrs, CTLFLAG_RW, 1331 &stat_direct_blk_ptrs, 0, ""); 1332 SYSCTL_INT(_debug_softdep, OID_AUTO, dir_entry, CTLFLAG_RW, 1333 &stat_dir_entry, 0, ""); 1334 SYSCTL_INT(_debug_softdep, OID_AUTO, jaddref_rollback, CTLFLAG_RW, 1335 &stat_jaddref, 0, ""); 1336 SYSCTL_INT(_debug_softdep, OID_AUTO, jnewblk_rollback, CTLFLAG_RW, 1337 &stat_jnewblk, 0, ""); 1338 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_low, CTLFLAG_RW, 1339 &stat_journal_low, 0, ""); 1340 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_min, CTLFLAG_RW, 1341 &stat_journal_min, 0, ""); 1342 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_wait, CTLFLAG_RW, 1343 &stat_journal_wait, 0, ""); 1344 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_filepage, CTLFLAG_RW, 1345 &stat_jwait_filepage, 0, ""); 1346 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_freeblks, CTLFLAG_RW, 1347 &stat_jwait_freeblks, 0, ""); 1348 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_inode, CTLFLAG_RW, 1349 &stat_jwait_inode, 0, ""); 1350 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_newblk, CTLFLAG_RW, 1351 &stat_jwait_newblk, 0, ""); 1352 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_blkrequests, CTLFLAG_RW, 1353 &stat_cleanup_blkrequests, 0, ""); 1354 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_inorequests, CTLFLAG_RW, 1355 &stat_cleanup_inorequests, 0, ""); 1356 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_high_delay, CTLFLAG_RW, 1357 &stat_cleanup_high_delay, 0, ""); 1358 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_retries, CTLFLAG_RW, 1359 &stat_cleanup_retries, 0, ""); 1360 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_failures, CTLFLAG_RW, 1361 &stat_cleanup_failures, 0, ""); 1362 SYSCTL_INT(_debug_softdep, OID_AUTO, flushcache, CTLFLAG_RW, 1363 &softdep_flushcache, 0, ""); 1364 SYSCTL_INT(_debug_softdep, OID_AUTO, emptyjblocks, CTLFLAG_RD, 1365 &stat_emptyjblocks, 0, ""); 1366 1367 SYSCTL_DECL(_vfs_ffs); 1368 1369 /* Whether to recompute the summary at mount time */ 1370 static int compute_summary_at_mount = 0; 1371 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW, 1372 &compute_summary_at_mount, 0, "Recompute summary at mount"); 1373 static int print_threads = 0; 1374 SYSCTL_INT(_debug_softdep, OID_AUTO, print_threads, CTLFLAG_RW, 1375 &print_threads, 0, "Notify flusher thread start/stop"); 1376 1377 /* List of all filesystems mounted with soft updates */ 1378 static TAILQ_HEAD(, mount_softdeps) softdepmounts; 1379 1380 /* 1381 * This function cleans the worklist for a filesystem. 1382 * Each filesystem running with soft dependencies gets its own 1383 * thread to run in this function. The thread is started up in 1384 * softdep_mount and shutdown in softdep_unmount. They show up 1385 * as part of the kernel "bufdaemon" process whose process 1386 * entry is available in bufdaemonproc. 1387 */ 1388 static int searchfailed; 1389 extern struct proc *bufdaemonproc; 1390 static void 1391 softdep_flush(addr) 1392 void *addr; 1393 { 1394 struct mount *mp; 1395 struct thread *td; 1396 struct ufsmount *ump; 1397 1398 td = curthread; 1399 td->td_pflags |= TDP_NORUNNINGBUF; 1400 mp = (struct mount *)addr; 1401 ump = VFSTOUFS(mp); 1402 atomic_add_int(&stat_flush_threads, 1); 1403 ACQUIRE_LOCK(ump); 1404 ump->softdep_flags &= ~FLUSH_STARTING; 1405 wakeup(&ump->softdep_flushtd); 1406 FREE_LOCK(ump); 1407 if (print_threads) { 1408 if (stat_flush_threads == 1) 1409 printf("Running %s at pid %d\n", bufdaemonproc->p_comm, 1410 bufdaemonproc->p_pid); 1411 printf("Start thread %s\n", td->td_name); 1412 } 1413 for (;;) { 1414 while (softdep_process_worklist(mp, 0) > 0 || 1415 (MOUNTEDSUJ(mp) && 1416 VFSTOUFS(mp)->softdep_jblocks->jb_suspended)) 1417 kthread_suspend_check(); 1418 ACQUIRE_LOCK(ump); 1419 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1420 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, 1421 "sdflush", hz / 2); 1422 ump->softdep_flags &= ~FLUSH_CLEANUP; 1423 /* 1424 * Check to see if we are done and need to exit. 1425 */ 1426 if ((ump->softdep_flags & FLUSH_EXIT) == 0) { 1427 FREE_LOCK(ump); 1428 continue; 1429 } 1430 ump->softdep_flags &= ~FLUSH_EXIT; 1431 FREE_LOCK(ump); 1432 wakeup(&ump->softdep_flags); 1433 if (print_threads) 1434 printf("Stop thread %s: searchfailed %d, did cleanups %d\n", td->td_name, searchfailed, ump->um_softdep->sd_cleanups); 1435 atomic_subtract_int(&stat_flush_threads, 1); 1436 kthread_exit(); 1437 panic("kthread_exit failed\n"); 1438 } 1439 } 1440 1441 static void 1442 worklist_speedup(mp) 1443 struct mount *mp; 1444 { 1445 struct ufsmount *ump; 1446 1447 ump = VFSTOUFS(mp); 1448 LOCK_OWNED(ump); 1449 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1450 ump->softdep_flags |= FLUSH_CLEANUP; 1451 wakeup(&ump->softdep_flushtd); 1452 } 1453 1454 static int 1455 softdep_speedup(ump) 1456 struct ufsmount *ump; 1457 { 1458 struct ufsmount *altump; 1459 struct mount_softdeps *sdp; 1460 1461 LOCK_OWNED(ump); 1462 worklist_speedup(ump->um_mountp); 1463 bd_speedup(); 1464 /* 1465 * If we have global shortages, then we need other 1466 * filesystems to help with the cleanup. Here we wakeup a 1467 * flusher thread for a filesystem that is over its fair 1468 * share of resources. 1469 */ 1470 if (req_clear_inodedeps || req_clear_remove) { 1471 ACQUIRE_GBLLOCK(&lk); 1472 TAILQ_FOREACH(sdp, &softdepmounts, sd_next) { 1473 if ((altump = sdp->sd_ump) == ump) 1474 continue; 1475 if (((req_clear_inodedeps && 1476 altump->softdep_curdeps[D_INODEDEP] > 1477 max_softdeps / stat_flush_threads) || 1478 (req_clear_remove && 1479 altump->softdep_curdeps[D_DIRREM] > 1480 (max_softdeps / 2) / stat_flush_threads)) && 1481 TRY_ACQUIRE_LOCK(altump)) 1482 break; 1483 } 1484 if (sdp == NULL) { 1485 searchfailed++; 1486 FREE_GBLLOCK(&lk); 1487 } else { 1488 /* 1489 * Move to the end of the list so we pick a 1490 * different one on out next try. 1491 */ 1492 TAILQ_REMOVE(&softdepmounts, sdp, sd_next); 1493 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next); 1494 FREE_GBLLOCK(&lk); 1495 if ((altump->softdep_flags & 1496 (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1497 altump->softdep_flags |= FLUSH_CLEANUP; 1498 altump->um_softdep->sd_cleanups++; 1499 wakeup(&altump->softdep_flushtd); 1500 FREE_LOCK(altump); 1501 } 1502 } 1503 return (speedup_syncer()); 1504 } 1505 1506 /* 1507 * Add an item to the end of the work queue. 1508 * This routine requires that the lock be held. 1509 * This is the only routine that adds items to the list. 1510 * The following routine is the only one that removes items 1511 * and does so in order from first to last. 1512 */ 1513 1514 #define WK_HEAD 0x0001 /* Add to HEAD. */ 1515 #define WK_NODELAY 0x0002 /* Process immediately. */ 1516 1517 static void 1518 add_to_worklist(wk, flags) 1519 struct worklist *wk; 1520 int flags; 1521 { 1522 struct ufsmount *ump; 1523 1524 ump = VFSTOUFS(wk->wk_mp); 1525 LOCK_OWNED(ump); 1526 if (wk->wk_state & ONWORKLIST) 1527 panic("add_to_worklist: %s(0x%X) already on list", 1528 TYPENAME(wk->wk_type), wk->wk_state); 1529 wk->wk_state |= ONWORKLIST; 1530 if (ump->softdep_on_worklist == 0) { 1531 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1532 ump->softdep_worklist_tail = wk; 1533 } else if (flags & WK_HEAD) { 1534 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1535 } else { 1536 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list); 1537 ump->softdep_worklist_tail = wk; 1538 } 1539 ump->softdep_on_worklist += 1; 1540 if (flags & WK_NODELAY) 1541 worklist_speedup(wk->wk_mp); 1542 } 1543 1544 /* 1545 * Remove the item to be processed. If we are removing the last 1546 * item on the list, we need to recalculate the tail pointer. 1547 */ 1548 static void 1549 remove_from_worklist(wk) 1550 struct worklist *wk; 1551 { 1552 struct ufsmount *ump; 1553 1554 ump = VFSTOUFS(wk->wk_mp); 1555 if (ump->softdep_worklist_tail == wk) 1556 ump->softdep_worklist_tail = 1557 (struct worklist *)wk->wk_list.le_prev; 1558 WORKLIST_REMOVE(wk); 1559 ump->softdep_on_worklist -= 1; 1560 } 1561 1562 static void 1563 wake_worklist(wk) 1564 struct worklist *wk; 1565 { 1566 if (wk->wk_state & IOWAITING) { 1567 wk->wk_state &= ~IOWAITING; 1568 wakeup(wk); 1569 } 1570 } 1571 1572 static void 1573 wait_worklist(wk, wmesg) 1574 struct worklist *wk; 1575 char *wmesg; 1576 { 1577 struct ufsmount *ump; 1578 1579 ump = VFSTOUFS(wk->wk_mp); 1580 wk->wk_state |= IOWAITING; 1581 msleep(wk, LOCK_PTR(ump), PVM, wmesg, 0); 1582 } 1583 1584 /* 1585 * Process that runs once per second to handle items in the background queue. 1586 * 1587 * Note that we ensure that everything is done in the order in which they 1588 * appear in the queue. The code below depends on this property to ensure 1589 * that blocks of a file are freed before the inode itself is freed. This 1590 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated 1591 * until all the old ones have been purged from the dependency lists. 1592 */ 1593 static int 1594 softdep_process_worklist(mp, full) 1595 struct mount *mp; 1596 int full; 1597 { 1598 int cnt, matchcnt; 1599 struct ufsmount *ump; 1600 long starttime; 1601 1602 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp")); 1603 if (MOUNTEDSOFTDEP(mp) == 0) 1604 return (0); 1605 matchcnt = 0; 1606 ump = VFSTOUFS(mp); 1607 ACQUIRE_LOCK(ump); 1608 starttime = time_second; 1609 softdep_process_journal(mp, NULL, full ? MNT_WAIT : 0); 1610 check_clear_deps(mp); 1611 while (ump->softdep_on_worklist > 0) { 1612 if ((cnt = process_worklist_item(mp, 10, LK_NOWAIT)) == 0) 1613 break; 1614 else 1615 matchcnt += cnt; 1616 check_clear_deps(mp); 1617 /* 1618 * We do not generally want to stop for buffer space, but if 1619 * we are really being a buffer hog, we will stop and wait. 1620 */ 1621 if (should_yield()) { 1622 FREE_LOCK(ump); 1623 kern_yield(PRI_USER); 1624 bwillwrite(); 1625 ACQUIRE_LOCK(ump); 1626 } 1627 /* 1628 * Never allow processing to run for more than one 1629 * second. This gives the syncer thread the opportunity 1630 * to pause if appropriate. 1631 */ 1632 if (!full && starttime != time_second) 1633 break; 1634 } 1635 if (full == 0) 1636 journal_unsuspend(ump); 1637 FREE_LOCK(ump); 1638 return (matchcnt); 1639 } 1640 1641 /* 1642 * Process all removes associated with a vnode if we are running out of 1643 * journal space. Any other process which attempts to flush these will 1644 * be unable as we have the vnodes locked. 1645 */ 1646 static void 1647 process_removes(vp) 1648 struct vnode *vp; 1649 { 1650 struct inodedep *inodedep; 1651 struct dirrem *dirrem; 1652 struct ufsmount *ump; 1653 struct mount *mp; 1654 ino_t inum; 1655 1656 mp = vp->v_mount; 1657 ump = VFSTOUFS(mp); 1658 LOCK_OWNED(ump); 1659 inum = VTOI(vp)->i_number; 1660 for (;;) { 1661 top: 1662 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1663 return; 1664 LIST_FOREACH(dirrem, &inodedep->id_dirremhd, dm_inonext) { 1665 /* 1666 * If another thread is trying to lock this vnode 1667 * it will fail but we must wait for it to do so 1668 * before we can proceed. 1669 */ 1670 if (dirrem->dm_state & INPROGRESS) { 1671 wait_worklist(&dirrem->dm_list, "pwrwait"); 1672 goto top; 1673 } 1674 if ((dirrem->dm_state & (COMPLETE | ONWORKLIST)) == 1675 (COMPLETE | ONWORKLIST)) 1676 break; 1677 } 1678 if (dirrem == NULL) 1679 return; 1680 remove_from_worklist(&dirrem->dm_list); 1681 FREE_LOCK(ump); 1682 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1683 panic("process_removes: suspended filesystem"); 1684 handle_workitem_remove(dirrem, 0); 1685 vn_finished_secondary_write(mp); 1686 ACQUIRE_LOCK(ump); 1687 } 1688 } 1689 1690 /* 1691 * Process all truncations associated with a vnode if we are running out 1692 * of journal space. This is called when the vnode lock is already held 1693 * and no other process can clear the truncation. This function returns 1694 * a value greater than zero if it did any work. 1695 */ 1696 static void 1697 process_truncates(vp) 1698 struct vnode *vp; 1699 { 1700 struct inodedep *inodedep; 1701 struct freeblks *freeblks; 1702 struct ufsmount *ump; 1703 struct mount *mp; 1704 ino_t inum; 1705 int cgwait; 1706 1707 mp = vp->v_mount; 1708 ump = VFSTOUFS(mp); 1709 LOCK_OWNED(ump); 1710 inum = VTOI(vp)->i_number; 1711 for (;;) { 1712 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1713 return; 1714 cgwait = 0; 1715 TAILQ_FOREACH(freeblks, &inodedep->id_freeblklst, fb_next) { 1716 /* Journal entries not yet written. */ 1717 if (!LIST_EMPTY(&freeblks->fb_jblkdephd)) { 1718 jwait(&LIST_FIRST( 1719 &freeblks->fb_jblkdephd)->jb_list, 1720 MNT_WAIT); 1721 break; 1722 } 1723 /* Another thread is executing this item. */ 1724 if (freeblks->fb_state & INPROGRESS) { 1725 wait_worklist(&freeblks->fb_list, "ptrwait"); 1726 break; 1727 } 1728 /* Freeblks is waiting on a inode write. */ 1729 if ((freeblks->fb_state & COMPLETE) == 0) { 1730 FREE_LOCK(ump); 1731 ffs_update(vp, 1); 1732 ACQUIRE_LOCK(ump); 1733 break; 1734 } 1735 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST)) == 1736 (ALLCOMPLETE | ONWORKLIST)) { 1737 remove_from_worklist(&freeblks->fb_list); 1738 freeblks->fb_state |= INPROGRESS; 1739 FREE_LOCK(ump); 1740 if (vn_start_secondary_write(NULL, &mp, 1741 V_NOWAIT)) 1742 panic("process_truncates: " 1743 "suspended filesystem"); 1744 handle_workitem_freeblocks(freeblks, 0); 1745 vn_finished_secondary_write(mp); 1746 ACQUIRE_LOCK(ump); 1747 break; 1748 } 1749 if (freeblks->fb_cgwait) 1750 cgwait++; 1751 } 1752 if (cgwait) { 1753 FREE_LOCK(ump); 1754 sync_cgs(mp, MNT_WAIT); 1755 ffs_sync_snap(mp, MNT_WAIT); 1756 ACQUIRE_LOCK(ump); 1757 continue; 1758 } 1759 if (freeblks == NULL) 1760 break; 1761 } 1762 return; 1763 } 1764 1765 /* 1766 * Process one item on the worklist. 1767 */ 1768 static int 1769 process_worklist_item(mp, target, flags) 1770 struct mount *mp; 1771 int target; 1772 int flags; 1773 { 1774 struct worklist sentinel; 1775 struct worklist *wk; 1776 struct ufsmount *ump; 1777 int matchcnt; 1778 int error; 1779 1780 KASSERT(mp != NULL, ("process_worklist_item: NULL mp")); 1781 /* 1782 * If we are being called because of a process doing a 1783 * copy-on-write, then it is not safe to write as we may 1784 * recurse into the copy-on-write routine. 1785 */ 1786 if (curthread->td_pflags & TDP_COWINPROGRESS) 1787 return (-1); 1788 PHOLD(curproc); /* Don't let the stack go away. */ 1789 ump = VFSTOUFS(mp); 1790 LOCK_OWNED(ump); 1791 matchcnt = 0; 1792 sentinel.wk_mp = NULL; 1793 sentinel.wk_type = D_SENTINEL; 1794 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, &sentinel, wk_list); 1795 for (wk = LIST_NEXT(&sentinel, wk_list); wk != NULL; 1796 wk = LIST_NEXT(&sentinel, wk_list)) { 1797 if (wk->wk_type == D_SENTINEL) { 1798 LIST_REMOVE(&sentinel, wk_list); 1799 LIST_INSERT_AFTER(wk, &sentinel, wk_list); 1800 continue; 1801 } 1802 if (wk->wk_state & INPROGRESS) 1803 panic("process_worklist_item: %p already in progress.", 1804 wk); 1805 wk->wk_state |= INPROGRESS; 1806 remove_from_worklist(wk); 1807 FREE_LOCK(ump); 1808 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1809 panic("process_worklist_item: suspended filesystem"); 1810 switch (wk->wk_type) { 1811 case D_DIRREM: 1812 /* removal of a directory entry */ 1813 error = handle_workitem_remove(WK_DIRREM(wk), flags); 1814 break; 1815 1816 case D_FREEBLKS: 1817 /* releasing blocks and/or fragments from a file */ 1818 error = handle_workitem_freeblocks(WK_FREEBLKS(wk), 1819 flags); 1820 break; 1821 1822 case D_FREEFRAG: 1823 /* releasing a fragment when replaced as a file grows */ 1824 handle_workitem_freefrag(WK_FREEFRAG(wk)); 1825 error = 0; 1826 break; 1827 1828 case D_FREEFILE: 1829 /* releasing an inode when its link count drops to 0 */ 1830 handle_workitem_freefile(WK_FREEFILE(wk)); 1831 error = 0; 1832 break; 1833 1834 default: 1835 panic("%s_process_worklist: Unknown type %s", 1836 "softdep", TYPENAME(wk->wk_type)); 1837 /* NOTREACHED */ 1838 } 1839 vn_finished_secondary_write(mp); 1840 ACQUIRE_LOCK(ump); 1841 if (error == 0) { 1842 if (++matchcnt == target) 1843 break; 1844 continue; 1845 } 1846 /* 1847 * We have to retry the worklist item later. Wake up any 1848 * waiters who may be able to complete it immediately and 1849 * add the item back to the head so we don't try to execute 1850 * it again. 1851 */ 1852 wk->wk_state &= ~INPROGRESS; 1853 wake_worklist(wk); 1854 add_to_worklist(wk, WK_HEAD); 1855 } 1856 /* Sentinal could've become the tail from remove_from_worklist. */ 1857 if (ump->softdep_worklist_tail == &sentinel) 1858 ump->softdep_worklist_tail = 1859 (struct worklist *)sentinel.wk_list.le_prev; 1860 LIST_REMOVE(&sentinel, wk_list); 1861 PRELE(curproc); 1862 return (matchcnt); 1863 } 1864 1865 /* 1866 * Move dependencies from one buffer to another. 1867 */ 1868 int 1869 softdep_move_dependencies(oldbp, newbp) 1870 struct buf *oldbp; 1871 struct buf *newbp; 1872 { 1873 struct worklist *wk, *wktail; 1874 struct ufsmount *ump; 1875 int dirty; 1876 1877 if ((wk = LIST_FIRST(&oldbp->b_dep)) == NULL) 1878 return (0); 1879 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 1880 ("softdep_move_dependencies called on non-softdep filesystem")); 1881 dirty = 0; 1882 wktail = NULL; 1883 ump = VFSTOUFS(wk->wk_mp); 1884 ACQUIRE_LOCK(ump); 1885 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) { 1886 LIST_REMOVE(wk, wk_list); 1887 if (wk->wk_type == D_BMSAFEMAP && 1888 bmsafemap_backgroundwrite(WK_BMSAFEMAP(wk), newbp)) 1889 dirty = 1; 1890 if (wktail == NULL) 1891 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list); 1892 else 1893 LIST_INSERT_AFTER(wktail, wk, wk_list); 1894 wktail = wk; 1895 } 1896 FREE_LOCK(ump); 1897 1898 return (dirty); 1899 } 1900 1901 /* 1902 * Purge the work list of all items associated with a particular mount point. 1903 */ 1904 int 1905 softdep_flushworklist(oldmnt, countp, td) 1906 struct mount *oldmnt; 1907 int *countp; 1908 struct thread *td; 1909 { 1910 struct vnode *devvp; 1911 struct ufsmount *ump; 1912 int count, error; 1913 1914 /* 1915 * Alternately flush the block device associated with the mount 1916 * point and process any dependencies that the flushing 1917 * creates. We continue until no more worklist dependencies 1918 * are found. 1919 */ 1920 *countp = 0; 1921 error = 0; 1922 ump = VFSTOUFS(oldmnt); 1923 devvp = ump->um_devvp; 1924 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) { 1925 *countp += count; 1926 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1927 error = VOP_FSYNC(devvp, MNT_WAIT, td); 1928 VOP_UNLOCK(devvp, 0); 1929 if (error != 0) 1930 break; 1931 } 1932 return (error); 1933 } 1934 1935 #define SU_WAITIDLE_RETRIES 20 1936 static int 1937 softdep_waitidle(struct mount *mp, int flags __unused) 1938 { 1939 struct ufsmount *ump; 1940 struct vnode *devvp; 1941 struct thread *td; 1942 int error, i; 1943 1944 ump = VFSTOUFS(mp); 1945 devvp = ump->um_devvp; 1946 td = curthread; 1947 error = 0; 1948 ACQUIRE_LOCK(ump); 1949 for (i = 0; i < SU_WAITIDLE_RETRIES && ump->softdep_deps != 0; i++) { 1950 ump->softdep_req = 1; 1951 KASSERT((flags & FORCECLOSE) == 0 || 1952 ump->softdep_on_worklist == 0, 1953 ("softdep_waitidle: work added after flush")); 1954 msleep(&ump->softdep_deps, LOCK_PTR(ump), PVM | PDROP, 1955 "softdeps", 10 * hz); 1956 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1957 error = VOP_FSYNC(devvp, MNT_WAIT, td); 1958 VOP_UNLOCK(devvp, 0); 1959 ACQUIRE_LOCK(ump); 1960 if (error != 0) 1961 break; 1962 } 1963 ump->softdep_req = 0; 1964 if (i == SU_WAITIDLE_RETRIES && error == 0 && ump->softdep_deps != 0) { 1965 error = EBUSY; 1966 printf("softdep_waitidle: Failed to flush worklist for %p\n", 1967 mp); 1968 } 1969 FREE_LOCK(ump); 1970 return (error); 1971 } 1972 1973 /* 1974 * Flush all vnodes and worklist items associated with a specified mount point. 1975 */ 1976 int 1977 softdep_flushfiles(oldmnt, flags, td) 1978 struct mount *oldmnt; 1979 int flags; 1980 struct thread *td; 1981 { 1982 #ifdef QUOTA 1983 struct ufsmount *ump; 1984 int i; 1985 #endif 1986 int error, early, depcount, loopcnt, retry_flush_count, retry; 1987 int morework; 1988 1989 KASSERT(MOUNTEDSOFTDEP(oldmnt) != 0, 1990 ("softdep_flushfiles called on non-softdep filesystem")); 1991 loopcnt = 10; 1992 retry_flush_count = 3; 1993 retry_flush: 1994 error = 0; 1995 1996 /* 1997 * Alternately flush the vnodes associated with the mount 1998 * point and process any dependencies that the flushing 1999 * creates. In theory, this loop can happen at most twice, 2000 * but we give it a few extra just to be sure. 2001 */ 2002 for (; loopcnt > 0; loopcnt--) { 2003 /* 2004 * Do another flush in case any vnodes were brought in 2005 * as part of the cleanup operations. 2006 */ 2007 early = retry_flush_count == 1 || (oldmnt->mnt_kern_flag & 2008 MNTK_UNMOUNT) == 0 ? 0 : EARLYFLUSH; 2009 if ((error = ffs_flushfiles(oldmnt, flags | early, td)) != 0) 2010 break; 2011 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 || 2012 depcount == 0) 2013 break; 2014 } 2015 /* 2016 * If we are unmounting then it is an error to fail. If we 2017 * are simply trying to downgrade to read-only, then filesystem 2018 * activity can keep us busy forever, so we just fail with EBUSY. 2019 */ 2020 if (loopcnt == 0) { 2021 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) 2022 panic("softdep_flushfiles: looping"); 2023 error = EBUSY; 2024 } 2025 if (!error) 2026 error = softdep_waitidle(oldmnt, flags); 2027 if (!error) { 2028 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) { 2029 retry = 0; 2030 MNT_ILOCK(oldmnt); 2031 KASSERT((oldmnt->mnt_kern_flag & MNTK_NOINSMNTQ) != 0, 2032 ("softdep_flushfiles: !MNTK_NOINSMNTQ")); 2033 morework = oldmnt->mnt_nvnodelistsize > 0; 2034 #ifdef QUOTA 2035 ump = VFSTOUFS(oldmnt); 2036 UFS_LOCK(ump); 2037 for (i = 0; i < MAXQUOTAS; i++) { 2038 if (ump->um_quotas[i] != NULLVP) 2039 morework = 1; 2040 } 2041 UFS_UNLOCK(ump); 2042 #endif 2043 if (morework) { 2044 if (--retry_flush_count > 0) { 2045 retry = 1; 2046 loopcnt = 3; 2047 } else 2048 error = EBUSY; 2049 } 2050 MNT_IUNLOCK(oldmnt); 2051 if (retry) 2052 goto retry_flush; 2053 } 2054 } 2055 return (error); 2056 } 2057 2058 /* 2059 * Structure hashing. 2060 * 2061 * There are four types of structures that can be looked up: 2062 * 1) pagedep structures identified by mount point, inode number, 2063 * and logical block. 2064 * 2) inodedep structures identified by mount point and inode number. 2065 * 3) newblk structures identified by mount point and 2066 * physical block number. 2067 * 4) bmsafemap structures identified by mount point and 2068 * cylinder group number. 2069 * 2070 * The "pagedep" and "inodedep" dependency structures are hashed 2071 * separately from the file blocks and inodes to which they correspond. 2072 * This separation helps when the in-memory copy of an inode or 2073 * file block must be replaced. It also obviates the need to access 2074 * an inode or file page when simply updating (or de-allocating) 2075 * dependency structures. Lookup of newblk structures is needed to 2076 * find newly allocated blocks when trying to associate them with 2077 * their allocdirect or allocindir structure. 2078 * 2079 * The lookup routines optionally create and hash a new instance when 2080 * an existing entry is not found. The bmsafemap lookup routine always 2081 * allocates a new structure if an existing one is not found. 2082 */ 2083 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */ 2084 2085 /* 2086 * Structures and routines associated with pagedep caching. 2087 */ 2088 #define PAGEDEP_HASH(ump, inum, lbn) \ 2089 (&(ump)->pagedep_hashtbl[((inum) + (lbn)) & (ump)->pagedep_hash_size]) 2090 2091 static int 2092 pagedep_find(pagedephd, ino, lbn, pagedeppp) 2093 struct pagedep_hashhead *pagedephd; 2094 ino_t ino; 2095 ufs_lbn_t lbn; 2096 struct pagedep **pagedeppp; 2097 { 2098 struct pagedep *pagedep; 2099 2100 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 2101 if (ino == pagedep->pd_ino && lbn == pagedep->pd_lbn) { 2102 *pagedeppp = pagedep; 2103 return (1); 2104 } 2105 } 2106 *pagedeppp = NULL; 2107 return (0); 2108 } 2109 /* 2110 * Look up a pagedep. Return 1 if found, 0 otherwise. 2111 * If not found, allocate if DEPALLOC flag is passed. 2112 * Found or allocated entry is returned in pagedeppp. 2113 * This routine must be called with splbio interrupts blocked. 2114 */ 2115 static int 2116 pagedep_lookup(mp, bp, ino, lbn, flags, pagedeppp) 2117 struct mount *mp; 2118 struct buf *bp; 2119 ino_t ino; 2120 ufs_lbn_t lbn; 2121 int flags; 2122 struct pagedep **pagedeppp; 2123 { 2124 struct pagedep *pagedep; 2125 struct pagedep_hashhead *pagedephd; 2126 struct worklist *wk; 2127 struct ufsmount *ump; 2128 int ret; 2129 int i; 2130 2131 ump = VFSTOUFS(mp); 2132 LOCK_OWNED(ump); 2133 if (bp) { 2134 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 2135 if (wk->wk_type == D_PAGEDEP) { 2136 *pagedeppp = WK_PAGEDEP(wk); 2137 return (1); 2138 } 2139 } 2140 } 2141 pagedephd = PAGEDEP_HASH(ump, ino, lbn); 2142 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp); 2143 if (ret) { 2144 if (((*pagedeppp)->pd_state & ONWORKLIST) == 0 && bp) 2145 WORKLIST_INSERT(&bp->b_dep, &(*pagedeppp)->pd_list); 2146 return (1); 2147 } 2148 if ((flags & DEPALLOC) == 0) 2149 return (0); 2150 FREE_LOCK(ump); 2151 pagedep = malloc(sizeof(struct pagedep), 2152 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO); 2153 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp); 2154 ACQUIRE_LOCK(ump); 2155 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp); 2156 if (*pagedeppp) { 2157 /* 2158 * This should never happen since we only create pagedeps 2159 * with the vnode lock held. Could be an assert. 2160 */ 2161 WORKITEM_FREE(pagedep, D_PAGEDEP); 2162 return (ret); 2163 } 2164 pagedep->pd_ino = ino; 2165 pagedep->pd_lbn = lbn; 2166 LIST_INIT(&pagedep->pd_dirremhd); 2167 LIST_INIT(&pagedep->pd_pendinghd); 2168 for (i = 0; i < DAHASHSZ; i++) 2169 LIST_INIT(&pagedep->pd_diraddhd[i]); 2170 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash); 2171 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list); 2172 *pagedeppp = pagedep; 2173 return (0); 2174 } 2175 2176 /* 2177 * Structures and routines associated with inodedep caching. 2178 */ 2179 #define INODEDEP_HASH(ump, inum) \ 2180 (&(ump)->inodedep_hashtbl[(inum) & (ump)->inodedep_hash_size]) 2181 2182 static int 2183 inodedep_find(inodedephd, inum, inodedeppp) 2184 struct inodedep_hashhead *inodedephd; 2185 ino_t inum; 2186 struct inodedep **inodedeppp; 2187 { 2188 struct inodedep *inodedep; 2189 2190 LIST_FOREACH(inodedep, inodedephd, id_hash) 2191 if (inum == inodedep->id_ino) 2192 break; 2193 if (inodedep) { 2194 *inodedeppp = inodedep; 2195 return (1); 2196 } 2197 *inodedeppp = NULL; 2198 2199 return (0); 2200 } 2201 /* 2202 * Look up an inodedep. Return 1 if found, 0 if not found. 2203 * If not found, allocate if DEPALLOC flag is passed. 2204 * Found or allocated entry is returned in inodedeppp. 2205 * This routine must be called with splbio interrupts blocked. 2206 */ 2207 static int 2208 inodedep_lookup(mp, inum, flags, inodedeppp) 2209 struct mount *mp; 2210 ino_t inum; 2211 int flags; 2212 struct inodedep **inodedeppp; 2213 { 2214 struct inodedep *inodedep; 2215 struct inodedep_hashhead *inodedephd; 2216 struct ufsmount *ump; 2217 struct fs *fs; 2218 2219 ump = VFSTOUFS(mp); 2220 LOCK_OWNED(ump); 2221 fs = ump->um_fs; 2222 inodedephd = INODEDEP_HASH(ump, inum); 2223 2224 if (inodedep_find(inodedephd, inum, inodedeppp)) 2225 return (1); 2226 if ((flags & DEPALLOC) == 0) 2227 return (0); 2228 /* 2229 * If the system is over its limit and our filesystem is 2230 * responsible for more than our share of that usage and 2231 * we are not in a rush, request some inodedep cleanup. 2232 */ 2233 if (softdep_excess_items(ump, D_INODEDEP)) 2234 schedule_cleanup(mp); 2235 else 2236 FREE_LOCK(ump); 2237 inodedep = malloc(sizeof(struct inodedep), 2238 M_INODEDEP, M_SOFTDEP_FLAGS); 2239 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp); 2240 ACQUIRE_LOCK(ump); 2241 if (inodedep_find(inodedephd, inum, inodedeppp)) { 2242 WORKITEM_FREE(inodedep, D_INODEDEP); 2243 return (1); 2244 } 2245 inodedep->id_fs = fs; 2246 inodedep->id_ino = inum; 2247 inodedep->id_state = ALLCOMPLETE; 2248 inodedep->id_nlinkdelta = 0; 2249 inodedep->id_savedino1 = NULL; 2250 inodedep->id_savedsize = -1; 2251 inodedep->id_savedextsize = -1; 2252 inodedep->id_savednlink = -1; 2253 inodedep->id_bmsafemap = NULL; 2254 inodedep->id_mkdiradd = NULL; 2255 LIST_INIT(&inodedep->id_dirremhd); 2256 LIST_INIT(&inodedep->id_pendinghd); 2257 LIST_INIT(&inodedep->id_inowait); 2258 LIST_INIT(&inodedep->id_bufwait); 2259 TAILQ_INIT(&inodedep->id_inoreflst); 2260 TAILQ_INIT(&inodedep->id_inoupdt); 2261 TAILQ_INIT(&inodedep->id_newinoupdt); 2262 TAILQ_INIT(&inodedep->id_extupdt); 2263 TAILQ_INIT(&inodedep->id_newextupdt); 2264 TAILQ_INIT(&inodedep->id_freeblklst); 2265 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash); 2266 *inodedeppp = inodedep; 2267 return (0); 2268 } 2269 2270 /* 2271 * Structures and routines associated with newblk caching. 2272 */ 2273 #define NEWBLK_HASH(ump, inum) \ 2274 (&(ump)->newblk_hashtbl[(inum) & (ump)->newblk_hash_size]) 2275 2276 static int 2277 newblk_find(newblkhd, newblkno, flags, newblkpp) 2278 struct newblk_hashhead *newblkhd; 2279 ufs2_daddr_t newblkno; 2280 int flags; 2281 struct newblk **newblkpp; 2282 { 2283 struct newblk *newblk; 2284 2285 LIST_FOREACH(newblk, newblkhd, nb_hash) { 2286 if (newblkno != newblk->nb_newblkno) 2287 continue; 2288 /* 2289 * If we're creating a new dependency don't match those that 2290 * have already been converted to allocdirects. This is for 2291 * a frag extend. 2292 */ 2293 if ((flags & DEPALLOC) && newblk->nb_list.wk_type != D_NEWBLK) 2294 continue; 2295 break; 2296 } 2297 if (newblk) { 2298 *newblkpp = newblk; 2299 return (1); 2300 } 2301 *newblkpp = NULL; 2302 return (0); 2303 } 2304 2305 /* 2306 * Look up a newblk. Return 1 if found, 0 if not found. 2307 * If not found, allocate if DEPALLOC flag is passed. 2308 * Found or allocated entry is returned in newblkpp. 2309 */ 2310 static int 2311 newblk_lookup(mp, newblkno, flags, newblkpp) 2312 struct mount *mp; 2313 ufs2_daddr_t newblkno; 2314 int flags; 2315 struct newblk **newblkpp; 2316 { 2317 struct newblk *newblk; 2318 struct newblk_hashhead *newblkhd; 2319 struct ufsmount *ump; 2320 2321 ump = VFSTOUFS(mp); 2322 LOCK_OWNED(ump); 2323 newblkhd = NEWBLK_HASH(ump, newblkno); 2324 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) 2325 return (1); 2326 if ((flags & DEPALLOC) == 0) 2327 return (0); 2328 if (softdep_excess_items(ump, D_NEWBLK) || 2329 softdep_excess_items(ump, D_ALLOCDIRECT) || 2330 softdep_excess_items(ump, D_ALLOCINDIR)) 2331 schedule_cleanup(mp); 2332 else 2333 FREE_LOCK(ump); 2334 newblk = malloc(sizeof(union allblk), M_NEWBLK, 2335 M_SOFTDEP_FLAGS | M_ZERO); 2336 workitem_alloc(&newblk->nb_list, D_NEWBLK, mp); 2337 ACQUIRE_LOCK(ump); 2338 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) { 2339 WORKITEM_FREE(newblk, D_NEWBLK); 2340 return (1); 2341 } 2342 newblk->nb_freefrag = NULL; 2343 LIST_INIT(&newblk->nb_indirdeps); 2344 LIST_INIT(&newblk->nb_newdirblk); 2345 LIST_INIT(&newblk->nb_jwork); 2346 newblk->nb_state = ATTACHED; 2347 newblk->nb_newblkno = newblkno; 2348 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash); 2349 *newblkpp = newblk; 2350 return (0); 2351 } 2352 2353 /* 2354 * Structures and routines associated with freed indirect block caching. 2355 */ 2356 #define INDIR_HASH(ump, blkno) \ 2357 (&(ump)->indir_hashtbl[(blkno) & (ump)->indir_hash_size]) 2358 2359 /* 2360 * Lookup an indirect block in the indir hash table. The freework is 2361 * removed and potentially freed. The caller must do a blocking journal 2362 * write before writing to the blkno. 2363 */ 2364 static int 2365 indirblk_lookup(mp, blkno) 2366 struct mount *mp; 2367 ufs2_daddr_t blkno; 2368 { 2369 struct freework *freework; 2370 struct indir_hashhead *wkhd; 2371 struct ufsmount *ump; 2372 2373 ump = VFSTOUFS(mp); 2374 wkhd = INDIR_HASH(ump, blkno); 2375 TAILQ_FOREACH(freework, wkhd, fw_next) { 2376 if (freework->fw_blkno != blkno) 2377 continue; 2378 indirblk_remove(freework); 2379 return (1); 2380 } 2381 return (0); 2382 } 2383 2384 /* 2385 * Insert an indirect block represented by freework into the indirblk 2386 * hash table so that it may prevent the block from being re-used prior 2387 * to the journal being written. 2388 */ 2389 static void 2390 indirblk_insert(freework) 2391 struct freework *freework; 2392 { 2393 struct jblocks *jblocks; 2394 struct jseg *jseg; 2395 struct ufsmount *ump; 2396 2397 ump = VFSTOUFS(freework->fw_list.wk_mp); 2398 jblocks = ump->softdep_jblocks; 2399 jseg = TAILQ_LAST(&jblocks->jb_segs, jseglst); 2400 if (jseg == NULL) 2401 return; 2402 2403 LIST_INSERT_HEAD(&jseg->js_indirs, freework, fw_segs); 2404 TAILQ_INSERT_HEAD(INDIR_HASH(ump, freework->fw_blkno), freework, 2405 fw_next); 2406 freework->fw_state &= ~DEPCOMPLETE; 2407 } 2408 2409 static void 2410 indirblk_remove(freework) 2411 struct freework *freework; 2412 { 2413 struct ufsmount *ump; 2414 2415 ump = VFSTOUFS(freework->fw_list.wk_mp); 2416 LIST_REMOVE(freework, fw_segs); 2417 TAILQ_REMOVE(INDIR_HASH(ump, freework->fw_blkno), freework, fw_next); 2418 freework->fw_state |= DEPCOMPLETE; 2419 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 2420 WORKITEM_FREE(freework, D_FREEWORK); 2421 } 2422 2423 /* 2424 * Executed during filesystem system initialization before 2425 * mounting any filesystems. 2426 */ 2427 void 2428 softdep_initialize() 2429 { 2430 2431 TAILQ_INIT(&softdepmounts); 2432 #ifdef __LP64__ 2433 max_softdeps = desiredvnodes * 4; 2434 #else 2435 max_softdeps = desiredvnodes * 2; 2436 #endif 2437 2438 /* initialise bioops hack */ 2439 bioops.io_start = softdep_disk_io_initiation; 2440 bioops.io_complete = softdep_disk_write_complete; 2441 bioops.io_deallocate = softdep_deallocate_dependencies; 2442 bioops.io_countdeps = softdep_count_dependencies; 2443 softdep_ast_cleanup = softdep_ast_cleanup_proc; 2444 2445 /* Initialize the callout with an mtx. */ 2446 callout_init_mtx(&softdep_callout, &lk, 0); 2447 } 2448 2449 /* 2450 * Executed after all filesystems have been unmounted during 2451 * filesystem module unload. 2452 */ 2453 void 2454 softdep_uninitialize() 2455 { 2456 2457 /* clear bioops hack */ 2458 bioops.io_start = NULL; 2459 bioops.io_complete = NULL; 2460 bioops.io_deallocate = NULL; 2461 bioops.io_countdeps = NULL; 2462 softdep_ast_cleanup = NULL; 2463 2464 callout_drain(&softdep_callout); 2465 } 2466 2467 /* 2468 * Called at mount time to notify the dependency code that a 2469 * filesystem wishes to use it. 2470 */ 2471 int 2472 softdep_mount(devvp, mp, fs, cred) 2473 struct vnode *devvp; 2474 struct mount *mp; 2475 struct fs *fs; 2476 struct ucred *cred; 2477 { 2478 struct csum_total cstotal; 2479 struct mount_softdeps *sdp; 2480 struct ufsmount *ump; 2481 struct cg *cgp; 2482 struct buf *bp; 2483 u_int cyl, i; 2484 int error; 2485 2486 sdp = malloc(sizeof(struct mount_softdeps), M_MOUNTDATA, 2487 M_WAITOK | M_ZERO); 2488 MNT_ILOCK(mp); 2489 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP; 2490 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) { 2491 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) | 2492 MNTK_SOFTDEP | MNTK_NOASYNC; 2493 } 2494 ump = VFSTOUFS(mp); 2495 ump->um_softdep = sdp; 2496 MNT_IUNLOCK(mp); 2497 rw_init(LOCK_PTR(ump), "Per-Filesystem Softdep Lock"); 2498 sdp->sd_ump = ump; 2499 LIST_INIT(&ump->softdep_workitem_pending); 2500 LIST_INIT(&ump->softdep_journal_pending); 2501 TAILQ_INIT(&ump->softdep_unlinked); 2502 LIST_INIT(&ump->softdep_dirtycg); 2503 ump->softdep_worklist_tail = NULL; 2504 ump->softdep_on_worklist = 0; 2505 ump->softdep_deps = 0; 2506 LIST_INIT(&ump->softdep_mkdirlisthd); 2507 ump->pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP, 2508 &ump->pagedep_hash_size); 2509 ump->pagedep_nextclean = 0; 2510 ump->inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP, 2511 &ump->inodedep_hash_size); 2512 ump->inodedep_nextclean = 0; 2513 ump->newblk_hashtbl = hashinit(max_softdeps / 2, M_NEWBLK, 2514 &ump->newblk_hash_size); 2515 ump->bmsafemap_hashtbl = hashinit(1024, M_BMSAFEMAP, 2516 &ump->bmsafemap_hash_size); 2517 i = 1 << (ffs(desiredvnodes / 10) - 1); 2518 ump->indir_hashtbl = malloc(i * sizeof(struct indir_hashhead), 2519 M_FREEWORK, M_WAITOK); 2520 ump->indir_hash_size = i - 1; 2521 for (i = 0; i <= ump->indir_hash_size; i++) 2522 TAILQ_INIT(&ump->indir_hashtbl[i]); 2523 ACQUIRE_GBLLOCK(&lk); 2524 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next); 2525 FREE_GBLLOCK(&lk); 2526 if ((fs->fs_flags & FS_SUJ) && 2527 (error = journal_mount(mp, fs, cred)) != 0) { 2528 printf("Failed to start journal: %d\n", error); 2529 softdep_unmount(mp); 2530 return (error); 2531 } 2532 /* 2533 * Start our flushing thread in the bufdaemon process. 2534 */ 2535 ACQUIRE_LOCK(ump); 2536 ump->softdep_flags |= FLUSH_STARTING; 2537 FREE_LOCK(ump); 2538 kproc_kthread_add(&softdep_flush, mp, &bufdaemonproc, 2539 &ump->softdep_flushtd, 0, 0, "softdepflush", "%s worker", 2540 mp->mnt_stat.f_mntonname); 2541 ACQUIRE_LOCK(ump); 2542 while ((ump->softdep_flags & FLUSH_STARTING) != 0) { 2543 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, "sdstart", 2544 hz / 2); 2545 } 2546 FREE_LOCK(ump); 2547 /* 2548 * When doing soft updates, the counters in the 2549 * superblock may have gotten out of sync. Recomputation 2550 * can take a long time and can be deferred for background 2551 * fsck. However, the old behavior of scanning the cylinder 2552 * groups and recalculating them at mount time is available 2553 * by setting vfs.ffs.compute_summary_at_mount to one. 2554 */ 2555 if (compute_summary_at_mount == 0 || fs->fs_clean != 0) 2556 return (0); 2557 bzero(&cstotal, sizeof cstotal); 2558 for (cyl = 0; cyl < fs->fs_ncg; cyl++) { 2559 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)), 2560 fs->fs_cgsize, cred, &bp)) != 0) { 2561 brelse(bp); 2562 softdep_unmount(mp); 2563 return (error); 2564 } 2565 cgp = (struct cg *)bp->b_data; 2566 cstotal.cs_nffree += cgp->cg_cs.cs_nffree; 2567 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree; 2568 cstotal.cs_nifree += cgp->cg_cs.cs_nifree; 2569 cstotal.cs_ndir += cgp->cg_cs.cs_ndir; 2570 fs->fs_cs(fs, cyl) = cgp->cg_cs; 2571 brelse(bp); 2572 } 2573 #ifdef INVARIANTS 2574 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal)) 2575 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt); 2576 #endif 2577 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal); 2578 return (0); 2579 } 2580 2581 void 2582 softdep_unmount(mp) 2583 struct mount *mp; 2584 { 2585 struct ufsmount *ump; 2586 #ifdef INVARIANTS 2587 int i; 2588 #endif 2589 2590 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 2591 ("softdep_unmount called on non-softdep filesystem")); 2592 ump = VFSTOUFS(mp); 2593 MNT_ILOCK(mp); 2594 mp->mnt_flag &= ~MNT_SOFTDEP; 2595 if (MOUNTEDSUJ(mp) == 0) { 2596 MNT_IUNLOCK(mp); 2597 } else { 2598 mp->mnt_flag &= ~MNT_SUJ; 2599 MNT_IUNLOCK(mp); 2600 journal_unmount(ump); 2601 } 2602 /* 2603 * Shut down our flushing thread. Check for NULL is if 2604 * softdep_mount errors out before the thread has been created. 2605 */ 2606 if (ump->softdep_flushtd != NULL) { 2607 ACQUIRE_LOCK(ump); 2608 ump->softdep_flags |= FLUSH_EXIT; 2609 wakeup(&ump->softdep_flushtd); 2610 msleep(&ump->softdep_flags, LOCK_PTR(ump), PVM | PDROP, 2611 "sdwait", 0); 2612 KASSERT((ump->softdep_flags & FLUSH_EXIT) == 0, 2613 ("Thread shutdown failed")); 2614 } 2615 /* 2616 * Free up our resources. 2617 */ 2618 ACQUIRE_GBLLOCK(&lk); 2619 TAILQ_REMOVE(&softdepmounts, ump->um_softdep, sd_next); 2620 FREE_GBLLOCK(&lk); 2621 rw_destroy(LOCK_PTR(ump)); 2622 hashdestroy(ump->pagedep_hashtbl, M_PAGEDEP, ump->pagedep_hash_size); 2623 hashdestroy(ump->inodedep_hashtbl, M_INODEDEP, ump->inodedep_hash_size); 2624 hashdestroy(ump->newblk_hashtbl, M_NEWBLK, ump->newblk_hash_size); 2625 hashdestroy(ump->bmsafemap_hashtbl, M_BMSAFEMAP, 2626 ump->bmsafemap_hash_size); 2627 free(ump->indir_hashtbl, M_FREEWORK); 2628 #ifdef INVARIANTS 2629 for (i = 0; i <= D_LAST; i++) 2630 KASSERT(ump->softdep_curdeps[i] == 0, 2631 ("Unmount %s: Dep type %s != 0 (%ld)", ump->um_fs->fs_fsmnt, 2632 TYPENAME(i), ump->softdep_curdeps[i])); 2633 #endif 2634 free(ump->um_softdep, M_MOUNTDATA); 2635 } 2636 2637 static struct jblocks * 2638 jblocks_create(void) 2639 { 2640 struct jblocks *jblocks; 2641 2642 jblocks = malloc(sizeof(*jblocks), M_JBLOCKS, M_WAITOK | M_ZERO); 2643 TAILQ_INIT(&jblocks->jb_segs); 2644 jblocks->jb_avail = 10; 2645 jblocks->jb_extent = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2646 M_JBLOCKS, M_WAITOK | M_ZERO); 2647 2648 return (jblocks); 2649 } 2650 2651 static ufs2_daddr_t 2652 jblocks_alloc(jblocks, bytes, actual) 2653 struct jblocks *jblocks; 2654 int bytes; 2655 int *actual; 2656 { 2657 ufs2_daddr_t daddr; 2658 struct jextent *jext; 2659 int freecnt; 2660 int blocks; 2661 2662 blocks = bytes / DEV_BSIZE; 2663 jext = &jblocks->jb_extent[jblocks->jb_head]; 2664 freecnt = jext->je_blocks - jblocks->jb_off; 2665 if (freecnt == 0) { 2666 jblocks->jb_off = 0; 2667 if (++jblocks->jb_head > jblocks->jb_used) 2668 jblocks->jb_head = 0; 2669 jext = &jblocks->jb_extent[jblocks->jb_head]; 2670 freecnt = jext->je_blocks; 2671 } 2672 if (freecnt > blocks) 2673 freecnt = blocks; 2674 *actual = freecnt * DEV_BSIZE; 2675 daddr = jext->je_daddr + jblocks->jb_off; 2676 jblocks->jb_off += freecnt; 2677 jblocks->jb_free -= freecnt; 2678 2679 return (daddr); 2680 } 2681 2682 static void 2683 jblocks_free(jblocks, mp, bytes) 2684 struct jblocks *jblocks; 2685 struct mount *mp; 2686 int bytes; 2687 { 2688 2689 LOCK_OWNED(VFSTOUFS(mp)); 2690 jblocks->jb_free += bytes / DEV_BSIZE; 2691 if (jblocks->jb_suspended) 2692 worklist_speedup(mp); 2693 wakeup(jblocks); 2694 } 2695 2696 static void 2697 jblocks_destroy(jblocks) 2698 struct jblocks *jblocks; 2699 { 2700 2701 if (jblocks->jb_extent) 2702 free(jblocks->jb_extent, M_JBLOCKS); 2703 free(jblocks, M_JBLOCKS); 2704 } 2705 2706 static void 2707 jblocks_add(jblocks, daddr, blocks) 2708 struct jblocks *jblocks; 2709 ufs2_daddr_t daddr; 2710 int blocks; 2711 { 2712 struct jextent *jext; 2713 2714 jblocks->jb_blocks += blocks; 2715 jblocks->jb_free += blocks; 2716 jext = &jblocks->jb_extent[jblocks->jb_used]; 2717 /* Adding the first block. */ 2718 if (jext->je_daddr == 0) { 2719 jext->je_daddr = daddr; 2720 jext->je_blocks = blocks; 2721 return; 2722 } 2723 /* Extending the last extent. */ 2724 if (jext->je_daddr + jext->je_blocks == daddr) { 2725 jext->je_blocks += blocks; 2726 return; 2727 } 2728 /* Adding a new extent. */ 2729 if (++jblocks->jb_used == jblocks->jb_avail) { 2730 jblocks->jb_avail *= 2; 2731 jext = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2732 M_JBLOCKS, M_WAITOK | M_ZERO); 2733 memcpy(jext, jblocks->jb_extent, 2734 sizeof(struct jextent) * jblocks->jb_used); 2735 free(jblocks->jb_extent, M_JBLOCKS); 2736 jblocks->jb_extent = jext; 2737 } 2738 jext = &jblocks->jb_extent[jblocks->jb_used]; 2739 jext->je_daddr = daddr; 2740 jext->je_blocks = blocks; 2741 return; 2742 } 2743 2744 int 2745 softdep_journal_lookup(mp, vpp) 2746 struct mount *mp; 2747 struct vnode **vpp; 2748 { 2749 struct componentname cnp; 2750 struct vnode *dvp; 2751 ino_t sujournal; 2752 int error; 2753 2754 error = VFS_VGET(mp, UFS_ROOTINO, LK_EXCLUSIVE, &dvp); 2755 if (error) 2756 return (error); 2757 bzero(&cnp, sizeof(cnp)); 2758 cnp.cn_nameiop = LOOKUP; 2759 cnp.cn_flags = ISLASTCN; 2760 cnp.cn_thread = curthread; 2761 cnp.cn_cred = curthread->td_ucred; 2762 cnp.cn_pnbuf = SUJ_FILE; 2763 cnp.cn_nameptr = SUJ_FILE; 2764 cnp.cn_namelen = strlen(SUJ_FILE); 2765 error = ufs_lookup_ino(dvp, NULL, &cnp, &sujournal); 2766 vput(dvp); 2767 if (error != 0) 2768 return (error); 2769 error = VFS_VGET(mp, sujournal, LK_EXCLUSIVE, vpp); 2770 return (error); 2771 } 2772 2773 /* 2774 * Open and verify the journal file. 2775 */ 2776 static int 2777 journal_mount(mp, fs, cred) 2778 struct mount *mp; 2779 struct fs *fs; 2780 struct ucred *cred; 2781 { 2782 struct jblocks *jblocks; 2783 struct ufsmount *ump; 2784 struct vnode *vp; 2785 struct inode *ip; 2786 ufs2_daddr_t blkno; 2787 int bcount; 2788 int error; 2789 int i; 2790 2791 ump = VFSTOUFS(mp); 2792 ump->softdep_journal_tail = NULL; 2793 ump->softdep_on_journal = 0; 2794 ump->softdep_accdeps = 0; 2795 ump->softdep_req = 0; 2796 ump->softdep_jblocks = NULL; 2797 error = softdep_journal_lookup(mp, &vp); 2798 if (error != 0) { 2799 printf("Failed to find journal. Use tunefs to create one\n"); 2800 return (error); 2801 } 2802 ip = VTOI(vp); 2803 if (ip->i_size < SUJ_MIN) { 2804 error = ENOSPC; 2805 goto out; 2806 } 2807 bcount = lblkno(fs, ip->i_size); /* Only use whole blocks. */ 2808 jblocks = jblocks_create(); 2809 for (i = 0; i < bcount; i++) { 2810 error = ufs_bmaparray(vp, i, &blkno, NULL, NULL, NULL); 2811 if (error) 2812 break; 2813 jblocks_add(jblocks, blkno, fsbtodb(fs, fs->fs_frag)); 2814 } 2815 if (error) { 2816 jblocks_destroy(jblocks); 2817 goto out; 2818 } 2819 jblocks->jb_low = jblocks->jb_free / 3; /* Reserve 33%. */ 2820 jblocks->jb_min = jblocks->jb_free / 10; /* Suspend at 10%. */ 2821 ump->softdep_jblocks = jblocks; 2822 out: 2823 if (error == 0) { 2824 MNT_ILOCK(mp); 2825 mp->mnt_flag |= MNT_SUJ; 2826 mp->mnt_flag &= ~MNT_SOFTDEP; 2827 MNT_IUNLOCK(mp); 2828 /* 2829 * Only validate the journal contents if the 2830 * filesystem is clean, otherwise we write the logs 2831 * but they'll never be used. If the filesystem was 2832 * still dirty when we mounted it the journal is 2833 * invalid and a new journal can only be valid if it 2834 * starts from a clean mount. 2835 */ 2836 if (fs->fs_clean) { 2837 DIP_SET(ip, i_modrev, fs->fs_mtime); 2838 ip->i_flags |= IN_MODIFIED; 2839 ffs_update(vp, 1); 2840 } 2841 } 2842 vput(vp); 2843 return (error); 2844 } 2845 2846 static void 2847 journal_unmount(ump) 2848 struct ufsmount *ump; 2849 { 2850 2851 if (ump->softdep_jblocks) 2852 jblocks_destroy(ump->softdep_jblocks); 2853 ump->softdep_jblocks = NULL; 2854 } 2855 2856 /* 2857 * Called when a journal record is ready to be written. Space is allocated 2858 * and the journal entry is created when the journal is flushed to stable 2859 * store. 2860 */ 2861 static void 2862 add_to_journal(wk) 2863 struct worklist *wk; 2864 { 2865 struct ufsmount *ump; 2866 2867 ump = VFSTOUFS(wk->wk_mp); 2868 LOCK_OWNED(ump); 2869 if (wk->wk_state & ONWORKLIST) 2870 panic("add_to_journal: %s(0x%X) already on list", 2871 TYPENAME(wk->wk_type), wk->wk_state); 2872 wk->wk_state |= ONWORKLIST | DEPCOMPLETE; 2873 if (LIST_EMPTY(&ump->softdep_journal_pending)) { 2874 ump->softdep_jblocks->jb_age = ticks; 2875 LIST_INSERT_HEAD(&ump->softdep_journal_pending, wk, wk_list); 2876 } else 2877 LIST_INSERT_AFTER(ump->softdep_journal_tail, wk, wk_list); 2878 ump->softdep_journal_tail = wk; 2879 ump->softdep_on_journal += 1; 2880 } 2881 2882 /* 2883 * Remove an arbitrary item for the journal worklist maintain the tail 2884 * pointer. This happens when a new operation obviates the need to 2885 * journal an old operation. 2886 */ 2887 static void 2888 remove_from_journal(wk) 2889 struct worklist *wk; 2890 { 2891 struct ufsmount *ump; 2892 2893 ump = VFSTOUFS(wk->wk_mp); 2894 LOCK_OWNED(ump); 2895 #ifdef INVARIANTS 2896 { 2897 struct worklist *wkn; 2898 2899 LIST_FOREACH(wkn, &ump->softdep_journal_pending, wk_list) 2900 if (wkn == wk) 2901 break; 2902 if (wkn == NULL) 2903 panic("remove_from_journal: %p is not in journal", wk); 2904 } 2905 #endif 2906 /* 2907 * We emulate a TAILQ to save space in most structures which do not 2908 * require TAILQ semantics. Here we must update the tail position 2909 * when removing the tail which is not the final entry. This works 2910 * only if the worklist linkage are at the beginning of the structure. 2911 */ 2912 if (ump->softdep_journal_tail == wk) 2913 ump->softdep_journal_tail = 2914 (struct worklist *)wk->wk_list.le_prev; 2915 WORKLIST_REMOVE(wk); 2916 ump->softdep_on_journal -= 1; 2917 } 2918 2919 /* 2920 * Check for journal space as well as dependency limits so the prelink 2921 * code can throttle both journaled and non-journaled filesystems. 2922 * Threshold is 0 for low and 1 for min. 2923 */ 2924 static int 2925 journal_space(ump, thresh) 2926 struct ufsmount *ump; 2927 int thresh; 2928 { 2929 struct jblocks *jblocks; 2930 int limit, avail; 2931 2932 jblocks = ump->softdep_jblocks; 2933 if (jblocks == NULL) 2934 return (1); 2935 /* 2936 * We use a tighter restriction here to prevent request_cleanup() 2937 * running in threads from running into locks we currently hold. 2938 * We have to be over the limit and our filesystem has to be 2939 * responsible for more than our share of that usage. 2940 */ 2941 limit = (max_softdeps / 10) * 9; 2942 if (dep_current[D_INODEDEP] > limit && 2943 ump->softdep_curdeps[D_INODEDEP] > limit / stat_flush_threads) 2944 return (0); 2945 if (thresh) 2946 thresh = jblocks->jb_min; 2947 else 2948 thresh = jblocks->jb_low; 2949 avail = (ump->softdep_on_journal * JREC_SIZE) / DEV_BSIZE; 2950 avail = jblocks->jb_free - avail; 2951 2952 return (avail > thresh); 2953 } 2954 2955 static void 2956 journal_suspend(ump) 2957 struct ufsmount *ump; 2958 { 2959 struct jblocks *jblocks; 2960 struct mount *mp; 2961 2962 mp = UFSTOVFS(ump); 2963 jblocks = ump->softdep_jblocks; 2964 MNT_ILOCK(mp); 2965 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) { 2966 stat_journal_min++; 2967 mp->mnt_kern_flag |= MNTK_SUSPEND; 2968 mp->mnt_susp_owner = ump->softdep_flushtd; 2969 } 2970 jblocks->jb_suspended = 1; 2971 MNT_IUNLOCK(mp); 2972 } 2973 2974 static int 2975 journal_unsuspend(struct ufsmount *ump) 2976 { 2977 struct jblocks *jblocks; 2978 struct mount *mp; 2979 2980 mp = UFSTOVFS(ump); 2981 jblocks = ump->softdep_jblocks; 2982 2983 if (jblocks != NULL && jblocks->jb_suspended && 2984 journal_space(ump, jblocks->jb_min)) { 2985 jblocks->jb_suspended = 0; 2986 FREE_LOCK(ump); 2987 mp->mnt_susp_owner = curthread; 2988 vfs_write_resume(mp, 0); 2989 ACQUIRE_LOCK(ump); 2990 return (1); 2991 } 2992 return (0); 2993 } 2994 2995 /* 2996 * Called before any allocation function to be certain that there is 2997 * sufficient space in the journal prior to creating any new records. 2998 * Since in the case of block allocation we may have multiple locked 2999 * buffers at the time of the actual allocation we can not block 3000 * when the journal records are created. Doing so would create a deadlock 3001 * if any of these buffers needed to be flushed to reclaim space. Instead 3002 * we require a sufficiently large amount of available space such that 3003 * each thread in the system could have passed this allocation check and 3004 * still have sufficient free space. With 20% of a minimum journal size 3005 * of 1MB we have 6553 records available. 3006 */ 3007 int 3008 softdep_prealloc(vp, waitok) 3009 struct vnode *vp; 3010 int waitok; 3011 { 3012 struct ufsmount *ump; 3013 3014 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 3015 ("softdep_prealloc called on non-softdep filesystem")); 3016 /* 3017 * Nothing to do if we are not running journaled soft updates. 3018 * If we currently hold the snapshot lock, we must avoid 3019 * handling other resources that could cause deadlock. Do not 3020 * touch quotas vnode since it is typically recursed with 3021 * other vnode locks held. 3022 */ 3023 if (DOINGSUJ(vp) == 0 || IS_SNAPSHOT(VTOI(vp)) || 3024 (vp->v_vflag & VV_SYSTEM) != 0) 3025 return (0); 3026 ump = VFSTOUFS(vp->v_mount); 3027 ACQUIRE_LOCK(ump); 3028 if (journal_space(ump, 0)) { 3029 FREE_LOCK(ump); 3030 return (0); 3031 } 3032 stat_journal_low++; 3033 FREE_LOCK(ump); 3034 if (waitok == MNT_NOWAIT) 3035 return (ENOSPC); 3036 /* 3037 * Attempt to sync this vnode once to flush any journal 3038 * work attached to it. 3039 */ 3040 if ((curthread->td_pflags & TDP_COWINPROGRESS) == 0) 3041 ffs_syncvnode(vp, waitok, 0); 3042 ACQUIRE_LOCK(ump); 3043 process_removes(vp); 3044 process_truncates(vp); 3045 if (journal_space(ump, 0) == 0) { 3046 softdep_speedup(ump); 3047 if (journal_space(ump, 1) == 0) 3048 journal_suspend(ump); 3049 } 3050 FREE_LOCK(ump); 3051 3052 return (0); 3053 } 3054 3055 /* 3056 * Before adjusting a link count on a vnode verify that we have sufficient 3057 * journal space. If not, process operations that depend on the currently 3058 * locked pair of vnodes to try to flush space as the syncer, buf daemon, 3059 * and softdep flush threads can not acquire these locks to reclaim space. 3060 */ 3061 static void 3062 softdep_prelink(dvp, vp) 3063 struct vnode *dvp; 3064 struct vnode *vp; 3065 { 3066 struct ufsmount *ump; 3067 3068 ump = VFSTOUFS(dvp->v_mount); 3069 LOCK_OWNED(ump); 3070 /* 3071 * Nothing to do if we have sufficient journal space. 3072 * If we currently hold the snapshot lock, we must avoid 3073 * handling other resources that could cause deadlock. 3074 */ 3075 if (journal_space(ump, 0) || (vp && IS_SNAPSHOT(VTOI(vp)))) 3076 return; 3077 stat_journal_low++; 3078 FREE_LOCK(ump); 3079 if (vp) 3080 ffs_syncvnode(vp, MNT_NOWAIT, 0); 3081 ffs_syncvnode(dvp, MNT_WAIT, 0); 3082 ACQUIRE_LOCK(ump); 3083 /* Process vp before dvp as it may create .. removes. */ 3084 if (vp) { 3085 process_removes(vp); 3086 process_truncates(vp); 3087 } 3088 process_removes(dvp); 3089 process_truncates(dvp); 3090 softdep_speedup(ump); 3091 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT); 3092 if (journal_space(ump, 0) == 0) { 3093 softdep_speedup(ump); 3094 if (journal_space(ump, 1) == 0) 3095 journal_suspend(ump); 3096 } 3097 } 3098 3099 static void 3100 jseg_write(ump, jseg, data) 3101 struct ufsmount *ump; 3102 struct jseg *jseg; 3103 uint8_t *data; 3104 { 3105 struct jsegrec *rec; 3106 3107 rec = (struct jsegrec *)data; 3108 rec->jsr_seq = jseg->js_seq; 3109 rec->jsr_oldest = jseg->js_oldseq; 3110 rec->jsr_cnt = jseg->js_cnt; 3111 rec->jsr_blocks = jseg->js_size / ump->um_devvp->v_bufobj.bo_bsize; 3112 rec->jsr_crc = 0; 3113 rec->jsr_time = ump->um_fs->fs_mtime; 3114 } 3115 3116 static inline void 3117 inoref_write(inoref, jseg, rec) 3118 struct inoref *inoref; 3119 struct jseg *jseg; 3120 struct jrefrec *rec; 3121 { 3122 3123 inoref->if_jsegdep->jd_seg = jseg; 3124 rec->jr_ino = inoref->if_ino; 3125 rec->jr_parent = inoref->if_parent; 3126 rec->jr_nlink = inoref->if_nlink; 3127 rec->jr_mode = inoref->if_mode; 3128 rec->jr_diroff = inoref->if_diroff; 3129 } 3130 3131 static void 3132 jaddref_write(jaddref, jseg, data) 3133 struct jaddref *jaddref; 3134 struct jseg *jseg; 3135 uint8_t *data; 3136 { 3137 struct jrefrec *rec; 3138 3139 rec = (struct jrefrec *)data; 3140 rec->jr_op = JOP_ADDREF; 3141 inoref_write(&jaddref->ja_ref, jseg, rec); 3142 } 3143 3144 static void 3145 jremref_write(jremref, jseg, data) 3146 struct jremref *jremref; 3147 struct jseg *jseg; 3148 uint8_t *data; 3149 { 3150 struct jrefrec *rec; 3151 3152 rec = (struct jrefrec *)data; 3153 rec->jr_op = JOP_REMREF; 3154 inoref_write(&jremref->jr_ref, jseg, rec); 3155 } 3156 3157 static void 3158 jmvref_write(jmvref, jseg, data) 3159 struct jmvref *jmvref; 3160 struct jseg *jseg; 3161 uint8_t *data; 3162 { 3163 struct jmvrec *rec; 3164 3165 rec = (struct jmvrec *)data; 3166 rec->jm_op = JOP_MVREF; 3167 rec->jm_ino = jmvref->jm_ino; 3168 rec->jm_parent = jmvref->jm_parent; 3169 rec->jm_oldoff = jmvref->jm_oldoff; 3170 rec->jm_newoff = jmvref->jm_newoff; 3171 } 3172 3173 static void 3174 jnewblk_write(jnewblk, jseg, data) 3175 struct jnewblk *jnewblk; 3176 struct jseg *jseg; 3177 uint8_t *data; 3178 { 3179 struct jblkrec *rec; 3180 3181 jnewblk->jn_jsegdep->jd_seg = jseg; 3182 rec = (struct jblkrec *)data; 3183 rec->jb_op = JOP_NEWBLK; 3184 rec->jb_ino = jnewblk->jn_ino; 3185 rec->jb_blkno = jnewblk->jn_blkno; 3186 rec->jb_lbn = jnewblk->jn_lbn; 3187 rec->jb_frags = jnewblk->jn_frags; 3188 rec->jb_oldfrags = jnewblk->jn_oldfrags; 3189 } 3190 3191 static void 3192 jfreeblk_write(jfreeblk, jseg, data) 3193 struct jfreeblk *jfreeblk; 3194 struct jseg *jseg; 3195 uint8_t *data; 3196 { 3197 struct jblkrec *rec; 3198 3199 jfreeblk->jf_dep.jb_jsegdep->jd_seg = jseg; 3200 rec = (struct jblkrec *)data; 3201 rec->jb_op = JOP_FREEBLK; 3202 rec->jb_ino = jfreeblk->jf_ino; 3203 rec->jb_blkno = jfreeblk->jf_blkno; 3204 rec->jb_lbn = jfreeblk->jf_lbn; 3205 rec->jb_frags = jfreeblk->jf_frags; 3206 rec->jb_oldfrags = 0; 3207 } 3208 3209 static void 3210 jfreefrag_write(jfreefrag, jseg, data) 3211 struct jfreefrag *jfreefrag; 3212 struct jseg *jseg; 3213 uint8_t *data; 3214 { 3215 struct jblkrec *rec; 3216 3217 jfreefrag->fr_jsegdep->jd_seg = jseg; 3218 rec = (struct jblkrec *)data; 3219 rec->jb_op = JOP_FREEBLK; 3220 rec->jb_ino = jfreefrag->fr_ino; 3221 rec->jb_blkno = jfreefrag->fr_blkno; 3222 rec->jb_lbn = jfreefrag->fr_lbn; 3223 rec->jb_frags = jfreefrag->fr_frags; 3224 rec->jb_oldfrags = 0; 3225 } 3226 3227 static void 3228 jtrunc_write(jtrunc, jseg, data) 3229 struct jtrunc *jtrunc; 3230 struct jseg *jseg; 3231 uint8_t *data; 3232 { 3233 struct jtrncrec *rec; 3234 3235 jtrunc->jt_dep.jb_jsegdep->jd_seg = jseg; 3236 rec = (struct jtrncrec *)data; 3237 rec->jt_op = JOP_TRUNC; 3238 rec->jt_ino = jtrunc->jt_ino; 3239 rec->jt_size = jtrunc->jt_size; 3240 rec->jt_extsize = jtrunc->jt_extsize; 3241 } 3242 3243 static void 3244 jfsync_write(jfsync, jseg, data) 3245 struct jfsync *jfsync; 3246 struct jseg *jseg; 3247 uint8_t *data; 3248 { 3249 struct jtrncrec *rec; 3250 3251 rec = (struct jtrncrec *)data; 3252 rec->jt_op = JOP_SYNC; 3253 rec->jt_ino = jfsync->jfs_ino; 3254 rec->jt_size = jfsync->jfs_size; 3255 rec->jt_extsize = jfsync->jfs_extsize; 3256 } 3257 3258 static void 3259 softdep_flushjournal(mp) 3260 struct mount *mp; 3261 { 3262 struct jblocks *jblocks; 3263 struct ufsmount *ump; 3264 3265 if (MOUNTEDSUJ(mp) == 0) 3266 return; 3267 ump = VFSTOUFS(mp); 3268 jblocks = ump->softdep_jblocks; 3269 ACQUIRE_LOCK(ump); 3270 while (ump->softdep_on_journal) { 3271 jblocks->jb_needseg = 1; 3272 softdep_process_journal(mp, NULL, MNT_WAIT); 3273 } 3274 FREE_LOCK(ump); 3275 } 3276 3277 static void softdep_synchronize_completed(struct bio *); 3278 static void softdep_synchronize(struct bio *, struct ufsmount *, void *); 3279 3280 static void 3281 softdep_synchronize_completed(bp) 3282 struct bio *bp; 3283 { 3284 struct jseg *oldest; 3285 struct jseg *jseg; 3286 struct ufsmount *ump; 3287 3288 /* 3289 * caller1 marks the last segment written before we issued the 3290 * synchronize cache. 3291 */ 3292 jseg = bp->bio_caller1; 3293 if (jseg == NULL) { 3294 g_destroy_bio(bp); 3295 return; 3296 } 3297 ump = VFSTOUFS(jseg->js_list.wk_mp); 3298 ACQUIRE_LOCK(ump); 3299 oldest = NULL; 3300 /* 3301 * Mark all the journal entries waiting on the synchronize cache 3302 * as completed so they may continue on. 3303 */ 3304 while (jseg != NULL && (jseg->js_state & COMPLETE) == 0) { 3305 jseg->js_state |= COMPLETE; 3306 oldest = jseg; 3307 jseg = TAILQ_PREV(jseg, jseglst, js_next); 3308 } 3309 /* 3310 * Restart deferred journal entry processing from the oldest 3311 * completed jseg. 3312 */ 3313 if (oldest) 3314 complete_jsegs(oldest); 3315 3316 FREE_LOCK(ump); 3317 g_destroy_bio(bp); 3318 } 3319 3320 /* 3321 * Send BIO_FLUSH/SYNCHRONIZE CACHE to the device to enforce write ordering 3322 * barriers. The journal must be written prior to any blocks that depend 3323 * on it and the journal can not be released until the blocks have be 3324 * written. This code handles both barriers simultaneously. 3325 */ 3326 static void 3327 softdep_synchronize(bp, ump, caller1) 3328 struct bio *bp; 3329 struct ufsmount *ump; 3330 void *caller1; 3331 { 3332 3333 bp->bio_cmd = BIO_FLUSH; 3334 bp->bio_flags |= BIO_ORDERED; 3335 bp->bio_data = NULL; 3336 bp->bio_offset = ump->um_cp->provider->mediasize; 3337 bp->bio_length = 0; 3338 bp->bio_done = softdep_synchronize_completed; 3339 bp->bio_caller1 = caller1; 3340 g_io_request(bp, 3341 (struct g_consumer *)ump->um_devvp->v_bufobj.bo_private); 3342 } 3343 3344 /* 3345 * Flush some journal records to disk. 3346 */ 3347 static void 3348 softdep_process_journal(mp, needwk, flags) 3349 struct mount *mp; 3350 struct worklist *needwk; 3351 int flags; 3352 { 3353 struct jblocks *jblocks; 3354 struct ufsmount *ump; 3355 struct worklist *wk; 3356 struct jseg *jseg; 3357 struct buf *bp; 3358 struct bio *bio; 3359 uint8_t *data; 3360 struct fs *fs; 3361 int shouldflush; 3362 int segwritten; 3363 int jrecmin; /* Minimum records per block. */ 3364 int jrecmax; /* Maximum records per block. */ 3365 int size; 3366 int cnt; 3367 int off; 3368 int devbsize; 3369 3370 if (MOUNTEDSUJ(mp) == 0) 3371 return; 3372 shouldflush = softdep_flushcache; 3373 bio = NULL; 3374 jseg = NULL; 3375 ump = VFSTOUFS(mp); 3376 LOCK_OWNED(ump); 3377 fs = ump->um_fs; 3378 jblocks = ump->softdep_jblocks; 3379 devbsize = ump->um_devvp->v_bufobj.bo_bsize; 3380 /* 3381 * We write anywhere between a disk block and fs block. The upper 3382 * bound is picked to prevent buffer cache fragmentation and limit 3383 * processing time per I/O. 3384 */ 3385 jrecmin = (devbsize / JREC_SIZE) - 1; /* -1 for seg header */ 3386 jrecmax = (fs->fs_bsize / devbsize) * jrecmin; 3387 segwritten = 0; 3388 for (;;) { 3389 cnt = ump->softdep_on_journal; 3390 /* 3391 * Criteria for writing a segment: 3392 * 1) We have a full block. 3393 * 2) We're called from jwait() and haven't found the 3394 * journal item yet. 3395 * 3) Always write if needseg is set. 3396 * 4) If we are called from process_worklist and have 3397 * not yet written anything we write a partial block 3398 * to enforce a 1 second maximum latency on journal 3399 * entries. 3400 */ 3401 if (cnt < (jrecmax - 1) && needwk == NULL && 3402 jblocks->jb_needseg == 0 && (segwritten || cnt == 0)) 3403 break; 3404 cnt++; 3405 /* 3406 * Verify some free journal space. softdep_prealloc() should 3407 * guarantee that we don't run out so this is indicative of 3408 * a problem with the flow control. Try to recover 3409 * gracefully in any event. 3410 */ 3411 while (jblocks->jb_free == 0) { 3412 if (flags != MNT_WAIT) 3413 break; 3414 printf("softdep: Out of journal space!\n"); 3415 softdep_speedup(ump); 3416 msleep(jblocks, LOCK_PTR(ump), PRIBIO, "jblocks", hz); 3417 } 3418 FREE_LOCK(ump); 3419 jseg = malloc(sizeof(*jseg), M_JSEG, M_SOFTDEP_FLAGS); 3420 workitem_alloc(&jseg->js_list, D_JSEG, mp); 3421 LIST_INIT(&jseg->js_entries); 3422 LIST_INIT(&jseg->js_indirs); 3423 jseg->js_state = ATTACHED; 3424 if (shouldflush == 0) 3425 jseg->js_state |= COMPLETE; 3426 else if (bio == NULL) 3427 bio = g_alloc_bio(); 3428 jseg->js_jblocks = jblocks; 3429 bp = geteblk(fs->fs_bsize, 0); 3430 ACQUIRE_LOCK(ump); 3431 /* 3432 * If there was a race while we were allocating the block 3433 * and jseg the entry we care about was likely written. 3434 * We bail out in both the WAIT and NOWAIT case and assume 3435 * the caller will loop if the entry it cares about is 3436 * not written. 3437 */ 3438 cnt = ump->softdep_on_journal; 3439 if (cnt + jblocks->jb_needseg == 0 || jblocks->jb_free == 0) { 3440 bp->b_flags |= B_INVAL | B_NOCACHE; 3441 WORKITEM_FREE(jseg, D_JSEG); 3442 FREE_LOCK(ump); 3443 brelse(bp); 3444 ACQUIRE_LOCK(ump); 3445 break; 3446 } 3447 /* 3448 * Calculate the disk block size required for the available 3449 * records rounded to the min size. 3450 */ 3451 if (cnt == 0) 3452 size = devbsize; 3453 else if (cnt < jrecmax) 3454 size = howmany(cnt, jrecmin) * devbsize; 3455 else 3456 size = fs->fs_bsize; 3457 /* 3458 * Allocate a disk block for this journal data and account 3459 * for truncation of the requested size if enough contiguous 3460 * space was not available. 3461 */ 3462 bp->b_blkno = jblocks_alloc(jblocks, size, &size); 3463 bp->b_lblkno = bp->b_blkno; 3464 bp->b_offset = bp->b_blkno * DEV_BSIZE; 3465 bp->b_bcount = size; 3466 bp->b_flags &= ~B_INVAL; 3467 bp->b_flags |= B_VALIDSUSPWRT | B_NOCOPY; 3468 /* 3469 * Initialize our jseg with cnt records. Assign the next 3470 * sequence number to it and link it in-order. 3471 */ 3472 cnt = MIN(cnt, (size / devbsize) * jrecmin); 3473 jseg->js_buf = bp; 3474 jseg->js_cnt = cnt; 3475 jseg->js_refs = cnt + 1; /* Self ref. */ 3476 jseg->js_size = size; 3477 jseg->js_seq = jblocks->jb_nextseq++; 3478 if (jblocks->jb_oldestseg == NULL) 3479 jblocks->jb_oldestseg = jseg; 3480 jseg->js_oldseq = jblocks->jb_oldestseg->js_seq; 3481 TAILQ_INSERT_TAIL(&jblocks->jb_segs, jseg, js_next); 3482 if (jblocks->jb_writeseg == NULL) 3483 jblocks->jb_writeseg = jseg; 3484 /* 3485 * Start filling in records from the pending list. 3486 */ 3487 data = bp->b_data; 3488 off = 0; 3489 3490 /* 3491 * Always put a header on the first block. 3492 * XXX As with below, there might not be a chance to get 3493 * into the loop. Ensure that something valid is written. 3494 */ 3495 jseg_write(ump, jseg, data); 3496 off += JREC_SIZE; 3497 data = bp->b_data + off; 3498 3499 /* 3500 * XXX Something is wrong here. There's no work to do, 3501 * but we need to perform and I/O and allow it to complete 3502 * anyways. 3503 */ 3504 if (LIST_EMPTY(&ump->softdep_journal_pending)) 3505 stat_emptyjblocks++; 3506 3507 while ((wk = LIST_FIRST(&ump->softdep_journal_pending)) 3508 != NULL) { 3509 if (cnt == 0) 3510 break; 3511 /* Place a segment header on every device block. */ 3512 if ((off % devbsize) == 0) { 3513 jseg_write(ump, jseg, data); 3514 off += JREC_SIZE; 3515 data = bp->b_data + off; 3516 } 3517 if (wk == needwk) 3518 needwk = NULL; 3519 remove_from_journal(wk); 3520 wk->wk_state |= INPROGRESS; 3521 WORKLIST_INSERT(&jseg->js_entries, wk); 3522 switch (wk->wk_type) { 3523 case D_JADDREF: 3524 jaddref_write(WK_JADDREF(wk), jseg, data); 3525 break; 3526 case D_JREMREF: 3527 jremref_write(WK_JREMREF(wk), jseg, data); 3528 break; 3529 case D_JMVREF: 3530 jmvref_write(WK_JMVREF(wk), jseg, data); 3531 break; 3532 case D_JNEWBLK: 3533 jnewblk_write(WK_JNEWBLK(wk), jseg, data); 3534 break; 3535 case D_JFREEBLK: 3536 jfreeblk_write(WK_JFREEBLK(wk), jseg, data); 3537 break; 3538 case D_JFREEFRAG: 3539 jfreefrag_write(WK_JFREEFRAG(wk), jseg, data); 3540 break; 3541 case D_JTRUNC: 3542 jtrunc_write(WK_JTRUNC(wk), jseg, data); 3543 break; 3544 case D_JFSYNC: 3545 jfsync_write(WK_JFSYNC(wk), jseg, data); 3546 break; 3547 default: 3548 panic("process_journal: Unknown type %s", 3549 TYPENAME(wk->wk_type)); 3550 /* NOTREACHED */ 3551 } 3552 off += JREC_SIZE; 3553 data = bp->b_data + off; 3554 cnt--; 3555 } 3556 3557 /* Clear any remaining space so we don't leak kernel data */ 3558 if (size > off) 3559 bzero(data, size - off); 3560 3561 /* 3562 * Write this one buffer and continue. 3563 */ 3564 segwritten = 1; 3565 jblocks->jb_needseg = 0; 3566 WORKLIST_INSERT(&bp->b_dep, &jseg->js_list); 3567 FREE_LOCK(ump); 3568 pbgetvp(ump->um_devvp, bp); 3569 /* 3570 * We only do the blocking wait once we find the journal 3571 * entry we're looking for. 3572 */ 3573 if (needwk == NULL && flags == MNT_WAIT) 3574 bwrite(bp); 3575 else 3576 bawrite(bp); 3577 ACQUIRE_LOCK(ump); 3578 } 3579 /* 3580 * If we wrote a segment issue a synchronize cache so the journal 3581 * is reflected on disk before the data is written. Since reclaiming 3582 * journal space also requires writing a journal record this 3583 * process also enforces a barrier before reclamation. 3584 */ 3585 if (segwritten && shouldflush) { 3586 softdep_synchronize(bio, ump, 3587 TAILQ_LAST(&jblocks->jb_segs, jseglst)); 3588 } else if (bio) 3589 g_destroy_bio(bio); 3590 /* 3591 * If we've suspended the filesystem because we ran out of journal 3592 * space either try to sync it here to make some progress or 3593 * unsuspend it if we already have. 3594 */ 3595 if (flags == 0 && jblocks->jb_suspended) { 3596 if (journal_unsuspend(ump)) 3597 return; 3598 FREE_LOCK(ump); 3599 VFS_SYNC(mp, MNT_NOWAIT); 3600 ffs_sbupdate(ump, MNT_WAIT, 0); 3601 ACQUIRE_LOCK(ump); 3602 } 3603 } 3604 3605 /* 3606 * Complete a jseg, allowing all dependencies awaiting journal writes 3607 * to proceed. Each journal dependency also attaches a jsegdep to dependent 3608 * structures so that the journal segment can be freed to reclaim space. 3609 */ 3610 static void 3611 complete_jseg(jseg) 3612 struct jseg *jseg; 3613 { 3614 struct worklist *wk; 3615 struct jmvref *jmvref; 3616 #ifdef INVARIANTS 3617 int i = 0; 3618 #endif 3619 3620 while ((wk = LIST_FIRST(&jseg->js_entries)) != NULL) { 3621 WORKLIST_REMOVE(wk); 3622 wk->wk_state &= ~INPROGRESS; 3623 wk->wk_state |= COMPLETE; 3624 KASSERT(i++ < jseg->js_cnt, 3625 ("handle_written_jseg: overflow %d >= %d", 3626 i - 1, jseg->js_cnt)); 3627 switch (wk->wk_type) { 3628 case D_JADDREF: 3629 handle_written_jaddref(WK_JADDREF(wk)); 3630 break; 3631 case D_JREMREF: 3632 handle_written_jremref(WK_JREMREF(wk)); 3633 break; 3634 case D_JMVREF: 3635 rele_jseg(jseg); /* No jsegdep. */ 3636 jmvref = WK_JMVREF(wk); 3637 LIST_REMOVE(jmvref, jm_deps); 3638 if ((jmvref->jm_pagedep->pd_state & ONWORKLIST) == 0) 3639 free_pagedep(jmvref->jm_pagedep); 3640 WORKITEM_FREE(jmvref, D_JMVREF); 3641 break; 3642 case D_JNEWBLK: 3643 handle_written_jnewblk(WK_JNEWBLK(wk)); 3644 break; 3645 case D_JFREEBLK: 3646 handle_written_jblkdep(&WK_JFREEBLK(wk)->jf_dep); 3647 break; 3648 case D_JTRUNC: 3649 handle_written_jblkdep(&WK_JTRUNC(wk)->jt_dep); 3650 break; 3651 case D_JFSYNC: 3652 rele_jseg(jseg); /* No jsegdep. */ 3653 WORKITEM_FREE(wk, D_JFSYNC); 3654 break; 3655 case D_JFREEFRAG: 3656 handle_written_jfreefrag(WK_JFREEFRAG(wk)); 3657 break; 3658 default: 3659 panic("handle_written_jseg: Unknown type %s", 3660 TYPENAME(wk->wk_type)); 3661 /* NOTREACHED */ 3662 } 3663 } 3664 /* Release the self reference so the structure may be freed. */ 3665 rele_jseg(jseg); 3666 } 3667 3668 /* 3669 * Determine which jsegs are ready for completion processing. Waits for 3670 * synchronize cache to complete as well as forcing in-order completion 3671 * of journal entries. 3672 */ 3673 static void 3674 complete_jsegs(jseg) 3675 struct jseg *jseg; 3676 { 3677 struct jblocks *jblocks; 3678 struct jseg *jsegn; 3679 3680 jblocks = jseg->js_jblocks; 3681 /* 3682 * Don't allow out of order completions. If this isn't the first 3683 * block wait for it to write before we're done. 3684 */ 3685 if (jseg != jblocks->jb_writeseg) 3686 return; 3687 /* Iterate through available jsegs processing their entries. */ 3688 while (jseg && (jseg->js_state & ALLCOMPLETE) == ALLCOMPLETE) { 3689 jblocks->jb_oldestwrseq = jseg->js_oldseq; 3690 jsegn = TAILQ_NEXT(jseg, js_next); 3691 complete_jseg(jseg); 3692 jseg = jsegn; 3693 } 3694 jblocks->jb_writeseg = jseg; 3695 /* 3696 * Attempt to free jsegs now that oldestwrseq may have advanced. 3697 */ 3698 free_jsegs(jblocks); 3699 } 3700 3701 /* 3702 * Mark a jseg as DEPCOMPLETE and throw away the buffer. Attempt to handle 3703 * the final completions. 3704 */ 3705 static void 3706 handle_written_jseg(jseg, bp) 3707 struct jseg *jseg; 3708 struct buf *bp; 3709 { 3710 3711 if (jseg->js_refs == 0) 3712 panic("handle_written_jseg: No self-reference on %p", jseg); 3713 jseg->js_state |= DEPCOMPLETE; 3714 /* 3715 * We'll never need this buffer again, set flags so it will be 3716 * discarded. 3717 */ 3718 bp->b_flags |= B_INVAL | B_NOCACHE; 3719 pbrelvp(bp); 3720 complete_jsegs(jseg); 3721 } 3722 3723 static inline struct jsegdep * 3724 inoref_jseg(inoref) 3725 struct inoref *inoref; 3726 { 3727 struct jsegdep *jsegdep; 3728 3729 jsegdep = inoref->if_jsegdep; 3730 inoref->if_jsegdep = NULL; 3731 3732 return (jsegdep); 3733 } 3734 3735 /* 3736 * Called once a jremref has made it to stable store. The jremref is marked 3737 * complete and we attempt to free it. Any pagedeps writes sleeping waiting 3738 * for the jremref to complete will be awoken by free_jremref. 3739 */ 3740 static void 3741 handle_written_jremref(jremref) 3742 struct jremref *jremref; 3743 { 3744 struct inodedep *inodedep; 3745 struct jsegdep *jsegdep; 3746 struct dirrem *dirrem; 3747 3748 /* Grab the jsegdep. */ 3749 jsegdep = inoref_jseg(&jremref->jr_ref); 3750 /* 3751 * Remove us from the inoref list. 3752 */ 3753 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 3754 0, &inodedep) == 0) 3755 panic("handle_written_jremref: Lost inodedep"); 3756 TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 3757 /* 3758 * Complete the dirrem. 3759 */ 3760 dirrem = jremref->jr_dirrem; 3761 jremref->jr_dirrem = NULL; 3762 LIST_REMOVE(jremref, jr_deps); 3763 jsegdep->jd_state |= jremref->jr_state & MKDIR_PARENT; 3764 jwork_insert(&dirrem->dm_jwork, jsegdep); 3765 if (LIST_EMPTY(&dirrem->dm_jremrefhd) && 3766 (dirrem->dm_state & COMPLETE) != 0) 3767 add_to_worklist(&dirrem->dm_list, 0); 3768 free_jremref(jremref); 3769 } 3770 3771 /* 3772 * Called once a jaddref has made it to stable store. The dependency is 3773 * marked complete and any dependent structures are added to the inode 3774 * bufwait list to be completed as soon as it is written. If a bitmap write 3775 * depends on this entry we move the inode into the inodedephd of the 3776 * bmsafemap dependency and attempt to remove the jaddref from the bmsafemap. 3777 */ 3778 static void 3779 handle_written_jaddref(jaddref) 3780 struct jaddref *jaddref; 3781 { 3782 struct jsegdep *jsegdep; 3783 struct inodedep *inodedep; 3784 struct diradd *diradd; 3785 struct mkdir *mkdir; 3786 3787 /* Grab the jsegdep. */ 3788 jsegdep = inoref_jseg(&jaddref->ja_ref); 3789 mkdir = NULL; 3790 diradd = NULL; 3791 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 3792 0, &inodedep) == 0) 3793 panic("handle_written_jaddref: Lost inodedep."); 3794 if (jaddref->ja_diradd == NULL) 3795 panic("handle_written_jaddref: No dependency"); 3796 if (jaddref->ja_diradd->da_list.wk_type == D_DIRADD) { 3797 diradd = jaddref->ja_diradd; 3798 WORKLIST_INSERT(&inodedep->id_bufwait, &diradd->da_list); 3799 } else if (jaddref->ja_state & MKDIR_PARENT) { 3800 mkdir = jaddref->ja_mkdir; 3801 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir->md_list); 3802 } else if (jaddref->ja_state & MKDIR_BODY) 3803 mkdir = jaddref->ja_mkdir; 3804 else 3805 panic("handle_written_jaddref: Unknown dependency %p", 3806 jaddref->ja_diradd); 3807 jaddref->ja_diradd = NULL; /* also clears ja_mkdir */ 3808 /* 3809 * Remove us from the inode list. 3810 */ 3811 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps); 3812 /* 3813 * The mkdir may be waiting on the jaddref to clear before freeing. 3814 */ 3815 if (mkdir) { 3816 KASSERT(mkdir->md_list.wk_type == D_MKDIR, 3817 ("handle_written_jaddref: Incorrect type for mkdir %s", 3818 TYPENAME(mkdir->md_list.wk_type))); 3819 mkdir->md_jaddref = NULL; 3820 diradd = mkdir->md_diradd; 3821 mkdir->md_state |= DEPCOMPLETE; 3822 complete_mkdir(mkdir); 3823 } 3824 jwork_insert(&diradd->da_jwork, jsegdep); 3825 if (jaddref->ja_state & NEWBLOCK) { 3826 inodedep->id_state |= ONDEPLIST; 3827 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_inodedephd, 3828 inodedep, id_deps); 3829 } 3830 free_jaddref(jaddref); 3831 } 3832 3833 /* 3834 * Called once a jnewblk journal is written. The allocdirect or allocindir 3835 * is placed in the bmsafemap to await notification of a written bitmap. If 3836 * the operation was canceled we add the segdep to the appropriate 3837 * dependency to free the journal space once the canceling operation 3838 * completes. 3839 */ 3840 static void 3841 handle_written_jnewblk(jnewblk) 3842 struct jnewblk *jnewblk; 3843 { 3844 struct bmsafemap *bmsafemap; 3845 struct freefrag *freefrag; 3846 struct freework *freework; 3847 struct jsegdep *jsegdep; 3848 struct newblk *newblk; 3849 3850 /* Grab the jsegdep. */ 3851 jsegdep = jnewblk->jn_jsegdep; 3852 jnewblk->jn_jsegdep = NULL; 3853 if (jnewblk->jn_dep == NULL) 3854 panic("handle_written_jnewblk: No dependency for the segdep."); 3855 switch (jnewblk->jn_dep->wk_type) { 3856 case D_NEWBLK: 3857 case D_ALLOCDIRECT: 3858 case D_ALLOCINDIR: 3859 /* 3860 * Add the written block to the bmsafemap so it can 3861 * be notified when the bitmap is on disk. 3862 */ 3863 newblk = WK_NEWBLK(jnewblk->jn_dep); 3864 newblk->nb_jnewblk = NULL; 3865 if ((newblk->nb_state & GOINGAWAY) == 0) { 3866 bmsafemap = newblk->nb_bmsafemap; 3867 newblk->nb_state |= ONDEPLIST; 3868 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, 3869 nb_deps); 3870 } 3871 jwork_insert(&newblk->nb_jwork, jsegdep); 3872 break; 3873 case D_FREEFRAG: 3874 /* 3875 * A newblock being removed by a freefrag when replaced by 3876 * frag extension. 3877 */ 3878 freefrag = WK_FREEFRAG(jnewblk->jn_dep); 3879 freefrag->ff_jdep = NULL; 3880 jwork_insert(&freefrag->ff_jwork, jsegdep); 3881 break; 3882 case D_FREEWORK: 3883 /* 3884 * A direct block was removed by truncate. 3885 */ 3886 freework = WK_FREEWORK(jnewblk->jn_dep); 3887 freework->fw_jnewblk = NULL; 3888 jwork_insert(&freework->fw_freeblks->fb_jwork, jsegdep); 3889 break; 3890 default: 3891 panic("handle_written_jnewblk: Unknown type %d.", 3892 jnewblk->jn_dep->wk_type); 3893 } 3894 jnewblk->jn_dep = NULL; 3895 free_jnewblk(jnewblk); 3896 } 3897 3898 /* 3899 * Cancel a jfreefrag that won't be needed, probably due to colliding with 3900 * an in-flight allocation that has not yet been committed. Divorce us 3901 * from the freefrag and mark it DEPCOMPLETE so that it may be added 3902 * to the worklist. 3903 */ 3904 static void 3905 cancel_jfreefrag(jfreefrag) 3906 struct jfreefrag *jfreefrag; 3907 { 3908 struct freefrag *freefrag; 3909 3910 if (jfreefrag->fr_jsegdep) { 3911 free_jsegdep(jfreefrag->fr_jsegdep); 3912 jfreefrag->fr_jsegdep = NULL; 3913 } 3914 freefrag = jfreefrag->fr_freefrag; 3915 jfreefrag->fr_freefrag = NULL; 3916 free_jfreefrag(jfreefrag); 3917 freefrag->ff_state |= DEPCOMPLETE; 3918 CTR1(KTR_SUJ, "cancel_jfreefrag: blkno %jd", freefrag->ff_blkno); 3919 } 3920 3921 /* 3922 * Free a jfreefrag when the parent freefrag is rendered obsolete. 3923 */ 3924 static void 3925 free_jfreefrag(jfreefrag) 3926 struct jfreefrag *jfreefrag; 3927 { 3928 3929 if (jfreefrag->fr_state & INPROGRESS) 3930 WORKLIST_REMOVE(&jfreefrag->fr_list); 3931 else if (jfreefrag->fr_state & ONWORKLIST) 3932 remove_from_journal(&jfreefrag->fr_list); 3933 if (jfreefrag->fr_freefrag != NULL) 3934 panic("free_jfreefrag: Still attached to a freefrag."); 3935 WORKITEM_FREE(jfreefrag, D_JFREEFRAG); 3936 } 3937 3938 /* 3939 * Called when the journal write for a jfreefrag completes. The parent 3940 * freefrag is added to the worklist if this completes its dependencies. 3941 */ 3942 static void 3943 handle_written_jfreefrag(jfreefrag) 3944 struct jfreefrag *jfreefrag; 3945 { 3946 struct jsegdep *jsegdep; 3947 struct freefrag *freefrag; 3948 3949 /* Grab the jsegdep. */ 3950 jsegdep = jfreefrag->fr_jsegdep; 3951 jfreefrag->fr_jsegdep = NULL; 3952 freefrag = jfreefrag->fr_freefrag; 3953 if (freefrag == NULL) 3954 panic("handle_written_jfreefrag: No freefrag."); 3955 freefrag->ff_state |= DEPCOMPLETE; 3956 freefrag->ff_jdep = NULL; 3957 jwork_insert(&freefrag->ff_jwork, jsegdep); 3958 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 3959 add_to_worklist(&freefrag->ff_list, 0); 3960 jfreefrag->fr_freefrag = NULL; 3961 free_jfreefrag(jfreefrag); 3962 } 3963 3964 /* 3965 * Called when the journal write for a jfreeblk completes. The jfreeblk 3966 * is removed from the freeblks list of pending journal writes and the 3967 * jsegdep is moved to the freeblks jwork to be completed when all blocks 3968 * have been reclaimed. 3969 */ 3970 static void 3971 handle_written_jblkdep(jblkdep) 3972 struct jblkdep *jblkdep; 3973 { 3974 struct freeblks *freeblks; 3975 struct jsegdep *jsegdep; 3976 3977 /* Grab the jsegdep. */ 3978 jsegdep = jblkdep->jb_jsegdep; 3979 jblkdep->jb_jsegdep = NULL; 3980 freeblks = jblkdep->jb_freeblks; 3981 LIST_REMOVE(jblkdep, jb_deps); 3982 jwork_insert(&freeblks->fb_jwork, jsegdep); 3983 /* 3984 * If the freeblks is all journaled, we can add it to the worklist. 3985 */ 3986 if (LIST_EMPTY(&freeblks->fb_jblkdephd) && 3987 (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 3988 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 3989 3990 free_jblkdep(jblkdep); 3991 } 3992 3993 static struct jsegdep * 3994 newjsegdep(struct worklist *wk) 3995 { 3996 struct jsegdep *jsegdep; 3997 3998 jsegdep = malloc(sizeof(*jsegdep), M_JSEGDEP, M_SOFTDEP_FLAGS); 3999 workitem_alloc(&jsegdep->jd_list, D_JSEGDEP, wk->wk_mp); 4000 jsegdep->jd_seg = NULL; 4001 4002 return (jsegdep); 4003 } 4004 4005 static struct jmvref * 4006 newjmvref(dp, ino, oldoff, newoff) 4007 struct inode *dp; 4008 ino_t ino; 4009 off_t oldoff; 4010 off_t newoff; 4011 { 4012 struct jmvref *jmvref; 4013 4014 jmvref = malloc(sizeof(*jmvref), M_JMVREF, M_SOFTDEP_FLAGS); 4015 workitem_alloc(&jmvref->jm_list, D_JMVREF, ITOVFS(dp)); 4016 jmvref->jm_list.wk_state = ATTACHED | DEPCOMPLETE; 4017 jmvref->jm_parent = dp->i_number; 4018 jmvref->jm_ino = ino; 4019 jmvref->jm_oldoff = oldoff; 4020 jmvref->jm_newoff = newoff; 4021 4022 return (jmvref); 4023 } 4024 4025 /* 4026 * Allocate a new jremref that tracks the removal of ip from dp with the 4027 * directory entry offset of diroff. Mark the entry as ATTACHED and 4028 * DEPCOMPLETE as we have all the information required for the journal write 4029 * and the directory has already been removed from the buffer. The caller 4030 * is responsible for linking the jremref into the pagedep and adding it 4031 * to the journal to write. The MKDIR_PARENT flag is set if we're doing 4032 * a DOTDOT addition so handle_workitem_remove() can properly assign 4033 * the jsegdep when we're done. 4034 */ 4035 static struct jremref * 4036 newjremref(struct dirrem *dirrem, struct inode *dp, struct inode *ip, 4037 off_t diroff, nlink_t nlink) 4038 { 4039 struct jremref *jremref; 4040 4041 jremref = malloc(sizeof(*jremref), M_JREMREF, M_SOFTDEP_FLAGS); 4042 workitem_alloc(&jremref->jr_list, D_JREMREF, ITOVFS(dp)); 4043 jremref->jr_state = ATTACHED; 4044 newinoref(&jremref->jr_ref, ip->i_number, dp->i_number, diroff, 4045 nlink, ip->i_mode); 4046 jremref->jr_dirrem = dirrem; 4047 4048 return (jremref); 4049 } 4050 4051 static inline void 4052 newinoref(struct inoref *inoref, ino_t ino, ino_t parent, off_t diroff, 4053 nlink_t nlink, uint16_t mode) 4054 { 4055 4056 inoref->if_jsegdep = newjsegdep(&inoref->if_list); 4057 inoref->if_diroff = diroff; 4058 inoref->if_ino = ino; 4059 inoref->if_parent = parent; 4060 inoref->if_nlink = nlink; 4061 inoref->if_mode = mode; 4062 } 4063 4064 /* 4065 * Allocate a new jaddref to track the addition of ino to dp at diroff. The 4066 * directory offset may not be known until later. The caller is responsible 4067 * adding the entry to the journal when this information is available. nlink 4068 * should be the link count prior to the addition and mode is only required 4069 * to have the correct FMT. 4070 */ 4071 static struct jaddref * 4072 newjaddref(struct inode *dp, ino_t ino, off_t diroff, int16_t nlink, 4073 uint16_t mode) 4074 { 4075 struct jaddref *jaddref; 4076 4077 jaddref = malloc(sizeof(*jaddref), M_JADDREF, M_SOFTDEP_FLAGS); 4078 workitem_alloc(&jaddref->ja_list, D_JADDREF, ITOVFS(dp)); 4079 jaddref->ja_state = ATTACHED; 4080 jaddref->ja_mkdir = NULL; 4081 newinoref(&jaddref->ja_ref, ino, dp->i_number, diroff, nlink, mode); 4082 4083 return (jaddref); 4084 } 4085 4086 /* 4087 * Create a new free dependency for a freework. The caller is responsible 4088 * for adjusting the reference count when it has the lock held. The freedep 4089 * will track an outstanding bitmap write that will ultimately clear the 4090 * freework to continue. 4091 */ 4092 static struct freedep * 4093 newfreedep(struct freework *freework) 4094 { 4095 struct freedep *freedep; 4096 4097 freedep = malloc(sizeof(*freedep), M_FREEDEP, M_SOFTDEP_FLAGS); 4098 workitem_alloc(&freedep->fd_list, D_FREEDEP, freework->fw_list.wk_mp); 4099 freedep->fd_freework = freework; 4100 4101 return (freedep); 4102 } 4103 4104 /* 4105 * Free a freedep structure once the buffer it is linked to is written. If 4106 * this is the last reference to the freework schedule it for completion. 4107 */ 4108 static void 4109 free_freedep(freedep) 4110 struct freedep *freedep; 4111 { 4112 struct freework *freework; 4113 4114 freework = freedep->fd_freework; 4115 freework->fw_freeblks->fb_cgwait--; 4116 if (--freework->fw_ref == 0) 4117 freework_enqueue(freework); 4118 WORKITEM_FREE(freedep, D_FREEDEP); 4119 } 4120 4121 /* 4122 * Allocate a new freework structure that may be a level in an indirect 4123 * when parent is not NULL or a top level block when it is. The top level 4124 * freework structures are allocated without the per-filesystem lock held 4125 * and before the freeblks is visible outside of softdep_setup_freeblocks(). 4126 */ 4127 static struct freework * 4128 newfreework(ump, freeblks, parent, lbn, nb, frags, off, journal) 4129 struct ufsmount *ump; 4130 struct freeblks *freeblks; 4131 struct freework *parent; 4132 ufs_lbn_t lbn; 4133 ufs2_daddr_t nb; 4134 int frags; 4135 int off; 4136 int journal; 4137 { 4138 struct freework *freework; 4139 4140 freework = malloc(sizeof(*freework), M_FREEWORK, M_SOFTDEP_FLAGS); 4141 workitem_alloc(&freework->fw_list, D_FREEWORK, freeblks->fb_list.wk_mp); 4142 freework->fw_state = ATTACHED; 4143 freework->fw_jnewblk = NULL; 4144 freework->fw_freeblks = freeblks; 4145 freework->fw_parent = parent; 4146 freework->fw_lbn = lbn; 4147 freework->fw_blkno = nb; 4148 freework->fw_frags = frags; 4149 freework->fw_indir = NULL; 4150 freework->fw_ref = (MOUNTEDSUJ(UFSTOVFS(ump)) == 0 || 4151 lbn >= -UFS_NXADDR) ? 0 : NINDIR(ump->um_fs) + 1; 4152 freework->fw_start = freework->fw_off = off; 4153 if (journal) 4154 newjfreeblk(freeblks, lbn, nb, frags); 4155 if (parent == NULL) { 4156 ACQUIRE_LOCK(ump); 4157 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 4158 freeblks->fb_ref++; 4159 FREE_LOCK(ump); 4160 } 4161 4162 return (freework); 4163 } 4164 4165 /* 4166 * Eliminate a jfreeblk for a block that does not need journaling. 4167 */ 4168 static void 4169 cancel_jfreeblk(freeblks, blkno) 4170 struct freeblks *freeblks; 4171 ufs2_daddr_t blkno; 4172 { 4173 struct jfreeblk *jfreeblk; 4174 struct jblkdep *jblkdep; 4175 4176 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) { 4177 if (jblkdep->jb_list.wk_type != D_JFREEBLK) 4178 continue; 4179 jfreeblk = WK_JFREEBLK(&jblkdep->jb_list); 4180 if (jfreeblk->jf_blkno == blkno) 4181 break; 4182 } 4183 if (jblkdep == NULL) 4184 return; 4185 CTR1(KTR_SUJ, "cancel_jfreeblk: blkno %jd", blkno); 4186 free_jsegdep(jblkdep->jb_jsegdep); 4187 LIST_REMOVE(jblkdep, jb_deps); 4188 WORKITEM_FREE(jfreeblk, D_JFREEBLK); 4189 } 4190 4191 /* 4192 * Allocate a new jfreeblk to journal top level block pointer when truncating 4193 * a file. The caller must add this to the worklist when the per-filesystem 4194 * lock is held. 4195 */ 4196 static struct jfreeblk * 4197 newjfreeblk(freeblks, lbn, blkno, frags) 4198 struct freeblks *freeblks; 4199 ufs_lbn_t lbn; 4200 ufs2_daddr_t blkno; 4201 int frags; 4202 { 4203 struct jfreeblk *jfreeblk; 4204 4205 jfreeblk = malloc(sizeof(*jfreeblk), M_JFREEBLK, M_SOFTDEP_FLAGS); 4206 workitem_alloc(&jfreeblk->jf_dep.jb_list, D_JFREEBLK, 4207 freeblks->fb_list.wk_mp); 4208 jfreeblk->jf_dep.jb_jsegdep = newjsegdep(&jfreeblk->jf_dep.jb_list); 4209 jfreeblk->jf_dep.jb_freeblks = freeblks; 4210 jfreeblk->jf_ino = freeblks->fb_inum; 4211 jfreeblk->jf_lbn = lbn; 4212 jfreeblk->jf_blkno = blkno; 4213 jfreeblk->jf_frags = frags; 4214 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jfreeblk->jf_dep, jb_deps); 4215 4216 return (jfreeblk); 4217 } 4218 4219 /* 4220 * The journal is only prepared to handle full-size block numbers, so we 4221 * have to adjust the record to reflect the change to a full-size block. 4222 * For example, suppose we have a block made up of fragments 8-15 and 4223 * want to free its last two fragments. We are given a request that says: 4224 * FREEBLK ino=5, blkno=14, lbn=0, frags=2, oldfrags=0 4225 * where frags are the number of fragments to free and oldfrags are the 4226 * number of fragments to keep. To block align it, we have to change it to 4227 * have a valid full-size blkno, so it becomes: 4228 * FREEBLK ino=5, blkno=8, lbn=0, frags=2, oldfrags=6 4229 */ 4230 static void 4231 adjust_newfreework(freeblks, frag_offset) 4232 struct freeblks *freeblks; 4233 int frag_offset; 4234 { 4235 struct jfreeblk *jfreeblk; 4236 4237 KASSERT((LIST_FIRST(&freeblks->fb_jblkdephd) != NULL && 4238 LIST_FIRST(&freeblks->fb_jblkdephd)->jb_list.wk_type == D_JFREEBLK), 4239 ("adjust_newfreework: Missing freeblks dependency")); 4240 4241 jfreeblk = WK_JFREEBLK(LIST_FIRST(&freeblks->fb_jblkdephd)); 4242 jfreeblk->jf_blkno -= frag_offset; 4243 jfreeblk->jf_frags += frag_offset; 4244 } 4245 4246 /* 4247 * Allocate a new jtrunc to track a partial truncation. 4248 */ 4249 static struct jtrunc * 4250 newjtrunc(freeblks, size, extsize) 4251 struct freeblks *freeblks; 4252 off_t size; 4253 int extsize; 4254 { 4255 struct jtrunc *jtrunc; 4256 4257 jtrunc = malloc(sizeof(*jtrunc), M_JTRUNC, M_SOFTDEP_FLAGS); 4258 workitem_alloc(&jtrunc->jt_dep.jb_list, D_JTRUNC, 4259 freeblks->fb_list.wk_mp); 4260 jtrunc->jt_dep.jb_jsegdep = newjsegdep(&jtrunc->jt_dep.jb_list); 4261 jtrunc->jt_dep.jb_freeblks = freeblks; 4262 jtrunc->jt_ino = freeblks->fb_inum; 4263 jtrunc->jt_size = size; 4264 jtrunc->jt_extsize = extsize; 4265 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jtrunc->jt_dep, jb_deps); 4266 4267 return (jtrunc); 4268 } 4269 4270 /* 4271 * If we're canceling a new bitmap we have to search for another ref 4272 * to move into the bmsafemap dep. This might be better expressed 4273 * with another structure. 4274 */ 4275 static void 4276 move_newblock_dep(jaddref, inodedep) 4277 struct jaddref *jaddref; 4278 struct inodedep *inodedep; 4279 { 4280 struct inoref *inoref; 4281 struct jaddref *jaddrefn; 4282 4283 jaddrefn = NULL; 4284 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4285 inoref = TAILQ_NEXT(inoref, if_deps)) { 4286 if ((jaddref->ja_state & NEWBLOCK) && 4287 inoref->if_list.wk_type == D_JADDREF) { 4288 jaddrefn = (struct jaddref *)inoref; 4289 break; 4290 } 4291 } 4292 if (jaddrefn == NULL) 4293 return; 4294 jaddrefn->ja_state &= ~(ATTACHED | UNDONE); 4295 jaddrefn->ja_state |= jaddref->ja_state & 4296 (ATTACHED | UNDONE | NEWBLOCK); 4297 jaddref->ja_state &= ~(ATTACHED | UNDONE | NEWBLOCK); 4298 jaddref->ja_state |= ATTACHED; 4299 LIST_REMOVE(jaddref, ja_bmdeps); 4300 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_jaddrefhd, jaddrefn, 4301 ja_bmdeps); 4302 } 4303 4304 /* 4305 * Cancel a jaddref either before it has been written or while it is being 4306 * written. This happens when a link is removed before the add reaches 4307 * the disk. The jaddref dependency is kept linked into the bmsafemap 4308 * and inode to prevent the link count or bitmap from reaching the disk 4309 * until handle_workitem_remove() re-adjusts the counts and bitmaps as 4310 * required. 4311 * 4312 * Returns 1 if the canceled addref requires journaling of the remove and 4313 * 0 otherwise. 4314 */ 4315 static int 4316 cancel_jaddref(jaddref, inodedep, wkhd) 4317 struct jaddref *jaddref; 4318 struct inodedep *inodedep; 4319 struct workhead *wkhd; 4320 { 4321 struct inoref *inoref; 4322 struct jsegdep *jsegdep; 4323 int needsj; 4324 4325 KASSERT((jaddref->ja_state & COMPLETE) == 0, 4326 ("cancel_jaddref: Canceling complete jaddref")); 4327 if (jaddref->ja_state & (INPROGRESS | COMPLETE)) 4328 needsj = 1; 4329 else 4330 needsj = 0; 4331 if (inodedep == NULL) 4332 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 4333 0, &inodedep) == 0) 4334 panic("cancel_jaddref: Lost inodedep"); 4335 /* 4336 * We must adjust the nlink of any reference operation that follows 4337 * us so that it is consistent with the in-memory reference. This 4338 * ensures that inode nlink rollbacks always have the correct link. 4339 */ 4340 if (needsj == 0) { 4341 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4342 inoref = TAILQ_NEXT(inoref, if_deps)) { 4343 if (inoref->if_state & GOINGAWAY) 4344 break; 4345 inoref->if_nlink--; 4346 } 4347 } 4348 jsegdep = inoref_jseg(&jaddref->ja_ref); 4349 if (jaddref->ja_state & NEWBLOCK) 4350 move_newblock_dep(jaddref, inodedep); 4351 wake_worklist(&jaddref->ja_list); 4352 jaddref->ja_mkdir = NULL; 4353 if (jaddref->ja_state & INPROGRESS) { 4354 jaddref->ja_state &= ~INPROGRESS; 4355 WORKLIST_REMOVE(&jaddref->ja_list); 4356 jwork_insert(wkhd, jsegdep); 4357 } else { 4358 free_jsegdep(jsegdep); 4359 if (jaddref->ja_state & DEPCOMPLETE) 4360 remove_from_journal(&jaddref->ja_list); 4361 } 4362 jaddref->ja_state |= (GOINGAWAY | DEPCOMPLETE); 4363 /* 4364 * Leave NEWBLOCK jaddrefs on the inodedep so handle_workitem_remove 4365 * can arrange for them to be freed with the bitmap. Otherwise we 4366 * no longer need this addref attached to the inoreflst and it 4367 * will incorrectly adjust nlink if we leave it. 4368 */ 4369 if ((jaddref->ja_state & NEWBLOCK) == 0) { 4370 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 4371 if_deps); 4372 jaddref->ja_state |= COMPLETE; 4373 free_jaddref(jaddref); 4374 return (needsj); 4375 } 4376 /* 4377 * Leave the head of the list for jsegdeps for fast merging. 4378 */ 4379 if (LIST_FIRST(wkhd) != NULL) { 4380 jaddref->ja_state |= ONWORKLIST; 4381 LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list); 4382 } else 4383 WORKLIST_INSERT(wkhd, &jaddref->ja_list); 4384 4385 return (needsj); 4386 } 4387 4388 /* 4389 * Attempt to free a jaddref structure when some work completes. This 4390 * should only succeed once the entry is written and all dependencies have 4391 * been notified. 4392 */ 4393 static void 4394 free_jaddref(jaddref) 4395 struct jaddref *jaddref; 4396 { 4397 4398 if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE) 4399 return; 4400 if (jaddref->ja_ref.if_jsegdep) 4401 panic("free_jaddref: segdep attached to jaddref %p(0x%X)\n", 4402 jaddref, jaddref->ja_state); 4403 if (jaddref->ja_state & NEWBLOCK) 4404 LIST_REMOVE(jaddref, ja_bmdeps); 4405 if (jaddref->ja_state & (INPROGRESS | ONWORKLIST)) 4406 panic("free_jaddref: Bad state %p(0x%X)", 4407 jaddref, jaddref->ja_state); 4408 if (jaddref->ja_mkdir != NULL) 4409 panic("free_jaddref: Work pending, 0x%X\n", jaddref->ja_state); 4410 WORKITEM_FREE(jaddref, D_JADDREF); 4411 } 4412 4413 /* 4414 * Free a jremref structure once it has been written or discarded. 4415 */ 4416 static void 4417 free_jremref(jremref) 4418 struct jremref *jremref; 4419 { 4420 4421 if (jremref->jr_ref.if_jsegdep) 4422 free_jsegdep(jremref->jr_ref.if_jsegdep); 4423 if (jremref->jr_state & INPROGRESS) 4424 panic("free_jremref: IO still pending"); 4425 WORKITEM_FREE(jremref, D_JREMREF); 4426 } 4427 4428 /* 4429 * Free a jnewblk structure. 4430 */ 4431 static void 4432 free_jnewblk(jnewblk) 4433 struct jnewblk *jnewblk; 4434 { 4435 4436 if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE) 4437 return; 4438 LIST_REMOVE(jnewblk, jn_deps); 4439 if (jnewblk->jn_dep != NULL) 4440 panic("free_jnewblk: Dependency still attached."); 4441 WORKITEM_FREE(jnewblk, D_JNEWBLK); 4442 } 4443 4444 /* 4445 * Cancel a jnewblk which has been been made redundant by frag extension. 4446 */ 4447 static void 4448 cancel_jnewblk(jnewblk, wkhd) 4449 struct jnewblk *jnewblk; 4450 struct workhead *wkhd; 4451 { 4452 struct jsegdep *jsegdep; 4453 4454 CTR1(KTR_SUJ, "cancel_jnewblk: blkno %jd", jnewblk->jn_blkno); 4455 jsegdep = jnewblk->jn_jsegdep; 4456 if (jnewblk->jn_jsegdep == NULL || jnewblk->jn_dep == NULL) 4457 panic("cancel_jnewblk: Invalid state"); 4458 jnewblk->jn_jsegdep = NULL; 4459 jnewblk->jn_dep = NULL; 4460 jnewblk->jn_state |= GOINGAWAY; 4461 if (jnewblk->jn_state & INPROGRESS) { 4462 jnewblk->jn_state &= ~INPROGRESS; 4463 WORKLIST_REMOVE(&jnewblk->jn_list); 4464 jwork_insert(wkhd, jsegdep); 4465 } else { 4466 free_jsegdep(jsegdep); 4467 remove_from_journal(&jnewblk->jn_list); 4468 } 4469 wake_worklist(&jnewblk->jn_list); 4470 WORKLIST_INSERT(wkhd, &jnewblk->jn_list); 4471 } 4472 4473 static void 4474 free_jblkdep(jblkdep) 4475 struct jblkdep *jblkdep; 4476 { 4477 4478 if (jblkdep->jb_list.wk_type == D_JFREEBLK) 4479 WORKITEM_FREE(jblkdep, D_JFREEBLK); 4480 else if (jblkdep->jb_list.wk_type == D_JTRUNC) 4481 WORKITEM_FREE(jblkdep, D_JTRUNC); 4482 else 4483 panic("free_jblkdep: Unexpected type %s", 4484 TYPENAME(jblkdep->jb_list.wk_type)); 4485 } 4486 4487 /* 4488 * Free a single jseg once it is no longer referenced in memory or on 4489 * disk. Reclaim journal blocks and dependencies waiting for the segment 4490 * to disappear. 4491 */ 4492 static void 4493 free_jseg(jseg, jblocks) 4494 struct jseg *jseg; 4495 struct jblocks *jblocks; 4496 { 4497 struct freework *freework; 4498 4499 /* 4500 * Free freework structures that were lingering to indicate freed 4501 * indirect blocks that forced journal write ordering on reallocate. 4502 */ 4503 while ((freework = LIST_FIRST(&jseg->js_indirs)) != NULL) 4504 indirblk_remove(freework); 4505 if (jblocks->jb_oldestseg == jseg) 4506 jblocks->jb_oldestseg = TAILQ_NEXT(jseg, js_next); 4507 TAILQ_REMOVE(&jblocks->jb_segs, jseg, js_next); 4508 jblocks_free(jblocks, jseg->js_list.wk_mp, jseg->js_size); 4509 KASSERT(LIST_EMPTY(&jseg->js_entries), 4510 ("free_jseg: Freed jseg has valid entries.")); 4511 WORKITEM_FREE(jseg, D_JSEG); 4512 } 4513 4514 /* 4515 * Free all jsegs that meet the criteria for being reclaimed and update 4516 * oldestseg. 4517 */ 4518 static void 4519 free_jsegs(jblocks) 4520 struct jblocks *jblocks; 4521 { 4522 struct jseg *jseg; 4523 4524 /* 4525 * Free only those jsegs which have none allocated before them to 4526 * preserve the journal space ordering. 4527 */ 4528 while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) { 4529 /* 4530 * Only reclaim space when nothing depends on this journal 4531 * set and another set has written that it is no longer 4532 * valid. 4533 */ 4534 if (jseg->js_refs != 0) { 4535 jblocks->jb_oldestseg = jseg; 4536 return; 4537 } 4538 if ((jseg->js_state & ALLCOMPLETE) != ALLCOMPLETE) 4539 break; 4540 if (jseg->js_seq > jblocks->jb_oldestwrseq) 4541 break; 4542 /* 4543 * We can free jsegs that didn't write entries when 4544 * oldestwrseq == js_seq. 4545 */ 4546 if (jseg->js_seq == jblocks->jb_oldestwrseq && 4547 jseg->js_cnt != 0) 4548 break; 4549 free_jseg(jseg, jblocks); 4550 } 4551 /* 4552 * If we exited the loop above we still must discover the 4553 * oldest valid segment. 4554 */ 4555 if (jseg) 4556 for (jseg = jblocks->jb_oldestseg; jseg != NULL; 4557 jseg = TAILQ_NEXT(jseg, js_next)) 4558 if (jseg->js_refs != 0) 4559 break; 4560 jblocks->jb_oldestseg = jseg; 4561 /* 4562 * The journal has no valid records but some jsegs may still be 4563 * waiting on oldestwrseq to advance. We force a small record 4564 * out to permit these lingering records to be reclaimed. 4565 */ 4566 if (jblocks->jb_oldestseg == NULL && !TAILQ_EMPTY(&jblocks->jb_segs)) 4567 jblocks->jb_needseg = 1; 4568 } 4569 4570 /* 4571 * Release one reference to a jseg and free it if the count reaches 0. This 4572 * should eventually reclaim journal space as well. 4573 */ 4574 static void 4575 rele_jseg(jseg) 4576 struct jseg *jseg; 4577 { 4578 4579 KASSERT(jseg->js_refs > 0, 4580 ("free_jseg: Invalid refcnt %d", jseg->js_refs)); 4581 if (--jseg->js_refs != 0) 4582 return; 4583 free_jsegs(jseg->js_jblocks); 4584 } 4585 4586 /* 4587 * Release a jsegdep and decrement the jseg count. 4588 */ 4589 static void 4590 free_jsegdep(jsegdep) 4591 struct jsegdep *jsegdep; 4592 { 4593 4594 if (jsegdep->jd_seg) 4595 rele_jseg(jsegdep->jd_seg); 4596 WORKITEM_FREE(jsegdep, D_JSEGDEP); 4597 } 4598 4599 /* 4600 * Wait for a journal item to make it to disk. Initiate journal processing 4601 * if required. 4602 */ 4603 static int 4604 jwait(wk, waitfor) 4605 struct worklist *wk; 4606 int waitfor; 4607 { 4608 4609 LOCK_OWNED(VFSTOUFS(wk->wk_mp)); 4610 /* 4611 * Blocking journal waits cause slow synchronous behavior. Record 4612 * stats on the frequency of these blocking operations. 4613 */ 4614 if (waitfor == MNT_WAIT) { 4615 stat_journal_wait++; 4616 switch (wk->wk_type) { 4617 case D_JREMREF: 4618 case D_JMVREF: 4619 stat_jwait_filepage++; 4620 break; 4621 case D_JTRUNC: 4622 case D_JFREEBLK: 4623 stat_jwait_freeblks++; 4624 break; 4625 case D_JNEWBLK: 4626 stat_jwait_newblk++; 4627 break; 4628 case D_JADDREF: 4629 stat_jwait_inode++; 4630 break; 4631 default: 4632 break; 4633 } 4634 } 4635 /* 4636 * If IO has not started we process the journal. We can't mark the 4637 * worklist item as IOWAITING because we drop the lock while 4638 * processing the journal and the worklist entry may be freed after 4639 * this point. The caller may call back in and re-issue the request. 4640 */ 4641 if ((wk->wk_state & INPROGRESS) == 0) { 4642 softdep_process_journal(wk->wk_mp, wk, waitfor); 4643 if (waitfor != MNT_WAIT) 4644 return (EBUSY); 4645 return (0); 4646 } 4647 if (waitfor != MNT_WAIT) 4648 return (EBUSY); 4649 wait_worklist(wk, "jwait"); 4650 return (0); 4651 } 4652 4653 /* 4654 * Lookup an inodedep based on an inode pointer and set the nlinkdelta as 4655 * appropriate. This is a convenience function to reduce duplicate code 4656 * for the setup and revert functions below. 4657 */ 4658 static struct inodedep * 4659 inodedep_lookup_ip(ip) 4660 struct inode *ip; 4661 { 4662 struct inodedep *inodedep; 4663 4664 KASSERT(ip->i_nlink >= ip->i_effnlink, 4665 ("inodedep_lookup_ip: bad delta")); 4666 (void) inodedep_lookup(ITOVFS(ip), ip->i_number, DEPALLOC, 4667 &inodedep); 4668 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 4669 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 4670 4671 return (inodedep); 4672 } 4673 4674 /* 4675 * Called prior to creating a new inode and linking it to a directory. The 4676 * jaddref structure must already be allocated by softdep_setup_inomapdep 4677 * and it is discovered here so we can initialize the mode and update 4678 * nlinkdelta. 4679 */ 4680 void 4681 softdep_setup_create(dp, ip) 4682 struct inode *dp; 4683 struct inode *ip; 4684 { 4685 struct inodedep *inodedep; 4686 struct jaddref *jaddref; 4687 struct vnode *dvp; 4688 4689 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4690 ("softdep_setup_create called on non-softdep filesystem")); 4691 KASSERT(ip->i_nlink == 1, 4692 ("softdep_setup_create: Invalid link count.")); 4693 dvp = ITOV(dp); 4694 ACQUIRE_LOCK(ITOUMP(dp)); 4695 inodedep = inodedep_lookup_ip(ip); 4696 if (DOINGSUJ(dvp)) { 4697 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4698 inoreflst); 4699 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 4700 ("softdep_setup_create: No addref structure present.")); 4701 } 4702 softdep_prelink(dvp, NULL); 4703 FREE_LOCK(ITOUMP(dp)); 4704 } 4705 4706 /* 4707 * Create a jaddref structure to track the addition of a DOTDOT link when 4708 * we are reparenting an inode as part of a rename. This jaddref will be 4709 * found by softdep_setup_directory_change. Adjusts nlinkdelta for 4710 * non-journaling softdep. 4711 */ 4712 void 4713 softdep_setup_dotdot_link(dp, ip) 4714 struct inode *dp; 4715 struct inode *ip; 4716 { 4717 struct inodedep *inodedep; 4718 struct jaddref *jaddref; 4719 struct vnode *dvp; 4720 4721 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4722 ("softdep_setup_dotdot_link called on non-softdep filesystem")); 4723 dvp = ITOV(dp); 4724 jaddref = NULL; 4725 /* 4726 * We don't set MKDIR_PARENT as this is not tied to a mkdir and 4727 * is used as a normal link would be. 4728 */ 4729 if (DOINGSUJ(dvp)) 4730 jaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 4731 dp->i_effnlink - 1, dp->i_mode); 4732 ACQUIRE_LOCK(ITOUMP(dp)); 4733 inodedep = inodedep_lookup_ip(dp); 4734 if (jaddref) 4735 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4736 if_deps); 4737 softdep_prelink(dvp, ITOV(ip)); 4738 FREE_LOCK(ITOUMP(dp)); 4739 } 4740 4741 /* 4742 * Create a jaddref structure to track a new link to an inode. The directory 4743 * offset is not known until softdep_setup_directory_add or 4744 * softdep_setup_directory_change. Adjusts nlinkdelta for non-journaling 4745 * softdep. 4746 */ 4747 void 4748 softdep_setup_link(dp, ip) 4749 struct inode *dp; 4750 struct inode *ip; 4751 { 4752 struct inodedep *inodedep; 4753 struct jaddref *jaddref; 4754 struct vnode *dvp; 4755 4756 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4757 ("softdep_setup_link called on non-softdep filesystem")); 4758 dvp = ITOV(dp); 4759 jaddref = NULL; 4760 if (DOINGSUJ(dvp)) 4761 jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1, 4762 ip->i_mode); 4763 ACQUIRE_LOCK(ITOUMP(dp)); 4764 inodedep = inodedep_lookup_ip(ip); 4765 if (jaddref) 4766 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4767 if_deps); 4768 softdep_prelink(dvp, ITOV(ip)); 4769 FREE_LOCK(ITOUMP(dp)); 4770 } 4771 4772 /* 4773 * Called to create the jaddref structures to track . and .. references as 4774 * well as lookup and further initialize the incomplete jaddref created 4775 * by softdep_setup_inomapdep when the inode was allocated. Adjusts 4776 * nlinkdelta for non-journaling softdep. 4777 */ 4778 void 4779 softdep_setup_mkdir(dp, ip) 4780 struct inode *dp; 4781 struct inode *ip; 4782 { 4783 struct inodedep *inodedep; 4784 struct jaddref *dotdotaddref; 4785 struct jaddref *dotaddref; 4786 struct jaddref *jaddref; 4787 struct vnode *dvp; 4788 4789 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4790 ("softdep_setup_mkdir called on non-softdep filesystem")); 4791 dvp = ITOV(dp); 4792 dotaddref = dotdotaddref = NULL; 4793 if (DOINGSUJ(dvp)) { 4794 dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1, 4795 ip->i_mode); 4796 dotaddref->ja_state |= MKDIR_BODY; 4797 dotdotaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 4798 dp->i_effnlink - 1, dp->i_mode); 4799 dotdotaddref->ja_state |= MKDIR_PARENT; 4800 } 4801 ACQUIRE_LOCK(ITOUMP(dp)); 4802 inodedep = inodedep_lookup_ip(ip); 4803 if (DOINGSUJ(dvp)) { 4804 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4805 inoreflst); 4806 KASSERT(jaddref != NULL, 4807 ("softdep_setup_mkdir: No addref structure present.")); 4808 KASSERT(jaddref->ja_parent == dp->i_number, 4809 ("softdep_setup_mkdir: bad parent %ju", 4810 (uintmax_t)jaddref->ja_parent)); 4811 TAILQ_INSERT_BEFORE(&jaddref->ja_ref, &dotaddref->ja_ref, 4812 if_deps); 4813 } 4814 inodedep = inodedep_lookup_ip(dp); 4815 if (DOINGSUJ(dvp)) 4816 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, 4817 &dotdotaddref->ja_ref, if_deps); 4818 softdep_prelink(ITOV(dp), NULL); 4819 FREE_LOCK(ITOUMP(dp)); 4820 } 4821 4822 /* 4823 * Called to track nlinkdelta of the inode and parent directories prior to 4824 * unlinking a directory. 4825 */ 4826 void 4827 softdep_setup_rmdir(dp, ip) 4828 struct inode *dp; 4829 struct inode *ip; 4830 { 4831 struct vnode *dvp; 4832 4833 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4834 ("softdep_setup_rmdir called on non-softdep filesystem")); 4835 dvp = ITOV(dp); 4836 ACQUIRE_LOCK(ITOUMP(dp)); 4837 (void) inodedep_lookup_ip(ip); 4838 (void) inodedep_lookup_ip(dp); 4839 softdep_prelink(dvp, ITOV(ip)); 4840 FREE_LOCK(ITOUMP(dp)); 4841 } 4842 4843 /* 4844 * Called to track nlinkdelta of the inode and parent directories prior to 4845 * unlink. 4846 */ 4847 void 4848 softdep_setup_unlink(dp, ip) 4849 struct inode *dp; 4850 struct inode *ip; 4851 { 4852 struct vnode *dvp; 4853 4854 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4855 ("softdep_setup_unlink called on non-softdep filesystem")); 4856 dvp = ITOV(dp); 4857 ACQUIRE_LOCK(ITOUMP(dp)); 4858 (void) inodedep_lookup_ip(ip); 4859 (void) inodedep_lookup_ip(dp); 4860 softdep_prelink(dvp, ITOV(ip)); 4861 FREE_LOCK(ITOUMP(dp)); 4862 } 4863 4864 /* 4865 * Called to release the journal structures created by a failed non-directory 4866 * creation. Adjusts nlinkdelta for non-journaling softdep. 4867 */ 4868 void 4869 softdep_revert_create(dp, ip) 4870 struct inode *dp; 4871 struct inode *ip; 4872 { 4873 struct inodedep *inodedep; 4874 struct jaddref *jaddref; 4875 struct vnode *dvp; 4876 4877 KASSERT(MOUNTEDSOFTDEP(ITOVFS((dp))) != 0, 4878 ("softdep_revert_create called on non-softdep filesystem")); 4879 dvp = ITOV(dp); 4880 ACQUIRE_LOCK(ITOUMP(dp)); 4881 inodedep = inodedep_lookup_ip(ip); 4882 if (DOINGSUJ(dvp)) { 4883 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4884 inoreflst); 4885 KASSERT(jaddref->ja_parent == dp->i_number, 4886 ("softdep_revert_create: addref parent mismatch")); 4887 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4888 } 4889 FREE_LOCK(ITOUMP(dp)); 4890 } 4891 4892 /* 4893 * Called to release the journal structures created by a failed link 4894 * addition. Adjusts nlinkdelta for non-journaling softdep. 4895 */ 4896 void 4897 softdep_revert_link(dp, ip) 4898 struct inode *dp; 4899 struct inode *ip; 4900 { 4901 struct inodedep *inodedep; 4902 struct jaddref *jaddref; 4903 struct vnode *dvp; 4904 4905 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4906 ("softdep_revert_link called on non-softdep filesystem")); 4907 dvp = ITOV(dp); 4908 ACQUIRE_LOCK(ITOUMP(dp)); 4909 inodedep = inodedep_lookup_ip(ip); 4910 if (DOINGSUJ(dvp)) { 4911 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4912 inoreflst); 4913 KASSERT(jaddref->ja_parent == dp->i_number, 4914 ("softdep_revert_link: addref parent mismatch")); 4915 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4916 } 4917 FREE_LOCK(ITOUMP(dp)); 4918 } 4919 4920 /* 4921 * Called to release the journal structures created by a failed mkdir 4922 * attempt. Adjusts nlinkdelta for non-journaling softdep. 4923 */ 4924 void 4925 softdep_revert_mkdir(dp, ip) 4926 struct inode *dp; 4927 struct inode *ip; 4928 { 4929 struct inodedep *inodedep; 4930 struct jaddref *jaddref; 4931 struct jaddref *dotaddref; 4932 struct vnode *dvp; 4933 4934 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4935 ("softdep_revert_mkdir called on non-softdep filesystem")); 4936 dvp = ITOV(dp); 4937 4938 ACQUIRE_LOCK(ITOUMP(dp)); 4939 inodedep = inodedep_lookup_ip(dp); 4940 if (DOINGSUJ(dvp)) { 4941 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4942 inoreflst); 4943 KASSERT(jaddref->ja_parent == ip->i_number, 4944 ("softdep_revert_mkdir: dotdot addref parent mismatch")); 4945 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4946 } 4947 inodedep = inodedep_lookup_ip(ip); 4948 if (DOINGSUJ(dvp)) { 4949 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4950 inoreflst); 4951 KASSERT(jaddref->ja_parent == dp->i_number, 4952 ("softdep_revert_mkdir: addref parent mismatch")); 4953 dotaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 4954 inoreflst, if_deps); 4955 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4956 KASSERT(dotaddref->ja_parent == ip->i_number, 4957 ("softdep_revert_mkdir: dot addref parent mismatch")); 4958 cancel_jaddref(dotaddref, inodedep, &inodedep->id_inowait); 4959 } 4960 FREE_LOCK(ITOUMP(dp)); 4961 } 4962 4963 /* 4964 * Called to correct nlinkdelta after a failed rmdir. 4965 */ 4966 void 4967 softdep_revert_rmdir(dp, ip) 4968 struct inode *dp; 4969 struct inode *ip; 4970 { 4971 4972 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4973 ("softdep_revert_rmdir called on non-softdep filesystem")); 4974 ACQUIRE_LOCK(ITOUMP(dp)); 4975 (void) inodedep_lookup_ip(ip); 4976 (void) inodedep_lookup_ip(dp); 4977 FREE_LOCK(ITOUMP(dp)); 4978 } 4979 4980 /* 4981 * Protecting the freemaps (or bitmaps). 4982 * 4983 * To eliminate the need to execute fsck before mounting a filesystem 4984 * after a power failure, one must (conservatively) guarantee that the 4985 * on-disk copy of the bitmaps never indicate that a live inode or block is 4986 * free. So, when a block or inode is allocated, the bitmap should be 4987 * updated (on disk) before any new pointers. When a block or inode is 4988 * freed, the bitmap should not be updated until all pointers have been 4989 * reset. The latter dependency is handled by the delayed de-allocation 4990 * approach described below for block and inode de-allocation. The former 4991 * dependency is handled by calling the following procedure when a block or 4992 * inode is allocated. When an inode is allocated an "inodedep" is created 4993 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk. 4994 * Each "inodedep" is also inserted into the hash indexing structure so 4995 * that any additional link additions can be made dependent on the inode 4996 * allocation. 4997 * 4998 * The ufs filesystem maintains a number of free block counts (e.g., per 4999 * cylinder group, per cylinder and per <cylinder, rotational position> pair) 5000 * in addition to the bitmaps. These counts are used to improve efficiency 5001 * during allocation and therefore must be consistent with the bitmaps. 5002 * There is no convenient way to guarantee post-crash consistency of these 5003 * counts with simple update ordering, for two main reasons: (1) The counts 5004 * and bitmaps for a single cylinder group block are not in the same disk 5005 * sector. If a disk write is interrupted (e.g., by power failure), one may 5006 * be written and the other not. (2) Some of the counts are located in the 5007 * superblock rather than the cylinder group block. So, we focus our soft 5008 * updates implementation on protecting the bitmaps. When mounting a 5009 * filesystem, we recompute the auxiliary counts from the bitmaps. 5010 */ 5011 5012 /* 5013 * Called just after updating the cylinder group block to allocate an inode. 5014 */ 5015 void 5016 softdep_setup_inomapdep(bp, ip, newinum, mode) 5017 struct buf *bp; /* buffer for cylgroup block with inode map */ 5018 struct inode *ip; /* inode related to allocation */ 5019 ino_t newinum; /* new inode number being allocated */ 5020 int mode; 5021 { 5022 struct inodedep *inodedep; 5023 struct bmsafemap *bmsafemap; 5024 struct jaddref *jaddref; 5025 struct mount *mp; 5026 struct fs *fs; 5027 5028 mp = ITOVFS(ip); 5029 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5030 ("softdep_setup_inomapdep called on non-softdep filesystem")); 5031 fs = VFSTOUFS(mp)->um_fs; 5032 jaddref = NULL; 5033 5034 /* 5035 * Allocate the journal reference add structure so that the bitmap 5036 * can be dependent on it. 5037 */ 5038 if (MOUNTEDSUJ(mp)) { 5039 jaddref = newjaddref(ip, newinum, 0, 0, mode); 5040 jaddref->ja_state |= NEWBLOCK; 5041 } 5042 5043 /* 5044 * Create a dependency for the newly allocated inode. 5045 * Panic if it already exists as something is seriously wrong. 5046 * Otherwise add it to the dependency list for the buffer holding 5047 * the cylinder group map from which it was allocated. 5048 * 5049 * We have to preallocate a bmsafemap entry in case it is needed 5050 * in bmsafemap_lookup since once we allocate the inodedep, we 5051 * have to finish initializing it before we can FREE_LOCK(). 5052 * By preallocating, we avoid FREE_LOCK() while doing a malloc 5053 * in bmsafemap_lookup. We cannot call bmsafemap_lookup before 5054 * creating the inodedep as it can be freed during the time 5055 * that we FREE_LOCK() while allocating the inodedep. We must 5056 * call workitem_alloc() before entering the locked section as 5057 * it also acquires the lock and we must avoid trying doing so 5058 * recursively. 5059 */ 5060 bmsafemap = malloc(sizeof(struct bmsafemap), 5061 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5062 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5063 ACQUIRE_LOCK(ITOUMP(ip)); 5064 if ((inodedep_lookup(mp, newinum, DEPALLOC, &inodedep))) 5065 panic("softdep_setup_inomapdep: dependency %p for new" 5066 "inode already exists", inodedep); 5067 bmsafemap = bmsafemap_lookup(mp, bp, ino_to_cg(fs, newinum), bmsafemap); 5068 if (jaddref) { 5069 LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps); 5070 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 5071 if_deps); 5072 } else { 5073 inodedep->id_state |= ONDEPLIST; 5074 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps); 5075 } 5076 inodedep->id_bmsafemap = bmsafemap; 5077 inodedep->id_state &= ~DEPCOMPLETE; 5078 FREE_LOCK(ITOUMP(ip)); 5079 } 5080 5081 /* 5082 * Called just after updating the cylinder group block to 5083 * allocate block or fragment. 5084 */ 5085 void 5086 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags) 5087 struct buf *bp; /* buffer for cylgroup block with block map */ 5088 struct mount *mp; /* filesystem doing allocation */ 5089 ufs2_daddr_t newblkno; /* number of newly allocated block */ 5090 int frags; /* Number of fragments. */ 5091 int oldfrags; /* Previous number of fragments for extend. */ 5092 { 5093 struct newblk *newblk; 5094 struct bmsafemap *bmsafemap; 5095 struct jnewblk *jnewblk; 5096 struct ufsmount *ump; 5097 struct fs *fs; 5098 5099 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5100 ("softdep_setup_blkmapdep called on non-softdep filesystem")); 5101 ump = VFSTOUFS(mp); 5102 fs = ump->um_fs; 5103 jnewblk = NULL; 5104 /* 5105 * Create a dependency for the newly allocated block. 5106 * Add it to the dependency list for the buffer holding 5107 * the cylinder group map from which it was allocated. 5108 */ 5109 if (MOUNTEDSUJ(mp)) { 5110 jnewblk = malloc(sizeof(*jnewblk), M_JNEWBLK, M_SOFTDEP_FLAGS); 5111 workitem_alloc(&jnewblk->jn_list, D_JNEWBLK, mp); 5112 jnewblk->jn_jsegdep = newjsegdep(&jnewblk->jn_list); 5113 jnewblk->jn_state = ATTACHED; 5114 jnewblk->jn_blkno = newblkno; 5115 jnewblk->jn_frags = frags; 5116 jnewblk->jn_oldfrags = oldfrags; 5117 #ifdef INVARIANTS 5118 { 5119 struct cg *cgp; 5120 uint8_t *blksfree; 5121 long bno; 5122 int i; 5123 5124 cgp = (struct cg *)bp->b_data; 5125 blksfree = cg_blksfree(cgp); 5126 bno = dtogd(fs, jnewblk->jn_blkno); 5127 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; 5128 i++) { 5129 if (isset(blksfree, bno + i)) 5130 panic("softdep_setup_blkmapdep: " 5131 "free fragment %d from %d-%d " 5132 "state 0x%X dep %p", i, 5133 jnewblk->jn_oldfrags, 5134 jnewblk->jn_frags, 5135 jnewblk->jn_state, 5136 jnewblk->jn_dep); 5137 } 5138 } 5139 #endif 5140 } 5141 5142 CTR3(KTR_SUJ, 5143 "softdep_setup_blkmapdep: blkno %jd frags %d oldfrags %d", 5144 newblkno, frags, oldfrags); 5145 ACQUIRE_LOCK(ump); 5146 if (newblk_lookup(mp, newblkno, DEPALLOC, &newblk) != 0) 5147 panic("softdep_setup_blkmapdep: found block"); 5148 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp, 5149 dtog(fs, newblkno), NULL); 5150 if (jnewblk) { 5151 jnewblk->jn_dep = (struct worklist *)newblk; 5152 LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps); 5153 } else { 5154 newblk->nb_state |= ONDEPLIST; 5155 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps); 5156 } 5157 newblk->nb_bmsafemap = bmsafemap; 5158 newblk->nb_jnewblk = jnewblk; 5159 FREE_LOCK(ump); 5160 } 5161 5162 #define BMSAFEMAP_HASH(ump, cg) \ 5163 (&(ump)->bmsafemap_hashtbl[(cg) & (ump)->bmsafemap_hash_size]) 5164 5165 static int 5166 bmsafemap_find(bmsafemaphd, cg, bmsafemapp) 5167 struct bmsafemap_hashhead *bmsafemaphd; 5168 int cg; 5169 struct bmsafemap **bmsafemapp; 5170 { 5171 struct bmsafemap *bmsafemap; 5172 5173 LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash) 5174 if (bmsafemap->sm_cg == cg) 5175 break; 5176 if (bmsafemap) { 5177 *bmsafemapp = bmsafemap; 5178 return (1); 5179 } 5180 *bmsafemapp = NULL; 5181 5182 return (0); 5183 } 5184 5185 /* 5186 * Find the bmsafemap associated with a cylinder group buffer. 5187 * If none exists, create one. The buffer must be locked when 5188 * this routine is called and this routine must be called with 5189 * the softdep lock held. To avoid giving up the lock while 5190 * allocating a new bmsafemap, a preallocated bmsafemap may be 5191 * provided. If it is provided but not needed, it is freed. 5192 */ 5193 static struct bmsafemap * 5194 bmsafemap_lookup(mp, bp, cg, newbmsafemap) 5195 struct mount *mp; 5196 struct buf *bp; 5197 int cg; 5198 struct bmsafemap *newbmsafemap; 5199 { 5200 struct bmsafemap_hashhead *bmsafemaphd; 5201 struct bmsafemap *bmsafemap, *collision; 5202 struct worklist *wk; 5203 struct ufsmount *ump; 5204 5205 ump = VFSTOUFS(mp); 5206 LOCK_OWNED(ump); 5207 KASSERT(bp != NULL, ("bmsafemap_lookup: missing buffer")); 5208 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 5209 if (wk->wk_type == D_BMSAFEMAP) { 5210 if (newbmsafemap) 5211 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5212 return (WK_BMSAFEMAP(wk)); 5213 } 5214 } 5215 bmsafemaphd = BMSAFEMAP_HASH(ump, cg); 5216 if (bmsafemap_find(bmsafemaphd, cg, &bmsafemap) == 1) { 5217 if (newbmsafemap) 5218 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5219 return (bmsafemap); 5220 } 5221 if (newbmsafemap) { 5222 bmsafemap = newbmsafemap; 5223 } else { 5224 FREE_LOCK(ump); 5225 bmsafemap = malloc(sizeof(struct bmsafemap), 5226 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5227 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5228 ACQUIRE_LOCK(ump); 5229 } 5230 bmsafemap->sm_buf = bp; 5231 LIST_INIT(&bmsafemap->sm_inodedephd); 5232 LIST_INIT(&bmsafemap->sm_inodedepwr); 5233 LIST_INIT(&bmsafemap->sm_newblkhd); 5234 LIST_INIT(&bmsafemap->sm_newblkwr); 5235 LIST_INIT(&bmsafemap->sm_jaddrefhd); 5236 LIST_INIT(&bmsafemap->sm_jnewblkhd); 5237 LIST_INIT(&bmsafemap->sm_freehd); 5238 LIST_INIT(&bmsafemap->sm_freewr); 5239 if (bmsafemap_find(bmsafemaphd, cg, &collision) == 1) { 5240 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 5241 return (collision); 5242 } 5243 bmsafemap->sm_cg = cg; 5244 LIST_INSERT_HEAD(bmsafemaphd, bmsafemap, sm_hash); 5245 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 5246 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list); 5247 return (bmsafemap); 5248 } 5249 5250 /* 5251 * Direct block allocation dependencies. 5252 * 5253 * When a new block is allocated, the corresponding disk locations must be 5254 * initialized (with zeros or new data) before the on-disk inode points to 5255 * them. Also, the freemap from which the block was allocated must be 5256 * updated (on disk) before the inode's pointer. These two dependencies are 5257 * independent of each other and are needed for all file blocks and indirect 5258 * blocks that are pointed to directly by the inode. Just before the 5259 * "in-core" version of the inode is updated with a newly allocated block 5260 * number, a procedure (below) is called to setup allocation dependency 5261 * structures. These structures are removed when the corresponding 5262 * dependencies are satisfied or when the block allocation becomes obsolete 5263 * (i.e., the file is deleted, the block is de-allocated, or the block is a 5264 * fragment that gets upgraded). All of these cases are handled in 5265 * procedures described later. 5266 * 5267 * When a file extension causes a fragment to be upgraded, either to a larger 5268 * fragment or to a full block, the on-disk location may change (if the 5269 * previous fragment could not simply be extended). In this case, the old 5270 * fragment must be de-allocated, but not until after the inode's pointer has 5271 * been updated. In most cases, this is handled by later procedures, which 5272 * will construct a "freefrag" structure to be added to the workitem queue 5273 * when the inode update is complete (or obsolete). The main exception to 5274 * this is when an allocation occurs while a pending allocation dependency 5275 * (for the same block pointer) remains. This case is handled in the main 5276 * allocation dependency setup procedure by immediately freeing the 5277 * unreferenced fragments. 5278 */ 5279 void 5280 softdep_setup_allocdirect(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 5281 struct inode *ip; /* inode to which block is being added */ 5282 ufs_lbn_t off; /* block pointer within inode */ 5283 ufs2_daddr_t newblkno; /* disk block number being added */ 5284 ufs2_daddr_t oldblkno; /* previous block number, 0 unless frag */ 5285 long newsize; /* size of new block */ 5286 long oldsize; /* size of new block */ 5287 struct buf *bp; /* bp for allocated block */ 5288 { 5289 struct allocdirect *adp, *oldadp; 5290 struct allocdirectlst *adphead; 5291 struct freefrag *freefrag; 5292 struct inodedep *inodedep; 5293 struct pagedep *pagedep; 5294 struct jnewblk *jnewblk; 5295 struct newblk *newblk; 5296 struct mount *mp; 5297 ufs_lbn_t lbn; 5298 5299 lbn = bp->b_lblkno; 5300 mp = ITOVFS(ip); 5301 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5302 ("softdep_setup_allocdirect called on non-softdep filesystem")); 5303 if (oldblkno && oldblkno != newblkno) 5304 /* 5305 * The usual case is that a smaller fragment that 5306 * was just allocated has been replaced with a bigger 5307 * fragment or a full-size block. If it is marked as 5308 * B_DELWRI, the current contents have not been written 5309 * to disk. It is possible that the block was written 5310 * earlier, but very uncommon. If the block has never 5311 * been written, there is no need to send a BIO_DELETE 5312 * for it when it is freed. The gain from avoiding the 5313 * TRIMs for the common case of unwritten blocks far 5314 * exceeds the cost of the write amplification for the 5315 * uncommon case of failing to send a TRIM for a block 5316 * that had been written. 5317 */ 5318 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn, 5319 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY); 5320 else 5321 freefrag = NULL; 5322 5323 CTR6(KTR_SUJ, 5324 "softdep_setup_allocdirect: ino %d blkno %jd oldblkno %jd " 5325 "off %jd newsize %ld oldsize %d", 5326 ip->i_number, newblkno, oldblkno, off, newsize, oldsize); 5327 ACQUIRE_LOCK(ITOUMP(ip)); 5328 if (off >= UFS_NDADDR) { 5329 if (lbn > 0) 5330 panic("softdep_setup_allocdirect: bad lbn %jd, off %jd", 5331 lbn, off); 5332 /* allocating an indirect block */ 5333 if (oldblkno != 0) 5334 panic("softdep_setup_allocdirect: non-zero indir"); 5335 } else { 5336 if (off != lbn) 5337 panic("softdep_setup_allocdirect: lbn %jd != off %jd", 5338 lbn, off); 5339 /* 5340 * Allocating a direct block. 5341 * 5342 * If we are allocating a directory block, then we must 5343 * allocate an associated pagedep to track additions and 5344 * deletions. 5345 */ 5346 if ((ip->i_mode & IFMT) == IFDIR) 5347 pagedep_lookup(mp, bp, ip->i_number, off, DEPALLOC, 5348 &pagedep); 5349 } 5350 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5351 panic("softdep_setup_allocdirect: lost block"); 5352 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5353 ("softdep_setup_allocdirect: newblk already initialized")); 5354 /* 5355 * Convert the newblk to an allocdirect. 5356 */ 5357 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT); 5358 adp = (struct allocdirect *)newblk; 5359 newblk->nb_freefrag = freefrag; 5360 adp->ad_offset = off; 5361 adp->ad_oldblkno = oldblkno; 5362 adp->ad_newsize = newsize; 5363 adp->ad_oldsize = oldsize; 5364 5365 /* 5366 * Finish initializing the journal. 5367 */ 5368 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5369 jnewblk->jn_ino = ip->i_number; 5370 jnewblk->jn_lbn = lbn; 5371 add_to_journal(&jnewblk->jn_list); 5372 } 5373 if (freefrag && freefrag->ff_jdep != NULL && 5374 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5375 add_to_journal(freefrag->ff_jdep); 5376 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 5377 adp->ad_inodedep = inodedep; 5378 5379 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5380 /* 5381 * The list of allocdirects must be kept in sorted and ascending 5382 * order so that the rollback routines can quickly determine the 5383 * first uncommitted block (the size of the file stored on disk 5384 * ends at the end of the lowest committed fragment, or if there 5385 * are no fragments, at the end of the highest committed block). 5386 * Since files generally grow, the typical case is that the new 5387 * block is to be added at the end of the list. We speed this 5388 * special case by checking against the last allocdirect in the 5389 * list before laboriously traversing the list looking for the 5390 * insertion point. 5391 */ 5392 adphead = &inodedep->id_newinoupdt; 5393 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5394 if (oldadp == NULL || oldadp->ad_offset <= off) { 5395 /* insert at end of list */ 5396 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5397 if (oldadp != NULL && oldadp->ad_offset == off) 5398 allocdirect_merge(adphead, adp, oldadp); 5399 FREE_LOCK(ITOUMP(ip)); 5400 return; 5401 } 5402 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5403 if (oldadp->ad_offset >= off) 5404 break; 5405 } 5406 if (oldadp == NULL) 5407 panic("softdep_setup_allocdirect: lost entry"); 5408 /* insert in middle of list */ 5409 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5410 if (oldadp->ad_offset == off) 5411 allocdirect_merge(adphead, adp, oldadp); 5412 5413 FREE_LOCK(ITOUMP(ip)); 5414 } 5415 5416 /* 5417 * Merge a newer and older journal record to be stored either in a 5418 * newblock or freefrag. This handles aggregating journal records for 5419 * fragment allocation into a second record as well as replacing a 5420 * journal free with an aborted journal allocation. A segment for the 5421 * oldest record will be placed on wkhd if it has been written. If not 5422 * the segment for the newer record will suffice. 5423 */ 5424 static struct worklist * 5425 jnewblk_merge(new, old, wkhd) 5426 struct worklist *new; 5427 struct worklist *old; 5428 struct workhead *wkhd; 5429 { 5430 struct jnewblk *njnewblk; 5431 struct jnewblk *jnewblk; 5432 5433 /* Handle NULLs to simplify callers. */ 5434 if (new == NULL) 5435 return (old); 5436 if (old == NULL) 5437 return (new); 5438 /* Replace a jfreefrag with a jnewblk. */ 5439 if (new->wk_type == D_JFREEFRAG) { 5440 if (WK_JNEWBLK(old)->jn_blkno != WK_JFREEFRAG(new)->fr_blkno) 5441 panic("jnewblk_merge: blkno mismatch: %p, %p", 5442 old, new); 5443 cancel_jfreefrag(WK_JFREEFRAG(new)); 5444 return (old); 5445 } 5446 if (old->wk_type != D_JNEWBLK || new->wk_type != D_JNEWBLK) 5447 panic("jnewblk_merge: Bad type: old %d new %d\n", 5448 old->wk_type, new->wk_type); 5449 /* 5450 * Handle merging of two jnewblk records that describe 5451 * different sets of fragments in the same block. 5452 */ 5453 jnewblk = WK_JNEWBLK(old); 5454 njnewblk = WK_JNEWBLK(new); 5455 if (jnewblk->jn_blkno != njnewblk->jn_blkno) 5456 panic("jnewblk_merge: Merging disparate blocks."); 5457 /* 5458 * The record may be rolled back in the cg. 5459 */ 5460 if (jnewblk->jn_state & UNDONE) { 5461 jnewblk->jn_state &= ~UNDONE; 5462 njnewblk->jn_state |= UNDONE; 5463 njnewblk->jn_state &= ~ATTACHED; 5464 } 5465 /* 5466 * We modify the newer addref and free the older so that if neither 5467 * has been written the most up-to-date copy will be on disk. If 5468 * both have been written but rolled back we only temporarily need 5469 * one of them to fix the bits when the cg write completes. 5470 */ 5471 jnewblk->jn_state |= ATTACHED | COMPLETE; 5472 njnewblk->jn_oldfrags = jnewblk->jn_oldfrags; 5473 cancel_jnewblk(jnewblk, wkhd); 5474 WORKLIST_REMOVE(&jnewblk->jn_list); 5475 free_jnewblk(jnewblk); 5476 return (new); 5477 } 5478 5479 /* 5480 * Replace an old allocdirect dependency with a newer one. 5481 * This routine must be called with splbio interrupts blocked. 5482 */ 5483 static void 5484 allocdirect_merge(adphead, newadp, oldadp) 5485 struct allocdirectlst *adphead; /* head of list holding allocdirects */ 5486 struct allocdirect *newadp; /* allocdirect being added */ 5487 struct allocdirect *oldadp; /* existing allocdirect being checked */ 5488 { 5489 struct worklist *wk; 5490 struct freefrag *freefrag; 5491 5492 freefrag = NULL; 5493 LOCK_OWNED(VFSTOUFS(newadp->ad_list.wk_mp)); 5494 if (newadp->ad_oldblkno != oldadp->ad_newblkno || 5495 newadp->ad_oldsize != oldadp->ad_newsize || 5496 newadp->ad_offset >= UFS_NDADDR) 5497 panic("%s %jd != new %jd || old size %ld != new %ld", 5498 "allocdirect_merge: old blkno", 5499 (intmax_t)newadp->ad_oldblkno, 5500 (intmax_t)oldadp->ad_newblkno, 5501 newadp->ad_oldsize, oldadp->ad_newsize); 5502 newadp->ad_oldblkno = oldadp->ad_oldblkno; 5503 newadp->ad_oldsize = oldadp->ad_oldsize; 5504 /* 5505 * If the old dependency had a fragment to free or had never 5506 * previously had a block allocated, then the new dependency 5507 * can immediately post its freefrag and adopt the old freefrag. 5508 * This action is done by swapping the freefrag dependencies. 5509 * The new dependency gains the old one's freefrag, and the 5510 * old one gets the new one and then immediately puts it on 5511 * the worklist when it is freed by free_newblk. It is 5512 * not possible to do this swap when the old dependency had a 5513 * non-zero size but no previous fragment to free. This condition 5514 * arises when the new block is an extension of the old block. 5515 * Here, the first part of the fragment allocated to the new 5516 * dependency is part of the block currently claimed on disk by 5517 * the old dependency, so cannot legitimately be freed until the 5518 * conditions for the new dependency are fulfilled. 5519 */ 5520 freefrag = newadp->ad_freefrag; 5521 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) { 5522 newadp->ad_freefrag = oldadp->ad_freefrag; 5523 oldadp->ad_freefrag = freefrag; 5524 } 5525 /* 5526 * If we are tracking a new directory-block allocation, 5527 * move it from the old allocdirect to the new allocdirect. 5528 */ 5529 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) { 5530 WORKLIST_REMOVE(wk); 5531 if (!LIST_EMPTY(&oldadp->ad_newdirblk)) 5532 panic("allocdirect_merge: extra newdirblk"); 5533 WORKLIST_INSERT(&newadp->ad_newdirblk, wk); 5534 } 5535 TAILQ_REMOVE(adphead, oldadp, ad_next); 5536 /* 5537 * We need to move any journal dependencies over to the freefrag 5538 * that releases this block if it exists. Otherwise we are 5539 * extending an existing block and we'll wait until that is 5540 * complete to release the journal space and extend the 5541 * new journal to cover this old space as well. 5542 */ 5543 if (freefrag == NULL) { 5544 if (oldadp->ad_newblkno != newadp->ad_newblkno) 5545 panic("allocdirect_merge: %jd != %jd", 5546 oldadp->ad_newblkno, newadp->ad_newblkno); 5547 newadp->ad_block.nb_jnewblk = (struct jnewblk *) 5548 jnewblk_merge(&newadp->ad_block.nb_jnewblk->jn_list, 5549 &oldadp->ad_block.nb_jnewblk->jn_list, 5550 &newadp->ad_block.nb_jwork); 5551 oldadp->ad_block.nb_jnewblk = NULL; 5552 cancel_newblk(&oldadp->ad_block, NULL, 5553 &newadp->ad_block.nb_jwork); 5554 } else { 5555 wk = (struct worklist *) cancel_newblk(&oldadp->ad_block, 5556 &freefrag->ff_list, &freefrag->ff_jwork); 5557 freefrag->ff_jdep = jnewblk_merge(freefrag->ff_jdep, wk, 5558 &freefrag->ff_jwork); 5559 } 5560 free_newblk(&oldadp->ad_block); 5561 } 5562 5563 /* 5564 * Allocate a jfreefrag structure to journal a single block free. 5565 */ 5566 static struct jfreefrag * 5567 newjfreefrag(freefrag, ip, blkno, size, lbn) 5568 struct freefrag *freefrag; 5569 struct inode *ip; 5570 ufs2_daddr_t blkno; 5571 long size; 5572 ufs_lbn_t lbn; 5573 { 5574 struct jfreefrag *jfreefrag; 5575 struct fs *fs; 5576 5577 fs = ITOFS(ip); 5578 jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG, 5579 M_SOFTDEP_FLAGS); 5580 workitem_alloc(&jfreefrag->fr_list, D_JFREEFRAG, ITOVFS(ip)); 5581 jfreefrag->fr_jsegdep = newjsegdep(&jfreefrag->fr_list); 5582 jfreefrag->fr_state = ATTACHED | DEPCOMPLETE; 5583 jfreefrag->fr_ino = ip->i_number; 5584 jfreefrag->fr_lbn = lbn; 5585 jfreefrag->fr_blkno = blkno; 5586 jfreefrag->fr_frags = numfrags(fs, size); 5587 jfreefrag->fr_freefrag = freefrag; 5588 5589 return (jfreefrag); 5590 } 5591 5592 /* 5593 * Allocate a new freefrag structure. 5594 */ 5595 static struct freefrag * 5596 newfreefrag(ip, blkno, size, lbn, key) 5597 struct inode *ip; 5598 ufs2_daddr_t blkno; 5599 long size; 5600 ufs_lbn_t lbn; 5601 u_long key; 5602 { 5603 struct freefrag *freefrag; 5604 struct ufsmount *ump; 5605 struct fs *fs; 5606 5607 CTR4(KTR_SUJ, "newfreefrag: ino %d blkno %jd size %ld lbn %jd", 5608 ip->i_number, blkno, size, lbn); 5609 ump = ITOUMP(ip); 5610 fs = ump->um_fs; 5611 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag) 5612 panic("newfreefrag: frag size"); 5613 freefrag = malloc(sizeof(struct freefrag), 5614 M_FREEFRAG, M_SOFTDEP_FLAGS); 5615 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ump)); 5616 freefrag->ff_state = ATTACHED; 5617 LIST_INIT(&freefrag->ff_jwork); 5618 freefrag->ff_inum = ip->i_number; 5619 freefrag->ff_vtype = ITOV(ip)->v_type; 5620 freefrag->ff_blkno = blkno; 5621 freefrag->ff_fragsize = size; 5622 freefrag->ff_key = key; 5623 5624 if (MOUNTEDSUJ(UFSTOVFS(ump))) { 5625 freefrag->ff_jdep = (struct worklist *) 5626 newjfreefrag(freefrag, ip, blkno, size, lbn); 5627 } else { 5628 freefrag->ff_state |= DEPCOMPLETE; 5629 freefrag->ff_jdep = NULL; 5630 } 5631 5632 return (freefrag); 5633 } 5634 5635 /* 5636 * This workitem de-allocates fragments that were replaced during 5637 * file block allocation. 5638 */ 5639 static void 5640 handle_workitem_freefrag(freefrag) 5641 struct freefrag *freefrag; 5642 { 5643 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp); 5644 struct workhead wkhd; 5645 5646 CTR3(KTR_SUJ, 5647 "handle_workitem_freefrag: ino %d blkno %jd size %ld", 5648 freefrag->ff_inum, freefrag->ff_blkno, freefrag->ff_fragsize); 5649 /* 5650 * It would be illegal to add new completion items to the 5651 * freefrag after it was schedule to be done so it must be 5652 * safe to modify the list head here. 5653 */ 5654 LIST_INIT(&wkhd); 5655 ACQUIRE_LOCK(ump); 5656 LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list); 5657 /* 5658 * If the journal has not been written we must cancel it here. 5659 */ 5660 if (freefrag->ff_jdep) { 5661 if (freefrag->ff_jdep->wk_type != D_JNEWBLK) 5662 panic("handle_workitem_freefrag: Unexpected type %d\n", 5663 freefrag->ff_jdep->wk_type); 5664 cancel_jnewblk(WK_JNEWBLK(freefrag->ff_jdep), &wkhd); 5665 } 5666 FREE_LOCK(ump); 5667 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno, 5668 freefrag->ff_fragsize, freefrag->ff_inum, freefrag->ff_vtype, 5669 &wkhd, freefrag->ff_key); 5670 ACQUIRE_LOCK(ump); 5671 WORKITEM_FREE(freefrag, D_FREEFRAG); 5672 FREE_LOCK(ump); 5673 } 5674 5675 /* 5676 * Set up a dependency structure for an external attributes data block. 5677 * This routine follows much of the structure of softdep_setup_allocdirect. 5678 * See the description of softdep_setup_allocdirect above for details. 5679 */ 5680 void 5681 softdep_setup_allocext(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 5682 struct inode *ip; 5683 ufs_lbn_t off; 5684 ufs2_daddr_t newblkno; 5685 ufs2_daddr_t oldblkno; 5686 long newsize; 5687 long oldsize; 5688 struct buf *bp; 5689 { 5690 struct allocdirect *adp, *oldadp; 5691 struct allocdirectlst *adphead; 5692 struct freefrag *freefrag; 5693 struct inodedep *inodedep; 5694 struct jnewblk *jnewblk; 5695 struct newblk *newblk; 5696 struct mount *mp; 5697 struct ufsmount *ump; 5698 ufs_lbn_t lbn; 5699 5700 mp = ITOVFS(ip); 5701 ump = VFSTOUFS(mp); 5702 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5703 ("softdep_setup_allocext called on non-softdep filesystem")); 5704 KASSERT(off < UFS_NXADDR, 5705 ("softdep_setup_allocext: lbn %lld > UFS_NXADDR", (long long)off)); 5706 5707 lbn = bp->b_lblkno; 5708 if (oldblkno && oldblkno != newblkno) 5709 /* 5710 * The usual case is that a smaller fragment that 5711 * was just allocated has been replaced with a bigger 5712 * fragment or a full-size block. If it is marked as 5713 * B_DELWRI, the current contents have not been written 5714 * to disk. It is possible that the block was written 5715 * earlier, but very uncommon. If the block has never 5716 * been written, there is no need to send a BIO_DELETE 5717 * for it when it is freed. The gain from avoiding the 5718 * TRIMs for the common case of unwritten blocks far 5719 * exceeds the cost of the write amplification for the 5720 * uncommon case of failing to send a TRIM for a block 5721 * that had been written. 5722 */ 5723 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn, 5724 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY); 5725 else 5726 freefrag = NULL; 5727 5728 ACQUIRE_LOCK(ump); 5729 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5730 panic("softdep_setup_allocext: lost block"); 5731 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5732 ("softdep_setup_allocext: newblk already initialized")); 5733 /* 5734 * Convert the newblk to an allocdirect. 5735 */ 5736 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT); 5737 adp = (struct allocdirect *)newblk; 5738 newblk->nb_freefrag = freefrag; 5739 adp->ad_offset = off; 5740 adp->ad_oldblkno = oldblkno; 5741 adp->ad_newsize = newsize; 5742 adp->ad_oldsize = oldsize; 5743 adp->ad_state |= EXTDATA; 5744 5745 /* 5746 * Finish initializing the journal. 5747 */ 5748 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5749 jnewblk->jn_ino = ip->i_number; 5750 jnewblk->jn_lbn = lbn; 5751 add_to_journal(&jnewblk->jn_list); 5752 } 5753 if (freefrag && freefrag->ff_jdep != NULL && 5754 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5755 add_to_journal(freefrag->ff_jdep); 5756 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 5757 adp->ad_inodedep = inodedep; 5758 5759 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5760 /* 5761 * The list of allocdirects must be kept in sorted and ascending 5762 * order so that the rollback routines can quickly determine the 5763 * first uncommitted block (the size of the file stored on disk 5764 * ends at the end of the lowest committed fragment, or if there 5765 * are no fragments, at the end of the highest committed block). 5766 * Since files generally grow, the typical case is that the new 5767 * block is to be added at the end of the list. We speed this 5768 * special case by checking against the last allocdirect in the 5769 * list before laboriously traversing the list looking for the 5770 * insertion point. 5771 */ 5772 adphead = &inodedep->id_newextupdt; 5773 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5774 if (oldadp == NULL || oldadp->ad_offset <= off) { 5775 /* insert at end of list */ 5776 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5777 if (oldadp != NULL && oldadp->ad_offset == off) 5778 allocdirect_merge(adphead, adp, oldadp); 5779 FREE_LOCK(ump); 5780 return; 5781 } 5782 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5783 if (oldadp->ad_offset >= off) 5784 break; 5785 } 5786 if (oldadp == NULL) 5787 panic("softdep_setup_allocext: lost entry"); 5788 /* insert in middle of list */ 5789 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5790 if (oldadp->ad_offset == off) 5791 allocdirect_merge(adphead, adp, oldadp); 5792 FREE_LOCK(ump); 5793 } 5794 5795 /* 5796 * Indirect block allocation dependencies. 5797 * 5798 * The same dependencies that exist for a direct block also exist when 5799 * a new block is allocated and pointed to by an entry in a block of 5800 * indirect pointers. The undo/redo states described above are also 5801 * used here. Because an indirect block contains many pointers that 5802 * may have dependencies, a second copy of the entire in-memory indirect 5803 * block is kept. The buffer cache copy is always completely up-to-date. 5804 * The second copy, which is used only as a source for disk writes, 5805 * contains only the safe pointers (i.e., those that have no remaining 5806 * update dependencies). The second copy is freed when all pointers 5807 * are safe. The cache is not allowed to replace indirect blocks with 5808 * pending update dependencies. If a buffer containing an indirect 5809 * block with dependencies is written, these routines will mark it 5810 * dirty again. It can only be successfully written once all the 5811 * dependencies are removed. The ffs_fsync routine in conjunction with 5812 * softdep_sync_metadata work together to get all the dependencies 5813 * removed so that a file can be successfully written to disk. Three 5814 * procedures are used when setting up indirect block pointer 5815 * dependencies. The division is necessary because of the organization 5816 * of the "balloc" routine and because of the distinction between file 5817 * pages and file metadata blocks. 5818 */ 5819 5820 /* 5821 * Allocate a new allocindir structure. 5822 */ 5823 static struct allocindir * 5824 newallocindir(ip, ptrno, newblkno, oldblkno, lbn) 5825 struct inode *ip; /* inode for file being extended */ 5826 int ptrno; /* offset of pointer in indirect block */ 5827 ufs2_daddr_t newblkno; /* disk block number being added */ 5828 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 5829 ufs_lbn_t lbn; 5830 { 5831 struct newblk *newblk; 5832 struct allocindir *aip; 5833 struct freefrag *freefrag; 5834 struct jnewblk *jnewblk; 5835 5836 if (oldblkno) 5837 freefrag = newfreefrag(ip, oldblkno, ITOFS(ip)->fs_bsize, lbn, 5838 SINGLETON_KEY); 5839 else 5840 freefrag = NULL; 5841 ACQUIRE_LOCK(ITOUMP(ip)); 5842 if (newblk_lookup(ITOVFS(ip), newblkno, 0, &newblk) == 0) 5843 panic("new_allocindir: lost block"); 5844 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5845 ("newallocindir: newblk already initialized")); 5846 WORKITEM_REASSIGN(newblk, D_ALLOCINDIR); 5847 newblk->nb_freefrag = freefrag; 5848 aip = (struct allocindir *)newblk; 5849 aip->ai_offset = ptrno; 5850 aip->ai_oldblkno = oldblkno; 5851 aip->ai_lbn = lbn; 5852 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5853 jnewblk->jn_ino = ip->i_number; 5854 jnewblk->jn_lbn = lbn; 5855 add_to_journal(&jnewblk->jn_list); 5856 } 5857 if (freefrag && freefrag->ff_jdep != NULL && 5858 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5859 add_to_journal(freefrag->ff_jdep); 5860 return (aip); 5861 } 5862 5863 /* 5864 * Called just before setting an indirect block pointer 5865 * to a newly allocated file page. 5866 */ 5867 void 5868 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 5869 struct inode *ip; /* inode for file being extended */ 5870 ufs_lbn_t lbn; /* allocated block number within file */ 5871 struct buf *bp; /* buffer with indirect blk referencing page */ 5872 int ptrno; /* offset of pointer in indirect block */ 5873 ufs2_daddr_t newblkno; /* disk block number being added */ 5874 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 5875 struct buf *nbp; /* buffer holding allocated page */ 5876 { 5877 struct inodedep *inodedep; 5878 struct freefrag *freefrag; 5879 struct allocindir *aip; 5880 struct pagedep *pagedep; 5881 struct mount *mp; 5882 struct ufsmount *ump; 5883 5884 mp = ITOVFS(ip); 5885 ump = VFSTOUFS(mp); 5886 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5887 ("softdep_setup_allocindir_page called on non-softdep filesystem")); 5888 KASSERT(lbn == nbp->b_lblkno, 5889 ("softdep_setup_allocindir_page: lbn %jd != lblkno %jd", 5890 lbn, bp->b_lblkno)); 5891 CTR4(KTR_SUJ, 5892 "softdep_setup_allocindir_page: ino %d blkno %jd oldblkno %jd " 5893 "lbn %jd", ip->i_number, newblkno, oldblkno, lbn); 5894 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page"); 5895 aip = newallocindir(ip, ptrno, newblkno, oldblkno, lbn); 5896 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 5897 /* 5898 * If we are allocating a directory page, then we must 5899 * allocate an associated pagedep to track additions and 5900 * deletions. 5901 */ 5902 if ((ip->i_mode & IFMT) == IFDIR) 5903 pagedep_lookup(mp, nbp, ip->i_number, lbn, DEPALLOC, &pagedep); 5904 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 5905 freefrag = setup_allocindir_phase2(bp, ip, inodedep, aip, lbn); 5906 FREE_LOCK(ump); 5907 if (freefrag) 5908 handle_workitem_freefrag(freefrag); 5909 } 5910 5911 /* 5912 * Called just before setting an indirect block pointer to a 5913 * newly allocated indirect block. 5914 */ 5915 void 5916 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 5917 struct buf *nbp; /* newly allocated indirect block */ 5918 struct inode *ip; /* inode for file being extended */ 5919 struct buf *bp; /* indirect block referencing allocated block */ 5920 int ptrno; /* offset of pointer in indirect block */ 5921 ufs2_daddr_t newblkno; /* disk block number being added */ 5922 { 5923 struct inodedep *inodedep; 5924 struct allocindir *aip; 5925 struct ufsmount *ump; 5926 ufs_lbn_t lbn; 5927 5928 ump = ITOUMP(ip); 5929 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 5930 ("softdep_setup_allocindir_meta called on non-softdep filesystem")); 5931 CTR3(KTR_SUJ, 5932 "softdep_setup_allocindir_meta: ino %d blkno %jd ptrno %d", 5933 ip->i_number, newblkno, ptrno); 5934 lbn = nbp->b_lblkno; 5935 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta"); 5936 aip = newallocindir(ip, ptrno, newblkno, 0, lbn); 5937 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep); 5938 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 5939 if (setup_allocindir_phase2(bp, ip, inodedep, aip, lbn)) 5940 panic("softdep_setup_allocindir_meta: Block already existed"); 5941 FREE_LOCK(ump); 5942 } 5943 5944 static void 5945 indirdep_complete(indirdep) 5946 struct indirdep *indirdep; 5947 { 5948 struct allocindir *aip; 5949 5950 LIST_REMOVE(indirdep, ir_next); 5951 indirdep->ir_state |= DEPCOMPLETE; 5952 5953 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) { 5954 LIST_REMOVE(aip, ai_next); 5955 free_newblk(&aip->ai_block); 5956 } 5957 /* 5958 * If this indirdep is not attached to a buf it was simply waiting 5959 * on completion to clear completehd. free_indirdep() asserts 5960 * that nothing is dangling. 5961 */ 5962 if ((indirdep->ir_state & ONWORKLIST) == 0) 5963 free_indirdep(indirdep); 5964 } 5965 5966 static struct indirdep * 5967 indirdep_lookup(mp, ip, bp) 5968 struct mount *mp; 5969 struct inode *ip; 5970 struct buf *bp; 5971 { 5972 struct indirdep *indirdep, *newindirdep; 5973 struct newblk *newblk; 5974 struct ufsmount *ump; 5975 struct worklist *wk; 5976 struct fs *fs; 5977 ufs2_daddr_t blkno; 5978 5979 ump = VFSTOUFS(mp); 5980 LOCK_OWNED(ump); 5981 indirdep = NULL; 5982 newindirdep = NULL; 5983 fs = ump->um_fs; 5984 for (;;) { 5985 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 5986 if (wk->wk_type != D_INDIRDEP) 5987 continue; 5988 indirdep = WK_INDIRDEP(wk); 5989 break; 5990 } 5991 /* Found on the buffer worklist, no new structure to free. */ 5992 if (indirdep != NULL && newindirdep == NULL) 5993 return (indirdep); 5994 if (indirdep != NULL && newindirdep != NULL) 5995 panic("indirdep_lookup: simultaneous create"); 5996 /* None found on the buffer and a new structure is ready. */ 5997 if (indirdep == NULL && newindirdep != NULL) 5998 break; 5999 /* None found and no new structure available. */ 6000 FREE_LOCK(ump); 6001 newindirdep = malloc(sizeof(struct indirdep), 6002 M_INDIRDEP, M_SOFTDEP_FLAGS); 6003 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, mp); 6004 newindirdep->ir_state = ATTACHED; 6005 if (I_IS_UFS1(ip)) 6006 newindirdep->ir_state |= UFS1FMT; 6007 TAILQ_INIT(&newindirdep->ir_trunc); 6008 newindirdep->ir_saveddata = NULL; 6009 LIST_INIT(&newindirdep->ir_deplisthd); 6010 LIST_INIT(&newindirdep->ir_donehd); 6011 LIST_INIT(&newindirdep->ir_writehd); 6012 LIST_INIT(&newindirdep->ir_completehd); 6013 if (bp->b_blkno == bp->b_lblkno) { 6014 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp, 6015 NULL, NULL); 6016 bp->b_blkno = blkno; 6017 } 6018 newindirdep->ir_freeblks = NULL; 6019 newindirdep->ir_savebp = 6020 getblk(ump->um_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0); 6021 newindirdep->ir_bp = bp; 6022 BUF_KERNPROC(newindirdep->ir_savebp); 6023 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount); 6024 ACQUIRE_LOCK(ump); 6025 } 6026 indirdep = newindirdep; 6027 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list); 6028 /* 6029 * If the block is not yet allocated we don't set DEPCOMPLETE so 6030 * that we don't free dependencies until the pointers are valid. 6031 * This could search b_dep for D_ALLOCDIRECT/D_ALLOCINDIR rather 6032 * than using the hash. 6033 */ 6034 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk)) 6035 LIST_INSERT_HEAD(&newblk->nb_indirdeps, indirdep, ir_next); 6036 else 6037 indirdep->ir_state |= DEPCOMPLETE; 6038 return (indirdep); 6039 } 6040 6041 /* 6042 * Called to finish the allocation of the "aip" allocated 6043 * by one of the two routines above. 6044 */ 6045 static struct freefrag * 6046 setup_allocindir_phase2(bp, ip, inodedep, aip, lbn) 6047 struct buf *bp; /* in-memory copy of the indirect block */ 6048 struct inode *ip; /* inode for file being extended */ 6049 struct inodedep *inodedep; /* Inodedep for ip */ 6050 struct allocindir *aip; /* allocindir allocated by the above routines */ 6051 ufs_lbn_t lbn; /* Logical block number for this block. */ 6052 { 6053 struct fs *fs; 6054 struct indirdep *indirdep; 6055 struct allocindir *oldaip; 6056 struct freefrag *freefrag; 6057 struct mount *mp; 6058 struct ufsmount *ump; 6059 6060 mp = ITOVFS(ip); 6061 ump = VFSTOUFS(mp); 6062 LOCK_OWNED(ump); 6063 fs = ump->um_fs; 6064 if (bp->b_lblkno >= 0) 6065 panic("setup_allocindir_phase2: not indir blk"); 6066 KASSERT(aip->ai_offset >= 0 && aip->ai_offset < NINDIR(fs), 6067 ("setup_allocindir_phase2: Bad offset %d", aip->ai_offset)); 6068 indirdep = indirdep_lookup(mp, ip, bp); 6069 KASSERT(indirdep->ir_savebp != NULL, 6070 ("setup_allocindir_phase2 NULL ir_savebp")); 6071 aip->ai_indirdep = indirdep; 6072 /* 6073 * Check for an unwritten dependency for this indirect offset. If 6074 * there is, merge the old dependency into the new one. This happens 6075 * as a result of reallocblk only. 6076 */ 6077 freefrag = NULL; 6078 if (aip->ai_oldblkno != 0) { 6079 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) { 6080 if (oldaip->ai_offset == aip->ai_offset) { 6081 freefrag = allocindir_merge(aip, oldaip); 6082 goto done; 6083 } 6084 } 6085 LIST_FOREACH(oldaip, &indirdep->ir_donehd, ai_next) { 6086 if (oldaip->ai_offset == aip->ai_offset) { 6087 freefrag = allocindir_merge(aip, oldaip); 6088 goto done; 6089 } 6090 } 6091 } 6092 done: 6093 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next); 6094 return (freefrag); 6095 } 6096 6097 /* 6098 * Merge two allocindirs which refer to the same block. Move newblock 6099 * dependencies and setup the freefrags appropriately. 6100 */ 6101 static struct freefrag * 6102 allocindir_merge(aip, oldaip) 6103 struct allocindir *aip; 6104 struct allocindir *oldaip; 6105 { 6106 struct freefrag *freefrag; 6107 struct worklist *wk; 6108 6109 if (oldaip->ai_newblkno != aip->ai_oldblkno) 6110 panic("allocindir_merge: blkno"); 6111 aip->ai_oldblkno = oldaip->ai_oldblkno; 6112 freefrag = aip->ai_freefrag; 6113 aip->ai_freefrag = oldaip->ai_freefrag; 6114 oldaip->ai_freefrag = NULL; 6115 KASSERT(freefrag != NULL, ("setup_allocindir_phase2: No freefrag")); 6116 /* 6117 * If we are tracking a new directory-block allocation, 6118 * move it from the old allocindir to the new allocindir. 6119 */ 6120 if ((wk = LIST_FIRST(&oldaip->ai_newdirblk)) != NULL) { 6121 WORKLIST_REMOVE(wk); 6122 if (!LIST_EMPTY(&oldaip->ai_newdirblk)) 6123 panic("allocindir_merge: extra newdirblk"); 6124 WORKLIST_INSERT(&aip->ai_newdirblk, wk); 6125 } 6126 /* 6127 * We can skip journaling for this freefrag and just complete 6128 * any pending journal work for the allocindir that is being 6129 * removed after the freefrag completes. 6130 */ 6131 if (freefrag->ff_jdep) 6132 cancel_jfreefrag(WK_JFREEFRAG(freefrag->ff_jdep)); 6133 LIST_REMOVE(oldaip, ai_next); 6134 freefrag->ff_jdep = (struct worklist *)cancel_newblk(&oldaip->ai_block, 6135 &freefrag->ff_list, &freefrag->ff_jwork); 6136 free_newblk(&oldaip->ai_block); 6137 6138 return (freefrag); 6139 } 6140 6141 static inline void 6142 setup_freedirect(freeblks, ip, i, needj) 6143 struct freeblks *freeblks; 6144 struct inode *ip; 6145 int i; 6146 int needj; 6147 { 6148 struct ufsmount *ump; 6149 ufs2_daddr_t blkno; 6150 int frags; 6151 6152 blkno = DIP(ip, i_db[i]); 6153 if (blkno == 0) 6154 return; 6155 DIP_SET(ip, i_db[i], 0); 6156 ump = ITOUMP(ip); 6157 frags = sblksize(ump->um_fs, ip->i_size, i); 6158 frags = numfrags(ump->um_fs, frags); 6159 newfreework(ump, freeblks, NULL, i, blkno, frags, 0, needj); 6160 } 6161 6162 static inline void 6163 setup_freeext(freeblks, ip, i, needj) 6164 struct freeblks *freeblks; 6165 struct inode *ip; 6166 int i; 6167 int needj; 6168 { 6169 struct ufsmount *ump; 6170 ufs2_daddr_t blkno; 6171 int frags; 6172 6173 blkno = ip->i_din2->di_extb[i]; 6174 if (blkno == 0) 6175 return; 6176 ip->i_din2->di_extb[i] = 0; 6177 ump = ITOUMP(ip); 6178 frags = sblksize(ump->um_fs, ip->i_din2->di_extsize, i); 6179 frags = numfrags(ump->um_fs, frags); 6180 newfreework(ump, freeblks, NULL, -1 - i, blkno, frags, 0, needj); 6181 } 6182 6183 static inline void 6184 setup_freeindir(freeblks, ip, i, lbn, needj) 6185 struct freeblks *freeblks; 6186 struct inode *ip; 6187 int i; 6188 ufs_lbn_t lbn; 6189 int needj; 6190 { 6191 struct ufsmount *ump; 6192 ufs2_daddr_t blkno; 6193 6194 blkno = DIP(ip, i_ib[i]); 6195 if (blkno == 0) 6196 return; 6197 DIP_SET(ip, i_ib[i], 0); 6198 ump = ITOUMP(ip); 6199 newfreework(ump, freeblks, NULL, lbn, blkno, ump->um_fs->fs_frag, 6200 0, needj); 6201 } 6202 6203 static inline struct freeblks * 6204 newfreeblks(mp, ip) 6205 struct mount *mp; 6206 struct inode *ip; 6207 { 6208 struct freeblks *freeblks; 6209 6210 freeblks = malloc(sizeof(struct freeblks), 6211 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO); 6212 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp); 6213 LIST_INIT(&freeblks->fb_jblkdephd); 6214 LIST_INIT(&freeblks->fb_jwork); 6215 freeblks->fb_ref = 0; 6216 freeblks->fb_cgwait = 0; 6217 freeblks->fb_state = ATTACHED; 6218 freeblks->fb_uid = ip->i_uid; 6219 freeblks->fb_inum = ip->i_number; 6220 freeblks->fb_vtype = ITOV(ip)->v_type; 6221 freeblks->fb_modrev = DIP(ip, i_modrev); 6222 freeblks->fb_devvp = ITODEVVP(ip); 6223 freeblks->fb_chkcnt = 0; 6224 freeblks->fb_len = 0; 6225 6226 return (freeblks); 6227 } 6228 6229 static void 6230 trunc_indirdep(indirdep, freeblks, bp, off) 6231 struct indirdep *indirdep; 6232 struct freeblks *freeblks; 6233 struct buf *bp; 6234 int off; 6235 { 6236 struct allocindir *aip, *aipn; 6237 6238 /* 6239 * The first set of allocindirs won't be in savedbp. 6240 */ 6241 LIST_FOREACH_SAFE(aip, &indirdep->ir_deplisthd, ai_next, aipn) 6242 if (aip->ai_offset > off) 6243 cancel_allocindir(aip, bp, freeblks, 1); 6244 LIST_FOREACH_SAFE(aip, &indirdep->ir_donehd, ai_next, aipn) 6245 if (aip->ai_offset > off) 6246 cancel_allocindir(aip, bp, freeblks, 1); 6247 /* 6248 * These will exist in savedbp. 6249 */ 6250 LIST_FOREACH_SAFE(aip, &indirdep->ir_writehd, ai_next, aipn) 6251 if (aip->ai_offset > off) 6252 cancel_allocindir(aip, NULL, freeblks, 0); 6253 LIST_FOREACH_SAFE(aip, &indirdep->ir_completehd, ai_next, aipn) 6254 if (aip->ai_offset > off) 6255 cancel_allocindir(aip, NULL, freeblks, 0); 6256 } 6257 6258 /* 6259 * Follow the chain of indirects down to lastlbn creating a freework 6260 * structure for each. This will be used to start indir_trunc() at 6261 * the right offset and create the journal records for the parrtial 6262 * truncation. A second step will handle the truncated dependencies. 6263 */ 6264 static int 6265 setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno) 6266 struct freeblks *freeblks; 6267 struct inode *ip; 6268 ufs_lbn_t lbn; 6269 ufs_lbn_t lastlbn; 6270 ufs2_daddr_t blkno; 6271 { 6272 struct indirdep *indirdep; 6273 struct indirdep *indirn; 6274 struct freework *freework; 6275 struct newblk *newblk; 6276 struct mount *mp; 6277 struct ufsmount *ump; 6278 struct buf *bp; 6279 uint8_t *start; 6280 uint8_t *end; 6281 ufs_lbn_t lbnadd; 6282 int level; 6283 int error; 6284 int off; 6285 6286 6287 freework = NULL; 6288 if (blkno == 0) 6289 return (0); 6290 mp = freeblks->fb_list.wk_mp; 6291 ump = VFSTOUFS(mp); 6292 bp = getblk(ITOV(ip), lbn, mp->mnt_stat.f_iosize, 0, 0, 0); 6293 if ((bp->b_flags & B_CACHE) == 0) { 6294 bp->b_blkno = blkptrtodb(VFSTOUFS(mp), blkno); 6295 bp->b_iocmd = BIO_READ; 6296 bp->b_flags &= ~B_INVAL; 6297 bp->b_ioflags &= ~BIO_ERROR; 6298 vfs_busy_pages(bp, 0); 6299 bp->b_iooffset = dbtob(bp->b_blkno); 6300 bstrategy(bp); 6301 #ifdef RACCT 6302 if (racct_enable) { 6303 PROC_LOCK(curproc); 6304 racct_add_buf(curproc, bp, 0); 6305 PROC_UNLOCK(curproc); 6306 } 6307 #endif /* RACCT */ 6308 curthread->td_ru.ru_inblock++; 6309 error = bufwait(bp); 6310 if (error) { 6311 brelse(bp); 6312 return (error); 6313 } 6314 } 6315 level = lbn_level(lbn); 6316 lbnadd = lbn_offset(ump->um_fs, level); 6317 /* 6318 * Compute the offset of the last block we want to keep. Store 6319 * in the freework the first block we want to completely free. 6320 */ 6321 off = (lastlbn - -(lbn + level)) / lbnadd; 6322 if (off + 1 == NINDIR(ump->um_fs)) 6323 goto nowork; 6324 freework = newfreework(ump, freeblks, NULL, lbn, blkno, 0, off + 1, 0); 6325 /* 6326 * Link the freework into the indirdep. This will prevent any new 6327 * allocations from proceeding until we are finished with the 6328 * truncate and the block is written. 6329 */ 6330 ACQUIRE_LOCK(ump); 6331 indirdep = indirdep_lookup(mp, ip, bp); 6332 if (indirdep->ir_freeblks) 6333 panic("setup_trunc_indir: indirdep already truncated."); 6334 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, freework, fw_next); 6335 freework->fw_indir = indirdep; 6336 /* 6337 * Cancel any allocindirs that will not make it to disk. 6338 * We have to do this for all copies of the indirdep that 6339 * live on this newblk. 6340 */ 6341 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 6342 if (newblk_lookup(mp, dbtofsb(ump->um_fs, bp->b_blkno), 0, 6343 &newblk) == 0) 6344 panic("setup_trunc_indir: lost block"); 6345 LIST_FOREACH(indirn, &newblk->nb_indirdeps, ir_next) 6346 trunc_indirdep(indirn, freeblks, bp, off); 6347 } else 6348 trunc_indirdep(indirdep, freeblks, bp, off); 6349 FREE_LOCK(ump); 6350 /* 6351 * Creation is protected by the buf lock. The saveddata is only 6352 * needed if a full truncation follows a partial truncation but it 6353 * is difficult to allocate in that case so we fetch it anyway. 6354 */ 6355 if (indirdep->ir_saveddata == NULL) 6356 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 6357 M_SOFTDEP_FLAGS); 6358 nowork: 6359 /* Fetch the blkno of the child and the zero start offset. */ 6360 if (I_IS_UFS1(ip)) { 6361 blkno = ((ufs1_daddr_t *)bp->b_data)[off]; 6362 start = (uint8_t *)&((ufs1_daddr_t *)bp->b_data)[off+1]; 6363 } else { 6364 blkno = ((ufs2_daddr_t *)bp->b_data)[off]; 6365 start = (uint8_t *)&((ufs2_daddr_t *)bp->b_data)[off+1]; 6366 } 6367 if (freework) { 6368 /* Zero the truncated pointers. */ 6369 end = bp->b_data + bp->b_bcount; 6370 bzero(start, end - start); 6371 bdwrite(bp); 6372 } else 6373 bqrelse(bp); 6374 if (level == 0) 6375 return (0); 6376 lbn++; /* adjust level */ 6377 lbn -= (off * lbnadd); 6378 return setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno); 6379 } 6380 6381 /* 6382 * Complete the partial truncation of an indirect block setup by 6383 * setup_trunc_indir(). This zeros the truncated pointers in the saved 6384 * copy and writes them to disk before the freeblks is allowed to complete. 6385 */ 6386 static void 6387 complete_trunc_indir(freework) 6388 struct freework *freework; 6389 { 6390 struct freework *fwn; 6391 struct indirdep *indirdep; 6392 struct ufsmount *ump; 6393 struct buf *bp; 6394 uintptr_t start; 6395 int count; 6396 6397 ump = VFSTOUFS(freework->fw_list.wk_mp); 6398 LOCK_OWNED(ump); 6399 indirdep = freework->fw_indir; 6400 for (;;) { 6401 bp = indirdep->ir_bp; 6402 /* See if the block was discarded. */ 6403 if (bp == NULL) 6404 break; 6405 /* Inline part of getdirtybuf(). We dont want bremfree. */ 6406 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) 6407 break; 6408 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 6409 LOCK_PTR(ump)) == 0) 6410 BUF_UNLOCK(bp); 6411 ACQUIRE_LOCK(ump); 6412 } 6413 freework->fw_state |= DEPCOMPLETE; 6414 TAILQ_REMOVE(&indirdep->ir_trunc, freework, fw_next); 6415 /* 6416 * Zero the pointers in the saved copy. 6417 */ 6418 if (indirdep->ir_state & UFS1FMT) 6419 start = sizeof(ufs1_daddr_t); 6420 else 6421 start = sizeof(ufs2_daddr_t); 6422 start *= freework->fw_start; 6423 count = indirdep->ir_savebp->b_bcount - start; 6424 start += (uintptr_t)indirdep->ir_savebp->b_data; 6425 bzero((char *)start, count); 6426 /* 6427 * We need to start the next truncation in the list if it has not 6428 * been started yet. 6429 */ 6430 fwn = TAILQ_FIRST(&indirdep->ir_trunc); 6431 if (fwn != NULL) { 6432 if (fwn->fw_freeblks == indirdep->ir_freeblks) 6433 TAILQ_REMOVE(&indirdep->ir_trunc, fwn, fw_next); 6434 if ((fwn->fw_state & ONWORKLIST) == 0) 6435 freework_enqueue(fwn); 6436 } 6437 /* 6438 * If bp is NULL the block was fully truncated, restore 6439 * the saved block list otherwise free it if it is no 6440 * longer needed. 6441 */ 6442 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 6443 if (bp == NULL) 6444 bcopy(indirdep->ir_saveddata, 6445 indirdep->ir_savebp->b_data, 6446 indirdep->ir_savebp->b_bcount); 6447 free(indirdep->ir_saveddata, M_INDIRDEP); 6448 indirdep->ir_saveddata = NULL; 6449 } 6450 /* 6451 * When bp is NULL there is a full truncation pending. We 6452 * must wait for this full truncation to be journaled before 6453 * we can release this freework because the disk pointers will 6454 * never be written as zero. 6455 */ 6456 if (bp == NULL) { 6457 if (LIST_EMPTY(&indirdep->ir_freeblks->fb_jblkdephd)) 6458 handle_written_freework(freework); 6459 else 6460 WORKLIST_INSERT(&indirdep->ir_freeblks->fb_freeworkhd, 6461 &freework->fw_list); 6462 } else { 6463 /* Complete when the real copy is written. */ 6464 WORKLIST_INSERT(&bp->b_dep, &freework->fw_list); 6465 BUF_UNLOCK(bp); 6466 } 6467 } 6468 6469 /* 6470 * Calculate the number of blocks we are going to release where datablocks 6471 * is the current total and length is the new file size. 6472 */ 6473 static ufs2_daddr_t 6474 blkcount(fs, datablocks, length) 6475 struct fs *fs; 6476 ufs2_daddr_t datablocks; 6477 off_t length; 6478 { 6479 off_t totblks, numblks; 6480 6481 totblks = 0; 6482 numblks = howmany(length, fs->fs_bsize); 6483 if (numblks <= UFS_NDADDR) { 6484 totblks = howmany(length, fs->fs_fsize); 6485 goto out; 6486 } 6487 totblks = blkstofrags(fs, numblks); 6488 numblks -= UFS_NDADDR; 6489 /* 6490 * Count all single, then double, then triple indirects required. 6491 * Subtracting one indirects worth of blocks for each pass 6492 * acknowledges one of each pointed to by the inode. 6493 */ 6494 for (;;) { 6495 totblks += blkstofrags(fs, howmany(numblks, NINDIR(fs))); 6496 numblks -= NINDIR(fs); 6497 if (numblks <= 0) 6498 break; 6499 numblks = howmany(numblks, NINDIR(fs)); 6500 } 6501 out: 6502 totblks = fsbtodb(fs, totblks); 6503 /* 6504 * Handle sparse files. We can't reclaim more blocks than the inode 6505 * references. We will correct it later in handle_complete_freeblks() 6506 * when we know the real count. 6507 */ 6508 if (totblks > datablocks) 6509 return (0); 6510 return (datablocks - totblks); 6511 } 6512 6513 /* 6514 * Handle freeblocks for journaled softupdate filesystems. 6515 * 6516 * Contrary to normal softupdates, we must preserve the block pointers in 6517 * indirects until their subordinates are free. This is to avoid journaling 6518 * every block that is freed which may consume more space than the journal 6519 * itself. The recovery program will see the free block journals at the 6520 * base of the truncated area and traverse them to reclaim space. The 6521 * pointers in the inode may be cleared immediately after the journal 6522 * records are written because each direct and indirect pointer in the 6523 * inode is recorded in a journal. This permits full truncation to proceed 6524 * asynchronously. The write order is journal -> inode -> cgs -> indirects. 6525 * 6526 * The algorithm is as follows: 6527 * 1) Traverse the in-memory state and create journal entries to release 6528 * the relevant blocks and full indirect trees. 6529 * 2) Traverse the indirect block chain adding partial truncation freework 6530 * records to indirects in the path to lastlbn. The freework will 6531 * prevent new allocation dependencies from being satisfied in this 6532 * indirect until the truncation completes. 6533 * 3) Read and lock the inode block, performing an update with the new size 6534 * and pointers. This prevents truncated data from becoming valid on 6535 * disk through step 4. 6536 * 4) Reap unsatisfied dependencies that are beyond the truncated area, 6537 * eliminate journal work for those records that do not require it. 6538 * 5) Schedule the journal records to be written followed by the inode block. 6539 * 6) Allocate any necessary frags for the end of file. 6540 * 7) Zero any partially truncated blocks. 6541 * 6542 * From this truncation proceeds asynchronously using the freework and 6543 * indir_trunc machinery. The file will not be extended again into a 6544 * partially truncated indirect block until all work is completed but 6545 * the normal dependency mechanism ensures that it is rolled back/forward 6546 * as appropriate. Further truncation may occur without delay and is 6547 * serialized in indir_trunc(). 6548 */ 6549 void 6550 softdep_journal_freeblocks(ip, cred, length, flags) 6551 struct inode *ip; /* The inode whose length is to be reduced */ 6552 struct ucred *cred; 6553 off_t length; /* The new length for the file */ 6554 int flags; /* IO_EXT and/or IO_NORMAL */ 6555 { 6556 struct freeblks *freeblks, *fbn; 6557 struct worklist *wk, *wkn; 6558 struct inodedep *inodedep; 6559 struct jblkdep *jblkdep; 6560 struct allocdirect *adp, *adpn; 6561 struct ufsmount *ump; 6562 struct fs *fs; 6563 struct buf *bp; 6564 struct vnode *vp; 6565 struct mount *mp; 6566 ufs2_daddr_t extblocks, datablocks; 6567 ufs_lbn_t tmpval, lbn, lastlbn; 6568 int frags, lastoff, iboff, allocblock, needj, error, i; 6569 6570 ump = ITOUMP(ip); 6571 mp = UFSTOVFS(ump); 6572 fs = ump->um_fs; 6573 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6574 ("softdep_journal_freeblocks called on non-softdep filesystem")); 6575 vp = ITOV(ip); 6576 needj = 1; 6577 iboff = -1; 6578 allocblock = 0; 6579 extblocks = 0; 6580 datablocks = 0; 6581 frags = 0; 6582 freeblks = newfreeblks(mp, ip); 6583 ACQUIRE_LOCK(ump); 6584 /* 6585 * If we're truncating a removed file that will never be written 6586 * we don't need to journal the block frees. The canceled journals 6587 * for the allocations will suffice. 6588 */ 6589 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6590 if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED && 6591 length == 0) 6592 needj = 0; 6593 CTR3(KTR_SUJ, "softdep_journal_freeblks: ip %d length %ld needj %d", 6594 ip->i_number, length, needj); 6595 FREE_LOCK(ump); 6596 /* 6597 * Calculate the lbn that we are truncating to. This results in -1 6598 * if we're truncating the 0 bytes. So it is the last lbn we want 6599 * to keep, not the first lbn we want to truncate. 6600 */ 6601 lastlbn = lblkno(fs, length + fs->fs_bsize - 1) - 1; 6602 lastoff = blkoff(fs, length); 6603 /* 6604 * Compute frags we are keeping in lastlbn. 0 means all. 6605 */ 6606 if (lastlbn >= 0 && lastlbn < UFS_NDADDR) { 6607 frags = fragroundup(fs, lastoff); 6608 /* adp offset of last valid allocdirect. */ 6609 iboff = lastlbn; 6610 } else if (lastlbn > 0) 6611 iboff = UFS_NDADDR; 6612 if (fs->fs_magic == FS_UFS2_MAGIC) 6613 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 6614 /* 6615 * Handle normal data blocks and indirects. This section saves 6616 * values used after the inode update to complete frag and indirect 6617 * truncation. 6618 */ 6619 if ((flags & IO_NORMAL) != 0) { 6620 /* 6621 * Handle truncation of whole direct and indirect blocks. 6622 */ 6623 for (i = iboff + 1; i < UFS_NDADDR; i++) 6624 setup_freedirect(freeblks, ip, i, needj); 6625 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR; 6626 i < UFS_NIADDR; 6627 i++, lbn += tmpval, tmpval *= NINDIR(fs)) { 6628 /* Release a whole indirect tree. */ 6629 if (lbn > lastlbn) { 6630 setup_freeindir(freeblks, ip, i, -lbn -i, 6631 needj); 6632 continue; 6633 } 6634 iboff = i + UFS_NDADDR; 6635 /* 6636 * Traverse partially truncated indirect tree. 6637 */ 6638 if (lbn <= lastlbn && lbn + tmpval - 1 > lastlbn) 6639 setup_trunc_indir(freeblks, ip, -lbn - i, 6640 lastlbn, DIP(ip, i_ib[i])); 6641 } 6642 /* 6643 * Handle partial truncation to a frag boundary. 6644 */ 6645 if (frags) { 6646 ufs2_daddr_t blkno; 6647 long oldfrags; 6648 6649 oldfrags = blksize(fs, ip, lastlbn); 6650 blkno = DIP(ip, i_db[lastlbn]); 6651 if (blkno && oldfrags != frags) { 6652 oldfrags -= frags; 6653 oldfrags = numfrags(fs, oldfrags); 6654 blkno += numfrags(fs, frags); 6655 newfreework(ump, freeblks, NULL, lastlbn, 6656 blkno, oldfrags, 0, needj); 6657 if (needj) 6658 adjust_newfreework(freeblks, 6659 numfrags(fs, frags)); 6660 } else if (blkno == 0) 6661 allocblock = 1; 6662 } 6663 /* 6664 * Add a journal record for partial truncate if we are 6665 * handling indirect blocks. Non-indirects need no extra 6666 * journaling. 6667 */ 6668 if (length != 0 && lastlbn >= UFS_NDADDR) { 6669 ip->i_flag |= IN_TRUNCATED; 6670 newjtrunc(freeblks, length, 0); 6671 } 6672 ip->i_size = length; 6673 DIP_SET(ip, i_size, ip->i_size); 6674 datablocks = DIP(ip, i_blocks) - extblocks; 6675 if (length != 0) 6676 datablocks = blkcount(fs, datablocks, length); 6677 freeblks->fb_len = length; 6678 } 6679 if ((flags & IO_EXT) != 0) { 6680 for (i = 0; i < UFS_NXADDR; i++) 6681 setup_freeext(freeblks, ip, i, needj); 6682 ip->i_din2->di_extsize = 0; 6683 datablocks += extblocks; 6684 } 6685 #ifdef QUOTA 6686 /* Reference the quotas in case the block count is wrong in the end. */ 6687 quotaref(vp, freeblks->fb_quota); 6688 (void) chkdq(ip, -datablocks, NOCRED, 0); 6689 #endif 6690 freeblks->fb_chkcnt = -datablocks; 6691 UFS_LOCK(ump); 6692 fs->fs_pendingblocks += datablocks; 6693 UFS_UNLOCK(ump); 6694 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 6695 /* 6696 * Handle truncation of incomplete alloc direct dependencies. We 6697 * hold the inode block locked to prevent incomplete dependencies 6698 * from reaching the disk while we are eliminating those that 6699 * have been truncated. This is a partially inlined ffs_update(). 6700 */ 6701 ufs_itimes(vp); 6702 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED); 6703 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 6704 (int)fs->fs_bsize, cred, &bp); 6705 if (error) { 6706 brelse(bp); 6707 softdep_error("softdep_journal_freeblocks", error); 6708 return; 6709 } 6710 if (bp->b_bufsize == fs->fs_bsize) 6711 bp->b_flags |= B_CLUSTEROK; 6712 softdep_update_inodeblock(ip, bp, 0); 6713 if (ump->um_fstype == UFS1) { 6714 *((struct ufs1_dinode *)bp->b_data + 6715 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1; 6716 } else { 6717 ffs_update_dinode_ckhash(fs, ip->i_din2); 6718 *((struct ufs2_dinode *)bp->b_data + 6719 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2; 6720 } 6721 ACQUIRE_LOCK(ump); 6722 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6723 if ((inodedep->id_state & IOSTARTED) != 0) 6724 panic("softdep_setup_freeblocks: inode busy"); 6725 /* 6726 * Add the freeblks structure to the list of operations that 6727 * must await the zero'ed inode being written to disk. If we 6728 * still have a bitmap dependency (needj), then the inode 6729 * has never been written to disk, so we can process the 6730 * freeblks below once we have deleted the dependencies. 6731 */ 6732 if (needj) 6733 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 6734 else 6735 freeblks->fb_state |= COMPLETE; 6736 if ((flags & IO_NORMAL) != 0) { 6737 TAILQ_FOREACH_SAFE(adp, &inodedep->id_inoupdt, ad_next, adpn) { 6738 if (adp->ad_offset > iboff) 6739 cancel_allocdirect(&inodedep->id_inoupdt, adp, 6740 freeblks); 6741 /* 6742 * Truncate the allocdirect. We could eliminate 6743 * or modify journal records as well. 6744 */ 6745 else if (adp->ad_offset == iboff && frags) 6746 adp->ad_newsize = frags; 6747 } 6748 } 6749 if ((flags & IO_EXT) != 0) 6750 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 6751 cancel_allocdirect(&inodedep->id_extupdt, adp, 6752 freeblks); 6753 /* 6754 * Scan the bufwait list for newblock dependencies that will never 6755 * make it to disk. 6756 */ 6757 LIST_FOREACH_SAFE(wk, &inodedep->id_bufwait, wk_list, wkn) { 6758 if (wk->wk_type != D_ALLOCDIRECT) 6759 continue; 6760 adp = WK_ALLOCDIRECT(wk); 6761 if (((flags & IO_NORMAL) != 0 && (adp->ad_offset > iboff)) || 6762 ((flags & IO_EXT) != 0 && (adp->ad_state & EXTDATA))) { 6763 cancel_jfreeblk(freeblks, adp->ad_newblkno); 6764 cancel_newblk(WK_NEWBLK(wk), NULL, &freeblks->fb_jwork); 6765 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 6766 } 6767 } 6768 /* 6769 * Add journal work. 6770 */ 6771 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) 6772 add_to_journal(&jblkdep->jb_list); 6773 FREE_LOCK(ump); 6774 bdwrite(bp); 6775 /* 6776 * Truncate dependency structures beyond length. 6777 */ 6778 trunc_dependencies(ip, freeblks, lastlbn, frags, flags); 6779 /* 6780 * This is only set when we need to allocate a fragment because 6781 * none existed at the end of a frag-sized file. It handles only 6782 * allocating a new, zero filled block. 6783 */ 6784 if (allocblock) { 6785 ip->i_size = length - lastoff; 6786 DIP_SET(ip, i_size, ip->i_size); 6787 error = UFS_BALLOC(vp, length - 1, 1, cred, BA_CLRBUF, &bp); 6788 if (error != 0) { 6789 softdep_error("softdep_journal_freeblks", error); 6790 return; 6791 } 6792 ip->i_size = length; 6793 DIP_SET(ip, i_size, length); 6794 ip->i_flag |= IN_CHANGE | IN_UPDATE; 6795 allocbuf(bp, frags); 6796 ffs_update(vp, 0); 6797 bawrite(bp); 6798 } else if (lastoff != 0 && vp->v_type != VDIR) { 6799 int size; 6800 6801 /* 6802 * Zero the end of a truncated frag or block. 6803 */ 6804 size = sblksize(fs, length, lastlbn); 6805 error = bread(vp, lastlbn, size, cred, &bp); 6806 if (error) { 6807 softdep_error("softdep_journal_freeblks", error); 6808 return; 6809 } 6810 bzero((char *)bp->b_data + lastoff, size - lastoff); 6811 bawrite(bp); 6812 6813 } 6814 ACQUIRE_LOCK(ump); 6815 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6816 TAILQ_INSERT_TAIL(&inodedep->id_freeblklst, freeblks, fb_next); 6817 freeblks->fb_state |= DEPCOMPLETE | ONDEPLIST; 6818 /* 6819 * We zero earlier truncations so they don't erroneously 6820 * update i_blocks. 6821 */ 6822 if (freeblks->fb_len == 0 && (flags & IO_NORMAL) != 0) 6823 TAILQ_FOREACH(fbn, &inodedep->id_freeblklst, fb_next) 6824 fbn->fb_len = 0; 6825 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE && 6826 LIST_EMPTY(&freeblks->fb_jblkdephd)) 6827 freeblks->fb_state |= INPROGRESS; 6828 else 6829 freeblks = NULL; 6830 FREE_LOCK(ump); 6831 if (freeblks) 6832 handle_workitem_freeblocks(freeblks, 0); 6833 trunc_pages(ip, length, extblocks, flags); 6834 6835 } 6836 6837 /* 6838 * Flush a JOP_SYNC to the journal. 6839 */ 6840 void 6841 softdep_journal_fsync(ip) 6842 struct inode *ip; 6843 { 6844 struct jfsync *jfsync; 6845 struct ufsmount *ump; 6846 6847 ump = ITOUMP(ip); 6848 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 6849 ("softdep_journal_fsync called on non-softdep filesystem")); 6850 if ((ip->i_flag & IN_TRUNCATED) == 0) 6851 return; 6852 ip->i_flag &= ~IN_TRUNCATED; 6853 jfsync = malloc(sizeof(*jfsync), M_JFSYNC, M_SOFTDEP_FLAGS | M_ZERO); 6854 workitem_alloc(&jfsync->jfs_list, D_JFSYNC, UFSTOVFS(ump)); 6855 jfsync->jfs_size = ip->i_size; 6856 jfsync->jfs_ino = ip->i_number; 6857 ACQUIRE_LOCK(ump); 6858 add_to_journal(&jfsync->jfs_list); 6859 jwait(&jfsync->jfs_list, MNT_WAIT); 6860 FREE_LOCK(ump); 6861 } 6862 6863 /* 6864 * Block de-allocation dependencies. 6865 * 6866 * When blocks are de-allocated, the on-disk pointers must be nullified before 6867 * the blocks are made available for use by other files. (The true 6868 * requirement is that old pointers must be nullified before new on-disk 6869 * pointers are set. We chose this slightly more stringent requirement to 6870 * reduce complexity.) Our implementation handles this dependency by updating 6871 * the inode (or indirect block) appropriately but delaying the actual block 6872 * de-allocation (i.e., freemap and free space count manipulation) until 6873 * after the updated versions reach stable storage. After the disk is 6874 * updated, the blocks can be safely de-allocated whenever it is convenient. 6875 * This implementation handles only the common case of reducing a file's 6876 * length to zero. Other cases are handled by the conventional synchronous 6877 * write approach. 6878 * 6879 * The ffs implementation with which we worked double-checks 6880 * the state of the block pointers and file size as it reduces 6881 * a file's length. Some of this code is replicated here in our 6882 * soft updates implementation. The freeblks->fb_chkcnt field is 6883 * used to transfer a part of this information to the procedure 6884 * that eventually de-allocates the blocks. 6885 * 6886 * This routine should be called from the routine that shortens 6887 * a file's length, before the inode's size or block pointers 6888 * are modified. It will save the block pointer information for 6889 * later release and zero the inode so that the calling routine 6890 * can release it. 6891 */ 6892 void 6893 softdep_setup_freeblocks(ip, length, flags) 6894 struct inode *ip; /* The inode whose length is to be reduced */ 6895 off_t length; /* The new length for the file */ 6896 int flags; /* IO_EXT and/or IO_NORMAL */ 6897 { 6898 struct ufs1_dinode *dp1; 6899 struct ufs2_dinode *dp2; 6900 struct freeblks *freeblks; 6901 struct inodedep *inodedep; 6902 struct allocdirect *adp; 6903 struct ufsmount *ump; 6904 struct buf *bp; 6905 struct fs *fs; 6906 ufs2_daddr_t extblocks, datablocks; 6907 struct mount *mp; 6908 int i, delay, error; 6909 ufs_lbn_t tmpval; 6910 ufs_lbn_t lbn; 6911 6912 ump = ITOUMP(ip); 6913 mp = UFSTOVFS(ump); 6914 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6915 ("softdep_setup_freeblocks called on non-softdep filesystem")); 6916 CTR2(KTR_SUJ, "softdep_setup_freeblks: ip %d length %ld", 6917 ip->i_number, length); 6918 KASSERT(length == 0, ("softdep_setup_freeblocks: non-zero length")); 6919 fs = ump->um_fs; 6920 if ((error = bread(ump->um_devvp, 6921 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 6922 (int)fs->fs_bsize, NOCRED, &bp)) != 0) { 6923 brelse(bp); 6924 softdep_error("softdep_setup_freeblocks", error); 6925 return; 6926 } 6927 freeblks = newfreeblks(mp, ip); 6928 extblocks = 0; 6929 datablocks = 0; 6930 if (fs->fs_magic == FS_UFS2_MAGIC) 6931 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 6932 if ((flags & IO_NORMAL) != 0) { 6933 for (i = 0; i < UFS_NDADDR; i++) 6934 setup_freedirect(freeblks, ip, i, 0); 6935 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR; 6936 i < UFS_NIADDR; 6937 i++, lbn += tmpval, tmpval *= NINDIR(fs)) 6938 setup_freeindir(freeblks, ip, i, -lbn -i, 0); 6939 ip->i_size = 0; 6940 DIP_SET(ip, i_size, 0); 6941 datablocks = DIP(ip, i_blocks) - extblocks; 6942 } 6943 if ((flags & IO_EXT) != 0) { 6944 for (i = 0; i < UFS_NXADDR; i++) 6945 setup_freeext(freeblks, ip, i, 0); 6946 ip->i_din2->di_extsize = 0; 6947 datablocks += extblocks; 6948 } 6949 #ifdef QUOTA 6950 /* Reference the quotas in case the block count is wrong in the end. */ 6951 quotaref(ITOV(ip), freeblks->fb_quota); 6952 (void) chkdq(ip, -datablocks, NOCRED, 0); 6953 #endif 6954 freeblks->fb_chkcnt = -datablocks; 6955 UFS_LOCK(ump); 6956 fs->fs_pendingblocks += datablocks; 6957 UFS_UNLOCK(ump); 6958 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 6959 /* 6960 * Push the zero'ed inode to its disk buffer so that we are free 6961 * to delete its dependencies below. Once the dependencies are gone 6962 * the buffer can be safely released. 6963 */ 6964 if (ump->um_fstype == UFS1) { 6965 dp1 = ((struct ufs1_dinode *)bp->b_data + 6966 ino_to_fsbo(fs, ip->i_number)); 6967 ip->i_din1->di_freelink = dp1->di_freelink; 6968 *dp1 = *ip->i_din1; 6969 } else { 6970 dp2 = ((struct ufs2_dinode *)bp->b_data + 6971 ino_to_fsbo(fs, ip->i_number)); 6972 ip->i_din2->di_freelink = dp2->di_freelink; 6973 ffs_update_dinode_ckhash(fs, ip->i_din2); 6974 *dp2 = *ip->i_din2; 6975 } 6976 /* 6977 * Find and eliminate any inode dependencies. 6978 */ 6979 ACQUIRE_LOCK(ump); 6980 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6981 if ((inodedep->id_state & IOSTARTED) != 0) 6982 panic("softdep_setup_freeblocks: inode busy"); 6983 /* 6984 * Add the freeblks structure to the list of operations that 6985 * must await the zero'ed inode being written to disk. If we 6986 * still have a bitmap dependency (delay == 0), then the inode 6987 * has never been written to disk, so we can process the 6988 * freeblks below once we have deleted the dependencies. 6989 */ 6990 delay = (inodedep->id_state & DEPCOMPLETE); 6991 if (delay) 6992 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 6993 else 6994 freeblks->fb_state |= COMPLETE; 6995 /* 6996 * Because the file length has been truncated to zero, any 6997 * pending block allocation dependency structures associated 6998 * with this inode are obsolete and can simply be de-allocated. 6999 * We must first merge the two dependency lists to get rid of 7000 * any duplicate freefrag structures, then purge the merged list. 7001 * If we still have a bitmap dependency, then the inode has never 7002 * been written to disk, so we can free any fragments without delay. 7003 */ 7004 if (flags & IO_NORMAL) { 7005 merge_inode_lists(&inodedep->id_newinoupdt, 7006 &inodedep->id_inoupdt); 7007 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 7008 cancel_allocdirect(&inodedep->id_inoupdt, adp, 7009 freeblks); 7010 } 7011 if (flags & IO_EXT) { 7012 merge_inode_lists(&inodedep->id_newextupdt, 7013 &inodedep->id_extupdt); 7014 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 7015 cancel_allocdirect(&inodedep->id_extupdt, adp, 7016 freeblks); 7017 } 7018 FREE_LOCK(ump); 7019 bdwrite(bp); 7020 trunc_dependencies(ip, freeblks, -1, 0, flags); 7021 ACQUIRE_LOCK(ump); 7022 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 7023 (void) free_inodedep(inodedep); 7024 freeblks->fb_state |= DEPCOMPLETE; 7025 /* 7026 * If the inode with zeroed block pointers is now on disk 7027 * we can start freeing blocks. 7028 */ 7029 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 7030 freeblks->fb_state |= INPROGRESS; 7031 else 7032 freeblks = NULL; 7033 FREE_LOCK(ump); 7034 if (freeblks) 7035 handle_workitem_freeblocks(freeblks, 0); 7036 trunc_pages(ip, length, extblocks, flags); 7037 } 7038 7039 /* 7040 * Eliminate pages from the page cache that back parts of this inode and 7041 * adjust the vnode pager's idea of our size. This prevents stale data 7042 * from hanging around in the page cache. 7043 */ 7044 static void 7045 trunc_pages(ip, length, extblocks, flags) 7046 struct inode *ip; 7047 off_t length; 7048 ufs2_daddr_t extblocks; 7049 int flags; 7050 { 7051 struct vnode *vp; 7052 struct fs *fs; 7053 ufs_lbn_t lbn; 7054 off_t end, extend; 7055 7056 vp = ITOV(ip); 7057 fs = ITOFS(ip); 7058 extend = OFF_TO_IDX(lblktosize(fs, -extblocks)); 7059 if ((flags & IO_EXT) != 0) 7060 vn_pages_remove(vp, extend, 0); 7061 if ((flags & IO_NORMAL) == 0) 7062 return; 7063 BO_LOCK(&vp->v_bufobj); 7064 drain_output(vp); 7065 BO_UNLOCK(&vp->v_bufobj); 7066 /* 7067 * The vnode pager eliminates file pages we eliminate indirects 7068 * below. 7069 */ 7070 vnode_pager_setsize(vp, length); 7071 /* 7072 * Calculate the end based on the last indirect we want to keep. If 7073 * the block extends into indirects we can just use the negative of 7074 * its lbn. Doubles and triples exist at lower numbers so we must 7075 * be careful not to remove those, if they exist. double and triple 7076 * indirect lbns do not overlap with others so it is not important 7077 * to verify how many levels are required. 7078 */ 7079 lbn = lblkno(fs, length); 7080 if (lbn >= UFS_NDADDR) { 7081 /* Calculate the virtual lbn of the triple indirect. */ 7082 lbn = -lbn - (UFS_NIADDR - 1); 7083 end = OFF_TO_IDX(lblktosize(fs, lbn)); 7084 } else 7085 end = extend; 7086 vn_pages_remove(vp, OFF_TO_IDX(OFF_MAX), end); 7087 } 7088 7089 /* 7090 * See if the buf bp is in the range eliminated by truncation. 7091 */ 7092 static int 7093 trunc_check_buf(bp, blkoffp, lastlbn, lastoff, flags) 7094 struct buf *bp; 7095 int *blkoffp; 7096 ufs_lbn_t lastlbn; 7097 int lastoff; 7098 int flags; 7099 { 7100 ufs_lbn_t lbn; 7101 7102 *blkoffp = 0; 7103 /* Only match ext/normal blocks as appropriate. */ 7104 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) || 7105 ((flags & IO_NORMAL) == 0 && (bp->b_xflags & BX_ALTDATA) == 0)) 7106 return (0); 7107 /* ALTDATA is always a full truncation. */ 7108 if ((bp->b_xflags & BX_ALTDATA) != 0) 7109 return (1); 7110 /* -1 is full truncation. */ 7111 if (lastlbn == -1) 7112 return (1); 7113 /* 7114 * If this is a partial truncate we only want those 7115 * blocks and indirect blocks that cover the range 7116 * we're after. 7117 */ 7118 lbn = bp->b_lblkno; 7119 if (lbn < 0) 7120 lbn = -(lbn + lbn_level(lbn)); 7121 if (lbn < lastlbn) 7122 return (0); 7123 /* Here we only truncate lblkno if it's partial. */ 7124 if (lbn == lastlbn) { 7125 if (lastoff == 0) 7126 return (0); 7127 *blkoffp = lastoff; 7128 } 7129 return (1); 7130 } 7131 7132 /* 7133 * Eliminate any dependencies that exist in memory beyond lblkno:off 7134 */ 7135 static void 7136 trunc_dependencies(ip, freeblks, lastlbn, lastoff, flags) 7137 struct inode *ip; 7138 struct freeblks *freeblks; 7139 ufs_lbn_t lastlbn; 7140 int lastoff; 7141 int flags; 7142 { 7143 struct bufobj *bo; 7144 struct vnode *vp; 7145 struct buf *bp; 7146 int blkoff; 7147 7148 /* 7149 * We must wait for any I/O in progress to finish so that 7150 * all potential buffers on the dirty list will be visible. 7151 * Once they are all there, walk the list and get rid of 7152 * any dependencies. 7153 */ 7154 vp = ITOV(ip); 7155 bo = &vp->v_bufobj; 7156 BO_LOCK(bo); 7157 drain_output(vp); 7158 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) 7159 bp->b_vflags &= ~BV_SCANNED; 7160 restart: 7161 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) { 7162 if (bp->b_vflags & BV_SCANNED) 7163 continue; 7164 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 7165 bp->b_vflags |= BV_SCANNED; 7166 continue; 7167 } 7168 KASSERT(bp->b_bufobj == bo, ("Wrong object in buffer")); 7169 if ((bp = getdirtybuf(bp, BO_LOCKPTR(bo), MNT_WAIT)) == NULL) 7170 goto restart; 7171 BO_UNLOCK(bo); 7172 if (deallocate_dependencies(bp, freeblks, blkoff)) 7173 bqrelse(bp); 7174 else 7175 brelse(bp); 7176 BO_LOCK(bo); 7177 goto restart; 7178 } 7179 /* 7180 * Now do the work of vtruncbuf while also matching indirect blocks. 7181 */ 7182 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) 7183 bp->b_vflags &= ~BV_SCANNED; 7184 cleanrestart: 7185 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) { 7186 if (bp->b_vflags & BV_SCANNED) 7187 continue; 7188 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 7189 bp->b_vflags |= BV_SCANNED; 7190 continue; 7191 } 7192 if (BUF_LOCK(bp, 7193 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 7194 BO_LOCKPTR(bo)) == ENOLCK) { 7195 BO_LOCK(bo); 7196 goto cleanrestart; 7197 } 7198 bp->b_vflags |= BV_SCANNED; 7199 bremfree(bp); 7200 if (blkoff != 0) { 7201 allocbuf(bp, blkoff); 7202 bqrelse(bp); 7203 } else { 7204 bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF; 7205 brelse(bp); 7206 } 7207 BO_LOCK(bo); 7208 goto cleanrestart; 7209 } 7210 drain_output(vp); 7211 BO_UNLOCK(bo); 7212 } 7213 7214 static int 7215 cancel_pagedep(pagedep, freeblks, blkoff) 7216 struct pagedep *pagedep; 7217 struct freeblks *freeblks; 7218 int blkoff; 7219 { 7220 struct jremref *jremref; 7221 struct jmvref *jmvref; 7222 struct dirrem *dirrem, *tmp; 7223 int i; 7224 7225 /* 7226 * Copy any directory remove dependencies to the list 7227 * to be processed after the freeblks proceeds. If 7228 * directory entry never made it to disk they 7229 * can be dumped directly onto the work list. 7230 */ 7231 LIST_FOREACH_SAFE(dirrem, &pagedep->pd_dirremhd, dm_next, tmp) { 7232 /* Skip this directory removal if it is intended to remain. */ 7233 if (dirrem->dm_offset < blkoff) 7234 continue; 7235 /* 7236 * If there are any dirrems we wait for the journal write 7237 * to complete and then restart the buf scan as the lock 7238 * has been dropped. 7239 */ 7240 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) { 7241 jwait(&jremref->jr_list, MNT_WAIT); 7242 return (ERESTART); 7243 } 7244 LIST_REMOVE(dirrem, dm_next); 7245 dirrem->dm_dirinum = pagedep->pd_ino; 7246 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list); 7247 } 7248 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) { 7249 jwait(&jmvref->jm_list, MNT_WAIT); 7250 return (ERESTART); 7251 } 7252 /* 7253 * When we're partially truncating a pagedep we just want to flush 7254 * journal entries and return. There can not be any adds in the 7255 * truncated portion of the directory and newblk must remain if 7256 * part of the block remains. 7257 */ 7258 if (blkoff != 0) { 7259 struct diradd *dap; 7260 7261 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 7262 if (dap->da_offset > blkoff) 7263 panic("cancel_pagedep: diradd %p off %d > %d", 7264 dap, dap->da_offset, blkoff); 7265 for (i = 0; i < DAHASHSZ; i++) 7266 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) 7267 if (dap->da_offset > blkoff) 7268 panic("cancel_pagedep: diradd %p off %d > %d", 7269 dap, dap->da_offset, blkoff); 7270 return (0); 7271 } 7272 /* 7273 * There should be no directory add dependencies present 7274 * as the directory could not be truncated until all 7275 * children were removed. 7276 */ 7277 KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL, 7278 ("deallocate_dependencies: pendinghd != NULL")); 7279 for (i = 0; i < DAHASHSZ; i++) 7280 KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL, 7281 ("deallocate_dependencies: diraddhd != NULL")); 7282 if ((pagedep->pd_state & NEWBLOCK) != 0) 7283 free_newdirblk(pagedep->pd_newdirblk); 7284 if (free_pagedep(pagedep) == 0) 7285 panic("Failed to free pagedep %p", pagedep); 7286 return (0); 7287 } 7288 7289 /* 7290 * Reclaim any dependency structures from a buffer that is about to 7291 * be reallocated to a new vnode. The buffer must be locked, thus, 7292 * no I/O completion operations can occur while we are manipulating 7293 * its associated dependencies. The mutex is held so that other I/O's 7294 * associated with related dependencies do not occur. 7295 */ 7296 static int 7297 deallocate_dependencies(bp, freeblks, off) 7298 struct buf *bp; 7299 struct freeblks *freeblks; 7300 int off; 7301 { 7302 struct indirdep *indirdep; 7303 struct pagedep *pagedep; 7304 struct worklist *wk, *wkn; 7305 struct ufsmount *ump; 7306 7307 ump = softdep_bp_to_mp(bp); 7308 if (ump == NULL) 7309 goto done; 7310 ACQUIRE_LOCK(ump); 7311 LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) { 7312 switch (wk->wk_type) { 7313 case D_INDIRDEP: 7314 indirdep = WK_INDIRDEP(wk); 7315 if (bp->b_lblkno >= 0 || 7316 bp->b_blkno != indirdep->ir_savebp->b_lblkno) 7317 panic("deallocate_dependencies: not indir"); 7318 cancel_indirdep(indirdep, bp, freeblks); 7319 continue; 7320 7321 case D_PAGEDEP: 7322 pagedep = WK_PAGEDEP(wk); 7323 if (cancel_pagedep(pagedep, freeblks, off)) { 7324 FREE_LOCK(ump); 7325 return (ERESTART); 7326 } 7327 continue; 7328 7329 case D_ALLOCINDIR: 7330 /* 7331 * Simply remove the allocindir, we'll find it via 7332 * the indirdep where we can clear pointers if 7333 * needed. 7334 */ 7335 WORKLIST_REMOVE(wk); 7336 continue; 7337 7338 case D_FREEWORK: 7339 /* 7340 * A truncation is waiting for the zero'd pointers 7341 * to be written. It can be freed when the freeblks 7342 * is journaled. 7343 */ 7344 WORKLIST_REMOVE(wk); 7345 wk->wk_state |= ONDEPLIST; 7346 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 7347 break; 7348 7349 case D_ALLOCDIRECT: 7350 if (off != 0) 7351 continue; 7352 /* FALLTHROUGH */ 7353 default: 7354 panic("deallocate_dependencies: Unexpected type %s", 7355 TYPENAME(wk->wk_type)); 7356 /* NOTREACHED */ 7357 } 7358 } 7359 FREE_LOCK(ump); 7360 done: 7361 /* 7362 * Don't throw away this buf, we were partially truncating and 7363 * some deps may always remain. 7364 */ 7365 if (off) { 7366 allocbuf(bp, off); 7367 bp->b_vflags |= BV_SCANNED; 7368 return (EBUSY); 7369 } 7370 bp->b_flags |= B_INVAL | B_NOCACHE; 7371 7372 return (0); 7373 } 7374 7375 /* 7376 * An allocdirect is being canceled due to a truncate. We must make sure 7377 * the journal entry is released in concert with the blkfree that releases 7378 * the storage. Completed journal entries must not be released until the 7379 * space is no longer pointed to by the inode or in the bitmap. 7380 */ 7381 static void 7382 cancel_allocdirect(adphead, adp, freeblks) 7383 struct allocdirectlst *adphead; 7384 struct allocdirect *adp; 7385 struct freeblks *freeblks; 7386 { 7387 struct freework *freework; 7388 struct newblk *newblk; 7389 struct worklist *wk; 7390 7391 TAILQ_REMOVE(adphead, adp, ad_next); 7392 newblk = (struct newblk *)adp; 7393 freework = NULL; 7394 /* 7395 * Find the correct freework structure. 7396 */ 7397 LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) { 7398 if (wk->wk_type != D_FREEWORK) 7399 continue; 7400 freework = WK_FREEWORK(wk); 7401 if (freework->fw_blkno == newblk->nb_newblkno) 7402 break; 7403 } 7404 if (freework == NULL) 7405 panic("cancel_allocdirect: Freework not found"); 7406 /* 7407 * If a newblk exists at all we still have the journal entry that 7408 * initiated the allocation so we do not need to journal the free. 7409 */ 7410 cancel_jfreeblk(freeblks, freework->fw_blkno); 7411 /* 7412 * If the journal hasn't been written the jnewblk must be passed 7413 * to the call to ffs_blkfree that reclaims the space. We accomplish 7414 * this by linking the journal dependency into the freework to be 7415 * freed when freework_freeblock() is called. If the journal has 7416 * been written we can simply reclaim the journal space when the 7417 * freeblks work is complete. 7418 */ 7419 freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list, 7420 &freeblks->fb_jwork); 7421 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 7422 } 7423 7424 7425 /* 7426 * Cancel a new block allocation. May be an indirect or direct block. We 7427 * remove it from various lists and return any journal record that needs to 7428 * be resolved by the caller. 7429 * 7430 * A special consideration is made for indirects which were never pointed 7431 * at on disk and will never be found once this block is released. 7432 */ 7433 static struct jnewblk * 7434 cancel_newblk(newblk, wk, wkhd) 7435 struct newblk *newblk; 7436 struct worklist *wk; 7437 struct workhead *wkhd; 7438 { 7439 struct jnewblk *jnewblk; 7440 7441 CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno); 7442 7443 newblk->nb_state |= GOINGAWAY; 7444 /* 7445 * Previously we traversed the completedhd on each indirdep 7446 * attached to this newblk to cancel them and gather journal 7447 * work. Since we need only the oldest journal segment and 7448 * the lowest point on the tree will always have the oldest 7449 * journal segment we are free to release the segments 7450 * of any subordinates and may leave the indirdep list to 7451 * indirdep_complete() when this newblk is freed. 7452 */ 7453 if (newblk->nb_state & ONDEPLIST) { 7454 newblk->nb_state &= ~ONDEPLIST; 7455 LIST_REMOVE(newblk, nb_deps); 7456 } 7457 if (newblk->nb_state & ONWORKLIST) 7458 WORKLIST_REMOVE(&newblk->nb_list); 7459 /* 7460 * If the journal entry hasn't been written we save a pointer to 7461 * the dependency that frees it until it is written or the 7462 * superseding operation completes. 7463 */ 7464 jnewblk = newblk->nb_jnewblk; 7465 if (jnewblk != NULL && wk != NULL) { 7466 newblk->nb_jnewblk = NULL; 7467 jnewblk->jn_dep = wk; 7468 } 7469 if (!LIST_EMPTY(&newblk->nb_jwork)) 7470 jwork_move(wkhd, &newblk->nb_jwork); 7471 /* 7472 * When truncating we must free the newdirblk early to remove 7473 * the pagedep from the hash before returning. 7474 */ 7475 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7476 free_newdirblk(WK_NEWDIRBLK(wk)); 7477 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7478 panic("cancel_newblk: extra newdirblk"); 7479 7480 return (jnewblk); 7481 } 7482 7483 /* 7484 * Schedule the freefrag associated with a newblk to be released once 7485 * the pointers are written and the previous block is no longer needed. 7486 */ 7487 static void 7488 newblk_freefrag(newblk) 7489 struct newblk *newblk; 7490 { 7491 struct freefrag *freefrag; 7492 7493 if (newblk->nb_freefrag == NULL) 7494 return; 7495 freefrag = newblk->nb_freefrag; 7496 newblk->nb_freefrag = NULL; 7497 freefrag->ff_state |= COMPLETE; 7498 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 7499 add_to_worklist(&freefrag->ff_list, 0); 7500 } 7501 7502 /* 7503 * Free a newblk. Generate a new freefrag work request if appropriate. 7504 * This must be called after the inode pointer and any direct block pointers 7505 * are valid or fully removed via truncate or frag extension. 7506 */ 7507 static void 7508 free_newblk(newblk) 7509 struct newblk *newblk; 7510 { 7511 struct indirdep *indirdep; 7512 struct worklist *wk; 7513 7514 KASSERT(newblk->nb_jnewblk == NULL, 7515 ("free_newblk: jnewblk %p still attached", newblk->nb_jnewblk)); 7516 KASSERT(newblk->nb_list.wk_type != D_NEWBLK, 7517 ("free_newblk: unclaimed newblk")); 7518 LOCK_OWNED(VFSTOUFS(newblk->nb_list.wk_mp)); 7519 newblk_freefrag(newblk); 7520 if (newblk->nb_state & ONDEPLIST) 7521 LIST_REMOVE(newblk, nb_deps); 7522 if (newblk->nb_state & ONWORKLIST) 7523 WORKLIST_REMOVE(&newblk->nb_list); 7524 LIST_REMOVE(newblk, nb_hash); 7525 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7526 free_newdirblk(WK_NEWDIRBLK(wk)); 7527 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7528 panic("free_newblk: extra newdirblk"); 7529 while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL) 7530 indirdep_complete(indirdep); 7531 handle_jwork(&newblk->nb_jwork); 7532 WORKITEM_FREE(newblk, D_NEWBLK); 7533 } 7534 7535 /* 7536 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep. 7537 * This routine must be called with splbio interrupts blocked. 7538 */ 7539 static void 7540 free_newdirblk(newdirblk) 7541 struct newdirblk *newdirblk; 7542 { 7543 struct pagedep *pagedep; 7544 struct diradd *dap; 7545 struct worklist *wk; 7546 7547 LOCK_OWNED(VFSTOUFS(newdirblk->db_list.wk_mp)); 7548 WORKLIST_REMOVE(&newdirblk->db_list); 7549 /* 7550 * If the pagedep is still linked onto the directory buffer 7551 * dependency chain, then some of the entries on the 7552 * pd_pendinghd list may not be committed to disk yet. In 7553 * this case, we will simply clear the NEWBLOCK flag and 7554 * let the pd_pendinghd list be processed when the pagedep 7555 * is next written. If the pagedep is no longer on the buffer 7556 * dependency chain, then all the entries on the pd_pending 7557 * list are committed to disk and we can free them here. 7558 */ 7559 pagedep = newdirblk->db_pagedep; 7560 pagedep->pd_state &= ~NEWBLOCK; 7561 if ((pagedep->pd_state & ONWORKLIST) == 0) { 7562 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 7563 free_diradd(dap, NULL); 7564 /* 7565 * If no dependencies remain, the pagedep will be freed. 7566 */ 7567 free_pagedep(pagedep); 7568 } 7569 /* Should only ever be one item in the list. */ 7570 while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) { 7571 WORKLIST_REMOVE(wk); 7572 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 7573 } 7574 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 7575 } 7576 7577 /* 7578 * Prepare an inode to be freed. The actual free operation is not 7579 * done until the zero'ed inode has been written to disk. 7580 */ 7581 void 7582 softdep_freefile(pvp, ino, mode) 7583 struct vnode *pvp; 7584 ino_t ino; 7585 int mode; 7586 { 7587 struct inode *ip = VTOI(pvp); 7588 struct inodedep *inodedep; 7589 struct freefile *freefile; 7590 struct freeblks *freeblks; 7591 struct ufsmount *ump; 7592 7593 ump = ITOUMP(ip); 7594 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 7595 ("softdep_freefile called on non-softdep filesystem")); 7596 /* 7597 * This sets up the inode de-allocation dependency. 7598 */ 7599 freefile = malloc(sizeof(struct freefile), 7600 M_FREEFILE, M_SOFTDEP_FLAGS); 7601 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount); 7602 freefile->fx_mode = mode; 7603 freefile->fx_oldinum = ino; 7604 freefile->fx_devvp = ump->um_devvp; 7605 LIST_INIT(&freefile->fx_jwork); 7606 UFS_LOCK(ump); 7607 ump->um_fs->fs_pendinginodes += 1; 7608 UFS_UNLOCK(ump); 7609 7610 /* 7611 * If the inodedep does not exist, then the zero'ed inode has 7612 * been written to disk. If the allocated inode has never been 7613 * written to disk, then the on-disk inode is zero'ed. In either 7614 * case we can free the file immediately. If the journal was 7615 * canceled before being written the inode will never make it to 7616 * disk and we must send the canceled journal entrys to 7617 * ffs_freefile() to be cleared in conjunction with the bitmap. 7618 * Any blocks waiting on the inode to write can be safely freed 7619 * here as it will never been written. 7620 */ 7621 ACQUIRE_LOCK(ump); 7622 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7623 if (inodedep) { 7624 /* 7625 * Clear out freeblks that no longer need to reference 7626 * this inode. 7627 */ 7628 while ((freeblks = 7629 TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) { 7630 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, 7631 fb_next); 7632 freeblks->fb_state &= ~ONDEPLIST; 7633 } 7634 /* 7635 * Remove this inode from the unlinked list. 7636 */ 7637 if (inodedep->id_state & UNLINKED) { 7638 /* 7639 * Save the journal work to be freed with the bitmap 7640 * before we clear UNLINKED. Otherwise it can be lost 7641 * if the inode block is written. 7642 */ 7643 handle_bufwait(inodedep, &freefile->fx_jwork); 7644 clear_unlinked_inodedep(inodedep); 7645 /* 7646 * Re-acquire inodedep as we've dropped the 7647 * per-filesystem lock in clear_unlinked_inodedep(). 7648 */ 7649 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7650 } 7651 } 7652 if (inodedep == NULL || check_inode_unwritten(inodedep)) { 7653 FREE_LOCK(ump); 7654 handle_workitem_freefile(freefile); 7655 return; 7656 } 7657 if ((inodedep->id_state & DEPCOMPLETE) == 0) 7658 inodedep->id_state |= GOINGAWAY; 7659 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list); 7660 FREE_LOCK(ump); 7661 if (ip->i_number == ino) 7662 ip->i_flag |= IN_MODIFIED; 7663 } 7664 7665 /* 7666 * Check to see if an inode has never been written to disk. If 7667 * so free the inodedep and return success, otherwise return failure. 7668 * This routine must be called with splbio interrupts blocked. 7669 * 7670 * If we still have a bitmap dependency, then the inode has never 7671 * been written to disk. Drop the dependency as it is no longer 7672 * necessary since the inode is being deallocated. We set the 7673 * ALLCOMPLETE flags since the bitmap now properly shows that the 7674 * inode is not allocated. Even if the inode is actively being 7675 * written, it has been rolled back to its zero'ed state, so we 7676 * are ensured that a zero inode is what is on the disk. For short 7677 * lived files, this change will usually result in removing all the 7678 * dependencies from the inode so that it can be freed immediately. 7679 */ 7680 static int 7681 check_inode_unwritten(inodedep) 7682 struct inodedep *inodedep; 7683 { 7684 7685 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7686 7687 if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 || 7688 !LIST_EMPTY(&inodedep->id_dirremhd) || 7689 !LIST_EMPTY(&inodedep->id_pendinghd) || 7690 !LIST_EMPTY(&inodedep->id_bufwait) || 7691 !LIST_EMPTY(&inodedep->id_inowait) || 7692 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 7693 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 7694 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 7695 !TAILQ_EMPTY(&inodedep->id_extupdt) || 7696 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 7697 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 7698 inodedep->id_mkdiradd != NULL || 7699 inodedep->id_nlinkdelta != 0) 7700 return (0); 7701 /* 7702 * Another process might be in initiate_write_inodeblock_ufs[12] 7703 * trying to allocate memory without holding "Softdep Lock". 7704 */ 7705 if ((inodedep->id_state & IOSTARTED) != 0 && 7706 inodedep->id_savedino1 == NULL) 7707 return (0); 7708 7709 if (inodedep->id_state & ONDEPLIST) 7710 LIST_REMOVE(inodedep, id_deps); 7711 inodedep->id_state &= ~ONDEPLIST; 7712 inodedep->id_state |= ALLCOMPLETE; 7713 inodedep->id_bmsafemap = NULL; 7714 if (inodedep->id_state & ONWORKLIST) 7715 WORKLIST_REMOVE(&inodedep->id_list); 7716 if (inodedep->id_savedino1 != NULL) { 7717 free(inodedep->id_savedino1, M_SAVEDINO); 7718 inodedep->id_savedino1 = NULL; 7719 } 7720 if (free_inodedep(inodedep) == 0) 7721 panic("check_inode_unwritten: busy inode"); 7722 return (1); 7723 } 7724 7725 static int 7726 check_inodedep_free(inodedep) 7727 struct inodedep *inodedep; 7728 { 7729 7730 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7731 if ((inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE || 7732 !LIST_EMPTY(&inodedep->id_dirremhd) || 7733 !LIST_EMPTY(&inodedep->id_pendinghd) || 7734 !LIST_EMPTY(&inodedep->id_bufwait) || 7735 !LIST_EMPTY(&inodedep->id_inowait) || 7736 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 7737 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 7738 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 7739 !TAILQ_EMPTY(&inodedep->id_extupdt) || 7740 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 7741 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 7742 inodedep->id_mkdiradd != NULL || 7743 inodedep->id_nlinkdelta != 0 || 7744 inodedep->id_savedino1 != NULL) 7745 return (0); 7746 return (1); 7747 } 7748 7749 /* 7750 * Try to free an inodedep structure. Return 1 if it could be freed. 7751 */ 7752 static int 7753 free_inodedep(inodedep) 7754 struct inodedep *inodedep; 7755 { 7756 7757 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7758 if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 || 7759 !check_inodedep_free(inodedep)) 7760 return (0); 7761 if (inodedep->id_state & ONDEPLIST) 7762 LIST_REMOVE(inodedep, id_deps); 7763 LIST_REMOVE(inodedep, id_hash); 7764 WORKITEM_FREE(inodedep, D_INODEDEP); 7765 return (1); 7766 } 7767 7768 /* 7769 * Free the block referenced by a freework structure. The parent freeblks 7770 * structure is released and completed when the final cg bitmap reaches 7771 * the disk. This routine may be freeing a jnewblk which never made it to 7772 * disk in which case we do not have to wait as the operation is undone 7773 * in memory immediately. 7774 */ 7775 static void 7776 freework_freeblock(freework, key) 7777 struct freework *freework; 7778 u_long key; 7779 { 7780 struct freeblks *freeblks; 7781 struct jnewblk *jnewblk; 7782 struct ufsmount *ump; 7783 struct workhead wkhd; 7784 struct fs *fs; 7785 int bsize; 7786 int needj; 7787 7788 ump = VFSTOUFS(freework->fw_list.wk_mp); 7789 LOCK_OWNED(ump); 7790 /* 7791 * Handle partial truncate separately. 7792 */ 7793 if (freework->fw_indir) { 7794 complete_trunc_indir(freework); 7795 return; 7796 } 7797 freeblks = freework->fw_freeblks; 7798 fs = ump->um_fs; 7799 needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0; 7800 bsize = lfragtosize(fs, freework->fw_frags); 7801 LIST_INIT(&wkhd); 7802 /* 7803 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives 7804 * on the indirblk hashtable and prevents premature freeing. 7805 */ 7806 freework->fw_state |= DEPCOMPLETE; 7807 /* 7808 * SUJ needs to wait for the segment referencing freed indirect 7809 * blocks to expire so that we know the checker will not confuse 7810 * a re-allocated indirect block with its old contents. 7811 */ 7812 if (needj && freework->fw_lbn <= -UFS_NDADDR) 7813 indirblk_insert(freework); 7814 /* 7815 * If we are canceling an existing jnewblk pass it to the free 7816 * routine, otherwise pass the freeblk which will ultimately 7817 * release the freeblks. If we're not journaling, we can just 7818 * free the freeblks immediately. 7819 */ 7820 jnewblk = freework->fw_jnewblk; 7821 if (jnewblk != NULL) { 7822 cancel_jnewblk(jnewblk, &wkhd); 7823 needj = 0; 7824 } else if (needj) { 7825 freework->fw_state |= DELAYEDFREE; 7826 freeblks->fb_cgwait++; 7827 WORKLIST_INSERT(&wkhd, &freework->fw_list); 7828 } 7829 FREE_LOCK(ump); 7830 freeblks_free(ump, freeblks, btodb(bsize)); 7831 CTR4(KTR_SUJ, 7832 "freework_freeblock: ino %jd blkno %jd lbn %jd size %d", 7833 freeblks->fb_inum, freework->fw_blkno, freework->fw_lbn, bsize); 7834 ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, bsize, 7835 freeblks->fb_inum, freeblks->fb_vtype, &wkhd, key); 7836 ACQUIRE_LOCK(ump); 7837 /* 7838 * The jnewblk will be discarded and the bits in the map never 7839 * made it to disk. We can immediately free the freeblk. 7840 */ 7841 if (needj == 0) 7842 handle_written_freework(freework); 7843 } 7844 7845 /* 7846 * We enqueue freework items that need processing back on the freeblks and 7847 * add the freeblks to the worklist. This makes it easier to find all work 7848 * required to flush a truncation in process_truncates(). 7849 */ 7850 static void 7851 freework_enqueue(freework) 7852 struct freework *freework; 7853 { 7854 struct freeblks *freeblks; 7855 7856 freeblks = freework->fw_freeblks; 7857 if ((freework->fw_state & INPROGRESS) == 0) 7858 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 7859 if ((freeblks->fb_state & 7860 (ONWORKLIST | INPROGRESS | ALLCOMPLETE)) == ALLCOMPLETE && 7861 LIST_EMPTY(&freeblks->fb_jblkdephd)) 7862 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 7863 } 7864 7865 /* 7866 * Start, continue, or finish the process of freeing an indirect block tree. 7867 * The free operation may be paused at any point with fw_off containing the 7868 * offset to restart from. This enables us to implement some flow control 7869 * for large truncates which may fan out and generate a huge number of 7870 * dependencies. 7871 */ 7872 static void 7873 handle_workitem_indirblk(freework) 7874 struct freework *freework; 7875 { 7876 struct freeblks *freeblks; 7877 struct ufsmount *ump; 7878 struct fs *fs; 7879 7880 freeblks = freework->fw_freeblks; 7881 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7882 fs = ump->um_fs; 7883 if (freework->fw_state & DEPCOMPLETE) { 7884 handle_written_freework(freework); 7885 return; 7886 } 7887 if (freework->fw_off == NINDIR(fs)) { 7888 freework_freeblock(freework, SINGLETON_KEY); 7889 return; 7890 } 7891 freework->fw_state |= INPROGRESS; 7892 FREE_LOCK(ump); 7893 indir_trunc(freework, fsbtodb(fs, freework->fw_blkno), 7894 freework->fw_lbn); 7895 ACQUIRE_LOCK(ump); 7896 } 7897 7898 /* 7899 * Called when a freework structure attached to a cg buf is written. The 7900 * ref on either the parent or the freeblks structure is released and 7901 * the freeblks is added back to the worklist if there is more work to do. 7902 */ 7903 static void 7904 handle_written_freework(freework) 7905 struct freework *freework; 7906 { 7907 struct freeblks *freeblks; 7908 struct freework *parent; 7909 7910 freeblks = freework->fw_freeblks; 7911 parent = freework->fw_parent; 7912 if (freework->fw_state & DELAYEDFREE) 7913 freeblks->fb_cgwait--; 7914 freework->fw_state |= COMPLETE; 7915 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 7916 WORKITEM_FREE(freework, D_FREEWORK); 7917 if (parent) { 7918 if (--parent->fw_ref == 0) 7919 freework_enqueue(parent); 7920 return; 7921 } 7922 if (--freeblks->fb_ref != 0) 7923 return; 7924 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) == 7925 ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd)) 7926 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 7927 } 7928 7929 /* 7930 * This workitem routine performs the block de-allocation. 7931 * The workitem is added to the pending list after the updated 7932 * inode block has been written to disk. As mentioned above, 7933 * checks regarding the number of blocks de-allocated (compared 7934 * to the number of blocks allocated for the file) are also 7935 * performed in this function. 7936 */ 7937 static int 7938 handle_workitem_freeblocks(freeblks, flags) 7939 struct freeblks *freeblks; 7940 int flags; 7941 { 7942 struct freework *freework; 7943 struct newblk *newblk; 7944 struct allocindir *aip; 7945 struct ufsmount *ump; 7946 struct worklist *wk; 7947 u_long key; 7948 7949 KASSERT(LIST_EMPTY(&freeblks->fb_jblkdephd), 7950 ("handle_workitem_freeblocks: Journal entries not written.")); 7951 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7952 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum); 7953 ACQUIRE_LOCK(ump); 7954 while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) { 7955 WORKLIST_REMOVE(wk); 7956 switch (wk->wk_type) { 7957 case D_DIRREM: 7958 wk->wk_state |= COMPLETE; 7959 add_to_worklist(wk, 0); 7960 continue; 7961 7962 case D_ALLOCDIRECT: 7963 free_newblk(WK_NEWBLK(wk)); 7964 continue; 7965 7966 case D_ALLOCINDIR: 7967 aip = WK_ALLOCINDIR(wk); 7968 freework = NULL; 7969 if (aip->ai_state & DELAYEDFREE) { 7970 FREE_LOCK(ump); 7971 freework = newfreework(ump, freeblks, NULL, 7972 aip->ai_lbn, aip->ai_newblkno, 7973 ump->um_fs->fs_frag, 0, 0); 7974 ACQUIRE_LOCK(ump); 7975 } 7976 newblk = WK_NEWBLK(wk); 7977 if (newblk->nb_jnewblk) { 7978 freework->fw_jnewblk = newblk->nb_jnewblk; 7979 newblk->nb_jnewblk->jn_dep = &freework->fw_list; 7980 newblk->nb_jnewblk = NULL; 7981 } 7982 free_newblk(newblk); 7983 continue; 7984 7985 case D_FREEWORK: 7986 freework = WK_FREEWORK(wk); 7987 if (freework->fw_lbn <= -UFS_NDADDR) 7988 handle_workitem_indirblk(freework); 7989 else 7990 freework_freeblock(freework, key); 7991 continue; 7992 default: 7993 panic("handle_workitem_freeblocks: Unknown type %s", 7994 TYPENAME(wk->wk_type)); 7995 } 7996 } 7997 if (freeblks->fb_ref != 0) { 7998 freeblks->fb_state &= ~INPROGRESS; 7999 wake_worklist(&freeblks->fb_list); 8000 freeblks = NULL; 8001 } 8002 FREE_LOCK(ump); 8003 ffs_blkrelease_finish(ump, key); 8004 if (freeblks) 8005 return handle_complete_freeblocks(freeblks, flags); 8006 return (0); 8007 } 8008 8009 /* 8010 * Handle completion of block free via truncate. This allows fs_pending 8011 * to track the actual free block count more closely than if we only updated 8012 * it at the end. We must be careful to handle cases where the block count 8013 * on free was incorrect. 8014 */ 8015 static void 8016 freeblks_free(ump, freeblks, blocks) 8017 struct ufsmount *ump; 8018 struct freeblks *freeblks; 8019 int blocks; 8020 { 8021 struct fs *fs; 8022 ufs2_daddr_t remain; 8023 8024 UFS_LOCK(ump); 8025 remain = -freeblks->fb_chkcnt; 8026 freeblks->fb_chkcnt += blocks; 8027 if (remain > 0) { 8028 if (remain < blocks) 8029 blocks = remain; 8030 fs = ump->um_fs; 8031 fs->fs_pendingblocks -= blocks; 8032 } 8033 UFS_UNLOCK(ump); 8034 } 8035 8036 /* 8037 * Once all of the freework workitems are complete we can retire the 8038 * freeblocks dependency and any journal work awaiting completion. This 8039 * can not be called until all other dependencies are stable on disk. 8040 */ 8041 static int 8042 handle_complete_freeblocks(freeblks, flags) 8043 struct freeblks *freeblks; 8044 int flags; 8045 { 8046 struct inodedep *inodedep; 8047 struct inode *ip; 8048 struct vnode *vp; 8049 struct fs *fs; 8050 struct ufsmount *ump; 8051 ufs2_daddr_t spare; 8052 8053 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8054 fs = ump->um_fs; 8055 flags = LK_EXCLUSIVE | flags; 8056 spare = freeblks->fb_chkcnt; 8057 8058 /* 8059 * If we did not release the expected number of blocks we may have 8060 * to adjust the inode block count here. Only do so if it wasn't 8061 * a truncation to zero and the modrev still matches. 8062 */ 8063 if (spare && freeblks->fb_len != 0) { 8064 if (ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_inum, 8065 flags, &vp, FFSV_FORCEINSMQ) != 0) 8066 return (EBUSY); 8067 ip = VTOI(vp); 8068 if (DIP(ip, i_modrev) == freeblks->fb_modrev) { 8069 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare); 8070 ip->i_flag |= IN_CHANGE; 8071 /* 8072 * We must wait so this happens before the 8073 * journal is reclaimed. 8074 */ 8075 ffs_update(vp, 1); 8076 } 8077 vput(vp); 8078 } 8079 if (spare < 0) { 8080 UFS_LOCK(ump); 8081 fs->fs_pendingblocks += spare; 8082 UFS_UNLOCK(ump); 8083 } 8084 #ifdef QUOTA 8085 /* Handle spare. */ 8086 if (spare) 8087 quotaadj(freeblks->fb_quota, ump, -spare); 8088 quotarele(freeblks->fb_quota); 8089 #endif 8090 ACQUIRE_LOCK(ump); 8091 if (freeblks->fb_state & ONDEPLIST) { 8092 inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum, 8093 0, &inodedep); 8094 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next); 8095 freeblks->fb_state &= ~ONDEPLIST; 8096 if (TAILQ_EMPTY(&inodedep->id_freeblklst)) 8097 free_inodedep(inodedep); 8098 } 8099 /* 8100 * All of the freeblock deps must be complete prior to this call 8101 * so it's now safe to complete earlier outstanding journal entries. 8102 */ 8103 handle_jwork(&freeblks->fb_jwork); 8104 WORKITEM_FREE(freeblks, D_FREEBLKS); 8105 FREE_LOCK(ump); 8106 return (0); 8107 } 8108 8109 /* 8110 * Release blocks associated with the freeblks and stored in the indirect 8111 * block dbn. If level is greater than SINGLE, the block is an indirect block 8112 * and recursive calls to indirtrunc must be used to cleanse other indirect 8113 * blocks. 8114 * 8115 * This handles partial and complete truncation of blocks. Partial is noted 8116 * with goingaway == 0. In this case the freework is completed after the 8117 * zero'd indirects are written to disk. For full truncation the freework 8118 * is completed after the block is freed. 8119 */ 8120 static void 8121 indir_trunc(freework, dbn, lbn) 8122 struct freework *freework; 8123 ufs2_daddr_t dbn; 8124 ufs_lbn_t lbn; 8125 { 8126 struct freework *nfreework; 8127 struct workhead wkhd; 8128 struct freeblks *freeblks; 8129 struct buf *bp; 8130 struct fs *fs; 8131 struct indirdep *indirdep; 8132 struct ufsmount *ump; 8133 ufs1_daddr_t *bap1; 8134 ufs2_daddr_t nb, nnb, *bap2; 8135 ufs_lbn_t lbnadd, nlbn; 8136 u_long key; 8137 int nblocks, ufs1fmt, freedblocks; 8138 int goingaway, freedeps, needj, level, cnt, i; 8139 8140 freeblks = freework->fw_freeblks; 8141 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8142 fs = ump->um_fs; 8143 /* 8144 * Get buffer of block pointers to be freed. There are three cases: 8145 * 8146 * 1) Partial truncate caches the indirdep pointer in the freework 8147 * which provides us a back copy to the save bp which holds the 8148 * pointers we want to clear. When this completes the zero 8149 * pointers are written to the real copy. 8150 * 2) The indirect is being completely truncated, cancel_indirdep() 8151 * eliminated the real copy and placed the indirdep on the saved 8152 * copy. The indirdep and buf are discarded when this completes. 8153 * 3) The indirect was not in memory, we read a copy off of the disk 8154 * using the devvp and drop and invalidate the buffer when we're 8155 * done. 8156 */ 8157 goingaway = 1; 8158 indirdep = NULL; 8159 if (freework->fw_indir != NULL) { 8160 goingaway = 0; 8161 indirdep = freework->fw_indir; 8162 bp = indirdep->ir_savebp; 8163 if (bp == NULL || bp->b_blkno != dbn) 8164 panic("indir_trunc: Bad saved buf %p blkno %jd", 8165 bp, (intmax_t)dbn); 8166 } else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) { 8167 /* 8168 * The lock prevents the buf dep list from changing and 8169 * indirects on devvp should only ever have one dependency. 8170 */ 8171 indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep)); 8172 if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0) 8173 panic("indir_trunc: Bad indirdep %p from buf %p", 8174 indirdep, bp); 8175 } else if (bread(freeblks->fb_devvp, dbn, (int)fs->fs_bsize, 8176 NOCRED, &bp) != 0) { 8177 brelse(bp); 8178 return; 8179 } 8180 ACQUIRE_LOCK(ump); 8181 /* Protects against a race with complete_trunc_indir(). */ 8182 freework->fw_state &= ~INPROGRESS; 8183 /* 8184 * If we have an indirdep we need to enforce the truncation order 8185 * and discard it when it is complete. 8186 */ 8187 if (indirdep) { 8188 if (freework != TAILQ_FIRST(&indirdep->ir_trunc) && 8189 !TAILQ_EMPTY(&indirdep->ir_trunc)) { 8190 /* 8191 * Add the complete truncate to the list on the 8192 * indirdep to enforce in-order processing. 8193 */ 8194 if (freework->fw_indir == NULL) 8195 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, 8196 freework, fw_next); 8197 FREE_LOCK(ump); 8198 return; 8199 } 8200 /* 8201 * If we're goingaway, free the indirdep. Otherwise it will 8202 * linger until the write completes. 8203 */ 8204 if (goingaway) 8205 free_indirdep(indirdep); 8206 } 8207 FREE_LOCK(ump); 8208 /* Initialize pointers depending on block size. */ 8209 if (ump->um_fstype == UFS1) { 8210 bap1 = (ufs1_daddr_t *)bp->b_data; 8211 nb = bap1[freework->fw_off]; 8212 ufs1fmt = 1; 8213 bap2 = NULL; 8214 } else { 8215 bap2 = (ufs2_daddr_t *)bp->b_data; 8216 nb = bap2[freework->fw_off]; 8217 ufs1fmt = 0; 8218 bap1 = NULL; 8219 } 8220 level = lbn_level(lbn); 8221 needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0; 8222 lbnadd = lbn_offset(fs, level); 8223 nblocks = btodb(fs->fs_bsize); 8224 nfreework = freework; 8225 freedeps = 0; 8226 cnt = 0; 8227 /* 8228 * Reclaim blocks. Traverses into nested indirect levels and 8229 * arranges for the current level to be freed when subordinates 8230 * are free when journaling. 8231 */ 8232 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum); 8233 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) { 8234 if (i != NINDIR(fs) - 1) { 8235 if (ufs1fmt) 8236 nnb = bap1[i+1]; 8237 else 8238 nnb = bap2[i+1]; 8239 } else 8240 nnb = 0; 8241 if (nb == 0) 8242 continue; 8243 cnt++; 8244 if (level != 0) { 8245 nlbn = (lbn + 1) - (i * lbnadd); 8246 if (needj != 0) { 8247 nfreework = newfreework(ump, freeblks, freework, 8248 nlbn, nb, fs->fs_frag, 0, 0); 8249 freedeps++; 8250 } 8251 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn); 8252 } else { 8253 struct freedep *freedep; 8254 8255 /* 8256 * Attempt to aggregate freedep dependencies for 8257 * all blocks being released to the same CG. 8258 */ 8259 LIST_INIT(&wkhd); 8260 if (needj != 0 && 8261 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) { 8262 freedep = newfreedep(freework); 8263 WORKLIST_INSERT_UNLOCKED(&wkhd, 8264 &freedep->fd_list); 8265 freedeps++; 8266 } 8267 CTR3(KTR_SUJ, 8268 "indir_trunc: ino %jd blkno %jd size %d", 8269 freeblks->fb_inum, nb, fs->fs_bsize); 8270 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb, 8271 fs->fs_bsize, freeblks->fb_inum, 8272 freeblks->fb_vtype, &wkhd, key); 8273 } 8274 } 8275 ffs_blkrelease_finish(ump, key); 8276 if (goingaway) { 8277 bp->b_flags |= B_INVAL | B_NOCACHE; 8278 brelse(bp); 8279 } 8280 freedblocks = 0; 8281 if (level == 0) 8282 freedblocks = (nblocks * cnt); 8283 if (needj == 0) 8284 freedblocks += nblocks; 8285 freeblks_free(ump, freeblks, freedblocks); 8286 /* 8287 * If we are journaling set up the ref counts and offset so this 8288 * indirect can be completed when its children are free. 8289 */ 8290 if (needj) { 8291 ACQUIRE_LOCK(ump); 8292 freework->fw_off = i; 8293 freework->fw_ref += freedeps; 8294 freework->fw_ref -= NINDIR(fs) + 1; 8295 if (level == 0) 8296 freeblks->fb_cgwait += freedeps; 8297 if (freework->fw_ref == 0) 8298 freework_freeblock(freework, SINGLETON_KEY); 8299 FREE_LOCK(ump); 8300 return; 8301 } 8302 /* 8303 * If we're not journaling we can free the indirect now. 8304 */ 8305 dbn = dbtofsb(fs, dbn); 8306 CTR3(KTR_SUJ, 8307 "indir_trunc 2: ino %jd blkno %jd size %d", 8308 freeblks->fb_inum, dbn, fs->fs_bsize); 8309 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize, 8310 freeblks->fb_inum, freeblks->fb_vtype, NULL, SINGLETON_KEY); 8311 /* Non SUJ softdep does single-threaded truncations. */ 8312 if (freework->fw_blkno == dbn) { 8313 freework->fw_state |= ALLCOMPLETE; 8314 ACQUIRE_LOCK(ump); 8315 handle_written_freework(freework); 8316 FREE_LOCK(ump); 8317 } 8318 return; 8319 } 8320 8321 /* 8322 * Cancel an allocindir when it is removed via truncation. When bp is not 8323 * NULL the indirect never appeared on disk and is scheduled to be freed 8324 * independently of the indir so we can more easily track journal work. 8325 */ 8326 static void 8327 cancel_allocindir(aip, bp, freeblks, trunc) 8328 struct allocindir *aip; 8329 struct buf *bp; 8330 struct freeblks *freeblks; 8331 int trunc; 8332 { 8333 struct indirdep *indirdep; 8334 struct freefrag *freefrag; 8335 struct newblk *newblk; 8336 8337 newblk = (struct newblk *)aip; 8338 LIST_REMOVE(aip, ai_next); 8339 /* 8340 * We must eliminate the pointer in bp if it must be freed on its 8341 * own due to partial truncate or pending journal work. 8342 */ 8343 if (bp && (trunc || newblk->nb_jnewblk)) { 8344 /* 8345 * Clear the pointer and mark the aip to be freed 8346 * directly if it never existed on disk. 8347 */ 8348 aip->ai_state |= DELAYEDFREE; 8349 indirdep = aip->ai_indirdep; 8350 if (indirdep->ir_state & UFS1FMT) 8351 ((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8352 else 8353 ((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8354 } 8355 /* 8356 * When truncating the previous pointer will be freed via 8357 * savedbp. Eliminate the freefrag which would dup free. 8358 */ 8359 if (trunc && (freefrag = newblk->nb_freefrag) != NULL) { 8360 newblk->nb_freefrag = NULL; 8361 if (freefrag->ff_jdep) 8362 cancel_jfreefrag( 8363 WK_JFREEFRAG(freefrag->ff_jdep)); 8364 jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork); 8365 WORKITEM_FREE(freefrag, D_FREEFRAG); 8366 } 8367 /* 8368 * If the journal hasn't been written the jnewblk must be passed 8369 * to the call to ffs_blkfree that reclaims the space. We accomplish 8370 * this by leaving the journal dependency on the newblk to be freed 8371 * when a freework is created in handle_workitem_freeblocks(). 8372 */ 8373 cancel_newblk(newblk, NULL, &freeblks->fb_jwork); 8374 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 8375 } 8376 8377 /* 8378 * Create the mkdir dependencies for . and .. in a new directory. Link them 8379 * in to a newdirblk so any subsequent additions are tracked properly. The 8380 * caller is responsible for adding the mkdir1 dependency to the journal 8381 * and updating id_mkdiradd. This function returns with the per-filesystem 8382 * lock held. 8383 */ 8384 static struct mkdir * 8385 setup_newdir(dap, newinum, dinum, newdirbp, mkdirp) 8386 struct diradd *dap; 8387 ino_t newinum; 8388 ino_t dinum; 8389 struct buf *newdirbp; 8390 struct mkdir **mkdirp; 8391 { 8392 struct newblk *newblk; 8393 struct pagedep *pagedep; 8394 struct inodedep *inodedep; 8395 struct newdirblk *newdirblk; 8396 struct mkdir *mkdir1, *mkdir2; 8397 struct worklist *wk; 8398 struct jaddref *jaddref; 8399 struct ufsmount *ump; 8400 struct mount *mp; 8401 8402 mp = dap->da_list.wk_mp; 8403 ump = VFSTOUFS(mp); 8404 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK, 8405 M_SOFTDEP_FLAGS); 8406 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8407 LIST_INIT(&newdirblk->db_mkdir); 8408 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8409 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp); 8410 mkdir1->md_state = ATTACHED | MKDIR_BODY; 8411 mkdir1->md_diradd = dap; 8412 mkdir1->md_jaddref = NULL; 8413 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8414 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp); 8415 mkdir2->md_state = ATTACHED | MKDIR_PARENT; 8416 mkdir2->md_diradd = dap; 8417 mkdir2->md_jaddref = NULL; 8418 if (MOUNTEDSUJ(mp) == 0) { 8419 mkdir1->md_state |= DEPCOMPLETE; 8420 mkdir2->md_state |= DEPCOMPLETE; 8421 } 8422 /* 8423 * Dependency on "." and ".." being written to disk. 8424 */ 8425 mkdir1->md_buf = newdirbp; 8426 ACQUIRE_LOCK(VFSTOUFS(mp)); 8427 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir1, md_mkdirs); 8428 /* 8429 * We must link the pagedep, allocdirect, and newdirblk for 8430 * the initial file page so the pointer to the new directory 8431 * is not written until the directory contents are live and 8432 * any subsequent additions are not marked live until the 8433 * block is reachable via the inode. 8434 */ 8435 if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0) 8436 panic("setup_newdir: lost pagedep"); 8437 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list) 8438 if (wk->wk_type == D_ALLOCDIRECT) 8439 break; 8440 if (wk == NULL) 8441 panic("setup_newdir: lost allocdirect"); 8442 if (pagedep->pd_state & NEWBLOCK) 8443 panic("setup_newdir: NEWBLOCK already set"); 8444 newblk = WK_NEWBLK(wk); 8445 pagedep->pd_state |= NEWBLOCK; 8446 pagedep->pd_newdirblk = newdirblk; 8447 newdirblk->db_pagedep = pagedep; 8448 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8449 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list); 8450 /* 8451 * Look up the inodedep for the parent directory so that we 8452 * can link mkdir2 into the pending dotdot jaddref or 8453 * the inode write if there is none. If the inode is 8454 * ALLCOMPLETE and no jaddref is present all dependencies have 8455 * been satisfied and mkdir2 can be freed. 8456 */ 8457 inodedep_lookup(mp, dinum, 0, &inodedep); 8458 if (MOUNTEDSUJ(mp)) { 8459 if (inodedep == NULL) 8460 panic("setup_newdir: Lost parent."); 8461 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8462 inoreflst); 8463 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum && 8464 (jaddref->ja_state & MKDIR_PARENT), 8465 ("setup_newdir: bad dotdot jaddref %p", jaddref)); 8466 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8467 mkdir2->md_jaddref = jaddref; 8468 jaddref->ja_mkdir = mkdir2; 8469 } else if (inodedep == NULL || 8470 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 8471 dap->da_state &= ~MKDIR_PARENT; 8472 WORKITEM_FREE(mkdir2, D_MKDIR); 8473 mkdir2 = NULL; 8474 } else { 8475 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8476 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list); 8477 } 8478 *mkdirp = mkdir2; 8479 8480 return (mkdir1); 8481 } 8482 8483 /* 8484 * Directory entry addition dependencies. 8485 * 8486 * When adding a new directory entry, the inode (with its incremented link 8487 * count) must be written to disk before the directory entry's pointer to it. 8488 * Also, if the inode is newly allocated, the corresponding freemap must be 8489 * updated (on disk) before the directory entry's pointer. These requirements 8490 * are met via undo/redo on the directory entry's pointer, which consists 8491 * simply of the inode number. 8492 * 8493 * As directory entries are added and deleted, the free space within a 8494 * directory block can become fragmented. The ufs filesystem will compact 8495 * a fragmented directory block to make space for a new entry. When this 8496 * occurs, the offsets of previously added entries change. Any "diradd" 8497 * dependency structures corresponding to these entries must be updated with 8498 * the new offsets. 8499 */ 8500 8501 /* 8502 * This routine is called after the in-memory inode's link 8503 * count has been incremented, but before the directory entry's 8504 * pointer to the inode has been set. 8505 */ 8506 int 8507 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 8508 struct buf *bp; /* buffer containing directory block */ 8509 struct inode *dp; /* inode for directory */ 8510 off_t diroffset; /* offset of new entry in directory */ 8511 ino_t newinum; /* inode referenced by new directory entry */ 8512 struct buf *newdirbp; /* non-NULL => contents of new mkdir */ 8513 int isnewblk; /* entry is in a newly allocated block */ 8514 { 8515 int offset; /* offset of new entry within directory block */ 8516 ufs_lbn_t lbn; /* block in directory containing new entry */ 8517 struct fs *fs; 8518 struct diradd *dap; 8519 struct newblk *newblk; 8520 struct pagedep *pagedep; 8521 struct inodedep *inodedep; 8522 struct newdirblk *newdirblk; 8523 struct mkdir *mkdir1, *mkdir2; 8524 struct jaddref *jaddref; 8525 struct ufsmount *ump; 8526 struct mount *mp; 8527 int isindir; 8528 8529 mp = ITOVFS(dp); 8530 ump = VFSTOUFS(mp); 8531 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 8532 ("softdep_setup_directory_add called on non-softdep filesystem")); 8533 /* 8534 * Whiteouts have no dependencies. 8535 */ 8536 if (newinum == UFS_WINO) { 8537 if (newdirbp != NULL) 8538 bdwrite(newdirbp); 8539 return (0); 8540 } 8541 jaddref = NULL; 8542 mkdir1 = mkdir2 = NULL; 8543 fs = ump->um_fs; 8544 lbn = lblkno(fs, diroffset); 8545 offset = blkoff(fs, diroffset); 8546 dap = malloc(sizeof(struct diradd), M_DIRADD, 8547 M_SOFTDEP_FLAGS|M_ZERO); 8548 workitem_alloc(&dap->da_list, D_DIRADD, mp); 8549 dap->da_offset = offset; 8550 dap->da_newinum = newinum; 8551 dap->da_state = ATTACHED; 8552 LIST_INIT(&dap->da_jwork); 8553 isindir = bp->b_lblkno >= UFS_NDADDR; 8554 newdirblk = NULL; 8555 if (isnewblk && 8556 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) { 8557 newdirblk = malloc(sizeof(struct newdirblk), 8558 M_NEWDIRBLK, M_SOFTDEP_FLAGS); 8559 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8560 LIST_INIT(&newdirblk->db_mkdir); 8561 } 8562 /* 8563 * If we're creating a new directory setup the dependencies and set 8564 * the dap state to wait for them. Otherwise it's COMPLETE and 8565 * we can move on. 8566 */ 8567 if (newdirbp == NULL) { 8568 dap->da_state |= DEPCOMPLETE; 8569 ACQUIRE_LOCK(ump); 8570 } else { 8571 dap->da_state |= MKDIR_BODY | MKDIR_PARENT; 8572 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp, 8573 &mkdir2); 8574 } 8575 /* 8576 * Link into parent directory pagedep to await its being written. 8577 */ 8578 pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep); 8579 #ifdef INVARIANTS 8580 if (diradd_lookup(pagedep, offset) != NULL) 8581 panic("softdep_setup_directory_add: %p already at off %d\n", 8582 diradd_lookup(pagedep, offset), offset); 8583 #endif 8584 dap->da_pagedep = pagedep; 8585 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap, 8586 da_pdlist); 8587 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 8588 /* 8589 * If we're journaling, link the diradd into the jaddref so it 8590 * may be completed after the journal entry is written. Otherwise, 8591 * link the diradd into its inodedep. If the inode is not yet 8592 * written place it on the bufwait list, otherwise do the post-inode 8593 * write processing to put it on the id_pendinghd list. 8594 */ 8595 if (MOUNTEDSUJ(mp)) { 8596 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8597 inoreflst); 8598 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 8599 ("softdep_setup_directory_add: bad jaddref %p", jaddref)); 8600 jaddref->ja_diroff = diroffset; 8601 jaddref->ja_diradd = dap; 8602 add_to_journal(&jaddref->ja_list); 8603 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) 8604 diradd_inode_written(dap, inodedep); 8605 else 8606 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 8607 /* 8608 * Add the journal entries for . and .. links now that the primary 8609 * link is written. 8610 */ 8611 if (mkdir1 != NULL && MOUNTEDSUJ(mp)) { 8612 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 8613 inoreflst, if_deps); 8614 KASSERT(jaddref != NULL && 8615 jaddref->ja_ino == jaddref->ja_parent && 8616 (jaddref->ja_state & MKDIR_BODY), 8617 ("softdep_setup_directory_add: bad dot jaddref %p", 8618 jaddref)); 8619 mkdir1->md_jaddref = jaddref; 8620 jaddref->ja_mkdir = mkdir1; 8621 /* 8622 * It is important that the dotdot journal entry 8623 * is added prior to the dot entry since dot writes 8624 * both the dot and dotdot links. These both must 8625 * be added after the primary link for the journal 8626 * to remain consistent. 8627 */ 8628 add_to_journal(&mkdir2->md_jaddref->ja_list); 8629 add_to_journal(&jaddref->ja_list); 8630 } 8631 /* 8632 * If we are adding a new directory remember this diradd so that if 8633 * we rename it we can keep the dot and dotdot dependencies. If 8634 * we are adding a new name for an inode that has a mkdiradd we 8635 * must be in rename and we have to move the dot and dotdot 8636 * dependencies to this new name. The old name is being orphaned 8637 * soon. 8638 */ 8639 if (mkdir1 != NULL) { 8640 if (inodedep->id_mkdiradd != NULL) 8641 panic("softdep_setup_directory_add: Existing mkdir"); 8642 inodedep->id_mkdiradd = dap; 8643 } else if (inodedep->id_mkdiradd) 8644 merge_diradd(inodedep, dap); 8645 if (newdirblk != NULL) { 8646 /* 8647 * There is nothing to do if we are already tracking 8648 * this block. 8649 */ 8650 if ((pagedep->pd_state & NEWBLOCK) != 0) { 8651 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 8652 FREE_LOCK(ump); 8653 return (0); 8654 } 8655 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk) 8656 == 0) 8657 panic("softdep_setup_directory_add: lost entry"); 8658 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8659 pagedep->pd_state |= NEWBLOCK; 8660 pagedep->pd_newdirblk = newdirblk; 8661 newdirblk->db_pagedep = pagedep; 8662 FREE_LOCK(ump); 8663 /* 8664 * If we extended into an indirect signal direnter to sync. 8665 */ 8666 if (isindir) 8667 return (1); 8668 return (0); 8669 } 8670 FREE_LOCK(ump); 8671 return (0); 8672 } 8673 8674 /* 8675 * This procedure is called to change the offset of a directory 8676 * entry when compacting a directory block which must be owned 8677 * exclusively by the caller. Note that the actual entry movement 8678 * must be done in this procedure to ensure that no I/O completions 8679 * occur while the move is in progress. 8680 */ 8681 void 8682 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 8683 struct buf *bp; /* Buffer holding directory block. */ 8684 struct inode *dp; /* inode for directory */ 8685 caddr_t base; /* address of dp->i_offset */ 8686 caddr_t oldloc; /* address of old directory location */ 8687 caddr_t newloc; /* address of new directory location */ 8688 int entrysize; /* size of directory entry */ 8689 { 8690 int offset, oldoffset, newoffset; 8691 struct pagedep *pagedep; 8692 struct jmvref *jmvref; 8693 struct diradd *dap; 8694 struct direct *de; 8695 struct mount *mp; 8696 struct ufsmount *ump; 8697 ufs_lbn_t lbn; 8698 int flags; 8699 8700 mp = ITOVFS(dp); 8701 ump = VFSTOUFS(mp); 8702 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 8703 ("softdep_change_directoryentry_offset called on " 8704 "non-softdep filesystem")); 8705 de = (struct direct *)oldloc; 8706 jmvref = NULL; 8707 flags = 0; 8708 /* 8709 * Moves are always journaled as it would be too complex to 8710 * determine if any affected adds or removes are present in the 8711 * journal. 8712 */ 8713 if (MOUNTEDSUJ(mp)) { 8714 flags = DEPALLOC; 8715 jmvref = newjmvref(dp, de->d_ino, 8716 dp->i_offset + (oldloc - base), 8717 dp->i_offset + (newloc - base)); 8718 } 8719 lbn = lblkno(ump->um_fs, dp->i_offset); 8720 offset = blkoff(ump->um_fs, dp->i_offset); 8721 oldoffset = offset + (oldloc - base); 8722 newoffset = offset + (newloc - base); 8723 ACQUIRE_LOCK(ump); 8724 if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0) 8725 goto done; 8726 dap = diradd_lookup(pagedep, oldoffset); 8727 if (dap) { 8728 dap->da_offset = newoffset; 8729 newoffset = DIRADDHASH(newoffset); 8730 oldoffset = DIRADDHASH(oldoffset); 8731 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE && 8732 newoffset != oldoffset) { 8733 LIST_REMOVE(dap, da_pdlist); 8734 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset], 8735 dap, da_pdlist); 8736 } 8737 } 8738 done: 8739 if (jmvref) { 8740 jmvref->jm_pagedep = pagedep; 8741 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps); 8742 add_to_journal(&jmvref->jm_list); 8743 } 8744 bcopy(oldloc, newloc, entrysize); 8745 FREE_LOCK(ump); 8746 } 8747 8748 /* 8749 * Move the mkdir dependencies and journal work from one diradd to another 8750 * when renaming a directory. The new name must depend on the mkdir deps 8751 * completing as the old name did. Directories can only have one valid link 8752 * at a time so one must be canonical. 8753 */ 8754 static void 8755 merge_diradd(inodedep, newdap) 8756 struct inodedep *inodedep; 8757 struct diradd *newdap; 8758 { 8759 struct diradd *olddap; 8760 struct mkdir *mkdir, *nextmd; 8761 struct ufsmount *ump; 8762 short state; 8763 8764 olddap = inodedep->id_mkdiradd; 8765 inodedep->id_mkdiradd = newdap; 8766 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8767 newdap->da_state &= ~DEPCOMPLETE; 8768 ump = VFSTOUFS(inodedep->id_list.wk_mp); 8769 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 8770 mkdir = nextmd) { 8771 nextmd = LIST_NEXT(mkdir, md_mkdirs); 8772 if (mkdir->md_diradd != olddap) 8773 continue; 8774 mkdir->md_diradd = newdap; 8775 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY); 8776 newdap->da_state |= state; 8777 olddap->da_state &= ~state; 8778 if ((olddap->da_state & 8779 (MKDIR_PARENT | MKDIR_BODY)) == 0) 8780 break; 8781 } 8782 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 8783 panic("merge_diradd: unfound ref"); 8784 } 8785 /* 8786 * Any mkdir related journal items are not safe to be freed until 8787 * the new name is stable. 8788 */ 8789 jwork_move(&newdap->da_jwork, &olddap->da_jwork); 8790 olddap->da_state |= DEPCOMPLETE; 8791 complete_diradd(olddap); 8792 } 8793 8794 /* 8795 * Move the diradd to the pending list when all diradd dependencies are 8796 * complete. 8797 */ 8798 static void 8799 complete_diradd(dap) 8800 struct diradd *dap; 8801 { 8802 struct pagedep *pagedep; 8803 8804 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 8805 if (dap->da_state & DIRCHG) 8806 pagedep = dap->da_previous->dm_pagedep; 8807 else 8808 pagedep = dap->da_pagedep; 8809 LIST_REMOVE(dap, da_pdlist); 8810 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 8811 } 8812 } 8813 8814 /* 8815 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal 8816 * add entries and conditonally journal the remove. 8817 */ 8818 static void 8819 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref) 8820 struct diradd *dap; 8821 struct dirrem *dirrem; 8822 struct jremref *jremref; 8823 struct jremref *dotremref; 8824 struct jremref *dotdotremref; 8825 { 8826 struct inodedep *inodedep; 8827 struct jaddref *jaddref; 8828 struct inoref *inoref; 8829 struct ufsmount *ump; 8830 struct mkdir *mkdir; 8831 8832 /* 8833 * If no remove references were allocated we're on a non-journaled 8834 * filesystem and can skip the cancel step. 8835 */ 8836 if (jremref == NULL) { 8837 free_diradd(dap, NULL); 8838 return; 8839 } 8840 /* 8841 * Cancel the primary name an free it if it does not require 8842 * journaling. 8843 */ 8844 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum, 8845 0, &inodedep) != 0) { 8846 /* Abort the addref that reference this diradd. */ 8847 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 8848 if (inoref->if_list.wk_type != D_JADDREF) 8849 continue; 8850 jaddref = (struct jaddref *)inoref; 8851 if (jaddref->ja_diradd != dap) 8852 continue; 8853 if (cancel_jaddref(jaddref, inodedep, 8854 &dirrem->dm_jwork) == 0) { 8855 free_jremref(jremref); 8856 jremref = NULL; 8857 } 8858 break; 8859 } 8860 } 8861 /* 8862 * Cancel subordinate names and free them if they do not require 8863 * journaling. 8864 */ 8865 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8866 ump = VFSTOUFS(dap->da_list.wk_mp); 8867 LIST_FOREACH(mkdir, &ump->softdep_mkdirlisthd, md_mkdirs) { 8868 if (mkdir->md_diradd != dap) 8869 continue; 8870 if ((jaddref = mkdir->md_jaddref) == NULL) 8871 continue; 8872 mkdir->md_jaddref = NULL; 8873 if (mkdir->md_state & MKDIR_PARENT) { 8874 if (cancel_jaddref(jaddref, NULL, 8875 &dirrem->dm_jwork) == 0) { 8876 free_jremref(dotdotremref); 8877 dotdotremref = NULL; 8878 } 8879 } else { 8880 if (cancel_jaddref(jaddref, inodedep, 8881 &dirrem->dm_jwork) == 0) { 8882 free_jremref(dotremref); 8883 dotremref = NULL; 8884 } 8885 } 8886 } 8887 } 8888 8889 if (jremref) 8890 journal_jremref(dirrem, jremref, inodedep); 8891 if (dotremref) 8892 journal_jremref(dirrem, dotremref, inodedep); 8893 if (dotdotremref) 8894 journal_jremref(dirrem, dotdotremref, NULL); 8895 jwork_move(&dirrem->dm_jwork, &dap->da_jwork); 8896 free_diradd(dap, &dirrem->dm_jwork); 8897 } 8898 8899 /* 8900 * Free a diradd dependency structure. This routine must be called 8901 * with splbio interrupts blocked. 8902 */ 8903 static void 8904 free_diradd(dap, wkhd) 8905 struct diradd *dap; 8906 struct workhead *wkhd; 8907 { 8908 struct dirrem *dirrem; 8909 struct pagedep *pagedep; 8910 struct inodedep *inodedep; 8911 struct mkdir *mkdir, *nextmd; 8912 struct ufsmount *ump; 8913 8914 ump = VFSTOUFS(dap->da_list.wk_mp); 8915 LOCK_OWNED(ump); 8916 LIST_REMOVE(dap, da_pdlist); 8917 if (dap->da_state & ONWORKLIST) 8918 WORKLIST_REMOVE(&dap->da_list); 8919 if ((dap->da_state & DIRCHG) == 0) { 8920 pagedep = dap->da_pagedep; 8921 } else { 8922 dirrem = dap->da_previous; 8923 pagedep = dirrem->dm_pagedep; 8924 dirrem->dm_dirinum = pagedep->pd_ino; 8925 dirrem->dm_state |= COMPLETE; 8926 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 8927 add_to_worklist(&dirrem->dm_list, 0); 8928 } 8929 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum, 8930 0, &inodedep) != 0) 8931 if (inodedep->id_mkdiradd == dap) 8932 inodedep->id_mkdiradd = NULL; 8933 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8934 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 8935 mkdir = nextmd) { 8936 nextmd = LIST_NEXT(mkdir, md_mkdirs); 8937 if (mkdir->md_diradd != dap) 8938 continue; 8939 dap->da_state &= 8940 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 8941 LIST_REMOVE(mkdir, md_mkdirs); 8942 if (mkdir->md_state & ONWORKLIST) 8943 WORKLIST_REMOVE(&mkdir->md_list); 8944 if (mkdir->md_jaddref != NULL) 8945 panic("free_diradd: Unexpected jaddref"); 8946 WORKITEM_FREE(mkdir, D_MKDIR); 8947 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) 8948 break; 8949 } 8950 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 8951 panic("free_diradd: unfound ref"); 8952 } 8953 if (inodedep) 8954 free_inodedep(inodedep); 8955 /* 8956 * Free any journal segments waiting for the directory write. 8957 */ 8958 handle_jwork(&dap->da_jwork); 8959 WORKITEM_FREE(dap, D_DIRADD); 8960 } 8961 8962 /* 8963 * Directory entry removal dependencies. 8964 * 8965 * When removing a directory entry, the entry's inode pointer must be 8966 * zero'ed on disk before the corresponding inode's link count is decremented 8967 * (possibly freeing the inode for re-use). This dependency is handled by 8968 * updating the directory entry but delaying the inode count reduction until 8969 * after the directory block has been written to disk. After this point, the 8970 * inode count can be decremented whenever it is convenient. 8971 */ 8972 8973 /* 8974 * This routine should be called immediately after removing 8975 * a directory entry. The inode's link count should not be 8976 * decremented by the calling procedure -- the soft updates 8977 * code will do this task when it is safe. 8978 */ 8979 void 8980 softdep_setup_remove(bp, dp, ip, isrmdir) 8981 struct buf *bp; /* buffer containing directory block */ 8982 struct inode *dp; /* inode for the directory being modified */ 8983 struct inode *ip; /* inode for directory entry being removed */ 8984 int isrmdir; /* indicates if doing RMDIR */ 8985 { 8986 struct dirrem *dirrem, *prevdirrem; 8987 struct inodedep *inodedep; 8988 struct ufsmount *ump; 8989 int direct; 8990 8991 ump = ITOUMP(ip); 8992 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 8993 ("softdep_setup_remove called on non-softdep filesystem")); 8994 /* 8995 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want 8996 * newdirrem() to setup the full directory remove which requires 8997 * isrmdir > 1. 8998 */ 8999 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 9000 /* 9001 * Add the dirrem to the inodedep's pending remove list for quick 9002 * discovery later. 9003 */ 9004 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) 9005 panic("softdep_setup_remove: Lost inodedep."); 9006 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 9007 dirrem->dm_state |= ONDEPLIST; 9008 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9009 9010 /* 9011 * If the COMPLETE flag is clear, then there were no active 9012 * entries and we want to roll back to a zeroed entry until 9013 * the new inode is committed to disk. If the COMPLETE flag is 9014 * set then we have deleted an entry that never made it to 9015 * disk. If the entry we deleted resulted from a name change, 9016 * then the old name still resides on disk. We cannot delete 9017 * its inode (returned to us in prevdirrem) until the zeroed 9018 * directory entry gets to disk. The new inode has never been 9019 * referenced on the disk, so can be deleted immediately. 9020 */ 9021 if ((dirrem->dm_state & COMPLETE) == 0) { 9022 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem, 9023 dm_next); 9024 FREE_LOCK(ump); 9025 } else { 9026 if (prevdirrem != NULL) 9027 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, 9028 prevdirrem, dm_next); 9029 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino; 9030 direct = LIST_EMPTY(&dirrem->dm_jremrefhd); 9031 FREE_LOCK(ump); 9032 if (direct) 9033 handle_workitem_remove(dirrem, 0); 9034 } 9035 } 9036 9037 /* 9038 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the 9039 * pd_pendinghd list of a pagedep. 9040 */ 9041 static struct diradd * 9042 diradd_lookup(pagedep, offset) 9043 struct pagedep *pagedep; 9044 int offset; 9045 { 9046 struct diradd *dap; 9047 9048 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist) 9049 if (dap->da_offset == offset) 9050 return (dap); 9051 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 9052 if (dap->da_offset == offset) 9053 return (dap); 9054 return (NULL); 9055 } 9056 9057 /* 9058 * Search for a .. diradd dependency in a directory that is being removed. 9059 * If the directory was renamed to a new parent we have a diradd rather 9060 * than a mkdir for the .. entry. We need to cancel it now before 9061 * it is found in truncate(). 9062 */ 9063 static struct jremref * 9064 cancel_diradd_dotdot(ip, dirrem, jremref) 9065 struct inode *ip; 9066 struct dirrem *dirrem; 9067 struct jremref *jremref; 9068 { 9069 struct pagedep *pagedep; 9070 struct diradd *dap; 9071 struct worklist *wk; 9072 9073 if (pagedep_lookup(ITOVFS(ip), NULL, ip->i_number, 0, 0, &pagedep) == 0) 9074 return (jremref); 9075 dap = diradd_lookup(pagedep, DOTDOT_OFFSET); 9076 if (dap == NULL) 9077 return (jremref); 9078 cancel_diradd(dap, dirrem, jremref, NULL, NULL); 9079 /* 9080 * Mark any journal work as belonging to the parent so it is freed 9081 * with the .. reference. 9082 */ 9083 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9084 wk->wk_state |= MKDIR_PARENT; 9085 return (NULL); 9086 } 9087 9088 /* 9089 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to 9090 * replace it with a dirrem/diradd pair as a result of re-parenting a 9091 * directory. This ensures that we don't simultaneously have a mkdir and 9092 * a diradd for the same .. entry. 9093 */ 9094 static struct jremref * 9095 cancel_mkdir_dotdot(ip, dirrem, jremref) 9096 struct inode *ip; 9097 struct dirrem *dirrem; 9098 struct jremref *jremref; 9099 { 9100 struct inodedep *inodedep; 9101 struct jaddref *jaddref; 9102 struct ufsmount *ump; 9103 struct mkdir *mkdir; 9104 struct diradd *dap; 9105 struct mount *mp; 9106 9107 mp = ITOVFS(ip); 9108 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9109 return (jremref); 9110 dap = inodedep->id_mkdiradd; 9111 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0) 9112 return (jremref); 9113 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9114 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 9115 mkdir = LIST_NEXT(mkdir, md_mkdirs)) 9116 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT) 9117 break; 9118 if (mkdir == NULL) 9119 panic("cancel_mkdir_dotdot: Unable to find mkdir\n"); 9120 if ((jaddref = mkdir->md_jaddref) != NULL) { 9121 mkdir->md_jaddref = NULL; 9122 jaddref->ja_state &= ~MKDIR_PARENT; 9123 if (inodedep_lookup(mp, jaddref->ja_ino, 0, &inodedep) == 0) 9124 panic("cancel_mkdir_dotdot: Lost parent inodedep"); 9125 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) { 9126 journal_jremref(dirrem, jremref, inodedep); 9127 jremref = NULL; 9128 } 9129 } 9130 if (mkdir->md_state & ONWORKLIST) 9131 WORKLIST_REMOVE(&mkdir->md_list); 9132 mkdir->md_state |= ALLCOMPLETE; 9133 complete_mkdir(mkdir); 9134 return (jremref); 9135 } 9136 9137 static void 9138 journal_jremref(dirrem, jremref, inodedep) 9139 struct dirrem *dirrem; 9140 struct jremref *jremref; 9141 struct inodedep *inodedep; 9142 { 9143 9144 if (inodedep == NULL) 9145 if (inodedep_lookup(jremref->jr_list.wk_mp, 9146 jremref->jr_ref.if_ino, 0, &inodedep) == 0) 9147 panic("journal_jremref: Lost inodedep"); 9148 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps); 9149 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 9150 add_to_journal(&jremref->jr_list); 9151 } 9152 9153 static void 9154 dirrem_journal(dirrem, jremref, dotremref, dotdotremref) 9155 struct dirrem *dirrem; 9156 struct jremref *jremref; 9157 struct jremref *dotremref; 9158 struct jremref *dotdotremref; 9159 { 9160 struct inodedep *inodedep; 9161 9162 9163 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0, 9164 &inodedep) == 0) 9165 panic("dirrem_journal: Lost inodedep"); 9166 journal_jremref(dirrem, jremref, inodedep); 9167 if (dotremref) 9168 journal_jremref(dirrem, dotremref, inodedep); 9169 if (dotdotremref) 9170 journal_jremref(dirrem, dotdotremref, NULL); 9171 } 9172 9173 /* 9174 * Allocate a new dirrem if appropriate and return it along with 9175 * its associated pagedep. Called without a lock, returns with lock. 9176 */ 9177 static struct dirrem * 9178 newdirrem(bp, dp, ip, isrmdir, prevdirremp) 9179 struct buf *bp; /* buffer containing directory block */ 9180 struct inode *dp; /* inode for the directory being modified */ 9181 struct inode *ip; /* inode for directory entry being removed */ 9182 int isrmdir; /* indicates if doing RMDIR */ 9183 struct dirrem **prevdirremp; /* previously referenced inode, if any */ 9184 { 9185 int offset; 9186 ufs_lbn_t lbn; 9187 struct diradd *dap; 9188 struct dirrem *dirrem; 9189 struct pagedep *pagedep; 9190 struct jremref *jremref; 9191 struct jremref *dotremref; 9192 struct jremref *dotdotremref; 9193 struct vnode *dvp; 9194 struct ufsmount *ump; 9195 9196 /* 9197 * Whiteouts have no deletion dependencies. 9198 */ 9199 if (ip == NULL) 9200 panic("newdirrem: whiteout"); 9201 dvp = ITOV(dp); 9202 ump = ITOUMP(dp); 9203 9204 /* 9205 * If the system is over its limit and our filesystem is 9206 * responsible for more than our share of that usage and 9207 * we are not a snapshot, request some inodedep cleanup. 9208 * Limiting the number of dirrem structures will also limit 9209 * the number of freefile and freeblks structures. 9210 */ 9211 ACQUIRE_LOCK(ump); 9212 if (!IS_SNAPSHOT(ip) && softdep_excess_items(ump, D_DIRREM)) 9213 schedule_cleanup(UFSTOVFS(ump)); 9214 else 9215 FREE_LOCK(ump); 9216 dirrem = malloc(sizeof(struct dirrem), M_DIRREM, M_SOFTDEP_FLAGS | 9217 M_ZERO); 9218 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount); 9219 LIST_INIT(&dirrem->dm_jremrefhd); 9220 LIST_INIT(&dirrem->dm_jwork); 9221 dirrem->dm_state = isrmdir ? RMDIR : 0; 9222 dirrem->dm_oldinum = ip->i_number; 9223 *prevdirremp = NULL; 9224 /* 9225 * Allocate remove reference structures to track journal write 9226 * dependencies. We will always have one for the link and 9227 * when doing directories we will always have one more for dot. 9228 * When renaming a directory we skip the dotdot link change so 9229 * this is not needed. 9230 */ 9231 jremref = dotremref = dotdotremref = NULL; 9232 if (DOINGSUJ(dvp)) { 9233 if (isrmdir) { 9234 jremref = newjremref(dirrem, dp, ip, dp->i_offset, 9235 ip->i_effnlink + 2); 9236 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET, 9237 ip->i_effnlink + 1); 9238 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET, 9239 dp->i_effnlink + 1); 9240 dotdotremref->jr_state |= MKDIR_PARENT; 9241 } else 9242 jremref = newjremref(dirrem, dp, ip, dp->i_offset, 9243 ip->i_effnlink + 1); 9244 } 9245 ACQUIRE_LOCK(ump); 9246 lbn = lblkno(ump->um_fs, dp->i_offset); 9247 offset = blkoff(ump->um_fs, dp->i_offset); 9248 pagedep_lookup(UFSTOVFS(ump), bp, dp->i_number, lbn, DEPALLOC, 9249 &pagedep); 9250 dirrem->dm_pagedep = pagedep; 9251 dirrem->dm_offset = offset; 9252 /* 9253 * If we're renaming a .. link to a new directory, cancel any 9254 * existing MKDIR_PARENT mkdir. If it has already been canceled 9255 * the jremref is preserved for any potential diradd in this 9256 * location. This can not coincide with a rmdir. 9257 */ 9258 if (dp->i_offset == DOTDOT_OFFSET) { 9259 if (isrmdir) 9260 panic("newdirrem: .. directory change during remove?"); 9261 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref); 9262 } 9263 /* 9264 * If we're removing a directory search for the .. dependency now and 9265 * cancel it. Any pending journal work will be added to the dirrem 9266 * to be completed when the workitem remove completes. 9267 */ 9268 if (isrmdir) 9269 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref); 9270 /* 9271 * Check for a diradd dependency for the same directory entry. 9272 * If present, then both dependencies become obsolete and can 9273 * be de-allocated. 9274 */ 9275 dap = diradd_lookup(pagedep, offset); 9276 if (dap == NULL) { 9277 /* 9278 * Link the jremref structures into the dirrem so they are 9279 * written prior to the pagedep. 9280 */ 9281 if (jremref) 9282 dirrem_journal(dirrem, jremref, dotremref, 9283 dotdotremref); 9284 return (dirrem); 9285 } 9286 /* 9287 * Must be ATTACHED at this point. 9288 */ 9289 if ((dap->da_state & ATTACHED) == 0) 9290 panic("newdirrem: not ATTACHED"); 9291 if (dap->da_newinum != ip->i_number) 9292 panic("newdirrem: inum %ju should be %ju", 9293 (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum); 9294 /* 9295 * If we are deleting a changed name that never made it to disk, 9296 * then return the dirrem describing the previous inode (which 9297 * represents the inode currently referenced from this entry on disk). 9298 */ 9299 if ((dap->da_state & DIRCHG) != 0) { 9300 *prevdirremp = dap->da_previous; 9301 dap->da_state &= ~DIRCHG; 9302 dap->da_pagedep = pagedep; 9303 } 9304 /* 9305 * We are deleting an entry that never made it to disk. 9306 * Mark it COMPLETE so we can delete its inode immediately. 9307 */ 9308 dirrem->dm_state |= COMPLETE; 9309 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref); 9310 #ifdef INVARIANTS 9311 if (isrmdir == 0) { 9312 struct worklist *wk; 9313 9314 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9315 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT)) 9316 panic("bad wk %p (0x%X)\n", wk, wk->wk_state); 9317 } 9318 #endif 9319 9320 return (dirrem); 9321 } 9322 9323 /* 9324 * Directory entry change dependencies. 9325 * 9326 * Changing an existing directory entry requires that an add operation 9327 * be completed first followed by a deletion. The semantics for the addition 9328 * are identical to the description of adding a new entry above except 9329 * that the rollback is to the old inode number rather than zero. Once 9330 * the addition dependency is completed, the removal is done as described 9331 * in the removal routine above. 9332 */ 9333 9334 /* 9335 * This routine should be called immediately after changing 9336 * a directory entry. The inode's link count should not be 9337 * decremented by the calling procedure -- the soft updates 9338 * code will perform this task when it is safe. 9339 */ 9340 void 9341 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 9342 struct buf *bp; /* buffer containing directory block */ 9343 struct inode *dp; /* inode for the directory being modified */ 9344 struct inode *ip; /* inode for directory entry being removed */ 9345 ino_t newinum; /* new inode number for changed entry */ 9346 int isrmdir; /* indicates if doing RMDIR */ 9347 { 9348 int offset; 9349 struct diradd *dap = NULL; 9350 struct dirrem *dirrem, *prevdirrem; 9351 struct pagedep *pagedep; 9352 struct inodedep *inodedep; 9353 struct jaddref *jaddref; 9354 struct mount *mp; 9355 struct ufsmount *ump; 9356 9357 mp = ITOVFS(dp); 9358 ump = VFSTOUFS(mp); 9359 offset = blkoff(ump->um_fs, dp->i_offset); 9360 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 9361 ("softdep_setup_directory_change called on non-softdep filesystem")); 9362 9363 /* 9364 * Whiteouts do not need diradd dependencies. 9365 */ 9366 if (newinum != UFS_WINO) { 9367 dap = malloc(sizeof(struct diradd), 9368 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO); 9369 workitem_alloc(&dap->da_list, D_DIRADD, mp); 9370 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE; 9371 dap->da_offset = offset; 9372 dap->da_newinum = newinum; 9373 LIST_INIT(&dap->da_jwork); 9374 } 9375 9376 /* 9377 * Allocate a new dirrem and ACQUIRE_LOCK. 9378 */ 9379 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 9380 pagedep = dirrem->dm_pagedep; 9381 /* 9382 * The possible values for isrmdir: 9383 * 0 - non-directory file rename 9384 * 1 - directory rename within same directory 9385 * inum - directory rename to new directory of given inode number 9386 * When renaming to a new directory, we are both deleting and 9387 * creating a new directory entry, so the link count on the new 9388 * directory should not change. Thus we do not need the followup 9389 * dirrem which is usually done in handle_workitem_remove. We set 9390 * the DIRCHG flag to tell handle_workitem_remove to skip the 9391 * followup dirrem. 9392 */ 9393 if (isrmdir > 1) 9394 dirrem->dm_state |= DIRCHG; 9395 9396 /* 9397 * Whiteouts have no additional dependencies, 9398 * so just put the dirrem on the correct list. 9399 */ 9400 if (newinum == UFS_WINO) { 9401 if ((dirrem->dm_state & COMPLETE) == 0) { 9402 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem, 9403 dm_next); 9404 } else { 9405 dirrem->dm_dirinum = pagedep->pd_ino; 9406 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9407 add_to_worklist(&dirrem->dm_list, 0); 9408 } 9409 FREE_LOCK(ump); 9410 return; 9411 } 9412 /* 9413 * Add the dirrem to the inodedep's pending remove list for quick 9414 * discovery later. A valid nlinkdelta ensures that this lookup 9415 * will not fail. 9416 */ 9417 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9418 panic("softdep_setup_directory_change: Lost inodedep."); 9419 dirrem->dm_state |= ONDEPLIST; 9420 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9421 9422 /* 9423 * If the COMPLETE flag is clear, then there were no active 9424 * entries and we want to roll back to the previous inode until 9425 * the new inode is committed to disk. If the COMPLETE flag is 9426 * set, then we have deleted an entry that never made it to disk. 9427 * If the entry we deleted resulted from a name change, then the old 9428 * inode reference still resides on disk. Any rollback that we do 9429 * needs to be to that old inode (returned to us in prevdirrem). If 9430 * the entry we deleted resulted from a create, then there is 9431 * no entry on the disk, so we want to roll back to zero rather 9432 * than the uncommitted inode. In either of the COMPLETE cases we 9433 * want to immediately free the unwritten and unreferenced inode. 9434 */ 9435 if ((dirrem->dm_state & COMPLETE) == 0) { 9436 dap->da_previous = dirrem; 9437 } else { 9438 if (prevdirrem != NULL) { 9439 dap->da_previous = prevdirrem; 9440 } else { 9441 dap->da_state &= ~DIRCHG; 9442 dap->da_pagedep = pagedep; 9443 } 9444 dirrem->dm_dirinum = pagedep->pd_ino; 9445 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9446 add_to_worklist(&dirrem->dm_list, 0); 9447 } 9448 /* 9449 * Lookup the jaddref for this journal entry. We must finish 9450 * initializing it and make the diradd write dependent on it. 9451 * If we're not journaling, put it on the id_bufwait list if the 9452 * inode is not yet written. If it is written, do the post-inode 9453 * write processing to put it on the id_pendinghd list. 9454 */ 9455 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 9456 if (MOUNTEDSUJ(mp)) { 9457 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 9458 inoreflst); 9459 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 9460 ("softdep_setup_directory_change: bad jaddref %p", 9461 jaddref)); 9462 jaddref->ja_diroff = dp->i_offset; 9463 jaddref->ja_diradd = dap; 9464 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9465 dap, da_pdlist); 9466 add_to_journal(&jaddref->ja_list); 9467 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 9468 dap->da_state |= COMPLETE; 9469 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 9470 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 9471 } else { 9472 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9473 dap, da_pdlist); 9474 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 9475 } 9476 /* 9477 * If we're making a new name for a directory that has not been 9478 * committed when need to move the dot and dotdot references to 9479 * this new name. 9480 */ 9481 if (inodedep->id_mkdiradd && dp->i_offset != DOTDOT_OFFSET) 9482 merge_diradd(inodedep, dap); 9483 FREE_LOCK(ump); 9484 } 9485 9486 /* 9487 * Called whenever the link count on an inode is changed. 9488 * It creates an inode dependency so that the new reference(s) 9489 * to the inode cannot be committed to disk until the updated 9490 * inode has been written. 9491 */ 9492 void 9493 softdep_change_linkcnt(ip) 9494 struct inode *ip; /* the inode with the increased link count */ 9495 { 9496 struct inodedep *inodedep; 9497 struct ufsmount *ump; 9498 9499 ump = ITOUMP(ip); 9500 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9501 ("softdep_change_linkcnt called on non-softdep filesystem")); 9502 ACQUIRE_LOCK(ump); 9503 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep); 9504 if (ip->i_nlink < ip->i_effnlink) 9505 panic("softdep_change_linkcnt: bad delta"); 9506 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9507 FREE_LOCK(ump); 9508 } 9509 9510 /* 9511 * Attach a sbdep dependency to the superblock buf so that we can keep 9512 * track of the head of the linked list of referenced but unlinked inodes. 9513 */ 9514 void 9515 softdep_setup_sbupdate(ump, fs, bp) 9516 struct ufsmount *ump; 9517 struct fs *fs; 9518 struct buf *bp; 9519 { 9520 struct sbdep *sbdep; 9521 struct worklist *wk; 9522 9523 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9524 ("softdep_setup_sbupdate called on non-softdep filesystem")); 9525 LIST_FOREACH(wk, &bp->b_dep, wk_list) 9526 if (wk->wk_type == D_SBDEP) 9527 break; 9528 if (wk != NULL) 9529 return; 9530 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS); 9531 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump)); 9532 sbdep->sb_fs = fs; 9533 sbdep->sb_ump = ump; 9534 ACQUIRE_LOCK(ump); 9535 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list); 9536 FREE_LOCK(ump); 9537 } 9538 9539 /* 9540 * Return the first unlinked inodedep which is ready to be the head of the 9541 * list. The inodedep and all those after it must have valid next pointers. 9542 */ 9543 static struct inodedep * 9544 first_unlinked_inodedep(ump) 9545 struct ufsmount *ump; 9546 { 9547 struct inodedep *inodedep; 9548 struct inodedep *idp; 9549 9550 LOCK_OWNED(ump); 9551 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst); 9552 inodedep; inodedep = idp) { 9553 if ((inodedep->id_state & UNLINKNEXT) == 0) 9554 return (NULL); 9555 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9556 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0) 9557 break; 9558 if ((inodedep->id_state & UNLINKPREV) == 0) 9559 break; 9560 } 9561 return (inodedep); 9562 } 9563 9564 /* 9565 * Set the sujfree unlinked head pointer prior to writing a superblock. 9566 */ 9567 static void 9568 initiate_write_sbdep(sbdep) 9569 struct sbdep *sbdep; 9570 { 9571 struct inodedep *inodedep; 9572 struct fs *bpfs; 9573 struct fs *fs; 9574 9575 bpfs = sbdep->sb_fs; 9576 fs = sbdep->sb_ump->um_fs; 9577 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9578 if (inodedep) { 9579 fs->fs_sujfree = inodedep->id_ino; 9580 inodedep->id_state |= UNLINKPREV; 9581 } else 9582 fs->fs_sujfree = 0; 9583 bpfs->fs_sujfree = fs->fs_sujfree; 9584 } 9585 9586 /* 9587 * After a superblock is written determine whether it must be written again 9588 * due to a changing unlinked list head. 9589 */ 9590 static int 9591 handle_written_sbdep(sbdep, bp) 9592 struct sbdep *sbdep; 9593 struct buf *bp; 9594 { 9595 struct inodedep *inodedep; 9596 struct fs *fs; 9597 9598 LOCK_OWNED(sbdep->sb_ump); 9599 fs = sbdep->sb_fs; 9600 /* 9601 * If the superblock doesn't match the in-memory list start over. 9602 */ 9603 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9604 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) || 9605 (inodedep == NULL && fs->fs_sujfree != 0)) { 9606 bdirty(bp); 9607 return (1); 9608 } 9609 WORKITEM_FREE(sbdep, D_SBDEP); 9610 if (fs->fs_sujfree == 0) 9611 return (0); 9612 /* 9613 * Now that we have a record of this inode in stable store allow it 9614 * to be written to free up pending work. Inodes may see a lot of 9615 * write activity after they are unlinked which we must not hold up. 9616 */ 9617 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 9618 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS) 9619 panic("handle_written_sbdep: Bad inodedep %p (0x%X)", 9620 inodedep, inodedep->id_state); 9621 if (inodedep->id_state & UNLINKONLIST) 9622 break; 9623 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST; 9624 } 9625 9626 return (0); 9627 } 9628 9629 /* 9630 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list. 9631 */ 9632 static void 9633 unlinked_inodedep(mp, inodedep) 9634 struct mount *mp; 9635 struct inodedep *inodedep; 9636 { 9637 struct ufsmount *ump; 9638 9639 ump = VFSTOUFS(mp); 9640 LOCK_OWNED(ump); 9641 if (MOUNTEDSUJ(mp) == 0) 9642 return; 9643 ump->um_fs->fs_fmod = 1; 9644 if (inodedep->id_state & UNLINKED) 9645 panic("unlinked_inodedep: %p already unlinked\n", inodedep); 9646 inodedep->id_state |= UNLINKED; 9647 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked); 9648 } 9649 9650 /* 9651 * Remove an inodedep from the unlinked inodedep list. This may require 9652 * disk writes if the inode has made it that far. 9653 */ 9654 static void 9655 clear_unlinked_inodedep(inodedep) 9656 struct inodedep *inodedep; 9657 { 9658 struct ufs2_dinode *dip; 9659 struct ufsmount *ump; 9660 struct inodedep *idp; 9661 struct inodedep *idn; 9662 struct fs *fs; 9663 struct buf *bp; 9664 ino_t ino; 9665 ino_t nino; 9666 ino_t pino; 9667 int error; 9668 9669 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9670 fs = ump->um_fs; 9671 ino = inodedep->id_ino; 9672 error = 0; 9673 for (;;) { 9674 LOCK_OWNED(ump); 9675 KASSERT((inodedep->id_state & UNLINKED) != 0, 9676 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9677 inodedep)); 9678 /* 9679 * If nothing has yet been written simply remove us from 9680 * the in memory list and return. This is the most common 9681 * case where handle_workitem_remove() loses the final 9682 * reference. 9683 */ 9684 if ((inodedep->id_state & UNLINKLINKS) == 0) 9685 break; 9686 /* 9687 * If we have a NEXT pointer and no PREV pointer we can simply 9688 * clear NEXT's PREV and remove ourselves from the list. Be 9689 * careful not to clear PREV if the superblock points at 9690 * next as well. 9691 */ 9692 idn = TAILQ_NEXT(inodedep, id_unlinked); 9693 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) { 9694 if (idn && fs->fs_sujfree != idn->id_ino) 9695 idn->id_state &= ~UNLINKPREV; 9696 break; 9697 } 9698 /* 9699 * Here we have an inodedep which is actually linked into 9700 * the list. We must remove it by forcing a write to the 9701 * link before us, whether it be the superblock or an inode. 9702 * Unfortunately the list may change while we're waiting 9703 * on the buf lock for either resource so we must loop until 9704 * we lock the right one. If both the superblock and an 9705 * inode point to this inode we must clear the inode first 9706 * followed by the superblock. 9707 */ 9708 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9709 pino = 0; 9710 if (idp && (idp->id_state & UNLINKNEXT)) 9711 pino = idp->id_ino; 9712 FREE_LOCK(ump); 9713 if (pino == 0) { 9714 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9715 (int)fs->fs_sbsize, 0, 0, 0); 9716 } else { 9717 error = bread(ump->um_devvp, 9718 fsbtodb(fs, ino_to_fsba(fs, pino)), 9719 (int)fs->fs_bsize, NOCRED, &bp); 9720 if (error) 9721 brelse(bp); 9722 } 9723 ACQUIRE_LOCK(ump); 9724 if (error) 9725 break; 9726 /* If the list has changed restart the loop. */ 9727 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9728 nino = 0; 9729 if (idp && (idp->id_state & UNLINKNEXT)) 9730 nino = idp->id_ino; 9731 if (nino != pino || 9732 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) { 9733 FREE_LOCK(ump); 9734 brelse(bp); 9735 ACQUIRE_LOCK(ump); 9736 continue; 9737 } 9738 nino = 0; 9739 idn = TAILQ_NEXT(inodedep, id_unlinked); 9740 if (idn) 9741 nino = idn->id_ino; 9742 /* 9743 * Remove us from the in memory list. After this we cannot 9744 * access the inodedep. 9745 */ 9746 KASSERT((inodedep->id_state & UNLINKED) != 0, 9747 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9748 inodedep)); 9749 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9750 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9751 FREE_LOCK(ump); 9752 /* 9753 * The predecessor's next pointer is manually updated here 9754 * so that the NEXT flag is never cleared for an element 9755 * that is in the list. 9756 */ 9757 if (pino == 0) { 9758 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 9759 ffs_oldfscompat_write((struct fs *)bp->b_data, ump); 9760 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, 9761 bp); 9762 } else if (fs->fs_magic == FS_UFS1_MAGIC) { 9763 ((struct ufs1_dinode *)bp->b_data + 9764 ino_to_fsbo(fs, pino))->di_freelink = nino; 9765 } else { 9766 dip = (struct ufs2_dinode *)bp->b_data + 9767 ino_to_fsbo(fs, pino); 9768 dip->di_freelink = nino; 9769 ffs_update_dinode_ckhash(fs, dip); 9770 } 9771 /* 9772 * If the bwrite fails we have no recourse to recover. The 9773 * filesystem is corrupted already. 9774 */ 9775 bwrite(bp); 9776 ACQUIRE_LOCK(ump); 9777 /* 9778 * If the superblock pointer still needs to be cleared force 9779 * a write here. 9780 */ 9781 if (fs->fs_sujfree == ino) { 9782 FREE_LOCK(ump); 9783 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9784 (int)fs->fs_sbsize, 0, 0, 0); 9785 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 9786 ffs_oldfscompat_write((struct fs *)bp->b_data, ump); 9787 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, 9788 bp); 9789 bwrite(bp); 9790 ACQUIRE_LOCK(ump); 9791 } 9792 9793 if (fs->fs_sujfree != ino) 9794 return; 9795 panic("clear_unlinked_inodedep: Failed to clear free head"); 9796 } 9797 if (inodedep->id_ino == fs->fs_sujfree) 9798 panic("clear_unlinked_inodedep: Freeing head of free list"); 9799 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9800 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9801 return; 9802 } 9803 9804 /* 9805 * This workitem decrements the inode's link count. 9806 * If the link count reaches zero, the file is removed. 9807 */ 9808 static int 9809 handle_workitem_remove(dirrem, flags) 9810 struct dirrem *dirrem; 9811 int flags; 9812 { 9813 struct inodedep *inodedep; 9814 struct workhead dotdotwk; 9815 struct worklist *wk; 9816 struct ufsmount *ump; 9817 struct mount *mp; 9818 struct vnode *vp; 9819 struct inode *ip; 9820 ino_t oldinum; 9821 9822 if (dirrem->dm_state & ONWORKLIST) 9823 panic("handle_workitem_remove: dirrem %p still on worklist", 9824 dirrem); 9825 oldinum = dirrem->dm_oldinum; 9826 mp = dirrem->dm_list.wk_mp; 9827 ump = VFSTOUFS(mp); 9828 flags |= LK_EXCLUSIVE; 9829 if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ) != 0) 9830 return (EBUSY); 9831 ip = VTOI(vp); 9832 ACQUIRE_LOCK(ump); 9833 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0) 9834 panic("handle_workitem_remove: lost inodedep"); 9835 if (dirrem->dm_state & ONDEPLIST) 9836 LIST_REMOVE(dirrem, dm_inonext); 9837 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 9838 ("handle_workitem_remove: Journal entries not written.")); 9839 9840 /* 9841 * Move all dependencies waiting on the remove to complete 9842 * from the dirrem to the inode inowait list to be completed 9843 * after the inode has been updated and written to disk. Any 9844 * marked MKDIR_PARENT are saved to be completed when the .. ref 9845 * is removed. 9846 */ 9847 LIST_INIT(&dotdotwk); 9848 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) { 9849 WORKLIST_REMOVE(wk); 9850 if (wk->wk_state & MKDIR_PARENT) { 9851 wk->wk_state &= ~MKDIR_PARENT; 9852 WORKLIST_INSERT(&dotdotwk, wk); 9853 continue; 9854 } 9855 WORKLIST_INSERT(&inodedep->id_inowait, wk); 9856 } 9857 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list); 9858 /* 9859 * Normal file deletion. 9860 */ 9861 if ((dirrem->dm_state & RMDIR) == 0) { 9862 ip->i_nlink--; 9863 DIP_SET(ip, i_nlink, ip->i_nlink); 9864 ip->i_flag |= IN_CHANGE; 9865 if (ip->i_nlink < ip->i_effnlink) 9866 panic("handle_workitem_remove: bad file delta"); 9867 if (ip->i_nlink == 0) 9868 unlinked_inodedep(mp, inodedep); 9869 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9870 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 9871 ("handle_workitem_remove: worklist not empty. %s", 9872 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type))); 9873 WORKITEM_FREE(dirrem, D_DIRREM); 9874 FREE_LOCK(ump); 9875 goto out; 9876 } 9877 /* 9878 * Directory deletion. Decrement reference count for both the 9879 * just deleted parent directory entry and the reference for ".". 9880 * Arrange to have the reference count on the parent decremented 9881 * to account for the loss of "..". 9882 */ 9883 ip->i_nlink -= 2; 9884 DIP_SET(ip, i_nlink, ip->i_nlink); 9885 ip->i_flag |= IN_CHANGE; 9886 if (ip->i_nlink < ip->i_effnlink) 9887 panic("handle_workitem_remove: bad dir delta"); 9888 if (ip->i_nlink == 0) 9889 unlinked_inodedep(mp, inodedep); 9890 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9891 /* 9892 * Rename a directory to a new parent. Since, we are both deleting 9893 * and creating a new directory entry, the link count on the new 9894 * directory should not change. Thus we skip the followup dirrem. 9895 */ 9896 if (dirrem->dm_state & DIRCHG) { 9897 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 9898 ("handle_workitem_remove: DIRCHG and worklist not empty.")); 9899 WORKITEM_FREE(dirrem, D_DIRREM); 9900 FREE_LOCK(ump); 9901 goto out; 9902 } 9903 dirrem->dm_state = ONDEPLIST; 9904 dirrem->dm_oldinum = dirrem->dm_dirinum; 9905 /* 9906 * Place the dirrem on the parent's diremhd list. 9907 */ 9908 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0) 9909 panic("handle_workitem_remove: lost dir inodedep"); 9910 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9911 /* 9912 * If the allocated inode has never been written to disk, then 9913 * the on-disk inode is zero'ed and we can remove the file 9914 * immediately. When journaling if the inode has been marked 9915 * unlinked and not DEPCOMPLETE we know it can never be written. 9916 */ 9917 inodedep_lookup(mp, oldinum, 0, &inodedep); 9918 if (inodedep == NULL || 9919 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED || 9920 check_inode_unwritten(inodedep)) { 9921 FREE_LOCK(ump); 9922 vput(vp); 9923 return handle_workitem_remove(dirrem, flags); 9924 } 9925 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list); 9926 FREE_LOCK(ump); 9927 ip->i_flag |= IN_CHANGE; 9928 out: 9929 ffs_update(vp, 0); 9930 vput(vp); 9931 return (0); 9932 } 9933 9934 /* 9935 * Inode de-allocation dependencies. 9936 * 9937 * When an inode's link count is reduced to zero, it can be de-allocated. We 9938 * found it convenient to postpone de-allocation until after the inode is 9939 * written to disk with its new link count (zero). At this point, all of the 9940 * on-disk inode's block pointers are nullified and, with careful dependency 9941 * list ordering, all dependencies related to the inode will be satisfied and 9942 * the corresponding dependency structures de-allocated. So, if/when the 9943 * inode is reused, there will be no mixing of old dependencies with new 9944 * ones. This artificial dependency is set up by the block de-allocation 9945 * procedure above (softdep_setup_freeblocks) and completed by the 9946 * following procedure. 9947 */ 9948 static void 9949 handle_workitem_freefile(freefile) 9950 struct freefile *freefile; 9951 { 9952 struct workhead wkhd; 9953 struct fs *fs; 9954 struct ufsmount *ump; 9955 int error; 9956 #ifdef INVARIANTS 9957 struct inodedep *idp; 9958 #endif 9959 9960 ump = VFSTOUFS(freefile->fx_list.wk_mp); 9961 fs = ump->um_fs; 9962 #ifdef INVARIANTS 9963 ACQUIRE_LOCK(ump); 9964 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp); 9965 FREE_LOCK(ump); 9966 if (error) 9967 panic("handle_workitem_freefile: inodedep %p survived", idp); 9968 #endif 9969 UFS_LOCK(ump); 9970 fs->fs_pendinginodes -= 1; 9971 UFS_UNLOCK(ump); 9972 LIST_INIT(&wkhd); 9973 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list); 9974 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp, 9975 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0) 9976 softdep_error("handle_workitem_freefile", error); 9977 ACQUIRE_LOCK(ump); 9978 WORKITEM_FREE(freefile, D_FREEFILE); 9979 FREE_LOCK(ump); 9980 } 9981 9982 9983 /* 9984 * Helper function which unlinks marker element from work list and returns 9985 * the next element on the list. 9986 */ 9987 static __inline struct worklist * 9988 markernext(struct worklist *marker) 9989 { 9990 struct worklist *next; 9991 9992 next = LIST_NEXT(marker, wk_list); 9993 LIST_REMOVE(marker, wk_list); 9994 return next; 9995 } 9996 9997 /* 9998 * Disk writes. 9999 * 10000 * The dependency structures constructed above are most actively used when file 10001 * system blocks are written to disk. No constraints are placed on when a 10002 * block can be written, but unsatisfied update dependencies are made safe by 10003 * modifying (or replacing) the source memory for the duration of the disk 10004 * write. When the disk write completes, the memory block is again brought 10005 * up-to-date. 10006 * 10007 * In-core inode structure reclamation. 10008 * 10009 * Because there are a finite number of "in-core" inode structures, they are 10010 * reused regularly. By transferring all inode-related dependencies to the 10011 * in-memory inode block and indexing them separately (via "inodedep"s), we 10012 * can allow "in-core" inode structures to be reused at any time and avoid 10013 * any increase in contention. 10014 * 10015 * Called just before entering the device driver to initiate a new disk I/O. 10016 * The buffer must be locked, thus, no I/O completion operations can occur 10017 * while we are manipulating its associated dependencies. 10018 */ 10019 static void 10020 softdep_disk_io_initiation(bp) 10021 struct buf *bp; /* structure describing disk write to occur */ 10022 { 10023 struct worklist *wk; 10024 struct worklist marker; 10025 struct inodedep *inodedep; 10026 struct freeblks *freeblks; 10027 struct jblkdep *jblkdep; 10028 struct newblk *newblk; 10029 struct ufsmount *ump; 10030 10031 /* 10032 * We only care about write operations. There should never 10033 * be dependencies for reads. 10034 */ 10035 if (bp->b_iocmd != BIO_WRITE) 10036 panic("softdep_disk_io_initiation: not write"); 10037 10038 if (bp->b_vflags & BV_BKGRDINPROG) 10039 panic("softdep_disk_io_initiation: Writing buffer with " 10040 "background write in progress: %p", bp); 10041 10042 ump = softdep_bp_to_mp(bp); 10043 if (ump == NULL) 10044 return; 10045 10046 marker.wk_type = D_LAST + 1; /* Not a normal workitem */ 10047 PHOLD(curproc); /* Don't swap out kernel stack */ 10048 ACQUIRE_LOCK(ump); 10049 /* 10050 * Do any necessary pre-I/O processing. 10051 */ 10052 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL; 10053 wk = markernext(&marker)) { 10054 LIST_INSERT_AFTER(wk, &marker, wk_list); 10055 switch (wk->wk_type) { 10056 10057 case D_PAGEDEP: 10058 initiate_write_filepage(WK_PAGEDEP(wk), bp); 10059 continue; 10060 10061 case D_INODEDEP: 10062 inodedep = WK_INODEDEP(wk); 10063 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) 10064 initiate_write_inodeblock_ufs1(inodedep, bp); 10065 else 10066 initiate_write_inodeblock_ufs2(inodedep, bp); 10067 continue; 10068 10069 case D_INDIRDEP: 10070 initiate_write_indirdep(WK_INDIRDEP(wk), bp); 10071 continue; 10072 10073 case D_BMSAFEMAP: 10074 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp); 10075 continue; 10076 10077 case D_JSEG: 10078 WK_JSEG(wk)->js_buf = NULL; 10079 continue; 10080 10081 case D_FREEBLKS: 10082 freeblks = WK_FREEBLKS(wk); 10083 jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd); 10084 /* 10085 * We have to wait for the freeblks to be journaled 10086 * before we can write an inodeblock with updated 10087 * pointers. Be careful to arrange the marker so 10088 * we revisit the freeblks if it's not removed by 10089 * the first jwait(). 10090 */ 10091 if (jblkdep != NULL) { 10092 LIST_REMOVE(&marker, wk_list); 10093 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10094 jwait(&jblkdep->jb_list, MNT_WAIT); 10095 } 10096 continue; 10097 case D_ALLOCDIRECT: 10098 case D_ALLOCINDIR: 10099 /* 10100 * We have to wait for the jnewblk to be journaled 10101 * before we can write to a block if the contents 10102 * may be confused with an earlier file's indirect 10103 * at recovery time. Handle the marker as described 10104 * above. 10105 */ 10106 newblk = WK_NEWBLK(wk); 10107 if (newblk->nb_jnewblk != NULL && 10108 indirblk_lookup(newblk->nb_list.wk_mp, 10109 newblk->nb_newblkno)) { 10110 LIST_REMOVE(&marker, wk_list); 10111 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10112 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 10113 } 10114 continue; 10115 10116 case D_SBDEP: 10117 initiate_write_sbdep(WK_SBDEP(wk)); 10118 continue; 10119 10120 case D_MKDIR: 10121 case D_FREEWORK: 10122 case D_FREEDEP: 10123 case D_JSEGDEP: 10124 continue; 10125 10126 default: 10127 panic("handle_disk_io_initiation: Unexpected type %s", 10128 TYPENAME(wk->wk_type)); 10129 /* NOTREACHED */ 10130 } 10131 } 10132 FREE_LOCK(ump); 10133 PRELE(curproc); /* Allow swapout of kernel stack */ 10134 } 10135 10136 /* 10137 * Called from within the procedure above to deal with unsatisfied 10138 * allocation dependencies in a directory. The buffer must be locked, 10139 * thus, no I/O completion operations can occur while we are 10140 * manipulating its associated dependencies. 10141 */ 10142 static void 10143 initiate_write_filepage(pagedep, bp) 10144 struct pagedep *pagedep; 10145 struct buf *bp; 10146 { 10147 struct jremref *jremref; 10148 struct jmvref *jmvref; 10149 struct dirrem *dirrem; 10150 struct diradd *dap; 10151 struct direct *ep; 10152 int i; 10153 10154 if (pagedep->pd_state & IOSTARTED) { 10155 /* 10156 * This can only happen if there is a driver that does not 10157 * understand chaining. Here biodone will reissue the call 10158 * to strategy for the incomplete buffers. 10159 */ 10160 printf("initiate_write_filepage: already started\n"); 10161 return; 10162 } 10163 pagedep->pd_state |= IOSTARTED; 10164 /* 10165 * Wait for all journal remove dependencies to hit the disk. 10166 * We can not allow any potentially conflicting directory adds 10167 * to be visible before removes and rollback is too difficult. 10168 * The per-filesystem lock may be dropped and re-acquired, however 10169 * we hold the buf locked so the dependency can not go away. 10170 */ 10171 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) 10172 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) 10173 jwait(&jremref->jr_list, MNT_WAIT); 10174 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) 10175 jwait(&jmvref->jm_list, MNT_WAIT); 10176 for (i = 0; i < DAHASHSZ; i++) { 10177 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 10178 ep = (struct direct *) 10179 ((char *)bp->b_data + dap->da_offset); 10180 if (ep->d_ino != dap->da_newinum) 10181 panic("%s: dir inum %ju != new %ju", 10182 "initiate_write_filepage", 10183 (uintmax_t)ep->d_ino, 10184 (uintmax_t)dap->da_newinum); 10185 if (dap->da_state & DIRCHG) 10186 ep->d_ino = dap->da_previous->dm_oldinum; 10187 else 10188 ep->d_ino = 0; 10189 dap->da_state &= ~ATTACHED; 10190 dap->da_state |= UNDONE; 10191 } 10192 } 10193 } 10194 10195 /* 10196 * Version of initiate_write_inodeblock that handles UFS1 dinodes. 10197 * Note that any bug fixes made to this routine must be done in the 10198 * version found below. 10199 * 10200 * Called from within the procedure above to deal with unsatisfied 10201 * allocation dependencies in an inodeblock. The buffer must be 10202 * locked, thus, no I/O completion operations can occur while we 10203 * are manipulating its associated dependencies. 10204 */ 10205 static void 10206 initiate_write_inodeblock_ufs1(inodedep, bp) 10207 struct inodedep *inodedep; 10208 struct buf *bp; /* The inode block */ 10209 { 10210 struct allocdirect *adp, *lastadp; 10211 struct ufs1_dinode *dp; 10212 struct ufs1_dinode *sip; 10213 struct inoref *inoref; 10214 struct ufsmount *ump; 10215 struct fs *fs; 10216 ufs_lbn_t i; 10217 #ifdef INVARIANTS 10218 ufs_lbn_t prevlbn = 0; 10219 #endif 10220 int deplist; 10221 10222 if (inodedep->id_state & IOSTARTED) 10223 panic("initiate_write_inodeblock_ufs1: already started"); 10224 inodedep->id_state |= IOSTARTED; 10225 fs = inodedep->id_fs; 10226 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10227 LOCK_OWNED(ump); 10228 dp = (struct ufs1_dinode *)bp->b_data + 10229 ino_to_fsbo(fs, inodedep->id_ino); 10230 10231 /* 10232 * If we're on the unlinked list but have not yet written our 10233 * next pointer initialize it here. 10234 */ 10235 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10236 struct inodedep *inon; 10237 10238 inon = TAILQ_NEXT(inodedep, id_unlinked); 10239 dp->di_freelink = inon ? inon->id_ino : 0; 10240 } 10241 /* 10242 * If the bitmap is not yet written, then the allocated 10243 * inode cannot be written to disk. 10244 */ 10245 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10246 if (inodedep->id_savedino1 != NULL) 10247 panic("initiate_write_inodeblock_ufs1: I/O underway"); 10248 FREE_LOCK(ump); 10249 sip = malloc(sizeof(struct ufs1_dinode), 10250 M_SAVEDINO, M_SOFTDEP_FLAGS); 10251 ACQUIRE_LOCK(ump); 10252 inodedep->id_savedino1 = sip; 10253 *inodedep->id_savedino1 = *dp; 10254 bzero((caddr_t)dp, sizeof(struct ufs1_dinode)); 10255 dp->di_gen = inodedep->id_savedino1->di_gen; 10256 dp->di_freelink = inodedep->id_savedino1->di_freelink; 10257 return; 10258 } 10259 /* 10260 * If no dependencies, then there is nothing to roll back. 10261 */ 10262 inodedep->id_savedsize = dp->di_size; 10263 inodedep->id_savedextsize = 0; 10264 inodedep->id_savednlink = dp->di_nlink; 10265 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10266 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10267 return; 10268 /* 10269 * Revert the link count to that of the first unwritten journal entry. 10270 */ 10271 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10272 if (inoref) 10273 dp->di_nlink = inoref->if_nlink; 10274 /* 10275 * Set the dependencies to busy. 10276 */ 10277 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10278 adp = TAILQ_NEXT(adp, ad_next)) { 10279 #ifdef INVARIANTS 10280 if (deplist != 0 && prevlbn >= adp->ad_offset) 10281 panic("softdep_write_inodeblock: lbn order"); 10282 prevlbn = adp->ad_offset; 10283 if (adp->ad_offset < UFS_NDADDR && 10284 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10285 panic("initiate_write_inodeblock_ufs1: " 10286 "direct pointer #%jd mismatch %d != %jd", 10287 (intmax_t)adp->ad_offset, 10288 dp->di_db[adp->ad_offset], 10289 (intmax_t)adp->ad_newblkno); 10290 if (adp->ad_offset >= UFS_NDADDR && 10291 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno) 10292 panic("initiate_write_inodeblock_ufs1: " 10293 "indirect pointer #%jd mismatch %d != %jd", 10294 (intmax_t)adp->ad_offset - UFS_NDADDR, 10295 dp->di_ib[adp->ad_offset - UFS_NDADDR], 10296 (intmax_t)adp->ad_newblkno); 10297 deplist |= 1 << adp->ad_offset; 10298 if ((adp->ad_state & ATTACHED) == 0) 10299 panic("initiate_write_inodeblock_ufs1: " 10300 "Unknown state 0x%x", adp->ad_state); 10301 #endif /* INVARIANTS */ 10302 adp->ad_state &= ~ATTACHED; 10303 adp->ad_state |= UNDONE; 10304 } 10305 /* 10306 * The on-disk inode cannot claim to be any larger than the last 10307 * fragment that has been written. Otherwise, the on-disk inode 10308 * might have fragments that were not the last block in the file 10309 * which would corrupt the filesystem. 10310 */ 10311 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10312 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10313 if (adp->ad_offset >= UFS_NDADDR) 10314 break; 10315 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10316 /* keep going until hitting a rollback to a frag */ 10317 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10318 continue; 10319 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10320 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) { 10321 #ifdef INVARIANTS 10322 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10323 panic("initiate_write_inodeblock_ufs1: " 10324 "lost dep1"); 10325 #endif /* INVARIANTS */ 10326 dp->di_db[i] = 0; 10327 } 10328 for (i = 0; i < UFS_NIADDR; i++) { 10329 #ifdef INVARIANTS 10330 if (dp->di_ib[i] != 0 && 10331 (deplist & ((1 << UFS_NDADDR) << i)) == 0) 10332 panic("initiate_write_inodeblock_ufs1: " 10333 "lost dep2"); 10334 #endif /* INVARIANTS */ 10335 dp->di_ib[i] = 0; 10336 } 10337 return; 10338 } 10339 /* 10340 * If we have zero'ed out the last allocated block of the file, 10341 * roll back the size to the last currently allocated block. 10342 * We know that this last allocated block is a full-sized as 10343 * we already checked for fragments in the loop above. 10344 */ 10345 if (lastadp != NULL && 10346 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10347 for (i = lastadp->ad_offset; i >= 0; i--) 10348 if (dp->di_db[i] != 0) 10349 break; 10350 dp->di_size = (i + 1) * fs->fs_bsize; 10351 } 10352 /* 10353 * The only dependencies are for indirect blocks. 10354 * 10355 * The file size for indirect block additions is not guaranteed. 10356 * Such a guarantee would be non-trivial to achieve. The conventional 10357 * synchronous write implementation also does not make this guarantee. 10358 * Fsck should catch and fix discrepancies. Arguably, the file size 10359 * can be over-estimated without destroying integrity when the file 10360 * moves into the indirect blocks (i.e., is large). If we want to 10361 * postpone fsck, we are stuck with this argument. 10362 */ 10363 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10364 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0; 10365 } 10366 10367 /* 10368 * Version of initiate_write_inodeblock that handles UFS2 dinodes. 10369 * Note that any bug fixes made to this routine must be done in the 10370 * version found above. 10371 * 10372 * Called from within the procedure above to deal with unsatisfied 10373 * allocation dependencies in an inodeblock. The buffer must be 10374 * locked, thus, no I/O completion operations can occur while we 10375 * are manipulating its associated dependencies. 10376 */ 10377 static void 10378 initiate_write_inodeblock_ufs2(inodedep, bp) 10379 struct inodedep *inodedep; 10380 struct buf *bp; /* The inode block */ 10381 { 10382 struct allocdirect *adp, *lastadp; 10383 struct ufs2_dinode *dp; 10384 struct ufs2_dinode *sip; 10385 struct inoref *inoref; 10386 struct ufsmount *ump; 10387 struct fs *fs; 10388 ufs_lbn_t i; 10389 #ifdef INVARIANTS 10390 ufs_lbn_t prevlbn = 0; 10391 #endif 10392 int deplist; 10393 10394 if (inodedep->id_state & IOSTARTED) 10395 panic("initiate_write_inodeblock_ufs2: already started"); 10396 inodedep->id_state |= IOSTARTED; 10397 fs = inodedep->id_fs; 10398 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10399 LOCK_OWNED(ump); 10400 dp = (struct ufs2_dinode *)bp->b_data + 10401 ino_to_fsbo(fs, inodedep->id_ino); 10402 10403 /* 10404 * If we're on the unlinked list but have not yet written our 10405 * next pointer initialize it here. 10406 */ 10407 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10408 struct inodedep *inon; 10409 10410 inon = TAILQ_NEXT(inodedep, id_unlinked); 10411 dp->di_freelink = inon ? inon->id_ino : 0; 10412 ffs_update_dinode_ckhash(fs, dp); 10413 } 10414 /* 10415 * If the bitmap is not yet written, then the allocated 10416 * inode cannot be written to disk. 10417 */ 10418 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10419 if (inodedep->id_savedino2 != NULL) 10420 panic("initiate_write_inodeblock_ufs2: I/O underway"); 10421 FREE_LOCK(ump); 10422 sip = malloc(sizeof(struct ufs2_dinode), 10423 M_SAVEDINO, M_SOFTDEP_FLAGS); 10424 ACQUIRE_LOCK(ump); 10425 inodedep->id_savedino2 = sip; 10426 *inodedep->id_savedino2 = *dp; 10427 bzero((caddr_t)dp, sizeof(struct ufs2_dinode)); 10428 dp->di_gen = inodedep->id_savedino2->di_gen; 10429 dp->di_freelink = inodedep->id_savedino2->di_freelink; 10430 return; 10431 } 10432 /* 10433 * If no dependencies, then there is nothing to roll back. 10434 */ 10435 inodedep->id_savedsize = dp->di_size; 10436 inodedep->id_savedextsize = dp->di_extsize; 10437 inodedep->id_savednlink = dp->di_nlink; 10438 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10439 TAILQ_EMPTY(&inodedep->id_extupdt) && 10440 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10441 return; 10442 /* 10443 * Revert the link count to that of the first unwritten journal entry. 10444 */ 10445 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10446 if (inoref) 10447 dp->di_nlink = inoref->if_nlink; 10448 10449 /* 10450 * Set the ext data dependencies to busy. 10451 */ 10452 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10453 adp = TAILQ_NEXT(adp, ad_next)) { 10454 #ifdef INVARIANTS 10455 if (deplist != 0 && prevlbn >= adp->ad_offset) 10456 panic("initiate_write_inodeblock_ufs2: lbn order"); 10457 prevlbn = adp->ad_offset; 10458 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno) 10459 panic("initiate_write_inodeblock_ufs2: " 10460 "ext pointer #%jd mismatch %jd != %jd", 10461 (intmax_t)adp->ad_offset, 10462 (intmax_t)dp->di_extb[adp->ad_offset], 10463 (intmax_t)adp->ad_newblkno); 10464 deplist |= 1 << adp->ad_offset; 10465 if ((adp->ad_state & ATTACHED) == 0) 10466 panic("initiate_write_inodeblock_ufs2: Unknown " 10467 "state 0x%x", adp->ad_state); 10468 #endif /* INVARIANTS */ 10469 adp->ad_state &= ~ATTACHED; 10470 adp->ad_state |= UNDONE; 10471 } 10472 /* 10473 * The on-disk inode cannot claim to be any larger than the last 10474 * fragment that has been written. Otherwise, the on-disk inode 10475 * might have fragments that were not the last block in the ext 10476 * data which would corrupt the filesystem. 10477 */ 10478 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10479 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10480 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno; 10481 /* keep going until hitting a rollback to a frag */ 10482 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10483 continue; 10484 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10485 for (i = adp->ad_offset + 1; i < UFS_NXADDR; i++) { 10486 #ifdef INVARIANTS 10487 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0) 10488 panic("initiate_write_inodeblock_ufs2: " 10489 "lost dep1"); 10490 #endif /* INVARIANTS */ 10491 dp->di_extb[i] = 0; 10492 } 10493 lastadp = NULL; 10494 break; 10495 } 10496 /* 10497 * If we have zero'ed out the last allocated block of the ext 10498 * data, roll back the size to the last currently allocated block. 10499 * We know that this last allocated block is a full-sized as 10500 * we already checked for fragments in the loop above. 10501 */ 10502 if (lastadp != NULL && 10503 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10504 for (i = lastadp->ad_offset; i >= 0; i--) 10505 if (dp->di_extb[i] != 0) 10506 break; 10507 dp->di_extsize = (i + 1) * fs->fs_bsize; 10508 } 10509 /* 10510 * Set the file data dependencies to busy. 10511 */ 10512 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10513 adp = TAILQ_NEXT(adp, ad_next)) { 10514 #ifdef INVARIANTS 10515 if (deplist != 0 && prevlbn >= adp->ad_offset) 10516 panic("softdep_write_inodeblock: lbn order"); 10517 if ((adp->ad_state & ATTACHED) == 0) 10518 panic("inodedep %p and adp %p not attached", inodedep, adp); 10519 prevlbn = adp->ad_offset; 10520 if (adp->ad_offset < UFS_NDADDR && 10521 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10522 panic("initiate_write_inodeblock_ufs2: " 10523 "direct pointer #%jd mismatch %jd != %jd", 10524 (intmax_t)adp->ad_offset, 10525 (intmax_t)dp->di_db[adp->ad_offset], 10526 (intmax_t)adp->ad_newblkno); 10527 if (adp->ad_offset >= UFS_NDADDR && 10528 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno) 10529 panic("initiate_write_inodeblock_ufs2: " 10530 "indirect pointer #%jd mismatch %jd != %jd", 10531 (intmax_t)adp->ad_offset - UFS_NDADDR, 10532 (intmax_t)dp->di_ib[adp->ad_offset - UFS_NDADDR], 10533 (intmax_t)adp->ad_newblkno); 10534 deplist |= 1 << adp->ad_offset; 10535 if ((adp->ad_state & ATTACHED) == 0) 10536 panic("initiate_write_inodeblock_ufs2: Unknown " 10537 "state 0x%x", adp->ad_state); 10538 #endif /* INVARIANTS */ 10539 adp->ad_state &= ~ATTACHED; 10540 adp->ad_state |= UNDONE; 10541 } 10542 /* 10543 * The on-disk inode cannot claim to be any larger than the last 10544 * fragment that has been written. Otherwise, the on-disk inode 10545 * might have fragments that were not the last block in the file 10546 * which would corrupt the filesystem. 10547 */ 10548 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10549 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10550 if (adp->ad_offset >= UFS_NDADDR) 10551 break; 10552 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10553 /* keep going until hitting a rollback to a frag */ 10554 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10555 continue; 10556 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10557 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) { 10558 #ifdef INVARIANTS 10559 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10560 panic("initiate_write_inodeblock_ufs2: " 10561 "lost dep2"); 10562 #endif /* INVARIANTS */ 10563 dp->di_db[i] = 0; 10564 } 10565 for (i = 0; i < UFS_NIADDR; i++) { 10566 #ifdef INVARIANTS 10567 if (dp->di_ib[i] != 0 && 10568 (deplist & ((1 << UFS_NDADDR) << i)) == 0) 10569 panic("initiate_write_inodeblock_ufs2: " 10570 "lost dep3"); 10571 #endif /* INVARIANTS */ 10572 dp->di_ib[i] = 0; 10573 } 10574 ffs_update_dinode_ckhash(fs, dp); 10575 return; 10576 } 10577 /* 10578 * If we have zero'ed out the last allocated block of the file, 10579 * roll back the size to the last currently allocated block. 10580 * We know that this last allocated block is a full-sized as 10581 * we already checked for fragments in the loop above. 10582 */ 10583 if (lastadp != NULL && 10584 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10585 for (i = lastadp->ad_offset; i >= 0; i--) 10586 if (dp->di_db[i] != 0) 10587 break; 10588 dp->di_size = (i + 1) * fs->fs_bsize; 10589 } 10590 /* 10591 * The only dependencies are for indirect blocks. 10592 * 10593 * The file size for indirect block additions is not guaranteed. 10594 * Such a guarantee would be non-trivial to achieve. The conventional 10595 * synchronous write implementation also does not make this guarantee. 10596 * Fsck should catch and fix discrepancies. Arguably, the file size 10597 * can be over-estimated without destroying integrity when the file 10598 * moves into the indirect blocks (i.e., is large). If we want to 10599 * postpone fsck, we are stuck with this argument. 10600 */ 10601 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10602 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0; 10603 ffs_update_dinode_ckhash(fs, dp); 10604 } 10605 10606 /* 10607 * Cancel an indirdep as a result of truncation. Release all of the 10608 * children allocindirs and place their journal work on the appropriate 10609 * list. 10610 */ 10611 static void 10612 cancel_indirdep(indirdep, bp, freeblks) 10613 struct indirdep *indirdep; 10614 struct buf *bp; 10615 struct freeblks *freeblks; 10616 { 10617 struct allocindir *aip; 10618 10619 /* 10620 * None of the indirect pointers will ever be visible, 10621 * so they can simply be tossed. GOINGAWAY ensures 10622 * that allocated pointers will be saved in the buffer 10623 * cache until they are freed. Note that they will 10624 * only be able to be found by their physical address 10625 * since the inode mapping the logical address will 10626 * be gone. The save buffer used for the safe copy 10627 * was allocated in setup_allocindir_phase2 using 10628 * the physical address so it could be used for this 10629 * purpose. Hence we swap the safe copy with the real 10630 * copy, allowing the safe copy to be freed and holding 10631 * on to the real copy for later use in indir_trunc. 10632 */ 10633 if (indirdep->ir_state & GOINGAWAY) 10634 panic("cancel_indirdep: already gone"); 10635 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 10636 indirdep->ir_state |= DEPCOMPLETE; 10637 LIST_REMOVE(indirdep, ir_next); 10638 } 10639 indirdep->ir_state |= GOINGAWAY; 10640 /* 10641 * Pass in bp for blocks still have journal writes 10642 * pending so we can cancel them on their own. 10643 */ 10644 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != NULL) 10645 cancel_allocindir(aip, bp, freeblks, 0); 10646 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) 10647 cancel_allocindir(aip, NULL, freeblks, 0); 10648 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) 10649 cancel_allocindir(aip, NULL, freeblks, 0); 10650 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) 10651 cancel_allocindir(aip, NULL, freeblks, 0); 10652 /* 10653 * If there are pending partial truncations we need to keep the 10654 * old block copy around until they complete. This is because 10655 * the current b_data is not a perfect superset of the available 10656 * blocks. 10657 */ 10658 if (TAILQ_EMPTY(&indirdep->ir_trunc)) 10659 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount); 10660 else 10661 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10662 WORKLIST_REMOVE(&indirdep->ir_list); 10663 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list); 10664 indirdep->ir_bp = NULL; 10665 indirdep->ir_freeblks = freeblks; 10666 } 10667 10668 /* 10669 * Free an indirdep once it no longer has new pointers to track. 10670 */ 10671 static void 10672 free_indirdep(indirdep) 10673 struct indirdep *indirdep; 10674 { 10675 10676 KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc), 10677 ("free_indirdep: Indir trunc list not empty.")); 10678 KASSERT(LIST_EMPTY(&indirdep->ir_completehd), 10679 ("free_indirdep: Complete head not empty.")); 10680 KASSERT(LIST_EMPTY(&indirdep->ir_writehd), 10681 ("free_indirdep: write head not empty.")); 10682 KASSERT(LIST_EMPTY(&indirdep->ir_donehd), 10683 ("free_indirdep: done head not empty.")); 10684 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd), 10685 ("free_indirdep: deplist head not empty.")); 10686 KASSERT((indirdep->ir_state & DEPCOMPLETE), 10687 ("free_indirdep: %p still on newblk list.", indirdep)); 10688 KASSERT(indirdep->ir_saveddata == NULL, 10689 ("free_indirdep: %p still has saved data.", indirdep)); 10690 if (indirdep->ir_state & ONWORKLIST) 10691 WORKLIST_REMOVE(&indirdep->ir_list); 10692 WORKITEM_FREE(indirdep, D_INDIRDEP); 10693 } 10694 10695 /* 10696 * Called before a write to an indirdep. This routine is responsible for 10697 * rolling back pointers to a safe state which includes only those 10698 * allocindirs which have been completed. 10699 */ 10700 static void 10701 initiate_write_indirdep(indirdep, bp) 10702 struct indirdep *indirdep; 10703 struct buf *bp; 10704 { 10705 struct ufsmount *ump; 10706 10707 indirdep->ir_state |= IOSTARTED; 10708 if (indirdep->ir_state & GOINGAWAY) 10709 panic("disk_io_initiation: indirdep gone"); 10710 /* 10711 * If there are no remaining dependencies, this will be writing 10712 * the real pointers. 10713 */ 10714 if (LIST_EMPTY(&indirdep->ir_deplisthd) && 10715 TAILQ_EMPTY(&indirdep->ir_trunc)) 10716 return; 10717 /* 10718 * Replace up-to-date version with safe version. 10719 */ 10720 if (indirdep->ir_saveddata == NULL) { 10721 ump = VFSTOUFS(indirdep->ir_list.wk_mp); 10722 LOCK_OWNED(ump); 10723 FREE_LOCK(ump); 10724 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 10725 M_SOFTDEP_FLAGS); 10726 ACQUIRE_LOCK(ump); 10727 } 10728 indirdep->ir_state &= ~ATTACHED; 10729 indirdep->ir_state |= UNDONE; 10730 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10731 bcopy(indirdep->ir_savebp->b_data, bp->b_data, 10732 bp->b_bcount); 10733 } 10734 10735 /* 10736 * Called when an inode has been cleared in a cg bitmap. This finally 10737 * eliminates any canceled jaddrefs 10738 */ 10739 void 10740 softdep_setup_inofree(mp, bp, ino, wkhd) 10741 struct mount *mp; 10742 struct buf *bp; 10743 ino_t ino; 10744 struct workhead *wkhd; 10745 { 10746 struct worklist *wk, *wkn; 10747 struct inodedep *inodedep; 10748 struct ufsmount *ump; 10749 uint8_t *inosused; 10750 struct cg *cgp; 10751 struct fs *fs; 10752 10753 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 10754 ("softdep_setup_inofree called on non-softdep filesystem")); 10755 ump = VFSTOUFS(mp); 10756 ACQUIRE_LOCK(ump); 10757 fs = ump->um_fs; 10758 cgp = (struct cg *)bp->b_data; 10759 inosused = cg_inosused(cgp); 10760 if (isset(inosused, ino % fs->fs_ipg)) 10761 panic("softdep_setup_inofree: inode %ju not freed.", 10762 (uintmax_t)ino); 10763 if (inodedep_lookup(mp, ino, 0, &inodedep)) 10764 panic("softdep_setup_inofree: ino %ju has existing inodedep %p", 10765 (uintmax_t)ino, inodedep); 10766 if (wkhd) { 10767 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) { 10768 if (wk->wk_type != D_JADDREF) 10769 continue; 10770 WORKLIST_REMOVE(wk); 10771 /* 10772 * We can free immediately even if the jaddref 10773 * isn't attached in a background write as now 10774 * the bitmaps are reconciled. 10775 */ 10776 wk->wk_state |= COMPLETE | ATTACHED; 10777 free_jaddref(WK_JADDREF(wk)); 10778 } 10779 jwork_move(&bp->b_dep, wkhd); 10780 } 10781 FREE_LOCK(ump); 10782 } 10783 10784 10785 /* 10786 * Called via ffs_blkfree() after a set of frags has been cleared from a cg 10787 * map. Any dependencies waiting for the write to clear are added to the 10788 * buf's list and any jnewblks that are being canceled are discarded 10789 * immediately. 10790 */ 10791 void 10792 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 10793 struct mount *mp; 10794 struct buf *bp; 10795 ufs2_daddr_t blkno; 10796 int frags; 10797 struct workhead *wkhd; 10798 { 10799 struct bmsafemap *bmsafemap; 10800 struct jnewblk *jnewblk; 10801 struct ufsmount *ump; 10802 struct worklist *wk; 10803 struct fs *fs; 10804 #ifdef INVARIANTS 10805 uint8_t *blksfree; 10806 struct cg *cgp; 10807 ufs2_daddr_t jstart; 10808 ufs2_daddr_t jend; 10809 ufs2_daddr_t end; 10810 long bno; 10811 int i; 10812 #endif 10813 10814 CTR3(KTR_SUJ, 10815 "softdep_setup_blkfree: blkno %jd frags %d wk head %p", 10816 blkno, frags, wkhd); 10817 10818 ump = VFSTOUFS(mp); 10819 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 10820 ("softdep_setup_blkfree called on non-softdep filesystem")); 10821 ACQUIRE_LOCK(ump); 10822 /* Lookup the bmsafemap so we track when it is dirty. */ 10823 fs = ump->um_fs; 10824 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 10825 /* 10826 * Detach any jnewblks which have been canceled. They must linger 10827 * until the bitmap is cleared again by ffs_blkfree() to prevent 10828 * an unjournaled allocation from hitting the disk. 10829 */ 10830 if (wkhd) { 10831 while ((wk = LIST_FIRST(wkhd)) != NULL) { 10832 CTR2(KTR_SUJ, 10833 "softdep_setup_blkfree: blkno %jd wk type %d", 10834 blkno, wk->wk_type); 10835 WORKLIST_REMOVE(wk); 10836 if (wk->wk_type != D_JNEWBLK) { 10837 WORKLIST_INSERT(&bmsafemap->sm_freehd, wk); 10838 continue; 10839 } 10840 jnewblk = WK_JNEWBLK(wk); 10841 KASSERT(jnewblk->jn_state & GOINGAWAY, 10842 ("softdep_setup_blkfree: jnewblk not canceled.")); 10843 #ifdef INVARIANTS 10844 /* 10845 * Assert that this block is free in the bitmap 10846 * before we discard the jnewblk. 10847 */ 10848 cgp = (struct cg *)bp->b_data; 10849 blksfree = cg_blksfree(cgp); 10850 bno = dtogd(fs, jnewblk->jn_blkno); 10851 for (i = jnewblk->jn_oldfrags; 10852 i < jnewblk->jn_frags; i++) { 10853 if (isset(blksfree, bno + i)) 10854 continue; 10855 panic("softdep_setup_blkfree: not free"); 10856 } 10857 #endif 10858 /* 10859 * Even if it's not attached we can free immediately 10860 * as the new bitmap is correct. 10861 */ 10862 wk->wk_state |= COMPLETE | ATTACHED; 10863 free_jnewblk(jnewblk); 10864 } 10865 } 10866 10867 #ifdef INVARIANTS 10868 /* 10869 * Assert that we are not freeing a block which has an outstanding 10870 * allocation dependency. 10871 */ 10872 fs = VFSTOUFS(mp)->um_fs; 10873 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 10874 end = blkno + frags; 10875 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 10876 /* 10877 * Don't match against blocks that will be freed when the 10878 * background write is done. 10879 */ 10880 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) == 10881 (COMPLETE | DEPCOMPLETE)) 10882 continue; 10883 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags; 10884 jend = jnewblk->jn_blkno + jnewblk->jn_frags; 10885 if ((blkno >= jstart && blkno < jend) || 10886 (end > jstart && end <= jend)) { 10887 printf("state 0x%X %jd - %d %d dep %p\n", 10888 jnewblk->jn_state, jnewblk->jn_blkno, 10889 jnewblk->jn_oldfrags, jnewblk->jn_frags, 10890 jnewblk->jn_dep); 10891 panic("softdep_setup_blkfree: " 10892 "%jd-%jd(%d) overlaps with %jd-%jd", 10893 blkno, end, frags, jstart, jend); 10894 } 10895 } 10896 #endif 10897 FREE_LOCK(ump); 10898 } 10899 10900 /* 10901 * Revert a block allocation when the journal record that describes it 10902 * is not yet written. 10903 */ 10904 static int 10905 jnewblk_rollback(jnewblk, fs, cgp, blksfree) 10906 struct jnewblk *jnewblk; 10907 struct fs *fs; 10908 struct cg *cgp; 10909 uint8_t *blksfree; 10910 { 10911 ufs1_daddr_t fragno; 10912 long cgbno, bbase; 10913 int frags, blk; 10914 int i; 10915 10916 frags = 0; 10917 cgbno = dtogd(fs, jnewblk->jn_blkno); 10918 /* 10919 * We have to test which frags need to be rolled back. We may 10920 * be operating on a stale copy when doing background writes. 10921 */ 10922 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) 10923 if (isclr(blksfree, cgbno + i)) 10924 frags++; 10925 if (frags == 0) 10926 return (0); 10927 /* 10928 * This is mostly ffs_blkfree() sans some validation and 10929 * superblock updates. 10930 */ 10931 if (frags == fs->fs_frag) { 10932 fragno = fragstoblks(fs, cgbno); 10933 ffs_setblock(fs, blksfree, fragno); 10934 ffs_clusteracct(fs, cgp, fragno, 1); 10935 cgp->cg_cs.cs_nbfree++; 10936 } else { 10937 cgbno += jnewblk->jn_oldfrags; 10938 bbase = cgbno - fragnum(fs, cgbno); 10939 /* Decrement the old frags. */ 10940 blk = blkmap(fs, blksfree, bbase); 10941 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 10942 /* Deallocate the fragment */ 10943 for (i = 0; i < frags; i++) 10944 setbit(blksfree, cgbno + i); 10945 cgp->cg_cs.cs_nffree += frags; 10946 /* Add back in counts associated with the new frags */ 10947 blk = blkmap(fs, blksfree, bbase); 10948 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 10949 /* If a complete block has been reassembled, account for it. */ 10950 fragno = fragstoblks(fs, bbase); 10951 if (ffs_isblock(fs, blksfree, fragno)) { 10952 cgp->cg_cs.cs_nffree -= fs->fs_frag; 10953 ffs_clusteracct(fs, cgp, fragno, 1); 10954 cgp->cg_cs.cs_nbfree++; 10955 } 10956 } 10957 stat_jnewblk++; 10958 jnewblk->jn_state &= ~ATTACHED; 10959 jnewblk->jn_state |= UNDONE; 10960 10961 return (frags); 10962 } 10963 10964 static void 10965 initiate_write_bmsafemap(bmsafemap, bp) 10966 struct bmsafemap *bmsafemap; 10967 struct buf *bp; /* The cg block. */ 10968 { 10969 struct jaddref *jaddref; 10970 struct jnewblk *jnewblk; 10971 uint8_t *inosused; 10972 uint8_t *blksfree; 10973 struct cg *cgp; 10974 struct fs *fs; 10975 ino_t ino; 10976 10977 /* 10978 * If this is a background write, we did this at the time that 10979 * the copy was made, so do not need to do it again. 10980 */ 10981 if (bmsafemap->sm_state & IOSTARTED) 10982 return; 10983 bmsafemap->sm_state |= IOSTARTED; 10984 /* 10985 * Clear any inode allocations which are pending journal writes. 10986 */ 10987 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) { 10988 cgp = (struct cg *)bp->b_data; 10989 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 10990 inosused = cg_inosused(cgp); 10991 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) { 10992 ino = jaddref->ja_ino % fs->fs_ipg; 10993 if (isset(inosused, ino)) { 10994 if ((jaddref->ja_mode & IFMT) == IFDIR) 10995 cgp->cg_cs.cs_ndir--; 10996 cgp->cg_cs.cs_nifree++; 10997 clrbit(inosused, ino); 10998 jaddref->ja_state &= ~ATTACHED; 10999 jaddref->ja_state |= UNDONE; 11000 stat_jaddref++; 11001 } else 11002 panic("initiate_write_bmsafemap: inode %ju " 11003 "marked free", (uintmax_t)jaddref->ja_ino); 11004 } 11005 } 11006 /* 11007 * Clear any block allocations which are pending journal writes. 11008 */ 11009 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 11010 cgp = (struct cg *)bp->b_data; 11011 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11012 blksfree = cg_blksfree(cgp); 11013 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 11014 if (jnewblk_rollback(jnewblk, fs, cgp, blksfree)) 11015 continue; 11016 panic("initiate_write_bmsafemap: block %jd " 11017 "marked free", jnewblk->jn_blkno); 11018 } 11019 } 11020 /* 11021 * Move allocation lists to the written lists so they can be 11022 * cleared once the block write is complete. 11023 */ 11024 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr, 11025 inodedep, id_deps); 11026 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 11027 newblk, nb_deps); 11028 LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist, 11029 wk_list); 11030 } 11031 11032 /* 11033 * This routine is called during the completion interrupt 11034 * service routine for a disk write (from the procedure called 11035 * by the device driver to inform the filesystem caches of 11036 * a request completion). It should be called early in this 11037 * procedure, before the block is made available to other 11038 * processes or other routines are called. 11039 * 11040 */ 11041 static void 11042 softdep_disk_write_complete(bp) 11043 struct buf *bp; /* describes the completed disk write */ 11044 { 11045 struct worklist *wk; 11046 struct worklist *owk; 11047 struct ufsmount *ump; 11048 struct workhead reattach; 11049 struct freeblks *freeblks; 11050 struct buf *sbp; 11051 11052 ump = softdep_bp_to_mp(bp); 11053 KASSERT(LIST_EMPTY(&bp->b_dep) || ump != NULL, 11054 ("softdep_disk_write_complete: softdep_bp_to_mp returned NULL " 11055 "with outstanding dependencies for buffer %p", bp)); 11056 if (ump == NULL) 11057 return; 11058 /* 11059 * If an error occurred while doing the write, then the data 11060 * has not hit the disk and the dependencies cannot be processed. 11061 * But we do have to go through and roll forward any dependencies 11062 * that were rolled back before the disk write. 11063 */ 11064 sbp = NULL; 11065 ACQUIRE_LOCK(ump); 11066 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) { 11067 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 11068 switch (wk->wk_type) { 11069 11070 case D_PAGEDEP: 11071 handle_written_filepage(WK_PAGEDEP(wk), bp, 0); 11072 continue; 11073 11074 case D_INODEDEP: 11075 handle_written_inodeblock(WK_INODEDEP(wk), 11076 bp, 0); 11077 continue; 11078 11079 case D_BMSAFEMAP: 11080 handle_written_bmsafemap(WK_BMSAFEMAP(wk), 11081 bp, 0); 11082 continue; 11083 11084 case D_INDIRDEP: 11085 handle_written_indirdep(WK_INDIRDEP(wk), 11086 bp, &sbp, 0); 11087 continue; 11088 default: 11089 /* nothing to roll forward */ 11090 continue; 11091 } 11092 } 11093 FREE_LOCK(ump); 11094 if (sbp) 11095 brelse(sbp); 11096 return; 11097 } 11098 LIST_INIT(&reattach); 11099 11100 /* 11101 * Ump SU lock must not be released anywhere in this code segment. 11102 */ 11103 owk = NULL; 11104 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) { 11105 WORKLIST_REMOVE(wk); 11106 atomic_add_long(&dep_write[wk->wk_type], 1); 11107 if (wk == owk) 11108 panic("duplicate worklist: %p\n", wk); 11109 owk = wk; 11110 switch (wk->wk_type) { 11111 11112 case D_PAGEDEP: 11113 if (handle_written_filepage(WK_PAGEDEP(wk), bp, 11114 WRITESUCCEEDED)) 11115 WORKLIST_INSERT(&reattach, wk); 11116 continue; 11117 11118 case D_INODEDEP: 11119 if (handle_written_inodeblock(WK_INODEDEP(wk), bp, 11120 WRITESUCCEEDED)) 11121 WORKLIST_INSERT(&reattach, wk); 11122 continue; 11123 11124 case D_BMSAFEMAP: 11125 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp, 11126 WRITESUCCEEDED)) 11127 WORKLIST_INSERT(&reattach, wk); 11128 continue; 11129 11130 case D_MKDIR: 11131 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 11132 continue; 11133 11134 case D_ALLOCDIRECT: 11135 wk->wk_state |= COMPLETE; 11136 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL); 11137 continue; 11138 11139 case D_ALLOCINDIR: 11140 wk->wk_state |= COMPLETE; 11141 handle_allocindir_partdone(WK_ALLOCINDIR(wk)); 11142 continue; 11143 11144 case D_INDIRDEP: 11145 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp, 11146 WRITESUCCEEDED)) 11147 WORKLIST_INSERT(&reattach, wk); 11148 continue; 11149 11150 case D_FREEBLKS: 11151 wk->wk_state |= COMPLETE; 11152 freeblks = WK_FREEBLKS(wk); 11153 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE && 11154 LIST_EMPTY(&freeblks->fb_jblkdephd)) 11155 add_to_worklist(wk, WK_NODELAY); 11156 continue; 11157 11158 case D_FREEWORK: 11159 handle_written_freework(WK_FREEWORK(wk)); 11160 break; 11161 11162 case D_JSEGDEP: 11163 free_jsegdep(WK_JSEGDEP(wk)); 11164 continue; 11165 11166 case D_JSEG: 11167 handle_written_jseg(WK_JSEG(wk), bp); 11168 continue; 11169 11170 case D_SBDEP: 11171 if (handle_written_sbdep(WK_SBDEP(wk), bp)) 11172 WORKLIST_INSERT(&reattach, wk); 11173 continue; 11174 11175 case D_FREEDEP: 11176 free_freedep(WK_FREEDEP(wk)); 11177 continue; 11178 11179 default: 11180 panic("handle_disk_write_complete: Unknown type %s", 11181 TYPENAME(wk->wk_type)); 11182 /* NOTREACHED */ 11183 } 11184 } 11185 /* 11186 * Reattach any requests that must be redone. 11187 */ 11188 while ((wk = LIST_FIRST(&reattach)) != NULL) { 11189 WORKLIST_REMOVE(wk); 11190 WORKLIST_INSERT(&bp->b_dep, wk); 11191 } 11192 FREE_LOCK(ump); 11193 if (sbp) 11194 brelse(sbp); 11195 } 11196 11197 /* 11198 * Called from within softdep_disk_write_complete above. Note that 11199 * this routine is always called from interrupt level with further 11200 * splbio interrupts blocked. 11201 */ 11202 static void 11203 handle_allocdirect_partdone(adp, wkhd) 11204 struct allocdirect *adp; /* the completed allocdirect */ 11205 struct workhead *wkhd; /* Work to do when inode is writtne. */ 11206 { 11207 struct allocdirectlst *listhead; 11208 struct allocdirect *listadp; 11209 struct inodedep *inodedep; 11210 long bsize; 11211 11212 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11213 return; 11214 /* 11215 * The on-disk inode cannot claim to be any larger than the last 11216 * fragment that has been written. Otherwise, the on-disk inode 11217 * might have fragments that were not the last block in the file 11218 * which would corrupt the filesystem. Thus, we cannot free any 11219 * allocdirects after one whose ad_oldblkno claims a fragment as 11220 * these blocks must be rolled back to zero before writing the inode. 11221 * We check the currently active set of allocdirects in id_inoupdt 11222 * or id_extupdt as appropriate. 11223 */ 11224 inodedep = adp->ad_inodedep; 11225 bsize = inodedep->id_fs->fs_bsize; 11226 if (adp->ad_state & EXTDATA) 11227 listhead = &inodedep->id_extupdt; 11228 else 11229 listhead = &inodedep->id_inoupdt; 11230 TAILQ_FOREACH(listadp, listhead, ad_next) { 11231 /* found our block */ 11232 if (listadp == adp) 11233 break; 11234 /* continue if ad_oldlbn is not a fragment */ 11235 if (listadp->ad_oldsize == 0 || 11236 listadp->ad_oldsize == bsize) 11237 continue; 11238 /* hit a fragment */ 11239 return; 11240 } 11241 /* 11242 * If we have reached the end of the current list without 11243 * finding the just finished dependency, then it must be 11244 * on the future dependency list. Future dependencies cannot 11245 * be freed until they are moved to the current list. 11246 */ 11247 if (listadp == NULL) { 11248 #ifdef INVARIANTS 11249 if (adp->ad_state & EXTDATA) 11250 listhead = &inodedep->id_newextupdt; 11251 else 11252 listhead = &inodedep->id_newinoupdt; 11253 TAILQ_FOREACH(listadp, listhead, ad_next) 11254 /* found our block */ 11255 if (listadp == adp) 11256 break; 11257 if (listadp == NULL) 11258 panic("handle_allocdirect_partdone: lost dep"); 11259 #endif /* INVARIANTS */ 11260 return; 11261 } 11262 /* 11263 * If we have found the just finished dependency, then queue 11264 * it along with anything that follows it that is complete. 11265 * Since the pointer has not yet been written in the inode 11266 * as the dependency prevents it, place the allocdirect on the 11267 * bufwait list where it will be freed once the pointer is 11268 * valid. 11269 */ 11270 if (wkhd == NULL) 11271 wkhd = &inodedep->id_bufwait; 11272 for (; adp; adp = listadp) { 11273 listadp = TAILQ_NEXT(adp, ad_next); 11274 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11275 return; 11276 TAILQ_REMOVE(listhead, adp, ad_next); 11277 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list); 11278 } 11279 } 11280 11281 /* 11282 * Called from within softdep_disk_write_complete above. This routine 11283 * completes successfully written allocindirs. 11284 */ 11285 static void 11286 handle_allocindir_partdone(aip) 11287 struct allocindir *aip; /* the completed allocindir */ 11288 { 11289 struct indirdep *indirdep; 11290 11291 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE) 11292 return; 11293 indirdep = aip->ai_indirdep; 11294 LIST_REMOVE(aip, ai_next); 11295 /* 11296 * Don't set a pointer while the buffer is undergoing IO or while 11297 * we have active truncations. 11298 */ 11299 if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) { 11300 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next); 11301 return; 11302 } 11303 if (indirdep->ir_state & UFS1FMT) 11304 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11305 aip->ai_newblkno; 11306 else 11307 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11308 aip->ai_newblkno; 11309 /* 11310 * Await the pointer write before freeing the allocindir. 11311 */ 11312 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next); 11313 } 11314 11315 /* 11316 * Release segments held on a jwork list. 11317 */ 11318 static void 11319 handle_jwork(wkhd) 11320 struct workhead *wkhd; 11321 { 11322 struct worklist *wk; 11323 11324 while ((wk = LIST_FIRST(wkhd)) != NULL) { 11325 WORKLIST_REMOVE(wk); 11326 switch (wk->wk_type) { 11327 case D_JSEGDEP: 11328 free_jsegdep(WK_JSEGDEP(wk)); 11329 continue; 11330 case D_FREEDEP: 11331 free_freedep(WK_FREEDEP(wk)); 11332 continue; 11333 case D_FREEFRAG: 11334 rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep)); 11335 WORKITEM_FREE(wk, D_FREEFRAG); 11336 continue; 11337 case D_FREEWORK: 11338 handle_written_freework(WK_FREEWORK(wk)); 11339 continue; 11340 default: 11341 panic("handle_jwork: Unknown type %s\n", 11342 TYPENAME(wk->wk_type)); 11343 } 11344 } 11345 } 11346 11347 /* 11348 * Handle the bufwait list on an inode when it is safe to release items 11349 * held there. This normally happens after an inode block is written but 11350 * may be delayed and handled later if there are pending journal items that 11351 * are not yet safe to be released. 11352 */ 11353 static struct freefile * 11354 handle_bufwait(inodedep, refhd) 11355 struct inodedep *inodedep; 11356 struct workhead *refhd; 11357 { 11358 struct jaddref *jaddref; 11359 struct freefile *freefile; 11360 struct worklist *wk; 11361 11362 freefile = NULL; 11363 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) { 11364 WORKLIST_REMOVE(wk); 11365 switch (wk->wk_type) { 11366 case D_FREEFILE: 11367 /* 11368 * We defer adding freefile to the worklist 11369 * until all other additions have been made to 11370 * ensure that it will be done after all the 11371 * old blocks have been freed. 11372 */ 11373 if (freefile != NULL) 11374 panic("handle_bufwait: freefile"); 11375 freefile = WK_FREEFILE(wk); 11376 continue; 11377 11378 case D_MKDIR: 11379 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT); 11380 continue; 11381 11382 case D_DIRADD: 11383 diradd_inode_written(WK_DIRADD(wk), inodedep); 11384 continue; 11385 11386 case D_FREEFRAG: 11387 wk->wk_state |= COMPLETE; 11388 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE) 11389 add_to_worklist(wk, 0); 11390 continue; 11391 11392 case D_DIRREM: 11393 wk->wk_state |= COMPLETE; 11394 add_to_worklist(wk, 0); 11395 continue; 11396 11397 case D_ALLOCDIRECT: 11398 case D_ALLOCINDIR: 11399 free_newblk(WK_NEWBLK(wk)); 11400 continue; 11401 11402 case D_JNEWBLK: 11403 wk->wk_state |= COMPLETE; 11404 free_jnewblk(WK_JNEWBLK(wk)); 11405 continue; 11406 11407 /* 11408 * Save freed journal segments and add references on 11409 * the supplied list which will delay their release 11410 * until the cg bitmap is cleared on disk. 11411 */ 11412 case D_JSEGDEP: 11413 if (refhd == NULL) 11414 free_jsegdep(WK_JSEGDEP(wk)); 11415 else 11416 WORKLIST_INSERT(refhd, wk); 11417 continue; 11418 11419 case D_JADDREF: 11420 jaddref = WK_JADDREF(wk); 11421 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 11422 if_deps); 11423 /* 11424 * Transfer any jaddrefs to the list to be freed with 11425 * the bitmap if we're handling a removed file. 11426 */ 11427 if (refhd == NULL) { 11428 wk->wk_state |= COMPLETE; 11429 free_jaddref(jaddref); 11430 } else 11431 WORKLIST_INSERT(refhd, wk); 11432 continue; 11433 11434 default: 11435 panic("handle_bufwait: Unknown type %p(%s)", 11436 wk, TYPENAME(wk->wk_type)); 11437 /* NOTREACHED */ 11438 } 11439 } 11440 return (freefile); 11441 } 11442 /* 11443 * Called from within softdep_disk_write_complete above to restore 11444 * in-memory inode block contents to their most up-to-date state. Note 11445 * that this routine is always called from interrupt level with further 11446 * interrupts from this device blocked. 11447 * 11448 * If the write did not succeed, we will do all the roll-forward 11449 * operations, but we will not take the actions that will allow its 11450 * dependencies to be processed. 11451 */ 11452 static int 11453 handle_written_inodeblock(inodedep, bp, flags) 11454 struct inodedep *inodedep; 11455 struct buf *bp; /* buffer containing the inode block */ 11456 int flags; 11457 { 11458 struct freefile *freefile; 11459 struct allocdirect *adp, *nextadp; 11460 struct ufs1_dinode *dp1 = NULL; 11461 struct ufs2_dinode *dp2 = NULL; 11462 struct workhead wkhd; 11463 int hadchanges, fstype; 11464 ino_t freelink; 11465 11466 LIST_INIT(&wkhd); 11467 hadchanges = 0; 11468 freefile = NULL; 11469 if ((inodedep->id_state & IOSTARTED) == 0) 11470 panic("handle_written_inodeblock: not started"); 11471 inodedep->id_state &= ~IOSTARTED; 11472 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) { 11473 fstype = UFS1; 11474 dp1 = (struct ufs1_dinode *)bp->b_data + 11475 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11476 freelink = dp1->di_freelink; 11477 } else { 11478 fstype = UFS2; 11479 dp2 = (struct ufs2_dinode *)bp->b_data + 11480 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11481 freelink = dp2->di_freelink; 11482 } 11483 /* 11484 * Leave this inodeblock dirty until it's in the list. 11485 */ 11486 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED && 11487 (flags & WRITESUCCEEDED)) { 11488 struct inodedep *inon; 11489 11490 inon = TAILQ_NEXT(inodedep, id_unlinked); 11491 if ((inon == NULL && freelink == 0) || 11492 (inon && inon->id_ino == freelink)) { 11493 if (inon) 11494 inon->id_state |= UNLINKPREV; 11495 inodedep->id_state |= UNLINKNEXT; 11496 } 11497 hadchanges = 1; 11498 } 11499 /* 11500 * If we had to rollback the inode allocation because of 11501 * bitmaps being incomplete, then simply restore it. 11502 * Keep the block dirty so that it will not be reclaimed until 11503 * all associated dependencies have been cleared and the 11504 * corresponding updates written to disk. 11505 */ 11506 if (inodedep->id_savedino1 != NULL) { 11507 hadchanges = 1; 11508 if (fstype == UFS1) 11509 *dp1 = *inodedep->id_savedino1; 11510 else 11511 *dp2 = *inodedep->id_savedino2; 11512 free(inodedep->id_savedino1, M_SAVEDINO); 11513 inodedep->id_savedino1 = NULL; 11514 if ((bp->b_flags & B_DELWRI) == 0) 11515 stat_inode_bitmap++; 11516 bdirty(bp); 11517 /* 11518 * If the inode is clear here and GOINGAWAY it will never 11519 * be written. Process the bufwait and clear any pending 11520 * work which may include the freefile. 11521 */ 11522 if (inodedep->id_state & GOINGAWAY) 11523 goto bufwait; 11524 return (1); 11525 } 11526 if (flags & WRITESUCCEEDED) 11527 inodedep->id_state |= COMPLETE; 11528 /* 11529 * Roll forward anything that had to be rolled back before 11530 * the inode could be updated. 11531 */ 11532 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) { 11533 nextadp = TAILQ_NEXT(adp, ad_next); 11534 if (adp->ad_state & ATTACHED) 11535 panic("handle_written_inodeblock: new entry"); 11536 if (fstype == UFS1) { 11537 if (adp->ad_offset < UFS_NDADDR) { 11538 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11539 panic("%s %s #%jd mismatch %d != %jd", 11540 "handle_written_inodeblock:", 11541 "direct pointer", 11542 (intmax_t)adp->ad_offset, 11543 dp1->di_db[adp->ad_offset], 11544 (intmax_t)adp->ad_oldblkno); 11545 dp1->di_db[adp->ad_offset] = adp->ad_newblkno; 11546 } else { 11547 if (dp1->di_ib[adp->ad_offset - UFS_NDADDR] != 11548 0) 11549 panic("%s: %s #%jd allocated as %d", 11550 "handle_written_inodeblock", 11551 "indirect pointer", 11552 (intmax_t)adp->ad_offset - 11553 UFS_NDADDR, 11554 dp1->di_ib[adp->ad_offset - 11555 UFS_NDADDR]); 11556 dp1->di_ib[adp->ad_offset - UFS_NDADDR] = 11557 adp->ad_newblkno; 11558 } 11559 } else { 11560 if (adp->ad_offset < UFS_NDADDR) { 11561 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11562 panic("%s: %s #%jd %s %jd != %jd", 11563 "handle_written_inodeblock", 11564 "direct pointer", 11565 (intmax_t)adp->ad_offset, "mismatch", 11566 (intmax_t)dp2->di_db[adp->ad_offset], 11567 (intmax_t)adp->ad_oldblkno); 11568 dp2->di_db[adp->ad_offset] = adp->ad_newblkno; 11569 } else { 11570 if (dp2->di_ib[adp->ad_offset - UFS_NDADDR] != 11571 0) 11572 panic("%s: %s #%jd allocated as %jd", 11573 "handle_written_inodeblock", 11574 "indirect pointer", 11575 (intmax_t)adp->ad_offset - 11576 UFS_NDADDR, 11577 (intmax_t) 11578 dp2->di_ib[adp->ad_offset - 11579 UFS_NDADDR]); 11580 dp2->di_ib[adp->ad_offset - UFS_NDADDR] = 11581 adp->ad_newblkno; 11582 } 11583 } 11584 adp->ad_state &= ~UNDONE; 11585 adp->ad_state |= ATTACHED; 11586 hadchanges = 1; 11587 } 11588 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) { 11589 nextadp = TAILQ_NEXT(adp, ad_next); 11590 if (adp->ad_state & ATTACHED) 11591 panic("handle_written_inodeblock: new entry"); 11592 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno) 11593 panic("%s: direct pointers #%jd %s %jd != %jd", 11594 "handle_written_inodeblock", 11595 (intmax_t)adp->ad_offset, "mismatch", 11596 (intmax_t)dp2->di_extb[adp->ad_offset], 11597 (intmax_t)adp->ad_oldblkno); 11598 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno; 11599 adp->ad_state &= ~UNDONE; 11600 adp->ad_state |= ATTACHED; 11601 hadchanges = 1; 11602 } 11603 if (hadchanges && (bp->b_flags & B_DELWRI) == 0) 11604 stat_direct_blk_ptrs++; 11605 /* 11606 * Reset the file size to its most up-to-date value. 11607 */ 11608 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1) 11609 panic("handle_written_inodeblock: bad size"); 11610 if (inodedep->id_savednlink > UFS_LINK_MAX) 11611 panic("handle_written_inodeblock: Invalid link count " 11612 "%jd for inodedep %p", (uintmax_t)inodedep->id_savednlink, 11613 inodedep); 11614 if (fstype == UFS1) { 11615 if (dp1->di_nlink != inodedep->id_savednlink) { 11616 dp1->di_nlink = inodedep->id_savednlink; 11617 hadchanges = 1; 11618 } 11619 if (dp1->di_size != inodedep->id_savedsize) { 11620 dp1->di_size = inodedep->id_savedsize; 11621 hadchanges = 1; 11622 } 11623 } else { 11624 if (dp2->di_nlink != inodedep->id_savednlink) { 11625 dp2->di_nlink = inodedep->id_savednlink; 11626 hadchanges = 1; 11627 } 11628 if (dp2->di_size != inodedep->id_savedsize) { 11629 dp2->di_size = inodedep->id_savedsize; 11630 hadchanges = 1; 11631 } 11632 if (dp2->di_extsize != inodedep->id_savedextsize) { 11633 dp2->di_extsize = inodedep->id_savedextsize; 11634 hadchanges = 1; 11635 } 11636 } 11637 inodedep->id_savedsize = -1; 11638 inodedep->id_savedextsize = -1; 11639 inodedep->id_savednlink = -1; 11640 /* 11641 * If there were any rollbacks in the inode block, then it must be 11642 * marked dirty so that its will eventually get written back in 11643 * its correct form. 11644 */ 11645 if (hadchanges) { 11646 if (fstype == UFS2) 11647 ffs_update_dinode_ckhash(inodedep->id_fs, dp2); 11648 bdirty(bp); 11649 } 11650 bufwait: 11651 /* 11652 * If the write did not succeed, we have done all the roll-forward 11653 * operations, but we cannot take the actions that will allow its 11654 * dependencies to be processed. 11655 */ 11656 if ((flags & WRITESUCCEEDED) == 0) 11657 return (hadchanges); 11658 /* 11659 * Process any allocdirects that completed during the update. 11660 */ 11661 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 11662 handle_allocdirect_partdone(adp, &wkhd); 11663 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 11664 handle_allocdirect_partdone(adp, &wkhd); 11665 /* 11666 * Process deallocations that were held pending until the 11667 * inode had been written to disk. Freeing of the inode 11668 * is delayed until after all blocks have been freed to 11669 * avoid creation of new <vfsid, inum, lbn> triples 11670 * before the old ones have been deleted. Completely 11671 * unlinked inodes are not processed until the unlinked 11672 * inode list is written or the last reference is removed. 11673 */ 11674 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) { 11675 freefile = handle_bufwait(inodedep, NULL); 11676 if (freefile && !LIST_EMPTY(&wkhd)) { 11677 WORKLIST_INSERT(&wkhd, &freefile->fx_list); 11678 freefile = NULL; 11679 } 11680 } 11681 /* 11682 * Move rolled forward dependency completions to the bufwait list 11683 * now that those that were already written have been processed. 11684 */ 11685 if (!LIST_EMPTY(&wkhd) && hadchanges == 0) 11686 panic("handle_written_inodeblock: bufwait but no changes"); 11687 jwork_move(&inodedep->id_bufwait, &wkhd); 11688 11689 if (freefile != NULL) { 11690 /* 11691 * If the inode is goingaway it was never written. Fake up 11692 * the state here so free_inodedep() can succeed. 11693 */ 11694 if (inodedep->id_state & GOINGAWAY) 11695 inodedep->id_state |= COMPLETE | DEPCOMPLETE; 11696 if (free_inodedep(inodedep) == 0) 11697 panic("handle_written_inodeblock: live inodedep %p", 11698 inodedep); 11699 add_to_worklist(&freefile->fx_list, 0); 11700 return (0); 11701 } 11702 11703 /* 11704 * If no outstanding dependencies, free it. 11705 */ 11706 if (free_inodedep(inodedep) || 11707 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 && 11708 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 && 11709 TAILQ_FIRST(&inodedep->id_extupdt) == 0 && 11710 LIST_FIRST(&inodedep->id_bufwait) == 0)) 11711 return (0); 11712 return (hadchanges); 11713 } 11714 11715 /* 11716 * Perform needed roll-forwards and kick off any dependencies that 11717 * can now be processed. 11718 * 11719 * If the write did not succeed, we will do all the roll-forward 11720 * operations, but we will not take the actions that will allow its 11721 * dependencies to be processed. 11722 */ 11723 static int 11724 handle_written_indirdep(indirdep, bp, bpp, flags) 11725 struct indirdep *indirdep; 11726 struct buf *bp; 11727 struct buf **bpp; 11728 int flags; 11729 { 11730 struct allocindir *aip; 11731 struct buf *sbp; 11732 int chgs; 11733 11734 if (indirdep->ir_state & GOINGAWAY) 11735 panic("handle_written_indirdep: indirdep gone"); 11736 if ((indirdep->ir_state & IOSTARTED) == 0) 11737 panic("handle_written_indirdep: IO not started"); 11738 chgs = 0; 11739 /* 11740 * If there were rollbacks revert them here. 11741 */ 11742 if (indirdep->ir_saveddata) { 11743 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount); 11744 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11745 free(indirdep->ir_saveddata, M_INDIRDEP); 11746 indirdep->ir_saveddata = NULL; 11747 } 11748 chgs = 1; 11749 } 11750 indirdep->ir_state &= ~(UNDONE | IOSTARTED); 11751 indirdep->ir_state |= ATTACHED; 11752 /* 11753 * If the write did not succeed, we have done all the roll-forward 11754 * operations, but we cannot take the actions that will allow its 11755 * dependencies to be processed. 11756 */ 11757 if ((flags & WRITESUCCEEDED) == 0) { 11758 stat_indir_blk_ptrs++; 11759 bdirty(bp); 11760 return (1); 11761 } 11762 /* 11763 * Move allocindirs with written pointers to the completehd if 11764 * the indirdep's pointer is not yet written. Otherwise 11765 * free them here. 11766 */ 11767 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) { 11768 LIST_REMOVE(aip, ai_next); 11769 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 11770 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip, 11771 ai_next); 11772 newblk_freefrag(&aip->ai_block); 11773 continue; 11774 } 11775 free_newblk(&aip->ai_block); 11776 } 11777 /* 11778 * Move allocindirs that have finished dependency processing from 11779 * the done list to the write list after updating the pointers. 11780 */ 11781 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11782 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) { 11783 handle_allocindir_partdone(aip); 11784 if (aip == LIST_FIRST(&indirdep->ir_donehd)) 11785 panic("disk_write_complete: not gone"); 11786 chgs = 1; 11787 } 11788 } 11789 /* 11790 * Preserve the indirdep if there were any changes or if it is not 11791 * yet valid on disk. 11792 */ 11793 if (chgs) { 11794 stat_indir_blk_ptrs++; 11795 bdirty(bp); 11796 return (1); 11797 } 11798 /* 11799 * If there were no changes we can discard the savedbp and detach 11800 * ourselves from the buf. We are only carrying completed pointers 11801 * in this case. 11802 */ 11803 sbp = indirdep->ir_savebp; 11804 sbp->b_flags |= B_INVAL | B_NOCACHE; 11805 indirdep->ir_savebp = NULL; 11806 indirdep->ir_bp = NULL; 11807 if (*bpp != NULL) 11808 panic("handle_written_indirdep: bp already exists."); 11809 *bpp = sbp; 11810 /* 11811 * The indirdep may not be freed until its parent points at it. 11812 */ 11813 if (indirdep->ir_state & DEPCOMPLETE) 11814 free_indirdep(indirdep); 11815 11816 return (0); 11817 } 11818 11819 /* 11820 * Process a diradd entry after its dependent inode has been written. 11821 * This routine must be called with splbio interrupts blocked. 11822 */ 11823 static void 11824 diradd_inode_written(dap, inodedep) 11825 struct diradd *dap; 11826 struct inodedep *inodedep; 11827 { 11828 11829 dap->da_state |= COMPLETE; 11830 complete_diradd(dap); 11831 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 11832 } 11833 11834 /* 11835 * Returns true if the bmsafemap will have rollbacks when written. Must only 11836 * be called with the per-filesystem lock and the buf lock on the cg held. 11837 */ 11838 static int 11839 bmsafemap_backgroundwrite(bmsafemap, bp) 11840 struct bmsafemap *bmsafemap; 11841 struct buf *bp; 11842 { 11843 int dirty; 11844 11845 LOCK_OWNED(VFSTOUFS(bmsafemap->sm_list.wk_mp)); 11846 dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) | 11847 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd); 11848 /* 11849 * If we're initiating a background write we need to process the 11850 * rollbacks as they exist now, not as they exist when IO starts. 11851 * No other consumers will look at the contents of the shadowed 11852 * buf so this is safe to do here. 11853 */ 11854 if (bp->b_xflags & BX_BKGRDMARKER) 11855 initiate_write_bmsafemap(bmsafemap, bp); 11856 11857 return (dirty); 11858 } 11859 11860 /* 11861 * Re-apply an allocation when a cg write is complete. 11862 */ 11863 static int 11864 jnewblk_rollforward(jnewblk, fs, cgp, blksfree) 11865 struct jnewblk *jnewblk; 11866 struct fs *fs; 11867 struct cg *cgp; 11868 uint8_t *blksfree; 11869 { 11870 ufs1_daddr_t fragno; 11871 ufs2_daddr_t blkno; 11872 long cgbno, bbase; 11873 int frags, blk; 11874 int i; 11875 11876 frags = 0; 11877 cgbno = dtogd(fs, jnewblk->jn_blkno); 11878 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) { 11879 if (isclr(blksfree, cgbno + i)) 11880 panic("jnewblk_rollforward: re-allocated fragment"); 11881 frags++; 11882 } 11883 if (frags == fs->fs_frag) { 11884 blkno = fragstoblks(fs, cgbno); 11885 ffs_clrblock(fs, blksfree, (long)blkno); 11886 ffs_clusteracct(fs, cgp, blkno, -1); 11887 cgp->cg_cs.cs_nbfree--; 11888 } else { 11889 bbase = cgbno - fragnum(fs, cgbno); 11890 cgbno += jnewblk->jn_oldfrags; 11891 /* If a complete block had been reassembled, account for it. */ 11892 fragno = fragstoblks(fs, bbase); 11893 if (ffs_isblock(fs, blksfree, fragno)) { 11894 cgp->cg_cs.cs_nffree += fs->fs_frag; 11895 ffs_clusteracct(fs, cgp, fragno, -1); 11896 cgp->cg_cs.cs_nbfree--; 11897 } 11898 /* Decrement the old frags. */ 11899 blk = blkmap(fs, blksfree, bbase); 11900 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 11901 /* Allocate the fragment */ 11902 for (i = 0; i < frags; i++) 11903 clrbit(blksfree, cgbno + i); 11904 cgp->cg_cs.cs_nffree -= frags; 11905 /* Add back in counts associated with the new frags */ 11906 blk = blkmap(fs, blksfree, bbase); 11907 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 11908 } 11909 return (frags); 11910 } 11911 11912 /* 11913 * Complete a write to a bmsafemap structure. Roll forward any bitmap 11914 * changes if it's not a background write. Set all written dependencies 11915 * to DEPCOMPLETE and free the structure if possible. 11916 * 11917 * If the write did not succeed, we will do all the roll-forward 11918 * operations, but we will not take the actions that will allow its 11919 * dependencies to be processed. 11920 */ 11921 static int 11922 handle_written_bmsafemap(bmsafemap, bp, flags) 11923 struct bmsafemap *bmsafemap; 11924 struct buf *bp; 11925 int flags; 11926 { 11927 struct newblk *newblk; 11928 struct inodedep *inodedep; 11929 struct jaddref *jaddref, *jatmp; 11930 struct jnewblk *jnewblk, *jntmp; 11931 struct ufsmount *ump; 11932 uint8_t *inosused; 11933 uint8_t *blksfree; 11934 struct cg *cgp; 11935 struct fs *fs; 11936 ino_t ino; 11937 int foreground; 11938 int chgs; 11939 11940 if ((bmsafemap->sm_state & IOSTARTED) == 0) 11941 panic("handle_written_bmsafemap: Not started\n"); 11942 ump = VFSTOUFS(bmsafemap->sm_list.wk_mp); 11943 chgs = 0; 11944 bmsafemap->sm_state &= ~IOSTARTED; 11945 foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0; 11946 /* 11947 * If write was successful, release journal work that was waiting 11948 * on the write. Otherwise move the work back. 11949 */ 11950 if (flags & WRITESUCCEEDED) 11951 handle_jwork(&bmsafemap->sm_freewr); 11952 else 11953 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, 11954 worklist, wk_list); 11955 11956 /* 11957 * Restore unwritten inode allocation pending jaddref writes. 11958 */ 11959 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) { 11960 cgp = (struct cg *)bp->b_data; 11961 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11962 inosused = cg_inosused(cgp); 11963 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd, 11964 ja_bmdeps, jatmp) { 11965 if ((jaddref->ja_state & UNDONE) == 0) 11966 continue; 11967 ino = jaddref->ja_ino % fs->fs_ipg; 11968 if (isset(inosused, ino)) 11969 panic("handle_written_bmsafemap: " 11970 "re-allocated inode"); 11971 /* Do the roll-forward only if it's a real copy. */ 11972 if (foreground) { 11973 if ((jaddref->ja_mode & IFMT) == IFDIR) 11974 cgp->cg_cs.cs_ndir++; 11975 cgp->cg_cs.cs_nifree--; 11976 setbit(inosused, ino); 11977 chgs = 1; 11978 } 11979 jaddref->ja_state &= ~UNDONE; 11980 jaddref->ja_state |= ATTACHED; 11981 free_jaddref(jaddref); 11982 } 11983 } 11984 /* 11985 * Restore any block allocations which are pending journal writes. 11986 */ 11987 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 11988 cgp = (struct cg *)bp->b_data; 11989 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11990 blksfree = cg_blksfree(cgp); 11991 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps, 11992 jntmp) { 11993 if ((jnewblk->jn_state & UNDONE) == 0) 11994 continue; 11995 /* Do the roll-forward only if it's a real copy. */ 11996 if (foreground && 11997 jnewblk_rollforward(jnewblk, fs, cgp, blksfree)) 11998 chgs = 1; 11999 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK); 12000 jnewblk->jn_state |= ATTACHED; 12001 free_jnewblk(jnewblk); 12002 } 12003 } 12004 /* 12005 * If the write did not succeed, we have done all the roll-forward 12006 * operations, but we cannot take the actions that will allow its 12007 * dependencies to be processed. 12008 */ 12009 if ((flags & WRITESUCCEEDED) == 0) { 12010 LIST_CONCAT(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 12011 newblk, nb_deps); 12012 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, 12013 worklist, wk_list); 12014 if (foreground) 12015 bdirty(bp); 12016 return (1); 12017 } 12018 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) { 12019 newblk->nb_state |= DEPCOMPLETE; 12020 newblk->nb_state &= ~ONDEPLIST; 12021 newblk->nb_bmsafemap = NULL; 12022 LIST_REMOVE(newblk, nb_deps); 12023 if (newblk->nb_list.wk_type == D_ALLOCDIRECT) 12024 handle_allocdirect_partdone( 12025 WK_ALLOCDIRECT(&newblk->nb_list), NULL); 12026 else if (newblk->nb_list.wk_type == D_ALLOCINDIR) 12027 handle_allocindir_partdone( 12028 WK_ALLOCINDIR(&newblk->nb_list)); 12029 else if (newblk->nb_list.wk_type != D_NEWBLK) 12030 panic("handle_written_bmsafemap: Unexpected type: %s", 12031 TYPENAME(newblk->nb_list.wk_type)); 12032 } 12033 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) { 12034 inodedep->id_state |= DEPCOMPLETE; 12035 inodedep->id_state &= ~ONDEPLIST; 12036 LIST_REMOVE(inodedep, id_deps); 12037 inodedep->id_bmsafemap = NULL; 12038 } 12039 LIST_REMOVE(bmsafemap, sm_next); 12040 if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) && 12041 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) && 12042 LIST_EMPTY(&bmsafemap->sm_newblkhd) && 12043 LIST_EMPTY(&bmsafemap->sm_inodedephd) && 12044 LIST_EMPTY(&bmsafemap->sm_freehd)) { 12045 LIST_REMOVE(bmsafemap, sm_hash); 12046 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 12047 return (0); 12048 } 12049 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 12050 if (foreground) 12051 bdirty(bp); 12052 return (1); 12053 } 12054 12055 /* 12056 * Try to free a mkdir dependency. 12057 */ 12058 static void 12059 complete_mkdir(mkdir) 12060 struct mkdir *mkdir; 12061 { 12062 struct diradd *dap; 12063 12064 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE) 12065 return; 12066 LIST_REMOVE(mkdir, md_mkdirs); 12067 dap = mkdir->md_diradd; 12068 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 12069 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) { 12070 dap->da_state |= DEPCOMPLETE; 12071 complete_diradd(dap); 12072 } 12073 WORKITEM_FREE(mkdir, D_MKDIR); 12074 } 12075 12076 /* 12077 * Handle the completion of a mkdir dependency. 12078 */ 12079 static void 12080 handle_written_mkdir(mkdir, type) 12081 struct mkdir *mkdir; 12082 int type; 12083 { 12084 12085 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type) 12086 panic("handle_written_mkdir: bad type"); 12087 mkdir->md_state |= COMPLETE; 12088 complete_mkdir(mkdir); 12089 } 12090 12091 static int 12092 free_pagedep(pagedep) 12093 struct pagedep *pagedep; 12094 { 12095 int i; 12096 12097 if (pagedep->pd_state & NEWBLOCK) 12098 return (0); 12099 if (!LIST_EMPTY(&pagedep->pd_dirremhd)) 12100 return (0); 12101 for (i = 0; i < DAHASHSZ; i++) 12102 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i])) 12103 return (0); 12104 if (!LIST_EMPTY(&pagedep->pd_pendinghd)) 12105 return (0); 12106 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd)) 12107 return (0); 12108 if (pagedep->pd_state & ONWORKLIST) 12109 WORKLIST_REMOVE(&pagedep->pd_list); 12110 LIST_REMOVE(pagedep, pd_hash); 12111 WORKITEM_FREE(pagedep, D_PAGEDEP); 12112 12113 return (1); 12114 } 12115 12116 /* 12117 * Called from within softdep_disk_write_complete above. 12118 * A write operation was just completed. Removed inodes can 12119 * now be freed and associated block pointers may be committed. 12120 * Note that this routine is always called from interrupt level 12121 * with further interrupts from this device blocked. 12122 * 12123 * If the write did not succeed, we will do all the roll-forward 12124 * operations, but we will not take the actions that will allow its 12125 * dependencies to be processed. 12126 */ 12127 static int 12128 handle_written_filepage(pagedep, bp, flags) 12129 struct pagedep *pagedep; 12130 struct buf *bp; /* buffer containing the written page */ 12131 int flags; 12132 { 12133 struct dirrem *dirrem; 12134 struct diradd *dap, *nextdap; 12135 struct direct *ep; 12136 int i, chgs; 12137 12138 if ((pagedep->pd_state & IOSTARTED) == 0) 12139 panic("handle_written_filepage: not started"); 12140 pagedep->pd_state &= ~IOSTARTED; 12141 if ((flags & WRITESUCCEEDED) == 0) 12142 goto rollforward; 12143 /* 12144 * Process any directory removals that have been committed. 12145 */ 12146 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) { 12147 LIST_REMOVE(dirrem, dm_next); 12148 dirrem->dm_state |= COMPLETE; 12149 dirrem->dm_dirinum = pagedep->pd_ino; 12150 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 12151 ("handle_written_filepage: Journal entries not written.")); 12152 add_to_worklist(&dirrem->dm_list, 0); 12153 } 12154 /* 12155 * Free any directory additions that have been committed. 12156 * If it is a newly allocated block, we have to wait until 12157 * the on-disk directory inode claims the new block. 12158 */ 12159 if ((pagedep->pd_state & NEWBLOCK) == 0) 12160 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 12161 free_diradd(dap, NULL); 12162 rollforward: 12163 /* 12164 * Uncommitted directory entries must be restored. 12165 */ 12166 for (chgs = 0, i = 0; i < DAHASHSZ; i++) { 12167 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap; 12168 dap = nextdap) { 12169 nextdap = LIST_NEXT(dap, da_pdlist); 12170 if (dap->da_state & ATTACHED) 12171 panic("handle_written_filepage: attached"); 12172 ep = (struct direct *) 12173 ((char *)bp->b_data + dap->da_offset); 12174 ep->d_ino = dap->da_newinum; 12175 dap->da_state &= ~UNDONE; 12176 dap->da_state |= ATTACHED; 12177 chgs = 1; 12178 /* 12179 * If the inode referenced by the directory has 12180 * been written out, then the dependency can be 12181 * moved to the pending list. 12182 */ 12183 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 12184 LIST_REMOVE(dap, da_pdlist); 12185 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, 12186 da_pdlist); 12187 } 12188 } 12189 } 12190 /* 12191 * If there were any rollbacks in the directory, then it must be 12192 * marked dirty so that its will eventually get written back in 12193 * its correct form. 12194 */ 12195 if (chgs || (flags & WRITESUCCEEDED) == 0) { 12196 if ((bp->b_flags & B_DELWRI) == 0) 12197 stat_dir_entry++; 12198 bdirty(bp); 12199 return (1); 12200 } 12201 /* 12202 * If we are not waiting for a new directory block to be 12203 * claimed by its inode, then the pagedep will be freed. 12204 * Otherwise it will remain to track any new entries on 12205 * the page in case they are fsync'ed. 12206 */ 12207 free_pagedep(pagedep); 12208 return (0); 12209 } 12210 12211 /* 12212 * Writing back in-core inode structures. 12213 * 12214 * The filesystem only accesses an inode's contents when it occupies an 12215 * "in-core" inode structure. These "in-core" structures are separate from 12216 * the page frames used to cache inode blocks. Only the latter are 12217 * transferred to/from the disk. So, when the updated contents of the 12218 * "in-core" inode structure are copied to the corresponding in-memory inode 12219 * block, the dependencies are also transferred. The following procedure is 12220 * called when copying a dirty "in-core" inode to a cached inode block. 12221 */ 12222 12223 /* 12224 * Called when an inode is loaded from disk. If the effective link count 12225 * differed from the actual link count when it was last flushed, then we 12226 * need to ensure that the correct effective link count is put back. 12227 */ 12228 void 12229 softdep_load_inodeblock(ip) 12230 struct inode *ip; /* the "in_core" copy of the inode */ 12231 { 12232 struct inodedep *inodedep; 12233 struct ufsmount *ump; 12234 12235 ump = ITOUMP(ip); 12236 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 12237 ("softdep_load_inodeblock called on non-softdep filesystem")); 12238 /* 12239 * Check for alternate nlink count. 12240 */ 12241 ip->i_effnlink = ip->i_nlink; 12242 ACQUIRE_LOCK(ump); 12243 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) { 12244 FREE_LOCK(ump); 12245 return; 12246 } 12247 ip->i_effnlink -= inodedep->id_nlinkdelta; 12248 FREE_LOCK(ump); 12249 } 12250 12251 /* 12252 * This routine is called just before the "in-core" inode 12253 * information is to be copied to the in-memory inode block. 12254 * Recall that an inode block contains several inodes. If 12255 * the force flag is set, then the dependencies will be 12256 * cleared so that the update can always be made. Note that 12257 * the buffer is locked when this routine is called, so we 12258 * will never be in the middle of writing the inode block 12259 * to disk. 12260 */ 12261 void 12262 softdep_update_inodeblock(ip, bp, waitfor) 12263 struct inode *ip; /* the "in_core" copy of the inode */ 12264 struct buf *bp; /* the buffer containing the inode block */ 12265 int waitfor; /* nonzero => update must be allowed */ 12266 { 12267 struct inodedep *inodedep; 12268 struct inoref *inoref; 12269 struct ufsmount *ump; 12270 struct worklist *wk; 12271 struct mount *mp; 12272 struct buf *ibp; 12273 struct fs *fs; 12274 int error; 12275 12276 ump = ITOUMP(ip); 12277 mp = UFSTOVFS(ump); 12278 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 12279 ("softdep_update_inodeblock called on non-softdep filesystem")); 12280 fs = ump->um_fs; 12281 /* 12282 * Preserve the freelink that is on disk. clear_unlinked_inodedep() 12283 * does not have access to the in-core ip so must write directly into 12284 * the inode block buffer when setting freelink. 12285 */ 12286 if (fs->fs_magic == FS_UFS1_MAGIC) 12287 DIP_SET(ip, i_freelink, ((struct ufs1_dinode *)bp->b_data + 12288 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12289 else 12290 DIP_SET(ip, i_freelink, ((struct ufs2_dinode *)bp->b_data + 12291 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12292 /* 12293 * If the effective link count is not equal to the actual link 12294 * count, then we must track the difference in an inodedep while 12295 * the inode is (potentially) tossed out of the cache. Otherwise, 12296 * if there is no existing inodedep, then there are no dependencies 12297 * to track. 12298 */ 12299 ACQUIRE_LOCK(ump); 12300 again: 12301 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12302 FREE_LOCK(ump); 12303 if (ip->i_effnlink != ip->i_nlink) 12304 panic("softdep_update_inodeblock: bad link count"); 12305 return; 12306 } 12307 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink) 12308 panic("softdep_update_inodeblock: bad delta"); 12309 /* 12310 * If we're flushing all dependencies we must also move any waiting 12311 * for journal writes onto the bufwait list prior to I/O. 12312 */ 12313 if (waitfor) { 12314 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12315 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12316 == DEPCOMPLETE) { 12317 jwait(&inoref->if_list, MNT_WAIT); 12318 goto again; 12319 } 12320 } 12321 } 12322 /* 12323 * Changes have been initiated. Anything depending on these 12324 * changes cannot occur until this inode has been written. 12325 */ 12326 inodedep->id_state &= ~COMPLETE; 12327 if ((inodedep->id_state & ONWORKLIST) == 0) 12328 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list); 12329 /* 12330 * Any new dependencies associated with the incore inode must 12331 * now be moved to the list associated with the buffer holding 12332 * the in-memory copy of the inode. Once merged process any 12333 * allocdirects that are completed by the merger. 12334 */ 12335 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt); 12336 if (!TAILQ_EMPTY(&inodedep->id_inoupdt)) 12337 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt), 12338 NULL); 12339 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt); 12340 if (!TAILQ_EMPTY(&inodedep->id_extupdt)) 12341 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt), 12342 NULL); 12343 /* 12344 * Now that the inode has been pushed into the buffer, the 12345 * operations dependent on the inode being written to disk 12346 * can be moved to the id_bufwait so that they will be 12347 * processed when the buffer I/O completes. 12348 */ 12349 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) { 12350 WORKLIST_REMOVE(wk); 12351 WORKLIST_INSERT(&inodedep->id_bufwait, wk); 12352 } 12353 /* 12354 * Newly allocated inodes cannot be written until the bitmap 12355 * that allocates them have been written (indicated by 12356 * DEPCOMPLETE being set in id_state). If we are doing a 12357 * forced sync (e.g., an fsync on a file), we force the bitmap 12358 * to be written so that the update can be done. 12359 */ 12360 if (waitfor == 0) { 12361 FREE_LOCK(ump); 12362 return; 12363 } 12364 retry: 12365 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) { 12366 FREE_LOCK(ump); 12367 return; 12368 } 12369 ibp = inodedep->id_bmsafemap->sm_buf; 12370 ibp = getdirtybuf(ibp, LOCK_PTR(ump), MNT_WAIT); 12371 if (ibp == NULL) { 12372 /* 12373 * If ibp came back as NULL, the dependency could have been 12374 * freed while we slept. Look it up again, and check to see 12375 * that it has completed. 12376 */ 12377 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 12378 goto retry; 12379 FREE_LOCK(ump); 12380 return; 12381 } 12382 FREE_LOCK(ump); 12383 if ((error = bwrite(ibp)) != 0) 12384 softdep_error("softdep_update_inodeblock: bwrite", error); 12385 } 12386 12387 /* 12388 * Merge the a new inode dependency list (such as id_newinoupdt) into an 12389 * old inode dependency list (such as id_inoupdt). This routine must be 12390 * called with splbio interrupts blocked. 12391 */ 12392 static void 12393 merge_inode_lists(newlisthead, oldlisthead) 12394 struct allocdirectlst *newlisthead; 12395 struct allocdirectlst *oldlisthead; 12396 { 12397 struct allocdirect *listadp, *newadp; 12398 12399 newadp = TAILQ_FIRST(newlisthead); 12400 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) { 12401 if (listadp->ad_offset < newadp->ad_offset) { 12402 listadp = TAILQ_NEXT(listadp, ad_next); 12403 continue; 12404 } 12405 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12406 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next); 12407 if (listadp->ad_offset == newadp->ad_offset) { 12408 allocdirect_merge(oldlisthead, newadp, 12409 listadp); 12410 listadp = newadp; 12411 } 12412 newadp = TAILQ_FIRST(newlisthead); 12413 } 12414 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) { 12415 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12416 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next); 12417 } 12418 } 12419 12420 /* 12421 * If we are doing an fsync, then we must ensure that any directory 12422 * entries for the inode have been written after the inode gets to disk. 12423 */ 12424 int 12425 softdep_fsync(vp) 12426 struct vnode *vp; /* the "in_core" copy of the inode */ 12427 { 12428 struct inodedep *inodedep; 12429 struct pagedep *pagedep; 12430 struct inoref *inoref; 12431 struct ufsmount *ump; 12432 struct worklist *wk; 12433 struct diradd *dap; 12434 struct mount *mp; 12435 struct vnode *pvp; 12436 struct inode *ip; 12437 struct buf *bp; 12438 struct fs *fs; 12439 struct thread *td = curthread; 12440 int error, flushparent, pagedep_new_block; 12441 ino_t parentino; 12442 ufs_lbn_t lbn; 12443 12444 ip = VTOI(vp); 12445 mp = vp->v_mount; 12446 ump = VFSTOUFS(mp); 12447 fs = ump->um_fs; 12448 if (MOUNTEDSOFTDEP(mp) == 0) 12449 return (0); 12450 ACQUIRE_LOCK(ump); 12451 restart: 12452 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12453 FREE_LOCK(ump); 12454 return (0); 12455 } 12456 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12457 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12458 == DEPCOMPLETE) { 12459 jwait(&inoref->if_list, MNT_WAIT); 12460 goto restart; 12461 } 12462 } 12463 if (!LIST_EMPTY(&inodedep->id_inowait) || 12464 !TAILQ_EMPTY(&inodedep->id_extupdt) || 12465 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 12466 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 12467 !TAILQ_EMPTY(&inodedep->id_newinoupdt)) 12468 panic("softdep_fsync: pending ops %p", inodedep); 12469 for (error = 0, flushparent = 0; ; ) { 12470 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL) 12471 break; 12472 if (wk->wk_type != D_DIRADD) 12473 panic("softdep_fsync: Unexpected type %s", 12474 TYPENAME(wk->wk_type)); 12475 dap = WK_DIRADD(wk); 12476 /* 12477 * Flush our parent if this directory entry has a MKDIR_PARENT 12478 * dependency or is contained in a newly allocated block. 12479 */ 12480 if (dap->da_state & DIRCHG) 12481 pagedep = dap->da_previous->dm_pagedep; 12482 else 12483 pagedep = dap->da_pagedep; 12484 parentino = pagedep->pd_ino; 12485 lbn = pagedep->pd_lbn; 12486 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) 12487 panic("softdep_fsync: dirty"); 12488 if ((dap->da_state & MKDIR_PARENT) || 12489 (pagedep->pd_state & NEWBLOCK)) 12490 flushparent = 1; 12491 else 12492 flushparent = 0; 12493 /* 12494 * If we are being fsync'ed as part of vgone'ing this vnode, 12495 * then we will not be able to release and recover the 12496 * vnode below, so we just have to give up on writing its 12497 * directory entry out. It will eventually be written, just 12498 * not now, but then the user was not asking to have it 12499 * written, so we are not breaking any promises. 12500 */ 12501 if (vp->v_iflag & VI_DOOMED) 12502 break; 12503 /* 12504 * We prevent deadlock by always fetching inodes from the 12505 * root, moving down the directory tree. Thus, when fetching 12506 * our parent directory, we first try to get the lock. If 12507 * that fails, we must unlock ourselves before requesting 12508 * the lock on our parent. See the comment in ufs_lookup 12509 * for details on possible races. 12510 */ 12511 FREE_LOCK(ump); 12512 if (ffs_vgetf(mp, parentino, LK_NOWAIT | LK_EXCLUSIVE, &pvp, 12513 FFSV_FORCEINSMQ)) { 12514 error = vfs_busy(mp, MBF_NOWAIT); 12515 if (error != 0) { 12516 vfs_ref(mp); 12517 VOP_UNLOCK(vp, 0); 12518 error = vfs_busy(mp, 0); 12519 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 12520 vfs_rel(mp); 12521 if (error != 0) 12522 return (ENOENT); 12523 if (vp->v_iflag & VI_DOOMED) { 12524 vfs_unbusy(mp); 12525 return (ENOENT); 12526 } 12527 } 12528 VOP_UNLOCK(vp, 0); 12529 error = ffs_vgetf(mp, parentino, LK_EXCLUSIVE, 12530 &pvp, FFSV_FORCEINSMQ); 12531 vfs_unbusy(mp); 12532 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 12533 if (vp->v_iflag & VI_DOOMED) { 12534 if (error == 0) 12535 vput(pvp); 12536 error = ENOENT; 12537 } 12538 if (error != 0) 12539 return (error); 12540 } 12541 /* 12542 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps 12543 * that are contained in direct blocks will be resolved by 12544 * doing a ffs_update. Pagedeps contained in indirect blocks 12545 * may require a complete sync'ing of the directory. So, we 12546 * try the cheap and fast ffs_update first, and if that fails, 12547 * then we do the slower ffs_syncvnode of the directory. 12548 */ 12549 if (flushparent) { 12550 int locked; 12551 12552 if ((error = ffs_update(pvp, 1)) != 0) { 12553 vput(pvp); 12554 return (error); 12555 } 12556 ACQUIRE_LOCK(ump); 12557 locked = 1; 12558 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) { 12559 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) { 12560 if (wk->wk_type != D_DIRADD) 12561 panic("softdep_fsync: Unexpected type %s", 12562 TYPENAME(wk->wk_type)); 12563 dap = WK_DIRADD(wk); 12564 if (dap->da_state & DIRCHG) 12565 pagedep = dap->da_previous->dm_pagedep; 12566 else 12567 pagedep = dap->da_pagedep; 12568 pagedep_new_block = pagedep->pd_state & NEWBLOCK; 12569 FREE_LOCK(ump); 12570 locked = 0; 12571 if (pagedep_new_block && (error = 12572 ffs_syncvnode(pvp, MNT_WAIT, 0))) { 12573 vput(pvp); 12574 return (error); 12575 } 12576 } 12577 } 12578 if (locked) 12579 FREE_LOCK(ump); 12580 } 12581 /* 12582 * Flush directory page containing the inode's name. 12583 */ 12584 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred, 12585 &bp); 12586 if (error == 0) 12587 error = bwrite(bp); 12588 else 12589 brelse(bp); 12590 vput(pvp); 12591 if (error != 0) 12592 return (error); 12593 ACQUIRE_LOCK(ump); 12594 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 12595 break; 12596 } 12597 FREE_LOCK(ump); 12598 return (0); 12599 } 12600 12601 /* 12602 * Flush all the dirty bitmaps associated with the block device 12603 * before flushing the rest of the dirty blocks so as to reduce 12604 * the number of dependencies that will have to be rolled back. 12605 * 12606 * XXX Unused? 12607 */ 12608 void 12609 softdep_fsync_mountdev(vp) 12610 struct vnode *vp; 12611 { 12612 struct buf *bp, *nbp; 12613 struct worklist *wk; 12614 struct bufobj *bo; 12615 12616 if (!vn_isdisk(vp, NULL)) 12617 panic("softdep_fsync_mountdev: vnode not a disk"); 12618 bo = &vp->v_bufobj; 12619 restart: 12620 BO_LOCK(bo); 12621 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 12622 /* 12623 * If it is already scheduled, skip to the next buffer. 12624 */ 12625 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) 12626 continue; 12627 12628 if ((bp->b_flags & B_DELWRI) == 0) 12629 panic("softdep_fsync_mountdev: not dirty"); 12630 /* 12631 * We are only interested in bitmaps with outstanding 12632 * dependencies. 12633 */ 12634 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL || 12635 wk->wk_type != D_BMSAFEMAP || 12636 (bp->b_vflags & BV_BKGRDINPROG)) { 12637 BUF_UNLOCK(bp); 12638 continue; 12639 } 12640 BO_UNLOCK(bo); 12641 bremfree(bp); 12642 (void) bawrite(bp); 12643 goto restart; 12644 } 12645 drain_output(vp); 12646 BO_UNLOCK(bo); 12647 } 12648 12649 /* 12650 * Sync all cylinder groups that were dirty at the time this function is 12651 * called. Newly dirtied cgs will be inserted before the sentinel. This 12652 * is used to flush freedep activity that may be holding up writes to a 12653 * indirect block. 12654 */ 12655 static int 12656 sync_cgs(mp, waitfor) 12657 struct mount *mp; 12658 int waitfor; 12659 { 12660 struct bmsafemap *bmsafemap; 12661 struct bmsafemap *sentinel; 12662 struct ufsmount *ump; 12663 struct buf *bp; 12664 int error; 12665 12666 sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK); 12667 sentinel->sm_cg = -1; 12668 ump = VFSTOUFS(mp); 12669 error = 0; 12670 ACQUIRE_LOCK(ump); 12671 LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next); 12672 for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL; 12673 bmsafemap = LIST_NEXT(sentinel, sm_next)) { 12674 /* Skip sentinels and cgs with no work to release. */ 12675 if (bmsafemap->sm_cg == -1 || 12676 (LIST_EMPTY(&bmsafemap->sm_freehd) && 12677 LIST_EMPTY(&bmsafemap->sm_freewr))) { 12678 LIST_REMOVE(sentinel, sm_next); 12679 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 12680 continue; 12681 } 12682 /* 12683 * If we don't get the lock and we're waiting try again, if 12684 * not move on to the next buf and try to sync it. 12685 */ 12686 bp = getdirtybuf(bmsafemap->sm_buf, LOCK_PTR(ump), waitfor); 12687 if (bp == NULL && waitfor == MNT_WAIT) 12688 continue; 12689 LIST_REMOVE(sentinel, sm_next); 12690 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 12691 if (bp == NULL) 12692 continue; 12693 FREE_LOCK(ump); 12694 if (waitfor == MNT_NOWAIT) 12695 bawrite(bp); 12696 else 12697 error = bwrite(bp); 12698 ACQUIRE_LOCK(ump); 12699 if (error) 12700 break; 12701 } 12702 LIST_REMOVE(sentinel, sm_next); 12703 FREE_LOCK(ump); 12704 free(sentinel, M_BMSAFEMAP); 12705 return (error); 12706 } 12707 12708 /* 12709 * This routine is called when we are trying to synchronously flush a 12710 * file. This routine must eliminate any filesystem metadata dependencies 12711 * so that the syncing routine can succeed. 12712 */ 12713 int 12714 softdep_sync_metadata(struct vnode *vp) 12715 { 12716 struct inode *ip; 12717 int error; 12718 12719 ip = VTOI(vp); 12720 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 12721 ("softdep_sync_metadata called on non-softdep filesystem")); 12722 /* 12723 * Ensure that any direct block dependencies have been cleared, 12724 * truncations are started, and inode references are journaled. 12725 */ 12726 ACQUIRE_LOCK(VFSTOUFS(vp->v_mount)); 12727 /* 12728 * Write all journal records to prevent rollbacks on devvp. 12729 */ 12730 if (vp->v_type == VCHR) 12731 softdep_flushjournal(vp->v_mount); 12732 error = flush_inodedep_deps(vp, vp->v_mount, ip->i_number); 12733 /* 12734 * Ensure that all truncates are written so we won't find deps on 12735 * indirect blocks. 12736 */ 12737 process_truncates(vp); 12738 FREE_LOCK(VFSTOUFS(vp->v_mount)); 12739 12740 return (error); 12741 } 12742 12743 /* 12744 * This routine is called when we are attempting to sync a buf with 12745 * dependencies. If waitfor is MNT_NOWAIT it attempts to schedule any 12746 * other IO it can but returns EBUSY if the buffer is not yet able to 12747 * be written. Dependencies which will not cause rollbacks will always 12748 * return 0. 12749 */ 12750 int 12751 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 12752 { 12753 struct indirdep *indirdep; 12754 struct pagedep *pagedep; 12755 struct allocindir *aip; 12756 struct newblk *newblk; 12757 struct ufsmount *ump; 12758 struct buf *nbp; 12759 struct worklist *wk; 12760 int i, error; 12761 12762 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 12763 ("softdep_sync_buf called on non-softdep filesystem")); 12764 /* 12765 * For VCHR we just don't want to force flush any dependencies that 12766 * will cause rollbacks. 12767 */ 12768 if (vp->v_type == VCHR) { 12769 if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0)) 12770 return (EBUSY); 12771 return (0); 12772 } 12773 ump = VFSTOUFS(vp->v_mount); 12774 ACQUIRE_LOCK(ump); 12775 /* 12776 * As we hold the buffer locked, none of its dependencies 12777 * will disappear. 12778 */ 12779 error = 0; 12780 top: 12781 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 12782 switch (wk->wk_type) { 12783 12784 case D_ALLOCDIRECT: 12785 case D_ALLOCINDIR: 12786 newblk = WK_NEWBLK(wk); 12787 if (newblk->nb_jnewblk != NULL) { 12788 if (waitfor == MNT_NOWAIT) { 12789 error = EBUSY; 12790 goto out_unlock; 12791 } 12792 jwait(&newblk->nb_jnewblk->jn_list, waitfor); 12793 goto top; 12794 } 12795 if (newblk->nb_state & DEPCOMPLETE || 12796 waitfor == MNT_NOWAIT) 12797 continue; 12798 nbp = newblk->nb_bmsafemap->sm_buf; 12799 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 12800 if (nbp == NULL) 12801 goto top; 12802 FREE_LOCK(ump); 12803 if ((error = bwrite(nbp)) != 0) 12804 goto out; 12805 ACQUIRE_LOCK(ump); 12806 continue; 12807 12808 case D_INDIRDEP: 12809 indirdep = WK_INDIRDEP(wk); 12810 if (waitfor == MNT_NOWAIT) { 12811 if (!TAILQ_EMPTY(&indirdep->ir_trunc) || 12812 !LIST_EMPTY(&indirdep->ir_deplisthd)) { 12813 error = EBUSY; 12814 goto out_unlock; 12815 } 12816 } 12817 if (!TAILQ_EMPTY(&indirdep->ir_trunc)) 12818 panic("softdep_sync_buf: truncation pending."); 12819 restart: 12820 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 12821 newblk = (struct newblk *)aip; 12822 if (newblk->nb_jnewblk != NULL) { 12823 jwait(&newblk->nb_jnewblk->jn_list, 12824 waitfor); 12825 goto restart; 12826 } 12827 if (newblk->nb_state & DEPCOMPLETE) 12828 continue; 12829 nbp = newblk->nb_bmsafemap->sm_buf; 12830 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 12831 if (nbp == NULL) 12832 goto restart; 12833 FREE_LOCK(ump); 12834 if ((error = bwrite(nbp)) != 0) 12835 goto out; 12836 ACQUIRE_LOCK(ump); 12837 goto restart; 12838 } 12839 continue; 12840 12841 case D_PAGEDEP: 12842 /* 12843 * Only flush directory entries in synchronous passes. 12844 */ 12845 if (waitfor != MNT_WAIT) { 12846 error = EBUSY; 12847 goto out_unlock; 12848 } 12849 /* 12850 * While syncing snapshots, we must allow recursive 12851 * lookups. 12852 */ 12853 BUF_AREC(bp); 12854 /* 12855 * We are trying to sync a directory that may 12856 * have dependencies on both its own metadata 12857 * and/or dependencies on the inodes of any 12858 * recently allocated files. We walk its diradd 12859 * lists pushing out the associated inode. 12860 */ 12861 pagedep = WK_PAGEDEP(wk); 12862 for (i = 0; i < DAHASHSZ; i++) { 12863 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0) 12864 continue; 12865 if ((error = flush_pagedep_deps(vp, wk->wk_mp, 12866 &pagedep->pd_diraddhd[i]))) { 12867 BUF_NOREC(bp); 12868 goto out_unlock; 12869 } 12870 } 12871 BUF_NOREC(bp); 12872 continue; 12873 12874 case D_FREEWORK: 12875 case D_FREEDEP: 12876 case D_JSEGDEP: 12877 case D_JNEWBLK: 12878 continue; 12879 12880 default: 12881 panic("softdep_sync_buf: Unknown type %s", 12882 TYPENAME(wk->wk_type)); 12883 /* NOTREACHED */ 12884 } 12885 } 12886 out_unlock: 12887 FREE_LOCK(ump); 12888 out: 12889 return (error); 12890 } 12891 12892 /* 12893 * Flush the dependencies associated with an inodedep. 12894 * Called with splbio blocked. 12895 */ 12896 static int 12897 flush_inodedep_deps(vp, mp, ino) 12898 struct vnode *vp; 12899 struct mount *mp; 12900 ino_t ino; 12901 { 12902 struct inodedep *inodedep; 12903 struct inoref *inoref; 12904 struct ufsmount *ump; 12905 int error, waitfor; 12906 12907 /* 12908 * This work is done in two passes. The first pass grabs most 12909 * of the buffers and begins asynchronously writing them. The 12910 * only way to wait for these asynchronous writes is to sleep 12911 * on the filesystem vnode which may stay busy for a long time 12912 * if the filesystem is active. So, instead, we make a second 12913 * pass over the dependencies blocking on each write. In the 12914 * usual case we will be blocking against a write that we 12915 * initiated, so when it is done the dependency will have been 12916 * resolved. Thus the second pass is expected to end quickly. 12917 * We give a brief window at the top of the loop to allow 12918 * any pending I/O to complete. 12919 */ 12920 ump = VFSTOUFS(mp); 12921 LOCK_OWNED(ump); 12922 for (error = 0, waitfor = MNT_NOWAIT; ; ) { 12923 if (error) 12924 return (error); 12925 FREE_LOCK(ump); 12926 ACQUIRE_LOCK(ump); 12927 restart: 12928 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 12929 return (0); 12930 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12931 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12932 == DEPCOMPLETE) { 12933 jwait(&inoref->if_list, MNT_WAIT); 12934 goto restart; 12935 } 12936 } 12937 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) || 12938 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) || 12939 flush_deplist(&inodedep->id_extupdt, waitfor, &error) || 12940 flush_deplist(&inodedep->id_newextupdt, waitfor, &error)) 12941 continue; 12942 /* 12943 * If pass2, we are done, otherwise do pass 2. 12944 */ 12945 if (waitfor == MNT_WAIT) 12946 break; 12947 waitfor = MNT_WAIT; 12948 } 12949 /* 12950 * Try freeing inodedep in case all dependencies have been removed. 12951 */ 12952 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0) 12953 (void) free_inodedep(inodedep); 12954 return (0); 12955 } 12956 12957 /* 12958 * Flush an inode dependency list. 12959 * Called with splbio blocked. 12960 */ 12961 static int 12962 flush_deplist(listhead, waitfor, errorp) 12963 struct allocdirectlst *listhead; 12964 int waitfor; 12965 int *errorp; 12966 { 12967 struct allocdirect *adp; 12968 struct newblk *newblk; 12969 struct ufsmount *ump; 12970 struct buf *bp; 12971 12972 if ((adp = TAILQ_FIRST(listhead)) == NULL) 12973 return (0); 12974 ump = VFSTOUFS(adp->ad_list.wk_mp); 12975 LOCK_OWNED(ump); 12976 TAILQ_FOREACH(adp, listhead, ad_next) { 12977 newblk = (struct newblk *)adp; 12978 if (newblk->nb_jnewblk != NULL) { 12979 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 12980 return (1); 12981 } 12982 if (newblk->nb_state & DEPCOMPLETE) 12983 continue; 12984 bp = newblk->nb_bmsafemap->sm_buf; 12985 bp = getdirtybuf(bp, LOCK_PTR(ump), waitfor); 12986 if (bp == NULL) { 12987 if (waitfor == MNT_NOWAIT) 12988 continue; 12989 return (1); 12990 } 12991 FREE_LOCK(ump); 12992 if (waitfor == MNT_NOWAIT) 12993 bawrite(bp); 12994 else 12995 *errorp = bwrite(bp); 12996 ACQUIRE_LOCK(ump); 12997 return (1); 12998 } 12999 return (0); 13000 } 13001 13002 /* 13003 * Flush dependencies associated with an allocdirect block. 13004 */ 13005 static int 13006 flush_newblk_dep(vp, mp, lbn) 13007 struct vnode *vp; 13008 struct mount *mp; 13009 ufs_lbn_t lbn; 13010 { 13011 struct newblk *newblk; 13012 struct ufsmount *ump; 13013 struct bufobj *bo; 13014 struct inode *ip; 13015 struct buf *bp; 13016 ufs2_daddr_t blkno; 13017 int error; 13018 13019 error = 0; 13020 bo = &vp->v_bufobj; 13021 ip = VTOI(vp); 13022 blkno = DIP(ip, i_db[lbn]); 13023 if (blkno == 0) 13024 panic("flush_newblk_dep: Missing block"); 13025 ump = VFSTOUFS(mp); 13026 ACQUIRE_LOCK(ump); 13027 /* 13028 * Loop until all dependencies related to this block are satisfied. 13029 * We must be careful to restart after each sleep in case a write 13030 * completes some part of this process for us. 13031 */ 13032 for (;;) { 13033 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) { 13034 FREE_LOCK(ump); 13035 break; 13036 } 13037 if (newblk->nb_list.wk_type != D_ALLOCDIRECT) 13038 panic("flush_newblk_dep: Bad newblk %p", newblk); 13039 /* 13040 * Flush the journal. 13041 */ 13042 if (newblk->nb_jnewblk != NULL) { 13043 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 13044 continue; 13045 } 13046 /* 13047 * Write the bitmap dependency. 13048 */ 13049 if ((newblk->nb_state & DEPCOMPLETE) == 0) { 13050 bp = newblk->nb_bmsafemap->sm_buf; 13051 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 13052 if (bp == NULL) 13053 continue; 13054 FREE_LOCK(ump); 13055 error = bwrite(bp); 13056 if (error) 13057 break; 13058 ACQUIRE_LOCK(ump); 13059 continue; 13060 } 13061 /* 13062 * Write the buffer. 13063 */ 13064 FREE_LOCK(ump); 13065 BO_LOCK(bo); 13066 bp = gbincore(bo, lbn); 13067 if (bp != NULL) { 13068 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 13069 LK_INTERLOCK, BO_LOCKPTR(bo)); 13070 if (error == ENOLCK) { 13071 ACQUIRE_LOCK(ump); 13072 error = 0; 13073 continue; /* Slept, retry */ 13074 } 13075 if (error != 0) 13076 break; /* Failed */ 13077 if (bp->b_flags & B_DELWRI) { 13078 bremfree(bp); 13079 error = bwrite(bp); 13080 if (error) 13081 break; 13082 } else 13083 BUF_UNLOCK(bp); 13084 } else 13085 BO_UNLOCK(bo); 13086 /* 13087 * We have to wait for the direct pointers to 13088 * point at the newdirblk before the dependency 13089 * will go away. 13090 */ 13091 error = ffs_update(vp, 1); 13092 if (error) 13093 break; 13094 ACQUIRE_LOCK(ump); 13095 } 13096 return (error); 13097 } 13098 13099 /* 13100 * Eliminate a pagedep dependency by flushing out all its diradd dependencies. 13101 * Called with splbio blocked. 13102 */ 13103 static int 13104 flush_pagedep_deps(pvp, mp, diraddhdp) 13105 struct vnode *pvp; 13106 struct mount *mp; 13107 struct diraddhd *diraddhdp; 13108 { 13109 struct inodedep *inodedep; 13110 struct inoref *inoref; 13111 struct ufsmount *ump; 13112 struct diradd *dap; 13113 struct vnode *vp; 13114 int error = 0; 13115 struct buf *bp; 13116 ino_t inum; 13117 struct diraddhd unfinished; 13118 13119 LIST_INIT(&unfinished); 13120 ump = VFSTOUFS(mp); 13121 LOCK_OWNED(ump); 13122 restart: 13123 while ((dap = LIST_FIRST(diraddhdp)) != NULL) { 13124 /* 13125 * Flush ourselves if this directory entry 13126 * has a MKDIR_PARENT dependency. 13127 */ 13128 if (dap->da_state & MKDIR_PARENT) { 13129 FREE_LOCK(ump); 13130 if ((error = ffs_update(pvp, 1)) != 0) 13131 break; 13132 ACQUIRE_LOCK(ump); 13133 /* 13134 * If that cleared dependencies, go on to next. 13135 */ 13136 if (dap != LIST_FIRST(diraddhdp)) 13137 continue; 13138 /* 13139 * All MKDIR_PARENT dependencies and all the 13140 * NEWBLOCK pagedeps that are contained in direct 13141 * blocks were resolved by doing above ffs_update. 13142 * Pagedeps contained in indirect blocks may 13143 * require a complete sync'ing of the directory. 13144 * We are in the midst of doing a complete sync, 13145 * so if they are not resolved in this pass we 13146 * defer them for now as they will be sync'ed by 13147 * our caller shortly. 13148 */ 13149 LIST_REMOVE(dap, da_pdlist); 13150 LIST_INSERT_HEAD(&unfinished, dap, da_pdlist); 13151 continue; 13152 } 13153 /* 13154 * A newly allocated directory must have its "." and 13155 * ".." entries written out before its name can be 13156 * committed in its parent. 13157 */ 13158 inum = dap->da_newinum; 13159 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 13160 panic("flush_pagedep_deps: lost inode1"); 13161 /* 13162 * Wait for any pending journal adds to complete so we don't 13163 * cause rollbacks while syncing. 13164 */ 13165 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 13166 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 13167 == DEPCOMPLETE) { 13168 jwait(&inoref->if_list, MNT_WAIT); 13169 goto restart; 13170 } 13171 } 13172 if (dap->da_state & MKDIR_BODY) { 13173 FREE_LOCK(ump); 13174 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp, 13175 FFSV_FORCEINSMQ))) 13176 break; 13177 error = flush_newblk_dep(vp, mp, 0); 13178 /* 13179 * If we still have the dependency we might need to 13180 * update the vnode to sync the new link count to 13181 * disk. 13182 */ 13183 if (error == 0 && dap == LIST_FIRST(diraddhdp)) 13184 error = ffs_update(vp, 1); 13185 vput(vp); 13186 if (error != 0) 13187 break; 13188 ACQUIRE_LOCK(ump); 13189 /* 13190 * If that cleared dependencies, go on to next. 13191 */ 13192 if (dap != LIST_FIRST(diraddhdp)) 13193 continue; 13194 if (dap->da_state & MKDIR_BODY) { 13195 inodedep_lookup(UFSTOVFS(ump), inum, 0, 13196 &inodedep); 13197 panic("flush_pagedep_deps: MKDIR_BODY " 13198 "inodedep %p dap %p vp %p", 13199 inodedep, dap, vp); 13200 } 13201 } 13202 /* 13203 * Flush the inode on which the directory entry depends. 13204 * Having accounted for MKDIR_PARENT and MKDIR_BODY above, 13205 * the only remaining dependency is that the updated inode 13206 * count must get pushed to disk. The inode has already 13207 * been pushed into its inode buffer (via VOP_UPDATE) at 13208 * the time of the reference count change. So we need only 13209 * locate that buffer, ensure that there will be no rollback 13210 * caused by a bitmap dependency, then write the inode buffer. 13211 */ 13212 retry: 13213 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 13214 panic("flush_pagedep_deps: lost inode"); 13215 /* 13216 * If the inode still has bitmap dependencies, 13217 * push them to disk. 13218 */ 13219 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) { 13220 bp = inodedep->id_bmsafemap->sm_buf; 13221 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 13222 if (bp == NULL) 13223 goto retry; 13224 FREE_LOCK(ump); 13225 if ((error = bwrite(bp)) != 0) 13226 break; 13227 ACQUIRE_LOCK(ump); 13228 if (dap != LIST_FIRST(diraddhdp)) 13229 continue; 13230 } 13231 /* 13232 * If the inode is still sitting in a buffer waiting 13233 * to be written or waiting for the link count to be 13234 * adjusted update it here to flush it to disk. 13235 */ 13236 if (dap == LIST_FIRST(diraddhdp)) { 13237 FREE_LOCK(ump); 13238 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp, 13239 FFSV_FORCEINSMQ))) 13240 break; 13241 error = ffs_update(vp, 1); 13242 vput(vp); 13243 if (error) 13244 break; 13245 ACQUIRE_LOCK(ump); 13246 } 13247 /* 13248 * If we have failed to get rid of all the dependencies 13249 * then something is seriously wrong. 13250 */ 13251 if (dap == LIST_FIRST(diraddhdp)) { 13252 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep); 13253 panic("flush_pagedep_deps: failed to flush " 13254 "inodedep %p ino %ju dap %p", 13255 inodedep, (uintmax_t)inum, dap); 13256 } 13257 } 13258 if (error) 13259 ACQUIRE_LOCK(ump); 13260 while ((dap = LIST_FIRST(&unfinished)) != NULL) { 13261 LIST_REMOVE(dap, da_pdlist); 13262 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist); 13263 } 13264 return (error); 13265 } 13266 13267 /* 13268 * A large burst of file addition or deletion activity can drive the 13269 * memory load excessively high. First attempt to slow things down 13270 * using the techniques below. If that fails, this routine requests 13271 * the offending operations to fall back to running synchronously 13272 * until the memory load returns to a reasonable level. 13273 */ 13274 int 13275 softdep_slowdown(vp) 13276 struct vnode *vp; 13277 { 13278 struct ufsmount *ump; 13279 int jlow; 13280 int max_softdeps_hard; 13281 13282 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 13283 ("softdep_slowdown called on non-softdep filesystem")); 13284 ump = VFSTOUFS(vp->v_mount); 13285 ACQUIRE_LOCK(ump); 13286 jlow = 0; 13287 /* 13288 * Check for journal space if needed. 13289 */ 13290 if (DOINGSUJ(vp)) { 13291 if (journal_space(ump, 0) == 0) 13292 jlow = 1; 13293 } 13294 /* 13295 * If the system is under its limits and our filesystem is 13296 * not responsible for more than our share of the usage and 13297 * we are not low on journal space, then no need to slow down. 13298 */ 13299 max_softdeps_hard = max_softdeps * 11 / 10; 13300 if (dep_current[D_DIRREM] < max_softdeps_hard / 2 && 13301 dep_current[D_INODEDEP] < max_softdeps_hard && 13302 dep_current[D_INDIRDEP] < max_softdeps_hard / 1000 && 13303 dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0 && 13304 ump->softdep_curdeps[D_DIRREM] < 13305 (max_softdeps_hard / 2) / stat_flush_threads && 13306 ump->softdep_curdeps[D_INODEDEP] < 13307 max_softdeps_hard / stat_flush_threads && 13308 ump->softdep_curdeps[D_INDIRDEP] < 13309 (max_softdeps_hard / 1000) / stat_flush_threads && 13310 ump->softdep_curdeps[D_FREEBLKS] < 13311 max_softdeps_hard / stat_flush_threads) { 13312 FREE_LOCK(ump); 13313 return (0); 13314 } 13315 /* 13316 * If the journal is low or our filesystem is over its limit 13317 * then speedup the cleanup. 13318 */ 13319 if (ump->softdep_curdeps[D_INDIRDEP] < 13320 (max_softdeps_hard / 1000) / stat_flush_threads || jlow) 13321 softdep_speedup(ump); 13322 stat_sync_limit_hit += 1; 13323 FREE_LOCK(ump); 13324 /* 13325 * We only slow down the rate at which new dependencies are 13326 * generated if we are not using journaling. With journaling, 13327 * the cleanup should always be sufficient to keep things 13328 * under control. 13329 */ 13330 if (DOINGSUJ(vp)) 13331 return (0); 13332 return (1); 13333 } 13334 13335 /* 13336 * Called by the allocation routines when they are about to fail 13337 * in the hope that we can free up the requested resource (inodes 13338 * or disk space). 13339 * 13340 * First check to see if the work list has anything on it. If it has, 13341 * clean up entries until we successfully free the requested resource. 13342 * Because this process holds inodes locked, we cannot handle any remove 13343 * requests that might block on a locked inode as that could lead to 13344 * deadlock. If the worklist yields none of the requested resource, 13345 * start syncing out vnodes to free up the needed space. 13346 */ 13347 int 13348 softdep_request_cleanup(fs, vp, cred, resource) 13349 struct fs *fs; 13350 struct vnode *vp; 13351 struct ucred *cred; 13352 int resource; 13353 { 13354 struct ufsmount *ump; 13355 struct mount *mp; 13356 long starttime; 13357 ufs2_daddr_t needed; 13358 int error, failed_vnode; 13359 13360 /* 13361 * If we are being called because of a process doing a 13362 * copy-on-write, then it is not safe to process any 13363 * worklist items as we will recurse into the copyonwrite 13364 * routine. This will result in an incoherent snapshot. 13365 * If the vnode that we hold is a snapshot, we must avoid 13366 * handling other resources that could cause deadlock. 13367 */ 13368 if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp))) 13369 return (0); 13370 13371 if (resource == FLUSH_BLOCKS_WAIT) 13372 stat_cleanup_blkrequests += 1; 13373 else 13374 stat_cleanup_inorequests += 1; 13375 13376 mp = vp->v_mount; 13377 ump = VFSTOUFS(mp); 13378 mtx_assert(UFS_MTX(ump), MA_OWNED); 13379 UFS_UNLOCK(ump); 13380 error = ffs_update(vp, 1); 13381 if (error != 0 || MOUNTEDSOFTDEP(mp) == 0) { 13382 UFS_LOCK(ump); 13383 return (0); 13384 } 13385 /* 13386 * If we are in need of resources, start by cleaning up 13387 * any block removals associated with our inode. 13388 */ 13389 ACQUIRE_LOCK(ump); 13390 process_removes(vp); 13391 process_truncates(vp); 13392 FREE_LOCK(ump); 13393 /* 13394 * Now clean up at least as many resources as we will need. 13395 * 13396 * When requested to clean up inodes, the number that are needed 13397 * is set by the number of simultaneous writers (mnt_writeopcount) 13398 * plus a bit of slop (2) in case some more writers show up while 13399 * we are cleaning. 13400 * 13401 * When requested to free up space, the amount of space that 13402 * we need is enough blocks to allocate a full-sized segment 13403 * (fs_contigsumsize). The number of such segments that will 13404 * be needed is set by the number of simultaneous writers 13405 * (mnt_writeopcount) plus a bit of slop (2) in case some more 13406 * writers show up while we are cleaning. 13407 * 13408 * Additionally, if we are unpriviledged and allocating space, 13409 * we need to ensure that we clean up enough blocks to get the 13410 * needed number of blocks over the threshold of the minimum 13411 * number of blocks required to be kept free by the filesystem 13412 * (fs_minfree). 13413 */ 13414 if (resource == FLUSH_INODES_WAIT) { 13415 needed = vp->v_mount->mnt_writeopcount + 2; 13416 } else if (resource == FLUSH_BLOCKS_WAIT) { 13417 needed = (vp->v_mount->mnt_writeopcount + 2) * 13418 fs->fs_contigsumsize; 13419 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE)) 13420 needed += fragstoblks(fs, 13421 roundup((fs->fs_dsize * fs->fs_minfree / 100) - 13422 fs->fs_cstotal.cs_nffree, fs->fs_frag)); 13423 } else { 13424 UFS_LOCK(ump); 13425 printf("softdep_request_cleanup: Unknown resource type %d\n", 13426 resource); 13427 return (0); 13428 } 13429 starttime = time_second; 13430 retry: 13431 if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 && 13432 fs->fs_cstotal.cs_nbfree <= needed) || 13433 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13434 fs->fs_cstotal.cs_nifree <= needed)) { 13435 ACQUIRE_LOCK(ump); 13436 if (ump->softdep_on_worklist > 0 && 13437 process_worklist_item(UFSTOVFS(ump), 13438 ump->softdep_on_worklist, LK_NOWAIT) != 0) 13439 stat_worklist_push += 1; 13440 FREE_LOCK(ump); 13441 } 13442 /* 13443 * If we still need resources and there are no more worklist 13444 * entries to process to obtain them, we have to start flushing 13445 * the dirty vnodes to force the release of additional requests 13446 * to the worklist that we can then process to reap addition 13447 * resources. We walk the vnodes associated with the mount point 13448 * until we get the needed worklist requests that we can reap. 13449 * 13450 * If there are several threads all needing to clean the same 13451 * mount point, only one is allowed to walk the mount list. 13452 * When several threads all try to walk the same mount list, 13453 * they end up competing with each other and often end up in 13454 * livelock. This approach ensures that forward progress is 13455 * made at the cost of occational ENOSPC errors being returned 13456 * that might otherwise have been avoided. 13457 */ 13458 error = 1; 13459 if ((resource == FLUSH_BLOCKS_WAIT && 13460 fs->fs_cstotal.cs_nbfree <= needed) || 13461 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13462 fs->fs_cstotal.cs_nifree <= needed)) { 13463 ACQUIRE_LOCK(ump); 13464 if ((ump->um_softdep->sd_flags & FLUSH_RC_ACTIVE) == 0) { 13465 ump->um_softdep->sd_flags |= FLUSH_RC_ACTIVE; 13466 FREE_LOCK(ump); 13467 failed_vnode = softdep_request_cleanup_flush(mp, ump); 13468 ACQUIRE_LOCK(ump); 13469 ump->um_softdep->sd_flags &= ~FLUSH_RC_ACTIVE; 13470 FREE_LOCK(ump); 13471 if (ump->softdep_on_worklist > 0) { 13472 stat_cleanup_retries += 1; 13473 if (!failed_vnode) 13474 goto retry; 13475 } 13476 } else { 13477 FREE_LOCK(ump); 13478 error = 0; 13479 } 13480 stat_cleanup_failures += 1; 13481 } 13482 if (time_second - starttime > stat_cleanup_high_delay) 13483 stat_cleanup_high_delay = time_second - starttime; 13484 UFS_LOCK(ump); 13485 return (error); 13486 } 13487 13488 /* 13489 * Scan the vnodes for the specified mount point flushing out any 13490 * vnodes that can be locked without waiting. Finally, try to flush 13491 * the device associated with the mount point if it can be locked 13492 * without waiting. 13493 * 13494 * We return 0 if we were able to lock every vnode in our scan. 13495 * If we had to skip one or more vnodes, we return 1. 13496 */ 13497 static int 13498 softdep_request_cleanup_flush(mp, ump) 13499 struct mount *mp; 13500 struct ufsmount *ump; 13501 { 13502 struct thread *td; 13503 struct vnode *lvp, *mvp; 13504 int failed_vnode; 13505 13506 failed_vnode = 0; 13507 td = curthread; 13508 MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) { 13509 if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) { 13510 VI_UNLOCK(lvp); 13511 continue; 13512 } 13513 if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT, 13514 td) != 0) { 13515 failed_vnode = 1; 13516 continue; 13517 } 13518 if (lvp->v_vflag & VV_NOSYNC) { /* unlinked */ 13519 vput(lvp); 13520 continue; 13521 } 13522 (void) ffs_syncvnode(lvp, MNT_NOWAIT, 0); 13523 vput(lvp); 13524 } 13525 lvp = ump->um_devvp; 13526 if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) { 13527 VOP_FSYNC(lvp, MNT_NOWAIT, td); 13528 VOP_UNLOCK(lvp, 0); 13529 } 13530 return (failed_vnode); 13531 } 13532 13533 static bool 13534 softdep_excess_items(struct ufsmount *ump, int item) 13535 { 13536 13537 KASSERT(item >= 0 && item < D_LAST, ("item %d", item)); 13538 return (dep_current[item] > max_softdeps && 13539 ump->softdep_curdeps[item] > max_softdeps / 13540 stat_flush_threads); 13541 } 13542 13543 static void 13544 schedule_cleanup(struct mount *mp) 13545 { 13546 struct ufsmount *ump; 13547 struct thread *td; 13548 13549 ump = VFSTOUFS(mp); 13550 LOCK_OWNED(ump); 13551 FREE_LOCK(ump); 13552 td = curthread; 13553 if ((td->td_pflags & TDP_KTHREAD) != 0 && 13554 (td->td_proc->p_flag2 & P2_AST_SU) == 0) { 13555 /* 13556 * No ast is delivered to kernel threads, so nobody 13557 * would deref the mp. Some kernel threads 13558 * explicitely check for AST, e.g. NFS daemon does 13559 * this in the serving loop. 13560 */ 13561 return; 13562 } 13563 if (td->td_su != NULL) 13564 vfs_rel(td->td_su); 13565 vfs_ref(mp); 13566 td->td_su = mp; 13567 thread_lock(td); 13568 td->td_flags |= TDF_ASTPENDING; 13569 thread_unlock(td); 13570 } 13571 13572 static void 13573 softdep_ast_cleanup_proc(struct thread *td) 13574 { 13575 struct mount *mp; 13576 struct ufsmount *ump; 13577 int error; 13578 bool req; 13579 13580 while ((mp = td->td_su) != NULL) { 13581 td->td_su = NULL; 13582 error = vfs_busy(mp, MBF_NOWAIT); 13583 vfs_rel(mp); 13584 if (error != 0) 13585 return; 13586 if (ffs_own_mount(mp) && MOUNTEDSOFTDEP(mp)) { 13587 ump = VFSTOUFS(mp); 13588 for (;;) { 13589 req = false; 13590 ACQUIRE_LOCK(ump); 13591 if (softdep_excess_items(ump, D_INODEDEP)) { 13592 req = true; 13593 request_cleanup(mp, FLUSH_INODES); 13594 } 13595 if (softdep_excess_items(ump, D_DIRREM)) { 13596 req = true; 13597 request_cleanup(mp, FLUSH_BLOCKS); 13598 } 13599 FREE_LOCK(ump); 13600 if (softdep_excess_items(ump, D_NEWBLK) || 13601 softdep_excess_items(ump, D_ALLOCDIRECT) || 13602 softdep_excess_items(ump, D_ALLOCINDIR)) { 13603 error = vn_start_write(NULL, &mp, 13604 V_WAIT); 13605 if (error == 0) { 13606 req = true; 13607 VFS_SYNC(mp, MNT_WAIT); 13608 vn_finished_write(mp); 13609 } 13610 } 13611 if ((td->td_pflags & TDP_KTHREAD) != 0 || !req) 13612 break; 13613 } 13614 } 13615 vfs_unbusy(mp); 13616 } 13617 if ((mp = td->td_su) != NULL) { 13618 td->td_su = NULL; 13619 vfs_rel(mp); 13620 } 13621 } 13622 13623 /* 13624 * If memory utilization has gotten too high, deliberately slow things 13625 * down and speed up the I/O processing. 13626 */ 13627 static int 13628 request_cleanup(mp, resource) 13629 struct mount *mp; 13630 int resource; 13631 { 13632 struct thread *td = curthread; 13633 struct ufsmount *ump; 13634 13635 ump = VFSTOUFS(mp); 13636 LOCK_OWNED(ump); 13637 /* 13638 * We never hold up the filesystem syncer or buf daemon. 13639 */ 13640 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF)) 13641 return (0); 13642 /* 13643 * First check to see if the work list has gotten backlogged. 13644 * If it has, co-opt this process to help clean up two entries. 13645 * Because this process may hold inodes locked, we cannot 13646 * handle any remove requests that might block on a locked 13647 * inode as that could lead to deadlock. We set TDP_SOFTDEP 13648 * to avoid recursively processing the worklist. 13649 */ 13650 if (ump->softdep_on_worklist > max_softdeps / 10) { 13651 td->td_pflags |= TDP_SOFTDEP; 13652 process_worklist_item(mp, 2, LK_NOWAIT); 13653 td->td_pflags &= ~TDP_SOFTDEP; 13654 stat_worklist_push += 2; 13655 return(1); 13656 } 13657 /* 13658 * Next, we attempt to speed up the syncer process. If that 13659 * is successful, then we allow the process to continue. 13660 */ 13661 if (softdep_speedup(ump) && 13662 resource != FLUSH_BLOCKS_WAIT && 13663 resource != FLUSH_INODES_WAIT) 13664 return(0); 13665 /* 13666 * If we are resource constrained on inode dependencies, try 13667 * flushing some dirty inodes. Otherwise, we are constrained 13668 * by file deletions, so try accelerating flushes of directories 13669 * with removal dependencies. We would like to do the cleanup 13670 * here, but we probably hold an inode locked at this point and 13671 * that might deadlock against one that we try to clean. So, 13672 * the best that we can do is request the syncer daemon to do 13673 * the cleanup for us. 13674 */ 13675 switch (resource) { 13676 13677 case FLUSH_INODES: 13678 case FLUSH_INODES_WAIT: 13679 ACQUIRE_GBLLOCK(&lk); 13680 stat_ino_limit_push += 1; 13681 req_clear_inodedeps += 1; 13682 FREE_GBLLOCK(&lk); 13683 stat_countp = &stat_ino_limit_hit; 13684 break; 13685 13686 case FLUSH_BLOCKS: 13687 case FLUSH_BLOCKS_WAIT: 13688 ACQUIRE_GBLLOCK(&lk); 13689 stat_blk_limit_push += 1; 13690 req_clear_remove += 1; 13691 FREE_GBLLOCK(&lk); 13692 stat_countp = &stat_blk_limit_hit; 13693 break; 13694 13695 default: 13696 panic("request_cleanup: unknown type"); 13697 } 13698 /* 13699 * Hopefully the syncer daemon will catch up and awaken us. 13700 * We wait at most tickdelay before proceeding in any case. 13701 */ 13702 ACQUIRE_GBLLOCK(&lk); 13703 FREE_LOCK(ump); 13704 proc_waiting += 1; 13705 if (callout_pending(&softdep_callout) == FALSE) 13706 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2, 13707 pause_timer, 0); 13708 13709 if ((td->td_pflags & TDP_KTHREAD) == 0) 13710 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0); 13711 proc_waiting -= 1; 13712 FREE_GBLLOCK(&lk); 13713 ACQUIRE_LOCK(ump); 13714 return (1); 13715 } 13716 13717 /* 13718 * Awaken processes pausing in request_cleanup and clear proc_waiting 13719 * to indicate that there is no longer a timer running. Pause_timer 13720 * will be called with the global softdep mutex (&lk) locked. 13721 */ 13722 static void 13723 pause_timer(arg) 13724 void *arg; 13725 { 13726 13727 GBLLOCK_OWNED(&lk); 13728 /* 13729 * The callout_ API has acquired mtx and will hold it around this 13730 * function call. 13731 */ 13732 *stat_countp += proc_waiting; 13733 wakeup(&proc_waiting); 13734 } 13735 13736 /* 13737 * If requested, try removing inode or removal dependencies. 13738 */ 13739 static void 13740 check_clear_deps(mp) 13741 struct mount *mp; 13742 { 13743 13744 /* 13745 * If we are suspended, it may be because of our using 13746 * too many inodedeps, so help clear them out. 13747 */ 13748 if (MOUNTEDSUJ(mp) && VFSTOUFS(mp)->softdep_jblocks->jb_suspended) 13749 clear_inodedeps(mp); 13750 /* 13751 * General requests for cleanup of backed up dependencies 13752 */ 13753 ACQUIRE_GBLLOCK(&lk); 13754 if (req_clear_inodedeps) { 13755 req_clear_inodedeps -= 1; 13756 FREE_GBLLOCK(&lk); 13757 clear_inodedeps(mp); 13758 ACQUIRE_GBLLOCK(&lk); 13759 wakeup(&proc_waiting); 13760 } 13761 if (req_clear_remove) { 13762 req_clear_remove -= 1; 13763 FREE_GBLLOCK(&lk); 13764 clear_remove(mp); 13765 ACQUIRE_GBLLOCK(&lk); 13766 wakeup(&proc_waiting); 13767 } 13768 FREE_GBLLOCK(&lk); 13769 } 13770 13771 /* 13772 * Flush out a directory with at least one removal dependency in an effort to 13773 * reduce the number of dirrem, freefile, and freeblks dependency structures. 13774 */ 13775 static void 13776 clear_remove(mp) 13777 struct mount *mp; 13778 { 13779 struct pagedep_hashhead *pagedephd; 13780 struct pagedep *pagedep; 13781 struct ufsmount *ump; 13782 struct vnode *vp; 13783 struct bufobj *bo; 13784 int error, cnt; 13785 ino_t ino; 13786 13787 ump = VFSTOUFS(mp); 13788 LOCK_OWNED(ump); 13789 13790 for (cnt = 0; cnt <= ump->pagedep_hash_size; cnt++) { 13791 pagedephd = &ump->pagedep_hashtbl[ump->pagedep_nextclean++]; 13792 if (ump->pagedep_nextclean > ump->pagedep_hash_size) 13793 ump->pagedep_nextclean = 0; 13794 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 13795 if (LIST_EMPTY(&pagedep->pd_dirremhd)) 13796 continue; 13797 ino = pagedep->pd_ino; 13798 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 13799 continue; 13800 FREE_LOCK(ump); 13801 13802 /* 13803 * Let unmount clear deps 13804 */ 13805 error = vfs_busy(mp, MBF_NOWAIT); 13806 if (error != 0) 13807 goto finish_write; 13808 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 13809 FFSV_FORCEINSMQ); 13810 vfs_unbusy(mp); 13811 if (error != 0) { 13812 softdep_error("clear_remove: vget", error); 13813 goto finish_write; 13814 } 13815 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 13816 softdep_error("clear_remove: fsync", error); 13817 bo = &vp->v_bufobj; 13818 BO_LOCK(bo); 13819 drain_output(vp); 13820 BO_UNLOCK(bo); 13821 vput(vp); 13822 finish_write: 13823 vn_finished_write(mp); 13824 ACQUIRE_LOCK(ump); 13825 return; 13826 } 13827 } 13828 } 13829 13830 /* 13831 * Clear out a block of dirty inodes in an effort to reduce 13832 * the number of inodedep dependency structures. 13833 */ 13834 static void 13835 clear_inodedeps(mp) 13836 struct mount *mp; 13837 { 13838 struct inodedep_hashhead *inodedephd; 13839 struct inodedep *inodedep; 13840 struct ufsmount *ump; 13841 struct vnode *vp; 13842 struct fs *fs; 13843 int error, cnt; 13844 ino_t firstino, lastino, ino; 13845 13846 ump = VFSTOUFS(mp); 13847 fs = ump->um_fs; 13848 LOCK_OWNED(ump); 13849 /* 13850 * Pick a random inode dependency to be cleared. 13851 * We will then gather up all the inodes in its block 13852 * that have dependencies and flush them out. 13853 */ 13854 for (cnt = 0; cnt <= ump->inodedep_hash_size; cnt++) { 13855 inodedephd = &ump->inodedep_hashtbl[ump->inodedep_nextclean++]; 13856 if (ump->inodedep_nextclean > ump->inodedep_hash_size) 13857 ump->inodedep_nextclean = 0; 13858 if ((inodedep = LIST_FIRST(inodedephd)) != NULL) 13859 break; 13860 } 13861 if (inodedep == NULL) 13862 return; 13863 /* 13864 * Find the last inode in the block with dependencies. 13865 */ 13866 firstino = rounddown2(inodedep->id_ino, INOPB(fs)); 13867 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--) 13868 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0) 13869 break; 13870 /* 13871 * Asynchronously push all but the last inode with dependencies. 13872 * Synchronously push the last inode with dependencies to ensure 13873 * that the inode block gets written to free up the inodedeps. 13874 */ 13875 for (ino = firstino; ino <= lastino; ino++) { 13876 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 13877 continue; 13878 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 13879 continue; 13880 FREE_LOCK(ump); 13881 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */ 13882 if (error != 0) { 13883 vn_finished_write(mp); 13884 ACQUIRE_LOCK(ump); 13885 return; 13886 } 13887 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 13888 FFSV_FORCEINSMQ)) != 0) { 13889 softdep_error("clear_inodedeps: vget", error); 13890 vfs_unbusy(mp); 13891 vn_finished_write(mp); 13892 ACQUIRE_LOCK(ump); 13893 return; 13894 } 13895 vfs_unbusy(mp); 13896 if (ino == lastino) { 13897 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0))) 13898 softdep_error("clear_inodedeps: fsync1", error); 13899 } else { 13900 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 13901 softdep_error("clear_inodedeps: fsync2", error); 13902 BO_LOCK(&vp->v_bufobj); 13903 drain_output(vp); 13904 BO_UNLOCK(&vp->v_bufobj); 13905 } 13906 vput(vp); 13907 vn_finished_write(mp); 13908 ACQUIRE_LOCK(ump); 13909 } 13910 } 13911 13912 void 13913 softdep_buf_append(bp, wkhd) 13914 struct buf *bp; 13915 struct workhead *wkhd; 13916 { 13917 struct worklist *wk; 13918 struct ufsmount *ump; 13919 13920 if ((wk = LIST_FIRST(wkhd)) == NULL) 13921 return; 13922 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 13923 ("softdep_buf_append called on non-softdep filesystem")); 13924 ump = VFSTOUFS(wk->wk_mp); 13925 ACQUIRE_LOCK(ump); 13926 while ((wk = LIST_FIRST(wkhd)) != NULL) { 13927 WORKLIST_REMOVE(wk); 13928 WORKLIST_INSERT(&bp->b_dep, wk); 13929 } 13930 FREE_LOCK(ump); 13931 13932 } 13933 13934 void 13935 softdep_inode_append(ip, cred, wkhd) 13936 struct inode *ip; 13937 struct ucred *cred; 13938 struct workhead *wkhd; 13939 { 13940 struct buf *bp; 13941 struct fs *fs; 13942 struct ufsmount *ump; 13943 int error; 13944 13945 ump = ITOUMP(ip); 13946 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 13947 ("softdep_inode_append called on non-softdep filesystem")); 13948 fs = ump->um_fs; 13949 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 13950 (int)fs->fs_bsize, cred, &bp); 13951 if (error) { 13952 bqrelse(bp); 13953 softdep_freework(wkhd); 13954 return; 13955 } 13956 softdep_buf_append(bp, wkhd); 13957 bqrelse(bp); 13958 } 13959 13960 void 13961 softdep_freework(wkhd) 13962 struct workhead *wkhd; 13963 { 13964 struct worklist *wk; 13965 struct ufsmount *ump; 13966 13967 if ((wk = LIST_FIRST(wkhd)) == NULL) 13968 return; 13969 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 13970 ("softdep_freework called on non-softdep filesystem")); 13971 ump = VFSTOUFS(wk->wk_mp); 13972 ACQUIRE_LOCK(ump); 13973 handle_jwork(wkhd); 13974 FREE_LOCK(ump); 13975 } 13976 13977 static struct ufsmount * 13978 softdep_bp_to_mp(bp) 13979 struct buf *bp; 13980 { 13981 struct mount *mp; 13982 struct vnode *vp; 13983 13984 if (LIST_EMPTY(&bp->b_dep)) 13985 return (NULL); 13986 vp = bp->b_vp; 13987 KASSERT(vp != NULL, 13988 ("%s, buffer with dependencies lacks vnode", __func__)); 13989 13990 /* 13991 * The ump mount point is stable after we get a correct 13992 * pointer, since bp is locked and this prevents unmount from 13993 * proceeding. But to get to it, we cannot dereference bp->b_dep 13994 * head wk_mp, because we do not yet own SU ump lock and 13995 * workitem might be freed while dereferenced. 13996 */ 13997 retry: 13998 switch (vp->v_type) { 13999 case VCHR: 14000 VI_LOCK(vp); 14001 mp = vp->v_type == VCHR ? vp->v_rdev->si_mountpt : NULL; 14002 VI_UNLOCK(vp); 14003 if (mp == NULL) 14004 goto retry; 14005 break; 14006 case VREG: 14007 case VDIR: 14008 case VLNK: 14009 case VFIFO: 14010 case VSOCK: 14011 mp = vp->v_mount; 14012 break; 14013 case VBLK: 14014 vn_printf(vp, "softdep_bp_to_mp: unexpected block device\n"); 14015 /* FALLTHROUGH */ 14016 case VNON: 14017 case VBAD: 14018 case VMARKER: 14019 mp = NULL; 14020 break; 14021 default: 14022 vn_printf(vp, "unknown vnode type"); 14023 mp = NULL; 14024 break; 14025 } 14026 return (VFSTOUFS(mp)); 14027 } 14028 14029 /* 14030 * Function to determine if the buffer has outstanding dependencies 14031 * that will cause a roll-back if the buffer is written. If wantcount 14032 * is set, return number of dependencies, otherwise just yes or no. 14033 */ 14034 static int 14035 softdep_count_dependencies(bp, wantcount) 14036 struct buf *bp; 14037 int wantcount; 14038 { 14039 struct worklist *wk; 14040 struct ufsmount *ump; 14041 struct bmsafemap *bmsafemap; 14042 struct freework *freework; 14043 struct inodedep *inodedep; 14044 struct indirdep *indirdep; 14045 struct freeblks *freeblks; 14046 struct allocindir *aip; 14047 struct pagedep *pagedep; 14048 struct dirrem *dirrem; 14049 struct newblk *newblk; 14050 struct mkdir *mkdir; 14051 struct diradd *dap; 14052 int i, retval; 14053 14054 ump = softdep_bp_to_mp(bp); 14055 if (ump == NULL) 14056 return (0); 14057 retval = 0; 14058 ACQUIRE_LOCK(ump); 14059 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 14060 switch (wk->wk_type) { 14061 14062 case D_INODEDEP: 14063 inodedep = WK_INODEDEP(wk); 14064 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 14065 /* bitmap allocation dependency */ 14066 retval += 1; 14067 if (!wantcount) 14068 goto out; 14069 } 14070 if (TAILQ_FIRST(&inodedep->id_inoupdt)) { 14071 /* direct block pointer dependency */ 14072 retval += 1; 14073 if (!wantcount) 14074 goto out; 14075 } 14076 if (TAILQ_FIRST(&inodedep->id_extupdt)) { 14077 /* direct block pointer dependency */ 14078 retval += 1; 14079 if (!wantcount) 14080 goto out; 14081 } 14082 if (TAILQ_FIRST(&inodedep->id_inoreflst)) { 14083 /* Add reference dependency. */ 14084 retval += 1; 14085 if (!wantcount) 14086 goto out; 14087 } 14088 continue; 14089 14090 case D_INDIRDEP: 14091 indirdep = WK_INDIRDEP(wk); 14092 14093 TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) { 14094 /* indirect truncation dependency */ 14095 retval += 1; 14096 if (!wantcount) 14097 goto out; 14098 } 14099 14100 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 14101 /* indirect block pointer dependency */ 14102 retval += 1; 14103 if (!wantcount) 14104 goto out; 14105 } 14106 continue; 14107 14108 case D_PAGEDEP: 14109 pagedep = WK_PAGEDEP(wk); 14110 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) { 14111 if (LIST_FIRST(&dirrem->dm_jremrefhd)) { 14112 /* Journal remove ref dependency. */ 14113 retval += 1; 14114 if (!wantcount) 14115 goto out; 14116 } 14117 } 14118 for (i = 0; i < DAHASHSZ; i++) { 14119 14120 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 14121 /* directory entry dependency */ 14122 retval += 1; 14123 if (!wantcount) 14124 goto out; 14125 } 14126 } 14127 continue; 14128 14129 case D_BMSAFEMAP: 14130 bmsafemap = WK_BMSAFEMAP(wk); 14131 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) { 14132 /* Add reference dependency. */ 14133 retval += 1; 14134 if (!wantcount) 14135 goto out; 14136 } 14137 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) { 14138 /* Allocate block dependency. */ 14139 retval += 1; 14140 if (!wantcount) 14141 goto out; 14142 } 14143 continue; 14144 14145 case D_FREEBLKS: 14146 freeblks = WK_FREEBLKS(wk); 14147 if (LIST_FIRST(&freeblks->fb_jblkdephd)) { 14148 /* Freeblk journal dependency. */ 14149 retval += 1; 14150 if (!wantcount) 14151 goto out; 14152 } 14153 continue; 14154 14155 case D_ALLOCDIRECT: 14156 case D_ALLOCINDIR: 14157 newblk = WK_NEWBLK(wk); 14158 if (newblk->nb_jnewblk) { 14159 /* Journal allocate dependency. */ 14160 retval += 1; 14161 if (!wantcount) 14162 goto out; 14163 } 14164 continue; 14165 14166 case D_MKDIR: 14167 mkdir = WK_MKDIR(wk); 14168 if (mkdir->md_jaddref) { 14169 /* Journal reference dependency. */ 14170 retval += 1; 14171 if (!wantcount) 14172 goto out; 14173 } 14174 continue; 14175 14176 case D_FREEWORK: 14177 case D_FREEDEP: 14178 case D_JSEGDEP: 14179 case D_JSEG: 14180 case D_SBDEP: 14181 /* never a dependency on these blocks */ 14182 continue; 14183 14184 default: 14185 panic("softdep_count_dependencies: Unexpected type %s", 14186 TYPENAME(wk->wk_type)); 14187 /* NOTREACHED */ 14188 } 14189 } 14190 out: 14191 FREE_LOCK(ump); 14192 return (retval); 14193 } 14194 14195 /* 14196 * Acquire exclusive access to a buffer. 14197 * Must be called with a locked mtx parameter. 14198 * Return acquired buffer or NULL on failure. 14199 */ 14200 static struct buf * 14201 getdirtybuf(bp, lock, waitfor) 14202 struct buf *bp; 14203 struct rwlock *lock; 14204 int waitfor; 14205 { 14206 int error; 14207 14208 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) { 14209 if (waitfor != MNT_WAIT) 14210 return (NULL); 14211 error = BUF_LOCK(bp, 14212 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock); 14213 /* 14214 * Even if we successfully acquire bp here, we have dropped 14215 * lock, which may violates our guarantee. 14216 */ 14217 if (error == 0) 14218 BUF_UNLOCK(bp); 14219 else if (error != ENOLCK) 14220 panic("getdirtybuf: inconsistent lock: %d", error); 14221 rw_wlock(lock); 14222 return (NULL); 14223 } 14224 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 14225 if (lock != BO_LOCKPTR(bp->b_bufobj) && waitfor == MNT_WAIT) { 14226 rw_wunlock(lock); 14227 BO_LOCK(bp->b_bufobj); 14228 BUF_UNLOCK(bp); 14229 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 14230 bp->b_vflags |= BV_BKGRDWAIT; 14231 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), 14232 PRIBIO | PDROP, "getbuf", 0); 14233 } else 14234 BO_UNLOCK(bp->b_bufobj); 14235 rw_wlock(lock); 14236 return (NULL); 14237 } 14238 BUF_UNLOCK(bp); 14239 if (waitfor != MNT_WAIT) 14240 return (NULL); 14241 #ifdef DEBUG_VFS_LOCKS 14242 if (bp->b_vp->v_type != VCHR) 14243 ASSERT_BO_WLOCKED(bp->b_bufobj); 14244 #endif 14245 bp->b_vflags |= BV_BKGRDWAIT; 14246 rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0); 14247 return (NULL); 14248 } 14249 if ((bp->b_flags & B_DELWRI) == 0) { 14250 BUF_UNLOCK(bp); 14251 return (NULL); 14252 } 14253 bremfree(bp); 14254 return (bp); 14255 } 14256 14257 14258 /* 14259 * Check if it is safe to suspend the file system now. On entry, 14260 * the vnode interlock for devvp should be held. Return 0 with 14261 * the mount interlock held if the file system can be suspended now, 14262 * otherwise return EAGAIN with the mount interlock held. 14263 */ 14264 int 14265 softdep_check_suspend(struct mount *mp, 14266 struct vnode *devvp, 14267 int softdep_depcnt, 14268 int softdep_accdepcnt, 14269 int secondary_writes, 14270 int secondary_accwrites) 14271 { 14272 struct bufobj *bo; 14273 struct ufsmount *ump; 14274 struct inodedep *inodedep; 14275 int error, unlinked; 14276 14277 bo = &devvp->v_bufobj; 14278 ASSERT_BO_WLOCKED(bo); 14279 14280 /* 14281 * If we are not running with soft updates, then we need only 14282 * deal with secondary writes as we try to suspend. 14283 */ 14284 if (MOUNTEDSOFTDEP(mp) == 0) { 14285 MNT_ILOCK(mp); 14286 while (mp->mnt_secondary_writes != 0) { 14287 BO_UNLOCK(bo); 14288 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 14289 (PUSER - 1) | PDROP, "secwr", 0); 14290 BO_LOCK(bo); 14291 MNT_ILOCK(mp); 14292 } 14293 14294 /* 14295 * Reasons for needing more work before suspend: 14296 * - Dirty buffers on devvp. 14297 * - Secondary writes occurred after start of vnode sync loop 14298 */ 14299 error = 0; 14300 if (bo->bo_numoutput > 0 || 14301 bo->bo_dirty.bv_cnt > 0 || 14302 secondary_writes != 0 || 14303 mp->mnt_secondary_writes != 0 || 14304 secondary_accwrites != mp->mnt_secondary_accwrites) 14305 error = EAGAIN; 14306 BO_UNLOCK(bo); 14307 return (error); 14308 } 14309 14310 /* 14311 * If we are running with soft updates, then we need to coordinate 14312 * with them as we try to suspend. 14313 */ 14314 ump = VFSTOUFS(mp); 14315 for (;;) { 14316 if (!TRY_ACQUIRE_LOCK(ump)) { 14317 BO_UNLOCK(bo); 14318 ACQUIRE_LOCK(ump); 14319 FREE_LOCK(ump); 14320 BO_LOCK(bo); 14321 continue; 14322 } 14323 MNT_ILOCK(mp); 14324 if (mp->mnt_secondary_writes != 0) { 14325 FREE_LOCK(ump); 14326 BO_UNLOCK(bo); 14327 msleep(&mp->mnt_secondary_writes, 14328 MNT_MTX(mp), 14329 (PUSER - 1) | PDROP, "secwr", 0); 14330 BO_LOCK(bo); 14331 continue; 14332 } 14333 break; 14334 } 14335 14336 unlinked = 0; 14337 if (MOUNTEDSUJ(mp)) { 14338 for (inodedep = TAILQ_FIRST(&ump->softdep_unlinked); 14339 inodedep != NULL; 14340 inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 14341 if ((inodedep->id_state & (UNLINKED | UNLINKLINKS | 14342 UNLINKONLIST)) != (UNLINKED | UNLINKLINKS | 14343 UNLINKONLIST) || 14344 !check_inodedep_free(inodedep)) 14345 continue; 14346 unlinked++; 14347 } 14348 } 14349 14350 /* 14351 * Reasons for needing more work before suspend: 14352 * - Dirty buffers on devvp. 14353 * - Softdep activity occurred after start of vnode sync loop 14354 * - Secondary writes occurred after start of vnode sync loop 14355 */ 14356 error = 0; 14357 if (bo->bo_numoutput > 0 || 14358 bo->bo_dirty.bv_cnt > 0 || 14359 softdep_depcnt != unlinked || 14360 ump->softdep_deps != unlinked || 14361 softdep_accdepcnt != ump->softdep_accdeps || 14362 secondary_writes != 0 || 14363 mp->mnt_secondary_writes != 0 || 14364 secondary_accwrites != mp->mnt_secondary_accwrites) 14365 error = EAGAIN; 14366 FREE_LOCK(ump); 14367 BO_UNLOCK(bo); 14368 return (error); 14369 } 14370 14371 14372 /* 14373 * Get the number of dependency structures for the file system, both 14374 * the current number and the total number allocated. These will 14375 * later be used to detect that softdep processing has occurred. 14376 */ 14377 void 14378 softdep_get_depcounts(struct mount *mp, 14379 int *softdep_depsp, 14380 int *softdep_accdepsp) 14381 { 14382 struct ufsmount *ump; 14383 14384 if (MOUNTEDSOFTDEP(mp) == 0) { 14385 *softdep_depsp = 0; 14386 *softdep_accdepsp = 0; 14387 return; 14388 } 14389 ump = VFSTOUFS(mp); 14390 ACQUIRE_LOCK(ump); 14391 *softdep_depsp = ump->softdep_deps; 14392 *softdep_accdepsp = ump->softdep_accdeps; 14393 FREE_LOCK(ump); 14394 } 14395 14396 /* 14397 * Wait for pending output on a vnode to complete. 14398 */ 14399 static void 14400 drain_output(vp) 14401 struct vnode *vp; 14402 { 14403 14404 ASSERT_VOP_LOCKED(vp, "drain_output"); 14405 (void)bufobj_wwait(&vp->v_bufobj, 0, 0); 14406 } 14407 14408 /* 14409 * Called whenever a buffer that is being invalidated or reallocated 14410 * contains dependencies. This should only happen if an I/O error has 14411 * occurred. The routine is called with the buffer locked. 14412 */ 14413 static void 14414 softdep_deallocate_dependencies(bp) 14415 struct buf *bp; 14416 { 14417 14418 if ((bp->b_ioflags & BIO_ERROR) == 0) 14419 panic("softdep_deallocate_dependencies: dangling deps"); 14420 if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL) 14421 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error); 14422 else 14423 printf("softdep_deallocate_dependencies: " 14424 "got error %d while accessing filesystem\n", bp->b_error); 14425 if (bp->b_error != ENXIO) 14426 panic("softdep_deallocate_dependencies: unrecovered I/O error"); 14427 } 14428 14429 /* 14430 * Function to handle asynchronous write errors in the filesystem. 14431 */ 14432 static void 14433 softdep_error(func, error) 14434 char *func; 14435 int error; 14436 { 14437 14438 /* XXX should do something better! */ 14439 printf("%s: got error %d while accessing filesystem\n", func, error); 14440 } 14441 14442 #ifdef DDB 14443 14444 /* exported to ffs_vfsops.c */ 14445 extern void db_print_ffs(struct ufsmount *ump); 14446 void 14447 db_print_ffs(struct ufsmount *ump) 14448 { 14449 db_printf("mp %p (%s) devvp %p\n", ump->um_mountp, 14450 ump->um_mountp->mnt_stat.f_mntonname, ump->um_devvp); 14451 db_printf(" fs %p su_wl %d su_deps %d su_req %d\n", 14452 ump->um_fs, ump->softdep_on_worklist, 14453 ump->softdep_deps, ump->softdep_req); 14454 } 14455 14456 static void 14457 worklist_print(struct worklist *wk, int verbose) 14458 { 14459 14460 if (!verbose) { 14461 db_printf("%s: %p state 0x%b\n", TYPENAME(wk->wk_type), wk, 14462 (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS); 14463 return; 14464 } 14465 db_printf("worklist: %p type %s state 0x%b next %p\n ", wk, 14466 TYPENAME(wk->wk_type), (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS, 14467 LIST_NEXT(wk, wk_list)); 14468 db_print_ffs(VFSTOUFS(wk->wk_mp)); 14469 } 14470 14471 static void 14472 inodedep_print(struct inodedep *inodedep, int verbose) 14473 { 14474 14475 worklist_print(&inodedep->id_list, 0); 14476 db_printf(" fs %p ino %jd inoblk %jd delta %jd nlink %jd\n", 14477 inodedep->id_fs, 14478 (intmax_t)inodedep->id_ino, 14479 (intmax_t)fsbtodb(inodedep->id_fs, 14480 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)), 14481 (intmax_t)inodedep->id_nlinkdelta, 14482 (intmax_t)inodedep->id_savednlink); 14483 14484 if (verbose == 0) 14485 return; 14486 14487 db_printf(" bmsafemap %p, mkdiradd %p, inoreflst %p\n", 14488 inodedep->id_bmsafemap, 14489 inodedep->id_mkdiradd, 14490 TAILQ_FIRST(&inodedep->id_inoreflst)); 14491 db_printf(" dirremhd %p, pendinghd %p, bufwait %p\n", 14492 LIST_FIRST(&inodedep->id_dirremhd), 14493 LIST_FIRST(&inodedep->id_pendinghd), 14494 LIST_FIRST(&inodedep->id_bufwait)); 14495 db_printf(" inowait %p, inoupdt %p, newinoupdt %p\n", 14496 LIST_FIRST(&inodedep->id_inowait), 14497 TAILQ_FIRST(&inodedep->id_inoupdt), 14498 TAILQ_FIRST(&inodedep->id_newinoupdt)); 14499 db_printf(" extupdt %p, newextupdt %p, freeblklst %p\n", 14500 TAILQ_FIRST(&inodedep->id_extupdt), 14501 TAILQ_FIRST(&inodedep->id_newextupdt), 14502 TAILQ_FIRST(&inodedep->id_freeblklst)); 14503 db_printf(" saveino %p, savedsize %jd, savedextsize %jd\n", 14504 inodedep->id_savedino1, 14505 (intmax_t)inodedep->id_savedsize, 14506 (intmax_t)inodedep->id_savedextsize); 14507 } 14508 14509 static void 14510 newblk_print(struct newblk *nbp) 14511 { 14512 14513 worklist_print(&nbp->nb_list, 0); 14514 db_printf(" newblkno %jd\n", (intmax_t)nbp->nb_newblkno); 14515 db_printf(" jnewblk %p, bmsafemap %p, freefrag %p\n", 14516 &nbp->nb_jnewblk, 14517 &nbp->nb_bmsafemap, 14518 &nbp->nb_freefrag); 14519 db_printf(" indirdeps %p, newdirblk %p, jwork %p\n", 14520 LIST_FIRST(&nbp->nb_indirdeps), 14521 LIST_FIRST(&nbp->nb_newdirblk), 14522 LIST_FIRST(&nbp->nb_jwork)); 14523 } 14524 14525 static void 14526 allocdirect_print(struct allocdirect *adp) 14527 { 14528 14529 newblk_print(&adp->ad_block); 14530 db_printf(" oldblkno %jd, oldsize %ld, newsize %ld\n", 14531 adp->ad_oldblkno, adp->ad_oldsize, adp->ad_newsize); 14532 db_printf(" offset %d, inodedep %p\n", 14533 adp->ad_offset, adp->ad_inodedep); 14534 } 14535 14536 static void 14537 allocindir_print(struct allocindir *aip) 14538 { 14539 14540 newblk_print(&aip->ai_block); 14541 db_printf(" oldblkno %jd, lbn %jd\n", 14542 (intmax_t)aip->ai_oldblkno, (intmax_t)aip->ai_lbn); 14543 db_printf(" offset %d, indirdep %p\n", 14544 aip->ai_offset, aip->ai_indirdep); 14545 } 14546 14547 static void 14548 mkdir_print(struct mkdir *mkdir) 14549 { 14550 14551 worklist_print(&mkdir->md_list, 0); 14552 db_printf(" diradd %p, jaddref %p, buf %p\n", 14553 mkdir->md_diradd, mkdir->md_jaddref, mkdir->md_buf); 14554 } 14555 14556 DB_SHOW_COMMAND(sd_inodedep, db_show_sd_inodedep) 14557 { 14558 14559 if (have_addr == 0) { 14560 db_printf("inodedep address required\n"); 14561 return; 14562 } 14563 inodedep_print((struct inodedep*)addr, 1); 14564 } 14565 14566 DB_SHOW_COMMAND(sd_allinodedeps, db_show_sd_allinodedeps) 14567 { 14568 struct inodedep_hashhead *inodedephd; 14569 struct inodedep *inodedep; 14570 struct ufsmount *ump; 14571 int cnt; 14572 14573 if (have_addr == 0) { 14574 db_printf("ufsmount address required\n"); 14575 return; 14576 } 14577 ump = (struct ufsmount *)addr; 14578 for (cnt = 0; cnt < ump->inodedep_hash_size; cnt++) { 14579 inodedephd = &ump->inodedep_hashtbl[cnt]; 14580 LIST_FOREACH(inodedep, inodedephd, id_hash) { 14581 inodedep_print(inodedep, 0); 14582 } 14583 } 14584 } 14585 14586 DB_SHOW_COMMAND(sd_worklist, db_show_sd_worklist) 14587 { 14588 14589 if (have_addr == 0) { 14590 db_printf("worklist address required\n"); 14591 return; 14592 } 14593 worklist_print((struct worklist *)addr, 1); 14594 } 14595 14596 DB_SHOW_COMMAND(sd_workhead, db_show_sd_workhead) 14597 { 14598 struct worklist *wk; 14599 struct workhead *wkhd; 14600 14601 if (have_addr == 0) { 14602 db_printf("worklist address required " 14603 "(for example value in bp->b_dep)\n"); 14604 return; 14605 } 14606 /* 14607 * We often do not have the address of the worklist head but 14608 * instead a pointer to its first entry (e.g., we have the 14609 * contents of bp->b_dep rather than &bp->b_dep). But the back 14610 * pointer of bp->b_dep will point at the head of the list, so 14611 * we cheat and use that instead. If we are in the middle of 14612 * a list we will still get the same result, so nothing 14613 * unexpected will result. 14614 */ 14615 wk = (struct worklist *)addr; 14616 if (wk == NULL) 14617 return; 14618 wkhd = (struct workhead *)wk->wk_list.le_prev; 14619 LIST_FOREACH(wk, wkhd, wk_list) { 14620 switch(wk->wk_type) { 14621 case D_INODEDEP: 14622 inodedep_print(WK_INODEDEP(wk), 0); 14623 continue; 14624 case D_ALLOCDIRECT: 14625 allocdirect_print(WK_ALLOCDIRECT(wk)); 14626 continue; 14627 case D_ALLOCINDIR: 14628 allocindir_print(WK_ALLOCINDIR(wk)); 14629 continue; 14630 case D_MKDIR: 14631 mkdir_print(WK_MKDIR(wk)); 14632 continue; 14633 default: 14634 worklist_print(wk, 0); 14635 continue; 14636 } 14637 } 14638 } 14639 14640 DB_SHOW_COMMAND(sd_mkdir, db_show_sd_mkdir) 14641 { 14642 if (have_addr == 0) { 14643 db_printf("mkdir address required\n"); 14644 return; 14645 } 14646 mkdir_print((struct mkdir *)addr); 14647 } 14648 14649 DB_SHOW_COMMAND(sd_mkdir_list, db_show_sd_mkdir_list) 14650 { 14651 struct mkdirlist *mkdirlisthd; 14652 struct mkdir *mkdir; 14653 14654 if (have_addr == 0) { 14655 db_printf("mkdir listhead address required\n"); 14656 return; 14657 } 14658 mkdirlisthd = (struct mkdirlist *)addr; 14659 LIST_FOREACH(mkdir, mkdirlisthd, md_mkdirs) { 14660 mkdir_print(mkdir); 14661 if (mkdir->md_diradd != NULL) { 14662 db_printf(" "); 14663 worklist_print(&mkdir->md_diradd->da_list, 0); 14664 } 14665 if (mkdir->md_jaddref != NULL) { 14666 db_printf(" "); 14667 worklist_print(&mkdir->md_jaddref->ja_list, 0); 14668 } 14669 } 14670 } 14671 14672 DB_SHOW_COMMAND(sd_allocdirect, db_show_sd_allocdirect) 14673 { 14674 if (have_addr == 0) { 14675 db_printf("allocdirect address required\n"); 14676 return; 14677 } 14678 allocdirect_print((struct allocdirect *)addr); 14679 } 14680 14681 DB_SHOW_COMMAND(sd_allocindir, db_show_sd_allocindir) 14682 { 14683 if (have_addr == 0) { 14684 db_printf("allocindir address required\n"); 14685 return; 14686 } 14687 allocindir_print((struct allocindir *)addr); 14688 } 14689 14690 #endif /* DDB */ 14691 14692 #endif /* SOFTUPDATES */ 14693