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", 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, 1315 CTLFLAG_RW | CTLFLAG_STATS, &stat_worklist_push, 0,""); 1316 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_push, 1317 CTLFLAG_RW | CTLFLAG_STATS, &stat_blk_limit_push, 0,""); 1318 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_push, 1319 CTLFLAG_RW | CTLFLAG_STATS, &stat_ino_limit_push, 0,""); 1320 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_hit, 1321 CTLFLAG_RW | CTLFLAG_STATS, &stat_blk_limit_hit, 0, ""); 1322 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_hit, 1323 CTLFLAG_RW | CTLFLAG_STATS, &stat_ino_limit_hit, 0, ""); 1324 SYSCTL_INT(_debug_softdep, OID_AUTO, sync_limit_hit, 1325 CTLFLAG_RW | CTLFLAG_STATS, &stat_sync_limit_hit, 0, ""); 1326 SYSCTL_INT(_debug_softdep, OID_AUTO, indir_blk_ptrs, 1327 CTLFLAG_RW | CTLFLAG_STATS, &stat_indir_blk_ptrs, 0, ""); 1328 SYSCTL_INT(_debug_softdep, OID_AUTO, inode_bitmap, 1329 CTLFLAG_RW | CTLFLAG_STATS, &stat_inode_bitmap, 0, ""); 1330 SYSCTL_INT(_debug_softdep, OID_AUTO, direct_blk_ptrs, 1331 CTLFLAG_RW | CTLFLAG_STATS, &stat_direct_blk_ptrs, 0, ""); 1332 SYSCTL_INT(_debug_softdep, OID_AUTO, dir_entry, 1333 CTLFLAG_RW | CTLFLAG_STATS, &stat_dir_entry, 0, ""); 1334 SYSCTL_INT(_debug_softdep, OID_AUTO, jaddref_rollback, 1335 CTLFLAG_RW | CTLFLAG_STATS, &stat_jaddref, 0, ""); 1336 SYSCTL_INT(_debug_softdep, OID_AUTO, jnewblk_rollback, 1337 CTLFLAG_RW | CTLFLAG_STATS, &stat_jnewblk, 0, ""); 1338 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_low, 1339 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_low, 0, ""); 1340 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_min, 1341 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_min, 0, ""); 1342 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_wait, 1343 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_wait, 0, ""); 1344 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_filepage, 1345 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_filepage, 0, ""); 1346 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_freeblks, 1347 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_freeblks, 0, ""); 1348 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_inode, 1349 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_inode, 0, ""); 1350 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_newblk, 1351 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_newblk, 0, ""); 1352 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_blkrequests, 1353 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_blkrequests, 0, ""); 1354 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_inorequests, 1355 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_inorequests, 0, ""); 1356 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_high_delay, 1357 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_high_delay, 0, ""); 1358 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_retries, 1359 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_retries, 0, ""); 1360 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_failures, 1361 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_failures, 0, ""); 1362 1363 SYSCTL_INT(_debug_softdep, OID_AUTO, flushcache, CTLFLAG_RW, 1364 &softdep_flushcache, 0, ""); 1365 SYSCTL_INT(_debug_softdep, OID_AUTO, emptyjblocks, CTLFLAG_RD, 1366 &stat_emptyjblocks, 0, ""); 1367 1368 SYSCTL_DECL(_vfs_ffs); 1369 1370 /* Whether to recompute the summary at mount time */ 1371 static int compute_summary_at_mount = 0; 1372 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW, 1373 &compute_summary_at_mount, 0, "Recompute summary at mount"); 1374 static int print_threads = 0; 1375 SYSCTL_INT(_debug_softdep, OID_AUTO, print_threads, CTLFLAG_RW, 1376 &print_threads, 0, "Notify flusher thread start/stop"); 1377 1378 /* List of all filesystems mounted with soft updates */ 1379 static TAILQ_HEAD(, mount_softdeps) softdepmounts; 1380 1381 /* 1382 * This function cleans the worklist for a filesystem. 1383 * Each filesystem running with soft dependencies gets its own 1384 * thread to run in this function. The thread is started up in 1385 * softdep_mount and shutdown in softdep_unmount. They show up 1386 * as part of the kernel "bufdaemon" process whose process 1387 * entry is available in bufdaemonproc. 1388 */ 1389 static int searchfailed; 1390 extern struct proc *bufdaemonproc; 1391 static void 1392 softdep_flush(addr) 1393 void *addr; 1394 { 1395 struct mount *mp; 1396 struct thread *td; 1397 struct ufsmount *ump; 1398 1399 td = curthread; 1400 td->td_pflags |= TDP_NORUNNINGBUF; 1401 mp = (struct mount *)addr; 1402 ump = VFSTOUFS(mp); 1403 atomic_add_int(&stat_flush_threads, 1); 1404 ACQUIRE_LOCK(ump); 1405 ump->softdep_flags &= ~FLUSH_STARTING; 1406 wakeup(&ump->softdep_flushtd); 1407 FREE_LOCK(ump); 1408 if (print_threads) { 1409 if (stat_flush_threads == 1) 1410 printf("Running %s at pid %d\n", bufdaemonproc->p_comm, 1411 bufdaemonproc->p_pid); 1412 printf("Start thread %s\n", td->td_name); 1413 } 1414 for (;;) { 1415 while (softdep_process_worklist(mp, 0) > 0 || 1416 (MOUNTEDSUJ(mp) && 1417 VFSTOUFS(mp)->softdep_jblocks->jb_suspended)) 1418 kthread_suspend_check(); 1419 ACQUIRE_LOCK(ump); 1420 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1421 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, 1422 "sdflush", hz / 2); 1423 ump->softdep_flags &= ~FLUSH_CLEANUP; 1424 /* 1425 * Check to see if we are done and need to exit. 1426 */ 1427 if ((ump->softdep_flags & FLUSH_EXIT) == 0) { 1428 FREE_LOCK(ump); 1429 continue; 1430 } 1431 ump->softdep_flags &= ~FLUSH_EXIT; 1432 FREE_LOCK(ump); 1433 wakeup(&ump->softdep_flags); 1434 if (print_threads) 1435 printf("Stop thread %s: searchfailed %d, did cleanups %d\n", td->td_name, searchfailed, ump->um_softdep->sd_cleanups); 1436 atomic_subtract_int(&stat_flush_threads, 1); 1437 kthread_exit(); 1438 panic("kthread_exit failed\n"); 1439 } 1440 } 1441 1442 static void 1443 worklist_speedup(mp) 1444 struct mount *mp; 1445 { 1446 struct ufsmount *ump; 1447 1448 ump = VFSTOUFS(mp); 1449 LOCK_OWNED(ump); 1450 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1451 ump->softdep_flags |= FLUSH_CLEANUP; 1452 wakeup(&ump->softdep_flushtd); 1453 } 1454 1455 static int 1456 softdep_speedup(ump) 1457 struct ufsmount *ump; 1458 { 1459 struct ufsmount *altump; 1460 struct mount_softdeps *sdp; 1461 1462 LOCK_OWNED(ump); 1463 worklist_speedup(ump->um_mountp); 1464 bd_speedup(); 1465 /* 1466 * If we have global shortages, then we need other 1467 * filesystems to help with the cleanup. Here we wakeup a 1468 * flusher thread for a filesystem that is over its fair 1469 * share of resources. 1470 */ 1471 if (req_clear_inodedeps || req_clear_remove) { 1472 ACQUIRE_GBLLOCK(&lk); 1473 TAILQ_FOREACH(sdp, &softdepmounts, sd_next) { 1474 if ((altump = sdp->sd_ump) == ump) 1475 continue; 1476 if (((req_clear_inodedeps && 1477 altump->softdep_curdeps[D_INODEDEP] > 1478 max_softdeps / stat_flush_threads) || 1479 (req_clear_remove && 1480 altump->softdep_curdeps[D_DIRREM] > 1481 (max_softdeps / 2) / stat_flush_threads)) && 1482 TRY_ACQUIRE_LOCK(altump)) 1483 break; 1484 } 1485 if (sdp == NULL) { 1486 searchfailed++; 1487 FREE_GBLLOCK(&lk); 1488 } else { 1489 /* 1490 * Move to the end of the list so we pick a 1491 * different one on out next try. 1492 */ 1493 TAILQ_REMOVE(&softdepmounts, sdp, sd_next); 1494 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next); 1495 FREE_GBLLOCK(&lk); 1496 if ((altump->softdep_flags & 1497 (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1498 altump->softdep_flags |= FLUSH_CLEANUP; 1499 altump->um_softdep->sd_cleanups++; 1500 wakeup(&altump->softdep_flushtd); 1501 FREE_LOCK(altump); 1502 } 1503 } 1504 return (speedup_syncer()); 1505 } 1506 1507 /* 1508 * Add an item to the end of the work queue. 1509 * This routine requires that the lock be held. 1510 * This is the only routine that adds items to the list. 1511 * The following routine is the only one that removes items 1512 * and does so in order from first to last. 1513 */ 1514 1515 #define WK_HEAD 0x0001 /* Add to HEAD. */ 1516 #define WK_NODELAY 0x0002 /* Process immediately. */ 1517 1518 static void 1519 add_to_worklist(wk, flags) 1520 struct worklist *wk; 1521 int flags; 1522 { 1523 struct ufsmount *ump; 1524 1525 ump = VFSTOUFS(wk->wk_mp); 1526 LOCK_OWNED(ump); 1527 if (wk->wk_state & ONWORKLIST) 1528 panic("add_to_worklist: %s(0x%X) already on list", 1529 TYPENAME(wk->wk_type), wk->wk_state); 1530 wk->wk_state |= ONWORKLIST; 1531 if (ump->softdep_on_worklist == 0) { 1532 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1533 ump->softdep_worklist_tail = wk; 1534 } else if (flags & WK_HEAD) { 1535 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1536 } else { 1537 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list); 1538 ump->softdep_worklist_tail = wk; 1539 } 1540 ump->softdep_on_worklist += 1; 1541 if (flags & WK_NODELAY) 1542 worklist_speedup(wk->wk_mp); 1543 } 1544 1545 /* 1546 * Remove the item to be processed. If we are removing the last 1547 * item on the list, we need to recalculate the tail pointer. 1548 */ 1549 static void 1550 remove_from_worklist(wk) 1551 struct worklist *wk; 1552 { 1553 struct ufsmount *ump; 1554 1555 ump = VFSTOUFS(wk->wk_mp); 1556 if (ump->softdep_worklist_tail == wk) 1557 ump->softdep_worklist_tail = 1558 (struct worklist *)wk->wk_list.le_prev; 1559 WORKLIST_REMOVE(wk); 1560 ump->softdep_on_worklist -= 1; 1561 } 1562 1563 static void 1564 wake_worklist(wk) 1565 struct worklist *wk; 1566 { 1567 if (wk->wk_state & IOWAITING) { 1568 wk->wk_state &= ~IOWAITING; 1569 wakeup(wk); 1570 } 1571 } 1572 1573 static void 1574 wait_worklist(wk, wmesg) 1575 struct worklist *wk; 1576 char *wmesg; 1577 { 1578 struct ufsmount *ump; 1579 1580 ump = VFSTOUFS(wk->wk_mp); 1581 wk->wk_state |= IOWAITING; 1582 msleep(wk, LOCK_PTR(ump), PVM, wmesg, 0); 1583 } 1584 1585 /* 1586 * Process that runs once per second to handle items in the background queue. 1587 * 1588 * Note that we ensure that everything is done in the order in which they 1589 * appear in the queue. The code below depends on this property to ensure 1590 * that blocks of a file are freed before the inode itself is freed. This 1591 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated 1592 * until all the old ones have been purged from the dependency lists. 1593 */ 1594 static int 1595 softdep_process_worklist(mp, full) 1596 struct mount *mp; 1597 int full; 1598 { 1599 int cnt, matchcnt; 1600 struct ufsmount *ump; 1601 long starttime; 1602 1603 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp")); 1604 if (MOUNTEDSOFTDEP(mp) == 0) 1605 return (0); 1606 matchcnt = 0; 1607 ump = VFSTOUFS(mp); 1608 ACQUIRE_LOCK(ump); 1609 starttime = time_second; 1610 softdep_process_journal(mp, NULL, full ? MNT_WAIT : 0); 1611 check_clear_deps(mp); 1612 while (ump->softdep_on_worklist > 0) { 1613 if ((cnt = process_worklist_item(mp, 10, LK_NOWAIT)) == 0) 1614 break; 1615 else 1616 matchcnt += cnt; 1617 check_clear_deps(mp); 1618 /* 1619 * We do not generally want to stop for buffer space, but if 1620 * we are really being a buffer hog, we will stop and wait. 1621 */ 1622 if (should_yield()) { 1623 FREE_LOCK(ump); 1624 kern_yield(PRI_USER); 1625 bwillwrite(); 1626 ACQUIRE_LOCK(ump); 1627 } 1628 /* 1629 * Never allow processing to run for more than one 1630 * second. This gives the syncer thread the opportunity 1631 * to pause if appropriate. 1632 */ 1633 if (!full && starttime != time_second) 1634 break; 1635 } 1636 if (full == 0) 1637 journal_unsuspend(ump); 1638 FREE_LOCK(ump); 1639 return (matchcnt); 1640 } 1641 1642 /* 1643 * Process all removes associated with a vnode if we are running out of 1644 * journal space. Any other process which attempts to flush these will 1645 * be unable as we have the vnodes locked. 1646 */ 1647 static void 1648 process_removes(vp) 1649 struct vnode *vp; 1650 { 1651 struct inodedep *inodedep; 1652 struct dirrem *dirrem; 1653 struct ufsmount *ump; 1654 struct mount *mp; 1655 ino_t inum; 1656 1657 mp = vp->v_mount; 1658 ump = VFSTOUFS(mp); 1659 LOCK_OWNED(ump); 1660 inum = VTOI(vp)->i_number; 1661 for (;;) { 1662 top: 1663 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1664 return; 1665 LIST_FOREACH(dirrem, &inodedep->id_dirremhd, dm_inonext) { 1666 /* 1667 * If another thread is trying to lock this vnode 1668 * it will fail but we must wait for it to do so 1669 * before we can proceed. 1670 */ 1671 if (dirrem->dm_state & INPROGRESS) { 1672 wait_worklist(&dirrem->dm_list, "pwrwait"); 1673 goto top; 1674 } 1675 if ((dirrem->dm_state & (COMPLETE | ONWORKLIST)) == 1676 (COMPLETE | ONWORKLIST)) 1677 break; 1678 } 1679 if (dirrem == NULL) 1680 return; 1681 remove_from_worklist(&dirrem->dm_list); 1682 FREE_LOCK(ump); 1683 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1684 panic("process_removes: suspended filesystem"); 1685 handle_workitem_remove(dirrem, 0); 1686 vn_finished_secondary_write(mp); 1687 ACQUIRE_LOCK(ump); 1688 } 1689 } 1690 1691 /* 1692 * Process all truncations associated with a vnode if we are running out 1693 * of journal space. This is called when the vnode lock is already held 1694 * and no other process can clear the truncation. This function returns 1695 * a value greater than zero if it did any work. 1696 */ 1697 static void 1698 process_truncates(vp) 1699 struct vnode *vp; 1700 { 1701 struct inodedep *inodedep; 1702 struct freeblks *freeblks; 1703 struct ufsmount *ump; 1704 struct mount *mp; 1705 ino_t inum; 1706 int cgwait; 1707 1708 mp = vp->v_mount; 1709 ump = VFSTOUFS(mp); 1710 LOCK_OWNED(ump); 1711 inum = VTOI(vp)->i_number; 1712 for (;;) { 1713 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1714 return; 1715 cgwait = 0; 1716 TAILQ_FOREACH(freeblks, &inodedep->id_freeblklst, fb_next) { 1717 /* Journal entries not yet written. */ 1718 if (!LIST_EMPTY(&freeblks->fb_jblkdephd)) { 1719 jwait(&LIST_FIRST( 1720 &freeblks->fb_jblkdephd)->jb_list, 1721 MNT_WAIT); 1722 break; 1723 } 1724 /* Another thread is executing this item. */ 1725 if (freeblks->fb_state & INPROGRESS) { 1726 wait_worklist(&freeblks->fb_list, "ptrwait"); 1727 break; 1728 } 1729 /* Freeblks is waiting on a inode write. */ 1730 if ((freeblks->fb_state & COMPLETE) == 0) { 1731 FREE_LOCK(ump); 1732 ffs_update(vp, 1); 1733 ACQUIRE_LOCK(ump); 1734 break; 1735 } 1736 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST)) == 1737 (ALLCOMPLETE | ONWORKLIST)) { 1738 remove_from_worklist(&freeblks->fb_list); 1739 freeblks->fb_state |= INPROGRESS; 1740 FREE_LOCK(ump); 1741 if (vn_start_secondary_write(NULL, &mp, 1742 V_NOWAIT)) 1743 panic("process_truncates: " 1744 "suspended filesystem"); 1745 handle_workitem_freeblocks(freeblks, 0); 1746 vn_finished_secondary_write(mp); 1747 ACQUIRE_LOCK(ump); 1748 break; 1749 } 1750 if (freeblks->fb_cgwait) 1751 cgwait++; 1752 } 1753 if (cgwait) { 1754 FREE_LOCK(ump); 1755 sync_cgs(mp, MNT_WAIT); 1756 ffs_sync_snap(mp, MNT_WAIT); 1757 ACQUIRE_LOCK(ump); 1758 continue; 1759 } 1760 if (freeblks == NULL) 1761 break; 1762 } 1763 return; 1764 } 1765 1766 /* 1767 * Process one item on the worklist. 1768 */ 1769 static int 1770 process_worklist_item(mp, target, flags) 1771 struct mount *mp; 1772 int target; 1773 int flags; 1774 { 1775 struct worklist sentinel; 1776 struct worklist *wk; 1777 struct ufsmount *ump; 1778 int matchcnt; 1779 int error; 1780 1781 KASSERT(mp != NULL, ("process_worklist_item: NULL mp")); 1782 /* 1783 * If we are being called because of a process doing a 1784 * copy-on-write, then it is not safe to write as we may 1785 * recurse into the copy-on-write routine. 1786 */ 1787 if (curthread->td_pflags & TDP_COWINPROGRESS) 1788 return (-1); 1789 PHOLD(curproc); /* Don't let the stack go away. */ 1790 ump = VFSTOUFS(mp); 1791 LOCK_OWNED(ump); 1792 matchcnt = 0; 1793 sentinel.wk_mp = NULL; 1794 sentinel.wk_type = D_SENTINEL; 1795 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, &sentinel, wk_list); 1796 for (wk = LIST_NEXT(&sentinel, wk_list); wk != NULL; 1797 wk = LIST_NEXT(&sentinel, wk_list)) { 1798 if (wk->wk_type == D_SENTINEL) { 1799 LIST_REMOVE(&sentinel, wk_list); 1800 LIST_INSERT_AFTER(wk, &sentinel, wk_list); 1801 continue; 1802 } 1803 if (wk->wk_state & INPROGRESS) 1804 panic("process_worklist_item: %p already in progress.", 1805 wk); 1806 wk->wk_state |= INPROGRESS; 1807 remove_from_worklist(wk); 1808 FREE_LOCK(ump); 1809 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1810 panic("process_worklist_item: suspended filesystem"); 1811 switch (wk->wk_type) { 1812 case D_DIRREM: 1813 /* removal of a directory entry */ 1814 error = handle_workitem_remove(WK_DIRREM(wk), flags); 1815 break; 1816 1817 case D_FREEBLKS: 1818 /* releasing blocks and/or fragments from a file */ 1819 error = handle_workitem_freeblocks(WK_FREEBLKS(wk), 1820 flags); 1821 break; 1822 1823 case D_FREEFRAG: 1824 /* releasing a fragment when replaced as a file grows */ 1825 handle_workitem_freefrag(WK_FREEFRAG(wk)); 1826 error = 0; 1827 break; 1828 1829 case D_FREEFILE: 1830 /* releasing an inode when its link count drops to 0 */ 1831 handle_workitem_freefile(WK_FREEFILE(wk)); 1832 error = 0; 1833 break; 1834 1835 default: 1836 panic("%s_process_worklist: Unknown type %s", 1837 "softdep", TYPENAME(wk->wk_type)); 1838 /* NOTREACHED */ 1839 } 1840 vn_finished_secondary_write(mp); 1841 ACQUIRE_LOCK(ump); 1842 if (error == 0) { 1843 if (++matchcnt == target) 1844 break; 1845 continue; 1846 } 1847 /* 1848 * We have to retry the worklist item later. Wake up any 1849 * waiters who may be able to complete it immediately and 1850 * add the item back to the head so we don't try to execute 1851 * it again. 1852 */ 1853 wk->wk_state &= ~INPROGRESS; 1854 wake_worklist(wk); 1855 add_to_worklist(wk, WK_HEAD); 1856 } 1857 /* Sentinal could've become the tail from remove_from_worklist. */ 1858 if (ump->softdep_worklist_tail == &sentinel) 1859 ump->softdep_worklist_tail = 1860 (struct worklist *)sentinel.wk_list.le_prev; 1861 LIST_REMOVE(&sentinel, wk_list); 1862 PRELE(curproc); 1863 return (matchcnt); 1864 } 1865 1866 /* 1867 * Move dependencies from one buffer to another. 1868 */ 1869 int 1870 softdep_move_dependencies(oldbp, newbp) 1871 struct buf *oldbp; 1872 struct buf *newbp; 1873 { 1874 struct worklist *wk, *wktail; 1875 struct ufsmount *ump; 1876 int dirty; 1877 1878 if ((wk = LIST_FIRST(&oldbp->b_dep)) == NULL) 1879 return (0); 1880 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 1881 ("softdep_move_dependencies called on non-softdep filesystem")); 1882 dirty = 0; 1883 wktail = NULL; 1884 ump = VFSTOUFS(wk->wk_mp); 1885 ACQUIRE_LOCK(ump); 1886 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) { 1887 LIST_REMOVE(wk, wk_list); 1888 if (wk->wk_type == D_BMSAFEMAP && 1889 bmsafemap_backgroundwrite(WK_BMSAFEMAP(wk), newbp)) 1890 dirty = 1; 1891 if (wktail == NULL) 1892 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list); 1893 else 1894 LIST_INSERT_AFTER(wktail, wk, wk_list); 1895 wktail = wk; 1896 } 1897 FREE_LOCK(ump); 1898 1899 return (dirty); 1900 } 1901 1902 /* 1903 * Purge the work list of all items associated with a particular mount point. 1904 */ 1905 int 1906 softdep_flushworklist(oldmnt, countp, td) 1907 struct mount *oldmnt; 1908 int *countp; 1909 struct thread *td; 1910 { 1911 struct vnode *devvp; 1912 struct ufsmount *ump; 1913 int count, error; 1914 1915 /* 1916 * Alternately flush the block device associated with the mount 1917 * point and process any dependencies that the flushing 1918 * creates. We continue until no more worklist dependencies 1919 * are found. 1920 */ 1921 *countp = 0; 1922 error = 0; 1923 ump = VFSTOUFS(oldmnt); 1924 devvp = ump->um_devvp; 1925 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) { 1926 *countp += count; 1927 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1928 error = VOP_FSYNC(devvp, MNT_WAIT, td); 1929 VOP_UNLOCK(devvp, 0); 1930 if (error != 0) 1931 break; 1932 } 1933 return (error); 1934 } 1935 1936 #define SU_WAITIDLE_RETRIES 20 1937 static int 1938 softdep_waitidle(struct mount *mp, int flags __unused) 1939 { 1940 struct ufsmount *ump; 1941 struct vnode *devvp; 1942 struct thread *td; 1943 int error, i; 1944 1945 ump = VFSTOUFS(mp); 1946 devvp = ump->um_devvp; 1947 td = curthread; 1948 error = 0; 1949 ACQUIRE_LOCK(ump); 1950 for (i = 0; i < SU_WAITIDLE_RETRIES && ump->softdep_deps != 0; i++) { 1951 ump->softdep_req = 1; 1952 KASSERT((flags & FORCECLOSE) == 0 || 1953 ump->softdep_on_worklist == 0, 1954 ("softdep_waitidle: work added after flush")); 1955 msleep(&ump->softdep_deps, LOCK_PTR(ump), PVM | PDROP, 1956 "softdeps", 10 * hz); 1957 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1958 error = VOP_FSYNC(devvp, MNT_WAIT, td); 1959 VOP_UNLOCK(devvp, 0); 1960 ACQUIRE_LOCK(ump); 1961 if (error != 0) 1962 break; 1963 } 1964 ump->softdep_req = 0; 1965 if (i == SU_WAITIDLE_RETRIES && error == 0 && ump->softdep_deps != 0) { 1966 error = EBUSY; 1967 printf("softdep_waitidle: Failed to flush worklist for %p\n", 1968 mp); 1969 } 1970 FREE_LOCK(ump); 1971 return (error); 1972 } 1973 1974 /* 1975 * Flush all vnodes and worklist items associated with a specified mount point. 1976 */ 1977 int 1978 softdep_flushfiles(oldmnt, flags, td) 1979 struct mount *oldmnt; 1980 int flags; 1981 struct thread *td; 1982 { 1983 #ifdef QUOTA 1984 struct ufsmount *ump; 1985 int i; 1986 #endif 1987 int error, early, depcount, loopcnt, retry_flush_count, retry; 1988 int morework; 1989 1990 KASSERT(MOUNTEDSOFTDEP(oldmnt) != 0, 1991 ("softdep_flushfiles called on non-softdep filesystem")); 1992 loopcnt = 10; 1993 retry_flush_count = 3; 1994 retry_flush: 1995 error = 0; 1996 1997 /* 1998 * Alternately flush the vnodes associated with the mount 1999 * point and process any dependencies that the flushing 2000 * creates. In theory, this loop can happen at most twice, 2001 * but we give it a few extra just to be sure. 2002 */ 2003 for (; loopcnt > 0; loopcnt--) { 2004 /* 2005 * Do another flush in case any vnodes were brought in 2006 * as part of the cleanup operations. 2007 */ 2008 early = retry_flush_count == 1 || (oldmnt->mnt_kern_flag & 2009 MNTK_UNMOUNT) == 0 ? 0 : EARLYFLUSH; 2010 if ((error = ffs_flushfiles(oldmnt, flags | early, td)) != 0) 2011 break; 2012 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 || 2013 depcount == 0) 2014 break; 2015 } 2016 /* 2017 * If we are unmounting then it is an error to fail. If we 2018 * are simply trying to downgrade to read-only, then filesystem 2019 * activity can keep us busy forever, so we just fail with EBUSY. 2020 */ 2021 if (loopcnt == 0) { 2022 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) 2023 panic("softdep_flushfiles: looping"); 2024 error = EBUSY; 2025 } 2026 if (!error) 2027 error = softdep_waitidle(oldmnt, flags); 2028 if (!error) { 2029 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) { 2030 retry = 0; 2031 MNT_ILOCK(oldmnt); 2032 morework = oldmnt->mnt_nvnodelistsize > 0; 2033 #ifdef QUOTA 2034 ump = VFSTOUFS(oldmnt); 2035 UFS_LOCK(ump); 2036 for (i = 0; i < MAXQUOTAS; i++) { 2037 if (ump->um_quotas[i] != NULLVP) 2038 morework = 1; 2039 } 2040 UFS_UNLOCK(ump); 2041 #endif 2042 if (morework) { 2043 if (--retry_flush_count > 0) { 2044 retry = 1; 2045 loopcnt = 3; 2046 } else 2047 error = EBUSY; 2048 } 2049 MNT_IUNLOCK(oldmnt); 2050 if (retry) 2051 goto retry_flush; 2052 } 2053 } 2054 return (error); 2055 } 2056 2057 /* 2058 * Structure hashing. 2059 * 2060 * There are four types of structures that can be looked up: 2061 * 1) pagedep structures identified by mount point, inode number, 2062 * and logical block. 2063 * 2) inodedep structures identified by mount point and inode number. 2064 * 3) newblk structures identified by mount point and 2065 * physical block number. 2066 * 4) bmsafemap structures identified by mount point and 2067 * cylinder group number. 2068 * 2069 * The "pagedep" and "inodedep" dependency structures are hashed 2070 * separately from the file blocks and inodes to which they correspond. 2071 * This separation helps when the in-memory copy of an inode or 2072 * file block must be replaced. It also obviates the need to access 2073 * an inode or file page when simply updating (or de-allocating) 2074 * dependency structures. Lookup of newblk structures is needed to 2075 * find newly allocated blocks when trying to associate them with 2076 * their allocdirect or allocindir structure. 2077 * 2078 * The lookup routines optionally create and hash a new instance when 2079 * an existing entry is not found. The bmsafemap lookup routine always 2080 * allocates a new structure if an existing one is not found. 2081 */ 2082 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */ 2083 2084 /* 2085 * Structures and routines associated with pagedep caching. 2086 */ 2087 #define PAGEDEP_HASH(ump, inum, lbn) \ 2088 (&(ump)->pagedep_hashtbl[((inum) + (lbn)) & (ump)->pagedep_hash_size]) 2089 2090 static int 2091 pagedep_find(pagedephd, ino, lbn, pagedeppp) 2092 struct pagedep_hashhead *pagedephd; 2093 ino_t ino; 2094 ufs_lbn_t lbn; 2095 struct pagedep **pagedeppp; 2096 { 2097 struct pagedep *pagedep; 2098 2099 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 2100 if (ino == pagedep->pd_ino && lbn == pagedep->pd_lbn) { 2101 *pagedeppp = pagedep; 2102 return (1); 2103 } 2104 } 2105 *pagedeppp = NULL; 2106 return (0); 2107 } 2108 /* 2109 * Look up a pagedep. Return 1 if found, 0 otherwise. 2110 * If not found, allocate if DEPALLOC flag is passed. 2111 * Found or allocated entry is returned in pagedeppp. 2112 */ 2113 static int 2114 pagedep_lookup(mp, bp, ino, lbn, flags, pagedeppp) 2115 struct mount *mp; 2116 struct buf *bp; 2117 ino_t ino; 2118 ufs_lbn_t lbn; 2119 int flags; 2120 struct pagedep **pagedeppp; 2121 { 2122 struct pagedep *pagedep; 2123 struct pagedep_hashhead *pagedephd; 2124 struct worklist *wk; 2125 struct ufsmount *ump; 2126 int ret; 2127 int i; 2128 2129 ump = VFSTOUFS(mp); 2130 LOCK_OWNED(ump); 2131 if (bp) { 2132 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 2133 if (wk->wk_type == D_PAGEDEP) { 2134 *pagedeppp = WK_PAGEDEP(wk); 2135 return (1); 2136 } 2137 } 2138 } 2139 pagedephd = PAGEDEP_HASH(ump, ino, lbn); 2140 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp); 2141 if (ret) { 2142 if (((*pagedeppp)->pd_state & ONWORKLIST) == 0 && bp) 2143 WORKLIST_INSERT(&bp->b_dep, &(*pagedeppp)->pd_list); 2144 return (1); 2145 } 2146 if ((flags & DEPALLOC) == 0) 2147 return (0); 2148 FREE_LOCK(ump); 2149 pagedep = malloc(sizeof(struct pagedep), 2150 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO); 2151 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp); 2152 ACQUIRE_LOCK(ump); 2153 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp); 2154 if (*pagedeppp) { 2155 /* 2156 * This should never happen since we only create pagedeps 2157 * with the vnode lock held. Could be an assert. 2158 */ 2159 WORKITEM_FREE(pagedep, D_PAGEDEP); 2160 return (ret); 2161 } 2162 pagedep->pd_ino = ino; 2163 pagedep->pd_lbn = lbn; 2164 LIST_INIT(&pagedep->pd_dirremhd); 2165 LIST_INIT(&pagedep->pd_pendinghd); 2166 for (i = 0; i < DAHASHSZ; i++) 2167 LIST_INIT(&pagedep->pd_diraddhd[i]); 2168 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash); 2169 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list); 2170 *pagedeppp = pagedep; 2171 return (0); 2172 } 2173 2174 /* 2175 * Structures and routines associated with inodedep caching. 2176 */ 2177 #define INODEDEP_HASH(ump, inum) \ 2178 (&(ump)->inodedep_hashtbl[(inum) & (ump)->inodedep_hash_size]) 2179 2180 static int 2181 inodedep_find(inodedephd, inum, inodedeppp) 2182 struct inodedep_hashhead *inodedephd; 2183 ino_t inum; 2184 struct inodedep **inodedeppp; 2185 { 2186 struct inodedep *inodedep; 2187 2188 LIST_FOREACH(inodedep, inodedephd, id_hash) 2189 if (inum == inodedep->id_ino) 2190 break; 2191 if (inodedep) { 2192 *inodedeppp = inodedep; 2193 return (1); 2194 } 2195 *inodedeppp = NULL; 2196 2197 return (0); 2198 } 2199 /* 2200 * Look up an inodedep. Return 1 if found, 0 if not found. 2201 * If not found, allocate if DEPALLOC flag is passed. 2202 * Found or allocated entry is returned in inodedeppp. 2203 */ 2204 static int 2205 inodedep_lookup(mp, inum, flags, inodedeppp) 2206 struct mount *mp; 2207 ino_t inum; 2208 int flags; 2209 struct inodedep **inodedeppp; 2210 { 2211 struct inodedep *inodedep; 2212 struct inodedep_hashhead *inodedephd; 2213 struct ufsmount *ump; 2214 struct fs *fs; 2215 2216 ump = VFSTOUFS(mp); 2217 LOCK_OWNED(ump); 2218 fs = ump->um_fs; 2219 inodedephd = INODEDEP_HASH(ump, inum); 2220 2221 if (inodedep_find(inodedephd, inum, inodedeppp)) 2222 return (1); 2223 if ((flags & DEPALLOC) == 0) 2224 return (0); 2225 /* 2226 * If the system is over its limit and our filesystem is 2227 * responsible for more than our share of that usage and 2228 * we are not in a rush, request some inodedep cleanup. 2229 */ 2230 if (softdep_excess_items(ump, D_INODEDEP)) 2231 schedule_cleanup(mp); 2232 else 2233 FREE_LOCK(ump); 2234 inodedep = malloc(sizeof(struct inodedep), 2235 M_INODEDEP, M_SOFTDEP_FLAGS); 2236 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp); 2237 ACQUIRE_LOCK(ump); 2238 if (inodedep_find(inodedephd, inum, inodedeppp)) { 2239 WORKITEM_FREE(inodedep, D_INODEDEP); 2240 return (1); 2241 } 2242 inodedep->id_fs = fs; 2243 inodedep->id_ino = inum; 2244 inodedep->id_state = ALLCOMPLETE; 2245 inodedep->id_nlinkdelta = 0; 2246 inodedep->id_savedino1 = NULL; 2247 inodedep->id_savedsize = -1; 2248 inodedep->id_savedextsize = -1; 2249 inodedep->id_savednlink = -1; 2250 inodedep->id_bmsafemap = NULL; 2251 inodedep->id_mkdiradd = NULL; 2252 LIST_INIT(&inodedep->id_dirremhd); 2253 LIST_INIT(&inodedep->id_pendinghd); 2254 LIST_INIT(&inodedep->id_inowait); 2255 LIST_INIT(&inodedep->id_bufwait); 2256 TAILQ_INIT(&inodedep->id_inoreflst); 2257 TAILQ_INIT(&inodedep->id_inoupdt); 2258 TAILQ_INIT(&inodedep->id_newinoupdt); 2259 TAILQ_INIT(&inodedep->id_extupdt); 2260 TAILQ_INIT(&inodedep->id_newextupdt); 2261 TAILQ_INIT(&inodedep->id_freeblklst); 2262 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash); 2263 *inodedeppp = inodedep; 2264 return (0); 2265 } 2266 2267 /* 2268 * Structures and routines associated with newblk caching. 2269 */ 2270 #define NEWBLK_HASH(ump, inum) \ 2271 (&(ump)->newblk_hashtbl[(inum) & (ump)->newblk_hash_size]) 2272 2273 static int 2274 newblk_find(newblkhd, newblkno, flags, newblkpp) 2275 struct newblk_hashhead *newblkhd; 2276 ufs2_daddr_t newblkno; 2277 int flags; 2278 struct newblk **newblkpp; 2279 { 2280 struct newblk *newblk; 2281 2282 LIST_FOREACH(newblk, newblkhd, nb_hash) { 2283 if (newblkno != newblk->nb_newblkno) 2284 continue; 2285 /* 2286 * If we're creating a new dependency don't match those that 2287 * have already been converted to allocdirects. This is for 2288 * a frag extend. 2289 */ 2290 if ((flags & DEPALLOC) && newblk->nb_list.wk_type != D_NEWBLK) 2291 continue; 2292 break; 2293 } 2294 if (newblk) { 2295 *newblkpp = newblk; 2296 return (1); 2297 } 2298 *newblkpp = NULL; 2299 return (0); 2300 } 2301 2302 /* 2303 * Look up a newblk. Return 1 if found, 0 if not found. 2304 * If not found, allocate if DEPALLOC flag is passed. 2305 * Found or allocated entry is returned in newblkpp. 2306 */ 2307 static int 2308 newblk_lookup(mp, newblkno, flags, newblkpp) 2309 struct mount *mp; 2310 ufs2_daddr_t newblkno; 2311 int flags; 2312 struct newblk **newblkpp; 2313 { 2314 struct newblk *newblk; 2315 struct newblk_hashhead *newblkhd; 2316 struct ufsmount *ump; 2317 2318 ump = VFSTOUFS(mp); 2319 LOCK_OWNED(ump); 2320 newblkhd = NEWBLK_HASH(ump, newblkno); 2321 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) 2322 return (1); 2323 if ((flags & DEPALLOC) == 0) 2324 return (0); 2325 if (softdep_excess_items(ump, D_NEWBLK) || 2326 softdep_excess_items(ump, D_ALLOCDIRECT) || 2327 softdep_excess_items(ump, D_ALLOCINDIR)) 2328 schedule_cleanup(mp); 2329 else 2330 FREE_LOCK(ump); 2331 newblk = malloc(sizeof(union allblk), M_NEWBLK, 2332 M_SOFTDEP_FLAGS | M_ZERO); 2333 workitem_alloc(&newblk->nb_list, D_NEWBLK, mp); 2334 ACQUIRE_LOCK(ump); 2335 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) { 2336 WORKITEM_FREE(newblk, D_NEWBLK); 2337 return (1); 2338 } 2339 newblk->nb_freefrag = NULL; 2340 LIST_INIT(&newblk->nb_indirdeps); 2341 LIST_INIT(&newblk->nb_newdirblk); 2342 LIST_INIT(&newblk->nb_jwork); 2343 newblk->nb_state = ATTACHED; 2344 newblk->nb_newblkno = newblkno; 2345 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash); 2346 *newblkpp = newblk; 2347 return (0); 2348 } 2349 2350 /* 2351 * Structures and routines associated with freed indirect block caching. 2352 */ 2353 #define INDIR_HASH(ump, blkno) \ 2354 (&(ump)->indir_hashtbl[(blkno) & (ump)->indir_hash_size]) 2355 2356 /* 2357 * Lookup an indirect block in the indir hash table. The freework is 2358 * removed and potentially freed. The caller must do a blocking journal 2359 * write before writing to the blkno. 2360 */ 2361 static int 2362 indirblk_lookup(mp, blkno) 2363 struct mount *mp; 2364 ufs2_daddr_t blkno; 2365 { 2366 struct freework *freework; 2367 struct indir_hashhead *wkhd; 2368 struct ufsmount *ump; 2369 2370 ump = VFSTOUFS(mp); 2371 wkhd = INDIR_HASH(ump, blkno); 2372 TAILQ_FOREACH(freework, wkhd, fw_next) { 2373 if (freework->fw_blkno != blkno) 2374 continue; 2375 indirblk_remove(freework); 2376 return (1); 2377 } 2378 return (0); 2379 } 2380 2381 /* 2382 * Insert an indirect block represented by freework into the indirblk 2383 * hash table so that it may prevent the block from being re-used prior 2384 * to the journal being written. 2385 */ 2386 static void 2387 indirblk_insert(freework) 2388 struct freework *freework; 2389 { 2390 struct jblocks *jblocks; 2391 struct jseg *jseg; 2392 struct ufsmount *ump; 2393 2394 ump = VFSTOUFS(freework->fw_list.wk_mp); 2395 jblocks = ump->softdep_jblocks; 2396 jseg = TAILQ_LAST(&jblocks->jb_segs, jseglst); 2397 if (jseg == NULL) 2398 return; 2399 2400 LIST_INSERT_HEAD(&jseg->js_indirs, freework, fw_segs); 2401 TAILQ_INSERT_HEAD(INDIR_HASH(ump, freework->fw_blkno), freework, 2402 fw_next); 2403 freework->fw_state &= ~DEPCOMPLETE; 2404 } 2405 2406 static void 2407 indirblk_remove(freework) 2408 struct freework *freework; 2409 { 2410 struct ufsmount *ump; 2411 2412 ump = VFSTOUFS(freework->fw_list.wk_mp); 2413 LIST_REMOVE(freework, fw_segs); 2414 TAILQ_REMOVE(INDIR_HASH(ump, freework->fw_blkno), freework, fw_next); 2415 freework->fw_state |= DEPCOMPLETE; 2416 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 2417 WORKITEM_FREE(freework, D_FREEWORK); 2418 } 2419 2420 /* 2421 * Executed during filesystem system initialization before 2422 * mounting any filesystems. 2423 */ 2424 void 2425 softdep_initialize() 2426 { 2427 2428 TAILQ_INIT(&softdepmounts); 2429 #ifdef __LP64__ 2430 max_softdeps = desiredvnodes * 4; 2431 #else 2432 max_softdeps = desiredvnodes * 2; 2433 #endif 2434 2435 /* initialise bioops hack */ 2436 bioops.io_start = softdep_disk_io_initiation; 2437 bioops.io_complete = softdep_disk_write_complete; 2438 bioops.io_deallocate = softdep_deallocate_dependencies; 2439 bioops.io_countdeps = softdep_count_dependencies; 2440 softdep_ast_cleanup = softdep_ast_cleanup_proc; 2441 2442 /* Initialize the callout with an mtx. */ 2443 callout_init_mtx(&softdep_callout, &lk, 0); 2444 } 2445 2446 /* 2447 * Executed after all filesystems have been unmounted during 2448 * filesystem module unload. 2449 */ 2450 void 2451 softdep_uninitialize() 2452 { 2453 2454 /* clear bioops hack */ 2455 bioops.io_start = NULL; 2456 bioops.io_complete = NULL; 2457 bioops.io_deallocate = NULL; 2458 bioops.io_countdeps = NULL; 2459 softdep_ast_cleanup = NULL; 2460 2461 callout_drain(&softdep_callout); 2462 } 2463 2464 /* 2465 * Called at mount time to notify the dependency code that a 2466 * filesystem wishes to use it. 2467 */ 2468 int 2469 softdep_mount(devvp, mp, fs, cred) 2470 struct vnode *devvp; 2471 struct mount *mp; 2472 struct fs *fs; 2473 struct ucred *cred; 2474 { 2475 struct csum_total cstotal; 2476 struct mount_softdeps *sdp; 2477 struct ufsmount *ump; 2478 struct cg *cgp; 2479 struct buf *bp; 2480 u_int cyl, i; 2481 int error; 2482 2483 sdp = malloc(sizeof(struct mount_softdeps), M_MOUNTDATA, 2484 M_WAITOK | M_ZERO); 2485 MNT_ILOCK(mp); 2486 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP; 2487 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) { 2488 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) | 2489 MNTK_SOFTDEP | MNTK_NOASYNC; 2490 } 2491 ump = VFSTOUFS(mp); 2492 ump->um_softdep = sdp; 2493 MNT_IUNLOCK(mp); 2494 rw_init(LOCK_PTR(ump), "per-fs softdep"); 2495 sdp->sd_ump = ump; 2496 LIST_INIT(&ump->softdep_workitem_pending); 2497 LIST_INIT(&ump->softdep_journal_pending); 2498 TAILQ_INIT(&ump->softdep_unlinked); 2499 LIST_INIT(&ump->softdep_dirtycg); 2500 ump->softdep_worklist_tail = NULL; 2501 ump->softdep_on_worklist = 0; 2502 ump->softdep_deps = 0; 2503 LIST_INIT(&ump->softdep_mkdirlisthd); 2504 ump->pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP, 2505 &ump->pagedep_hash_size); 2506 ump->pagedep_nextclean = 0; 2507 ump->inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP, 2508 &ump->inodedep_hash_size); 2509 ump->inodedep_nextclean = 0; 2510 ump->newblk_hashtbl = hashinit(max_softdeps / 2, M_NEWBLK, 2511 &ump->newblk_hash_size); 2512 ump->bmsafemap_hashtbl = hashinit(1024, M_BMSAFEMAP, 2513 &ump->bmsafemap_hash_size); 2514 i = 1 << (ffs(desiredvnodes / 10) - 1); 2515 ump->indir_hashtbl = malloc(i * sizeof(struct indir_hashhead), 2516 M_FREEWORK, M_WAITOK); 2517 ump->indir_hash_size = i - 1; 2518 for (i = 0; i <= ump->indir_hash_size; i++) 2519 TAILQ_INIT(&ump->indir_hashtbl[i]); 2520 ACQUIRE_GBLLOCK(&lk); 2521 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next); 2522 FREE_GBLLOCK(&lk); 2523 if ((fs->fs_flags & FS_SUJ) && 2524 (error = journal_mount(mp, fs, cred)) != 0) { 2525 printf("Failed to start journal: %d\n", error); 2526 softdep_unmount(mp); 2527 return (error); 2528 } 2529 /* 2530 * Start our flushing thread in the bufdaemon process. 2531 */ 2532 ACQUIRE_LOCK(ump); 2533 ump->softdep_flags |= FLUSH_STARTING; 2534 FREE_LOCK(ump); 2535 kproc_kthread_add(&softdep_flush, mp, &bufdaemonproc, 2536 &ump->softdep_flushtd, 0, 0, "softdepflush", "%s worker", 2537 mp->mnt_stat.f_mntonname); 2538 ACQUIRE_LOCK(ump); 2539 while ((ump->softdep_flags & FLUSH_STARTING) != 0) { 2540 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, "sdstart", 2541 hz / 2); 2542 } 2543 FREE_LOCK(ump); 2544 /* 2545 * When doing soft updates, the counters in the 2546 * superblock may have gotten out of sync. Recomputation 2547 * can take a long time and can be deferred for background 2548 * fsck. However, the old behavior of scanning the cylinder 2549 * groups and recalculating them at mount time is available 2550 * by setting vfs.ffs.compute_summary_at_mount to one. 2551 */ 2552 if (compute_summary_at_mount == 0 || fs->fs_clean != 0) 2553 return (0); 2554 bzero(&cstotal, sizeof cstotal); 2555 for (cyl = 0; cyl < fs->fs_ncg; cyl++) { 2556 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)), 2557 fs->fs_cgsize, cred, &bp)) != 0) { 2558 brelse(bp); 2559 softdep_unmount(mp); 2560 return (error); 2561 } 2562 cgp = (struct cg *)bp->b_data; 2563 cstotal.cs_nffree += cgp->cg_cs.cs_nffree; 2564 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree; 2565 cstotal.cs_nifree += cgp->cg_cs.cs_nifree; 2566 cstotal.cs_ndir += cgp->cg_cs.cs_ndir; 2567 fs->fs_cs(fs, cyl) = cgp->cg_cs; 2568 brelse(bp); 2569 } 2570 #ifdef INVARIANTS 2571 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal)) 2572 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt); 2573 #endif 2574 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal); 2575 return (0); 2576 } 2577 2578 void 2579 softdep_unmount(mp) 2580 struct mount *mp; 2581 { 2582 struct ufsmount *ump; 2583 #ifdef INVARIANTS 2584 int i; 2585 #endif 2586 2587 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 2588 ("softdep_unmount called on non-softdep filesystem")); 2589 ump = VFSTOUFS(mp); 2590 MNT_ILOCK(mp); 2591 mp->mnt_flag &= ~MNT_SOFTDEP; 2592 if (MOUNTEDSUJ(mp) == 0) { 2593 MNT_IUNLOCK(mp); 2594 } else { 2595 mp->mnt_flag &= ~MNT_SUJ; 2596 MNT_IUNLOCK(mp); 2597 journal_unmount(ump); 2598 } 2599 /* 2600 * Shut down our flushing thread. Check for NULL is if 2601 * softdep_mount errors out before the thread has been created. 2602 */ 2603 if (ump->softdep_flushtd != NULL) { 2604 ACQUIRE_LOCK(ump); 2605 ump->softdep_flags |= FLUSH_EXIT; 2606 wakeup(&ump->softdep_flushtd); 2607 msleep(&ump->softdep_flags, LOCK_PTR(ump), PVM | PDROP, 2608 "sdwait", 0); 2609 KASSERT((ump->softdep_flags & FLUSH_EXIT) == 0, 2610 ("Thread shutdown failed")); 2611 } 2612 /* 2613 * Free up our resources. 2614 */ 2615 ACQUIRE_GBLLOCK(&lk); 2616 TAILQ_REMOVE(&softdepmounts, ump->um_softdep, sd_next); 2617 FREE_GBLLOCK(&lk); 2618 rw_destroy(LOCK_PTR(ump)); 2619 hashdestroy(ump->pagedep_hashtbl, M_PAGEDEP, ump->pagedep_hash_size); 2620 hashdestroy(ump->inodedep_hashtbl, M_INODEDEP, ump->inodedep_hash_size); 2621 hashdestroy(ump->newblk_hashtbl, M_NEWBLK, ump->newblk_hash_size); 2622 hashdestroy(ump->bmsafemap_hashtbl, M_BMSAFEMAP, 2623 ump->bmsafemap_hash_size); 2624 free(ump->indir_hashtbl, M_FREEWORK); 2625 #ifdef INVARIANTS 2626 for (i = 0; i <= D_LAST; i++) 2627 KASSERT(ump->softdep_curdeps[i] == 0, 2628 ("Unmount %s: Dep type %s != 0 (%ld)", ump->um_fs->fs_fsmnt, 2629 TYPENAME(i), ump->softdep_curdeps[i])); 2630 #endif 2631 free(ump->um_softdep, M_MOUNTDATA); 2632 } 2633 2634 static struct jblocks * 2635 jblocks_create(void) 2636 { 2637 struct jblocks *jblocks; 2638 2639 jblocks = malloc(sizeof(*jblocks), M_JBLOCKS, M_WAITOK | M_ZERO); 2640 TAILQ_INIT(&jblocks->jb_segs); 2641 jblocks->jb_avail = 10; 2642 jblocks->jb_extent = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2643 M_JBLOCKS, M_WAITOK | M_ZERO); 2644 2645 return (jblocks); 2646 } 2647 2648 static ufs2_daddr_t 2649 jblocks_alloc(jblocks, bytes, actual) 2650 struct jblocks *jblocks; 2651 int bytes; 2652 int *actual; 2653 { 2654 ufs2_daddr_t daddr; 2655 struct jextent *jext; 2656 int freecnt; 2657 int blocks; 2658 2659 blocks = bytes / DEV_BSIZE; 2660 jext = &jblocks->jb_extent[jblocks->jb_head]; 2661 freecnt = jext->je_blocks - jblocks->jb_off; 2662 if (freecnt == 0) { 2663 jblocks->jb_off = 0; 2664 if (++jblocks->jb_head > jblocks->jb_used) 2665 jblocks->jb_head = 0; 2666 jext = &jblocks->jb_extent[jblocks->jb_head]; 2667 freecnt = jext->je_blocks; 2668 } 2669 if (freecnt > blocks) 2670 freecnt = blocks; 2671 *actual = freecnt * DEV_BSIZE; 2672 daddr = jext->je_daddr + jblocks->jb_off; 2673 jblocks->jb_off += freecnt; 2674 jblocks->jb_free -= freecnt; 2675 2676 return (daddr); 2677 } 2678 2679 static void 2680 jblocks_free(jblocks, mp, bytes) 2681 struct jblocks *jblocks; 2682 struct mount *mp; 2683 int bytes; 2684 { 2685 2686 LOCK_OWNED(VFSTOUFS(mp)); 2687 jblocks->jb_free += bytes / DEV_BSIZE; 2688 if (jblocks->jb_suspended) 2689 worklist_speedup(mp); 2690 wakeup(jblocks); 2691 } 2692 2693 static void 2694 jblocks_destroy(jblocks) 2695 struct jblocks *jblocks; 2696 { 2697 2698 if (jblocks->jb_extent) 2699 free(jblocks->jb_extent, M_JBLOCKS); 2700 free(jblocks, M_JBLOCKS); 2701 } 2702 2703 static void 2704 jblocks_add(jblocks, daddr, blocks) 2705 struct jblocks *jblocks; 2706 ufs2_daddr_t daddr; 2707 int blocks; 2708 { 2709 struct jextent *jext; 2710 2711 jblocks->jb_blocks += blocks; 2712 jblocks->jb_free += blocks; 2713 jext = &jblocks->jb_extent[jblocks->jb_used]; 2714 /* Adding the first block. */ 2715 if (jext->je_daddr == 0) { 2716 jext->je_daddr = daddr; 2717 jext->je_blocks = blocks; 2718 return; 2719 } 2720 /* Extending the last extent. */ 2721 if (jext->je_daddr + jext->je_blocks == daddr) { 2722 jext->je_blocks += blocks; 2723 return; 2724 } 2725 /* Adding a new extent. */ 2726 if (++jblocks->jb_used == jblocks->jb_avail) { 2727 jblocks->jb_avail *= 2; 2728 jext = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2729 M_JBLOCKS, M_WAITOK | M_ZERO); 2730 memcpy(jext, jblocks->jb_extent, 2731 sizeof(struct jextent) * jblocks->jb_used); 2732 free(jblocks->jb_extent, M_JBLOCKS); 2733 jblocks->jb_extent = jext; 2734 } 2735 jext = &jblocks->jb_extent[jblocks->jb_used]; 2736 jext->je_daddr = daddr; 2737 jext->je_blocks = blocks; 2738 return; 2739 } 2740 2741 int 2742 softdep_journal_lookup(mp, vpp) 2743 struct mount *mp; 2744 struct vnode **vpp; 2745 { 2746 struct componentname cnp; 2747 struct vnode *dvp; 2748 ino_t sujournal; 2749 int error; 2750 2751 error = VFS_VGET(mp, UFS_ROOTINO, LK_EXCLUSIVE, &dvp); 2752 if (error) 2753 return (error); 2754 bzero(&cnp, sizeof(cnp)); 2755 cnp.cn_nameiop = LOOKUP; 2756 cnp.cn_flags = ISLASTCN; 2757 cnp.cn_thread = curthread; 2758 cnp.cn_cred = curthread->td_ucred; 2759 cnp.cn_pnbuf = SUJ_FILE; 2760 cnp.cn_nameptr = SUJ_FILE; 2761 cnp.cn_namelen = strlen(SUJ_FILE); 2762 error = ufs_lookup_ino(dvp, NULL, &cnp, &sujournal); 2763 vput(dvp); 2764 if (error != 0) 2765 return (error); 2766 error = VFS_VGET(mp, sujournal, LK_EXCLUSIVE, vpp); 2767 return (error); 2768 } 2769 2770 /* 2771 * Open and verify the journal file. 2772 */ 2773 static int 2774 journal_mount(mp, fs, cred) 2775 struct mount *mp; 2776 struct fs *fs; 2777 struct ucred *cred; 2778 { 2779 struct jblocks *jblocks; 2780 struct ufsmount *ump; 2781 struct vnode *vp; 2782 struct inode *ip; 2783 ufs2_daddr_t blkno; 2784 int bcount; 2785 int error; 2786 int i; 2787 2788 ump = VFSTOUFS(mp); 2789 ump->softdep_journal_tail = NULL; 2790 ump->softdep_on_journal = 0; 2791 ump->softdep_accdeps = 0; 2792 ump->softdep_req = 0; 2793 ump->softdep_jblocks = NULL; 2794 error = softdep_journal_lookup(mp, &vp); 2795 if (error != 0) { 2796 printf("Failed to find journal. Use tunefs to create one\n"); 2797 return (error); 2798 } 2799 ip = VTOI(vp); 2800 if (ip->i_size < SUJ_MIN) { 2801 error = ENOSPC; 2802 goto out; 2803 } 2804 bcount = lblkno(fs, ip->i_size); /* Only use whole blocks. */ 2805 jblocks = jblocks_create(); 2806 for (i = 0; i < bcount; i++) { 2807 error = ufs_bmaparray(vp, i, &blkno, NULL, NULL, NULL); 2808 if (error) 2809 break; 2810 jblocks_add(jblocks, blkno, fsbtodb(fs, fs->fs_frag)); 2811 } 2812 if (error) { 2813 jblocks_destroy(jblocks); 2814 goto out; 2815 } 2816 jblocks->jb_low = jblocks->jb_free / 3; /* Reserve 33%. */ 2817 jblocks->jb_min = jblocks->jb_free / 10; /* Suspend at 10%. */ 2818 ump->softdep_jblocks = jblocks; 2819 out: 2820 if (error == 0) { 2821 MNT_ILOCK(mp); 2822 mp->mnt_flag |= MNT_SUJ; 2823 mp->mnt_flag &= ~MNT_SOFTDEP; 2824 MNT_IUNLOCK(mp); 2825 /* 2826 * Only validate the journal contents if the 2827 * filesystem is clean, otherwise we write the logs 2828 * but they'll never be used. If the filesystem was 2829 * still dirty when we mounted it the journal is 2830 * invalid and a new journal can only be valid if it 2831 * starts from a clean mount. 2832 */ 2833 if (fs->fs_clean) { 2834 DIP_SET(ip, i_modrev, fs->fs_mtime); 2835 ip->i_flags |= IN_MODIFIED; 2836 ffs_update(vp, 1); 2837 } 2838 } 2839 vput(vp); 2840 return (error); 2841 } 2842 2843 static void 2844 journal_unmount(ump) 2845 struct ufsmount *ump; 2846 { 2847 2848 if (ump->softdep_jblocks) 2849 jblocks_destroy(ump->softdep_jblocks); 2850 ump->softdep_jblocks = NULL; 2851 } 2852 2853 /* 2854 * Called when a journal record is ready to be written. Space is allocated 2855 * and the journal entry is created when the journal is flushed to stable 2856 * store. 2857 */ 2858 static void 2859 add_to_journal(wk) 2860 struct worklist *wk; 2861 { 2862 struct ufsmount *ump; 2863 2864 ump = VFSTOUFS(wk->wk_mp); 2865 LOCK_OWNED(ump); 2866 if (wk->wk_state & ONWORKLIST) 2867 panic("add_to_journal: %s(0x%X) already on list", 2868 TYPENAME(wk->wk_type), wk->wk_state); 2869 wk->wk_state |= ONWORKLIST | DEPCOMPLETE; 2870 if (LIST_EMPTY(&ump->softdep_journal_pending)) { 2871 ump->softdep_jblocks->jb_age = ticks; 2872 LIST_INSERT_HEAD(&ump->softdep_journal_pending, wk, wk_list); 2873 } else 2874 LIST_INSERT_AFTER(ump->softdep_journal_tail, wk, wk_list); 2875 ump->softdep_journal_tail = wk; 2876 ump->softdep_on_journal += 1; 2877 } 2878 2879 /* 2880 * Remove an arbitrary item for the journal worklist maintain the tail 2881 * pointer. This happens when a new operation obviates the need to 2882 * journal an old operation. 2883 */ 2884 static void 2885 remove_from_journal(wk) 2886 struct worklist *wk; 2887 { 2888 struct ufsmount *ump; 2889 2890 ump = VFSTOUFS(wk->wk_mp); 2891 LOCK_OWNED(ump); 2892 #ifdef INVARIANTS 2893 { 2894 struct worklist *wkn; 2895 2896 LIST_FOREACH(wkn, &ump->softdep_journal_pending, wk_list) 2897 if (wkn == wk) 2898 break; 2899 if (wkn == NULL) 2900 panic("remove_from_journal: %p is not in journal", wk); 2901 } 2902 #endif 2903 /* 2904 * We emulate a TAILQ to save space in most structures which do not 2905 * require TAILQ semantics. Here we must update the tail position 2906 * when removing the tail which is not the final entry. This works 2907 * only if the worklist linkage are at the beginning of the structure. 2908 */ 2909 if (ump->softdep_journal_tail == wk) 2910 ump->softdep_journal_tail = 2911 (struct worklist *)wk->wk_list.le_prev; 2912 WORKLIST_REMOVE(wk); 2913 ump->softdep_on_journal -= 1; 2914 } 2915 2916 /* 2917 * Check for journal space as well as dependency limits so the prelink 2918 * code can throttle both journaled and non-journaled filesystems. 2919 * Threshold is 0 for low and 1 for min. 2920 */ 2921 static int 2922 journal_space(ump, thresh) 2923 struct ufsmount *ump; 2924 int thresh; 2925 { 2926 struct jblocks *jblocks; 2927 int limit, avail; 2928 2929 jblocks = ump->softdep_jblocks; 2930 if (jblocks == NULL) 2931 return (1); 2932 /* 2933 * We use a tighter restriction here to prevent request_cleanup() 2934 * running in threads from running into locks we currently hold. 2935 * We have to be over the limit and our filesystem has to be 2936 * responsible for more than our share of that usage. 2937 */ 2938 limit = (max_softdeps / 10) * 9; 2939 if (dep_current[D_INODEDEP] > limit && 2940 ump->softdep_curdeps[D_INODEDEP] > limit / stat_flush_threads) 2941 return (0); 2942 if (thresh) 2943 thresh = jblocks->jb_min; 2944 else 2945 thresh = jblocks->jb_low; 2946 avail = (ump->softdep_on_journal * JREC_SIZE) / DEV_BSIZE; 2947 avail = jblocks->jb_free - avail; 2948 2949 return (avail > thresh); 2950 } 2951 2952 static void 2953 journal_suspend(ump) 2954 struct ufsmount *ump; 2955 { 2956 struct jblocks *jblocks; 2957 struct mount *mp; 2958 bool set; 2959 2960 mp = UFSTOVFS(ump); 2961 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) 2962 return; 2963 2964 jblocks = ump->softdep_jblocks; 2965 vfs_op_enter(mp); 2966 set = false; 2967 MNT_ILOCK(mp); 2968 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) { 2969 stat_journal_min++; 2970 mp->mnt_kern_flag |= MNTK_SUSPEND; 2971 mp->mnt_susp_owner = ump->softdep_flushtd; 2972 set = true; 2973 } 2974 jblocks->jb_suspended = 1; 2975 MNT_IUNLOCK(mp); 2976 if (!set) 2977 vfs_op_exit(mp); 2978 } 2979 2980 static int 2981 journal_unsuspend(struct ufsmount *ump) 2982 { 2983 struct jblocks *jblocks; 2984 struct mount *mp; 2985 2986 mp = UFSTOVFS(ump); 2987 jblocks = ump->softdep_jblocks; 2988 2989 if (jblocks != NULL && jblocks->jb_suspended && 2990 journal_space(ump, jblocks->jb_min)) { 2991 jblocks->jb_suspended = 0; 2992 FREE_LOCK(ump); 2993 mp->mnt_susp_owner = curthread; 2994 vfs_write_resume(mp, 0); 2995 ACQUIRE_LOCK(ump); 2996 return (1); 2997 } 2998 return (0); 2999 } 3000 3001 /* 3002 * Called before any allocation function to be certain that there is 3003 * sufficient space in the journal prior to creating any new records. 3004 * Since in the case of block allocation we may have multiple locked 3005 * buffers at the time of the actual allocation we can not block 3006 * when the journal records are created. Doing so would create a deadlock 3007 * if any of these buffers needed to be flushed to reclaim space. Instead 3008 * we require a sufficiently large amount of available space such that 3009 * each thread in the system could have passed this allocation check and 3010 * still have sufficient free space. With 20% of a minimum journal size 3011 * of 1MB we have 6553 records available. 3012 */ 3013 int 3014 softdep_prealloc(vp, waitok) 3015 struct vnode *vp; 3016 int waitok; 3017 { 3018 struct ufsmount *ump; 3019 3020 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 3021 ("softdep_prealloc called on non-softdep filesystem")); 3022 /* 3023 * Nothing to do if we are not running journaled soft updates. 3024 * If we currently hold the snapshot lock, we must avoid 3025 * handling other resources that could cause deadlock. Do not 3026 * touch quotas vnode since it is typically recursed with 3027 * other vnode locks held. 3028 */ 3029 if (DOINGSUJ(vp) == 0 || IS_SNAPSHOT(VTOI(vp)) || 3030 (vp->v_vflag & VV_SYSTEM) != 0) 3031 return (0); 3032 ump = VFSTOUFS(vp->v_mount); 3033 ACQUIRE_LOCK(ump); 3034 if (journal_space(ump, 0)) { 3035 FREE_LOCK(ump); 3036 return (0); 3037 } 3038 stat_journal_low++; 3039 FREE_LOCK(ump); 3040 if (waitok == MNT_NOWAIT) 3041 return (ENOSPC); 3042 /* 3043 * Attempt to sync this vnode once to flush any journal 3044 * work attached to it. 3045 */ 3046 if ((curthread->td_pflags & TDP_COWINPROGRESS) == 0) 3047 ffs_syncvnode(vp, waitok, 0); 3048 ACQUIRE_LOCK(ump); 3049 process_removes(vp); 3050 process_truncates(vp); 3051 if (journal_space(ump, 0) == 0) { 3052 softdep_speedup(ump); 3053 if (journal_space(ump, 1) == 0) 3054 journal_suspend(ump); 3055 } 3056 FREE_LOCK(ump); 3057 3058 return (0); 3059 } 3060 3061 /* 3062 * Before adjusting a link count on a vnode verify that we have sufficient 3063 * journal space. If not, process operations that depend on the currently 3064 * locked pair of vnodes to try to flush space as the syncer, buf daemon, 3065 * and softdep flush threads can not acquire these locks to reclaim space. 3066 */ 3067 static void 3068 softdep_prelink(dvp, vp) 3069 struct vnode *dvp; 3070 struct vnode *vp; 3071 { 3072 struct ufsmount *ump; 3073 3074 ump = VFSTOUFS(dvp->v_mount); 3075 LOCK_OWNED(ump); 3076 /* 3077 * Nothing to do if we have sufficient journal space. 3078 * If we currently hold the snapshot lock, we must avoid 3079 * handling other resources that could cause deadlock. 3080 */ 3081 if (journal_space(ump, 0) || (vp && IS_SNAPSHOT(VTOI(vp)))) 3082 return; 3083 stat_journal_low++; 3084 FREE_LOCK(ump); 3085 if (vp) 3086 ffs_syncvnode(vp, MNT_NOWAIT, 0); 3087 ffs_syncvnode(dvp, MNT_WAIT, 0); 3088 ACQUIRE_LOCK(ump); 3089 /* Process vp before dvp as it may create .. removes. */ 3090 if (vp) { 3091 process_removes(vp); 3092 process_truncates(vp); 3093 } 3094 process_removes(dvp); 3095 process_truncates(dvp); 3096 softdep_speedup(ump); 3097 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT); 3098 if (journal_space(ump, 0) == 0) { 3099 softdep_speedup(ump); 3100 if (journal_space(ump, 1) == 0) 3101 journal_suspend(ump); 3102 } 3103 } 3104 3105 static void 3106 jseg_write(ump, jseg, data) 3107 struct ufsmount *ump; 3108 struct jseg *jseg; 3109 uint8_t *data; 3110 { 3111 struct jsegrec *rec; 3112 3113 rec = (struct jsegrec *)data; 3114 rec->jsr_seq = jseg->js_seq; 3115 rec->jsr_oldest = jseg->js_oldseq; 3116 rec->jsr_cnt = jseg->js_cnt; 3117 rec->jsr_blocks = jseg->js_size / ump->um_devvp->v_bufobj.bo_bsize; 3118 rec->jsr_crc = 0; 3119 rec->jsr_time = ump->um_fs->fs_mtime; 3120 } 3121 3122 static inline void 3123 inoref_write(inoref, jseg, rec) 3124 struct inoref *inoref; 3125 struct jseg *jseg; 3126 struct jrefrec *rec; 3127 { 3128 3129 inoref->if_jsegdep->jd_seg = jseg; 3130 rec->jr_ino = inoref->if_ino; 3131 rec->jr_parent = inoref->if_parent; 3132 rec->jr_nlink = inoref->if_nlink; 3133 rec->jr_mode = inoref->if_mode; 3134 rec->jr_diroff = inoref->if_diroff; 3135 } 3136 3137 static void 3138 jaddref_write(jaddref, jseg, data) 3139 struct jaddref *jaddref; 3140 struct jseg *jseg; 3141 uint8_t *data; 3142 { 3143 struct jrefrec *rec; 3144 3145 rec = (struct jrefrec *)data; 3146 rec->jr_op = JOP_ADDREF; 3147 inoref_write(&jaddref->ja_ref, jseg, rec); 3148 } 3149 3150 static void 3151 jremref_write(jremref, jseg, data) 3152 struct jremref *jremref; 3153 struct jseg *jseg; 3154 uint8_t *data; 3155 { 3156 struct jrefrec *rec; 3157 3158 rec = (struct jrefrec *)data; 3159 rec->jr_op = JOP_REMREF; 3160 inoref_write(&jremref->jr_ref, jseg, rec); 3161 } 3162 3163 static void 3164 jmvref_write(jmvref, jseg, data) 3165 struct jmvref *jmvref; 3166 struct jseg *jseg; 3167 uint8_t *data; 3168 { 3169 struct jmvrec *rec; 3170 3171 rec = (struct jmvrec *)data; 3172 rec->jm_op = JOP_MVREF; 3173 rec->jm_ino = jmvref->jm_ino; 3174 rec->jm_parent = jmvref->jm_parent; 3175 rec->jm_oldoff = jmvref->jm_oldoff; 3176 rec->jm_newoff = jmvref->jm_newoff; 3177 } 3178 3179 static void 3180 jnewblk_write(jnewblk, jseg, data) 3181 struct jnewblk *jnewblk; 3182 struct jseg *jseg; 3183 uint8_t *data; 3184 { 3185 struct jblkrec *rec; 3186 3187 jnewblk->jn_jsegdep->jd_seg = jseg; 3188 rec = (struct jblkrec *)data; 3189 rec->jb_op = JOP_NEWBLK; 3190 rec->jb_ino = jnewblk->jn_ino; 3191 rec->jb_blkno = jnewblk->jn_blkno; 3192 rec->jb_lbn = jnewblk->jn_lbn; 3193 rec->jb_frags = jnewblk->jn_frags; 3194 rec->jb_oldfrags = jnewblk->jn_oldfrags; 3195 } 3196 3197 static void 3198 jfreeblk_write(jfreeblk, jseg, data) 3199 struct jfreeblk *jfreeblk; 3200 struct jseg *jseg; 3201 uint8_t *data; 3202 { 3203 struct jblkrec *rec; 3204 3205 jfreeblk->jf_dep.jb_jsegdep->jd_seg = jseg; 3206 rec = (struct jblkrec *)data; 3207 rec->jb_op = JOP_FREEBLK; 3208 rec->jb_ino = jfreeblk->jf_ino; 3209 rec->jb_blkno = jfreeblk->jf_blkno; 3210 rec->jb_lbn = jfreeblk->jf_lbn; 3211 rec->jb_frags = jfreeblk->jf_frags; 3212 rec->jb_oldfrags = 0; 3213 } 3214 3215 static void 3216 jfreefrag_write(jfreefrag, jseg, data) 3217 struct jfreefrag *jfreefrag; 3218 struct jseg *jseg; 3219 uint8_t *data; 3220 { 3221 struct jblkrec *rec; 3222 3223 jfreefrag->fr_jsegdep->jd_seg = jseg; 3224 rec = (struct jblkrec *)data; 3225 rec->jb_op = JOP_FREEBLK; 3226 rec->jb_ino = jfreefrag->fr_ino; 3227 rec->jb_blkno = jfreefrag->fr_blkno; 3228 rec->jb_lbn = jfreefrag->fr_lbn; 3229 rec->jb_frags = jfreefrag->fr_frags; 3230 rec->jb_oldfrags = 0; 3231 } 3232 3233 static void 3234 jtrunc_write(jtrunc, jseg, data) 3235 struct jtrunc *jtrunc; 3236 struct jseg *jseg; 3237 uint8_t *data; 3238 { 3239 struct jtrncrec *rec; 3240 3241 jtrunc->jt_dep.jb_jsegdep->jd_seg = jseg; 3242 rec = (struct jtrncrec *)data; 3243 rec->jt_op = JOP_TRUNC; 3244 rec->jt_ino = jtrunc->jt_ino; 3245 rec->jt_size = jtrunc->jt_size; 3246 rec->jt_extsize = jtrunc->jt_extsize; 3247 } 3248 3249 static void 3250 jfsync_write(jfsync, jseg, data) 3251 struct jfsync *jfsync; 3252 struct jseg *jseg; 3253 uint8_t *data; 3254 { 3255 struct jtrncrec *rec; 3256 3257 rec = (struct jtrncrec *)data; 3258 rec->jt_op = JOP_SYNC; 3259 rec->jt_ino = jfsync->jfs_ino; 3260 rec->jt_size = jfsync->jfs_size; 3261 rec->jt_extsize = jfsync->jfs_extsize; 3262 } 3263 3264 static void 3265 softdep_flushjournal(mp) 3266 struct mount *mp; 3267 { 3268 struct jblocks *jblocks; 3269 struct ufsmount *ump; 3270 3271 if (MOUNTEDSUJ(mp) == 0) 3272 return; 3273 ump = VFSTOUFS(mp); 3274 jblocks = ump->softdep_jblocks; 3275 ACQUIRE_LOCK(ump); 3276 while (ump->softdep_on_journal) { 3277 jblocks->jb_needseg = 1; 3278 softdep_process_journal(mp, NULL, MNT_WAIT); 3279 } 3280 FREE_LOCK(ump); 3281 } 3282 3283 static void softdep_synchronize_completed(struct bio *); 3284 static void softdep_synchronize(struct bio *, struct ufsmount *, void *); 3285 3286 static void 3287 softdep_synchronize_completed(bp) 3288 struct bio *bp; 3289 { 3290 struct jseg *oldest; 3291 struct jseg *jseg; 3292 struct ufsmount *ump; 3293 3294 /* 3295 * caller1 marks the last segment written before we issued the 3296 * synchronize cache. 3297 */ 3298 jseg = bp->bio_caller1; 3299 if (jseg == NULL) { 3300 g_destroy_bio(bp); 3301 return; 3302 } 3303 ump = VFSTOUFS(jseg->js_list.wk_mp); 3304 ACQUIRE_LOCK(ump); 3305 oldest = NULL; 3306 /* 3307 * Mark all the journal entries waiting on the synchronize cache 3308 * as completed so they may continue on. 3309 */ 3310 while (jseg != NULL && (jseg->js_state & COMPLETE) == 0) { 3311 jseg->js_state |= COMPLETE; 3312 oldest = jseg; 3313 jseg = TAILQ_PREV(jseg, jseglst, js_next); 3314 } 3315 /* 3316 * Restart deferred journal entry processing from the oldest 3317 * completed jseg. 3318 */ 3319 if (oldest) 3320 complete_jsegs(oldest); 3321 3322 FREE_LOCK(ump); 3323 g_destroy_bio(bp); 3324 } 3325 3326 /* 3327 * Send BIO_FLUSH/SYNCHRONIZE CACHE to the device to enforce write ordering 3328 * barriers. The journal must be written prior to any blocks that depend 3329 * on it and the journal can not be released until the blocks have be 3330 * written. This code handles both barriers simultaneously. 3331 */ 3332 static void 3333 softdep_synchronize(bp, ump, caller1) 3334 struct bio *bp; 3335 struct ufsmount *ump; 3336 void *caller1; 3337 { 3338 3339 bp->bio_cmd = BIO_FLUSH; 3340 bp->bio_flags |= BIO_ORDERED; 3341 bp->bio_data = NULL; 3342 bp->bio_offset = ump->um_cp->provider->mediasize; 3343 bp->bio_length = 0; 3344 bp->bio_done = softdep_synchronize_completed; 3345 bp->bio_caller1 = caller1; 3346 g_io_request(bp, ump->um_cp); 3347 } 3348 3349 /* 3350 * Flush some journal records to disk. 3351 */ 3352 static void 3353 softdep_process_journal(mp, needwk, flags) 3354 struct mount *mp; 3355 struct worklist *needwk; 3356 int flags; 3357 { 3358 struct jblocks *jblocks; 3359 struct ufsmount *ump; 3360 struct worklist *wk; 3361 struct jseg *jseg; 3362 struct buf *bp; 3363 struct bio *bio; 3364 uint8_t *data; 3365 struct fs *fs; 3366 int shouldflush; 3367 int segwritten; 3368 int jrecmin; /* Minimum records per block. */ 3369 int jrecmax; /* Maximum records per block. */ 3370 int size; 3371 int cnt; 3372 int off; 3373 int devbsize; 3374 3375 if (MOUNTEDSUJ(mp) == 0) 3376 return; 3377 shouldflush = softdep_flushcache; 3378 bio = NULL; 3379 jseg = NULL; 3380 ump = VFSTOUFS(mp); 3381 LOCK_OWNED(ump); 3382 fs = ump->um_fs; 3383 jblocks = ump->softdep_jblocks; 3384 devbsize = ump->um_devvp->v_bufobj.bo_bsize; 3385 /* 3386 * We write anywhere between a disk block and fs block. The upper 3387 * bound is picked to prevent buffer cache fragmentation and limit 3388 * processing time per I/O. 3389 */ 3390 jrecmin = (devbsize / JREC_SIZE) - 1; /* -1 for seg header */ 3391 jrecmax = (fs->fs_bsize / devbsize) * jrecmin; 3392 segwritten = 0; 3393 for (;;) { 3394 cnt = ump->softdep_on_journal; 3395 /* 3396 * Criteria for writing a segment: 3397 * 1) We have a full block. 3398 * 2) We're called from jwait() and haven't found the 3399 * journal item yet. 3400 * 3) Always write if needseg is set. 3401 * 4) If we are called from process_worklist and have 3402 * not yet written anything we write a partial block 3403 * to enforce a 1 second maximum latency on journal 3404 * entries. 3405 */ 3406 if (cnt < (jrecmax - 1) && needwk == NULL && 3407 jblocks->jb_needseg == 0 && (segwritten || cnt == 0)) 3408 break; 3409 cnt++; 3410 /* 3411 * Verify some free journal space. softdep_prealloc() should 3412 * guarantee that we don't run out so this is indicative of 3413 * a problem with the flow control. Try to recover 3414 * gracefully in any event. 3415 */ 3416 while (jblocks->jb_free == 0) { 3417 if (flags != MNT_WAIT) 3418 break; 3419 printf("softdep: Out of journal space!\n"); 3420 softdep_speedup(ump); 3421 msleep(jblocks, LOCK_PTR(ump), PRIBIO, "jblocks", hz); 3422 } 3423 FREE_LOCK(ump); 3424 jseg = malloc(sizeof(*jseg), M_JSEG, M_SOFTDEP_FLAGS); 3425 workitem_alloc(&jseg->js_list, D_JSEG, mp); 3426 LIST_INIT(&jseg->js_entries); 3427 LIST_INIT(&jseg->js_indirs); 3428 jseg->js_state = ATTACHED; 3429 if (shouldflush == 0) 3430 jseg->js_state |= COMPLETE; 3431 else if (bio == NULL) 3432 bio = g_alloc_bio(); 3433 jseg->js_jblocks = jblocks; 3434 bp = geteblk(fs->fs_bsize, 0); 3435 ACQUIRE_LOCK(ump); 3436 /* 3437 * If there was a race while we were allocating the block 3438 * and jseg the entry we care about was likely written. 3439 * We bail out in both the WAIT and NOWAIT case and assume 3440 * the caller will loop if the entry it cares about is 3441 * not written. 3442 */ 3443 cnt = ump->softdep_on_journal; 3444 if (cnt + jblocks->jb_needseg == 0 || jblocks->jb_free == 0) { 3445 bp->b_flags |= B_INVAL | B_NOCACHE; 3446 WORKITEM_FREE(jseg, D_JSEG); 3447 FREE_LOCK(ump); 3448 brelse(bp); 3449 ACQUIRE_LOCK(ump); 3450 break; 3451 } 3452 /* 3453 * Calculate the disk block size required for the available 3454 * records rounded to the min size. 3455 */ 3456 if (cnt == 0) 3457 size = devbsize; 3458 else if (cnt < jrecmax) 3459 size = howmany(cnt, jrecmin) * devbsize; 3460 else 3461 size = fs->fs_bsize; 3462 /* 3463 * Allocate a disk block for this journal data and account 3464 * for truncation of the requested size if enough contiguous 3465 * space was not available. 3466 */ 3467 bp->b_blkno = jblocks_alloc(jblocks, size, &size); 3468 bp->b_lblkno = bp->b_blkno; 3469 bp->b_offset = bp->b_blkno * DEV_BSIZE; 3470 bp->b_bcount = size; 3471 bp->b_flags &= ~B_INVAL; 3472 bp->b_flags |= B_VALIDSUSPWRT | B_NOCOPY; 3473 /* 3474 * Initialize our jseg with cnt records. Assign the next 3475 * sequence number to it and link it in-order. 3476 */ 3477 cnt = MIN(cnt, (size / devbsize) * jrecmin); 3478 jseg->js_buf = bp; 3479 jseg->js_cnt = cnt; 3480 jseg->js_refs = cnt + 1; /* Self ref. */ 3481 jseg->js_size = size; 3482 jseg->js_seq = jblocks->jb_nextseq++; 3483 if (jblocks->jb_oldestseg == NULL) 3484 jblocks->jb_oldestseg = jseg; 3485 jseg->js_oldseq = jblocks->jb_oldestseg->js_seq; 3486 TAILQ_INSERT_TAIL(&jblocks->jb_segs, jseg, js_next); 3487 if (jblocks->jb_writeseg == NULL) 3488 jblocks->jb_writeseg = jseg; 3489 /* 3490 * Start filling in records from the pending list. 3491 */ 3492 data = bp->b_data; 3493 off = 0; 3494 3495 /* 3496 * Always put a header on the first block. 3497 * XXX As with below, there might not be a chance to get 3498 * into the loop. Ensure that something valid is written. 3499 */ 3500 jseg_write(ump, jseg, data); 3501 off += JREC_SIZE; 3502 data = bp->b_data + off; 3503 3504 /* 3505 * XXX Something is wrong here. There's no work to do, 3506 * but we need to perform and I/O and allow it to complete 3507 * anyways. 3508 */ 3509 if (LIST_EMPTY(&ump->softdep_journal_pending)) 3510 stat_emptyjblocks++; 3511 3512 while ((wk = LIST_FIRST(&ump->softdep_journal_pending)) 3513 != NULL) { 3514 if (cnt == 0) 3515 break; 3516 /* Place a segment header on every device block. */ 3517 if ((off % devbsize) == 0) { 3518 jseg_write(ump, jseg, data); 3519 off += JREC_SIZE; 3520 data = bp->b_data + off; 3521 } 3522 if (wk == needwk) 3523 needwk = NULL; 3524 remove_from_journal(wk); 3525 wk->wk_state |= INPROGRESS; 3526 WORKLIST_INSERT(&jseg->js_entries, wk); 3527 switch (wk->wk_type) { 3528 case D_JADDREF: 3529 jaddref_write(WK_JADDREF(wk), jseg, data); 3530 break; 3531 case D_JREMREF: 3532 jremref_write(WK_JREMREF(wk), jseg, data); 3533 break; 3534 case D_JMVREF: 3535 jmvref_write(WK_JMVREF(wk), jseg, data); 3536 break; 3537 case D_JNEWBLK: 3538 jnewblk_write(WK_JNEWBLK(wk), jseg, data); 3539 break; 3540 case D_JFREEBLK: 3541 jfreeblk_write(WK_JFREEBLK(wk), jseg, data); 3542 break; 3543 case D_JFREEFRAG: 3544 jfreefrag_write(WK_JFREEFRAG(wk), jseg, data); 3545 break; 3546 case D_JTRUNC: 3547 jtrunc_write(WK_JTRUNC(wk), jseg, data); 3548 break; 3549 case D_JFSYNC: 3550 jfsync_write(WK_JFSYNC(wk), jseg, data); 3551 break; 3552 default: 3553 panic("process_journal: Unknown type %s", 3554 TYPENAME(wk->wk_type)); 3555 /* NOTREACHED */ 3556 } 3557 off += JREC_SIZE; 3558 data = bp->b_data + off; 3559 cnt--; 3560 } 3561 3562 /* Clear any remaining space so we don't leak kernel data */ 3563 if (size > off) 3564 bzero(data, size - off); 3565 3566 /* 3567 * Write this one buffer and continue. 3568 */ 3569 segwritten = 1; 3570 jblocks->jb_needseg = 0; 3571 WORKLIST_INSERT(&bp->b_dep, &jseg->js_list); 3572 FREE_LOCK(ump); 3573 pbgetvp(ump->um_devvp, bp); 3574 /* 3575 * We only do the blocking wait once we find the journal 3576 * entry we're looking for. 3577 */ 3578 if (needwk == NULL && flags == MNT_WAIT) 3579 bwrite(bp); 3580 else 3581 bawrite(bp); 3582 ACQUIRE_LOCK(ump); 3583 } 3584 /* 3585 * If we wrote a segment issue a synchronize cache so the journal 3586 * is reflected on disk before the data is written. Since reclaiming 3587 * journal space also requires writing a journal record this 3588 * process also enforces a barrier before reclamation. 3589 */ 3590 if (segwritten && shouldflush) { 3591 softdep_synchronize(bio, ump, 3592 TAILQ_LAST(&jblocks->jb_segs, jseglst)); 3593 } else if (bio) 3594 g_destroy_bio(bio); 3595 /* 3596 * If we've suspended the filesystem because we ran out of journal 3597 * space either try to sync it here to make some progress or 3598 * unsuspend it if we already have. 3599 */ 3600 if (flags == 0 && jblocks->jb_suspended) { 3601 if (journal_unsuspend(ump)) 3602 return; 3603 FREE_LOCK(ump); 3604 VFS_SYNC(mp, MNT_NOWAIT); 3605 ffs_sbupdate(ump, MNT_WAIT, 0); 3606 ACQUIRE_LOCK(ump); 3607 } 3608 } 3609 3610 /* 3611 * Complete a jseg, allowing all dependencies awaiting journal writes 3612 * to proceed. Each journal dependency also attaches a jsegdep to dependent 3613 * structures so that the journal segment can be freed to reclaim space. 3614 */ 3615 static void 3616 complete_jseg(jseg) 3617 struct jseg *jseg; 3618 { 3619 struct worklist *wk; 3620 struct jmvref *jmvref; 3621 #ifdef INVARIANTS 3622 int i = 0; 3623 #endif 3624 3625 while ((wk = LIST_FIRST(&jseg->js_entries)) != NULL) { 3626 WORKLIST_REMOVE(wk); 3627 wk->wk_state &= ~INPROGRESS; 3628 wk->wk_state |= COMPLETE; 3629 KASSERT(i++ < jseg->js_cnt, 3630 ("handle_written_jseg: overflow %d >= %d", 3631 i - 1, jseg->js_cnt)); 3632 switch (wk->wk_type) { 3633 case D_JADDREF: 3634 handle_written_jaddref(WK_JADDREF(wk)); 3635 break; 3636 case D_JREMREF: 3637 handle_written_jremref(WK_JREMREF(wk)); 3638 break; 3639 case D_JMVREF: 3640 rele_jseg(jseg); /* No jsegdep. */ 3641 jmvref = WK_JMVREF(wk); 3642 LIST_REMOVE(jmvref, jm_deps); 3643 if ((jmvref->jm_pagedep->pd_state & ONWORKLIST) == 0) 3644 free_pagedep(jmvref->jm_pagedep); 3645 WORKITEM_FREE(jmvref, D_JMVREF); 3646 break; 3647 case D_JNEWBLK: 3648 handle_written_jnewblk(WK_JNEWBLK(wk)); 3649 break; 3650 case D_JFREEBLK: 3651 handle_written_jblkdep(&WK_JFREEBLK(wk)->jf_dep); 3652 break; 3653 case D_JTRUNC: 3654 handle_written_jblkdep(&WK_JTRUNC(wk)->jt_dep); 3655 break; 3656 case D_JFSYNC: 3657 rele_jseg(jseg); /* No jsegdep. */ 3658 WORKITEM_FREE(wk, D_JFSYNC); 3659 break; 3660 case D_JFREEFRAG: 3661 handle_written_jfreefrag(WK_JFREEFRAG(wk)); 3662 break; 3663 default: 3664 panic("handle_written_jseg: Unknown type %s", 3665 TYPENAME(wk->wk_type)); 3666 /* NOTREACHED */ 3667 } 3668 } 3669 /* Release the self reference so the structure may be freed. */ 3670 rele_jseg(jseg); 3671 } 3672 3673 /* 3674 * Determine which jsegs are ready for completion processing. Waits for 3675 * synchronize cache to complete as well as forcing in-order completion 3676 * of journal entries. 3677 */ 3678 static void 3679 complete_jsegs(jseg) 3680 struct jseg *jseg; 3681 { 3682 struct jblocks *jblocks; 3683 struct jseg *jsegn; 3684 3685 jblocks = jseg->js_jblocks; 3686 /* 3687 * Don't allow out of order completions. If this isn't the first 3688 * block wait for it to write before we're done. 3689 */ 3690 if (jseg != jblocks->jb_writeseg) 3691 return; 3692 /* Iterate through available jsegs processing their entries. */ 3693 while (jseg && (jseg->js_state & ALLCOMPLETE) == ALLCOMPLETE) { 3694 jblocks->jb_oldestwrseq = jseg->js_oldseq; 3695 jsegn = TAILQ_NEXT(jseg, js_next); 3696 complete_jseg(jseg); 3697 jseg = jsegn; 3698 } 3699 jblocks->jb_writeseg = jseg; 3700 /* 3701 * Attempt to free jsegs now that oldestwrseq may have advanced. 3702 */ 3703 free_jsegs(jblocks); 3704 } 3705 3706 /* 3707 * Mark a jseg as DEPCOMPLETE and throw away the buffer. Attempt to handle 3708 * the final completions. 3709 */ 3710 static void 3711 handle_written_jseg(jseg, bp) 3712 struct jseg *jseg; 3713 struct buf *bp; 3714 { 3715 3716 if (jseg->js_refs == 0) 3717 panic("handle_written_jseg: No self-reference on %p", jseg); 3718 jseg->js_state |= DEPCOMPLETE; 3719 /* 3720 * We'll never need this buffer again, set flags so it will be 3721 * discarded. 3722 */ 3723 bp->b_flags |= B_INVAL | B_NOCACHE; 3724 pbrelvp(bp); 3725 complete_jsegs(jseg); 3726 } 3727 3728 static inline struct jsegdep * 3729 inoref_jseg(inoref) 3730 struct inoref *inoref; 3731 { 3732 struct jsegdep *jsegdep; 3733 3734 jsegdep = inoref->if_jsegdep; 3735 inoref->if_jsegdep = NULL; 3736 3737 return (jsegdep); 3738 } 3739 3740 /* 3741 * Called once a jremref has made it to stable store. The jremref is marked 3742 * complete and we attempt to free it. Any pagedeps writes sleeping waiting 3743 * for the jremref to complete will be awoken by free_jremref. 3744 */ 3745 static void 3746 handle_written_jremref(jremref) 3747 struct jremref *jremref; 3748 { 3749 struct inodedep *inodedep; 3750 struct jsegdep *jsegdep; 3751 struct dirrem *dirrem; 3752 3753 /* Grab the jsegdep. */ 3754 jsegdep = inoref_jseg(&jremref->jr_ref); 3755 /* 3756 * Remove us from the inoref list. 3757 */ 3758 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 3759 0, &inodedep) == 0) 3760 panic("handle_written_jremref: Lost inodedep"); 3761 TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 3762 /* 3763 * Complete the dirrem. 3764 */ 3765 dirrem = jremref->jr_dirrem; 3766 jremref->jr_dirrem = NULL; 3767 LIST_REMOVE(jremref, jr_deps); 3768 jsegdep->jd_state |= jremref->jr_state & MKDIR_PARENT; 3769 jwork_insert(&dirrem->dm_jwork, jsegdep); 3770 if (LIST_EMPTY(&dirrem->dm_jremrefhd) && 3771 (dirrem->dm_state & COMPLETE) != 0) 3772 add_to_worklist(&dirrem->dm_list, 0); 3773 free_jremref(jremref); 3774 } 3775 3776 /* 3777 * Called once a jaddref has made it to stable store. The dependency is 3778 * marked complete and any dependent structures are added to the inode 3779 * bufwait list to be completed as soon as it is written. If a bitmap write 3780 * depends on this entry we move the inode into the inodedephd of the 3781 * bmsafemap dependency and attempt to remove the jaddref from the bmsafemap. 3782 */ 3783 static void 3784 handle_written_jaddref(jaddref) 3785 struct jaddref *jaddref; 3786 { 3787 struct jsegdep *jsegdep; 3788 struct inodedep *inodedep; 3789 struct diradd *diradd; 3790 struct mkdir *mkdir; 3791 3792 /* Grab the jsegdep. */ 3793 jsegdep = inoref_jseg(&jaddref->ja_ref); 3794 mkdir = NULL; 3795 diradd = NULL; 3796 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 3797 0, &inodedep) == 0) 3798 panic("handle_written_jaddref: Lost inodedep."); 3799 if (jaddref->ja_diradd == NULL) 3800 panic("handle_written_jaddref: No dependency"); 3801 if (jaddref->ja_diradd->da_list.wk_type == D_DIRADD) { 3802 diradd = jaddref->ja_diradd; 3803 WORKLIST_INSERT(&inodedep->id_bufwait, &diradd->da_list); 3804 } else if (jaddref->ja_state & MKDIR_PARENT) { 3805 mkdir = jaddref->ja_mkdir; 3806 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir->md_list); 3807 } else if (jaddref->ja_state & MKDIR_BODY) 3808 mkdir = jaddref->ja_mkdir; 3809 else 3810 panic("handle_written_jaddref: Unknown dependency %p", 3811 jaddref->ja_diradd); 3812 jaddref->ja_diradd = NULL; /* also clears ja_mkdir */ 3813 /* 3814 * Remove us from the inode list. 3815 */ 3816 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps); 3817 /* 3818 * The mkdir may be waiting on the jaddref to clear before freeing. 3819 */ 3820 if (mkdir) { 3821 KASSERT(mkdir->md_list.wk_type == D_MKDIR, 3822 ("handle_written_jaddref: Incorrect type for mkdir %s", 3823 TYPENAME(mkdir->md_list.wk_type))); 3824 mkdir->md_jaddref = NULL; 3825 diradd = mkdir->md_diradd; 3826 mkdir->md_state |= DEPCOMPLETE; 3827 complete_mkdir(mkdir); 3828 } 3829 jwork_insert(&diradd->da_jwork, jsegdep); 3830 if (jaddref->ja_state & NEWBLOCK) { 3831 inodedep->id_state |= ONDEPLIST; 3832 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_inodedephd, 3833 inodedep, id_deps); 3834 } 3835 free_jaddref(jaddref); 3836 } 3837 3838 /* 3839 * Called once a jnewblk journal is written. The allocdirect or allocindir 3840 * is placed in the bmsafemap to await notification of a written bitmap. If 3841 * the operation was canceled we add the segdep to the appropriate 3842 * dependency to free the journal space once the canceling operation 3843 * completes. 3844 */ 3845 static void 3846 handle_written_jnewblk(jnewblk) 3847 struct jnewblk *jnewblk; 3848 { 3849 struct bmsafemap *bmsafemap; 3850 struct freefrag *freefrag; 3851 struct freework *freework; 3852 struct jsegdep *jsegdep; 3853 struct newblk *newblk; 3854 3855 /* Grab the jsegdep. */ 3856 jsegdep = jnewblk->jn_jsegdep; 3857 jnewblk->jn_jsegdep = NULL; 3858 if (jnewblk->jn_dep == NULL) 3859 panic("handle_written_jnewblk: No dependency for the segdep."); 3860 switch (jnewblk->jn_dep->wk_type) { 3861 case D_NEWBLK: 3862 case D_ALLOCDIRECT: 3863 case D_ALLOCINDIR: 3864 /* 3865 * Add the written block to the bmsafemap so it can 3866 * be notified when the bitmap is on disk. 3867 */ 3868 newblk = WK_NEWBLK(jnewblk->jn_dep); 3869 newblk->nb_jnewblk = NULL; 3870 if ((newblk->nb_state & GOINGAWAY) == 0) { 3871 bmsafemap = newblk->nb_bmsafemap; 3872 newblk->nb_state |= ONDEPLIST; 3873 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, 3874 nb_deps); 3875 } 3876 jwork_insert(&newblk->nb_jwork, jsegdep); 3877 break; 3878 case D_FREEFRAG: 3879 /* 3880 * A newblock being removed by a freefrag when replaced by 3881 * frag extension. 3882 */ 3883 freefrag = WK_FREEFRAG(jnewblk->jn_dep); 3884 freefrag->ff_jdep = NULL; 3885 jwork_insert(&freefrag->ff_jwork, jsegdep); 3886 break; 3887 case D_FREEWORK: 3888 /* 3889 * A direct block was removed by truncate. 3890 */ 3891 freework = WK_FREEWORK(jnewblk->jn_dep); 3892 freework->fw_jnewblk = NULL; 3893 jwork_insert(&freework->fw_freeblks->fb_jwork, jsegdep); 3894 break; 3895 default: 3896 panic("handle_written_jnewblk: Unknown type %d.", 3897 jnewblk->jn_dep->wk_type); 3898 } 3899 jnewblk->jn_dep = NULL; 3900 free_jnewblk(jnewblk); 3901 } 3902 3903 /* 3904 * Cancel a jfreefrag that won't be needed, probably due to colliding with 3905 * an in-flight allocation that has not yet been committed. Divorce us 3906 * from the freefrag and mark it DEPCOMPLETE so that it may be added 3907 * to the worklist. 3908 */ 3909 static void 3910 cancel_jfreefrag(jfreefrag) 3911 struct jfreefrag *jfreefrag; 3912 { 3913 struct freefrag *freefrag; 3914 3915 if (jfreefrag->fr_jsegdep) { 3916 free_jsegdep(jfreefrag->fr_jsegdep); 3917 jfreefrag->fr_jsegdep = NULL; 3918 } 3919 freefrag = jfreefrag->fr_freefrag; 3920 jfreefrag->fr_freefrag = NULL; 3921 free_jfreefrag(jfreefrag); 3922 freefrag->ff_state |= DEPCOMPLETE; 3923 CTR1(KTR_SUJ, "cancel_jfreefrag: blkno %jd", freefrag->ff_blkno); 3924 } 3925 3926 /* 3927 * Free a jfreefrag when the parent freefrag is rendered obsolete. 3928 */ 3929 static void 3930 free_jfreefrag(jfreefrag) 3931 struct jfreefrag *jfreefrag; 3932 { 3933 3934 if (jfreefrag->fr_state & INPROGRESS) 3935 WORKLIST_REMOVE(&jfreefrag->fr_list); 3936 else if (jfreefrag->fr_state & ONWORKLIST) 3937 remove_from_journal(&jfreefrag->fr_list); 3938 if (jfreefrag->fr_freefrag != NULL) 3939 panic("free_jfreefrag: Still attached to a freefrag."); 3940 WORKITEM_FREE(jfreefrag, D_JFREEFRAG); 3941 } 3942 3943 /* 3944 * Called when the journal write for a jfreefrag completes. The parent 3945 * freefrag is added to the worklist if this completes its dependencies. 3946 */ 3947 static void 3948 handle_written_jfreefrag(jfreefrag) 3949 struct jfreefrag *jfreefrag; 3950 { 3951 struct jsegdep *jsegdep; 3952 struct freefrag *freefrag; 3953 3954 /* Grab the jsegdep. */ 3955 jsegdep = jfreefrag->fr_jsegdep; 3956 jfreefrag->fr_jsegdep = NULL; 3957 freefrag = jfreefrag->fr_freefrag; 3958 if (freefrag == NULL) 3959 panic("handle_written_jfreefrag: No freefrag."); 3960 freefrag->ff_state |= DEPCOMPLETE; 3961 freefrag->ff_jdep = NULL; 3962 jwork_insert(&freefrag->ff_jwork, jsegdep); 3963 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 3964 add_to_worklist(&freefrag->ff_list, 0); 3965 jfreefrag->fr_freefrag = NULL; 3966 free_jfreefrag(jfreefrag); 3967 } 3968 3969 /* 3970 * Called when the journal write for a jfreeblk completes. The jfreeblk 3971 * is removed from the freeblks list of pending journal writes and the 3972 * jsegdep is moved to the freeblks jwork to be completed when all blocks 3973 * have been reclaimed. 3974 */ 3975 static void 3976 handle_written_jblkdep(jblkdep) 3977 struct jblkdep *jblkdep; 3978 { 3979 struct freeblks *freeblks; 3980 struct jsegdep *jsegdep; 3981 3982 /* Grab the jsegdep. */ 3983 jsegdep = jblkdep->jb_jsegdep; 3984 jblkdep->jb_jsegdep = NULL; 3985 freeblks = jblkdep->jb_freeblks; 3986 LIST_REMOVE(jblkdep, jb_deps); 3987 jwork_insert(&freeblks->fb_jwork, jsegdep); 3988 /* 3989 * If the freeblks is all journaled, we can add it to the worklist. 3990 */ 3991 if (LIST_EMPTY(&freeblks->fb_jblkdephd) && 3992 (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 3993 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 3994 3995 free_jblkdep(jblkdep); 3996 } 3997 3998 static struct jsegdep * 3999 newjsegdep(struct worklist *wk) 4000 { 4001 struct jsegdep *jsegdep; 4002 4003 jsegdep = malloc(sizeof(*jsegdep), M_JSEGDEP, M_SOFTDEP_FLAGS); 4004 workitem_alloc(&jsegdep->jd_list, D_JSEGDEP, wk->wk_mp); 4005 jsegdep->jd_seg = NULL; 4006 4007 return (jsegdep); 4008 } 4009 4010 static struct jmvref * 4011 newjmvref(dp, ino, oldoff, newoff) 4012 struct inode *dp; 4013 ino_t ino; 4014 off_t oldoff; 4015 off_t newoff; 4016 { 4017 struct jmvref *jmvref; 4018 4019 jmvref = malloc(sizeof(*jmvref), M_JMVREF, M_SOFTDEP_FLAGS); 4020 workitem_alloc(&jmvref->jm_list, D_JMVREF, ITOVFS(dp)); 4021 jmvref->jm_list.wk_state = ATTACHED | DEPCOMPLETE; 4022 jmvref->jm_parent = dp->i_number; 4023 jmvref->jm_ino = ino; 4024 jmvref->jm_oldoff = oldoff; 4025 jmvref->jm_newoff = newoff; 4026 4027 return (jmvref); 4028 } 4029 4030 /* 4031 * Allocate a new jremref that tracks the removal of ip from dp with the 4032 * directory entry offset of diroff. Mark the entry as ATTACHED and 4033 * DEPCOMPLETE as we have all the information required for the journal write 4034 * and the directory has already been removed from the buffer. The caller 4035 * is responsible for linking the jremref into the pagedep and adding it 4036 * to the journal to write. The MKDIR_PARENT flag is set if we're doing 4037 * a DOTDOT addition so handle_workitem_remove() can properly assign 4038 * the jsegdep when we're done. 4039 */ 4040 static struct jremref * 4041 newjremref(struct dirrem *dirrem, struct inode *dp, struct inode *ip, 4042 off_t diroff, nlink_t nlink) 4043 { 4044 struct jremref *jremref; 4045 4046 jremref = malloc(sizeof(*jremref), M_JREMREF, M_SOFTDEP_FLAGS); 4047 workitem_alloc(&jremref->jr_list, D_JREMREF, ITOVFS(dp)); 4048 jremref->jr_state = ATTACHED; 4049 newinoref(&jremref->jr_ref, ip->i_number, dp->i_number, diroff, 4050 nlink, ip->i_mode); 4051 jremref->jr_dirrem = dirrem; 4052 4053 return (jremref); 4054 } 4055 4056 static inline void 4057 newinoref(struct inoref *inoref, ino_t ino, ino_t parent, off_t diroff, 4058 nlink_t nlink, uint16_t mode) 4059 { 4060 4061 inoref->if_jsegdep = newjsegdep(&inoref->if_list); 4062 inoref->if_diroff = diroff; 4063 inoref->if_ino = ino; 4064 inoref->if_parent = parent; 4065 inoref->if_nlink = nlink; 4066 inoref->if_mode = mode; 4067 } 4068 4069 /* 4070 * Allocate a new jaddref to track the addition of ino to dp at diroff. The 4071 * directory offset may not be known until later. The caller is responsible 4072 * adding the entry to the journal when this information is available. nlink 4073 * should be the link count prior to the addition and mode is only required 4074 * to have the correct FMT. 4075 */ 4076 static struct jaddref * 4077 newjaddref(struct inode *dp, ino_t ino, off_t diroff, int16_t nlink, 4078 uint16_t mode) 4079 { 4080 struct jaddref *jaddref; 4081 4082 jaddref = malloc(sizeof(*jaddref), M_JADDREF, M_SOFTDEP_FLAGS); 4083 workitem_alloc(&jaddref->ja_list, D_JADDREF, ITOVFS(dp)); 4084 jaddref->ja_state = ATTACHED; 4085 jaddref->ja_mkdir = NULL; 4086 newinoref(&jaddref->ja_ref, ino, dp->i_number, diroff, nlink, mode); 4087 4088 return (jaddref); 4089 } 4090 4091 /* 4092 * Create a new free dependency for a freework. The caller is responsible 4093 * for adjusting the reference count when it has the lock held. The freedep 4094 * will track an outstanding bitmap write that will ultimately clear the 4095 * freework to continue. 4096 */ 4097 static struct freedep * 4098 newfreedep(struct freework *freework) 4099 { 4100 struct freedep *freedep; 4101 4102 freedep = malloc(sizeof(*freedep), M_FREEDEP, M_SOFTDEP_FLAGS); 4103 workitem_alloc(&freedep->fd_list, D_FREEDEP, freework->fw_list.wk_mp); 4104 freedep->fd_freework = freework; 4105 4106 return (freedep); 4107 } 4108 4109 /* 4110 * Free a freedep structure once the buffer it is linked to is written. If 4111 * this is the last reference to the freework schedule it for completion. 4112 */ 4113 static void 4114 free_freedep(freedep) 4115 struct freedep *freedep; 4116 { 4117 struct freework *freework; 4118 4119 freework = freedep->fd_freework; 4120 freework->fw_freeblks->fb_cgwait--; 4121 if (--freework->fw_ref == 0) 4122 freework_enqueue(freework); 4123 WORKITEM_FREE(freedep, D_FREEDEP); 4124 } 4125 4126 /* 4127 * Allocate a new freework structure that may be a level in an indirect 4128 * when parent is not NULL or a top level block when it is. The top level 4129 * freework structures are allocated without the per-filesystem lock held 4130 * and before the freeblks is visible outside of softdep_setup_freeblocks(). 4131 */ 4132 static struct freework * 4133 newfreework(ump, freeblks, parent, lbn, nb, frags, off, journal) 4134 struct ufsmount *ump; 4135 struct freeblks *freeblks; 4136 struct freework *parent; 4137 ufs_lbn_t lbn; 4138 ufs2_daddr_t nb; 4139 int frags; 4140 int off; 4141 int journal; 4142 { 4143 struct freework *freework; 4144 4145 freework = malloc(sizeof(*freework), M_FREEWORK, M_SOFTDEP_FLAGS); 4146 workitem_alloc(&freework->fw_list, D_FREEWORK, freeblks->fb_list.wk_mp); 4147 freework->fw_state = ATTACHED; 4148 freework->fw_jnewblk = NULL; 4149 freework->fw_freeblks = freeblks; 4150 freework->fw_parent = parent; 4151 freework->fw_lbn = lbn; 4152 freework->fw_blkno = nb; 4153 freework->fw_frags = frags; 4154 freework->fw_indir = NULL; 4155 freework->fw_ref = (MOUNTEDSUJ(UFSTOVFS(ump)) == 0 || 4156 lbn >= -UFS_NXADDR) ? 0 : NINDIR(ump->um_fs) + 1; 4157 freework->fw_start = freework->fw_off = off; 4158 if (journal) 4159 newjfreeblk(freeblks, lbn, nb, frags); 4160 if (parent == NULL) { 4161 ACQUIRE_LOCK(ump); 4162 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 4163 freeblks->fb_ref++; 4164 FREE_LOCK(ump); 4165 } 4166 4167 return (freework); 4168 } 4169 4170 /* 4171 * Eliminate a jfreeblk for a block that does not need journaling. 4172 */ 4173 static void 4174 cancel_jfreeblk(freeblks, blkno) 4175 struct freeblks *freeblks; 4176 ufs2_daddr_t blkno; 4177 { 4178 struct jfreeblk *jfreeblk; 4179 struct jblkdep *jblkdep; 4180 4181 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) { 4182 if (jblkdep->jb_list.wk_type != D_JFREEBLK) 4183 continue; 4184 jfreeblk = WK_JFREEBLK(&jblkdep->jb_list); 4185 if (jfreeblk->jf_blkno == blkno) 4186 break; 4187 } 4188 if (jblkdep == NULL) 4189 return; 4190 CTR1(KTR_SUJ, "cancel_jfreeblk: blkno %jd", blkno); 4191 free_jsegdep(jblkdep->jb_jsegdep); 4192 LIST_REMOVE(jblkdep, jb_deps); 4193 WORKITEM_FREE(jfreeblk, D_JFREEBLK); 4194 } 4195 4196 /* 4197 * Allocate a new jfreeblk to journal top level block pointer when truncating 4198 * a file. The caller must add this to the worklist when the per-filesystem 4199 * lock is held. 4200 */ 4201 static struct jfreeblk * 4202 newjfreeblk(freeblks, lbn, blkno, frags) 4203 struct freeblks *freeblks; 4204 ufs_lbn_t lbn; 4205 ufs2_daddr_t blkno; 4206 int frags; 4207 { 4208 struct jfreeblk *jfreeblk; 4209 4210 jfreeblk = malloc(sizeof(*jfreeblk), M_JFREEBLK, M_SOFTDEP_FLAGS); 4211 workitem_alloc(&jfreeblk->jf_dep.jb_list, D_JFREEBLK, 4212 freeblks->fb_list.wk_mp); 4213 jfreeblk->jf_dep.jb_jsegdep = newjsegdep(&jfreeblk->jf_dep.jb_list); 4214 jfreeblk->jf_dep.jb_freeblks = freeblks; 4215 jfreeblk->jf_ino = freeblks->fb_inum; 4216 jfreeblk->jf_lbn = lbn; 4217 jfreeblk->jf_blkno = blkno; 4218 jfreeblk->jf_frags = frags; 4219 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jfreeblk->jf_dep, jb_deps); 4220 4221 return (jfreeblk); 4222 } 4223 4224 /* 4225 * The journal is only prepared to handle full-size block numbers, so we 4226 * have to adjust the record to reflect the change to a full-size block. 4227 * For example, suppose we have a block made up of fragments 8-15 and 4228 * want to free its last two fragments. We are given a request that says: 4229 * FREEBLK ino=5, blkno=14, lbn=0, frags=2, oldfrags=0 4230 * where frags are the number of fragments to free and oldfrags are the 4231 * number of fragments to keep. To block align it, we have to change it to 4232 * have a valid full-size blkno, so it becomes: 4233 * FREEBLK ino=5, blkno=8, lbn=0, frags=2, oldfrags=6 4234 */ 4235 static void 4236 adjust_newfreework(freeblks, frag_offset) 4237 struct freeblks *freeblks; 4238 int frag_offset; 4239 { 4240 struct jfreeblk *jfreeblk; 4241 4242 KASSERT((LIST_FIRST(&freeblks->fb_jblkdephd) != NULL && 4243 LIST_FIRST(&freeblks->fb_jblkdephd)->jb_list.wk_type == D_JFREEBLK), 4244 ("adjust_newfreework: Missing freeblks dependency")); 4245 4246 jfreeblk = WK_JFREEBLK(LIST_FIRST(&freeblks->fb_jblkdephd)); 4247 jfreeblk->jf_blkno -= frag_offset; 4248 jfreeblk->jf_frags += frag_offset; 4249 } 4250 4251 /* 4252 * Allocate a new jtrunc to track a partial truncation. 4253 */ 4254 static struct jtrunc * 4255 newjtrunc(freeblks, size, extsize) 4256 struct freeblks *freeblks; 4257 off_t size; 4258 int extsize; 4259 { 4260 struct jtrunc *jtrunc; 4261 4262 jtrunc = malloc(sizeof(*jtrunc), M_JTRUNC, M_SOFTDEP_FLAGS); 4263 workitem_alloc(&jtrunc->jt_dep.jb_list, D_JTRUNC, 4264 freeblks->fb_list.wk_mp); 4265 jtrunc->jt_dep.jb_jsegdep = newjsegdep(&jtrunc->jt_dep.jb_list); 4266 jtrunc->jt_dep.jb_freeblks = freeblks; 4267 jtrunc->jt_ino = freeblks->fb_inum; 4268 jtrunc->jt_size = size; 4269 jtrunc->jt_extsize = extsize; 4270 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jtrunc->jt_dep, jb_deps); 4271 4272 return (jtrunc); 4273 } 4274 4275 /* 4276 * If we're canceling a new bitmap we have to search for another ref 4277 * to move into the bmsafemap dep. This might be better expressed 4278 * with another structure. 4279 */ 4280 static void 4281 move_newblock_dep(jaddref, inodedep) 4282 struct jaddref *jaddref; 4283 struct inodedep *inodedep; 4284 { 4285 struct inoref *inoref; 4286 struct jaddref *jaddrefn; 4287 4288 jaddrefn = NULL; 4289 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4290 inoref = TAILQ_NEXT(inoref, if_deps)) { 4291 if ((jaddref->ja_state & NEWBLOCK) && 4292 inoref->if_list.wk_type == D_JADDREF) { 4293 jaddrefn = (struct jaddref *)inoref; 4294 break; 4295 } 4296 } 4297 if (jaddrefn == NULL) 4298 return; 4299 jaddrefn->ja_state &= ~(ATTACHED | UNDONE); 4300 jaddrefn->ja_state |= jaddref->ja_state & 4301 (ATTACHED | UNDONE | NEWBLOCK); 4302 jaddref->ja_state &= ~(ATTACHED | UNDONE | NEWBLOCK); 4303 jaddref->ja_state |= ATTACHED; 4304 LIST_REMOVE(jaddref, ja_bmdeps); 4305 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_jaddrefhd, jaddrefn, 4306 ja_bmdeps); 4307 } 4308 4309 /* 4310 * Cancel a jaddref either before it has been written or while it is being 4311 * written. This happens when a link is removed before the add reaches 4312 * the disk. The jaddref dependency is kept linked into the bmsafemap 4313 * and inode to prevent the link count or bitmap from reaching the disk 4314 * until handle_workitem_remove() re-adjusts the counts and bitmaps as 4315 * required. 4316 * 4317 * Returns 1 if the canceled addref requires journaling of the remove and 4318 * 0 otherwise. 4319 */ 4320 static int 4321 cancel_jaddref(jaddref, inodedep, wkhd) 4322 struct jaddref *jaddref; 4323 struct inodedep *inodedep; 4324 struct workhead *wkhd; 4325 { 4326 struct inoref *inoref; 4327 struct jsegdep *jsegdep; 4328 int needsj; 4329 4330 KASSERT((jaddref->ja_state & COMPLETE) == 0, 4331 ("cancel_jaddref: Canceling complete jaddref")); 4332 if (jaddref->ja_state & (INPROGRESS | COMPLETE)) 4333 needsj = 1; 4334 else 4335 needsj = 0; 4336 if (inodedep == NULL) 4337 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 4338 0, &inodedep) == 0) 4339 panic("cancel_jaddref: Lost inodedep"); 4340 /* 4341 * We must adjust the nlink of any reference operation that follows 4342 * us so that it is consistent with the in-memory reference. This 4343 * ensures that inode nlink rollbacks always have the correct link. 4344 */ 4345 if (needsj == 0) { 4346 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4347 inoref = TAILQ_NEXT(inoref, if_deps)) { 4348 if (inoref->if_state & GOINGAWAY) 4349 break; 4350 inoref->if_nlink--; 4351 } 4352 } 4353 jsegdep = inoref_jseg(&jaddref->ja_ref); 4354 if (jaddref->ja_state & NEWBLOCK) 4355 move_newblock_dep(jaddref, inodedep); 4356 wake_worklist(&jaddref->ja_list); 4357 jaddref->ja_mkdir = NULL; 4358 if (jaddref->ja_state & INPROGRESS) { 4359 jaddref->ja_state &= ~INPROGRESS; 4360 WORKLIST_REMOVE(&jaddref->ja_list); 4361 jwork_insert(wkhd, jsegdep); 4362 } else { 4363 free_jsegdep(jsegdep); 4364 if (jaddref->ja_state & DEPCOMPLETE) 4365 remove_from_journal(&jaddref->ja_list); 4366 } 4367 jaddref->ja_state |= (GOINGAWAY | DEPCOMPLETE); 4368 /* 4369 * Leave NEWBLOCK jaddrefs on the inodedep so handle_workitem_remove 4370 * can arrange for them to be freed with the bitmap. Otherwise we 4371 * no longer need this addref attached to the inoreflst and it 4372 * will incorrectly adjust nlink if we leave it. 4373 */ 4374 if ((jaddref->ja_state & NEWBLOCK) == 0) { 4375 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 4376 if_deps); 4377 jaddref->ja_state |= COMPLETE; 4378 free_jaddref(jaddref); 4379 return (needsj); 4380 } 4381 /* 4382 * Leave the head of the list for jsegdeps for fast merging. 4383 */ 4384 if (LIST_FIRST(wkhd) != NULL) { 4385 jaddref->ja_state |= ONWORKLIST; 4386 LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list); 4387 } else 4388 WORKLIST_INSERT(wkhd, &jaddref->ja_list); 4389 4390 return (needsj); 4391 } 4392 4393 /* 4394 * Attempt to free a jaddref structure when some work completes. This 4395 * should only succeed once the entry is written and all dependencies have 4396 * been notified. 4397 */ 4398 static void 4399 free_jaddref(jaddref) 4400 struct jaddref *jaddref; 4401 { 4402 4403 if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE) 4404 return; 4405 if (jaddref->ja_ref.if_jsegdep) 4406 panic("free_jaddref: segdep attached to jaddref %p(0x%X)\n", 4407 jaddref, jaddref->ja_state); 4408 if (jaddref->ja_state & NEWBLOCK) 4409 LIST_REMOVE(jaddref, ja_bmdeps); 4410 if (jaddref->ja_state & (INPROGRESS | ONWORKLIST)) 4411 panic("free_jaddref: Bad state %p(0x%X)", 4412 jaddref, jaddref->ja_state); 4413 if (jaddref->ja_mkdir != NULL) 4414 panic("free_jaddref: Work pending, 0x%X\n", jaddref->ja_state); 4415 WORKITEM_FREE(jaddref, D_JADDREF); 4416 } 4417 4418 /* 4419 * Free a jremref structure once it has been written or discarded. 4420 */ 4421 static void 4422 free_jremref(jremref) 4423 struct jremref *jremref; 4424 { 4425 4426 if (jremref->jr_ref.if_jsegdep) 4427 free_jsegdep(jremref->jr_ref.if_jsegdep); 4428 if (jremref->jr_state & INPROGRESS) 4429 panic("free_jremref: IO still pending"); 4430 WORKITEM_FREE(jremref, D_JREMREF); 4431 } 4432 4433 /* 4434 * Free a jnewblk structure. 4435 */ 4436 static void 4437 free_jnewblk(jnewblk) 4438 struct jnewblk *jnewblk; 4439 { 4440 4441 if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE) 4442 return; 4443 LIST_REMOVE(jnewblk, jn_deps); 4444 if (jnewblk->jn_dep != NULL) 4445 panic("free_jnewblk: Dependency still attached."); 4446 WORKITEM_FREE(jnewblk, D_JNEWBLK); 4447 } 4448 4449 /* 4450 * Cancel a jnewblk which has been been made redundant by frag extension. 4451 */ 4452 static void 4453 cancel_jnewblk(jnewblk, wkhd) 4454 struct jnewblk *jnewblk; 4455 struct workhead *wkhd; 4456 { 4457 struct jsegdep *jsegdep; 4458 4459 CTR1(KTR_SUJ, "cancel_jnewblk: blkno %jd", jnewblk->jn_blkno); 4460 jsegdep = jnewblk->jn_jsegdep; 4461 if (jnewblk->jn_jsegdep == NULL || jnewblk->jn_dep == NULL) 4462 panic("cancel_jnewblk: Invalid state"); 4463 jnewblk->jn_jsegdep = NULL; 4464 jnewblk->jn_dep = NULL; 4465 jnewblk->jn_state |= GOINGAWAY; 4466 if (jnewblk->jn_state & INPROGRESS) { 4467 jnewblk->jn_state &= ~INPROGRESS; 4468 WORKLIST_REMOVE(&jnewblk->jn_list); 4469 jwork_insert(wkhd, jsegdep); 4470 } else { 4471 free_jsegdep(jsegdep); 4472 remove_from_journal(&jnewblk->jn_list); 4473 } 4474 wake_worklist(&jnewblk->jn_list); 4475 WORKLIST_INSERT(wkhd, &jnewblk->jn_list); 4476 } 4477 4478 static void 4479 free_jblkdep(jblkdep) 4480 struct jblkdep *jblkdep; 4481 { 4482 4483 if (jblkdep->jb_list.wk_type == D_JFREEBLK) 4484 WORKITEM_FREE(jblkdep, D_JFREEBLK); 4485 else if (jblkdep->jb_list.wk_type == D_JTRUNC) 4486 WORKITEM_FREE(jblkdep, D_JTRUNC); 4487 else 4488 panic("free_jblkdep: Unexpected type %s", 4489 TYPENAME(jblkdep->jb_list.wk_type)); 4490 } 4491 4492 /* 4493 * Free a single jseg once it is no longer referenced in memory or on 4494 * disk. Reclaim journal blocks and dependencies waiting for the segment 4495 * to disappear. 4496 */ 4497 static void 4498 free_jseg(jseg, jblocks) 4499 struct jseg *jseg; 4500 struct jblocks *jblocks; 4501 { 4502 struct freework *freework; 4503 4504 /* 4505 * Free freework structures that were lingering to indicate freed 4506 * indirect blocks that forced journal write ordering on reallocate. 4507 */ 4508 while ((freework = LIST_FIRST(&jseg->js_indirs)) != NULL) 4509 indirblk_remove(freework); 4510 if (jblocks->jb_oldestseg == jseg) 4511 jblocks->jb_oldestseg = TAILQ_NEXT(jseg, js_next); 4512 TAILQ_REMOVE(&jblocks->jb_segs, jseg, js_next); 4513 jblocks_free(jblocks, jseg->js_list.wk_mp, jseg->js_size); 4514 KASSERT(LIST_EMPTY(&jseg->js_entries), 4515 ("free_jseg: Freed jseg has valid entries.")); 4516 WORKITEM_FREE(jseg, D_JSEG); 4517 } 4518 4519 /* 4520 * Free all jsegs that meet the criteria for being reclaimed and update 4521 * oldestseg. 4522 */ 4523 static void 4524 free_jsegs(jblocks) 4525 struct jblocks *jblocks; 4526 { 4527 struct jseg *jseg; 4528 4529 /* 4530 * Free only those jsegs which have none allocated before them to 4531 * preserve the journal space ordering. 4532 */ 4533 while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) { 4534 /* 4535 * Only reclaim space when nothing depends on this journal 4536 * set and another set has written that it is no longer 4537 * valid. 4538 */ 4539 if (jseg->js_refs != 0) { 4540 jblocks->jb_oldestseg = jseg; 4541 return; 4542 } 4543 if ((jseg->js_state & ALLCOMPLETE) != ALLCOMPLETE) 4544 break; 4545 if (jseg->js_seq > jblocks->jb_oldestwrseq) 4546 break; 4547 /* 4548 * We can free jsegs that didn't write entries when 4549 * oldestwrseq == js_seq. 4550 */ 4551 if (jseg->js_seq == jblocks->jb_oldestwrseq && 4552 jseg->js_cnt != 0) 4553 break; 4554 free_jseg(jseg, jblocks); 4555 } 4556 /* 4557 * If we exited the loop above we still must discover the 4558 * oldest valid segment. 4559 */ 4560 if (jseg) 4561 for (jseg = jblocks->jb_oldestseg; jseg != NULL; 4562 jseg = TAILQ_NEXT(jseg, js_next)) 4563 if (jseg->js_refs != 0) 4564 break; 4565 jblocks->jb_oldestseg = jseg; 4566 /* 4567 * The journal has no valid records but some jsegs may still be 4568 * waiting on oldestwrseq to advance. We force a small record 4569 * out to permit these lingering records to be reclaimed. 4570 */ 4571 if (jblocks->jb_oldestseg == NULL && !TAILQ_EMPTY(&jblocks->jb_segs)) 4572 jblocks->jb_needseg = 1; 4573 } 4574 4575 /* 4576 * Release one reference to a jseg and free it if the count reaches 0. This 4577 * should eventually reclaim journal space as well. 4578 */ 4579 static void 4580 rele_jseg(jseg) 4581 struct jseg *jseg; 4582 { 4583 4584 KASSERT(jseg->js_refs > 0, 4585 ("free_jseg: Invalid refcnt %d", jseg->js_refs)); 4586 if (--jseg->js_refs != 0) 4587 return; 4588 free_jsegs(jseg->js_jblocks); 4589 } 4590 4591 /* 4592 * Release a jsegdep and decrement the jseg count. 4593 */ 4594 static void 4595 free_jsegdep(jsegdep) 4596 struct jsegdep *jsegdep; 4597 { 4598 4599 if (jsegdep->jd_seg) 4600 rele_jseg(jsegdep->jd_seg); 4601 WORKITEM_FREE(jsegdep, D_JSEGDEP); 4602 } 4603 4604 /* 4605 * Wait for a journal item to make it to disk. Initiate journal processing 4606 * if required. 4607 */ 4608 static int 4609 jwait(wk, waitfor) 4610 struct worklist *wk; 4611 int waitfor; 4612 { 4613 4614 LOCK_OWNED(VFSTOUFS(wk->wk_mp)); 4615 /* 4616 * Blocking journal waits cause slow synchronous behavior. Record 4617 * stats on the frequency of these blocking operations. 4618 */ 4619 if (waitfor == MNT_WAIT) { 4620 stat_journal_wait++; 4621 switch (wk->wk_type) { 4622 case D_JREMREF: 4623 case D_JMVREF: 4624 stat_jwait_filepage++; 4625 break; 4626 case D_JTRUNC: 4627 case D_JFREEBLK: 4628 stat_jwait_freeblks++; 4629 break; 4630 case D_JNEWBLK: 4631 stat_jwait_newblk++; 4632 break; 4633 case D_JADDREF: 4634 stat_jwait_inode++; 4635 break; 4636 default: 4637 break; 4638 } 4639 } 4640 /* 4641 * If IO has not started we process the journal. We can't mark the 4642 * worklist item as IOWAITING because we drop the lock while 4643 * processing the journal and the worklist entry may be freed after 4644 * this point. The caller may call back in and re-issue the request. 4645 */ 4646 if ((wk->wk_state & INPROGRESS) == 0) { 4647 softdep_process_journal(wk->wk_mp, wk, waitfor); 4648 if (waitfor != MNT_WAIT) 4649 return (EBUSY); 4650 return (0); 4651 } 4652 if (waitfor != MNT_WAIT) 4653 return (EBUSY); 4654 wait_worklist(wk, "jwait"); 4655 return (0); 4656 } 4657 4658 /* 4659 * Lookup an inodedep based on an inode pointer and set the nlinkdelta as 4660 * appropriate. This is a convenience function to reduce duplicate code 4661 * for the setup and revert functions below. 4662 */ 4663 static struct inodedep * 4664 inodedep_lookup_ip(ip) 4665 struct inode *ip; 4666 { 4667 struct inodedep *inodedep; 4668 4669 KASSERT(ip->i_nlink >= ip->i_effnlink, 4670 ("inodedep_lookup_ip: bad delta")); 4671 (void) inodedep_lookup(ITOVFS(ip), ip->i_number, DEPALLOC, 4672 &inodedep); 4673 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 4674 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 4675 4676 return (inodedep); 4677 } 4678 4679 /* 4680 * Called prior to creating a new inode and linking it to a directory. The 4681 * jaddref structure must already be allocated by softdep_setup_inomapdep 4682 * and it is discovered here so we can initialize the mode and update 4683 * nlinkdelta. 4684 */ 4685 void 4686 softdep_setup_create(dp, ip) 4687 struct inode *dp; 4688 struct inode *ip; 4689 { 4690 struct inodedep *inodedep; 4691 struct jaddref *jaddref; 4692 struct vnode *dvp; 4693 4694 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4695 ("softdep_setup_create called on non-softdep filesystem")); 4696 KASSERT(ip->i_nlink == 1, 4697 ("softdep_setup_create: Invalid link count.")); 4698 dvp = ITOV(dp); 4699 ACQUIRE_LOCK(ITOUMP(dp)); 4700 inodedep = inodedep_lookup_ip(ip); 4701 if (DOINGSUJ(dvp)) { 4702 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4703 inoreflst); 4704 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 4705 ("softdep_setup_create: No addref structure present.")); 4706 } 4707 softdep_prelink(dvp, NULL); 4708 FREE_LOCK(ITOUMP(dp)); 4709 } 4710 4711 /* 4712 * Create a jaddref structure to track the addition of a DOTDOT link when 4713 * we are reparenting an inode as part of a rename. This jaddref will be 4714 * found by softdep_setup_directory_change. Adjusts nlinkdelta for 4715 * non-journaling softdep. 4716 */ 4717 void 4718 softdep_setup_dotdot_link(dp, ip) 4719 struct inode *dp; 4720 struct inode *ip; 4721 { 4722 struct inodedep *inodedep; 4723 struct jaddref *jaddref; 4724 struct vnode *dvp; 4725 4726 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4727 ("softdep_setup_dotdot_link called on non-softdep filesystem")); 4728 dvp = ITOV(dp); 4729 jaddref = NULL; 4730 /* 4731 * We don't set MKDIR_PARENT as this is not tied to a mkdir and 4732 * is used as a normal link would be. 4733 */ 4734 if (DOINGSUJ(dvp)) 4735 jaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 4736 dp->i_effnlink - 1, dp->i_mode); 4737 ACQUIRE_LOCK(ITOUMP(dp)); 4738 inodedep = inodedep_lookup_ip(dp); 4739 if (jaddref) 4740 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4741 if_deps); 4742 softdep_prelink(dvp, ITOV(ip)); 4743 FREE_LOCK(ITOUMP(dp)); 4744 } 4745 4746 /* 4747 * Create a jaddref structure to track a new link to an inode. The directory 4748 * offset is not known until softdep_setup_directory_add or 4749 * softdep_setup_directory_change. Adjusts nlinkdelta for non-journaling 4750 * softdep. 4751 */ 4752 void 4753 softdep_setup_link(dp, ip) 4754 struct inode *dp; 4755 struct inode *ip; 4756 { 4757 struct inodedep *inodedep; 4758 struct jaddref *jaddref; 4759 struct vnode *dvp; 4760 4761 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4762 ("softdep_setup_link called on non-softdep filesystem")); 4763 dvp = ITOV(dp); 4764 jaddref = NULL; 4765 if (DOINGSUJ(dvp)) 4766 jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1, 4767 ip->i_mode); 4768 ACQUIRE_LOCK(ITOUMP(dp)); 4769 inodedep = inodedep_lookup_ip(ip); 4770 if (jaddref) 4771 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4772 if_deps); 4773 softdep_prelink(dvp, ITOV(ip)); 4774 FREE_LOCK(ITOUMP(dp)); 4775 } 4776 4777 /* 4778 * Called to create the jaddref structures to track . and .. references as 4779 * well as lookup and further initialize the incomplete jaddref created 4780 * by softdep_setup_inomapdep when the inode was allocated. Adjusts 4781 * nlinkdelta for non-journaling softdep. 4782 */ 4783 void 4784 softdep_setup_mkdir(dp, ip) 4785 struct inode *dp; 4786 struct inode *ip; 4787 { 4788 struct inodedep *inodedep; 4789 struct jaddref *dotdotaddref; 4790 struct jaddref *dotaddref; 4791 struct jaddref *jaddref; 4792 struct vnode *dvp; 4793 4794 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4795 ("softdep_setup_mkdir called on non-softdep filesystem")); 4796 dvp = ITOV(dp); 4797 dotaddref = dotdotaddref = NULL; 4798 if (DOINGSUJ(dvp)) { 4799 dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1, 4800 ip->i_mode); 4801 dotaddref->ja_state |= MKDIR_BODY; 4802 dotdotaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 4803 dp->i_effnlink - 1, dp->i_mode); 4804 dotdotaddref->ja_state |= MKDIR_PARENT; 4805 } 4806 ACQUIRE_LOCK(ITOUMP(dp)); 4807 inodedep = inodedep_lookup_ip(ip); 4808 if (DOINGSUJ(dvp)) { 4809 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4810 inoreflst); 4811 KASSERT(jaddref != NULL, 4812 ("softdep_setup_mkdir: No addref structure present.")); 4813 KASSERT(jaddref->ja_parent == dp->i_number, 4814 ("softdep_setup_mkdir: bad parent %ju", 4815 (uintmax_t)jaddref->ja_parent)); 4816 TAILQ_INSERT_BEFORE(&jaddref->ja_ref, &dotaddref->ja_ref, 4817 if_deps); 4818 } 4819 inodedep = inodedep_lookup_ip(dp); 4820 if (DOINGSUJ(dvp)) 4821 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, 4822 &dotdotaddref->ja_ref, if_deps); 4823 softdep_prelink(ITOV(dp), NULL); 4824 FREE_LOCK(ITOUMP(dp)); 4825 } 4826 4827 /* 4828 * Called to track nlinkdelta of the inode and parent directories prior to 4829 * unlinking a directory. 4830 */ 4831 void 4832 softdep_setup_rmdir(dp, ip) 4833 struct inode *dp; 4834 struct inode *ip; 4835 { 4836 struct vnode *dvp; 4837 4838 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4839 ("softdep_setup_rmdir called on non-softdep filesystem")); 4840 dvp = ITOV(dp); 4841 ACQUIRE_LOCK(ITOUMP(dp)); 4842 (void) inodedep_lookup_ip(ip); 4843 (void) inodedep_lookup_ip(dp); 4844 softdep_prelink(dvp, ITOV(ip)); 4845 FREE_LOCK(ITOUMP(dp)); 4846 } 4847 4848 /* 4849 * Called to track nlinkdelta of the inode and parent directories prior to 4850 * unlink. 4851 */ 4852 void 4853 softdep_setup_unlink(dp, ip) 4854 struct inode *dp; 4855 struct inode *ip; 4856 { 4857 struct vnode *dvp; 4858 4859 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4860 ("softdep_setup_unlink called on non-softdep filesystem")); 4861 dvp = ITOV(dp); 4862 ACQUIRE_LOCK(ITOUMP(dp)); 4863 (void) inodedep_lookup_ip(ip); 4864 (void) inodedep_lookup_ip(dp); 4865 softdep_prelink(dvp, ITOV(ip)); 4866 FREE_LOCK(ITOUMP(dp)); 4867 } 4868 4869 /* 4870 * Called to release the journal structures created by a failed non-directory 4871 * creation. Adjusts nlinkdelta for non-journaling softdep. 4872 */ 4873 void 4874 softdep_revert_create(dp, ip) 4875 struct inode *dp; 4876 struct inode *ip; 4877 { 4878 struct inodedep *inodedep; 4879 struct jaddref *jaddref; 4880 struct vnode *dvp; 4881 4882 KASSERT(MOUNTEDSOFTDEP(ITOVFS((dp))) != 0, 4883 ("softdep_revert_create called on non-softdep filesystem")); 4884 dvp = ITOV(dp); 4885 ACQUIRE_LOCK(ITOUMP(dp)); 4886 inodedep = inodedep_lookup_ip(ip); 4887 if (DOINGSUJ(dvp)) { 4888 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4889 inoreflst); 4890 KASSERT(jaddref->ja_parent == dp->i_number, 4891 ("softdep_revert_create: addref parent mismatch")); 4892 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4893 } 4894 FREE_LOCK(ITOUMP(dp)); 4895 } 4896 4897 /* 4898 * Called to release the journal structures created by a failed link 4899 * addition. Adjusts nlinkdelta for non-journaling softdep. 4900 */ 4901 void 4902 softdep_revert_link(dp, ip) 4903 struct inode *dp; 4904 struct inode *ip; 4905 { 4906 struct inodedep *inodedep; 4907 struct jaddref *jaddref; 4908 struct vnode *dvp; 4909 4910 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4911 ("softdep_revert_link called on non-softdep filesystem")); 4912 dvp = ITOV(dp); 4913 ACQUIRE_LOCK(ITOUMP(dp)); 4914 inodedep = inodedep_lookup_ip(ip); 4915 if (DOINGSUJ(dvp)) { 4916 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4917 inoreflst); 4918 KASSERT(jaddref->ja_parent == dp->i_number, 4919 ("softdep_revert_link: addref parent mismatch")); 4920 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4921 } 4922 FREE_LOCK(ITOUMP(dp)); 4923 } 4924 4925 /* 4926 * Called to release the journal structures created by a failed mkdir 4927 * attempt. Adjusts nlinkdelta for non-journaling softdep. 4928 */ 4929 void 4930 softdep_revert_mkdir(dp, ip) 4931 struct inode *dp; 4932 struct inode *ip; 4933 { 4934 struct inodedep *inodedep; 4935 struct jaddref *jaddref; 4936 struct jaddref *dotaddref; 4937 struct vnode *dvp; 4938 4939 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4940 ("softdep_revert_mkdir called on non-softdep filesystem")); 4941 dvp = ITOV(dp); 4942 4943 ACQUIRE_LOCK(ITOUMP(dp)); 4944 inodedep = inodedep_lookup_ip(dp); 4945 if (DOINGSUJ(dvp)) { 4946 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4947 inoreflst); 4948 KASSERT(jaddref->ja_parent == ip->i_number, 4949 ("softdep_revert_mkdir: dotdot addref parent mismatch")); 4950 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4951 } 4952 inodedep = inodedep_lookup_ip(ip); 4953 if (DOINGSUJ(dvp)) { 4954 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4955 inoreflst); 4956 KASSERT(jaddref->ja_parent == dp->i_number, 4957 ("softdep_revert_mkdir: addref parent mismatch")); 4958 dotaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 4959 inoreflst, if_deps); 4960 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4961 KASSERT(dotaddref->ja_parent == ip->i_number, 4962 ("softdep_revert_mkdir: dot addref parent mismatch")); 4963 cancel_jaddref(dotaddref, inodedep, &inodedep->id_inowait); 4964 } 4965 FREE_LOCK(ITOUMP(dp)); 4966 } 4967 4968 /* 4969 * Called to correct nlinkdelta after a failed rmdir. 4970 */ 4971 void 4972 softdep_revert_rmdir(dp, ip) 4973 struct inode *dp; 4974 struct inode *ip; 4975 { 4976 4977 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4978 ("softdep_revert_rmdir called on non-softdep filesystem")); 4979 ACQUIRE_LOCK(ITOUMP(dp)); 4980 (void) inodedep_lookup_ip(ip); 4981 (void) inodedep_lookup_ip(dp); 4982 FREE_LOCK(ITOUMP(dp)); 4983 } 4984 4985 /* 4986 * Protecting the freemaps (or bitmaps). 4987 * 4988 * To eliminate the need to execute fsck before mounting a filesystem 4989 * after a power failure, one must (conservatively) guarantee that the 4990 * on-disk copy of the bitmaps never indicate that a live inode or block is 4991 * free. So, when a block or inode is allocated, the bitmap should be 4992 * updated (on disk) before any new pointers. When a block or inode is 4993 * freed, the bitmap should not be updated until all pointers have been 4994 * reset. The latter dependency is handled by the delayed de-allocation 4995 * approach described below for block and inode de-allocation. The former 4996 * dependency is handled by calling the following procedure when a block or 4997 * inode is allocated. When an inode is allocated an "inodedep" is created 4998 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk. 4999 * Each "inodedep" is also inserted into the hash indexing structure so 5000 * that any additional link additions can be made dependent on the inode 5001 * allocation. 5002 * 5003 * The ufs filesystem maintains a number of free block counts (e.g., per 5004 * cylinder group, per cylinder and per <cylinder, rotational position> pair) 5005 * in addition to the bitmaps. These counts are used to improve efficiency 5006 * during allocation and therefore must be consistent with the bitmaps. 5007 * There is no convenient way to guarantee post-crash consistency of these 5008 * counts with simple update ordering, for two main reasons: (1) The counts 5009 * and bitmaps for a single cylinder group block are not in the same disk 5010 * sector. If a disk write is interrupted (e.g., by power failure), one may 5011 * be written and the other not. (2) Some of the counts are located in the 5012 * superblock rather than the cylinder group block. So, we focus our soft 5013 * updates implementation on protecting the bitmaps. When mounting a 5014 * filesystem, we recompute the auxiliary counts from the bitmaps. 5015 */ 5016 5017 /* 5018 * Called just after updating the cylinder group block to allocate an inode. 5019 */ 5020 void 5021 softdep_setup_inomapdep(bp, ip, newinum, mode) 5022 struct buf *bp; /* buffer for cylgroup block with inode map */ 5023 struct inode *ip; /* inode related to allocation */ 5024 ino_t newinum; /* new inode number being allocated */ 5025 int mode; 5026 { 5027 struct inodedep *inodedep; 5028 struct bmsafemap *bmsafemap; 5029 struct jaddref *jaddref; 5030 struct mount *mp; 5031 struct fs *fs; 5032 5033 mp = ITOVFS(ip); 5034 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5035 ("softdep_setup_inomapdep called on non-softdep filesystem")); 5036 fs = VFSTOUFS(mp)->um_fs; 5037 jaddref = NULL; 5038 5039 /* 5040 * Allocate the journal reference add structure so that the bitmap 5041 * can be dependent on it. 5042 */ 5043 if (MOUNTEDSUJ(mp)) { 5044 jaddref = newjaddref(ip, newinum, 0, 0, mode); 5045 jaddref->ja_state |= NEWBLOCK; 5046 } 5047 5048 /* 5049 * Create a dependency for the newly allocated inode. 5050 * Panic if it already exists as something is seriously wrong. 5051 * Otherwise add it to the dependency list for the buffer holding 5052 * the cylinder group map from which it was allocated. 5053 * 5054 * We have to preallocate a bmsafemap entry in case it is needed 5055 * in bmsafemap_lookup since once we allocate the inodedep, we 5056 * have to finish initializing it before we can FREE_LOCK(). 5057 * By preallocating, we avoid FREE_LOCK() while doing a malloc 5058 * in bmsafemap_lookup. We cannot call bmsafemap_lookup before 5059 * creating the inodedep as it can be freed during the time 5060 * that we FREE_LOCK() while allocating the inodedep. We must 5061 * call workitem_alloc() before entering the locked section as 5062 * it also acquires the lock and we must avoid trying doing so 5063 * recursively. 5064 */ 5065 bmsafemap = malloc(sizeof(struct bmsafemap), 5066 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5067 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5068 ACQUIRE_LOCK(ITOUMP(ip)); 5069 if ((inodedep_lookup(mp, newinum, DEPALLOC, &inodedep))) 5070 panic("softdep_setup_inomapdep: dependency %p for new" 5071 "inode already exists", inodedep); 5072 bmsafemap = bmsafemap_lookup(mp, bp, ino_to_cg(fs, newinum), bmsafemap); 5073 if (jaddref) { 5074 LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps); 5075 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 5076 if_deps); 5077 } else { 5078 inodedep->id_state |= ONDEPLIST; 5079 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps); 5080 } 5081 inodedep->id_bmsafemap = bmsafemap; 5082 inodedep->id_state &= ~DEPCOMPLETE; 5083 FREE_LOCK(ITOUMP(ip)); 5084 } 5085 5086 /* 5087 * Called just after updating the cylinder group block to 5088 * allocate block or fragment. 5089 */ 5090 void 5091 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags) 5092 struct buf *bp; /* buffer for cylgroup block with block map */ 5093 struct mount *mp; /* filesystem doing allocation */ 5094 ufs2_daddr_t newblkno; /* number of newly allocated block */ 5095 int frags; /* Number of fragments. */ 5096 int oldfrags; /* Previous number of fragments for extend. */ 5097 { 5098 struct newblk *newblk; 5099 struct bmsafemap *bmsafemap; 5100 struct jnewblk *jnewblk; 5101 struct ufsmount *ump; 5102 struct fs *fs; 5103 5104 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5105 ("softdep_setup_blkmapdep called on non-softdep filesystem")); 5106 ump = VFSTOUFS(mp); 5107 fs = ump->um_fs; 5108 jnewblk = NULL; 5109 /* 5110 * Create a dependency for the newly allocated block. 5111 * Add it to the dependency list for the buffer holding 5112 * the cylinder group map from which it was allocated. 5113 */ 5114 if (MOUNTEDSUJ(mp)) { 5115 jnewblk = malloc(sizeof(*jnewblk), M_JNEWBLK, M_SOFTDEP_FLAGS); 5116 workitem_alloc(&jnewblk->jn_list, D_JNEWBLK, mp); 5117 jnewblk->jn_jsegdep = newjsegdep(&jnewblk->jn_list); 5118 jnewblk->jn_state = ATTACHED; 5119 jnewblk->jn_blkno = newblkno; 5120 jnewblk->jn_frags = frags; 5121 jnewblk->jn_oldfrags = oldfrags; 5122 #ifdef INVARIANTS 5123 { 5124 struct cg *cgp; 5125 uint8_t *blksfree; 5126 long bno; 5127 int i; 5128 5129 cgp = (struct cg *)bp->b_data; 5130 blksfree = cg_blksfree(cgp); 5131 bno = dtogd(fs, jnewblk->jn_blkno); 5132 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; 5133 i++) { 5134 if (isset(blksfree, bno + i)) 5135 panic("softdep_setup_blkmapdep: " 5136 "free fragment %d from %d-%d " 5137 "state 0x%X dep %p", i, 5138 jnewblk->jn_oldfrags, 5139 jnewblk->jn_frags, 5140 jnewblk->jn_state, 5141 jnewblk->jn_dep); 5142 } 5143 } 5144 #endif 5145 } 5146 5147 CTR3(KTR_SUJ, 5148 "softdep_setup_blkmapdep: blkno %jd frags %d oldfrags %d", 5149 newblkno, frags, oldfrags); 5150 ACQUIRE_LOCK(ump); 5151 if (newblk_lookup(mp, newblkno, DEPALLOC, &newblk) != 0) 5152 panic("softdep_setup_blkmapdep: found block"); 5153 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp, 5154 dtog(fs, newblkno), NULL); 5155 if (jnewblk) { 5156 jnewblk->jn_dep = (struct worklist *)newblk; 5157 LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps); 5158 } else { 5159 newblk->nb_state |= ONDEPLIST; 5160 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps); 5161 } 5162 newblk->nb_bmsafemap = bmsafemap; 5163 newblk->nb_jnewblk = jnewblk; 5164 FREE_LOCK(ump); 5165 } 5166 5167 #define BMSAFEMAP_HASH(ump, cg) \ 5168 (&(ump)->bmsafemap_hashtbl[(cg) & (ump)->bmsafemap_hash_size]) 5169 5170 static int 5171 bmsafemap_find(bmsafemaphd, cg, bmsafemapp) 5172 struct bmsafemap_hashhead *bmsafemaphd; 5173 int cg; 5174 struct bmsafemap **bmsafemapp; 5175 { 5176 struct bmsafemap *bmsafemap; 5177 5178 LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash) 5179 if (bmsafemap->sm_cg == cg) 5180 break; 5181 if (bmsafemap) { 5182 *bmsafemapp = bmsafemap; 5183 return (1); 5184 } 5185 *bmsafemapp = NULL; 5186 5187 return (0); 5188 } 5189 5190 /* 5191 * Find the bmsafemap associated with a cylinder group buffer. 5192 * If none exists, create one. The buffer must be locked when 5193 * this routine is called and this routine must be called with 5194 * the softdep lock held. To avoid giving up the lock while 5195 * allocating a new bmsafemap, a preallocated bmsafemap may be 5196 * provided. If it is provided but not needed, it is freed. 5197 */ 5198 static struct bmsafemap * 5199 bmsafemap_lookup(mp, bp, cg, newbmsafemap) 5200 struct mount *mp; 5201 struct buf *bp; 5202 int cg; 5203 struct bmsafemap *newbmsafemap; 5204 { 5205 struct bmsafemap_hashhead *bmsafemaphd; 5206 struct bmsafemap *bmsafemap, *collision; 5207 struct worklist *wk; 5208 struct ufsmount *ump; 5209 5210 ump = VFSTOUFS(mp); 5211 LOCK_OWNED(ump); 5212 KASSERT(bp != NULL, ("bmsafemap_lookup: missing buffer")); 5213 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 5214 if (wk->wk_type == D_BMSAFEMAP) { 5215 if (newbmsafemap) 5216 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5217 return (WK_BMSAFEMAP(wk)); 5218 } 5219 } 5220 bmsafemaphd = BMSAFEMAP_HASH(ump, cg); 5221 if (bmsafemap_find(bmsafemaphd, cg, &bmsafemap) == 1) { 5222 if (newbmsafemap) 5223 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5224 return (bmsafemap); 5225 } 5226 if (newbmsafemap) { 5227 bmsafemap = newbmsafemap; 5228 } else { 5229 FREE_LOCK(ump); 5230 bmsafemap = malloc(sizeof(struct bmsafemap), 5231 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5232 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5233 ACQUIRE_LOCK(ump); 5234 } 5235 bmsafemap->sm_buf = bp; 5236 LIST_INIT(&bmsafemap->sm_inodedephd); 5237 LIST_INIT(&bmsafemap->sm_inodedepwr); 5238 LIST_INIT(&bmsafemap->sm_newblkhd); 5239 LIST_INIT(&bmsafemap->sm_newblkwr); 5240 LIST_INIT(&bmsafemap->sm_jaddrefhd); 5241 LIST_INIT(&bmsafemap->sm_jnewblkhd); 5242 LIST_INIT(&bmsafemap->sm_freehd); 5243 LIST_INIT(&bmsafemap->sm_freewr); 5244 if (bmsafemap_find(bmsafemaphd, cg, &collision) == 1) { 5245 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 5246 return (collision); 5247 } 5248 bmsafemap->sm_cg = cg; 5249 LIST_INSERT_HEAD(bmsafemaphd, bmsafemap, sm_hash); 5250 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 5251 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list); 5252 return (bmsafemap); 5253 } 5254 5255 /* 5256 * Direct block allocation dependencies. 5257 * 5258 * When a new block is allocated, the corresponding disk locations must be 5259 * initialized (with zeros or new data) before the on-disk inode points to 5260 * them. Also, the freemap from which the block was allocated must be 5261 * updated (on disk) before the inode's pointer. These two dependencies are 5262 * independent of each other and are needed for all file blocks and indirect 5263 * blocks that are pointed to directly by the inode. Just before the 5264 * "in-core" version of the inode is updated with a newly allocated block 5265 * number, a procedure (below) is called to setup allocation dependency 5266 * structures. These structures are removed when the corresponding 5267 * dependencies are satisfied or when the block allocation becomes obsolete 5268 * (i.e., the file is deleted, the block is de-allocated, or the block is a 5269 * fragment that gets upgraded). All of these cases are handled in 5270 * procedures described later. 5271 * 5272 * When a file extension causes a fragment to be upgraded, either to a larger 5273 * fragment or to a full block, the on-disk location may change (if the 5274 * previous fragment could not simply be extended). In this case, the old 5275 * fragment must be de-allocated, but not until after the inode's pointer has 5276 * been updated. In most cases, this is handled by later procedures, which 5277 * will construct a "freefrag" structure to be added to the workitem queue 5278 * when the inode update is complete (or obsolete). The main exception to 5279 * this is when an allocation occurs while a pending allocation dependency 5280 * (for the same block pointer) remains. This case is handled in the main 5281 * allocation dependency setup procedure by immediately freeing the 5282 * unreferenced fragments. 5283 */ 5284 void 5285 softdep_setup_allocdirect(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 5286 struct inode *ip; /* inode to which block is being added */ 5287 ufs_lbn_t off; /* block pointer within inode */ 5288 ufs2_daddr_t newblkno; /* disk block number being added */ 5289 ufs2_daddr_t oldblkno; /* previous block number, 0 unless frag */ 5290 long newsize; /* size of new block */ 5291 long oldsize; /* size of new block */ 5292 struct buf *bp; /* bp for allocated block */ 5293 { 5294 struct allocdirect *adp, *oldadp; 5295 struct allocdirectlst *adphead; 5296 struct freefrag *freefrag; 5297 struct inodedep *inodedep; 5298 struct pagedep *pagedep; 5299 struct jnewblk *jnewblk; 5300 struct newblk *newblk; 5301 struct mount *mp; 5302 ufs_lbn_t lbn; 5303 5304 lbn = bp->b_lblkno; 5305 mp = ITOVFS(ip); 5306 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5307 ("softdep_setup_allocdirect called on non-softdep filesystem")); 5308 if (oldblkno && oldblkno != newblkno) 5309 /* 5310 * The usual case is that a smaller fragment that 5311 * was just allocated has been replaced with a bigger 5312 * fragment or a full-size block. If it is marked as 5313 * B_DELWRI, the current contents have not been written 5314 * to disk. It is possible that the block was written 5315 * earlier, but very uncommon. If the block has never 5316 * been written, there is no need to send a BIO_DELETE 5317 * for it when it is freed. The gain from avoiding the 5318 * TRIMs for the common case of unwritten blocks far 5319 * exceeds the cost of the write amplification for the 5320 * uncommon case of failing to send a TRIM for a block 5321 * that had been written. 5322 */ 5323 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn, 5324 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY); 5325 else 5326 freefrag = NULL; 5327 5328 CTR6(KTR_SUJ, 5329 "softdep_setup_allocdirect: ino %d blkno %jd oldblkno %jd " 5330 "off %jd newsize %ld oldsize %d", 5331 ip->i_number, newblkno, oldblkno, off, newsize, oldsize); 5332 ACQUIRE_LOCK(ITOUMP(ip)); 5333 if (off >= UFS_NDADDR) { 5334 if (lbn > 0) 5335 panic("softdep_setup_allocdirect: bad lbn %jd, off %jd", 5336 lbn, off); 5337 /* allocating an indirect block */ 5338 if (oldblkno != 0) 5339 panic("softdep_setup_allocdirect: non-zero indir"); 5340 } else { 5341 if (off != lbn) 5342 panic("softdep_setup_allocdirect: lbn %jd != off %jd", 5343 lbn, off); 5344 /* 5345 * Allocating a direct block. 5346 * 5347 * If we are allocating a directory block, then we must 5348 * allocate an associated pagedep to track additions and 5349 * deletions. 5350 */ 5351 if ((ip->i_mode & IFMT) == IFDIR) 5352 pagedep_lookup(mp, bp, ip->i_number, off, DEPALLOC, 5353 &pagedep); 5354 } 5355 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5356 panic("softdep_setup_allocdirect: lost block"); 5357 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5358 ("softdep_setup_allocdirect: newblk already initialized")); 5359 /* 5360 * Convert the newblk to an allocdirect. 5361 */ 5362 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT); 5363 adp = (struct allocdirect *)newblk; 5364 newblk->nb_freefrag = freefrag; 5365 adp->ad_offset = off; 5366 adp->ad_oldblkno = oldblkno; 5367 adp->ad_newsize = newsize; 5368 adp->ad_oldsize = oldsize; 5369 5370 /* 5371 * Finish initializing the journal. 5372 */ 5373 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5374 jnewblk->jn_ino = ip->i_number; 5375 jnewblk->jn_lbn = lbn; 5376 add_to_journal(&jnewblk->jn_list); 5377 } 5378 if (freefrag && freefrag->ff_jdep != NULL && 5379 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5380 add_to_journal(freefrag->ff_jdep); 5381 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 5382 adp->ad_inodedep = inodedep; 5383 5384 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5385 /* 5386 * The list of allocdirects must be kept in sorted and ascending 5387 * order so that the rollback routines can quickly determine the 5388 * first uncommitted block (the size of the file stored on disk 5389 * ends at the end of the lowest committed fragment, or if there 5390 * are no fragments, at the end of the highest committed block). 5391 * Since files generally grow, the typical case is that the new 5392 * block is to be added at the end of the list. We speed this 5393 * special case by checking against the last allocdirect in the 5394 * list before laboriously traversing the list looking for the 5395 * insertion point. 5396 */ 5397 adphead = &inodedep->id_newinoupdt; 5398 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5399 if (oldadp == NULL || oldadp->ad_offset <= off) { 5400 /* insert at end of list */ 5401 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5402 if (oldadp != NULL && oldadp->ad_offset == off) 5403 allocdirect_merge(adphead, adp, oldadp); 5404 FREE_LOCK(ITOUMP(ip)); 5405 return; 5406 } 5407 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5408 if (oldadp->ad_offset >= off) 5409 break; 5410 } 5411 if (oldadp == NULL) 5412 panic("softdep_setup_allocdirect: lost entry"); 5413 /* insert in middle of list */ 5414 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5415 if (oldadp->ad_offset == off) 5416 allocdirect_merge(adphead, adp, oldadp); 5417 5418 FREE_LOCK(ITOUMP(ip)); 5419 } 5420 5421 /* 5422 * Merge a newer and older journal record to be stored either in a 5423 * newblock or freefrag. This handles aggregating journal records for 5424 * fragment allocation into a second record as well as replacing a 5425 * journal free with an aborted journal allocation. A segment for the 5426 * oldest record will be placed on wkhd if it has been written. If not 5427 * the segment for the newer record will suffice. 5428 */ 5429 static struct worklist * 5430 jnewblk_merge(new, old, wkhd) 5431 struct worklist *new; 5432 struct worklist *old; 5433 struct workhead *wkhd; 5434 { 5435 struct jnewblk *njnewblk; 5436 struct jnewblk *jnewblk; 5437 5438 /* Handle NULLs to simplify callers. */ 5439 if (new == NULL) 5440 return (old); 5441 if (old == NULL) 5442 return (new); 5443 /* Replace a jfreefrag with a jnewblk. */ 5444 if (new->wk_type == D_JFREEFRAG) { 5445 if (WK_JNEWBLK(old)->jn_blkno != WK_JFREEFRAG(new)->fr_blkno) 5446 panic("jnewblk_merge: blkno mismatch: %p, %p", 5447 old, new); 5448 cancel_jfreefrag(WK_JFREEFRAG(new)); 5449 return (old); 5450 } 5451 if (old->wk_type != D_JNEWBLK || new->wk_type != D_JNEWBLK) 5452 panic("jnewblk_merge: Bad type: old %d new %d\n", 5453 old->wk_type, new->wk_type); 5454 /* 5455 * Handle merging of two jnewblk records that describe 5456 * different sets of fragments in the same block. 5457 */ 5458 jnewblk = WK_JNEWBLK(old); 5459 njnewblk = WK_JNEWBLK(new); 5460 if (jnewblk->jn_blkno != njnewblk->jn_blkno) 5461 panic("jnewblk_merge: Merging disparate blocks."); 5462 /* 5463 * The record may be rolled back in the cg. 5464 */ 5465 if (jnewblk->jn_state & UNDONE) { 5466 jnewblk->jn_state &= ~UNDONE; 5467 njnewblk->jn_state |= UNDONE; 5468 njnewblk->jn_state &= ~ATTACHED; 5469 } 5470 /* 5471 * We modify the newer addref and free the older so that if neither 5472 * has been written the most up-to-date copy will be on disk. If 5473 * both have been written but rolled back we only temporarily need 5474 * one of them to fix the bits when the cg write completes. 5475 */ 5476 jnewblk->jn_state |= ATTACHED | COMPLETE; 5477 njnewblk->jn_oldfrags = jnewblk->jn_oldfrags; 5478 cancel_jnewblk(jnewblk, wkhd); 5479 WORKLIST_REMOVE(&jnewblk->jn_list); 5480 free_jnewblk(jnewblk); 5481 return (new); 5482 } 5483 5484 /* 5485 * Replace an old allocdirect dependency with a newer one. 5486 */ 5487 static void 5488 allocdirect_merge(adphead, newadp, oldadp) 5489 struct allocdirectlst *adphead; /* head of list holding allocdirects */ 5490 struct allocdirect *newadp; /* allocdirect being added */ 5491 struct allocdirect *oldadp; /* existing allocdirect being checked */ 5492 { 5493 struct worklist *wk; 5494 struct freefrag *freefrag; 5495 5496 freefrag = NULL; 5497 LOCK_OWNED(VFSTOUFS(newadp->ad_list.wk_mp)); 5498 if (newadp->ad_oldblkno != oldadp->ad_newblkno || 5499 newadp->ad_oldsize != oldadp->ad_newsize || 5500 newadp->ad_offset >= UFS_NDADDR) 5501 panic("%s %jd != new %jd || old size %ld != new %ld", 5502 "allocdirect_merge: old blkno", 5503 (intmax_t)newadp->ad_oldblkno, 5504 (intmax_t)oldadp->ad_newblkno, 5505 newadp->ad_oldsize, oldadp->ad_newsize); 5506 newadp->ad_oldblkno = oldadp->ad_oldblkno; 5507 newadp->ad_oldsize = oldadp->ad_oldsize; 5508 /* 5509 * If the old dependency had a fragment to free or had never 5510 * previously had a block allocated, then the new dependency 5511 * can immediately post its freefrag and adopt the old freefrag. 5512 * This action is done by swapping the freefrag dependencies. 5513 * The new dependency gains the old one's freefrag, and the 5514 * old one gets the new one and then immediately puts it on 5515 * the worklist when it is freed by free_newblk. It is 5516 * not possible to do this swap when the old dependency had a 5517 * non-zero size but no previous fragment to free. This condition 5518 * arises when the new block is an extension of the old block. 5519 * Here, the first part of the fragment allocated to the new 5520 * dependency is part of the block currently claimed on disk by 5521 * the old dependency, so cannot legitimately be freed until the 5522 * conditions for the new dependency are fulfilled. 5523 */ 5524 freefrag = newadp->ad_freefrag; 5525 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) { 5526 newadp->ad_freefrag = oldadp->ad_freefrag; 5527 oldadp->ad_freefrag = freefrag; 5528 } 5529 /* 5530 * If we are tracking a new directory-block allocation, 5531 * move it from the old allocdirect to the new allocdirect. 5532 */ 5533 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) { 5534 WORKLIST_REMOVE(wk); 5535 if (!LIST_EMPTY(&oldadp->ad_newdirblk)) 5536 panic("allocdirect_merge: extra newdirblk"); 5537 WORKLIST_INSERT(&newadp->ad_newdirblk, wk); 5538 } 5539 TAILQ_REMOVE(adphead, oldadp, ad_next); 5540 /* 5541 * We need to move any journal dependencies over to the freefrag 5542 * that releases this block if it exists. Otherwise we are 5543 * extending an existing block and we'll wait until that is 5544 * complete to release the journal space and extend the 5545 * new journal to cover this old space as well. 5546 */ 5547 if (freefrag == NULL) { 5548 if (oldadp->ad_newblkno != newadp->ad_newblkno) 5549 panic("allocdirect_merge: %jd != %jd", 5550 oldadp->ad_newblkno, newadp->ad_newblkno); 5551 newadp->ad_block.nb_jnewblk = (struct jnewblk *) 5552 jnewblk_merge(&newadp->ad_block.nb_jnewblk->jn_list, 5553 &oldadp->ad_block.nb_jnewblk->jn_list, 5554 &newadp->ad_block.nb_jwork); 5555 oldadp->ad_block.nb_jnewblk = NULL; 5556 cancel_newblk(&oldadp->ad_block, NULL, 5557 &newadp->ad_block.nb_jwork); 5558 } else { 5559 wk = (struct worklist *) cancel_newblk(&oldadp->ad_block, 5560 &freefrag->ff_list, &freefrag->ff_jwork); 5561 freefrag->ff_jdep = jnewblk_merge(freefrag->ff_jdep, wk, 5562 &freefrag->ff_jwork); 5563 } 5564 free_newblk(&oldadp->ad_block); 5565 } 5566 5567 /* 5568 * Allocate a jfreefrag structure to journal a single block free. 5569 */ 5570 static struct jfreefrag * 5571 newjfreefrag(freefrag, ip, blkno, size, lbn) 5572 struct freefrag *freefrag; 5573 struct inode *ip; 5574 ufs2_daddr_t blkno; 5575 long size; 5576 ufs_lbn_t lbn; 5577 { 5578 struct jfreefrag *jfreefrag; 5579 struct fs *fs; 5580 5581 fs = ITOFS(ip); 5582 jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG, 5583 M_SOFTDEP_FLAGS); 5584 workitem_alloc(&jfreefrag->fr_list, D_JFREEFRAG, ITOVFS(ip)); 5585 jfreefrag->fr_jsegdep = newjsegdep(&jfreefrag->fr_list); 5586 jfreefrag->fr_state = ATTACHED | DEPCOMPLETE; 5587 jfreefrag->fr_ino = ip->i_number; 5588 jfreefrag->fr_lbn = lbn; 5589 jfreefrag->fr_blkno = blkno; 5590 jfreefrag->fr_frags = numfrags(fs, size); 5591 jfreefrag->fr_freefrag = freefrag; 5592 5593 return (jfreefrag); 5594 } 5595 5596 /* 5597 * Allocate a new freefrag structure. 5598 */ 5599 static struct freefrag * 5600 newfreefrag(ip, blkno, size, lbn, key) 5601 struct inode *ip; 5602 ufs2_daddr_t blkno; 5603 long size; 5604 ufs_lbn_t lbn; 5605 u_long key; 5606 { 5607 struct freefrag *freefrag; 5608 struct ufsmount *ump; 5609 struct fs *fs; 5610 5611 CTR4(KTR_SUJ, "newfreefrag: ino %d blkno %jd size %ld lbn %jd", 5612 ip->i_number, blkno, size, lbn); 5613 ump = ITOUMP(ip); 5614 fs = ump->um_fs; 5615 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag) 5616 panic("newfreefrag: frag size"); 5617 freefrag = malloc(sizeof(struct freefrag), 5618 M_FREEFRAG, M_SOFTDEP_FLAGS); 5619 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ump)); 5620 freefrag->ff_state = ATTACHED; 5621 LIST_INIT(&freefrag->ff_jwork); 5622 freefrag->ff_inum = ip->i_number; 5623 freefrag->ff_vtype = ITOV(ip)->v_type; 5624 freefrag->ff_blkno = blkno; 5625 freefrag->ff_fragsize = size; 5626 freefrag->ff_key = key; 5627 5628 if (MOUNTEDSUJ(UFSTOVFS(ump))) { 5629 freefrag->ff_jdep = (struct worklist *) 5630 newjfreefrag(freefrag, ip, blkno, size, lbn); 5631 } else { 5632 freefrag->ff_state |= DEPCOMPLETE; 5633 freefrag->ff_jdep = NULL; 5634 } 5635 5636 return (freefrag); 5637 } 5638 5639 /* 5640 * This workitem de-allocates fragments that were replaced during 5641 * file block allocation. 5642 */ 5643 static void 5644 handle_workitem_freefrag(freefrag) 5645 struct freefrag *freefrag; 5646 { 5647 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp); 5648 struct workhead wkhd; 5649 5650 CTR3(KTR_SUJ, 5651 "handle_workitem_freefrag: ino %d blkno %jd size %ld", 5652 freefrag->ff_inum, freefrag->ff_blkno, freefrag->ff_fragsize); 5653 /* 5654 * It would be illegal to add new completion items to the 5655 * freefrag after it was schedule to be done so it must be 5656 * safe to modify the list head here. 5657 */ 5658 LIST_INIT(&wkhd); 5659 ACQUIRE_LOCK(ump); 5660 LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list); 5661 /* 5662 * If the journal has not been written we must cancel it here. 5663 */ 5664 if (freefrag->ff_jdep) { 5665 if (freefrag->ff_jdep->wk_type != D_JNEWBLK) 5666 panic("handle_workitem_freefrag: Unexpected type %d\n", 5667 freefrag->ff_jdep->wk_type); 5668 cancel_jnewblk(WK_JNEWBLK(freefrag->ff_jdep), &wkhd); 5669 } 5670 FREE_LOCK(ump); 5671 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno, 5672 freefrag->ff_fragsize, freefrag->ff_inum, freefrag->ff_vtype, 5673 &wkhd, freefrag->ff_key); 5674 ACQUIRE_LOCK(ump); 5675 WORKITEM_FREE(freefrag, D_FREEFRAG); 5676 FREE_LOCK(ump); 5677 } 5678 5679 /* 5680 * Set up a dependency structure for an external attributes data block. 5681 * This routine follows much of the structure of softdep_setup_allocdirect. 5682 * See the description of softdep_setup_allocdirect above for details. 5683 */ 5684 void 5685 softdep_setup_allocext(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 5686 struct inode *ip; 5687 ufs_lbn_t off; 5688 ufs2_daddr_t newblkno; 5689 ufs2_daddr_t oldblkno; 5690 long newsize; 5691 long oldsize; 5692 struct buf *bp; 5693 { 5694 struct allocdirect *adp, *oldadp; 5695 struct allocdirectlst *adphead; 5696 struct freefrag *freefrag; 5697 struct inodedep *inodedep; 5698 struct jnewblk *jnewblk; 5699 struct newblk *newblk; 5700 struct mount *mp; 5701 struct ufsmount *ump; 5702 ufs_lbn_t lbn; 5703 5704 mp = ITOVFS(ip); 5705 ump = VFSTOUFS(mp); 5706 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5707 ("softdep_setup_allocext called on non-softdep filesystem")); 5708 KASSERT(off < UFS_NXADDR, 5709 ("softdep_setup_allocext: lbn %lld > UFS_NXADDR", (long long)off)); 5710 5711 lbn = bp->b_lblkno; 5712 if (oldblkno && oldblkno != newblkno) 5713 /* 5714 * The usual case is that a smaller fragment that 5715 * was just allocated has been replaced with a bigger 5716 * fragment or a full-size block. If it is marked as 5717 * B_DELWRI, the current contents have not been written 5718 * to disk. It is possible that the block was written 5719 * earlier, but very uncommon. If the block has never 5720 * been written, there is no need to send a BIO_DELETE 5721 * for it when it is freed. The gain from avoiding the 5722 * TRIMs for the common case of unwritten blocks far 5723 * exceeds the cost of the write amplification for the 5724 * uncommon case of failing to send a TRIM for a block 5725 * that had been written. 5726 */ 5727 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn, 5728 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY); 5729 else 5730 freefrag = NULL; 5731 5732 ACQUIRE_LOCK(ump); 5733 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5734 panic("softdep_setup_allocext: lost block"); 5735 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5736 ("softdep_setup_allocext: newblk already initialized")); 5737 /* 5738 * Convert the newblk to an allocdirect. 5739 */ 5740 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT); 5741 adp = (struct allocdirect *)newblk; 5742 newblk->nb_freefrag = freefrag; 5743 adp->ad_offset = off; 5744 adp->ad_oldblkno = oldblkno; 5745 adp->ad_newsize = newsize; 5746 adp->ad_oldsize = oldsize; 5747 adp->ad_state |= EXTDATA; 5748 5749 /* 5750 * Finish initializing the journal. 5751 */ 5752 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5753 jnewblk->jn_ino = ip->i_number; 5754 jnewblk->jn_lbn = lbn; 5755 add_to_journal(&jnewblk->jn_list); 5756 } 5757 if (freefrag && freefrag->ff_jdep != NULL && 5758 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5759 add_to_journal(freefrag->ff_jdep); 5760 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 5761 adp->ad_inodedep = inodedep; 5762 5763 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5764 /* 5765 * The list of allocdirects must be kept in sorted and ascending 5766 * order so that the rollback routines can quickly determine the 5767 * first uncommitted block (the size of the file stored on disk 5768 * ends at the end of the lowest committed fragment, or if there 5769 * are no fragments, at the end of the highest committed block). 5770 * Since files generally grow, the typical case is that the new 5771 * block is to be added at the end of the list. We speed this 5772 * special case by checking against the last allocdirect in the 5773 * list before laboriously traversing the list looking for the 5774 * insertion point. 5775 */ 5776 adphead = &inodedep->id_newextupdt; 5777 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5778 if (oldadp == NULL || oldadp->ad_offset <= off) { 5779 /* insert at end of list */ 5780 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5781 if (oldadp != NULL && oldadp->ad_offset == off) 5782 allocdirect_merge(adphead, adp, oldadp); 5783 FREE_LOCK(ump); 5784 return; 5785 } 5786 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5787 if (oldadp->ad_offset >= off) 5788 break; 5789 } 5790 if (oldadp == NULL) 5791 panic("softdep_setup_allocext: lost entry"); 5792 /* insert in middle of list */ 5793 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5794 if (oldadp->ad_offset == off) 5795 allocdirect_merge(adphead, adp, oldadp); 5796 FREE_LOCK(ump); 5797 } 5798 5799 /* 5800 * Indirect block allocation dependencies. 5801 * 5802 * The same dependencies that exist for a direct block also exist when 5803 * a new block is allocated and pointed to by an entry in a block of 5804 * indirect pointers. The undo/redo states described above are also 5805 * used here. Because an indirect block contains many pointers that 5806 * may have dependencies, a second copy of the entire in-memory indirect 5807 * block is kept. The buffer cache copy is always completely up-to-date. 5808 * The second copy, which is used only as a source for disk writes, 5809 * contains only the safe pointers (i.e., those that have no remaining 5810 * update dependencies). The second copy is freed when all pointers 5811 * are safe. The cache is not allowed to replace indirect blocks with 5812 * pending update dependencies. If a buffer containing an indirect 5813 * block with dependencies is written, these routines will mark it 5814 * dirty again. It can only be successfully written once all the 5815 * dependencies are removed. The ffs_fsync routine in conjunction with 5816 * softdep_sync_metadata work together to get all the dependencies 5817 * removed so that a file can be successfully written to disk. Three 5818 * procedures are used when setting up indirect block pointer 5819 * dependencies. The division is necessary because of the organization 5820 * of the "balloc" routine and because of the distinction between file 5821 * pages and file metadata blocks. 5822 */ 5823 5824 /* 5825 * Allocate a new allocindir structure. 5826 */ 5827 static struct allocindir * 5828 newallocindir(ip, ptrno, newblkno, oldblkno, lbn) 5829 struct inode *ip; /* inode for file being extended */ 5830 int ptrno; /* offset of pointer in indirect block */ 5831 ufs2_daddr_t newblkno; /* disk block number being added */ 5832 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 5833 ufs_lbn_t lbn; 5834 { 5835 struct newblk *newblk; 5836 struct allocindir *aip; 5837 struct freefrag *freefrag; 5838 struct jnewblk *jnewblk; 5839 5840 if (oldblkno) 5841 freefrag = newfreefrag(ip, oldblkno, ITOFS(ip)->fs_bsize, lbn, 5842 SINGLETON_KEY); 5843 else 5844 freefrag = NULL; 5845 ACQUIRE_LOCK(ITOUMP(ip)); 5846 if (newblk_lookup(ITOVFS(ip), newblkno, 0, &newblk) == 0) 5847 panic("new_allocindir: lost block"); 5848 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5849 ("newallocindir: newblk already initialized")); 5850 WORKITEM_REASSIGN(newblk, D_ALLOCINDIR); 5851 newblk->nb_freefrag = freefrag; 5852 aip = (struct allocindir *)newblk; 5853 aip->ai_offset = ptrno; 5854 aip->ai_oldblkno = oldblkno; 5855 aip->ai_lbn = lbn; 5856 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5857 jnewblk->jn_ino = ip->i_number; 5858 jnewblk->jn_lbn = lbn; 5859 add_to_journal(&jnewblk->jn_list); 5860 } 5861 if (freefrag && freefrag->ff_jdep != NULL && 5862 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5863 add_to_journal(freefrag->ff_jdep); 5864 return (aip); 5865 } 5866 5867 /* 5868 * Called just before setting an indirect block pointer 5869 * to a newly allocated file page. 5870 */ 5871 void 5872 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 5873 struct inode *ip; /* inode for file being extended */ 5874 ufs_lbn_t lbn; /* allocated block number within file */ 5875 struct buf *bp; /* buffer with indirect blk referencing page */ 5876 int ptrno; /* offset of pointer in indirect block */ 5877 ufs2_daddr_t newblkno; /* disk block number being added */ 5878 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 5879 struct buf *nbp; /* buffer holding allocated page */ 5880 { 5881 struct inodedep *inodedep; 5882 struct freefrag *freefrag; 5883 struct allocindir *aip; 5884 struct pagedep *pagedep; 5885 struct mount *mp; 5886 struct ufsmount *ump; 5887 5888 mp = ITOVFS(ip); 5889 ump = VFSTOUFS(mp); 5890 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5891 ("softdep_setup_allocindir_page called on non-softdep filesystem")); 5892 KASSERT(lbn == nbp->b_lblkno, 5893 ("softdep_setup_allocindir_page: lbn %jd != lblkno %jd", 5894 lbn, bp->b_lblkno)); 5895 CTR4(KTR_SUJ, 5896 "softdep_setup_allocindir_page: ino %d blkno %jd oldblkno %jd " 5897 "lbn %jd", ip->i_number, newblkno, oldblkno, lbn); 5898 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page"); 5899 aip = newallocindir(ip, ptrno, newblkno, oldblkno, lbn); 5900 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 5901 /* 5902 * If we are allocating a directory page, then we must 5903 * allocate an associated pagedep to track additions and 5904 * deletions. 5905 */ 5906 if ((ip->i_mode & IFMT) == IFDIR) 5907 pagedep_lookup(mp, nbp, ip->i_number, lbn, DEPALLOC, &pagedep); 5908 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 5909 freefrag = setup_allocindir_phase2(bp, ip, inodedep, aip, lbn); 5910 FREE_LOCK(ump); 5911 if (freefrag) 5912 handle_workitem_freefrag(freefrag); 5913 } 5914 5915 /* 5916 * Called just before setting an indirect block pointer to a 5917 * newly allocated indirect block. 5918 */ 5919 void 5920 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 5921 struct buf *nbp; /* newly allocated indirect block */ 5922 struct inode *ip; /* inode for file being extended */ 5923 struct buf *bp; /* indirect block referencing allocated block */ 5924 int ptrno; /* offset of pointer in indirect block */ 5925 ufs2_daddr_t newblkno; /* disk block number being added */ 5926 { 5927 struct inodedep *inodedep; 5928 struct allocindir *aip; 5929 struct ufsmount *ump; 5930 ufs_lbn_t lbn; 5931 5932 ump = ITOUMP(ip); 5933 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 5934 ("softdep_setup_allocindir_meta called on non-softdep filesystem")); 5935 CTR3(KTR_SUJ, 5936 "softdep_setup_allocindir_meta: ino %d blkno %jd ptrno %d", 5937 ip->i_number, newblkno, ptrno); 5938 lbn = nbp->b_lblkno; 5939 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta"); 5940 aip = newallocindir(ip, ptrno, newblkno, 0, lbn); 5941 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep); 5942 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 5943 if (setup_allocindir_phase2(bp, ip, inodedep, aip, lbn)) 5944 panic("softdep_setup_allocindir_meta: Block already existed"); 5945 FREE_LOCK(ump); 5946 } 5947 5948 static void 5949 indirdep_complete(indirdep) 5950 struct indirdep *indirdep; 5951 { 5952 struct allocindir *aip; 5953 5954 LIST_REMOVE(indirdep, ir_next); 5955 indirdep->ir_state |= DEPCOMPLETE; 5956 5957 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) { 5958 LIST_REMOVE(aip, ai_next); 5959 free_newblk(&aip->ai_block); 5960 } 5961 /* 5962 * If this indirdep is not attached to a buf it was simply waiting 5963 * on completion to clear completehd. free_indirdep() asserts 5964 * that nothing is dangling. 5965 */ 5966 if ((indirdep->ir_state & ONWORKLIST) == 0) 5967 free_indirdep(indirdep); 5968 } 5969 5970 static struct indirdep * 5971 indirdep_lookup(mp, ip, bp) 5972 struct mount *mp; 5973 struct inode *ip; 5974 struct buf *bp; 5975 { 5976 struct indirdep *indirdep, *newindirdep; 5977 struct newblk *newblk; 5978 struct ufsmount *ump; 5979 struct worklist *wk; 5980 struct fs *fs; 5981 ufs2_daddr_t blkno; 5982 5983 ump = VFSTOUFS(mp); 5984 LOCK_OWNED(ump); 5985 indirdep = NULL; 5986 newindirdep = NULL; 5987 fs = ump->um_fs; 5988 for (;;) { 5989 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 5990 if (wk->wk_type != D_INDIRDEP) 5991 continue; 5992 indirdep = WK_INDIRDEP(wk); 5993 break; 5994 } 5995 /* Found on the buffer worklist, no new structure to free. */ 5996 if (indirdep != NULL && newindirdep == NULL) 5997 return (indirdep); 5998 if (indirdep != NULL && newindirdep != NULL) 5999 panic("indirdep_lookup: simultaneous create"); 6000 /* None found on the buffer and a new structure is ready. */ 6001 if (indirdep == NULL && newindirdep != NULL) 6002 break; 6003 /* None found and no new structure available. */ 6004 FREE_LOCK(ump); 6005 newindirdep = malloc(sizeof(struct indirdep), 6006 M_INDIRDEP, M_SOFTDEP_FLAGS); 6007 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, mp); 6008 newindirdep->ir_state = ATTACHED; 6009 if (I_IS_UFS1(ip)) 6010 newindirdep->ir_state |= UFS1FMT; 6011 TAILQ_INIT(&newindirdep->ir_trunc); 6012 newindirdep->ir_saveddata = NULL; 6013 LIST_INIT(&newindirdep->ir_deplisthd); 6014 LIST_INIT(&newindirdep->ir_donehd); 6015 LIST_INIT(&newindirdep->ir_writehd); 6016 LIST_INIT(&newindirdep->ir_completehd); 6017 if (bp->b_blkno == bp->b_lblkno) { 6018 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp, 6019 NULL, NULL); 6020 bp->b_blkno = blkno; 6021 } 6022 newindirdep->ir_freeblks = NULL; 6023 newindirdep->ir_savebp = 6024 getblk(ump->um_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0); 6025 newindirdep->ir_bp = bp; 6026 BUF_KERNPROC(newindirdep->ir_savebp); 6027 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount); 6028 ACQUIRE_LOCK(ump); 6029 } 6030 indirdep = newindirdep; 6031 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list); 6032 /* 6033 * If the block is not yet allocated we don't set DEPCOMPLETE so 6034 * that we don't free dependencies until the pointers are valid. 6035 * This could search b_dep for D_ALLOCDIRECT/D_ALLOCINDIR rather 6036 * than using the hash. 6037 */ 6038 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk)) 6039 LIST_INSERT_HEAD(&newblk->nb_indirdeps, indirdep, ir_next); 6040 else 6041 indirdep->ir_state |= DEPCOMPLETE; 6042 return (indirdep); 6043 } 6044 6045 /* 6046 * Called to finish the allocation of the "aip" allocated 6047 * by one of the two routines above. 6048 */ 6049 static struct freefrag * 6050 setup_allocindir_phase2(bp, ip, inodedep, aip, lbn) 6051 struct buf *bp; /* in-memory copy of the indirect block */ 6052 struct inode *ip; /* inode for file being extended */ 6053 struct inodedep *inodedep; /* Inodedep for ip */ 6054 struct allocindir *aip; /* allocindir allocated by the above routines */ 6055 ufs_lbn_t lbn; /* Logical block number for this block. */ 6056 { 6057 struct fs *fs; 6058 struct indirdep *indirdep; 6059 struct allocindir *oldaip; 6060 struct freefrag *freefrag; 6061 struct mount *mp; 6062 struct ufsmount *ump; 6063 6064 mp = ITOVFS(ip); 6065 ump = VFSTOUFS(mp); 6066 LOCK_OWNED(ump); 6067 fs = ump->um_fs; 6068 if (bp->b_lblkno >= 0) 6069 panic("setup_allocindir_phase2: not indir blk"); 6070 KASSERT(aip->ai_offset >= 0 && aip->ai_offset < NINDIR(fs), 6071 ("setup_allocindir_phase2: Bad offset %d", aip->ai_offset)); 6072 indirdep = indirdep_lookup(mp, ip, bp); 6073 KASSERT(indirdep->ir_savebp != NULL, 6074 ("setup_allocindir_phase2 NULL ir_savebp")); 6075 aip->ai_indirdep = indirdep; 6076 /* 6077 * Check for an unwritten dependency for this indirect offset. If 6078 * there is, merge the old dependency into the new one. This happens 6079 * as a result of reallocblk only. 6080 */ 6081 freefrag = NULL; 6082 if (aip->ai_oldblkno != 0) { 6083 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) { 6084 if (oldaip->ai_offset == aip->ai_offset) { 6085 freefrag = allocindir_merge(aip, oldaip); 6086 goto done; 6087 } 6088 } 6089 LIST_FOREACH(oldaip, &indirdep->ir_donehd, ai_next) { 6090 if (oldaip->ai_offset == aip->ai_offset) { 6091 freefrag = allocindir_merge(aip, oldaip); 6092 goto done; 6093 } 6094 } 6095 } 6096 done: 6097 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next); 6098 return (freefrag); 6099 } 6100 6101 /* 6102 * Merge two allocindirs which refer to the same block. Move newblock 6103 * dependencies and setup the freefrags appropriately. 6104 */ 6105 static struct freefrag * 6106 allocindir_merge(aip, oldaip) 6107 struct allocindir *aip; 6108 struct allocindir *oldaip; 6109 { 6110 struct freefrag *freefrag; 6111 struct worklist *wk; 6112 6113 if (oldaip->ai_newblkno != aip->ai_oldblkno) 6114 panic("allocindir_merge: blkno"); 6115 aip->ai_oldblkno = oldaip->ai_oldblkno; 6116 freefrag = aip->ai_freefrag; 6117 aip->ai_freefrag = oldaip->ai_freefrag; 6118 oldaip->ai_freefrag = NULL; 6119 KASSERT(freefrag != NULL, ("setup_allocindir_phase2: No freefrag")); 6120 /* 6121 * If we are tracking a new directory-block allocation, 6122 * move it from the old allocindir to the new allocindir. 6123 */ 6124 if ((wk = LIST_FIRST(&oldaip->ai_newdirblk)) != NULL) { 6125 WORKLIST_REMOVE(wk); 6126 if (!LIST_EMPTY(&oldaip->ai_newdirblk)) 6127 panic("allocindir_merge: extra newdirblk"); 6128 WORKLIST_INSERT(&aip->ai_newdirblk, wk); 6129 } 6130 /* 6131 * We can skip journaling for this freefrag and just complete 6132 * any pending journal work for the allocindir that is being 6133 * removed after the freefrag completes. 6134 */ 6135 if (freefrag->ff_jdep) 6136 cancel_jfreefrag(WK_JFREEFRAG(freefrag->ff_jdep)); 6137 LIST_REMOVE(oldaip, ai_next); 6138 freefrag->ff_jdep = (struct worklist *)cancel_newblk(&oldaip->ai_block, 6139 &freefrag->ff_list, &freefrag->ff_jwork); 6140 free_newblk(&oldaip->ai_block); 6141 6142 return (freefrag); 6143 } 6144 6145 static inline void 6146 setup_freedirect(freeblks, ip, i, needj) 6147 struct freeblks *freeblks; 6148 struct inode *ip; 6149 int i; 6150 int needj; 6151 { 6152 struct ufsmount *ump; 6153 ufs2_daddr_t blkno; 6154 int frags; 6155 6156 blkno = DIP(ip, i_db[i]); 6157 if (blkno == 0) 6158 return; 6159 DIP_SET(ip, i_db[i], 0); 6160 ump = ITOUMP(ip); 6161 frags = sblksize(ump->um_fs, ip->i_size, i); 6162 frags = numfrags(ump->um_fs, frags); 6163 newfreework(ump, freeblks, NULL, i, blkno, frags, 0, needj); 6164 } 6165 6166 static inline void 6167 setup_freeext(freeblks, ip, i, needj) 6168 struct freeblks *freeblks; 6169 struct inode *ip; 6170 int i; 6171 int needj; 6172 { 6173 struct ufsmount *ump; 6174 ufs2_daddr_t blkno; 6175 int frags; 6176 6177 blkno = ip->i_din2->di_extb[i]; 6178 if (blkno == 0) 6179 return; 6180 ip->i_din2->di_extb[i] = 0; 6181 ump = ITOUMP(ip); 6182 frags = sblksize(ump->um_fs, ip->i_din2->di_extsize, i); 6183 frags = numfrags(ump->um_fs, frags); 6184 newfreework(ump, freeblks, NULL, -1 - i, blkno, frags, 0, needj); 6185 } 6186 6187 static inline void 6188 setup_freeindir(freeblks, ip, i, lbn, needj) 6189 struct freeblks *freeblks; 6190 struct inode *ip; 6191 int i; 6192 ufs_lbn_t lbn; 6193 int needj; 6194 { 6195 struct ufsmount *ump; 6196 ufs2_daddr_t blkno; 6197 6198 blkno = DIP(ip, i_ib[i]); 6199 if (blkno == 0) 6200 return; 6201 DIP_SET(ip, i_ib[i], 0); 6202 ump = ITOUMP(ip); 6203 newfreework(ump, freeblks, NULL, lbn, blkno, ump->um_fs->fs_frag, 6204 0, needj); 6205 } 6206 6207 static inline struct freeblks * 6208 newfreeblks(mp, ip) 6209 struct mount *mp; 6210 struct inode *ip; 6211 { 6212 struct freeblks *freeblks; 6213 6214 freeblks = malloc(sizeof(struct freeblks), 6215 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO); 6216 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp); 6217 LIST_INIT(&freeblks->fb_jblkdephd); 6218 LIST_INIT(&freeblks->fb_jwork); 6219 freeblks->fb_ref = 0; 6220 freeblks->fb_cgwait = 0; 6221 freeblks->fb_state = ATTACHED; 6222 freeblks->fb_uid = ip->i_uid; 6223 freeblks->fb_inum = ip->i_number; 6224 freeblks->fb_vtype = ITOV(ip)->v_type; 6225 freeblks->fb_modrev = DIP(ip, i_modrev); 6226 freeblks->fb_devvp = ITODEVVP(ip); 6227 freeblks->fb_chkcnt = 0; 6228 freeblks->fb_len = 0; 6229 6230 return (freeblks); 6231 } 6232 6233 static void 6234 trunc_indirdep(indirdep, freeblks, bp, off) 6235 struct indirdep *indirdep; 6236 struct freeblks *freeblks; 6237 struct buf *bp; 6238 int off; 6239 { 6240 struct allocindir *aip, *aipn; 6241 6242 /* 6243 * The first set of allocindirs won't be in savedbp. 6244 */ 6245 LIST_FOREACH_SAFE(aip, &indirdep->ir_deplisthd, ai_next, aipn) 6246 if (aip->ai_offset > off) 6247 cancel_allocindir(aip, bp, freeblks, 1); 6248 LIST_FOREACH_SAFE(aip, &indirdep->ir_donehd, ai_next, aipn) 6249 if (aip->ai_offset > off) 6250 cancel_allocindir(aip, bp, freeblks, 1); 6251 /* 6252 * These will exist in savedbp. 6253 */ 6254 LIST_FOREACH_SAFE(aip, &indirdep->ir_writehd, ai_next, aipn) 6255 if (aip->ai_offset > off) 6256 cancel_allocindir(aip, NULL, freeblks, 0); 6257 LIST_FOREACH_SAFE(aip, &indirdep->ir_completehd, ai_next, aipn) 6258 if (aip->ai_offset > off) 6259 cancel_allocindir(aip, NULL, freeblks, 0); 6260 } 6261 6262 /* 6263 * Follow the chain of indirects down to lastlbn creating a freework 6264 * structure for each. This will be used to start indir_trunc() at 6265 * the right offset and create the journal records for the parrtial 6266 * truncation. A second step will handle the truncated dependencies. 6267 */ 6268 static int 6269 setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno) 6270 struct freeblks *freeblks; 6271 struct inode *ip; 6272 ufs_lbn_t lbn; 6273 ufs_lbn_t lastlbn; 6274 ufs2_daddr_t blkno; 6275 { 6276 struct indirdep *indirdep; 6277 struct indirdep *indirn; 6278 struct freework *freework; 6279 struct newblk *newblk; 6280 struct mount *mp; 6281 struct ufsmount *ump; 6282 struct buf *bp; 6283 uint8_t *start; 6284 uint8_t *end; 6285 ufs_lbn_t lbnadd; 6286 int level; 6287 int error; 6288 int off; 6289 6290 6291 freework = NULL; 6292 if (blkno == 0) 6293 return (0); 6294 mp = freeblks->fb_list.wk_mp; 6295 ump = VFSTOUFS(mp); 6296 /* 6297 * Here, calls to VOP_BMAP() will fail. However, we already have 6298 * the on-disk address, so we just pass it to bread() instead of 6299 * having bread() attempt to calculate it using VOP_BMAP(). 6300 */ 6301 error = breadn_flags(ITOV(ip), lbn, blkptrtodb(ump, blkno), 6302 (int)mp->mnt_stat.f_iosize, NULL, NULL, 0, NOCRED, 0, NULL, &bp); 6303 if (error) 6304 return (error); 6305 level = lbn_level(lbn); 6306 lbnadd = lbn_offset(ump->um_fs, level); 6307 /* 6308 * Compute the offset of the last block we want to keep. Store 6309 * in the freework the first block we want to completely free. 6310 */ 6311 off = (lastlbn - -(lbn + level)) / lbnadd; 6312 if (off + 1 == NINDIR(ump->um_fs)) 6313 goto nowork; 6314 freework = newfreework(ump, freeblks, NULL, lbn, blkno, 0, off + 1, 0); 6315 /* 6316 * Link the freework into the indirdep. This will prevent any new 6317 * allocations from proceeding until we are finished with the 6318 * truncate and the block is written. 6319 */ 6320 ACQUIRE_LOCK(ump); 6321 indirdep = indirdep_lookup(mp, ip, bp); 6322 if (indirdep->ir_freeblks) 6323 panic("setup_trunc_indir: indirdep already truncated."); 6324 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, freework, fw_next); 6325 freework->fw_indir = indirdep; 6326 /* 6327 * Cancel any allocindirs that will not make it to disk. 6328 * We have to do this for all copies of the indirdep that 6329 * live on this newblk. 6330 */ 6331 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 6332 if (newblk_lookup(mp, dbtofsb(ump->um_fs, bp->b_blkno), 0, 6333 &newblk) == 0) 6334 panic("setup_trunc_indir: lost block"); 6335 LIST_FOREACH(indirn, &newblk->nb_indirdeps, ir_next) 6336 trunc_indirdep(indirn, freeblks, bp, off); 6337 } else 6338 trunc_indirdep(indirdep, freeblks, bp, off); 6339 FREE_LOCK(ump); 6340 /* 6341 * Creation is protected by the buf lock. The saveddata is only 6342 * needed if a full truncation follows a partial truncation but it 6343 * is difficult to allocate in that case so we fetch it anyway. 6344 */ 6345 if (indirdep->ir_saveddata == NULL) 6346 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 6347 M_SOFTDEP_FLAGS); 6348 nowork: 6349 /* Fetch the blkno of the child and the zero start offset. */ 6350 if (I_IS_UFS1(ip)) { 6351 blkno = ((ufs1_daddr_t *)bp->b_data)[off]; 6352 start = (uint8_t *)&((ufs1_daddr_t *)bp->b_data)[off+1]; 6353 } else { 6354 blkno = ((ufs2_daddr_t *)bp->b_data)[off]; 6355 start = (uint8_t *)&((ufs2_daddr_t *)bp->b_data)[off+1]; 6356 } 6357 if (freework) { 6358 /* Zero the truncated pointers. */ 6359 end = bp->b_data + bp->b_bcount; 6360 bzero(start, end - start); 6361 bdwrite(bp); 6362 } else 6363 bqrelse(bp); 6364 if (level == 0) 6365 return (0); 6366 lbn++; /* adjust level */ 6367 lbn -= (off * lbnadd); 6368 return setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno); 6369 } 6370 6371 /* 6372 * Complete the partial truncation of an indirect block setup by 6373 * setup_trunc_indir(). This zeros the truncated pointers in the saved 6374 * copy and writes them to disk before the freeblks is allowed to complete. 6375 */ 6376 static void 6377 complete_trunc_indir(freework) 6378 struct freework *freework; 6379 { 6380 struct freework *fwn; 6381 struct indirdep *indirdep; 6382 struct ufsmount *ump; 6383 struct buf *bp; 6384 uintptr_t start; 6385 int count; 6386 6387 ump = VFSTOUFS(freework->fw_list.wk_mp); 6388 LOCK_OWNED(ump); 6389 indirdep = freework->fw_indir; 6390 for (;;) { 6391 bp = indirdep->ir_bp; 6392 /* See if the block was discarded. */ 6393 if (bp == NULL) 6394 break; 6395 /* Inline part of getdirtybuf(). We dont want bremfree. */ 6396 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) 6397 break; 6398 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 6399 LOCK_PTR(ump)) == 0) 6400 BUF_UNLOCK(bp); 6401 ACQUIRE_LOCK(ump); 6402 } 6403 freework->fw_state |= DEPCOMPLETE; 6404 TAILQ_REMOVE(&indirdep->ir_trunc, freework, fw_next); 6405 /* 6406 * Zero the pointers in the saved copy. 6407 */ 6408 if (indirdep->ir_state & UFS1FMT) 6409 start = sizeof(ufs1_daddr_t); 6410 else 6411 start = sizeof(ufs2_daddr_t); 6412 start *= freework->fw_start; 6413 count = indirdep->ir_savebp->b_bcount - start; 6414 start += (uintptr_t)indirdep->ir_savebp->b_data; 6415 bzero((char *)start, count); 6416 /* 6417 * We need to start the next truncation in the list if it has not 6418 * been started yet. 6419 */ 6420 fwn = TAILQ_FIRST(&indirdep->ir_trunc); 6421 if (fwn != NULL) { 6422 if (fwn->fw_freeblks == indirdep->ir_freeblks) 6423 TAILQ_REMOVE(&indirdep->ir_trunc, fwn, fw_next); 6424 if ((fwn->fw_state & ONWORKLIST) == 0) 6425 freework_enqueue(fwn); 6426 } 6427 /* 6428 * If bp is NULL the block was fully truncated, restore 6429 * the saved block list otherwise free it if it is no 6430 * longer needed. 6431 */ 6432 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 6433 if (bp == NULL) 6434 bcopy(indirdep->ir_saveddata, 6435 indirdep->ir_savebp->b_data, 6436 indirdep->ir_savebp->b_bcount); 6437 free(indirdep->ir_saveddata, M_INDIRDEP); 6438 indirdep->ir_saveddata = NULL; 6439 } 6440 /* 6441 * When bp is NULL there is a full truncation pending. We 6442 * must wait for this full truncation to be journaled before 6443 * we can release this freework because the disk pointers will 6444 * never be written as zero. 6445 */ 6446 if (bp == NULL) { 6447 if (LIST_EMPTY(&indirdep->ir_freeblks->fb_jblkdephd)) 6448 handle_written_freework(freework); 6449 else 6450 WORKLIST_INSERT(&indirdep->ir_freeblks->fb_freeworkhd, 6451 &freework->fw_list); 6452 } else { 6453 /* Complete when the real copy is written. */ 6454 WORKLIST_INSERT(&bp->b_dep, &freework->fw_list); 6455 BUF_UNLOCK(bp); 6456 } 6457 } 6458 6459 /* 6460 * Calculate the number of blocks we are going to release where datablocks 6461 * is the current total and length is the new file size. 6462 */ 6463 static ufs2_daddr_t 6464 blkcount(fs, datablocks, length) 6465 struct fs *fs; 6466 ufs2_daddr_t datablocks; 6467 off_t length; 6468 { 6469 off_t totblks, numblks; 6470 6471 totblks = 0; 6472 numblks = howmany(length, fs->fs_bsize); 6473 if (numblks <= UFS_NDADDR) { 6474 totblks = howmany(length, fs->fs_fsize); 6475 goto out; 6476 } 6477 totblks = blkstofrags(fs, numblks); 6478 numblks -= UFS_NDADDR; 6479 /* 6480 * Count all single, then double, then triple indirects required. 6481 * Subtracting one indirects worth of blocks for each pass 6482 * acknowledges one of each pointed to by the inode. 6483 */ 6484 for (;;) { 6485 totblks += blkstofrags(fs, howmany(numblks, NINDIR(fs))); 6486 numblks -= NINDIR(fs); 6487 if (numblks <= 0) 6488 break; 6489 numblks = howmany(numblks, NINDIR(fs)); 6490 } 6491 out: 6492 totblks = fsbtodb(fs, totblks); 6493 /* 6494 * Handle sparse files. We can't reclaim more blocks than the inode 6495 * references. We will correct it later in handle_complete_freeblks() 6496 * when we know the real count. 6497 */ 6498 if (totblks > datablocks) 6499 return (0); 6500 return (datablocks - totblks); 6501 } 6502 6503 /* 6504 * Handle freeblocks for journaled softupdate filesystems. 6505 * 6506 * Contrary to normal softupdates, we must preserve the block pointers in 6507 * indirects until their subordinates are free. This is to avoid journaling 6508 * every block that is freed which may consume more space than the journal 6509 * itself. The recovery program will see the free block journals at the 6510 * base of the truncated area and traverse them to reclaim space. The 6511 * pointers in the inode may be cleared immediately after the journal 6512 * records are written because each direct and indirect pointer in the 6513 * inode is recorded in a journal. This permits full truncation to proceed 6514 * asynchronously. The write order is journal -> inode -> cgs -> indirects. 6515 * 6516 * The algorithm is as follows: 6517 * 1) Traverse the in-memory state and create journal entries to release 6518 * the relevant blocks and full indirect trees. 6519 * 2) Traverse the indirect block chain adding partial truncation freework 6520 * records to indirects in the path to lastlbn. The freework will 6521 * prevent new allocation dependencies from being satisfied in this 6522 * indirect until the truncation completes. 6523 * 3) Read and lock the inode block, performing an update with the new size 6524 * and pointers. This prevents truncated data from becoming valid on 6525 * disk through step 4. 6526 * 4) Reap unsatisfied dependencies that are beyond the truncated area, 6527 * eliminate journal work for those records that do not require it. 6528 * 5) Schedule the journal records to be written followed by the inode block. 6529 * 6) Allocate any necessary frags for the end of file. 6530 * 7) Zero any partially truncated blocks. 6531 * 6532 * From this truncation proceeds asynchronously using the freework and 6533 * indir_trunc machinery. The file will not be extended again into a 6534 * partially truncated indirect block until all work is completed but 6535 * the normal dependency mechanism ensures that it is rolled back/forward 6536 * as appropriate. Further truncation may occur without delay and is 6537 * serialized in indir_trunc(). 6538 */ 6539 void 6540 softdep_journal_freeblocks(ip, cred, length, flags) 6541 struct inode *ip; /* The inode whose length is to be reduced */ 6542 struct ucred *cred; 6543 off_t length; /* The new length for the file */ 6544 int flags; /* IO_EXT and/or IO_NORMAL */ 6545 { 6546 struct freeblks *freeblks, *fbn; 6547 struct worklist *wk, *wkn; 6548 struct inodedep *inodedep; 6549 struct jblkdep *jblkdep; 6550 struct allocdirect *adp, *adpn; 6551 struct ufsmount *ump; 6552 struct fs *fs; 6553 struct buf *bp; 6554 struct vnode *vp; 6555 struct mount *mp; 6556 ufs2_daddr_t extblocks, datablocks; 6557 ufs_lbn_t tmpval, lbn, lastlbn; 6558 int frags, lastoff, iboff, allocblock, needj, error, i; 6559 6560 ump = ITOUMP(ip); 6561 mp = UFSTOVFS(ump); 6562 fs = ump->um_fs; 6563 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6564 ("softdep_journal_freeblocks called on non-softdep filesystem")); 6565 vp = ITOV(ip); 6566 needj = 1; 6567 iboff = -1; 6568 allocblock = 0; 6569 extblocks = 0; 6570 datablocks = 0; 6571 frags = 0; 6572 freeblks = newfreeblks(mp, ip); 6573 ACQUIRE_LOCK(ump); 6574 /* 6575 * If we're truncating a removed file that will never be written 6576 * we don't need to journal the block frees. The canceled journals 6577 * for the allocations will suffice. 6578 */ 6579 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6580 if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED && 6581 length == 0) 6582 needj = 0; 6583 CTR3(KTR_SUJ, "softdep_journal_freeblks: ip %d length %ld needj %d", 6584 ip->i_number, length, needj); 6585 FREE_LOCK(ump); 6586 /* 6587 * Calculate the lbn that we are truncating to. This results in -1 6588 * if we're truncating the 0 bytes. So it is the last lbn we want 6589 * to keep, not the first lbn we want to truncate. 6590 */ 6591 lastlbn = lblkno(fs, length + fs->fs_bsize - 1) - 1; 6592 lastoff = blkoff(fs, length); 6593 /* 6594 * Compute frags we are keeping in lastlbn. 0 means all. 6595 */ 6596 if (lastlbn >= 0 && lastlbn < UFS_NDADDR) { 6597 frags = fragroundup(fs, lastoff); 6598 /* adp offset of last valid allocdirect. */ 6599 iboff = lastlbn; 6600 } else if (lastlbn > 0) 6601 iboff = UFS_NDADDR; 6602 if (fs->fs_magic == FS_UFS2_MAGIC) 6603 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 6604 /* 6605 * Handle normal data blocks and indirects. This section saves 6606 * values used after the inode update to complete frag and indirect 6607 * truncation. 6608 */ 6609 if ((flags & IO_NORMAL) != 0) { 6610 /* 6611 * Handle truncation of whole direct and indirect blocks. 6612 */ 6613 for (i = iboff + 1; i < UFS_NDADDR; i++) 6614 setup_freedirect(freeblks, ip, i, needj); 6615 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR; 6616 i < UFS_NIADDR; 6617 i++, lbn += tmpval, tmpval *= NINDIR(fs)) { 6618 /* Release a whole indirect tree. */ 6619 if (lbn > lastlbn) { 6620 setup_freeindir(freeblks, ip, i, -lbn -i, 6621 needj); 6622 continue; 6623 } 6624 iboff = i + UFS_NDADDR; 6625 /* 6626 * Traverse partially truncated indirect tree. 6627 */ 6628 if (lbn <= lastlbn && lbn + tmpval - 1 > lastlbn) 6629 setup_trunc_indir(freeblks, ip, -lbn - i, 6630 lastlbn, DIP(ip, i_ib[i])); 6631 } 6632 /* 6633 * Handle partial truncation to a frag boundary. 6634 */ 6635 if (frags) { 6636 ufs2_daddr_t blkno; 6637 long oldfrags; 6638 6639 oldfrags = blksize(fs, ip, lastlbn); 6640 blkno = DIP(ip, i_db[lastlbn]); 6641 if (blkno && oldfrags != frags) { 6642 oldfrags -= frags; 6643 oldfrags = numfrags(fs, oldfrags); 6644 blkno += numfrags(fs, frags); 6645 newfreework(ump, freeblks, NULL, lastlbn, 6646 blkno, oldfrags, 0, needj); 6647 if (needj) 6648 adjust_newfreework(freeblks, 6649 numfrags(fs, frags)); 6650 } else if (blkno == 0) 6651 allocblock = 1; 6652 } 6653 /* 6654 * Add a journal record for partial truncate if we are 6655 * handling indirect blocks. Non-indirects need no extra 6656 * journaling. 6657 */ 6658 if (length != 0 && lastlbn >= UFS_NDADDR) { 6659 ip->i_flag |= IN_TRUNCATED; 6660 newjtrunc(freeblks, length, 0); 6661 } 6662 ip->i_size = length; 6663 DIP_SET(ip, i_size, ip->i_size); 6664 datablocks = DIP(ip, i_blocks) - extblocks; 6665 if (length != 0) 6666 datablocks = blkcount(fs, datablocks, length); 6667 freeblks->fb_len = length; 6668 } 6669 if ((flags & IO_EXT) != 0) { 6670 for (i = 0; i < UFS_NXADDR; i++) 6671 setup_freeext(freeblks, ip, i, needj); 6672 ip->i_din2->di_extsize = 0; 6673 datablocks += extblocks; 6674 } 6675 #ifdef QUOTA 6676 /* Reference the quotas in case the block count is wrong in the end. */ 6677 quotaref(vp, freeblks->fb_quota); 6678 (void) chkdq(ip, -datablocks, NOCRED, FORCE); 6679 #endif 6680 freeblks->fb_chkcnt = -datablocks; 6681 UFS_LOCK(ump); 6682 fs->fs_pendingblocks += datablocks; 6683 UFS_UNLOCK(ump); 6684 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 6685 /* 6686 * Handle truncation of incomplete alloc direct dependencies. We 6687 * hold the inode block locked to prevent incomplete dependencies 6688 * from reaching the disk while we are eliminating those that 6689 * have been truncated. This is a partially inlined ffs_update(). 6690 */ 6691 ufs_itimes(vp); 6692 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED); 6693 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 6694 (int)fs->fs_bsize, cred, &bp); 6695 if (error) { 6696 softdep_error("softdep_journal_freeblocks", error); 6697 return; 6698 } 6699 if (bp->b_bufsize == fs->fs_bsize) 6700 bp->b_flags |= B_CLUSTEROK; 6701 softdep_update_inodeblock(ip, bp, 0); 6702 if (ump->um_fstype == UFS1) { 6703 *((struct ufs1_dinode *)bp->b_data + 6704 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1; 6705 } else { 6706 ffs_update_dinode_ckhash(fs, ip->i_din2); 6707 *((struct ufs2_dinode *)bp->b_data + 6708 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2; 6709 } 6710 ACQUIRE_LOCK(ump); 6711 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6712 if ((inodedep->id_state & IOSTARTED) != 0) 6713 panic("softdep_setup_freeblocks: inode busy"); 6714 /* 6715 * Add the freeblks structure to the list of operations that 6716 * must await the zero'ed inode being written to disk. If we 6717 * still have a bitmap dependency (needj), then the inode 6718 * has never been written to disk, so we can process the 6719 * freeblks below once we have deleted the dependencies. 6720 */ 6721 if (needj) 6722 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 6723 else 6724 freeblks->fb_state |= COMPLETE; 6725 if ((flags & IO_NORMAL) != 0) { 6726 TAILQ_FOREACH_SAFE(adp, &inodedep->id_inoupdt, ad_next, adpn) { 6727 if (adp->ad_offset > iboff) 6728 cancel_allocdirect(&inodedep->id_inoupdt, adp, 6729 freeblks); 6730 /* 6731 * Truncate the allocdirect. We could eliminate 6732 * or modify journal records as well. 6733 */ 6734 else if (adp->ad_offset == iboff && frags) 6735 adp->ad_newsize = frags; 6736 } 6737 } 6738 if ((flags & IO_EXT) != 0) 6739 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 6740 cancel_allocdirect(&inodedep->id_extupdt, adp, 6741 freeblks); 6742 /* 6743 * Scan the bufwait list for newblock dependencies that will never 6744 * make it to disk. 6745 */ 6746 LIST_FOREACH_SAFE(wk, &inodedep->id_bufwait, wk_list, wkn) { 6747 if (wk->wk_type != D_ALLOCDIRECT) 6748 continue; 6749 adp = WK_ALLOCDIRECT(wk); 6750 if (((flags & IO_NORMAL) != 0 && (adp->ad_offset > iboff)) || 6751 ((flags & IO_EXT) != 0 && (adp->ad_state & EXTDATA))) { 6752 cancel_jfreeblk(freeblks, adp->ad_newblkno); 6753 cancel_newblk(WK_NEWBLK(wk), NULL, &freeblks->fb_jwork); 6754 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 6755 } 6756 } 6757 /* 6758 * Add journal work. 6759 */ 6760 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) 6761 add_to_journal(&jblkdep->jb_list); 6762 FREE_LOCK(ump); 6763 bdwrite(bp); 6764 /* 6765 * Truncate dependency structures beyond length. 6766 */ 6767 trunc_dependencies(ip, freeblks, lastlbn, frags, flags); 6768 /* 6769 * This is only set when we need to allocate a fragment because 6770 * none existed at the end of a frag-sized file. It handles only 6771 * allocating a new, zero filled block. 6772 */ 6773 if (allocblock) { 6774 ip->i_size = length - lastoff; 6775 DIP_SET(ip, i_size, ip->i_size); 6776 error = UFS_BALLOC(vp, length - 1, 1, cred, BA_CLRBUF, &bp); 6777 if (error != 0) { 6778 softdep_error("softdep_journal_freeblks", error); 6779 return; 6780 } 6781 ip->i_size = length; 6782 DIP_SET(ip, i_size, length); 6783 ip->i_flag |= IN_CHANGE | IN_UPDATE; 6784 allocbuf(bp, frags); 6785 ffs_update(vp, 0); 6786 bawrite(bp); 6787 } else if (lastoff != 0 && vp->v_type != VDIR) { 6788 int size; 6789 6790 /* 6791 * Zero the end of a truncated frag or block. 6792 */ 6793 size = sblksize(fs, length, lastlbn); 6794 error = bread(vp, lastlbn, size, cred, &bp); 6795 if (error) { 6796 softdep_error("softdep_journal_freeblks", error); 6797 return; 6798 } 6799 bzero((char *)bp->b_data + lastoff, size - lastoff); 6800 bawrite(bp); 6801 6802 } 6803 ACQUIRE_LOCK(ump); 6804 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6805 TAILQ_INSERT_TAIL(&inodedep->id_freeblklst, freeblks, fb_next); 6806 freeblks->fb_state |= DEPCOMPLETE | ONDEPLIST; 6807 /* 6808 * We zero earlier truncations so they don't erroneously 6809 * update i_blocks. 6810 */ 6811 if (freeblks->fb_len == 0 && (flags & IO_NORMAL) != 0) 6812 TAILQ_FOREACH(fbn, &inodedep->id_freeblklst, fb_next) 6813 fbn->fb_len = 0; 6814 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE && 6815 LIST_EMPTY(&freeblks->fb_jblkdephd)) 6816 freeblks->fb_state |= INPROGRESS; 6817 else 6818 freeblks = NULL; 6819 FREE_LOCK(ump); 6820 if (freeblks) 6821 handle_workitem_freeblocks(freeblks, 0); 6822 trunc_pages(ip, length, extblocks, flags); 6823 6824 } 6825 6826 /* 6827 * Flush a JOP_SYNC to the journal. 6828 */ 6829 void 6830 softdep_journal_fsync(ip) 6831 struct inode *ip; 6832 { 6833 struct jfsync *jfsync; 6834 struct ufsmount *ump; 6835 6836 ump = ITOUMP(ip); 6837 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 6838 ("softdep_journal_fsync called on non-softdep filesystem")); 6839 if ((ip->i_flag & IN_TRUNCATED) == 0) 6840 return; 6841 ip->i_flag &= ~IN_TRUNCATED; 6842 jfsync = malloc(sizeof(*jfsync), M_JFSYNC, M_SOFTDEP_FLAGS | M_ZERO); 6843 workitem_alloc(&jfsync->jfs_list, D_JFSYNC, UFSTOVFS(ump)); 6844 jfsync->jfs_size = ip->i_size; 6845 jfsync->jfs_ino = ip->i_number; 6846 ACQUIRE_LOCK(ump); 6847 add_to_journal(&jfsync->jfs_list); 6848 jwait(&jfsync->jfs_list, MNT_WAIT); 6849 FREE_LOCK(ump); 6850 } 6851 6852 /* 6853 * Block de-allocation dependencies. 6854 * 6855 * When blocks are de-allocated, the on-disk pointers must be nullified before 6856 * the blocks are made available for use by other files. (The true 6857 * requirement is that old pointers must be nullified before new on-disk 6858 * pointers are set. We chose this slightly more stringent requirement to 6859 * reduce complexity.) Our implementation handles this dependency by updating 6860 * the inode (or indirect block) appropriately but delaying the actual block 6861 * de-allocation (i.e., freemap and free space count manipulation) until 6862 * after the updated versions reach stable storage. After the disk is 6863 * updated, the blocks can be safely de-allocated whenever it is convenient. 6864 * This implementation handles only the common case of reducing a file's 6865 * length to zero. Other cases are handled by the conventional synchronous 6866 * write approach. 6867 * 6868 * The ffs implementation with which we worked double-checks 6869 * the state of the block pointers and file size as it reduces 6870 * a file's length. Some of this code is replicated here in our 6871 * soft updates implementation. The freeblks->fb_chkcnt field is 6872 * used to transfer a part of this information to the procedure 6873 * that eventually de-allocates the blocks. 6874 * 6875 * This routine should be called from the routine that shortens 6876 * a file's length, before the inode's size or block pointers 6877 * are modified. It will save the block pointer information for 6878 * later release and zero the inode so that the calling routine 6879 * can release it. 6880 */ 6881 void 6882 softdep_setup_freeblocks(ip, length, flags) 6883 struct inode *ip; /* The inode whose length is to be reduced */ 6884 off_t length; /* The new length for the file */ 6885 int flags; /* IO_EXT and/or IO_NORMAL */ 6886 { 6887 struct ufs1_dinode *dp1; 6888 struct ufs2_dinode *dp2; 6889 struct freeblks *freeblks; 6890 struct inodedep *inodedep; 6891 struct allocdirect *adp; 6892 struct ufsmount *ump; 6893 struct buf *bp; 6894 struct fs *fs; 6895 ufs2_daddr_t extblocks, datablocks; 6896 struct mount *mp; 6897 int i, delay, error; 6898 ufs_lbn_t tmpval; 6899 ufs_lbn_t lbn; 6900 6901 ump = ITOUMP(ip); 6902 mp = UFSTOVFS(ump); 6903 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6904 ("softdep_setup_freeblocks called on non-softdep filesystem")); 6905 CTR2(KTR_SUJ, "softdep_setup_freeblks: ip %d length %ld", 6906 ip->i_number, length); 6907 KASSERT(length == 0, ("softdep_setup_freeblocks: non-zero length")); 6908 fs = ump->um_fs; 6909 if ((error = bread(ump->um_devvp, 6910 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 6911 (int)fs->fs_bsize, NOCRED, &bp)) != 0) { 6912 brelse(bp); 6913 softdep_error("softdep_setup_freeblocks", error); 6914 return; 6915 } 6916 freeblks = newfreeblks(mp, ip); 6917 extblocks = 0; 6918 datablocks = 0; 6919 if (fs->fs_magic == FS_UFS2_MAGIC) 6920 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 6921 if ((flags & IO_NORMAL) != 0) { 6922 for (i = 0; i < UFS_NDADDR; i++) 6923 setup_freedirect(freeblks, ip, i, 0); 6924 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR; 6925 i < UFS_NIADDR; 6926 i++, lbn += tmpval, tmpval *= NINDIR(fs)) 6927 setup_freeindir(freeblks, ip, i, -lbn -i, 0); 6928 ip->i_size = 0; 6929 DIP_SET(ip, i_size, 0); 6930 datablocks = DIP(ip, i_blocks) - extblocks; 6931 } 6932 if ((flags & IO_EXT) != 0) { 6933 for (i = 0; i < UFS_NXADDR; i++) 6934 setup_freeext(freeblks, ip, i, 0); 6935 ip->i_din2->di_extsize = 0; 6936 datablocks += extblocks; 6937 } 6938 #ifdef QUOTA 6939 /* Reference the quotas in case the block count is wrong in the end. */ 6940 quotaref(ITOV(ip), freeblks->fb_quota); 6941 (void) chkdq(ip, -datablocks, NOCRED, FORCE); 6942 #endif 6943 freeblks->fb_chkcnt = -datablocks; 6944 UFS_LOCK(ump); 6945 fs->fs_pendingblocks += datablocks; 6946 UFS_UNLOCK(ump); 6947 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 6948 /* 6949 * Push the zero'ed inode to its disk buffer so that we are free 6950 * to delete its dependencies below. Once the dependencies are gone 6951 * the buffer can be safely released. 6952 */ 6953 if (ump->um_fstype == UFS1) { 6954 dp1 = ((struct ufs1_dinode *)bp->b_data + 6955 ino_to_fsbo(fs, ip->i_number)); 6956 ip->i_din1->di_freelink = dp1->di_freelink; 6957 *dp1 = *ip->i_din1; 6958 } else { 6959 dp2 = ((struct ufs2_dinode *)bp->b_data + 6960 ino_to_fsbo(fs, ip->i_number)); 6961 ip->i_din2->di_freelink = dp2->di_freelink; 6962 ffs_update_dinode_ckhash(fs, ip->i_din2); 6963 *dp2 = *ip->i_din2; 6964 } 6965 /* 6966 * Find and eliminate any inode dependencies. 6967 */ 6968 ACQUIRE_LOCK(ump); 6969 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6970 if ((inodedep->id_state & IOSTARTED) != 0) 6971 panic("softdep_setup_freeblocks: inode busy"); 6972 /* 6973 * Add the freeblks structure to the list of operations that 6974 * must await the zero'ed inode being written to disk. If we 6975 * still have a bitmap dependency (delay == 0), then the inode 6976 * has never been written to disk, so we can process the 6977 * freeblks below once we have deleted the dependencies. 6978 */ 6979 delay = (inodedep->id_state & DEPCOMPLETE); 6980 if (delay) 6981 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 6982 else 6983 freeblks->fb_state |= COMPLETE; 6984 /* 6985 * Because the file length has been truncated to zero, any 6986 * pending block allocation dependency structures associated 6987 * with this inode are obsolete and can simply be de-allocated. 6988 * We must first merge the two dependency lists to get rid of 6989 * any duplicate freefrag structures, then purge the merged list. 6990 * If we still have a bitmap dependency, then the inode has never 6991 * been written to disk, so we can free any fragments without delay. 6992 */ 6993 if (flags & IO_NORMAL) { 6994 merge_inode_lists(&inodedep->id_newinoupdt, 6995 &inodedep->id_inoupdt); 6996 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 6997 cancel_allocdirect(&inodedep->id_inoupdt, adp, 6998 freeblks); 6999 } 7000 if (flags & IO_EXT) { 7001 merge_inode_lists(&inodedep->id_newextupdt, 7002 &inodedep->id_extupdt); 7003 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 7004 cancel_allocdirect(&inodedep->id_extupdt, adp, 7005 freeblks); 7006 } 7007 FREE_LOCK(ump); 7008 bdwrite(bp); 7009 trunc_dependencies(ip, freeblks, -1, 0, flags); 7010 ACQUIRE_LOCK(ump); 7011 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 7012 (void) free_inodedep(inodedep); 7013 freeblks->fb_state |= DEPCOMPLETE; 7014 /* 7015 * If the inode with zeroed block pointers is now on disk 7016 * we can start freeing blocks. 7017 */ 7018 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 7019 freeblks->fb_state |= INPROGRESS; 7020 else 7021 freeblks = NULL; 7022 FREE_LOCK(ump); 7023 if (freeblks) 7024 handle_workitem_freeblocks(freeblks, 0); 7025 trunc_pages(ip, length, extblocks, flags); 7026 } 7027 7028 /* 7029 * Eliminate pages from the page cache that back parts of this inode and 7030 * adjust the vnode pager's idea of our size. This prevents stale data 7031 * from hanging around in the page cache. 7032 */ 7033 static void 7034 trunc_pages(ip, length, extblocks, flags) 7035 struct inode *ip; 7036 off_t length; 7037 ufs2_daddr_t extblocks; 7038 int flags; 7039 { 7040 struct vnode *vp; 7041 struct fs *fs; 7042 ufs_lbn_t lbn; 7043 off_t end, extend; 7044 7045 vp = ITOV(ip); 7046 fs = ITOFS(ip); 7047 extend = OFF_TO_IDX(lblktosize(fs, -extblocks)); 7048 if ((flags & IO_EXT) != 0) 7049 vn_pages_remove(vp, extend, 0); 7050 if ((flags & IO_NORMAL) == 0) 7051 return; 7052 BO_LOCK(&vp->v_bufobj); 7053 drain_output(vp); 7054 BO_UNLOCK(&vp->v_bufobj); 7055 /* 7056 * The vnode pager eliminates file pages we eliminate indirects 7057 * below. 7058 */ 7059 vnode_pager_setsize(vp, length); 7060 /* 7061 * Calculate the end based on the last indirect we want to keep. If 7062 * the block extends into indirects we can just use the negative of 7063 * its lbn. Doubles and triples exist at lower numbers so we must 7064 * be careful not to remove those, if they exist. double and triple 7065 * indirect lbns do not overlap with others so it is not important 7066 * to verify how many levels are required. 7067 */ 7068 lbn = lblkno(fs, length); 7069 if (lbn >= UFS_NDADDR) { 7070 /* Calculate the virtual lbn of the triple indirect. */ 7071 lbn = -lbn - (UFS_NIADDR - 1); 7072 end = OFF_TO_IDX(lblktosize(fs, lbn)); 7073 } else 7074 end = extend; 7075 vn_pages_remove(vp, OFF_TO_IDX(OFF_MAX), end); 7076 } 7077 7078 /* 7079 * See if the buf bp is in the range eliminated by truncation. 7080 */ 7081 static int 7082 trunc_check_buf(bp, blkoffp, lastlbn, lastoff, flags) 7083 struct buf *bp; 7084 int *blkoffp; 7085 ufs_lbn_t lastlbn; 7086 int lastoff; 7087 int flags; 7088 { 7089 ufs_lbn_t lbn; 7090 7091 *blkoffp = 0; 7092 /* Only match ext/normal blocks as appropriate. */ 7093 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) || 7094 ((flags & IO_NORMAL) == 0 && (bp->b_xflags & BX_ALTDATA) == 0)) 7095 return (0); 7096 /* ALTDATA is always a full truncation. */ 7097 if ((bp->b_xflags & BX_ALTDATA) != 0) 7098 return (1); 7099 /* -1 is full truncation. */ 7100 if (lastlbn == -1) 7101 return (1); 7102 /* 7103 * If this is a partial truncate we only want those 7104 * blocks and indirect blocks that cover the range 7105 * we're after. 7106 */ 7107 lbn = bp->b_lblkno; 7108 if (lbn < 0) 7109 lbn = -(lbn + lbn_level(lbn)); 7110 if (lbn < lastlbn) 7111 return (0); 7112 /* Here we only truncate lblkno if it's partial. */ 7113 if (lbn == lastlbn) { 7114 if (lastoff == 0) 7115 return (0); 7116 *blkoffp = lastoff; 7117 } 7118 return (1); 7119 } 7120 7121 /* 7122 * Eliminate any dependencies that exist in memory beyond lblkno:off 7123 */ 7124 static void 7125 trunc_dependencies(ip, freeblks, lastlbn, lastoff, flags) 7126 struct inode *ip; 7127 struct freeblks *freeblks; 7128 ufs_lbn_t lastlbn; 7129 int lastoff; 7130 int flags; 7131 { 7132 struct bufobj *bo; 7133 struct vnode *vp; 7134 struct buf *bp; 7135 int blkoff; 7136 7137 /* 7138 * We must wait for any I/O in progress to finish so that 7139 * all potential buffers on the dirty list will be visible. 7140 * Once they are all there, walk the list and get rid of 7141 * any dependencies. 7142 */ 7143 vp = ITOV(ip); 7144 bo = &vp->v_bufobj; 7145 BO_LOCK(bo); 7146 drain_output(vp); 7147 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) 7148 bp->b_vflags &= ~BV_SCANNED; 7149 restart: 7150 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) { 7151 if (bp->b_vflags & BV_SCANNED) 7152 continue; 7153 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 7154 bp->b_vflags |= BV_SCANNED; 7155 continue; 7156 } 7157 KASSERT(bp->b_bufobj == bo, ("Wrong object in buffer")); 7158 if ((bp = getdirtybuf(bp, BO_LOCKPTR(bo), MNT_WAIT)) == NULL) 7159 goto restart; 7160 BO_UNLOCK(bo); 7161 if (deallocate_dependencies(bp, freeblks, blkoff)) 7162 bqrelse(bp); 7163 else 7164 brelse(bp); 7165 BO_LOCK(bo); 7166 goto restart; 7167 } 7168 /* 7169 * Now do the work of vtruncbuf while also matching indirect blocks. 7170 */ 7171 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) 7172 bp->b_vflags &= ~BV_SCANNED; 7173 cleanrestart: 7174 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) { 7175 if (bp->b_vflags & BV_SCANNED) 7176 continue; 7177 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 7178 bp->b_vflags |= BV_SCANNED; 7179 continue; 7180 } 7181 if (BUF_LOCK(bp, 7182 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 7183 BO_LOCKPTR(bo)) == ENOLCK) { 7184 BO_LOCK(bo); 7185 goto cleanrestart; 7186 } 7187 bp->b_vflags |= BV_SCANNED; 7188 bremfree(bp); 7189 if (blkoff != 0) { 7190 allocbuf(bp, blkoff); 7191 bqrelse(bp); 7192 } else { 7193 bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF; 7194 brelse(bp); 7195 } 7196 BO_LOCK(bo); 7197 goto cleanrestart; 7198 } 7199 drain_output(vp); 7200 BO_UNLOCK(bo); 7201 } 7202 7203 static int 7204 cancel_pagedep(pagedep, freeblks, blkoff) 7205 struct pagedep *pagedep; 7206 struct freeblks *freeblks; 7207 int blkoff; 7208 { 7209 struct jremref *jremref; 7210 struct jmvref *jmvref; 7211 struct dirrem *dirrem, *tmp; 7212 int i; 7213 7214 /* 7215 * Copy any directory remove dependencies to the list 7216 * to be processed after the freeblks proceeds. If 7217 * directory entry never made it to disk they 7218 * can be dumped directly onto the work list. 7219 */ 7220 LIST_FOREACH_SAFE(dirrem, &pagedep->pd_dirremhd, dm_next, tmp) { 7221 /* Skip this directory removal if it is intended to remain. */ 7222 if (dirrem->dm_offset < blkoff) 7223 continue; 7224 /* 7225 * If there are any dirrems we wait for the journal write 7226 * to complete and then restart the buf scan as the lock 7227 * has been dropped. 7228 */ 7229 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) { 7230 jwait(&jremref->jr_list, MNT_WAIT); 7231 return (ERESTART); 7232 } 7233 LIST_REMOVE(dirrem, dm_next); 7234 dirrem->dm_dirinum = pagedep->pd_ino; 7235 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list); 7236 } 7237 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) { 7238 jwait(&jmvref->jm_list, MNT_WAIT); 7239 return (ERESTART); 7240 } 7241 /* 7242 * When we're partially truncating a pagedep we just want to flush 7243 * journal entries and return. There can not be any adds in the 7244 * truncated portion of the directory and newblk must remain if 7245 * part of the block remains. 7246 */ 7247 if (blkoff != 0) { 7248 struct diradd *dap; 7249 7250 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 7251 if (dap->da_offset > blkoff) 7252 panic("cancel_pagedep: diradd %p off %d > %d", 7253 dap, dap->da_offset, blkoff); 7254 for (i = 0; i < DAHASHSZ; i++) 7255 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) 7256 if (dap->da_offset > blkoff) 7257 panic("cancel_pagedep: diradd %p off %d > %d", 7258 dap, dap->da_offset, blkoff); 7259 return (0); 7260 } 7261 /* 7262 * There should be no directory add dependencies present 7263 * as the directory could not be truncated until all 7264 * children were removed. 7265 */ 7266 KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL, 7267 ("deallocate_dependencies: pendinghd != NULL")); 7268 for (i = 0; i < DAHASHSZ; i++) 7269 KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL, 7270 ("deallocate_dependencies: diraddhd != NULL")); 7271 if ((pagedep->pd_state & NEWBLOCK) != 0) 7272 free_newdirblk(pagedep->pd_newdirblk); 7273 if (free_pagedep(pagedep) == 0) 7274 panic("Failed to free pagedep %p", pagedep); 7275 return (0); 7276 } 7277 7278 /* 7279 * Reclaim any dependency structures from a buffer that is about to 7280 * be reallocated to a new vnode. The buffer must be locked, thus, 7281 * no I/O completion operations can occur while we are manipulating 7282 * its associated dependencies. The mutex is held so that other I/O's 7283 * associated with related dependencies do not occur. 7284 */ 7285 static int 7286 deallocate_dependencies(bp, freeblks, off) 7287 struct buf *bp; 7288 struct freeblks *freeblks; 7289 int off; 7290 { 7291 struct indirdep *indirdep; 7292 struct pagedep *pagedep; 7293 struct worklist *wk, *wkn; 7294 struct ufsmount *ump; 7295 7296 ump = softdep_bp_to_mp(bp); 7297 if (ump == NULL) 7298 goto done; 7299 ACQUIRE_LOCK(ump); 7300 LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) { 7301 switch (wk->wk_type) { 7302 case D_INDIRDEP: 7303 indirdep = WK_INDIRDEP(wk); 7304 if (bp->b_lblkno >= 0 || 7305 bp->b_blkno != indirdep->ir_savebp->b_lblkno) 7306 panic("deallocate_dependencies: not indir"); 7307 cancel_indirdep(indirdep, bp, freeblks); 7308 continue; 7309 7310 case D_PAGEDEP: 7311 pagedep = WK_PAGEDEP(wk); 7312 if (cancel_pagedep(pagedep, freeblks, off)) { 7313 FREE_LOCK(ump); 7314 return (ERESTART); 7315 } 7316 continue; 7317 7318 case D_ALLOCINDIR: 7319 /* 7320 * Simply remove the allocindir, we'll find it via 7321 * the indirdep where we can clear pointers if 7322 * needed. 7323 */ 7324 WORKLIST_REMOVE(wk); 7325 continue; 7326 7327 case D_FREEWORK: 7328 /* 7329 * A truncation is waiting for the zero'd pointers 7330 * to be written. It can be freed when the freeblks 7331 * is journaled. 7332 */ 7333 WORKLIST_REMOVE(wk); 7334 wk->wk_state |= ONDEPLIST; 7335 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 7336 break; 7337 7338 case D_ALLOCDIRECT: 7339 if (off != 0) 7340 continue; 7341 /* FALLTHROUGH */ 7342 default: 7343 panic("deallocate_dependencies: Unexpected type %s", 7344 TYPENAME(wk->wk_type)); 7345 /* NOTREACHED */ 7346 } 7347 } 7348 FREE_LOCK(ump); 7349 done: 7350 /* 7351 * Don't throw away this buf, we were partially truncating and 7352 * some deps may always remain. 7353 */ 7354 if (off) { 7355 allocbuf(bp, off); 7356 bp->b_vflags |= BV_SCANNED; 7357 return (EBUSY); 7358 } 7359 bp->b_flags |= B_INVAL | B_NOCACHE; 7360 7361 return (0); 7362 } 7363 7364 /* 7365 * An allocdirect is being canceled due to a truncate. We must make sure 7366 * the journal entry is released in concert with the blkfree that releases 7367 * the storage. Completed journal entries must not be released until the 7368 * space is no longer pointed to by the inode or in the bitmap. 7369 */ 7370 static void 7371 cancel_allocdirect(adphead, adp, freeblks) 7372 struct allocdirectlst *adphead; 7373 struct allocdirect *adp; 7374 struct freeblks *freeblks; 7375 { 7376 struct freework *freework; 7377 struct newblk *newblk; 7378 struct worklist *wk; 7379 7380 TAILQ_REMOVE(adphead, adp, ad_next); 7381 newblk = (struct newblk *)adp; 7382 freework = NULL; 7383 /* 7384 * Find the correct freework structure. 7385 */ 7386 LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) { 7387 if (wk->wk_type != D_FREEWORK) 7388 continue; 7389 freework = WK_FREEWORK(wk); 7390 if (freework->fw_blkno == newblk->nb_newblkno) 7391 break; 7392 } 7393 if (freework == NULL) 7394 panic("cancel_allocdirect: Freework not found"); 7395 /* 7396 * If a newblk exists at all we still have the journal entry that 7397 * initiated the allocation so we do not need to journal the free. 7398 */ 7399 cancel_jfreeblk(freeblks, freework->fw_blkno); 7400 /* 7401 * If the journal hasn't been written the jnewblk must be passed 7402 * to the call to ffs_blkfree that reclaims the space. We accomplish 7403 * this by linking the journal dependency into the freework to be 7404 * freed when freework_freeblock() is called. If the journal has 7405 * been written we can simply reclaim the journal space when the 7406 * freeblks work is complete. 7407 */ 7408 freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list, 7409 &freeblks->fb_jwork); 7410 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 7411 } 7412 7413 7414 /* 7415 * Cancel a new block allocation. May be an indirect or direct block. We 7416 * remove it from various lists and return any journal record that needs to 7417 * be resolved by the caller. 7418 * 7419 * A special consideration is made for indirects which were never pointed 7420 * at on disk and will never be found once this block is released. 7421 */ 7422 static struct jnewblk * 7423 cancel_newblk(newblk, wk, wkhd) 7424 struct newblk *newblk; 7425 struct worklist *wk; 7426 struct workhead *wkhd; 7427 { 7428 struct jnewblk *jnewblk; 7429 7430 CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno); 7431 7432 newblk->nb_state |= GOINGAWAY; 7433 /* 7434 * Previously we traversed the completedhd on each indirdep 7435 * attached to this newblk to cancel them and gather journal 7436 * work. Since we need only the oldest journal segment and 7437 * the lowest point on the tree will always have the oldest 7438 * journal segment we are free to release the segments 7439 * of any subordinates and may leave the indirdep list to 7440 * indirdep_complete() when this newblk is freed. 7441 */ 7442 if (newblk->nb_state & ONDEPLIST) { 7443 newblk->nb_state &= ~ONDEPLIST; 7444 LIST_REMOVE(newblk, nb_deps); 7445 } 7446 if (newblk->nb_state & ONWORKLIST) 7447 WORKLIST_REMOVE(&newblk->nb_list); 7448 /* 7449 * If the journal entry hasn't been written we save a pointer to 7450 * the dependency that frees it until it is written or the 7451 * superseding operation completes. 7452 */ 7453 jnewblk = newblk->nb_jnewblk; 7454 if (jnewblk != NULL && wk != NULL) { 7455 newblk->nb_jnewblk = NULL; 7456 jnewblk->jn_dep = wk; 7457 } 7458 if (!LIST_EMPTY(&newblk->nb_jwork)) 7459 jwork_move(wkhd, &newblk->nb_jwork); 7460 /* 7461 * When truncating we must free the newdirblk early to remove 7462 * the pagedep from the hash before returning. 7463 */ 7464 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7465 free_newdirblk(WK_NEWDIRBLK(wk)); 7466 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7467 panic("cancel_newblk: extra newdirblk"); 7468 7469 return (jnewblk); 7470 } 7471 7472 /* 7473 * Schedule the freefrag associated with a newblk to be released once 7474 * the pointers are written and the previous block is no longer needed. 7475 */ 7476 static void 7477 newblk_freefrag(newblk) 7478 struct newblk *newblk; 7479 { 7480 struct freefrag *freefrag; 7481 7482 if (newblk->nb_freefrag == NULL) 7483 return; 7484 freefrag = newblk->nb_freefrag; 7485 newblk->nb_freefrag = NULL; 7486 freefrag->ff_state |= COMPLETE; 7487 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 7488 add_to_worklist(&freefrag->ff_list, 0); 7489 } 7490 7491 /* 7492 * Free a newblk. Generate a new freefrag work request if appropriate. 7493 * This must be called after the inode pointer and any direct block pointers 7494 * are valid or fully removed via truncate or frag extension. 7495 */ 7496 static void 7497 free_newblk(newblk) 7498 struct newblk *newblk; 7499 { 7500 struct indirdep *indirdep; 7501 struct worklist *wk; 7502 7503 KASSERT(newblk->nb_jnewblk == NULL, 7504 ("free_newblk: jnewblk %p still attached", newblk->nb_jnewblk)); 7505 KASSERT(newblk->nb_list.wk_type != D_NEWBLK, 7506 ("free_newblk: unclaimed newblk")); 7507 LOCK_OWNED(VFSTOUFS(newblk->nb_list.wk_mp)); 7508 newblk_freefrag(newblk); 7509 if (newblk->nb_state & ONDEPLIST) 7510 LIST_REMOVE(newblk, nb_deps); 7511 if (newblk->nb_state & ONWORKLIST) 7512 WORKLIST_REMOVE(&newblk->nb_list); 7513 LIST_REMOVE(newblk, nb_hash); 7514 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7515 free_newdirblk(WK_NEWDIRBLK(wk)); 7516 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7517 panic("free_newblk: extra newdirblk"); 7518 while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL) 7519 indirdep_complete(indirdep); 7520 handle_jwork(&newblk->nb_jwork); 7521 WORKITEM_FREE(newblk, D_NEWBLK); 7522 } 7523 7524 /* 7525 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep. 7526 */ 7527 static void 7528 free_newdirblk(newdirblk) 7529 struct newdirblk *newdirblk; 7530 { 7531 struct pagedep *pagedep; 7532 struct diradd *dap; 7533 struct worklist *wk; 7534 7535 LOCK_OWNED(VFSTOUFS(newdirblk->db_list.wk_mp)); 7536 WORKLIST_REMOVE(&newdirblk->db_list); 7537 /* 7538 * If the pagedep is still linked onto the directory buffer 7539 * dependency chain, then some of the entries on the 7540 * pd_pendinghd list may not be committed to disk yet. In 7541 * this case, we will simply clear the NEWBLOCK flag and 7542 * let the pd_pendinghd list be processed when the pagedep 7543 * is next written. If the pagedep is no longer on the buffer 7544 * dependency chain, then all the entries on the pd_pending 7545 * list are committed to disk and we can free them here. 7546 */ 7547 pagedep = newdirblk->db_pagedep; 7548 pagedep->pd_state &= ~NEWBLOCK; 7549 if ((pagedep->pd_state & ONWORKLIST) == 0) { 7550 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 7551 free_diradd(dap, NULL); 7552 /* 7553 * If no dependencies remain, the pagedep will be freed. 7554 */ 7555 free_pagedep(pagedep); 7556 } 7557 /* Should only ever be one item in the list. */ 7558 while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) { 7559 WORKLIST_REMOVE(wk); 7560 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 7561 } 7562 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 7563 } 7564 7565 /* 7566 * Prepare an inode to be freed. The actual free operation is not 7567 * done until the zero'ed inode has been written to disk. 7568 */ 7569 void 7570 softdep_freefile(pvp, ino, mode) 7571 struct vnode *pvp; 7572 ino_t ino; 7573 int mode; 7574 { 7575 struct inode *ip = VTOI(pvp); 7576 struct inodedep *inodedep; 7577 struct freefile *freefile; 7578 struct freeblks *freeblks; 7579 struct ufsmount *ump; 7580 7581 ump = ITOUMP(ip); 7582 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 7583 ("softdep_freefile called on non-softdep filesystem")); 7584 /* 7585 * This sets up the inode de-allocation dependency. 7586 */ 7587 freefile = malloc(sizeof(struct freefile), 7588 M_FREEFILE, M_SOFTDEP_FLAGS); 7589 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount); 7590 freefile->fx_mode = mode; 7591 freefile->fx_oldinum = ino; 7592 freefile->fx_devvp = ump->um_devvp; 7593 LIST_INIT(&freefile->fx_jwork); 7594 UFS_LOCK(ump); 7595 ump->um_fs->fs_pendinginodes += 1; 7596 UFS_UNLOCK(ump); 7597 7598 /* 7599 * If the inodedep does not exist, then the zero'ed inode has 7600 * been written to disk. If the allocated inode has never been 7601 * written to disk, then the on-disk inode is zero'ed. In either 7602 * case we can free the file immediately. If the journal was 7603 * canceled before being written the inode will never make it to 7604 * disk and we must send the canceled journal entrys to 7605 * ffs_freefile() to be cleared in conjunction with the bitmap. 7606 * Any blocks waiting on the inode to write can be safely freed 7607 * here as it will never been written. 7608 */ 7609 ACQUIRE_LOCK(ump); 7610 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7611 if (inodedep) { 7612 /* 7613 * Clear out freeblks that no longer need to reference 7614 * this inode. 7615 */ 7616 while ((freeblks = 7617 TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) { 7618 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, 7619 fb_next); 7620 freeblks->fb_state &= ~ONDEPLIST; 7621 } 7622 /* 7623 * Remove this inode from the unlinked list. 7624 */ 7625 if (inodedep->id_state & UNLINKED) { 7626 /* 7627 * Save the journal work to be freed with the bitmap 7628 * before we clear UNLINKED. Otherwise it can be lost 7629 * if the inode block is written. 7630 */ 7631 handle_bufwait(inodedep, &freefile->fx_jwork); 7632 clear_unlinked_inodedep(inodedep); 7633 /* 7634 * Re-acquire inodedep as we've dropped the 7635 * per-filesystem lock in clear_unlinked_inodedep(). 7636 */ 7637 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7638 } 7639 } 7640 if (inodedep == NULL || check_inode_unwritten(inodedep)) { 7641 FREE_LOCK(ump); 7642 handle_workitem_freefile(freefile); 7643 return; 7644 } 7645 if ((inodedep->id_state & DEPCOMPLETE) == 0) 7646 inodedep->id_state |= GOINGAWAY; 7647 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list); 7648 FREE_LOCK(ump); 7649 if (ip->i_number == ino) 7650 ip->i_flag |= IN_MODIFIED; 7651 } 7652 7653 /* 7654 * Check to see if an inode has never been written to disk. If 7655 * so free the inodedep and return success, otherwise return failure. 7656 * 7657 * If we still have a bitmap dependency, then the inode has never 7658 * been written to disk. Drop the dependency as it is no longer 7659 * necessary since the inode is being deallocated. We set the 7660 * ALLCOMPLETE flags since the bitmap now properly shows that the 7661 * inode is not allocated. Even if the inode is actively being 7662 * written, it has been rolled back to its zero'ed state, so we 7663 * are ensured that a zero inode is what is on the disk. For short 7664 * lived files, this change will usually result in removing all the 7665 * dependencies from the inode so that it can be freed immediately. 7666 */ 7667 static int 7668 check_inode_unwritten(inodedep) 7669 struct inodedep *inodedep; 7670 { 7671 7672 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7673 7674 if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 || 7675 !LIST_EMPTY(&inodedep->id_dirremhd) || 7676 !LIST_EMPTY(&inodedep->id_pendinghd) || 7677 !LIST_EMPTY(&inodedep->id_bufwait) || 7678 !LIST_EMPTY(&inodedep->id_inowait) || 7679 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 7680 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 7681 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 7682 !TAILQ_EMPTY(&inodedep->id_extupdt) || 7683 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 7684 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 7685 inodedep->id_mkdiradd != NULL || 7686 inodedep->id_nlinkdelta != 0) 7687 return (0); 7688 /* 7689 * Another process might be in initiate_write_inodeblock_ufs[12] 7690 * trying to allocate memory without holding "Softdep Lock". 7691 */ 7692 if ((inodedep->id_state & IOSTARTED) != 0 && 7693 inodedep->id_savedino1 == NULL) 7694 return (0); 7695 7696 if (inodedep->id_state & ONDEPLIST) 7697 LIST_REMOVE(inodedep, id_deps); 7698 inodedep->id_state &= ~ONDEPLIST; 7699 inodedep->id_state |= ALLCOMPLETE; 7700 inodedep->id_bmsafemap = NULL; 7701 if (inodedep->id_state & ONWORKLIST) 7702 WORKLIST_REMOVE(&inodedep->id_list); 7703 if (inodedep->id_savedino1 != NULL) { 7704 free(inodedep->id_savedino1, M_SAVEDINO); 7705 inodedep->id_savedino1 = NULL; 7706 } 7707 if (free_inodedep(inodedep) == 0) 7708 panic("check_inode_unwritten: busy inode"); 7709 return (1); 7710 } 7711 7712 static int 7713 check_inodedep_free(inodedep) 7714 struct inodedep *inodedep; 7715 { 7716 7717 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7718 if ((inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE || 7719 !LIST_EMPTY(&inodedep->id_dirremhd) || 7720 !LIST_EMPTY(&inodedep->id_pendinghd) || 7721 !LIST_EMPTY(&inodedep->id_bufwait) || 7722 !LIST_EMPTY(&inodedep->id_inowait) || 7723 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 7724 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 7725 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 7726 !TAILQ_EMPTY(&inodedep->id_extupdt) || 7727 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 7728 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 7729 inodedep->id_mkdiradd != NULL || 7730 inodedep->id_nlinkdelta != 0 || 7731 inodedep->id_savedino1 != NULL) 7732 return (0); 7733 return (1); 7734 } 7735 7736 /* 7737 * Try to free an inodedep structure. Return 1 if it could be freed. 7738 */ 7739 static int 7740 free_inodedep(inodedep) 7741 struct inodedep *inodedep; 7742 { 7743 7744 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7745 if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 || 7746 !check_inodedep_free(inodedep)) 7747 return (0); 7748 if (inodedep->id_state & ONDEPLIST) 7749 LIST_REMOVE(inodedep, id_deps); 7750 LIST_REMOVE(inodedep, id_hash); 7751 WORKITEM_FREE(inodedep, D_INODEDEP); 7752 return (1); 7753 } 7754 7755 /* 7756 * Free the block referenced by a freework structure. The parent freeblks 7757 * structure is released and completed when the final cg bitmap reaches 7758 * the disk. This routine may be freeing a jnewblk which never made it to 7759 * disk in which case we do not have to wait as the operation is undone 7760 * in memory immediately. 7761 */ 7762 static void 7763 freework_freeblock(freework, key) 7764 struct freework *freework; 7765 u_long key; 7766 { 7767 struct freeblks *freeblks; 7768 struct jnewblk *jnewblk; 7769 struct ufsmount *ump; 7770 struct workhead wkhd; 7771 struct fs *fs; 7772 int bsize; 7773 int needj; 7774 7775 ump = VFSTOUFS(freework->fw_list.wk_mp); 7776 LOCK_OWNED(ump); 7777 /* 7778 * Handle partial truncate separately. 7779 */ 7780 if (freework->fw_indir) { 7781 complete_trunc_indir(freework); 7782 return; 7783 } 7784 freeblks = freework->fw_freeblks; 7785 fs = ump->um_fs; 7786 needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0; 7787 bsize = lfragtosize(fs, freework->fw_frags); 7788 LIST_INIT(&wkhd); 7789 /* 7790 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives 7791 * on the indirblk hashtable and prevents premature freeing. 7792 */ 7793 freework->fw_state |= DEPCOMPLETE; 7794 /* 7795 * SUJ needs to wait for the segment referencing freed indirect 7796 * blocks to expire so that we know the checker will not confuse 7797 * a re-allocated indirect block with its old contents. 7798 */ 7799 if (needj && freework->fw_lbn <= -UFS_NDADDR) 7800 indirblk_insert(freework); 7801 /* 7802 * If we are canceling an existing jnewblk pass it to the free 7803 * routine, otherwise pass the freeblk which will ultimately 7804 * release the freeblks. If we're not journaling, we can just 7805 * free the freeblks immediately. 7806 */ 7807 jnewblk = freework->fw_jnewblk; 7808 if (jnewblk != NULL) { 7809 cancel_jnewblk(jnewblk, &wkhd); 7810 needj = 0; 7811 } else if (needj) { 7812 freework->fw_state |= DELAYEDFREE; 7813 freeblks->fb_cgwait++; 7814 WORKLIST_INSERT(&wkhd, &freework->fw_list); 7815 } 7816 FREE_LOCK(ump); 7817 freeblks_free(ump, freeblks, btodb(bsize)); 7818 CTR4(KTR_SUJ, 7819 "freework_freeblock: ino %jd blkno %jd lbn %jd size %d", 7820 freeblks->fb_inum, freework->fw_blkno, freework->fw_lbn, bsize); 7821 ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, bsize, 7822 freeblks->fb_inum, freeblks->fb_vtype, &wkhd, key); 7823 ACQUIRE_LOCK(ump); 7824 /* 7825 * The jnewblk will be discarded and the bits in the map never 7826 * made it to disk. We can immediately free the freeblk. 7827 */ 7828 if (needj == 0) 7829 handle_written_freework(freework); 7830 } 7831 7832 /* 7833 * We enqueue freework items that need processing back on the freeblks and 7834 * add the freeblks to the worklist. This makes it easier to find all work 7835 * required to flush a truncation in process_truncates(). 7836 */ 7837 static void 7838 freework_enqueue(freework) 7839 struct freework *freework; 7840 { 7841 struct freeblks *freeblks; 7842 7843 freeblks = freework->fw_freeblks; 7844 if ((freework->fw_state & INPROGRESS) == 0) 7845 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 7846 if ((freeblks->fb_state & 7847 (ONWORKLIST | INPROGRESS | ALLCOMPLETE)) == ALLCOMPLETE && 7848 LIST_EMPTY(&freeblks->fb_jblkdephd)) 7849 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 7850 } 7851 7852 /* 7853 * Start, continue, or finish the process of freeing an indirect block tree. 7854 * The free operation may be paused at any point with fw_off containing the 7855 * offset to restart from. This enables us to implement some flow control 7856 * for large truncates which may fan out and generate a huge number of 7857 * dependencies. 7858 */ 7859 static void 7860 handle_workitem_indirblk(freework) 7861 struct freework *freework; 7862 { 7863 struct freeblks *freeblks; 7864 struct ufsmount *ump; 7865 struct fs *fs; 7866 7867 freeblks = freework->fw_freeblks; 7868 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7869 fs = ump->um_fs; 7870 if (freework->fw_state & DEPCOMPLETE) { 7871 handle_written_freework(freework); 7872 return; 7873 } 7874 if (freework->fw_off == NINDIR(fs)) { 7875 freework_freeblock(freework, SINGLETON_KEY); 7876 return; 7877 } 7878 freework->fw_state |= INPROGRESS; 7879 FREE_LOCK(ump); 7880 indir_trunc(freework, fsbtodb(fs, freework->fw_blkno), 7881 freework->fw_lbn); 7882 ACQUIRE_LOCK(ump); 7883 } 7884 7885 /* 7886 * Called when a freework structure attached to a cg buf is written. The 7887 * ref on either the parent or the freeblks structure is released and 7888 * the freeblks is added back to the worklist if there is more work to do. 7889 */ 7890 static void 7891 handle_written_freework(freework) 7892 struct freework *freework; 7893 { 7894 struct freeblks *freeblks; 7895 struct freework *parent; 7896 7897 freeblks = freework->fw_freeblks; 7898 parent = freework->fw_parent; 7899 if (freework->fw_state & DELAYEDFREE) 7900 freeblks->fb_cgwait--; 7901 freework->fw_state |= COMPLETE; 7902 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 7903 WORKITEM_FREE(freework, D_FREEWORK); 7904 if (parent) { 7905 if (--parent->fw_ref == 0) 7906 freework_enqueue(parent); 7907 return; 7908 } 7909 if (--freeblks->fb_ref != 0) 7910 return; 7911 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) == 7912 ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd)) 7913 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 7914 } 7915 7916 /* 7917 * This workitem routine performs the block de-allocation. 7918 * The workitem is added to the pending list after the updated 7919 * inode block has been written to disk. As mentioned above, 7920 * checks regarding the number of blocks de-allocated (compared 7921 * to the number of blocks allocated for the file) are also 7922 * performed in this function. 7923 */ 7924 static int 7925 handle_workitem_freeblocks(freeblks, flags) 7926 struct freeblks *freeblks; 7927 int flags; 7928 { 7929 struct freework *freework; 7930 struct newblk *newblk; 7931 struct allocindir *aip; 7932 struct ufsmount *ump; 7933 struct worklist *wk; 7934 u_long key; 7935 7936 KASSERT(LIST_EMPTY(&freeblks->fb_jblkdephd), 7937 ("handle_workitem_freeblocks: Journal entries not written.")); 7938 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7939 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum); 7940 ACQUIRE_LOCK(ump); 7941 while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) { 7942 WORKLIST_REMOVE(wk); 7943 switch (wk->wk_type) { 7944 case D_DIRREM: 7945 wk->wk_state |= COMPLETE; 7946 add_to_worklist(wk, 0); 7947 continue; 7948 7949 case D_ALLOCDIRECT: 7950 free_newblk(WK_NEWBLK(wk)); 7951 continue; 7952 7953 case D_ALLOCINDIR: 7954 aip = WK_ALLOCINDIR(wk); 7955 freework = NULL; 7956 if (aip->ai_state & DELAYEDFREE) { 7957 FREE_LOCK(ump); 7958 freework = newfreework(ump, freeblks, NULL, 7959 aip->ai_lbn, aip->ai_newblkno, 7960 ump->um_fs->fs_frag, 0, 0); 7961 ACQUIRE_LOCK(ump); 7962 } 7963 newblk = WK_NEWBLK(wk); 7964 if (newblk->nb_jnewblk) { 7965 freework->fw_jnewblk = newblk->nb_jnewblk; 7966 newblk->nb_jnewblk->jn_dep = &freework->fw_list; 7967 newblk->nb_jnewblk = NULL; 7968 } 7969 free_newblk(newblk); 7970 continue; 7971 7972 case D_FREEWORK: 7973 freework = WK_FREEWORK(wk); 7974 if (freework->fw_lbn <= -UFS_NDADDR) 7975 handle_workitem_indirblk(freework); 7976 else 7977 freework_freeblock(freework, key); 7978 continue; 7979 default: 7980 panic("handle_workitem_freeblocks: Unknown type %s", 7981 TYPENAME(wk->wk_type)); 7982 } 7983 } 7984 if (freeblks->fb_ref != 0) { 7985 freeblks->fb_state &= ~INPROGRESS; 7986 wake_worklist(&freeblks->fb_list); 7987 freeblks = NULL; 7988 } 7989 FREE_LOCK(ump); 7990 ffs_blkrelease_finish(ump, key); 7991 if (freeblks) 7992 return handle_complete_freeblocks(freeblks, flags); 7993 return (0); 7994 } 7995 7996 /* 7997 * Handle completion of block free via truncate. This allows fs_pending 7998 * to track the actual free block count more closely than if we only updated 7999 * it at the end. We must be careful to handle cases where the block count 8000 * on free was incorrect. 8001 */ 8002 static void 8003 freeblks_free(ump, freeblks, blocks) 8004 struct ufsmount *ump; 8005 struct freeblks *freeblks; 8006 int blocks; 8007 { 8008 struct fs *fs; 8009 ufs2_daddr_t remain; 8010 8011 UFS_LOCK(ump); 8012 remain = -freeblks->fb_chkcnt; 8013 freeblks->fb_chkcnt += blocks; 8014 if (remain > 0) { 8015 if (remain < blocks) 8016 blocks = remain; 8017 fs = ump->um_fs; 8018 fs->fs_pendingblocks -= blocks; 8019 } 8020 UFS_UNLOCK(ump); 8021 } 8022 8023 /* 8024 * Once all of the freework workitems are complete we can retire the 8025 * freeblocks dependency and any journal work awaiting completion. This 8026 * can not be called until all other dependencies are stable on disk. 8027 */ 8028 static int 8029 handle_complete_freeblocks(freeblks, flags) 8030 struct freeblks *freeblks; 8031 int flags; 8032 { 8033 struct inodedep *inodedep; 8034 struct inode *ip; 8035 struct vnode *vp; 8036 struct fs *fs; 8037 struct ufsmount *ump; 8038 ufs2_daddr_t spare; 8039 8040 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8041 fs = ump->um_fs; 8042 flags = LK_EXCLUSIVE | flags; 8043 spare = freeblks->fb_chkcnt; 8044 8045 /* 8046 * If we did not release the expected number of blocks we may have 8047 * to adjust the inode block count here. Only do so if it wasn't 8048 * a truncation to zero and the modrev still matches. 8049 */ 8050 if (spare && freeblks->fb_len != 0) { 8051 if (ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_inum, 8052 flags, &vp, FFSV_FORCEINSMQ) != 0) 8053 return (EBUSY); 8054 ip = VTOI(vp); 8055 if (ip->i_mode == 0) { 8056 vgone(vp); 8057 } else if (DIP(ip, i_modrev) == freeblks->fb_modrev) { 8058 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare); 8059 ip->i_flag |= IN_CHANGE; 8060 /* 8061 * We must wait so this happens before the 8062 * journal is reclaimed. 8063 */ 8064 ffs_update(vp, 1); 8065 } 8066 vput(vp); 8067 } 8068 if (spare < 0) { 8069 UFS_LOCK(ump); 8070 fs->fs_pendingblocks += spare; 8071 UFS_UNLOCK(ump); 8072 } 8073 #ifdef QUOTA 8074 /* Handle spare. */ 8075 if (spare) 8076 quotaadj(freeblks->fb_quota, ump, -spare); 8077 quotarele(freeblks->fb_quota); 8078 #endif 8079 ACQUIRE_LOCK(ump); 8080 if (freeblks->fb_state & ONDEPLIST) { 8081 inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum, 8082 0, &inodedep); 8083 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next); 8084 freeblks->fb_state &= ~ONDEPLIST; 8085 if (TAILQ_EMPTY(&inodedep->id_freeblklst)) 8086 free_inodedep(inodedep); 8087 } 8088 /* 8089 * All of the freeblock deps must be complete prior to this call 8090 * so it's now safe to complete earlier outstanding journal entries. 8091 */ 8092 handle_jwork(&freeblks->fb_jwork); 8093 WORKITEM_FREE(freeblks, D_FREEBLKS); 8094 FREE_LOCK(ump); 8095 return (0); 8096 } 8097 8098 /* 8099 * Release blocks associated with the freeblks and stored in the indirect 8100 * block dbn. If level is greater than SINGLE, the block is an indirect block 8101 * and recursive calls to indirtrunc must be used to cleanse other indirect 8102 * blocks. 8103 * 8104 * This handles partial and complete truncation of blocks. Partial is noted 8105 * with goingaway == 0. In this case the freework is completed after the 8106 * zero'd indirects are written to disk. For full truncation the freework 8107 * is completed after the block is freed. 8108 */ 8109 static void 8110 indir_trunc(freework, dbn, lbn) 8111 struct freework *freework; 8112 ufs2_daddr_t dbn; 8113 ufs_lbn_t lbn; 8114 { 8115 struct freework *nfreework; 8116 struct workhead wkhd; 8117 struct freeblks *freeblks; 8118 struct buf *bp; 8119 struct fs *fs; 8120 struct indirdep *indirdep; 8121 struct mount *mp; 8122 struct ufsmount *ump; 8123 ufs1_daddr_t *bap1; 8124 ufs2_daddr_t nb, nnb, *bap2; 8125 ufs_lbn_t lbnadd, nlbn; 8126 u_long key; 8127 int nblocks, ufs1fmt, freedblocks; 8128 int goingaway, freedeps, needj, level, cnt, i; 8129 8130 freeblks = freework->fw_freeblks; 8131 mp = freeblks->fb_list.wk_mp; 8132 ump = VFSTOUFS(mp); 8133 fs = ump->um_fs; 8134 /* 8135 * Get buffer of block pointers to be freed. There are three cases: 8136 * 8137 * 1) Partial truncate caches the indirdep pointer in the freework 8138 * which provides us a back copy to the save bp which holds the 8139 * pointers we want to clear. When this completes the zero 8140 * pointers are written to the real copy. 8141 * 2) The indirect is being completely truncated, cancel_indirdep() 8142 * eliminated the real copy and placed the indirdep on the saved 8143 * copy. The indirdep and buf are discarded when this completes. 8144 * 3) The indirect was not in memory, we read a copy off of the disk 8145 * using the devvp and drop and invalidate the buffer when we're 8146 * done. 8147 */ 8148 goingaway = 1; 8149 indirdep = NULL; 8150 if (freework->fw_indir != NULL) { 8151 goingaway = 0; 8152 indirdep = freework->fw_indir; 8153 bp = indirdep->ir_savebp; 8154 if (bp == NULL || bp->b_blkno != dbn) 8155 panic("indir_trunc: Bad saved buf %p blkno %jd", 8156 bp, (intmax_t)dbn); 8157 } else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) { 8158 /* 8159 * The lock prevents the buf dep list from changing and 8160 * indirects on devvp should only ever have one dependency. 8161 */ 8162 indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep)); 8163 if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0) 8164 panic("indir_trunc: Bad indirdep %p from buf %p", 8165 indirdep, bp); 8166 } else if (bread(freeblks->fb_devvp, dbn, (int)fs->fs_bsize, 8167 NOCRED, &bp) != 0) { 8168 brelse(bp); 8169 return; 8170 } 8171 ACQUIRE_LOCK(ump); 8172 /* Protects against a race with complete_trunc_indir(). */ 8173 freework->fw_state &= ~INPROGRESS; 8174 /* 8175 * If we have an indirdep we need to enforce the truncation order 8176 * and discard it when it is complete. 8177 */ 8178 if (indirdep) { 8179 if (freework != TAILQ_FIRST(&indirdep->ir_trunc) && 8180 !TAILQ_EMPTY(&indirdep->ir_trunc)) { 8181 /* 8182 * Add the complete truncate to the list on the 8183 * indirdep to enforce in-order processing. 8184 */ 8185 if (freework->fw_indir == NULL) 8186 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, 8187 freework, fw_next); 8188 FREE_LOCK(ump); 8189 return; 8190 } 8191 /* 8192 * If we're goingaway, free the indirdep. Otherwise it will 8193 * linger until the write completes. 8194 */ 8195 if (goingaway) 8196 free_indirdep(indirdep); 8197 } 8198 FREE_LOCK(ump); 8199 /* Initialize pointers depending on block size. */ 8200 if (ump->um_fstype == UFS1) { 8201 bap1 = (ufs1_daddr_t *)bp->b_data; 8202 nb = bap1[freework->fw_off]; 8203 ufs1fmt = 1; 8204 bap2 = NULL; 8205 } else { 8206 bap2 = (ufs2_daddr_t *)bp->b_data; 8207 nb = bap2[freework->fw_off]; 8208 ufs1fmt = 0; 8209 bap1 = NULL; 8210 } 8211 level = lbn_level(lbn); 8212 needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0; 8213 lbnadd = lbn_offset(fs, level); 8214 nblocks = btodb(fs->fs_bsize); 8215 nfreework = freework; 8216 freedeps = 0; 8217 cnt = 0; 8218 /* 8219 * Reclaim blocks. Traverses into nested indirect levels and 8220 * arranges for the current level to be freed when subordinates 8221 * are free when journaling. 8222 */ 8223 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum); 8224 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) { 8225 if (UFS_CHECK_BLKNO(mp, freeblks->fb_inum, nb, 8226 fs->fs_bsize) != 0) 8227 nb = 0; 8228 if (i != NINDIR(fs) - 1) { 8229 if (ufs1fmt) 8230 nnb = bap1[i+1]; 8231 else 8232 nnb = bap2[i+1]; 8233 } else 8234 nnb = 0; 8235 if (nb == 0) 8236 continue; 8237 cnt++; 8238 if (level != 0) { 8239 nlbn = (lbn + 1) - (i * lbnadd); 8240 if (needj != 0) { 8241 nfreework = newfreework(ump, freeblks, freework, 8242 nlbn, nb, fs->fs_frag, 0, 0); 8243 freedeps++; 8244 } 8245 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn); 8246 } else { 8247 struct freedep *freedep; 8248 8249 /* 8250 * Attempt to aggregate freedep dependencies for 8251 * all blocks being released to the same CG. 8252 */ 8253 LIST_INIT(&wkhd); 8254 if (needj != 0 && 8255 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) { 8256 freedep = newfreedep(freework); 8257 WORKLIST_INSERT_UNLOCKED(&wkhd, 8258 &freedep->fd_list); 8259 freedeps++; 8260 } 8261 CTR3(KTR_SUJ, 8262 "indir_trunc: ino %jd blkno %jd size %d", 8263 freeblks->fb_inum, nb, fs->fs_bsize); 8264 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb, 8265 fs->fs_bsize, freeblks->fb_inum, 8266 freeblks->fb_vtype, &wkhd, key); 8267 } 8268 } 8269 ffs_blkrelease_finish(ump, key); 8270 if (goingaway) { 8271 bp->b_flags |= B_INVAL | B_NOCACHE; 8272 brelse(bp); 8273 } 8274 freedblocks = 0; 8275 if (level == 0) 8276 freedblocks = (nblocks * cnt); 8277 if (needj == 0) 8278 freedblocks += nblocks; 8279 freeblks_free(ump, freeblks, freedblocks); 8280 /* 8281 * If we are journaling set up the ref counts and offset so this 8282 * indirect can be completed when its children are free. 8283 */ 8284 if (needj) { 8285 ACQUIRE_LOCK(ump); 8286 freework->fw_off = i; 8287 freework->fw_ref += freedeps; 8288 freework->fw_ref -= NINDIR(fs) + 1; 8289 if (level == 0) 8290 freeblks->fb_cgwait += freedeps; 8291 if (freework->fw_ref == 0) 8292 freework_freeblock(freework, SINGLETON_KEY); 8293 FREE_LOCK(ump); 8294 return; 8295 } 8296 /* 8297 * If we're not journaling we can free the indirect now. 8298 */ 8299 dbn = dbtofsb(fs, dbn); 8300 CTR3(KTR_SUJ, 8301 "indir_trunc 2: ino %jd blkno %jd size %d", 8302 freeblks->fb_inum, dbn, fs->fs_bsize); 8303 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize, 8304 freeblks->fb_inum, freeblks->fb_vtype, NULL, SINGLETON_KEY); 8305 /* Non SUJ softdep does single-threaded truncations. */ 8306 if (freework->fw_blkno == dbn) { 8307 freework->fw_state |= ALLCOMPLETE; 8308 ACQUIRE_LOCK(ump); 8309 handle_written_freework(freework); 8310 FREE_LOCK(ump); 8311 } 8312 return; 8313 } 8314 8315 /* 8316 * Cancel an allocindir when it is removed via truncation. When bp is not 8317 * NULL the indirect never appeared on disk and is scheduled to be freed 8318 * independently of the indir so we can more easily track journal work. 8319 */ 8320 static void 8321 cancel_allocindir(aip, bp, freeblks, trunc) 8322 struct allocindir *aip; 8323 struct buf *bp; 8324 struct freeblks *freeblks; 8325 int trunc; 8326 { 8327 struct indirdep *indirdep; 8328 struct freefrag *freefrag; 8329 struct newblk *newblk; 8330 8331 newblk = (struct newblk *)aip; 8332 LIST_REMOVE(aip, ai_next); 8333 /* 8334 * We must eliminate the pointer in bp if it must be freed on its 8335 * own due to partial truncate or pending journal work. 8336 */ 8337 if (bp && (trunc || newblk->nb_jnewblk)) { 8338 /* 8339 * Clear the pointer and mark the aip to be freed 8340 * directly if it never existed on disk. 8341 */ 8342 aip->ai_state |= DELAYEDFREE; 8343 indirdep = aip->ai_indirdep; 8344 if (indirdep->ir_state & UFS1FMT) 8345 ((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8346 else 8347 ((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8348 } 8349 /* 8350 * When truncating the previous pointer will be freed via 8351 * savedbp. Eliminate the freefrag which would dup free. 8352 */ 8353 if (trunc && (freefrag = newblk->nb_freefrag) != NULL) { 8354 newblk->nb_freefrag = NULL; 8355 if (freefrag->ff_jdep) 8356 cancel_jfreefrag( 8357 WK_JFREEFRAG(freefrag->ff_jdep)); 8358 jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork); 8359 WORKITEM_FREE(freefrag, D_FREEFRAG); 8360 } 8361 /* 8362 * If the journal hasn't been written the jnewblk must be passed 8363 * to the call to ffs_blkfree that reclaims the space. We accomplish 8364 * this by leaving the journal dependency on the newblk to be freed 8365 * when a freework is created in handle_workitem_freeblocks(). 8366 */ 8367 cancel_newblk(newblk, NULL, &freeblks->fb_jwork); 8368 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 8369 } 8370 8371 /* 8372 * Create the mkdir dependencies for . and .. in a new directory. Link them 8373 * in to a newdirblk so any subsequent additions are tracked properly. The 8374 * caller is responsible for adding the mkdir1 dependency to the journal 8375 * and updating id_mkdiradd. This function returns with the per-filesystem 8376 * lock held. 8377 */ 8378 static struct mkdir * 8379 setup_newdir(dap, newinum, dinum, newdirbp, mkdirp) 8380 struct diradd *dap; 8381 ino_t newinum; 8382 ino_t dinum; 8383 struct buf *newdirbp; 8384 struct mkdir **mkdirp; 8385 { 8386 struct newblk *newblk; 8387 struct pagedep *pagedep; 8388 struct inodedep *inodedep; 8389 struct newdirblk *newdirblk; 8390 struct mkdir *mkdir1, *mkdir2; 8391 struct worklist *wk; 8392 struct jaddref *jaddref; 8393 struct ufsmount *ump; 8394 struct mount *mp; 8395 8396 mp = dap->da_list.wk_mp; 8397 ump = VFSTOUFS(mp); 8398 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK, 8399 M_SOFTDEP_FLAGS); 8400 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8401 LIST_INIT(&newdirblk->db_mkdir); 8402 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8403 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp); 8404 mkdir1->md_state = ATTACHED | MKDIR_BODY; 8405 mkdir1->md_diradd = dap; 8406 mkdir1->md_jaddref = NULL; 8407 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8408 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp); 8409 mkdir2->md_state = ATTACHED | MKDIR_PARENT; 8410 mkdir2->md_diradd = dap; 8411 mkdir2->md_jaddref = NULL; 8412 if (MOUNTEDSUJ(mp) == 0) { 8413 mkdir1->md_state |= DEPCOMPLETE; 8414 mkdir2->md_state |= DEPCOMPLETE; 8415 } 8416 /* 8417 * Dependency on "." and ".." being written to disk. 8418 */ 8419 mkdir1->md_buf = newdirbp; 8420 ACQUIRE_LOCK(VFSTOUFS(mp)); 8421 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir1, md_mkdirs); 8422 /* 8423 * We must link the pagedep, allocdirect, and newdirblk for 8424 * the initial file page so the pointer to the new directory 8425 * is not written until the directory contents are live and 8426 * any subsequent additions are not marked live until the 8427 * block is reachable via the inode. 8428 */ 8429 if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0) 8430 panic("setup_newdir: lost pagedep"); 8431 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list) 8432 if (wk->wk_type == D_ALLOCDIRECT) 8433 break; 8434 if (wk == NULL) 8435 panic("setup_newdir: lost allocdirect"); 8436 if (pagedep->pd_state & NEWBLOCK) 8437 panic("setup_newdir: NEWBLOCK already set"); 8438 newblk = WK_NEWBLK(wk); 8439 pagedep->pd_state |= NEWBLOCK; 8440 pagedep->pd_newdirblk = newdirblk; 8441 newdirblk->db_pagedep = pagedep; 8442 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8443 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list); 8444 /* 8445 * Look up the inodedep for the parent directory so that we 8446 * can link mkdir2 into the pending dotdot jaddref or 8447 * the inode write if there is none. If the inode is 8448 * ALLCOMPLETE and no jaddref is present all dependencies have 8449 * been satisfied and mkdir2 can be freed. 8450 */ 8451 inodedep_lookup(mp, dinum, 0, &inodedep); 8452 if (MOUNTEDSUJ(mp)) { 8453 if (inodedep == NULL) 8454 panic("setup_newdir: Lost parent."); 8455 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8456 inoreflst); 8457 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum && 8458 (jaddref->ja_state & MKDIR_PARENT), 8459 ("setup_newdir: bad dotdot jaddref %p", jaddref)); 8460 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8461 mkdir2->md_jaddref = jaddref; 8462 jaddref->ja_mkdir = mkdir2; 8463 } else if (inodedep == NULL || 8464 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 8465 dap->da_state &= ~MKDIR_PARENT; 8466 WORKITEM_FREE(mkdir2, D_MKDIR); 8467 mkdir2 = NULL; 8468 } else { 8469 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8470 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list); 8471 } 8472 *mkdirp = mkdir2; 8473 8474 return (mkdir1); 8475 } 8476 8477 /* 8478 * Directory entry addition dependencies. 8479 * 8480 * When adding a new directory entry, the inode (with its incremented link 8481 * count) must be written to disk before the directory entry's pointer to it. 8482 * Also, if the inode is newly allocated, the corresponding freemap must be 8483 * updated (on disk) before the directory entry's pointer. These requirements 8484 * are met via undo/redo on the directory entry's pointer, which consists 8485 * simply of the inode number. 8486 * 8487 * As directory entries are added and deleted, the free space within a 8488 * directory block can become fragmented. The ufs filesystem will compact 8489 * a fragmented directory block to make space for a new entry. When this 8490 * occurs, the offsets of previously added entries change. Any "diradd" 8491 * dependency structures corresponding to these entries must be updated with 8492 * the new offsets. 8493 */ 8494 8495 /* 8496 * This routine is called after the in-memory inode's link 8497 * count has been incremented, but before the directory entry's 8498 * pointer to the inode has been set. 8499 */ 8500 int 8501 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 8502 struct buf *bp; /* buffer containing directory block */ 8503 struct inode *dp; /* inode for directory */ 8504 off_t diroffset; /* offset of new entry in directory */ 8505 ino_t newinum; /* inode referenced by new directory entry */ 8506 struct buf *newdirbp; /* non-NULL => contents of new mkdir */ 8507 int isnewblk; /* entry is in a newly allocated block */ 8508 { 8509 int offset; /* offset of new entry within directory block */ 8510 ufs_lbn_t lbn; /* block in directory containing new entry */ 8511 struct fs *fs; 8512 struct diradd *dap; 8513 struct newblk *newblk; 8514 struct pagedep *pagedep; 8515 struct inodedep *inodedep; 8516 struct newdirblk *newdirblk; 8517 struct mkdir *mkdir1, *mkdir2; 8518 struct jaddref *jaddref; 8519 struct ufsmount *ump; 8520 struct mount *mp; 8521 int isindir; 8522 8523 mp = ITOVFS(dp); 8524 ump = VFSTOUFS(mp); 8525 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 8526 ("softdep_setup_directory_add called on non-softdep filesystem")); 8527 /* 8528 * Whiteouts have no dependencies. 8529 */ 8530 if (newinum == UFS_WINO) { 8531 if (newdirbp != NULL) 8532 bdwrite(newdirbp); 8533 return (0); 8534 } 8535 jaddref = NULL; 8536 mkdir1 = mkdir2 = NULL; 8537 fs = ump->um_fs; 8538 lbn = lblkno(fs, diroffset); 8539 offset = blkoff(fs, diroffset); 8540 dap = malloc(sizeof(struct diradd), M_DIRADD, 8541 M_SOFTDEP_FLAGS|M_ZERO); 8542 workitem_alloc(&dap->da_list, D_DIRADD, mp); 8543 dap->da_offset = offset; 8544 dap->da_newinum = newinum; 8545 dap->da_state = ATTACHED; 8546 LIST_INIT(&dap->da_jwork); 8547 isindir = bp->b_lblkno >= UFS_NDADDR; 8548 newdirblk = NULL; 8549 if (isnewblk && 8550 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) { 8551 newdirblk = malloc(sizeof(struct newdirblk), 8552 M_NEWDIRBLK, M_SOFTDEP_FLAGS); 8553 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8554 LIST_INIT(&newdirblk->db_mkdir); 8555 } 8556 /* 8557 * If we're creating a new directory setup the dependencies and set 8558 * the dap state to wait for them. Otherwise it's COMPLETE and 8559 * we can move on. 8560 */ 8561 if (newdirbp == NULL) { 8562 dap->da_state |= DEPCOMPLETE; 8563 ACQUIRE_LOCK(ump); 8564 } else { 8565 dap->da_state |= MKDIR_BODY | MKDIR_PARENT; 8566 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp, 8567 &mkdir2); 8568 } 8569 /* 8570 * Link into parent directory pagedep to await its being written. 8571 */ 8572 pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep); 8573 #ifdef INVARIANTS 8574 if (diradd_lookup(pagedep, offset) != NULL) 8575 panic("softdep_setup_directory_add: %p already at off %d\n", 8576 diradd_lookup(pagedep, offset), offset); 8577 #endif 8578 dap->da_pagedep = pagedep; 8579 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap, 8580 da_pdlist); 8581 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 8582 /* 8583 * If we're journaling, link the diradd into the jaddref so it 8584 * may be completed after the journal entry is written. Otherwise, 8585 * link the diradd into its inodedep. If the inode is not yet 8586 * written place it on the bufwait list, otherwise do the post-inode 8587 * write processing to put it on the id_pendinghd list. 8588 */ 8589 if (MOUNTEDSUJ(mp)) { 8590 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8591 inoreflst); 8592 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 8593 ("softdep_setup_directory_add: bad jaddref %p", jaddref)); 8594 jaddref->ja_diroff = diroffset; 8595 jaddref->ja_diradd = dap; 8596 add_to_journal(&jaddref->ja_list); 8597 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) 8598 diradd_inode_written(dap, inodedep); 8599 else 8600 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 8601 /* 8602 * Add the journal entries for . and .. links now that the primary 8603 * link is written. 8604 */ 8605 if (mkdir1 != NULL && MOUNTEDSUJ(mp)) { 8606 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 8607 inoreflst, if_deps); 8608 KASSERT(jaddref != NULL && 8609 jaddref->ja_ino == jaddref->ja_parent && 8610 (jaddref->ja_state & MKDIR_BODY), 8611 ("softdep_setup_directory_add: bad dot jaddref %p", 8612 jaddref)); 8613 mkdir1->md_jaddref = jaddref; 8614 jaddref->ja_mkdir = mkdir1; 8615 /* 8616 * It is important that the dotdot journal entry 8617 * is added prior to the dot entry since dot writes 8618 * both the dot and dotdot links. These both must 8619 * be added after the primary link for the journal 8620 * to remain consistent. 8621 */ 8622 add_to_journal(&mkdir2->md_jaddref->ja_list); 8623 add_to_journal(&jaddref->ja_list); 8624 } 8625 /* 8626 * If we are adding a new directory remember this diradd so that if 8627 * we rename it we can keep the dot and dotdot dependencies. If 8628 * we are adding a new name for an inode that has a mkdiradd we 8629 * must be in rename and we have to move the dot and dotdot 8630 * dependencies to this new name. The old name is being orphaned 8631 * soon. 8632 */ 8633 if (mkdir1 != NULL) { 8634 if (inodedep->id_mkdiradd != NULL) 8635 panic("softdep_setup_directory_add: Existing mkdir"); 8636 inodedep->id_mkdiradd = dap; 8637 } else if (inodedep->id_mkdiradd) 8638 merge_diradd(inodedep, dap); 8639 if (newdirblk != NULL) { 8640 /* 8641 * There is nothing to do if we are already tracking 8642 * this block. 8643 */ 8644 if ((pagedep->pd_state & NEWBLOCK) != 0) { 8645 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 8646 FREE_LOCK(ump); 8647 return (0); 8648 } 8649 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk) 8650 == 0) 8651 panic("softdep_setup_directory_add: lost entry"); 8652 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8653 pagedep->pd_state |= NEWBLOCK; 8654 pagedep->pd_newdirblk = newdirblk; 8655 newdirblk->db_pagedep = pagedep; 8656 FREE_LOCK(ump); 8657 /* 8658 * If we extended into an indirect signal direnter to sync. 8659 */ 8660 if (isindir) 8661 return (1); 8662 return (0); 8663 } 8664 FREE_LOCK(ump); 8665 return (0); 8666 } 8667 8668 /* 8669 * This procedure is called to change the offset of a directory 8670 * entry when compacting a directory block which must be owned 8671 * exclusively by the caller. Note that the actual entry movement 8672 * must be done in this procedure to ensure that no I/O completions 8673 * occur while the move is in progress. 8674 */ 8675 void 8676 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 8677 struct buf *bp; /* Buffer holding directory block. */ 8678 struct inode *dp; /* inode for directory */ 8679 caddr_t base; /* address of dp->i_offset */ 8680 caddr_t oldloc; /* address of old directory location */ 8681 caddr_t newloc; /* address of new directory location */ 8682 int entrysize; /* size of directory entry */ 8683 { 8684 int offset, oldoffset, newoffset; 8685 struct pagedep *pagedep; 8686 struct jmvref *jmvref; 8687 struct diradd *dap; 8688 struct direct *de; 8689 struct mount *mp; 8690 struct ufsmount *ump; 8691 ufs_lbn_t lbn; 8692 int flags; 8693 8694 mp = ITOVFS(dp); 8695 ump = VFSTOUFS(mp); 8696 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 8697 ("softdep_change_directoryentry_offset called on " 8698 "non-softdep filesystem")); 8699 de = (struct direct *)oldloc; 8700 jmvref = NULL; 8701 flags = 0; 8702 /* 8703 * Moves are always journaled as it would be too complex to 8704 * determine if any affected adds or removes are present in the 8705 * journal. 8706 */ 8707 if (MOUNTEDSUJ(mp)) { 8708 flags = DEPALLOC; 8709 jmvref = newjmvref(dp, de->d_ino, 8710 dp->i_offset + (oldloc - base), 8711 dp->i_offset + (newloc - base)); 8712 } 8713 lbn = lblkno(ump->um_fs, dp->i_offset); 8714 offset = blkoff(ump->um_fs, dp->i_offset); 8715 oldoffset = offset + (oldloc - base); 8716 newoffset = offset + (newloc - base); 8717 ACQUIRE_LOCK(ump); 8718 if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0) 8719 goto done; 8720 dap = diradd_lookup(pagedep, oldoffset); 8721 if (dap) { 8722 dap->da_offset = newoffset; 8723 newoffset = DIRADDHASH(newoffset); 8724 oldoffset = DIRADDHASH(oldoffset); 8725 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE && 8726 newoffset != oldoffset) { 8727 LIST_REMOVE(dap, da_pdlist); 8728 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset], 8729 dap, da_pdlist); 8730 } 8731 } 8732 done: 8733 if (jmvref) { 8734 jmvref->jm_pagedep = pagedep; 8735 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps); 8736 add_to_journal(&jmvref->jm_list); 8737 } 8738 bcopy(oldloc, newloc, entrysize); 8739 FREE_LOCK(ump); 8740 } 8741 8742 /* 8743 * Move the mkdir dependencies and journal work from one diradd to another 8744 * when renaming a directory. The new name must depend on the mkdir deps 8745 * completing as the old name did. Directories can only have one valid link 8746 * at a time so one must be canonical. 8747 */ 8748 static void 8749 merge_diradd(inodedep, newdap) 8750 struct inodedep *inodedep; 8751 struct diradd *newdap; 8752 { 8753 struct diradd *olddap; 8754 struct mkdir *mkdir, *nextmd; 8755 struct ufsmount *ump; 8756 short state; 8757 8758 olddap = inodedep->id_mkdiradd; 8759 inodedep->id_mkdiradd = newdap; 8760 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8761 newdap->da_state &= ~DEPCOMPLETE; 8762 ump = VFSTOUFS(inodedep->id_list.wk_mp); 8763 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 8764 mkdir = nextmd) { 8765 nextmd = LIST_NEXT(mkdir, md_mkdirs); 8766 if (mkdir->md_diradd != olddap) 8767 continue; 8768 mkdir->md_diradd = newdap; 8769 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY); 8770 newdap->da_state |= state; 8771 olddap->da_state &= ~state; 8772 if ((olddap->da_state & 8773 (MKDIR_PARENT | MKDIR_BODY)) == 0) 8774 break; 8775 } 8776 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 8777 panic("merge_diradd: unfound ref"); 8778 } 8779 /* 8780 * Any mkdir related journal items are not safe to be freed until 8781 * the new name is stable. 8782 */ 8783 jwork_move(&newdap->da_jwork, &olddap->da_jwork); 8784 olddap->da_state |= DEPCOMPLETE; 8785 complete_diradd(olddap); 8786 } 8787 8788 /* 8789 * Move the diradd to the pending list when all diradd dependencies are 8790 * complete. 8791 */ 8792 static void 8793 complete_diradd(dap) 8794 struct diradd *dap; 8795 { 8796 struct pagedep *pagedep; 8797 8798 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 8799 if (dap->da_state & DIRCHG) 8800 pagedep = dap->da_previous->dm_pagedep; 8801 else 8802 pagedep = dap->da_pagedep; 8803 LIST_REMOVE(dap, da_pdlist); 8804 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 8805 } 8806 } 8807 8808 /* 8809 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal 8810 * add entries and conditonally journal the remove. 8811 */ 8812 static void 8813 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref) 8814 struct diradd *dap; 8815 struct dirrem *dirrem; 8816 struct jremref *jremref; 8817 struct jremref *dotremref; 8818 struct jremref *dotdotremref; 8819 { 8820 struct inodedep *inodedep; 8821 struct jaddref *jaddref; 8822 struct inoref *inoref; 8823 struct ufsmount *ump; 8824 struct mkdir *mkdir; 8825 8826 /* 8827 * If no remove references were allocated we're on a non-journaled 8828 * filesystem and can skip the cancel step. 8829 */ 8830 if (jremref == NULL) { 8831 free_diradd(dap, NULL); 8832 return; 8833 } 8834 /* 8835 * Cancel the primary name an free it if it does not require 8836 * journaling. 8837 */ 8838 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum, 8839 0, &inodedep) != 0) { 8840 /* Abort the addref that reference this diradd. */ 8841 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 8842 if (inoref->if_list.wk_type != D_JADDREF) 8843 continue; 8844 jaddref = (struct jaddref *)inoref; 8845 if (jaddref->ja_diradd != dap) 8846 continue; 8847 if (cancel_jaddref(jaddref, inodedep, 8848 &dirrem->dm_jwork) == 0) { 8849 free_jremref(jremref); 8850 jremref = NULL; 8851 } 8852 break; 8853 } 8854 } 8855 /* 8856 * Cancel subordinate names and free them if they do not require 8857 * journaling. 8858 */ 8859 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8860 ump = VFSTOUFS(dap->da_list.wk_mp); 8861 LIST_FOREACH(mkdir, &ump->softdep_mkdirlisthd, md_mkdirs) { 8862 if (mkdir->md_diradd != dap) 8863 continue; 8864 if ((jaddref = mkdir->md_jaddref) == NULL) 8865 continue; 8866 mkdir->md_jaddref = NULL; 8867 if (mkdir->md_state & MKDIR_PARENT) { 8868 if (cancel_jaddref(jaddref, NULL, 8869 &dirrem->dm_jwork) == 0) { 8870 free_jremref(dotdotremref); 8871 dotdotremref = NULL; 8872 } 8873 } else { 8874 if (cancel_jaddref(jaddref, inodedep, 8875 &dirrem->dm_jwork) == 0) { 8876 free_jremref(dotremref); 8877 dotremref = NULL; 8878 } 8879 } 8880 } 8881 } 8882 8883 if (jremref) 8884 journal_jremref(dirrem, jremref, inodedep); 8885 if (dotremref) 8886 journal_jremref(dirrem, dotremref, inodedep); 8887 if (dotdotremref) 8888 journal_jremref(dirrem, dotdotremref, NULL); 8889 jwork_move(&dirrem->dm_jwork, &dap->da_jwork); 8890 free_diradd(dap, &dirrem->dm_jwork); 8891 } 8892 8893 /* 8894 * Free a diradd dependency structure. 8895 */ 8896 static void 8897 free_diradd(dap, wkhd) 8898 struct diradd *dap; 8899 struct workhead *wkhd; 8900 { 8901 struct dirrem *dirrem; 8902 struct pagedep *pagedep; 8903 struct inodedep *inodedep; 8904 struct mkdir *mkdir, *nextmd; 8905 struct ufsmount *ump; 8906 8907 ump = VFSTOUFS(dap->da_list.wk_mp); 8908 LOCK_OWNED(ump); 8909 LIST_REMOVE(dap, da_pdlist); 8910 if (dap->da_state & ONWORKLIST) 8911 WORKLIST_REMOVE(&dap->da_list); 8912 if ((dap->da_state & DIRCHG) == 0) { 8913 pagedep = dap->da_pagedep; 8914 } else { 8915 dirrem = dap->da_previous; 8916 pagedep = dirrem->dm_pagedep; 8917 dirrem->dm_dirinum = pagedep->pd_ino; 8918 dirrem->dm_state |= COMPLETE; 8919 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 8920 add_to_worklist(&dirrem->dm_list, 0); 8921 } 8922 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum, 8923 0, &inodedep) != 0) 8924 if (inodedep->id_mkdiradd == dap) 8925 inodedep->id_mkdiradd = NULL; 8926 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8927 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 8928 mkdir = nextmd) { 8929 nextmd = LIST_NEXT(mkdir, md_mkdirs); 8930 if (mkdir->md_diradd != dap) 8931 continue; 8932 dap->da_state &= 8933 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 8934 LIST_REMOVE(mkdir, md_mkdirs); 8935 if (mkdir->md_state & ONWORKLIST) 8936 WORKLIST_REMOVE(&mkdir->md_list); 8937 if (mkdir->md_jaddref != NULL) 8938 panic("free_diradd: Unexpected jaddref"); 8939 WORKITEM_FREE(mkdir, D_MKDIR); 8940 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) 8941 break; 8942 } 8943 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 8944 panic("free_diradd: unfound ref"); 8945 } 8946 if (inodedep) 8947 free_inodedep(inodedep); 8948 /* 8949 * Free any journal segments waiting for the directory write. 8950 */ 8951 handle_jwork(&dap->da_jwork); 8952 WORKITEM_FREE(dap, D_DIRADD); 8953 } 8954 8955 /* 8956 * Directory entry removal dependencies. 8957 * 8958 * When removing a directory entry, the entry's inode pointer must be 8959 * zero'ed on disk before the corresponding inode's link count is decremented 8960 * (possibly freeing the inode for re-use). This dependency is handled by 8961 * updating the directory entry but delaying the inode count reduction until 8962 * after the directory block has been written to disk. After this point, the 8963 * inode count can be decremented whenever it is convenient. 8964 */ 8965 8966 /* 8967 * This routine should be called immediately after removing 8968 * a directory entry. The inode's link count should not be 8969 * decremented by the calling procedure -- the soft updates 8970 * code will do this task when it is safe. 8971 */ 8972 void 8973 softdep_setup_remove(bp, dp, ip, isrmdir) 8974 struct buf *bp; /* buffer containing directory block */ 8975 struct inode *dp; /* inode for the directory being modified */ 8976 struct inode *ip; /* inode for directory entry being removed */ 8977 int isrmdir; /* indicates if doing RMDIR */ 8978 { 8979 struct dirrem *dirrem, *prevdirrem; 8980 struct inodedep *inodedep; 8981 struct ufsmount *ump; 8982 int direct; 8983 8984 ump = ITOUMP(ip); 8985 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 8986 ("softdep_setup_remove called on non-softdep filesystem")); 8987 /* 8988 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want 8989 * newdirrem() to setup the full directory remove which requires 8990 * isrmdir > 1. 8991 */ 8992 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 8993 /* 8994 * Add the dirrem to the inodedep's pending remove list for quick 8995 * discovery later. 8996 */ 8997 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) 8998 panic("softdep_setup_remove: Lost inodedep."); 8999 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 9000 dirrem->dm_state |= ONDEPLIST; 9001 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9002 9003 /* 9004 * If the COMPLETE flag is clear, then there were no active 9005 * entries and we want to roll back to a zeroed entry until 9006 * the new inode is committed to disk. If the COMPLETE flag is 9007 * set then we have deleted an entry that never made it to 9008 * disk. If the entry we deleted resulted from a name change, 9009 * then the old name still resides on disk. We cannot delete 9010 * its inode (returned to us in prevdirrem) until the zeroed 9011 * directory entry gets to disk. The new inode has never been 9012 * referenced on the disk, so can be deleted immediately. 9013 */ 9014 if ((dirrem->dm_state & COMPLETE) == 0) { 9015 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem, 9016 dm_next); 9017 FREE_LOCK(ump); 9018 } else { 9019 if (prevdirrem != NULL) 9020 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, 9021 prevdirrem, dm_next); 9022 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino; 9023 direct = LIST_EMPTY(&dirrem->dm_jremrefhd); 9024 FREE_LOCK(ump); 9025 if (direct) 9026 handle_workitem_remove(dirrem, 0); 9027 } 9028 } 9029 9030 /* 9031 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the 9032 * pd_pendinghd list of a pagedep. 9033 */ 9034 static struct diradd * 9035 diradd_lookup(pagedep, offset) 9036 struct pagedep *pagedep; 9037 int offset; 9038 { 9039 struct diradd *dap; 9040 9041 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist) 9042 if (dap->da_offset == offset) 9043 return (dap); 9044 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 9045 if (dap->da_offset == offset) 9046 return (dap); 9047 return (NULL); 9048 } 9049 9050 /* 9051 * Search for a .. diradd dependency in a directory that is being removed. 9052 * If the directory was renamed to a new parent we have a diradd rather 9053 * than a mkdir for the .. entry. We need to cancel it now before 9054 * it is found in truncate(). 9055 */ 9056 static struct jremref * 9057 cancel_diradd_dotdot(ip, dirrem, jremref) 9058 struct inode *ip; 9059 struct dirrem *dirrem; 9060 struct jremref *jremref; 9061 { 9062 struct pagedep *pagedep; 9063 struct diradd *dap; 9064 struct worklist *wk; 9065 9066 if (pagedep_lookup(ITOVFS(ip), NULL, ip->i_number, 0, 0, &pagedep) == 0) 9067 return (jremref); 9068 dap = diradd_lookup(pagedep, DOTDOT_OFFSET); 9069 if (dap == NULL) 9070 return (jremref); 9071 cancel_diradd(dap, dirrem, jremref, NULL, NULL); 9072 /* 9073 * Mark any journal work as belonging to the parent so it is freed 9074 * with the .. reference. 9075 */ 9076 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9077 wk->wk_state |= MKDIR_PARENT; 9078 return (NULL); 9079 } 9080 9081 /* 9082 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to 9083 * replace it with a dirrem/diradd pair as a result of re-parenting a 9084 * directory. This ensures that we don't simultaneously have a mkdir and 9085 * a diradd for the same .. entry. 9086 */ 9087 static struct jremref * 9088 cancel_mkdir_dotdot(ip, dirrem, jremref) 9089 struct inode *ip; 9090 struct dirrem *dirrem; 9091 struct jremref *jremref; 9092 { 9093 struct inodedep *inodedep; 9094 struct jaddref *jaddref; 9095 struct ufsmount *ump; 9096 struct mkdir *mkdir; 9097 struct diradd *dap; 9098 struct mount *mp; 9099 9100 mp = ITOVFS(ip); 9101 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9102 return (jremref); 9103 dap = inodedep->id_mkdiradd; 9104 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0) 9105 return (jremref); 9106 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9107 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 9108 mkdir = LIST_NEXT(mkdir, md_mkdirs)) 9109 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT) 9110 break; 9111 if (mkdir == NULL) 9112 panic("cancel_mkdir_dotdot: Unable to find mkdir\n"); 9113 if ((jaddref = mkdir->md_jaddref) != NULL) { 9114 mkdir->md_jaddref = NULL; 9115 jaddref->ja_state &= ~MKDIR_PARENT; 9116 if (inodedep_lookup(mp, jaddref->ja_ino, 0, &inodedep) == 0) 9117 panic("cancel_mkdir_dotdot: Lost parent inodedep"); 9118 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) { 9119 journal_jremref(dirrem, jremref, inodedep); 9120 jremref = NULL; 9121 } 9122 } 9123 if (mkdir->md_state & ONWORKLIST) 9124 WORKLIST_REMOVE(&mkdir->md_list); 9125 mkdir->md_state |= ALLCOMPLETE; 9126 complete_mkdir(mkdir); 9127 return (jremref); 9128 } 9129 9130 static void 9131 journal_jremref(dirrem, jremref, inodedep) 9132 struct dirrem *dirrem; 9133 struct jremref *jremref; 9134 struct inodedep *inodedep; 9135 { 9136 9137 if (inodedep == NULL) 9138 if (inodedep_lookup(jremref->jr_list.wk_mp, 9139 jremref->jr_ref.if_ino, 0, &inodedep) == 0) 9140 panic("journal_jremref: Lost inodedep"); 9141 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps); 9142 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 9143 add_to_journal(&jremref->jr_list); 9144 } 9145 9146 static void 9147 dirrem_journal(dirrem, jremref, dotremref, dotdotremref) 9148 struct dirrem *dirrem; 9149 struct jremref *jremref; 9150 struct jremref *dotremref; 9151 struct jremref *dotdotremref; 9152 { 9153 struct inodedep *inodedep; 9154 9155 9156 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0, 9157 &inodedep) == 0) 9158 panic("dirrem_journal: Lost inodedep"); 9159 journal_jremref(dirrem, jremref, inodedep); 9160 if (dotremref) 9161 journal_jremref(dirrem, dotremref, inodedep); 9162 if (dotdotremref) 9163 journal_jremref(dirrem, dotdotremref, NULL); 9164 } 9165 9166 /* 9167 * Allocate a new dirrem if appropriate and return it along with 9168 * its associated pagedep. Called without a lock, returns with lock. 9169 */ 9170 static struct dirrem * 9171 newdirrem(bp, dp, ip, isrmdir, prevdirremp) 9172 struct buf *bp; /* buffer containing directory block */ 9173 struct inode *dp; /* inode for the directory being modified */ 9174 struct inode *ip; /* inode for directory entry being removed */ 9175 int isrmdir; /* indicates if doing RMDIR */ 9176 struct dirrem **prevdirremp; /* previously referenced inode, if any */ 9177 { 9178 int offset; 9179 ufs_lbn_t lbn; 9180 struct diradd *dap; 9181 struct dirrem *dirrem; 9182 struct pagedep *pagedep; 9183 struct jremref *jremref; 9184 struct jremref *dotremref; 9185 struct jremref *dotdotremref; 9186 struct vnode *dvp; 9187 struct ufsmount *ump; 9188 9189 /* 9190 * Whiteouts have no deletion dependencies. 9191 */ 9192 if (ip == NULL) 9193 panic("newdirrem: whiteout"); 9194 dvp = ITOV(dp); 9195 ump = ITOUMP(dp); 9196 9197 /* 9198 * If the system is over its limit and our filesystem is 9199 * responsible for more than our share of that usage and 9200 * we are not a snapshot, request some inodedep cleanup. 9201 * Limiting the number of dirrem structures will also limit 9202 * the number of freefile and freeblks structures. 9203 */ 9204 ACQUIRE_LOCK(ump); 9205 if (!IS_SNAPSHOT(ip) && softdep_excess_items(ump, D_DIRREM)) 9206 schedule_cleanup(UFSTOVFS(ump)); 9207 else 9208 FREE_LOCK(ump); 9209 dirrem = malloc(sizeof(struct dirrem), M_DIRREM, M_SOFTDEP_FLAGS | 9210 M_ZERO); 9211 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount); 9212 LIST_INIT(&dirrem->dm_jremrefhd); 9213 LIST_INIT(&dirrem->dm_jwork); 9214 dirrem->dm_state = isrmdir ? RMDIR : 0; 9215 dirrem->dm_oldinum = ip->i_number; 9216 *prevdirremp = NULL; 9217 /* 9218 * Allocate remove reference structures to track journal write 9219 * dependencies. We will always have one for the link and 9220 * when doing directories we will always have one more for dot. 9221 * When renaming a directory we skip the dotdot link change so 9222 * this is not needed. 9223 */ 9224 jremref = dotremref = dotdotremref = NULL; 9225 if (DOINGSUJ(dvp)) { 9226 if (isrmdir) { 9227 jremref = newjremref(dirrem, dp, ip, dp->i_offset, 9228 ip->i_effnlink + 2); 9229 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET, 9230 ip->i_effnlink + 1); 9231 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET, 9232 dp->i_effnlink + 1); 9233 dotdotremref->jr_state |= MKDIR_PARENT; 9234 } else 9235 jremref = newjremref(dirrem, dp, ip, dp->i_offset, 9236 ip->i_effnlink + 1); 9237 } 9238 ACQUIRE_LOCK(ump); 9239 lbn = lblkno(ump->um_fs, dp->i_offset); 9240 offset = blkoff(ump->um_fs, dp->i_offset); 9241 pagedep_lookup(UFSTOVFS(ump), bp, dp->i_number, lbn, DEPALLOC, 9242 &pagedep); 9243 dirrem->dm_pagedep = pagedep; 9244 dirrem->dm_offset = offset; 9245 /* 9246 * If we're renaming a .. link to a new directory, cancel any 9247 * existing MKDIR_PARENT mkdir. If it has already been canceled 9248 * the jremref is preserved for any potential diradd in this 9249 * location. This can not coincide with a rmdir. 9250 */ 9251 if (dp->i_offset == DOTDOT_OFFSET) { 9252 if (isrmdir) 9253 panic("newdirrem: .. directory change during remove?"); 9254 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref); 9255 } 9256 /* 9257 * If we're removing a directory search for the .. dependency now and 9258 * cancel it. Any pending journal work will be added to the dirrem 9259 * to be completed when the workitem remove completes. 9260 */ 9261 if (isrmdir) 9262 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref); 9263 /* 9264 * Check for a diradd dependency for the same directory entry. 9265 * If present, then both dependencies become obsolete and can 9266 * be de-allocated. 9267 */ 9268 dap = diradd_lookup(pagedep, offset); 9269 if (dap == NULL) { 9270 /* 9271 * Link the jremref structures into the dirrem so they are 9272 * written prior to the pagedep. 9273 */ 9274 if (jremref) 9275 dirrem_journal(dirrem, jremref, dotremref, 9276 dotdotremref); 9277 return (dirrem); 9278 } 9279 /* 9280 * Must be ATTACHED at this point. 9281 */ 9282 if ((dap->da_state & ATTACHED) == 0) 9283 panic("newdirrem: not ATTACHED"); 9284 if (dap->da_newinum != ip->i_number) 9285 panic("newdirrem: inum %ju should be %ju", 9286 (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum); 9287 /* 9288 * If we are deleting a changed name that never made it to disk, 9289 * then return the dirrem describing the previous inode (which 9290 * represents the inode currently referenced from this entry on disk). 9291 */ 9292 if ((dap->da_state & DIRCHG) != 0) { 9293 *prevdirremp = dap->da_previous; 9294 dap->da_state &= ~DIRCHG; 9295 dap->da_pagedep = pagedep; 9296 } 9297 /* 9298 * We are deleting an entry that never made it to disk. 9299 * Mark it COMPLETE so we can delete its inode immediately. 9300 */ 9301 dirrem->dm_state |= COMPLETE; 9302 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref); 9303 #ifdef INVARIANTS 9304 if (isrmdir == 0) { 9305 struct worklist *wk; 9306 9307 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9308 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT)) 9309 panic("bad wk %p (0x%X)\n", wk, wk->wk_state); 9310 } 9311 #endif 9312 9313 return (dirrem); 9314 } 9315 9316 /* 9317 * Directory entry change dependencies. 9318 * 9319 * Changing an existing directory entry requires that an add operation 9320 * be completed first followed by a deletion. The semantics for the addition 9321 * are identical to the description of adding a new entry above except 9322 * that the rollback is to the old inode number rather than zero. Once 9323 * the addition dependency is completed, the removal is done as described 9324 * in the removal routine above. 9325 */ 9326 9327 /* 9328 * This routine should be called immediately after changing 9329 * a directory entry. The inode's link count should not be 9330 * decremented by the calling procedure -- the soft updates 9331 * code will perform this task when it is safe. 9332 */ 9333 void 9334 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 9335 struct buf *bp; /* buffer containing directory block */ 9336 struct inode *dp; /* inode for the directory being modified */ 9337 struct inode *ip; /* inode for directory entry being removed */ 9338 ino_t newinum; /* new inode number for changed entry */ 9339 int isrmdir; /* indicates if doing RMDIR */ 9340 { 9341 int offset; 9342 struct diradd *dap = NULL; 9343 struct dirrem *dirrem, *prevdirrem; 9344 struct pagedep *pagedep; 9345 struct inodedep *inodedep; 9346 struct jaddref *jaddref; 9347 struct mount *mp; 9348 struct ufsmount *ump; 9349 9350 mp = ITOVFS(dp); 9351 ump = VFSTOUFS(mp); 9352 offset = blkoff(ump->um_fs, dp->i_offset); 9353 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 9354 ("softdep_setup_directory_change called on non-softdep filesystem")); 9355 9356 /* 9357 * Whiteouts do not need diradd dependencies. 9358 */ 9359 if (newinum != UFS_WINO) { 9360 dap = malloc(sizeof(struct diradd), 9361 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO); 9362 workitem_alloc(&dap->da_list, D_DIRADD, mp); 9363 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE; 9364 dap->da_offset = offset; 9365 dap->da_newinum = newinum; 9366 LIST_INIT(&dap->da_jwork); 9367 } 9368 9369 /* 9370 * Allocate a new dirrem and ACQUIRE_LOCK. 9371 */ 9372 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 9373 pagedep = dirrem->dm_pagedep; 9374 /* 9375 * The possible values for isrmdir: 9376 * 0 - non-directory file rename 9377 * 1 - directory rename within same directory 9378 * inum - directory rename to new directory of given inode number 9379 * When renaming to a new directory, we are both deleting and 9380 * creating a new directory entry, so the link count on the new 9381 * directory should not change. Thus we do not need the followup 9382 * dirrem which is usually done in handle_workitem_remove. We set 9383 * the DIRCHG flag to tell handle_workitem_remove to skip the 9384 * followup dirrem. 9385 */ 9386 if (isrmdir > 1) 9387 dirrem->dm_state |= DIRCHG; 9388 9389 /* 9390 * Whiteouts have no additional dependencies, 9391 * so just put the dirrem on the correct list. 9392 */ 9393 if (newinum == UFS_WINO) { 9394 if ((dirrem->dm_state & COMPLETE) == 0) { 9395 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem, 9396 dm_next); 9397 } else { 9398 dirrem->dm_dirinum = pagedep->pd_ino; 9399 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9400 add_to_worklist(&dirrem->dm_list, 0); 9401 } 9402 FREE_LOCK(ump); 9403 return; 9404 } 9405 /* 9406 * Add the dirrem to the inodedep's pending remove list for quick 9407 * discovery later. A valid nlinkdelta ensures that this lookup 9408 * will not fail. 9409 */ 9410 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9411 panic("softdep_setup_directory_change: Lost inodedep."); 9412 dirrem->dm_state |= ONDEPLIST; 9413 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9414 9415 /* 9416 * If the COMPLETE flag is clear, then there were no active 9417 * entries and we want to roll back to the previous inode until 9418 * the new inode is committed to disk. If the COMPLETE flag is 9419 * set, then we have deleted an entry that never made it to disk. 9420 * If the entry we deleted resulted from a name change, then the old 9421 * inode reference still resides on disk. Any rollback that we do 9422 * needs to be to that old inode (returned to us in prevdirrem). If 9423 * the entry we deleted resulted from a create, then there is 9424 * no entry on the disk, so we want to roll back to zero rather 9425 * than the uncommitted inode. In either of the COMPLETE cases we 9426 * want to immediately free the unwritten and unreferenced inode. 9427 */ 9428 if ((dirrem->dm_state & COMPLETE) == 0) { 9429 dap->da_previous = dirrem; 9430 } else { 9431 if (prevdirrem != NULL) { 9432 dap->da_previous = prevdirrem; 9433 } else { 9434 dap->da_state &= ~DIRCHG; 9435 dap->da_pagedep = pagedep; 9436 } 9437 dirrem->dm_dirinum = pagedep->pd_ino; 9438 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9439 add_to_worklist(&dirrem->dm_list, 0); 9440 } 9441 /* 9442 * Lookup the jaddref for this journal entry. We must finish 9443 * initializing it and make the diradd write dependent on it. 9444 * If we're not journaling, put it on the id_bufwait list if the 9445 * inode is not yet written. If it is written, do the post-inode 9446 * write processing to put it on the id_pendinghd list. 9447 */ 9448 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 9449 if (MOUNTEDSUJ(mp)) { 9450 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 9451 inoreflst); 9452 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 9453 ("softdep_setup_directory_change: bad jaddref %p", 9454 jaddref)); 9455 jaddref->ja_diroff = dp->i_offset; 9456 jaddref->ja_diradd = dap; 9457 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9458 dap, da_pdlist); 9459 add_to_journal(&jaddref->ja_list); 9460 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 9461 dap->da_state |= COMPLETE; 9462 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 9463 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 9464 } else { 9465 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9466 dap, da_pdlist); 9467 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 9468 } 9469 /* 9470 * If we're making a new name for a directory that has not been 9471 * committed when need to move the dot and dotdot references to 9472 * this new name. 9473 */ 9474 if (inodedep->id_mkdiradd && dp->i_offset != DOTDOT_OFFSET) 9475 merge_diradd(inodedep, dap); 9476 FREE_LOCK(ump); 9477 } 9478 9479 /* 9480 * Called whenever the link count on an inode is changed. 9481 * It creates an inode dependency so that the new reference(s) 9482 * to the inode cannot be committed to disk until the updated 9483 * inode has been written. 9484 */ 9485 void 9486 softdep_change_linkcnt(ip) 9487 struct inode *ip; /* the inode with the increased link count */ 9488 { 9489 struct inodedep *inodedep; 9490 struct ufsmount *ump; 9491 9492 ump = ITOUMP(ip); 9493 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9494 ("softdep_change_linkcnt called on non-softdep filesystem")); 9495 ACQUIRE_LOCK(ump); 9496 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep); 9497 if (ip->i_nlink < ip->i_effnlink) 9498 panic("softdep_change_linkcnt: bad delta"); 9499 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9500 FREE_LOCK(ump); 9501 } 9502 9503 /* 9504 * Attach a sbdep dependency to the superblock buf so that we can keep 9505 * track of the head of the linked list of referenced but unlinked inodes. 9506 */ 9507 void 9508 softdep_setup_sbupdate(ump, fs, bp) 9509 struct ufsmount *ump; 9510 struct fs *fs; 9511 struct buf *bp; 9512 { 9513 struct sbdep *sbdep; 9514 struct worklist *wk; 9515 9516 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9517 ("softdep_setup_sbupdate called on non-softdep filesystem")); 9518 LIST_FOREACH(wk, &bp->b_dep, wk_list) 9519 if (wk->wk_type == D_SBDEP) 9520 break; 9521 if (wk != NULL) 9522 return; 9523 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS); 9524 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump)); 9525 sbdep->sb_fs = fs; 9526 sbdep->sb_ump = ump; 9527 ACQUIRE_LOCK(ump); 9528 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list); 9529 FREE_LOCK(ump); 9530 } 9531 9532 /* 9533 * Return the first unlinked inodedep which is ready to be the head of the 9534 * list. The inodedep and all those after it must have valid next pointers. 9535 */ 9536 static struct inodedep * 9537 first_unlinked_inodedep(ump) 9538 struct ufsmount *ump; 9539 { 9540 struct inodedep *inodedep; 9541 struct inodedep *idp; 9542 9543 LOCK_OWNED(ump); 9544 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst); 9545 inodedep; inodedep = idp) { 9546 if ((inodedep->id_state & UNLINKNEXT) == 0) 9547 return (NULL); 9548 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9549 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0) 9550 break; 9551 if ((inodedep->id_state & UNLINKPREV) == 0) 9552 break; 9553 } 9554 return (inodedep); 9555 } 9556 9557 /* 9558 * Set the sujfree unlinked head pointer prior to writing a superblock. 9559 */ 9560 static void 9561 initiate_write_sbdep(sbdep) 9562 struct sbdep *sbdep; 9563 { 9564 struct inodedep *inodedep; 9565 struct fs *bpfs; 9566 struct fs *fs; 9567 9568 bpfs = sbdep->sb_fs; 9569 fs = sbdep->sb_ump->um_fs; 9570 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9571 if (inodedep) { 9572 fs->fs_sujfree = inodedep->id_ino; 9573 inodedep->id_state |= UNLINKPREV; 9574 } else 9575 fs->fs_sujfree = 0; 9576 bpfs->fs_sujfree = fs->fs_sujfree; 9577 /* 9578 * Because we have made changes to the superblock, we need to 9579 * recompute its check-hash. 9580 */ 9581 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs); 9582 } 9583 9584 /* 9585 * After a superblock is written determine whether it must be written again 9586 * due to a changing unlinked list head. 9587 */ 9588 static int 9589 handle_written_sbdep(sbdep, bp) 9590 struct sbdep *sbdep; 9591 struct buf *bp; 9592 { 9593 struct inodedep *inodedep; 9594 struct fs *fs; 9595 9596 LOCK_OWNED(sbdep->sb_ump); 9597 fs = sbdep->sb_fs; 9598 /* 9599 * If the superblock doesn't match the in-memory list start over. 9600 */ 9601 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9602 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) || 9603 (inodedep == NULL && fs->fs_sujfree != 0)) { 9604 bdirty(bp); 9605 return (1); 9606 } 9607 WORKITEM_FREE(sbdep, D_SBDEP); 9608 if (fs->fs_sujfree == 0) 9609 return (0); 9610 /* 9611 * Now that we have a record of this inode in stable store allow it 9612 * to be written to free up pending work. Inodes may see a lot of 9613 * write activity after they are unlinked which we must not hold up. 9614 */ 9615 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 9616 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS) 9617 panic("handle_written_sbdep: Bad inodedep %p (0x%X)", 9618 inodedep, inodedep->id_state); 9619 if (inodedep->id_state & UNLINKONLIST) 9620 break; 9621 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST; 9622 } 9623 9624 return (0); 9625 } 9626 9627 /* 9628 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list. 9629 */ 9630 static void 9631 unlinked_inodedep(mp, inodedep) 9632 struct mount *mp; 9633 struct inodedep *inodedep; 9634 { 9635 struct ufsmount *ump; 9636 9637 ump = VFSTOUFS(mp); 9638 LOCK_OWNED(ump); 9639 if (MOUNTEDSUJ(mp) == 0) 9640 return; 9641 ump->um_fs->fs_fmod = 1; 9642 if (inodedep->id_state & UNLINKED) 9643 panic("unlinked_inodedep: %p already unlinked\n", inodedep); 9644 inodedep->id_state |= UNLINKED; 9645 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked); 9646 } 9647 9648 /* 9649 * Remove an inodedep from the unlinked inodedep list. This may require 9650 * disk writes if the inode has made it that far. 9651 */ 9652 static void 9653 clear_unlinked_inodedep(inodedep) 9654 struct inodedep *inodedep; 9655 { 9656 struct ufs2_dinode *dip; 9657 struct ufsmount *ump; 9658 struct inodedep *idp; 9659 struct inodedep *idn; 9660 struct fs *fs, *bpfs; 9661 struct buf *bp; 9662 ino_t ino; 9663 ino_t nino; 9664 ino_t pino; 9665 int error; 9666 9667 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9668 fs = ump->um_fs; 9669 ino = inodedep->id_ino; 9670 error = 0; 9671 for (;;) { 9672 LOCK_OWNED(ump); 9673 KASSERT((inodedep->id_state & UNLINKED) != 0, 9674 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9675 inodedep)); 9676 /* 9677 * If nothing has yet been written simply remove us from 9678 * the in memory list and return. This is the most common 9679 * case where handle_workitem_remove() loses the final 9680 * reference. 9681 */ 9682 if ((inodedep->id_state & UNLINKLINKS) == 0) 9683 break; 9684 /* 9685 * If we have a NEXT pointer and no PREV pointer we can simply 9686 * clear NEXT's PREV and remove ourselves from the list. Be 9687 * careful not to clear PREV if the superblock points at 9688 * next as well. 9689 */ 9690 idn = TAILQ_NEXT(inodedep, id_unlinked); 9691 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) { 9692 if (idn && fs->fs_sujfree != idn->id_ino) 9693 idn->id_state &= ~UNLINKPREV; 9694 break; 9695 } 9696 /* 9697 * Here we have an inodedep which is actually linked into 9698 * the list. We must remove it by forcing a write to the 9699 * link before us, whether it be the superblock or an inode. 9700 * Unfortunately the list may change while we're waiting 9701 * on the buf lock for either resource so we must loop until 9702 * we lock the right one. If both the superblock and an 9703 * inode point to this inode we must clear the inode first 9704 * followed by the superblock. 9705 */ 9706 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9707 pino = 0; 9708 if (idp && (idp->id_state & UNLINKNEXT)) 9709 pino = idp->id_ino; 9710 FREE_LOCK(ump); 9711 if (pino == 0) { 9712 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9713 (int)fs->fs_sbsize, 0, 0, 0); 9714 } else { 9715 error = bread(ump->um_devvp, 9716 fsbtodb(fs, ino_to_fsba(fs, pino)), 9717 (int)fs->fs_bsize, NOCRED, &bp); 9718 if (error) 9719 brelse(bp); 9720 } 9721 ACQUIRE_LOCK(ump); 9722 if (error) 9723 break; 9724 /* If the list has changed restart the loop. */ 9725 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9726 nino = 0; 9727 if (idp && (idp->id_state & UNLINKNEXT)) 9728 nino = idp->id_ino; 9729 if (nino != pino || 9730 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) { 9731 FREE_LOCK(ump); 9732 brelse(bp); 9733 ACQUIRE_LOCK(ump); 9734 continue; 9735 } 9736 nino = 0; 9737 idn = TAILQ_NEXT(inodedep, id_unlinked); 9738 if (idn) 9739 nino = idn->id_ino; 9740 /* 9741 * Remove us from the in memory list. After this we cannot 9742 * access the inodedep. 9743 */ 9744 KASSERT((inodedep->id_state & UNLINKED) != 0, 9745 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9746 inodedep)); 9747 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9748 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9749 FREE_LOCK(ump); 9750 /* 9751 * The predecessor's next pointer is manually updated here 9752 * so that the NEXT flag is never cleared for an element 9753 * that is in the list. 9754 */ 9755 if (pino == 0) { 9756 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 9757 bpfs = (struct fs *)bp->b_data; 9758 ffs_oldfscompat_write(bpfs, ump); 9759 softdep_setup_sbupdate(ump, bpfs, bp); 9760 /* 9761 * Because we may have made changes to the superblock, 9762 * we need to recompute its check-hash. 9763 */ 9764 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs); 9765 } else if (fs->fs_magic == FS_UFS1_MAGIC) { 9766 ((struct ufs1_dinode *)bp->b_data + 9767 ino_to_fsbo(fs, pino))->di_freelink = nino; 9768 } else { 9769 dip = (struct ufs2_dinode *)bp->b_data + 9770 ino_to_fsbo(fs, pino); 9771 dip->di_freelink = nino; 9772 ffs_update_dinode_ckhash(fs, dip); 9773 } 9774 /* 9775 * If the bwrite fails we have no recourse to recover. The 9776 * filesystem is corrupted already. 9777 */ 9778 bwrite(bp); 9779 ACQUIRE_LOCK(ump); 9780 /* 9781 * If the superblock pointer still needs to be cleared force 9782 * a write here. 9783 */ 9784 if (fs->fs_sujfree == ino) { 9785 FREE_LOCK(ump); 9786 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9787 (int)fs->fs_sbsize, 0, 0, 0); 9788 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 9789 bpfs = (struct fs *)bp->b_data; 9790 ffs_oldfscompat_write(bpfs, ump); 9791 softdep_setup_sbupdate(ump, bpfs, bp); 9792 /* 9793 * Because we may have made changes to the superblock, 9794 * we need to recompute its check-hash. 9795 */ 9796 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs); 9797 bwrite(bp); 9798 ACQUIRE_LOCK(ump); 9799 } 9800 9801 if (fs->fs_sujfree != ino) 9802 return; 9803 panic("clear_unlinked_inodedep: Failed to clear free head"); 9804 } 9805 if (inodedep->id_ino == fs->fs_sujfree) 9806 panic("clear_unlinked_inodedep: Freeing head of free list"); 9807 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9808 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9809 return; 9810 } 9811 9812 /* 9813 * This workitem decrements the inode's link count. 9814 * If the link count reaches zero, the file is removed. 9815 */ 9816 static int 9817 handle_workitem_remove(dirrem, flags) 9818 struct dirrem *dirrem; 9819 int flags; 9820 { 9821 struct inodedep *inodedep; 9822 struct workhead dotdotwk; 9823 struct worklist *wk; 9824 struct ufsmount *ump; 9825 struct mount *mp; 9826 struct vnode *vp; 9827 struct inode *ip; 9828 ino_t oldinum; 9829 9830 if (dirrem->dm_state & ONWORKLIST) 9831 panic("handle_workitem_remove: dirrem %p still on worklist", 9832 dirrem); 9833 oldinum = dirrem->dm_oldinum; 9834 mp = dirrem->dm_list.wk_mp; 9835 ump = VFSTOUFS(mp); 9836 flags |= LK_EXCLUSIVE; 9837 if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ) != 0) 9838 return (EBUSY); 9839 ip = VTOI(vp); 9840 MPASS(ip->i_mode != 0); 9841 ACQUIRE_LOCK(ump); 9842 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0) 9843 panic("handle_workitem_remove: lost inodedep"); 9844 if (dirrem->dm_state & ONDEPLIST) 9845 LIST_REMOVE(dirrem, dm_inonext); 9846 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 9847 ("handle_workitem_remove: Journal entries not written.")); 9848 9849 /* 9850 * Move all dependencies waiting on the remove to complete 9851 * from the dirrem to the inode inowait list to be completed 9852 * after the inode has been updated and written to disk. Any 9853 * marked MKDIR_PARENT are saved to be completed when the .. ref 9854 * is removed. 9855 */ 9856 LIST_INIT(&dotdotwk); 9857 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) { 9858 WORKLIST_REMOVE(wk); 9859 if (wk->wk_state & MKDIR_PARENT) { 9860 wk->wk_state &= ~MKDIR_PARENT; 9861 WORKLIST_INSERT(&dotdotwk, wk); 9862 continue; 9863 } 9864 WORKLIST_INSERT(&inodedep->id_inowait, wk); 9865 } 9866 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list); 9867 /* 9868 * Normal file deletion. 9869 */ 9870 if ((dirrem->dm_state & RMDIR) == 0) { 9871 ip->i_nlink--; 9872 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: file ino " 9873 "%ju negative i_nlink %d", (intmax_t)ip->i_number, 9874 ip->i_nlink)); 9875 DIP_SET(ip, i_nlink, ip->i_nlink); 9876 ip->i_flag |= IN_CHANGE; 9877 if (ip->i_nlink < ip->i_effnlink) 9878 panic("handle_workitem_remove: bad file delta"); 9879 if (ip->i_nlink == 0) 9880 unlinked_inodedep(mp, inodedep); 9881 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9882 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 9883 ("handle_workitem_remove: worklist not empty. %s", 9884 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type))); 9885 WORKITEM_FREE(dirrem, D_DIRREM); 9886 FREE_LOCK(ump); 9887 goto out; 9888 } 9889 /* 9890 * Directory deletion. Decrement reference count for both the 9891 * just deleted parent directory entry and the reference for ".". 9892 * Arrange to have the reference count on the parent decremented 9893 * to account for the loss of "..". 9894 */ 9895 ip->i_nlink -= 2; 9896 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: directory ino " 9897 "%ju negative i_nlink %d", (intmax_t)ip->i_number, ip->i_nlink)); 9898 DIP_SET(ip, i_nlink, ip->i_nlink); 9899 ip->i_flag |= IN_CHANGE; 9900 if (ip->i_nlink < ip->i_effnlink) 9901 panic("handle_workitem_remove: bad dir delta"); 9902 if (ip->i_nlink == 0) 9903 unlinked_inodedep(mp, inodedep); 9904 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9905 /* 9906 * Rename a directory to a new parent. Since, we are both deleting 9907 * and creating a new directory entry, the link count on the new 9908 * directory should not change. Thus we skip the followup dirrem. 9909 */ 9910 if (dirrem->dm_state & DIRCHG) { 9911 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 9912 ("handle_workitem_remove: DIRCHG and worklist not empty.")); 9913 WORKITEM_FREE(dirrem, D_DIRREM); 9914 FREE_LOCK(ump); 9915 goto out; 9916 } 9917 dirrem->dm_state = ONDEPLIST; 9918 dirrem->dm_oldinum = dirrem->dm_dirinum; 9919 /* 9920 * Place the dirrem on the parent's diremhd list. 9921 */ 9922 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0) 9923 panic("handle_workitem_remove: lost dir inodedep"); 9924 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9925 /* 9926 * If the allocated inode has never been written to disk, then 9927 * the on-disk inode is zero'ed and we can remove the file 9928 * immediately. When journaling if the inode has been marked 9929 * unlinked and not DEPCOMPLETE we know it can never be written. 9930 */ 9931 inodedep_lookup(mp, oldinum, 0, &inodedep); 9932 if (inodedep == NULL || 9933 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED || 9934 check_inode_unwritten(inodedep)) { 9935 FREE_LOCK(ump); 9936 vput(vp); 9937 return handle_workitem_remove(dirrem, flags); 9938 } 9939 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list); 9940 FREE_LOCK(ump); 9941 ip->i_flag |= IN_CHANGE; 9942 out: 9943 ffs_update(vp, 0); 9944 vput(vp); 9945 return (0); 9946 } 9947 9948 /* 9949 * Inode de-allocation dependencies. 9950 * 9951 * When an inode's link count is reduced to zero, it can be de-allocated. We 9952 * found it convenient to postpone de-allocation until after the inode is 9953 * written to disk with its new link count (zero). At this point, all of the 9954 * on-disk inode's block pointers are nullified and, with careful dependency 9955 * list ordering, all dependencies related to the inode will be satisfied and 9956 * the corresponding dependency structures de-allocated. So, if/when the 9957 * inode is reused, there will be no mixing of old dependencies with new 9958 * ones. This artificial dependency is set up by the block de-allocation 9959 * procedure above (softdep_setup_freeblocks) and completed by the 9960 * following procedure. 9961 */ 9962 static void 9963 handle_workitem_freefile(freefile) 9964 struct freefile *freefile; 9965 { 9966 struct workhead wkhd; 9967 struct fs *fs; 9968 struct ufsmount *ump; 9969 int error; 9970 #ifdef INVARIANTS 9971 struct inodedep *idp; 9972 #endif 9973 9974 ump = VFSTOUFS(freefile->fx_list.wk_mp); 9975 fs = ump->um_fs; 9976 #ifdef INVARIANTS 9977 ACQUIRE_LOCK(ump); 9978 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp); 9979 FREE_LOCK(ump); 9980 if (error) 9981 panic("handle_workitem_freefile: inodedep %p survived", idp); 9982 #endif 9983 UFS_LOCK(ump); 9984 fs->fs_pendinginodes -= 1; 9985 UFS_UNLOCK(ump); 9986 LIST_INIT(&wkhd); 9987 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list); 9988 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp, 9989 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0) 9990 softdep_error("handle_workitem_freefile", error); 9991 ACQUIRE_LOCK(ump); 9992 WORKITEM_FREE(freefile, D_FREEFILE); 9993 FREE_LOCK(ump); 9994 } 9995 9996 9997 /* 9998 * Helper function which unlinks marker element from work list and returns 9999 * the next element on the list. 10000 */ 10001 static __inline struct worklist * 10002 markernext(struct worklist *marker) 10003 { 10004 struct worklist *next; 10005 10006 next = LIST_NEXT(marker, wk_list); 10007 LIST_REMOVE(marker, wk_list); 10008 return next; 10009 } 10010 10011 /* 10012 * Disk writes. 10013 * 10014 * The dependency structures constructed above are most actively used when file 10015 * system blocks are written to disk. No constraints are placed on when a 10016 * block can be written, but unsatisfied update dependencies are made safe by 10017 * modifying (or replacing) the source memory for the duration of the disk 10018 * write. When the disk write completes, the memory block is again brought 10019 * up-to-date. 10020 * 10021 * In-core inode structure reclamation. 10022 * 10023 * Because there are a finite number of "in-core" inode structures, they are 10024 * reused regularly. By transferring all inode-related dependencies to the 10025 * in-memory inode block and indexing them separately (via "inodedep"s), we 10026 * can allow "in-core" inode structures to be reused at any time and avoid 10027 * any increase in contention. 10028 * 10029 * Called just before entering the device driver to initiate a new disk I/O. 10030 * The buffer must be locked, thus, no I/O completion operations can occur 10031 * while we are manipulating its associated dependencies. 10032 */ 10033 static void 10034 softdep_disk_io_initiation(bp) 10035 struct buf *bp; /* structure describing disk write to occur */ 10036 { 10037 struct worklist *wk; 10038 struct worklist marker; 10039 struct inodedep *inodedep; 10040 struct freeblks *freeblks; 10041 struct jblkdep *jblkdep; 10042 struct newblk *newblk; 10043 struct ufsmount *ump; 10044 10045 /* 10046 * We only care about write operations. There should never 10047 * be dependencies for reads. 10048 */ 10049 if (bp->b_iocmd != BIO_WRITE) 10050 panic("softdep_disk_io_initiation: not write"); 10051 10052 if (bp->b_vflags & BV_BKGRDINPROG) 10053 panic("softdep_disk_io_initiation: Writing buffer with " 10054 "background write in progress: %p", bp); 10055 10056 ump = softdep_bp_to_mp(bp); 10057 if (ump == NULL) 10058 return; 10059 10060 marker.wk_type = D_LAST + 1; /* Not a normal workitem */ 10061 PHOLD(curproc); /* Don't swap out kernel stack */ 10062 ACQUIRE_LOCK(ump); 10063 /* 10064 * Do any necessary pre-I/O processing. 10065 */ 10066 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL; 10067 wk = markernext(&marker)) { 10068 LIST_INSERT_AFTER(wk, &marker, wk_list); 10069 switch (wk->wk_type) { 10070 10071 case D_PAGEDEP: 10072 initiate_write_filepage(WK_PAGEDEP(wk), bp); 10073 continue; 10074 10075 case D_INODEDEP: 10076 inodedep = WK_INODEDEP(wk); 10077 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) 10078 initiate_write_inodeblock_ufs1(inodedep, bp); 10079 else 10080 initiate_write_inodeblock_ufs2(inodedep, bp); 10081 continue; 10082 10083 case D_INDIRDEP: 10084 initiate_write_indirdep(WK_INDIRDEP(wk), bp); 10085 continue; 10086 10087 case D_BMSAFEMAP: 10088 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp); 10089 continue; 10090 10091 case D_JSEG: 10092 WK_JSEG(wk)->js_buf = NULL; 10093 continue; 10094 10095 case D_FREEBLKS: 10096 freeblks = WK_FREEBLKS(wk); 10097 jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd); 10098 /* 10099 * We have to wait for the freeblks to be journaled 10100 * before we can write an inodeblock with updated 10101 * pointers. Be careful to arrange the marker so 10102 * we revisit the freeblks if it's not removed by 10103 * the first jwait(). 10104 */ 10105 if (jblkdep != NULL) { 10106 LIST_REMOVE(&marker, wk_list); 10107 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10108 jwait(&jblkdep->jb_list, MNT_WAIT); 10109 } 10110 continue; 10111 case D_ALLOCDIRECT: 10112 case D_ALLOCINDIR: 10113 /* 10114 * We have to wait for the jnewblk to be journaled 10115 * before we can write to a block if the contents 10116 * may be confused with an earlier file's indirect 10117 * at recovery time. Handle the marker as described 10118 * above. 10119 */ 10120 newblk = WK_NEWBLK(wk); 10121 if (newblk->nb_jnewblk != NULL && 10122 indirblk_lookup(newblk->nb_list.wk_mp, 10123 newblk->nb_newblkno)) { 10124 LIST_REMOVE(&marker, wk_list); 10125 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10126 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 10127 } 10128 continue; 10129 10130 case D_SBDEP: 10131 initiate_write_sbdep(WK_SBDEP(wk)); 10132 continue; 10133 10134 case D_MKDIR: 10135 case D_FREEWORK: 10136 case D_FREEDEP: 10137 case D_JSEGDEP: 10138 continue; 10139 10140 default: 10141 panic("handle_disk_io_initiation: Unexpected type %s", 10142 TYPENAME(wk->wk_type)); 10143 /* NOTREACHED */ 10144 } 10145 } 10146 FREE_LOCK(ump); 10147 PRELE(curproc); /* Allow swapout of kernel stack */ 10148 } 10149 10150 /* 10151 * Called from within the procedure above to deal with unsatisfied 10152 * allocation dependencies in a directory. The buffer must be locked, 10153 * thus, no I/O completion operations can occur while we are 10154 * manipulating its associated dependencies. 10155 */ 10156 static void 10157 initiate_write_filepage(pagedep, bp) 10158 struct pagedep *pagedep; 10159 struct buf *bp; 10160 { 10161 struct jremref *jremref; 10162 struct jmvref *jmvref; 10163 struct dirrem *dirrem; 10164 struct diradd *dap; 10165 struct direct *ep; 10166 int i; 10167 10168 if (pagedep->pd_state & IOSTARTED) { 10169 /* 10170 * This can only happen if there is a driver that does not 10171 * understand chaining. Here biodone will reissue the call 10172 * to strategy for the incomplete buffers. 10173 */ 10174 printf("initiate_write_filepage: already started\n"); 10175 return; 10176 } 10177 pagedep->pd_state |= IOSTARTED; 10178 /* 10179 * Wait for all journal remove dependencies to hit the disk. 10180 * We can not allow any potentially conflicting directory adds 10181 * to be visible before removes and rollback is too difficult. 10182 * The per-filesystem lock may be dropped and re-acquired, however 10183 * we hold the buf locked so the dependency can not go away. 10184 */ 10185 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) 10186 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) 10187 jwait(&jremref->jr_list, MNT_WAIT); 10188 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) 10189 jwait(&jmvref->jm_list, MNT_WAIT); 10190 for (i = 0; i < DAHASHSZ; i++) { 10191 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 10192 ep = (struct direct *) 10193 ((char *)bp->b_data + dap->da_offset); 10194 if (ep->d_ino != dap->da_newinum) 10195 panic("%s: dir inum %ju != new %ju", 10196 "initiate_write_filepage", 10197 (uintmax_t)ep->d_ino, 10198 (uintmax_t)dap->da_newinum); 10199 if (dap->da_state & DIRCHG) 10200 ep->d_ino = dap->da_previous->dm_oldinum; 10201 else 10202 ep->d_ino = 0; 10203 dap->da_state &= ~ATTACHED; 10204 dap->da_state |= UNDONE; 10205 } 10206 } 10207 } 10208 10209 /* 10210 * Version of initiate_write_inodeblock that handles UFS1 dinodes. 10211 * Note that any bug fixes made to this routine must be done in the 10212 * version found below. 10213 * 10214 * Called from within the procedure above to deal with unsatisfied 10215 * allocation dependencies in an inodeblock. The buffer must be 10216 * locked, thus, no I/O completion operations can occur while we 10217 * are manipulating its associated dependencies. 10218 */ 10219 static void 10220 initiate_write_inodeblock_ufs1(inodedep, bp) 10221 struct inodedep *inodedep; 10222 struct buf *bp; /* The inode block */ 10223 { 10224 struct allocdirect *adp, *lastadp; 10225 struct ufs1_dinode *dp; 10226 struct ufs1_dinode *sip; 10227 struct inoref *inoref; 10228 struct ufsmount *ump; 10229 struct fs *fs; 10230 ufs_lbn_t i; 10231 #ifdef INVARIANTS 10232 ufs_lbn_t prevlbn = 0; 10233 #endif 10234 int deplist; 10235 10236 if (inodedep->id_state & IOSTARTED) 10237 panic("initiate_write_inodeblock_ufs1: already started"); 10238 inodedep->id_state |= IOSTARTED; 10239 fs = inodedep->id_fs; 10240 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10241 LOCK_OWNED(ump); 10242 dp = (struct ufs1_dinode *)bp->b_data + 10243 ino_to_fsbo(fs, inodedep->id_ino); 10244 10245 /* 10246 * If we're on the unlinked list but have not yet written our 10247 * next pointer initialize it here. 10248 */ 10249 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10250 struct inodedep *inon; 10251 10252 inon = TAILQ_NEXT(inodedep, id_unlinked); 10253 dp->di_freelink = inon ? inon->id_ino : 0; 10254 } 10255 /* 10256 * If the bitmap is not yet written, then the allocated 10257 * inode cannot be written to disk. 10258 */ 10259 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10260 if (inodedep->id_savedino1 != NULL) 10261 panic("initiate_write_inodeblock_ufs1: I/O underway"); 10262 FREE_LOCK(ump); 10263 sip = malloc(sizeof(struct ufs1_dinode), 10264 M_SAVEDINO, M_SOFTDEP_FLAGS); 10265 ACQUIRE_LOCK(ump); 10266 inodedep->id_savedino1 = sip; 10267 *inodedep->id_savedino1 = *dp; 10268 bzero((caddr_t)dp, sizeof(struct ufs1_dinode)); 10269 dp->di_gen = inodedep->id_savedino1->di_gen; 10270 dp->di_freelink = inodedep->id_savedino1->di_freelink; 10271 return; 10272 } 10273 /* 10274 * If no dependencies, then there is nothing to roll back. 10275 */ 10276 inodedep->id_savedsize = dp->di_size; 10277 inodedep->id_savedextsize = 0; 10278 inodedep->id_savednlink = dp->di_nlink; 10279 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10280 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10281 return; 10282 /* 10283 * Revert the link count to that of the first unwritten journal entry. 10284 */ 10285 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10286 if (inoref) 10287 dp->di_nlink = inoref->if_nlink; 10288 /* 10289 * Set the dependencies to busy. 10290 */ 10291 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10292 adp = TAILQ_NEXT(adp, ad_next)) { 10293 #ifdef INVARIANTS 10294 if (deplist != 0 && prevlbn >= adp->ad_offset) 10295 panic("softdep_write_inodeblock: lbn order"); 10296 prevlbn = adp->ad_offset; 10297 if (adp->ad_offset < UFS_NDADDR && 10298 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10299 panic("initiate_write_inodeblock_ufs1: " 10300 "direct pointer #%jd mismatch %d != %jd", 10301 (intmax_t)adp->ad_offset, 10302 dp->di_db[adp->ad_offset], 10303 (intmax_t)adp->ad_newblkno); 10304 if (adp->ad_offset >= UFS_NDADDR && 10305 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno) 10306 panic("initiate_write_inodeblock_ufs1: " 10307 "indirect pointer #%jd mismatch %d != %jd", 10308 (intmax_t)adp->ad_offset - UFS_NDADDR, 10309 dp->di_ib[adp->ad_offset - UFS_NDADDR], 10310 (intmax_t)adp->ad_newblkno); 10311 deplist |= 1 << adp->ad_offset; 10312 if ((adp->ad_state & ATTACHED) == 0) 10313 panic("initiate_write_inodeblock_ufs1: " 10314 "Unknown state 0x%x", adp->ad_state); 10315 #endif /* INVARIANTS */ 10316 adp->ad_state &= ~ATTACHED; 10317 adp->ad_state |= UNDONE; 10318 } 10319 /* 10320 * The on-disk inode cannot claim to be any larger than the last 10321 * fragment that has been written. Otherwise, the on-disk inode 10322 * might have fragments that were not the last block in the file 10323 * which would corrupt the filesystem. 10324 */ 10325 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10326 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10327 if (adp->ad_offset >= UFS_NDADDR) 10328 break; 10329 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10330 /* keep going until hitting a rollback to a frag */ 10331 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10332 continue; 10333 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10334 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) { 10335 #ifdef INVARIANTS 10336 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10337 panic("initiate_write_inodeblock_ufs1: " 10338 "lost dep1"); 10339 #endif /* INVARIANTS */ 10340 dp->di_db[i] = 0; 10341 } 10342 for (i = 0; i < UFS_NIADDR; i++) { 10343 #ifdef INVARIANTS 10344 if (dp->di_ib[i] != 0 && 10345 (deplist & ((1 << UFS_NDADDR) << i)) == 0) 10346 panic("initiate_write_inodeblock_ufs1: " 10347 "lost dep2"); 10348 #endif /* INVARIANTS */ 10349 dp->di_ib[i] = 0; 10350 } 10351 return; 10352 } 10353 /* 10354 * If we have zero'ed out the last allocated block of the file, 10355 * roll back the size to the last currently allocated block. 10356 * We know that this last allocated block is a full-sized as 10357 * we already checked for fragments in the loop above. 10358 */ 10359 if (lastadp != NULL && 10360 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10361 for (i = lastadp->ad_offset; i >= 0; i--) 10362 if (dp->di_db[i] != 0) 10363 break; 10364 dp->di_size = (i + 1) * fs->fs_bsize; 10365 } 10366 /* 10367 * The only dependencies are for indirect blocks. 10368 * 10369 * The file size for indirect block additions is not guaranteed. 10370 * Such a guarantee would be non-trivial to achieve. The conventional 10371 * synchronous write implementation also does not make this guarantee. 10372 * Fsck should catch and fix discrepancies. Arguably, the file size 10373 * can be over-estimated without destroying integrity when the file 10374 * moves into the indirect blocks (i.e., is large). If we want to 10375 * postpone fsck, we are stuck with this argument. 10376 */ 10377 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10378 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0; 10379 } 10380 10381 /* 10382 * Version of initiate_write_inodeblock that handles UFS2 dinodes. 10383 * Note that any bug fixes made to this routine must be done in the 10384 * version found above. 10385 * 10386 * Called from within the procedure above to deal with unsatisfied 10387 * allocation dependencies in an inodeblock. The buffer must be 10388 * locked, thus, no I/O completion operations can occur while we 10389 * are manipulating its associated dependencies. 10390 */ 10391 static void 10392 initiate_write_inodeblock_ufs2(inodedep, bp) 10393 struct inodedep *inodedep; 10394 struct buf *bp; /* The inode block */ 10395 { 10396 struct allocdirect *adp, *lastadp; 10397 struct ufs2_dinode *dp; 10398 struct ufs2_dinode *sip; 10399 struct inoref *inoref; 10400 struct ufsmount *ump; 10401 struct fs *fs; 10402 ufs_lbn_t i; 10403 #ifdef INVARIANTS 10404 ufs_lbn_t prevlbn = 0; 10405 #endif 10406 int deplist; 10407 10408 if (inodedep->id_state & IOSTARTED) 10409 panic("initiate_write_inodeblock_ufs2: already started"); 10410 inodedep->id_state |= IOSTARTED; 10411 fs = inodedep->id_fs; 10412 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10413 LOCK_OWNED(ump); 10414 dp = (struct ufs2_dinode *)bp->b_data + 10415 ino_to_fsbo(fs, inodedep->id_ino); 10416 10417 /* 10418 * If we're on the unlinked list but have not yet written our 10419 * next pointer initialize it here. 10420 */ 10421 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10422 struct inodedep *inon; 10423 10424 inon = TAILQ_NEXT(inodedep, id_unlinked); 10425 dp->di_freelink = inon ? inon->id_ino : 0; 10426 ffs_update_dinode_ckhash(fs, dp); 10427 } 10428 /* 10429 * If the bitmap is not yet written, then the allocated 10430 * inode cannot be written to disk. 10431 */ 10432 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10433 if (inodedep->id_savedino2 != NULL) 10434 panic("initiate_write_inodeblock_ufs2: I/O underway"); 10435 FREE_LOCK(ump); 10436 sip = malloc(sizeof(struct ufs2_dinode), 10437 M_SAVEDINO, M_SOFTDEP_FLAGS); 10438 ACQUIRE_LOCK(ump); 10439 inodedep->id_savedino2 = sip; 10440 *inodedep->id_savedino2 = *dp; 10441 bzero((caddr_t)dp, sizeof(struct ufs2_dinode)); 10442 dp->di_gen = inodedep->id_savedino2->di_gen; 10443 dp->di_freelink = inodedep->id_savedino2->di_freelink; 10444 return; 10445 } 10446 /* 10447 * If no dependencies, then there is nothing to roll back. 10448 */ 10449 inodedep->id_savedsize = dp->di_size; 10450 inodedep->id_savedextsize = dp->di_extsize; 10451 inodedep->id_savednlink = dp->di_nlink; 10452 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10453 TAILQ_EMPTY(&inodedep->id_extupdt) && 10454 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10455 return; 10456 /* 10457 * Revert the link count to that of the first unwritten journal entry. 10458 */ 10459 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10460 if (inoref) 10461 dp->di_nlink = inoref->if_nlink; 10462 10463 /* 10464 * Set the ext data dependencies to busy. 10465 */ 10466 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10467 adp = TAILQ_NEXT(adp, ad_next)) { 10468 #ifdef INVARIANTS 10469 if (deplist != 0 && prevlbn >= adp->ad_offset) 10470 panic("initiate_write_inodeblock_ufs2: lbn order"); 10471 prevlbn = adp->ad_offset; 10472 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno) 10473 panic("initiate_write_inodeblock_ufs2: " 10474 "ext pointer #%jd mismatch %jd != %jd", 10475 (intmax_t)adp->ad_offset, 10476 (intmax_t)dp->di_extb[adp->ad_offset], 10477 (intmax_t)adp->ad_newblkno); 10478 deplist |= 1 << adp->ad_offset; 10479 if ((adp->ad_state & ATTACHED) == 0) 10480 panic("initiate_write_inodeblock_ufs2: Unknown " 10481 "state 0x%x", adp->ad_state); 10482 #endif /* INVARIANTS */ 10483 adp->ad_state &= ~ATTACHED; 10484 adp->ad_state |= UNDONE; 10485 } 10486 /* 10487 * The on-disk inode cannot claim to be any larger than the last 10488 * fragment that has been written. Otherwise, the on-disk inode 10489 * might have fragments that were not the last block in the ext 10490 * data which would corrupt the filesystem. 10491 */ 10492 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10493 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10494 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno; 10495 /* keep going until hitting a rollback to a frag */ 10496 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10497 continue; 10498 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10499 for (i = adp->ad_offset + 1; i < UFS_NXADDR; i++) { 10500 #ifdef INVARIANTS 10501 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0) 10502 panic("initiate_write_inodeblock_ufs2: " 10503 "lost dep1"); 10504 #endif /* INVARIANTS */ 10505 dp->di_extb[i] = 0; 10506 } 10507 lastadp = NULL; 10508 break; 10509 } 10510 /* 10511 * If we have zero'ed out the last allocated block of the ext 10512 * data, roll back the size to the last currently allocated block. 10513 * We know that this last allocated block is a full-sized as 10514 * we already checked for fragments in the loop above. 10515 */ 10516 if (lastadp != NULL && 10517 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10518 for (i = lastadp->ad_offset; i >= 0; i--) 10519 if (dp->di_extb[i] != 0) 10520 break; 10521 dp->di_extsize = (i + 1) * fs->fs_bsize; 10522 } 10523 /* 10524 * Set the file data dependencies to busy. 10525 */ 10526 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10527 adp = TAILQ_NEXT(adp, ad_next)) { 10528 #ifdef INVARIANTS 10529 if (deplist != 0 && prevlbn >= adp->ad_offset) 10530 panic("softdep_write_inodeblock: lbn order"); 10531 if ((adp->ad_state & ATTACHED) == 0) 10532 panic("inodedep %p and adp %p not attached", inodedep, adp); 10533 prevlbn = adp->ad_offset; 10534 if (adp->ad_offset < UFS_NDADDR && 10535 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10536 panic("initiate_write_inodeblock_ufs2: " 10537 "direct pointer #%jd mismatch %jd != %jd", 10538 (intmax_t)adp->ad_offset, 10539 (intmax_t)dp->di_db[adp->ad_offset], 10540 (intmax_t)adp->ad_newblkno); 10541 if (adp->ad_offset >= UFS_NDADDR && 10542 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno) 10543 panic("initiate_write_inodeblock_ufs2: " 10544 "indirect pointer #%jd mismatch %jd != %jd", 10545 (intmax_t)adp->ad_offset - UFS_NDADDR, 10546 (intmax_t)dp->di_ib[adp->ad_offset - UFS_NDADDR], 10547 (intmax_t)adp->ad_newblkno); 10548 deplist |= 1 << adp->ad_offset; 10549 if ((adp->ad_state & ATTACHED) == 0) 10550 panic("initiate_write_inodeblock_ufs2: Unknown " 10551 "state 0x%x", adp->ad_state); 10552 #endif /* INVARIANTS */ 10553 adp->ad_state &= ~ATTACHED; 10554 adp->ad_state |= UNDONE; 10555 } 10556 /* 10557 * The on-disk inode cannot claim to be any larger than the last 10558 * fragment that has been written. Otherwise, the on-disk inode 10559 * might have fragments that were not the last block in the file 10560 * which would corrupt the filesystem. 10561 */ 10562 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10563 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10564 if (adp->ad_offset >= UFS_NDADDR) 10565 break; 10566 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10567 /* keep going until hitting a rollback to a frag */ 10568 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10569 continue; 10570 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10571 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) { 10572 #ifdef INVARIANTS 10573 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10574 panic("initiate_write_inodeblock_ufs2: " 10575 "lost dep2"); 10576 #endif /* INVARIANTS */ 10577 dp->di_db[i] = 0; 10578 } 10579 for (i = 0; i < UFS_NIADDR; i++) { 10580 #ifdef INVARIANTS 10581 if (dp->di_ib[i] != 0 && 10582 (deplist & ((1 << UFS_NDADDR) << i)) == 0) 10583 panic("initiate_write_inodeblock_ufs2: " 10584 "lost dep3"); 10585 #endif /* INVARIANTS */ 10586 dp->di_ib[i] = 0; 10587 } 10588 ffs_update_dinode_ckhash(fs, dp); 10589 return; 10590 } 10591 /* 10592 * If we have zero'ed out the last allocated block of the file, 10593 * roll back the size to the last currently allocated block. 10594 * We know that this last allocated block is a full-sized as 10595 * we already checked for fragments in the loop above. 10596 */ 10597 if (lastadp != NULL && 10598 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10599 for (i = lastadp->ad_offset; i >= 0; i--) 10600 if (dp->di_db[i] != 0) 10601 break; 10602 dp->di_size = (i + 1) * fs->fs_bsize; 10603 } 10604 /* 10605 * The only dependencies are for indirect blocks. 10606 * 10607 * The file size for indirect block additions is not guaranteed. 10608 * Such a guarantee would be non-trivial to achieve. The conventional 10609 * synchronous write implementation also does not make this guarantee. 10610 * Fsck should catch and fix discrepancies. Arguably, the file size 10611 * can be over-estimated without destroying integrity when the file 10612 * moves into the indirect blocks (i.e., is large). If we want to 10613 * postpone fsck, we are stuck with this argument. 10614 */ 10615 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10616 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0; 10617 ffs_update_dinode_ckhash(fs, dp); 10618 } 10619 10620 /* 10621 * Cancel an indirdep as a result of truncation. Release all of the 10622 * children allocindirs and place their journal work on the appropriate 10623 * list. 10624 */ 10625 static void 10626 cancel_indirdep(indirdep, bp, freeblks) 10627 struct indirdep *indirdep; 10628 struct buf *bp; 10629 struct freeblks *freeblks; 10630 { 10631 struct allocindir *aip; 10632 10633 /* 10634 * None of the indirect pointers will ever be visible, 10635 * so they can simply be tossed. GOINGAWAY ensures 10636 * that allocated pointers will be saved in the buffer 10637 * cache until they are freed. Note that they will 10638 * only be able to be found by their physical address 10639 * since the inode mapping the logical address will 10640 * be gone. The save buffer used for the safe copy 10641 * was allocated in setup_allocindir_phase2 using 10642 * the physical address so it could be used for this 10643 * purpose. Hence we swap the safe copy with the real 10644 * copy, allowing the safe copy to be freed and holding 10645 * on to the real copy for later use in indir_trunc. 10646 */ 10647 if (indirdep->ir_state & GOINGAWAY) 10648 panic("cancel_indirdep: already gone"); 10649 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 10650 indirdep->ir_state |= DEPCOMPLETE; 10651 LIST_REMOVE(indirdep, ir_next); 10652 } 10653 indirdep->ir_state |= GOINGAWAY; 10654 /* 10655 * Pass in bp for blocks still have journal writes 10656 * pending so we can cancel them on their own. 10657 */ 10658 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != NULL) 10659 cancel_allocindir(aip, bp, freeblks, 0); 10660 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) 10661 cancel_allocindir(aip, NULL, freeblks, 0); 10662 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) 10663 cancel_allocindir(aip, NULL, freeblks, 0); 10664 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) 10665 cancel_allocindir(aip, NULL, freeblks, 0); 10666 /* 10667 * If there are pending partial truncations we need to keep the 10668 * old block copy around until they complete. This is because 10669 * the current b_data is not a perfect superset of the available 10670 * blocks. 10671 */ 10672 if (TAILQ_EMPTY(&indirdep->ir_trunc)) 10673 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount); 10674 else 10675 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10676 WORKLIST_REMOVE(&indirdep->ir_list); 10677 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list); 10678 indirdep->ir_bp = NULL; 10679 indirdep->ir_freeblks = freeblks; 10680 } 10681 10682 /* 10683 * Free an indirdep once it no longer has new pointers to track. 10684 */ 10685 static void 10686 free_indirdep(indirdep) 10687 struct indirdep *indirdep; 10688 { 10689 10690 KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc), 10691 ("free_indirdep: Indir trunc list not empty.")); 10692 KASSERT(LIST_EMPTY(&indirdep->ir_completehd), 10693 ("free_indirdep: Complete head not empty.")); 10694 KASSERT(LIST_EMPTY(&indirdep->ir_writehd), 10695 ("free_indirdep: write head not empty.")); 10696 KASSERT(LIST_EMPTY(&indirdep->ir_donehd), 10697 ("free_indirdep: done head not empty.")); 10698 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd), 10699 ("free_indirdep: deplist head not empty.")); 10700 KASSERT((indirdep->ir_state & DEPCOMPLETE), 10701 ("free_indirdep: %p still on newblk list.", indirdep)); 10702 KASSERT(indirdep->ir_saveddata == NULL, 10703 ("free_indirdep: %p still has saved data.", indirdep)); 10704 if (indirdep->ir_state & ONWORKLIST) 10705 WORKLIST_REMOVE(&indirdep->ir_list); 10706 WORKITEM_FREE(indirdep, D_INDIRDEP); 10707 } 10708 10709 /* 10710 * Called before a write to an indirdep. This routine is responsible for 10711 * rolling back pointers to a safe state which includes only those 10712 * allocindirs which have been completed. 10713 */ 10714 static void 10715 initiate_write_indirdep(indirdep, bp) 10716 struct indirdep *indirdep; 10717 struct buf *bp; 10718 { 10719 struct ufsmount *ump; 10720 10721 indirdep->ir_state |= IOSTARTED; 10722 if (indirdep->ir_state & GOINGAWAY) 10723 panic("disk_io_initiation: indirdep gone"); 10724 /* 10725 * If there are no remaining dependencies, this will be writing 10726 * the real pointers. 10727 */ 10728 if (LIST_EMPTY(&indirdep->ir_deplisthd) && 10729 TAILQ_EMPTY(&indirdep->ir_trunc)) 10730 return; 10731 /* 10732 * Replace up-to-date version with safe version. 10733 */ 10734 if (indirdep->ir_saveddata == NULL) { 10735 ump = VFSTOUFS(indirdep->ir_list.wk_mp); 10736 LOCK_OWNED(ump); 10737 FREE_LOCK(ump); 10738 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 10739 M_SOFTDEP_FLAGS); 10740 ACQUIRE_LOCK(ump); 10741 } 10742 indirdep->ir_state &= ~ATTACHED; 10743 indirdep->ir_state |= UNDONE; 10744 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10745 bcopy(indirdep->ir_savebp->b_data, bp->b_data, 10746 bp->b_bcount); 10747 } 10748 10749 /* 10750 * Called when an inode has been cleared in a cg bitmap. This finally 10751 * eliminates any canceled jaddrefs 10752 */ 10753 void 10754 softdep_setup_inofree(mp, bp, ino, wkhd) 10755 struct mount *mp; 10756 struct buf *bp; 10757 ino_t ino; 10758 struct workhead *wkhd; 10759 { 10760 struct worklist *wk, *wkn; 10761 struct inodedep *inodedep; 10762 struct ufsmount *ump; 10763 uint8_t *inosused; 10764 struct cg *cgp; 10765 struct fs *fs; 10766 10767 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 10768 ("softdep_setup_inofree called on non-softdep filesystem")); 10769 ump = VFSTOUFS(mp); 10770 ACQUIRE_LOCK(ump); 10771 fs = ump->um_fs; 10772 cgp = (struct cg *)bp->b_data; 10773 inosused = cg_inosused(cgp); 10774 if (isset(inosused, ino % fs->fs_ipg)) 10775 panic("softdep_setup_inofree: inode %ju not freed.", 10776 (uintmax_t)ino); 10777 if (inodedep_lookup(mp, ino, 0, &inodedep)) 10778 panic("softdep_setup_inofree: ino %ju has existing inodedep %p", 10779 (uintmax_t)ino, inodedep); 10780 if (wkhd) { 10781 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) { 10782 if (wk->wk_type != D_JADDREF) 10783 continue; 10784 WORKLIST_REMOVE(wk); 10785 /* 10786 * We can free immediately even if the jaddref 10787 * isn't attached in a background write as now 10788 * the bitmaps are reconciled. 10789 */ 10790 wk->wk_state |= COMPLETE | ATTACHED; 10791 free_jaddref(WK_JADDREF(wk)); 10792 } 10793 jwork_move(&bp->b_dep, wkhd); 10794 } 10795 FREE_LOCK(ump); 10796 } 10797 10798 /* 10799 * Called via ffs_blkfree() after a set of frags has been cleared from a cg 10800 * map. Any dependencies waiting for the write to clear are added to the 10801 * buf's list and any jnewblks that are being canceled are discarded 10802 * immediately. 10803 */ 10804 void 10805 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 10806 struct mount *mp; 10807 struct buf *bp; 10808 ufs2_daddr_t blkno; 10809 int frags; 10810 struct workhead *wkhd; 10811 { 10812 struct bmsafemap *bmsafemap; 10813 struct jnewblk *jnewblk; 10814 struct ufsmount *ump; 10815 struct worklist *wk; 10816 struct fs *fs; 10817 #ifdef INVARIANTS 10818 uint8_t *blksfree; 10819 struct cg *cgp; 10820 ufs2_daddr_t jstart; 10821 ufs2_daddr_t jend; 10822 ufs2_daddr_t end; 10823 long bno; 10824 int i; 10825 #endif 10826 10827 CTR3(KTR_SUJ, 10828 "softdep_setup_blkfree: blkno %jd frags %d wk head %p", 10829 blkno, frags, wkhd); 10830 10831 ump = VFSTOUFS(mp); 10832 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 10833 ("softdep_setup_blkfree called on non-softdep filesystem")); 10834 ACQUIRE_LOCK(ump); 10835 /* Lookup the bmsafemap so we track when it is dirty. */ 10836 fs = ump->um_fs; 10837 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 10838 /* 10839 * Detach any jnewblks which have been canceled. They must linger 10840 * until the bitmap is cleared again by ffs_blkfree() to prevent 10841 * an unjournaled allocation from hitting the disk. 10842 */ 10843 if (wkhd) { 10844 while ((wk = LIST_FIRST(wkhd)) != NULL) { 10845 CTR2(KTR_SUJ, 10846 "softdep_setup_blkfree: blkno %jd wk type %d", 10847 blkno, wk->wk_type); 10848 WORKLIST_REMOVE(wk); 10849 if (wk->wk_type != D_JNEWBLK) { 10850 WORKLIST_INSERT(&bmsafemap->sm_freehd, wk); 10851 continue; 10852 } 10853 jnewblk = WK_JNEWBLK(wk); 10854 KASSERT(jnewblk->jn_state & GOINGAWAY, 10855 ("softdep_setup_blkfree: jnewblk not canceled.")); 10856 #ifdef INVARIANTS 10857 /* 10858 * Assert that this block is free in the bitmap 10859 * before we discard the jnewblk. 10860 */ 10861 cgp = (struct cg *)bp->b_data; 10862 blksfree = cg_blksfree(cgp); 10863 bno = dtogd(fs, jnewblk->jn_blkno); 10864 for (i = jnewblk->jn_oldfrags; 10865 i < jnewblk->jn_frags; i++) { 10866 if (isset(blksfree, bno + i)) 10867 continue; 10868 panic("softdep_setup_blkfree: not free"); 10869 } 10870 #endif 10871 /* 10872 * Even if it's not attached we can free immediately 10873 * as the new bitmap is correct. 10874 */ 10875 wk->wk_state |= COMPLETE | ATTACHED; 10876 free_jnewblk(jnewblk); 10877 } 10878 } 10879 10880 #ifdef INVARIANTS 10881 /* 10882 * Assert that we are not freeing a block which has an outstanding 10883 * allocation dependency. 10884 */ 10885 fs = VFSTOUFS(mp)->um_fs; 10886 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 10887 end = blkno + frags; 10888 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 10889 /* 10890 * Don't match against blocks that will be freed when the 10891 * background write is done. 10892 */ 10893 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) == 10894 (COMPLETE | DEPCOMPLETE)) 10895 continue; 10896 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags; 10897 jend = jnewblk->jn_blkno + jnewblk->jn_frags; 10898 if ((blkno >= jstart && blkno < jend) || 10899 (end > jstart && end <= jend)) { 10900 printf("state 0x%X %jd - %d %d dep %p\n", 10901 jnewblk->jn_state, jnewblk->jn_blkno, 10902 jnewblk->jn_oldfrags, jnewblk->jn_frags, 10903 jnewblk->jn_dep); 10904 panic("softdep_setup_blkfree: " 10905 "%jd-%jd(%d) overlaps with %jd-%jd", 10906 blkno, end, frags, jstart, jend); 10907 } 10908 } 10909 #endif 10910 FREE_LOCK(ump); 10911 } 10912 10913 /* 10914 * Revert a block allocation when the journal record that describes it 10915 * is not yet written. 10916 */ 10917 static int 10918 jnewblk_rollback(jnewblk, fs, cgp, blksfree) 10919 struct jnewblk *jnewblk; 10920 struct fs *fs; 10921 struct cg *cgp; 10922 uint8_t *blksfree; 10923 { 10924 ufs1_daddr_t fragno; 10925 long cgbno, bbase; 10926 int frags, blk; 10927 int i; 10928 10929 frags = 0; 10930 cgbno = dtogd(fs, jnewblk->jn_blkno); 10931 /* 10932 * We have to test which frags need to be rolled back. We may 10933 * be operating on a stale copy when doing background writes. 10934 */ 10935 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) 10936 if (isclr(blksfree, cgbno + i)) 10937 frags++; 10938 if (frags == 0) 10939 return (0); 10940 /* 10941 * This is mostly ffs_blkfree() sans some validation and 10942 * superblock updates. 10943 */ 10944 if (frags == fs->fs_frag) { 10945 fragno = fragstoblks(fs, cgbno); 10946 ffs_setblock(fs, blksfree, fragno); 10947 ffs_clusteracct(fs, cgp, fragno, 1); 10948 cgp->cg_cs.cs_nbfree++; 10949 } else { 10950 cgbno += jnewblk->jn_oldfrags; 10951 bbase = cgbno - fragnum(fs, cgbno); 10952 /* Decrement the old frags. */ 10953 blk = blkmap(fs, blksfree, bbase); 10954 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 10955 /* Deallocate the fragment */ 10956 for (i = 0; i < frags; i++) 10957 setbit(blksfree, cgbno + i); 10958 cgp->cg_cs.cs_nffree += frags; 10959 /* Add back in counts associated with the new frags */ 10960 blk = blkmap(fs, blksfree, bbase); 10961 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 10962 /* If a complete block has been reassembled, account for it. */ 10963 fragno = fragstoblks(fs, bbase); 10964 if (ffs_isblock(fs, blksfree, fragno)) { 10965 cgp->cg_cs.cs_nffree -= fs->fs_frag; 10966 ffs_clusteracct(fs, cgp, fragno, 1); 10967 cgp->cg_cs.cs_nbfree++; 10968 } 10969 } 10970 stat_jnewblk++; 10971 jnewblk->jn_state &= ~ATTACHED; 10972 jnewblk->jn_state |= UNDONE; 10973 10974 return (frags); 10975 } 10976 10977 static void 10978 initiate_write_bmsafemap(bmsafemap, bp) 10979 struct bmsafemap *bmsafemap; 10980 struct buf *bp; /* The cg block. */ 10981 { 10982 struct jaddref *jaddref; 10983 struct jnewblk *jnewblk; 10984 uint8_t *inosused; 10985 uint8_t *blksfree; 10986 struct cg *cgp; 10987 struct fs *fs; 10988 ino_t ino; 10989 10990 /* 10991 * If this is a background write, we did this at the time that 10992 * the copy was made, so do not need to do it again. 10993 */ 10994 if (bmsafemap->sm_state & IOSTARTED) 10995 return; 10996 bmsafemap->sm_state |= IOSTARTED; 10997 /* 10998 * Clear any inode allocations which are pending journal writes. 10999 */ 11000 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) { 11001 cgp = (struct cg *)bp->b_data; 11002 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11003 inosused = cg_inosused(cgp); 11004 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) { 11005 ino = jaddref->ja_ino % fs->fs_ipg; 11006 if (isset(inosused, ino)) { 11007 if ((jaddref->ja_mode & IFMT) == IFDIR) 11008 cgp->cg_cs.cs_ndir--; 11009 cgp->cg_cs.cs_nifree++; 11010 clrbit(inosused, ino); 11011 jaddref->ja_state &= ~ATTACHED; 11012 jaddref->ja_state |= UNDONE; 11013 stat_jaddref++; 11014 } else 11015 panic("initiate_write_bmsafemap: inode %ju " 11016 "marked free", (uintmax_t)jaddref->ja_ino); 11017 } 11018 } 11019 /* 11020 * Clear any block allocations which are pending journal writes. 11021 */ 11022 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 11023 cgp = (struct cg *)bp->b_data; 11024 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11025 blksfree = cg_blksfree(cgp); 11026 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 11027 if (jnewblk_rollback(jnewblk, fs, cgp, blksfree)) 11028 continue; 11029 panic("initiate_write_bmsafemap: block %jd " 11030 "marked free", jnewblk->jn_blkno); 11031 } 11032 } 11033 /* 11034 * Move allocation lists to the written lists so they can be 11035 * cleared once the block write is complete. 11036 */ 11037 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr, 11038 inodedep, id_deps); 11039 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 11040 newblk, nb_deps); 11041 LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist, 11042 wk_list); 11043 } 11044 11045 /* 11046 * This routine is called during the completion interrupt 11047 * service routine for a disk write (from the procedure called 11048 * by the device driver to inform the filesystem caches of 11049 * a request completion). It should be called early in this 11050 * procedure, before the block is made available to other 11051 * processes or other routines are called. 11052 * 11053 */ 11054 static void 11055 softdep_disk_write_complete(bp) 11056 struct buf *bp; /* describes the completed disk write */ 11057 { 11058 struct worklist *wk; 11059 struct worklist *owk; 11060 struct ufsmount *ump; 11061 struct workhead reattach; 11062 struct freeblks *freeblks; 11063 struct buf *sbp; 11064 11065 ump = softdep_bp_to_mp(bp); 11066 KASSERT(LIST_EMPTY(&bp->b_dep) || ump != NULL, 11067 ("softdep_disk_write_complete: softdep_bp_to_mp returned NULL " 11068 "with outstanding dependencies for buffer %p", bp)); 11069 if (ump == NULL) 11070 return; 11071 /* 11072 * If an error occurred while doing the write, then the data 11073 * has not hit the disk and the dependencies cannot be processed. 11074 * But we do have to go through and roll forward any dependencies 11075 * that were rolled back before the disk write. 11076 */ 11077 sbp = NULL; 11078 ACQUIRE_LOCK(ump); 11079 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) { 11080 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 11081 switch (wk->wk_type) { 11082 11083 case D_PAGEDEP: 11084 handle_written_filepage(WK_PAGEDEP(wk), bp, 0); 11085 continue; 11086 11087 case D_INODEDEP: 11088 handle_written_inodeblock(WK_INODEDEP(wk), 11089 bp, 0); 11090 continue; 11091 11092 case D_BMSAFEMAP: 11093 handle_written_bmsafemap(WK_BMSAFEMAP(wk), 11094 bp, 0); 11095 continue; 11096 11097 case D_INDIRDEP: 11098 handle_written_indirdep(WK_INDIRDEP(wk), 11099 bp, &sbp, 0); 11100 continue; 11101 default: 11102 /* nothing to roll forward */ 11103 continue; 11104 } 11105 } 11106 FREE_LOCK(ump); 11107 if (sbp) 11108 brelse(sbp); 11109 return; 11110 } 11111 LIST_INIT(&reattach); 11112 11113 /* 11114 * Ump SU lock must not be released anywhere in this code segment. 11115 */ 11116 owk = NULL; 11117 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) { 11118 WORKLIST_REMOVE(wk); 11119 atomic_add_long(&dep_write[wk->wk_type], 1); 11120 if (wk == owk) 11121 panic("duplicate worklist: %p\n", wk); 11122 owk = wk; 11123 switch (wk->wk_type) { 11124 11125 case D_PAGEDEP: 11126 if (handle_written_filepage(WK_PAGEDEP(wk), bp, 11127 WRITESUCCEEDED)) 11128 WORKLIST_INSERT(&reattach, wk); 11129 continue; 11130 11131 case D_INODEDEP: 11132 if (handle_written_inodeblock(WK_INODEDEP(wk), bp, 11133 WRITESUCCEEDED)) 11134 WORKLIST_INSERT(&reattach, wk); 11135 continue; 11136 11137 case D_BMSAFEMAP: 11138 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp, 11139 WRITESUCCEEDED)) 11140 WORKLIST_INSERT(&reattach, wk); 11141 continue; 11142 11143 case D_MKDIR: 11144 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 11145 continue; 11146 11147 case D_ALLOCDIRECT: 11148 wk->wk_state |= COMPLETE; 11149 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL); 11150 continue; 11151 11152 case D_ALLOCINDIR: 11153 wk->wk_state |= COMPLETE; 11154 handle_allocindir_partdone(WK_ALLOCINDIR(wk)); 11155 continue; 11156 11157 case D_INDIRDEP: 11158 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp, 11159 WRITESUCCEEDED)) 11160 WORKLIST_INSERT(&reattach, wk); 11161 continue; 11162 11163 case D_FREEBLKS: 11164 wk->wk_state |= COMPLETE; 11165 freeblks = WK_FREEBLKS(wk); 11166 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE && 11167 LIST_EMPTY(&freeblks->fb_jblkdephd)) 11168 add_to_worklist(wk, WK_NODELAY); 11169 continue; 11170 11171 case D_FREEWORK: 11172 handle_written_freework(WK_FREEWORK(wk)); 11173 break; 11174 11175 case D_JSEGDEP: 11176 free_jsegdep(WK_JSEGDEP(wk)); 11177 continue; 11178 11179 case D_JSEG: 11180 handle_written_jseg(WK_JSEG(wk), bp); 11181 continue; 11182 11183 case D_SBDEP: 11184 if (handle_written_sbdep(WK_SBDEP(wk), bp)) 11185 WORKLIST_INSERT(&reattach, wk); 11186 continue; 11187 11188 case D_FREEDEP: 11189 free_freedep(WK_FREEDEP(wk)); 11190 continue; 11191 11192 default: 11193 panic("handle_disk_write_complete: Unknown type %s", 11194 TYPENAME(wk->wk_type)); 11195 /* NOTREACHED */ 11196 } 11197 } 11198 /* 11199 * Reattach any requests that must be redone. 11200 */ 11201 while ((wk = LIST_FIRST(&reattach)) != NULL) { 11202 WORKLIST_REMOVE(wk); 11203 WORKLIST_INSERT(&bp->b_dep, wk); 11204 } 11205 FREE_LOCK(ump); 11206 if (sbp) 11207 brelse(sbp); 11208 } 11209 11210 /* 11211 * Called from within softdep_disk_write_complete above. 11212 */ 11213 static void 11214 handle_allocdirect_partdone(adp, wkhd) 11215 struct allocdirect *adp; /* the completed allocdirect */ 11216 struct workhead *wkhd; /* Work to do when inode is writtne. */ 11217 { 11218 struct allocdirectlst *listhead; 11219 struct allocdirect *listadp; 11220 struct inodedep *inodedep; 11221 long bsize; 11222 11223 LOCK_OWNED(VFSTOUFS(adp->ad_block.nb_list.wk_mp)); 11224 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11225 return; 11226 /* 11227 * The on-disk inode cannot claim to be any larger than the last 11228 * fragment that has been written. Otherwise, the on-disk inode 11229 * might have fragments that were not the last block in the file 11230 * which would corrupt the filesystem. Thus, we cannot free any 11231 * allocdirects after one whose ad_oldblkno claims a fragment as 11232 * these blocks must be rolled back to zero before writing the inode. 11233 * We check the currently active set of allocdirects in id_inoupdt 11234 * or id_extupdt as appropriate. 11235 */ 11236 inodedep = adp->ad_inodedep; 11237 bsize = inodedep->id_fs->fs_bsize; 11238 if (adp->ad_state & EXTDATA) 11239 listhead = &inodedep->id_extupdt; 11240 else 11241 listhead = &inodedep->id_inoupdt; 11242 TAILQ_FOREACH(listadp, listhead, ad_next) { 11243 /* found our block */ 11244 if (listadp == adp) 11245 break; 11246 /* continue if ad_oldlbn is not a fragment */ 11247 if (listadp->ad_oldsize == 0 || 11248 listadp->ad_oldsize == bsize) 11249 continue; 11250 /* hit a fragment */ 11251 return; 11252 } 11253 /* 11254 * If we have reached the end of the current list without 11255 * finding the just finished dependency, then it must be 11256 * on the future dependency list. Future dependencies cannot 11257 * be freed until they are moved to the current list. 11258 */ 11259 if (listadp == NULL) { 11260 #ifdef INVARIANTS 11261 if (adp->ad_state & EXTDATA) 11262 listhead = &inodedep->id_newextupdt; 11263 else 11264 listhead = &inodedep->id_newinoupdt; 11265 TAILQ_FOREACH(listadp, listhead, ad_next) 11266 /* found our block */ 11267 if (listadp == adp) 11268 break; 11269 if (listadp == NULL) 11270 panic("handle_allocdirect_partdone: lost dep"); 11271 #endif /* INVARIANTS */ 11272 return; 11273 } 11274 /* 11275 * If we have found the just finished dependency, then queue 11276 * it along with anything that follows it that is complete. 11277 * Since the pointer has not yet been written in the inode 11278 * as the dependency prevents it, place the allocdirect on the 11279 * bufwait list where it will be freed once the pointer is 11280 * valid. 11281 */ 11282 if (wkhd == NULL) 11283 wkhd = &inodedep->id_bufwait; 11284 for (; adp; adp = listadp) { 11285 listadp = TAILQ_NEXT(adp, ad_next); 11286 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11287 return; 11288 TAILQ_REMOVE(listhead, adp, ad_next); 11289 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list); 11290 } 11291 } 11292 11293 /* 11294 * Called from within softdep_disk_write_complete above. This routine 11295 * completes successfully written allocindirs. 11296 */ 11297 static void 11298 handle_allocindir_partdone(aip) 11299 struct allocindir *aip; /* the completed allocindir */ 11300 { 11301 struct indirdep *indirdep; 11302 11303 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE) 11304 return; 11305 indirdep = aip->ai_indirdep; 11306 LIST_REMOVE(aip, ai_next); 11307 /* 11308 * Don't set a pointer while the buffer is undergoing IO or while 11309 * we have active truncations. 11310 */ 11311 if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) { 11312 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next); 11313 return; 11314 } 11315 if (indirdep->ir_state & UFS1FMT) 11316 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11317 aip->ai_newblkno; 11318 else 11319 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11320 aip->ai_newblkno; 11321 /* 11322 * Await the pointer write before freeing the allocindir. 11323 */ 11324 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next); 11325 } 11326 11327 /* 11328 * Release segments held on a jwork list. 11329 */ 11330 static void 11331 handle_jwork(wkhd) 11332 struct workhead *wkhd; 11333 { 11334 struct worklist *wk; 11335 11336 while ((wk = LIST_FIRST(wkhd)) != NULL) { 11337 WORKLIST_REMOVE(wk); 11338 switch (wk->wk_type) { 11339 case D_JSEGDEP: 11340 free_jsegdep(WK_JSEGDEP(wk)); 11341 continue; 11342 case D_FREEDEP: 11343 free_freedep(WK_FREEDEP(wk)); 11344 continue; 11345 case D_FREEFRAG: 11346 rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep)); 11347 WORKITEM_FREE(wk, D_FREEFRAG); 11348 continue; 11349 case D_FREEWORK: 11350 handle_written_freework(WK_FREEWORK(wk)); 11351 continue; 11352 default: 11353 panic("handle_jwork: Unknown type %s\n", 11354 TYPENAME(wk->wk_type)); 11355 } 11356 } 11357 } 11358 11359 /* 11360 * Handle the bufwait list on an inode when it is safe to release items 11361 * held there. This normally happens after an inode block is written but 11362 * may be delayed and handled later if there are pending journal items that 11363 * are not yet safe to be released. 11364 */ 11365 static struct freefile * 11366 handle_bufwait(inodedep, refhd) 11367 struct inodedep *inodedep; 11368 struct workhead *refhd; 11369 { 11370 struct jaddref *jaddref; 11371 struct freefile *freefile; 11372 struct worklist *wk; 11373 11374 freefile = NULL; 11375 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) { 11376 WORKLIST_REMOVE(wk); 11377 switch (wk->wk_type) { 11378 case D_FREEFILE: 11379 /* 11380 * We defer adding freefile to the worklist 11381 * until all other additions have been made to 11382 * ensure that it will be done after all the 11383 * old blocks have been freed. 11384 */ 11385 if (freefile != NULL) 11386 panic("handle_bufwait: freefile"); 11387 freefile = WK_FREEFILE(wk); 11388 continue; 11389 11390 case D_MKDIR: 11391 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT); 11392 continue; 11393 11394 case D_DIRADD: 11395 diradd_inode_written(WK_DIRADD(wk), inodedep); 11396 continue; 11397 11398 case D_FREEFRAG: 11399 wk->wk_state |= COMPLETE; 11400 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE) 11401 add_to_worklist(wk, 0); 11402 continue; 11403 11404 case D_DIRREM: 11405 wk->wk_state |= COMPLETE; 11406 add_to_worklist(wk, 0); 11407 continue; 11408 11409 case D_ALLOCDIRECT: 11410 case D_ALLOCINDIR: 11411 free_newblk(WK_NEWBLK(wk)); 11412 continue; 11413 11414 case D_JNEWBLK: 11415 wk->wk_state |= COMPLETE; 11416 free_jnewblk(WK_JNEWBLK(wk)); 11417 continue; 11418 11419 /* 11420 * Save freed journal segments and add references on 11421 * the supplied list which will delay their release 11422 * until the cg bitmap is cleared on disk. 11423 */ 11424 case D_JSEGDEP: 11425 if (refhd == NULL) 11426 free_jsegdep(WK_JSEGDEP(wk)); 11427 else 11428 WORKLIST_INSERT(refhd, wk); 11429 continue; 11430 11431 case D_JADDREF: 11432 jaddref = WK_JADDREF(wk); 11433 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 11434 if_deps); 11435 /* 11436 * Transfer any jaddrefs to the list to be freed with 11437 * the bitmap if we're handling a removed file. 11438 */ 11439 if (refhd == NULL) { 11440 wk->wk_state |= COMPLETE; 11441 free_jaddref(jaddref); 11442 } else 11443 WORKLIST_INSERT(refhd, wk); 11444 continue; 11445 11446 default: 11447 panic("handle_bufwait: Unknown type %p(%s)", 11448 wk, TYPENAME(wk->wk_type)); 11449 /* NOTREACHED */ 11450 } 11451 } 11452 return (freefile); 11453 } 11454 /* 11455 * Called from within softdep_disk_write_complete above to restore 11456 * in-memory inode block contents to their most up-to-date state. Note 11457 * that this routine is always called from interrupt level with further 11458 * interrupts from this device blocked. 11459 * 11460 * If the write did not succeed, we will do all the roll-forward 11461 * operations, but we will not take the actions that will allow its 11462 * dependencies to be processed. 11463 */ 11464 static int 11465 handle_written_inodeblock(inodedep, bp, flags) 11466 struct inodedep *inodedep; 11467 struct buf *bp; /* buffer containing the inode block */ 11468 int flags; 11469 { 11470 struct freefile *freefile; 11471 struct allocdirect *adp, *nextadp; 11472 struct ufs1_dinode *dp1 = NULL; 11473 struct ufs2_dinode *dp2 = NULL; 11474 struct workhead wkhd; 11475 int hadchanges, fstype; 11476 ino_t freelink; 11477 11478 LIST_INIT(&wkhd); 11479 hadchanges = 0; 11480 freefile = NULL; 11481 if ((inodedep->id_state & IOSTARTED) == 0) 11482 panic("handle_written_inodeblock: not started"); 11483 inodedep->id_state &= ~IOSTARTED; 11484 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) { 11485 fstype = UFS1; 11486 dp1 = (struct ufs1_dinode *)bp->b_data + 11487 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11488 freelink = dp1->di_freelink; 11489 } else { 11490 fstype = UFS2; 11491 dp2 = (struct ufs2_dinode *)bp->b_data + 11492 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11493 freelink = dp2->di_freelink; 11494 } 11495 /* 11496 * Leave this inodeblock dirty until it's in the list. 11497 */ 11498 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED && 11499 (flags & WRITESUCCEEDED)) { 11500 struct inodedep *inon; 11501 11502 inon = TAILQ_NEXT(inodedep, id_unlinked); 11503 if ((inon == NULL && freelink == 0) || 11504 (inon && inon->id_ino == freelink)) { 11505 if (inon) 11506 inon->id_state |= UNLINKPREV; 11507 inodedep->id_state |= UNLINKNEXT; 11508 } 11509 hadchanges = 1; 11510 } 11511 /* 11512 * If we had to rollback the inode allocation because of 11513 * bitmaps being incomplete, then simply restore it. 11514 * Keep the block dirty so that it will not be reclaimed until 11515 * all associated dependencies have been cleared and the 11516 * corresponding updates written to disk. 11517 */ 11518 if (inodedep->id_savedino1 != NULL) { 11519 hadchanges = 1; 11520 if (fstype == UFS1) 11521 *dp1 = *inodedep->id_savedino1; 11522 else 11523 *dp2 = *inodedep->id_savedino2; 11524 free(inodedep->id_savedino1, M_SAVEDINO); 11525 inodedep->id_savedino1 = NULL; 11526 if ((bp->b_flags & B_DELWRI) == 0) 11527 stat_inode_bitmap++; 11528 bdirty(bp); 11529 /* 11530 * If the inode is clear here and GOINGAWAY it will never 11531 * be written. Process the bufwait and clear any pending 11532 * work which may include the freefile. 11533 */ 11534 if (inodedep->id_state & GOINGAWAY) 11535 goto bufwait; 11536 return (1); 11537 } 11538 if (flags & WRITESUCCEEDED) 11539 inodedep->id_state |= COMPLETE; 11540 /* 11541 * Roll forward anything that had to be rolled back before 11542 * the inode could be updated. 11543 */ 11544 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) { 11545 nextadp = TAILQ_NEXT(adp, ad_next); 11546 if (adp->ad_state & ATTACHED) 11547 panic("handle_written_inodeblock: new entry"); 11548 if (fstype == UFS1) { 11549 if (adp->ad_offset < UFS_NDADDR) { 11550 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11551 panic("%s %s #%jd mismatch %d != %jd", 11552 "handle_written_inodeblock:", 11553 "direct pointer", 11554 (intmax_t)adp->ad_offset, 11555 dp1->di_db[adp->ad_offset], 11556 (intmax_t)adp->ad_oldblkno); 11557 dp1->di_db[adp->ad_offset] = adp->ad_newblkno; 11558 } else { 11559 if (dp1->di_ib[adp->ad_offset - UFS_NDADDR] != 11560 0) 11561 panic("%s: %s #%jd allocated as %d", 11562 "handle_written_inodeblock", 11563 "indirect pointer", 11564 (intmax_t)adp->ad_offset - 11565 UFS_NDADDR, 11566 dp1->di_ib[adp->ad_offset - 11567 UFS_NDADDR]); 11568 dp1->di_ib[adp->ad_offset - UFS_NDADDR] = 11569 adp->ad_newblkno; 11570 } 11571 } else { 11572 if (adp->ad_offset < UFS_NDADDR) { 11573 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11574 panic("%s: %s #%jd %s %jd != %jd", 11575 "handle_written_inodeblock", 11576 "direct pointer", 11577 (intmax_t)adp->ad_offset, "mismatch", 11578 (intmax_t)dp2->di_db[adp->ad_offset], 11579 (intmax_t)adp->ad_oldblkno); 11580 dp2->di_db[adp->ad_offset] = adp->ad_newblkno; 11581 } else { 11582 if (dp2->di_ib[adp->ad_offset - UFS_NDADDR] != 11583 0) 11584 panic("%s: %s #%jd allocated as %jd", 11585 "handle_written_inodeblock", 11586 "indirect pointer", 11587 (intmax_t)adp->ad_offset - 11588 UFS_NDADDR, 11589 (intmax_t) 11590 dp2->di_ib[adp->ad_offset - 11591 UFS_NDADDR]); 11592 dp2->di_ib[adp->ad_offset - UFS_NDADDR] = 11593 adp->ad_newblkno; 11594 } 11595 } 11596 adp->ad_state &= ~UNDONE; 11597 adp->ad_state |= ATTACHED; 11598 hadchanges = 1; 11599 } 11600 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) { 11601 nextadp = TAILQ_NEXT(adp, ad_next); 11602 if (adp->ad_state & ATTACHED) 11603 panic("handle_written_inodeblock: new entry"); 11604 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno) 11605 panic("%s: direct pointers #%jd %s %jd != %jd", 11606 "handle_written_inodeblock", 11607 (intmax_t)adp->ad_offset, "mismatch", 11608 (intmax_t)dp2->di_extb[adp->ad_offset], 11609 (intmax_t)adp->ad_oldblkno); 11610 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno; 11611 adp->ad_state &= ~UNDONE; 11612 adp->ad_state |= ATTACHED; 11613 hadchanges = 1; 11614 } 11615 if (hadchanges && (bp->b_flags & B_DELWRI) == 0) 11616 stat_direct_blk_ptrs++; 11617 /* 11618 * Reset the file size to its most up-to-date value. 11619 */ 11620 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1) 11621 panic("handle_written_inodeblock: bad size"); 11622 if (inodedep->id_savednlink > UFS_LINK_MAX) 11623 panic("handle_written_inodeblock: Invalid link count " 11624 "%jd for inodedep %p", (uintmax_t)inodedep->id_savednlink, 11625 inodedep); 11626 if (fstype == UFS1) { 11627 if (dp1->di_nlink != inodedep->id_savednlink) { 11628 dp1->di_nlink = inodedep->id_savednlink; 11629 hadchanges = 1; 11630 } 11631 if (dp1->di_size != inodedep->id_savedsize) { 11632 dp1->di_size = inodedep->id_savedsize; 11633 hadchanges = 1; 11634 } 11635 } else { 11636 if (dp2->di_nlink != inodedep->id_savednlink) { 11637 dp2->di_nlink = inodedep->id_savednlink; 11638 hadchanges = 1; 11639 } 11640 if (dp2->di_size != inodedep->id_savedsize) { 11641 dp2->di_size = inodedep->id_savedsize; 11642 hadchanges = 1; 11643 } 11644 if (dp2->di_extsize != inodedep->id_savedextsize) { 11645 dp2->di_extsize = inodedep->id_savedextsize; 11646 hadchanges = 1; 11647 } 11648 } 11649 inodedep->id_savedsize = -1; 11650 inodedep->id_savedextsize = -1; 11651 inodedep->id_savednlink = -1; 11652 /* 11653 * If there were any rollbacks in the inode block, then it must be 11654 * marked dirty so that its will eventually get written back in 11655 * its correct form. 11656 */ 11657 if (hadchanges) { 11658 if (fstype == UFS2) 11659 ffs_update_dinode_ckhash(inodedep->id_fs, dp2); 11660 bdirty(bp); 11661 } 11662 bufwait: 11663 /* 11664 * If the write did not succeed, we have done all the roll-forward 11665 * operations, but we cannot take the actions that will allow its 11666 * dependencies to be processed. 11667 */ 11668 if ((flags & WRITESUCCEEDED) == 0) 11669 return (hadchanges); 11670 /* 11671 * Process any allocdirects that completed during the update. 11672 */ 11673 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 11674 handle_allocdirect_partdone(adp, &wkhd); 11675 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 11676 handle_allocdirect_partdone(adp, &wkhd); 11677 /* 11678 * Process deallocations that were held pending until the 11679 * inode had been written to disk. Freeing of the inode 11680 * is delayed until after all blocks have been freed to 11681 * avoid creation of new <vfsid, inum, lbn> triples 11682 * before the old ones have been deleted. Completely 11683 * unlinked inodes are not processed until the unlinked 11684 * inode list is written or the last reference is removed. 11685 */ 11686 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) { 11687 freefile = handle_bufwait(inodedep, NULL); 11688 if (freefile && !LIST_EMPTY(&wkhd)) { 11689 WORKLIST_INSERT(&wkhd, &freefile->fx_list); 11690 freefile = NULL; 11691 } 11692 } 11693 /* 11694 * Move rolled forward dependency completions to the bufwait list 11695 * now that those that were already written have been processed. 11696 */ 11697 if (!LIST_EMPTY(&wkhd) && hadchanges == 0) 11698 panic("handle_written_inodeblock: bufwait but no changes"); 11699 jwork_move(&inodedep->id_bufwait, &wkhd); 11700 11701 if (freefile != NULL) { 11702 /* 11703 * If the inode is goingaway it was never written. Fake up 11704 * the state here so free_inodedep() can succeed. 11705 */ 11706 if (inodedep->id_state & GOINGAWAY) 11707 inodedep->id_state |= COMPLETE | DEPCOMPLETE; 11708 if (free_inodedep(inodedep) == 0) 11709 panic("handle_written_inodeblock: live inodedep %p", 11710 inodedep); 11711 add_to_worklist(&freefile->fx_list, 0); 11712 return (0); 11713 } 11714 11715 /* 11716 * If no outstanding dependencies, free it. 11717 */ 11718 if (free_inodedep(inodedep) || 11719 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 && 11720 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 && 11721 TAILQ_FIRST(&inodedep->id_extupdt) == 0 && 11722 LIST_FIRST(&inodedep->id_bufwait) == 0)) 11723 return (0); 11724 return (hadchanges); 11725 } 11726 11727 /* 11728 * Perform needed roll-forwards and kick off any dependencies that 11729 * can now be processed. 11730 * 11731 * If the write did not succeed, we will do all the roll-forward 11732 * operations, but we will not take the actions that will allow its 11733 * dependencies to be processed. 11734 */ 11735 static int 11736 handle_written_indirdep(indirdep, bp, bpp, flags) 11737 struct indirdep *indirdep; 11738 struct buf *bp; 11739 struct buf **bpp; 11740 int flags; 11741 { 11742 struct allocindir *aip; 11743 struct buf *sbp; 11744 int chgs; 11745 11746 if (indirdep->ir_state & GOINGAWAY) 11747 panic("handle_written_indirdep: indirdep gone"); 11748 if ((indirdep->ir_state & IOSTARTED) == 0) 11749 panic("handle_written_indirdep: IO not started"); 11750 chgs = 0; 11751 /* 11752 * If there were rollbacks revert them here. 11753 */ 11754 if (indirdep->ir_saveddata) { 11755 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount); 11756 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11757 free(indirdep->ir_saveddata, M_INDIRDEP); 11758 indirdep->ir_saveddata = NULL; 11759 } 11760 chgs = 1; 11761 } 11762 indirdep->ir_state &= ~(UNDONE | IOSTARTED); 11763 indirdep->ir_state |= ATTACHED; 11764 /* 11765 * If the write did not succeed, we have done all the roll-forward 11766 * operations, but we cannot take the actions that will allow its 11767 * dependencies to be processed. 11768 */ 11769 if ((flags & WRITESUCCEEDED) == 0) { 11770 stat_indir_blk_ptrs++; 11771 bdirty(bp); 11772 return (1); 11773 } 11774 /* 11775 * Move allocindirs with written pointers to the completehd if 11776 * the indirdep's pointer is not yet written. Otherwise 11777 * free them here. 11778 */ 11779 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) { 11780 LIST_REMOVE(aip, ai_next); 11781 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 11782 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip, 11783 ai_next); 11784 newblk_freefrag(&aip->ai_block); 11785 continue; 11786 } 11787 free_newblk(&aip->ai_block); 11788 } 11789 /* 11790 * Move allocindirs that have finished dependency processing from 11791 * the done list to the write list after updating the pointers. 11792 */ 11793 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11794 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) { 11795 handle_allocindir_partdone(aip); 11796 if (aip == LIST_FIRST(&indirdep->ir_donehd)) 11797 panic("disk_write_complete: not gone"); 11798 chgs = 1; 11799 } 11800 } 11801 /* 11802 * Preserve the indirdep if there were any changes or if it is not 11803 * yet valid on disk. 11804 */ 11805 if (chgs) { 11806 stat_indir_blk_ptrs++; 11807 bdirty(bp); 11808 return (1); 11809 } 11810 /* 11811 * If there were no changes we can discard the savedbp and detach 11812 * ourselves from the buf. We are only carrying completed pointers 11813 * in this case. 11814 */ 11815 sbp = indirdep->ir_savebp; 11816 sbp->b_flags |= B_INVAL | B_NOCACHE; 11817 indirdep->ir_savebp = NULL; 11818 indirdep->ir_bp = NULL; 11819 if (*bpp != NULL) 11820 panic("handle_written_indirdep: bp already exists."); 11821 *bpp = sbp; 11822 /* 11823 * The indirdep may not be freed until its parent points at it. 11824 */ 11825 if (indirdep->ir_state & DEPCOMPLETE) 11826 free_indirdep(indirdep); 11827 11828 return (0); 11829 } 11830 11831 /* 11832 * Process a diradd entry after its dependent inode has been written. 11833 */ 11834 static void 11835 diradd_inode_written(dap, inodedep) 11836 struct diradd *dap; 11837 struct inodedep *inodedep; 11838 { 11839 11840 LOCK_OWNED(VFSTOUFS(dap->da_list.wk_mp)); 11841 dap->da_state |= COMPLETE; 11842 complete_diradd(dap); 11843 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 11844 } 11845 11846 /* 11847 * Returns true if the bmsafemap will have rollbacks when written. Must only 11848 * be called with the per-filesystem lock and the buf lock on the cg held. 11849 */ 11850 static int 11851 bmsafemap_backgroundwrite(bmsafemap, bp) 11852 struct bmsafemap *bmsafemap; 11853 struct buf *bp; 11854 { 11855 int dirty; 11856 11857 LOCK_OWNED(VFSTOUFS(bmsafemap->sm_list.wk_mp)); 11858 dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) | 11859 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd); 11860 /* 11861 * If we're initiating a background write we need to process the 11862 * rollbacks as they exist now, not as they exist when IO starts. 11863 * No other consumers will look at the contents of the shadowed 11864 * buf so this is safe to do here. 11865 */ 11866 if (bp->b_xflags & BX_BKGRDMARKER) 11867 initiate_write_bmsafemap(bmsafemap, bp); 11868 11869 return (dirty); 11870 } 11871 11872 /* 11873 * Re-apply an allocation when a cg write is complete. 11874 */ 11875 static int 11876 jnewblk_rollforward(jnewblk, fs, cgp, blksfree) 11877 struct jnewblk *jnewblk; 11878 struct fs *fs; 11879 struct cg *cgp; 11880 uint8_t *blksfree; 11881 { 11882 ufs1_daddr_t fragno; 11883 ufs2_daddr_t blkno; 11884 long cgbno, bbase; 11885 int frags, blk; 11886 int i; 11887 11888 frags = 0; 11889 cgbno = dtogd(fs, jnewblk->jn_blkno); 11890 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) { 11891 if (isclr(blksfree, cgbno + i)) 11892 panic("jnewblk_rollforward: re-allocated fragment"); 11893 frags++; 11894 } 11895 if (frags == fs->fs_frag) { 11896 blkno = fragstoblks(fs, cgbno); 11897 ffs_clrblock(fs, blksfree, (long)blkno); 11898 ffs_clusteracct(fs, cgp, blkno, -1); 11899 cgp->cg_cs.cs_nbfree--; 11900 } else { 11901 bbase = cgbno - fragnum(fs, cgbno); 11902 cgbno += jnewblk->jn_oldfrags; 11903 /* If a complete block had been reassembled, account for it. */ 11904 fragno = fragstoblks(fs, bbase); 11905 if (ffs_isblock(fs, blksfree, fragno)) { 11906 cgp->cg_cs.cs_nffree += fs->fs_frag; 11907 ffs_clusteracct(fs, cgp, fragno, -1); 11908 cgp->cg_cs.cs_nbfree--; 11909 } 11910 /* Decrement the old frags. */ 11911 blk = blkmap(fs, blksfree, bbase); 11912 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 11913 /* Allocate the fragment */ 11914 for (i = 0; i < frags; i++) 11915 clrbit(blksfree, cgbno + i); 11916 cgp->cg_cs.cs_nffree -= frags; 11917 /* Add back in counts associated with the new frags */ 11918 blk = blkmap(fs, blksfree, bbase); 11919 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 11920 } 11921 return (frags); 11922 } 11923 11924 /* 11925 * Complete a write to a bmsafemap structure. Roll forward any bitmap 11926 * changes if it's not a background write. Set all written dependencies 11927 * to DEPCOMPLETE and free the structure if possible. 11928 * 11929 * If the write did not succeed, we will do all the roll-forward 11930 * operations, but we will not take the actions that will allow its 11931 * dependencies to be processed. 11932 */ 11933 static int 11934 handle_written_bmsafemap(bmsafemap, bp, flags) 11935 struct bmsafemap *bmsafemap; 11936 struct buf *bp; 11937 int flags; 11938 { 11939 struct newblk *newblk; 11940 struct inodedep *inodedep; 11941 struct jaddref *jaddref, *jatmp; 11942 struct jnewblk *jnewblk, *jntmp; 11943 struct ufsmount *ump; 11944 uint8_t *inosused; 11945 uint8_t *blksfree; 11946 struct cg *cgp; 11947 struct fs *fs; 11948 ino_t ino; 11949 int foreground; 11950 int chgs; 11951 11952 if ((bmsafemap->sm_state & IOSTARTED) == 0) 11953 panic("handle_written_bmsafemap: Not started\n"); 11954 ump = VFSTOUFS(bmsafemap->sm_list.wk_mp); 11955 chgs = 0; 11956 bmsafemap->sm_state &= ~IOSTARTED; 11957 foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0; 11958 /* 11959 * If write was successful, release journal work that was waiting 11960 * on the write. Otherwise move the work back. 11961 */ 11962 if (flags & WRITESUCCEEDED) 11963 handle_jwork(&bmsafemap->sm_freewr); 11964 else 11965 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, 11966 worklist, wk_list); 11967 11968 /* 11969 * Restore unwritten inode allocation pending jaddref writes. 11970 */ 11971 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) { 11972 cgp = (struct cg *)bp->b_data; 11973 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11974 inosused = cg_inosused(cgp); 11975 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd, 11976 ja_bmdeps, jatmp) { 11977 if ((jaddref->ja_state & UNDONE) == 0) 11978 continue; 11979 ino = jaddref->ja_ino % fs->fs_ipg; 11980 if (isset(inosused, ino)) 11981 panic("handle_written_bmsafemap: " 11982 "re-allocated inode"); 11983 /* Do the roll-forward only if it's a real copy. */ 11984 if (foreground) { 11985 if ((jaddref->ja_mode & IFMT) == IFDIR) 11986 cgp->cg_cs.cs_ndir++; 11987 cgp->cg_cs.cs_nifree--; 11988 setbit(inosused, ino); 11989 chgs = 1; 11990 } 11991 jaddref->ja_state &= ~UNDONE; 11992 jaddref->ja_state |= ATTACHED; 11993 free_jaddref(jaddref); 11994 } 11995 } 11996 /* 11997 * Restore any block allocations which are pending journal writes. 11998 */ 11999 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 12000 cgp = (struct cg *)bp->b_data; 12001 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 12002 blksfree = cg_blksfree(cgp); 12003 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps, 12004 jntmp) { 12005 if ((jnewblk->jn_state & UNDONE) == 0) 12006 continue; 12007 /* Do the roll-forward only if it's a real copy. */ 12008 if (foreground && 12009 jnewblk_rollforward(jnewblk, fs, cgp, blksfree)) 12010 chgs = 1; 12011 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK); 12012 jnewblk->jn_state |= ATTACHED; 12013 free_jnewblk(jnewblk); 12014 } 12015 } 12016 /* 12017 * If the write did not succeed, we have done all the roll-forward 12018 * operations, but we cannot take the actions that will allow its 12019 * dependencies to be processed. 12020 */ 12021 if ((flags & WRITESUCCEEDED) == 0) { 12022 LIST_CONCAT(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 12023 newblk, nb_deps); 12024 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, 12025 worklist, wk_list); 12026 if (foreground) 12027 bdirty(bp); 12028 return (1); 12029 } 12030 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) { 12031 newblk->nb_state |= DEPCOMPLETE; 12032 newblk->nb_state &= ~ONDEPLIST; 12033 newblk->nb_bmsafemap = NULL; 12034 LIST_REMOVE(newblk, nb_deps); 12035 if (newblk->nb_list.wk_type == D_ALLOCDIRECT) 12036 handle_allocdirect_partdone( 12037 WK_ALLOCDIRECT(&newblk->nb_list), NULL); 12038 else if (newblk->nb_list.wk_type == D_ALLOCINDIR) 12039 handle_allocindir_partdone( 12040 WK_ALLOCINDIR(&newblk->nb_list)); 12041 else if (newblk->nb_list.wk_type != D_NEWBLK) 12042 panic("handle_written_bmsafemap: Unexpected type: %s", 12043 TYPENAME(newblk->nb_list.wk_type)); 12044 } 12045 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) { 12046 inodedep->id_state |= DEPCOMPLETE; 12047 inodedep->id_state &= ~ONDEPLIST; 12048 LIST_REMOVE(inodedep, id_deps); 12049 inodedep->id_bmsafemap = NULL; 12050 } 12051 LIST_REMOVE(bmsafemap, sm_next); 12052 if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) && 12053 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) && 12054 LIST_EMPTY(&bmsafemap->sm_newblkhd) && 12055 LIST_EMPTY(&bmsafemap->sm_inodedephd) && 12056 LIST_EMPTY(&bmsafemap->sm_freehd)) { 12057 LIST_REMOVE(bmsafemap, sm_hash); 12058 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 12059 return (0); 12060 } 12061 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 12062 if (foreground) 12063 bdirty(bp); 12064 return (1); 12065 } 12066 12067 /* 12068 * Try to free a mkdir dependency. 12069 */ 12070 static void 12071 complete_mkdir(mkdir) 12072 struct mkdir *mkdir; 12073 { 12074 struct diradd *dap; 12075 12076 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE) 12077 return; 12078 LIST_REMOVE(mkdir, md_mkdirs); 12079 dap = mkdir->md_diradd; 12080 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 12081 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) { 12082 dap->da_state |= DEPCOMPLETE; 12083 complete_diradd(dap); 12084 } 12085 WORKITEM_FREE(mkdir, D_MKDIR); 12086 } 12087 12088 /* 12089 * Handle the completion of a mkdir dependency. 12090 */ 12091 static void 12092 handle_written_mkdir(mkdir, type) 12093 struct mkdir *mkdir; 12094 int type; 12095 { 12096 12097 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type) 12098 panic("handle_written_mkdir: bad type"); 12099 mkdir->md_state |= COMPLETE; 12100 complete_mkdir(mkdir); 12101 } 12102 12103 static int 12104 free_pagedep(pagedep) 12105 struct pagedep *pagedep; 12106 { 12107 int i; 12108 12109 if (pagedep->pd_state & NEWBLOCK) 12110 return (0); 12111 if (!LIST_EMPTY(&pagedep->pd_dirremhd)) 12112 return (0); 12113 for (i = 0; i < DAHASHSZ; i++) 12114 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i])) 12115 return (0); 12116 if (!LIST_EMPTY(&pagedep->pd_pendinghd)) 12117 return (0); 12118 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd)) 12119 return (0); 12120 if (pagedep->pd_state & ONWORKLIST) 12121 WORKLIST_REMOVE(&pagedep->pd_list); 12122 LIST_REMOVE(pagedep, pd_hash); 12123 WORKITEM_FREE(pagedep, D_PAGEDEP); 12124 12125 return (1); 12126 } 12127 12128 /* 12129 * Called from within softdep_disk_write_complete above. 12130 * A write operation was just completed. Removed inodes can 12131 * now be freed and associated block pointers may be committed. 12132 * Note that this routine is always called from interrupt level 12133 * with further interrupts from this device blocked. 12134 * 12135 * If the write did not succeed, we will do all the roll-forward 12136 * operations, but we will not take the actions that will allow its 12137 * dependencies to be processed. 12138 */ 12139 static int 12140 handle_written_filepage(pagedep, bp, flags) 12141 struct pagedep *pagedep; 12142 struct buf *bp; /* buffer containing the written page */ 12143 int flags; 12144 { 12145 struct dirrem *dirrem; 12146 struct diradd *dap, *nextdap; 12147 struct direct *ep; 12148 int i, chgs; 12149 12150 if ((pagedep->pd_state & IOSTARTED) == 0) 12151 panic("handle_written_filepage: not started"); 12152 pagedep->pd_state &= ~IOSTARTED; 12153 if ((flags & WRITESUCCEEDED) == 0) 12154 goto rollforward; 12155 /* 12156 * Process any directory removals that have been committed. 12157 */ 12158 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) { 12159 LIST_REMOVE(dirrem, dm_next); 12160 dirrem->dm_state |= COMPLETE; 12161 dirrem->dm_dirinum = pagedep->pd_ino; 12162 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 12163 ("handle_written_filepage: Journal entries not written.")); 12164 add_to_worklist(&dirrem->dm_list, 0); 12165 } 12166 /* 12167 * Free any directory additions that have been committed. 12168 * If it is a newly allocated block, we have to wait until 12169 * the on-disk directory inode claims the new block. 12170 */ 12171 if ((pagedep->pd_state & NEWBLOCK) == 0) 12172 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 12173 free_diradd(dap, NULL); 12174 rollforward: 12175 /* 12176 * Uncommitted directory entries must be restored. 12177 */ 12178 for (chgs = 0, i = 0; i < DAHASHSZ; i++) { 12179 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap; 12180 dap = nextdap) { 12181 nextdap = LIST_NEXT(dap, da_pdlist); 12182 if (dap->da_state & ATTACHED) 12183 panic("handle_written_filepage: attached"); 12184 ep = (struct direct *) 12185 ((char *)bp->b_data + dap->da_offset); 12186 ep->d_ino = dap->da_newinum; 12187 dap->da_state &= ~UNDONE; 12188 dap->da_state |= ATTACHED; 12189 chgs = 1; 12190 /* 12191 * If the inode referenced by the directory has 12192 * been written out, then the dependency can be 12193 * moved to the pending list. 12194 */ 12195 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 12196 LIST_REMOVE(dap, da_pdlist); 12197 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, 12198 da_pdlist); 12199 } 12200 } 12201 } 12202 /* 12203 * If there were any rollbacks in the directory, then it must be 12204 * marked dirty so that its will eventually get written back in 12205 * its correct form. 12206 */ 12207 if (chgs || (flags & WRITESUCCEEDED) == 0) { 12208 if ((bp->b_flags & B_DELWRI) == 0) 12209 stat_dir_entry++; 12210 bdirty(bp); 12211 return (1); 12212 } 12213 /* 12214 * If we are not waiting for a new directory block to be 12215 * claimed by its inode, then the pagedep will be freed. 12216 * Otherwise it will remain to track any new entries on 12217 * the page in case they are fsync'ed. 12218 */ 12219 free_pagedep(pagedep); 12220 return (0); 12221 } 12222 12223 /* 12224 * Writing back in-core inode structures. 12225 * 12226 * The filesystem only accesses an inode's contents when it occupies an 12227 * "in-core" inode structure. These "in-core" structures are separate from 12228 * the page frames used to cache inode blocks. Only the latter are 12229 * transferred to/from the disk. So, when the updated contents of the 12230 * "in-core" inode structure are copied to the corresponding in-memory inode 12231 * block, the dependencies are also transferred. The following procedure is 12232 * called when copying a dirty "in-core" inode to a cached inode block. 12233 */ 12234 12235 /* 12236 * Called when an inode is loaded from disk. If the effective link count 12237 * differed from the actual link count when it was last flushed, then we 12238 * need to ensure that the correct effective link count is put back. 12239 */ 12240 void 12241 softdep_load_inodeblock(ip) 12242 struct inode *ip; /* the "in_core" copy of the inode */ 12243 { 12244 struct inodedep *inodedep; 12245 struct ufsmount *ump; 12246 12247 ump = ITOUMP(ip); 12248 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 12249 ("softdep_load_inodeblock called on non-softdep filesystem")); 12250 /* 12251 * Check for alternate nlink count. 12252 */ 12253 ip->i_effnlink = ip->i_nlink; 12254 ACQUIRE_LOCK(ump); 12255 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) { 12256 FREE_LOCK(ump); 12257 return; 12258 } 12259 ip->i_effnlink -= inodedep->id_nlinkdelta; 12260 KASSERT(ip->i_effnlink >= 0, 12261 ("softdep_load_inodeblock: negative i_effnlink")); 12262 FREE_LOCK(ump); 12263 } 12264 12265 /* 12266 * This routine is called just before the "in-core" inode 12267 * information is to be copied to the in-memory inode block. 12268 * Recall that an inode block contains several inodes. If 12269 * the force flag is set, then the dependencies will be 12270 * cleared so that the update can always be made. Note that 12271 * the buffer is locked when this routine is called, so we 12272 * will never be in the middle of writing the inode block 12273 * to disk. 12274 */ 12275 void 12276 softdep_update_inodeblock(ip, bp, waitfor) 12277 struct inode *ip; /* the "in_core" copy of the inode */ 12278 struct buf *bp; /* the buffer containing the inode block */ 12279 int waitfor; /* nonzero => update must be allowed */ 12280 { 12281 struct inodedep *inodedep; 12282 struct inoref *inoref; 12283 struct ufsmount *ump; 12284 struct worklist *wk; 12285 struct mount *mp; 12286 struct buf *ibp; 12287 struct fs *fs; 12288 int error; 12289 12290 ump = ITOUMP(ip); 12291 mp = UFSTOVFS(ump); 12292 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 12293 ("softdep_update_inodeblock called on non-softdep filesystem")); 12294 fs = ump->um_fs; 12295 /* 12296 * Preserve the freelink that is on disk. clear_unlinked_inodedep() 12297 * does not have access to the in-core ip so must write directly into 12298 * the inode block buffer when setting freelink. 12299 */ 12300 if (fs->fs_magic == FS_UFS1_MAGIC) 12301 DIP_SET(ip, i_freelink, ((struct ufs1_dinode *)bp->b_data + 12302 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12303 else 12304 DIP_SET(ip, i_freelink, ((struct ufs2_dinode *)bp->b_data + 12305 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12306 /* 12307 * If the effective link count is not equal to the actual link 12308 * count, then we must track the difference in an inodedep while 12309 * the inode is (potentially) tossed out of the cache. Otherwise, 12310 * if there is no existing inodedep, then there are no dependencies 12311 * to track. 12312 */ 12313 ACQUIRE_LOCK(ump); 12314 again: 12315 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12316 FREE_LOCK(ump); 12317 if (ip->i_effnlink != ip->i_nlink) 12318 panic("softdep_update_inodeblock: bad link count"); 12319 return; 12320 } 12321 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink) 12322 panic("softdep_update_inodeblock: bad delta"); 12323 /* 12324 * If we're flushing all dependencies we must also move any waiting 12325 * for journal writes onto the bufwait list prior to I/O. 12326 */ 12327 if (waitfor) { 12328 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12329 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12330 == DEPCOMPLETE) { 12331 jwait(&inoref->if_list, MNT_WAIT); 12332 goto again; 12333 } 12334 } 12335 } 12336 /* 12337 * Changes have been initiated. Anything depending on these 12338 * changes cannot occur until this inode has been written. 12339 */ 12340 inodedep->id_state &= ~COMPLETE; 12341 if ((inodedep->id_state & ONWORKLIST) == 0) 12342 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list); 12343 /* 12344 * Any new dependencies associated with the incore inode must 12345 * now be moved to the list associated with the buffer holding 12346 * the in-memory copy of the inode. Once merged process any 12347 * allocdirects that are completed by the merger. 12348 */ 12349 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt); 12350 if (!TAILQ_EMPTY(&inodedep->id_inoupdt)) 12351 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt), 12352 NULL); 12353 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt); 12354 if (!TAILQ_EMPTY(&inodedep->id_extupdt)) 12355 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt), 12356 NULL); 12357 /* 12358 * Now that the inode has been pushed into the buffer, the 12359 * operations dependent on the inode being written to disk 12360 * can be moved to the id_bufwait so that they will be 12361 * processed when the buffer I/O completes. 12362 */ 12363 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) { 12364 WORKLIST_REMOVE(wk); 12365 WORKLIST_INSERT(&inodedep->id_bufwait, wk); 12366 } 12367 /* 12368 * Newly allocated inodes cannot be written until the bitmap 12369 * that allocates them have been written (indicated by 12370 * DEPCOMPLETE being set in id_state). If we are doing a 12371 * forced sync (e.g., an fsync on a file), we force the bitmap 12372 * to be written so that the update can be done. 12373 */ 12374 if (waitfor == 0) { 12375 FREE_LOCK(ump); 12376 return; 12377 } 12378 retry: 12379 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) { 12380 FREE_LOCK(ump); 12381 return; 12382 } 12383 ibp = inodedep->id_bmsafemap->sm_buf; 12384 ibp = getdirtybuf(ibp, LOCK_PTR(ump), MNT_WAIT); 12385 if (ibp == NULL) { 12386 /* 12387 * If ibp came back as NULL, the dependency could have been 12388 * freed while we slept. Look it up again, and check to see 12389 * that it has completed. 12390 */ 12391 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 12392 goto retry; 12393 FREE_LOCK(ump); 12394 return; 12395 } 12396 FREE_LOCK(ump); 12397 if ((error = bwrite(ibp)) != 0) 12398 softdep_error("softdep_update_inodeblock: bwrite", error); 12399 } 12400 12401 /* 12402 * Merge the a new inode dependency list (such as id_newinoupdt) into an 12403 * old inode dependency list (such as id_inoupdt). 12404 */ 12405 static void 12406 merge_inode_lists(newlisthead, oldlisthead) 12407 struct allocdirectlst *newlisthead; 12408 struct allocdirectlst *oldlisthead; 12409 { 12410 struct allocdirect *listadp, *newadp; 12411 12412 newadp = TAILQ_FIRST(newlisthead); 12413 if (newadp != NULL) 12414 LOCK_OWNED(VFSTOUFS(newadp->ad_block.nb_list.wk_mp)); 12415 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) { 12416 if (listadp->ad_offset < newadp->ad_offset) { 12417 listadp = TAILQ_NEXT(listadp, ad_next); 12418 continue; 12419 } 12420 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12421 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next); 12422 if (listadp->ad_offset == newadp->ad_offset) { 12423 allocdirect_merge(oldlisthead, newadp, 12424 listadp); 12425 listadp = newadp; 12426 } 12427 newadp = TAILQ_FIRST(newlisthead); 12428 } 12429 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) { 12430 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12431 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next); 12432 } 12433 } 12434 12435 /* 12436 * If we are doing an fsync, then we must ensure that any directory 12437 * entries for the inode have been written after the inode gets to disk. 12438 */ 12439 int 12440 softdep_fsync(vp) 12441 struct vnode *vp; /* the "in_core" copy of the inode */ 12442 { 12443 struct inodedep *inodedep; 12444 struct pagedep *pagedep; 12445 struct inoref *inoref; 12446 struct ufsmount *ump; 12447 struct worklist *wk; 12448 struct diradd *dap; 12449 struct mount *mp; 12450 struct vnode *pvp; 12451 struct inode *ip; 12452 struct buf *bp; 12453 struct fs *fs; 12454 struct thread *td = curthread; 12455 int error, flushparent, pagedep_new_block; 12456 ino_t parentino; 12457 ufs_lbn_t lbn; 12458 12459 ip = VTOI(vp); 12460 mp = vp->v_mount; 12461 ump = VFSTOUFS(mp); 12462 fs = ump->um_fs; 12463 if (MOUNTEDSOFTDEP(mp) == 0) 12464 return (0); 12465 ACQUIRE_LOCK(ump); 12466 restart: 12467 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12468 FREE_LOCK(ump); 12469 return (0); 12470 } 12471 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12472 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12473 == DEPCOMPLETE) { 12474 jwait(&inoref->if_list, MNT_WAIT); 12475 goto restart; 12476 } 12477 } 12478 if (!LIST_EMPTY(&inodedep->id_inowait) || 12479 !TAILQ_EMPTY(&inodedep->id_extupdt) || 12480 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 12481 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 12482 !TAILQ_EMPTY(&inodedep->id_newinoupdt)) 12483 panic("softdep_fsync: pending ops %p", inodedep); 12484 for (error = 0, flushparent = 0; ; ) { 12485 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL) 12486 break; 12487 if (wk->wk_type != D_DIRADD) 12488 panic("softdep_fsync: Unexpected type %s", 12489 TYPENAME(wk->wk_type)); 12490 dap = WK_DIRADD(wk); 12491 /* 12492 * Flush our parent if this directory entry has a MKDIR_PARENT 12493 * dependency or is contained in a newly allocated block. 12494 */ 12495 if (dap->da_state & DIRCHG) 12496 pagedep = dap->da_previous->dm_pagedep; 12497 else 12498 pagedep = dap->da_pagedep; 12499 parentino = pagedep->pd_ino; 12500 lbn = pagedep->pd_lbn; 12501 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) 12502 panic("softdep_fsync: dirty"); 12503 if ((dap->da_state & MKDIR_PARENT) || 12504 (pagedep->pd_state & NEWBLOCK)) 12505 flushparent = 1; 12506 else 12507 flushparent = 0; 12508 /* 12509 * If we are being fsync'ed as part of vgone'ing this vnode, 12510 * then we will not be able to release and recover the 12511 * vnode below, so we just have to give up on writing its 12512 * directory entry out. It will eventually be written, just 12513 * not now, but then the user was not asking to have it 12514 * written, so we are not breaking any promises. 12515 */ 12516 if (VN_IS_DOOMED(vp)) 12517 break; 12518 /* 12519 * We prevent deadlock by always fetching inodes from the 12520 * root, moving down the directory tree. Thus, when fetching 12521 * our parent directory, we first try to get the lock. If 12522 * that fails, we must unlock ourselves before requesting 12523 * the lock on our parent. See the comment in ufs_lookup 12524 * for details on possible races. 12525 */ 12526 FREE_LOCK(ump); 12527 if (ffs_vgetf(mp, parentino, LK_NOWAIT | LK_EXCLUSIVE, &pvp, 12528 FFSV_FORCEINSMQ)) { 12529 /* 12530 * Unmount cannot proceed after unlock because 12531 * caller must have called vn_start_write(). 12532 */ 12533 VOP_UNLOCK(vp, 0); 12534 error = ffs_vgetf(mp, parentino, LK_EXCLUSIVE, 12535 &pvp, FFSV_FORCEINSMQ); 12536 MPASS(VTOI(pvp)->i_mode != 0); 12537 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 12538 if (VN_IS_DOOMED(vp)) { 12539 if (error == 0) 12540 vput(pvp); 12541 error = ENOENT; 12542 } 12543 if (error != 0) 12544 return (error); 12545 } 12546 /* 12547 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps 12548 * that are contained in direct blocks will be resolved by 12549 * doing a ffs_update. Pagedeps contained in indirect blocks 12550 * may require a complete sync'ing of the directory. So, we 12551 * try the cheap and fast ffs_update first, and if that fails, 12552 * then we do the slower ffs_syncvnode of the directory. 12553 */ 12554 if (flushparent) { 12555 int locked; 12556 12557 if ((error = ffs_update(pvp, 1)) != 0) { 12558 vput(pvp); 12559 return (error); 12560 } 12561 ACQUIRE_LOCK(ump); 12562 locked = 1; 12563 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) { 12564 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) { 12565 if (wk->wk_type != D_DIRADD) 12566 panic("softdep_fsync: Unexpected type %s", 12567 TYPENAME(wk->wk_type)); 12568 dap = WK_DIRADD(wk); 12569 if (dap->da_state & DIRCHG) 12570 pagedep = dap->da_previous->dm_pagedep; 12571 else 12572 pagedep = dap->da_pagedep; 12573 pagedep_new_block = pagedep->pd_state & NEWBLOCK; 12574 FREE_LOCK(ump); 12575 locked = 0; 12576 if (pagedep_new_block && (error = 12577 ffs_syncvnode(pvp, MNT_WAIT, 0))) { 12578 vput(pvp); 12579 return (error); 12580 } 12581 } 12582 } 12583 if (locked) 12584 FREE_LOCK(ump); 12585 } 12586 /* 12587 * Flush directory page containing the inode's name. 12588 */ 12589 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred, 12590 &bp); 12591 if (error == 0) 12592 error = bwrite(bp); 12593 else 12594 brelse(bp); 12595 vput(pvp); 12596 if (error != 0) 12597 return (error); 12598 ACQUIRE_LOCK(ump); 12599 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 12600 break; 12601 } 12602 FREE_LOCK(ump); 12603 return (0); 12604 } 12605 12606 /* 12607 * Flush all the dirty bitmaps associated with the block device 12608 * before flushing the rest of the dirty blocks so as to reduce 12609 * the number of dependencies that will have to be rolled back. 12610 * 12611 * XXX Unused? 12612 */ 12613 void 12614 softdep_fsync_mountdev(vp) 12615 struct vnode *vp; 12616 { 12617 struct buf *bp, *nbp; 12618 struct worklist *wk; 12619 struct bufobj *bo; 12620 12621 if (!vn_isdisk(vp, NULL)) 12622 panic("softdep_fsync_mountdev: vnode not a disk"); 12623 bo = &vp->v_bufobj; 12624 restart: 12625 BO_LOCK(bo); 12626 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 12627 /* 12628 * If it is already scheduled, skip to the next buffer. 12629 */ 12630 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) 12631 continue; 12632 12633 if ((bp->b_flags & B_DELWRI) == 0) 12634 panic("softdep_fsync_mountdev: not dirty"); 12635 /* 12636 * We are only interested in bitmaps with outstanding 12637 * dependencies. 12638 */ 12639 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL || 12640 wk->wk_type != D_BMSAFEMAP || 12641 (bp->b_vflags & BV_BKGRDINPROG)) { 12642 BUF_UNLOCK(bp); 12643 continue; 12644 } 12645 BO_UNLOCK(bo); 12646 bremfree(bp); 12647 (void) bawrite(bp); 12648 goto restart; 12649 } 12650 drain_output(vp); 12651 BO_UNLOCK(bo); 12652 } 12653 12654 /* 12655 * Sync all cylinder groups that were dirty at the time this function is 12656 * called. Newly dirtied cgs will be inserted before the sentinel. This 12657 * is used to flush freedep activity that may be holding up writes to a 12658 * indirect block. 12659 */ 12660 static int 12661 sync_cgs(mp, waitfor) 12662 struct mount *mp; 12663 int waitfor; 12664 { 12665 struct bmsafemap *bmsafemap; 12666 struct bmsafemap *sentinel; 12667 struct ufsmount *ump; 12668 struct buf *bp; 12669 int error; 12670 12671 sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK); 12672 sentinel->sm_cg = -1; 12673 ump = VFSTOUFS(mp); 12674 error = 0; 12675 ACQUIRE_LOCK(ump); 12676 LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next); 12677 for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL; 12678 bmsafemap = LIST_NEXT(sentinel, sm_next)) { 12679 /* Skip sentinels and cgs with no work to release. */ 12680 if (bmsafemap->sm_cg == -1 || 12681 (LIST_EMPTY(&bmsafemap->sm_freehd) && 12682 LIST_EMPTY(&bmsafemap->sm_freewr))) { 12683 LIST_REMOVE(sentinel, sm_next); 12684 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 12685 continue; 12686 } 12687 /* 12688 * If we don't get the lock and we're waiting try again, if 12689 * not move on to the next buf and try to sync it. 12690 */ 12691 bp = getdirtybuf(bmsafemap->sm_buf, LOCK_PTR(ump), waitfor); 12692 if (bp == NULL && waitfor == MNT_WAIT) 12693 continue; 12694 LIST_REMOVE(sentinel, sm_next); 12695 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 12696 if (bp == NULL) 12697 continue; 12698 FREE_LOCK(ump); 12699 if (waitfor == MNT_NOWAIT) 12700 bawrite(bp); 12701 else 12702 error = bwrite(bp); 12703 ACQUIRE_LOCK(ump); 12704 if (error) 12705 break; 12706 } 12707 LIST_REMOVE(sentinel, sm_next); 12708 FREE_LOCK(ump); 12709 free(sentinel, M_BMSAFEMAP); 12710 return (error); 12711 } 12712 12713 /* 12714 * This routine is called when we are trying to synchronously flush a 12715 * file. This routine must eliminate any filesystem metadata dependencies 12716 * so that the syncing routine can succeed. 12717 */ 12718 int 12719 softdep_sync_metadata(struct vnode *vp) 12720 { 12721 struct inode *ip; 12722 int error; 12723 12724 ip = VTOI(vp); 12725 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 12726 ("softdep_sync_metadata called on non-softdep filesystem")); 12727 /* 12728 * Ensure that any direct block dependencies have been cleared, 12729 * truncations are started, and inode references are journaled. 12730 */ 12731 ACQUIRE_LOCK(VFSTOUFS(vp->v_mount)); 12732 /* 12733 * Write all journal records to prevent rollbacks on devvp. 12734 */ 12735 if (vp->v_type == VCHR) 12736 softdep_flushjournal(vp->v_mount); 12737 error = flush_inodedep_deps(vp, vp->v_mount, ip->i_number); 12738 /* 12739 * Ensure that all truncates are written so we won't find deps on 12740 * indirect blocks. 12741 */ 12742 process_truncates(vp); 12743 FREE_LOCK(VFSTOUFS(vp->v_mount)); 12744 12745 return (error); 12746 } 12747 12748 /* 12749 * This routine is called when we are attempting to sync a buf with 12750 * dependencies. If waitfor is MNT_NOWAIT it attempts to schedule any 12751 * other IO it can but returns EBUSY if the buffer is not yet able to 12752 * be written. Dependencies which will not cause rollbacks will always 12753 * return 0. 12754 */ 12755 int 12756 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 12757 { 12758 struct indirdep *indirdep; 12759 struct pagedep *pagedep; 12760 struct allocindir *aip; 12761 struct newblk *newblk; 12762 struct ufsmount *ump; 12763 struct buf *nbp; 12764 struct worklist *wk; 12765 int i, error; 12766 12767 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 12768 ("softdep_sync_buf called on non-softdep filesystem")); 12769 /* 12770 * For VCHR we just don't want to force flush any dependencies that 12771 * will cause rollbacks. 12772 */ 12773 if (vp->v_type == VCHR) { 12774 if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0)) 12775 return (EBUSY); 12776 return (0); 12777 } 12778 ump = VFSTOUFS(vp->v_mount); 12779 ACQUIRE_LOCK(ump); 12780 /* 12781 * As we hold the buffer locked, none of its dependencies 12782 * will disappear. 12783 */ 12784 error = 0; 12785 top: 12786 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 12787 switch (wk->wk_type) { 12788 12789 case D_ALLOCDIRECT: 12790 case D_ALLOCINDIR: 12791 newblk = WK_NEWBLK(wk); 12792 if (newblk->nb_jnewblk != NULL) { 12793 if (waitfor == MNT_NOWAIT) { 12794 error = EBUSY; 12795 goto out_unlock; 12796 } 12797 jwait(&newblk->nb_jnewblk->jn_list, waitfor); 12798 goto top; 12799 } 12800 if (newblk->nb_state & DEPCOMPLETE || 12801 waitfor == MNT_NOWAIT) 12802 continue; 12803 nbp = newblk->nb_bmsafemap->sm_buf; 12804 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 12805 if (nbp == NULL) 12806 goto top; 12807 FREE_LOCK(ump); 12808 if ((error = bwrite(nbp)) != 0) 12809 goto out; 12810 ACQUIRE_LOCK(ump); 12811 continue; 12812 12813 case D_INDIRDEP: 12814 indirdep = WK_INDIRDEP(wk); 12815 if (waitfor == MNT_NOWAIT) { 12816 if (!TAILQ_EMPTY(&indirdep->ir_trunc) || 12817 !LIST_EMPTY(&indirdep->ir_deplisthd)) { 12818 error = EBUSY; 12819 goto out_unlock; 12820 } 12821 } 12822 if (!TAILQ_EMPTY(&indirdep->ir_trunc)) 12823 panic("softdep_sync_buf: truncation pending."); 12824 restart: 12825 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 12826 newblk = (struct newblk *)aip; 12827 if (newblk->nb_jnewblk != NULL) { 12828 jwait(&newblk->nb_jnewblk->jn_list, 12829 waitfor); 12830 goto restart; 12831 } 12832 if (newblk->nb_state & DEPCOMPLETE) 12833 continue; 12834 nbp = newblk->nb_bmsafemap->sm_buf; 12835 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 12836 if (nbp == NULL) 12837 goto restart; 12838 FREE_LOCK(ump); 12839 if ((error = bwrite(nbp)) != 0) 12840 goto out; 12841 ACQUIRE_LOCK(ump); 12842 goto restart; 12843 } 12844 continue; 12845 12846 case D_PAGEDEP: 12847 /* 12848 * Only flush directory entries in synchronous passes. 12849 */ 12850 if (waitfor != MNT_WAIT) { 12851 error = EBUSY; 12852 goto out_unlock; 12853 } 12854 /* 12855 * While syncing snapshots, we must allow recursive 12856 * lookups. 12857 */ 12858 BUF_AREC(bp); 12859 /* 12860 * We are trying to sync a directory that may 12861 * have dependencies on both its own metadata 12862 * and/or dependencies on the inodes of any 12863 * recently allocated files. We walk its diradd 12864 * lists pushing out the associated inode. 12865 */ 12866 pagedep = WK_PAGEDEP(wk); 12867 for (i = 0; i < DAHASHSZ; i++) { 12868 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0) 12869 continue; 12870 if ((error = flush_pagedep_deps(vp, wk->wk_mp, 12871 &pagedep->pd_diraddhd[i]))) { 12872 BUF_NOREC(bp); 12873 goto out_unlock; 12874 } 12875 } 12876 BUF_NOREC(bp); 12877 continue; 12878 12879 case D_FREEWORK: 12880 case D_FREEDEP: 12881 case D_JSEGDEP: 12882 case D_JNEWBLK: 12883 continue; 12884 12885 default: 12886 panic("softdep_sync_buf: Unknown type %s", 12887 TYPENAME(wk->wk_type)); 12888 /* NOTREACHED */ 12889 } 12890 } 12891 out_unlock: 12892 FREE_LOCK(ump); 12893 out: 12894 return (error); 12895 } 12896 12897 /* 12898 * Flush the dependencies associated with an inodedep. 12899 */ 12900 static int 12901 flush_inodedep_deps(vp, mp, ino) 12902 struct vnode *vp; 12903 struct mount *mp; 12904 ino_t ino; 12905 { 12906 struct inodedep *inodedep; 12907 struct inoref *inoref; 12908 struct ufsmount *ump; 12909 int error, waitfor; 12910 12911 /* 12912 * This work is done in two passes. The first pass grabs most 12913 * of the buffers and begins asynchronously writing them. The 12914 * only way to wait for these asynchronous writes is to sleep 12915 * on the filesystem vnode which may stay busy for a long time 12916 * if the filesystem is active. So, instead, we make a second 12917 * pass over the dependencies blocking on each write. In the 12918 * usual case we will be blocking against a write that we 12919 * initiated, so when it is done the dependency will have been 12920 * resolved. Thus the second pass is expected to end quickly. 12921 * We give a brief window at the top of the loop to allow 12922 * any pending I/O to complete. 12923 */ 12924 ump = VFSTOUFS(mp); 12925 LOCK_OWNED(ump); 12926 for (error = 0, waitfor = MNT_NOWAIT; ; ) { 12927 if (error) 12928 return (error); 12929 FREE_LOCK(ump); 12930 ACQUIRE_LOCK(ump); 12931 restart: 12932 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 12933 return (0); 12934 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12935 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12936 == DEPCOMPLETE) { 12937 jwait(&inoref->if_list, MNT_WAIT); 12938 goto restart; 12939 } 12940 } 12941 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) || 12942 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) || 12943 flush_deplist(&inodedep->id_extupdt, waitfor, &error) || 12944 flush_deplist(&inodedep->id_newextupdt, waitfor, &error)) 12945 continue; 12946 /* 12947 * If pass2, we are done, otherwise do pass 2. 12948 */ 12949 if (waitfor == MNT_WAIT) 12950 break; 12951 waitfor = MNT_WAIT; 12952 } 12953 /* 12954 * Try freeing inodedep in case all dependencies have been removed. 12955 */ 12956 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0) 12957 (void) free_inodedep(inodedep); 12958 return (0); 12959 } 12960 12961 /* 12962 * Flush an inode dependency list. 12963 */ 12964 static int 12965 flush_deplist(listhead, waitfor, errorp) 12966 struct allocdirectlst *listhead; 12967 int waitfor; 12968 int *errorp; 12969 { 12970 struct allocdirect *adp; 12971 struct newblk *newblk; 12972 struct ufsmount *ump; 12973 struct buf *bp; 12974 12975 if ((adp = TAILQ_FIRST(listhead)) == NULL) 12976 return (0); 12977 ump = VFSTOUFS(adp->ad_list.wk_mp); 12978 LOCK_OWNED(ump); 12979 TAILQ_FOREACH(adp, listhead, ad_next) { 12980 newblk = (struct newblk *)adp; 12981 if (newblk->nb_jnewblk != NULL) { 12982 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 12983 return (1); 12984 } 12985 if (newblk->nb_state & DEPCOMPLETE) 12986 continue; 12987 bp = newblk->nb_bmsafemap->sm_buf; 12988 bp = getdirtybuf(bp, LOCK_PTR(ump), waitfor); 12989 if (bp == NULL) { 12990 if (waitfor == MNT_NOWAIT) 12991 continue; 12992 return (1); 12993 } 12994 FREE_LOCK(ump); 12995 if (waitfor == MNT_NOWAIT) 12996 bawrite(bp); 12997 else 12998 *errorp = bwrite(bp); 12999 ACQUIRE_LOCK(ump); 13000 return (1); 13001 } 13002 return (0); 13003 } 13004 13005 /* 13006 * Flush dependencies associated with an allocdirect block. 13007 */ 13008 static int 13009 flush_newblk_dep(vp, mp, lbn) 13010 struct vnode *vp; 13011 struct mount *mp; 13012 ufs_lbn_t lbn; 13013 { 13014 struct newblk *newblk; 13015 struct ufsmount *ump; 13016 struct bufobj *bo; 13017 struct inode *ip; 13018 struct buf *bp; 13019 ufs2_daddr_t blkno; 13020 int error; 13021 13022 error = 0; 13023 bo = &vp->v_bufobj; 13024 ip = VTOI(vp); 13025 blkno = DIP(ip, i_db[lbn]); 13026 if (blkno == 0) 13027 panic("flush_newblk_dep: Missing block"); 13028 ump = VFSTOUFS(mp); 13029 ACQUIRE_LOCK(ump); 13030 /* 13031 * Loop until all dependencies related to this block are satisfied. 13032 * We must be careful to restart after each sleep in case a write 13033 * completes some part of this process for us. 13034 */ 13035 for (;;) { 13036 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) { 13037 FREE_LOCK(ump); 13038 break; 13039 } 13040 if (newblk->nb_list.wk_type != D_ALLOCDIRECT) 13041 panic("flush_newblk_dep: Bad newblk %p", newblk); 13042 /* 13043 * Flush the journal. 13044 */ 13045 if (newblk->nb_jnewblk != NULL) { 13046 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 13047 continue; 13048 } 13049 /* 13050 * Write the bitmap dependency. 13051 */ 13052 if ((newblk->nb_state & DEPCOMPLETE) == 0) { 13053 bp = newblk->nb_bmsafemap->sm_buf; 13054 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 13055 if (bp == NULL) 13056 continue; 13057 FREE_LOCK(ump); 13058 error = bwrite(bp); 13059 if (error) 13060 break; 13061 ACQUIRE_LOCK(ump); 13062 continue; 13063 } 13064 /* 13065 * Write the buffer. 13066 */ 13067 FREE_LOCK(ump); 13068 BO_LOCK(bo); 13069 bp = gbincore(bo, lbn); 13070 if (bp != NULL) { 13071 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 13072 LK_INTERLOCK, BO_LOCKPTR(bo)); 13073 if (error == ENOLCK) { 13074 ACQUIRE_LOCK(ump); 13075 error = 0; 13076 continue; /* Slept, retry */ 13077 } 13078 if (error != 0) 13079 break; /* Failed */ 13080 if (bp->b_flags & B_DELWRI) { 13081 bremfree(bp); 13082 error = bwrite(bp); 13083 if (error) 13084 break; 13085 } else 13086 BUF_UNLOCK(bp); 13087 } else 13088 BO_UNLOCK(bo); 13089 /* 13090 * We have to wait for the direct pointers to 13091 * point at the newdirblk before the dependency 13092 * will go away. 13093 */ 13094 error = ffs_update(vp, 1); 13095 if (error) 13096 break; 13097 ACQUIRE_LOCK(ump); 13098 } 13099 return (error); 13100 } 13101 13102 /* 13103 * Eliminate a pagedep dependency by flushing out all its diradd dependencies. 13104 */ 13105 static int 13106 flush_pagedep_deps(pvp, mp, diraddhdp) 13107 struct vnode *pvp; 13108 struct mount *mp; 13109 struct diraddhd *diraddhdp; 13110 { 13111 struct inodedep *inodedep; 13112 struct inoref *inoref; 13113 struct ufsmount *ump; 13114 struct diradd *dap; 13115 struct vnode *vp; 13116 int error = 0; 13117 struct buf *bp; 13118 ino_t inum; 13119 struct diraddhd unfinished; 13120 13121 LIST_INIT(&unfinished); 13122 ump = VFSTOUFS(mp); 13123 LOCK_OWNED(ump); 13124 restart: 13125 while ((dap = LIST_FIRST(diraddhdp)) != NULL) { 13126 /* 13127 * Flush ourselves if this directory entry 13128 * has a MKDIR_PARENT dependency. 13129 */ 13130 if (dap->da_state & MKDIR_PARENT) { 13131 FREE_LOCK(ump); 13132 if ((error = ffs_update(pvp, 1)) != 0) 13133 break; 13134 ACQUIRE_LOCK(ump); 13135 /* 13136 * If that cleared dependencies, go on to next. 13137 */ 13138 if (dap != LIST_FIRST(diraddhdp)) 13139 continue; 13140 /* 13141 * All MKDIR_PARENT dependencies and all the 13142 * NEWBLOCK pagedeps that are contained in direct 13143 * blocks were resolved by doing above ffs_update. 13144 * Pagedeps contained in indirect blocks may 13145 * require a complete sync'ing of the directory. 13146 * We are in the midst of doing a complete sync, 13147 * so if they are not resolved in this pass we 13148 * defer them for now as they will be sync'ed by 13149 * our caller shortly. 13150 */ 13151 LIST_REMOVE(dap, da_pdlist); 13152 LIST_INSERT_HEAD(&unfinished, dap, da_pdlist); 13153 continue; 13154 } 13155 /* 13156 * A newly allocated directory must have its "." and 13157 * ".." entries written out before its name can be 13158 * committed in its parent. 13159 */ 13160 inum = dap->da_newinum; 13161 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 13162 panic("flush_pagedep_deps: lost inode1"); 13163 /* 13164 * Wait for any pending journal adds to complete so we don't 13165 * cause rollbacks while syncing. 13166 */ 13167 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 13168 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 13169 == DEPCOMPLETE) { 13170 jwait(&inoref->if_list, MNT_WAIT); 13171 goto restart; 13172 } 13173 } 13174 if (dap->da_state & MKDIR_BODY) { 13175 FREE_LOCK(ump); 13176 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp, 13177 FFSV_FORCEINSMQ))) 13178 break; 13179 MPASS(VTOI(vp)->i_mode != 0); 13180 error = flush_newblk_dep(vp, mp, 0); 13181 /* 13182 * If we still have the dependency we might need to 13183 * update the vnode to sync the new link count to 13184 * disk. 13185 */ 13186 if (error == 0 && dap == LIST_FIRST(diraddhdp)) 13187 error = ffs_update(vp, 1); 13188 vput(vp); 13189 if (error != 0) 13190 break; 13191 ACQUIRE_LOCK(ump); 13192 /* 13193 * If that cleared dependencies, go on to next. 13194 */ 13195 if (dap != LIST_FIRST(diraddhdp)) 13196 continue; 13197 if (dap->da_state & MKDIR_BODY) { 13198 inodedep_lookup(UFSTOVFS(ump), inum, 0, 13199 &inodedep); 13200 panic("flush_pagedep_deps: MKDIR_BODY " 13201 "inodedep %p dap %p vp %p", 13202 inodedep, dap, vp); 13203 } 13204 } 13205 /* 13206 * Flush the inode on which the directory entry depends. 13207 * Having accounted for MKDIR_PARENT and MKDIR_BODY above, 13208 * the only remaining dependency is that the updated inode 13209 * count must get pushed to disk. The inode has already 13210 * been pushed into its inode buffer (via VOP_UPDATE) at 13211 * the time of the reference count change. So we need only 13212 * locate that buffer, ensure that there will be no rollback 13213 * caused by a bitmap dependency, then write the inode buffer. 13214 */ 13215 retry: 13216 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 13217 panic("flush_pagedep_deps: lost inode"); 13218 /* 13219 * If the inode still has bitmap dependencies, 13220 * push them to disk. 13221 */ 13222 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) { 13223 bp = inodedep->id_bmsafemap->sm_buf; 13224 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 13225 if (bp == NULL) 13226 goto retry; 13227 FREE_LOCK(ump); 13228 if ((error = bwrite(bp)) != 0) 13229 break; 13230 ACQUIRE_LOCK(ump); 13231 if (dap != LIST_FIRST(diraddhdp)) 13232 continue; 13233 } 13234 /* 13235 * If the inode is still sitting in a buffer waiting 13236 * to be written or waiting for the link count to be 13237 * adjusted update it here to flush it to disk. 13238 */ 13239 if (dap == LIST_FIRST(diraddhdp)) { 13240 FREE_LOCK(ump); 13241 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp, 13242 FFSV_FORCEINSMQ))) 13243 break; 13244 MPASS(VTOI(vp)->i_mode != 0); 13245 error = ffs_update(vp, 1); 13246 vput(vp); 13247 if (error) 13248 break; 13249 ACQUIRE_LOCK(ump); 13250 } 13251 /* 13252 * If we have failed to get rid of all the dependencies 13253 * then something is seriously wrong. 13254 */ 13255 if (dap == LIST_FIRST(diraddhdp)) { 13256 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep); 13257 panic("flush_pagedep_deps: failed to flush " 13258 "inodedep %p ino %ju dap %p", 13259 inodedep, (uintmax_t)inum, dap); 13260 } 13261 } 13262 if (error) 13263 ACQUIRE_LOCK(ump); 13264 while ((dap = LIST_FIRST(&unfinished)) != NULL) { 13265 LIST_REMOVE(dap, da_pdlist); 13266 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist); 13267 } 13268 return (error); 13269 } 13270 13271 /* 13272 * A large burst of file addition or deletion activity can drive the 13273 * memory load excessively high. First attempt to slow things down 13274 * using the techniques below. If that fails, this routine requests 13275 * the offending operations to fall back to running synchronously 13276 * until the memory load returns to a reasonable level. 13277 */ 13278 int 13279 softdep_slowdown(vp) 13280 struct vnode *vp; 13281 { 13282 struct ufsmount *ump; 13283 int jlow; 13284 int max_softdeps_hard; 13285 13286 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 13287 ("softdep_slowdown called on non-softdep filesystem")); 13288 ump = VFSTOUFS(vp->v_mount); 13289 ACQUIRE_LOCK(ump); 13290 jlow = 0; 13291 /* 13292 * Check for journal space if needed. 13293 */ 13294 if (DOINGSUJ(vp)) { 13295 if (journal_space(ump, 0) == 0) 13296 jlow = 1; 13297 } 13298 /* 13299 * If the system is under its limits and our filesystem is 13300 * not responsible for more than our share of the usage and 13301 * we are not low on journal space, then no need to slow down. 13302 */ 13303 max_softdeps_hard = max_softdeps * 11 / 10; 13304 if (dep_current[D_DIRREM] < max_softdeps_hard / 2 && 13305 dep_current[D_INODEDEP] < max_softdeps_hard && 13306 dep_current[D_INDIRDEP] < max_softdeps_hard / 1000 && 13307 dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0 && 13308 ump->softdep_curdeps[D_DIRREM] < 13309 (max_softdeps_hard / 2) / stat_flush_threads && 13310 ump->softdep_curdeps[D_INODEDEP] < 13311 max_softdeps_hard / stat_flush_threads && 13312 ump->softdep_curdeps[D_INDIRDEP] < 13313 (max_softdeps_hard / 1000) / stat_flush_threads && 13314 ump->softdep_curdeps[D_FREEBLKS] < 13315 max_softdeps_hard / stat_flush_threads) { 13316 FREE_LOCK(ump); 13317 return (0); 13318 } 13319 /* 13320 * If the journal is low or our filesystem is over its limit 13321 * then speedup the cleanup. 13322 */ 13323 if (ump->softdep_curdeps[D_INDIRDEP] < 13324 (max_softdeps_hard / 1000) / stat_flush_threads || jlow) 13325 softdep_speedup(ump); 13326 stat_sync_limit_hit += 1; 13327 FREE_LOCK(ump); 13328 /* 13329 * We only slow down the rate at which new dependencies are 13330 * generated if we are not using journaling. With journaling, 13331 * the cleanup should always be sufficient to keep things 13332 * under control. 13333 */ 13334 if (DOINGSUJ(vp)) 13335 return (0); 13336 return (1); 13337 } 13338 13339 /* 13340 * Called by the allocation routines when they are about to fail 13341 * in the hope that we can free up the requested resource (inodes 13342 * or disk space). 13343 * 13344 * First check to see if the work list has anything on it. If it has, 13345 * clean up entries until we successfully free the requested resource. 13346 * Because this process holds inodes locked, we cannot handle any remove 13347 * requests that might block on a locked inode as that could lead to 13348 * deadlock. If the worklist yields none of the requested resource, 13349 * start syncing out vnodes to free up the needed space. 13350 */ 13351 int 13352 softdep_request_cleanup(fs, vp, cred, resource) 13353 struct fs *fs; 13354 struct vnode *vp; 13355 struct ucred *cred; 13356 int resource; 13357 { 13358 struct ufsmount *ump; 13359 struct mount *mp; 13360 long starttime; 13361 size_t resid; 13362 ufs2_daddr_t needed; 13363 int error, failed_vnode; 13364 13365 /* 13366 * If we are being called because of a process doing a 13367 * copy-on-write, then it is not safe to process any 13368 * worklist items as we will recurse into the copyonwrite 13369 * routine. This will result in an incoherent snapshot. 13370 * If the vnode that we hold is a snapshot, we must avoid 13371 * handling other resources that could cause deadlock. 13372 */ 13373 if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp))) 13374 return (0); 13375 13376 if (resource == FLUSH_BLOCKS_WAIT) 13377 stat_cleanup_blkrequests += 1; 13378 else 13379 stat_cleanup_inorequests += 1; 13380 13381 mp = vp->v_mount; 13382 ump = VFSTOUFS(mp); 13383 mtx_assert(UFS_MTX(ump), MA_OWNED); 13384 UFS_UNLOCK(ump); 13385 error = ffs_update(vp, 1); 13386 if (error != 0 || MOUNTEDSOFTDEP(mp) == 0) { 13387 UFS_LOCK(ump); 13388 return (0); 13389 } 13390 /* 13391 * If we are in need of resources, start by cleaning up 13392 * any block removals associated with our inode. 13393 */ 13394 ACQUIRE_LOCK(ump); 13395 process_removes(vp); 13396 process_truncates(vp); 13397 FREE_LOCK(ump); 13398 /* 13399 * Now clean up at least as many resources as we will need. 13400 * 13401 * When requested to clean up inodes, the number that are needed 13402 * is set by the number of simultaneous writers (mnt_writeopcount) 13403 * plus a bit of slop (2) in case some more writers show up while 13404 * we are cleaning. 13405 * 13406 * When requested to free up space, the amount of space that 13407 * we need is enough blocks to allocate a full-sized segment 13408 * (fs_contigsumsize). The number of such segments that will 13409 * be needed is set by the number of simultaneous writers 13410 * (mnt_writeopcount) plus a bit of slop (2) in case some more 13411 * writers show up while we are cleaning. 13412 * 13413 * Additionally, if we are unpriviledged and allocating space, 13414 * we need to ensure that we clean up enough blocks to get the 13415 * needed number of blocks over the threshold of the minimum 13416 * number of blocks required to be kept free by the filesystem 13417 * (fs_minfree). 13418 */ 13419 if (resource == FLUSH_INODES_WAIT) { 13420 needed = vfs_mount_fetch_counter(vp->v_mount, 13421 MNT_COUNT_WRITEOPCOUNT) + 2; 13422 } else if (resource == FLUSH_BLOCKS_WAIT) { 13423 needed = (vfs_mount_fetch_counter(vp->v_mount, 13424 MNT_COUNT_WRITEOPCOUNT) + 2) * fs->fs_contigsumsize; 13425 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE)) 13426 needed += fragstoblks(fs, 13427 roundup((fs->fs_dsize * fs->fs_minfree / 100) - 13428 fs->fs_cstotal.cs_nffree, fs->fs_frag)); 13429 } else { 13430 printf("softdep_request_cleanup: Unknown resource type %d\n", 13431 resource); 13432 UFS_LOCK(ump); 13433 return (0); 13434 } 13435 starttime = time_second; 13436 retry: 13437 if (resource == FLUSH_BLOCKS_WAIT && 13438 fs->fs_cstotal.cs_nbfree <= needed) 13439 g_io_speedup(needed * fs->fs_bsize, BIO_SPEEDUP_TRIM, &resid, 13440 ump->um_cp); 13441 if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 && 13442 fs->fs_cstotal.cs_nbfree <= needed) || 13443 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13444 fs->fs_cstotal.cs_nifree <= needed)) { 13445 ACQUIRE_LOCK(ump); 13446 if (ump->softdep_on_worklist > 0 && 13447 process_worklist_item(UFSTOVFS(ump), 13448 ump->softdep_on_worklist, LK_NOWAIT) != 0) 13449 stat_worklist_push += 1; 13450 FREE_LOCK(ump); 13451 } 13452 /* 13453 * If we still need resources and there are no more worklist 13454 * entries to process to obtain them, we have to start flushing 13455 * the dirty vnodes to force the release of additional requests 13456 * to the worklist that we can then process to reap addition 13457 * resources. We walk the vnodes associated with the mount point 13458 * until we get the needed worklist requests that we can reap. 13459 * 13460 * If there are several threads all needing to clean the same 13461 * mount point, only one is allowed to walk the mount list. 13462 * When several threads all try to walk the same mount list, 13463 * they end up competing with each other and often end up in 13464 * livelock. This approach ensures that forward progress is 13465 * made at the cost of occational ENOSPC errors being returned 13466 * that might otherwise have been avoided. 13467 */ 13468 error = 1; 13469 if ((resource == FLUSH_BLOCKS_WAIT && 13470 fs->fs_cstotal.cs_nbfree <= needed) || 13471 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13472 fs->fs_cstotal.cs_nifree <= needed)) { 13473 ACQUIRE_LOCK(ump); 13474 if ((ump->um_softdep->sd_flags & FLUSH_RC_ACTIVE) == 0) { 13475 ump->um_softdep->sd_flags |= FLUSH_RC_ACTIVE; 13476 FREE_LOCK(ump); 13477 failed_vnode = softdep_request_cleanup_flush(mp, ump); 13478 ACQUIRE_LOCK(ump); 13479 ump->um_softdep->sd_flags &= ~FLUSH_RC_ACTIVE; 13480 FREE_LOCK(ump); 13481 if (ump->softdep_on_worklist > 0) { 13482 stat_cleanup_retries += 1; 13483 if (!failed_vnode) 13484 goto retry; 13485 } 13486 } else { 13487 FREE_LOCK(ump); 13488 error = 0; 13489 } 13490 stat_cleanup_failures += 1; 13491 } 13492 if (time_second - starttime > stat_cleanup_high_delay) 13493 stat_cleanup_high_delay = time_second - starttime; 13494 UFS_LOCK(ump); 13495 return (error); 13496 } 13497 13498 /* 13499 * Scan the vnodes for the specified mount point flushing out any 13500 * vnodes that can be locked without waiting. Finally, try to flush 13501 * the device associated with the mount point if it can be locked 13502 * without waiting. 13503 * 13504 * We return 0 if we were able to lock every vnode in our scan. 13505 * If we had to skip one or more vnodes, we return 1. 13506 */ 13507 static int 13508 softdep_request_cleanup_flush(mp, ump) 13509 struct mount *mp; 13510 struct ufsmount *ump; 13511 { 13512 struct thread *td; 13513 struct vnode *lvp, *mvp; 13514 int failed_vnode; 13515 13516 failed_vnode = 0; 13517 td = curthread; 13518 MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) { 13519 if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) { 13520 VI_UNLOCK(lvp); 13521 continue; 13522 } 13523 if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT, 13524 td) != 0) { 13525 failed_vnode = 1; 13526 continue; 13527 } 13528 if (lvp->v_vflag & VV_NOSYNC) { /* unlinked */ 13529 vput(lvp); 13530 continue; 13531 } 13532 (void) ffs_syncvnode(lvp, MNT_NOWAIT, 0); 13533 vput(lvp); 13534 } 13535 lvp = ump->um_devvp; 13536 if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) { 13537 VOP_FSYNC(lvp, MNT_NOWAIT, td); 13538 VOP_UNLOCK(lvp, 0); 13539 } 13540 return (failed_vnode); 13541 } 13542 13543 static bool 13544 softdep_excess_items(struct ufsmount *ump, int item) 13545 { 13546 13547 KASSERT(item >= 0 && item < D_LAST, ("item %d", item)); 13548 return (dep_current[item] > max_softdeps && 13549 ump->softdep_curdeps[item] > max_softdeps / 13550 stat_flush_threads); 13551 } 13552 13553 static void 13554 schedule_cleanup(struct mount *mp) 13555 { 13556 struct ufsmount *ump; 13557 struct thread *td; 13558 13559 ump = VFSTOUFS(mp); 13560 LOCK_OWNED(ump); 13561 FREE_LOCK(ump); 13562 td = curthread; 13563 if ((td->td_pflags & TDP_KTHREAD) != 0 && 13564 (td->td_proc->p_flag2 & P2_AST_SU) == 0) { 13565 /* 13566 * No ast is delivered to kernel threads, so nobody 13567 * would deref the mp. Some kernel threads 13568 * explicitely check for AST, e.g. NFS daemon does 13569 * this in the serving loop. 13570 */ 13571 return; 13572 } 13573 if (td->td_su != NULL) 13574 vfs_rel(td->td_su); 13575 vfs_ref(mp); 13576 td->td_su = mp; 13577 thread_lock(td); 13578 td->td_flags |= TDF_ASTPENDING; 13579 thread_unlock(td); 13580 } 13581 13582 static void 13583 softdep_ast_cleanup_proc(struct thread *td) 13584 { 13585 struct mount *mp; 13586 struct ufsmount *ump; 13587 int error; 13588 bool req; 13589 13590 while ((mp = td->td_su) != NULL) { 13591 td->td_su = NULL; 13592 error = vfs_busy(mp, MBF_NOWAIT); 13593 vfs_rel(mp); 13594 if (error != 0) 13595 return; 13596 if (ffs_own_mount(mp) && MOUNTEDSOFTDEP(mp)) { 13597 ump = VFSTOUFS(mp); 13598 for (;;) { 13599 req = false; 13600 ACQUIRE_LOCK(ump); 13601 if (softdep_excess_items(ump, D_INODEDEP)) { 13602 req = true; 13603 request_cleanup(mp, FLUSH_INODES); 13604 } 13605 if (softdep_excess_items(ump, D_DIRREM)) { 13606 req = true; 13607 request_cleanup(mp, FLUSH_BLOCKS); 13608 } 13609 FREE_LOCK(ump); 13610 if (softdep_excess_items(ump, D_NEWBLK) || 13611 softdep_excess_items(ump, D_ALLOCDIRECT) || 13612 softdep_excess_items(ump, D_ALLOCINDIR)) { 13613 error = vn_start_write(NULL, &mp, 13614 V_WAIT); 13615 if (error == 0) { 13616 req = true; 13617 VFS_SYNC(mp, MNT_WAIT); 13618 vn_finished_write(mp); 13619 } 13620 } 13621 if ((td->td_pflags & TDP_KTHREAD) != 0 || !req) 13622 break; 13623 } 13624 } 13625 vfs_unbusy(mp); 13626 } 13627 if ((mp = td->td_su) != NULL) { 13628 td->td_su = NULL; 13629 vfs_rel(mp); 13630 } 13631 } 13632 13633 /* 13634 * If memory utilization has gotten too high, deliberately slow things 13635 * down and speed up the I/O processing. 13636 */ 13637 static int 13638 request_cleanup(mp, resource) 13639 struct mount *mp; 13640 int resource; 13641 { 13642 struct thread *td = curthread; 13643 struct ufsmount *ump; 13644 13645 ump = VFSTOUFS(mp); 13646 LOCK_OWNED(ump); 13647 /* 13648 * We never hold up the filesystem syncer or buf daemon. 13649 */ 13650 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF)) 13651 return (0); 13652 /* 13653 * First check to see if the work list has gotten backlogged. 13654 * If it has, co-opt this process to help clean up two entries. 13655 * Because this process may hold inodes locked, we cannot 13656 * handle any remove requests that might block on a locked 13657 * inode as that could lead to deadlock. We set TDP_SOFTDEP 13658 * to avoid recursively processing the worklist. 13659 */ 13660 if (ump->softdep_on_worklist > max_softdeps / 10) { 13661 td->td_pflags |= TDP_SOFTDEP; 13662 process_worklist_item(mp, 2, LK_NOWAIT); 13663 td->td_pflags &= ~TDP_SOFTDEP; 13664 stat_worklist_push += 2; 13665 return(1); 13666 } 13667 /* 13668 * Next, we attempt to speed up the syncer process. If that 13669 * is successful, then we allow the process to continue. 13670 */ 13671 if (softdep_speedup(ump) && 13672 resource != FLUSH_BLOCKS_WAIT && 13673 resource != FLUSH_INODES_WAIT) 13674 return(0); 13675 /* 13676 * If we are resource constrained on inode dependencies, try 13677 * flushing some dirty inodes. Otherwise, we are constrained 13678 * by file deletions, so try accelerating flushes of directories 13679 * with removal dependencies. We would like to do the cleanup 13680 * here, but we probably hold an inode locked at this point and 13681 * that might deadlock against one that we try to clean. So, 13682 * the best that we can do is request the syncer daemon to do 13683 * the cleanup for us. 13684 */ 13685 switch (resource) { 13686 13687 case FLUSH_INODES: 13688 case FLUSH_INODES_WAIT: 13689 ACQUIRE_GBLLOCK(&lk); 13690 stat_ino_limit_push += 1; 13691 req_clear_inodedeps += 1; 13692 FREE_GBLLOCK(&lk); 13693 stat_countp = &stat_ino_limit_hit; 13694 break; 13695 13696 case FLUSH_BLOCKS: 13697 case FLUSH_BLOCKS_WAIT: 13698 ACQUIRE_GBLLOCK(&lk); 13699 stat_blk_limit_push += 1; 13700 req_clear_remove += 1; 13701 FREE_GBLLOCK(&lk); 13702 stat_countp = &stat_blk_limit_hit; 13703 break; 13704 13705 default: 13706 panic("request_cleanup: unknown type"); 13707 } 13708 /* 13709 * Hopefully the syncer daemon will catch up and awaken us. 13710 * We wait at most tickdelay before proceeding in any case. 13711 */ 13712 ACQUIRE_GBLLOCK(&lk); 13713 FREE_LOCK(ump); 13714 proc_waiting += 1; 13715 if (callout_pending(&softdep_callout) == FALSE) 13716 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2, 13717 pause_timer, 0); 13718 13719 if ((td->td_pflags & TDP_KTHREAD) == 0) 13720 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0); 13721 proc_waiting -= 1; 13722 FREE_GBLLOCK(&lk); 13723 ACQUIRE_LOCK(ump); 13724 return (1); 13725 } 13726 13727 /* 13728 * Awaken processes pausing in request_cleanup and clear proc_waiting 13729 * to indicate that there is no longer a timer running. Pause_timer 13730 * will be called with the global softdep mutex (&lk) locked. 13731 */ 13732 static void 13733 pause_timer(arg) 13734 void *arg; 13735 { 13736 13737 GBLLOCK_OWNED(&lk); 13738 /* 13739 * The callout_ API has acquired mtx and will hold it around this 13740 * function call. 13741 */ 13742 *stat_countp += proc_waiting; 13743 wakeup(&proc_waiting); 13744 } 13745 13746 /* 13747 * If requested, try removing inode or removal dependencies. 13748 */ 13749 static void 13750 check_clear_deps(mp) 13751 struct mount *mp; 13752 { 13753 struct ufsmount *ump; 13754 size_t resid; 13755 13756 /* 13757 * Tell the lower layers that any TRIM or WRITE transactions 13758 * that have been delayed for performance reasons should 13759 * proceed to help alleviate the shortage faster. 13760 */ 13761 ump = VFSTOUFS(mp); 13762 FREE_LOCK(ump); 13763 g_io_speedup(0, BIO_SPEEDUP_TRIM | BIO_SPEEDUP_WRITE, &resid, ump->um_cp); 13764 ACQUIRE_LOCK(ump); 13765 13766 13767 /* 13768 * If we are suspended, it may be because of our using 13769 * too many inodedeps, so help clear them out. 13770 */ 13771 if (MOUNTEDSUJ(mp) && VFSTOUFS(mp)->softdep_jblocks->jb_suspended) 13772 clear_inodedeps(mp); 13773 13774 /* 13775 * General requests for cleanup of backed up dependencies 13776 */ 13777 ACQUIRE_GBLLOCK(&lk); 13778 if (req_clear_inodedeps) { 13779 req_clear_inodedeps -= 1; 13780 FREE_GBLLOCK(&lk); 13781 clear_inodedeps(mp); 13782 ACQUIRE_GBLLOCK(&lk); 13783 wakeup(&proc_waiting); 13784 } 13785 if (req_clear_remove) { 13786 req_clear_remove -= 1; 13787 FREE_GBLLOCK(&lk); 13788 clear_remove(mp); 13789 ACQUIRE_GBLLOCK(&lk); 13790 wakeup(&proc_waiting); 13791 } 13792 FREE_GBLLOCK(&lk); 13793 } 13794 13795 /* 13796 * Flush out a directory with at least one removal dependency in an effort to 13797 * reduce the number of dirrem, freefile, and freeblks dependency structures. 13798 */ 13799 static void 13800 clear_remove(mp) 13801 struct mount *mp; 13802 { 13803 struct pagedep_hashhead *pagedephd; 13804 struct pagedep *pagedep; 13805 struct ufsmount *ump; 13806 struct vnode *vp; 13807 struct bufobj *bo; 13808 int error, cnt; 13809 ino_t ino; 13810 13811 ump = VFSTOUFS(mp); 13812 LOCK_OWNED(ump); 13813 13814 for (cnt = 0; cnt <= ump->pagedep_hash_size; cnt++) { 13815 pagedephd = &ump->pagedep_hashtbl[ump->pagedep_nextclean++]; 13816 if (ump->pagedep_nextclean > ump->pagedep_hash_size) 13817 ump->pagedep_nextclean = 0; 13818 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 13819 if (LIST_EMPTY(&pagedep->pd_dirremhd)) 13820 continue; 13821 ino = pagedep->pd_ino; 13822 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 13823 continue; 13824 FREE_LOCK(ump); 13825 13826 /* 13827 * Let unmount clear deps 13828 */ 13829 error = vfs_busy(mp, MBF_NOWAIT); 13830 if (error != 0) 13831 goto finish_write; 13832 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 13833 FFSV_FORCEINSMQ); 13834 vfs_unbusy(mp); 13835 if (error != 0) { 13836 softdep_error("clear_remove: vget", error); 13837 goto finish_write; 13838 } 13839 MPASS(VTOI(vp)->i_mode != 0); 13840 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 13841 softdep_error("clear_remove: fsync", error); 13842 bo = &vp->v_bufobj; 13843 BO_LOCK(bo); 13844 drain_output(vp); 13845 BO_UNLOCK(bo); 13846 vput(vp); 13847 finish_write: 13848 vn_finished_write(mp); 13849 ACQUIRE_LOCK(ump); 13850 return; 13851 } 13852 } 13853 } 13854 13855 /* 13856 * Clear out a block of dirty inodes in an effort to reduce 13857 * the number of inodedep dependency structures. 13858 */ 13859 static void 13860 clear_inodedeps(mp) 13861 struct mount *mp; 13862 { 13863 struct inodedep_hashhead *inodedephd; 13864 struct inodedep *inodedep; 13865 struct ufsmount *ump; 13866 struct vnode *vp; 13867 struct fs *fs; 13868 int error, cnt; 13869 ino_t firstino, lastino, ino; 13870 13871 ump = VFSTOUFS(mp); 13872 fs = ump->um_fs; 13873 LOCK_OWNED(ump); 13874 /* 13875 * Pick a random inode dependency to be cleared. 13876 * We will then gather up all the inodes in its block 13877 * that have dependencies and flush them out. 13878 */ 13879 for (cnt = 0; cnt <= ump->inodedep_hash_size; cnt++) { 13880 inodedephd = &ump->inodedep_hashtbl[ump->inodedep_nextclean++]; 13881 if (ump->inodedep_nextclean > ump->inodedep_hash_size) 13882 ump->inodedep_nextclean = 0; 13883 if ((inodedep = LIST_FIRST(inodedephd)) != NULL) 13884 break; 13885 } 13886 if (inodedep == NULL) 13887 return; 13888 /* 13889 * Find the last inode in the block with dependencies. 13890 */ 13891 firstino = rounddown2(inodedep->id_ino, INOPB(fs)); 13892 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--) 13893 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0) 13894 break; 13895 /* 13896 * Asynchronously push all but the last inode with dependencies. 13897 * Synchronously push the last inode with dependencies to ensure 13898 * that the inode block gets written to free up the inodedeps. 13899 */ 13900 for (ino = firstino; ino <= lastino; ino++) { 13901 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 13902 continue; 13903 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 13904 continue; 13905 FREE_LOCK(ump); 13906 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */ 13907 if (error != 0) { 13908 vn_finished_write(mp); 13909 ACQUIRE_LOCK(ump); 13910 return; 13911 } 13912 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 13913 FFSV_FORCEINSMQ)) != 0) { 13914 softdep_error("clear_inodedeps: vget", error); 13915 vfs_unbusy(mp); 13916 vn_finished_write(mp); 13917 ACQUIRE_LOCK(ump); 13918 return; 13919 } 13920 vfs_unbusy(mp); 13921 if (VTOI(vp)->i_mode == 0) { 13922 vgone(vp); 13923 } else if (ino == lastino) { 13924 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0))) 13925 softdep_error("clear_inodedeps: fsync1", error); 13926 } else { 13927 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 13928 softdep_error("clear_inodedeps: fsync2", error); 13929 BO_LOCK(&vp->v_bufobj); 13930 drain_output(vp); 13931 BO_UNLOCK(&vp->v_bufobj); 13932 } 13933 vput(vp); 13934 vn_finished_write(mp); 13935 ACQUIRE_LOCK(ump); 13936 } 13937 } 13938 13939 void 13940 softdep_buf_append(bp, wkhd) 13941 struct buf *bp; 13942 struct workhead *wkhd; 13943 { 13944 struct worklist *wk; 13945 struct ufsmount *ump; 13946 13947 if ((wk = LIST_FIRST(wkhd)) == NULL) 13948 return; 13949 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 13950 ("softdep_buf_append called on non-softdep filesystem")); 13951 ump = VFSTOUFS(wk->wk_mp); 13952 ACQUIRE_LOCK(ump); 13953 while ((wk = LIST_FIRST(wkhd)) != NULL) { 13954 WORKLIST_REMOVE(wk); 13955 WORKLIST_INSERT(&bp->b_dep, wk); 13956 } 13957 FREE_LOCK(ump); 13958 13959 } 13960 13961 void 13962 softdep_inode_append(ip, cred, wkhd) 13963 struct inode *ip; 13964 struct ucred *cred; 13965 struct workhead *wkhd; 13966 { 13967 struct buf *bp; 13968 struct fs *fs; 13969 struct ufsmount *ump; 13970 int error; 13971 13972 ump = ITOUMP(ip); 13973 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 13974 ("softdep_inode_append called on non-softdep filesystem")); 13975 fs = ump->um_fs; 13976 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 13977 (int)fs->fs_bsize, cred, &bp); 13978 if (error) { 13979 bqrelse(bp); 13980 softdep_freework(wkhd); 13981 return; 13982 } 13983 softdep_buf_append(bp, wkhd); 13984 bqrelse(bp); 13985 } 13986 13987 void 13988 softdep_freework(wkhd) 13989 struct workhead *wkhd; 13990 { 13991 struct worklist *wk; 13992 struct ufsmount *ump; 13993 13994 if ((wk = LIST_FIRST(wkhd)) == NULL) 13995 return; 13996 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 13997 ("softdep_freework called on non-softdep filesystem")); 13998 ump = VFSTOUFS(wk->wk_mp); 13999 ACQUIRE_LOCK(ump); 14000 handle_jwork(wkhd); 14001 FREE_LOCK(ump); 14002 } 14003 14004 static struct ufsmount * 14005 softdep_bp_to_mp(bp) 14006 struct buf *bp; 14007 { 14008 struct mount *mp; 14009 struct vnode *vp; 14010 14011 if (LIST_EMPTY(&bp->b_dep)) 14012 return (NULL); 14013 vp = bp->b_vp; 14014 KASSERT(vp != NULL, 14015 ("%s, buffer with dependencies lacks vnode", __func__)); 14016 14017 /* 14018 * The ump mount point is stable after we get a correct 14019 * pointer, since bp is locked and this prevents unmount from 14020 * proceeding. But to get to it, we cannot dereference bp->b_dep 14021 * head wk_mp, because we do not yet own SU ump lock and 14022 * workitem might be freed while dereferenced. 14023 */ 14024 retry: 14025 switch (vp->v_type) { 14026 case VCHR: 14027 VI_LOCK(vp); 14028 mp = vp->v_type == VCHR ? vp->v_rdev->si_mountpt : NULL; 14029 VI_UNLOCK(vp); 14030 if (mp == NULL) 14031 goto retry; 14032 break; 14033 case VREG: 14034 case VDIR: 14035 case VLNK: 14036 case VFIFO: 14037 case VSOCK: 14038 mp = vp->v_mount; 14039 break; 14040 case VBLK: 14041 vn_printf(vp, "softdep_bp_to_mp: unexpected block device\n"); 14042 /* FALLTHROUGH */ 14043 case VNON: 14044 case VBAD: 14045 case VMARKER: 14046 mp = NULL; 14047 break; 14048 default: 14049 vn_printf(vp, "unknown vnode type"); 14050 mp = NULL; 14051 break; 14052 } 14053 return (VFSTOUFS(mp)); 14054 } 14055 14056 /* 14057 * Function to determine if the buffer has outstanding dependencies 14058 * that will cause a roll-back if the buffer is written. If wantcount 14059 * is set, return number of dependencies, otherwise just yes or no. 14060 */ 14061 static int 14062 softdep_count_dependencies(bp, wantcount) 14063 struct buf *bp; 14064 int wantcount; 14065 { 14066 struct worklist *wk; 14067 struct ufsmount *ump; 14068 struct bmsafemap *bmsafemap; 14069 struct freework *freework; 14070 struct inodedep *inodedep; 14071 struct indirdep *indirdep; 14072 struct freeblks *freeblks; 14073 struct allocindir *aip; 14074 struct pagedep *pagedep; 14075 struct dirrem *dirrem; 14076 struct newblk *newblk; 14077 struct mkdir *mkdir; 14078 struct diradd *dap; 14079 int i, retval; 14080 14081 ump = softdep_bp_to_mp(bp); 14082 if (ump == NULL) 14083 return (0); 14084 retval = 0; 14085 ACQUIRE_LOCK(ump); 14086 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 14087 switch (wk->wk_type) { 14088 14089 case D_INODEDEP: 14090 inodedep = WK_INODEDEP(wk); 14091 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 14092 /* bitmap allocation dependency */ 14093 retval += 1; 14094 if (!wantcount) 14095 goto out; 14096 } 14097 if (TAILQ_FIRST(&inodedep->id_inoupdt)) { 14098 /* direct block pointer dependency */ 14099 retval += 1; 14100 if (!wantcount) 14101 goto out; 14102 } 14103 if (TAILQ_FIRST(&inodedep->id_extupdt)) { 14104 /* direct block pointer dependency */ 14105 retval += 1; 14106 if (!wantcount) 14107 goto out; 14108 } 14109 if (TAILQ_FIRST(&inodedep->id_inoreflst)) { 14110 /* Add reference dependency. */ 14111 retval += 1; 14112 if (!wantcount) 14113 goto out; 14114 } 14115 continue; 14116 14117 case D_INDIRDEP: 14118 indirdep = WK_INDIRDEP(wk); 14119 14120 TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) { 14121 /* indirect truncation dependency */ 14122 retval += 1; 14123 if (!wantcount) 14124 goto out; 14125 } 14126 14127 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 14128 /* indirect block pointer dependency */ 14129 retval += 1; 14130 if (!wantcount) 14131 goto out; 14132 } 14133 continue; 14134 14135 case D_PAGEDEP: 14136 pagedep = WK_PAGEDEP(wk); 14137 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) { 14138 if (LIST_FIRST(&dirrem->dm_jremrefhd)) { 14139 /* Journal remove ref dependency. */ 14140 retval += 1; 14141 if (!wantcount) 14142 goto out; 14143 } 14144 } 14145 for (i = 0; i < DAHASHSZ; i++) { 14146 14147 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 14148 /* directory entry dependency */ 14149 retval += 1; 14150 if (!wantcount) 14151 goto out; 14152 } 14153 } 14154 continue; 14155 14156 case D_BMSAFEMAP: 14157 bmsafemap = WK_BMSAFEMAP(wk); 14158 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) { 14159 /* Add reference dependency. */ 14160 retval += 1; 14161 if (!wantcount) 14162 goto out; 14163 } 14164 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) { 14165 /* Allocate block dependency. */ 14166 retval += 1; 14167 if (!wantcount) 14168 goto out; 14169 } 14170 continue; 14171 14172 case D_FREEBLKS: 14173 freeblks = WK_FREEBLKS(wk); 14174 if (LIST_FIRST(&freeblks->fb_jblkdephd)) { 14175 /* Freeblk journal dependency. */ 14176 retval += 1; 14177 if (!wantcount) 14178 goto out; 14179 } 14180 continue; 14181 14182 case D_ALLOCDIRECT: 14183 case D_ALLOCINDIR: 14184 newblk = WK_NEWBLK(wk); 14185 if (newblk->nb_jnewblk) { 14186 /* Journal allocate dependency. */ 14187 retval += 1; 14188 if (!wantcount) 14189 goto out; 14190 } 14191 continue; 14192 14193 case D_MKDIR: 14194 mkdir = WK_MKDIR(wk); 14195 if (mkdir->md_jaddref) { 14196 /* Journal reference dependency. */ 14197 retval += 1; 14198 if (!wantcount) 14199 goto out; 14200 } 14201 continue; 14202 14203 case D_FREEWORK: 14204 case D_FREEDEP: 14205 case D_JSEGDEP: 14206 case D_JSEG: 14207 case D_SBDEP: 14208 /* never a dependency on these blocks */ 14209 continue; 14210 14211 default: 14212 panic("softdep_count_dependencies: Unexpected type %s", 14213 TYPENAME(wk->wk_type)); 14214 /* NOTREACHED */ 14215 } 14216 } 14217 out: 14218 FREE_LOCK(ump); 14219 return (retval); 14220 } 14221 14222 /* 14223 * Acquire exclusive access to a buffer. 14224 * Must be called with a locked mtx parameter. 14225 * Return acquired buffer or NULL on failure. 14226 */ 14227 static struct buf * 14228 getdirtybuf(bp, lock, waitfor) 14229 struct buf *bp; 14230 struct rwlock *lock; 14231 int waitfor; 14232 { 14233 int error; 14234 14235 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) { 14236 if (waitfor != MNT_WAIT) 14237 return (NULL); 14238 error = BUF_LOCK(bp, 14239 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock); 14240 /* 14241 * Even if we successfully acquire bp here, we have dropped 14242 * lock, which may violates our guarantee. 14243 */ 14244 if (error == 0) 14245 BUF_UNLOCK(bp); 14246 else if (error != ENOLCK) 14247 panic("getdirtybuf: inconsistent lock: %d", error); 14248 rw_wlock(lock); 14249 return (NULL); 14250 } 14251 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 14252 if (lock != BO_LOCKPTR(bp->b_bufobj) && waitfor == MNT_WAIT) { 14253 rw_wunlock(lock); 14254 BO_LOCK(bp->b_bufobj); 14255 BUF_UNLOCK(bp); 14256 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 14257 bp->b_vflags |= BV_BKGRDWAIT; 14258 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), 14259 PRIBIO | PDROP, "getbuf", 0); 14260 } else 14261 BO_UNLOCK(bp->b_bufobj); 14262 rw_wlock(lock); 14263 return (NULL); 14264 } 14265 BUF_UNLOCK(bp); 14266 if (waitfor != MNT_WAIT) 14267 return (NULL); 14268 #ifdef DEBUG_VFS_LOCKS 14269 if (bp->b_vp->v_type != VCHR) 14270 ASSERT_BO_WLOCKED(bp->b_bufobj); 14271 #endif 14272 bp->b_vflags |= BV_BKGRDWAIT; 14273 rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0); 14274 return (NULL); 14275 } 14276 if ((bp->b_flags & B_DELWRI) == 0) { 14277 BUF_UNLOCK(bp); 14278 return (NULL); 14279 } 14280 bremfree(bp); 14281 return (bp); 14282 } 14283 14284 14285 /* 14286 * Check if it is safe to suspend the file system now. On entry, 14287 * the vnode interlock for devvp should be held. Return 0 with 14288 * the mount interlock held if the file system can be suspended now, 14289 * otherwise return EAGAIN with the mount interlock held. 14290 */ 14291 int 14292 softdep_check_suspend(struct mount *mp, 14293 struct vnode *devvp, 14294 int softdep_depcnt, 14295 int softdep_accdepcnt, 14296 int secondary_writes, 14297 int secondary_accwrites) 14298 { 14299 struct bufobj *bo; 14300 struct ufsmount *ump; 14301 struct inodedep *inodedep; 14302 int error, unlinked; 14303 14304 bo = &devvp->v_bufobj; 14305 ASSERT_BO_WLOCKED(bo); 14306 14307 /* 14308 * If we are not running with soft updates, then we need only 14309 * deal with secondary writes as we try to suspend. 14310 */ 14311 if (MOUNTEDSOFTDEP(mp) == 0) { 14312 MNT_ILOCK(mp); 14313 while (mp->mnt_secondary_writes != 0) { 14314 BO_UNLOCK(bo); 14315 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 14316 (PUSER - 1) | PDROP, "secwr", 0); 14317 BO_LOCK(bo); 14318 MNT_ILOCK(mp); 14319 } 14320 14321 /* 14322 * Reasons for needing more work before suspend: 14323 * - Dirty buffers on devvp. 14324 * - Secondary writes occurred after start of vnode sync loop 14325 */ 14326 error = 0; 14327 if (bo->bo_numoutput > 0 || 14328 bo->bo_dirty.bv_cnt > 0 || 14329 secondary_writes != 0 || 14330 mp->mnt_secondary_writes != 0 || 14331 secondary_accwrites != mp->mnt_secondary_accwrites) 14332 error = EAGAIN; 14333 BO_UNLOCK(bo); 14334 return (error); 14335 } 14336 14337 /* 14338 * If we are running with soft updates, then we need to coordinate 14339 * with them as we try to suspend. 14340 */ 14341 ump = VFSTOUFS(mp); 14342 for (;;) { 14343 if (!TRY_ACQUIRE_LOCK(ump)) { 14344 BO_UNLOCK(bo); 14345 ACQUIRE_LOCK(ump); 14346 FREE_LOCK(ump); 14347 BO_LOCK(bo); 14348 continue; 14349 } 14350 MNT_ILOCK(mp); 14351 if (mp->mnt_secondary_writes != 0) { 14352 FREE_LOCK(ump); 14353 BO_UNLOCK(bo); 14354 msleep(&mp->mnt_secondary_writes, 14355 MNT_MTX(mp), 14356 (PUSER - 1) | PDROP, "secwr", 0); 14357 BO_LOCK(bo); 14358 continue; 14359 } 14360 break; 14361 } 14362 14363 unlinked = 0; 14364 if (MOUNTEDSUJ(mp)) { 14365 for (inodedep = TAILQ_FIRST(&ump->softdep_unlinked); 14366 inodedep != NULL; 14367 inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 14368 if ((inodedep->id_state & (UNLINKED | UNLINKLINKS | 14369 UNLINKONLIST)) != (UNLINKED | UNLINKLINKS | 14370 UNLINKONLIST) || 14371 !check_inodedep_free(inodedep)) 14372 continue; 14373 unlinked++; 14374 } 14375 } 14376 14377 /* 14378 * Reasons for needing more work before suspend: 14379 * - Dirty buffers on devvp. 14380 * - Softdep activity occurred after start of vnode sync loop 14381 * - Secondary writes occurred after start of vnode sync loop 14382 */ 14383 error = 0; 14384 if (bo->bo_numoutput > 0 || 14385 bo->bo_dirty.bv_cnt > 0 || 14386 softdep_depcnt != unlinked || 14387 ump->softdep_deps != unlinked || 14388 softdep_accdepcnt != ump->softdep_accdeps || 14389 secondary_writes != 0 || 14390 mp->mnt_secondary_writes != 0 || 14391 secondary_accwrites != mp->mnt_secondary_accwrites) 14392 error = EAGAIN; 14393 FREE_LOCK(ump); 14394 BO_UNLOCK(bo); 14395 return (error); 14396 } 14397 14398 14399 /* 14400 * Get the number of dependency structures for the file system, both 14401 * the current number and the total number allocated. These will 14402 * later be used to detect that softdep processing has occurred. 14403 */ 14404 void 14405 softdep_get_depcounts(struct mount *mp, 14406 int *softdep_depsp, 14407 int *softdep_accdepsp) 14408 { 14409 struct ufsmount *ump; 14410 14411 if (MOUNTEDSOFTDEP(mp) == 0) { 14412 *softdep_depsp = 0; 14413 *softdep_accdepsp = 0; 14414 return; 14415 } 14416 ump = VFSTOUFS(mp); 14417 ACQUIRE_LOCK(ump); 14418 *softdep_depsp = ump->softdep_deps; 14419 *softdep_accdepsp = ump->softdep_accdeps; 14420 FREE_LOCK(ump); 14421 } 14422 14423 /* 14424 * Wait for pending output on a vnode to complete. 14425 */ 14426 static void 14427 drain_output(vp) 14428 struct vnode *vp; 14429 { 14430 14431 ASSERT_VOP_LOCKED(vp, "drain_output"); 14432 (void)bufobj_wwait(&vp->v_bufobj, 0, 0); 14433 } 14434 14435 /* 14436 * Called whenever a buffer that is being invalidated or reallocated 14437 * contains dependencies. This should only happen if an I/O error has 14438 * occurred. The routine is called with the buffer locked. 14439 */ 14440 static void 14441 softdep_deallocate_dependencies(bp) 14442 struct buf *bp; 14443 { 14444 14445 if ((bp->b_ioflags & BIO_ERROR) == 0) 14446 panic("softdep_deallocate_dependencies: dangling deps"); 14447 if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL) 14448 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error); 14449 else 14450 printf("softdep_deallocate_dependencies: " 14451 "got error %d while accessing filesystem\n", bp->b_error); 14452 if (bp->b_error != ENXIO) 14453 panic("softdep_deallocate_dependencies: unrecovered I/O error"); 14454 } 14455 14456 /* 14457 * Function to handle asynchronous write errors in the filesystem. 14458 */ 14459 static void 14460 softdep_error(func, error) 14461 char *func; 14462 int error; 14463 { 14464 14465 /* XXX should do something better! */ 14466 printf("%s: got error %d while accessing filesystem\n", func, error); 14467 } 14468 14469 #ifdef DDB 14470 14471 /* exported to ffs_vfsops.c */ 14472 extern void db_print_ffs(struct ufsmount *ump); 14473 void 14474 db_print_ffs(struct ufsmount *ump) 14475 { 14476 db_printf("mp %p (%s) devvp %p\n", ump->um_mountp, 14477 ump->um_mountp->mnt_stat.f_mntonname, ump->um_devvp); 14478 db_printf(" fs %p su_wl %d su_deps %d su_req %d\n", 14479 ump->um_fs, ump->softdep_on_worklist, 14480 ump->softdep_deps, ump->softdep_req); 14481 } 14482 14483 static void 14484 worklist_print(struct worklist *wk, int verbose) 14485 { 14486 14487 if (!verbose) { 14488 db_printf("%s: %p state 0x%b\n", TYPENAME(wk->wk_type), wk, 14489 (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS); 14490 return; 14491 } 14492 db_printf("worklist: %p type %s state 0x%b next %p\n ", wk, 14493 TYPENAME(wk->wk_type), (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS, 14494 LIST_NEXT(wk, wk_list)); 14495 db_print_ffs(VFSTOUFS(wk->wk_mp)); 14496 } 14497 14498 static void 14499 inodedep_print(struct inodedep *inodedep, int verbose) 14500 { 14501 14502 worklist_print(&inodedep->id_list, 0); 14503 db_printf(" fs %p ino %jd inoblk %jd delta %jd nlink %jd\n", 14504 inodedep->id_fs, 14505 (intmax_t)inodedep->id_ino, 14506 (intmax_t)fsbtodb(inodedep->id_fs, 14507 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)), 14508 (intmax_t)inodedep->id_nlinkdelta, 14509 (intmax_t)inodedep->id_savednlink); 14510 14511 if (verbose == 0) 14512 return; 14513 14514 db_printf(" bmsafemap %p, mkdiradd %p, inoreflst %p\n", 14515 inodedep->id_bmsafemap, 14516 inodedep->id_mkdiradd, 14517 TAILQ_FIRST(&inodedep->id_inoreflst)); 14518 db_printf(" dirremhd %p, pendinghd %p, bufwait %p\n", 14519 LIST_FIRST(&inodedep->id_dirremhd), 14520 LIST_FIRST(&inodedep->id_pendinghd), 14521 LIST_FIRST(&inodedep->id_bufwait)); 14522 db_printf(" inowait %p, inoupdt %p, newinoupdt %p\n", 14523 LIST_FIRST(&inodedep->id_inowait), 14524 TAILQ_FIRST(&inodedep->id_inoupdt), 14525 TAILQ_FIRST(&inodedep->id_newinoupdt)); 14526 db_printf(" extupdt %p, newextupdt %p, freeblklst %p\n", 14527 TAILQ_FIRST(&inodedep->id_extupdt), 14528 TAILQ_FIRST(&inodedep->id_newextupdt), 14529 TAILQ_FIRST(&inodedep->id_freeblklst)); 14530 db_printf(" saveino %p, savedsize %jd, savedextsize %jd\n", 14531 inodedep->id_savedino1, 14532 (intmax_t)inodedep->id_savedsize, 14533 (intmax_t)inodedep->id_savedextsize); 14534 } 14535 14536 static void 14537 newblk_print(struct newblk *nbp) 14538 { 14539 14540 worklist_print(&nbp->nb_list, 0); 14541 db_printf(" newblkno %jd\n", (intmax_t)nbp->nb_newblkno); 14542 db_printf(" jnewblk %p, bmsafemap %p, freefrag %p\n", 14543 &nbp->nb_jnewblk, 14544 &nbp->nb_bmsafemap, 14545 &nbp->nb_freefrag); 14546 db_printf(" indirdeps %p, newdirblk %p, jwork %p\n", 14547 LIST_FIRST(&nbp->nb_indirdeps), 14548 LIST_FIRST(&nbp->nb_newdirblk), 14549 LIST_FIRST(&nbp->nb_jwork)); 14550 } 14551 14552 static void 14553 allocdirect_print(struct allocdirect *adp) 14554 { 14555 14556 newblk_print(&adp->ad_block); 14557 db_printf(" oldblkno %jd, oldsize %ld, newsize %ld\n", 14558 adp->ad_oldblkno, adp->ad_oldsize, adp->ad_newsize); 14559 db_printf(" offset %d, inodedep %p\n", 14560 adp->ad_offset, adp->ad_inodedep); 14561 } 14562 14563 static void 14564 allocindir_print(struct allocindir *aip) 14565 { 14566 14567 newblk_print(&aip->ai_block); 14568 db_printf(" oldblkno %jd, lbn %jd\n", 14569 (intmax_t)aip->ai_oldblkno, (intmax_t)aip->ai_lbn); 14570 db_printf(" offset %d, indirdep %p\n", 14571 aip->ai_offset, aip->ai_indirdep); 14572 } 14573 14574 static void 14575 mkdir_print(struct mkdir *mkdir) 14576 { 14577 14578 worklist_print(&mkdir->md_list, 0); 14579 db_printf(" diradd %p, jaddref %p, buf %p\n", 14580 mkdir->md_diradd, mkdir->md_jaddref, mkdir->md_buf); 14581 } 14582 14583 DB_SHOW_COMMAND(sd_inodedep, db_show_sd_inodedep) 14584 { 14585 14586 if (have_addr == 0) { 14587 db_printf("inodedep address required\n"); 14588 return; 14589 } 14590 inodedep_print((struct inodedep*)addr, 1); 14591 } 14592 14593 DB_SHOW_COMMAND(sd_allinodedeps, db_show_sd_allinodedeps) 14594 { 14595 struct inodedep_hashhead *inodedephd; 14596 struct inodedep *inodedep; 14597 struct ufsmount *ump; 14598 int cnt; 14599 14600 if (have_addr == 0) { 14601 db_printf("ufsmount address required\n"); 14602 return; 14603 } 14604 ump = (struct ufsmount *)addr; 14605 for (cnt = 0; cnt < ump->inodedep_hash_size; cnt++) { 14606 inodedephd = &ump->inodedep_hashtbl[cnt]; 14607 LIST_FOREACH(inodedep, inodedephd, id_hash) { 14608 inodedep_print(inodedep, 0); 14609 } 14610 } 14611 } 14612 14613 DB_SHOW_COMMAND(sd_worklist, db_show_sd_worklist) 14614 { 14615 14616 if (have_addr == 0) { 14617 db_printf("worklist address required\n"); 14618 return; 14619 } 14620 worklist_print((struct worklist *)addr, 1); 14621 } 14622 14623 DB_SHOW_COMMAND(sd_workhead, db_show_sd_workhead) 14624 { 14625 struct worklist *wk; 14626 struct workhead *wkhd; 14627 14628 if (have_addr == 0) { 14629 db_printf("worklist address required " 14630 "(for example value in bp->b_dep)\n"); 14631 return; 14632 } 14633 /* 14634 * We often do not have the address of the worklist head but 14635 * instead a pointer to its first entry (e.g., we have the 14636 * contents of bp->b_dep rather than &bp->b_dep). But the back 14637 * pointer of bp->b_dep will point at the head of the list, so 14638 * we cheat and use that instead. If we are in the middle of 14639 * a list we will still get the same result, so nothing 14640 * unexpected will result. 14641 */ 14642 wk = (struct worklist *)addr; 14643 if (wk == NULL) 14644 return; 14645 wkhd = (struct workhead *)wk->wk_list.le_prev; 14646 LIST_FOREACH(wk, wkhd, wk_list) { 14647 switch(wk->wk_type) { 14648 case D_INODEDEP: 14649 inodedep_print(WK_INODEDEP(wk), 0); 14650 continue; 14651 case D_ALLOCDIRECT: 14652 allocdirect_print(WK_ALLOCDIRECT(wk)); 14653 continue; 14654 case D_ALLOCINDIR: 14655 allocindir_print(WK_ALLOCINDIR(wk)); 14656 continue; 14657 case D_MKDIR: 14658 mkdir_print(WK_MKDIR(wk)); 14659 continue; 14660 default: 14661 worklist_print(wk, 0); 14662 continue; 14663 } 14664 } 14665 } 14666 14667 DB_SHOW_COMMAND(sd_mkdir, db_show_sd_mkdir) 14668 { 14669 if (have_addr == 0) { 14670 db_printf("mkdir address required\n"); 14671 return; 14672 } 14673 mkdir_print((struct mkdir *)addr); 14674 } 14675 14676 DB_SHOW_COMMAND(sd_mkdir_list, db_show_sd_mkdir_list) 14677 { 14678 struct mkdirlist *mkdirlisthd; 14679 struct mkdir *mkdir; 14680 14681 if (have_addr == 0) { 14682 db_printf("mkdir listhead address required\n"); 14683 return; 14684 } 14685 mkdirlisthd = (struct mkdirlist *)addr; 14686 LIST_FOREACH(mkdir, mkdirlisthd, md_mkdirs) { 14687 mkdir_print(mkdir); 14688 if (mkdir->md_diradd != NULL) { 14689 db_printf(" "); 14690 worklist_print(&mkdir->md_diradd->da_list, 0); 14691 } 14692 if (mkdir->md_jaddref != NULL) { 14693 db_printf(" "); 14694 worklist_print(&mkdir->md_jaddref->ja_list, 0); 14695 } 14696 } 14697 } 14698 14699 DB_SHOW_COMMAND(sd_allocdirect, db_show_sd_allocdirect) 14700 { 14701 if (have_addr == 0) { 14702 db_printf("allocdirect address required\n"); 14703 return; 14704 } 14705 allocdirect_print((struct allocdirect *)addr); 14706 } 14707 14708 DB_SHOW_COMMAND(sd_allocindir, db_show_sd_allocindir) 14709 { 14710 if (have_addr == 0) { 14711 db_printf("allocindir address required\n"); 14712 return; 14713 } 14714 allocindir_print((struct allocindir *)addr); 14715 } 14716 14717 #endif /* DDB */ 14718 14719 #endif /* SOFTUPDATES */ 14720