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 (DIP(ip, i_modrev) == freeblks->fb_modrev) { 8056 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare); 8057 ip->i_flag |= IN_CHANGE; 8058 /* 8059 * We must wait so this happens before the 8060 * journal is reclaimed. 8061 */ 8062 ffs_update(vp, 1); 8063 } 8064 vput(vp); 8065 } 8066 if (spare < 0) { 8067 UFS_LOCK(ump); 8068 fs->fs_pendingblocks += spare; 8069 UFS_UNLOCK(ump); 8070 } 8071 #ifdef QUOTA 8072 /* Handle spare. */ 8073 if (spare) 8074 quotaadj(freeblks->fb_quota, ump, -spare); 8075 quotarele(freeblks->fb_quota); 8076 #endif 8077 ACQUIRE_LOCK(ump); 8078 if (freeblks->fb_state & ONDEPLIST) { 8079 inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum, 8080 0, &inodedep); 8081 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next); 8082 freeblks->fb_state &= ~ONDEPLIST; 8083 if (TAILQ_EMPTY(&inodedep->id_freeblklst)) 8084 free_inodedep(inodedep); 8085 } 8086 /* 8087 * All of the freeblock deps must be complete prior to this call 8088 * so it's now safe to complete earlier outstanding journal entries. 8089 */ 8090 handle_jwork(&freeblks->fb_jwork); 8091 WORKITEM_FREE(freeblks, D_FREEBLKS); 8092 FREE_LOCK(ump); 8093 return (0); 8094 } 8095 8096 /* 8097 * Release blocks associated with the freeblks and stored in the indirect 8098 * block dbn. If level is greater than SINGLE, the block is an indirect block 8099 * and recursive calls to indirtrunc must be used to cleanse other indirect 8100 * blocks. 8101 * 8102 * This handles partial and complete truncation of blocks. Partial is noted 8103 * with goingaway == 0. In this case the freework is completed after the 8104 * zero'd indirects are written to disk. For full truncation the freework 8105 * is completed after the block is freed. 8106 */ 8107 static void 8108 indir_trunc(freework, dbn, lbn) 8109 struct freework *freework; 8110 ufs2_daddr_t dbn; 8111 ufs_lbn_t lbn; 8112 { 8113 struct freework *nfreework; 8114 struct workhead wkhd; 8115 struct freeblks *freeblks; 8116 struct buf *bp; 8117 struct fs *fs; 8118 struct indirdep *indirdep; 8119 struct mount *mp; 8120 struct ufsmount *ump; 8121 ufs1_daddr_t *bap1; 8122 ufs2_daddr_t nb, nnb, *bap2; 8123 ufs_lbn_t lbnadd, nlbn; 8124 u_long key; 8125 int nblocks, ufs1fmt, freedblocks; 8126 int goingaway, freedeps, needj, level, cnt, i; 8127 8128 freeblks = freework->fw_freeblks; 8129 mp = freeblks->fb_list.wk_mp; 8130 ump = VFSTOUFS(mp); 8131 fs = ump->um_fs; 8132 /* 8133 * Get buffer of block pointers to be freed. There are three cases: 8134 * 8135 * 1) Partial truncate caches the indirdep pointer in the freework 8136 * which provides us a back copy to the save bp which holds the 8137 * pointers we want to clear. When this completes the zero 8138 * pointers are written to the real copy. 8139 * 2) The indirect is being completely truncated, cancel_indirdep() 8140 * eliminated the real copy and placed the indirdep on the saved 8141 * copy. The indirdep and buf are discarded when this completes. 8142 * 3) The indirect was not in memory, we read a copy off of the disk 8143 * using the devvp and drop and invalidate the buffer when we're 8144 * done. 8145 */ 8146 goingaway = 1; 8147 indirdep = NULL; 8148 if (freework->fw_indir != NULL) { 8149 goingaway = 0; 8150 indirdep = freework->fw_indir; 8151 bp = indirdep->ir_savebp; 8152 if (bp == NULL || bp->b_blkno != dbn) 8153 panic("indir_trunc: Bad saved buf %p blkno %jd", 8154 bp, (intmax_t)dbn); 8155 } else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) { 8156 /* 8157 * The lock prevents the buf dep list from changing and 8158 * indirects on devvp should only ever have one dependency. 8159 */ 8160 indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep)); 8161 if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0) 8162 panic("indir_trunc: Bad indirdep %p from buf %p", 8163 indirdep, bp); 8164 } else if (bread(freeblks->fb_devvp, dbn, (int)fs->fs_bsize, 8165 NOCRED, &bp) != 0) { 8166 brelse(bp); 8167 return; 8168 } 8169 ACQUIRE_LOCK(ump); 8170 /* Protects against a race with complete_trunc_indir(). */ 8171 freework->fw_state &= ~INPROGRESS; 8172 /* 8173 * If we have an indirdep we need to enforce the truncation order 8174 * and discard it when it is complete. 8175 */ 8176 if (indirdep) { 8177 if (freework != TAILQ_FIRST(&indirdep->ir_trunc) && 8178 !TAILQ_EMPTY(&indirdep->ir_trunc)) { 8179 /* 8180 * Add the complete truncate to the list on the 8181 * indirdep to enforce in-order processing. 8182 */ 8183 if (freework->fw_indir == NULL) 8184 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, 8185 freework, fw_next); 8186 FREE_LOCK(ump); 8187 return; 8188 } 8189 /* 8190 * If we're goingaway, free the indirdep. Otherwise it will 8191 * linger until the write completes. 8192 */ 8193 if (goingaway) 8194 free_indirdep(indirdep); 8195 } 8196 FREE_LOCK(ump); 8197 /* Initialize pointers depending on block size. */ 8198 if (ump->um_fstype == UFS1) { 8199 bap1 = (ufs1_daddr_t *)bp->b_data; 8200 nb = bap1[freework->fw_off]; 8201 ufs1fmt = 1; 8202 bap2 = NULL; 8203 } else { 8204 bap2 = (ufs2_daddr_t *)bp->b_data; 8205 nb = bap2[freework->fw_off]; 8206 ufs1fmt = 0; 8207 bap1 = NULL; 8208 } 8209 level = lbn_level(lbn); 8210 needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0; 8211 lbnadd = lbn_offset(fs, level); 8212 nblocks = btodb(fs->fs_bsize); 8213 nfreework = freework; 8214 freedeps = 0; 8215 cnt = 0; 8216 /* 8217 * Reclaim blocks. Traverses into nested indirect levels and 8218 * arranges for the current level to be freed when subordinates 8219 * are free when journaling. 8220 */ 8221 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum); 8222 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) { 8223 if (UFS_CHECK_BLKNO(mp, freeblks->fb_inum, nb, 8224 fs->fs_bsize) != 0) 8225 nb = 0; 8226 if (i != NINDIR(fs) - 1) { 8227 if (ufs1fmt) 8228 nnb = bap1[i+1]; 8229 else 8230 nnb = bap2[i+1]; 8231 } else 8232 nnb = 0; 8233 if (nb == 0) 8234 continue; 8235 cnt++; 8236 if (level != 0) { 8237 nlbn = (lbn + 1) - (i * lbnadd); 8238 if (needj != 0) { 8239 nfreework = newfreework(ump, freeblks, freework, 8240 nlbn, nb, fs->fs_frag, 0, 0); 8241 freedeps++; 8242 } 8243 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn); 8244 } else { 8245 struct freedep *freedep; 8246 8247 /* 8248 * Attempt to aggregate freedep dependencies for 8249 * all blocks being released to the same CG. 8250 */ 8251 LIST_INIT(&wkhd); 8252 if (needj != 0 && 8253 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) { 8254 freedep = newfreedep(freework); 8255 WORKLIST_INSERT_UNLOCKED(&wkhd, 8256 &freedep->fd_list); 8257 freedeps++; 8258 } 8259 CTR3(KTR_SUJ, 8260 "indir_trunc: ino %jd blkno %jd size %d", 8261 freeblks->fb_inum, nb, fs->fs_bsize); 8262 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb, 8263 fs->fs_bsize, freeblks->fb_inum, 8264 freeblks->fb_vtype, &wkhd, key); 8265 } 8266 } 8267 ffs_blkrelease_finish(ump, key); 8268 if (goingaway) { 8269 bp->b_flags |= B_INVAL | B_NOCACHE; 8270 brelse(bp); 8271 } 8272 freedblocks = 0; 8273 if (level == 0) 8274 freedblocks = (nblocks * cnt); 8275 if (needj == 0) 8276 freedblocks += nblocks; 8277 freeblks_free(ump, freeblks, freedblocks); 8278 /* 8279 * If we are journaling set up the ref counts and offset so this 8280 * indirect can be completed when its children are free. 8281 */ 8282 if (needj) { 8283 ACQUIRE_LOCK(ump); 8284 freework->fw_off = i; 8285 freework->fw_ref += freedeps; 8286 freework->fw_ref -= NINDIR(fs) + 1; 8287 if (level == 0) 8288 freeblks->fb_cgwait += freedeps; 8289 if (freework->fw_ref == 0) 8290 freework_freeblock(freework, SINGLETON_KEY); 8291 FREE_LOCK(ump); 8292 return; 8293 } 8294 /* 8295 * If we're not journaling we can free the indirect now. 8296 */ 8297 dbn = dbtofsb(fs, dbn); 8298 CTR3(KTR_SUJ, 8299 "indir_trunc 2: ino %jd blkno %jd size %d", 8300 freeblks->fb_inum, dbn, fs->fs_bsize); 8301 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize, 8302 freeblks->fb_inum, freeblks->fb_vtype, NULL, SINGLETON_KEY); 8303 /* Non SUJ softdep does single-threaded truncations. */ 8304 if (freework->fw_blkno == dbn) { 8305 freework->fw_state |= ALLCOMPLETE; 8306 ACQUIRE_LOCK(ump); 8307 handle_written_freework(freework); 8308 FREE_LOCK(ump); 8309 } 8310 return; 8311 } 8312 8313 /* 8314 * Cancel an allocindir when it is removed via truncation. When bp is not 8315 * NULL the indirect never appeared on disk and is scheduled to be freed 8316 * independently of the indir so we can more easily track journal work. 8317 */ 8318 static void 8319 cancel_allocindir(aip, bp, freeblks, trunc) 8320 struct allocindir *aip; 8321 struct buf *bp; 8322 struct freeblks *freeblks; 8323 int trunc; 8324 { 8325 struct indirdep *indirdep; 8326 struct freefrag *freefrag; 8327 struct newblk *newblk; 8328 8329 newblk = (struct newblk *)aip; 8330 LIST_REMOVE(aip, ai_next); 8331 /* 8332 * We must eliminate the pointer in bp if it must be freed on its 8333 * own due to partial truncate or pending journal work. 8334 */ 8335 if (bp && (trunc || newblk->nb_jnewblk)) { 8336 /* 8337 * Clear the pointer and mark the aip to be freed 8338 * directly if it never existed on disk. 8339 */ 8340 aip->ai_state |= DELAYEDFREE; 8341 indirdep = aip->ai_indirdep; 8342 if (indirdep->ir_state & UFS1FMT) 8343 ((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8344 else 8345 ((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8346 } 8347 /* 8348 * When truncating the previous pointer will be freed via 8349 * savedbp. Eliminate the freefrag which would dup free. 8350 */ 8351 if (trunc && (freefrag = newblk->nb_freefrag) != NULL) { 8352 newblk->nb_freefrag = NULL; 8353 if (freefrag->ff_jdep) 8354 cancel_jfreefrag( 8355 WK_JFREEFRAG(freefrag->ff_jdep)); 8356 jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork); 8357 WORKITEM_FREE(freefrag, D_FREEFRAG); 8358 } 8359 /* 8360 * If the journal hasn't been written the jnewblk must be passed 8361 * to the call to ffs_blkfree that reclaims the space. We accomplish 8362 * this by leaving the journal dependency on the newblk to be freed 8363 * when a freework is created in handle_workitem_freeblocks(). 8364 */ 8365 cancel_newblk(newblk, NULL, &freeblks->fb_jwork); 8366 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 8367 } 8368 8369 /* 8370 * Create the mkdir dependencies for . and .. in a new directory. Link them 8371 * in to a newdirblk so any subsequent additions are tracked properly. The 8372 * caller is responsible for adding the mkdir1 dependency to the journal 8373 * and updating id_mkdiradd. This function returns with the per-filesystem 8374 * lock held. 8375 */ 8376 static struct mkdir * 8377 setup_newdir(dap, newinum, dinum, newdirbp, mkdirp) 8378 struct diradd *dap; 8379 ino_t newinum; 8380 ino_t dinum; 8381 struct buf *newdirbp; 8382 struct mkdir **mkdirp; 8383 { 8384 struct newblk *newblk; 8385 struct pagedep *pagedep; 8386 struct inodedep *inodedep; 8387 struct newdirblk *newdirblk; 8388 struct mkdir *mkdir1, *mkdir2; 8389 struct worklist *wk; 8390 struct jaddref *jaddref; 8391 struct ufsmount *ump; 8392 struct mount *mp; 8393 8394 mp = dap->da_list.wk_mp; 8395 ump = VFSTOUFS(mp); 8396 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK, 8397 M_SOFTDEP_FLAGS); 8398 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8399 LIST_INIT(&newdirblk->db_mkdir); 8400 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8401 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp); 8402 mkdir1->md_state = ATTACHED | MKDIR_BODY; 8403 mkdir1->md_diradd = dap; 8404 mkdir1->md_jaddref = NULL; 8405 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8406 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp); 8407 mkdir2->md_state = ATTACHED | MKDIR_PARENT; 8408 mkdir2->md_diradd = dap; 8409 mkdir2->md_jaddref = NULL; 8410 if (MOUNTEDSUJ(mp) == 0) { 8411 mkdir1->md_state |= DEPCOMPLETE; 8412 mkdir2->md_state |= DEPCOMPLETE; 8413 } 8414 /* 8415 * Dependency on "." and ".." being written to disk. 8416 */ 8417 mkdir1->md_buf = newdirbp; 8418 ACQUIRE_LOCK(VFSTOUFS(mp)); 8419 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir1, md_mkdirs); 8420 /* 8421 * We must link the pagedep, allocdirect, and newdirblk for 8422 * the initial file page so the pointer to the new directory 8423 * is not written until the directory contents are live and 8424 * any subsequent additions are not marked live until the 8425 * block is reachable via the inode. 8426 */ 8427 if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0) 8428 panic("setup_newdir: lost pagedep"); 8429 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list) 8430 if (wk->wk_type == D_ALLOCDIRECT) 8431 break; 8432 if (wk == NULL) 8433 panic("setup_newdir: lost allocdirect"); 8434 if (pagedep->pd_state & NEWBLOCK) 8435 panic("setup_newdir: NEWBLOCK already set"); 8436 newblk = WK_NEWBLK(wk); 8437 pagedep->pd_state |= NEWBLOCK; 8438 pagedep->pd_newdirblk = newdirblk; 8439 newdirblk->db_pagedep = pagedep; 8440 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8441 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list); 8442 /* 8443 * Look up the inodedep for the parent directory so that we 8444 * can link mkdir2 into the pending dotdot jaddref or 8445 * the inode write if there is none. If the inode is 8446 * ALLCOMPLETE and no jaddref is present all dependencies have 8447 * been satisfied and mkdir2 can be freed. 8448 */ 8449 inodedep_lookup(mp, dinum, 0, &inodedep); 8450 if (MOUNTEDSUJ(mp)) { 8451 if (inodedep == NULL) 8452 panic("setup_newdir: Lost parent."); 8453 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8454 inoreflst); 8455 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum && 8456 (jaddref->ja_state & MKDIR_PARENT), 8457 ("setup_newdir: bad dotdot jaddref %p", jaddref)); 8458 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8459 mkdir2->md_jaddref = jaddref; 8460 jaddref->ja_mkdir = mkdir2; 8461 } else if (inodedep == NULL || 8462 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 8463 dap->da_state &= ~MKDIR_PARENT; 8464 WORKITEM_FREE(mkdir2, D_MKDIR); 8465 mkdir2 = NULL; 8466 } else { 8467 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8468 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list); 8469 } 8470 *mkdirp = mkdir2; 8471 8472 return (mkdir1); 8473 } 8474 8475 /* 8476 * Directory entry addition dependencies. 8477 * 8478 * When adding a new directory entry, the inode (with its incremented link 8479 * count) must be written to disk before the directory entry's pointer to it. 8480 * Also, if the inode is newly allocated, the corresponding freemap must be 8481 * updated (on disk) before the directory entry's pointer. These requirements 8482 * are met via undo/redo on the directory entry's pointer, which consists 8483 * simply of the inode number. 8484 * 8485 * As directory entries are added and deleted, the free space within a 8486 * directory block can become fragmented. The ufs filesystem will compact 8487 * a fragmented directory block to make space for a new entry. When this 8488 * occurs, the offsets of previously added entries change. Any "diradd" 8489 * dependency structures corresponding to these entries must be updated with 8490 * the new offsets. 8491 */ 8492 8493 /* 8494 * This routine is called after the in-memory inode's link 8495 * count has been incremented, but before the directory entry's 8496 * pointer to the inode has been set. 8497 */ 8498 int 8499 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 8500 struct buf *bp; /* buffer containing directory block */ 8501 struct inode *dp; /* inode for directory */ 8502 off_t diroffset; /* offset of new entry in directory */ 8503 ino_t newinum; /* inode referenced by new directory entry */ 8504 struct buf *newdirbp; /* non-NULL => contents of new mkdir */ 8505 int isnewblk; /* entry is in a newly allocated block */ 8506 { 8507 int offset; /* offset of new entry within directory block */ 8508 ufs_lbn_t lbn; /* block in directory containing new entry */ 8509 struct fs *fs; 8510 struct diradd *dap; 8511 struct newblk *newblk; 8512 struct pagedep *pagedep; 8513 struct inodedep *inodedep; 8514 struct newdirblk *newdirblk; 8515 struct mkdir *mkdir1, *mkdir2; 8516 struct jaddref *jaddref; 8517 struct ufsmount *ump; 8518 struct mount *mp; 8519 int isindir; 8520 8521 mp = ITOVFS(dp); 8522 ump = VFSTOUFS(mp); 8523 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 8524 ("softdep_setup_directory_add called on non-softdep filesystem")); 8525 /* 8526 * Whiteouts have no dependencies. 8527 */ 8528 if (newinum == UFS_WINO) { 8529 if (newdirbp != NULL) 8530 bdwrite(newdirbp); 8531 return (0); 8532 } 8533 jaddref = NULL; 8534 mkdir1 = mkdir2 = NULL; 8535 fs = ump->um_fs; 8536 lbn = lblkno(fs, diroffset); 8537 offset = blkoff(fs, diroffset); 8538 dap = malloc(sizeof(struct diradd), M_DIRADD, 8539 M_SOFTDEP_FLAGS|M_ZERO); 8540 workitem_alloc(&dap->da_list, D_DIRADD, mp); 8541 dap->da_offset = offset; 8542 dap->da_newinum = newinum; 8543 dap->da_state = ATTACHED; 8544 LIST_INIT(&dap->da_jwork); 8545 isindir = bp->b_lblkno >= UFS_NDADDR; 8546 newdirblk = NULL; 8547 if (isnewblk && 8548 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) { 8549 newdirblk = malloc(sizeof(struct newdirblk), 8550 M_NEWDIRBLK, M_SOFTDEP_FLAGS); 8551 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8552 LIST_INIT(&newdirblk->db_mkdir); 8553 } 8554 /* 8555 * If we're creating a new directory setup the dependencies and set 8556 * the dap state to wait for them. Otherwise it's COMPLETE and 8557 * we can move on. 8558 */ 8559 if (newdirbp == NULL) { 8560 dap->da_state |= DEPCOMPLETE; 8561 ACQUIRE_LOCK(ump); 8562 } else { 8563 dap->da_state |= MKDIR_BODY | MKDIR_PARENT; 8564 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp, 8565 &mkdir2); 8566 } 8567 /* 8568 * Link into parent directory pagedep to await its being written. 8569 */ 8570 pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep); 8571 #ifdef INVARIANTS 8572 if (diradd_lookup(pagedep, offset) != NULL) 8573 panic("softdep_setup_directory_add: %p already at off %d\n", 8574 diradd_lookup(pagedep, offset), offset); 8575 #endif 8576 dap->da_pagedep = pagedep; 8577 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap, 8578 da_pdlist); 8579 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 8580 /* 8581 * If we're journaling, link the diradd into the jaddref so it 8582 * may be completed after the journal entry is written. Otherwise, 8583 * link the diradd into its inodedep. If the inode is not yet 8584 * written place it on the bufwait list, otherwise do the post-inode 8585 * write processing to put it on the id_pendinghd list. 8586 */ 8587 if (MOUNTEDSUJ(mp)) { 8588 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8589 inoreflst); 8590 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 8591 ("softdep_setup_directory_add: bad jaddref %p", jaddref)); 8592 jaddref->ja_diroff = diroffset; 8593 jaddref->ja_diradd = dap; 8594 add_to_journal(&jaddref->ja_list); 8595 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) 8596 diradd_inode_written(dap, inodedep); 8597 else 8598 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 8599 /* 8600 * Add the journal entries for . and .. links now that the primary 8601 * link is written. 8602 */ 8603 if (mkdir1 != NULL && MOUNTEDSUJ(mp)) { 8604 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 8605 inoreflst, if_deps); 8606 KASSERT(jaddref != NULL && 8607 jaddref->ja_ino == jaddref->ja_parent && 8608 (jaddref->ja_state & MKDIR_BODY), 8609 ("softdep_setup_directory_add: bad dot jaddref %p", 8610 jaddref)); 8611 mkdir1->md_jaddref = jaddref; 8612 jaddref->ja_mkdir = mkdir1; 8613 /* 8614 * It is important that the dotdot journal entry 8615 * is added prior to the dot entry since dot writes 8616 * both the dot and dotdot links. These both must 8617 * be added after the primary link for the journal 8618 * to remain consistent. 8619 */ 8620 add_to_journal(&mkdir2->md_jaddref->ja_list); 8621 add_to_journal(&jaddref->ja_list); 8622 } 8623 /* 8624 * If we are adding a new directory remember this diradd so that if 8625 * we rename it we can keep the dot and dotdot dependencies. If 8626 * we are adding a new name for an inode that has a mkdiradd we 8627 * must be in rename and we have to move the dot and dotdot 8628 * dependencies to this new name. The old name is being orphaned 8629 * soon. 8630 */ 8631 if (mkdir1 != NULL) { 8632 if (inodedep->id_mkdiradd != NULL) 8633 panic("softdep_setup_directory_add: Existing mkdir"); 8634 inodedep->id_mkdiradd = dap; 8635 } else if (inodedep->id_mkdiradd) 8636 merge_diradd(inodedep, dap); 8637 if (newdirblk != NULL) { 8638 /* 8639 * There is nothing to do if we are already tracking 8640 * this block. 8641 */ 8642 if ((pagedep->pd_state & NEWBLOCK) != 0) { 8643 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 8644 FREE_LOCK(ump); 8645 return (0); 8646 } 8647 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk) 8648 == 0) 8649 panic("softdep_setup_directory_add: lost entry"); 8650 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8651 pagedep->pd_state |= NEWBLOCK; 8652 pagedep->pd_newdirblk = newdirblk; 8653 newdirblk->db_pagedep = pagedep; 8654 FREE_LOCK(ump); 8655 /* 8656 * If we extended into an indirect signal direnter to sync. 8657 */ 8658 if (isindir) 8659 return (1); 8660 return (0); 8661 } 8662 FREE_LOCK(ump); 8663 return (0); 8664 } 8665 8666 /* 8667 * This procedure is called to change the offset of a directory 8668 * entry when compacting a directory block which must be owned 8669 * exclusively by the caller. Note that the actual entry movement 8670 * must be done in this procedure to ensure that no I/O completions 8671 * occur while the move is in progress. 8672 */ 8673 void 8674 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 8675 struct buf *bp; /* Buffer holding directory block. */ 8676 struct inode *dp; /* inode for directory */ 8677 caddr_t base; /* address of dp->i_offset */ 8678 caddr_t oldloc; /* address of old directory location */ 8679 caddr_t newloc; /* address of new directory location */ 8680 int entrysize; /* size of directory entry */ 8681 { 8682 int offset, oldoffset, newoffset; 8683 struct pagedep *pagedep; 8684 struct jmvref *jmvref; 8685 struct diradd *dap; 8686 struct direct *de; 8687 struct mount *mp; 8688 struct ufsmount *ump; 8689 ufs_lbn_t lbn; 8690 int flags; 8691 8692 mp = ITOVFS(dp); 8693 ump = VFSTOUFS(mp); 8694 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 8695 ("softdep_change_directoryentry_offset called on " 8696 "non-softdep filesystem")); 8697 de = (struct direct *)oldloc; 8698 jmvref = NULL; 8699 flags = 0; 8700 /* 8701 * Moves are always journaled as it would be too complex to 8702 * determine if any affected adds or removes are present in the 8703 * journal. 8704 */ 8705 if (MOUNTEDSUJ(mp)) { 8706 flags = DEPALLOC; 8707 jmvref = newjmvref(dp, de->d_ino, 8708 dp->i_offset + (oldloc - base), 8709 dp->i_offset + (newloc - base)); 8710 } 8711 lbn = lblkno(ump->um_fs, dp->i_offset); 8712 offset = blkoff(ump->um_fs, dp->i_offset); 8713 oldoffset = offset + (oldloc - base); 8714 newoffset = offset + (newloc - base); 8715 ACQUIRE_LOCK(ump); 8716 if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0) 8717 goto done; 8718 dap = diradd_lookup(pagedep, oldoffset); 8719 if (dap) { 8720 dap->da_offset = newoffset; 8721 newoffset = DIRADDHASH(newoffset); 8722 oldoffset = DIRADDHASH(oldoffset); 8723 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE && 8724 newoffset != oldoffset) { 8725 LIST_REMOVE(dap, da_pdlist); 8726 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset], 8727 dap, da_pdlist); 8728 } 8729 } 8730 done: 8731 if (jmvref) { 8732 jmvref->jm_pagedep = pagedep; 8733 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps); 8734 add_to_journal(&jmvref->jm_list); 8735 } 8736 bcopy(oldloc, newloc, entrysize); 8737 FREE_LOCK(ump); 8738 } 8739 8740 /* 8741 * Move the mkdir dependencies and journal work from one diradd to another 8742 * when renaming a directory. The new name must depend on the mkdir deps 8743 * completing as the old name did. Directories can only have one valid link 8744 * at a time so one must be canonical. 8745 */ 8746 static void 8747 merge_diradd(inodedep, newdap) 8748 struct inodedep *inodedep; 8749 struct diradd *newdap; 8750 { 8751 struct diradd *olddap; 8752 struct mkdir *mkdir, *nextmd; 8753 struct ufsmount *ump; 8754 short state; 8755 8756 olddap = inodedep->id_mkdiradd; 8757 inodedep->id_mkdiradd = newdap; 8758 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8759 newdap->da_state &= ~DEPCOMPLETE; 8760 ump = VFSTOUFS(inodedep->id_list.wk_mp); 8761 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 8762 mkdir = nextmd) { 8763 nextmd = LIST_NEXT(mkdir, md_mkdirs); 8764 if (mkdir->md_diradd != olddap) 8765 continue; 8766 mkdir->md_diradd = newdap; 8767 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY); 8768 newdap->da_state |= state; 8769 olddap->da_state &= ~state; 8770 if ((olddap->da_state & 8771 (MKDIR_PARENT | MKDIR_BODY)) == 0) 8772 break; 8773 } 8774 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 8775 panic("merge_diradd: unfound ref"); 8776 } 8777 /* 8778 * Any mkdir related journal items are not safe to be freed until 8779 * the new name is stable. 8780 */ 8781 jwork_move(&newdap->da_jwork, &olddap->da_jwork); 8782 olddap->da_state |= DEPCOMPLETE; 8783 complete_diradd(olddap); 8784 } 8785 8786 /* 8787 * Move the diradd to the pending list when all diradd dependencies are 8788 * complete. 8789 */ 8790 static void 8791 complete_diradd(dap) 8792 struct diradd *dap; 8793 { 8794 struct pagedep *pagedep; 8795 8796 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 8797 if (dap->da_state & DIRCHG) 8798 pagedep = dap->da_previous->dm_pagedep; 8799 else 8800 pagedep = dap->da_pagedep; 8801 LIST_REMOVE(dap, da_pdlist); 8802 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 8803 } 8804 } 8805 8806 /* 8807 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal 8808 * add entries and conditonally journal the remove. 8809 */ 8810 static void 8811 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref) 8812 struct diradd *dap; 8813 struct dirrem *dirrem; 8814 struct jremref *jremref; 8815 struct jremref *dotremref; 8816 struct jremref *dotdotremref; 8817 { 8818 struct inodedep *inodedep; 8819 struct jaddref *jaddref; 8820 struct inoref *inoref; 8821 struct ufsmount *ump; 8822 struct mkdir *mkdir; 8823 8824 /* 8825 * If no remove references were allocated we're on a non-journaled 8826 * filesystem and can skip the cancel step. 8827 */ 8828 if (jremref == NULL) { 8829 free_diradd(dap, NULL); 8830 return; 8831 } 8832 /* 8833 * Cancel the primary name an free it if it does not require 8834 * journaling. 8835 */ 8836 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum, 8837 0, &inodedep) != 0) { 8838 /* Abort the addref that reference this diradd. */ 8839 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 8840 if (inoref->if_list.wk_type != D_JADDREF) 8841 continue; 8842 jaddref = (struct jaddref *)inoref; 8843 if (jaddref->ja_diradd != dap) 8844 continue; 8845 if (cancel_jaddref(jaddref, inodedep, 8846 &dirrem->dm_jwork) == 0) { 8847 free_jremref(jremref); 8848 jremref = NULL; 8849 } 8850 break; 8851 } 8852 } 8853 /* 8854 * Cancel subordinate names and free them if they do not require 8855 * journaling. 8856 */ 8857 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8858 ump = VFSTOUFS(dap->da_list.wk_mp); 8859 LIST_FOREACH(mkdir, &ump->softdep_mkdirlisthd, md_mkdirs) { 8860 if (mkdir->md_diradd != dap) 8861 continue; 8862 if ((jaddref = mkdir->md_jaddref) == NULL) 8863 continue; 8864 mkdir->md_jaddref = NULL; 8865 if (mkdir->md_state & MKDIR_PARENT) { 8866 if (cancel_jaddref(jaddref, NULL, 8867 &dirrem->dm_jwork) == 0) { 8868 free_jremref(dotdotremref); 8869 dotdotremref = NULL; 8870 } 8871 } else { 8872 if (cancel_jaddref(jaddref, inodedep, 8873 &dirrem->dm_jwork) == 0) { 8874 free_jremref(dotremref); 8875 dotremref = NULL; 8876 } 8877 } 8878 } 8879 } 8880 8881 if (jremref) 8882 journal_jremref(dirrem, jremref, inodedep); 8883 if (dotremref) 8884 journal_jremref(dirrem, dotremref, inodedep); 8885 if (dotdotremref) 8886 journal_jremref(dirrem, dotdotremref, NULL); 8887 jwork_move(&dirrem->dm_jwork, &dap->da_jwork); 8888 free_diradd(dap, &dirrem->dm_jwork); 8889 } 8890 8891 /* 8892 * Free a diradd dependency structure. 8893 */ 8894 static void 8895 free_diradd(dap, wkhd) 8896 struct diradd *dap; 8897 struct workhead *wkhd; 8898 { 8899 struct dirrem *dirrem; 8900 struct pagedep *pagedep; 8901 struct inodedep *inodedep; 8902 struct mkdir *mkdir, *nextmd; 8903 struct ufsmount *ump; 8904 8905 ump = VFSTOUFS(dap->da_list.wk_mp); 8906 LOCK_OWNED(ump); 8907 LIST_REMOVE(dap, da_pdlist); 8908 if (dap->da_state & ONWORKLIST) 8909 WORKLIST_REMOVE(&dap->da_list); 8910 if ((dap->da_state & DIRCHG) == 0) { 8911 pagedep = dap->da_pagedep; 8912 } else { 8913 dirrem = dap->da_previous; 8914 pagedep = dirrem->dm_pagedep; 8915 dirrem->dm_dirinum = pagedep->pd_ino; 8916 dirrem->dm_state |= COMPLETE; 8917 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 8918 add_to_worklist(&dirrem->dm_list, 0); 8919 } 8920 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum, 8921 0, &inodedep) != 0) 8922 if (inodedep->id_mkdiradd == dap) 8923 inodedep->id_mkdiradd = NULL; 8924 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8925 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 8926 mkdir = nextmd) { 8927 nextmd = LIST_NEXT(mkdir, md_mkdirs); 8928 if (mkdir->md_diradd != dap) 8929 continue; 8930 dap->da_state &= 8931 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 8932 LIST_REMOVE(mkdir, md_mkdirs); 8933 if (mkdir->md_state & ONWORKLIST) 8934 WORKLIST_REMOVE(&mkdir->md_list); 8935 if (mkdir->md_jaddref != NULL) 8936 panic("free_diradd: Unexpected jaddref"); 8937 WORKITEM_FREE(mkdir, D_MKDIR); 8938 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) 8939 break; 8940 } 8941 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 8942 panic("free_diradd: unfound ref"); 8943 } 8944 if (inodedep) 8945 free_inodedep(inodedep); 8946 /* 8947 * Free any journal segments waiting for the directory write. 8948 */ 8949 handle_jwork(&dap->da_jwork); 8950 WORKITEM_FREE(dap, D_DIRADD); 8951 } 8952 8953 /* 8954 * Directory entry removal dependencies. 8955 * 8956 * When removing a directory entry, the entry's inode pointer must be 8957 * zero'ed on disk before the corresponding inode's link count is decremented 8958 * (possibly freeing the inode for re-use). This dependency is handled by 8959 * updating the directory entry but delaying the inode count reduction until 8960 * after the directory block has been written to disk. After this point, the 8961 * inode count can be decremented whenever it is convenient. 8962 */ 8963 8964 /* 8965 * This routine should be called immediately after removing 8966 * a directory entry. The inode's link count should not be 8967 * decremented by the calling procedure -- the soft updates 8968 * code will do this task when it is safe. 8969 */ 8970 void 8971 softdep_setup_remove(bp, dp, ip, isrmdir) 8972 struct buf *bp; /* buffer containing directory block */ 8973 struct inode *dp; /* inode for the directory being modified */ 8974 struct inode *ip; /* inode for directory entry being removed */ 8975 int isrmdir; /* indicates if doing RMDIR */ 8976 { 8977 struct dirrem *dirrem, *prevdirrem; 8978 struct inodedep *inodedep; 8979 struct ufsmount *ump; 8980 int direct; 8981 8982 ump = ITOUMP(ip); 8983 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 8984 ("softdep_setup_remove called on non-softdep filesystem")); 8985 /* 8986 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want 8987 * newdirrem() to setup the full directory remove which requires 8988 * isrmdir > 1. 8989 */ 8990 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 8991 /* 8992 * Add the dirrem to the inodedep's pending remove list for quick 8993 * discovery later. 8994 */ 8995 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) 8996 panic("softdep_setup_remove: Lost inodedep."); 8997 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 8998 dirrem->dm_state |= ONDEPLIST; 8999 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9000 9001 /* 9002 * If the COMPLETE flag is clear, then there were no active 9003 * entries and we want to roll back to a zeroed entry until 9004 * the new inode is committed to disk. If the COMPLETE flag is 9005 * set then we have deleted an entry that never made it to 9006 * disk. If the entry we deleted resulted from a name change, 9007 * then the old name still resides on disk. We cannot delete 9008 * its inode (returned to us in prevdirrem) until the zeroed 9009 * directory entry gets to disk. The new inode has never been 9010 * referenced on the disk, so can be deleted immediately. 9011 */ 9012 if ((dirrem->dm_state & COMPLETE) == 0) { 9013 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem, 9014 dm_next); 9015 FREE_LOCK(ump); 9016 } else { 9017 if (prevdirrem != NULL) 9018 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, 9019 prevdirrem, dm_next); 9020 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino; 9021 direct = LIST_EMPTY(&dirrem->dm_jremrefhd); 9022 FREE_LOCK(ump); 9023 if (direct) 9024 handle_workitem_remove(dirrem, 0); 9025 } 9026 } 9027 9028 /* 9029 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the 9030 * pd_pendinghd list of a pagedep. 9031 */ 9032 static struct diradd * 9033 diradd_lookup(pagedep, offset) 9034 struct pagedep *pagedep; 9035 int offset; 9036 { 9037 struct diradd *dap; 9038 9039 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist) 9040 if (dap->da_offset == offset) 9041 return (dap); 9042 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 9043 if (dap->da_offset == offset) 9044 return (dap); 9045 return (NULL); 9046 } 9047 9048 /* 9049 * Search for a .. diradd dependency in a directory that is being removed. 9050 * If the directory was renamed to a new parent we have a diradd rather 9051 * than a mkdir for the .. entry. We need to cancel it now before 9052 * it is found in truncate(). 9053 */ 9054 static struct jremref * 9055 cancel_diradd_dotdot(ip, dirrem, jremref) 9056 struct inode *ip; 9057 struct dirrem *dirrem; 9058 struct jremref *jremref; 9059 { 9060 struct pagedep *pagedep; 9061 struct diradd *dap; 9062 struct worklist *wk; 9063 9064 if (pagedep_lookup(ITOVFS(ip), NULL, ip->i_number, 0, 0, &pagedep) == 0) 9065 return (jremref); 9066 dap = diradd_lookup(pagedep, DOTDOT_OFFSET); 9067 if (dap == NULL) 9068 return (jremref); 9069 cancel_diradd(dap, dirrem, jremref, NULL, NULL); 9070 /* 9071 * Mark any journal work as belonging to the parent so it is freed 9072 * with the .. reference. 9073 */ 9074 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9075 wk->wk_state |= MKDIR_PARENT; 9076 return (NULL); 9077 } 9078 9079 /* 9080 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to 9081 * replace it with a dirrem/diradd pair as a result of re-parenting a 9082 * directory. This ensures that we don't simultaneously have a mkdir and 9083 * a diradd for the same .. entry. 9084 */ 9085 static struct jremref * 9086 cancel_mkdir_dotdot(ip, dirrem, jremref) 9087 struct inode *ip; 9088 struct dirrem *dirrem; 9089 struct jremref *jremref; 9090 { 9091 struct inodedep *inodedep; 9092 struct jaddref *jaddref; 9093 struct ufsmount *ump; 9094 struct mkdir *mkdir; 9095 struct diradd *dap; 9096 struct mount *mp; 9097 9098 mp = ITOVFS(ip); 9099 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9100 return (jremref); 9101 dap = inodedep->id_mkdiradd; 9102 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0) 9103 return (jremref); 9104 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9105 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 9106 mkdir = LIST_NEXT(mkdir, md_mkdirs)) 9107 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT) 9108 break; 9109 if (mkdir == NULL) 9110 panic("cancel_mkdir_dotdot: Unable to find mkdir\n"); 9111 if ((jaddref = mkdir->md_jaddref) != NULL) { 9112 mkdir->md_jaddref = NULL; 9113 jaddref->ja_state &= ~MKDIR_PARENT; 9114 if (inodedep_lookup(mp, jaddref->ja_ino, 0, &inodedep) == 0) 9115 panic("cancel_mkdir_dotdot: Lost parent inodedep"); 9116 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) { 9117 journal_jremref(dirrem, jremref, inodedep); 9118 jremref = NULL; 9119 } 9120 } 9121 if (mkdir->md_state & ONWORKLIST) 9122 WORKLIST_REMOVE(&mkdir->md_list); 9123 mkdir->md_state |= ALLCOMPLETE; 9124 complete_mkdir(mkdir); 9125 return (jremref); 9126 } 9127 9128 static void 9129 journal_jremref(dirrem, jremref, inodedep) 9130 struct dirrem *dirrem; 9131 struct jremref *jremref; 9132 struct inodedep *inodedep; 9133 { 9134 9135 if (inodedep == NULL) 9136 if (inodedep_lookup(jremref->jr_list.wk_mp, 9137 jremref->jr_ref.if_ino, 0, &inodedep) == 0) 9138 panic("journal_jremref: Lost inodedep"); 9139 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps); 9140 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 9141 add_to_journal(&jremref->jr_list); 9142 } 9143 9144 static void 9145 dirrem_journal(dirrem, jremref, dotremref, dotdotremref) 9146 struct dirrem *dirrem; 9147 struct jremref *jremref; 9148 struct jremref *dotremref; 9149 struct jremref *dotdotremref; 9150 { 9151 struct inodedep *inodedep; 9152 9153 9154 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0, 9155 &inodedep) == 0) 9156 panic("dirrem_journal: Lost inodedep"); 9157 journal_jremref(dirrem, jremref, inodedep); 9158 if (dotremref) 9159 journal_jremref(dirrem, dotremref, inodedep); 9160 if (dotdotremref) 9161 journal_jremref(dirrem, dotdotremref, NULL); 9162 } 9163 9164 /* 9165 * Allocate a new dirrem if appropriate and return it along with 9166 * its associated pagedep. Called without a lock, returns with lock. 9167 */ 9168 static struct dirrem * 9169 newdirrem(bp, dp, ip, isrmdir, prevdirremp) 9170 struct buf *bp; /* buffer containing directory block */ 9171 struct inode *dp; /* inode for the directory being modified */ 9172 struct inode *ip; /* inode for directory entry being removed */ 9173 int isrmdir; /* indicates if doing RMDIR */ 9174 struct dirrem **prevdirremp; /* previously referenced inode, if any */ 9175 { 9176 int offset; 9177 ufs_lbn_t lbn; 9178 struct diradd *dap; 9179 struct dirrem *dirrem; 9180 struct pagedep *pagedep; 9181 struct jremref *jremref; 9182 struct jremref *dotremref; 9183 struct jremref *dotdotremref; 9184 struct vnode *dvp; 9185 struct ufsmount *ump; 9186 9187 /* 9188 * Whiteouts have no deletion dependencies. 9189 */ 9190 if (ip == NULL) 9191 panic("newdirrem: whiteout"); 9192 dvp = ITOV(dp); 9193 ump = ITOUMP(dp); 9194 9195 /* 9196 * If the system is over its limit and our filesystem is 9197 * responsible for more than our share of that usage and 9198 * we are not a snapshot, request some inodedep cleanup. 9199 * Limiting the number of dirrem structures will also limit 9200 * the number of freefile and freeblks structures. 9201 */ 9202 ACQUIRE_LOCK(ump); 9203 if (!IS_SNAPSHOT(ip) && softdep_excess_items(ump, D_DIRREM)) 9204 schedule_cleanup(UFSTOVFS(ump)); 9205 else 9206 FREE_LOCK(ump); 9207 dirrem = malloc(sizeof(struct dirrem), M_DIRREM, M_SOFTDEP_FLAGS | 9208 M_ZERO); 9209 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount); 9210 LIST_INIT(&dirrem->dm_jremrefhd); 9211 LIST_INIT(&dirrem->dm_jwork); 9212 dirrem->dm_state = isrmdir ? RMDIR : 0; 9213 dirrem->dm_oldinum = ip->i_number; 9214 *prevdirremp = NULL; 9215 /* 9216 * Allocate remove reference structures to track journal write 9217 * dependencies. We will always have one for the link and 9218 * when doing directories we will always have one more for dot. 9219 * When renaming a directory we skip the dotdot link change so 9220 * this is not needed. 9221 */ 9222 jremref = dotremref = dotdotremref = NULL; 9223 if (DOINGSUJ(dvp)) { 9224 if (isrmdir) { 9225 jremref = newjremref(dirrem, dp, ip, dp->i_offset, 9226 ip->i_effnlink + 2); 9227 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET, 9228 ip->i_effnlink + 1); 9229 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET, 9230 dp->i_effnlink + 1); 9231 dotdotremref->jr_state |= MKDIR_PARENT; 9232 } else 9233 jremref = newjremref(dirrem, dp, ip, dp->i_offset, 9234 ip->i_effnlink + 1); 9235 } 9236 ACQUIRE_LOCK(ump); 9237 lbn = lblkno(ump->um_fs, dp->i_offset); 9238 offset = blkoff(ump->um_fs, dp->i_offset); 9239 pagedep_lookup(UFSTOVFS(ump), bp, dp->i_number, lbn, DEPALLOC, 9240 &pagedep); 9241 dirrem->dm_pagedep = pagedep; 9242 dirrem->dm_offset = offset; 9243 /* 9244 * If we're renaming a .. link to a new directory, cancel any 9245 * existing MKDIR_PARENT mkdir. If it has already been canceled 9246 * the jremref is preserved for any potential diradd in this 9247 * location. This can not coincide with a rmdir. 9248 */ 9249 if (dp->i_offset == DOTDOT_OFFSET) { 9250 if (isrmdir) 9251 panic("newdirrem: .. directory change during remove?"); 9252 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref); 9253 } 9254 /* 9255 * If we're removing a directory search for the .. dependency now and 9256 * cancel it. Any pending journal work will be added to the dirrem 9257 * to be completed when the workitem remove completes. 9258 */ 9259 if (isrmdir) 9260 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref); 9261 /* 9262 * Check for a diradd dependency for the same directory entry. 9263 * If present, then both dependencies become obsolete and can 9264 * be de-allocated. 9265 */ 9266 dap = diradd_lookup(pagedep, offset); 9267 if (dap == NULL) { 9268 /* 9269 * Link the jremref structures into the dirrem so they are 9270 * written prior to the pagedep. 9271 */ 9272 if (jremref) 9273 dirrem_journal(dirrem, jremref, dotremref, 9274 dotdotremref); 9275 return (dirrem); 9276 } 9277 /* 9278 * Must be ATTACHED at this point. 9279 */ 9280 if ((dap->da_state & ATTACHED) == 0) 9281 panic("newdirrem: not ATTACHED"); 9282 if (dap->da_newinum != ip->i_number) 9283 panic("newdirrem: inum %ju should be %ju", 9284 (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum); 9285 /* 9286 * If we are deleting a changed name that never made it to disk, 9287 * then return the dirrem describing the previous inode (which 9288 * represents the inode currently referenced from this entry on disk). 9289 */ 9290 if ((dap->da_state & DIRCHG) != 0) { 9291 *prevdirremp = dap->da_previous; 9292 dap->da_state &= ~DIRCHG; 9293 dap->da_pagedep = pagedep; 9294 } 9295 /* 9296 * We are deleting an entry that never made it to disk. 9297 * Mark it COMPLETE so we can delete its inode immediately. 9298 */ 9299 dirrem->dm_state |= COMPLETE; 9300 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref); 9301 #ifdef INVARIANTS 9302 if (isrmdir == 0) { 9303 struct worklist *wk; 9304 9305 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9306 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT)) 9307 panic("bad wk %p (0x%X)\n", wk, wk->wk_state); 9308 } 9309 #endif 9310 9311 return (dirrem); 9312 } 9313 9314 /* 9315 * Directory entry change dependencies. 9316 * 9317 * Changing an existing directory entry requires that an add operation 9318 * be completed first followed by a deletion. The semantics for the addition 9319 * are identical to the description of adding a new entry above except 9320 * that the rollback is to the old inode number rather than zero. Once 9321 * the addition dependency is completed, the removal is done as described 9322 * in the removal routine above. 9323 */ 9324 9325 /* 9326 * This routine should be called immediately after changing 9327 * a directory entry. The inode's link count should not be 9328 * decremented by the calling procedure -- the soft updates 9329 * code will perform this task when it is safe. 9330 */ 9331 void 9332 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 9333 struct buf *bp; /* buffer containing directory block */ 9334 struct inode *dp; /* inode for the directory being modified */ 9335 struct inode *ip; /* inode for directory entry being removed */ 9336 ino_t newinum; /* new inode number for changed entry */ 9337 int isrmdir; /* indicates if doing RMDIR */ 9338 { 9339 int offset; 9340 struct diradd *dap = NULL; 9341 struct dirrem *dirrem, *prevdirrem; 9342 struct pagedep *pagedep; 9343 struct inodedep *inodedep; 9344 struct jaddref *jaddref; 9345 struct mount *mp; 9346 struct ufsmount *ump; 9347 9348 mp = ITOVFS(dp); 9349 ump = VFSTOUFS(mp); 9350 offset = blkoff(ump->um_fs, dp->i_offset); 9351 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 9352 ("softdep_setup_directory_change called on non-softdep filesystem")); 9353 9354 /* 9355 * Whiteouts do not need diradd dependencies. 9356 */ 9357 if (newinum != UFS_WINO) { 9358 dap = malloc(sizeof(struct diradd), 9359 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO); 9360 workitem_alloc(&dap->da_list, D_DIRADD, mp); 9361 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE; 9362 dap->da_offset = offset; 9363 dap->da_newinum = newinum; 9364 LIST_INIT(&dap->da_jwork); 9365 } 9366 9367 /* 9368 * Allocate a new dirrem and ACQUIRE_LOCK. 9369 */ 9370 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 9371 pagedep = dirrem->dm_pagedep; 9372 /* 9373 * The possible values for isrmdir: 9374 * 0 - non-directory file rename 9375 * 1 - directory rename within same directory 9376 * inum - directory rename to new directory of given inode number 9377 * When renaming to a new directory, we are both deleting and 9378 * creating a new directory entry, so the link count on the new 9379 * directory should not change. Thus we do not need the followup 9380 * dirrem which is usually done in handle_workitem_remove. We set 9381 * the DIRCHG flag to tell handle_workitem_remove to skip the 9382 * followup dirrem. 9383 */ 9384 if (isrmdir > 1) 9385 dirrem->dm_state |= DIRCHG; 9386 9387 /* 9388 * Whiteouts have no additional dependencies, 9389 * so just put the dirrem on the correct list. 9390 */ 9391 if (newinum == UFS_WINO) { 9392 if ((dirrem->dm_state & COMPLETE) == 0) { 9393 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem, 9394 dm_next); 9395 } else { 9396 dirrem->dm_dirinum = pagedep->pd_ino; 9397 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9398 add_to_worklist(&dirrem->dm_list, 0); 9399 } 9400 FREE_LOCK(ump); 9401 return; 9402 } 9403 /* 9404 * Add the dirrem to the inodedep's pending remove list for quick 9405 * discovery later. A valid nlinkdelta ensures that this lookup 9406 * will not fail. 9407 */ 9408 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9409 panic("softdep_setup_directory_change: Lost inodedep."); 9410 dirrem->dm_state |= ONDEPLIST; 9411 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9412 9413 /* 9414 * If the COMPLETE flag is clear, then there were no active 9415 * entries and we want to roll back to the previous inode until 9416 * the new inode is committed to disk. If the COMPLETE flag is 9417 * set, then we have deleted an entry that never made it to disk. 9418 * If the entry we deleted resulted from a name change, then the old 9419 * inode reference still resides on disk. Any rollback that we do 9420 * needs to be to that old inode (returned to us in prevdirrem). If 9421 * the entry we deleted resulted from a create, then there is 9422 * no entry on the disk, so we want to roll back to zero rather 9423 * than the uncommitted inode. In either of the COMPLETE cases we 9424 * want to immediately free the unwritten and unreferenced inode. 9425 */ 9426 if ((dirrem->dm_state & COMPLETE) == 0) { 9427 dap->da_previous = dirrem; 9428 } else { 9429 if (prevdirrem != NULL) { 9430 dap->da_previous = prevdirrem; 9431 } else { 9432 dap->da_state &= ~DIRCHG; 9433 dap->da_pagedep = pagedep; 9434 } 9435 dirrem->dm_dirinum = pagedep->pd_ino; 9436 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9437 add_to_worklist(&dirrem->dm_list, 0); 9438 } 9439 /* 9440 * Lookup the jaddref for this journal entry. We must finish 9441 * initializing it and make the diradd write dependent on it. 9442 * If we're not journaling, put it on the id_bufwait list if the 9443 * inode is not yet written. If it is written, do the post-inode 9444 * write processing to put it on the id_pendinghd list. 9445 */ 9446 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 9447 if (MOUNTEDSUJ(mp)) { 9448 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 9449 inoreflst); 9450 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 9451 ("softdep_setup_directory_change: bad jaddref %p", 9452 jaddref)); 9453 jaddref->ja_diroff = dp->i_offset; 9454 jaddref->ja_diradd = dap; 9455 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9456 dap, da_pdlist); 9457 add_to_journal(&jaddref->ja_list); 9458 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 9459 dap->da_state |= COMPLETE; 9460 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 9461 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 9462 } else { 9463 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9464 dap, da_pdlist); 9465 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 9466 } 9467 /* 9468 * If we're making a new name for a directory that has not been 9469 * committed when need to move the dot and dotdot references to 9470 * this new name. 9471 */ 9472 if (inodedep->id_mkdiradd && dp->i_offset != DOTDOT_OFFSET) 9473 merge_diradd(inodedep, dap); 9474 FREE_LOCK(ump); 9475 } 9476 9477 /* 9478 * Called whenever the link count on an inode is changed. 9479 * It creates an inode dependency so that the new reference(s) 9480 * to the inode cannot be committed to disk until the updated 9481 * inode has been written. 9482 */ 9483 void 9484 softdep_change_linkcnt(ip) 9485 struct inode *ip; /* the inode with the increased link count */ 9486 { 9487 struct inodedep *inodedep; 9488 struct ufsmount *ump; 9489 9490 ump = ITOUMP(ip); 9491 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9492 ("softdep_change_linkcnt called on non-softdep filesystem")); 9493 ACQUIRE_LOCK(ump); 9494 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep); 9495 if (ip->i_nlink < ip->i_effnlink) 9496 panic("softdep_change_linkcnt: bad delta"); 9497 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9498 FREE_LOCK(ump); 9499 } 9500 9501 /* 9502 * Attach a sbdep dependency to the superblock buf so that we can keep 9503 * track of the head of the linked list of referenced but unlinked inodes. 9504 */ 9505 void 9506 softdep_setup_sbupdate(ump, fs, bp) 9507 struct ufsmount *ump; 9508 struct fs *fs; 9509 struct buf *bp; 9510 { 9511 struct sbdep *sbdep; 9512 struct worklist *wk; 9513 9514 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9515 ("softdep_setup_sbupdate called on non-softdep filesystem")); 9516 LIST_FOREACH(wk, &bp->b_dep, wk_list) 9517 if (wk->wk_type == D_SBDEP) 9518 break; 9519 if (wk != NULL) 9520 return; 9521 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS); 9522 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump)); 9523 sbdep->sb_fs = fs; 9524 sbdep->sb_ump = ump; 9525 ACQUIRE_LOCK(ump); 9526 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list); 9527 FREE_LOCK(ump); 9528 } 9529 9530 /* 9531 * Return the first unlinked inodedep which is ready to be the head of the 9532 * list. The inodedep and all those after it must have valid next pointers. 9533 */ 9534 static struct inodedep * 9535 first_unlinked_inodedep(ump) 9536 struct ufsmount *ump; 9537 { 9538 struct inodedep *inodedep; 9539 struct inodedep *idp; 9540 9541 LOCK_OWNED(ump); 9542 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst); 9543 inodedep; inodedep = idp) { 9544 if ((inodedep->id_state & UNLINKNEXT) == 0) 9545 return (NULL); 9546 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9547 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0) 9548 break; 9549 if ((inodedep->id_state & UNLINKPREV) == 0) 9550 break; 9551 } 9552 return (inodedep); 9553 } 9554 9555 /* 9556 * Set the sujfree unlinked head pointer prior to writing a superblock. 9557 */ 9558 static void 9559 initiate_write_sbdep(sbdep) 9560 struct sbdep *sbdep; 9561 { 9562 struct inodedep *inodedep; 9563 struct fs *bpfs; 9564 struct fs *fs; 9565 9566 bpfs = sbdep->sb_fs; 9567 fs = sbdep->sb_ump->um_fs; 9568 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9569 if (inodedep) { 9570 fs->fs_sujfree = inodedep->id_ino; 9571 inodedep->id_state |= UNLINKPREV; 9572 } else 9573 fs->fs_sujfree = 0; 9574 bpfs->fs_sujfree = fs->fs_sujfree; 9575 /* 9576 * Because we have made changes to the superblock, we need to 9577 * recompute its check-hash. 9578 */ 9579 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs); 9580 } 9581 9582 /* 9583 * After a superblock is written determine whether it must be written again 9584 * due to a changing unlinked list head. 9585 */ 9586 static int 9587 handle_written_sbdep(sbdep, bp) 9588 struct sbdep *sbdep; 9589 struct buf *bp; 9590 { 9591 struct inodedep *inodedep; 9592 struct fs *fs; 9593 9594 LOCK_OWNED(sbdep->sb_ump); 9595 fs = sbdep->sb_fs; 9596 /* 9597 * If the superblock doesn't match the in-memory list start over. 9598 */ 9599 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9600 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) || 9601 (inodedep == NULL && fs->fs_sujfree != 0)) { 9602 bdirty(bp); 9603 return (1); 9604 } 9605 WORKITEM_FREE(sbdep, D_SBDEP); 9606 if (fs->fs_sujfree == 0) 9607 return (0); 9608 /* 9609 * Now that we have a record of this inode in stable store allow it 9610 * to be written to free up pending work. Inodes may see a lot of 9611 * write activity after they are unlinked which we must not hold up. 9612 */ 9613 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 9614 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS) 9615 panic("handle_written_sbdep: Bad inodedep %p (0x%X)", 9616 inodedep, inodedep->id_state); 9617 if (inodedep->id_state & UNLINKONLIST) 9618 break; 9619 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST; 9620 } 9621 9622 return (0); 9623 } 9624 9625 /* 9626 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list. 9627 */ 9628 static void 9629 unlinked_inodedep(mp, inodedep) 9630 struct mount *mp; 9631 struct inodedep *inodedep; 9632 { 9633 struct ufsmount *ump; 9634 9635 ump = VFSTOUFS(mp); 9636 LOCK_OWNED(ump); 9637 if (MOUNTEDSUJ(mp) == 0) 9638 return; 9639 ump->um_fs->fs_fmod = 1; 9640 if (inodedep->id_state & UNLINKED) 9641 panic("unlinked_inodedep: %p already unlinked\n", inodedep); 9642 inodedep->id_state |= UNLINKED; 9643 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked); 9644 } 9645 9646 /* 9647 * Remove an inodedep from the unlinked inodedep list. This may require 9648 * disk writes if the inode has made it that far. 9649 */ 9650 static void 9651 clear_unlinked_inodedep(inodedep) 9652 struct inodedep *inodedep; 9653 { 9654 struct ufs2_dinode *dip; 9655 struct ufsmount *ump; 9656 struct inodedep *idp; 9657 struct inodedep *idn; 9658 struct fs *fs, *bpfs; 9659 struct buf *bp; 9660 ino_t ino; 9661 ino_t nino; 9662 ino_t pino; 9663 int error; 9664 9665 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9666 fs = ump->um_fs; 9667 ino = inodedep->id_ino; 9668 error = 0; 9669 for (;;) { 9670 LOCK_OWNED(ump); 9671 KASSERT((inodedep->id_state & UNLINKED) != 0, 9672 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9673 inodedep)); 9674 /* 9675 * If nothing has yet been written simply remove us from 9676 * the in memory list and return. This is the most common 9677 * case where handle_workitem_remove() loses the final 9678 * reference. 9679 */ 9680 if ((inodedep->id_state & UNLINKLINKS) == 0) 9681 break; 9682 /* 9683 * If we have a NEXT pointer and no PREV pointer we can simply 9684 * clear NEXT's PREV and remove ourselves from the list. Be 9685 * careful not to clear PREV if the superblock points at 9686 * next as well. 9687 */ 9688 idn = TAILQ_NEXT(inodedep, id_unlinked); 9689 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) { 9690 if (idn && fs->fs_sujfree != idn->id_ino) 9691 idn->id_state &= ~UNLINKPREV; 9692 break; 9693 } 9694 /* 9695 * Here we have an inodedep which is actually linked into 9696 * the list. We must remove it by forcing a write to the 9697 * link before us, whether it be the superblock or an inode. 9698 * Unfortunately the list may change while we're waiting 9699 * on the buf lock for either resource so we must loop until 9700 * we lock the right one. If both the superblock and an 9701 * inode point to this inode we must clear the inode first 9702 * followed by the superblock. 9703 */ 9704 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9705 pino = 0; 9706 if (idp && (idp->id_state & UNLINKNEXT)) 9707 pino = idp->id_ino; 9708 FREE_LOCK(ump); 9709 if (pino == 0) { 9710 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9711 (int)fs->fs_sbsize, 0, 0, 0); 9712 } else { 9713 error = bread(ump->um_devvp, 9714 fsbtodb(fs, ino_to_fsba(fs, pino)), 9715 (int)fs->fs_bsize, NOCRED, &bp); 9716 if (error) 9717 brelse(bp); 9718 } 9719 ACQUIRE_LOCK(ump); 9720 if (error) 9721 break; 9722 /* If the list has changed restart the loop. */ 9723 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9724 nino = 0; 9725 if (idp && (idp->id_state & UNLINKNEXT)) 9726 nino = idp->id_ino; 9727 if (nino != pino || 9728 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) { 9729 FREE_LOCK(ump); 9730 brelse(bp); 9731 ACQUIRE_LOCK(ump); 9732 continue; 9733 } 9734 nino = 0; 9735 idn = TAILQ_NEXT(inodedep, id_unlinked); 9736 if (idn) 9737 nino = idn->id_ino; 9738 /* 9739 * Remove us from the in memory list. After this we cannot 9740 * access the inodedep. 9741 */ 9742 KASSERT((inodedep->id_state & UNLINKED) != 0, 9743 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9744 inodedep)); 9745 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9746 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9747 FREE_LOCK(ump); 9748 /* 9749 * The predecessor's next pointer is manually updated here 9750 * so that the NEXT flag is never cleared for an element 9751 * that is in the list. 9752 */ 9753 if (pino == 0) { 9754 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 9755 bpfs = (struct fs *)bp->b_data; 9756 ffs_oldfscompat_write(bpfs, ump); 9757 softdep_setup_sbupdate(ump, bpfs, bp); 9758 /* 9759 * Because we may have made changes to the superblock, 9760 * we need to recompute its check-hash. 9761 */ 9762 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs); 9763 } else if (fs->fs_magic == FS_UFS1_MAGIC) { 9764 ((struct ufs1_dinode *)bp->b_data + 9765 ino_to_fsbo(fs, pino))->di_freelink = nino; 9766 } else { 9767 dip = (struct ufs2_dinode *)bp->b_data + 9768 ino_to_fsbo(fs, pino); 9769 dip->di_freelink = nino; 9770 ffs_update_dinode_ckhash(fs, dip); 9771 } 9772 /* 9773 * If the bwrite fails we have no recourse to recover. The 9774 * filesystem is corrupted already. 9775 */ 9776 bwrite(bp); 9777 ACQUIRE_LOCK(ump); 9778 /* 9779 * If the superblock pointer still needs to be cleared force 9780 * a write here. 9781 */ 9782 if (fs->fs_sujfree == ino) { 9783 FREE_LOCK(ump); 9784 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9785 (int)fs->fs_sbsize, 0, 0, 0); 9786 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 9787 bpfs = (struct fs *)bp->b_data; 9788 ffs_oldfscompat_write(bpfs, ump); 9789 softdep_setup_sbupdate(ump, bpfs, bp); 9790 /* 9791 * Because we may have made changes to the superblock, 9792 * we need to recompute its check-hash. 9793 */ 9794 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs); 9795 bwrite(bp); 9796 ACQUIRE_LOCK(ump); 9797 } 9798 9799 if (fs->fs_sujfree != ino) 9800 return; 9801 panic("clear_unlinked_inodedep: Failed to clear free head"); 9802 } 9803 if (inodedep->id_ino == fs->fs_sujfree) 9804 panic("clear_unlinked_inodedep: Freeing head of free list"); 9805 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9806 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9807 return; 9808 } 9809 9810 /* 9811 * This workitem decrements the inode's link count. 9812 * If the link count reaches zero, the file is removed. 9813 */ 9814 static int 9815 handle_workitem_remove(dirrem, flags) 9816 struct dirrem *dirrem; 9817 int flags; 9818 { 9819 struct inodedep *inodedep; 9820 struct workhead dotdotwk; 9821 struct worklist *wk; 9822 struct ufsmount *ump; 9823 struct mount *mp; 9824 struct vnode *vp; 9825 struct inode *ip; 9826 ino_t oldinum; 9827 9828 if (dirrem->dm_state & ONWORKLIST) 9829 panic("handle_workitem_remove: dirrem %p still on worklist", 9830 dirrem); 9831 oldinum = dirrem->dm_oldinum; 9832 mp = dirrem->dm_list.wk_mp; 9833 ump = VFSTOUFS(mp); 9834 flags |= LK_EXCLUSIVE; 9835 if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ) != 0) 9836 return (EBUSY); 9837 ip = VTOI(vp); 9838 ACQUIRE_LOCK(ump); 9839 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0) 9840 panic("handle_workitem_remove: lost inodedep"); 9841 if (dirrem->dm_state & ONDEPLIST) 9842 LIST_REMOVE(dirrem, dm_inonext); 9843 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 9844 ("handle_workitem_remove: Journal entries not written.")); 9845 9846 /* 9847 * Move all dependencies waiting on the remove to complete 9848 * from the dirrem to the inode inowait list to be completed 9849 * after the inode has been updated and written to disk. Any 9850 * marked MKDIR_PARENT are saved to be completed when the .. ref 9851 * is removed. 9852 */ 9853 LIST_INIT(&dotdotwk); 9854 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) { 9855 WORKLIST_REMOVE(wk); 9856 if (wk->wk_state & MKDIR_PARENT) { 9857 wk->wk_state &= ~MKDIR_PARENT; 9858 WORKLIST_INSERT(&dotdotwk, wk); 9859 continue; 9860 } 9861 WORKLIST_INSERT(&inodedep->id_inowait, wk); 9862 } 9863 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list); 9864 /* 9865 * Normal file deletion. 9866 */ 9867 if ((dirrem->dm_state & RMDIR) == 0) { 9868 ip->i_nlink--; 9869 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: file ino " 9870 "%ju negative i_nlink %d", (intmax_t)ip->i_number, 9871 ip->i_nlink)); 9872 DIP_SET(ip, i_nlink, ip->i_nlink); 9873 ip->i_flag |= IN_CHANGE; 9874 if (ip->i_nlink < ip->i_effnlink) 9875 panic("handle_workitem_remove: bad file delta"); 9876 if (ip->i_nlink == 0) 9877 unlinked_inodedep(mp, inodedep); 9878 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9879 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 9880 ("handle_workitem_remove: worklist not empty. %s", 9881 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type))); 9882 WORKITEM_FREE(dirrem, D_DIRREM); 9883 FREE_LOCK(ump); 9884 goto out; 9885 } 9886 /* 9887 * Directory deletion. Decrement reference count for both the 9888 * just deleted parent directory entry and the reference for ".". 9889 * Arrange to have the reference count on the parent decremented 9890 * to account for the loss of "..". 9891 */ 9892 ip->i_nlink -= 2; 9893 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: directory ino " 9894 "%ju negative i_nlink %d", (intmax_t)ip->i_number, ip->i_nlink)); 9895 DIP_SET(ip, i_nlink, ip->i_nlink); 9896 ip->i_flag |= IN_CHANGE; 9897 if (ip->i_nlink < ip->i_effnlink) 9898 panic("handle_workitem_remove: bad dir delta"); 9899 if (ip->i_nlink == 0) 9900 unlinked_inodedep(mp, inodedep); 9901 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9902 /* 9903 * Rename a directory to a new parent. Since, we are both deleting 9904 * and creating a new directory entry, the link count on the new 9905 * directory should not change. Thus we skip the followup dirrem. 9906 */ 9907 if (dirrem->dm_state & DIRCHG) { 9908 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 9909 ("handle_workitem_remove: DIRCHG and worklist not empty.")); 9910 WORKITEM_FREE(dirrem, D_DIRREM); 9911 FREE_LOCK(ump); 9912 goto out; 9913 } 9914 dirrem->dm_state = ONDEPLIST; 9915 dirrem->dm_oldinum = dirrem->dm_dirinum; 9916 /* 9917 * Place the dirrem on the parent's diremhd list. 9918 */ 9919 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0) 9920 panic("handle_workitem_remove: lost dir inodedep"); 9921 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9922 /* 9923 * If the allocated inode has never been written to disk, then 9924 * the on-disk inode is zero'ed and we can remove the file 9925 * immediately. When journaling if the inode has been marked 9926 * unlinked and not DEPCOMPLETE we know it can never be written. 9927 */ 9928 inodedep_lookup(mp, oldinum, 0, &inodedep); 9929 if (inodedep == NULL || 9930 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED || 9931 check_inode_unwritten(inodedep)) { 9932 FREE_LOCK(ump); 9933 vput(vp); 9934 return handle_workitem_remove(dirrem, flags); 9935 } 9936 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list); 9937 FREE_LOCK(ump); 9938 ip->i_flag |= IN_CHANGE; 9939 out: 9940 ffs_update(vp, 0); 9941 vput(vp); 9942 return (0); 9943 } 9944 9945 /* 9946 * Inode de-allocation dependencies. 9947 * 9948 * When an inode's link count is reduced to zero, it can be de-allocated. We 9949 * found it convenient to postpone de-allocation until after the inode is 9950 * written to disk with its new link count (zero). At this point, all of the 9951 * on-disk inode's block pointers are nullified and, with careful dependency 9952 * list ordering, all dependencies related to the inode will be satisfied and 9953 * the corresponding dependency structures de-allocated. So, if/when the 9954 * inode is reused, there will be no mixing of old dependencies with new 9955 * ones. This artificial dependency is set up by the block de-allocation 9956 * procedure above (softdep_setup_freeblocks) and completed by the 9957 * following procedure. 9958 */ 9959 static void 9960 handle_workitem_freefile(freefile) 9961 struct freefile *freefile; 9962 { 9963 struct workhead wkhd; 9964 struct fs *fs; 9965 struct ufsmount *ump; 9966 int error; 9967 #ifdef INVARIANTS 9968 struct inodedep *idp; 9969 #endif 9970 9971 ump = VFSTOUFS(freefile->fx_list.wk_mp); 9972 fs = ump->um_fs; 9973 #ifdef INVARIANTS 9974 ACQUIRE_LOCK(ump); 9975 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp); 9976 FREE_LOCK(ump); 9977 if (error) 9978 panic("handle_workitem_freefile: inodedep %p survived", idp); 9979 #endif 9980 UFS_LOCK(ump); 9981 fs->fs_pendinginodes -= 1; 9982 UFS_UNLOCK(ump); 9983 LIST_INIT(&wkhd); 9984 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list); 9985 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp, 9986 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0) 9987 softdep_error("handle_workitem_freefile", error); 9988 ACQUIRE_LOCK(ump); 9989 WORKITEM_FREE(freefile, D_FREEFILE); 9990 FREE_LOCK(ump); 9991 } 9992 9993 9994 /* 9995 * Helper function which unlinks marker element from work list and returns 9996 * the next element on the list. 9997 */ 9998 static __inline struct worklist * 9999 markernext(struct worklist *marker) 10000 { 10001 struct worklist *next; 10002 10003 next = LIST_NEXT(marker, wk_list); 10004 LIST_REMOVE(marker, wk_list); 10005 return next; 10006 } 10007 10008 /* 10009 * Disk writes. 10010 * 10011 * The dependency structures constructed above are most actively used when file 10012 * system blocks are written to disk. No constraints are placed on when a 10013 * block can be written, but unsatisfied update dependencies are made safe by 10014 * modifying (or replacing) the source memory for the duration of the disk 10015 * write. When the disk write completes, the memory block is again brought 10016 * up-to-date. 10017 * 10018 * In-core inode structure reclamation. 10019 * 10020 * Because there are a finite number of "in-core" inode structures, they are 10021 * reused regularly. By transferring all inode-related dependencies to the 10022 * in-memory inode block and indexing them separately (via "inodedep"s), we 10023 * can allow "in-core" inode structures to be reused at any time and avoid 10024 * any increase in contention. 10025 * 10026 * Called just before entering the device driver to initiate a new disk I/O. 10027 * The buffer must be locked, thus, no I/O completion operations can occur 10028 * while we are manipulating its associated dependencies. 10029 */ 10030 static void 10031 softdep_disk_io_initiation(bp) 10032 struct buf *bp; /* structure describing disk write to occur */ 10033 { 10034 struct worklist *wk; 10035 struct worklist marker; 10036 struct inodedep *inodedep; 10037 struct freeblks *freeblks; 10038 struct jblkdep *jblkdep; 10039 struct newblk *newblk; 10040 struct ufsmount *ump; 10041 10042 /* 10043 * We only care about write operations. There should never 10044 * be dependencies for reads. 10045 */ 10046 if (bp->b_iocmd != BIO_WRITE) 10047 panic("softdep_disk_io_initiation: not write"); 10048 10049 if (bp->b_vflags & BV_BKGRDINPROG) 10050 panic("softdep_disk_io_initiation: Writing buffer with " 10051 "background write in progress: %p", bp); 10052 10053 ump = softdep_bp_to_mp(bp); 10054 if (ump == NULL) 10055 return; 10056 10057 marker.wk_type = D_LAST + 1; /* Not a normal workitem */ 10058 PHOLD(curproc); /* Don't swap out kernel stack */ 10059 ACQUIRE_LOCK(ump); 10060 /* 10061 * Do any necessary pre-I/O processing. 10062 */ 10063 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL; 10064 wk = markernext(&marker)) { 10065 LIST_INSERT_AFTER(wk, &marker, wk_list); 10066 switch (wk->wk_type) { 10067 10068 case D_PAGEDEP: 10069 initiate_write_filepage(WK_PAGEDEP(wk), bp); 10070 continue; 10071 10072 case D_INODEDEP: 10073 inodedep = WK_INODEDEP(wk); 10074 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) 10075 initiate_write_inodeblock_ufs1(inodedep, bp); 10076 else 10077 initiate_write_inodeblock_ufs2(inodedep, bp); 10078 continue; 10079 10080 case D_INDIRDEP: 10081 initiate_write_indirdep(WK_INDIRDEP(wk), bp); 10082 continue; 10083 10084 case D_BMSAFEMAP: 10085 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp); 10086 continue; 10087 10088 case D_JSEG: 10089 WK_JSEG(wk)->js_buf = NULL; 10090 continue; 10091 10092 case D_FREEBLKS: 10093 freeblks = WK_FREEBLKS(wk); 10094 jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd); 10095 /* 10096 * We have to wait for the freeblks to be journaled 10097 * before we can write an inodeblock with updated 10098 * pointers. Be careful to arrange the marker so 10099 * we revisit the freeblks if it's not removed by 10100 * the first jwait(). 10101 */ 10102 if (jblkdep != NULL) { 10103 LIST_REMOVE(&marker, wk_list); 10104 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10105 jwait(&jblkdep->jb_list, MNT_WAIT); 10106 } 10107 continue; 10108 case D_ALLOCDIRECT: 10109 case D_ALLOCINDIR: 10110 /* 10111 * We have to wait for the jnewblk to be journaled 10112 * before we can write to a block if the contents 10113 * may be confused with an earlier file's indirect 10114 * at recovery time. Handle the marker as described 10115 * above. 10116 */ 10117 newblk = WK_NEWBLK(wk); 10118 if (newblk->nb_jnewblk != NULL && 10119 indirblk_lookup(newblk->nb_list.wk_mp, 10120 newblk->nb_newblkno)) { 10121 LIST_REMOVE(&marker, wk_list); 10122 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10123 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 10124 } 10125 continue; 10126 10127 case D_SBDEP: 10128 initiate_write_sbdep(WK_SBDEP(wk)); 10129 continue; 10130 10131 case D_MKDIR: 10132 case D_FREEWORK: 10133 case D_FREEDEP: 10134 case D_JSEGDEP: 10135 continue; 10136 10137 default: 10138 panic("handle_disk_io_initiation: Unexpected type %s", 10139 TYPENAME(wk->wk_type)); 10140 /* NOTREACHED */ 10141 } 10142 } 10143 FREE_LOCK(ump); 10144 PRELE(curproc); /* Allow swapout of kernel stack */ 10145 } 10146 10147 /* 10148 * Called from within the procedure above to deal with unsatisfied 10149 * allocation dependencies in a directory. The buffer must be locked, 10150 * thus, no I/O completion operations can occur while we are 10151 * manipulating its associated dependencies. 10152 */ 10153 static void 10154 initiate_write_filepage(pagedep, bp) 10155 struct pagedep *pagedep; 10156 struct buf *bp; 10157 { 10158 struct jremref *jremref; 10159 struct jmvref *jmvref; 10160 struct dirrem *dirrem; 10161 struct diradd *dap; 10162 struct direct *ep; 10163 int i; 10164 10165 if (pagedep->pd_state & IOSTARTED) { 10166 /* 10167 * This can only happen if there is a driver that does not 10168 * understand chaining. Here biodone will reissue the call 10169 * to strategy for the incomplete buffers. 10170 */ 10171 printf("initiate_write_filepage: already started\n"); 10172 return; 10173 } 10174 pagedep->pd_state |= IOSTARTED; 10175 /* 10176 * Wait for all journal remove dependencies to hit the disk. 10177 * We can not allow any potentially conflicting directory adds 10178 * to be visible before removes and rollback is too difficult. 10179 * The per-filesystem lock may be dropped and re-acquired, however 10180 * we hold the buf locked so the dependency can not go away. 10181 */ 10182 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) 10183 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) 10184 jwait(&jremref->jr_list, MNT_WAIT); 10185 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) 10186 jwait(&jmvref->jm_list, MNT_WAIT); 10187 for (i = 0; i < DAHASHSZ; i++) { 10188 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 10189 ep = (struct direct *) 10190 ((char *)bp->b_data + dap->da_offset); 10191 if (ep->d_ino != dap->da_newinum) 10192 panic("%s: dir inum %ju != new %ju", 10193 "initiate_write_filepage", 10194 (uintmax_t)ep->d_ino, 10195 (uintmax_t)dap->da_newinum); 10196 if (dap->da_state & DIRCHG) 10197 ep->d_ino = dap->da_previous->dm_oldinum; 10198 else 10199 ep->d_ino = 0; 10200 dap->da_state &= ~ATTACHED; 10201 dap->da_state |= UNDONE; 10202 } 10203 } 10204 } 10205 10206 /* 10207 * Version of initiate_write_inodeblock that handles UFS1 dinodes. 10208 * Note that any bug fixes made to this routine must be done in the 10209 * version found below. 10210 * 10211 * Called from within the procedure above to deal with unsatisfied 10212 * allocation dependencies in an inodeblock. The buffer must be 10213 * locked, thus, no I/O completion operations can occur while we 10214 * are manipulating its associated dependencies. 10215 */ 10216 static void 10217 initiate_write_inodeblock_ufs1(inodedep, bp) 10218 struct inodedep *inodedep; 10219 struct buf *bp; /* The inode block */ 10220 { 10221 struct allocdirect *adp, *lastadp; 10222 struct ufs1_dinode *dp; 10223 struct ufs1_dinode *sip; 10224 struct inoref *inoref; 10225 struct ufsmount *ump; 10226 struct fs *fs; 10227 ufs_lbn_t i; 10228 #ifdef INVARIANTS 10229 ufs_lbn_t prevlbn = 0; 10230 #endif 10231 int deplist; 10232 10233 if (inodedep->id_state & IOSTARTED) 10234 panic("initiate_write_inodeblock_ufs1: already started"); 10235 inodedep->id_state |= IOSTARTED; 10236 fs = inodedep->id_fs; 10237 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10238 LOCK_OWNED(ump); 10239 dp = (struct ufs1_dinode *)bp->b_data + 10240 ino_to_fsbo(fs, inodedep->id_ino); 10241 10242 /* 10243 * If we're on the unlinked list but have not yet written our 10244 * next pointer initialize it here. 10245 */ 10246 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10247 struct inodedep *inon; 10248 10249 inon = TAILQ_NEXT(inodedep, id_unlinked); 10250 dp->di_freelink = inon ? inon->id_ino : 0; 10251 } 10252 /* 10253 * If the bitmap is not yet written, then the allocated 10254 * inode cannot be written to disk. 10255 */ 10256 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10257 if (inodedep->id_savedino1 != NULL) 10258 panic("initiate_write_inodeblock_ufs1: I/O underway"); 10259 FREE_LOCK(ump); 10260 sip = malloc(sizeof(struct ufs1_dinode), 10261 M_SAVEDINO, M_SOFTDEP_FLAGS); 10262 ACQUIRE_LOCK(ump); 10263 inodedep->id_savedino1 = sip; 10264 *inodedep->id_savedino1 = *dp; 10265 bzero((caddr_t)dp, sizeof(struct ufs1_dinode)); 10266 dp->di_gen = inodedep->id_savedino1->di_gen; 10267 dp->di_freelink = inodedep->id_savedino1->di_freelink; 10268 return; 10269 } 10270 /* 10271 * If no dependencies, then there is nothing to roll back. 10272 */ 10273 inodedep->id_savedsize = dp->di_size; 10274 inodedep->id_savedextsize = 0; 10275 inodedep->id_savednlink = dp->di_nlink; 10276 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10277 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10278 return; 10279 /* 10280 * Revert the link count to that of the first unwritten journal entry. 10281 */ 10282 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10283 if (inoref) 10284 dp->di_nlink = inoref->if_nlink; 10285 /* 10286 * Set the dependencies to busy. 10287 */ 10288 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10289 adp = TAILQ_NEXT(adp, ad_next)) { 10290 #ifdef INVARIANTS 10291 if (deplist != 0 && prevlbn >= adp->ad_offset) 10292 panic("softdep_write_inodeblock: lbn order"); 10293 prevlbn = adp->ad_offset; 10294 if (adp->ad_offset < UFS_NDADDR && 10295 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10296 panic("initiate_write_inodeblock_ufs1: " 10297 "direct pointer #%jd mismatch %d != %jd", 10298 (intmax_t)adp->ad_offset, 10299 dp->di_db[adp->ad_offset], 10300 (intmax_t)adp->ad_newblkno); 10301 if (adp->ad_offset >= UFS_NDADDR && 10302 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno) 10303 panic("initiate_write_inodeblock_ufs1: " 10304 "indirect pointer #%jd mismatch %d != %jd", 10305 (intmax_t)adp->ad_offset - UFS_NDADDR, 10306 dp->di_ib[adp->ad_offset - UFS_NDADDR], 10307 (intmax_t)adp->ad_newblkno); 10308 deplist |= 1 << adp->ad_offset; 10309 if ((adp->ad_state & ATTACHED) == 0) 10310 panic("initiate_write_inodeblock_ufs1: " 10311 "Unknown state 0x%x", adp->ad_state); 10312 #endif /* INVARIANTS */ 10313 adp->ad_state &= ~ATTACHED; 10314 adp->ad_state |= UNDONE; 10315 } 10316 /* 10317 * The on-disk inode cannot claim to be any larger than the last 10318 * fragment that has been written. Otherwise, the on-disk inode 10319 * might have fragments that were not the last block in the file 10320 * which would corrupt the filesystem. 10321 */ 10322 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10323 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10324 if (adp->ad_offset >= UFS_NDADDR) 10325 break; 10326 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10327 /* keep going until hitting a rollback to a frag */ 10328 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10329 continue; 10330 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10331 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) { 10332 #ifdef INVARIANTS 10333 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10334 panic("initiate_write_inodeblock_ufs1: " 10335 "lost dep1"); 10336 #endif /* INVARIANTS */ 10337 dp->di_db[i] = 0; 10338 } 10339 for (i = 0; i < UFS_NIADDR; i++) { 10340 #ifdef INVARIANTS 10341 if (dp->di_ib[i] != 0 && 10342 (deplist & ((1 << UFS_NDADDR) << i)) == 0) 10343 panic("initiate_write_inodeblock_ufs1: " 10344 "lost dep2"); 10345 #endif /* INVARIANTS */ 10346 dp->di_ib[i] = 0; 10347 } 10348 return; 10349 } 10350 /* 10351 * If we have zero'ed out the last allocated block of the file, 10352 * roll back the size to the last currently allocated block. 10353 * We know that this last allocated block is a full-sized as 10354 * we already checked for fragments in the loop above. 10355 */ 10356 if (lastadp != NULL && 10357 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10358 for (i = lastadp->ad_offset; i >= 0; i--) 10359 if (dp->di_db[i] != 0) 10360 break; 10361 dp->di_size = (i + 1) * fs->fs_bsize; 10362 } 10363 /* 10364 * The only dependencies are for indirect blocks. 10365 * 10366 * The file size for indirect block additions is not guaranteed. 10367 * Such a guarantee would be non-trivial to achieve. The conventional 10368 * synchronous write implementation also does not make this guarantee. 10369 * Fsck should catch and fix discrepancies. Arguably, the file size 10370 * can be over-estimated without destroying integrity when the file 10371 * moves into the indirect blocks (i.e., is large). If we want to 10372 * postpone fsck, we are stuck with this argument. 10373 */ 10374 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10375 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0; 10376 } 10377 10378 /* 10379 * Version of initiate_write_inodeblock that handles UFS2 dinodes. 10380 * Note that any bug fixes made to this routine must be done in the 10381 * version found above. 10382 * 10383 * Called from within the procedure above to deal with unsatisfied 10384 * allocation dependencies in an inodeblock. The buffer must be 10385 * locked, thus, no I/O completion operations can occur while we 10386 * are manipulating its associated dependencies. 10387 */ 10388 static void 10389 initiate_write_inodeblock_ufs2(inodedep, bp) 10390 struct inodedep *inodedep; 10391 struct buf *bp; /* The inode block */ 10392 { 10393 struct allocdirect *adp, *lastadp; 10394 struct ufs2_dinode *dp; 10395 struct ufs2_dinode *sip; 10396 struct inoref *inoref; 10397 struct ufsmount *ump; 10398 struct fs *fs; 10399 ufs_lbn_t i; 10400 #ifdef INVARIANTS 10401 ufs_lbn_t prevlbn = 0; 10402 #endif 10403 int deplist; 10404 10405 if (inodedep->id_state & IOSTARTED) 10406 panic("initiate_write_inodeblock_ufs2: already started"); 10407 inodedep->id_state |= IOSTARTED; 10408 fs = inodedep->id_fs; 10409 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10410 LOCK_OWNED(ump); 10411 dp = (struct ufs2_dinode *)bp->b_data + 10412 ino_to_fsbo(fs, inodedep->id_ino); 10413 10414 /* 10415 * If we're on the unlinked list but have not yet written our 10416 * next pointer initialize it here. 10417 */ 10418 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10419 struct inodedep *inon; 10420 10421 inon = TAILQ_NEXT(inodedep, id_unlinked); 10422 dp->di_freelink = inon ? inon->id_ino : 0; 10423 ffs_update_dinode_ckhash(fs, dp); 10424 } 10425 /* 10426 * If the bitmap is not yet written, then the allocated 10427 * inode cannot be written to disk. 10428 */ 10429 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10430 if (inodedep->id_savedino2 != NULL) 10431 panic("initiate_write_inodeblock_ufs2: I/O underway"); 10432 FREE_LOCK(ump); 10433 sip = malloc(sizeof(struct ufs2_dinode), 10434 M_SAVEDINO, M_SOFTDEP_FLAGS); 10435 ACQUIRE_LOCK(ump); 10436 inodedep->id_savedino2 = sip; 10437 *inodedep->id_savedino2 = *dp; 10438 bzero((caddr_t)dp, sizeof(struct ufs2_dinode)); 10439 dp->di_gen = inodedep->id_savedino2->di_gen; 10440 dp->di_freelink = inodedep->id_savedino2->di_freelink; 10441 return; 10442 } 10443 /* 10444 * If no dependencies, then there is nothing to roll back. 10445 */ 10446 inodedep->id_savedsize = dp->di_size; 10447 inodedep->id_savedextsize = dp->di_extsize; 10448 inodedep->id_savednlink = dp->di_nlink; 10449 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10450 TAILQ_EMPTY(&inodedep->id_extupdt) && 10451 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10452 return; 10453 /* 10454 * Revert the link count to that of the first unwritten journal entry. 10455 */ 10456 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10457 if (inoref) 10458 dp->di_nlink = inoref->if_nlink; 10459 10460 /* 10461 * Set the ext data dependencies to busy. 10462 */ 10463 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10464 adp = TAILQ_NEXT(adp, ad_next)) { 10465 #ifdef INVARIANTS 10466 if (deplist != 0 && prevlbn >= adp->ad_offset) 10467 panic("initiate_write_inodeblock_ufs2: lbn order"); 10468 prevlbn = adp->ad_offset; 10469 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno) 10470 panic("initiate_write_inodeblock_ufs2: " 10471 "ext pointer #%jd mismatch %jd != %jd", 10472 (intmax_t)adp->ad_offset, 10473 (intmax_t)dp->di_extb[adp->ad_offset], 10474 (intmax_t)adp->ad_newblkno); 10475 deplist |= 1 << adp->ad_offset; 10476 if ((adp->ad_state & ATTACHED) == 0) 10477 panic("initiate_write_inodeblock_ufs2: Unknown " 10478 "state 0x%x", adp->ad_state); 10479 #endif /* INVARIANTS */ 10480 adp->ad_state &= ~ATTACHED; 10481 adp->ad_state |= UNDONE; 10482 } 10483 /* 10484 * The on-disk inode cannot claim to be any larger than the last 10485 * fragment that has been written. Otherwise, the on-disk inode 10486 * might have fragments that were not the last block in the ext 10487 * data which would corrupt the filesystem. 10488 */ 10489 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10490 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10491 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno; 10492 /* keep going until hitting a rollback to a frag */ 10493 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10494 continue; 10495 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10496 for (i = adp->ad_offset + 1; i < UFS_NXADDR; i++) { 10497 #ifdef INVARIANTS 10498 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0) 10499 panic("initiate_write_inodeblock_ufs2: " 10500 "lost dep1"); 10501 #endif /* INVARIANTS */ 10502 dp->di_extb[i] = 0; 10503 } 10504 lastadp = NULL; 10505 break; 10506 } 10507 /* 10508 * If we have zero'ed out the last allocated block of the ext 10509 * data, roll back the size to the last currently allocated block. 10510 * We know that this last allocated block is a full-sized as 10511 * we already checked for fragments in the loop above. 10512 */ 10513 if (lastadp != NULL && 10514 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10515 for (i = lastadp->ad_offset; i >= 0; i--) 10516 if (dp->di_extb[i] != 0) 10517 break; 10518 dp->di_extsize = (i + 1) * fs->fs_bsize; 10519 } 10520 /* 10521 * Set the file data dependencies to busy. 10522 */ 10523 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10524 adp = TAILQ_NEXT(adp, ad_next)) { 10525 #ifdef INVARIANTS 10526 if (deplist != 0 && prevlbn >= adp->ad_offset) 10527 panic("softdep_write_inodeblock: lbn order"); 10528 if ((adp->ad_state & ATTACHED) == 0) 10529 panic("inodedep %p and adp %p not attached", inodedep, adp); 10530 prevlbn = adp->ad_offset; 10531 if (adp->ad_offset < UFS_NDADDR && 10532 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10533 panic("initiate_write_inodeblock_ufs2: " 10534 "direct pointer #%jd mismatch %jd != %jd", 10535 (intmax_t)adp->ad_offset, 10536 (intmax_t)dp->di_db[adp->ad_offset], 10537 (intmax_t)adp->ad_newblkno); 10538 if (adp->ad_offset >= UFS_NDADDR && 10539 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno) 10540 panic("initiate_write_inodeblock_ufs2: " 10541 "indirect pointer #%jd mismatch %jd != %jd", 10542 (intmax_t)adp->ad_offset - UFS_NDADDR, 10543 (intmax_t)dp->di_ib[adp->ad_offset - UFS_NDADDR], 10544 (intmax_t)adp->ad_newblkno); 10545 deplist |= 1 << adp->ad_offset; 10546 if ((adp->ad_state & ATTACHED) == 0) 10547 panic("initiate_write_inodeblock_ufs2: Unknown " 10548 "state 0x%x", adp->ad_state); 10549 #endif /* INVARIANTS */ 10550 adp->ad_state &= ~ATTACHED; 10551 adp->ad_state |= UNDONE; 10552 } 10553 /* 10554 * The on-disk inode cannot claim to be any larger than the last 10555 * fragment that has been written. Otherwise, the on-disk inode 10556 * might have fragments that were not the last block in the file 10557 * which would corrupt the filesystem. 10558 */ 10559 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10560 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10561 if (adp->ad_offset >= UFS_NDADDR) 10562 break; 10563 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10564 /* keep going until hitting a rollback to a frag */ 10565 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10566 continue; 10567 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10568 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) { 10569 #ifdef INVARIANTS 10570 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10571 panic("initiate_write_inodeblock_ufs2: " 10572 "lost dep2"); 10573 #endif /* INVARIANTS */ 10574 dp->di_db[i] = 0; 10575 } 10576 for (i = 0; i < UFS_NIADDR; i++) { 10577 #ifdef INVARIANTS 10578 if (dp->di_ib[i] != 0 && 10579 (deplist & ((1 << UFS_NDADDR) << i)) == 0) 10580 panic("initiate_write_inodeblock_ufs2: " 10581 "lost dep3"); 10582 #endif /* INVARIANTS */ 10583 dp->di_ib[i] = 0; 10584 } 10585 ffs_update_dinode_ckhash(fs, dp); 10586 return; 10587 } 10588 /* 10589 * If we have zero'ed out the last allocated block of the file, 10590 * roll back the size to the last currently allocated block. 10591 * We know that this last allocated block is a full-sized as 10592 * we already checked for fragments in the loop above. 10593 */ 10594 if (lastadp != NULL && 10595 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10596 for (i = lastadp->ad_offset; i >= 0; i--) 10597 if (dp->di_db[i] != 0) 10598 break; 10599 dp->di_size = (i + 1) * fs->fs_bsize; 10600 } 10601 /* 10602 * The only dependencies are for indirect blocks. 10603 * 10604 * The file size for indirect block additions is not guaranteed. 10605 * Such a guarantee would be non-trivial to achieve. The conventional 10606 * synchronous write implementation also does not make this guarantee. 10607 * Fsck should catch and fix discrepancies. Arguably, the file size 10608 * can be over-estimated without destroying integrity when the file 10609 * moves into the indirect blocks (i.e., is large). If we want to 10610 * postpone fsck, we are stuck with this argument. 10611 */ 10612 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10613 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0; 10614 ffs_update_dinode_ckhash(fs, dp); 10615 } 10616 10617 /* 10618 * Cancel an indirdep as a result of truncation. Release all of the 10619 * children allocindirs and place their journal work on the appropriate 10620 * list. 10621 */ 10622 static void 10623 cancel_indirdep(indirdep, bp, freeblks) 10624 struct indirdep *indirdep; 10625 struct buf *bp; 10626 struct freeblks *freeblks; 10627 { 10628 struct allocindir *aip; 10629 10630 /* 10631 * None of the indirect pointers will ever be visible, 10632 * so they can simply be tossed. GOINGAWAY ensures 10633 * that allocated pointers will be saved in the buffer 10634 * cache until they are freed. Note that they will 10635 * only be able to be found by their physical address 10636 * since the inode mapping the logical address will 10637 * be gone. The save buffer used for the safe copy 10638 * was allocated in setup_allocindir_phase2 using 10639 * the physical address so it could be used for this 10640 * purpose. Hence we swap the safe copy with the real 10641 * copy, allowing the safe copy to be freed and holding 10642 * on to the real copy for later use in indir_trunc. 10643 */ 10644 if (indirdep->ir_state & GOINGAWAY) 10645 panic("cancel_indirdep: already gone"); 10646 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 10647 indirdep->ir_state |= DEPCOMPLETE; 10648 LIST_REMOVE(indirdep, ir_next); 10649 } 10650 indirdep->ir_state |= GOINGAWAY; 10651 /* 10652 * Pass in bp for blocks still have journal writes 10653 * pending so we can cancel them on their own. 10654 */ 10655 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != NULL) 10656 cancel_allocindir(aip, bp, freeblks, 0); 10657 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) 10658 cancel_allocindir(aip, NULL, freeblks, 0); 10659 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) 10660 cancel_allocindir(aip, NULL, freeblks, 0); 10661 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) 10662 cancel_allocindir(aip, NULL, freeblks, 0); 10663 /* 10664 * If there are pending partial truncations we need to keep the 10665 * old block copy around until they complete. This is because 10666 * the current b_data is not a perfect superset of the available 10667 * blocks. 10668 */ 10669 if (TAILQ_EMPTY(&indirdep->ir_trunc)) 10670 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount); 10671 else 10672 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10673 WORKLIST_REMOVE(&indirdep->ir_list); 10674 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list); 10675 indirdep->ir_bp = NULL; 10676 indirdep->ir_freeblks = freeblks; 10677 } 10678 10679 /* 10680 * Free an indirdep once it no longer has new pointers to track. 10681 */ 10682 static void 10683 free_indirdep(indirdep) 10684 struct indirdep *indirdep; 10685 { 10686 10687 KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc), 10688 ("free_indirdep: Indir trunc list not empty.")); 10689 KASSERT(LIST_EMPTY(&indirdep->ir_completehd), 10690 ("free_indirdep: Complete head not empty.")); 10691 KASSERT(LIST_EMPTY(&indirdep->ir_writehd), 10692 ("free_indirdep: write head not empty.")); 10693 KASSERT(LIST_EMPTY(&indirdep->ir_donehd), 10694 ("free_indirdep: done head not empty.")); 10695 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd), 10696 ("free_indirdep: deplist head not empty.")); 10697 KASSERT((indirdep->ir_state & DEPCOMPLETE), 10698 ("free_indirdep: %p still on newblk list.", indirdep)); 10699 KASSERT(indirdep->ir_saveddata == NULL, 10700 ("free_indirdep: %p still has saved data.", indirdep)); 10701 if (indirdep->ir_state & ONWORKLIST) 10702 WORKLIST_REMOVE(&indirdep->ir_list); 10703 WORKITEM_FREE(indirdep, D_INDIRDEP); 10704 } 10705 10706 /* 10707 * Called before a write to an indirdep. This routine is responsible for 10708 * rolling back pointers to a safe state which includes only those 10709 * allocindirs which have been completed. 10710 */ 10711 static void 10712 initiate_write_indirdep(indirdep, bp) 10713 struct indirdep *indirdep; 10714 struct buf *bp; 10715 { 10716 struct ufsmount *ump; 10717 10718 indirdep->ir_state |= IOSTARTED; 10719 if (indirdep->ir_state & GOINGAWAY) 10720 panic("disk_io_initiation: indirdep gone"); 10721 /* 10722 * If there are no remaining dependencies, this will be writing 10723 * the real pointers. 10724 */ 10725 if (LIST_EMPTY(&indirdep->ir_deplisthd) && 10726 TAILQ_EMPTY(&indirdep->ir_trunc)) 10727 return; 10728 /* 10729 * Replace up-to-date version with safe version. 10730 */ 10731 if (indirdep->ir_saveddata == NULL) { 10732 ump = VFSTOUFS(indirdep->ir_list.wk_mp); 10733 LOCK_OWNED(ump); 10734 FREE_LOCK(ump); 10735 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 10736 M_SOFTDEP_FLAGS); 10737 ACQUIRE_LOCK(ump); 10738 } 10739 indirdep->ir_state &= ~ATTACHED; 10740 indirdep->ir_state |= UNDONE; 10741 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10742 bcopy(indirdep->ir_savebp->b_data, bp->b_data, 10743 bp->b_bcount); 10744 } 10745 10746 /* 10747 * Called when an inode has been cleared in a cg bitmap. This finally 10748 * eliminates any canceled jaddrefs 10749 */ 10750 void 10751 softdep_setup_inofree(mp, bp, ino, wkhd) 10752 struct mount *mp; 10753 struct buf *bp; 10754 ino_t ino; 10755 struct workhead *wkhd; 10756 { 10757 struct worklist *wk, *wkn; 10758 struct inodedep *inodedep; 10759 struct ufsmount *ump; 10760 uint8_t *inosused; 10761 struct cg *cgp; 10762 struct fs *fs; 10763 10764 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 10765 ("softdep_setup_inofree called on non-softdep filesystem")); 10766 ump = VFSTOUFS(mp); 10767 ACQUIRE_LOCK(ump); 10768 fs = ump->um_fs; 10769 cgp = (struct cg *)bp->b_data; 10770 inosused = cg_inosused(cgp); 10771 if (isset(inosused, ino % fs->fs_ipg)) 10772 panic("softdep_setup_inofree: inode %ju not freed.", 10773 (uintmax_t)ino); 10774 if (inodedep_lookup(mp, ino, 0, &inodedep)) 10775 panic("softdep_setup_inofree: ino %ju has existing inodedep %p", 10776 (uintmax_t)ino, inodedep); 10777 if (wkhd) { 10778 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) { 10779 if (wk->wk_type != D_JADDREF) 10780 continue; 10781 WORKLIST_REMOVE(wk); 10782 /* 10783 * We can free immediately even if the jaddref 10784 * isn't attached in a background write as now 10785 * the bitmaps are reconciled. 10786 */ 10787 wk->wk_state |= COMPLETE | ATTACHED; 10788 free_jaddref(WK_JADDREF(wk)); 10789 } 10790 jwork_move(&bp->b_dep, wkhd); 10791 } 10792 FREE_LOCK(ump); 10793 } 10794 10795 /* 10796 * Called via ffs_blkfree() after a set of frags has been cleared from a cg 10797 * map. Any dependencies waiting for the write to clear are added to the 10798 * buf's list and any jnewblks that are being canceled are discarded 10799 * immediately. 10800 */ 10801 void 10802 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 10803 struct mount *mp; 10804 struct buf *bp; 10805 ufs2_daddr_t blkno; 10806 int frags; 10807 struct workhead *wkhd; 10808 { 10809 struct bmsafemap *bmsafemap; 10810 struct jnewblk *jnewblk; 10811 struct ufsmount *ump; 10812 struct worklist *wk; 10813 struct fs *fs; 10814 #ifdef INVARIANTS 10815 uint8_t *blksfree; 10816 struct cg *cgp; 10817 ufs2_daddr_t jstart; 10818 ufs2_daddr_t jend; 10819 ufs2_daddr_t end; 10820 long bno; 10821 int i; 10822 #endif 10823 10824 CTR3(KTR_SUJ, 10825 "softdep_setup_blkfree: blkno %jd frags %d wk head %p", 10826 blkno, frags, wkhd); 10827 10828 ump = VFSTOUFS(mp); 10829 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 10830 ("softdep_setup_blkfree called on non-softdep filesystem")); 10831 ACQUIRE_LOCK(ump); 10832 /* Lookup the bmsafemap so we track when it is dirty. */ 10833 fs = ump->um_fs; 10834 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 10835 /* 10836 * Detach any jnewblks which have been canceled. They must linger 10837 * until the bitmap is cleared again by ffs_blkfree() to prevent 10838 * an unjournaled allocation from hitting the disk. 10839 */ 10840 if (wkhd) { 10841 while ((wk = LIST_FIRST(wkhd)) != NULL) { 10842 CTR2(KTR_SUJ, 10843 "softdep_setup_blkfree: blkno %jd wk type %d", 10844 blkno, wk->wk_type); 10845 WORKLIST_REMOVE(wk); 10846 if (wk->wk_type != D_JNEWBLK) { 10847 WORKLIST_INSERT(&bmsafemap->sm_freehd, wk); 10848 continue; 10849 } 10850 jnewblk = WK_JNEWBLK(wk); 10851 KASSERT(jnewblk->jn_state & GOINGAWAY, 10852 ("softdep_setup_blkfree: jnewblk not canceled.")); 10853 #ifdef INVARIANTS 10854 /* 10855 * Assert that this block is free in the bitmap 10856 * before we discard the jnewblk. 10857 */ 10858 cgp = (struct cg *)bp->b_data; 10859 blksfree = cg_blksfree(cgp); 10860 bno = dtogd(fs, jnewblk->jn_blkno); 10861 for (i = jnewblk->jn_oldfrags; 10862 i < jnewblk->jn_frags; i++) { 10863 if (isset(blksfree, bno + i)) 10864 continue; 10865 panic("softdep_setup_blkfree: not free"); 10866 } 10867 #endif 10868 /* 10869 * Even if it's not attached we can free immediately 10870 * as the new bitmap is correct. 10871 */ 10872 wk->wk_state |= COMPLETE | ATTACHED; 10873 free_jnewblk(jnewblk); 10874 } 10875 } 10876 10877 #ifdef INVARIANTS 10878 /* 10879 * Assert that we are not freeing a block which has an outstanding 10880 * allocation dependency. 10881 */ 10882 fs = VFSTOUFS(mp)->um_fs; 10883 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 10884 end = blkno + frags; 10885 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 10886 /* 10887 * Don't match against blocks that will be freed when the 10888 * background write is done. 10889 */ 10890 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) == 10891 (COMPLETE | DEPCOMPLETE)) 10892 continue; 10893 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags; 10894 jend = jnewblk->jn_blkno + jnewblk->jn_frags; 10895 if ((blkno >= jstart && blkno < jend) || 10896 (end > jstart && end <= jend)) { 10897 printf("state 0x%X %jd - %d %d dep %p\n", 10898 jnewblk->jn_state, jnewblk->jn_blkno, 10899 jnewblk->jn_oldfrags, jnewblk->jn_frags, 10900 jnewblk->jn_dep); 10901 panic("softdep_setup_blkfree: " 10902 "%jd-%jd(%d) overlaps with %jd-%jd", 10903 blkno, end, frags, jstart, jend); 10904 } 10905 } 10906 #endif 10907 FREE_LOCK(ump); 10908 } 10909 10910 /* 10911 * Revert a block allocation when the journal record that describes it 10912 * is not yet written. 10913 */ 10914 static int 10915 jnewblk_rollback(jnewblk, fs, cgp, blksfree) 10916 struct jnewblk *jnewblk; 10917 struct fs *fs; 10918 struct cg *cgp; 10919 uint8_t *blksfree; 10920 { 10921 ufs1_daddr_t fragno; 10922 long cgbno, bbase; 10923 int frags, blk; 10924 int i; 10925 10926 frags = 0; 10927 cgbno = dtogd(fs, jnewblk->jn_blkno); 10928 /* 10929 * We have to test which frags need to be rolled back. We may 10930 * be operating on a stale copy when doing background writes. 10931 */ 10932 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) 10933 if (isclr(blksfree, cgbno + i)) 10934 frags++; 10935 if (frags == 0) 10936 return (0); 10937 /* 10938 * This is mostly ffs_blkfree() sans some validation and 10939 * superblock updates. 10940 */ 10941 if (frags == fs->fs_frag) { 10942 fragno = fragstoblks(fs, cgbno); 10943 ffs_setblock(fs, blksfree, fragno); 10944 ffs_clusteracct(fs, cgp, fragno, 1); 10945 cgp->cg_cs.cs_nbfree++; 10946 } else { 10947 cgbno += jnewblk->jn_oldfrags; 10948 bbase = cgbno - fragnum(fs, cgbno); 10949 /* Decrement the old frags. */ 10950 blk = blkmap(fs, blksfree, bbase); 10951 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 10952 /* Deallocate the fragment */ 10953 for (i = 0; i < frags; i++) 10954 setbit(blksfree, cgbno + i); 10955 cgp->cg_cs.cs_nffree += frags; 10956 /* Add back in counts associated with the new frags */ 10957 blk = blkmap(fs, blksfree, bbase); 10958 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 10959 /* If a complete block has been reassembled, account for it. */ 10960 fragno = fragstoblks(fs, bbase); 10961 if (ffs_isblock(fs, blksfree, fragno)) { 10962 cgp->cg_cs.cs_nffree -= fs->fs_frag; 10963 ffs_clusteracct(fs, cgp, fragno, 1); 10964 cgp->cg_cs.cs_nbfree++; 10965 } 10966 } 10967 stat_jnewblk++; 10968 jnewblk->jn_state &= ~ATTACHED; 10969 jnewblk->jn_state |= UNDONE; 10970 10971 return (frags); 10972 } 10973 10974 static void 10975 initiate_write_bmsafemap(bmsafemap, bp) 10976 struct bmsafemap *bmsafemap; 10977 struct buf *bp; /* The cg block. */ 10978 { 10979 struct jaddref *jaddref; 10980 struct jnewblk *jnewblk; 10981 uint8_t *inosused; 10982 uint8_t *blksfree; 10983 struct cg *cgp; 10984 struct fs *fs; 10985 ino_t ino; 10986 10987 /* 10988 * If this is a background write, we did this at the time that 10989 * the copy was made, so do not need to do it again. 10990 */ 10991 if (bmsafemap->sm_state & IOSTARTED) 10992 return; 10993 bmsafemap->sm_state |= IOSTARTED; 10994 /* 10995 * Clear any inode allocations which are pending journal writes. 10996 */ 10997 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) { 10998 cgp = (struct cg *)bp->b_data; 10999 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11000 inosused = cg_inosused(cgp); 11001 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) { 11002 ino = jaddref->ja_ino % fs->fs_ipg; 11003 if (isset(inosused, ino)) { 11004 if ((jaddref->ja_mode & IFMT) == IFDIR) 11005 cgp->cg_cs.cs_ndir--; 11006 cgp->cg_cs.cs_nifree++; 11007 clrbit(inosused, ino); 11008 jaddref->ja_state &= ~ATTACHED; 11009 jaddref->ja_state |= UNDONE; 11010 stat_jaddref++; 11011 } else 11012 panic("initiate_write_bmsafemap: inode %ju " 11013 "marked free", (uintmax_t)jaddref->ja_ino); 11014 } 11015 } 11016 /* 11017 * Clear any block allocations which are pending journal writes. 11018 */ 11019 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 11020 cgp = (struct cg *)bp->b_data; 11021 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11022 blksfree = cg_blksfree(cgp); 11023 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 11024 if (jnewblk_rollback(jnewblk, fs, cgp, blksfree)) 11025 continue; 11026 panic("initiate_write_bmsafemap: block %jd " 11027 "marked free", jnewblk->jn_blkno); 11028 } 11029 } 11030 /* 11031 * Move allocation lists to the written lists so they can be 11032 * cleared once the block write is complete. 11033 */ 11034 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr, 11035 inodedep, id_deps); 11036 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 11037 newblk, nb_deps); 11038 LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist, 11039 wk_list); 11040 } 11041 11042 /* 11043 * This routine is called during the completion interrupt 11044 * service routine for a disk write (from the procedure called 11045 * by the device driver to inform the filesystem caches of 11046 * a request completion). It should be called early in this 11047 * procedure, before the block is made available to other 11048 * processes or other routines are called. 11049 * 11050 */ 11051 static void 11052 softdep_disk_write_complete(bp) 11053 struct buf *bp; /* describes the completed disk write */ 11054 { 11055 struct worklist *wk; 11056 struct worklist *owk; 11057 struct ufsmount *ump; 11058 struct workhead reattach; 11059 struct freeblks *freeblks; 11060 struct buf *sbp; 11061 11062 ump = softdep_bp_to_mp(bp); 11063 KASSERT(LIST_EMPTY(&bp->b_dep) || ump != NULL, 11064 ("softdep_disk_write_complete: softdep_bp_to_mp returned NULL " 11065 "with outstanding dependencies for buffer %p", bp)); 11066 if (ump == NULL) 11067 return; 11068 /* 11069 * If an error occurred while doing the write, then the data 11070 * has not hit the disk and the dependencies cannot be processed. 11071 * But we do have to go through and roll forward any dependencies 11072 * that were rolled back before the disk write. 11073 */ 11074 sbp = NULL; 11075 ACQUIRE_LOCK(ump); 11076 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) { 11077 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 11078 switch (wk->wk_type) { 11079 11080 case D_PAGEDEP: 11081 handle_written_filepage(WK_PAGEDEP(wk), bp, 0); 11082 continue; 11083 11084 case D_INODEDEP: 11085 handle_written_inodeblock(WK_INODEDEP(wk), 11086 bp, 0); 11087 continue; 11088 11089 case D_BMSAFEMAP: 11090 handle_written_bmsafemap(WK_BMSAFEMAP(wk), 11091 bp, 0); 11092 continue; 11093 11094 case D_INDIRDEP: 11095 handle_written_indirdep(WK_INDIRDEP(wk), 11096 bp, &sbp, 0); 11097 continue; 11098 default: 11099 /* nothing to roll forward */ 11100 continue; 11101 } 11102 } 11103 FREE_LOCK(ump); 11104 if (sbp) 11105 brelse(sbp); 11106 return; 11107 } 11108 LIST_INIT(&reattach); 11109 11110 /* 11111 * Ump SU lock must not be released anywhere in this code segment. 11112 */ 11113 owk = NULL; 11114 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) { 11115 WORKLIST_REMOVE(wk); 11116 atomic_add_long(&dep_write[wk->wk_type], 1); 11117 if (wk == owk) 11118 panic("duplicate worklist: %p\n", wk); 11119 owk = wk; 11120 switch (wk->wk_type) { 11121 11122 case D_PAGEDEP: 11123 if (handle_written_filepage(WK_PAGEDEP(wk), bp, 11124 WRITESUCCEEDED)) 11125 WORKLIST_INSERT(&reattach, wk); 11126 continue; 11127 11128 case D_INODEDEP: 11129 if (handle_written_inodeblock(WK_INODEDEP(wk), bp, 11130 WRITESUCCEEDED)) 11131 WORKLIST_INSERT(&reattach, wk); 11132 continue; 11133 11134 case D_BMSAFEMAP: 11135 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp, 11136 WRITESUCCEEDED)) 11137 WORKLIST_INSERT(&reattach, wk); 11138 continue; 11139 11140 case D_MKDIR: 11141 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 11142 continue; 11143 11144 case D_ALLOCDIRECT: 11145 wk->wk_state |= COMPLETE; 11146 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL); 11147 continue; 11148 11149 case D_ALLOCINDIR: 11150 wk->wk_state |= COMPLETE; 11151 handle_allocindir_partdone(WK_ALLOCINDIR(wk)); 11152 continue; 11153 11154 case D_INDIRDEP: 11155 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp, 11156 WRITESUCCEEDED)) 11157 WORKLIST_INSERT(&reattach, wk); 11158 continue; 11159 11160 case D_FREEBLKS: 11161 wk->wk_state |= COMPLETE; 11162 freeblks = WK_FREEBLKS(wk); 11163 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE && 11164 LIST_EMPTY(&freeblks->fb_jblkdephd)) 11165 add_to_worklist(wk, WK_NODELAY); 11166 continue; 11167 11168 case D_FREEWORK: 11169 handle_written_freework(WK_FREEWORK(wk)); 11170 break; 11171 11172 case D_JSEGDEP: 11173 free_jsegdep(WK_JSEGDEP(wk)); 11174 continue; 11175 11176 case D_JSEG: 11177 handle_written_jseg(WK_JSEG(wk), bp); 11178 continue; 11179 11180 case D_SBDEP: 11181 if (handle_written_sbdep(WK_SBDEP(wk), bp)) 11182 WORKLIST_INSERT(&reattach, wk); 11183 continue; 11184 11185 case D_FREEDEP: 11186 free_freedep(WK_FREEDEP(wk)); 11187 continue; 11188 11189 default: 11190 panic("handle_disk_write_complete: Unknown type %s", 11191 TYPENAME(wk->wk_type)); 11192 /* NOTREACHED */ 11193 } 11194 } 11195 /* 11196 * Reattach any requests that must be redone. 11197 */ 11198 while ((wk = LIST_FIRST(&reattach)) != NULL) { 11199 WORKLIST_REMOVE(wk); 11200 WORKLIST_INSERT(&bp->b_dep, wk); 11201 } 11202 FREE_LOCK(ump); 11203 if (sbp) 11204 brelse(sbp); 11205 } 11206 11207 /* 11208 * Called from within softdep_disk_write_complete above. 11209 */ 11210 static void 11211 handle_allocdirect_partdone(adp, wkhd) 11212 struct allocdirect *adp; /* the completed allocdirect */ 11213 struct workhead *wkhd; /* Work to do when inode is writtne. */ 11214 { 11215 struct allocdirectlst *listhead; 11216 struct allocdirect *listadp; 11217 struct inodedep *inodedep; 11218 long bsize; 11219 11220 LOCK_OWNED(VFSTOUFS(adp->ad_block.nb_list.wk_mp)); 11221 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11222 return; 11223 /* 11224 * The on-disk inode cannot claim to be any larger than the last 11225 * fragment that has been written. Otherwise, the on-disk inode 11226 * might have fragments that were not the last block in the file 11227 * which would corrupt the filesystem. Thus, we cannot free any 11228 * allocdirects after one whose ad_oldblkno claims a fragment as 11229 * these blocks must be rolled back to zero before writing the inode. 11230 * We check the currently active set of allocdirects in id_inoupdt 11231 * or id_extupdt as appropriate. 11232 */ 11233 inodedep = adp->ad_inodedep; 11234 bsize = inodedep->id_fs->fs_bsize; 11235 if (adp->ad_state & EXTDATA) 11236 listhead = &inodedep->id_extupdt; 11237 else 11238 listhead = &inodedep->id_inoupdt; 11239 TAILQ_FOREACH(listadp, listhead, ad_next) { 11240 /* found our block */ 11241 if (listadp == adp) 11242 break; 11243 /* continue if ad_oldlbn is not a fragment */ 11244 if (listadp->ad_oldsize == 0 || 11245 listadp->ad_oldsize == bsize) 11246 continue; 11247 /* hit a fragment */ 11248 return; 11249 } 11250 /* 11251 * If we have reached the end of the current list without 11252 * finding the just finished dependency, then it must be 11253 * on the future dependency list. Future dependencies cannot 11254 * be freed until they are moved to the current list. 11255 */ 11256 if (listadp == NULL) { 11257 #ifdef INVARIANTS 11258 if (adp->ad_state & EXTDATA) 11259 listhead = &inodedep->id_newextupdt; 11260 else 11261 listhead = &inodedep->id_newinoupdt; 11262 TAILQ_FOREACH(listadp, listhead, ad_next) 11263 /* found our block */ 11264 if (listadp == adp) 11265 break; 11266 if (listadp == NULL) 11267 panic("handle_allocdirect_partdone: lost dep"); 11268 #endif /* INVARIANTS */ 11269 return; 11270 } 11271 /* 11272 * If we have found the just finished dependency, then queue 11273 * it along with anything that follows it that is complete. 11274 * Since the pointer has not yet been written in the inode 11275 * as the dependency prevents it, place the allocdirect on the 11276 * bufwait list where it will be freed once the pointer is 11277 * valid. 11278 */ 11279 if (wkhd == NULL) 11280 wkhd = &inodedep->id_bufwait; 11281 for (; adp; adp = listadp) { 11282 listadp = TAILQ_NEXT(adp, ad_next); 11283 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11284 return; 11285 TAILQ_REMOVE(listhead, adp, ad_next); 11286 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list); 11287 } 11288 } 11289 11290 /* 11291 * Called from within softdep_disk_write_complete above. This routine 11292 * completes successfully written allocindirs. 11293 */ 11294 static void 11295 handle_allocindir_partdone(aip) 11296 struct allocindir *aip; /* the completed allocindir */ 11297 { 11298 struct indirdep *indirdep; 11299 11300 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE) 11301 return; 11302 indirdep = aip->ai_indirdep; 11303 LIST_REMOVE(aip, ai_next); 11304 /* 11305 * Don't set a pointer while the buffer is undergoing IO or while 11306 * we have active truncations. 11307 */ 11308 if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) { 11309 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next); 11310 return; 11311 } 11312 if (indirdep->ir_state & UFS1FMT) 11313 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11314 aip->ai_newblkno; 11315 else 11316 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11317 aip->ai_newblkno; 11318 /* 11319 * Await the pointer write before freeing the allocindir. 11320 */ 11321 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next); 11322 } 11323 11324 /* 11325 * Release segments held on a jwork list. 11326 */ 11327 static void 11328 handle_jwork(wkhd) 11329 struct workhead *wkhd; 11330 { 11331 struct worklist *wk; 11332 11333 while ((wk = LIST_FIRST(wkhd)) != NULL) { 11334 WORKLIST_REMOVE(wk); 11335 switch (wk->wk_type) { 11336 case D_JSEGDEP: 11337 free_jsegdep(WK_JSEGDEP(wk)); 11338 continue; 11339 case D_FREEDEP: 11340 free_freedep(WK_FREEDEP(wk)); 11341 continue; 11342 case D_FREEFRAG: 11343 rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep)); 11344 WORKITEM_FREE(wk, D_FREEFRAG); 11345 continue; 11346 case D_FREEWORK: 11347 handle_written_freework(WK_FREEWORK(wk)); 11348 continue; 11349 default: 11350 panic("handle_jwork: Unknown type %s\n", 11351 TYPENAME(wk->wk_type)); 11352 } 11353 } 11354 } 11355 11356 /* 11357 * Handle the bufwait list on an inode when it is safe to release items 11358 * held there. This normally happens after an inode block is written but 11359 * may be delayed and handled later if there are pending journal items that 11360 * are not yet safe to be released. 11361 */ 11362 static struct freefile * 11363 handle_bufwait(inodedep, refhd) 11364 struct inodedep *inodedep; 11365 struct workhead *refhd; 11366 { 11367 struct jaddref *jaddref; 11368 struct freefile *freefile; 11369 struct worklist *wk; 11370 11371 freefile = NULL; 11372 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) { 11373 WORKLIST_REMOVE(wk); 11374 switch (wk->wk_type) { 11375 case D_FREEFILE: 11376 /* 11377 * We defer adding freefile to the worklist 11378 * until all other additions have been made to 11379 * ensure that it will be done after all the 11380 * old blocks have been freed. 11381 */ 11382 if (freefile != NULL) 11383 panic("handle_bufwait: freefile"); 11384 freefile = WK_FREEFILE(wk); 11385 continue; 11386 11387 case D_MKDIR: 11388 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT); 11389 continue; 11390 11391 case D_DIRADD: 11392 diradd_inode_written(WK_DIRADD(wk), inodedep); 11393 continue; 11394 11395 case D_FREEFRAG: 11396 wk->wk_state |= COMPLETE; 11397 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE) 11398 add_to_worklist(wk, 0); 11399 continue; 11400 11401 case D_DIRREM: 11402 wk->wk_state |= COMPLETE; 11403 add_to_worklist(wk, 0); 11404 continue; 11405 11406 case D_ALLOCDIRECT: 11407 case D_ALLOCINDIR: 11408 free_newblk(WK_NEWBLK(wk)); 11409 continue; 11410 11411 case D_JNEWBLK: 11412 wk->wk_state |= COMPLETE; 11413 free_jnewblk(WK_JNEWBLK(wk)); 11414 continue; 11415 11416 /* 11417 * Save freed journal segments and add references on 11418 * the supplied list which will delay their release 11419 * until the cg bitmap is cleared on disk. 11420 */ 11421 case D_JSEGDEP: 11422 if (refhd == NULL) 11423 free_jsegdep(WK_JSEGDEP(wk)); 11424 else 11425 WORKLIST_INSERT(refhd, wk); 11426 continue; 11427 11428 case D_JADDREF: 11429 jaddref = WK_JADDREF(wk); 11430 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 11431 if_deps); 11432 /* 11433 * Transfer any jaddrefs to the list to be freed with 11434 * the bitmap if we're handling a removed file. 11435 */ 11436 if (refhd == NULL) { 11437 wk->wk_state |= COMPLETE; 11438 free_jaddref(jaddref); 11439 } else 11440 WORKLIST_INSERT(refhd, wk); 11441 continue; 11442 11443 default: 11444 panic("handle_bufwait: Unknown type %p(%s)", 11445 wk, TYPENAME(wk->wk_type)); 11446 /* NOTREACHED */ 11447 } 11448 } 11449 return (freefile); 11450 } 11451 /* 11452 * Called from within softdep_disk_write_complete above to restore 11453 * in-memory inode block contents to their most up-to-date state. Note 11454 * that this routine is always called from interrupt level with further 11455 * interrupts from this device blocked. 11456 * 11457 * If the write did not succeed, we will do all the roll-forward 11458 * operations, but we will not take the actions that will allow its 11459 * dependencies to be processed. 11460 */ 11461 static int 11462 handle_written_inodeblock(inodedep, bp, flags) 11463 struct inodedep *inodedep; 11464 struct buf *bp; /* buffer containing the inode block */ 11465 int flags; 11466 { 11467 struct freefile *freefile; 11468 struct allocdirect *adp, *nextadp; 11469 struct ufs1_dinode *dp1 = NULL; 11470 struct ufs2_dinode *dp2 = NULL; 11471 struct workhead wkhd; 11472 int hadchanges, fstype; 11473 ino_t freelink; 11474 11475 LIST_INIT(&wkhd); 11476 hadchanges = 0; 11477 freefile = NULL; 11478 if ((inodedep->id_state & IOSTARTED) == 0) 11479 panic("handle_written_inodeblock: not started"); 11480 inodedep->id_state &= ~IOSTARTED; 11481 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) { 11482 fstype = UFS1; 11483 dp1 = (struct ufs1_dinode *)bp->b_data + 11484 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11485 freelink = dp1->di_freelink; 11486 } else { 11487 fstype = UFS2; 11488 dp2 = (struct ufs2_dinode *)bp->b_data + 11489 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11490 freelink = dp2->di_freelink; 11491 } 11492 /* 11493 * Leave this inodeblock dirty until it's in the list. 11494 */ 11495 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED && 11496 (flags & WRITESUCCEEDED)) { 11497 struct inodedep *inon; 11498 11499 inon = TAILQ_NEXT(inodedep, id_unlinked); 11500 if ((inon == NULL && freelink == 0) || 11501 (inon && inon->id_ino == freelink)) { 11502 if (inon) 11503 inon->id_state |= UNLINKPREV; 11504 inodedep->id_state |= UNLINKNEXT; 11505 } 11506 hadchanges = 1; 11507 } 11508 /* 11509 * If we had to rollback the inode allocation because of 11510 * bitmaps being incomplete, then simply restore it. 11511 * Keep the block dirty so that it will not be reclaimed until 11512 * all associated dependencies have been cleared and the 11513 * corresponding updates written to disk. 11514 */ 11515 if (inodedep->id_savedino1 != NULL) { 11516 hadchanges = 1; 11517 if (fstype == UFS1) 11518 *dp1 = *inodedep->id_savedino1; 11519 else 11520 *dp2 = *inodedep->id_savedino2; 11521 free(inodedep->id_savedino1, M_SAVEDINO); 11522 inodedep->id_savedino1 = NULL; 11523 if ((bp->b_flags & B_DELWRI) == 0) 11524 stat_inode_bitmap++; 11525 bdirty(bp); 11526 /* 11527 * If the inode is clear here and GOINGAWAY it will never 11528 * be written. Process the bufwait and clear any pending 11529 * work which may include the freefile. 11530 */ 11531 if (inodedep->id_state & GOINGAWAY) 11532 goto bufwait; 11533 return (1); 11534 } 11535 if (flags & WRITESUCCEEDED) 11536 inodedep->id_state |= COMPLETE; 11537 /* 11538 * Roll forward anything that had to be rolled back before 11539 * the inode could be updated. 11540 */ 11541 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) { 11542 nextadp = TAILQ_NEXT(adp, ad_next); 11543 if (adp->ad_state & ATTACHED) 11544 panic("handle_written_inodeblock: new entry"); 11545 if (fstype == UFS1) { 11546 if (adp->ad_offset < UFS_NDADDR) { 11547 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11548 panic("%s %s #%jd mismatch %d != %jd", 11549 "handle_written_inodeblock:", 11550 "direct pointer", 11551 (intmax_t)adp->ad_offset, 11552 dp1->di_db[adp->ad_offset], 11553 (intmax_t)adp->ad_oldblkno); 11554 dp1->di_db[adp->ad_offset] = adp->ad_newblkno; 11555 } else { 11556 if (dp1->di_ib[adp->ad_offset - UFS_NDADDR] != 11557 0) 11558 panic("%s: %s #%jd allocated as %d", 11559 "handle_written_inodeblock", 11560 "indirect pointer", 11561 (intmax_t)adp->ad_offset - 11562 UFS_NDADDR, 11563 dp1->di_ib[adp->ad_offset - 11564 UFS_NDADDR]); 11565 dp1->di_ib[adp->ad_offset - UFS_NDADDR] = 11566 adp->ad_newblkno; 11567 } 11568 } else { 11569 if (adp->ad_offset < UFS_NDADDR) { 11570 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11571 panic("%s: %s #%jd %s %jd != %jd", 11572 "handle_written_inodeblock", 11573 "direct pointer", 11574 (intmax_t)adp->ad_offset, "mismatch", 11575 (intmax_t)dp2->di_db[adp->ad_offset], 11576 (intmax_t)adp->ad_oldblkno); 11577 dp2->di_db[adp->ad_offset] = adp->ad_newblkno; 11578 } else { 11579 if (dp2->di_ib[adp->ad_offset - UFS_NDADDR] != 11580 0) 11581 panic("%s: %s #%jd allocated as %jd", 11582 "handle_written_inodeblock", 11583 "indirect pointer", 11584 (intmax_t)adp->ad_offset - 11585 UFS_NDADDR, 11586 (intmax_t) 11587 dp2->di_ib[adp->ad_offset - 11588 UFS_NDADDR]); 11589 dp2->di_ib[adp->ad_offset - UFS_NDADDR] = 11590 adp->ad_newblkno; 11591 } 11592 } 11593 adp->ad_state &= ~UNDONE; 11594 adp->ad_state |= ATTACHED; 11595 hadchanges = 1; 11596 } 11597 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) { 11598 nextadp = TAILQ_NEXT(adp, ad_next); 11599 if (adp->ad_state & ATTACHED) 11600 panic("handle_written_inodeblock: new entry"); 11601 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno) 11602 panic("%s: direct pointers #%jd %s %jd != %jd", 11603 "handle_written_inodeblock", 11604 (intmax_t)adp->ad_offset, "mismatch", 11605 (intmax_t)dp2->di_extb[adp->ad_offset], 11606 (intmax_t)adp->ad_oldblkno); 11607 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno; 11608 adp->ad_state &= ~UNDONE; 11609 adp->ad_state |= ATTACHED; 11610 hadchanges = 1; 11611 } 11612 if (hadchanges && (bp->b_flags & B_DELWRI) == 0) 11613 stat_direct_blk_ptrs++; 11614 /* 11615 * Reset the file size to its most up-to-date value. 11616 */ 11617 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1) 11618 panic("handle_written_inodeblock: bad size"); 11619 if (inodedep->id_savednlink > UFS_LINK_MAX) 11620 panic("handle_written_inodeblock: Invalid link count " 11621 "%jd for inodedep %p", (uintmax_t)inodedep->id_savednlink, 11622 inodedep); 11623 if (fstype == UFS1) { 11624 if (dp1->di_nlink != inodedep->id_savednlink) { 11625 dp1->di_nlink = inodedep->id_savednlink; 11626 hadchanges = 1; 11627 } 11628 if (dp1->di_size != inodedep->id_savedsize) { 11629 dp1->di_size = inodedep->id_savedsize; 11630 hadchanges = 1; 11631 } 11632 } else { 11633 if (dp2->di_nlink != inodedep->id_savednlink) { 11634 dp2->di_nlink = inodedep->id_savednlink; 11635 hadchanges = 1; 11636 } 11637 if (dp2->di_size != inodedep->id_savedsize) { 11638 dp2->di_size = inodedep->id_savedsize; 11639 hadchanges = 1; 11640 } 11641 if (dp2->di_extsize != inodedep->id_savedextsize) { 11642 dp2->di_extsize = inodedep->id_savedextsize; 11643 hadchanges = 1; 11644 } 11645 } 11646 inodedep->id_savedsize = -1; 11647 inodedep->id_savedextsize = -1; 11648 inodedep->id_savednlink = -1; 11649 /* 11650 * If there were any rollbacks in the inode block, then it must be 11651 * marked dirty so that its will eventually get written back in 11652 * its correct form. 11653 */ 11654 if (hadchanges) { 11655 if (fstype == UFS2) 11656 ffs_update_dinode_ckhash(inodedep->id_fs, dp2); 11657 bdirty(bp); 11658 } 11659 bufwait: 11660 /* 11661 * If the write did not succeed, we have done all the roll-forward 11662 * operations, but we cannot take the actions that will allow its 11663 * dependencies to be processed. 11664 */ 11665 if ((flags & WRITESUCCEEDED) == 0) 11666 return (hadchanges); 11667 /* 11668 * Process any allocdirects that completed during the update. 11669 */ 11670 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 11671 handle_allocdirect_partdone(adp, &wkhd); 11672 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 11673 handle_allocdirect_partdone(adp, &wkhd); 11674 /* 11675 * Process deallocations that were held pending until the 11676 * inode had been written to disk. Freeing of the inode 11677 * is delayed until after all blocks have been freed to 11678 * avoid creation of new <vfsid, inum, lbn> triples 11679 * before the old ones have been deleted. Completely 11680 * unlinked inodes are not processed until the unlinked 11681 * inode list is written or the last reference is removed. 11682 */ 11683 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) { 11684 freefile = handle_bufwait(inodedep, NULL); 11685 if (freefile && !LIST_EMPTY(&wkhd)) { 11686 WORKLIST_INSERT(&wkhd, &freefile->fx_list); 11687 freefile = NULL; 11688 } 11689 } 11690 /* 11691 * Move rolled forward dependency completions to the bufwait list 11692 * now that those that were already written have been processed. 11693 */ 11694 if (!LIST_EMPTY(&wkhd) && hadchanges == 0) 11695 panic("handle_written_inodeblock: bufwait but no changes"); 11696 jwork_move(&inodedep->id_bufwait, &wkhd); 11697 11698 if (freefile != NULL) { 11699 /* 11700 * If the inode is goingaway it was never written. Fake up 11701 * the state here so free_inodedep() can succeed. 11702 */ 11703 if (inodedep->id_state & GOINGAWAY) 11704 inodedep->id_state |= COMPLETE | DEPCOMPLETE; 11705 if (free_inodedep(inodedep) == 0) 11706 panic("handle_written_inodeblock: live inodedep %p", 11707 inodedep); 11708 add_to_worklist(&freefile->fx_list, 0); 11709 return (0); 11710 } 11711 11712 /* 11713 * If no outstanding dependencies, free it. 11714 */ 11715 if (free_inodedep(inodedep) || 11716 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 && 11717 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 && 11718 TAILQ_FIRST(&inodedep->id_extupdt) == 0 && 11719 LIST_FIRST(&inodedep->id_bufwait) == 0)) 11720 return (0); 11721 return (hadchanges); 11722 } 11723 11724 /* 11725 * Perform needed roll-forwards and kick off any dependencies that 11726 * can now be processed. 11727 * 11728 * If the write did not succeed, we will do all the roll-forward 11729 * operations, but we will not take the actions that will allow its 11730 * dependencies to be processed. 11731 */ 11732 static int 11733 handle_written_indirdep(indirdep, bp, bpp, flags) 11734 struct indirdep *indirdep; 11735 struct buf *bp; 11736 struct buf **bpp; 11737 int flags; 11738 { 11739 struct allocindir *aip; 11740 struct buf *sbp; 11741 int chgs; 11742 11743 if (indirdep->ir_state & GOINGAWAY) 11744 panic("handle_written_indirdep: indirdep gone"); 11745 if ((indirdep->ir_state & IOSTARTED) == 0) 11746 panic("handle_written_indirdep: IO not started"); 11747 chgs = 0; 11748 /* 11749 * If there were rollbacks revert them here. 11750 */ 11751 if (indirdep->ir_saveddata) { 11752 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount); 11753 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11754 free(indirdep->ir_saveddata, M_INDIRDEP); 11755 indirdep->ir_saveddata = NULL; 11756 } 11757 chgs = 1; 11758 } 11759 indirdep->ir_state &= ~(UNDONE | IOSTARTED); 11760 indirdep->ir_state |= ATTACHED; 11761 /* 11762 * If the write did not succeed, we have done all the roll-forward 11763 * operations, but we cannot take the actions that will allow its 11764 * dependencies to be processed. 11765 */ 11766 if ((flags & WRITESUCCEEDED) == 0) { 11767 stat_indir_blk_ptrs++; 11768 bdirty(bp); 11769 return (1); 11770 } 11771 /* 11772 * Move allocindirs with written pointers to the completehd if 11773 * the indirdep's pointer is not yet written. Otherwise 11774 * free them here. 11775 */ 11776 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) { 11777 LIST_REMOVE(aip, ai_next); 11778 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 11779 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip, 11780 ai_next); 11781 newblk_freefrag(&aip->ai_block); 11782 continue; 11783 } 11784 free_newblk(&aip->ai_block); 11785 } 11786 /* 11787 * Move allocindirs that have finished dependency processing from 11788 * the done list to the write list after updating the pointers. 11789 */ 11790 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11791 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) { 11792 handle_allocindir_partdone(aip); 11793 if (aip == LIST_FIRST(&indirdep->ir_donehd)) 11794 panic("disk_write_complete: not gone"); 11795 chgs = 1; 11796 } 11797 } 11798 /* 11799 * Preserve the indirdep if there were any changes or if it is not 11800 * yet valid on disk. 11801 */ 11802 if (chgs) { 11803 stat_indir_blk_ptrs++; 11804 bdirty(bp); 11805 return (1); 11806 } 11807 /* 11808 * If there were no changes we can discard the savedbp and detach 11809 * ourselves from the buf. We are only carrying completed pointers 11810 * in this case. 11811 */ 11812 sbp = indirdep->ir_savebp; 11813 sbp->b_flags |= B_INVAL | B_NOCACHE; 11814 indirdep->ir_savebp = NULL; 11815 indirdep->ir_bp = NULL; 11816 if (*bpp != NULL) 11817 panic("handle_written_indirdep: bp already exists."); 11818 *bpp = sbp; 11819 /* 11820 * The indirdep may not be freed until its parent points at it. 11821 */ 11822 if (indirdep->ir_state & DEPCOMPLETE) 11823 free_indirdep(indirdep); 11824 11825 return (0); 11826 } 11827 11828 /* 11829 * Process a diradd entry after its dependent inode has been written. 11830 */ 11831 static void 11832 diradd_inode_written(dap, inodedep) 11833 struct diradd *dap; 11834 struct inodedep *inodedep; 11835 { 11836 11837 LOCK_OWNED(VFSTOUFS(dap->da_list.wk_mp)); 11838 dap->da_state |= COMPLETE; 11839 complete_diradd(dap); 11840 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 11841 } 11842 11843 /* 11844 * Returns true if the bmsafemap will have rollbacks when written. Must only 11845 * be called with the per-filesystem lock and the buf lock on the cg held. 11846 */ 11847 static int 11848 bmsafemap_backgroundwrite(bmsafemap, bp) 11849 struct bmsafemap *bmsafemap; 11850 struct buf *bp; 11851 { 11852 int dirty; 11853 11854 LOCK_OWNED(VFSTOUFS(bmsafemap->sm_list.wk_mp)); 11855 dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) | 11856 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd); 11857 /* 11858 * If we're initiating a background write we need to process the 11859 * rollbacks as they exist now, not as they exist when IO starts. 11860 * No other consumers will look at the contents of the shadowed 11861 * buf so this is safe to do here. 11862 */ 11863 if (bp->b_xflags & BX_BKGRDMARKER) 11864 initiate_write_bmsafemap(bmsafemap, bp); 11865 11866 return (dirty); 11867 } 11868 11869 /* 11870 * Re-apply an allocation when a cg write is complete. 11871 */ 11872 static int 11873 jnewblk_rollforward(jnewblk, fs, cgp, blksfree) 11874 struct jnewblk *jnewblk; 11875 struct fs *fs; 11876 struct cg *cgp; 11877 uint8_t *blksfree; 11878 { 11879 ufs1_daddr_t fragno; 11880 ufs2_daddr_t blkno; 11881 long cgbno, bbase; 11882 int frags, blk; 11883 int i; 11884 11885 frags = 0; 11886 cgbno = dtogd(fs, jnewblk->jn_blkno); 11887 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) { 11888 if (isclr(blksfree, cgbno + i)) 11889 panic("jnewblk_rollforward: re-allocated fragment"); 11890 frags++; 11891 } 11892 if (frags == fs->fs_frag) { 11893 blkno = fragstoblks(fs, cgbno); 11894 ffs_clrblock(fs, blksfree, (long)blkno); 11895 ffs_clusteracct(fs, cgp, blkno, -1); 11896 cgp->cg_cs.cs_nbfree--; 11897 } else { 11898 bbase = cgbno - fragnum(fs, cgbno); 11899 cgbno += jnewblk->jn_oldfrags; 11900 /* If a complete block had been reassembled, account for it. */ 11901 fragno = fragstoblks(fs, bbase); 11902 if (ffs_isblock(fs, blksfree, fragno)) { 11903 cgp->cg_cs.cs_nffree += fs->fs_frag; 11904 ffs_clusteracct(fs, cgp, fragno, -1); 11905 cgp->cg_cs.cs_nbfree--; 11906 } 11907 /* Decrement the old frags. */ 11908 blk = blkmap(fs, blksfree, bbase); 11909 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 11910 /* Allocate the fragment */ 11911 for (i = 0; i < frags; i++) 11912 clrbit(blksfree, cgbno + i); 11913 cgp->cg_cs.cs_nffree -= frags; 11914 /* Add back in counts associated with the new frags */ 11915 blk = blkmap(fs, blksfree, bbase); 11916 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 11917 } 11918 return (frags); 11919 } 11920 11921 /* 11922 * Complete a write to a bmsafemap structure. Roll forward any bitmap 11923 * changes if it's not a background write. Set all written dependencies 11924 * to DEPCOMPLETE and free the structure if possible. 11925 * 11926 * If the write did not succeed, we will do all the roll-forward 11927 * operations, but we will not take the actions that will allow its 11928 * dependencies to be processed. 11929 */ 11930 static int 11931 handle_written_bmsafemap(bmsafemap, bp, flags) 11932 struct bmsafemap *bmsafemap; 11933 struct buf *bp; 11934 int flags; 11935 { 11936 struct newblk *newblk; 11937 struct inodedep *inodedep; 11938 struct jaddref *jaddref, *jatmp; 11939 struct jnewblk *jnewblk, *jntmp; 11940 struct ufsmount *ump; 11941 uint8_t *inosused; 11942 uint8_t *blksfree; 11943 struct cg *cgp; 11944 struct fs *fs; 11945 ino_t ino; 11946 int foreground; 11947 int chgs; 11948 11949 if ((bmsafemap->sm_state & IOSTARTED) == 0) 11950 panic("handle_written_bmsafemap: Not started\n"); 11951 ump = VFSTOUFS(bmsafemap->sm_list.wk_mp); 11952 chgs = 0; 11953 bmsafemap->sm_state &= ~IOSTARTED; 11954 foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0; 11955 /* 11956 * If write was successful, release journal work that was waiting 11957 * on the write. Otherwise move the work back. 11958 */ 11959 if (flags & WRITESUCCEEDED) 11960 handle_jwork(&bmsafemap->sm_freewr); 11961 else 11962 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, 11963 worklist, wk_list); 11964 11965 /* 11966 * Restore unwritten inode allocation pending jaddref writes. 11967 */ 11968 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) { 11969 cgp = (struct cg *)bp->b_data; 11970 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11971 inosused = cg_inosused(cgp); 11972 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd, 11973 ja_bmdeps, jatmp) { 11974 if ((jaddref->ja_state & UNDONE) == 0) 11975 continue; 11976 ino = jaddref->ja_ino % fs->fs_ipg; 11977 if (isset(inosused, ino)) 11978 panic("handle_written_bmsafemap: " 11979 "re-allocated inode"); 11980 /* Do the roll-forward only if it's a real copy. */ 11981 if (foreground) { 11982 if ((jaddref->ja_mode & IFMT) == IFDIR) 11983 cgp->cg_cs.cs_ndir++; 11984 cgp->cg_cs.cs_nifree--; 11985 setbit(inosused, ino); 11986 chgs = 1; 11987 } 11988 jaddref->ja_state &= ~UNDONE; 11989 jaddref->ja_state |= ATTACHED; 11990 free_jaddref(jaddref); 11991 } 11992 } 11993 /* 11994 * Restore any block allocations which are pending journal writes. 11995 */ 11996 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 11997 cgp = (struct cg *)bp->b_data; 11998 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11999 blksfree = cg_blksfree(cgp); 12000 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps, 12001 jntmp) { 12002 if ((jnewblk->jn_state & UNDONE) == 0) 12003 continue; 12004 /* Do the roll-forward only if it's a real copy. */ 12005 if (foreground && 12006 jnewblk_rollforward(jnewblk, fs, cgp, blksfree)) 12007 chgs = 1; 12008 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK); 12009 jnewblk->jn_state |= ATTACHED; 12010 free_jnewblk(jnewblk); 12011 } 12012 } 12013 /* 12014 * If the write did not succeed, we have done all the roll-forward 12015 * operations, but we cannot take the actions that will allow its 12016 * dependencies to be processed. 12017 */ 12018 if ((flags & WRITESUCCEEDED) == 0) { 12019 LIST_CONCAT(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 12020 newblk, nb_deps); 12021 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, 12022 worklist, wk_list); 12023 if (foreground) 12024 bdirty(bp); 12025 return (1); 12026 } 12027 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) { 12028 newblk->nb_state |= DEPCOMPLETE; 12029 newblk->nb_state &= ~ONDEPLIST; 12030 newblk->nb_bmsafemap = NULL; 12031 LIST_REMOVE(newblk, nb_deps); 12032 if (newblk->nb_list.wk_type == D_ALLOCDIRECT) 12033 handle_allocdirect_partdone( 12034 WK_ALLOCDIRECT(&newblk->nb_list), NULL); 12035 else if (newblk->nb_list.wk_type == D_ALLOCINDIR) 12036 handle_allocindir_partdone( 12037 WK_ALLOCINDIR(&newblk->nb_list)); 12038 else if (newblk->nb_list.wk_type != D_NEWBLK) 12039 panic("handle_written_bmsafemap: Unexpected type: %s", 12040 TYPENAME(newblk->nb_list.wk_type)); 12041 } 12042 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) { 12043 inodedep->id_state |= DEPCOMPLETE; 12044 inodedep->id_state &= ~ONDEPLIST; 12045 LIST_REMOVE(inodedep, id_deps); 12046 inodedep->id_bmsafemap = NULL; 12047 } 12048 LIST_REMOVE(bmsafemap, sm_next); 12049 if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) && 12050 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) && 12051 LIST_EMPTY(&bmsafemap->sm_newblkhd) && 12052 LIST_EMPTY(&bmsafemap->sm_inodedephd) && 12053 LIST_EMPTY(&bmsafemap->sm_freehd)) { 12054 LIST_REMOVE(bmsafemap, sm_hash); 12055 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 12056 return (0); 12057 } 12058 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 12059 if (foreground) 12060 bdirty(bp); 12061 return (1); 12062 } 12063 12064 /* 12065 * Try to free a mkdir dependency. 12066 */ 12067 static void 12068 complete_mkdir(mkdir) 12069 struct mkdir *mkdir; 12070 { 12071 struct diradd *dap; 12072 12073 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE) 12074 return; 12075 LIST_REMOVE(mkdir, md_mkdirs); 12076 dap = mkdir->md_diradd; 12077 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 12078 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) { 12079 dap->da_state |= DEPCOMPLETE; 12080 complete_diradd(dap); 12081 } 12082 WORKITEM_FREE(mkdir, D_MKDIR); 12083 } 12084 12085 /* 12086 * Handle the completion of a mkdir dependency. 12087 */ 12088 static void 12089 handle_written_mkdir(mkdir, type) 12090 struct mkdir *mkdir; 12091 int type; 12092 { 12093 12094 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type) 12095 panic("handle_written_mkdir: bad type"); 12096 mkdir->md_state |= COMPLETE; 12097 complete_mkdir(mkdir); 12098 } 12099 12100 static int 12101 free_pagedep(pagedep) 12102 struct pagedep *pagedep; 12103 { 12104 int i; 12105 12106 if (pagedep->pd_state & NEWBLOCK) 12107 return (0); 12108 if (!LIST_EMPTY(&pagedep->pd_dirremhd)) 12109 return (0); 12110 for (i = 0; i < DAHASHSZ; i++) 12111 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i])) 12112 return (0); 12113 if (!LIST_EMPTY(&pagedep->pd_pendinghd)) 12114 return (0); 12115 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd)) 12116 return (0); 12117 if (pagedep->pd_state & ONWORKLIST) 12118 WORKLIST_REMOVE(&pagedep->pd_list); 12119 LIST_REMOVE(pagedep, pd_hash); 12120 WORKITEM_FREE(pagedep, D_PAGEDEP); 12121 12122 return (1); 12123 } 12124 12125 /* 12126 * Called from within softdep_disk_write_complete above. 12127 * A write operation was just completed. Removed inodes can 12128 * now be freed and associated block pointers may be committed. 12129 * Note that this routine is always called from interrupt level 12130 * with further interrupts from this device blocked. 12131 * 12132 * If the write did not succeed, we will do all the roll-forward 12133 * operations, but we will not take the actions that will allow its 12134 * dependencies to be processed. 12135 */ 12136 static int 12137 handle_written_filepage(pagedep, bp, flags) 12138 struct pagedep *pagedep; 12139 struct buf *bp; /* buffer containing the written page */ 12140 int flags; 12141 { 12142 struct dirrem *dirrem; 12143 struct diradd *dap, *nextdap; 12144 struct direct *ep; 12145 int i, chgs; 12146 12147 if ((pagedep->pd_state & IOSTARTED) == 0) 12148 panic("handle_written_filepage: not started"); 12149 pagedep->pd_state &= ~IOSTARTED; 12150 if ((flags & WRITESUCCEEDED) == 0) 12151 goto rollforward; 12152 /* 12153 * Process any directory removals that have been committed. 12154 */ 12155 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) { 12156 LIST_REMOVE(dirrem, dm_next); 12157 dirrem->dm_state |= COMPLETE; 12158 dirrem->dm_dirinum = pagedep->pd_ino; 12159 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 12160 ("handle_written_filepage: Journal entries not written.")); 12161 add_to_worklist(&dirrem->dm_list, 0); 12162 } 12163 /* 12164 * Free any directory additions that have been committed. 12165 * If it is a newly allocated block, we have to wait until 12166 * the on-disk directory inode claims the new block. 12167 */ 12168 if ((pagedep->pd_state & NEWBLOCK) == 0) 12169 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 12170 free_diradd(dap, NULL); 12171 rollforward: 12172 /* 12173 * Uncommitted directory entries must be restored. 12174 */ 12175 for (chgs = 0, i = 0; i < DAHASHSZ; i++) { 12176 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap; 12177 dap = nextdap) { 12178 nextdap = LIST_NEXT(dap, da_pdlist); 12179 if (dap->da_state & ATTACHED) 12180 panic("handle_written_filepage: attached"); 12181 ep = (struct direct *) 12182 ((char *)bp->b_data + dap->da_offset); 12183 ep->d_ino = dap->da_newinum; 12184 dap->da_state &= ~UNDONE; 12185 dap->da_state |= ATTACHED; 12186 chgs = 1; 12187 /* 12188 * If the inode referenced by the directory has 12189 * been written out, then the dependency can be 12190 * moved to the pending list. 12191 */ 12192 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 12193 LIST_REMOVE(dap, da_pdlist); 12194 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, 12195 da_pdlist); 12196 } 12197 } 12198 } 12199 /* 12200 * If there were any rollbacks in the directory, then it must be 12201 * marked dirty so that its will eventually get written back in 12202 * its correct form. 12203 */ 12204 if (chgs || (flags & WRITESUCCEEDED) == 0) { 12205 if ((bp->b_flags & B_DELWRI) == 0) 12206 stat_dir_entry++; 12207 bdirty(bp); 12208 return (1); 12209 } 12210 /* 12211 * If we are not waiting for a new directory block to be 12212 * claimed by its inode, then the pagedep will be freed. 12213 * Otherwise it will remain to track any new entries on 12214 * the page in case they are fsync'ed. 12215 */ 12216 free_pagedep(pagedep); 12217 return (0); 12218 } 12219 12220 /* 12221 * Writing back in-core inode structures. 12222 * 12223 * The filesystem only accesses an inode's contents when it occupies an 12224 * "in-core" inode structure. These "in-core" structures are separate from 12225 * the page frames used to cache inode blocks. Only the latter are 12226 * transferred to/from the disk. So, when the updated contents of the 12227 * "in-core" inode structure are copied to the corresponding in-memory inode 12228 * block, the dependencies are also transferred. The following procedure is 12229 * called when copying a dirty "in-core" inode to a cached inode block. 12230 */ 12231 12232 /* 12233 * Called when an inode is loaded from disk. If the effective link count 12234 * differed from the actual link count when it was last flushed, then we 12235 * need to ensure that the correct effective link count is put back. 12236 */ 12237 void 12238 softdep_load_inodeblock(ip) 12239 struct inode *ip; /* the "in_core" copy of the inode */ 12240 { 12241 struct inodedep *inodedep; 12242 struct ufsmount *ump; 12243 12244 ump = ITOUMP(ip); 12245 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 12246 ("softdep_load_inodeblock called on non-softdep filesystem")); 12247 /* 12248 * Check for alternate nlink count. 12249 */ 12250 ip->i_effnlink = ip->i_nlink; 12251 ACQUIRE_LOCK(ump); 12252 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) { 12253 FREE_LOCK(ump); 12254 return; 12255 } 12256 ip->i_effnlink -= inodedep->id_nlinkdelta; 12257 KASSERT(ip->i_effnlink >= 0, 12258 ("softdep_load_inodeblock: negative i_effnlink")); 12259 FREE_LOCK(ump); 12260 } 12261 12262 /* 12263 * This routine is called just before the "in-core" inode 12264 * information is to be copied to the in-memory inode block. 12265 * Recall that an inode block contains several inodes. If 12266 * the force flag is set, then the dependencies will be 12267 * cleared so that the update can always be made. Note that 12268 * the buffer is locked when this routine is called, so we 12269 * will never be in the middle of writing the inode block 12270 * to disk. 12271 */ 12272 void 12273 softdep_update_inodeblock(ip, bp, waitfor) 12274 struct inode *ip; /* the "in_core" copy of the inode */ 12275 struct buf *bp; /* the buffer containing the inode block */ 12276 int waitfor; /* nonzero => update must be allowed */ 12277 { 12278 struct inodedep *inodedep; 12279 struct inoref *inoref; 12280 struct ufsmount *ump; 12281 struct worklist *wk; 12282 struct mount *mp; 12283 struct buf *ibp; 12284 struct fs *fs; 12285 int error; 12286 12287 ump = ITOUMP(ip); 12288 mp = UFSTOVFS(ump); 12289 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 12290 ("softdep_update_inodeblock called on non-softdep filesystem")); 12291 fs = ump->um_fs; 12292 /* 12293 * Preserve the freelink that is on disk. clear_unlinked_inodedep() 12294 * does not have access to the in-core ip so must write directly into 12295 * the inode block buffer when setting freelink. 12296 */ 12297 if (fs->fs_magic == FS_UFS1_MAGIC) 12298 DIP_SET(ip, i_freelink, ((struct ufs1_dinode *)bp->b_data + 12299 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12300 else 12301 DIP_SET(ip, i_freelink, ((struct ufs2_dinode *)bp->b_data + 12302 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12303 /* 12304 * If the effective link count is not equal to the actual link 12305 * count, then we must track the difference in an inodedep while 12306 * the inode is (potentially) tossed out of the cache. Otherwise, 12307 * if there is no existing inodedep, then there are no dependencies 12308 * to track. 12309 */ 12310 ACQUIRE_LOCK(ump); 12311 again: 12312 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12313 FREE_LOCK(ump); 12314 if (ip->i_effnlink != ip->i_nlink) 12315 panic("softdep_update_inodeblock: bad link count"); 12316 return; 12317 } 12318 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink) 12319 panic("softdep_update_inodeblock: bad delta"); 12320 /* 12321 * If we're flushing all dependencies we must also move any waiting 12322 * for journal writes onto the bufwait list prior to I/O. 12323 */ 12324 if (waitfor) { 12325 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12326 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12327 == DEPCOMPLETE) { 12328 jwait(&inoref->if_list, MNT_WAIT); 12329 goto again; 12330 } 12331 } 12332 } 12333 /* 12334 * Changes have been initiated. Anything depending on these 12335 * changes cannot occur until this inode has been written. 12336 */ 12337 inodedep->id_state &= ~COMPLETE; 12338 if ((inodedep->id_state & ONWORKLIST) == 0) 12339 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list); 12340 /* 12341 * Any new dependencies associated with the incore inode must 12342 * now be moved to the list associated with the buffer holding 12343 * the in-memory copy of the inode. Once merged process any 12344 * allocdirects that are completed by the merger. 12345 */ 12346 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt); 12347 if (!TAILQ_EMPTY(&inodedep->id_inoupdt)) 12348 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt), 12349 NULL); 12350 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt); 12351 if (!TAILQ_EMPTY(&inodedep->id_extupdt)) 12352 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt), 12353 NULL); 12354 /* 12355 * Now that the inode has been pushed into the buffer, the 12356 * operations dependent on the inode being written to disk 12357 * can be moved to the id_bufwait so that they will be 12358 * processed when the buffer I/O completes. 12359 */ 12360 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) { 12361 WORKLIST_REMOVE(wk); 12362 WORKLIST_INSERT(&inodedep->id_bufwait, wk); 12363 } 12364 /* 12365 * Newly allocated inodes cannot be written until the bitmap 12366 * that allocates them have been written (indicated by 12367 * DEPCOMPLETE being set in id_state). If we are doing a 12368 * forced sync (e.g., an fsync on a file), we force the bitmap 12369 * to be written so that the update can be done. 12370 */ 12371 if (waitfor == 0) { 12372 FREE_LOCK(ump); 12373 return; 12374 } 12375 retry: 12376 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) { 12377 FREE_LOCK(ump); 12378 return; 12379 } 12380 ibp = inodedep->id_bmsafemap->sm_buf; 12381 ibp = getdirtybuf(ibp, LOCK_PTR(ump), MNT_WAIT); 12382 if (ibp == NULL) { 12383 /* 12384 * If ibp came back as NULL, the dependency could have been 12385 * freed while we slept. Look it up again, and check to see 12386 * that it has completed. 12387 */ 12388 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 12389 goto retry; 12390 FREE_LOCK(ump); 12391 return; 12392 } 12393 FREE_LOCK(ump); 12394 if ((error = bwrite(ibp)) != 0) 12395 softdep_error("softdep_update_inodeblock: bwrite", error); 12396 } 12397 12398 /* 12399 * Merge the a new inode dependency list (such as id_newinoupdt) into an 12400 * old inode dependency list (such as id_inoupdt). 12401 */ 12402 static void 12403 merge_inode_lists(newlisthead, oldlisthead) 12404 struct allocdirectlst *newlisthead; 12405 struct allocdirectlst *oldlisthead; 12406 { 12407 struct allocdirect *listadp, *newadp; 12408 12409 newadp = TAILQ_FIRST(newlisthead); 12410 if (newadp != NULL) 12411 LOCK_OWNED(VFSTOUFS(newadp->ad_block.nb_list.wk_mp)); 12412 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) { 12413 if (listadp->ad_offset < newadp->ad_offset) { 12414 listadp = TAILQ_NEXT(listadp, ad_next); 12415 continue; 12416 } 12417 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12418 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next); 12419 if (listadp->ad_offset == newadp->ad_offset) { 12420 allocdirect_merge(oldlisthead, newadp, 12421 listadp); 12422 listadp = newadp; 12423 } 12424 newadp = TAILQ_FIRST(newlisthead); 12425 } 12426 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) { 12427 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12428 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next); 12429 } 12430 } 12431 12432 /* 12433 * If we are doing an fsync, then we must ensure that any directory 12434 * entries for the inode have been written after the inode gets to disk. 12435 */ 12436 int 12437 softdep_fsync(vp) 12438 struct vnode *vp; /* the "in_core" copy of the inode */ 12439 { 12440 struct inodedep *inodedep; 12441 struct pagedep *pagedep; 12442 struct inoref *inoref; 12443 struct ufsmount *ump; 12444 struct worklist *wk; 12445 struct diradd *dap; 12446 struct mount *mp; 12447 struct vnode *pvp; 12448 struct inode *ip; 12449 struct buf *bp; 12450 struct fs *fs; 12451 struct thread *td = curthread; 12452 int error, flushparent, pagedep_new_block; 12453 ino_t parentino; 12454 ufs_lbn_t lbn; 12455 12456 ip = VTOI(vp); 12457 mp = vp->v_mount; 12458 ump = VFSTOUFS(mp); 12459 fs = ump->um_fs; 12460 if (MOUNTEDSOFTDEP(mp) == 0) 12461 return (0); 12462 ACQUIRE_LOCK(ump); 12463 restart: 12464 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12465 FREE_LOCK(ump); 12466 return (0); 12467 } 12468 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12469 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12470 == DEPCOMPLETE) { 12471 jwait(&inoref->if_list, MNT_WAIT); 12472 goto restart; 12473 } 12474 } 12475 if (!LIST_EMPTY(&inodedep->id_inowait) || 12476 !TAILQ_EMPTY(&inodedep->id_extupdt) || 12477 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 12478 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 12479 !TAILQ_EMPTY(&inodedep->id_newinoupdt)) 12480 panic("softdep_fsync: pending ops %p", inodedep); 12481 for (error = 0, flushparent = 0; ; ) { 12482 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL) 12483 break; 12484 if (wk->wk_type != D_DIRADD) 12485 panic("softdep_fsync: Unexpected type %s", 12486 TYPENAME(wk->wk_type)); 12487 dap = WK_DIRADD(wk); 12488 /* 12489 * Flush our parent if this directory entry has a MKDIR_PARENT 12490 * dependency or is contained in a newly allocated block. 12491 */ 12492 if (dap->da_state & DIRCHG) 12493 pagedep = dap->da_previous->dm_pagedep; 12494 else 12495 pagedep = dap->da_pagedep; 12496 parentino = pagedep->pd_ino; 12497 lbn = pagedep->pd_lbn; 12498 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) 12499 panic("softdep_fsync: dirty"); 12500 if ((dap->da_state & MKDIR_PARENT) || 12501 (pagedep->pd_state & NEWBLOCK)) 12502 flushparent = 1; 12503 else 12504 flushparent = 0; 12505 /* 12506 * If we are being fsync'ed as part of vgone'ing this vnode, 12507 * then we will not be able to release and recover the 12508 * vnode below, so we just have to give up on writing its 12509 * directory entry out. It will eventually be written, just 12510 * not now, but then the user was not asking to have it 12511 * written, so we are not breaking any promises. 12512 */ 12513 if (VN_IS_DOOMED(vp)) 12514 break; 12515 /* 12516 * We prevent deadlock by always fetching inodes from the 12517 * root, moving down the directory tree. Thus, when fetching 12518 * our parent directory, we first try to get the lock. If 12519 * that fails, we must unlock ourselves before requesting 12520 * the lock on our parent. See the comment in ufs_lookup 12521 * for details on possible races. 12522 */ 12523 FREE_LOCK(ump); 12524 if (ffs_vgetf(mp, parentino, LK_NOWAIT | LK_EXCLUSIVE, &pvp, 12525 FFSV_FORCEINSMQ)) { 12526 /* 12527 * Unmount cannot proceed after unlock because 12528 * caller must have called vn_start_write(). 12529 */ 12530 VOP_UNLOCK(vp, 0); 12531 error = ffs_vgetf(mp, parentino, LK_EXCLUSIVE, 12532 &pvp, FFSV_FORCEINSMQ); 12533 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 12534 if (VN_IS_DOOMED(vp)) { 12535 if (error == 0) 12536 vput(pvp); 12537 error = ENOENT; 12538 } 12539 if (error != 0) 12540 return (error); 12541 } 12542 /* 12543 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps 12544 * that are contained in direct blocks will be resolved by 12545 * doing a ffs_update. Pagedeps contained in indirect blocks 12546 * may require a complete sync'ing of the directory. So, we 12547 * try the cheap and fast ffs_update first, and if that fails, 12548 * then we do the slower ffs_syncvnode of the directory. 12549 */ 12550 if (flushparent) { 12551 int locked; 12552 12553 if ((error = ffs_update(pvp, 1)) != 0) { 12554 vput(pvp); 12555 return (error); 12556 } 12557 ACQUIRE_LOCK(ump); 12558 locked = 1; 12559 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) { 12560 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) { 12561 if (wk->wk_type != D_DIRADD) 12562 panic("softdep_fsync: Unexpected type %s", 12563 TYPENAME(wk->wk_type)); 12564 dap = WK_DIRADD(wk); 12565 if (dap->da_state & DIRCHG) 12566 pagedep = dap->da_previous->dm_pagedep; 12567 else 12568 pagedep = dap->da_pagedep; 12569 pagedep_new_block = pagedep->pd_state & NEWBLOCK; 12570 FREE_LOCK(ump); 12571 locked = 0; 12572 if (pagedep_new_block && (error = 12573 ffs_syncvnode(pvp, MNT_WAIT, 0))) { 12574 vput(pvp); 12575 return (error); 12576 } 12577 } 12578 } 12579 if (locked) 12580 FREE_LOCK(ump); 12581 } 12582 /* 12583 * Flush directory page containing the inode's name. 12584 */ 12585 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred, 12586 &bp); 12587 if (error == 0) 12588 error = bwrite(bp); 12589 else 12590 brelse(bp); 12591 vput(pvp); 12592 if (error != 0) 12593 return (error); 12594 ACQUIRE_LOCK(ump); 12595 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 12596 break; 12597 } 12598 FREE_LOCK(ump); 12599 return (0); 12600 } 12601 12602 /* 12603 * Flush all the dirty bitmaps associated with the block device 12604 * before flushing the rest of the dirty blocks so as to reduce 12605 * the number of dependencies that will have to be rolled back. 12606 * 12607 * XXX Unused? 12608 */ 12609 void 12610 softdep_fsync_mountdev(vp) 12611 struct vnode *vp; 12612 { 12613 struct buf *bp, *nbp; 12614 struct worklist *wk; 12615 struct bufobj *bo; 12616 12617 if (!vn_isdisk(vp, NULL)) 12618 panic("softdep_fsync_mountdev: vnode not a disk"); 12619 bo = &vp->v_bufobj; 12620 restart: 12621 BO_LOCK(bo); 12622 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 12623 /* 12624 * If it is already scheduled, skip to the next buffer. 12625 */ 12626 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) 12627 continue; 12628 12629 if ((bp->b_flags & B_DELWRI) == 0) 12630 panic("softdep_fsync_mountdev: not dirty"); 12631 /* 12632 * We are only interested in bitmaps with outstanding 12633 * dependencies. 12634 */ 12635 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL || 12636 wk->wk_type != D_BMSAFEMAP || 12637 (bp->b_vflags & BV_BKGRDINPROG)) { 12638 BUF_UNLOCK(bp); 12639 continue; 12640 } 12641 BO_UNLOCK(bo); 12642 bremfree(bp); 12643 (void) bawrite(bp); 12644 goto restart; 12645 } 12646 drain_output(vp); 12647 BO_UNLOCK(bo); 12648 } 12649 12650 /* 12651 * Sync all cylinder groups that were dirty at the time this function is 12652 * called. Newly dirtied cgs will be inserted before the sentinel. This 12653 * is used to flush freedep activity that may be holding up writes to a 12654 * indirect block. 12655 */ 12656 static int 12657 sync_cgs(mp, waitfor) 12658 struct mount *mp; 12659 int waitfor; 12660 { 12661 struct bmsafemap *bmsafemap; 12662 struct bmsafemap *sentinel; 12663 struct ufsmount *ump; 12664 struct buf *bp; 12665 int error; 12666 12667 sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK); 12668 sentinel->sm_cg = -1; 12669 ump = VFSTOUFS(mp); 12670 error = 0; 12671 ACQUIRE_LOCK(ump); 12672 LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next); 12673 for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL; 12674 bmsafemap = LIST_NEXT(sentinel, sm_next)) { 12675 /* Skip sentinels and cgs with no work to release. */ 12676 if (bmsafemap->sm_cg == -1 || 12677 (LIST_EMPTY(&bmsafemap->sm_freehd) && 12678 LIST_EMPTY(&bmsafemap->sm_freewr))) { 12679 LIST_REMOVE(sentinel, sm_next); 12680 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 12681 continue; 12682 } 12683 /* 12684 * If we don't get the lock and we're waiting try again, if 12685 * not move on to the next buf and try to sync it. 12686 */ 12687 bp = getdirtybuf(bmsafemap->sm_buf, LOCK_PTR(ump), waitfor); 12688 if (bp == NULL && waitfor == MNT_WAIT) 12689 continue; 12690 LIST_REMOVE(sentinel, sm_next); 12691 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 12692 if (bp == NULL) 12693 continue; 12694 FREE_LOCK(ump); 12695 if (waitfor == MNT_NOWAIT) 12696 bawrite(bp); 12697 else 12698 error = bwrite(bp); 12699 ACQUIRE_LOCK(ump); 12700 if (error) 12701 break; 12702 } 12703 LIST_REMOVE(sentinel, sm_next); 12704 FREE_LOCK(ump); 12705 free(sentinel, M_BMSAFEMAP); 12706 return (error); 12707 } 12708 12709 /* 12710 * This routine is called when we are trying to synchronously flush a 12711 * file. This routine must eliminate any filesystem metadata dependencies 12712 * so that the syncing routine can succeed. 12713 */ 12714 int 12715 softdep_sync_metadata(struct vnode *vp) 12716 { 12717 struct inode *ip; 12718 int error; 12719 12720 ip = VTOI(vp); 12721 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 12722 ("softdep_sync_metadata called on non-softdep filesystem")); 12723 /* 12724 * Ensure that any direct block dependencies have been cleared, 12725 * truncations are started, and inode references are journaled. 12726 */ 12727 ACQUIRE_LOCK(VFSTOUFS(vp->v_mount)); 12728 /* 12729 * Write all journal records to prevent rollbacks on devvp. 12730 */ 12731 if (vp->v_type == VCHR) 12732 softdep_flushjournal(vp->v_mount); 12733 error = flush_inodedep_deps(vp, vp->v_mount, ip->i_number); 12734 /* 12735 * Ensure that all truncates are written so we won't find deps on 12736 * indirect blocks. 12737 */ 12738 process_truncates(vp); 12739 FREE_LOCK(VFSTOUFS(vp->v_mount)); 12740 12741 return (error); 12742 } 12743 12744 /* 12745 * This routine is called when we are attempting to sync a buf with 12746 * dependencies. If waitfor is MNT_NOWAIT it attempts to schedule any 12747 * other IO it can but returns EBUSY if the buffer is not yet able to 12748 * be written. Dependencies which will not cause rollbacks will always 12749 * return 0. 12750 */ 12751 int 12752 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 12753 { 12754 struct indirdep *indirdep; 12755 struct pagedep *pagedep; 12756 struct allocindir *aip; 12757 struct newblk *newblk; 12758 struct ufsmount *ump; 12759 struct buf *nbp; 12760 struct worklist *wk; 12761 int i, error; 12762 12763 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 12764 ("softdep_sync_buf called on non-softdep filesystem")); 12765 /* 12766 * For VCHR we just don't want to force flush any dependencies that 12767 * will cause rollbacks. 12768 */ 12769 if (vp->v_type == VCHR) { 12770 if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0)) 12771 return (EBUSY); 12772 return (0); 12773 } 12774 ump = VFSTOUFS(vp->v_mount); 12775 ACQUIRE_LOCK(ump); 12776 /* 12777 * As we hold the buffer locked, none of its dependencies 12778 * will disappear. 12779 */ 12780 error = 0; 12781 top: 12782 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 12783 switch (wk->wk_type) { 12784 12785 case D_ALLOCDIRECT: 12786 case D_ALLOCINDIR: 12787 newblk = WK_NEWBLK(wk); 12788 if (newblk->nb_jnewblk != NULL) { 12789 if (waitfor == MNT_NOWAIT) { 12790 error = EBUSY; 12791 goto out_unlock; 12792 } 12793 jwait(&newblk->nb_jnewblk->jn_list, waitfor); 12794 goto top; 12795 } 12796 if (newblk->nb_state & DEPCOMPLETE || 12797 waitfor == MNT_NOWAIT) 12798 continue; 12799 nbp = newblk->nb_bmsafemap->sm_buf; 12800 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 12801 if (nbp == NULL) 12802 goto top; 12803 FREE_LOCK(ump); 12804 if ((error = bwrite(nbp)) != 0) 12805 goto out; 12806 ACQUIRE_LOCK(ump); 12807 continue; 12808 12809 case D_INDIRDEP: 12810 indirdep = WK_INDIRDEP(wk); 12811 if (waitfor == MNT_NOWAIT) { 12812 if (!TAILQ_EMPTY(&indirdep->ir_trunc) || 12813 !LIST_EMPTY(&indirdep->ir_deplisthd)) { 12814 error = EBUSY; 12815 goto out_unlock; 12816 } 12817 } 12818 if (!TAILQ_EMPTY(&indirdep->ir_trunc)) 12819 panic("softdep_sync_buf: truncation pending."); 12820 restart: 12821 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 12822 newblk = (struct newblk *)aip; 12823 if (newblk->nb_jnewblk != NULL) { 12824 jwait(&newblk->nb_jnewblk->jn_list, 12825 waitfor); 12826 goto restart; 12827 } 12828 if (newblk->nb_state & DEPCOMPLETE) 12829 continue; 12830 nbp = newblk->nb_bmsafemap->sm_buf; 12831 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 12832 if (nbp == NULL) 12833 goto restart; 12834 FREE_LOCK(ump); 12835 if ((error = bwrite(nbp)) != 0) 12836 goto out; 12837 ACQUIRE_LOCK(ump); 12838 goto restart; 12839 } 12840 continue; 12841 12842 case D_PAGEDEP: 12843 /* 12844 * Only flush directory entries in synchronous passes. 12845 */ 12846 if (waitfor != MNT_WAIT) { 12847 error = EBUSY; 12848 goto out_unlock; 12849 } 12850 /* 12851 * While syncing snapshots, we must allow recursive 12852 * lookups. 12853 */ 12854 BUF_AREC(bp); 12855 /* 12856 * We are trying to sync a directory that may 12857 * have dependencies on both its own metadata 12858 * and/or dependencies on the inodes of any 12859 * recently allocated files. We walk its diradd 12860 * lists pushing out the associated inode. 12861 */ 12862 pagedep = WK_PAGEDEP(wk); 12863 for (i = 0; i < DAHASHSZ; i++) { 12864 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0) 12865 continue; 12866 if ((error = flush_pagedep_deps(vp, wk->wk_mp, 12867 &pagedep->pd_diraddhd[i]))) { 12868 BUF_NOREC(bp); 12869 goto out_unlock; 12870 } 12871 } 12872 BUF_NOREC(bp); 12873 continue; 12874 12875 case D_FREEWORK: 12876 case D_FREEDEP: 12877 case D_JSEGDEP: 12878 case D_JNEWBLK: 12879 continue; 12880 12881 default: 12882 panic("softdep_sync_buf: Unknown type %s", 12883 TYPENAME(wk->wk_type)); 12884 /* NOTREACHED */ 12885 } 12886 } 12887 out_unlock: 12888 FREE_LOCK(ump); 12889 out: 12890 return (error); 12891 } 12892 12893 /* 12894 * Flush the dependencies associated with an inodedep. 12895 */ 12896 static int 12897 flush_inodedep_deps(vp, mp, ino) 12898 struct vnode *vp; 12899 struct mount *mp; 12900 ino_t ino; 12901 { 12902 struct inodedep *inodedep; 12903 struct inoref *inoref; 12904 struct ufsmount *ump; 12905 int error, waitfor; 12906 12907 /* 12908 * This work is done in two passes. The first pass grabs most 12909 * of the buffers and begins asynchronously writing them. The 12910 * only way to wait for these asynchronous writes is to sleep 12911 * on the filesystem vnode which may stay busy for a long time 12912 * if the filesystem is active. So, instead, we make a second 12913 * pass over the dependencies blocking on each write. In the 12914 * usual case we will be blocking against a write that we 12915 * initiated, so when it is done the dependency will have been 12916 * resolved. Thus the second pass is expected to end quickly. 12917 * We give a brief window at the top of the loop to allow 12918 * any pending I/O to complete. 12919 */ 12920 ump = VFSTOUFS(mp); 12921 LOCK_OWNED(ump); 12922 for (error = 0, waitfor = MNT_NOWAIT; ; ) { 12923 if (error) 12924 return (error); 12925 FREE_LOCK(ump); 12926 ACQUIRE_LOCK(ump); 12927 restart: 12928 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 12929 return (0); 12930 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12931 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12932 == DEPCOMPLETE) { 12933 jwait(&inoref->if_list, MNT_WAIT); 12934 goto restart; 12935 } 12936 } 12937 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) || 12938 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) || 12939 flush_deplist(&inodedep->id_extupdt, waitfor, &error) || 12940 flush_deplist(&inodedep->id_newextupdt, waitfor, &error)) 12941 continue; 12942 /* 12943 * If pass2, we are done, otherwise do pass 2. 12944 */ 12945 if (waitfor == MNT_WAIT) 12946 break; 12947 waitfor = MNT_WAIT; 12948 } 12949 /* 12950 * Try freeing inodedep in case all dependencies have been removed. 12951 */ 12952 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0) 12953 (void) free_inodedep(inodedep); 12954 return (0); 12955 } 12956 12957 /* 12958 * Flush an inode dependency list. 12959 */ 12960 static int 12961 flush_deplist(listhead, waitfor, errorp) 12962 struct allocdirectlst *listhead; 12963 int waitfor; 12964 int *errorp; 12965 { 12966 struct allocdirect *adp; 12967 struct newblk *newblk; 12968 struct ufsmount *ump; 12969 struct buf *bp; 12970 12971 if ((adp = TAILQ_FIRST(listhead)) == NULL) 12972 return (0); 12973 ump = VFSTOUFS(adp->ad_list.wk_mp); 12974 LOCK_OWNED(ump); 12975 TAILQ_FOREACH(adp, listhead, ad_next) { 12976 newblk = (struct newblk *)adp; 12977 if (newblk->nb_jnewblk != NULL) { 12978 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 12979 return (1); 12980 } 12981 if (newblk->nb_state & DEPCOMPLETE) 12982 continue; 12983 bp = newblk->nb_bmsafemap->sm_buf; 12984 bp = getdirtybuf(bp, LOCK_PTR(ump), waitfor); 12985 if (bp == NULL) { 12986 if (waitfor == MNT_NOWAIT) 12987 continue; 12988 return (1); 12989 } 12990 FREE_LOCK(ump); 12991 if (waitfor == MNT_NOWAIT) 12992 bawrite(bp); 12993 else 12994 *errorp = bwrite(bp); 12995 ACQUIRE_LOCK(ump); 12996 return (1); 12997 } 12998 return (0); 12999 } 13000 13001 /* 13002 * Flush dependencies associated with an allocdirect block. 13003 */ 13004 static int 13005 flush_newblk_dep(vp, mp, lbn) 13006 struct vnode *vp; 13007 struct mount *mp; 13008 ufs_lbn_t lbn; 13009 { 13010 struct newblk *newblk; 13011 struct ufsmount *ump; 13012 struct bufobj *bo; 13013 struct inode *ip; 13014 struct buf *bp; 13015 ufs2_daddr_t blkno; 13016 int error; 13017 13018 error = 0; 13019 bo = &vp->v_bufobj; 13020 ip = VTOI(vp); 13021 blkno = DIP(ip, i_db[lbn]); 13022 if (blkno == 0) 13023 panic("flush_newblk_dep: Missing block"); 13024 ump = VFSTOUFS(mp); 13025 ACQUIRE_LOCK(ump); 13026 /* 13027 * Loop until all dependencies related to this block are satisfied. 13028 * We must be careful to restart after each sleep in case a write 13029 * completes some part of this process for us. 13030 */ 13031 for (;;) { 13032 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) { 13033 FREE_LOCK(ump); 13034 break; 13035 } 13036 if (newblk->nb_list.wk_type != D_ALLOCDIRECT) 13037 panic("flush_newblk_dep: Bad newblk %p", newblk); 13038 /* 13039 * Flush the journal. 13040 */ 13041 if (newblk->nb_jnewblk != NULL) { 13042 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 13043 continue; 13044 } 13045 /* 13046 * Write the bitmap dependency. 13047 */ 13048 if ((newblk->nb_state & DEPCOMPLETE) == 0) { 13049 bp = newblk->nb_bmsafemap->sm_buf; 13050 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 13051 if (bp == NULL) 13052 continue; 13053 FREE_LOCK(ump); 13054 error = bwrite(bp); 13055 if (error) 13056 break; 13057 ACQUIRE_LOCK(ump); 13058 continue; 13059 } 13060 /* 13061 * Write the buffer. 13062 */ 13063 FREE_LOCK(ump); 13064 BO_LOCK(bo); 13065 bp = gbincore(bo, lbn); 13066 if (bp != NULL) { 13067 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 13068 LK_INTERLOCK, BO_LOCKPTR(bo)); 13069 if (error == ENOLCK) { 13070 ACQUIRE_LOCK(ump); 13071 error = 0; 13072 continue; /* Slept, retry */ 13073 } 13074 if (error != 0) 13075 break; /* Failed */ 13076 if (bp->b_flags & B_DELWRI) { 13077 bremfree(bp); 13078 error = bwrite(bp); 13079 if (error) 13080 break; 13081 } else 13082 BUF_UNLOCK(bp); 13083 } else 13084 BO_UNLOCK(bo); 13085 /* 13086 * We have to wait for the direct pointers to 13087 * point at the newdirblk before the dependency 13088 * will go away. 13089 */ 13090 error = ffs_update(vp, 1); 13091 if (error) 13092 break; 13093 ACQUIRE_LOCK(ump); 13094 } 13095 return (error); 13096 } 13097 13098 /* 13099 * Eliminate a pagedep dependency by flushing out all its diradd dependencies. 13100 */ 13101 static int 13102 flush_pagedep_deps(pvp, mp, diraddhdp) 13103 struct vnode *pvp; 13104 struct mount *mp; 13105 struct diraddhd *diraddhdp; 13106 { 13107 struct inodedep *inodedep; 13108 struct inoref *inoref; 13109 struct ufsmount *ump; 13110 struct diradd *dap; 13111 struct vnode *vp; 13112 int error = 0; 13113 struct buf *bp; 13114 ino_t inum; 13115 struct diraddhd unfinished; 13116 13117 LIST_INIT(&unfinished); 13118 ump = VFSTOUFS(mp); 13119 LOCK_OWNED(ump); 13120 restart: 13121 while ((dap = LIST_FIRST(diraddhdp)) != NULL) { 13122 /* 13123 * Flush ourselves if this directory entry 13124 * has a MKDIR_PARENT dependency. 13125 */ 13126 if (dap->da_state & MKDIR_PARENT) { 13127 FREE_LOCK(ump); 13128 if ((error = ffs_update(pvp, 1)) != 0) 13129 break; 13130 ACQUIRE_LOCK(ump); 13131 /* 13132 * If that cleared dependencies, go on to next. 13133 */ 13134 if (dap != LIST_FIRST(diraddhdp)) 13135 continue; 13136 /* 13137 * All MKDIR_PARENT dependencies and all the 13138 * NEWBLOCK pagedeps that are contained in direct 13139 * blocks were resolved by doing above ffs_update. 13140 * Pagedeps contained in indirect blocks may 13141 * require a complete sync'ing of the directory. 13142 * We are in the midst of doing a complete sync, 13143 * so if they are not resolved in this pass we 13144 * defer them for now as they will be sync'ed by 13145 * our caller shortly. 13146 */ 13147 LIST_REMOVE(dap, da_pdlist); 13148 LIST_INSERT_HEAD(&unfinished, dap, da_pdlist); 13149 continue; 13150 } 13151 /* 13152 * A newly allocated directory must have its "." and 13153 * ".." entries written out before its name can be 13154 * committed in its parent. 13155 */ 13156 inum = dap->da_newinum; 13157 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 13158 panic("flush_pagedep_deps: lost inode1"); 13159 /* 13160 * Wait for any pending journal adds to complete so we don't 13161 * cause rollbacks while syncing. 13162 */ 13163 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 13164 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 13165 == DEPCOMPLETE) { 13166 jwait(&inoref->if_list, MNT_WAIT); 13167 goto restart; 13168 } 13169 } 13170 if (dap->da_state & MKDIR_BODY) { 13171 FREE_LOCK(ump); 13172 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp, 13173 FFSV_FORCEINSMQ))) 13174 break; 13175 error = flush_newblk_dep(vp, mp, 0); 13176 /* 13177 * If we still have the dependency we might need to 13178 * update the vnode to sync the new link count to 13179 * disk. 13180 */ 13181 if (error == 0 && dap == LIST_FIRST(diraddhdp)) 13182 error = ffs_update(vp, 1); 13183 vput(vp); 13184 if (error != 0) 13185 break; 13186 ACQUIRE_LOCK(ump); 13187 /* 13188 * If that cleared dependencies, go on to next. 13189 */ 13190 if (dap != LIST_FIRST(diraddhdp)) 13191 continue; 13192 if (dap->da_state & MKDIR_BODY) { 13193 inodedep_lookup(UFSTOVFS(ump), inum, 0, 13194 &inodedep); 13195 panic("flush_pagedep_deps: MKDIR_BODY " 13196 "inodedep %p dap %p vp %p", 13197 inodedep, dap, vp); 13198 } 13199 } 13200 /* 13201 * Flush the inode on which the directory entry depends. 13202 * Having accounted for MKDIR_PARENT and MKDIR_BODY above, 13203 * the only remaining dependency is that the updated inode 13204 * count must get pushed to disk. The inode has already 13205 * been pushed into its inode buffer (via VOP_UPDATE) at 13206 * the time of the reference count change. So we need only 13207 * locate that buffer, ensure that there will be no rollback 13208 * caused by a bitmap dependency, then write the inode buffer. 13209 */ 13210 retry: 13211 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 13212 panic("flush_pagedep_deps: lost inode"); 13213 /* 13214 * If the inode still has bitmap dependencies, 13215 * push them to disk. 13216 */ 13217 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) { 13218 bp = inodedep->id_bmsafemap->sm_buf; 13219 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 13220 if (bp == NULL) 13221 goto retry; 13222 FREE_LOCK(ump); 13223 if ((error = bwrite(bp)) != 0) 13224 break; 13225 ACQUIRE_LOCK(ump); 13226 if (dap != LIST_FIRST(diraddhdp)) 13227 continue; 13228 } 13229 /* 13230 * If the inode is still sitting in a buffer waiting 13231 * to be written or waiting for the link count to be 13232 * adjusted update it here to flush it to disk. 13233 */ 13234 if (dap == LIST_FIRST(diraddhdp)) { 13235 FREE_LOCK(ump); 13236 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp, 13237 FFSV_FORCEINSMQ))) 13238 break; 13239 error = ffs_update(vp, 1); 13240 vput(vp); 13241 if (error) 13242 break; 13243 ACQUIRE_LOCK(ump); 13244 } 13245 /* 13246 * If we have failed to get rid of all the dependencies 13247 * then something is seriously wrong. 13248 */ 13249 if (dap == LIST_FIRST(diraddhdp)) { 13250 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep); 13251 panic("flush_pagedep_deps: failed to flush " 13252 "inodedep %p ino %ju dap %p", 13253 inodedep, (uintmax_t)inum, dap); 13254 } 13255 } 13256 if (error) 13257 ACQUIRE_LOCK(ump); 13258 while ((dap = LIST_FIRST(&unfinished)) != NULL) { 13259 LIST_REMOVE(dap, da_pdlist); 13260 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist); 13261 } 13262 return (error); 13263 } 13264 13265 /* 13266 * A large burst of file addition or deletion activity can drive the 13267 * memory load excessively high. First attempt to slow things down 13268 * using the techniques below. If that fails, this routine requests 13269 * the offending operations to fall back to running synchronously 13270 * until the memory load returns to a reasonable level. 13271 */ 13272 int 13273 softdep_slowdown(vp) 13274 struct vnode *vp; 13275 { 13276 struct ufsmount *ump; 13277 int jlow; 13278 int max_softdeps_hard; 13279 13280 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 13281 ("softdep_slowdown called on non-softdep filesystem")); 13282 ump = VFSTOUFS(vp->v_mount); 13283 ACQUIRE_LOCK(ump); 13284 jlow = 0; 13285 /* 13286 * Check for journal space if needed. 13287 */ 13288 if (DOINGSUJ(vp)) { 13289 if (journal_space(ump, 0) == 0) 13290 jlow = 1; 13291 } 13292 /* 13293 * If the system is under its limits and our filesystem is 13294 * not responsible for more than our share of the usage and 13295 * we are not low on journal space, then no need to slow down. 13296 */ 13297 max_softdeps_hard = max_softdeps * 11 / 10; 13298 if (dep_current[D_DIRREM] < max_softdeps_hard / 2 && 13299 dep_current[D_INODEDEP] < max_softdeps_hard && 13300 dep_current[D_INDIRDEP] < max_softdeps_hard / 1000 && 13301 dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0 && 13302 ump->softdep_curdeps[D_DIRREM] < 13303 (max_softdeps_hard / 2) / stat_flush_threads && 13304 ump->softdep_curdeps[D_INODEDEP] < 13305 max_softdeps_hard / stat_flush_threads && 13306 ump->softdep_curdeps[D_INDIRDEP] < 13307 (max_softdeps_hard / 1000) / stat_flush_threads && 13308 ump->softdep_curdeps[D_FREEBLKS] < 13309 max_softdeps_hard / stat_flush_threads) { 13310 FREE_LOCK(ump); 13311 return (0); 13312 } 13313 /* 13314 * If the journal is low or our filesystem is over its limit 13315 * then speedup the cleanup. 13316 */ 13317 if (ump->softdep_curdeps[D_INDIRDEP] < 13318 (max_softdeps_hard / 1000) / stat_flush_threads || jlow) 13319 softdep_speedup(ump); 13320 stat_sync_limit_hit += 1; 13321 FREE_LOCK(ump); 13322 /* 13323 * We only slow down the rate at which new dependencies are 13324 * generated if we are not using journaling. With journaling, 13325 * the cleanup should always be sufficient to keep things 13326 * under control. 13327 */ 13328 if (DOINGSUJ(vp)) 13329 return (0); 13330 return (1); 13331 } 13332 13333 /* 13334 * Called by the allocation routines when they are about to fail 13335 * in the hope that we can free up the requested resource (inodes 13336 * or disk space). 13337 * 13338 * First check to see if the work list has anything on it. If it has, 13339 * clean up entries until we successfully free the requested resource. 13340 * Because this process holds inodes locked, we cannot handle any remove 13341 * requests that might block on a locked inode as that could lead to 13342 * deadlock. If the worklist yields none of the requested resource, 13343 * start syncing out vnodes to free up the needed space. 13344 */ 13345 int 13346 softdep_request_cleanup(fs, vp, cred, resource) 13347 struct fs *fs; 13348 struct vnode *vp; 13349 struct ucred *cred; 13350 int resource; 13351 { 13352 struct ufsmount *ump; 13353 struct mount *mp; 13354 long starttime; 13355 size_t resid; 13356 ufs2_daddr_t needed; 13357 int error, failed_vnode; 13358 13359 /* 13360 * If we are being called because of a process doing a 13361 * copy-on-write, then it is not safe to process any 13362 * worklist items as we will recurse into the copyonwrite 13363 * routine. This will result in an incoherent snapshot. 13364 * If the vnode that we hold is a snapshot, we must avoid 13365 * handling other resources that could cause deadlock. 13366 */ 13367 if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp))) 13368 return (0); 13369 13370 if (resource == FLUSH_BLOCKS_WAIT) 13371 stat_cleanup_blkrequests += 1; 13372 else 13373 stat_cleanup_inorequests += 1; 13374 13375 mp = vp->v_mount; 13376 ump = VFSTOUFS(mp); 13377 mtx_assert(UFS_MTX(ump), MA_OWNED); 13378 UFS_UNLOCK(ump); 13379 error = ffs_update(vp, 1); 13380 if (error != 0 || MOUNTEDSOFTDEP(mp) == 0) { 13381 UFS_LOCK(ump); 13382 return (0); 13383 } 13384 /* 13385 * If we are in need of resources, start by cleaning up 13386 * any block removals associated with our inode. 13387 */ 13388 ACQUIRE_LOCK(ump); 13389 process_removes(vp); 13390 process_truncates(vp); 13391 FREE_LOCK(ump); 13392 /* 13393 * Now clean up at least as many resources as we will need. 13394 * 13395 * When requested to clean up inodes, the number that are needed 13396 * is set by the number of simultaneous writers (mnt_writeopcount) 13397 * plus a bit of slop (2) in case some more writers show up while 13398 * we are cleaning. 13399 * 13400 * When requested to free up space, the amount of space that 13401 * we need is enough blocks to allocate a full-sized segment 13402 * (fs_contigsumsize). The number of such segments that will 13403 * be needed is set by the number of simultaneous writers 13404 * (mnt_writeopcount) plus a bit of slop (2) in case some more 13405 * writers show up while we are cleaning. 13406 * 13407 * Additionally, if we are unpriviledged and allocating space, 13408 * we need to ensure that we clean up enough blocks to get the 13409 * needed number of blocks over the threshold of the minimum 13410 * number of blocks required to be kept free by the filesystem 13411 * (fs_minfree). 13412 */ 13413 if (resource == FLUSH_INODES_WAIT) { 13414 needed = vfs_mount_fetch_counter(vp->v_mount, 13415 MNT_COUNT_WRITEOPCOUNT) + 2; 13416 } else if (resource == FLUSH_BLOCKS_WAIT) { 13417 needed = (vfs_mount_fetch_counter(vp->v_mount, 13418 MNT_COUNT_WRITEOPCOUNT) + 2) * fs->fs_contigsumsize; 13419 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE)) 13420 needed += fragstoblks(fs, 13421 roundup((fs->fs_dsize * fs->fs_minfree / 100) - 13422 fs->fs_cstotal.cs_nffree, fs->fs_frag)); 13423 } else { 13424 printf("softdep_request_cleanup: Unknown resource type %d\n", 13425 resource); 13426 UFS_LOCK(ump); 13427 return (0); 13428 } 13429 starttime = time_second; 13430 retry: 13431 if (resource == FLUSH_BLOCKS_WAIT && 13432 fs->fs_cstotal.cs_nbfree <= needed) 13433 g_io_speedup(needed * fs->fs_bsize, BIO_SPEEDUP_TRIM, &resid, 13434 ump->um_cp); 13435 if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 && 13436 fs->fs_cstotal.cs_nbfree <= needed) || 13437 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13438 fs->fs_cstotal.cs_nifree <= needed)) { 13439 ACQUIRE_LOCK(ump); 13440 if (ump->softdep_on_worklist > 0 && 13441 process_worklist_item(UFSTOVFS(ump), 13442 ump->softdep_on_worklist, LK_NOWAIT) != 0) 13443 stat_worklist_push += 1; 13444 FREE_LOCK(ump); 13445 } 13446 /* 13447 * If we still need resources and there are no more worklist 13448 * entries to process to obtain them, we have to start flushing 13449 * the dirty vnodes to force the release of additional requests 13450 * to the worklist that we can then process to reap addition 13451 * resources. We walk the vnodes associated with the mount point 13452 * until we get the needed worklist requests that we can reap. 13453 * 13454 * If there are several threads all needing to clean the same 13455 * mount point, only one is allowed to walk the mount list. 13456 * When several threads all try to walk the same mount list, 13457 * they end up competing with each other and often end up in 13458 * livelock. This approach ensures that forward progress is 13459 * made at the cost of occational ENOSPC errors being returned 13460 * that might otherwise have been avoided. 13461 */ 13462 error = 1; 13463 if ((resource == FLUSH_BLOCKS_WAIT && 13464 fs->fs_cstotal.cs_nbfree <= needed) || 13465 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13466 fs->fs_cstotal.cs_nifree <= needed)) { 13467 ACQUIRE_LOCK(ump); 13468 if ((ump->um_softdep->sd_flags & FLUSH_RC_ACTIVE) == 0) { 13469 ump->um_softdep->sd_flags |= FLUSH_RC_ACTIVE; 13470 FREE_LOCK(ump); 13471 failed_vnode = softdep_request_cleanup_flush(mp, ump); 13472 ACQUIRE_LOCK(ump); 13473 ump->um_softdep->sd_flags &= ~FLUSH_RC_ACTIVE; 13474 FREE_LOCK(ump); 13475 if (ump->softdep_on_worklist > 0) { 13476 stat_cleanup_retries += 1; 13477 if (!failed_vnode) 13478 goto retry; 13479 } 13480 } else { 13481 FREE_LOCK(ump); 13482 error = 0; 13483 } 13484 stat_cleanup_failures += 1; 13485 } 13486 if (time_second - starttime > stat_cleanup_high_delay) 13487 stat_cleanup_high_delay = time_second - starttime; 13488 UFS_LOCK(ump); 13489 return (error); 13490 } 13491 13492 /* 13493 * Scan the vnodes for the specified mount point flushing out any 13494 * vnodes that can be locked without waiting. Finally, try to flush 13495 * the device associated with the mount point if it can be locked 13496 * without waiting. 13497 * 13498 * We return 0 if we were able to lock every vnode in our scan. 13499 * If we had to skip one or more vnodes, we return 1. 13500 */ 13501 static int 13502 softdep_request_cleanup_flush(mp, ump) 13503 struct mount *mp; 13504 struct ufsmount *ump; 13505 { 13506 struct thread *td; 13507 struct vnode *lvp, *mvp; 13508 int failed_vnode; 13509 13510 failed_vnode = 0; 13511 td = curthread; 13512 MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) { 13513 if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) { 13514 VI_UNLOCK(lvp); 13515 continue; 13516 } 13517 if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT, 13518 td) != 0) { 13519 failed_vnode = 1; 13520 continue; 13521 } 13522 if (lvp->v_vflag & VV_NOSYNC) { /* unlinked */ 13523 vput(lvp); 13524 continue; 13525 } 13526 (void) ffs_syncvnode(lvp, MNT_NOWAIT, 0); 13527 vput(lvp); 13528 } 13529 lvp = ump->um_devvp; 13530 if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) { 13531 VOP_FSYNC(lvp, MNT_NOWAIT, td); 13532 VOP_UNLOCK(lvp, 0); 13533 } 13534 return (failed_vnode); 13535 } 13536 13537 static bool 13538 softdep_excess_items(struct ufsmount *ump, int item) 13539 { 13540 13541 KASSERT(item >= 0 && item < D_LAST, ("item %d", item)); 13542 return (dep_current[item] > max_softdeps && 13543 ump->softdep_curdeps[item] > max_softdeps / 13544 stat_flush_threads); 13545 } 13546 13547 static void 13548 schedule_cleanup(struct mount *mp) 13549 { 13550 struct ufsmount *ump; 13551 struct thread *td; 13552 13553 ump = VFSTOUFS(mp); 13554 LOCK_OWNED(ump); 13555 FREE_LOCK(ump); 13556 td = curthread; 13557 if ((td->td_pflags & TDP_KTHREAD) != 0 && 13558 (td->td_proc->p_flag2 & P2_AST_SU) == 0) { 13559 /* 13560 * No ast is delivered to kernel threads, so nobody 13561 * would deref the mp. Some kernel threads 13562 * explicitely check for AST, e.g. NFS daemon does 13563 * this in the serving loop. 13564 */ 13565 return; 13566 } 13567 if (td->td_su != NULL) 13568 vfs_rel(td->td_su); 13569 vfs_ref(mp); 13570 td->td_su = mp; 13571 thread_lock(td); 13572 td->td_flags |= TDF_ASTPENDING; 13573 thread_unlock(td); 13574 } 13575 13576 static void 13577 softdep_ast_cleanup_proc(struct thread *td) 13578 { 13579 struct mount *mp; 13580 struct ufsmount *ump; 13581 int error; 13582 bool req; 13583 13584 while ((mp = td->td_su) != NULL) { 13585 td->td_su = NULL; 13586 error = vfs_busy(mp, MBF_NOWAIT); 13587 vfs_rel(mp); 13588 if (error != 0) 13589 return; 13590 if (ffs_own_mount(mp) && MOUNTEDSOFTDEP(mp)) { 13591 ump = VFSTOUFS(mp); 13592 for (;;) { 13593 req = false; 13594 ACQUIRE_LOCK(ump); 13595 if (softdep_excess_items(ump, D_INODEDEP)) { 13596 req = true; 13597 request_cleanup(mp, FLUSH_INODES); 13598 } 13599 if (softdep_excess_items(ump, D_DIRREM)) { 13600 req = true; 13601 request_cleanup(mp, FLUSH_BLOCKS); 13602 } 13603 FREE_LOCK(ump); 13604 if (softdep_excess_items(ump, D_NEWBLK) || 13605 softdep_excess_items(ump, D_ALLOCDIRECT) || 13606 softdep_excess_items(ump, D_ALLOCINDIR)) { 13607 error = vn_start_write(NULL, &mp, 13608 V_WAIT); 13609 if (error == 0) { 13610 req = true; 13611 VFS_SYNC(mp, MNT_WAIT); 13612 vn_finished_write(mp); 13613 } 13614 } 13615 if ((td->td_pflags & TDP_KTHREAD) != 0 || !req) 13616 break; 13617 } 13618 } 13619 vfs_unbusy(mp); 13620 } 13621 if ((mp = td->td_su) != NULL) { 13622 td->td_su = NULL; 13623 vfs_rel(mp); 13624 } 13625 } 13626 13627 /* 13628 * If memory utilization has gotten too high, deliberately slow things 13629 * down and speed up the I/O processing. 13630 */ 13631 static int 13632 request_cleanup(mp, resource) 13633 struct mount *mp; 13634 int resource; 13635 { 13636 struct thread *td = curthread; 13637 struct ufsmount *ump; 13638 13639 ump = VFSTOUFS(mp); 13640 LOCK_OWNED(ump); 13641 /* 13642 * We never hold up the filesystem syncer or buf daemon. 13643 */ 13644 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF)) 13645 return (0); 13646 /* 13647 * First check to see if the work list has gotten backlogged. 13648 * If it has, co-opt this process to help clean up two entries. 13649 * Because this process may hold inodes locked, we cannot 13650 * handle any remove requests that might block on a locked 13651 * inode as that could lead to deadlock. We set TDP_SOFTDEP 13652 * to avoid recursively processing the worklist. 13653 */ 13654 if (ump->softdep_on_worklist > max_softdeps / 10) { 13655 td->td_pflags |= TDP_SOFTDEP; 13656 process_worklist_item(mp, 2, LK_NOWAIT); 13657 td->td_pflags &= ~TDP_SOFTDEP; 13658 stat_worklist_push += 2; 13659 return(1); 13660 } 13661 /* 13662 * Next, we attempt to speed up the syncer process. If that 13663 * is successful, then we allow the process to continue. 13664 */ 13665 if (softdep_speedup(ump) && 13666 resource != FLUSH_BLOCKS_WAIT && 13667 resource != FLUSH_INODES_WAIT) 13668 return(0); 13669 /* 13670 * If we are resource constrained on inode dependencies, try 13671 * flushing some dirty inodes. Otherwise, we are constrained 13672 * by file deletions, so try accelerating flushes of directories 13673 * with removal dependencies. We would like to do the cleanup 13674 * here, but we probably hold an inode locked at this point and 13675 * that might deadlock against one that we try to clean. So, 13676 * the best that we can do is request the syncer daemon to do 13677 * the cleanup for us. 13678 */ 13679 switch (resource) { 13680 13681 case FLUSH_INODES: 13682 case FLUSH_INODES_WAIT: 13683 ACQUIRE_GBLLOCK(&lk); 13684 stat_ino_limit_push += 1; 13685 req_clear_inodedeps += 1; 13686 FREE_GBLLOCK(&lk); 13687 stat_countp = &stat_ino_limit_hit; 13688 break; 13689 13690 case FLUSH_BLOCKS: 13691 case FLUSH_BLOCKS_WAIT: 13692 ACQUIRE_GBLLOCK(&lk); 13693 stat_blk_limit_push += 1; 13694 req_clear_remove += 1; 13695 FREE_GBLLOCK(&lk); 13696 stat_countp = &stat_blk_limit_hit; 13697 break; 13698 13699 default: 13700 panic("request_cleanup: unknown type"); 13701 } 13702 /* 13703 * Hopefully the syncer daemon will catch up and awaken us. 13704 * We wait at most tickdelay before proceeding in any case. 13705 */ 13706 ACQUIRE_GBLLOCK(&lk); 13707 FREE_LOCK(ump); 13708 proc_waiting += 1; 13709 if (callout_pending(&softdep_callout) == FALSE) 13710 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2, 13711 pause_timer, 0); 13712 13713 if ((td->td_pflags & TDP_KTHREAD) == 0) 13714 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0); 13715 proc_waiting -= 1; 13716 FREE_GBLLOCK(&lk); 13717 ACQUIRE_LOCK(ump); 13718 return (1); 13719 } 13720 13721 /* 13722 * Awaken processes pausing in request_cleanup and clear proc_waiting 13723 * to indicate that there is no longer a timer running. Pause_timer 13724 * will be called with the global softdep mutex (&lk) locked. 13725 */ 13726 static void 13727 pause_timer(arg) 13728 void *arg; 13729 { 13730 13731 GBLLOCK_OWNED(&lk); 13732 /* 13733 * The callout_ API has acquired mtx and will hold it around this 13734 * function call. 13735 */ 13736 *stat_countp += proc_waiting; 13737 wakeup(&proc_waiting); 13738 } 13739 13740 /* 13741 * If requested, try removing inode or removal dependencies. 13742 */ 13743 static void 13744 check_clear_deps(mp) 13745 struct mount *mp; 13746 { 13747 struct ufsmount *ump; 13748 size_t resid; 13749 13750 /* 13751 * Tell the lower layers that any TRIM or WRITE transactions 13752 * that have been delayed for performance reasons should 13753 * proceed to help alleviate the shortage faster. 13754 */ 13755 ump = VFSTOUFS(mp); 13756 FREE_LOCK(ump); 13757 g_io_speedup(0, BIO_SPEEDUP_TRIM | BIO_SPEEDUP_WRITE, &resid, ump->um_cp); 13758 ACQUIRE_LOCK(ump); 13759 13760 13761 /* 13762 * If we are suspended, it may be because of our using 13763 * too many inodedeps, so help clear them out. 13764 */ 13765 if (MOUNTEDSUJ(mp) && VFSTOUFS(mp)->softdep_jblocks->jb_suspended) 13766 clear_inodedeps(mp); 13767 13768 /* 13769 * General requests for cleanup of backed up dependencies 13770 */ 13771 ACQUIRE_GBLLOCK(&lk); 13772 if (req_clear_inodedeps) { 13773 req_clear_inodedeps -= 1; 13774 FREE_GBLLOCK(&lk); 13775 clear_inodedeps(mp); 13776 ACQUIRE_GBLLOCK(&lk); 13777 wakeup(&proc_waiting); 13778 } 13779 if (req_clear_remove) { 13780 req_clear_remove -= 1; 13781 FREE_GBLLOCK(&lk); 13782 clear_remove(mp); 13783 ACQUIRE_GBLLOCK(&lk); 13784 wakeup(&proc_waiting); 13785 } 13786 FREE_GBLLOCK(&lk); 13787 } 13788 13789 /* 13790 * Flush out a directory with at least one removal dependency in an effort to 13791 * reduce the number of dirrem, freefile, and freeblks dependency structures. 13792 */ 13793 static void 13794 clear_remove(mp) 13795 struct mount *mp; 13796 { 13797 struct pagedep_hashhead *pagedephd; 13798 struct pagedep *pagedep; 13799 struct ufsmount *ump; 13800 struct vnode *vp; 13801 struct bufobj *bo; 13802 int error, cnt; 13803 ino_t ino; 13804 13805 ump = VFSTOUFS(mp); 13806 LOCK_OWNED(ump); 13807 13808 for (cnt = 0; cnt <= ump->pagedep_hash_size; cnt++) { 13809 pagedephd = &ump->pagedep_hashtbl[ump->pagedep_nextclean++]; 13810 if (ump->pagedep_nextclean > ump->pagedep_hash_size) 13811 ump->pagedep_nextclean = 0; 13812 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 13813 if (LIST_EMPTY(&pagedep->pd_dirremhd)) 13814 continue; 13815 ino = pagedep->pd_ino; 13816 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 13817 continue; 13818 FREE_LOCK(ump); 13819 13820 /* 13821 * Let unmount clear deps 13822 */ 13823 error = vfs_busy(mp, MBF_NOWAIT); 13824 if (error != 0) 13825 goto finish_write; 13826 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 13827 FFSV_FORCEINSMQ); 13828 vfs_unbusy(mp); 13829 if (error != 0) { 13830 softdep_error("clear_remove: vget", error); 13831 goto finish_write; 13832 } 13833 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 13834 softdep_error("clear_remove: fsync", error); 13835 bo = &vp->v_bufobj; 13836 BO_LOCK(bo); 13837 drain_output(vp); 13838 BO_UNLOCK(bo); 13839 vput(vp); 13840 finish_write: 13841 vn_finished_write(mp); 13842 ACQUIRE_LOCK(ump); 13843 return; 13844 } 13845 } 13846 } 13847 13848 /* 13849 * Clear out a block of dirty inodes in an effort to reduce 13850 * the number of inodedep dependency structures. 13851 */ 13852 static void 13853 clear_inodedeps(mp) 13854 struct mount *mp; 13855 { 13856 struct inodedep_hashhead *inodedephd; 13857 struct inodedep *inodedep; 13858 struct ufsmount *ump; 13859 struct vnode *vp; 13860 struct fs *fs; 13861 int error, cnt; 13862 ino_t firstino, lastino, ino; 13863 13864 ump = VFSTOUFS(mp); 13865 fs = ump->um_fs; 13866 LOCK_OWNED(ump); 13867 /* 13868 * Pick a random inode dependency to be cleared. 13869 * We will then gather up all the inodes in its block 13870 * that have dependencies and flush them out. 13871 */ 13872 for (cnt = 0; cnt <= ump->inodedep_hash_size; cnt++) { 13873 inodedephd = &ump->inodedep_hashtbl[ump->inodedep_nextclean++]; 13874 if (ump->inodedep_nextclean > ump->inodedep_hash_size) 13875 ump->inodedep_nextclean = 0; 13876 if ((inodedep = LIST_FIRST(inodedephd)) != NULL) 13877 break; 13878 } 13879 if (inodedep == NULL) 13880 return; 13881 /* 13882 * Find the last inode in the block with dependencies. 13883 */ 13884 firstino = rounddown2(inodedep->id_ino, INOPB(fs)); 13885 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--) 13886 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0) 13887 break; 13888 /* 13889 * Asynchronously push all but the last inode with dependencies. 13890 * Synchronously push the last inode with dependencies to ensure 13891 * that the inode block gets written to free up the inodedeps. 13892 */ 13893 for (ino = firstino; ino <= lastino; ino++) { 13894 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 13895 continue; 13896 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 13897 continue; 13898 FREE_LOCK(ump); 13899 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */ 13900 if (error != 0) { 13901 vn_finished_write(mp); 13902 ACQUIRE_LOCK(ump); 13903 return; 13904 } 13905 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 13906 FFSV_FORCEINSMQ)) != 0) { 13907 softdep_error("clear_inodedeps: vget", error); 13908 vfs_unbusy(mp); 13909 vn_finished_write(mp); 13910 ACQUIRE_LOCK(ump); 13911 return; 13912 } 13913 vfs_unbusy(mp); 13914 if (ino == lastino) { 13915 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0))) 13916 softdep_error("clear_inodedeps: fsync1", error); 13917 } else { 13918 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 13919 softdep_error("clear_inodedeps: fsync2", error); 13920 BO_LOCK(&vp->v_bufobj); 13921 drain_output(vp); 13922 BO_UNLOCK(&vp->v_bufobj); 13923 } 13924 vput(vp); 13925 vn_finished_write(mp); 13926 ACQUIRE_LOCK(ump); 13927 } 13928 } 13929 13930 void 13931 softdep_buf_append(bp, wkhd) 13932 struct buf *bp; 13933 struct workhead *wkhd; 13934 { 13935 struct worklist *wk; 13936 struct ufsmount *ump; 13937 13938 if ((wk = LIST_FIRST(wkhd)) == NULL) 13939 return; 13940 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 13941 ("softdep_buf_append called on non-softdep filesystem")); 13942 ump = VFSTOUFS(wk->wk_mp); 13943 ACQUIRE_LOCK(ump); 13944 while ((wk = LIST_FIRST(wkhd)) != NULL) { 13945 WORKLIST_REMOVE(wk); 13946 WORKLIST_INSERT(&bp->b_dep, wk); 13947 } 13948 FREE_LOCK(ump); 13949 13950 } 13951 13952 void 13953 softdep_inode_append(ip, cred, wkhd) 13954 struct inode *ip; 13955 struct ucred *cred; 13956 struct workhead *wkhd; 13957 { 13958 struct buf *bp; 13959 struct fs *fs; 13960 struct ufsmount *ump; 13961 int error; 13962 13963 ump = ITOUMP(ip); 13964 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 13965 ("softdep_inode_append called on non-softdep filesystem")); 13966 fs = ump->um_fs; 13967 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 13968 (int)fs->fs_bsize, cred, &bp); 13969 if (error) { 13970 bqrelse(bp); 13971 softdep_freework(wkhd); 13972 return; 13973 } 13974 softdep_buf_append(bp, wkhd); 13975 bqrelse(bp); 13976 } 13977 13978 void 13979 softdep_freework(wkhd) 13980 struct workhead *wkhd; 13981 { 13982 struct worklist *wk; 13983 struct ufsmount *ump; 13984 13985 if ((wk = LIST_FIRST(wkhd)) == NULL) 13986 return; 13987 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 13988 ("softdep_freework called on non-softdep filesystem")); 13989 ump = VFSTOUFS(wk->wk_mp); 13990 ACQUIRE_LOCK(ump); 13991 handle_jwork(wkhd); 13992 FREE_LOCK(ump); 13993 } 13994 13995 static struct ufsmount * 13996 softdep_bp_to_mp(bp) 13997 struct buf *bp; 13998 { 13999 struct mount *mp; 14000 struct vnode *vp; 14001 14002 if (LIST_EMPTY(&bp->b_dep)) 14003 return (NULL); 14004 vp = bp->b_vp; 14005 KASSERT(vp != NULL, 14006 ("%s, buffer with dependencies lacks vnode", __func__)); 14007 14008 /* 14009 * The ump mount point is stable after we get a correct 14010 * pointer, since bp is locked and this prevents unmount from 14011 * proceeding. But to get to it, we cannot dereference bp->b_dep 14012 * head wk_mp, because we do not yet own SU ump lock and 14013 * workitem might be freed while dereferenced. 14014 */ 14015 retry: 14016 switch (vp->v_type) { 14017 case VCHR: 14018 VI_LOCK(vp); 14019 mp = vp->v_type == VCHR ? vp->v_rdev->si_mountpt : NULL; 14020 VI_UNLOCK(vp); 14021 if (mp == NULL) 14022 goto retry; 14023 break; 14024 case VREG: 14025 case VDIR: 14026 case VLNK: 14027 case VFIFO: 14028 case VSOCK: 14029 mp = vp->v_mount; 14030 break; 14031 case VBLK: 14032 vn_printf(vp, "softdep_bp_to_mp: unexpected block device\n"); 14033 /* FALLTHROUGH */ 14034 case VNON: 14035 case VBAD: 14036 case VMARKER: 14037 mp = NULL; 14038 break; 14039 default: 14040 vn_printf(vp, "unknown vnode type"); 14041 mp = NULL; 14042 break; 14043 } 14044 return (VFSTOUFS(mp)); 14045 } 14046 14047 /* 14048 * Function to determine if the buffer has outstanding dependencies 14049 * that will cause a roll-back if the buffer is written. If wantcount 14050 * is set, return number of dependencies, otherwise just yes or no. 14051 */ 14052 static int 14053 softdep_count_dependencies(bp, wantcount) 14054 struct buf *bp; 14055 int wantcount; 14056 { 14057 struct worklist *wk; 14058 struct ufsmount *ump; 14059 struct bmsafemap *bmsafemap; 14060 struct freework *freework; 14061 struct inodedep *inodedep; 14062 struct indirdep *indirdep; 14063 struct freeblks *freeblks; 14064 struct allocindir *aip; 14065 struct pagedep *pagedep; 14066 struct dirrem *dirrem; 14067 struct newblk *newblk; 14068 struct mkdir *mkdir; 14069 struct diradd *dap; 14070 int i, retval; 14071 14072 ump = softdep_bp_to_mp(bp); 14073 if (ump == NULL) 14074 return (0); 14075 retval = 0; 14076 ACQUIRE_LOCK(ump); 14077 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 14078 switch (wk->wk_type) { 14079 14080 case D_INODEDEP: 14081 inodedep = WK_INODEDEP(wk); 14082 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 14083 /* bitmap allocation dependency */ 14084 retval += 1; 14085 if (!wantcount) 14086 goto out; 14087 } 14088 if (TAILQ_FIRST(&inodedep->id_inoupdt)) { 14089 /* direct block pointer dependency */ 14090 retval += 1; 14091 if (!wantcount) 14092 goto out; 14093 } 14094 if (TAILQ_FIRST(&inodedep->id_extupdt)) { 14095 /* direct block pointer dependency */ 14096 retval += 1; 14097 if (!wantcount) 14098 goto out; 14099 } 14100 if (TAILQ_FIRST(&inodedep->id_inoreflst)) { 14101 /* Add reference dependency. */ 14102 retval += 1; 14103 if (!wantcount) 14104 goto out; 14105 } 14106 continue; 14107 14108 case D_INDIRDEP: 14109 indirdep = WK_INDIRDEP(wk); 14110 14111 TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) { 14112 /* indirect truncation dependency */ 14113 retval += 1; 14114 if (!wantcount) 14115 goto out; 14116 } 14117 14118 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 14119 /* indirect block pointer dependency */ 14120 retval += 1; 14121 if (!wantcount) 14122 goto out; 14123 } 14124 continue; 14125 14126 case D_PAGEDEP: 14127 pagedep = WK_PAGEDEP(wk); 14128 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) { 14129 if (LIST_FIRST(&dirrem->dm_jremrefhd)) { 14130 /* Journal remove ref dependency. */ 14131 retval += 1; 14132 if (!wantcount) 14133 goto out; 14134 } 14135 } 14136 for (i = 0; i < DAHASHSZ; i++) { 14137 14138 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 14139 /* directory entry dependency */ 14140 retval += 1; 14141 if (!wantcount) 14142 goto out; 14143 } 14144 } 14145 continue; 14146 14147 case D_BMSAFEMAP: 14148 bmsafemap = WK_BMSAFEMAP(wk); 14149 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) { 14150 /* Add reference dependency. */ 14151 retval += 1; 14152 if (!wantcount) 14153 goto out; 14154 } 14155 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) { 14156 /* Allocate block dependency. */ 14157 retval += 1; 14158 if (!wantcount) 14159 goto out; 14160 } 14161 continue; 14162 14163 case D_FREEBLKS: 14164 freeblks = WK_FREEBLKS(wk); 14165 if (LIST_FIRST(&freeblks->fb_jblkdephd)) { 14166 /* Freeblk journal dependency. */ 14167 retval += 1; 14168 if (!wantcount) 14169 goto out; 14170 } 14171 continue; 14172 14173 case D_ALLOCDIRECT: 14174 case D_ALLOCINDIR: 14175 newblk = WK_NEWBLK(wk); 14176 if (newblk->nb_jnewblk) { 14177 /* Journal allocate dependency. */ 14178 retval += 1; 14179 if (!wantcount) 14180 goto out; 14181 } 14182 continue; 14183 14184 case D_MKDIR: 14185 mkdir = WK_MKDIR(wk); 14186 if (mkdir->md_jaddref) { 14187 /* Journal reference dependency. */ 14188 retval += 1; 14189 if (!wantcount) 14190 goto out; 14191 } 14192 continue; 14193 14194 case D_FREEWORK: 14195 case D_FREEDEP: 14196 case D_JSEGDEP: 14197 case D_JSEG: 14198 case D_SBDEP: 14199 /* never a dependency on these blocks */ 14200 continue; 14201 14202 default: 14203 panic("softdep_count_dependencies: Unexpected type %s", 14204 TYPENAME(wk->wk_type)); 14205 /* NOTREACHED */ 14206 } 14207 } 14208 out: 14209 FREE_LOCK(ump); 14210 return (retval); 14211 } 14212 14213 /* 14214 * Acquire exclusive access to a buffer. 14215 * Must be called with a locked mtx parameter. 14216 * Return acquired buffer or NULL on failure. 14217 */ 14218 static struct buf * 14219 getdirtybuf(bp, lock, waitfor) 14220 struct buf *bp; 14221 struct rwlock *lock; 14222 int waitfor; 14223 { 14224 int error; 14225 14226 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) { 14227 if (waitfor != MNT_WAIT) 14228 return (NULL); 14229 error = BUF_LOCK(bp, 14230 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock); 14231 /* 14232 * Even if we successfully acquire bp here, we have dropped 14233 * lock, which may violates our guarantee. 14234 */ 14235 if (error == 0) 14236 BUF_UNLOCK(bp); 14237 else if (error != ENOLCK) 14238 panic("getdirtybuf: inconsistent lock: %d", error); 14239 rw_wlock(lock); 14240 return (NULL); 14241 } 14242 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 14243 if (lock != BO_LOCKPTR(bp->b_bufobj) && waitfor == MNT_WAIT) { 14244 rw_wunlock(lock); 14245 BO_LOCK(bp->b_bufobj); 14246 BUF_UNLOCK(bp); 14247 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 14248 bp->b_vflags |= BV_BKGRDWAIT; 14249 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), 14250 PRIBIO | PDROP, "getbuf", 0); 14251 } else 14252 BO_UNLOCK(bp->b_bufobj); 14253 rw_wlock(lock); 14254 return (NULL); 14255 } 14256 BUF_UNLOCK(bp); 14257 if (waitfor != MNT_WAIT) 14258 return (NULL); 14259 #ifdef DEBUG_VFS_LOCKS 14260 if (bp->b_vp->v_type != VCHR) 14261 ASSERT_BO_WLOCKED(bp->b_bufobj); 14262 #endif 14263 bp->b_vflags |= BV_BKGRDWAIT; 14264 rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0); 14265 return (NULL); 14266 } 14267 if ((bp->b_flags & B_DELWRI) == 0) { 14268 BUF_UNLOCK(bp); 14269 return (NULL); 14270 } 14271 bremfree(bp); 14272 return (bp); 14273 } 14274 14275 14276 /* 14277 * Check if it is safe to suspend the file system now. On entry, 14278 * the vnode interlock for devvp should be held. Return 0 with 14279 * the mount interlock held if the file system can be suspended now, 14280 * otherwise return EAGAIN with the mount interlock held. 14281 */ 14282 int 14283 softdep_check_suspend(struct mount *mp, 14284 struct vnode *devvp, 14285 int softdep_depcnt, 14286 int softdep_accdepcnt, 14287 int secondary_writes, 14288 int secondary_accwrites) 14289 { 14290 struct bufobj *bo; 14291 struct ufsmount *ump; 14292 struct inodedep *inodedep; 14293 int error, unlinked; 14294 14295 bo = &devvp->v_bufobj; 14296 ASSERT_BO_WLOCKED(bo); 14297 14298 /* 14299 * If we are not running with soft updates, then we need only 14300 * deal with secondary writes as we try to suspend. 14301 */ 14302 if (MOUNTEDSOFTDEP(mp) == 0) { 14303 MNT_ILOCK(mp); 14304 while (mp->mnt_secondary_writes != 0) { 14305 BO_UNLOCK(bo); 14306 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 14307 (PUSER - 1) | PDROP, "secwr", 0); 14308 BO_LOCK(bo); 14309 MNT_ILOCK(mp); 14310 } 14311 14312 /* 14313 * Reasons for needing more work before suspend: 14314 * - Dirty buffers on devvp. 14315 * - Secondary writes occurred after start of vnode sync loop 14316 */ 14317 error = 0; 14318 if (bo->bo_numoutput > 0 || 14319 bo->bo_dirty.bv_cnt > 0 || 14320 secondary_writes != 0 || 14321 mp->mnt_secondary_writes != 0 || 14322 secondary_accwrites != mp->mnt_secondary_accwrites) 14323 error = EAGAIN; 14324 BO_UNLOCK(bo); 14325 return (error); 14326 } 14327 14328 /* 14329 * If we are running with soft updates, then we need to coordinate 14330 * with them as we try to suspend. 14331 */ 14332 ump = VFSTOUFS(mp); 14333 for (;;) { 14334 if (!TRY_ACQUIRE_LOCK(ump)) { 14335 BO_UNLOCK(bo); 14336 ACQUIRE_LOCK(ump); 14337 FREE_LOCK(ump); 14338 BO_LOCK(bo); 14339 continue; 14340 } 14341 MNT_ILOCK(mp); 14342 if (mp->mnt_secondary_writes != 0) { 14343 FREE_LOCK(ump); 14344 BO_UNLOCK(bo); 14345 msleep(&mp->mnt_secondary_writes, 14346 MNT_MTX(mp), 14347 (PUSER - 1) | PDROP, "secwr", 0); 14348 BO_LOCK(bo); 14349 continue; 14350 } 14351 break; 14352 } 14353 14354 unlinked = 0; 14355 if (MOUNTEDSUJ(mp)) { 14356 for (inodedep = TAILQ_FIRST(&ump->softdep_unlinked); 14357 inodedep != NULL; 14358 inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 14359 if ((inodedep->id_state & (UNLINKED | UNLINKLINKS | 14360 UNLINKONLIST)) != (UNLINKED | UNLINKLINKS | 14361 UNLINKONLIST) || 14362 !check_inodedep_free(inodedep)) 14363 continue; 14364 unlinked++; 14365 } 14366 } 14367 14368 /* 14369 * Reasons for needing more work before suspend: 14370 * - Dirty buffers on devvp. 14371 * - Softdep activity occurred after start of vnode sync loop 14372 * - Secondary writes occurred after start of vnode sync loop 14373 */ 14374 error = 0; 14375 if (bo->bo_numoutput > 0 || 14376 bo->bo_dirty.bv_cnt > 0 || 14377 softdep_depcnt != unlinked || 14378 ump->softdep_deps != unlinked || 14379 softdep_accdepcnt != ump->softdep_accdeps || 14380 secondary_writes != 0 || 14381 mp->mnt_secondary_writes != 0 || 14382 secondary_accwrites != mp->mnt_secondary_accwrites) 14383 error = EAGAIN; 14384 FREE_LOCK(ump); 14385 BO_UNLOCK(bo); 14386 return (error); 14387 } 14388 14389 14390 /* 14391 * Get the number of dependency structures for the file system, both 14392 * the current number and the total number allocated. These will 14393 * later be used to detect that softdep processing has occurred. 14394 */ 14395 void 14396 softdep_get_depcounts(struct mount *mp, 14397 int *softdep_depsp, 14398 int *softdep_accdepsp) 14399 { 14400 struct ufsmount *ump; 14401 14402 if (MOUNTEDSOFTDEP(mp) == 0) { 14403 *softdep_depsp = 0; 14404 *softdep_accdepsp = 0; 14405 return; 14406 } 14407 ump = VFSTOUFS(mp); 14408 ACQUIRE_LOCK(ump); 14409 *softdep_depsp = ump->softdep_deps; 14410 *softdep_accdepsp = ump->softdep_accdeps; 14411 FREE_LOCK(ump); 14412 } 14413 14414 /* 14415 * Wait for pending output on a vnode to complete. 14416 */ 14417 static void 14418 drain_output(vp) 14419 struct vnode *vp; 14420 { 14421 14422 ASSERT_VOP_LOCKED(vp, "drain_output"); 14423 (void)bufobj_wwait(&vp->v_bufobj, 0, 0); 14424 } 14425 14426 /* 14427 * Called whenever a buffer that is being invalidated or reallocated 14428 * contains dependencies. This should only happen if an I/O error has 14429 * occurred. The routine is called with the buffer locked. 14430 */ 14431 static void 14432 softdep_deallocate_dependencies(bp) 14433 struct buf *bp; 14434 { 14435 14436 if ((bp->b_ioflags & BIO_ERROR) == 0) 14437 panic("softdep_deallocate_dependencies: dangling deps"); 14438 if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL) 14439 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error); 14440 else 14441 printf("softdep_deallocate_dependencies: " 14442 "got error %d while accessing filesystem\n", bp->b_error); 14443 if (bp->b_error != ENXIO) 14444 panic("softdep_deallocate_dependencies: unrecovered I/O error"); 14445 } 14446 14447 /* 14448 * Function to handle asynchronous write errors in the filesystem. 14449 */ 14450 static void 14451 softdep_error(func, error) 14452 char *func; 14453 int error; 14454 { 14455 14456 /* XXX should do something better! */ 14457 printf("%s: got error %d while accessing filesystem\n", func, error); 14458 } 14459 14460 #ifdef DDB 14461 14462 /* exported to ffs_vfsops.c */ 14463 extern void db_print_ffs(struct ufsmount *ump); 14464 void 14465 db_print_ffs(struct ufsmount *ump) 14466 { 14467 db_printf("mp %p (%s) devvp %p\n", ump->um_mountp, 14468 ump->um_mountp->mnt_stat.f_mntonname, ump->um_devvp); 14469 db_printf(" fs %p su_wl %d su_deps %d su_req %d\n", 14470 ump->um_fs, ump->softdep_on_worklist, 14471 ump->softdep_deps, ump->softdep_req); 14472 } 14473 14474 static void 14475 worklist_print(struct worklist *wk, int verbose) 14476 { 14477 14478 if (!verbose) { 14479 db_printf("%s: %p state 0x%b\n", TYPENAME(wk->wk_type), wk, 14480 (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS); 14481 return; 14482 } 14483 db_printf("worklist: %p type %s state 0x%b next %p\n ", wk, 14484 TYPENAME(wk->wk_type), (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS, 14485 LIST_NEXT(wk, wk_list)); 14486 db_print_ffs(VFSTOUFS(wk->wk_mp)); 14487 } 14488 14489 static void 14490 inodedep_print(struct inodedep *inodedep, int verbose) 14491 { 14492 14493 worklist_print(&inodedep->id_list, 0); 14494 db_printf(" fs %p ino %jd inoblk %jd delta %jd nlink %jd\n", 14495 inodedep->id_fs, 14496 (intmax_t)inodedep->id_ino, 14497 (intmax_t)fsbtodb(inodedep->id_fs, 14498 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)), 14499 (intmax_t)inodedep->id_nlinkdelta, 14500 (intmax_t)inodedep->id_savednlink); 14501 14502 if (verbose == 0) 14503 return; 14504 14505 db_printf(" bmsafemap %p, mkdiradd %p, inoreflst %p\n", 14506 inodedep->id_bmsafemap, 14507 inodedep->id_mkdiradd, 14508 TAILQ_FIRST(&inodedep->id_inoreflst)); 14509 db_printf(" dirremhd %p, pendinghd %p, bufwait %p\n", 14510 LIST_FIRST(&inodedep->id_dirremhd), 14511 LIST_FIRST(&inodedep->id_pendinghd), 14512 LIST_FIRST(&inodedep->id_bufwait)); 14513 db_printf(" inowait %p, inoupdt %p, newinoupdt %p\n", 14514 LIST_FIRST(&inodedep->id_inowait), 14515 TAILQ_FIRST(&inodedep->id_inoupdt), 14516 TAILQ_FIRST(&inodedep->id_newinoupdt)); 14517 db_printf(" extupdt %p, newextupdt %p, freeblklst %p\n", 14518 TAILQ_FIRST(&inodedep->id_extupdt), 14519 TAILQ_FIRST(&inodedep->id_newextupdt), 14520 TAILQ_FIRST(&inodedep->id_freeblklst)); 14521 db_printf(" saveino %p, savedsize %jd, savedextsize %jd\n", 14522 inodedep->id_savedino1, 14523 (intmax_t)inodedep->id_savedsize, 14524 (intmax_t)inodedep->id_savedextsize); 14525 } 14526 14527 static void 14528 newblk_print(struct newblk *nbp) 14529 { 14530 14531 worklist_print(&nbp->nb_list, 0); 14532 db_printf(" newblkno %jd\n", (intmax_t)nbp->nb_newblkno); 14533 db_printf(" jnewblk %p, bmsafemap %p, freefrag %p\n", 14534 &nbp->nb_jnewblk, 14535 &nbp->nb_bmsafemap, 14536 &nbp->nb_freefrag); 14537 db_printf(" indirdeps %p, newdirblk %p, jwork %p\n", 14538 LIST_FIRST(&nbp->nb_indirdeps), 14539 LIST_FIRST(&nbp->nb_newdirblk), 14540 LIST_FIRST(&nbp->nb_jwork)); 14541 } 14542 14543 static void 14544 allocdirect_print(struct allocdirect *adp) 14545 { 14546 14547 newblk_print(&adp->ad_block); 14548 db_printf(" oldblkno %jd, oldsize %ld, newsize %ld\n", 14549 adp->ad_oldblkno, adp->ad_oldsize, adp->ad_newsize); 14550 db_printf(" offset %d, inodedep %p\n", 14551 adp->ad_offset, adp->ad_inodedep); 14552 } 14553 14554 static void 14555 allocindir_print(struct allocindir *aip) 14556 { 14557 14558 newblk_print(&aip->ai_block); 14559 db_printf(" oldblkno %jd, lbn %jd\n", 14560 (intmax_t)aip->ai_oldblkno, (intmax_t)aip->ai_lbn); 14561 db_printf(" offset %d, indirdep %p\n", 14562 aip->ai_offset, aip->ai_indirdep); 14563 } 14564 14565 static void 14566 mkdir_print(struct mkdir *mkdir) 14567 { 14568 14569 worklist_print(&mkdir->md_list, 0); 14570 db_printf(" diradd %p, jaddref %p, buf %p\n", 14571 mkdir->md_diradd, mkdir->md_jaddref, mkdir->md_buf); 14572 } 14573 14574 DB_SHOW_COMMAND(sd_inodedep, db_show_sd_inodedep) 14575 { 14576 14577 if (have_addr == 0) { 14578 db_printf("inodedep address required\n"); 14579 return; 14580 } 14581 inodedep_print((struct inodedep*)addr, 1); 14582 } 14583 14584 DB_SHOW_COMMAND(sd_allinodedeps, db_show_sd_allinodedeps) 14585 { 14586 struct inodedep_hashhead *inodedephd; 14587 struct inodedep *inodedep; 14588 struct ufsmount *ump; 14589 int cnt; 14590 14591 if (have_addr == 0) { 14592 db_printf("ufsmount address required\n"); 14593 return; 14594 } 14595 ump = (struct ufsmount *)addr; 14596 for (cnt = 0; cnt < ump->inodedep_hash_size; cnt++) { 14597 inodedephd = &ump->inodedep_hashtbl[cnt]; 14598 LIST_FOREACH(inodedep, inodedephd, id_hash) { 14599 inodedep_print(inodedep, 0); 14600 } 14601 } 14602 } 14603 14604 DB_SHOW_COMMAND(sd_worklist, db_show_sd_worklist) 14605 { 14606 14607 if (have_addr == 0) { 14608 db_printf("worklist address required\n"); 14609 return; 14610 } 14611 worklist_print((struct worklist *)addr, 1); 14612 } 14613 14614 DB_SHOW_COMMAND(sd_workhead, db_show_sd_workhead) 14615 { 14616 struct worklist *wk; 14617 struct workhead *wkhd; 14618 14619 if (have_addr == 0) { 14620 db_printf("worklist address required " 14621 "(for example value in bp->b_dep)\n"); 14622 return; 14623 } 14624 /* 14625 * We often do not have the address of the worklist head but 14626 * instead a pointer to its first entry (e.g., we have the 14627 * contents of bp->b_dep rather than &bp->b_dep). But the back 14628 * pointer of bp->b_dep will point at the head of the list, so 14629 * we cheat and use that instead. If we are in the middle of 14630 * a list we will still get the same result, so nothing 14631 * unexpected will result. 14632 */ 14633 wk = (struct worklist *)addr; 14634 if (wk == NULL) 14635 return; 14636 wkhd = (struct workhead *)wk->wk_list.le_prev; 14637 LIST_FOREACH(wk, wkhd, wk_list) { 14638 switch(wk->wk_type) { 14639 case D_INODEDEP: 14640 inodedep_print(WK_INODEDEP(wk), 0); 14641 continue; 14642 case D_ALLOCDIRECT: 14643 allocdirect_print(WK_ALLOCDIRECT(wk)); 14644 continue; 14645 case D_ALLOCINDIR: 14646 allocindir_print(WK_ALLOCINDIR(wk)); 14647 continue; 14648 case D_MKDIR: 14649 mkdir_print(WK_MKDIR(wk)); 14650 continue; 14651 default: 14652 worklist_print(wk, 0); 14653 continue; 14654 } 14655 } 14656 } 14657 14658 DB_SHOW_COMMAND(sd_mkdir, db_show_sd_mkdir) 14659 { 14660 if (have_addr == 0) { 14661 db_printf("mkdir address required\n"); 14662 return; 14663 } 14664 mkdir_print((struct mkdir *)addr); 14665 } 14666 14667 DB_SHOW_COMMAND(sd_mkdir_list, db_show_sd_mkdir_list) 14668 { 14669 struct mkdirlist *mkdirlisthd; 14670 struct mkdir *mkdir; 14671 14672 if (have_addr == 0) { 14673 db_printf("mkdir listhead address required\n"); 14674 return; 14675 } 14676 mkdirlisthd = (struct mkdirlist *)addr; 14677 LIST_FOREACH(mkdir, mkdirlisthd, md_mkdirs) { 14678 mkdir_print(mkdir); 14679 if (mkdir->md_diradd != NULL) { 14680 db_printf(" "); 14681 worklist_print(&mkdir->md_diradd->da_list, 0); 14682 } 14683 if (mkdir->md_jaddref != NULL) { 14684 db_printf(" "); 14685 worklist_print(&mkdir->md_jaddref->ja_list, 0); 14686 } 14687 } 14688 } 14689 14690 DB_SHOW_COMMAND(sd_allocdirect, db_show_sd_allocdirect) 14691 { 14692 if (have_addr == 0) { 14693 db_printf("allocdirect address required\n"); 14694 return; 14695 } 14696 allocdirect_print((struct allocdirect *)addr); 14697 } 14698 14699 DB_SHOW_COMMAND(sd_allocindir, db_show_sd_allocindir) 14700 { 14701 if (have_addr == 0) { 14702 db_printf("allocindir address required\n"); 14703 return; 14704 } 14705 allocindir_print((struct allocindir *)addr); 14706 } 14707 14708 #endif /* DDB */ 14709 14710 #endif /* SOFTUPDATES */ 14711