1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright 1998, 2000 Marshall Kirk McKusick. 5 * Copyright 2009, 2010 Jeffrey W. Roberson <jeff@FreeBSD.org> 6 * All rights reserved. 7 * 8 * The soft updates code is derived from the appendix of a University 9 * of Michigan technical report (Gregory R. Ganger and Yale N. Patt, 10 * "Soft Updates: A Solution to the Metadata Update Problem in File 11 * Systems", CSE-TR-254-95, August 1995). 12 * 13 * Further information about soft updates can be obtained from: 14 * 15 * Marshall Kirk McKusick http://www.mckusick.com/softdep/ 16 * 1614 Oxford Street mckusick@mckusick.com 17 * Berkeley, CA 94709-1608 +1-510-843-9542 18 * USA 19 * 20 * Redistribution and use in source and binary forms, with or without 21 * modification, are permitted provided that the following conditions 22 * are met: 23 * 24 * 1. Redistributions of source code must retain the above copyright 25 * notice, this list of conditions and the following disclaimer. 26 * 2. Redistributions in binary form must reproduce the above copyright 27 * notice, this list of conditions and the following disclaimer in the 28 * documentation and/or other materials provided with the distribution. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 33 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 35 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 36 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 37 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 38 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 39 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * from: @(#)ffs_softdep.c 9.59 (McKusick) 6/21/00 42 */ 43 44 #include <sys/cdefs.h> 45 __FBSDID("$FreeBSD$"); 46 47 #include "opt_ffs.h" 48 #include "opt_quota.h" 49 #include "opt_ddb.h" 50 51 /* 52 * For now we want the safety net that the DEBUG flag provides. 53 */ 54 #ifndef DEBUG 55 #define DEBUG 56 #endif 57 58 #include <sys/param.h> 59 #include <sys/kernel.h> 60 #include <sys/systm.h> 61 #include <sys/bio.h> 62 #include <sys/buf.h> 63 #include <sys/kdb.h> 64 #include <sys/kthread.h> 65 #include <sys/ktr.h> 66 #include <sys/limits.h> 67 #include <sys/lock.h> 68 #include <sys/malloc.h> 69 #include <sys/mount.h> 70 #include <sys/mutex.h> 71 #include <sys/namei.h> 72 #include <sys/priv.h> 73 #include <sys/proc.h> 74 #include <sys/racct.h> 75 #include <sys/rwlock.h> 76 #include <sys/stat.h> 77 #include <sys/sysctl.h> 78 #include <sys/syslog.h> 79 #include <sys/vnode.h> 80 #include <sys/conf.h> 81 82 #include <ufs/ufs/dir.h> 83 #include <ufs/ufs/extattr.h> 84 #include <ufs/ufs/quota.h> 85 #include <ufs/ufs/inode.h> 86 #include <ufs/ufs/ufsmount.h> 87 #include <ufs/ffs/fs.h> 88 #include <ufs/ffs/softdep.h> 89 #include <ufs/ffs/ffs_extern.h> 90 #include <ufs/ufs/ufs_extern.h> 91 92 #include <vm/vm.h> 93 #include <vm/vm_extern.h> 94 #include <vm/vm_object.h> 95 96 #include <geom/geom.h> 97 98 #include <ddb/ddb.h> 99 100 #define KTR_SUJ 0 /* Define to KTR_SPARE. */ 101 102 #ifndef SOFTUPDATES 103 104 int 105 softdep_flushfiles(oldmnt, flags, td) 106 struct mount *oldmnt; 107 int flags; 108 struct thread *td; 109 { 110 111 panic("softdep_flushfiles called"); 112 } 113 114 int 115 softdep_mount(devvp, mp, fs, cred) 116 struct vnode *devvp; 117 struct mount *mp; 118 struct fs *fs; 119 struct ucred *cred; 120 { 121 122 return (0); 123 } 124 125 void 126 softdep_initialize() 127 { 128 129 return; 130 } 131 132 void 133 softdep_uninitialize() 134 { 135 136 return; 137 } 138 139 void 140 softdep_unmount(mp) 141 struct mount *mp; 142 { 143 144 panic("softdep_unmount called"); 145 } 146 147 void 148 softdep_setup_sbupdate(ump, fs, bp) 149 struct ufsmount *ump; 150 struct fs *fs; 151 struct buf *bp; 152 { 153 154 panic("softdep_setup_sbupdate called"); 155 } 156 157 void 158 softdep_setup_inomapdep(bp, ip, newinum, mode) 159 struct buf *bp; 160 struct inode *ip; 161 ino_t newinum; 162 int mode; 163 { 164 165 panic("softdep_setup_inomapdep called"); 166 } 167 168 void 169 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags) 170 struct buf *bp; 171 struct mount *mp; 172 ufs2_daddr_t newblkno; 173 int frags; 174 int oldfrags; 175 { 176 177 panic("softdep_setup_blkmapdep called"); 178 } 179 180 void 181 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 182 struct inode *ip; 183 ufs_lbn_t lbn; 184 ufs2_daddr_t newblkno; 185 ufs2_daddr_t oldblkno; 186 long newsize; 187 long oldsize; 188 struct buf *bp; 189 { 190 191 panic("softdep_setup_allocdirect called"); 192 } 193 194 void 195 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 196 struct inode *ip; 197 ufs_lbn_t lbn; 198 ufs2_daddr_t newblkno; 199 ufs2_daddr_t oldblkno; 200 long newsize; 201 long oldsize; 202 struct buf *bp; 203 { 204 205 panic("softdep_setup_allocext called"); 206 } 207 208 void 209 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 210 struct inode *ip; 211 ufs_lbn_t lbn; 212 struct buf *bp; 213 int ptrno; 214 ufs2_daddr_t newblkno; 215 ufs2_daddr_t oldblkno; 216 struct buf *nbp; 217 { 218 219 panic("softdep_setup_allocindir_page called"); 220 } 221 222 void 223 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 224 struct buf *nbp; 225 struct inode *ip; 226 struct buf *bp; 227 int ptrno; 228 ufs2_daddr_t newblkno; 229 { 230 231 panic("softdep_setup_allocindir_meta called"); 232 } 233 234 void 235 softdep_journal_freeblocks(ip, cred, length, flags) 236 struct inode *ip; 237 struct ucred *cred; 238 off_t length; 239 int flags; 240 { 241 242 panic("softdep_journal_freeblocks called"); 243 } 244 245 void 246 softdep_journal_fsync(ip) 247 struct inode *ip; 248 { 249 250 panic("softdep_journal_fsync called"); 251 } 252 253 void 254 softdep_setup_freeblocks(ip, length, flags) 255 struct inode *ip; 256 off_t length; 257 int flags; 258 { 259 260 panic("softdep_setup_freeblocks called"); 261 } 262 263 void 264 softdep_freefile(pvp, ino, mode) 265 struct vnode *pvp; 266 ino_t ino; 267 int mode; 268 { 269 270 panic("softdep_freefile called"); 271 } 272 273 int 274 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 275 struct buf *bp; 276 struct inode *dp; 277 off_t diroffset; 278 ino_t newinum; 279 struct buf *newdirbp; 280 int isnewblk; 281 { 282 283 panic("softdep_setup_directory_add called"); 284 } 285 286 void 287 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 288 struct buf *bp; 289 struct inode *dp; 290 caddr_t base; 291 caddr_t oldloc; 292 caddr_t newloc; 293 int entrysize; 294 { 295 296 panic("softdep_change_directoryentry_offset called"); 297 } 298 299 void 300 softdep_setup_remove(bp, dp, ip, isrmdir) 301 struct buf *bp; 302 struct inode *dp; 303 struct inode *ip; 304 int isrmdir; 305 { 306 307 panic("softdep_setup_remove called"); 308 } 309 310 void 311 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 312 struct buf *bp; 313 struct inode *dp; 314 struct inode *ip; 315 ino_t newinum; 316 int isrmdir; 317 { 318 319 panic("softdep_setup_directory_change called"); 320 } 321 322 void 323 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 324 struct mount *mp; 325 struct buf *bp; 326 ufs2_daddr_t blkno; 327 int frags; 328 struct workhead *wkhd; 329 { 330 331 panic("%s called", __FUNCTION__); 332 } 333 334 void 335 softdep_setup_inofree(mp, bp, ino, wkhd) 336 struct mount *mp; 337 struct buf *bp; 338 ino_t ino; 339 struct workhead *wkhd; 340 { 341 342 panic("%s called", __FUNCTION__); 343 } 344 345 void 346 softdep_setup_unlink(dp, ip) 347 struct inode *dp; 348 struct inode *ip; 349 { 350 351 panic("%s called", __FUNCTION__); 352 } 353 354 void 355 softdep_setup_link(dp, ip) 356 struct inode *dp; 357 struct inode *ip; 358 { 359 360 panic("%s called", __FUNCTION__); 361 } 362 363 void 364 softdep_revert_link(dp, ip) 365 struct inode *dp; 366 struct inode *ip; 367 { 368 369 panic("%s called", __FUNCTION__); 370 } 371 372 void 373 softdep_setup_rmdir(dp, ip) 374 struct inode *dp; 375 struct inode *ip; 376 { 377 378 panic("%s called", __FUNCTION__); 379 } 380 381 void 382 softdep_revert_rmdir(dp, ip) 383 struct inode *dp; 384 struct inode *ip; 385 { 386 387 panic("%s called", __FUNCTION__); 388 } 389 390 void 391 softdep_setup_create(dp, ip) 392 struct inode *dp; 393 struct inode *ip; 394 { 395 396 panic("%s called", __FUNCTION__); 397 } 398 399 void 400 softdep_revert_create(dp, ip) 401 struct inode *dp; 402 struct inode *ip; 403 { 404 405 panic("%s called", __FUNCTION__); 406 } 407 408 void 409 softdep_setup_mkdir(dp, ip) 410 struct inode *dp; 411 struct inode *ip; 412 { 413 414 panic("%s called", __FUNCTION__); 415 } 416 417 void 418 softdep_revert_mkdir(dp, ip) 419 struct inode *dp; 420 struct inode *ip; 421 { 422 423 panic("%s called", __FUNCTION__); 424 } 425 426 void 427 softdep_setup_dotdot_link(dp, ip) 428 struct inode *dp; 429 struct inode *ip; 430 { 431 432 panic("%s called", __FUNCTION__); 433 } 434 435 int 436 softdep_prealloc(vp, waitok) 437 struct vnode *vp; 438 int waitok; 439 { 440 441 panic("%s called", __FUNCTION__); 442 } 443 444 int 445 softdep_journal_lookup(mp, vpp) 446 struct mount *mp; 447 struct vnode **vpp; 448 { 449 450 return (ENOENT); 451 } 452 453 void 454 softdep_change_linkcnt(ip) 455 struct inode *ip; 456 { 457 458 panic("softdep_change_linkcnt called"); 459 } 460 461 void 462 softdep_load_inodeblock(ip) 463 struct inode *ip; 464 { 465 466 panic("softdep_load_inodeblock called"); 467 } 468 469 void 470 softdep_update_inodeblock(ip, bp, waitfor) 471 struct inode *ip; 472 struct buf *bp; 473 int waitfor; 474 { 475 476 panic("softdep_update_inodeblock called"); 477 } 478 479 int 480 softdep_fsync(vp) 481 struct vnode *vp; /* the "in_core" copy of the inode */ 482 { 483 484 return (0); 485 } 486 487 void 488 softdep_fsync_mountdev(vp) 489 struct vnode *vp; 490 { 491 492 return; 493 } 494 495 int 496 softdep_flushworklist(oldmnt, countp, td) 497 struct mount *oldmnt; 498 int *countp; 499 struct thread *td; 500 { 501 502 *countp = 0; 503 return (0); 504 } 505 506 int 507 softdep_sync_metadata(struct vnode *vp) 508 { 509 510 panic("softdep_sync_metadata called"); 511 } 512 513 int 514 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 515 { 516 517 panic("softdep_sync_buf called"); 518 } 519 520 int 521 softdep_slowdown(vp) 522 struct vnode *vp; 523 { 524 525 panic("softdep_slowdown called"); 526 } 527 528 int 529 softdep_request_cleanup(fs, vp, cred, resource) 530 struct fs *fs; 531 struct vnode *vp; 532 struct ucred *cred; 533 int resource; 534 { 535 536 return (0); 537 } 538 539 int 540 softdep_check_suspend(struct mount *mp, 541 struct vnode *devvp, 542 int softdep_depcnt, 543 int softdep_accdepcnt, 544 int secondary_writes, 545 int secondary_accwrites) 546 { 547 struct bufobj *bo; 548 int error; 549 550 (void) softdep_depcnt, 551 (void) softdep_accdepcnt; 552 553 bo = &devvp->v_bufobj; 554 ASSERT_BO_WLOCKED(bo); 555 556 MNT_ILOCK(mp); 557 while (mp->mnt_secondary_writes != 0) { 558 BO_UNLOCK(bo); 559 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 560 (PUSER - 1) | PDROP, "secwr", 0); 561 BO_LOCK(bo); 562 MNT_ILOCK(mp); 563 } 564 565 /* 566 * Reasons for needing more work before suspend: 567 * - Dirty buffers on devvp. 568 * - Secondary writes occurred after start of vnode sync loop 569 */ 570 error = 0; 571 if (bo->bo_numoutput > 0 || 572 bo->bo_dirty.bv_cnt > 0 || 573 secondary_writes != 0 || 574 mp->mnt_secondary_writes != 0 || 575 secondary_accwrites != mp->mnt_secondary_accwrites) 576 error = EAGAIN; 577 BO_UNLOCK(bo); 578 return (error); 579 } 580 581 void 582 softdep_get_depcounts(struct mount *mp, 583 int *softdepactivep, 584 int *softdepactiveaccp) 585 { 586 (void) mp; 587 *softdepactivep = 0; 588 *softdepactiveaccp = 0; 589 } 590 591 void 592 softdep_buf_append(bp, wkhd) 593 struct buf *bp; 594 struct workhead *wkhd; 595 { 596 597 panic("softdep_buf_appendwork called"); 598 } 599 600 void 601 softdep_inode_append(ip, cred, wkhd) 602 struct inode *ip; 603 struct ucred *cred; 604 struct workhead *wkhd; 605 { 606 607 panic("softdep_inode_appendwork called"); 608 } 609 610 void 611 softdep_freework(wkhd) 612 struct workhead *wkhd; 613 { 614 615 panic("softdep_freework called"); 616 } 617 618 #else 619 620 FEATURE(softupdates, "FFS soft-updates support"); 621 622 static SYSCTL_NODE(_debug, OID_AUTO, softdep, CTLFLAG_RW, 0, 623 "soft updates stats"); 624 static SYSCTL_NODE(_debug_softdep, OID_AUTO, total, CTLFLAG_RW, 0, 625 "total dependencies allocated"); 626 static SYSCTL_NODE(_debug_softdep, OID_AUTO, highuse, CTLFLAG_RW, 0, 627 "high use dependencies allocated"); 628 static SYSCTL_NODE(_debug_softdep, OID_AUTO, current, CTLFLAG_RW, 0, 629 "current dependencies allocated"); 630 static SYSCTL_NODE(_debug_softdep, OID_AUTO, write, CTLFLAG_RW, 0, 631 "current dependencies written"); 632 633 unsigned long dep_current[D_LAST + 1]; 634 unsigned long dep_highuse[D_LAST + 1]; 635 unsigned long dep_total[D_LAST + 1]; 636 unsigned long dep_write[D_LAST + 1]; 637 638 #define SOFTDEP_TYPE(type, str, long) \ 639 static MALLOC_DEFINE(M_ ## type, #str, long); \ 640 SYSCTL_ULONG(_debug_softdep_total, OID_AUTO, str, CTLFLAG_RD, \ 641 &dep_total[D_ ## type], 0, ""); \ 642 SYSCTL_ULONG(_debug_softdep_current, OID_AUTO, str, CTLFLAG_RD, \ 643 &dep_current[D_ ## type], 0, ""); \ 644 SYSCTL_ULONG(_debug_softdep_highuse, OID_AUTO, str, CTLFLAG_RD, \ 645 &dep_highuse[D_ ## type], 0, ""); \ 646 SYSCTL_ULONG(_debug_softdep_write, OID_AUTO, str, CTLFLAG_RD, \ 647 &dep_write[D_ ## type], 0, ""); 648 649 SOFTDEP_TYPE(PAGEDEP, pagedep, "File page dependencies"); 650 SOFTDEP_TYPE(INODEDEP, inodedep, "Inode dependencies"); 651 SOFTDEP_TYPE(BMSAFEMAP, bmsafemap, 652 "Block or frag allocated from cyl group map"); 653 SOFTDEP_TYPE(NEWBLK, newblk, "New block or frag allocation dependency"); 654 SOFTDEP_TYPE(ALLOCDIRECT, allocdirect, "Block or frag dependency for an inode"); 655 SOFTDEP_TYPE(INDIRDEP, indirdep, "Indirect block dependencies"); 656 SOFTDEP_TYPE(ALLOCINDIR, allocindir, "Block dependency for an indirect block"); 657 SOFTDEP_TYPE(FREEFRAG, freefrag, "Previously used frag for an inode"); 658 SOFTDEP_TYPE(FREEBLKS, freeblks, "Blocks freed from an inode"); 659 SOFTDEP_TYPE(FREEFILE, freefile, "Inode deallocated"); 660 SOFTDEP_TYPE(DIRADD, diradd, "New directory entry"); 661 SOFTDEP_TYPE(MKDIR, mkdir, "New directory"); 662 SOFTDEP_TYPE(DIRREM, dirrem, "Directory entry deleted"); 663 SOFTDEP_TYPE(NEWDIRBLK, newdirblk, "Unclaimed new directory block"); 664 SOFTDEP_TYPE(FREEWORK, freework, "free an inode block"); 665 SOFTDEP_TYPE(FREEDEP, freedep, "track a block free"); 666 SOFTDEP_TYPE(JADDREF, jaddref, "Journal inode ref add"); 667 SOFTDEP_TYPE(JREMREF, jremref, "Journal inode ref remove"); 668 SOFTDEP_TYPE(JMVREF, jmvref, "Journal inode ref move"); 669 SOFTDEP_TYPE(JNEWBLK, jnewblk, "Journal new block"); 670 SOFTDEP_TYPE(JFREEBLK, jfreeblk, "Journal free block"); 671 SOFTDEP_TYPE(JFREEFRAG, jfreefrag, "Journal free frag"); 672 SOFTDEP_TYPE(JSEG, jseg, "Journal segment"); 673 SOFTDEP_TYPE(JSEGDEP, jsegdep, "Journal segment complete"); 674 SOFTDEP_TYPE(SBDEP, sbdep, "Superblock write dependency"); 675 SOFTDEP_TYPE(JTRUNC, jtrunc, "Journal inode truncation"); 676 SOFTDEP_TYPE(JFSYNC, jfsync, "Journal fsync complete"); 677 678 static MALLOC_DEFINE(M_SENTINEL, "sentinel", "Worklist sentinel"); 679 680 static MALLOC_DEFINE(M_SAVEDINO, "savedino", "Saved inodes"); 681 static MALLOC_DEFINE(M_JBLOCKS, "jblocks", "Journal block locations"); 682 static MALLOC_DEFINE(M_MOUNTDATA, "softdep", "Softdep per-mount data"); 683 684 #define M_SOFTDEP_FLAGS (M_WAITOK) 685 686 /* 687 * translate from workitem type to memory type 688 * MUST match the defines above, such that memtype[D_XXX] == M_XXX 689 */ 690 static struct malloc_type *memtype[] = { 691 NULL, 692 M_PAGEDEP, 693 M_INODEDEP, 694 M_BMSAFEMAP, 695 M_NEWBLK, 696 M_ALLOCDIRECT, 697 M_INDIRDEP, 698 M_ALLOCINDIR, 699 M_FREEFRAG, 700 M_FREEBLKS, 701 M_FREEFILE, 702 M_DIRADD, 703 M_MKDIR, 704 M_DIRREM, 705 M_NEWDIRBLK, 706 M_FREEWORK, 707 M_FREEDEP, 708 M_JADDREF, 709 M_JREMREF, 710 M_JMVREF, 711 M_JNEWBLK, 712 M_JFREEBLK, 713 M_JFREEFRAG, 714 M_JSEG, 715 M_JSEGDEP, 716 M_SBDEP, 717 M_JTRUNC, 718 M_JFSYNC, 719 M_SENTINEL 720 }; 721 722 #define DtoM(type) (memtype[type]) 723 724 /* 725 * Names of malloc types. 726 */ 727 #define TYPENAME(type) \ 728 ((unsigned)(type) <= D_LAST && (unsigned)(type) >= D_FIRST ? \ 729 memtype[type]->ks_shortdesc : "???") 730 /* 731 * End system adaptation definitions. 732 */ 733 734 #define DOTDOT_OFFSET offsetof(struct dirtemplate, dotdot_ino) 735 #define DOT_OFFSET offsetof(struct dirtemplate, dot_ino) 736 737 /* 738 * Internal function prototypes. 739 */ 740 static void check_clear_deps(struct mount *); 741 static void softdep_error(char *, int); 742 static int softdep_process_worklist(struct mount *, int); 743 static int softdep_waitidle(struct mount *, int); 744 static void drain_output(struct vnode *); 745 static struct buf *getdirtybuf(struct buf *, struct rwlock *, int); 746 static int check_inodedep_free(struct inodedep *); 747 static void clear_remove(struct mount *); 748 static void clear_inodedeps(struct mount *); 749 static void unlinked_inodedep(struct mount *, struct inodedep *); 750 static void clear_unlinked_inodedep(struct inodedep *); 751 static struct inodedep *first_unlinked_inodedep(struct ufsmount *); 752 static int flush_pagedep_deps(struct vnode *, struct mount *, 753 struct diraddhd *); 754 static int free_pagedep(struct pagedep *); 755 static int flush_newblk_dep(struct vnode *, struct mount *, ufs_lbn_t); 756 static int flush_inodedep_deps(struct vnode *, struct mount *, ino_t); 757 static int flush_deplist(struct allocdirectlst *, int, int *); 758 static int sync_cgs(struct mount *, int); 759 static int handle_written_filepage(struct pagedep *, struct buf *, int); 760 static int handle_written_sbdep(struct sbdep *, struct buf *); 761 static void initiate_write_sbdep(struct sbdep *); 762 static void diradd_inode_written(struct diradd *, struct inodedep *); 763 static int handle_written_indirdep(struct indirdep *, struct buf *, 764 struct buf**, int); 765 static int handle_written_inodeblock(struct inodedep *, struct buf *, int); 766 static int jnewblk_rollforward(struct jnewblk *, struct fs *, struct cg *, 767 uint8_t *); 768 static int handle_written_bmsafemap(struct bmsafemap *, struct buf *, int); 769 static void handle_written_jaddref(struct jaddref *); 770 static void handle_written_jremref(struct jremref *); 771 static void handle_written_jseg(struct jseg *, struct buf *); 772 static void handle_written_jnewblk(struct jnewblk *); 773 static void handle_written_jblkdep(struct jblkdep *); 774 static void handle_written_jfreefrag(struct jfreefrag *); 775 static void complete_jseg(struct jseg *); 776 static void complete_jsegs(struct jseg *); 777 static void jseg_write(struct ufsmount *ump, struct jseg *, uint8_t *); 778 static void jaddref_write(struct jaddref *, struct jseg *, uint8_t *); 779 static void jremref_write(struct jremref *, struct jseg *, uint8_t *); 780 static void jmvref_write(struct jmvref *, struct jseg *, uint8_t *); 781 static void jtrunc_write(struct jtrunc *, struct jseg *, uint8_t *); 782 static void jfsync_write(struct jfsync *, struct jseg *, uint8_t *data); 783 static void jnewblk_write(struct jnewblk *, struct jseg *, uint8_t *); 784 static void jfreeblk_write(struct jfreeblk *, struct jseg *, uint8_t *); 785 static void jfreefrag_write(struct jfreefrag *, struct jseg *, uint8_t *); 786 static inline void inoref_write(struct inoref *, struct jseg *, 787 struct jrefrec *); 788 static void handle_allocdirect_partdone(struct allocdirect *, 789 struct workhead *); 790 static struct jnewblk *cancel_newblk(struct newblk *, struct worklist *, 791 struct workhead *); 792 static void indirdep_complete(struct indirdep *); 793 static int indirblk_lookup(struct mount *, ufs2_daddr_t); 794 static void indirblk_insert(struct freework *); 795 static void indirblk_remove(struct freework *); 796 static void handle_allocindir_partdone(struct allocindir *); 797 static void initiate_write_filepage(struct pagedep *, struct buf *); 798 static void initiate_write_indirdep(struct indirdep*, struct buf *); 799 static void handle_written_mkdir(struct mkdir *, int); 800 static int jnewblk_rollback(struct jnewblk *, struct fs *, struct cg *, 801 uint8_t *); 802 static void initiate_write_bmsafemap(struct bmsafemap *, struct buf *); 803 static void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *); 804 static void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *); 805 static void handle_workitem_freefile(struct freefile *); 806 static int handle_workitem_remove(struct dirrem *, int); 807 static struct dirrem *newdirrem(struct buf *, struct inode *, 808 struct inode *, int, struct dirrem **); 809 static struct indirdep *indirdep_lookup(struct mount *, struct inode *, 810 struct buf *); 811 static void cancel_indirdep(struct indirdep *, struct buf *, 812 struct freeblks *); 813 static void free_indirdep(struct indirdep *); 814 static void free_diradd(struct diradd *, struct workhead *); 815 static void merge_diradd(struct inodedep *, struct diradd *); 816 static void complete_diradd(struct diradd *); 817 static struct diradd *diradd_lookup(struct pagedep *, int); 818 static struct jremref *cancel_diradd_dotdot(struct inode *, struct dirrem *, 819 struct jremref *); 820 static struct jremref *cancel_mkdir_dotdot(struct inode *, struct dirrem *, 821 struct jremref *); 822 static void cancel_diradd(struct diradd *, struct dirrem *, struct jremref *, 823 struct jremref *, struct jremref *); 824 static void dirrem_journal(struct dirrem *, struct jremref *, struct jremref *, 825 struct jremref *); 826 static void cancel_allocindir(struct allocindir *, struct buf *bp, 827 struct freeblks *, int); 828 static int setup_trunc_indir(struct freeblks *, struct inode *, 829 ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t); 830 static void complete_trunc_indir(struct freework *); 831 static void trunc_indirdep(struct indirdep *, struct freeblks *, struct buf *, 832 int); 833 static void complete_mkdir(struct mkdir *); 834 static void free_newdirblk(struct newdirblk *); 835 static void free_jremref(struct jremref *); 836 static void free_jaddref(struct jaddref *); 837 static void free_jsegdep(struct jsegdep *); 838 static void free_jsegs(struct jblocks *); 839 static void rele_jseg(struct jseg *); 840 static void free_jseg(struct jseg *, struct jblocks *); 841 static void free_jnewblk(struct jnewblk *); 842 static void free_jblkdep(struct jblkdep *); 843 static void free_jfreefrag(struct jfreefrag *); 844 static void free_freedep(struct freedep *); 845 static void journal_jremref(struct dirrem *, struct jremref *, 846 struct inodedep *); 847 static void cancel_jnewblk(struct jnewblk *, struct workhead *); 848 static int cancel_jaddref(struct jaddref *, struct inodedep *, 849 struct workhead *); 850 static void cancel_jfreefrag(struct jfreefrag *); 851 static inline void setup_freedirect(struct freeblks *, struct inode *, 852 int, int); 853 static inline void setup_freeext(struct freeblks *, struct inode *, int, int); 854 static inline void setup_freeindir(struct freeblks *, struct inode *, int, 855 ufs_lbn_t, int); 856 static inline struct freeblks *newfreeblks(struct mount *, struct inode *); 857 static void freeblks_free(struct ufsmount *, struct freeblks *, int); 858 static void indir_trunc(struct freework *, ufs2_daddr_t, ufs_lbn_t); 859 static ufs2_daddr_t blkcount(struct fs *, ufs2_daddr_t, off_t); 860 static int trunc_check_buf(struct buf *, int *, ufs_lbn_t, int, int); 861 static void trunc_dependencies(struct inode *, struct freeblks *, ufs_lbn_t, 862 int, int); 863 static void trunc_pages(struct inode *, off_t, ufs2_daddr_t, int); 864 static int cancel_pagedep(struct pagedep *, struct freeblks *, int); 865 static int deallocate_dependencies(struct buf *, struct freeblks *, int); 866 static void newblk_freefrag(struct newblk*); 867 static void free_newblk(struct newblk *); 868 static void cancel_allocdirect(struct allocdirectlst *, 869 struct allocdirect *, struct freeblks *); 870 static int check_inode_unwritten(struct inodedep *); 871 static int free_inodedep(struct inodedep *); 872 static void freework_freeblock(struct freework *, u_long); 873 static void freework_enqueue(struct freework *); 874 static int handle_workitem_freeblocks(struct freeblks *, int); 875 static int handle_complete_freeblocks(struct freeblks *, int); 876 static void handle_workitem_indirblk(struct freework *); 877 static void handle_written_freework(struct freework *); 878 static void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *); 879 static struct worklist *jnewblk_merge(struct worklist *, struct worklist *, 880 struct workhead *); 881 static struct freefrag *setup_allocindir_phase2(struct buf *, struct inode *, 882 struct inodedep *, struct allocindir *, ufs_lbn_t); 883 static struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t, 884 ufs2_daddr_t, ufs_lbn_t); 885 static void handle_workitem_freefrag(struct freefrag *); 886 static struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long, 887 ufs_lbn_t, u_long); 888 static void allocdirect_merge(struct allocdirectlst *, 889 struct allocdirect *, struct allocdirect *); 890 static struct freefrag *allocindir_merge(struct allocindir *, 891 struct allocindir *); 892 static int bmsafemap_find(struct bmsafemap_hashhead *, int, 893 struct bmsafemap **); 894 static struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *, 895 int cg, struct bmsafemap *); 896 static int newblk_find(struct newblk_hashhead *, ufs2_daddr_t, int, 897 struct newblk **); 898 static int newblk_lookup(struct mount *, ufs2_daddr_t, int, struct newblk **); 899 static int inodedep_find(struct inodedep_hashhead *, ino_t, 900 struct inodedep **); 901 static int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **); 902 static int pagedep_lookup(struct mount *, struct buf *bp, ino_t, ufs_lbn_t, 903 int, struct pagedep **); 904 static int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t, 905 struct pagedep **); 906 static void pause_timer(void *); 907 static int request_cleanup(struct mount *, int); 908 static int softdep_request_cleanup_flush(struct mount *, struct ufsmount *); 909 static void schedule_cleanup(struct mount *); 910 static void softdep_ast_cleanup_proc(struct thread *); 911 static struct ufsmount *softdep_bp_to_mp(struct buf *bp); 912 static int process_worklist_item(struct mount *, int, int); 913 static void process_removes(struct vnode *); 914 static void process_truncates(struct vnode *); 915 static void jwork_move(struct workhead *, struct workhead *); 916 static void jwork_insert(struct workhead *, struct jsegdep *); 917 static void add_to_worklist(struct worklist *, int); 918 static void wake_worklist(struct worklist *); 919 static void wait_worklist(struct worklist *, char *); 920 static void remove_from_worklist(struct worklist *); 921 static void softdep_flush(void *); 922 static void softdep_flushjournal(struct mount *); 923 static int softdep_speedup(struct ufsmount *); 924 static void worklist_speedup(struct mount *); 925 static int journal_mount(struct mount *, struct fs *, struct ucred *); 926 static void journal_unmount(struct ufsmount *); 927 static int journal_space(struct ufsmount *, int); 928 static void journal_suspend(struct ufsmount *); 929 static int journal_unsuspend(struct ufsmount *ump); 930 static void softdep_prelink(struct vnode *, struct vnode *); 931 static void add_to_journal(struct worklist *); 932 static void remove_from_journal(struct worklist *); 933 static bool softdep_excess_items(struct ufsmount *, int); 934 static void softdep_process_journal(struct mount *, struct worklist *, int); 935 static struct jremref *newjremref(struct dirrem *, struct inode *, 936 struct inode *ip, off_t, nlink_t); 937 static struct jaddref *newjaddref(struct inode *, ino_t, off_t, int16_t, 938 uint16_t); 939 static inline void newinoref(struct inoref *, ino_t, ino_t, off_t, nlink_t, 940 uint16_t); 941 static inline struct jsegdep *inoref_jseg(struct inoref *); 942 static struct jmvref *newjmvref(struct inode *, ino_t, off_t, off_t); 943 static struct jfreeblk *newjfreeblk(struct freeblks *, ufs_lbn_t, 944 ufs2_daddr_t, int); 945 static void adjust_newfreework(struct freeblks *, int); 946 static struct jtrunc *newjtrunc(struct freeblks *, off_t, int); 947 static void move_newblock_dep(struct jaddref *, struct inodedep *); 948 static void cancel_jfreeblk(struct freeblks *, ufs2_daddr_t); 949 static struct jfreefrag *newjfreefrag(struct freefrag *, struct inode *, 950 ufs2_daddr_t, long, ufs_lbn_t); 951 static struct freework *newfreework(struct ufsmount *, struct freeblks *, 952 struct freework *, ufs_lbn_t, ufs2_daddr_t, int, int, int); 953 static int jwait(struct worklist *, int); 954 static struct inodedep *inodedep_lookup_ip(struct inode *); 955 static int bmsafemap_backgroundwrite(struct bmsafemap *, struct buf *); 956 static struct freefile *handle_bufwait(struct inodedep *, struct workhead *); 957 static void handle_jwork(struct workhead *); 958 static struct mkdir *setup_newdir(struct diradd *, ino_t, ino_t, struct buf *, 959 struct mkdir **); 960 static struct jblocks *jblocks_create(void); 961 static ufs2_daddr_t jblocks_alloc(struct jblocks *, int, int *); 962 static void jblocks_free(struct jblocks *, struct mount *, int); 963 static void jblocks_destroy(struct jblocks *); 964 static void jblocks_add(struct jblocks *, ufs2_daddr_t, int); 965 966 /* 967 * Exported softdep operations. 968 */ 969 static void softdep_disk_io_initiation(struct buf *); 970 static void softdep_disk_write_complete(struct buf *); 971 static void softdep_deallocate_dependencies(struct buf *); 972 static int softdep_count_dependencies(struct buf *bp, int); 973 974 /* 975 * Global lock over all of soft updates. 976 */ 977 static struct mtx lk; 978 MTX_SYSINIT(softdep_lock, &lk, "Global Softdep Lock", MTX_DEF); 979 980 #define ACQUIRE_GBLLOCK(lk) mtx_lock(lk) 981 #define FREE_GBLLOCK(lk) mtx_unlock(lk) 982 #define GBLLOCK_OWNED(lk) mtx_assert((lk), MA_OWNED) 983 984 /* 985 * Per-filesystem soft-updates locking. 986 */ 987 #define LOCK_PTR(ump) (&(ump)->um_softdep->sd_fslock) 988 #define TRY_ACQUIRE_LOCK(ump) rw_try_wlock(&(ump)->um_softdep->sd_fslock) 989 #define ACQUIRE_LOCK(ump) rw_wlock(&(ump)->um_softdep->sd_fslock) 990 #define FREE_LOCK(ump) rw_wunlock(&(ump)->um_softdep->sd_fslock) 991 #define LOCK_OWNED(ump) rw_assert(&(ump)->um_softdep->sd_fslock, \ 992 RA_WLOCKED) 993 994 #define BUF_AREC(bp) lockallowrecurse(&(bp)->b_lock) 995 #define BUF_NOREC(bp) lockdisablerecurse(&(bp)->b_lock) 996 997 /* 998 * Worklist queue management. 999 * These routines require that the lock be held. 1000 */ 1001 #ifndef /* NOT */ DEBUG 1002 #define WORKLIST_INSERT(head, item) do { \ 1003 (item)->wk_state |= ONWORKLIST; \ 1004 LIST_INSERT_HEAD(head, item, wk_list); \ 1005 } while (0) 1006 #define WORKLIST_REMOVE(item) do { \ 1007 (item)->wk_state &= ~ONWORKLIST; \ 1008 LIST_REMOVE(item, wk_list); \ 1009 } while (0) 1010 #define WORKLIST_INSERT_UNLOCKED WORKLIST_INSERT 1011 #define WORKLIST_REMOVE_UNLOCKED WORKLIST_REMOVE 1012 1013 #else /* DEBUG */ 1014 static void worklist_insert(struct workhead *, struct worklist *, int); 1015 static void worklist_remove(struct worklist *, int); 1016 1017 #define WORKLIST_INSERT(head, item) worklist_insert(head, item, 1) 1018 #define WORKLIST_INSERT_UNLOCKED(head, item) worklist_insert(head, item, 0) 1019 #define WORKLIST_REMOVE(item) worklist_remove(item, 1) 1020 #define WORKLIST_REMOVE_UNLOCKED(item) worklist_remove(item, 0) 1021 1022 static void 1023 worklist_insert(head, item, locked) 1024 struct workhead *head; 1025 struct worklist *item; 1026 int locked; 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 item, TYPENAME(item->wk_type), item->wk_state); 1034 item->wk_state |= ONWORKLIST; 1035 LIST_INSERT_HEAD(head, item, wk_list); 1036 } 1037 1038 static void 1039 worklist_remove(item, locked) 1040 struct worklist *item; 1041 int locked; 1042 { 1043 1044 if (locked) 1045 LOCK_OWNED(VFSTOUFS(item->wk_mp)); 1046 if ((item->wk_state & ONWORKLIST) == 0) 1047 panic("worklist_remove: %p %s(0x%X) not on list", 1048 item, TYPENAME(item->wk_type), item->wk_state); 1049 item->wk_state &= ~ONWORKLIST; 1050 LIST_REMOVE(item, wk_list); 1051 } 1052 #endif /* DEBUG */ 1053 1054 /* 1055 * Merge two jsegdeps keeping only the oldest one as newer references 1056 * can't be discarded until after older references. 1057 */ 1058 static inline struct jsegdep * 1059 jsegdep_merge(struct jsegdep *one, struct jsegdep *two) 1060 { 1061 struct jsegdep *swp; 1062 1063 if (two == NULL) 1064 return (one); 1065 1066 if (one->jd_seg->js_seq > two->jd_seg->js_seq) { 1067 swp = one; 1068 one = two; 1069 two = swp; 1070 } 1071 WORKLIST_REMOVE(&two->jd_list); 1072 free_jsegdep(two); 1073 1074 return (one); 1075 } 1076 1077 /* 1078 * If two freedeps are compatible free one to reduce list size. 1079 */ 1080 static inline struct freedep * 1081 freedep_merge(struct freedep *one, struct freedep *two) 1082 { 1083 if (two == NULL) 1084 return (one); 1085 1086 if (one->fd_freework == two->fd_freework) { 1087 WORKLIST_REMOVE(&two->fd_list); 1088 free_freedep(two); 1089 } 1090 return (one); 1091 } 1092 1093 /* 1094 * Move journal work from one list to another. Duplicate freedeps and 1095 * jsegdeps are coalesced to keep the lists as small as possible. 1096 */ 1097 static void 1098 jwork_move(dst, src) 1099 struct workhead *dst; 1100 struct workhead *src; 1101 { 1102 struct freedep *freedep; 1103 struct jsegdep *jsegdep; 1104 struct worklist *wkn; 1105 struct worklist *wk; 1106 1107 KASSERT(dst != src, 1108 ("jwork_move: dst == src")); 1109 freedep = NULL; 1110 jsegdep = NULL; 1111 LIST_FOREACH_SAFE(wk, dst, wk_list, wkn) { 1112 if (wk->wk_type == D_JSEGDEP) 1113 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1114 else if (wk->wk_type == D_FREEDEP) 1115 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1116 } 1117 1118 while ((wk = LIST_FIRST(src)) != NULL) { 1119 WORKLIST_REMOVE(wk); 1120 WORKLIST_INSERT(dst, wk); 1121 if (wk->wk_type == D_JSEGDEP) { 1122 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1123 continue; 1124 } 1125 if (wk->wk_type == D_FREEDEP) 1126 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1127 } 1128 } 1129 1130 static void 1131 jwork_insert(dst, jsegdep) 1132 struct workhead *dst; 1133 struct jsegdep *jsegdep; 1134 { 1135 struct jsegdep *jsegdepn; 1136 struct worklist *wk; 1137 1138 LIST_FOREACH(wk, dst, wk_list) 1139 if (wk->wk_type == D_JSEGDEP) 1140 break; 1141 if (wk == NULL) { 1142 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1143 return; 1144 } 1145 jsegdepn = WK_JSEGDEP(wk); 1146 if (jsegdep->jd_seg->js_seq < jsegdepn->jd_seg->js_seq) { 1147 WORKLIST_REMOVE(wk); 1148 free_jsegdep(jsegdepn); 1149 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1150 } else 1151 free_jsegdep(jsegdep); 1152 } 1153 1154 /* 1155 * Routines for tracking and managing workitems. 1156 */ 1157 static void workitem_free(struct worklist *, int); 1158 static void workitem_alloc(struct worklist *, int, struct mount *); 1159 static void workitem_reassign(struct worklist *, int); 1160 1161 #define WORKITEM_FREE(item, type) \ 1162 workitem_free((struct worklist *)(item), (type)) 1163 #define WORKITEM_REASSIGN(item, type) \ 1164 workitem_reassign((struct worklist *)(item), (type)) 1165 1166 static void 1167 workitem_free(item, type) 1168 struct worklist *item; 1169 int type; 1170 { 1171 struct ufsmount *ump; 1172 1173 #ifdef DEBUG 1174 if (item->wk_state & ONWORKLIST) 1175 panic("workitem_free: %s(0x%X) still on list", 1176 TYPENAME(item->wk_type), item->wk_state); 1177 if (item->wk_type != type && type != D_NEWBLK) 1178 panic("workitem_free: type mismatch %s != %s", 1179 TYPENAME(item->wk_type), TYPENAME(type)); 1180 #endif 1181 if (item->wk_state & IOWAITING) 1182 wakeup(item); 1183 ump = VFSTOUFS(item->wk_mp); 1184 LOCK_OWNED(ump); 1185 KASSERT(ump->softdep_deps > 0, 1186 ("workitem_free: %s: softdep_deps going negative", 1187 ump->um_fs->fs_fsmnt)); 1188 if (--ump->softdep_deps == 0 && ump->softdep_req) 1189 wakeup(&ump->softdep_deps); 1190 KASSERT(dep_current[item->wk_type] > 0, 1191 ("workitem_free: %s: dep_current[%s] going negative", 1192 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1193 KASSERT(ump->softdep_curdeps[item->wk_type] > 0, 1194 ("workitem_free: %s: softdep_curdeps[%s] going negative", 1195 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1196 atomic_subtract_long(&dep_current[item->wk_type], 1); 1197 ump->softdep_curdeps[item->wk_type] -= 1; 1198 free(item, DtoM(type)); 1199 } 1200 1201 static void 1202 workitem_alloc(item, type, mp) 1203 struct worklist *item; 1204 int type; 1205 struct mount *mp; 1206 { 1207 struct ufsmount *ump; 1208 1209 item->wk_type = type; 1210 item->wk_mp = mp; 1211 item->wk_state = 0; 1212 1213 ump = VFSTOUFS(mp); 1214 ACQUIRE_GBLLOCK(&lk); 1215 dep_current[type]++; 1216 if (dep_current[type] > dep_highuse[type]) 1217 dep_highuse[type] = dep_current[type]; 1218 dep_total[type]++; 1219 FREE_GBLLOCK(&lk); 1220 ACQUIRE_LOCK(ump); 1221 ump->softdep_curdeps[type] += 1; 1222 ump->softdep_deps++; 1223 ump->softdep_accdeps++; 1224 FREE_LOCK(ump); 1225 } 1226 1227 static void 1228 workitem_reassign(item, newtype) 1229 struct worklist *item; 1230 int newtype; 1231 { 1232 struct ufsmount *ump; 1233 1234 ump = VFSTOUFS(item->wk_mp); 1235 LOCK_OWNED(ump); 1236 KASSERT(ump->softdep_curdeps[item->wk_type] > 0, 1237 ("workitem_reassign: %s: softdep_curdeps[%s] going negative", 1238 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1239 ump->softdep_curdeps[item->wk_type] -= 1; 1240 ump->softdep_curdeps[newtype] += 1; 1241 KASSERT(dep_current[item->wk_type] > 0, 1242 ("workitem_reassign: %s: dep_current[%s] going negative", 1243 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1244 ACQUIRE_GBLLOCK(&lk); 1245 dep_current[newtype]++; 1246 dep_current[item->wk_type]--; 1247 if (dep_current[newtype] > dep_highuse[newtype]) 1248 dep_highuse[newtype] = dep_current[newtype]; 1249 dep_total[newtype]++; 1250 FREE_GBLLOCK(&lk); 1251 item->wk_type = newtype; 1252 } 1253 1254 /* 1255 * Workitem queue management 1256 */ 1257 static int max_softdeps; /* maximum number of structs before slowdown */ 1258 static int tickdelay = 2; /* number of ticks to pause during slowdown */ 1259 static int proc_waiting; /* tracks whether we have a timeout posted */ 1260 static int *stat_countp; /* statistic to count in proc_waiting timeout */ 1261 static struct callout softdep_callout; 1262 static int req_clear_inodedeps; /* syncer process flush some inodedeps */ 1263 static int req_clear_remove; /* syncer process flush some freeblks */ 1264 static int softdep_flushcache = 0; /* Should we do BIO_FLUSH? */ 1265 1266 /* 1267 * runtime statistics 1268 */ 1269 static int stat_flush_threads; /* number of softdep flushing threads */ 1270 static int stat_worklist_push; /* number of worklist cleanups */ 1271 static int stat_blk_limit_push; /* number of times block limit neared */ 1272 static int stat_ino_limit_push; /* number of times inode limit neared */ 1273 static int stat_blk_limit_hit; /* number of times block slowdown imposed */ 1274 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */ 1275 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */ 1276 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */ 1277 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */ 1278 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */ 1279 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */ 1280 static int stat_jaddref; /* bufs redirtied as ino bitmap can not write */ 1281 static int stat_jnewblk; /* bufs redirtied as blk bitmap can not write */ 1282 static int stat_journal_min; /* Times hit journal min threshold */ 1283 static int stat_journal_low; /* Times hit journal low threshold */ 1284 static int stat_journal_wait; /* Times blocked in jwait(). */ 1285 static int stat_jwait_filepage; /* Times blocked in jwait() for filepage. */ 1286 static int stat_jwait_freeblks; /* Times blocked in jwait() for freeblks. */ 1287 static int stat_jwait_inode; /* Times blocked in jwait() for inodes. */ 1288 static int stat_jwait_newblk; /* Times blocked in jwait() for newblks. */ 1289 static int stat_cleanup_high_delay; /* Maximum cleanup delay (in ticks) */ 1290 static int stat_cleanup_blkrequests; /* Number of block cleanup requests */ 1291 static int stat_cleanup_inorequests; /* Number of inode cleanup requests */ 1292 static int stat_cleanup_retries; /* Number of cleanups that needed to flush */ 1293 static int stat_cleanup_failures; /* Number of cleanup requests that failed */ 1294 static int stat_emptyjblocks; /* Number of potentially empty journal blocks */ 1295 1296 SYSCTL_INT(_debug_softdep, OID_AUTO, max_softdeps, CTLFLAG_RW, 1297 &max_softdeps, 0, ""); 1298 SYSCTL_INT(_debug_softdep, OID_AUTO, tickdelay, CTLFLAG_RW, 1299 &tickdelay, 0, ""); 1300 SYSCTL_INT(_debug_softdep, OID_AUTO, flush_threads, CTLFLAG_RD, 1301 &stat_flush_threads, 0, ""); 1302 SYSCTL_INT(_debug_softdep, OID_AUTO, worklist_push, CTLFLAG_RW, 1303 &stat_worklist_push, 0,""); 1304 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_push, CTLFLAG_RW, 1305 &stat_blk_limit_push, 0,""); 1306 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_push, CTLFLAG_RW, 1307 &stat_ino_limit_push, 0,""); 1308 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_hit, CTLFLAG_RW, 1309 &stat_blk_limit_hit, 0, ""); 1310 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_hit, CTLFLAG_RW, 1311 &stat_ino_limit_hit, 0, ""); 1312 SYSCTL_INT(_debug_softdep, OID_AUTO, sync_limit_hit, CTLFLAG_RW, 1313 &stat_sync_limit_hit, 0, ""); 1314 SYSCTL_INT(_debug_softdep, OID_AUTO, indir_blk_ptrs, CTLFLAG_RW, 1315 &stat_indir_blk_ptrs, 0, ""); 1316 SYSCTL_INT(_debug_softdep, OID_AUTO, inode_bitmap, CTLFLAG_RW, 1317 &stat_inode_bitmap, 0, ""); 1318 SYSCTL_INT(_debug_softdep, OID_AUTO, direct_blk_ptrs, CTLFLAG_RW, 1319 &stat_direct_blk_ptrs, 0, ""); 1320 SYSCTL_INT(_debug_softdep, OID_AUTO, dir_entry, CTLFLAG_RW, 1321 &stat_dir_entry, 0, ""); 1322 SYSCTL_INT(_debug_softdep, OID_AUTO, jaddref_rollback, CTLFLAG_RW, 1323 &stat_jaddref, 0, ""); 1324 SYSCTL_INT(_debug_softdep, OID_AUTO, jnewblk_rollback, CTLFLAG_RW, 1325 &stat_jnewblk, 0, ""); 1326 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_low, CTLFLAG_RW, 1327 &stat_journal_low, 0, ""); 1328 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_min, CTLFLAG_RW, 1329 &stat_journal_min, 0, ""); 1330 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_wait, CTLFLAG_RW, 1331 &stat_journal_wait, 0, ""); 1332 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_filepage, CTLFLAG_RW, 1333 &stat_jwait_filepage, 0, ""); 1334 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_freeblks, CTLFLAG_RW, 1335 &stat_jwait_freeblks, 0, ""); 1336 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_inode, CTLFLAG_RW, 1337 &stat_jwait_inode, 0, ""); 1338 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_newblk, CTLFLAG_RW, 1339 &stat_jwait_newblk, 0, ""); 1340 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_blkrequests, CTLFLAG_RW, 1341 &stat_cleanup_blkrequests, 0, ""); 1342 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_inorequests, CTLFLAG_RW, 1343 &stat_cleanup_inorequests, 0, ""); 1344 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_high_delay, CTLFLAG_RW, 1345 &stat_cleanup_high_delay, 0, ""); 1346 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_retries, CTLFLAG_RW, 1347 &stat_cleanup_retries, 0, ""); 1348 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_failures, CTLFLAG_RW, 1349 &stat_cleanup_failures, 0, ""); 1350 SYSCTL_INT(_debug_softdep, OID_AUTO, flushcache, CTLFLAG_RW, 1351 &softdep_flushcache, 0, ""); 1352 SYSCTL_INT(_debug_softdep, OID_AUTO, emptyjblocks, CTLFLAG_RD, 1353 &stat_emptyjblocks, 0, ""); 1354 1355 SYSCTL_DECL(_vfs_ffs); 1356 1357 /* Whether to recompute the summary at mount time */ 1358 static int compute_summary_at_mount = 0; 1359 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW, 1360 &compute_summary_at_mount, 0, "Recompute summary at mount"); 1361 static int print_threads = 0; 1362 SYSCTL_INT(_debug_softdep, OID_AUTO, print_threads, CTLFLAG_RW, 1363 &print_threads, 0, "Notify flusher thread start/stop"); 1364 1365 /* List of all filesystems mounted with soft updates */ 1366 static TAILQ_HEAD(, mount_softdeps) softdepmounts; 1367 1368 /* 1369 * This function cleans the worklist for a filesystem. 1370 * Each filesystem running with soft dependencies gets its own 1371 * thread to run in this function. The thread is started up in 1372 * softdep_mount and shutdown in softdep_unmount. They show up 1373 * as part of the kernel "bufdaemon" process whose process 1374 * entry is available in bufdaemonproc. 1375 */ 1376 static int searchfailed; 1377 extern struct proc *bufdaemonproc; 1378 static void 1379 softdep_flush(addr) 1380 void *addr; 1381 { 1382 struct mount *mp; 1383 struct thread *td; 1384 struct ufsmount *ump; 1385 1386 td = curthread; 1387 td->td_pflags |= TDP_NORUNNINGBUF; 1388 mp = (struct mount *)addr; 1389 ump = VFSTOUFS(mp); 1390 atomic_add_int(&stat_flush_threads, 1); 1391 ACQUIRE_LOCK(ump); 1392 ump->softdep_flags &= ~FLUSH_STARTING; 1393 wakeup(&ump->softdep_flushtd); 1394 FREE_LOCK(ump); 1395 if (print_threads) { 1396 if (stat_flush_threads == 1) 1397 printf("Running %s at pid %d\n", bufdaemonproc->p_comm, 1398 bufdaemonproc->p_pid); 1399 printf("Start thread %s\n", td->td_name); 1400 } 1401 for (;;) { 1402 while (softdep_process_worklist(mp, 0) > 0 || 1403 (MOUNTEDSUJ(mp) && 1404 VFSTOUFS(mp)->softdep_jblocks->jb_suspended)) 1405 kthread_suspend_check(); 1406 ACQUIRE_LOCK(ump); 1407 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1408 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, 1409 "sdflush", hz / 2); 1410 ump->softdep_flags &= ~FLUSH_CLEANUP; 1411 /* 1412 * Check to see if we are done and need to exit. 1413 */ 1414 if ((ump->softdep_flags & FLUSH_EXIT) == 0) { 1415 FREE_LOCK(ump); 1416 continue; 1417 } 1418 ump->softdep_flags &= ~FLUSH_EXIT; 1419 FREE_LOCK(ump); 1420 wakeup(&ump->softdep_flags); 1421 if (print_threads) 1422 printf("Stop thread %s: searchfailed %d, did cleanups %d\n", td->td_name, searchfailed, ump->um_softdep->sd_cleanups); 1423 atomic_subtract_int(&stat_flush_threads, 1); 1424 kthread_exit(); 1425 panic("kthread_exit failed\n"); 1426 } 1427 } 1428 1429 static void 1430 worklist_speedup(mp) 1431 struct mount *mp; 1432 { 1433 struct ufsmount *ump; 1434 1435 ump = VFSTOUFS(mp); 1436 LOCK_OWNED(ump); 1437 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1438 ump->softdep_flags |= FLUSH_CLEANUP; 1439 wakeup(&ump->softdep_flushtd); 1440 } 1441 1442 static int 1443 softdep_speedup(ump) 1444 struct ufsmount *ump; 1445 { 1446 struct ufsmount *altump; 1447 struct mount_softdeps *sdp; 1448 1449 LOCK_OWNED(ump); 1450 worklist_speedup(ump->um_mountp); 1451 bd_speedup(); 1452 /* 1453 * If we have global shortages, then we need other 1454 * filesystems to help with the cleanup. Here we wakeup a 1455 * flusher thread for a filesystem that is over its fair 1456 * share of resources. 1457 */ 1458 if (req_clear_inodedeps || req_clear_remove) { 1459 ACQUIRE_GBLLOCK(&lk); 1460 TAILQ_FOREACH(sdp, &softdepmounts, sd_next) { 1461 if ((altump = sdp->sd_ump) == ump) 1462 continue; 1463 if (((req_clear_inodedeps && 1464 altump->softdep_curdeps[D_INODEDEP] > 1465 max_softdeps / stat_flush_threads) || 1466 (req_clear_remove && 1467 altump->softdep_curdeps[D_DIRREM] > 1468 (max_softdeps / 2) / stat_flush_threads)) && 1469 TRY_ACQUIRE_LOCK(altump)) 1470 break; 1471 } 1472 if (sdp == NULL) { 1473 searchfailed++; 1474 FREE_GBLLOCK(&lk); 1475 } else { 1476 /* 1477 * Move to the end of the list so we pick a 1478 * different one on out next try. 1479 */ 1480 TAILQ_REMOVE(&softdepmounts, sdp, sd_next); 1481 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next); 1482 FREE_GBLLOCK(&lk); 1483 if ((altump->softdep_flags & 1484 (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1485 altump->softdep_flags |= FLUSH_CLEANUP; 1486 altump->um_softdep->sd_cleanups++; 1487 wakeup(&altump->softdep_flushtd); 1488 FREE_LOCK(altump); 1489 } 1490 } 1491 return (speedup_syncer()); 1492 } 1493 1494 /* 1495 * Add an item to the end of the work queue. 1496 * This routine requires that the lock be held. 1497 * This is the only routine that adds items to the list. 1498 * The following routine is the only one that removes items 1499 * and does so in order from first to last. 1500 */ 1501 1502 #define WK_HEAD 0x0001 /* Add to HEAD. */ 1503 #define WK_NODELAY 0x0002 /* Process immediately. */ 1504 1505 static void 1506 add_to_worklist(wk, flags) 1507 struct worklist *wk; 1508 int flags; 1509 { 1510 struct ufsmount *ump; 1511 1512 ump = VFSTOUFS(wk->wk_mp); 1513 LOCK_OWNED(ump); 1514 if (wk->wk_state & ONWORKLIST) 1515 panic("add_to_worklist: %s(0x%X) already on list", 1516 TYPENAME(wk->wk_type), wk->wk_state); 1517 wk->wk_state |= ONWORKLIST; 1518 if (ump->softdep_on_worklist == 0) { 1519 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1520 ump->softdep_worklist_tail = wk; 1521 } else if (flags & WK_HEAD) { 1522 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1523 } else { 1524 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list); 1525 ump->softdep_worklist_tail = wk; 1526 } 1527 ump->softdep_on_worklist += 1; 1528 if (flags & WK_NODELAY) 1529 worklist_speedup(wk->wk_mp); 1530 } 1531 1532 /* 1533 * Remove the item to be processed. If we are removing the last 1534 * item on the list, we need to recalculate the tail pointer. 1535 */ 1536 static void 1537 remove_from_worklist(wk) 1538 struct worklist *wk; 1539 { 1540 struct ufsmount *ump; 1541 1542 ump = VFSTOUFS(wk->wk_mp); 1543 if (ump->softdep_worklist_tail == wk) 1544 ump->softdep_worklist_tail = 1545 (struct worklist *)wk->wk_list.le_prev; 1546 WORKLIST_REMOVE(wk); 1547 ump->softdep_on_worklist -= 1; 1548 } 1549 1550 static void 1551 wake_worklist(wk) 1552 struct worklist *wk; 1553 { 1554 if (wk->wk_state & IOWAITING) { 1555 wk->wk_state &= ~IOWAITING; 1556 wakeup(wk); 1557 } 1558 } 1559 1560 static void 1561 wait_worklist(wk, wmesg) 1562 struct worklist *wk; 1563 char *wmesg; 1564 { 1565 struct ufsmount *ump; 1566 1567 ump = VFSTOUFS(wk->wk_mp); 1568 wk->wk_state |= IOWAITING; 1569 msleep(wk, LOCK_PTR(ump), PVM, wmesg, 0); 1570 } 1571 1572 /* 1573 * Process that runs once per second to handle items in the background queue. 1574 * 1575 * Note that we ensure that everything is done in the order in which they 1576 * appear in the queue. The code below depends on this property to ensure 1577 * that blocks of a file are freed before the inode itself is freed. This 1578 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated 1579 * until all the old ones have been purged from the dependency lists. 1580 */ 1581 static int 1582 softdep_process_worklist(mp, full) 1583 struct mount *mp; 1584 int full; 1585 { 1586 int cnt, matchcnt; 1587 struct ufsmount *ump; 1588 long starttime; 1589 1590 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp")); 1591 if (MOUNTEDSOFTDEP(mp) == 0) 1592 return (0); 1593 matchcnt = 0; 1594 ump = VFSTOUFS(mp); 1595 ACQUIRE_LOCK(ump); 1596 starttime = time_second; 1597 softdep_process_journal(mp, NULL, full ? MNT_WAIT : 0); 1598 check_clear_deps(mp); 1599 while (ump->softdep_on_worklist > 0) { 1600 if ((cnt = process_worklist_item(mp, 10, LK_NOWAIT)) == 0) 1601 break; 1602 else 1603 matchcnt += cnt; 1604 check_clear_deps(mp); 1605 /* 1606 * We do not generally want to stop for buffer space, but if 1607 * we are really being a buffer hog, we will stop and wait. 1608 */ 1609 if (should_yield()) { 1610 FREE_LOCK(ump); 1611 kern_yield(PRI_USER); 1612 bwillwrite(); 1613 ACQUIRE_LOCK(ump); 1614 } 1615 /* 1616 * Never allow processing to run for more than one 1617 * second. This gives the syncer thread the opportunity 1618 * to pause if appropriate. 1619 */ 1620 if (!full && starttime != time_second) 1621 break; 1622 } 1623 if (full == 0) 1624 journal_unsuspend(ump); 1625 FREE_LOCK(ump); 1626 return (matchcnt); 1627 } 1628 1629 /* 1630 * Process all removes associated with a vnode if we are running out of 1631 * journal space. Any other process which attempts to flush these will 1632 * be unable as we have the vnodes locked. 1633 */ 1634 static void 1635 process_removes(vp) 1636 struct vnode *vp; 1637 { 1638 struct inodedep *inodedep; 1639 struct dirrem *dirrem; 1640 struct ufsmount *ump; 1641 struct mount *mp; 1642 ino_t inum; 1643 1644 mp = vp->v_mount; 1645 ump = VFSTOUFS(mp); 1646 LOCK_OWNED(ump); 1647 inum = VTOI(vp)->i_number; 1648 for (;;) { 1649 top: 1650 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1651 return; 1652 LIST_FOREACH(dirrem, &inodedep->id_dirremhd, dm_inonext) { 1653 /* 1654 * If another thread is trying to lock this vnode 1655 * it will fail but we must wait for it to do so 1656 * before we can proceed. 1657 */ 1658 if (dirrem->dm_state & INPROGRESS) { 1659 wait_worklist(&dirrem->dm_list, "pwrwait"); 1660 goto top; 1661 } 1662 if ((dirrem->dm_state & (COMPLETE | ONWORKLIST)) == 1663 (COMPLETE | ONWORKLIST)) 1664 break; 1665 } 1666 if (dirrem == NULL) 1667 return; 1668 remove_from_worklist(&dirrem->dm_list); 1669 FREE_LOCK(ump); 1670 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1671 panic("process_removes: suspended filesystem"); 1672 handle_workitem_remove(dirrem, 0); 1673 vn_finished_secondary_write(mp); 1674 ACQUIRE_LOCK(ump); 1675 } 1676 } 1677 1678 /* 1679 * Process all truncations associated with a vnode if we are running out 1680 * of journal space. This is called when the vnode lock is already held 1681 * and no other process can clear the truncation. This function returns 1682 * a value greater than zero if it did any work. 1683 */ 1684 static void 1685 process_truncates(vp) 1686 struct vnode *vp; 1687 { 1688 struct inodedep *inodedep; 1689 struct freeblks *freeblks; 1690 struct ufsmount *ump; 1691 struct mount *mp; 1692 ino_t inum; 1693 int cgwait; 1694 1695 mp = vp->v_mount; 1696 ump = VFSTOUFS(mp); 1697 LOCK_OWNED(ump); 1698 inum = VTOI(vp)->i_number; 1699 for (;;) { 1700 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1701 return; 1702 cgwait = 0; 1703 TAILQ_FOREACH(freeblks, &inodedep->id_freeblklst, fb_next) { 1704 /* Journal entries not yet written. */ 1705 if (!LIST_EMPTY(&freeblks->fb_jblkdephd)) { 1706 jwait(&LIST_FIRST( 1707 &freeblks->fb_jblkdephd)->jb_list, 1708 MNT_WAIT); 1709 break; 1710 } 1711 /* Another thread is executing this item. */ 1712 if (freeblks->fb_state & INPROGRESS) { 1713 wait_worklist(&freeblks->fb_list, "ptrwait"); 1714 break; 1715 } 1716 /* Freeblks is waiting on a inode write. */ 1717 if ((freeblks->fb_state & COMPLETE) == 0) { 1718 FREE_LOCK(ump); 1719 ffs_update(vp, 1); 1720 ACQUIRE_LOCK(ump); 1721 break; 1722 } 1723 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST)) == 1724 (ALLCOMPLETE | ONWORKLIST)) { 1725 remove_from_worklist(&freeblks->fb_list); 1726 freeblks->fb_state |= INPROGRESS; 1727 FREE_LOCK(ump); 1728 if (vn_start_secondary_write(NULL, &mp, 1729 V_NOWAIT)) 1730 panic("process_truncates: " 1731 "suspended filesystem"); 1732 handle_workitem_freeblocks(freeblks, 0); 1733 vn_finished_secondary_write(mp); 1734 ACQUIRE_LOCK(ump); 1735 break; 1736 } 1737 if (freeblks->fb_cgwait) 1738 cgwait++; 1739 } 1740 if (cgwait) { 1741 FREE_LOCK(ump); 1742 sync_cgs(mp, MNT_WAIT); 1743 ffs_sync_snap(mp, MNT_WAIT); 1744 ACQUIRE_LOCK(ump); 1745 continue; 1746 } 1747 if (freeblks == NULL) 1748 break; 1749 } 1750 return; 1751 } 1752 1753 /* 1754 * Process one item on the worklist. 1755 */ 1756 static int 1757 process_worklist_item(mp, target, flags) 1758 struct mount *mp; 1759 int target; 1760 int flags; 1761 { 1762 struct worklist sentinel; 1763 struct worklist *wk; 1764 struct ufsmount *ump; 1765 int matchcnt; 1766 int error; 1767 1768 KASSERT(mp != NULL, ("process_worklist_item: NULL mp")); 1769 /* 1770 * If we are being called because of a process doing a 1771 * copy-on-write, then it is not safe to write as we may 1772 * recurse into the copy-on-write routine. 1773 */ 1774 if (curthread->td_pflags & TDP_COWINPROGRESS) 1775 return (-1); 1776 PHOLD(curproc); /* Don't let the stack go away. */ 1777 ump = VFSTOUFS(mp); 1778 LOCK_OWNED(ump); 1779 matchcnt = 0; 1780 sentinel.wk_mp = NULL; 1781 sentinel.wk_type = D_SENTINEL; 1782 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, &sentinel, wk_list); 1783 for (wk = LIST_NEXT(&sentinel, wk_list); wk != NULL; 1784 wk = LIST_NEXT(&sentinel, wk_list)) { 1785 if (wk->wk_type == D_SENTINEL) { 1786 LIST_REMOVE(&sentinel, wk_list); 1787 LIST_INSERT_AFTER(wk, &sentinel, wk_list); 1788 continue; 1789 } 1790 if (wk->wk_state & INPROGRESS) 1791 panic("process_worklist_item: %p already in progress.", 1792 wk); 1793 wk->wk_state |= INPROGRESS; 1794 remove_from_worklist(wk); 1795 FREE_LOCK(ump); 1796 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1797 panic("process_worklist_item: suspended filesystem"); 1798 switch (wk->wk_type) { 1799 case D_DIRREM: 1800 /* removal of a directory entry */ 1801 error = handle_workitem_remove(WK_DIRREM(wk), flags); 1802 break; 1803 1804 case D_FREEBLKS: 1805 /* releasing blocks and/or fragments from a file */ 1806 error = handle_workitem_freeblocks(WK_FREEBLKS(wk), 1807 flags); 1808 break; 1809 1810 case D_FREEFRAG: 1811 /* releasing a fragment when replaced as a file grows */ 1812 handle_workitem_freefrag(WK_FREEFRAG(wk)); 1813 error = 0; 1814 break; 1815 1816 case D_FREEFILE: 1817 /* releasing an inode when its link count drops to 0 */ 1818 handle_workitem_freefile(WK_FREEFILE(wk)); 1819 error = 0; 1820 break; 1821 1822 default: 1823 panic("%s_process_worklist: Unknown type %s", 1824 "softdep", TYPENAME(wk->wk_type)); 1825 /* NOTREACHED */ 1826 } 1827 vn_finished_secondary_write(mp); 1828 ACQUIRE_LOCK(ump); 1829 if (error == 0) { 1830 if (++matchcnt == target) 1831 break; 1832 continue; 1833 } 1834 /* 1835 * We have to retry the worklist item later. Wake up any 1836 * waiters who may be able to complete it immediately and 1837 * add the item back to the head so we don't try to execute 1838 * it again. 1839 */ 1840 wk->wk_state &= ~INPROGRESS; 1841 wake_worklist(wk); 1842 add_to_worklist(wk, WK_HEAD); 1843 } 1844 /* Sentinal could've become the tail from remove_from_worklist. */ 1845 if (ump->softdep_worklist_tail == &sentinel) 1846 ump->softdep_worklist_tail = 1847 (struct worklist *)sentinel.wk_list.le_prev; 1848 LIST_REMOVE(&sentinel, wk_list); 1849 PRELE(curproc); 1850 return (matchcnt); 1851 } 1852 1853 /* 1854 * Move dependencies from one buffer to another. 1855 */ 1856 int 1857 softdep_move_dependencies(oldbp, newbp) 1858 struct buf *oldbp; 1859 struct buf *newbp; 1860 { 1861 struct worklist *wk, *wktail; 1862 struct ufsmount *ump; 1863 int dirty; 1864 1865 if ((wk = LIST_FIRST(&oldbp->b_dep)) == NULL) 1866 return (0); 1867 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 1868 ("softdep_move_dependencies called on non-softdep filesystem")); 1869 dirty = 0; 1870 wktail = NULL; 1871 ump = VFSTOUFS(wk->wk_mp); 1872 ACQUIRE_LOCK(ump); 1873 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) { 1874 LIST_REMOVE(wk, wk_list); 1875 if (wk->wk_type == D_BMSAFEMAP && 1876 bmsafemap_backgroundwrite(WK_BMSAFEMAP(wk), newbp)) 1877 dirty = 1; 1878 if (wktail == NULL) 1879 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list); 1880 else 1881 LIST_INSERT_AFTER(wktail, wk, wk_list); 1882 wktail = wk; 1883 } 1884 FREE_LOCK(ump); 1885 1886 return (dirty); 1887 } 1888 1889 /* 1890 * Purge the work list of all items associated with a particular mount point. 1891 */ 1892 int 1893 softdep_flushworklist(oldmnt, countp, td) 1894 struct mount *oldmnt; 1895 int *countp; 1896 struct thread *td; 1897 { 1898 struct vnode *devvp; 1899 struct ufsmount *ump; 1900 int count, error; 1901 1902 /* 1903 * Alternately flush the block device associated with the mount 1904 * point and process any dependencies that the flushing 1905 * creates. We continue until no more worklist dependencies 1906 * are found. 1907 */ 1908 *countp = 0; 1909 error = 0; 1910 ump = VFSTOUFS(oldmnt); 1911 devvp = ump->um_devvp; 1912 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) { 1913 *countp += count; 1914 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1915 error = VOP_FSYNC(devvp, MNT_WAIT, td); 1916 VOP_UNLOCK(devvp, 0); 1917 if (error != 0) 1918 break; 1919 } 1920 return (error); 1921 } 1922 1923 #define SU_WAITIDLE_RETRIES 20 1924 static int 1925 softdep_waitidle(struct mount *mp, int flags __unused) 1926 { 1927 struct ufsmount *ump; 1928 struct vnode *devvp; 1929 struct thread *td; 1930 int error, i; 1931 1932 ump = VFSTOUFS(mp); 1933 devvp = ump->um_devvp; 1934 td = curthread; 1935 error = 0; 1936 ACQUIRE_LOCK(ump); 1937 for (i = 0; i < SU_WAITIDLE_RETRIES && ump->softdep_deps != 0; i++) { 1938 ump->softdep_req = 1; 1939 KASSERT((flags & FORCECLOSE) == 0 || 1940 ump->softdep_on_worklist == 0, 1941 ("softdep_waitidle: work added after flush")); 1942 msleep(&ump->softdep_deps, LOCK_PTR(ump), PVM | PDROP, 1943 "softdeps", 10 * hz); 1944 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1945 error = VOP_FSYNC(devvp, MNT_WAIT, td); 1946 VOP_UNLOCK(devvp, 0); 1947 ACQUIRE_LOCK(ump); 1948 if (error != 0) 1949 break; 1950 } 1951 ump->softdep_req = 0; 1952 if (i == SU_WAITIDLE_RETRIES && error == 0 && ump->softdep_deps != 0) { 1953 error = EBUSY; 1954 printf("softdep_waitidle: Failed to flush worklist for %p\n", 1955 mp); 1956 } 1957 FREE_LOCK(ump); 1958 return (error); 1959 } 1960 1961 /* 1962 * Flush all vnodes and worklist items associated with a specified mount point. 1963 */ 1964 int 1965 softdep_flushfiles(oldmnt, flags, td) 1966 struct mount *oldmnt; 1967 int flags; 1968 struct thread *td; 1969 { 1970 #ifdef QUOTA 1971 struct ufsmount *ump; 1972 int i; 1973 #endif 1974 int error, early, depcount, loopcnt, retry_flush_count, retry; 1975 int morework; 1976 1977 KASSERT(MOUNTEDSOFTDEP(oldmnt) != 0, 1978 ("softdep_flushfiles called on non-softdep filesystem")); 1979 loopcnt = 10; 1980 retry_flush_count = 3; 1981 retry_flush: 1982 error = 0; 1983 1984 /* 1985 * Alternately flush the vnodes associated with the mount 1986 * point and process any dependencies that the flushing 1987 * creates. In theory, this loop can happen at most twice, 1988 * but we give it a few extra just to be sure. 1989 */ 1990 for (; loopcnt > 0; loopcnt--) { 1991 /* 1992 * Do another flush in case any vnodes were brought in 1993 * as part of the cleanup operations. 1994 */ 1995 early = retry_flush_count == 1 || (oldmnt->mnt_kern_flag & 1996 MNTK_UNMOUNT) == 0 ? 0 : EARLYFLUSH; 1997 if ((error = ffs_flushfiles(oldmnt, flags | early, td)) != 0) 1998 break; 1999 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 || 2000 depcount == 0) 2001 break; 2002 } 2003 /* 2004 * If we are unmounting then it is an error to fail. If we 2005 * are simply trying to downgrade to read-only, then filesystem 2006 * activity can keep us busy forever, so we just fail with EBUSY. 2007 */ 2008 if (loopcnt == 0) { 2009 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) 2010 panic("softdep_flushfiles: looping"); 2011 error = EBUSY; 2012 } 2013 if (!error) 2014 error = softdep_waitidle(oldmnt, flags); 2015 if (!error) { 2016 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) { 2017 retry = 0; 2018 MNT_ILOCK(oldmnt); 2019 KASSERT((oldmnt->mnt_kern_flag & MNTK_NOINSMNTQ) != 0, 2020 ("softdep_flushfiles: !MNTK_NOINSMNTQ")); 2021 morework = oldmnt->mnt_nvnodelistsize > 0; 2022 #ifdef QUOTA 2023 ump = VFSTOUFS(oldmnt); 2024 UFS_LOCK(ump); 2025 for (i = 0; i < MAXQUOTAS; i++) { 2026 if (ump->um_quotas[i] != NULLVP) 2027 morework = 1; 2028 } 2029 UFS_UNLOCK(ump); 2030 #endif 2031 if (morework) { 2032 if (--retry_flush_count > 0) { 2033 retry = 1; 2034 loopcnt = 3; 2035 } else 2036 error = EBUSY; 2037 } 2038 MNT_IUNLOCK(oldmnt); 2039 if (retry) 2040 goto retry_flush; 2041 } 2042 } 2043 return (error); 2044 } 2045 2046 /* 2047 * Structure hashing. 2048 * 2049 * There are four types of structures that can be looked up: 2050 * 1) pagedep structures identified by mount point, inode number, 2051 * and logical block. 2052 * 2) inodedep structures identified by mount point and inode number. 2053 * 3) newblk structures identified by mount point and 2054 * physical block number. 2055 * 4) bmsafemap structures identified by mount point and 2056 * cylinder group number. 2057 * 2058 * The "pagedep" and "inodedep" dependency structures are hashed 2059 * separately from the file blocks and inodes to which they correspond. 2060 * This separation helps when the in-memory copy of an inode or 2061 * file block must be replaced. It also obviates the need to access 2062 * an inode or file page when simply updating (or de-allocating) 2063 * dependency structures. Lookup of newblk structures is needed to 2064 * find newly allocated blocks when trying to associate them with 2065 * their allocdirect or allocindir structure. 2066 * 2067 * The lookup routines optionally create and hash a new instance when 2068 * an existing entry is not found. The bmsafemap lookup routine always 2069 * allocates a new structure if an existing one is not found. 2070 */ 2071 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */ 2072 2073 /* 2074 * Structures and routines associated with pagedep caching. 2075 */ 2076 #define PAGEDEP_HASH(ump, inum, lbn) \ 2077 (&(ump)->pagedep_hashtbl[((inum) + (lbn)) & (ump)->pagedep_hash_size]) 2078 2079 static int 2080 pagedep_find(pagedephd, ino, lbn, pagedeppp) 2081 struct pagedep_hashhead *pagedephd; 2082 ino_t ino; 2083 ufs_lbn_t lbn; 2084 struct pagedep **pagedeppp; 2085 { 2086 struct pagedep *pagedep; 2087 2088 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 2089 if (ino == pagedep->pd_ino && lbn == pagedep->pd_lbn) { 2090 *pagedeppp = pagedep; 2091 return (1); 2092 } 2093 } 2094 *pagedeppp = NULL; 2095 return (0); 2096 } 2097 /* 2098 * Look up a pagedep. Return 1 if found, 0 otherwise. 2099 * If not found, allocate if DEPALLOC flag is passed. 2100 * Found or allocated entry is returned in pagedeppp. 2101 * This routine must be called with splbio interrupts blocked. 2102 */ 2103 static int 2104 pagedep_lookup(mp, bp, ino, lbn, flags, pagedeppp) 2105 struct mount *mp; 2106 struct buf *bp; 2107 ino_t ino; 2108 ufs_lbn_t lbn; 2109 int flags; 2110 struct pagedep **pagedeppp; 2111 { 2112 struct pagedep *pagedep; 2113 struct pagedep_hashhead *pagedephd; 2114 struct worklist *wk; 2115 struct ufsmount *ump; 2116 int ret; 2117 int i; 2118 2119 ump = VFSTOUFS(mp); 2120 LOCK_OWNED(ump); 2121 if (bp) { 2122 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 2123 if (wk->wk_type == D_PAGEDEP) { 2124 *pagedeppp = WK_PAGEDEP(wk); 2125 return (1); 2126 } 2127 } 2128 } 2129 pagedephd = PAGEDEP_HASH(ump, ino, lbn); 2130 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp); 2131 if (ret) { 2132 if (((*pagedeppp)->pd_state & ONWORKLIST) == 0 && bp) 2133 WORKLIST_INSERT(&bp->b_dep, &(*pagedeppp)->pd_list); 2134 return (1); 2135 } 2136 if ((flags & DEPALLOC) == 0) 2137 return (0); 2138 FREE_LOCK(ump); 2139 pagedep = malloc(sizeof(struct pagedep), 2140 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO); 2141 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp); 2142 ACQUIRE_LOCK(ump); 2143 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp); 2144 if (*pagedeppp) { 2145 /* 2146 * This should never happen since we only create pagedeps 2147 * with the vnode lock held. Could be an assert. 2148 */ 2149 WORKITEM_FREE(pagedep, D_PAGEDEP); 2150 return (ret); 2151 } 2152 pagedep->pd_ino = ino; 2153 pagedep->pd_lbn = lbn; 2154 LIST_INIT(&pagedep->pd_dirremhd); 2155 LIST_INIT(&pagedep->pd_pendinghd); 2156 for (i = 0; i < DAHASHSZ; i++) 2157 LIST_INIT(&pagedep->pd_diraddhd[i]); 2158 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash); 2159 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list); 2160 *pagedeppp = pagedep; 2161 return (0); 2162 } 2163 2164 /* 2165 * Structures and routines associated with inodedep caching. 2166 */ 2167 #define INODEDEP_HASH(ump, inum) \ 2168 (&(ump)->inodedep_hashtbl[(inum) & (ump)->inodedep_hash_size]) 2169 2170 static int 2171 inodedep_find(inodedephd, inum, inodedeppp) 2172 struct inodedep_hashhead *inodedephd; 2173 ino_t inum; 2174 struct inodedep **inodedeppp; 2175 { 2176 struct inodedep *inodedep; 2177 2178 LIST_FOREACH(inodedep, inodedephd, id_hash) 2179 if (inum == inodedep->id_ino) 2180 break; 2181 if (inodedep) { 2182 *inodedeppp = inodedep; 2183 return (1); 2184 } 2185 *inodedeppp = NULL; 2186 2187 return (0); 2188 } 2189 /* 2190 * Look up an inodedep. Return 1 if found, 0 if not found. 2191 * If not found, allocate if DEPALLOC flag is passed. 2192 * Found or allocated entry is returned in inodedeppp. 2193 * This routine must be called with splbio interrupts blocked. 2194 */ 2195 static int 2196 inodedep_lookup(mp, inum, flags, inodedeppp) 2197 struct mount *mp; 2198 ino_t inum; 2199 int flags; 2200 struct inodedep **inodedeppp; 2201 { 2202 struct inodedep *inodedep; 2203 struct inodedep_hashhead *inodedephd; 2204 struct ufsmount *ump; 2205 struct fs *fs; 2206 2207 ump = VFSTOUFS(mp); 2208 LOCK_OWNED(ump); 2209 fs = ump->um_fs; 2210 inodedephd = INODEDEP_HASH(ump, inum); 2211 2212 if (inodedep_find(inodedephd, inum, inodedeppp)) 2213 return (1); 2214 if ((flags & DEPALLOC) == 0) 2215 return (0); 2216 /* 2217 * If the system is over its limit and our filesystem is 2218 * responsible for more than our share of that usage and 2219 * we are not in a rush, request some inodedep cleanup. 2220 */ 2221 if (softdep_excess_items(ump, D_INODEDEP)) 2222 schedule_cleanup(mp); 2223 else 2224 FREE_LOCK(ump); 2225 inodedep = malloc(sizeof(struct inodedep), 2226 M_INODEDEP, M_SOFTDEP_FLAGS); 2227 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp); 2228 ACQUIRE_LOCK(ump); 2229 if (inodedep_find(inodedephd, inum, inodedeppp)) { 2230 WORKITEM_FREE(inodedep, D_INODEDEP); 2231 return (1); 2232 } 2233 inodedep->id_fs = fs; 2234 inodedep->id_ino = inum; 2235 inodedep->id_state = ALLCOMPLETE; 2236 inodedep->id_nlinkdelta = 0; 2237 inodedep->id_savedino1 = NULL; 2238 inodedep->id_savedsize = -1; 2239 inodedep->id_savedextsize = -1; 2240 inodedep->id_savednlink = -1; 2241 inodedep->id_bmsafemap = NULL; 2242 inodedep->id_mkdiradd = NULL; 2243 LIST_INIT(&inodedep->id_dirremhd); 2244 LIST_INIT(&inodedep->id_pendinghd); 2245 LIST_INIT(&inodedep->id_inowait); 2246 LIST_INIT(&inodedep->id_bufwait); 2247 TAILQ_INIT(&inodedep->id_inoreflst); 2248 TAILQ_INIT(&inodedep->id_inoupdt); 2249 TAILQ_INIT(&inodedep->id_newinoupdt); 2250 TAILQ_INIT(&inodedep->id_extupdt); 2251 TAILQ_INIT(&inodedep->id_newextupdt); 2252 TAILQ_INIT(&inodedep->id_freeblklst); 2253 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash); 2254 *inodedeppp = inodedep; 2255 return (0); 2256 } 2257 2258 /* 2259 * Structures and routines associated with newblk caching. 2260 */ 2261 #define NEWBLK_HASH(ump, inum) \ 2262 (&(ump)->newblk_hashtbl[(inum) & (ump)->newblk_hash_size]) 2263 2264 static int 2265 newblk_find(newblkhd, newblkno, flags, newblkpp) 2266 struct newblk_hashhead *newblkhd; 2267 ufs2_daddr_t newblkno; 2268 int flags; 2269 struct newblk **newblkpp; 2270 { 2271 struct newblk *newblk; 2272 2273 LIST_FOREACH(newblk, newblkhd, nb_hash) { 2274 if (newblkno != newblk->nb_newblkno) 2275 continue; 2276 /* 2277 * If we're creating a new dependency don't match those that 2278 * have already been converted to allocdirects. This is for 2279 * a frag extend. 2280 */ 2281 if ((flags & DEPALLOC) && newblk->nb_list.wk_type != D_NEWBLK) 2282 continue; 2283 break; 2284 } 2285 if (newblk) { 2286 *newblkpp = newblk; 2287 return (1); 2288 } 2289 *newblkpp = NULL; 2290 return (0); 2291 } 2292 2293 /* 2294 * Look up a newblk. Return 1 if found, 0 if not found. 2295 * If not found, allocate if DEPALLOC flag is passed. 2296 * Found or allocated entry is returned in newblkpp. 2297 */ 2298 static int 2299 newblk_lookup(mp, newblkno, flags, newblkpp) 2300 struct mount *mp; 2301 ufs2_daddr_t newblkno; 2302 int flags; 2303 struct newblk **newblkpp; 2304 { 2305 struct newblk *newblk; 2306 struct newblk_hashhead *newblkhd; 2307 struct ufsmount *ump; 2308 2309 ump = VFSTOUFS(mp); 2310 LOCK_OWNED(ump); 2311 newblkhd = NEWBLK_HASH(ump, newblkno); 2312 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) 2313 return (1); 2314 if ((flags & DEPALLOC) == 0) 2315 return (0); 2316 if (softdep_excess_items(ump, D_NEWBLK) || 2317 softdep_excess_items(ump, D_ALLOCDIRECT) || 2318 softdep_excess_items(ump, D_ALLOCINDIR)) 2319 schedule_cleanup(mp); 2320 else 2321 FREE_LOCK(ump); 2322 newblk = malloc(sizeof(union allblk), M_NEWBLK, 2323 M_SOFTDEP_FLAGS | M_ZERO); 2324 workitem_alloc(&newblk->nb_list, D_NEWBLK, mp); 2325 ACQUIRE_LOCK(ump); 2326 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) { 2327 WORKITEM_FREE(newblk, D_NEWBLK); 2328 return (1); 2329 } 2330 newblk->nb_freefrag = NULL; 2331 LIST_INIT(&newblk->nb_indirdeps); 2332 LIST_INIT(&newblk->nb_newdirblk); 2333 LIST_INIT(&newblk->nb_jwork); 2334 newblk->nb_state = ATTACHED; 2335 newblk->nb_newblkno = newblkno; 2336 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash); 2337 *newblkpp = newblk; 2338 return (0); 2339 } 2340 2341 /* 2342 * Structures and routines associated with freed indirect block caching. 2343 */ 2344 #define INDIR_HASH(ump, blkno) \ 2345 (&(ump)->indir_hashtbl[(blkno) & (ump)->indir_hash_size]) 2346 2347 /* 2348 * Lookup an indirect block in the indir hash table. The freework is 2349 * removed and potentially freed. The caller must do a blocking journal 2350 * write before writing to the blkno. 2351 */ 2352 static int 2353 indirblk_lookup(mp, blkno) 2354 struct mount *mp; 2355 ufs2_daddr_t blkno; 2356 { 2357 struct freework *freework; 2358 struct indir_hashhead *wkhd; 2359 struct ufsmount *ump; 2360 2361 ump = VFSTOUFS(mp); 2362 wkhd = INDIR_HASH(ump, blkno); 2363 TAILQ_FOREACH(freework, wkhd, fw_next) { 2364 if (freework->fw_blkno != blkno) 2365 continue; 2366 indirblk_remove(freework); 2367 return (1); 2368 } 2369 return (0); 2370 } 2371 2372 /* 2373 * Insert an indirect block represented by freework into the indirblk 2374 * hash table so that it may prevent the block from being re-used prior 2375 * to the journal being written. 2376 */ 2377 static void 2378 indirblk_insert(freework) 2379 struct freework *freework; 2380 { 2381 struct jblocks *jblocks; 2382 struct jseg *jseg; 2383 struct ufsmount *ump; 2384 2385 ump = VFSTOUFS(freework->fw_list.wk_mp); 2386 jblocks = ump->softdep_jblocks; 2387 jseg = TAILQ_LAST(&jblocks->jb_segs, jseglst); 2388 if (jseg == NULL) 2389 return; 2390 2391 LIST_INSERT_HEAD(&jseg->js_indirs, freework, fw_segs); 2392 TAILQ_INSERT_HEAD(INDIR_HASH(ump, freework->fw_blkno), freework, 2393 fw_next); 2394 freework->fw_state &= ~DEPCOMPLETE; 2395 } 2396 2397 static void 2398 indirblk_remove(freework) 2399 struct freework *freework; 2400 { 2401 struct ufsmount *ump; 2402 2403 ump = VFSTOUFS(freework->fw_list.wk_mp); 2404 LIST_REMOVE(freework, fw_segs); 2405 TAILQ_REMOVE(INDIR_HASH(ump, freework->fw_blkno), freework, fw_next); 2406 freework->fw_state |= DEPCOMPLETE; 2407 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 2408 WORKITEM_FREE(freework, D_FREEWORK); 2409 } 2410 2411 /* 2412 * Executed during filesystem system initialization before 2413 * mounting any filesystems. 2414 */ 2415 void 2416 softdep_initialize() 2417 { 2418 2419 TAILQ_INIT(&softdepmounts); 2420 #ifdef __LP64__ 2421 max_softdeps = desiredvnodes * 4; 2422 #else 2423 max_softdeps = desiredvnodes * 2; 2424 #endif 2425 2426 /* initialise bioops hack */ 2427 bioops.io_start = softdep_disk_io_initiation; 2428 bioops.io_complete = softdep_disk_write_complete; 2429 bioops.io_deallocate = softdep_deallocate_dependencies; 2430 bioops.io_countdeps = softdep_count_dependencies; 2431 softdep_ast_cleanup = softdep_ast_cleanup_proc; 2432 2433 /* Initialize the callout with an mtx. */ 2434 callout_init_mtx(&softdep_callout, &lk, 0); 2435 } 2436 2437 /* 2438 * Executed after all filesystems have been unmounted during 2439 * filesystem module unload. 2440 */ 2441 void 2442 softdep_uninitialize() 2443 { 2444 2445 /* clear bioops hack */ 2446 bioops.io_start = NULL; 2447 bioops.io_complete = NULL; 2448 bioops.io_deallocate = NULL; 2449 bioops.io_countdeps = NULL; 2450 softdep_ast_cleanup = NULL; 2451 2452 callout_drain(&softdep_callout); 2453 } 2454 2455 /* 2456 * Called at mount time to notify the dependency code that a 2457 * filesystem wishes to use it. 2458 */ 2459 int 2460 softdep_mount(devvp, mp, fs, cred) 2461 struct vnode *devvp; 2462 struct mount *mp; 2463 struct fs *fs; 2464 struct ucred *cred; 2465 { 2466 struct csum_total cstotal; 2467 struct mount_softdeps *sdp; 2468 struct ufsmount *ump; 2469 struct cg *cgp; 2470 struct buf *bp; 2471 u_int cyl, i; 2472 int error; 2473 2474 sdp = malloc(sizeof(struct mount_softdeps), M_MOUNTDATA, 2475 M_WAITOK | M_ZERO); 2476 MNT_ILOCK(mp); 2477 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP; 2478 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) { 2479 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) | 2480 MNTK_SOFTDEP | MNTK_NOASYNC; 2481 } 2482 ump = VFSTOUFS(mp); 2483 ump->um_softdep = sdp; 2484 MNT_IUNLOCK(mp); 2485 rw_init(LOCK_PTR(ump), "Per-Filesystem Softdep Lock"); 2486 sdp->sd_ump = ump; 2487 LIST_INIT(&ump->softdep_workitem_pending); 2488 LIST_INIT(&ump->softdep_journal_pending); 2489 TAILQ_INIT(&ump->softdep_unlinked); 2490 LIST_INIT(&ump->softdep_dirtycg); 2491 ump->softdep_worklist_tail = NULL; 2492 ump->softdep_on_worklist = 0; 2493 ump->softdep_deps = 0; 2494 LIST_INIT(&ump->softdep_mkdirlisthd); 2495 ump->pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP, 2496 &ump->pagedep_hash_size); 2497 ump->pagedep_nextclean = 0; 2498 ump->inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP, 2499 &ump->inodedep_hash_size); 2500 ump->inodedep_nextclean = 0; 2501 ump->newblk_hashtbl = hashinit(max_softdeps / 2, M_NEWBLK, 2502 &ump->newblk_hash_size); 2503 ump->bmsafemap_hashtbl = hashinit(1024, M_BMSAFEMAP, 2504 &ump->bmsafemap_hash_size); 2505 i = 1 << (ffs(desiredvnodes / 10) - 1); 2506 ump->indir_hashtbl = malloc(i * sizeof(struct indir_hashhead), 2507 M_FREEWORK, M_WAITOK); 2508 ump->indir_hash_size = i - 1; 2509 for (i = 0; i <= ump->indir_hash_size; i++) 2510 TAILQ_INIT(&ump->indir_hashtbl[i]); 2511 ACQUIRE_GBLLOCK(&lk); 2512 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next); 2513 FREE_GBLLOCK(&lk); 2514 if ((fs->fs_flags & FS_SUJ) && 2515 (error = journal_mount(mp, fs, cred)) != 0) { 2516 printf("Failed to start journal: %d\n", error); 2517 softdep_unmount(mp); 2518 return (error); 2519 } 2520 /* 2521 * Start our flushing thread in the bufdaemon process. 2522 */ 2523 ACQUIRE_LOCK(ump); 2524 ump->softdep_flags |= FLUSH_STARTING; 2525 FREE_LOCK(ump); 2526 kproc_kthread_add(&softdep_flush, mp, &bufdaemonproc, 2527 &ump->softdep_flushtd, 0, 0, "softdepflush", "%s worker", 2528 mp->mnt_stat.f_mntonname); 2529 ACQUIRE_LOCK(ump); 2530 while ((ump->softdep_flags & FLUSH_STARTING) != 0) { 2531 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, "sdstart", 2532 hz / 2); 2533 } 2534 FREE_LOCK(ump); 2535 /* 2536 * When doing soft updates, the counters in the 2537 * superblock may have gotten out of sync. Recomputation 2538 * can take a long time and can be deferred for background 2539 * fsck. However, the old behavior of scanning the cylinder 2540 * groups and recalculating them at mount time is available 2541 * by setting vfs.ffs.compute_summary_at_mount to one. 2542 */ 2543 if (compute_summary_at_mount == 0 || fs->fs_clean != 0) 2544 return (0); 2545 bzero(&cstotal, sizeof cstotal); 2546 for (cyl = 0; cyl < fs->fs_ncg; cyl++) { 2547 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)), 2548 fs->fs_cgsize, cred, &bp)) != 0) { 2549 brelse(bp); 2550 softdep_unmount(mp); 2551 return (error); 2552 } 2553 cgp = (struct cg *)bp->b_data; 2554 cstotal.cs_nffree += cgp->cg_cs.cs_nffree; 2555 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree; 2556 cstotal.cs_nifree += cgp->cg_cs.cs_nifree; 2557 cstotal.cs_ndir += cgp->cg_cs.cs_ndir; 2558 fs->fs_cs(fs, cyl) = cgp->cg_cs; 2559 brelse(bp); 2560 } 2561 #ifdef DEBUG 2562 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal)) 2563 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt); 2564 #endif 2565 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal); 2566 return (0); 2567 } 2568 2569 void 2570 softdep_unmount(mp) 2571 struct mount *mp; 2572 { 2573 struct ufsmount *ump; 2574 #ifdef INVARIANTS 2575 int i; 2576 #endif 2577 2578 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 2579 ("softdep_unmount called on non-softdep filesystem")); 2580 ump = VFSTOUFS(mp); 2581 MNT_ILOCK(mp); 2582 mp->mnt_flag &= ~MNT_SOFTDEP; 2583 if (MOUNTEDSUJ(mp) == 0) { 2584 MNT_IUNLOCK(mp); 2585 } else { 2586 mp->mnt_flag &= ~MNT_SUJ; 2587 MNT_IUNLOCK(mp); 2588 journal_unmount(ump); 2589 } 2590 /* 2591 * Shut down our flushing thread. Check for NULL is if 2592 * softdep_mount errors out before the thread has been created. 2593 */ 2594 if (ump->softdep_flushtd != NULL) { 2595 ACQUIRE_LOCK(ump); 2596 ump->softdep_flags |= FLUSH_EXIT; 2597 wakeup(&ump->softdep_flushtd); 2598 msleep(&ump->softdep_flags, LOCK_PTR(ump), PVM | PDROP, 2599 "sdwait", 0); 2600 KASSERT((ump->softdep_flags & FLUSH_EXIT) == 0, 2601 ("Thread shutdown failed")); 2602 } 2603 /* 2604 * Free up our resources. 2605 */ 2606 ACQUIRE_GBLLOCK(&lk); 2607 TAILQ_REMOVE(&softdepmounts, ump->um_softdep, sd_next); 2608 FREE_GBLLOCK(&lk); 2609 rw_destroy(LOCK_PTR(ump)); 2610 hashdestroy(ump->pagedep_hashtbl, M_PAGEDEP, ump->pagedep_hash_size); 2611 hashdestroy(ump->inodedep_hashtbl, M_INODEDEP, ump->inodedep_hash_size); 2612 hashdestroy(ump->newblk_hashtbl, M_NEWBLK, ump->newblk_hash_size); 2613 hashdestroy(ump->bmsafemap_hashtbl, M_BMSAFEMAP, 2614 ump->bmsafemap_hash_size); 2615 free(ump->indir_hashtbl, M_FREEWORK); 2616 #ifdef INVARIANTS 2617 for (i = 0; i <= D_LAST; i++) 2618 KASSERT(ump->softdep_curdeps[i] == 0, 2619 ("Unmount %s: Dep type %s != 0 (%ld)", ump->um_fs->fs_fsmnt, 2620 TYPENAME(i), ump->softdep_curdeps[i])); 2621 #endif 2622 free(ump->um_softdep, M_MOUNTDATA); 2623 } 2624 2625 static struct jblocks * 2626 jblocks_create(void) 2627 { 2628 struct jblocks *jblocks; 2629 2630 jblocks = malloc(sizeof(*jblocks), M_JBLOCKS, M_WAITOK | M_ZERO); 2631 TAILQ_INIT(&jblocks->jb_segs); 2632 jblocks->jb_avail = 10; 2633 jblocks->jb_extent = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2634 M_JBLOCKS, M_WAITOK | M_ZERO); 2635 2636 return (jblocks); 2637 } 2638 2639 static ufs2_daddr_t 2640 jblocks_alloc(jblocks, bytes, actual) 2641 struct jblocks *jblocks; 2642 int bytes; 2643 int *actual; 2644 { 2645 ufs2_daddr_t daddr; 2646 struct jextent *jext; 2647 int freecnt; 2648 int blocks; 2649 2650 blocks = bytes / DEV_BSIZE; 2651 jext = &jblocks->jb_extent[jblocks->jb_head]; 2652 freecnt = jext->je_blocks - jblocks->jb_off; 2653 if (freecnt == 0) { 2654 jblocks->jb_off = 0; 2655 if (++jblocks->jb_head > jblocks->jb_used) 2656 jblocks->jb_head = 0; 2657 jext = &jblocks->jb_extent[jblocks->jb_head]; 2658 freecnt = jext->je_blocks; 2659 } 2660 if (freecnt > blocks) 2661 freecnt = blocks; 2662 *actual = freecnt * DEV_BSIZE; 2663 daddr = jext->je_daddr + jblocks->jb_off; 2664 jblocks->jb_off += freecnt; 2665 jblocks->jb_free -= freecnt; 2666 2667 return (daddr); 2668 } 2669 2670 static void 2671 jblocks_free(jblocks, mp, bytes) 2672 struct jblocks *jblocks; 2673 struct mount *mp; 2674 int bytes; 2675 { 2676 2677 LOCK_OWNED(VFSTOUFS(mp)); 2678 jblocks->jb_free += bytes / DEV_BSIZE; 2679 if (jblocks->jb_suspended) 2680 worklist_speedup(mp); 2681 wakeup(jblocks); 2682 } 2683 2684 static void 2685 jblocks_destroy(jblocks) 2686 struct jblocks *jblocks; 2687 { 2688 2689 if (jblocks->jb_extent) 2690 free(jblocks->jb_extent, M_JBLOCKS); 2691 free(jblocks, M_JBLOCKS); 2692 } 2693 2694 static void 2695 jblocks_add(jblocks, daddr, blocks) 2696 struct jblocks *jblocks; 2697 ufs2_daddr_t daddr; 2698 int blocks; 2699 { 2700 struct jextent *jext; 2701 2702 jblocks->jb_blocks += blocks; 2703 jblocks->jb_free += blocks; 2704 jext = &jblocks->jb_extent[jblocks->jb_used]; 2705 /* Adding the first block. */ 2706 if (jext->je_daddr == 0) { 2707 jext->je_daddr = daddr; 2708 jext->je_blocks = blocks; 2709 return; 2710 } 2711 /* Extending the last extent. */ 2712 if (jext->je_daddr + jext->je_blocks == daddr) { 2713 jext->je_blocks += blocks; 2714 return; 2715 } 2716 /* Adding a new extent. */ 2717 if (++jblocks->jb_used == jblocks->jb_avail) { 2718 jblocks->jb_avail *= 2; 2719 jext = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2720 M_JBLOCKS, M_WAITOK | M_ZERO); 2721 memcpy(jext, jblocks->jb_extent, 2722 sizeof(struct jextent) * jblocks->jb_used); 2723 free(jblocks->jb_extent, M_JBLOCKS); 2724 jblocks->jb_extent = jext; 2725 } 2726 jext = &jblocks->jb_extent[jblocks->jb_used]; 2727 jext->je_daddr = daddr; 2728 jext->je_blocks = blocks; 2729 return; 2730 } 2731 2732 int 2733 softdep_journal_lookup(mp, vpp) 2734 struct mount *mp; 2735 struct vnode **vpp; 2736 { 2737 struct componentname cnp; 2738 struct vnode *dvp; 2739 ino_t sujournal; 2740 int error; 2741 2742 error = VFS_VGET(mp, UFS_ROOTINO, LK_EXCLUSIVE, &dvp); 2743 if (error) 2744 return (error); 2745 bzero(&cnp, sizeof(cnp)); 2746 cnp.cn_nameiop = LOOKUP; 2747 cnp.cn_flags = ISLASTCN; 2748 cnp.cn_thread = curthread; 2749 cnp.cn_cred = curthread->td_ucred; 2750 cnp.cn_pnbuf = SUJ_FILE; 2751 cnp.cn_nameptr = SUJ_FILE; 2752 cnp.cn_namelen = strlen(SUJ_FILE); 2753 error = ufs_lookup_ino(dvp, NULL, &cnp, &sujournal); 2754 vput(dvp); 2755 if (error != 0) 2756 return (error); 2757 error = VFS_VGET(mp, sujournal, LK_EXCLUSIVE, vpp); 2758 return (error); 2759 } 2760 2761 /* 2762 * Open and verify the journal file. 2763 */ 2764 static int 2765 journal_mount(mp, fs, cred) 2766 struct mount *mp; 2767 struct fs *fs; 2768 struct ucred *cred; 2769 { 2770 struct jblocks *jblocks; 2771 struct ufsmount *ump; 2772 struct vnode *vp; 2773 struct inode *ip; 2774 ufs2_daddr_t blkno; 2775 int bcount; 2776 int error; 2777 int i; 2778 2779 ump = VFSTOUFS(mp); 2780 ump->softdep_journal_tail = NULL; 2781 ump->softdep_on_journal = 0; 2782 ump->softdep_accdeps = 0; 2783 ump->softdep_req = 0; 2784 ump->softdep_jblocks = NULL; 2785 error = softdep_journal_lookup(mp, &vp); 2786 if (error != 0) { 2787 printf("Failed to find journal. Use tunefs to create one\n"); 2788 return (error); 2789 } 2790 ip = VTOI(vp); 2791 if (ip->i_size < SUJ_MIN) { 2792 error = ENOSPC; 2793 goto out; 2794 } 2795 bcount = lblkno(fs, ip->i_size); /* Only use whole blocks. */ 2796 jblocks = jblocks_create(); 2797 for (i = 0; i < bcount; i++) { 2798 error = ufs_bmaparray(vp, i, &blkno, NULL, NULL, NULL); 2799 if (error) 2800 break; 2801 jblocks_add(jblocks, blkno, fsbtodb(fs, fs->fs_frag)); 2802 } 2803 if (error) { 2804 jblocks_destroy(jblocks); 2805 goto out; 2806 } 2807 jblocks->jb_low = jblocks->jb_free / 3; /* Reserve 33%. */ 2808 jblocks->jb_min = jblocks->jb_free / 10; /* Suspend at 10%. */ 2809 ump->softdep_jblocks = jblocks; 2810 out: 2811 if (error == 0) { 2812 MNT_ILOCK(mp); 2813 mp->mnt_flag |= MNT_SUJ; 2814 mp->mnt_flag &= ~MNT_SOFTDEP; 2815 MNT_IUNLOCK(mp); 2816 /* 2817 * Only validate the journal contents if the 2818 * filesystem is clean, otherwise we write the logs 2819 * but they'll never be used. If the filesystem was 2820 * still dirty when we mounted it the journal is 2821 * invalid and a new journal can only be valid if it 2822 * starts from a clean mount. 2823 */ 2824 if (fs->fs_clean) { 2825 DIP_SET(ip, i_modrev, fs->fs_mtime); 2826 ip->i_flags |= IN_MODIFIED; 2827 ffs_update(vp, 1); 2828 } 2829 } 2830 vput(vp); 2831 return (error); 2832 } 2833 2834 static void 2835 journal_unmount(ump) 2836 struct ufsmount *ump; 2837 { 2838 2839 if (ump->softdep_jblocks) 2840 jblocks_destroy(ump->softdep_jblocks); 2841 ump->softdep_jblocks = NULL; 2842 } 2843 2844 /* 2845 * Called when a journal record is ready to be written. Space is allocated 2846 * and the journal entry is created when the journal is flushed to stable 2847 * store. 2848 */ 2849 static void 2850 add_to_journal(wk) 2851 struct worklist *wk; 2852 { 2853 struct ufsmount *ump; 2854 2855 ump = VFSTOUFS(wk->wk_mp); 2856 LOCK_OWNED(ump); 2857 if (wk->wk_state & ONWORKLIST) 2858 panic("add_to_journal: %s(0x%X) already on list", 2859 TYPENAME(wk->wk_type), wk->wk_state); 2860 wk->wk_state |= ONWORKLIST | DEPCOMPLETE; 2861 if (LIST_EMPTY(&ump->softdep_journal_pending)) { 2862 ump->softdep_jblocks->jb_age = ticks; 2863 LIST_INSERT_HEAD(&ump->softdep_journal_pending, wk, wk_list); 2864 } else 2865 LIST_INSERT_AFTER(ump->softdep_journal_tail, wk, wk_list); 2866 ump->softdep_journal_tail = wk; 2867 ump->softdep_on_journal += 1; 2868 } 2869 2870 /* 2871 * Remove an arbitrary item for the journal worklist maintain the tail 2872 * pointer. This happens when a new operation obviates the need to 2873 * journal an old operation. 2874 */ 2875 static void 2876 remove_from_journal(wk) 2877 struct worklist *wk; 2878 { 2879 struct ufsmount *ump; 2880 2881 ump = VFSTOUFS(wk->wk_mp); 2882 LOCK_OWNED(ump); 2883 #ifdef SUJ_DEBUG 2884 { 2885 struct worklist *wkn; 2886 2887 LIST_FOREACH(wkn, &ump->softdep_journal_pending, wk_list) 2888 if (wkn == wk) 2889 break; 2890 if (wkn == NULL) 2891 panic("remove_from_journal: %p is not in journal", wk); 2892 } 2893 #endif 2894 /* 2895 * We emulate a TAILQ to save space in most structures which do not 2896 * require TAILQ semantics. Here we must update the tail position 2897 * when removing the tail which is not the final entry. This works 2898 * only if the worklist linkage are at the beginning of the structure. 2899 */ 2900 if (ump->softdep_journal_tail == wk) 2901 ump->softdep_journal_tail = 2902 (struct worklist *)wk->wk_list.le_prev; 2903 WORKLIST_REMOVE(wk); 2904 ump->softdep_on_journal -= 1; 2905 } 2906 2907 /* 2908 * Check for journal space as well as dependency limits so the prelink 2909 * code can throttle both journaled and non-journaled filesystems. 2910 * Threshold is 0 for low and 1 for min. 2911 */ 2912 static int 2913 journal_space(ump, thresh) 2914 struct ufsmount *ump; 2915 int thresh; 2916 { 2917 struct jblocks *jblocks; 2918 int limit, avail; 2919 2920 jblocks = ump->softdep_jblocks; 2921 if (jblocks == NULL) 2922 return (1); 2923 /* 2924 * We use a tighter restriction here to prevent request_cleanup() 2925 * running in threads from running into locks we currently hold. 2926 * We have to be over the limit and our filesystem has to be 2927 * responsible for more than our share of that usage. 2928 */ 2929 limit = (max_softdeps / 10) * 9; 2930 if (dep_current[D_INODEDEP] > limit && 2931 ump->softdep_curdeps[D_INODEDEP] > limit / stat_flush_threads) 2932 return (0); 2933 if (thresh) 2934 thresh = jblocks->jb_min; 2935 else 2936 thresh = jblocks->jb_low; 2937 avail = (ump->softdep_on_journal * JREC_SIZE) / DEV_BSIZE; 2938 avail = jblocks->jb_free - avail; 2939 2940 return (avail > thresh); 2941 } 2942 2943 static void 2944 journal_suspend(ump) 2945 struct ufsmount *ump; 2946 { 2947 struct jblocks *jblocks; 2948 struct mount *mp; 2949 2950 mp = UFSTOVFS(ump); 2951 jblocks = ump->softdep_jblocks; 2952 MNT_ILOCK(mp); 2953 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) { 2954 stat_journal_min++; 2955 mp->mnt_kern_flag |= MNTK_SUSPEND; 2956 mp->mnt_susp_owner = ump->softdep_flushtd; 2957 } 2958 jblocks->jb_suspended = 1; 2959 MNT_IUNLOCK(mp); 2960 } 2961 2962 static int 2963 journal_unsuspend(struct ufsmount *ump) 2964 { 2965 struct jblocks *jblocks; 2966 struct mount *mp; 2967 2968 mp = UFSTOVFS(ump); 2969 jblocks = ump->softdep_jblocks; 2970 2971 if (jblocks != NULL && jblocks->jb_suspended && 2972 journal_space(ump, jblocks->jb_min)) { 2973 jblocks->jb_suspended = 0; 2974 FREE_LOCK(ump); 2975 mp->mnt_susp_owner = curthread; 2976 vfs_write_resume(mp, 0); 2977 ACQUIRE_LOCK(ump); 2978 return (1); 2979 } 2980 return (0); 2981 } 2982 2983 /* 2984 * Called before any allocation function to be certain that there is 2985 * sufficient space in the journal prior to creating any new records. 2986 * Since in the case of block allocation we may have multiple locked 2987 * buffers at the time of the actual allocation we can not block 2988 * when the journal records are created. Doing so would create a deadlock 2989 * if any of these buffers needed to be flushed to reclaim space. Instead 2990 * we require a sufficiently large amount of available space such that 2991 * each thread in the system could have passed this allocation check and 2992 * still have sufficient free space. With 20% of a minimum journal size 2993 * of 1MB we have 6553 records available. 2994 */ 2995 int 2996 softdep_prealloc(vp, waitok) 2997 struct vnode *vp; 2998 int waitok; 2999 { 3000 struct ufsmount *ump; 3001 3002 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 3003 ("softdep_prealloc called on non-softdep filesystem")); 3004 /* 3005 * Nothing to do if we are not running journaled soft updates. 3006 * If we currently hold the snapshot lock, we must avoid 3007 * handling other resources that could cause deadlock. Do not 3008 * touch quotas vnode since it is typically recursed with 3009 * other vnode locks held. 3010 */ 3011 if (DOINGSUJ(vp) == 0 || IS_SNAPSHOT(VTOI(vp)) || 3012 (vp->v_vflag & VV_SYSTEM) != 0) 3013 return (0); 3014 ump = VFSTOUFS(vp->v_mount); 3015 ACQUIRE_LOCK(ump); 3016 if (journal_space(ump, 0)) { 3017 FREE_LOCK(ump); 3018 return (0); 3019 } 3020 stat_journal_low++; 3021 FREE_LOCK(ump); 3022 if (waitok == MNT_NOWAIT) 3023 return (ENOSPC); 3024 /* 3025 * Attempt to sync this vnode once to flush any journal 3026 * work attached to it. 3027 */ 3028 if ((curthread->td_pflags & TDP_COWINPROGRESS) == 0) 3029 ffs_syncvnode(vp, waitok, 0); 3030 ACQUIRE_LOCK(ump); 3031 process_removes(vp); 3032 process_truncates(vp); 3033 if (journal_space(ump, 0) == 0) { 3034 softdep_speedup(ump); 3035 if (journal_space(ump, 1) == 0) 3036 journal_suspend(ump); 3037 } 3038 FREE_LOCK(ump); 3039 3040 return (0); 3041 } 3042 3043 /* 3044 * Before adjusting a link count on a vnode verify that we have sufficient 3045 * journal space. If not, process operations that depend on the currently 3046 * locked pair of vnodes to try to flush space as the syncer, buf daemon, 3047 * and softdep flush threads can not acquire these locks to reclaim space. 3048 */ 3049 static void 3050 softdep_prelink(dvp, vp) 3051 struct vnode *dvp; 3052 struct vnode *vp; 3053 { 3054 struct ufsmount *ump; 3055 3056 ump = VFSTOUFS(dvp->v_mount); 3057 LOCK_OWNED(ump); 3058 /* 3059 * Nothing to do if we have sufficient journal space. 3060 * If we currently hold the snapshot lock, we must avoid 3061 * handling other resources that could cause deadlock. 3062 */ 3063 if (journal_space(ump, 0) || (vp && IS_SNAPSHOT(VTOI(vp)))) 3064 return; 3065 stat_journal_low++; 3066 FREE_LOCK(ump); 3067 if (vp) 3068 ffs_syncvnode(vp, MNT_NOWAIT, 0); 3069 ffs_syncvnode(dvp, MNT_WAIT, 0); 3070 ACQUIRE_LOCK(ump); 3071 /* Process vp before dvp as it may create .. removes. */ 3072 if (vp) { 3073 process_removes(vp); 3074 process_truncates(vp); 3075 } 3076 process_removes(dvp); 3077 process_truncates(dvp); 3078 softdep_speedup(ump); 3079 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT); 3080 if (journal_space(ump, 0) == 0) { 3081 softdep_speedup(ump); 3082 if (journal_space(ump, 1) == 0) 3083 journal_suspend(ump); 3084 } 3085 } 3086 3087 static void 3088 jseg_write(ump, jseg, data) 3089 struct ufsmount *ump; 3090 struct jseg *jseg; 3091 uint8_t *data; 3092 { 3093 struct jsegrec *rec; 3094 3095 rec = (struct jsegrec *)data; 3096 rec->jsr_seq = jseg->js_seq; 3097 rec->jsr_oldest = jseg->js_oldseq; 3098 rec->jsr_cnt = jseg->js_cnt; 3099 rec->jsr_blocks = jseg->js_size / ump->um_devvp->v_bufobj.bo_bsize; 3100 rec->jsr_crc = 0; 3101 rec->jsr_time = ump->um_fs->fs_mtime; 3102 } 3103 3104 static inline void 3105 inoref_write(inoref, jseg, rec) 3106 struct inoref *inoref; 3107 struct jseg *jseg; 3108 struct jrefrec *rec; 3109 { 3110 3111 inoref->if_jsegdep->jd_seg = jseg; 3112 rec->jr_ino = inoref->if_ino; 3113 rec->jr_parent = inoref->if_parent; 3114 rec->jr_nlink = inoref->if_nlink; 3115 rec->jr_mode = inoref->if_mode; 3116 rec->jr_diroff = inoref->if_diroff; 3117 } 3118 3119 static void 3120 jaddref_write(jaddref, jseg, data) 3121 struct jaddref *jaddref; 3122 struct jseg *jseg; 3123 uint8_t *data; 3124 { 3125 struct jrefrec *rec; 3126 3127 rec = (struct jrefrec *)data; 3128 rec->jr_op = JOP_ADDREF; 3129 inoref_write(&jaddref->ja_ref, jseg, rec); 3130 } 3131 3132 static void 3133 jremref_write(jremref, jseg, data) 3134 struct jremref *jremref; 3135 struct jseg *jseg; 3136 uint8_t *data; 3137 { 3138 struct jrefrec *rec; 3139 3140 rec = (struct jrefrec *)data; 3141 rec->jr_op = JOP_REMREF; 3142 inoref_write(&jremref->jr_ref, jseg, rec); 3143 } 3144 3145 static void 3146 jmvref_write(jmvref, jseg, data) 3147 struct jmvref *jmvref; 3148 struct jseg *jseg; 3149 uint8_t *data; 3150 { 3151 struct jmvrec *rec; 3152 3153 rec = (struct jmvrec *)data; 3154 rec->jm_op = JOP_MVREF; 3155 rec->jm_ino = jmvref->jm_ino; 3156 rec->jm_parent = jmvref->jm_parent; 3157 rec->jm_oldoff = jmvref->jm_oldoff; 3158 rec->jm_newoff = jmvref->jm_newoff; 3159 } 3160 3161 static void 3162 jnewblk_write(jnewblk, jseg, data) 3163 struct jnewblk *jnewblk; 3164 struct jseg *jseg; 3165 uint8_t *data; 3166 { 3167 struct jblkrec *rec; 3168 3169 jnewblk->jn_jsegdep->jd_seg = jseg; 3170 rec = (struct jblkrec *)data; 3171 rec->jb_op = JOP_NEWBLK; 3172 rec->jb_ino = jnewblk->jn_ino; 3173 rec->jb_blkno = jnewblk->jn_blkno; 3174 rec->jb_lbn = jnewblk->jn_lbn; 3175 rec->jb_frags = jnewblk->jn_frags; 3176 rec->jb_oldfrags = jnewblk->jn_oldfrags; 3177 } 3178 3179 static void 3180 jfreeblk_write(jfreeblk, jseg, data) 3181 struct jfreeblk *jfreeblk; 3182 struct jseg *jseg; 3183 uint8_t *data; 3184 { 3185 struct jblkrec *rec; 3186 3187 jfreeblk->jf_dep.jb_jsegdep->jd_seg = jseg; 3188 rec = (struct jblkrec *)data; 3189 rec->jb_op = JOP_FREEBLK; 3190 rec->jb_ino = jfreeblk->jf_ino; 3191 rec->jb_blkno = jfreeblk->jf_blkno; 3192 rec->jb_lbn = jfreeblk->jf_lbn; 3193 rec->jb_frags = jfreeblk->jf_frags; 3194 rec->jb_oldfrags = 0; 3195 } 3196 3197 static void 3198 jfreefrag_write(jfreefrag, jseg, data) 3199 struct jfreefrag *jfreefrag; 3200 struct jseg *jseg; 3201 uint8_t *data; 3202 { 3203 struct jblkrec *rec; 3204 3205 jfreefrag->fr_jsegdep->jd_seg = jseg; 3206 rec = (struct jblkrec *)data; 3207 rec->jb_op = JOP_FREEBLK; 3208 rec->jb_ino = jfreefrag->fr_ino; 3209 rec->jb_blkno = jfreefrag->fr_blkno; 3210 rec->jb_lbn = jfreefrag->fr_lbn; 3211 rec->jb_frags = jfreefrag->fr_frags; 3212 rec->jb_oldfrags = 0; 3213 } 3214 3215 static void 3216 jtrunc_write(jtrunc, jseg, data) 3217 struct jtrunc *jtrunc; 3218 struct jseg *jseg; 3219 uint8_t *data; 3220 { 3221 struct jtrncrec *rec; 3222 3223 jtrunc->jt_dep.jb_jsegdep->jd_seg = jseg; 3224 rec = (struct jtrncrec *)data; 3225 rec->jt_op = JOP_TRUNC; 3226 rec->jt_ino = jtrunc->jt_ino; 3227 rec->jt_size = jtrunc->jt_size; 3228 rec->jt_extsize = jtrunc->jt_extsize; 3229 } 3230 3231 static void 3232 jfsync_write(jfsync, jseg, data) 3233 struct jfsync *jfsync; 3234 struct jseg *jseg; 3235 uint8_t *data; 3236 { 3237 struct jtrncrec *rec; 3238 3239 rec = (struct jtrncrec *)data; 3240 rec->jt_op = JOP_SYNC; 3241 rec->jt_ino = jfsync->jfs_ino; 3242 rec->jt_size = jfsync->jfs_size; 3243 rec->jt_extsize = jfsync->jfs_extsize; 3244 } 3245 3246 static void 3247 softdep_flushjournal(mp) 3248 struct mount *mp; 3249 { 3250 struct jblocks *jblocks; 3251 struct ufsmount *ump; 3252 3253 if (MOUNTEDSUJ(mp) == 0) 3254 return; 3255 ump = VFSTOUFS(mp); 3256 jblocks = ump->softdep_jblocks; 3257 ACQUIRE_LOCK(ump); 3258 while (ump->softdep_on_journal) { 3259 jblocks->jb_needseg = 1; 3260 softdep_process_journal(mp, NULL, MNT_WAIT); 3261 } 3262 FREE_LOCK(ump); 3263 } 3264 3265 static void softdep_synchronize_completed(struct bio *); 3266 static void softdep_synchronize(struct bio *, struct ufsmount *, void *); 3267 3268 static void 3269 softdep_synchronize_completed(bp) 3270 struct bio *bp; 3271 { 3272 struct jseg *oldest; 3273 struct jseg *jseg; 3274 struct ufsmount *ump; 3275 3276 /* 3277 * caller1 marks the last segment written before we issued the 3278 * synchronize cache. 3279 */ 3280 jseg = bp->bio_caller1; 3281 if (jseg == NULL) { 3282 g_destroy_bio(bp); 3283 return; 3284 } 3285 ump = VFSTOUFS(jseg->js_list.wk_mp); 3286 ACQUIRE_LOCK(ump); 3287 oldest = NULL; 3288 /* 3289 * Mark all the journal entries waiting on the synchronize cache 3290 * as completed so they may continue on. 3291 */ 3292 while (jseg != NULL && (jseg->js_state & COMPLETE) == 0) { 3293 jseg->js_state |= COMPLETE; 3294 oldest = jseg; 3295 jseg = TAILQ_PREV(jseg, jseglst, js_next); 3296 } 3297 /* 3298 * Restart deferred journal entry processing from the oldest 3299 * completed jseg. 3300 */ 3301 if (oldest) 3302 complete_jsegs(oldest); 3303 3304 FREE_LOCK(ump); 3305 g_destroy_bio(bp); 3306 } 3307 3308 /* 3309 * Send BIO_FLUSH/SYNCHRONIZE CACHE to the device to enforce write ordering 3310 * barriers. The journal must be written prior to any blocks that depend 3311 * on it and the journal can not be released until the blocks have be 3312 * written. This code handles both barriers simultaneously. 3313 */ 3314 static void 3315 softdep_synchronize(bp, ump, caller1) 3316 struct bio *bp; 3317 struct ufsmount *ump; 3318 void *caller1; 3319 { 3320 3321 bp->bio_cmd = BIO_FLUSH; 3322 bp->bio_flags |= BIO_ORDERED; 3323 bp->bio_data = NULL; 3324 bp->bio_offset = ump->um_cp->provider->mediasize; 3325 bp->bio_length = 0; 3326 bp->bio_done = softdep_synchronize_completed; 3327 bp->bio_caller1 = caller1; 3328 g_io_request(bp, 3329 (struct g_consumer *)ump->um_devvp->v_bufobj.bo_private); 3330 } 3331 3332 /* 3333 * Flush some journal records to disk. 3334 */ 3335 static void 3336 softdep_process_journal(mp, needwk, flags) 3337 struct mount *mp; 3338 struct worklist *needwk; 3339 int flags; 3340 { 3341 struct jblocks *jblocks; 3342 struct ufsmount *ump; 3343 struct worklist *wk; 3344 struct jseg *jseg; 3345 struct buf *bp; 3346 struct bio *bio; 3347 uint8_t *data; 3348 struct fs *fs; 3349 int shouldflush; 3350 int segwritten; 3351 int jrecmin; /* Minimum records per block. */ 3352 int jrecmax; /* Maximum records per block. */ 3353 int size; 3354 int cnt; 3355 int off; 3356 int devbsize; 3357 3358 if (MOUNTEDSUJ(mp) == 0) 3359 return; 3360 shouldflush = softdep_flushcache; 3361 bio = NULL; 3362 jseg = NULL; 3363 ump = VFSTOUFS(mp); 3364 LOCK_OWNED(ump); 3365 fs = ump->um_fs; 3366 jblocks = ump->softdep_jblocks; 3367 devbsize = ump->um_devvp->v_bufobj.bo_bsize; 3368 /* 3369 * We write anywhere between a disk block and fs block. The upper 3370 * bound is picked to prevent buffer cache fragmentation and limit 3371 * processing time per I/O. 3372 */ 3373 jrecmin = (devbsize / JREC_SIZE) - 1; /* -1 for seg header */ 3374 jrecmax = (fs->fs_bsize / devbsize) * jrecmin; 3375 segwritten = 0; 3376 for (;;) { 3377 cnt = ump->softdep_on_journal; 3378 /* 3379 * Criteria for writing a segment: 3380 * 1) We have a full block. 3381 * 2) We're called from jwait() and haven't found the 3382 * journal item yet. 3383 * 3) Always write if needseg is set. 3384 * 4) If we are called from process_worklist and have 3385 * not yet written anything we write a partial block 3386 * to enforce a 1 second maximum latency on journal 3387 * entries. 3388 */ 3389 if (cnt < (jrecmax - 1) && needwk == NULL && 3390 jblocks->jb_needseg == 0 && (segwritten || cnt == 0)) 3391 break; 3392 cnt++; 3393 /* 3394 * Verify some free journal space. softdep_prealloc() should 3395 * guarantee that we don't run out so this is indicative of 3396 * a problem with the flow control. Try to recover 3397 * gracefully in any event. 3398 */ 3399 while (jblocks->jb_free == 0) { 3400 if (flags != MNT_WAIT) 3401 break; 3402 printf("softdep: Out of journal space!\n"); 3403 softdep_speedup(ump); 3404 msleep(jblocks, LOCK_PTR(ump), PRIBIO, "jblocks", hz); 3405 } 3406 FREE_LOCK(ump); 3407 jseg = malloc(sizeof(*jseg), M_JSEG, M_SOFTDEP_FLAGS); 3408 workitem_alloc(&jseg->js_list, D_JSEG, mp); 3409 LIST_INIT(&jseg->js_entries); 3410 LIST_INIT(&jseg->js_indirs); 3411 jseg->js_state = ATTACHED; 3412 if (shouldflush == 0) 3413 jseg->js_state |= COMPLETE; 3414 else if (bio == NULL) 3415 bio = g_alloc_bio(); 3416 jseg->js_jblocks = jblocks; 3417 bp = geteblk(fs->fs_bsize, 0); 3418 ACQUIRE_LOCK(ump); 3419 /* 3420 * If there was a race while we were allocating the block 3421 * and jseg the entry we care about was likely written. 3422 * We bail out in both the WAIT and NOWAIT case and assume 3423 * the caller will loop if the entry it cares about is 3424 * not written. 3425 */ 3426 cnt = ump->softdep_on_journal; 3427 if (cnt + jblocks->jb_needseg == 0 || jblocks->jb_free == 0) { 3428 bp->b_flags |= B_INVAL | B_NOCACHE; 3429 WORKITEM_FREE(jseg, D_JSEG); 3430 FREE_LOCK(ump); 3431 brelse(bp); 3432 ACQUIRE_LOCK(ump); 3433 break; 3434 } 3435 /* 3436 * Calculate the disk block size required for the available 3437 * records rounded to the min size. 3438 */ 3439 if (cnt == 0) 3440 size = devbsize; 3441 else if (cnt < jrecmax) 3442 size = howmany(cnt, jrecmin) * devbsize; 3443 else 3444 size = fs->fs_bsize; 3445 /* 3446 * Allocate a disk block for this journal data and account 3447 * for truncation of the requested size if enough contiguous 3448 * space was not available. 3449 */ 3450 bp->b_blkno = jblocks_alloc(jblocks, size, &size); 3451 bp->b_lblkno = bp->b_blkno; 3452 bp->b_offset = bp->b_blkno * DEV_BSIZE; 3453 bp->b_bcount = size; 3454 bp->b_flags &= ~B_INVAL; 3455 bp->b_flags |= B_VALIDSUSPWRT | B_NOCOPY; 3456 /* 3457 * Initialize our jseg with cnt records. Assign the next 3458 * sequence number to it and link it in-order. 3459 */ 3460 cnt = MIN(cnt, (size / devbsize) * jrecmin); 3461 jseg->js_buf = bp; 3462 jseg->js_cnt = cnt; 3463 jseg->js_refs = cnt + 1; /* Self ref. */ 3464 jseg->js_size = size; 3465 jseg->js_seq = jblocks->jb_nextseq++; 3466 if (jblocks->jb_oldestseg == NULL) 3467 jblocks->jb_oldestseg = jseg; 3468 jseg->js_oldseq = jblocks->jb_oldestseg->js_seq; 3469 TAILQ_INSERT_TAIL(&jblocks->jb_segs, jseg, js_next); 3470 if (jblocks->jb_writeseg == NULL) 3471 jblocks->jb_writeseg = jseg; 3472 /* 3473 * Start filling in records from the pending list. 3474 */ 3475 data = bp->b_data; 3476 off = 0; 3477 3478 /* 3479 * Always put a header on the first block. 3480 * XXX As with below, there might not be a chance to get 3481 * into the loop. Ensure that something valid is written. 3482 */ 3483 jseg_write(ump, jseg, data); 3484 off += JREC_SIZE; 3485 data = bp->b_data + off; 3486 3487 /* 3488 * XXX Something is wrong here. There's no work to do, 3489 * but we need to perform and I/O and allow it to complete 3490 * anyways. 3491 */ 3492 if (LIST_EMPTY(&ump->softdep_journal_pending)) 3493 stat_emptyjblocks++; 3494 3495 while ((wk = LIST_FIRST(&ump->softdep_journal_pending)) 3496 != NULL) { 3497 if (cnt == 0) 3498 break; 3499 /* Place a segment header on every device block. */ 3500 if ((off % devbsize) == 0) { 3501 jseg_write(ump, jseg, data); 3502 off += JREC_SIZE; 3503 data = bp->b_data + off; 3504 } 3505 if (wk == needwk) 3506 needwk = NULL; 3507 remove_from_journal(wk); 3508 wk->wk_state |= INPROGRESS; 3509 WORKLIST_INSERT(&jseg->js_entries, wk); 3510 switch (wk->wk_type) { 3511 case D_JADDREF: 3512 jaddref_write(WK_JADDREF(wk), jseg, data); 3513 break; 3514 case D_JREMREF: 3515 jremref_write(WK_JREMREF(wk), jseg, data); 3516 break; 3517 case D_JMVREF: 3518 jmvref_write(WK_JMVREF(wk), jseg, data); 3519 break; 3520 case D_JNEWBLK: 3521 jnewblk_write(WK_JNEWBLK(wk), jseg, data); 3522 break; 3523 case D_JFREEBLK: 3524 jfreeblk_write(WK_JFREEBLK(wk), jseg, data); 3525 break; 3526 case D_JFREEFRAG: 3527 jfreefrag_write(WK_JFREEFRAG(wk), jseg, data); 3528 break; 3529 case D_JTRUNC: 3530 jtrunc_write(WK_JTRUNC(wk), jseg, data); 3531 break; 3532 case D_JFSYNC: 3533 jfsync_write(WK_JFSYNC(wk), jseg, data); 3534 break; 3535 default: 3536 panic("process_journal: Unknown type %s", 3537 TYPENAME(wk->wk_type)); 3538 /* NOTREACHED */ 3539 } 3540 off += JREC_SIZE; 3541 data = bp->b_data + off; 3542 cnt--; 3543 } 3544 3545 /* Clear any remaining space so we don't leak kernel data */ 3546 if (size > off) 3547 bzero(data, size - off); 3548 3549 /* 3550 * Write this one buffer and continue. 3551 */ 3552 segwritten = 1; 3553 jblocks->jb_needseg = 0; 3554 WORKLIST_INSERT(&bp->b_dep, &jseg->js_list); 3555 FREE_LOCK(ump); 3556 pbgetvp(ump->um_devvp, bp); 3557 /* 3558 * We only do the blocking wait once we find the journal 3559 * entry we're looking for. 3560 */ 3561 if (needwk == NULL && flags == MNT_WAIT) 3562 bwrite(bp); 3563 else 3564 bawrite(bp); 3565 ACQUIRE_LOCK(ump); 3566 } 3567 /* 3568 * If we wrote a segment issue a synchronize cache so the journal 3569 * is reflected on disk before the data is written. Since reclaiming 3570 * journal space also requires writing a journal record this 3571 * process also enforces a barrier before reclamation. 3572 */ 3573 if (segwritten && shouldflush) { 3574 softdep_synchronize(bio, ump, 3575 TAILQ_LAST(&jblocks->jb_segs, jseglst)); 3576 } else if (bio) 3577 g_destroy_bio(bio); 3578 /* 3579 * If we've suspended the filesystem because we ran out of journal 3580 * space either try to sync it here to make some progress or 3581 * unsuspend it if we already have. 3582 */ 3583 if (flags == 0 && jblocks->jb_suspended) { 3584 if (journal_unsuspend(ump)) 3585 return; 3586 FREE_LOCK(ump); 3587 VFS_SYNC(mp, MNT_NOWAIT); 3588 ffs_sbupdate(ump, MNT_WAIT, 0); 3589 ACQUIRE_LOCK(ump); 3590 } 3591 } 3592 3593 /* 3594 * Complete a jseg, allowing all dependencies awaiting journal writes 3595 * to proceed. Each journal dependency also attaches a jsegdep to dependent 3596 * structures so that the journal segment can be freed to reclaim space. 3597 */ 3598 static void 3599 complete_jseg(jseg) 3600 struct jseg *jseg; 3601 { 3602 struct worklist *wk; 3603 struct jmvref *jmvref; 3604 #ifdef INVARIANTS 3605 int i = 0; 3606 #endif 3607 3608 while ((wk = LIST_FIRST(&jseg->js_entries)) != NULL) { 3609 WORKLIST_REMOVE(wk); 3610 wk->wk_state &= ~INPROGRESS; 3611 wk->wk_state |= COMPLETE; 3612 KASSERT(i++ < jseg->js_cnt, 3613 ("handle_written_jseg: overflow %d >= %d", 3614 i - 1, jseg->js_cnt)); 3615 switch (wk->wk_type) { 3616 case D_JADDREF: 3617 handle_written_jaddref(WK_JADDREF(wk)); 3618 break; 3619 case D_JREMREF: 3620 handle_written_jremref(WK_JREMREF(wk)); 3621 break; 3622 case D_JMVREF: 3623 rele_jseg(jseg); /* No jsegdep. */ 3624 jmvref = WK_JMVREF(wk); 3625 LIST_REMOVE(jmvref, jm_deps); 3626 if ((jmvref->jm_pagedep->pd_state & ONWORKLIST) == 0) 3627 free_pagedep(jmvref->jm_pagedep); 3628 WORKITEM_FREE(jmvref, D_JMVREF); 3629 break; 3630 case D_JNEWBLK: 3631 handle_written_jnewblk(WK_JNEWBLK(wk)); 3632 break; 3633 case D_JFREEBLK: 3634 handle_written_jblkdep(&WK_JFREEBLK(wk)->jf_dep); 3635 break; 3636 case D_JTRUNC: 3637 handle_written_jblkdep(&WK_JTRUNC(wk)->jt_dep); 3638 break; 3639 case D_JFSYNC: 3640 rele_jseg(jseg); /* No jsegdep. */ 3641 WORKITEM_FREE(wk, D_JFSYNC); 3642 break; 3643 case D_JFREEFRAG: 3644 handle_written_jfreefrag(WK_JFREEFRAG(wk)); 3645 break; 3646 default: 3647 panic("handle_written_jseg: Unknown type %s", 3648 TYPENAME(wk->wk_type)); 3649 /* NOTREACHED */ 3650 } 3651 } 3652 /* Release the self reference so the structure may be freed. */ 3653 rele_jseg(jseg); 3654 } 3655 3656 /* 3657 * Determine which jsegs are ready for completion processing. Waits for 3658 * synchronize cache to complete as well as forcing in-order completion 3659 * of journal entries. 3660 */ 3661 static void 3662 complete_jsegs(jseg) 3663 struct jseg *jseg; 3664 { 3665 struct jblocks *jblocks; 3666 struct jseg *jsegn; 3667 3668 jblocks = jseg->js_jblocks; 3669 /* 3670 * Don't allow out of order completions. If this isn't the first 3671 * block wait for it to write before we're done. 3672 */ 3673 if (jseg != jblocks->jb_writeseg) 3674 return; 3675 /* Iterate through available jsegs processing their entries. */ 3676 while (jseg && (jseg->js_state & ALLCOMPLETE) == ALLCOMPLETE) { 3677 jblocks->jb_oldestwrseq = jseg->js_oldseq; 3678 jsegn = TAILQ_NEXT(jseg, js_next); 3679 complete_jseg(jseg); 3680 jseg = jsegn; 3681 } 3682 jblocks->jb_writeseg = jseg; 3683 /* 3684 * Attempt to free jsegs now that oldestwrseq may have advanced. 3685 */ 3686 free_jsegs(jblocks); 3687 } 3688 3689 /* 3690 * Mark a jseg as DEPCOMPLETE and throw away the buffer. Attempt to handle 3691 * the final completions. 3692 */ 3693 static void 3694 handle_written_jseg(jseg, bp) 3695 struct jseg *jseg; 3696 struct buf *bp; 3697 { 3698 3699 if (jseg->js_refs == 0) 3700 panic("handle_written_jseg: No self-reference on %p", jseg); 3701 jseg->js_state |= DEPCOMPLETE; 3702 /* 3703 * We'll never need this buffer again, set flags so it will be 3704 * discarded. 3705 */ 3706 bp->b_flags |= B_INVAL | B_NOCACHE; 3707 pbrelvp(bp); 3708 complete_jsegs(jseg); 3709 } 3710 3711 static inline struct jsegdep * 3712 inoref_jseg(inoref) 3713 struct inoref *inoref; 3714 { 3715 struct jsegdep *jsegdep; 3716 3717 jsegdep = inoref->if_jsegdep; 3718 inoref->if_jsegdep = NULL; 3719 3720 return (jsegdep); 3721 } 3722 3723 /* 3724 * Called once a jremref has made it to stable store. The jremref is marked 3725 * complete and we attempt to free it. Any pagedeps writes sleeping waiting 3726 * for the jremref to complete will be awoken by free_jremref. 3727 */ 3728 static void 3729 handle_written_jremref(jremref) 3730 struct jremref *jremref; 3731 { 3732 struct inodedep *inodedep; 3733 struct jsegdep *jsegdep; 3734 struct dirrem *dirrem; 3735 3736 /* Grab the jsegdep. */ 3737 jsegdep = inoref_jseg(&jremref->jr_ref); 3738 /* 3739 * Remove us from the inoref list. 3740 */ 3741 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 3742 0, &inodedep) == 0) 3743 panic("handle_written_jremref: Lost inodedep"); 3744 TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 3745 /* 3746 * Complete the dirrem. 3747 */ 3748 dirrem = jremref->jr_dirrem; 3749 jremref->jr_dirrem = NULL; 3750 LIST_REMOVE(jremref, jr_deps); 3751 jsegdep->jd_state |= jremref->jr_state & MKDIR_PARENT; 3752 jwork_insert(&dirrem->dm_jwork, jsegdep); 3753 if (LIST_EMPTY(&dirrem->dm_jremrefhd) && 3754 (dirrem->dm_state & COMPLETE) != 0) 3755 add_to_worklist(&dirrem->dm_list, 0); 3756 free_jremref(jremref); 3757 } 3758 3759 /* 3760 * Called once a jaddref has made it to stable store. The dependency is 3761 * marked complete and any dependent structures are added to the inode 3762 * bufwait list to be completed as soon as it is written. If a bitmap write 3763 * depends on this entry we move the inode into the inodedephd of the 3764 * bmsafemap dependency and attempt to remove the jaddref from the bmsafemap. 3765 */ 3766 static void 3767 handle_written_jaddref(jaddref) 3768 struct jaddref *jaddref; 3769 { 3770 struct jsegdep *jsegdep; 3771 struct inodedep *inodedep; 3772 struct diradd *diradd; 3773 struct mkdir *mkdir; 3774 3775 /* Grab the jsegdep. */ 3776 jsegdep = inoref_jseg(&jaddref->ja_ref); 3777 mkdir = NULL; 3778 diradd = NULL; 3779 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 3780 0, &inodedep) == 0) 3781 panic("handle_written_jaddref: Lost inodedep."); 3782 if (jaddref->ja_diradd == NULL) 3783 panic("handle_written_jaddref: No dependency"); 3784 if (jaddref->ja_diradd->da_list.wk_type == D_DIRADD) { 3785 diradd = jaddref->ja_diradd; 3786 WORKLIST_INSERT(&inodedep->id_bufwait, &diradd->da_list); 3787 } else if (jaddref->ja_state & MKDIR_PARENT) { 3788 mkdir = jaddref->ja_mkdir; 3789 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir->md_list); 3790 } else if (jaddref->ja_state & MKDIR_BODY) 3791 mkdir = jaddref->ja_mkdir; 3792 else 3793 panic("handle_written_jaddref: Unknown dependency %p", 3794 jaddref->ja_diradd); 3795 jaddref->ja_diradd = NULL; /* also clears ja_mkdir */ 3796 /* 3797 * Remove us from the inode list. 3798 */ 3799 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps); 3800 /* 3801 * The mkdir may be waiting on the jaddref to clear before freeing. 3802 */ 3803 if (mkdir) { 3804 KASSERT(mkdir->md_list.wk_type == D_MKDIR, 3805 ("handle_written_jaddref: Incorrect type for mkdir %s", 3806 TYPENAME(mkdir->md_list.wk_type))); 3807 mkdir->md_jaddref = NULL; 3808 diradd = mkdir->md_diradd; 3809 mkdir->md_state |= DEPCOMPLETE; 3810 complete_mkdir(mkdir); 3811 } 3812 jwork_insert(&diradd->da_jwork, jsegdep); 3813 if (jaddref->ja_state & NEWBLOCK) { 3814 inodedep->id_state |= ONDEPLIST; 3815 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_inodedephd, 3816 inodedep, id_deps); 3817 } 3818 free_jaddref(jaddref); 3819 } 3820 3821 /* 3822 * Called once a jnewblk journal is written. The allocdirect or allocindir 3823 * is placed in the bmsafemap to await notification of a written bitmap. If 3824 * the operation was canceled we add the segdep to the appropriate 3825 * dependency to free the journal space once the canceling operation 3826 * completes. 3827 */ 3828 static void 3829 handle_written_jnewblk(jnewblk) 3830 struct jnewblk *jnewblk; 3831 { 3832 struct bmsafemap *bmsafemap; 3833 struct freefrag *freefrag; 3834 struct freework *freework; 3835 struct jsegdep *jsegdep; 3836 struct newblk *newblk; 3837 3838 /* Grab the jsegdep. */ 3839 jsegdep = jnewblk->jn_jsegdep; 3840 jnewblk->jn_jsegdep = NULL; 3841 if (jnewblk->jn_dep == NULL) 3842 panic("handle_written_jnewblk: No dependency for the segdep."); 3843 switch (jnewblk->jn_dep->wk_type) { 3844 case D_NEWBLK: 3845 case D_ALLOCDIRECT: 3846 case D_ALLOCINDIR: 3847 /* 3848 * Add the written block to the bmsafemap so it can 3849 * be notified when the bitmap is on disk. 3850 */ 3851 newblk = WK_NEWBLK(jnewblk->jn_dep); 3852 newblk->nb_jnewblk = NULL; 3853 if ((newblk->nb_state & GOINGAWAY) == 0) { 3854 bmsafemap = newblk->nb_bmsafemap; 3855 newblk->nb_state |= ONDEPLIST; 3856 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, 3857 nb_deps); 3858 } 3859 jwork_insert(&newblk->nb_jwork, jsegdep); 3860 break; 3861 case D_FREEFRAG: 3862 /* 3863 * A newblock being removed by a freefrag when replaced by 3864 * frag extension. 3865 */ 3866 freefrag = WK_FREEFRAG(jnewblk->jn_dep); 3867 freefrag->ff_jdep = NULL; 3868 jwork_insert(&freefrag->ff_jwork, jsegdep); 3869 break; 3870 case D_FREEWORK: 3871 /* 3872 * A direct block was removed by truncate. 3873 */ 3874 freework = WK_FREEWORK(jnewblk->jn_dep); 3875 freework->fw_jnewblk = NULL; 3876 jwork_insert(&freework->fw_freeblks->fb_jwork, jsegdep); 3877 break; 3878 default: 3879 panic("handle_written_jnewblk: Unknown type %d.", 3880 jnewblk->jn_dep->wk_type); 3881 } 3882 jnewblk->jn_dep = NULL; 3883 free_jnewblk(jnewblk); 3884 } 3885 3886 /* 3887 * Cancel a jfreefrag that won't be needed, probably due to colliding with 3888 * an in-flight allocation that has not yet been committed. Divorce us 3889 * from the freefrag and mark it DEPCOMPLETE so that it may be added 3890 * to the worklist. 3891 */ 3892 static void 3893 cancel_jfreefrag(jfreefrag) 3894 struct jfreefrag *jfreefrag; 3895 { 3896 struct freefrag *freefrag; 3897 3898 if (jfreefrag->fr_jsegdep) { 3899 free_jsegdep(jfreefrag->fr_jsegdep); 3900 jfreefrag->fr_jsegdep = NULL; 3901 } 3902 freefrag = jfreefrag->fr_freefrag; 3903 jfreefrag->fr_freefrag = NULL; 3904 free_jfreefrag(jfreefrag); 3905 freefrag->ff_state |= DEPCOMPLETE; 3906 CTR1(KTR_SUJ, "cancel_jfreefrag: blkno %jd", freefrag->ff_blkno); 3907 } 3908 3909 /* 3910 * Free a jfreefrag when the parent freefrag is rendered obsolete. 3911 */ 3912 static void 3913 free_jfreefrag(jfreefrag) 3914 struct jfreefrag *jfreefrag; 3915 { 3916 3917 if (jfreefrag->fr_state & INPROGRESS) 3918 WORKLIST_REMOVE(&jfreefrag->fr_list); 3919 else if (jfreefrag->fr_state & ONWORKLIST) 3920 remove_from_journal(&jfreefrag->fr_list); 3921 if (jfreefrag->fr_freefrag != NULL) 3922 panic("free_jfreefrag: Still attached to a freefrag."); 3923 WORKITEM_FREE(jfreefrag, D_JFREEFRAG); 3924 } 3925 3926 /* 3927 * Called when the journal write for a jfreefrag completes. The parent 3928 * freefrag is added to the worklist if this completes its dependencies. 3929 */ 3930 static void 3931 handle_written_jfreefrag(jfreefrag) 3932 struct jfreefrag *jfreefrag; 3933 { 3934 struct jsegdep *jsegdep; 3935 struct freefrag *freefrag; 3936 3937 /* Grab the jsegdep. */ 3938 jsegdep = jfreefrag->fr_jsegdep; 3939 jfreefrag->fr_jsegdep = NULL; 3940 freefrag = jfreefrag->fr_freefrag; 3941 if (freefrag == NULL) 3942 panic("handle_written_jfreefrag: No freefrag."); 3943 freefrag->ff_state |= DEPCOMPLETE; 3944 freefrag->ff_jdep = NULL; 3945 jwork_insert(&freefrag->ff_jwork, jsegdep); 3946 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 3947 add_to_worklist(&freefrag->ff_list, 0); 3948 jfreefrag->fr_freefrag = NULL; 3949 free_jfreefrag(jfreefrag); 3950 } 3951 3952 /* 3953 * Called when the journal write for a jfreeblk completes. The jfreeblk 3954 * is removed from the freeblks list of pending journal writes and the 3955 * jsegdep is moved to the freeblks jwork to be completed when all blocks 3956 * have been reclaimed. 3957 */ 3958 static void 3959 handle_written_jblkdep(jblkdep) 3960 struct jblkdep *jblkdep; 3961 { 3962 struct freeblks *freeblks; 3963 struct jsegdep *jsegdep; 3964 3965 /* Grab the jsegdep. */ 3966 jsegdep = jblkdep->jb_jsegdep; 3967 jblkdep->jb_jsegdep = NULL; 3968 freeblks = jblkdep->jb_freeblks; 3969 LIST_REMOVE(jblkdep, jb_deps); 3970 jwork_insert(&freeblks->fb_jwork, jsegdep); 3971 /* 3972 * If the freeblks is all journaled, we can add it to the worklist. 3973 */ 3974 if (LIST_EMPTY(&freeblks->fb_jblkdephd) && 3975 (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 3976 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 3977 3978 free_jblkdep(jblkdep); 3979 } 3980 3981 static struct jsegdep * 3982 newjsegdep(struct worklist *wk) 3983 { 3984 struct jsegdep *jsegdep; 3985 3986 jsegdep = malloc(sizeof(*jsegdep), M_JSEGDEP, M_SOFTDEP_FLAGS); 3987 workitem_alloc(&jsegdep->jd_list, D_JSEGDEP, wk->wk_mp); 3988 jsegdep->jd_seg = NULL; 3989 3990 return (jsegdep); 3991 } 3992 3993 static struct jmvref * 3994 newjmvref(dp, ino, oldoff, newoff) 3995 struct inode *dp; 3996 ino_t ino; 3997 off_t oldoff; 3998 off_t newoff; 3999 { 4000 struct jmvref *jmvref; 4001 4002 jmvref = malloc(sizeof(*jmvref), M_JMVREF, M_SOFTDEP_FLAGS); 4003 workitem_alloc(&jmvref->jm_list, D_JMVREF, ITOVFS(dp)); 4004 jmvref->jm_list.wk_state = ATTACHED | DEPCOMPLETE; 4005 jmvref->jm_parent = dp->i_number; 4006 jmvref->jm_ino = ino; 4007 jmvref->jm_oldoff = oldoff; 4008 jmvref->jm_newoff = newoff; 4009 4010 return (jmvref); 4011 } 4012 4013 /* 4014 * Allocate a new jremref that tracks the removal of ip from dp with the 4015 * directory entry offset of diroff. Mark the entry as ATTACHED and 4016 * DEPCOMPLETE as we have all the information required for the journal write 4017 * and the directory has already been removed from the buffer. The caller 4018 * is responsible for linking the jremref into the pagedep and adding it 4019 * to the journal to write. The MKDIR_PARENT flag is set if we're doing 4020 * a DOTDOT addition so handle_workitem_remove() can properly assign 4021 * the jsegdep when we're done. 4022 */ 4023 static struct jremref * 4024 newjremref(struct dirrem *dirrem, struct inode *dp, struct inode *ip, 4025 off_t diroff, nlink_t nlink) 4026 { 4027 struct jremref *jremref; 4028 4029 jremref = malloc(sizeof(*jremref), M_JREMREF, M_SOFTDEP_FLAGS); 4030 workitem_alloc(&jremref->jr_list, D_JREMREF, ITOVFS(dp)); 4031 jremref->jr_state = ATTACHED; 4032 newinoref(&jremref->jr_ref, ip->i_number, dp->i_number, diroff, 4033 nlink, ip->i_mode); 4034 jremref->jr_dirrem = dirrem; 4035 4036 return (jremref); 4037 } 4038 4039 static inline void 4040 newinoref(struct inoref *inoref, ino_t ino, ino_t parent, off_t diroff, 4041 nlink_t nlink, uint16_t mode) 4042 { 4043 4044 inoref->if_jsegdep = newjsegdep(&inoref->if_list); 4045 inoref->if_diroff = diroff; 4046 inoref->if_ino = ino; 4047 inoref->if_parent = parent; 4048 inoref->if_nlink = nlink; 4049 inoref->if_mode = mode; 4050 } 4051 4052 /* 4053 * Allocate a new jaddref to track the addition of ino to dp at diroff. The 4054 * directory offset may not be known until later. The caller is responsible 4055 * adding the entry to the journal when this information is available. nlink 4056 * should be the link count prior to the addition and mode is only required 4057 * to have the correct FMT. 4058 */ 4059 static struct jaddref * 4060 newjaddref(struct inode *dp, ino_t ino, off_t diroff, int16_t nlink, 4061 uint16_t mode) 4062 { 4063 struct jaddref *jaddref; 4064 4065 jaddref = malloc(sizeof(*jaddref), M_JADDREF, M_SOFTDEP_FLAGS); 4066 workitem_alloc(&jaddref->ja_list, D_JADDREF, ITOVFS(dp)); 4067 jaddref->ja_state = ATTACHED; 4068 jaddref->ja_mkdir = NULL; 4069 newinoref(&jaddref->ja_ref, ino, dp->i_number, diroff, nlink, mode); 4070 4071 return (jaddref); 4072 } 4073 4074 /* 4075 * Create a new free dependency for a freework. The caller is responsible 4076 * for adjusting the reference count when it has the lock held. The freedep 4077 * will track an outstanding bitmap write that will ultimately clear the 4078 * freework to continue. 4079 */ 4080 static struct freedep * 4081 newfreedep(struct freework *freework) 4082 { 4083 struct freedep *freedep; 4084 4085 freedep = malloc(sizeof(*freedep), M_FREEDEP, M_SOFTDEP_FLAGS); 4086 workitem_alloc(&freedep->fd_list, D_FREEDEP, freework->fw_list.wk_mp); 4087 freedep->fd_freework = freework; 4088 4089 return (freedep); 4090 } 4091 4092 /* 4093 * Free a freedep structure once the buffer it is linked to is written. If 4094 * this is the last reference to the freework schedule it for completion. 4095 */ 4096 static void 4097 free_freedep(freedep) 4098 struct freedep *freedep; 4099 { 4100 struct freework *freework; 4101 4102 freework = freedep->fd_freework; 4103 freework->fw_freeblks->fb_cgwait--; 4104 if (--freework->fw_ref == 0) 4105 freework_enqueue(freework); 4106 WORKITEM_FREE(freedep, D_FREEDEP); 4107 } 4108 4109 /* 4110 * Allocate a new freework structure that may be a level in an indirect 4111 * when parent is not NULL or a top level block when it is. The top level 4112 * freework structures are allocated without the per-filesystem lock held 4113 * and before the freeblks is visible outside of softdep_setup_freeblocks(). 4114 */ 4115 static struct freework * 4116 newfreework(ump, freeblks, parent, lbn, nb, frags, off, journal) 4117 struct ufsmount *ump; 4118 struct freeblks *freeblks; 4119 struct freework *parent; 4120 ufs_lbn_t lbn; 4121 ufs2_daddr_t nb; 4122 int frags; 4123 int off; 4124 int journal; 4125 { 4126 struct freework *freework; 4127 4128 freework = malloc(sizeof(*freework), M_FREEWORK, M_SOFTDEP_FLAGS); 4129 workitem_alloc(&freework->fw_list, D_FREEWORK, freeblks->fb_list.wk_mp); 4130 freework->fw_state = ATTACHED; 4131 freework->fw_jnewblk = NULL; 4132 freework->fw_freeblks = freeblks; 4133 freework->fw_parent = parent; 4134 freework->fw_lbn = lbn; 4135 freework->fw_blkno = nb; 4136 freework->fw_frags = frags; 4137 freework->fw_indir = NULL; 4138 freework->fw_ref = (MOUNTEDSUJ(UFSTOVFS(ump)) == 0 || 4139 lbn >= -UFS_NXADDR) ? 0 : NINDIR(ump->um_fs) + 1; 4140 freework->fw_start = freework->fw_off = off; 4141 if (journal) 4142 newjfreeblk(freeblks, lbn, nb, frags); 4143 if (parent == NULL) { 4144 ACQUIRE_LOCK(ump); 4145 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 4146 freeblks->fb_ref++; 4147 FREE_LOCK(ump); 4148 } 4149 4150 return (freework); 4151 } 4152 4153 /* 4154 * Eliminate a jfreeblk for a block that does not need journaling. 4155 */ 4156 static void 4157 cancel_jfreeblk(freeblks, blkno) 4158 struct freeblks *freeblks; 4159 ufs2_daddr_t blkno; 4160 { 4161 struct jfreeblk *jfreeblk; 4162 struct jblkdep *jblkdep; 4163 4164 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) { 4165 if (jblkdep->jb_list.wk_type != D_JFREEBLK) 4166 continue; 4167 jfreeblk = WK_JFREEBLK(&jblkdep->jb_list); 4168 if (jfreeblk->jf_blkno == blkno) 4169 break; 4170 } 4171 if (jblkdep == NULL) 4172 return; 4173 CTR1(KTR_SUJ, "cancel_jfreeblk: blkno %jd", blkno); 4174 free_jsegdep(jblkdep->jb_jsegdep); 4175 LIST_REMOVE(jblkdep, jb_deps); 4176 WORKITEM_FREE(jfreeblk, D_JFREEBLK); 4177 } 4178 4179 /* 4180 * Allocate a new jfreeblk to journal top level block pointer when truncating 4181 * a file. The caller must add this to the worklist when the per-filesystem 4182 * lock is held. 4183 */ 4184 static struct jfreeblk * 4185 newjfreeblk(freeblks, lbn, blkno, frags) 4186 struct freeblks *freeblks; 4187 ufs_lbn_t lbn; 4188 ufs2_daddr_t blkno; 4189 int frags; 4190 { 4191 struct jfreeblk *jfreeblk; 4192 4193 jfreeblk = malloc(sizeof(*jfreeblk), M_JFREEBLK, M_SOFTDEP_FLAGS); 4194 workitem_alloc(&jfreeblk->jf_dep.jb_list, D_JFREEBLK, 4195 freeblks->fb_list.wk_mp); 4196 jfreeblk->jf_dep.jb_jsegdep = newjsegdep(&jfreeblk->jf_dep.jb_list); 4197 jfreeblk->jf_dep.jb_freeblks = freeblks; 4198 jfreeblk->jf_ino = freeblks->fb_inum; 4199 jfreeblk->jf_lbn = lbn; 4200 jfreeblk->jf_blkno = blkno; 4201 jfreeblk->jf_frags = frags; 4202 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jfreeblk->jf_dep, jb_deps); 4203 4204 return (jfreeblk); 4205 } 4206 4207 /* 4208 * The journal is only prepared to handle full-size block numbers, so we 4209 * have to adjust the record to reflect the change to a full-size block. 4210 * For example, suppose we have a block made up of fragments 8-15 and 4211 * want to free its last two fragments. We are given a request that says: 4212 * FREEBLK ino=5, blkno=14, lbn=0, frags=2, oldfrags=0 4213 * where frags are the number of fragments to free and oldfrags are the 4214 * number of fragments to keep. To block align it, we have to change it to 4215 * have a valid full-size blkno, so it becomes: 4216 * FREEBLK ino=5, blkno=8, lbn=0, frags=2, oldfrags=6 4217 */ 4218 static void 4219 adjust_newfreework(freeblks, frag_offset) 4220 struct freeblks *freeblks; 4221 int frag_offset; 4222 { 4223 struct jfreeblk *jfreeblk; 4224 4225 KASSERT((LIST_FIRST(&freeblks->fb_jblkdephd) != NULL && 4226 LIST_FIRST(&freeblks->fb_jblkdephd)->jb_list.wk_type == D_JFREEBLK), 4227 ("adjust_newfreework: Missing freeblks dependency")); 4228 4229 jfreeblk = WK_JFREEBLK(LIST_FIRST(&freeblks->fb_jblkdephd)); 4230 jfreeblk->jf_blkno -= frag_offset; 4231 jfreeblk->jf_frags += frag_offset; 4232 } 4233 4234 /* 4235 * Allocate a new jtrunc to track a partial truncation. 4236 */ 4237 static struct jtrunc * 4238 newjtrunc(freeblks, size, extsize) 4239 struct freeblks *freeblks; 4240 off_t size; 4241 int extsize; 4242 { 4243 struct jtrunc *jtrunc; 4244 4245 jtrunc = malloc(sizeof(*jtrunc), M_JTRUNC, M_SOFTDEP_FLAGS); 4246 workitem_alloc(&jtrunc->jt_dep.jb_list, D_JTRUNC, 4247 freeblks->fb_list.wk_mp); 4248 jtrunc->jt_dep.jb_jsegdep = newjsegdep(&jtrunc->jt_dep.jb_list); 4249 jtrunc->jt_dep.jb_freeblks = freeblks; 4250 jtrunc->jt_ino = freeblks->fb_inum; 4251 jtrunc->jt_size = size; 4252 jtrunc->jt_extsize = extsize; 4253 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jtrunc->jt_dep, jb_deps); 4254 4255 return (jtrunc); 4256 } 4257 4258 /* 4259 * If we're canceling a new bitmap we have to search for another ref 4260 * to move into the bmsafemap dep. This might be better expressed 4261 * with another structure. 4262 */ 4263 static void 4264 move_newblock_dep(jaddref, inodedep) 4265 struct jaddref *jaddref; 4266 struct inodedep *inodedep; 4267 { 4268 struct inoref *inoref; 4269 struct jaddref *jaddrefn; 4270 4271 jaddrefn = NULL; 4272 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4273 inoref = TAILQ_NEXT(inoref, if_deps)) { 4274 if ((jaddref->ja_state & NEWBLOCK) && 4275 inoref->if_list.wk_type == D_JADDREF) { 4276 jaddrefn = (struct jaddref *)inoref; 4277 break; 4278 } 4279 } 4280 if (jaddrefn == NULL) 4281 return; 4282 jaddrefn->ja_state &= ~(ATTACHED | UNDONE); 4283 jaddrefn->ja_state |= jaddref->ja_state & 4284 (ATTACHED | UNDONE | NEWBLOCK); 4285 jaddref->ja_state &= ~(ATTACHED | UNDONE | NEWBLOCK); 4286 jaddref->ja_state |= ATTACHED; 4287 LIST_REMOVE(jaddref, ja_bmdeps); 4288 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_jaddrefhd, jaddrefn, 4289 ja_bmdeps); 4290 } 4291 4292 /* 4293 * Cancel a jaddref either before it has been written or while it is being 4294 * written. This happens when a link is removed before the add reaches 4295 * the disk. The jaddref dependency is kept linked into the bmsafemap 4296 * and inode to prevent the link count or bitmap from reaching the disk 4297 * until handle_workitem_remove() re-adjusts the counts and bitmaps as 4298 * required. 4299 * 4300 * Returns 1 if the canceled addref requires journaling of the remove and 4301 * 0 otherwise. 4302 */ 4303 static int 4304 cancel_jaddref(jaddref, inodedep, wkhd) 4305 struct jaddref *jaddref; 4306 struct inodedep *inodedep; 4307 struct workhead *wkhd; 4308 { 4309 struct inoref *inoref; 4310 struct jsegdep *jsegdep; 4311 int needsj; 4312 4313 KASSERT((jaddref->ja_state & COMPLETE) == 0, 4314 ("cancel_jaddref: Canceling complete jaddref")); 4315 if (jaddref->ja_state & (INPROGRESS | COMPLETE)) 4316 needsj = 1; 4317 else 4318 needsj = 0; 4319 if (inodedep == NULL) 4320 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 4321 0, &inodedep) == 0) 4322 panic("cancel_jaddref: Lost inodedep"); 4323 /* 4324 * We must adjust the nlink of any reference operation that follows 4325 * us so that it is consistent with the in-memory reference. This 4326 * ensures that inode nlink rollbacks always have the correct link. 4327 */ 4328 if (needsj == 0) { 4329 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4330 inoref = TAILQ_NEXT(inoref, if_deps)) { 4331 if (inoref->if_state & GOINGAWAY) 4332 break; 4333 inoref->if_nlink--; 4334 } 4335 } 4336 jsegdep = inoref_jseg(&jaddref->ja_ref); 4337 if (jaddref->ja_state & NEWBLOCK) 4338 move_newblock_dep(jaddref, inodedep); 4339 wake_worklist(&jaddref->ja_list); 4340 jaddref->ja_mkdir = NULL; 4341 if (jaddref->ja_state & INPROGRESS) { 4342 jaddref->ja_state &= ~INPROGRESS; 4343 WORKLIST_REMOVE(&jaddref->ja_list); 4344 jwork_insert(wkhd, jsegdep); 4345 } else { 4346 free_jsegdep(jsegdep); 4347 if (jaddref->ja_state & DEPCOMPLETE) 4348 remove_from_journal(&jaddref->ja_list); 4349 } 4350 jaddref->ja_state |= (GOINGAWAY | DEPCOMPLETE); 4351 /* 4352 * Leave NEWBLOCK jaddrefs on the inodedep so handle_workitem_remove 4353 * can arrange for them to be freed with the bitmap. Otherwise we 4354 * no longer need this addref attached to the inoreflst and it 4355 * will incorrectly adjust nlink if we leave it. 4356 */ 4357 if ((jaddref->ja_state & NEWBLOCK) == 0) { 4358 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 4359 if_deps); 4360 jaddref->ja_state |= COMPLETE; 4361 free_jaddref(jaddref); 4362 return (needsj); 4363 } 4364 /* 4365 * Leave the head of the list for jsegdeps for fast merging. 4366 */ 4367 if (LIST_FIRST(wkhd) != NULL) { 4368 jaddref->ja_state |= ONWORKLIST; 4369 LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list); 4370 } else 4371 WORKLIST_INSERT(wkhd, &jaddref->ja_list); 4372 4373 return (needsj); 4374 } 4375 4376 /* 4377 * Attempt to free a jaddref structure when some work completes. This 4378 * should only succeed once the entry is written and all dependencies have 4379 * been notified. 4380 */ 4381 static void 4382 free_jaddref(jaddref) 4383 struct jaddref *jaddref; 4384 { 4385 4386 if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE) 4387 return; 4388 if (jaddref->ja_ref.if_jsegdep) 4389 panic("free_jaddref: segdep attached to jaddref %p(0x%X)\n", 4390 jaddref, jaddref->ja_state); 4391 if (jaddref->ja_state & NEWBLOCK) 4392 LIST_REMOVE(jaddref, ja_bmdeps); 4393 if (jaddref->ja_state & (INPROGRESS | ONWORKLIST)) 4394 panic("free_jaddref: Bad state %p(0x%X)", 4395 jaddref, jaddref->ja_state); 4396 if (jaddref->ja_mkdir != NULL) 4397 panic("free_jaddref: Work pending, 0x%X\n", jaddref->ja_state); 4398 WORKITEM_FREE(jaddref, D_JADDREF); 4399 } 4400 4401 /* 4402 * Free a jremref structure once it has been written or discarded. 4403 */ 4404 static void 4405 free_jremref(jremref) 4406 struct jremref *jremref; 4407 { 4408 4409 if (jremref->jr_ref.if_jsegdep) 4410 free_jsegdep(jremref->jr_ref.if_jsegdep); 4411 if (jremref->jr_state & INPROGRESS) 4412 panic("free_jremref: IO still pending"); 4413 WORKITEM_FREE(jremref, D_JREMREF); 4414 } 4415 4416 /* 4417 * Free a jnewblk structure. 4418 */ 4419 static void 4420 free_jnewblk(jnewblk) 4421 struct jnewblk *jnewblk; 4422 { 4423 4424 if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE) 4425 return; 4426 LIST_REMOVE(jnewblk, jn_deps); 4427 if (jnewblk->jn_dep != NULL) 4428 panic("free_jnewblk: Dependency still attached."); 4429 WORKITEM_FREE(jnewblk, D_JNEWBLK); 4430 } 4431 4432 /* 4433 * Cancel a jnewblk which has been been made redundant by frag extension. 4434 */ 4435 static void 4436 cancel_jnewblk(jnewblk, wkhd) 4437 struct jnewblk *jnewblk; 4438 struct workhead *wkhd; 4439 { 4440 struct jsegdep *jsegdep; 4441 4442 CTR1(KTR_SUJ, "cancel_jnewblk: blkno %jd", jnewblk->jn_blkno); 4443 jsegdep = jnewblk->jn_jsegdep; 4444 if (jnewblk->jn_jsegdep == NULL || jnewblk->jn_dep == NULL) 4445 panic("cancel_jnewblk: Invalid state"); 4446 jnewblk->jn_jsegdep = NULL; 4447 jnewblk->jn_dep = NULL; 4448 jnewblk->jn_state |= GOINGAWAY; 4449 if (jnewblk->jn_state & INPROGRESS) { 4450 jnewblk->jn_state &= ~INPROGRESS; 4451 WORKLIST_REMOVE(&jnewblk->jn_list); 4452 jwork_insert(wkhd, jsegdep); 4453 } else { 4454 free_jsegdep(jsegdep); 4455 remove_from_journal(&jnewblk->jn_list); 4456 } 4457 wake_worklist(&jnewblk->jn_list); 4458 WORKLIST_INSERT(wkhd, &jnewblk->jn_list); 4459 } 4460 4461 static void 4462 free_jblkdep(jblkdep) 4463 struct jblkdep *jblkdep; 4464 { 4465 4466 if (jblkdep->jb_list.wk_type == D_JFREEBLK) 4467 WORKITEM_FREE(jblkdep, D_JFREEBLK); 4468 else if (jblkdep->jb_list.wk_type == D_JTRUNC) 4469 WORKITEM_FREE(jblkdep, D_JTRUNC); 4470 else 4471 panic("free_jblkdep: Unexpected type %s", 4472 TYPENAME(jblkdep->jb_list.wk_type)); 4473 } 4474 4475 /* 4476 * Free a single jseg once it is no longer referenced in memory or on 4477 * disk. Reclaim journal blocks and dependencies waiting for the segment 4478 * to disappear. 4479 */ 4480 static void 4481 free_jseg(jseg, jblocks) 4482 struct jseg *jseg; 4483 struct jblocks *jblocks; 4484 { 4485 struct freework *freework; 4486 4487 /* 4488 * Free freework structures that were lingering to indicate freed 4489 * indirect blocks that forced journal write ordering on reallocate. 4490 */ 4491 while ((freework = LIST_FIRST(&jseg->js_indirs)) != NULL) 4492 indirblk_remove(freework); 4493 if (jblocks->jb_oldestseg == jseg) 4494 jblocks->jb_oldestseg = TAILQ_NEXT(jseg, js_next); 4495 TAILQ_REMOVE(&jblocks->jb_segs, jseg, js_next); 4496 jblocks_free(jblocks, jseg->js_list.wk_mp, jseg->js_size); 4497 KASSERT(LIST_EMPTY(&jseg->js_entries), 4498 ("free_jseg: Freed jseg has valid entries.")); 4499 WORKITEM_FREE(jseg, D_JSEG); 4500 } 4501 4502 /* 4503 * Free all jsegs that meet the criteria for being reclaimed and update 4504 * oldestseg. 4505 */ 4506 static void 4507 free_jsegs(jblocks) 4508 struct jblocks *jblocks; 4509 { 4510 struct jseg *jseg; 4511 4512 /* 4513 * Free only those jsegs which have none allocated before them to 4514 * preserve the journal space ordering. 4515 */ 4516 while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) { 4517 /* 4518 * Only reclaim space when nothing depends on this journal 4519 * set and another set has written that it is no longer 4520 * valid. 4521 */ 4522 if (jseg->js_refs != 0) { 4523 jblocks->jb_oldestseg = jseg; 4524 return; 4525 } 4526 if ((jseg->js_state & ALLCOMPLETE) != ALLCOMPLETE) 4527 break; 4528 if (jseg->js_seq > jblocks->jb_oldestwrseq) 4529 break; 4530 /* 4531 * We can free jsegs that didn't write entries when 4532 * oldestwrseq == js_seq. 4533 */ 4534 if (jseg->js_seq == jblocks->jb_oldestwrseq && 4535 jseg->js_cnt != 0) 4536 break; 4537 free_jseg(jseg, jblocks); 4538 } 4539 /* 4540 * If we exited the loop above we still must discover the 4541 * oldest valid segment. 4542 */ 4543 if (jseg) 4544 for (jseg = jblocks->jb_oldestseg; jseg != NULL; 4545 jseg = TAILQ_NEXT(jseg, js_next)) 4546 if (jseg->js_refs != 0) 4547 break; 4548 jblocks->jb_oldestseg = jseg; 4549 /* 4550 * The journal has no valid records but some jsegs may still be 4551 * waiting on oldestwrseq to advance. We force a small record 4552 * out to permit these lingering records to be reclaimed. 4553 */ 4554 if (jblocks->jb_oldestseg == NULL && !TAILQ_EMPTY(&jblocks->jb_segs)) 4555 jblocks->jb_needseg = 1; 4556 } 4557 4558 /* 4559 * Release one reference to a jseg and free it if the count reaches 0. This 4560 * should eventually reclaim journal space as well. 4561 */ 4562 static void 4563 rele_jseg(jseg) 4564 struct jseg *jseg; 4565 { 4566 4567 KASSERT(jseg->js_refs > 0, 4568 ("free_jseg: Invalid refcnt %d", jseg->js_refs)); 4569 if (--jseg->js_refs != 0) 4570 return; 4571 free_jsegs(jseg->js_jblocks); 4572 } 4573 4574 /* 4575 * Release a jsegdep and decrement the jseg count. 4576 */ 4577 static void 4578 free_jsegdep(jsegdep) 4579 struct jsegdep *jsegdep; 4580 { 4581 4582 if (jsegdep->jd_seg) 4583 rele_jseg(jsegdep->jd_seg); 4584 WORKITEM_FREE(jsegdep, D_JSEGDEP); 4585 } 4586 4587 /* 4588 * Wait for a journal item to make it to disk. Initiate journal processing 4589 * if required. 4590 */ 4591 static int 4592 jwait(wk, waitfor) 4593 struct worklist *wk; 4594 int waitfor; 4595 { 4596 4597 LOCK_OWNED(VFSTOUFS(wk->wk_mp)); 4598 /* 4599 * Blocking journal waits cause slow synchronous behavior. Record 4600 * stats on the frequency of these blocking operations. 4601 */ 4602 if (waitfor == MNT_WAIT) { 4603 stat_journal_wait++; 4604 switch (wk->wk_type) { 4605 case D_JREMREF: 4606 case D_JMVREF: 4607 stat_jwait_filepage++; 4608 break; 4609 case D_JTRUNC: 4610 case D_JFREEBLK: 4611 stat_jwait_freeblks++; 4612 break; 4613 case D_JNEWBLK: 4614 stat_jwait_newblk++; 4615 break; 4616 case D_JADDREF: 4617 stat_jwait_inode++; 4618 break; 4619 default: 4620 break; 4621 } 4622 } 4623 /* 4624 * If IO has not started we process the journal. We can't mark the 4625 * worklist item as IOWAITING because we drop the lock while 4626 * processing the journal and the worklist entry may be freed after 4627 * this point. The caller may call back in and re-issue the request. 4628 */ 4629 if ((wk->wk_state & INPROGRESS) == 0) { 4630 softdep_process_journal(wk->wk_mp, wk, waitfor); 4631 if (waitfor != MNT_WAIT) 4632 return (EBUSY); 4633 return (0); 4634 } 4635 if (waitfor != MNT_WAIT) 4636 return (EBUSY); 4637 wait_worklist(wk, "jwait"); 4638 return (0); 4639 } 4640 4641 /* 4642 * Lookup an inodedep based on an inode pointer and set the nlinkdelta as 4643 * appropriate. This is a convenience function to reduce duplicate code 4644 * for the setup and revert functions below. 4645 */ 4646 static struct inodedep * 4647 inodedep_lookup_ip(ip) 4648 struct inode *ip; 4649 { 4650 struct inodedep *inodedep; 4651 4652 KASSERT(ip->i_nlink >= ip->i_effnlink, 4653 ("inodedep_lookup_ip: bad delta")); 4654 (void) inodedep_lookup(ITOVFS(ip), ip->i_number, DEPALLOC, 4655 &inodedep); 4656 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 4657 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 4658 4659 return (inodedep); 4660 } 4661 4662 /* 4663 * Called prior to creating a new inode and linking it to a directory. The 4664 * jaddref structure must already be allocated by softdep_setup_inomapdep 4665 * and it is discovered here so we can initialize the mode and update 4666 * nlinkdelta. 4667 */ 4668 void 4669 softdep_setup_create(dp, ip) 4670 struct inode *dp; 4671 struct inode *ip; 4672 { 4673 struct inodedep *inodedep; 4674 struct jaddref *jaddref; 4675 struct vnode *dvp; 4676 4677 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4678 ("softdep_setup_create called on non-softdep filesystem")); 4679 KASSERT(ip->i_nlink == 1, 4680 ("softdep_setup_create: Invalid link count.")); 4681 dvp = ITOV(dp); 4682 ACQUIRE_LOCK(ITOUMP(dp)); 4683 inodedep = inodedep_lookup_ip(ip); 4684 if (DOINGSUJ(dvp)) { 4685 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4686 inoreflst); 4687 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 4688 ("softdep_setup_create: No addref structure present.")); 4689 } 4690 softdep_prelink(dvp, NULL); 4691 FREE_LOCK(ITOUMP(dp)); 4692 } 4693 4694 /* 4695 * Create a jaddref structure to track the addition of a DOTDOT link when 4696 * we are reparenting an inode as part of a rename. This jaddref will be 4697 * found by softdep_setup_directory_change. Adjusts nlinkdelta for 4698 * non-journaling softdep. 4699 */ 4700 void 4701 softdep_setup_dotdot_link(dp, ip) 4702 struct inode *dp; 4703 struct inode *ip; 4704 { 4705 struct inodedep *inodedep; 4706 struct jaddref *jaddref; 4707 struct vnode *dvp; 4708 4709 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4710 ("softdep_setup_dotdot_link called on non-softdep filesystem")); 4711 dvp = ITOV(dp); 4712 jaddref = NULL; 4713 /* 4714 * We don't set MKDIR_PARENT as this is not tied to a mkdir and 4715 * is used as a normal link would be. 4716 */ 4717 if (DOINGSUJ(dvp)) 4718 jaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 4719 dp->i_effnlink - 1, dp->i_mode); 4720 ACQUIRE_LOCK(ITOUMP(dp)); 4721 inodedep = inodedep_lookup_ip(dp); 4722 if (jaddref) 4723 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4724 if_deps); 4725 softdep_prelink(dvp, ITOV(ip)); 4726 FREE_LOCK(ITOUMP(dp)); 4727 } 4728 4729 /* 4730 * Create a jaddref structure to track a new link to an inode. The directory 4731 * offset is not known until softdep_setup_directory_add or 4732 * softdep_setup_directory_change. Adjusts nlinkdelta for non-journaling 4733 * softdep. 4734 */ 4735 void 4736 softdep_setup_link(dp, ip) 4737 struct inode *dp; 4738 struct inode *ip; 4739 { 4740 struct inodedep *inodedep; 4741 struct jaddref *jaddref; 4742 struct vnode *dvp; 4743 4744 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4745 ("softdep_setup_link called on non-softdep filesystem")); 4746 dvp = ITOV(dp); 4747 jaddref = NULL; 4748 if (DOINGSUJ(dvp)) 4749 jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1, 4750 ip->i_mode); 4751 ACQUIRE_LOCK(ITOUMP(dp)); 4752 inodedep = inodedep_lookup_ip(ip); 4753 if (jaddref) 4754 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4755 if_deps); 4756 softdep_prelink(dvp, ITOV(ip)); 4757 FREE_LOCK(ITOUMP(dp)); 4758 } 4759 4760 /* 4761 * Called to create the jaddref structures to track . and .. references as 4762 * well as lookup and further initialize the incomplete jaddref created 4763 * by softdep_setup_inomapdep when the inode was allocated. Adjusts 4764 * nlinkdelta for non-journaling softdep. 4765 */ 4766 void 4767 softdep_setup_mkdir(dp, ip) 4768 struct inode *dp; 4769 struct inode *ip; 4770 { 4771 struct inodedep *inodedep; 4772 struct jaddref *dotdotaddref; 4773 struct jaddref *dotaddref; 4774 struct jaddref *jaddref; 4775 struct vnode *dvp; 4776 4777 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4778 ("softdep_setup_mkdir called on non-softdep filesystem")); 4779 dvp = ITOV(dp); 4780 dotaddref = dotdotaddref = NULL; 4781 if (DOINGSUJ(dvp)) { 4782 dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1, 4783 ip->i_mode); 4784 dotaddref->ja_state |= MKDIR_BODY; 4785 dotdotaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 4786 dp->i_effnlink - 1, dp->i_mode); 4787 dotdotaddref->ja_state |= MKDIR_PARENT; 4788 } 4789 ACQUIRE_LOCK(ITOUMP(dp)); 4790 inodedep = inodedep_lookup_ip(ip); 4791 if (DOINGSUJ(dvp)) { 4792 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4793 inoreflst); 4794 KASSERT(jaddref != NULL, 4795 ("softdep_setup_mkdir: No addref structure present.")); 4796 KASSERT(jaddref->ja_parent == dp->i_number, 4797 ("softdep_setup_mkdir: bad parent %ju", 4798 (uintmax_t)jaddref->ja_parent)); 4799 TAILQ_INSERT_BEFORE(&jaddref->ja_ref, &dotaddref->ja_ref, 4800 if_deps); 4801 } 4802 inodedep = inodedep_lookup_ip(dp); 4803 if (DOINGSUJ(dvp)) 4804 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, 4805 &dotdotaddref->ja_ref, if_deps); 4806 softdep_prelink(ITOV(dp), NULL); 4807 FREE_LOCK(ITOUMP(dp)); 4808 } 4809 4810 /* 4811 * Called to track nlinkdelta of the inode and parent directories prior to 4812 * unlinking a directory. 4813 */ 4814 void 4815 softdep_setup_rmdir(dp, ip) 4816 struct inode *dp; 4817 struct inode *ip; 4818 { 4819 struct vnode *dvp; 4820 4821 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4822 ("softdep_setup_rmdir called on non-softdep filesystem")); 4823 dvp = ITOV(dp); 4824 ACQUIRE_LOCK(ITOUMP(dp)); 4825 (void) inodedep_lookup_ip(ip); 4826 (void) inodedep_lookup_ip(dp); 4827 softdep_prelink(dvp, ITOV(ip)); 4828 FREE_LOCK(ITOUMP(dp)); 4829 } 4830 4831 /* 4832 * Called to track nlinkdelta of the inode and parent directories prior to 4833 * unlink. 4834 */ 4835 void 4836 softdep_setup_unlink(dp, ip) 4837 struct inode *dp; 4838 struct inode *ip; 4839 { 4840 struct vnode *dvp; 4841 4842 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4843 ("softdep_setup_unlink called on non-softdep filesystem")); 4844 dvp = ITOV(dp); 4845 ACQUIRE_LOCK(ITOUMP(dp)); 4846 (void) inodedep_lookup_ip(ip); 4847 (void) inodedep_lookup_ip(dp); 4848 softdep_prelink(dvp, ITOV(ip)); 4849 FREE_LOCK(ITOUMP(dp)); 4850 } 4851 4852 /* 4853 * Called to release the journal structures created by a failed non-directory 4854 * creation. Adjusts nlinkdelta for non-journaling softdep. 4855 */ 4856 void 4857 softdep_revert_create(dp, ip) 4858 struct inode *dp; 4859 struct inode *ip; 4860 { 4861 struct inodedep *inodedep; 4862 struct jaddref *jaddref; 4863 struct vnode *dvp; 4864 4865 KASSERT(MOUNTEDSOFTDEP(ITOVFS((dp))) != 0, 4866 ("softdep_revert_create called on non-softdep filesystem")); 4867 dvp = ITOV(dp); 4868 ACQUIRE_LOCK(ITOUMP(dp)); 4869 inodedep = inodedep_lookup_ip(ip); 4870 if (DOINGSUJ(dvp)) { 4871 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4872 inoreflst); 4873 KASSERT(jaddref->ja_parent == dp->i_number, 4874 ("softdep_revert_create: addref parent mismatch")); 4875 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4876 } 4877 FREE_LOCK(ITOUMP(dp)); 4878 } 4879 4880 /* 4881 * Called to release the journal structures created by a failed link 4882 * addition. Adjusts nlinkdelta for non-journaling softdep. 4883 */ 4884 void 4885 softdep_revert_link(dp, ip) 4886 struct inode *dp; 4887 struct inode *ip; 4888 { 4889 struct inodedep *inodedep; 4890 struct jaddref *jaddref; 4891 struct vnode *dvp; 4892 4893 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4894 ("softdep_revert_link called on non-softdep filesystem")); 4895 dvp = ITOV(dp); 4896 ACQUIRE_LOCK(ITOUMP(dp)); 4897 inodedep = inodedep_lookup_ip(ip); 4898 if (DOINGSUJ(dvp)) { 4899 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4900 inoreflst); 4901 KASSERT(jaddref->ja_parent == dp->i_number, 4902 ("softdep_revert_link: addref parent mismatch")); 4903 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4904 } 4905 FREE_LOCK(ITOUMP(dp)); 4906 } 4907 4908 /* 4909 * Called to release the journal structures created by a failed mkdir 4910 * attempt. Adjusts nlinkdelta for non-journaling softdep. 4911 */ 4912 void 4913 softdep_revert_mkdir(dp, ip) 4914 struct inode *dp; 4915 struct inode *ip; 4916 { 4917 struct inodedep *inodedep; 4918 struct jaddref *jaddref; 4919 struct jaddref *dotaddref; 4920 struct vnode *dvp; 4921 4922 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4923 ("softdep_revert_mkdir called on non-softdep filesystem")); 4924 dvp = ITOV(dp); 4925 4926 ACQUIRE_LOCK(ITOUMP(dp)); 4927 inodedep = inodedep_lookup_ip(dp); 4928 if (DOINGSUJ(dvp)) { 4929 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4930 inoreflst); 4931 KASSERT(jaddref->ja_parent == ip->i_number, 4932 ("softdep_revert_mkdir: dotdot addref parent mismatch")); 4933 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4934 } 4935 inodedep = inodedep_lookup_ip(ip); 4936 if (DOINGSUJ(dvp)) { 4937 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4938 inoreflst); 4939 KASSERT(jaddref->ja_parent == dp->i_number, 4940 ("softdep_revert_mkdir: addref parent mismatch")); 4941 dotaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 4942 inoreflst, if_deps); 4943 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4944 KASSERT(dotaddref->ja_parent == ip->i_number, 4945 ("softdep_revert_mkdir: dot addref parent mismatch")); 4946 cancel_jaddref(dotaddref, inodedep, &inodedep->id_inowait); 4947 } 4948 FREE_LOCK(ITOUMP(dp)); 4949 } 4950 4951 /* 4952 * Called to correct nlinkdelta after a failed rmdir. 4953 */ 4954 void 4955 softdep_revert_rmdir(dp, ip) 4956 struct inode *dp; 4957 struct inode *ip; 4958 { 4959 4960 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4961 ("softdep_revert_rmdir called on non-softdep filesystem")); 4962 ACQUIRE_LOCK(ITOUMP(dp)); 4963 (void) inodedep_lookup_ip(ip); 4964 (void) inodedep_lookup_ip(dp); 4965 FREE_LOCK(ITOUMP(dp)); 4966 } 4967 4968 /* 4969 * Protecting the freemaps (or bitmaps). 4970 * 4971 * To eliminate the need to execute fsck before mounting a filesystem 4972 * after a power failure, one must (conservatively) guarantee that the 4973 * on-disk copy of the bitmaps never indicate that a live inode or block is 4974 * free. So, when a block or inode is allocated, the bitmap should be 4975 * updated (on disk) before any new pointers. When a block or inode is 4976 * freed, the bitmap should not be updated until all pointers have been 4977 * reset. The latter dependency is handled by the delayed de-allocation 4978 * approach described below for block and inode de-allocation. The former 4979 * dependency is handled by calling the following procedure when a block or 4980 * inode is allocated. When an inode is allocated an "inodedep" is created 4981 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk. 4982 * Each "inodedep" is also inserted into the hash indexing structure so 4983 * that any additional link additions can be made dependent on the inode 4984 * allocation. 4985 * 4986 * The ufs filesystem maintains a number of free block counts (e.g., per 4987 * cylinder group, per cylinder and per <cylinder, rotational position> pair) 4988 * in addition to the bitmaps. These counts are used to improve efficiency 4989 * during allocation and therefore must be consistent with the bitmaps. 4990 * There is no convenient way to guarantee post-crash consistency of these 4991 * counts with simple update ordering, for two main reasons: (1) The counts 4992 * and bitmaps for a single cylinder group block are not in the same disk 4993 * sector. If a disk write is interrupted (e.g., by power failure), one may 4994 * be written and the other not. (2) Some of the counts are located in the 4995 * superblock rather than the cylinder group block. So, we focus our soft 4996 * updates implementation on protecting the bitmaps. When mounting a 4997 * filesystem, we recompute the auxiliary counts from the bitmaps. 4998 */ 4999 5000 /* 5001 * Called just after updating the cylinder group block to allocate an inode. 5002 */ 5003 void 5004 softdep_setup_inomapdep(bp, ip, newinum, mode) 5005 struct buf *bp; /* buffer for cylgroup block with inode map */ 5006 struct inode *ip; /* inode related to allocation */ 5007 ino_t newinum; /* new inode number being allocated */ 5008 int mode; 5009 { 5010 struct inodedep *inodedep; 5011 struct bmsafemap *bmsafemap; 5012 struct jaddref *jaddref; 5013 struct mount *mp; 5014 struct fs *fs; 5015 5016 mp = ITOVFS(ip); 5017 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5018 ("softdep_setup_inomapdep called on non-softdep filesystem")); 5019 fs = VFSTOUFS(mp)->um_fs; 5020 jaddref = NULL; 5021 5022 /* 5023 * Allocate the journal reference add structure so that the bitmap 5024 * can be dependent on it. 5025 */ 5026 if (MOUNTEDSUJ(mp)) { 5027 jaddref = newjaddref(ip, newinum, 0, 0, mode); 5028 jaddref->ja_state |= NEWBLOCK; 5029 } 5030 5031 /* 5032 * Create a dependency for the newly allocated inode. 5033 * Panic if it already exists as something is seriously wrong. 5034 * Otherwise add it to the dependency list for the buffer holding 5035 * the cylinder group map from which it was allocated. 5036 * 5037 * We have to preallocate a bmsafemap entry in case it is needed 5038 * in bmsafemap_lookup since once we allocate the inodedep, we 5039 * have to finish initializing it before we can FREE_LOCK(). 5040 * By preallocating, we avoid FREE_LOCK() while doing a malloc 5041 * in bmsafemap_lookup. We cannot call bmsafemap_lookup before 5042 * creating the inodedep as it can be freed during the time 5043 * that we FREE_LOCK() while allocating the inodedep. We must 5044 * call workitem_alloc() before entering the locked section as 5045 * it also acquires the lock and we must avoid trying doing so 5046 * recursively. 5047 */ 5048 bmsafemap = malloc(sizeof(struct bmsafemap), 5049 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5050 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5051 ACQUIRE_LOCK(ITOUMP(ip)); 5052 if ((inodedep_lookup(mp, newinum, DEPALLOC, &inodedep))) 5053 panic("softdep_setup_inomapdep: dependency %p for new" 5054 "inode already exists", inodedep); 5055 bmsafemap = bmsafemap_lookup(mp, bp, ino_to_cg(fs, newinum), bmsafemap); 5056 if (jaddref) { 5057 LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps); 5058 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 5059 if_deps); 5060 } else { 5061 inodedep->id_state |= ONDEPLIST; 5062 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps); 5063 } 5064 inodedep->id_bmsafemap = bmsafemap; 5065 inodedep->id_state &= ~DEPCOMPLETE; 5066 FREE_LOCK(ITOUMP(ip)); 5067 } 5068 5069 /* 5070 * Called just after updating the cylinder group block to 5071 * allocate block or fragment. 5072 */ 5073 void 5074 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags) 5075 struct buf *bp; /* buffer for cylgroup block with block map */ 5076 struct mount *mp; /* filesystem doing allocation */ 5077 ufs2_daddr_t newblkno; /* number of newly allocated block */ 5078 int frags; /* Number of fragments. */ 5079 int oldfrags; /* Previous number of fragments for extend. */ 5080 { 5081 struct newblk *newblk; 5082 struct bmsafemap *bmsafemap; 5083 struct jnewblk *jnewblk; 5084 struct ufsmount *ump; 5085 struct fs *fs; 5086 5087 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5088 ("softdep_setup_blkmapdep called on non-softdep filesystem")); 5089 ump = VFSTOUFS(mp); 5090 fs = ump->um_fs; 5091 jnewblk = NULL; 5092 /* 5093 * Create a dependency for the newly allocated block. 5094 * Add it to the dependency list for the buffer holding 5095 * the cylinder group map from which it was allocated. 5096 */ 5097 if (MOUNTEDSUJ(mp)) { 5098 jnewblk = malloc(sizeof(*jnewblk), M_JNEWBLK, M_SOFTDEP_FLAGS); 5099 workitem_alloc(&jnewblk->jn_list, D_JNEWBLK, mp); 5100 jnewblk->jn_jsegdep = newjsegdep(&jnewblk->jn_list); 5101 jnewblk->jn_state = ATTACHED; 5102 jnewblk->jn_blkno = newblkno; 5103 jnewblk->jn_frags = frags; 5104 jnewblk->jn_oldfrags = oldfrags; 5105 #ifdef SUJ_DEBUG 5106 { 5107 struct cg *cgp; 5108 uint8_t *blksfree; 5109 long bno; 5110 int i; 5111 5112 cgp = (struct cg *)bp->b_data; 5113 blksfree = cg_blksfree(cgp); 5114 bno = dtogd(fs, jnewblk->jn_blkno); 5115 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; 5116 i++) { 5117 if (isset(blksfree, bno + i)) 5118 panic("softdep_setup_blkmapdep: " 5119 "free fragment %d from %d-%d " 5120 "state 0x%X dep %p", i, 5121 jnewblk->jn_oldfrags, 5122 jnewblk->jn_frags, 5123 jnewblk->jn_state, 5124 jnewblk->jn_dep); 5125 } 5126 } 5127 #endif 5128 } 5129 5130 CTR3(KTR_SUJ, 5131 "softdep_setup_blkmapdep: blkno %jd frags %d oldfrags %d", 5132 newblkno, frags, oldfrags); 5133 ACQUIRE_LOCK(ump); 5134 if (newblk_lookup(mp, newblkno, DEPALLOC, &newblk) != 0) 5135 panic("softdep_setup_blkmapdep: found block"); 5136 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp, 5137 dtog(fs, newblkno), NULL); 5138 if (jnewblk) { 5139 jnewblk->jn_dep = (struct worklist *)newblk; 5140 LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps); 5141 } else { 5142 newblk->nb_state |= ONDEPLIST; 5143 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps); 5144 } 5145 newblk->nb_bmsafemap = bmsafemap; 5146 newblk->nb_jnewblk = jnewblk; 5147 FREE_LOCK(ump); 5148 } 5149 5150 #define BMSAFEMAP_HASH(ump, cg) \ 5151 (&(ump)->bmsafemap_hashtbl[(cg) & (ump)->bmsafemap_hash_size]) 5152 5153 static int 5154 bmsafemap_find(bmsafemaphd, cg, bmsafemapp) 5155 struct bmsafemap_hashhead *bmsafemaphd; 5156 int cg; 5157 struct bmsafemap **bmsafemapp; 5158 { 5159 struct bmsafemap *bmsafemap; 5160 5161 LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash) 5162 if (bmsafemap->sm_cg == cg) 5163 break; 5164 if (bmsafemap) { 5165 *bmsafemapp = bmsafemap; 5166 return (1); 5167 } 5168 *bmsafemapp = NULL; 5169 5170 return (0); 5171 } 5172 5173 /* 5174 * Find the bmsafemap associated with a cylinder group buffer. 5175 * If none exists, create one. The buffer must be locked when 5176 * this routine is called and this routine must be called with 5177 * the softdep lock held. To avoid giving up the lock while 5178 * allocating a new bmsafemap, a preallocated bmsafemap may be 5179 * provided. If it is provided but not needed, it is freed. 5180 */ 5181 static struct bmsafemap * 5182 bmsafemap_lookup(mp, bp, cg, newbmsafemap) 5183 struct mount *mp; 5184 struct buf *bp; 5185 int cg; 5186 struct bmsafemap *newbmsafemap; 5187 { 5188 struct bmsafemap_hashhead *bmsafemaphd; 5189 struct bmsafemap *bmsafemap, *collision; 5190 struct worklist *wk; 5191 struct ufsmount *ump; 5192 5193 ump = VFSTOUFS(mp); 5194 LOCK_OWNED(ump); 5195 KASSERT(bp != NULL, ("bmsafemap_lookup: missing buffer")); 5196 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 5197 if (wk->wk_type == D_BMSAFEMAP) { 5198 if (newbmsafemap) 5199 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5200 return (WK_BMSAFEMAP(wk)); 5201 } 5202 } 5203 bmsafemaphd = BMSAFEMAP_HASH(ump, cg); 5204 if (bmsafemap_find(bmsafemaphd, cg, &bmsafemap) == 1) { 5205 if (newbmsafemap) 5206 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5207 return (bmsafemap); 5208 } 5209 if (newbmsafemap) { 5210 bmsafemap = newbmsafemap; 5211 } else { 5212 FREE_LOCK(ump); 5213 bmsafemap = malloc(sizeof(struct bmsafemap), 5214 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5215 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5216 ACQUIRE_LOCK(ump); 5217 } 5218 bmsafemap->sm_buf = bp; 5219 LIST_INIT(&bmsafemap->sm_inodedephd); 5220 LIST_INIT(&bmsafemap->sm_inodedepwr); 5221 LIST_INIT(&bmsafemap->sm_newblkhd); 5222 LIST_INIT(&bmsafemap->sm_newblkwr); 5223 LIST_INIT(&bmsafemap->sm_jaddrefhd); 5224 LIST_INIT(&bmsafemap->sm_jnewblkhd); 5225 LIST_INIT(&bmsafemap->sm_freehd); 5226 LIST_INIT(&bmsafemap->sm_freewr); 5227 if (bmsafemap_find(bmsafemaphd, cg, &collision) == 1) { 5228 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 5229 return (collision); 5230 } 5231 bmsafemap->sm_cg = cg; 5232 LIST_INSERT_HEAD(bmsafemaphd, bmsafemap, sm_hash); 5233 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 5234 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list); 5235 return (bmsafemap); 5236 } 5237 5238 /* 5239 * Direct block allocation dependencies. 5240 * 5241 * When a new block is allocated, the corresponding disk locations must be 5242 * initialized (with zeros or new data) before the on-disk inode points to 5243 * them. Also, the freemap from which the block was allocated must be 5244 * updated (on disk) before the inode's pointer. These two dependencies are 5245 * independent of each other and are needed for all file blocks and indirect 5246 * blocks that are pointed to directly by the inode. Just before the 5247 * "in-core" version of the inode is updated with a newly allocated block 5248 * number, a procedure (below) is called to setup allocation dependency 5249 * structures. These structures are removed when the corresponding 5250 * dependencies are satisfied or when the block allocation becomes obsolete 5251 * (i.e., the file is deleted, the block is de-allocated, or the block is a 5252 * fragment that gets upgraded). All of these cases are handled in 5253 * procedures described later. 5254 * 5255 * When a file extension causes a fragment to be upgraded, either to a larger 5256 * fragment or to a full block, the on-disk location may change (if the 5257 * previous fragment could not simply be extended). In this case, the old 5258 * fragment must be de-allocated, but not until after the inode's pointer has 5259 * been updated. In most cases, this is handled by later procedures, which 5260 * will construct a "freefrag" structure to be added to the workitem queue 5261 * when the inode update is complete (or obsolete). The main exception to 5262 * this is when an allocation occurs while a pending allocation dependency 5263 * (for the same block pointer) remains. This case is handled in the main 5264 * allocation dependency setup procedure by immediately freeing the 5265 * unreferenced fragments. 5266 */ 5267 void 5268 softdep_setup_allocdirect(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 5269 struct inode *ip; /* inode to which block is being added */ 5270 ufs_lbn_t off; /* block pointer within inode */ 5271 ufs2_daddr_t newblkno; /* disk block number being added */ 5272 ufs2_daddr_t oldblkno; /* previous block number, 0 unless frag */ 5273 long newsize; /* size of new block */ 5274 long oldsize; /* size of new block */ 5275 struct buf *bp; /* bp for allocated block */ 5276 { 5277 struct allocdirect *adp, *oldadp; 5278 struct allocdirectlst *adphead; 5279 struct freefrag *freefrag; 5280 struct inodedep *inodedep; 5281 struct pagedep *pagedep; 5282 struct jnewblk *jnewblk; 5283 struct newblk *newblk; 5284 struct mount *mp; 5285 ufs_lbn_t lbn; 5286 5287 lbn = bp->b_lblkno; 5288 mp = ITOVFS(ip); 5289 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5290 ("softdep_setup_allocdirect called on non-softdep filesystem")); 5291 if (oldblkno && oldblkno != newblkno) 5292 /* 5293 * The usual case is that a smaller fragment that 5294 * was just allocated has been replaced with a bigger 5295 * fragment or a full-size block. If it is marked as 5296 * B_DELWRI, the current contents have not been written 5297 * to disk. It is possible that the block was written 5298 * earlier, but very uncommon. If the block has never 5299 * been written, there is no need to send a BIO_DELETE 5300 * for it when it is freed. The gain from avoiding the 5301 * TRIMs for the common case of unwritten blocks far 5302 * exceeds the cost of the write amplification for the 5303 * uncommon case of failing to send a TRIM for a block 5304 * that had been written. 5305 */ 5306 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn, 5307 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY); 5308 else 5309 freefrag = NULL; 5310 5311 CTR6(KTR_SUJ, 5312 "softdep_setup_allocdirect: ino %d blkno %jd oldblkno %jd " 5313 "off %jd newsize %ld oldsize %d", 5314 ip->i_number, newblkno, oldblkno, off, newsize, oldsize); 5315 ACQUIRE_LOCK(ITOUMP(ip)); 5316 if (off >= UFS_NDADDR) { 5317 if (lbn > 0) 5318 panic("softdep_setup_allocdirect: bad lbn %jd, off %jd", 5319 lbn, off); 5320 /* allocating an indirect block */ 5321 if (oldblkno != 0) 5322 panic("softdep_setup_allocdirect: non-zero indir"); 5323 } else { 5324 if (off != lbn) 5325 panic("softdep_setup_allocdirect: lbn %jd != off %jd", 5326 lbn, off); 5327 /* 5328 * Allocating a direct block. 5329 * 5330 * If we are allocating a directory block, then we must 5331 * allocate an associated pagedep to track additions and 5332 * deletions. 5333 */ 5334 if ((ip->i_mode & IFMT) == IFDIR) 5335 pagedep_lookup(mp, bp, ip->i_number, off, DEPALLOC, 5336 &pagedep); 5337 } 5338 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5339 panic("softdep_setup_allocdirect: lost block"); 5340 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5341 ("softdep_setup_allocdirect: newblk already initialized")); 5342 /* 5343 * Convert the newblk to an allocdirect. 5344 */ 5345 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT); 5346 adp = (struct allocdirect *)newblk; 5347 newblk->nb_freefrag = freefrag; 5348 adp->ad_offset = off; 5349 adp->ad_oldblkno = oldblkno; 5350 adp->ad_newsize = newsize; 5351 adp->ad_oldsize = oldsize; 5352 5353 /* 5354 * Finish initializing the journal. 5355 */ 5356 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5357 jnewblk->jn_ino = ip->i_number; 5358 jnewblk->jn_lbn = lbn; 5359 add_to_journal(&jnewblk->jn_list); 5360 } 5361 if (freefrag && freefrag->ff_jdep != NULL && 5362 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5363 add_to_journal(freefrag->ff_jdep); 5364 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 5365 adp->ad_inodedep = inodedep; 5366 5367 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5368 /* 5369 * The list of allocdirects must be kept in sorted and ascending 5370 * order so that the rollback routines can quickly determine the 5371 * first uncommitted block (the size of the file stored on disk 5372 * ends at the end of the lowest committed fragment, or if there 5373 * are no fragments, at the end of the highest committed block). 5374 * Since files generally grow, the typical case is that the new 5375 * block is to be added at the end of the list. We speed this 5376 * special case by checking against the last allocdirect in the 5377 * list before laboriously traversing the list looking for the 5378 * insertion point. 5379 */ 5380 adphead = &inodedep->id_newinoupdt; 5381 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5382 if (oldadp == NULL || oldadp->ad_offset <= off) { 5383 /* insert at end of list */ 5384 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5385 if (oldadp != NULL && oldadp->ad_offset == off) 5386 allocdirect_merge(adphead, adp, oldadp); 5387 FREE_LOCK(ITOUMP(ip)); 5388 return; 5389 } 5390 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5391 if (oldadp->ad_offset >= off) 5392 break; 5393 } 5394 if (oldadp == NULL) 5395 panic("softdep_setup_allocdirect: lost entry"); 5396 /* insert in middle of list */ 5397 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5398 if (oldadp->ad_offset == off) 5399 allocdirect_merge(adphead, adp, oldadp); 5400 5401 FREE_LOCK(ITOUMP(ip)); 5402 } 5403 5404 /* 5405 * Merge a newer and older journal record to be stored either in a 5406 * newblock or freefrag. This handles aggregating journal records for 5407 * fragment allocation into a second record as well as replacing a 5408 * journal free with an aborted journal allocation. A segment for the 5409 * oldest record will be placed on wkhd if it has been written. If not 5410 * the segment for the newer record will suffice. 5411 */ 5412 static struct worklist * 5413 jnewblk_merge(new, old, wkhd) 5414 struct worklist *new; 5415 struct worklist *old; 5416 struct workhead *wkhd; 5417 { 5418 struct jnewblk *njnewblk; 5419 struct jnewblk *jnewblk; 5420 5421 /* Handle NULLs to simplify callers. */ 5422 if (new == NULL) 5423 return (old); 5424 if (old == NULL) 5425 return (new); 5426 /* Replace a jfreefrag with a jnewblk. */ 5427 if (new->wk_type == D_JFREEFRAG) { 5428 if (WK_JNEWBLK(old)->jn_blkno != WK_JFREEFRAG(new)->fr_blkno) 5429 panic("jnewblk_merge: blkno mismatch: %p, %p", 5430 old, new); 5431 cancel_jfreefrag(WK_JFREEFRAG(new)); 5432 return (old); 5433 } 5434 if (old->wk_type != D_JNEWBLK || new->wk_type != D_JNEWBLK) 5435 panic("jnewblk_merge: Bad type: old %d new %d\n", 5436 old->wk_type, new->wk_type); 5437 /* 5438 * Handle merging of two jnewblk records that describe 5439 * different sets of fragments in the same block. 5440 */ 5441 jnewblk = WK_JNEWBLK(old); 5442 njnewblk = WK_JNEWBLK(new); 5443 if (jnewblk->jn_blkno != njnewblk->jn_blkno) 5444 panic("jnewblk_merge: Merging disparate blocks."); 5445 /* 5446 * The record may be rolled back in the cg. 5447 */ 5448 if (jnewblk->jn_state & UNDONE) { 5449 jnewblk->jn_state &= ~UNDONE; 5450 njnewblk->jn_state |= UNDONE; 5451 njnewblk->jn_state &= ~ATTACHED; 5452 } 5453 /* 5454 * We modify the newer addref and free the older so that if neither 5455 * has been written the most up-to-date copy will be on disk. If 5456 * both have been written but rolled back we only temporarily need 5457 * one of them to fix the bits when the cg write completes. 5458 */ 5459 jnewblk->jn_state |= ATTACHED | COMPLETE; 5460 njnewblk->jn_oldfrags = jnewblk->jn_oldfrags; 5461 cancel_jnewblk(jnewblk, wkhd); 5462 WORKLIST_REMOVE(&jnewblk->jn_list); 5463 free_jnewblk(jnewblk); 5464 return (new); 5465 } 5466 5467 /* 5468 * Replace an old allocdirect dependency with a newer one. 5469 * This routine must be called with splbio interrupts blocked. 5470 */ 5471 static void 5472 allocdirect_merge(adphead, newadp, oldadp) 5473 struct allocdirectlst *adphead; /* head of list holding allocdirects */ 5474 struct allocdirect *newadp; /* allocdirect being added */ 5475 struct allocdirect *oldadp; /* existing allocdirect being checked */ 5476 { 5477 struct worklist *wk; 5478 struct freefrag *freefrag; 5479 5480 freefrag = NULL; 5481 LOCK_OWNED(VFSTOUFS(newadp->ad_list.wk_mp)); 5482 if (newadp->ad_oldblkno != oldadp->ad_newblkno || 5483 newadp->ad_oldsize != oldadp->ad_newsize || 5484 newadp->ad_offset >= UFS_NDADDR) 5485 panic("%s %jd != new %jd || old size %ld != new %ld", 5486 "allocdirect_merge: old blkno", 5487 (intmax_t)newadp->ad_oldblkno, 5488 (intmax_t)oldadp->ad_newblkno, 5489 newadp->ad_oldsize, oldadp->ad_newsize); 5490 newadp->ad_oldblkno = oldadp->ad_oldblkno; 5491 newadp->ad_oldsize = oldadp->ad_oldsize; 5492 /* 5493 * If the old dependency had a fragment to free or had never 5494 * previously had a block allocated, then the new dependency 5495 * can immediately post its freefrag and adopt the old freefrag. 5496 * This action is done by swapping the freefrag dependencies. 5497 * The new dependency gains the old one's freefrag, and the 5498 * old one gets the new one and then immediately puts it on 5499 * the worklist when it is freed by free_newblk. It is 5500 * not possible to do this swap when the old dependency had a 5501 * non-zero size but no previous fragment to free. This condition 5502 * arises when the new block is an extension of the old block. 5503 * Here, the first part of the fragment allocated to the new 5504 * dependency is part of the block currently claimed on disk by 5505 * the old dependency, so cannot legitimately be freed until the 5506 * conditions for the new dependency are fulfilled. 5507 */ 5508 freefrag = newadp->ad_freefrag; 5509 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) { 5510 newadp->ad_freefrag = oldadp->ad_freefrag; 5511 oldadp->ad_freefrag = freefrag; 5512 } 5513 /* 5514 * If we are tracking a new directory-block allocation, 5515 * move it from the old allocdirect to the new allocdirect. 5516 */ 5517 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) { 5518 WORKLIST_REMOVE(wk); 5519 if (!LIST_EMPTY(&oldadp->ad_newdirblk)) 5520 panic("allocdirect_merge: extra newdirblk"); 5521 WORKLIST_INSERT(&newadp->ad_newdirblk, wk); 5522 } 5523 TAILQ_REMOVE(adphead, oldadp, ad_next); 5524 /* 5525 * We need to move any journal dependencies over to the freefrag 5526 * that releases this block if it exists. Otherwise we are 5527 * extending an existing block and we'll wait until that is 5528 * complete to release the journal space and extend the 5529 * new journal to cover this old space as well. 5530 */ 5531 if (freefrag == NULL) { 5532 if (oldadp->ad_newblkno != newadp->ad_newblkno) 5533 panic("allocdirect_merge: %jd != %jd", 5534 oldadp->ad_newblkno, newadp->ad_newblkno); 5535 newadp->ad_block.nb_jnewblk = (struct jnewblk *) 5536 jnewblk_merge(&newadp->ad_block.nb_jnewblk->jn_list, 5537 &oldadp->ad_block.nb_jnewblk->jn_list, 5538 &newadp->ad_block.nb_jwork); 5539 oldadp->ad_block.nb_jnewblk = NULL; 5540 cancel_newblk(&oldadp->ad_block, NULL, 5541 &newadp->ad_block.nb_jwork); 5542 } else { 5543 wk = (struct worklist *) cancel_newblk(&oldadp->ad_block, 5544 &freefrag->ff_list, &freefrag->ff_jwork); 5545 freefrag->ff_jdep = jnewblk_merge(freefrag->ff_jdep, wk, 5546 &freefrag->ff_jwork); 5547 } 5548 free_newblk(&oldadp->ad_block); 5549 } 5550 5551 /* 5552 * Allocate a jfreefrag structure to journal a single block free. 5553 */ 5554 static struct jfreefrag * 5555 newjfreefrag(freefrag, ip, blkno, size, lbn) 5556 struct freefrag *freefrag; 5557 struct inode *ip; 5558 ufs2_daddr_t blkno; 5559 long size; 5560 ufs_lbn_t lbn; 5561 { 5562 struct jfreefrag *jfreefrag; 5563 struct fs *fs; 5564 5565 fs = ITOFS(ip); 5566 jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG, 5567 M_SOFTDEP_FLAGS); 5568 workitem_alloc(&jfreefrag->fr_list, D_JFREEFRAG, ITOVFS(ip)); 5569 jfreefrag->fr_jsegdep = newjsegdep(&jfreefrag->fr_list); 5570 jfreefrag->fr_state = ATTACHED | DEPCOMPLETE; 5571 jfreefrag->fr_ino = ip->i_number; 5572 jfreefrag->fr_lbn = lbn; 5573 jfreefrag->fr_blkno = blkno; 5574 jfreefrag->fr_frags = numfrags(fs, size); 5575 jfreefrag->fr_freefrag = freefrag; 5576 5577 return (jfreefrag); 5578 } 5579 5580 /* 5581 * Allocate a new freefrag structure. 5582 */ 5583 static struct freefrag * 5584 newfreefrag(ip, blkno, size, lbn, key) 5585 struct inode *ip; 5586 ufs2_daddr_t blkno; 5587 long size; 5588 ufs_lbn_t lbn; 5589 u_long key; 5590 { 5591 struct freefrag *freefrag; 5592 struct ufsmount *ump; 5593 struct fs *fs; 5594 5595 CTR4(KTR_SUJ, "newfreefrag: ino %d blkno %jd size %ld lbn %jd", 5596 ip->i_number, blkno, size, lbn); 5597 ump = ITOUMP(ip); 5598 fs = ump->um_fs; 5599 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag) 5600 panic("newfreefrag: frag size"); 5601 freefrag = malloc(sizeof(struct freefrag), 5602 M_FREEFRAG, M_SOFTDEP_FLAGS); 5603 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ump)); 5604 freefrag->ff_state = ATTACHED; 5605 LIST_INIT(&freefrag->ff_jwork); 5606 freefrag->ff_inum = ip->i_number; 5607 freefrag->ff_vtype = ITOV(ip)->v_type; 5608 freefrag->ff_blkno = blkno; 5609 freefrag->ff_fragsize = size; 5610 freefrag->ff_key = key; 5611 5612 if (MOUNTEDSUJ(UFSTOVFS(ump))) { 5613 freefrag->ff_jdep = (struct worklist *) 5614 newjfreefrag(freefrag, ip, blkno, size, lbn); 5615 } else { 5616 freefrag->ff_state |= DEPCOMPLETE; 5617 freefrag->ff_jdep = NULL; 5618 } 5619 5620 return (freefrag); 5621 } 5622 5623 /* 5624 * This workitem de-allocates fragments that were replaced during 5625 * file block allocation. 5626 */ 5627 static void 5628 handle_workitem_freefrag(freefrag) 5629 struct freefrag *freefrag; 5630 { 5631 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp); 5632 struct workhead wkhd; 5633 5634 CTR3(KTR_SUJ, 5635 "handle_workitem_freefrag: ino %d blkno %jd size %ld", 5636 freefrag->ff_inum, freefrag->ff_blkno, freefrag->ff_fragsize); 5637 /* 5638 * It would be illegal to add new completion items to the 5639 * freefrag after it was schedule to be done so it must be 5640 * safe to modify the list head here. 5641 */ 5642 LIST_INIT(&wkhd); 5643 ACQUIRE_LOCK(ump); 5644 LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list); 5645 /* 5646 * If the journal has not been written we must cancel it here. 5647 */ 5648 if (freefrag->ff_jdep) { 5649 if (freefrag->ff_jdep->wk_type != D_JNEWBLK) 5650 panic("handle_workitem_freefrag: Unexpected type %d\n", 5651 freefrag->ff_jdep->wk_type); 5652 cancel_jnewblk(WK_JNEWBLK(freefrag->ff_jdep), &wkhd); 5653 } 5654 FREE_LOCK(ump); 5655 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno, 5656 freefrag->ff_fragsize, freefrag->ff_inum, freefrag->ff_vtype, 5657 &wkhd, freefrag->ff_key); 5658 ACQUIRE_LOCK(ump); 5659 WORKITEM_FREE(freefrag, D_FREEFRAG); 5660 FREE_LOCK(ump); 5661 } 5662 5663 /* 5664 * Set up a dependency structure for an external attributes data block. 5665 * This routine follows much of the structure of softdep_setup_allocdirect. 5666 * See the description of softdep_setup_allocdirect above for details. 5667 */ 5668 void 5669 softdep_setup_allocext(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 5670 struct inode *ip; 5671 ufs_lbn_t off; 5672 ufs2_daddr_t newblkno; 5673 ufs2_daddr_t oldblkno; 5674 long newsize; 5675 long oldsize; 5676 struct buf *bp; 5677 { 5678 struct allocdirect *adp, *oldadp; 5679 struct allocdirectlst *adphead; 5680 struct freefrag *freefrag; 5681 struct inodedep *inodedep; 5682 struct jnewblk *jnewblk; 5683 struct newblk *newblk; 5684 struct mount *mp; 5685 struct ufsmount *ump; 5686 ufs_lbn_t lbn; 5687 5688 mp = ITOVFS(ip); 5689 ump = VFSTOUFS(mp); 5690 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5691 ("softdep_setup_allocext called on non-softdep filesystem")); 5692 KASSERT(off < UFS_NXADDR, 5693 ("softdep_setup_allocext: lbn %lld > UFS_NXADDR", (long long)off)); 5694 5695 lbn = bp->b_lblkno; 5696 if (oldblkno && oldblkno != newblkno) 5697 /* 5698 * The usual case is that a smaller fragment that 5699 * was just allocated has been replaced with a bigger 5700 * fragment or a full-size block. If it is marked as 5701 * B_DELWRI, the current contents have not been written 5702 * to disk. It is possible that the block was written 5703 * earlier, but very uncommon. If the block has never 5704 * been written, there is no need to send a BIO_DELETE 5705 * for it when it is freed. The gain from avoiding the 5706 * TRIMs for the common case of unwritten blocks far 5707 * exceeds the cost of the write amplification for the 5708 * uncommon case of failing to send a TRIM for a block 5709 * that had been written. 5710 */ 5711 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn, 5712 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY); 5713 else 5714 freefrag = NULL; 5715 5716 ACQUIRE_LOCK(ump); 5717 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5718 panic("softdep_setup_allocext: lost block"); 5719 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5720 ("softdep_setup_allocext: newblk already initialized")); 5721 /* 5722 * Convert the newblk to an allocdirect. 5723 */ 5724 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT); 5725 adp = (struct allocdirect *)newblk; 5726 newblk->nb_freefrag = freefrag; 5727 adp->ad_offset = off; 5728 adp->ad_oldblkno = oldblkno; 5729 adp->ad_newsize = newsize; 5730 adp->ad_oldsize = oldsize; 5731 adp->ad_state |= EXTDATA; 5732 5733 /* 5734 * Finish initializing the journal. 5735 */ 5736 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5737 jnewblk->jn_ino = ip->i_number; 5738 jnewblk->jn_lbn = lbn; 5739 add_to_journal(&jnewblk->jn_list); 5740 } 5741 if (freefrag && freefrag->ff_jdep != NULL && 5742 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5743 add_to_journal(freefrag->ff_jdep); 5744 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 5745 adp->ad_inodedep = inodedep; 5746 5747 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5748 /* 5749 * The list of allocdirects must be kept in sorted and ascending 5750 * order so that the rollback routines can quickly determine the 5751 * first uncommitted block (the size of the file stored on disk 5752 * ends at the end of the lowest committed fragment, or if there 5753 * are no fragments, at the end of the highest committed block). 5754 * Since files generally grow, the typical case is that the new 5755 * block is to be added at the end of the list. We speed this 5756 * special case by checking against the last allocdirect in the 5757 * list before laboriously traversing the list looking for the 5758 * insertion point. 5759 */ 5760 adphead = &inodedep->id_newextupdt; 5761 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5762 if (oldadp == NULL || oldadp->ad_offset <= off) { 5763 /* insert at end of list */ 5764 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5765 if (oldadp != NULL && oldadp->ad_offset == off) 5766 allocdirect_merge(adphead, adp, oldadp); 5767 FREE_LOCK(ump); 5768 return; 5769 } 5770 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5771 if (oldadp->ad_offset >= off) 5772 break; 5773 } 5774 if (oldadp == NULL) 5775 panic("softdep_setup_allocext: lost entry"); 5776 /* insert in middle of list */ 5777 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5778 if (oldadp->ad_offset == off) 5779 allocdirect_merge(adphead, adp, oldadp); 5780 FREE_LOCK(ump); 5781 } 5782 5783 /* 5784 * Indirect block allocation dependencies. 5785 * 5786 * The same dependencies that exist for a direct block also exist when 5787 * a new block is allocated and pointed to by an entry in a block of 5788 * indirect pointers. The undo/redo states described above are also 5789 * used here. Because an indirect block contains many pointers that 5790 * may have dependencies, a second copy of the entire in-memory indirect 5791 * block is kept. The buffer cache copy is always completely up-to-date. 5792 * The second copy, which is used only as a source for disk writes, 5793 * contains only the safe pointers (i.e., those that have no remaining 5794 * update dependencies). The second copy is freed when all pointers 5795 * are safe. The cache is not allowed to replace indirect blocks with 5796 * pending update dependencies. If a buffer containing an indirect 5797 * block with dependencies is written, these routines will mark it 5798 * dirty again. It can only be successfully written once all the 5799 * dependencies are removed. The ffs_fsync routine in conjunction with 5800 * softdep_sync_metadata work together to get all the dependencies 5801 * removed so that a file can be successfully written to disk. Three 5802 * procedures are used when setting up indirect block pointer 5803 * dependencies. The division is necessary because of the organization 5804 * of the "balloc" routine and because of the distinction between file 5805 * pages and file metadata blocks. 5806 */ 5807 5808 /* 5809 * Allocate a new allocindir structure. 5810 */ 5811 static struct allocindir * 5812 newallocindir(ip, ptrno, newblkno, oldblkno, lbn) 5813 struct inode *ip; /* inode for file being extended */ 5814 int ptrno; /* offset of pointer in indirect block */ 5815 ufs2_daddr_t newblkno; /* disk block number being added */ 5816 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 5817 ufs_lbn_t lbn; 5818 { 5819 struct newblk *newblk; 5820 struct allocindir *aip; 5821 struct freefrag *freefrag; 5822 struct jnewblk *jnewblk; 5823 5824 if (oldblkno) 5825 freefrag = newfreefrag(ip, oldblkno, ITOFS(ip)->fs_bsize, lbn, 5826 SINGLETON_KEY); 5827 else 5828 freefrag = NULL; 5829 ACQUIRE_LOCK(ITOUMP(ip)); 5830 if (newblk_lookup(ITOVFS(ip), newblkno, 0, &newblk) == 0) 5831 panic("new_allocindir: lost block"); 5832 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5833 ("newallocindir: newblk already initialized")); 5834 WORKITEM_REASSIGN(newblk, D_ALLOCINDIR); 5835 newblk->nb_freefrag = freefrag; 5836 aip = (struct allocindir *)newblk; 5837 aip->ai_offset = ptrno; 5838 aip->ai_oldblkno = oldblkno; 5839 aip->ai_lbn = lbn; 5840 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5841 jnewblk->jn_ino = ip->i_number; 5842 jnewblk->jn_lbn = lbn; 5843 add_to_journal(&jnewblk->jn_list); 5844 } 5845 if (freefrag && freefrag->ff_jdep != NULL && 5846 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5847 add_to_journal(freefrag->ff_jdep); 5848 return (aip); 5849 } 5850 5851 /* 5852 * Called just before setting an indirect block pointer 5853 * to a newly allocated file page. 5854 */ 5855 void 5856 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 5857 struct inode *ip; /* inode for file being extended */ 5858 ufs_lbn_t lbn; /* allocated block number within file */ 5859 struct buf *bp; /* buffer with indirect blk referencing page */ 5860 int ptrno; /* offset of pointer in indirect block */ 5861 ufs2_daddr_t newblkno; /* disk block number being added */ 5862 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 5863 struct buf *nbp; /* buffer holding allocated page */ 5864 { 5865 struct inodedep *inodedep; 5866 struct freefrag *freefrag; 5867 struct allocindir *aip; 5868 struct pagedep *pagedep; 5869 struct mount *mp; 5870 struct ufsmount *ump; 5871 5872 mp = ITOVFS(ip); 5873 ump = VFSTOUFS(mp); 5874 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5875 ("softdep_setup_allocindir_page called on non-softdep filesystem")); 5876 KASSERT(lbn == nbp->b_lblkno, 5877 ("softdep_setup_allocindir_page: lbn %jd != lblkno %jd", 5878 lbn, bp->b_lblkno)); 5879 CTR4(KTR_SUJ, 5880 "softdep_setup_allocindir_page: ino %d blkno %jd oldblkno %jd " 5881 "lbn %jd", ip->i_number, newblkno, oldblkno, lbn); 5882 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page"); 5883 aip = newallocindir(ip, ptrno, newblkno, oldblkno, lbn); 5884 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 5885 /* 5886 * If we are allocating a directory page, then we must 5887 * allocate an associated pagedep to track additions and 5888 * deletions. 5889 */ 5890 if ((ip->i_mode & IFMT) == IFDIR) 5891 pagedep_lookup(mp, nbp, ip->i_number, lbn, DEPALLOC, &pagedep); 5892 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 5893 freefrag = setup_allocindir_phase2(bp, ip, inodedep, aip, lbn); 5894 FREE_LOCK(ump); 5895 if (freefrag) 5896 handle_workitem_freefrag(freefrag); 5897 } 5898 5899 /* 5900 * Called just before setting an indirect block pointer to a 5901 * newly allocated indirect block. 5902 */ 5903 void 5904 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 5905 struct buf *nbp; /* newly allocated indirect block */ 5906 struct inode *ip; /* inode for file being extended */ 5907 struct buf *bp; /* indirect block referencing allocated block */ 5908 int ptrno; /* offset of pointer in indirect block */ 5909 ufs2_daddr_t newblkno; /* disk block number being added */ 5910 { 5911 struct inodedep *inodedep; 5912 struct allocindir *aip; 5913 struct ufsmount *ump; 5914 ufs_lbn_t lbn; 5915 5916 ump = ITOUMP(ip); 5917 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 5918 ("softdep_setup_allocindir_meta called on non-softdep filesystem")); 5919 CTR3(KTR_SUJ, 5920 "softdep_setup_allocindir_meta: ino %d blkno %jd ptrno %d", 5921 ip->i_number, newblkno, ptrno); 5922 lbn = nbp->b_lblkno; 5923 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta"); 5924 aip = newallocindir(ip, ptrno, newblkno, 0, lbn); 5925 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep); 5926 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 5927 if (setup_allocindir_phase2(bp, ip, inodedep, aip, lbn)) 5928 panic("softdep_setup_allocindir_meta: Block already existed"); 5929 FREE_LOCK(ump); 5930 } 5931 5932 static void 5933 indirdep_complete(indirdep) 5934 struct indirdep *indirdep; 5935 { 5936 struct allocindir *aip; 5937 5938 LIST_REMOVE(indirdep, ir_next); 5939 indirdep->ir_state |= DEPCOMPLETE; 5940 5941 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) { 5942 LIST_REMOVE(aip, ai_next); 5943 free_newblk(&aip->ai_block); 5944 } 5945 /* 5946 * If this indirdep is not attached to a buf it was simply waiting 5947 * on completion to clear completehd. free_indirdep() asserts 5948 * that nothing is dangling. 5949 */ 5950 if ((indirdep->ir_state & ONWORKLIST) == 0) 5951 free_indirdep(indirdep); 5952 } 5953 5954 static struct indirdep * 5955 indirdep_lookup(mp, ip, bp) 5956 struct mount *mp; 5957 struct inode *ip; 5958 struct buf *bp; 5959 { 5960 struct indirdep *indirdep, *newindirdep; 5961 struct newblk *newblk; 5962 struct ufsmount *ump; 5963 struct worklist *wk; 5964 struct fs *fs; 5965 ufs2_daddr_t blkno; 5966 5967 ump = VFSTOUFS(mp); 5968 LOCK_OWNED(ump); 5969 indirdep = NULL; 5970 newindirdep = NULL; 5971 fs = ump->um_fs; 5972 for (;;) { 5973 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 5974 if (wk->wk_type != D_INDIRDEP) 5975 continue; 5976 indirdep = WK_INDIRDEP(wk); 5977 break; 5978 } 5979 /* Found on the buffer worklist, no new structure to free. */ 5980 if (indirdep != NULL && newindirdep == NULL) 5981 return (indirdep); 5982 if (indirdep != NULL && newindirdep != NULL) 5983 panic("indirdep_lookup: simultaneous create"); 5984 /* None found on the buffer and a new structure is ready. */ 5985 if (indirdep == NULL && newindirdep != NULL) 5986 break; 5987 /* None found and no new structure available. */ 5988 FREE_LOCK(ump); 5989 newindirdep = malloc(sizeof(struct indirdep), 5990 M_INDIRDEP, M_SOFTDEP_FLAGS); 5991 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, mp); 5992 newindirdep->ir_state = ATTACHED; 5993 if (I_IS_UFS1(ip)) 5994 newindirdep->ir_state |= UFS1FMT; 5995 TAILQ_INIT(&newindirdep->ir_trunc); 5996 newindirdep->ir_saveddata = NULL; 5997 LIST_INIT(&newindirdep->ir_deplisthd); 5998 LIST_INIT(&newindirdep->ir_donehd); 5999 LIST_INIT(&newindirdep->ir_writehd); 6000 LIST_INIT(&newindirdep->ir_completehd); 6001 if (bp->b_blkno == bp->b_lblkno) { 6002 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp, 6003 NULL, NULL); 6004 bp->b_blkno = blkno; 6005 } 6006 newindirdep->ir_freeblks = NULL; 6007 newindirdep->ir_savebp = 6008 getblk(ump->um_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0); 6009 newindirdep->ir_bp = bp; 6010 BUF_KERNPROC(newindirdep->ir_savebp); 6011 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount); 6012 ACQUIRE_LOCK(ump); 6013 } 6014 indirdep = newindirdep; 6015 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list); 6016 /* 6017 * If the block is not yet allocated we don't set DEPCOMPLETE so 6018 * that we don't free dependencies until the pointers are valid. 6019 * This could search b_dep for D_ALLOCDIRECT/D_ALLOCINDIR rather 6020 * than using the hash. 6021 */ 6022 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk)) 6023 LIST_INSERT_HEAD(&newblk->nb_indirdeps, indirdep, ir_next); 6024 else 6025 indirdep->ir_state |= DEPCOMPLETE; 6026 return (indirdep); 6027 } 6028 6029 /* 6030 * Called to finish the allocation of the "aip" allocated 6031 * by one of the two routines above. 6032 */ 6033 static struct freefrag * 6034 setup_allocindir_phase2(bp, ip, inodedep, aip, lbn) 6035 struct buf *bp; /* in-memory copy of the indirect block */ 6036 struct inode *ip; /* inode for file being extended */ 6037 struct inodedep *inodedep; /* Inodedep for ip */ 6038 struct allocindir *aip; /* allocindir allocated by the above routines */ 6039 ufs_lbn_t lbn; /* Logical block number for this block. */ 6040 { 6041 struct fs *fs; 6042 struct indirdep *indirdep; 6043 struct allocindir *oldaip; 6044 struct freefrag *freefrag; 6045 struct mount *mp; 6046 struct ufsmount *ump; 6047 6048 mp = ITOVFS(ip); 6049 ump = VFSTOUFS(mp); 6050 LOCK_OWNED(ump); 6051 fs = ump->um_fs; 6052 if (bp->b_lblkno >= 0) 6053 panic("setup_allocindir_phase2: not indir blk"); 6054 KASSERT(aip->ai_offset >= 0 && aip->ai_offset < NINDIR(fs), 6055 ("setup_allocindir_phase2: Bad offset %d", aip->ai_offset)); 6056 indirdep = indirdep_lookup(mp, ip, bp); 6057 KASSERT(indirdep->ir_savebp != NULL, 6058 ("setup_allocindir_phase2 NULL ir_savebp")); 6059 aip->ai_indirdep = indirdep; 6060 /* 6061 * Check for an unwritten dependency for this indirect offset. If 6062 * there is, merge the old dependency into the new one. This happens 6063 * as a result of reallocblk only. 6064 */ 6065 freefrag = NULL; 6066 if (aip->ai_oldblkno != 0) { 6067 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) { 6068 if (oldaip->ai_offset == aip->ai_offset) { 6069 freefrag = allocindir_merge(aip, oldaip); 6070 goto done; 6071 } 6072 } 6073 LIST_FOREACH(oldaip, &indirdep->ir_donehd, ai_next) { 6074 if (oldaip->ai_offset == aip->ai_offset) { 6075 freefrag = allocindir_merge(aip, oldaip); 6076 goto done; 6077 } 6078 } 6079 } 6080 done: 6081 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next); 6082 return (freefrag); 6083 } 6084 6085 /* 6086 * Merge two allocindirs which refer to the same block. Move newblock 6087 * dependencies and setup the freefrags appropriately. 6088 */ 6089 static struct freefrag * 6090 allocindir_merge(aip, oldaip) 6091 struct allocindir *aip; 6092 struct allocindir *oldaip; 6093 { 6094 struct freefrag *freefrag; 6095 struct worklist *wk; 6096 6097 if (oldaip->ai_newblkno != aip->ai_oldblkno) 6098 panic("allocindir_merge: blkno"); 6099 aip->ai_oldblkno = oldaip->ai_oldblkno; 6100 freefrag = aip->ai_freefrag; 6101 aip->ai_freefrag = oldaip->ai_freefrag; 6102 oldaip->ai_freefrag = NULL; 6103 KASSERT(freefrag != NULL, ("setup_allocindir_phase2: No freefrag")); 6104 /* 6105 * If we are tracking a new directory-block allocation, 6106 * move it from the old allocindir to the new allocindir. 6107 */ 6108 if ((wk = LIST_FIRST(&oldaip->ai_newdirblk)) != NULL) { 6109 WORKLIST_REMOVE(wk); 6110 if (!LIST_EMPTY(&oldaip->ai_newdirblk)) 6111 panic("allocindir_merge: extra newdirblk"); 6112 WORKLIST_INSERT(&aip->ai_newdirblk, wk); 6113 } 6114 /* 6115 * We can skip journaling for this freefrag and just complete 6116 * any pending journal work for the allocindir that is being 6117 * removed after the freefrag completes. 6118 */ 6119 if (freefrag->ff_jdep) 6120 cancel_jfreefrag(WK_JFREEFRAG(freefrag->ff_jdep)); 6121 LIST_REMOVE(oldaip, ai_next); 6122 freefrag->ff_jdep = (struct worklist *)cancel_newblk(&oldaip->ai_block, 6123 &freefrag->ff_list, &freefrag->ff_jwork); 6124 free_newblk(&oldaip->ai_block); 6125 6126 return (freefrag); 6127 } 6128 6129 static inline void 6130 setup_freedirect(freeblks, ip, i, needj) 6131 struct freeblks *freeblks; 6132 struct inode *ip; 6133 int i; 6134 int needj; 6135 { 6136 struct ufsmount *ump; 6137 ufs2_daddr_t blkno; 6138 int frags; 6139 6140 blkno = DIP(ip, i_db[i]); 6141 if (blkno == 0) 6142 return; 6143 DIP_SET(ip, i_db[i], 0); 6144 ump = ITOUMP(ip); 6145 frags = sblksize(ump->um_fs, ip->i_size, i); 6146 frags = numfrags(ump->um_fs, frags); 6147 newfreework(ump, freeblks, NULL, i, blkno, frags, 0, needj); 6148 } 6149 6150 static inline void 6151 setup_freeext(freeblks, ip, i, needj) 6152 struct freeblks *freeblks; 6153 struct inode *ip; 6154 int i; 6155 int needj; 6156 { 6157 struct ufsmount *ump; 6158 ufs2_daddr_t blkno; 6159 int frags; 6160 6161 blkno = ip->i_din2->di_extb[i]; 6162 if (blkno == 0) 6163 return; 6164 ip->i_din2->di_extb[i] = 0; 6165 ump = ITOUMP(ip); 6166 frags = sblksize(ump->um_fs, ip->i_din2->di_extsize, i); 6167 frags = numfrags(ump->um_fs, frags); 6168 newfreework(ump, freeblks, NULL, -1 - i, blkno, frags, 0, needj); 6169 } 6170 6171 static inline void 6172 setup_freeindir(freeblks, ip, i, lbn, needj) 6173 struct freeblks *freeblks; 6174 struct inode *ip; 6175 int i; 6176 ufs_lbn_t lbn; 6177 int needj; 6178 { 6179 struct ufsmount *ump; 6180 ufs2_daddr_t blkno; 6181 6182 blkno = DIP(ip, i_ib[i]); 6183 if (blkno == 0) 6184 return; 6185 DIP_SET(ip, i_ib[i], 0); 6186 ump = ITOUMP(ip); 6187 newfreework(ump, freeblks, NULL, lbn, blkno, ump->um_fs->fs_frag, 6188 0, needj); 6189 } 6190 6191 static inline struct freeblks * 6192 newfreeblks(mp, ip) 6193 struct mount *mp; 6194 struct inode *ip; 6195 { 6196 struct freeblks *freeblks; 6197 6198 freeblks = malloc(sizeof(struct freeblks), 6199 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO); 6200 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp); 6201 LIST_INIT(&freeblks->fb_jblkdephd); 6202 LIST_INIT(&freeblks->fb_jwork); 6203 freeblks->fb_ref = 0; 6204 freeblks->fb_cgwait = 0; 6205 freeblks->fb_state = ATTACHED; 6206 freeblks->fb_uid = ip->i_uid; 6207 freeblks->fb_inum = ip->i_number; 6208 freeblks->fb_vtype = ITOV(ip)->v_type; 6209 freeblks->fb_modrev = DIP(ip, i_modrev); 6210 freeblks->fb_devvp = ITODEVVP(ip); 6211 freeblks->fb_chkcnt = 0; 6212 freeblks->fb_len = 0; 6213 6214 return (freeblks); 6215 } 6216 6217 static void 6218 trunc_indirdep(indirdep, freeblks, bp, off) 6219 struct indirdep *indirdep; 6220 struct freeblks *freeblks; 6221 struct buf *bp; 6222 int off; 6223 { 6224 struct allocindir *aip, *aipn; 6225 6226 /* 6227 * The first set of allocindirs won't be in savedbp. 6228 */ 6229 LIST_FOREACH_SAFE(aip, &indirdep->ir_deplisthd, ai_next, aipn) 6230 if (aip->ai_offset > off) 6231 cancel_allocindir(aip, bp, freeblks, 1); 6232 LIST_FOREACH_SAFE(aip, &indirdep->ir_donehd, ai_next, aipn) 6233 if (aip->ai_offset > off) 6234 cancel_allocindir(aip, bp, freeblks, 1); 6235 /* 6236 * These will exist in savedbp. 6237 */ 6238 LIST_FOREACH_SAFE(aip, &indirdep->ir_writehd, ai_next, aipn) 6239 if (aip->ai_offset > off) 6240 cancel_allocindir(aip, NULL, freeblks, 0); 6241 LIST_FOREACH_SAFE(aip, &indirdep->ir_completehd, ai_next, aipn) 6242 if (aip->ai_offset > off) 6243 cancel_allocindir(aip, NULL, freeblks, 0); 6244 } 6245 6246 /* 6247 * Follow the chain of indirects down to lastlbn creating a freework 6248 * structure for each. This will be used to start indir_trunc() at 6249 * the right offset and create the journal records for the parrtial 6250 * truncation. A second step will handle the truncated dependencies. 6251 */ 6252 static int 6253 setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno) 6254 struct freeblks *freeblks; 6255 struct inode *ip; 6256 ufs_lbn_t lbn; 6257 ufs_lbn_t lastlbn; 6258 ufs2_daddr_t blkno; 6259 { 6260 struct indirdep *indirdep; 6261 struct indirdep *indirn; 6262 struct freework *freework; 6263 struct newblk *newblk; 6264 struct mount *mp; 6265 struct ufsmount *ump; 6266 struct buf *bp; 6267 uint8_t *start; 6268 uint8_t *end; 6269 ufs_lbn_t lbnadd; 6270 int level; 6271 int error; 6272 int off; 6273 6274 6275 freework = NULL; 6276 if (blkno == 0) 6277 return (0); 6278 mp = freeblks->fb_list.wk_mp; 6279 ump = VFSTOUFS(mp); 6280 bp = getblk(ITOV(ip), lbn, mp->mnt_stat.f_iosize, 0, 0, 0); 6281 if ((bp->b_flags & B_CACHE) == 0) { 6282 bp->b_blkno = blkptrtodb(VFSTOUFS(mp), blkno); 6283 bp->b_iocmd = BIO_READ; 6284 bp->b_flags &= ~B_INVAL; 6285 bp->b_ioflags &= ~BIO_ERROR; 6286 vfs_busy_pages(bp, 0); 6287 bp->b_iooffset = dbtob(bp->b_blkno); 6288 bstrategy(bp); 6289 #ifdef RACCT 6290 if (racct_enable) { 6291 PROC_LOCK(curproc); 6292 racct_add_buf(curproc, bp, 0); 6293 PROC_UNLOCK(curproc); 6294 } 6295 #endif /* RACCT */ 6296 curthread->td_ru.ru_inblock++; 6297 error = bufwait(bp); 6298 if (error) { 6299 brelse(bp); 6300 return (error); 6301 } 6302 } 6303 level = lbn_level(lbn); 6304 lbnadd = lbn_offset(ump->um_fs, level); 6305 /* 6306 * Compute the offset of the last block we want to keep. Store 6307 * in the freework the first block we want to completely free. 6308 */ 6309 off = (lastlbn - -(lbn + level)) / lbnadd; 6310 if (off + 1 == NINDIR(ump->um_fs)) 6311 goto nowork; 6312 freework = newfreework(ump, freeblks, NULL, lbn, blkno, 0, off + 1, 0); 6313 /* 6314 * Link the freework into the indirdep. This will prevent any new 6315 * allocations from proceeding until we are finished with the 6316 * truncate and the block is written. 6317 */ 6318 ACQUIRE_LOCK(ump); 6319 indirdep = indirdep_lookup(mp, ip, bp); 6320 if (indirdep->ir_freeblks) 6321 panic("setup_trunc_indir: indirdep already truncated."); 6322 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, freework, fw_next); 6323 freework->fw_indir = indirdep; 6324 /* 6325 * Cancel any allocindirs that will not make it to disk. 6326 * We have to do this for all copies of the indirdep that 6327 * live on this newblk. 6328 */ 6329 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 6330 if (newblk_lookup(mp, dbtofsb(ump->um_fs, bp->b_blkno), 0, 6331 &newblk) == 0) 6332 panic("setup_trunc_indir: lost block"); 6333 LIST_FOREACH(indirn, &newblk->nb_indirdeps, ir_next) 6334 trunc_indirdep(indirn, freeblks, bp, off); 6335 } else 6336 trunc_indirdep(indirdep, freeblks, bp, off); 6337 FREE_LOCK(ump); 6338 /* 6339 * Creation is protected by the buf lock. The saveddata is only 6340 * needed if a full truncation follows a partial truncation but it 6341 * is difficult to allocate in that case so we fetch it anyway. 6342 */ 6343 if (indirdep->ir_saveddata == NULL) 6344 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 6345 M_SOFTDEP_FLAGS); 6346 nowork: 6347 /* Fetch the blkno of the child and the zero start offset. */ 6348 if (I_IS_UFS1(ip)) { 6349 blkno = ((ufs1_daddr_t *)bp->b_data)[off]; 6350 start = (uint8_t *)&((ufs1_daddr_t *)bp->b_data)[off+1]; 6351 } else { 6352 blkno = ((ufs2_daddr_t *)bp->b_data)[off]; 6353 start = (uint8_t *)&((ufs2_daddr_t *)bp->b_data)[off+1]; 6354 } 6355 if (freework) { 6356 /* Zero the truncated pointers. */ 6357 end = bp->b_data + bp->b_bcount; 6358 bzero(start, end - start); 6359 bdwrite(bp); 6360 } else 6361 bqrelse(bp); 6362 if (level == 0) 6363 return (0); 6364 lbn++; /* adjust level */ 6365 lbn -= (off * lbnadd); 6366 return setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno); 6367 } 6368 6369 /* 6370 * Complete the partial truncation of an indirect block setup by 6371 * setup_trunc_indir(). This zeros the truncated pointers in the saved 6372 * copy and writes them to disk before the freeblks is allowed to complete. 6373 */ 6374 static void 6375 complete_trunc_indir(freework) 6376 struct freework *freework; 6377 { 6378 struct freework *fwn; 6379 struct indirdep *indirdep; 6380 struct ufsmount *ump; 6381 struct buf *bp; 6382 uintptr_t start; 6383 int count; 6384 6385 ump = VFSTOUFS(freework->fw_list.wk_mp); 6386 LOCK_OWNED(ump); 6387 indirdep = freework->fw_indir; 6388 for (;;) { 6389 bp = indirdep->ir_bp; 6390 /* See if the block was discarded. */ 6391 if (bp == NULL) 6392 break; 6393 /* Inline part of getdirtybuf(). We dont want bremfree. */ 6394 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) 6395 break; 6396 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 6397 LOCK_PTR(ump)) == 0) 6398 BUF_UNLOCK(bp); 6399 ACQUIRE_LOCK(ump); 6400 } 6401 freework->fw_state |= DEPCOMPLETE; 6402 TAILQ_REMOVE(&indirdep->ir_trunc, freework, fw_next); 6403 /* 6404 * Zero the pointers in the saved copy. 6405 */ 6406 if (indirdep->ir_state & UFS1FMT) 6407 start = sizeof(ufs1_daddr_t); 6408 else 6409 start = sizeof(ufs2_daddr_t); 6410 start *= freework->fw_start; 6411 count = indirdep->ir_savebp->b_bcount - start; 6412 start += (uintptr_t)indirdep->ir_savebp->b_data; 6413 bzero((char *)start, count); 6414 /* 6415 * We need to start the next truncation in the list if it has not 6416 * been started yet. 6417 */ 6418 fwn = TAILQ_FIRST(&indirdep->ir_trunc); 6419 if (fwn != NULL) { 6420 if (fwn->fw_freeblks == indirdep->ir_freeblks) 6421 TAILQ_REMOVE(&indirdep->ir_trunc, fwn, fw_next); 6422 if ((fwn->fw_state & ONWORKLIST) == 0) 6423 freework_enqueue(fwn); 6424 } 6425 /* 6426 * If bp is NULL the block was fully truncated, restore 6427 * the saved block list otherwise free it if it is no 6428 * longer needed. 6429 */ 6430 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 6431 if (bp == NULL) 6432 bcopy(indirdep->ir_saveddata, 6433 indirdep->ir_savebp->b_data, 6434 indirdep->ir_savebp->b_bcount); 6435 free(indirdep->ir_saveddata, M_INDIRDEP); 6436 indirdep->ir_saveddata = NULL; 6437 } 6438 /* 6439 * When bp is NULL there is a full truncation pending. We 6440 * must wait for this full truncation to be journaled before 6441 * we can release this freework because the disk pointers will 6442 * never be written as zero. 6443 */ 6444 if (bp == NULL) { 6445 if (LIST_EMPTY(&indirdep->ir_freeblks->fb_jblkdephd)) 6446 handle_written_freework(freework); 6447 else 6448 WORKLIST_INSERT(&indirdep->ir_freeblks->fb_freeworkhd, 6449 &freework->fw_list); 6450 } else { 6451 /* Complete when the real copy is written. */ 6452 WORKLIST_INSERT(&bp->b_dep, &freework->fw_list); 6453 BUF_UNLOCK(bp); 6454 } 6455 } 6456 6457 /* 6458 * Calculate the number of blocks we are going to release where datablocks 6459 * is the current total and length is the new file size. 6460 */ 6461 static ufs2_daddr_t 6462 blkcount(fs, datablocks, length) 6463 struct fs *fs; 6464 ufs2_daddr_t datablocks; 6465 off_t length; 6466 { 6467 off_t totblks, numblks; 6468 6469 totblks = 0; 6470 numblks = howmany(length, fs->fs_bsize); 6471 if (numblks <= UFS_NDADDR) { 6472 totblks = howmany(length, fs->fs_fsize); 6473 goto out; 6474 } 6475 totblks = blkstofrags(fs, numblks); 6476 numblks -= UFS_NDADDR; 6477 /* 6478 * Count all single, then double, then triple indirects required. 6479 * Subtracting one indirects worth of blocks for each pass 6480 * acknowledges one of each pointed to by the inode. 6481 */ 6482 for (;;) { 6483 totblks += blkstofrags(fs, howmany(numblks, NINDIR(fs))); 6484 numblks -= NINDIR(fs); 6485 if (numblks <= 0) 6486 break; 6487 numblks = howmany(numblks, NINDIR(fs)); 6488 } 6489 out: 6490 totblks = fsbtodb(fs, totblks); 6491 /* 6492 * Handle sparse files. We can't reclaim more blocks than the inode 6493 * references. We will correct it later in handle_complete_freeblks() 6494 * when we know the real count. 6495 */ 6496 if (totblks > datablocks) 6497 return (0); 6498 return (datablocks - totblks); 6499 } 6500 6501 /* 6502 * Handle freeblocks for journaled softupdate filesystems. 6503 * 6504 * Contrary to normal softupdates, we must preserve the block pointers in 6505 * indirects until their subordinates are free. This is to avoid journaling 6506 * every block that is freed which may consume more space than the journal 6507 * itself. The recovery program will see the free block journals at the 6508 * base of the truncated area and traverse them to reclaim space. The 6509 * pointers in the inode may be cleared immediately after the journal 6510 * records are written because each direct and indirect pointer in the 6511 * inode is recorded in a journal. This permits full truncation to proceed 6512 * asynchronously. The write order is journal -> inode -> cgs -> indirects. 6513 * 6514 * The algorithm is as follows: 6515 * 1) Traverse the in-memory state and create journal entries to release 6516 * the relevant blocks and full indirect trees. 6517 * 2) Traverse the indirect block chain adding partial truncation freework 6518 * records to indirects in the path to lastlbn. The freework will 6519 * prevent new allocation dependencies from being satisfied in this 6520 * indirect until the truncation completes. 6521 * 3) Read and lock the inode block, performing an update with the new size 6522 * and pointers. This prevents truncated data from becoming valid on 6523 * disk through step 4. 6524 * 4) Reap unsatisfied dependencies that are beyond the truncated area, 6525 * eliminate journal work for those records that do not require it. 6526 * 5) Schedule the journal records to be written followed by the inode block. 6527 * 6) Allocate any necessary frags for the end of file. 6528 * 7) Zero any partially truncated blocks. 6529 * 6530 * From this truncation proceeds asynchronously using the freework and 6531 * indir_trunc machinery. The file will not be extended again into a 6532 * partially truncated indirect block until all work is completed but 6533 * the normal dependency mechanism ensures that it is rolled back/forward 6534 * as appropriate. Further truncation may occur without delay and is 6535 * serialized in indir_trunc(). 6536 */ 6537 void 6538 softdep_journal_freeblocks(ip, cred, length, flags) 6539 struct inode *ip; /* The inode whose length is to be reduced */ 6540 struct ucred *cred; 6541 off_t length; /* The new length for the file */ 6542 int flags; /* IO_EXT and/or IO_NORMAL */ 6543 { 6544 struct freeblks *freeblks, *fbn; 6545 struct worklist *wk, *wkn; 6546 struct inodedep *inodedep; 6547 struct jblkdep *jblkdep; 6548 struct allocdirect *adp, *adpn; 6549 struct ufsmount *ump; 6550 struct fs *fs; 6551 struct buf *bp; 6552 struct vnode *vp; 6553 struct mount *mp; 6554 ufs2_daddr_t extblocks, datablocks; 6555 ufs_lbn_t tmpval, lbn, lastlbn; 6556 int frags, lastoff, iboff, allocblock, needj, error, i; 6557 6558 ump = ITOUMP(ip); 6559 mp = UFSTOVFS(ump); 6560 fs = ump->um_fs; 6561 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6562 ("softdep_journal_freeblocks called on non-softdep filesystem")); 6563 vp = ITOV(ip); 6564 needj = 1; 6565 iboff = -1; 6566 allocblock = 0; 6567 extblocks = 0; 6568 datablocks = 0; 6569 frags = 0; 6570 freeblks = newfreeblks(mp, ip); 6571 ACQUIRE_LOCK(ump); 6572 /* 6573 * If we're truncating a removed file that will never be written 6574 * we don't need to journal the block frees. The canceled journals 6575 * for the allocations will suffice. 6576 */ 6577 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6578 if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED && 6579 length == 0) 6580 needj = 0; 6581 CTR3(KTR_SUJ, "softdep_journal_freeblks: ip %d length %ld needj %d", 6582 ip->i_number, length, needj); 6583 FREE_LOCK(ump); 6584 /* 6585 * Calculate the lbn that we are truncating to. This results in -1 6586 * if we're truncating the 0 bytes. So it is the last lbn we want 6587 * to keep, not the first lbn we want to truncate. 6588 */ 6589 lastlbn = lblkno(fs, length + fs->fs_bsize - 1) - 1; 6590 lastoff = blkoff(fs, length); 6591 /* 6592 * Compute frags we are keeping in lastlbn. 0 means all. 6593 */ 6594 if (lastlbn >= 0 && lastlbn < UFS_NDADDR) { 6595 frags = fragroundup(fs, lastoff); 6596 /* adp offset of last valid allocdirect. */ 6597 iboff = lastlbn; 6598 } else if (lastlbn > 0) 6599 iboff = UFS_NDADDR; 6600 if (fs->fs_magic == FS_UFS2_MAGIC) 6601 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 6602 /* 6603 * Handle normal data blocks and indirects. This section saves 6604 * values used after the inode update to complete frag and indirect 6605 * truncation. 6606 */ 6607 if ((flags & IO_NORMAL) != 0) { 6608 /* 6609 * Handle truncation of whole direct and indirect blocks. 6610 */ 6611 for (i = iboff + 1; i < UFS_NDADDR; i++) 6612 setup_freedirect(freeblks, ip, i, needj); 6613 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR; 6614 i < UFS_NIADDR; 6615 i++, lbn += tmpval, tmpval *= NINDIR(fs)) { 6616 /* Release a whole indirect tree. */ 6617 if (lbn > lastlbn) { 6618 setup_freeindir(freeblks, ip, i, -lbn -i, 6619 needj); 6620 continue; 6621 } 6622 iboff = i + UFS_NDADDR; 6623 /* 6624 * Traverse partially truncated indirect tree. 6625 */ 6626 if (lbn <= lastlbn && lbn + tmpval - 1 > lastlbn) 6627 setup_trunc_indir(freeblks, ip, -lbn - i, 6628 lastlbn, DIP(ip, i_ib[i])); 6629 } 6630 /* 6631 * Handle partial truncation to a frag boundary. 6632 */ 6633 if (frags) { 6634 ufs2_daddr_t blkno; 6635 long oldfrags; 6636 6637 oldfrags = blksize(fs, ip, lastlbn); 6638 blkno = DIP(ip, i_db[lastlbn]); 6639 if (blkno && oldfrags != frags) { 6640 oldfrags -= frags; 6641 oldfrags = numfrags(fs, oldfrags); 6642 blkno += numfrags(fs, frags); 6643 newfreework(ump, freeblks, NULL, lastlbn, 6644 blkno, oldfrags, 0, needj); 6645 if (needj) 6646 adjust_newfreework(freeblks, 6647 numfrags(fs, frags)); 6648 } else if (blkno == 0) 6649 allocblock = 1; 6650 } 6651 /* 6652 * Add a journal record for partial truncate if we are 6653 * handling indirect blocks. Non-indirects need no extra 6654 * journaling. 6655 */ 6656 if (length != 0 && lastlbn >= UFS_NDADDR) { 6657 ip->i_flag |= IN_TRUNCATED; 6658 newjtrunc(freeblks, length, 0); 6659 } 6660 ip->i_size = length; 6661 DIP_SET(ip, i_size, ip->i_size); 6662 datablocks = DIP(ip, i_blocks) - extblocks; 6663 if (length != 0) 6664 datablocks = blkcount(fs, datablocks, length); 6665 freeblks->fb_len = length; 6666 } 6667 if ((flags & IO_EXT) != 0) { 6668 for (i = 0; i < UFS_NXADDR; i++) 6669 setup_freeext(freeblks, ip, i, needj); 6670 ip->i_din2->di_extsize = 0; 6671 datablocks += extblocks; 6672 } 6673 #ifdef QUOTA 6674 /* Reference the quotas in case the block count is wrong in the end. */ 6675 quotaref(vp, freeblks->fb_quota); 6676 (void) chkdq(ip, -datablocks, NOCRED, 0); 6677 #endif 6678 freeblks->fb_chkcnt = -datablocks; 6679 UFS_LOCK(ump); 6680 fs->fs_pendingblocks += datablocks; 6681 UFS_UNLOCK(ump); 6682 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 6683 /* 6684 * Handle truncation of incomplete alloc direct dependencies. We 6685 * hold the inode block locked to prevent incomplete dependencies 6686 * from reaching the disk while we are eliminating those that 6687 * have been truncated. This is a partially inlined ffs_update(). 6688 */ 6689 ufs_itimes(vp); 6690 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED); 6691 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 6692 (int)fs->fs_bsize, cred, &bp); 6693 if (error) { 6694 brelse(bp); 6695 softdep_error("softdep_journal_freeblocks", error); 6696 return; 6697 } 6698 if (bp->b_bufsize == fs->fs_bsize) 6699 bp->b_flags |= B_CLUSTEROK; 6700 softdep_update_inodeblock(ip, bp, 0); 6701 if (ump->um_fstype == UFS1) { 6702 *((struct ufs1_dinode *)bp->b_data + 6703 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1; 6704 } else { 6705 *((struct ufs2_dinode *)bp->b_data + 6706 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2; 6707 } 6708 ACQUIRE_LOCK(ump); 6709 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6710 if ((inodedep->id_state & IOSTARTED) != 0) 6711 panic("softdep_setup_freeblocks: inode busy"); 6712 /* 6713 * Add the freeblks structure to the list of operations that 6714 * must await the zero'ed inode being written to disk. If we 6715 * still have a bitmap dependency (needj), then the inode 6716 * has never been written to disk, so we can process the 6717 * freeblks below once we have deleted the dependencies. 6718 */ 6719 if (needj) 6720 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 6721 else 6722 freeblks->fb_state |= COMPLETE; 6723 if ((flags & IO_NORMAL) != 0) { 6724 TAILQ_FOREACH_SAFE(adp, &inodedep->id_inoupdt, ad_next, adpn) { 6725 if (adp->ad_offset > iboff) 6726 cancel_allocdirect(&inodedep->id_inoupdt, adp, 6727 freeblks); 6728 /* 6729 * Truncate the allocdirect. We could eliminate 6730 * or modify journal records as well. 6731 */ 6732 else if (adp->ad_offset == iboff && frags) 6733 adp->ad_newsize = frags; 6734 } 6735 } 6736 if ((flags & IO_EXT) != 0) 6737 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 6738 cancel_allocdirect(&inodedep->id_extupdt, adp, 6739 freeblks); 6740 /* 6741 * Scan the bufwait list for newblock dependencies that will never 6742 * make it to disk. 6743 */ 6744 LIST_FOREACH_SAFE(wk, &inodedep->id_bufwait, wk_list, wkn) { 6745 if (wk->wk_type != D_ALLOCDIRECT) 6746 continue; 6747 adp = WK_ALLOCDIRECT(wk); 6748 if (((flags & IO_NORMAL) != 0 && (adp->ad_offset > iboff)) || 6749 ((flags & IO_EXT) != 0 && (adp->ad_state & EXTDATA))) { 6750 cancel_jfreeblk(freeblks, adp->ad_newblkno); 6751 cancel_newblk(WK_NEWBLK(wk), NULL, &freeblks->fb_jwork); 6752 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 6753 } 6754 } 6755 /* 6756 * Add journal work. 6757 */ 6758 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) 6759 add_to_journal(&jblkdep->jb_list); 6760 FREE_LOCK(ump); 6761 bdwrite(bp); 6762 /* 6763 * Truncate dependency structures beyond length. 6764 */ 6765 trunc_dependencies(ip, freeblks, lastlbn, frags, flags); 6766 /* 6767 * This is only set when we need to allocate a fragment because 6768 * none existed at the end of a frag-sized file. It handles only 6769 * allocating a new, zero filled block. 6770 */ 6771 if (allocblock) { 6772 ip->i_size = length - lastoff; 6773 DIP_SET(ip, i_size, ip->i_size); 6774 error = UFS_BALLOC(vp, length - 1, 1, cred, BA_CLRBUF, &bp); 6775 if (error != 0) { 6776 softdep_error("softdep_journal_freeblks", error); 6777 return; 6778 } 6779 ip->i_size = length; 6780 DIP_SET(ip, i_size, length); 6781 ip->i_flag |= IN_CHANGE | IN_UPDATE; 6782 allocbuf(bp, frags); 6783 ffs_update(vp, 0); 6784 bawrite(bp); 6785 } else if (lastoff != 0 && vp->v_type != VDIR) { 6786 int size; 6787 6788 /* 6789 * Zero the end of a truncated frag or block. 6790 */ 6791 size = sblksize(fs, length, lastlbn); 6792 error = bread(vp, lastlbn, size, cred, &bp); 6793 if (error) { 6794 softdep_error("softdep_journal_freeblks", error); 6795 return; 6796 } 6797 bzero((char *)bp->b_data + lastoff, size - lastoff); 6798 bawrite(bp); 6799 6800 } 6801 ACQUIRE_LOCK(ump); 6802 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6803 TAILQ_INSERT_TAIL(&inodedep->id_freeblklst, freeblks, fb_next); 6804 freeblks->fb_state |= DEPCOMPLETE | ONDEPLIST; 6805 /* 6806 * We zero earlier truncations so they don't erroneously 6807 * update i_blocks. 6808 */ 6809 if (freeblks->fb_len == 0 && (flags & IO_NORMAL) != 0) 6810 TAILQ_FOREACH(fbn, &inodedep->id_freeblklst, fb_next) 6811 fbn->fb_len = 0; 6812 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE && 6813 LIST_EMPTY(&freeblks->fb_jblkdephd)) 6814 freeblks->fb_state |= INPROGRESS; 6815 else 6816 freeblks = NULL; 6817 FREE_LOCK(ump); 6818 if (freeblks) 6819 handle_workitem_freeblocks(freeblks, 0); 6820 trunc_pages(ip, length, extblocks, flags); 6821 6822 } 6823 6824 /* 6825 * Flush a JOP_SYNC to the journal. 6826 */ 6827 void 6828 softdep_journal_fsync(ip) 6829 struct inode *ip; 6830 { 6831 struct jfsync *jfsync; 6832 struct ufsmount *ump; 6833 6834 ump = ITOUMP(ip); 6835 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 6836 ("softdep_journal_fsync called on non-softdep filesystem")); 6837 if ((ip->i_flag & IN_TRUNCATED) == 0) 6838 return; 6839 ip->i_flag &= ~IN_TRUNCATED; 6840 jfsync = malloc(sizeof(*jfsync), M_JFSYNC, M_SOFTDEP_FLAGS | M_ZERO); 6841 workitem_alloc(&jfsync->jfs_list, D_JFSYNC, UFSTOVFS(ump)); 6842 jfsync->jfs_size = ip->i_size; 6843 jfsync->jfs_ino = ip->i_number; 6844 ACQUIRE_LOCK(ump); 6845 add_to_journal(&jfsync->jfs_list); 6846 jwait(&jfsync->jfs_list, MNT_WAIT); 6847 FREE_LOCK(ump); 6848 } 6849 6850 /* 6851 * Block de-allocation dependencies. 6852 * 6853 * When blocks are de-allocated, the on-disk pointers must be nullified before 6854 * the blocks are made available for use by other files. (The true 6855 * requirement is that old pointers must be nullified before new on-disk 6856 * pointers are set. We chose this slightly more stringent requirement to 6857 * reduce complexity.) Our implementation handles this dependency by updating 6858 * the inode (or indirect block) appropriately but delaying the actual block 6859 * de-allocation (i.e., freemap and free space count manipulation) until 6860 * after the updated versions reach stable storage. After the disk is 6861 * updated, the blocks can be safely de-allocated whenever it is convenient. 6862 * This implementation handles only the common case of reducing a file's 6863 * length to zero. Other cases are handled by the conventional synchronous 6864 * write approach. 6865 * 6866 * The ffs implementation with which we worked double-checks 6867 * the state of the block pointers and file size as it reduces 6868 * a file's length. Some of this code is replicated here in our 6869 * soft updates implementation. The freeblks->fb_chkcnt field is 6870 * used to transfer a part of this information to the procedure 6871 * that eventually de-allocates the blocks. 6872 * 6873 * This routine should be called from the routine that shortens 6874 * a file's length, before the inode's size or block pointers 6875 * are modified. It will save the block pointer information for 6876 * later release and zero the inode so that the calling routine 6877 * can release it. 6878 */ 6879 void 6880 softdep_setup_freeblocks(ip, length, flags) 6881 struct inode *ip; /* The inode whose length is to be reduced */ 6882 off_t length; /* The new length for the file */ 6883 int flags; /* IO_EXT and/or IO_NORMAL */ 6884 { 6885 struct ufs1_dinode *dp1; 6886 struct ufs2_dinode *dp2; 6887 struct freeblks *freeblks; 6888 struct inodedep *inodedep; 6889 struct allocdirect *adp; 6890 struct ufsmount *ump; 6891 struct buf *bp; 6892 struct fs *fs; 6893 ufs2_daddr_t extblocks, datablocks; 6894 struct mount *mp; 6895 int i, delay, error; 6896 ufs_lbn_t tmpval; 6897 ufs_lbn_t lbn; 6898 6899 ump = ITOUMP(ip); 6900 mp = UFSTOVFS(ump); 6901 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6902 ("softdep_setup_freeblocks called on non-softdep filesystem")); 6903 CTR2(KTR_SUJ, "softdep_setup_freeblks: ip %d length %ld", 6904 ip->i_number, length); 6905 KASSERT(length == 0, ("softdep_setup_freeblocks: non-zero length")); 6906 fs = ump->um_fs; 6907 if ((error = bread(ump->um_devvp, 6908 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 6909 (int)fs->fs_bsize, NOCRED, &bp)) != 0) { 6910 brelse(bp); 6911 softdep_error("softdep_setup_freeblocks", error); 6912 return; 6913 } 6914 freeblks = newfreeblks(mp, ip); 6915 extblocks = 0; 6916 datablocks = 0; 6917 if (fs->fs_magic == FS_UFS2_MAGIC) 6918 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 6919 if ((flags & IO_NORMAL) != 0) { 6920 for (i = 0; i < UFS_NDADDR; i++) 6921 setup_freedirect(freeblks, ip, i, 0); 6922 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR; 6923 i < UFS_NIADDR; 6924 i++, lbn += tmpval, tmpval *= NINDIR(fs)) 6925 setup_freeindir(freeblks, ip, i, -lbn -i, 0); 6926 ip->i_size = 0; 6927 DIP_SET(ip, i_size, 0); 6928 datablocks = DIP(ip, i_blocks) - extblocks; 6929 } 6930 if ((flags & IO_EXT) != 0) { 6931 for (i = 0; i < UFS_NXADDR; i++) 6932 setup_freeext(freeblks, ip, i, 0); 6933 ip->i_din2->di_extsize = 0; 6934 datablocks += extblocks; 6935 } 6936 #ifdef QUOTA 6937 /* Reference the quotas in case the block count is wrong in the end. */ 6938 quotaref(ITOV(ip), freeblks->fb_quota); 6939 (void) chkdq(ip, -datablocks, NOCRED, 0); 6940 #endif 6941 freeblks->fb_chkcnt = -datablocks; 6942 UFS_LOCK(ump); 6943 fs->fs_pendingblocks += datablocks; 6944 UFS_UNLOCK(ump); 6945 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 6946 /* 6947 * Push the zero'ed inode to its disk buffer so that we are free 6948 * to delete its dependencies below. Once the dependencies are gone 6949 * the buffer can be safely released. 6950 */ 6951 if (ump->um_fstype == UFS1) { 6952 dp1 = ((struct ufs1_dinode *)bp->b_data + 6953 ino_to_fsbo(fs, ip->i_number)); 6954 ip->i_din1->di_freelink = dp1->di_freelink; 6955 *dp1 = *ip->i_din1; 6956 } else { 6957 dp2 = ((struct ufs2_dinode *)bp->b_data + 6958 ino_to_fsbo(fs, ip->i_number)); 6959 ip->i_din2->di_freelink = dp2->di_freelink; 6960 *dp2 = *ip->i_din2; 6961 } 6962 /* 6963 * Find and eliminate any inode dependencies. 6964 */ 6965 ACQUIRE_LOCK(ump); 6966 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6967 if ((inodedep->id_state & IOSTARTED) != 0) 6968 panic("softdep_setup_freeblocks: inode busy"); 6969 /* 6970 * Add the freeblks structure to the list of operations that 6971 * must await the zero'ed inode being written to disk. If we 6972 * still have a bitmap dependency (delay == 0), then the inode 6973 * has never been written to disk, so we can process the 6974 * freeblks below once we have deleted the dependencies. 6975 */ 6976 delay = (inodedep->id_state & DEPCOMPLETE); 6977 if (delay) 6978 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 6979 else 6980 freeblks->fb_state |= COMPLETE; 6981 /* 6982 * Because the file length has been truncated to zero, any 6983 * pending block allocation dependency structures associated 6984 * with this inode are obsolete and can simply be de-allocated. 6985 * We must first merge the two dependency lists to get rid of 6986 * any duplicate freefrag structures, then purge the merged list. 6987 * If we still have a bitmap dependency, then the inode has never 6988 * been written to disk, so we can free any fragments without delay. 6989 */ 6990 if (flags & IO_NORMAL) { 6991 merge_inode_lists(&inodedep->id_newinoupdt, 6992 &inodedep->id_inoupdt); 6993 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 6994 cancel_allocdirect(&inodedep->id_inoupdt, adp, 6995 freeblks); 6996 } 6997 if (flags & IO_EXT) { 6998 merge_inode_lists(&inodedep->id_newextupdt, 6999 &inodedep->id_extupdt); 7000 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 7001 cancel_allocdirect(&inodedep->id_extupdt, adp, 7002 freeblks); 7003 } 7004 FREE_LOCK(ump); 7005 bdwrite(bp); 7006 trunc_dependencies(ip, freeblks, -1, 0, flags); 7007 ACQUIRE_LOCK(ump); 7008 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 7009 (void) free_inodedep(inodedep); 7010 freeblks->fb_state |= DEPCOMPLETE; 7011 /* 7012 * If the inode with zeroed block pointers is now on disk 7013 * we can start freeing blocks. 7014 */ 7015 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 7016 freeblks->fb_state |= INPROGRESS; 7017 else 7018 freeblks = NULL; 7019 FREE_LOCK(ump); 7020 if (freeblks) 7021 handle_workitem_freeblocks(freeblks, 0); 7022 trunc_pages(ip, length, extblocks, flags); 7023 } 7024 7025 /* 7026 * Eliminate pages from the page cache that back parts of this inode and 7027 * adjust the vnode pager's idea of our size. This prevents stale data 7028 * from hanging around in the page cache. 7029 */ 7030 static void 7031 trunc_pages(ip, length, extblocks, flags) 7032 struct inode *ip; 7033 off_t length; 7034 ufs2_daddr_t extblocks; 7035 int flags; 7036 { 7037 struct vnode *vp; 7038 struct fs *fs; 7039 ufs_lbn_t lbn; 7040 off_t end, extend; 7041 7042 vp = ITOV(ip); 7043 fs = ITOFS(ip); 7044 extend = OFF_TO_IDX(lblktosize(fs, -extblocks)); 7045 if ((flags & IO_EXT) != 0) 7046 vn_pages_remove(vp, extend, 0); 7047 if ((flags & IO_NORMAL) == 0) 7048 return; 7049 BO_LOCK(&vp->v_bufobj); 7050 drain_output(vp); 7051 BO_UNLOCK(&vp->v_bufobj); 7052 /* 7053 * The vnode pager eliminates file pages we eliminate indirects 7054 * below. 7055 */ 7056 vnode_pager_setsize(vp, length); 7057 /* 7058 * Calculate the end based on the last indirect we want to keep. If 7059 * the block extends into indirects we can just use the negative of 7060 * its lbn. Doubles and triples exist at lower numbers so we must 7061 * be careful not to remove those, if they exist. double and triple 7062 * indirect lbns do not overlap with others so it is not important 7063 * to verify how many levels are required. 7064 */ 7065 lbn = lblkno(fs, length); 7066 if (lbn >= UFS_NDADDR) { 7067 /* Calculate the virtual lbn of the triple indirect. */ 7068 lbn = -lbn - (UFS_NIADDR - 1); 7069 end = OFF_TO_IDX(lblktosize(fs, lbn)); 7070 } else 7071 end = extend; 7072 vn_pages_remove(vp, OFF_TO_IDX(OFF_MAX), end); 7073 } 7074 7075 /* 7076 * See if the buf bp is in the range eliminated by truncation. 7077 */ 7078 static int 7079 trunc_check_buf(bp, blkoffp, lastlbn, lastoff, flags) 7080 struct buf *bp; 7081 int *blkoffp; 7082 ufs_lbn_t lastlbn; 7083 int lastoff; 7084 int flags; 7085 { 7086 ufs_lbn_t lbn; 7087 7088 *blkoffp = 0; 7089 /* Only match ext/normal blocks as appropriate. */ 7090 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) || 7091 ((flags & IO_NORMAL) == 0 && (bp->b_xflags & BX_ALTDATA) == 0)) 7092 return (0); 7093 /* ALTDATA is always a full truncation. */ 7094 if ((bp->b_xflags & BX_ALTDATA) != 0) 7095 return (1); 7096 /* -1 is full truncation. */ 7097 if (lastlbn == -1) 7098 return (1); 7099 /* 7100 * If this is a partial truncate we only want those 7101 * blocks and indirect blocks that cover the range 7102 * we're after. 7103 */ 7104 lbn = bp->b_lblkno; 7105 if (lbn < 0) 7106 lbn = -(lbn + lbn_level(lbn)); 7107 if (lbn < lastlbn) 7108 return (0); 7109 /* Here we only truncate lblkno if it's partial. */ 7110 if (lbn == lastlbn) { 7111 if (lastoff == 0) 7112 return (0); 7113 *blkoffp = lastoff; 7114 } 7115 return (1); 7116 } 7117 7118 /* 7119 * Eliminate any dependencies that exist in memory beyond lblkno:off 7120 */ 7121 static void 7122 trunc_dependencies(ip, freeblks, lastlbn, lastoff, flags) 7123 struct inode *ip; 7124 struct freeblks *freeblks; 7125 ufs_lbn_t lastlbn; 7126 int lastoff; 7127 int flags; 7128 { 7129 struct bufobj *bo; 7130 struct vnode *vp; 7131 struct buf *bp; 7132 int blkoff; 7133 7134 /* 7135 * We must wait for any I/O in progress to finish so that 7136 * all potential buffers on the dirty list will be visible. 7137 * Once they are all there, walk the list and get rid of 7138 * any dependencies. 7139 */ 7140 vp = ITOV(ip); 7141 bo = &vp->v_bufobj; 7142 BO_LOCK(bo); 7143 drain_output(vp); 7144 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) 7145 bp->b_vflags &= ~BV_SCANNED; 7146 restart: 7147 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) { 7148 if (bp->b_vflags & BV_SCANNED) 7149 continue; 7150 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 7151 bp->b_vflags |= BV_SCANNED; 7152 continue; 7153 } 7154 KASSERT(bp->b_bufobj == bo, ("Wrong object in buffer")); 7155 if ((bp = getdirtybuf(bp, BO_LOCKPTR(bo), MNT_WAIT)) == NULL) 7156 goto restart; 7157 BO_UNLOCK(bo); 7158 if (deallocate_dependencies(bp, freeblks, blkoff)) 7159 bqrelse(bp); 7160 else 7161 brelse(bp); 7162 BO_LOCK(bo); 7163 goto restart; 7164 } 7165 /* 7166 * Now do the work of vtruncbuf while also matching indirect blocks. 7167 */ 7168 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) 7169 bp->b_vflags &= ~BV_SCANNED; 7170 cleanrestart: 7171 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) { 7172 if (bp->b_vflags & BV_SCANNED) 7173 continue; 7174 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 7175 bp->b_vflags |= BV_SCANNED; 7176 continue; 7177 } 7178 if (BUF_LOCK(bp, 7179 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 7180 BO_LOCKPTR(bo)) == ENOLCK) { 7181 BO_LOCK(bo); 7182 goto cleanrestart; 7183 } 7184 bp->b_vflags |= BV_SCANNED; 7185 bremfree(bp); 7186 if (blkoff != 0) { 7187 allocbuf(bp, blkoff); 7188 bqrelse(bp); 7189 } else { 7190 bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF; 7191 brelse(bp); 7192 } 7193 BO_LOCK(bo); 7194 goto cleanrestart; 7195 } 7196 drain_output(vp); 7197 BO_UNLOCK(bo); 7198 } 7199 7200 static int 7201 cancel_pagedep(pagedep, freeblks, blkoff) 7202 struct pagedep *pagedep; 7203 struct freeblks *freeblks; 7204 int blkoff; 7205 { 7206 struct jremref *jremref; 7207 struct jmvref *jmvref; 7208 struct dirrem *dirrem, *tmp; 7209 int i; 7210 7211 /* 7212 * Copy any directory remove dependencies to the list 7213 * to be processed after the freeblks proceeds. If 7214 * directory entry never made it to disk they 7215 * can be dumped directly onto the work list. 7216 */ 7217 LIST_FOREACH_SAFE(dirrem, &pagedep->pd_dirremhd, dm_next, tmp) { 7218 /* Skip this directory removal if it is intended to remain. */ 7219 if (dirrem->dm_offset < blkoff) 7220 continue; 7221 /* 7222 * If there are any dirrems we wait for the journal write 7223 * to complete and then restart the buf scan as the lock 7224 * has been dropped. 7225 */ 7226 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) { 7227 jwait(&jremref->jr_list, MNT_WAIT); 7228 return (ERESTART); 7229 } 7230 LIST_REMOVE(dirrem, dm_next); 7231 dirrem->dm_dirinum = pagedep->pd_ino; 7232 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list); 7233 } 7234 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) { 7235 jwait(&jmvref->jm_list, MNT_WAIT); 7236 return (ERESTART); 7237 } 7238 /* 7239 * When we're partially truncating a pagedep we just want to flush 7240 * journal entries and return. There can not be any adds in the 7241 * truncated portion of the directory and newblk must remain if 7242 * part of the block remains. 7243 */ 7244 if (blkoff != 0) { 7245 struct diradd *dap; 7246 7247 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 7248 if (dap->da_offset > blkoff) 7249 panic("cancel_pagedep: diradd %p off %d > %d", 7250 dap, dap->da_offset, blkoff); 7251 for (i = 0; i < DAHASHSZ; i++) 7252 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) 7253 if (dap->da_offset > blkoff) 7254 panic("cancel_pagedep: diradd %p off %d > %d", 7255 dap, dap->da_offset, blkoff); 7256 return (0); 7257 } 7258 /* 7259 * There should be no directory add dependencies present 7260 * as the directory could not be truncated until all 7261 * children were removed. 7262 */ 7263 KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL, 7264 ("deallocate_dependencies: pendinghd != NULL")); 7265 for (i = 0; i < DAHASHSZ; i++) 7266 KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL, 7267 ("deallocate_dependencies: diraddhd != NULL")); 7268 if ((pagedep->pd_state & NEWBLOCK) != 0) 7269 free_newdirblk(pagedep->pd_newdirblk); 7270 if (free_pagedep(pagedep) == 0) 7271 panic("Failed to free pagedep %p", pagedep); 7272 return (0); 7273 } 7274 7275 /* 7276 * Reclaim any dependency structures from a buffer that is about to 7277 * be reallocated to a new vnode. The buffer must be locked, thus, 7278 * no I/O completion operations can occur while we are manipulating 7279 * its associated dependencies. The mutex is held so that other I/O's 7280 * associated with related dependencies do not occur. 7281 */ 7282 static int 7283 deallocate_dependencies(bp, freeblks, off) 7284 struct buf *bp; 7285 struct freeblks *freeblks; 7286 int off; 7287 { 7288 struct indirdep *indirdep; 7289 struct pagedep *pagedep; 7290 struct worklist *wk, *wkn; 7291 struct ufsmount *ump; 7292 7293 ump = softdep_bp_to_mp(bp); 7294 if (ump == NULL) 7295 goto done; 7296 ACQUIRE_LOCK(ump); 7297 LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) { 7298 switch (wk->wk_type) { 7299 case D_INDIRDEP: 7300 indirdep = WK_INDIRDEP(wk); 7301 if (bp->b_lblkno >= 0 || 7302 bp->b_blkno != indirdep->ir_savebp->b_lblkno) 7303 panic("deallocate_dependencies: not indir"); 7304 cancel_indirdep(indirdep, bp, freeblks); 7305 continue; 7306 7307 case D_PAGEDEP: 7308 pagedep = WK_PAGEDEP(wk); 7309 if (cancel_pagedep(pagedep, freeblks, off)) { 7310 FREE_LOCK(ump); 7311 return (ERESTART); 7312 } 7313 continue; 7314 7315 case D_ALLOCINDIR: 7316 /* 7317 * Simply remove the allocindir, we'll find it via 7318 * the indirdep where we can clear pointers if 7319 * needed. 7320 */ 7321 WORKLIST_REMOVE(wk); 7322 continue; 7323 7324 case D_FREEWORK: 7325 /* 7326 * A truncation is waiting for the zero'd pointers 7327 * to be written. It can be freed when the freeblks 7328 * is journaled. 7329 */ 7330 WORKLIST_REMOVE(wk); 7331 wk->wk_state |= ONDEPLIST; 7332 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 7333 break; 7334 7335 case D_ALLOCDIRECT: 7336 if (off != 0) 7337 continue; 7338 /* FALLTHROUGH */ 7339 default: 7340 panic("deallocate_dependencies: Unexpected type %s", 7341 TYPENAME(wk->wk_type)); 7342 /* NOTREACHED */ 7343 } 7344 } 7345 FREE_LOCK(ump); 7346 done: 7347 /* 7348 * Don't throw away this buf, we were partially truncating and 7349 * some deps may always remain. 7350 */ 7351 if (off) { 7352 allocbuf(bp, off); 7353 bp->b_vflags |= BV_SCANNED; 7354 return (EBUSY); 7355 } 7356 bp->b_flags |= B_INVAL | B_NOCACHE; 7357 7358 return (0); 7359 } 7360 7361 /* 7362 * An allocdirect is being canceled due to a truncate. We must make sure 7363 * the journal entry is released in concert with the blkfree that releases 7364 * the storage. Completed journal entries must not be released until the 7365 * space is no longer pointed to by the inode or in the bitmap. 7366 */ 7367 static void 7368 cancel_allocdirect(adphead, adp, freeblks) 7369 struct allocdirectlst *adphead; 7370 struct allocdirect *adp; 7371 struct freeblks *freeblks; 7372 { 7373 struct freework *freework; 7374 struct newblk *newblk; 7375 struct worklist *wk; 7376 7377 TAILQ_REMOVE(adphead, adp, ad_next); 7378 newblk = (struct newblk *)adp; 7379 freework = NULL; 7380 /* 7381 * Find the correct freework structure. 7382 */ 7383 LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) { 7384 if (wk->wk_type != D_FREEWORK) 7385 continue; 7386 freework = WK_FREEWORK(wk); 7387 if (freework->fw_blkno == newblk->nb_newblkno) 7388 break; 7389 } 7390 if (freework == NULL) 7391 panic("cancel_allocdirect: Freework not found"); 7392 /* 7393 * If a newblk exists at all we still have the journal entry that 7394 * initiated the allocation so we do not need to journal the free. 7395 */ 7396 cancel_jfreeblk(freeblks, freework->fw_blkno); 7397 /* 7398 * If the journal hasn't been written the jnewblk must be passed 7399 * to the call to ffs_blkfree that reclaims the space. We accomplish 7400 * this by linking the journal dependency into the freework to be 7401 * freed when freework_freeblock() is called. If the journal has 7402 * been written we can simply reclaim the journal space when the 7403 * freeblks work is complete. 7404 */ 7405 freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list, 7406 &freeblks->fb_jwork); 7407 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 7408 } 7409 7410 7411 /* 7412 * Cancel a new block allocation. May be an indirect or direct block. We 7413 * remove it from various lists and return any journal record that needs to 7414 * be resolved by the caller. 7415 * 7416 * A special consideration is made for indirects which were never pointed 7417 * at on disk and will never be found once this block is released. 7418 */ 7419 static struct jnewblk * 7420 cancel_newblk(newblk, wk, wkhd) 7421 struct newblk *newblk; 7422 struct worklist *wk; 7423 struct workhead *wkhd; 7424 { 7425 struct jnewblk *jnewblk; 7426 7427 CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno); 7428 7429 newblk->nb_state |= GOINGAWAY; 7430 /* 7431 * Previously we traversed the completedhd on each indirdep 7432 * attached to this newblk to cancel them and gather journal 7433 * work. Since we need only the oldest journal segment and 7434 * the lowest point on the tree will always have the oldest 7435 * journal segment we are free to release the segments 7436 * of any subordinates and may leave the indirdep list to 7437 * indirdep_complete() when this newblk is freed. 7438 */ 7439 if (newblk->nb_state & ONDEPLIST) { 7440 newblk->nb_state &= ~ONDEPLIST; 7441 LIST_REMOVE(newblk, nb_deps); 7442 } 7443 if (newblk->nb_state & ONWORKLIST) 7444 WORKLIST_REMOVE(&newblk->nb_list); 7445 /* 7446 * If the journal entry hasn't been written we save a pointer to 7447 * the dependency that frees it until it is written or the 7448 * superseding operation completes. 7449 */ 7450 jnewblk = newblk->nb_jnewblk; 7451 if (jnewblk != NULL && wk != NULL) { 7452 newblk->nb_jnewblk = NULL; 7453 jnewblk->jn_dep = wk; 7454 } 7455 if (!LIST_EMPTY(&newblk->nb_jwork)) 7456 jwork_move(wkhd, &newblk->nb_jwork); 7457 /* 7458 * When truncating we must free the newdirblk early to remove 7459 * the pagedep from the hash before returning. 7460 */ 7461 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7462 free_newdirblk(WK_NEWDIRBLK(wk)); 7463 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7464 panic("cancel_newblk: extra newdirblk"); 7465 7466 return (jnewblk); 7467 } 7468 7469 /* 7470 * Schedule the freefrag associated with a newblk to be released once 7471 * the pointers are written and the previous block is no longer needed. 7472 */ 7473 static void 7474 newblk_freefrag(newblk) 7475 struct newblk *newblk; 7476 { 7477 struct freefrag *freefrag; 7478 7479 if (newblk->nb_freefrag == NULL) 7480 return; 7481 freefrag = newblk->nb_freefrag; 7482 newblk->nb_freefrag = NULL; 7483 freefrag->ff_state |= COMPLETE; 7484 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 7485 add_to_worklist(&freefrag->ff_list, 0); 7486 } 7487 7488 /* 7489 * Free a newblk. Generate a new freefrag work request if appropriate. 7490 * This must be called after the inode pointer and any direct block pointers 7491 * are valid or fully removed via truncate or frag extension. 7492 */ 7493 static void 7494 free_newblk(newblk) 7495 struct newblk *newblk; 7496 { 7497 struct indirdep *indirdep; 7498 struct worklist *wk; 7499 7500 KASSERT(newblk->nb_jnewblk == NULL, 7501 ("free_newblk: jnewblk %p still attached", newblk->nb_jnewblk)); 7502 KASSERT(newblk->nb_list.wk_type != D_NEWBLK, 7503 ("free_newblk: unclaimed newblk")); 7504 LOCK_OWNED(VFSTOUFS(newblk->nb_list.wk_mp)); 7505 newblk_freefrag(newblk); 7506 if (newblk->nb_state & ONDEPLIST) 7507 LIST_REMOVE(newblk, nb_deps); 7508 if (newblk->nb_state & ONWORKLIST) 7509 WORKLIST_REMOVE(&newblk->nb_list); 7510 LIST_REMOVE(newblk, nb_hash); 7511 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7512 free_newdirblk(WK_NEWDIRBLK(wk)); 7513 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7514 panic("free_newblk: extra newdirblk"); 7515 while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL) 7516 indirdep_complete(indirdep); 7517 handle_jwork(&newblk->nb_jwork); 7518 WORKITEM_FREE(newblk, D_NEWBLK); 7519 } 7520 7521 /* 7522 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep. 7523 * This routine must be called with splbio interrupts blocked. 7524 */ 7525 static void 7526 free_newdirblk(newdirblk) 7527 struct newdirblk *newdirblk; 7528 { 7529 struct pagedep *pagedep; 7530 struct diradd *dap; 7531 struct worklist *wk; 7532 7533 LOCK_OWNED(VFSTOUFS(newdirblk->db_list.wk_mp)); 7534 WORKLIST_REMOVE(&newdirblk->db_list); 7535 /* 7536 * If the pagedep is still linked onto the directory buffer 7537 * dependency chain, then some of the entries on the 7538 * pd_pendinghd list may not be committed to disk yet. In 7539 * this case, we will simply clear the NEWBLOCK flag and 7540 * let the pd_pendinghd list be processed when the pagedep 7541 * is next written. If the pagedep is no longer on the buffer 7542 * dependency chain, then all the entries on the pd_pending 7543 * list are committed to disk and we can free them here. 7544 */ 7545 pagedep = newdirblk->db_pagedep; 7546 pagedep->pd_state &= ~NEWBLOCK; 7547 if ((pagedep->pd_state & ONWORKLIST) == 0) { 7548 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 7549 free_diradd(dap, NULL); 7550 /* 7551 * If no dependencies remain, the pagedep will be freed. 7552 */ 7553 free_pagedep(pagedep); 7554 } 7555 /* Should only ever be one item in the list. */ 7556 while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) { 7557 WORKLIST_REMOVE(wk); 7558 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 7559 } 7560 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 7561 } 7562 7563 /* 7564 * Prepare an inode to be freed. The actual free operation is not 7565 * done until the zero'ed inode has been written to disk. 7566 */ 7567 void 7568 softdep_freefile(pvp, ino, mode) 7569 struct vnode *pvp; 7570 ino_t ino; 7571 int mode; 7572 { 7573 struct inode *ip = VTOI(pvp); 7574 struct inodedep *inodedep; 7575 struct freefile *freefile; 7576 struct freeblks *freeblks; 7577 struct ufsmount *ump; 7578 7579 ump = ITOUMP(ip); 7580 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 7581 ("softdep_freefile called on non-softdep filesystem")); 7582 /* 7583 * This sets up the inode de-allocation dependency. 7584 */ 7585 freefile = malloc(sizeof(struct freefile), 7586 M_FREEFILE, M_SOFTDEP_FLAGS); 7587 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount); 7588 freefile->fx_mode = mode; 7589 freefile->fx_oldinum = ino; 7590 freefile->fx_devvp = ump->um_devvp; 7591 LIST_INIT(&freefile->fx_jwork); 7592 UFS_LOCK(ump); 7593 ump->um_fs->fs_pendinginodes += 1; 7594 UFS_UNLOCK(ump); 7595 7596 /* 7597 * If the inodedep does not exist, then the zero'ed inode has 7598 * been written to disk. If the allocated inode has never been 7599 * written to disk, then the on-disk inode is zero'ed. In either 7600 * case we can free the file immediately. If the journal was 7601 * canceled before being written the inode will never make it to 7602 * disk and we must send the canceled journal entrys to 7603 * ffs_freefile() to be cleared in conjunction with the bitmap. 7604 * Any blocks waiting on the inode to write can be safely freed 7605 * here as it will never been written. 7606 */ 7607 ACQUIRE_LOCK(ump); 7608 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7609 if (inodedep) { 7610 /* 7611 * Clear out freeblks that no longer need to reference 7612 * this inode. 7613 */ 7614 while ((freeblks = 7615 TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) { 7616 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, 7617 fb_next); 7618 freeblks->fb_state &= ~ONDEPLIST; 7619 } 7620 /* 7621 * Remove this inode from the unlinked list. 7622 */ 7623 if (inodedep->id_state & UNLINKED) { 7624 /* 7625 * Save the journal work to be freed with the bitmap 7626 * before we clear UNLINKED. Otherwise it can be lost 7627 * if the inode block is written. 7628 */ 7629 handle_bufwait(inodedep, &freefile->fx_jwork); 7630 clear_unlinked_inodedep(inodedep); 7631 /* 7632 * Re-acquire inodedep as we've dropped the 7633 * per-filesystem lock in clear_unlinked_inodedep(). 7634 */ 7635 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7636 } 7637 } 7638 if (inodedep == NULL || check_inode_unwritten(inodedep)) { 7639 FREE_LOCK(ump); 7640 handle_workitem_freefile(freefile); 7641 return; 7642 } 7643 if ((inodedep->id_state & DEPCOMPLETE) == 0) 7644 inodedep->id_state |= GOINGAWAY; 7645 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list); 7646 FREE_LOCK(ump); 7647 if (ip->i_number == ino) 7648 ip->i_flag |= IN_MODIFIED; 7649 } 7650 7651 /* 7652 * Check to see if an inode has never been written to disk. If 7653 * so free the inodedep and return success, otherwise return failure. 7654 * This routine must be called with splbio interrupts blocked. 7655 * 7656 * If we still have a bitmap dependency, then the inode has never 7657 * been written to disk. Drop the dependency as it is no longer 7658 * necessary since the inode is being deallocated. We set the 7659 * ALLCOMPLETE flags since the bitmap now properly shows that the 7660 * inode is not allocated. Even if the inode is actively being 7661 * written, it has been rolled back to its zero'ed state, so we 7662 * are ensured that a zero inode is what is on the disk. For short 7663 * lived files, this change will usually result in removing all the 7664 * dependencies from the inode so that it can be freed immediately. 7665 */ 7666 static int 7667 check_inode_unwritten(inodedep) 7668 struct inodedep *inodedep; 7669 { 7670 7671 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7672 7673 if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 || 7674 !LIST_EMPTY(&inodedep->id_dirremhd) || 7675 !LIST_EMPTY(&inodedep->id_pendinghd) || 7676 !LIST_EMPTY(&inodedep->id_bufwait) || 7677 !LIST_EMPTY(&inodedep->id_inowait) || 7678 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 7679 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 7680 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 7681 !TAILQ_EMPTY(&inodedep->id_extupdt) || 7682 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 7683 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 7684 inodedep->id_mkdiradd != NULL || 7685 inodedep->id_nlinkdelta != 0) 7686 return (0); 7687 /* 7688 * Another process might be in initiate_write_inodeblock_ufs[12] 7689 * trying to allocate memory without holding "Softdep Lock". 7690 */ 7691 if ((inodedep->id_state & IOSTARTED) != 0 && 7692 inodedep->id_savedino1 == NULL) 7693 return (0); 7694 7695 if (inodedep->id_state & ONDEPLIST) 7696 LIST_REMOVE(inodedep, id_deps); 7697 inodedep->id_state &= ~ONDEPLIST; 7698 inodedep->id_state |= ALLCOMPLETE; 7699 inodedep->id_bmsafemap = NULL; 7700 if (inodedep->id_state & ONWORKLIST) 7701 WORKLIST_REMOVE(&inodedep->id_list); 7702 if (inodedep->id_savedino1 != NULL) { 7703 free(inodedep->id_savedino1, M_SAVEDINO); 7704 inodedep->id_savedino1 = NULL; 7705 } 7706 if (free_inodedep(inodedep) == 0) 7707 panic("check_inode_unwritten: busy inode"); 7708 return (1); 7709 } 7710 7711 static int 7712 check_inodedep_free(inodedep) 7713 struct inodedep *inodedep; 7714 { 7715 7716 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7717 if ((inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE || 7718 !LIST_EMPTY(&inodedep->id_dirremhd) || 7719 !LIST_EMPTY(&inodedep->id_pendinghd) || 7720 !LIST_EMPTY(&inodedep->id_bufwait) || 7721 !LIST_EMPTY(&inodedep->id_inowait) || 7722 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 7723 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 7724 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 7725 !TAILQ_EMPTY(&inodedep->id_extupdt) || 7726 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 7727 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 7728 inodedep->id_mkdiradd != NULL || 7729 inodedep->id_nlinkdelta != 0 || 7730 inodedep->id_savedino1 != NULL) 7731 return (0); 7732 return (1); 7733 } 7734 7735 /* 7736 * Try to free an inodedep structure. Return 1 if it could be freed. 7737 */ 7738 static int 7739 free_inodedep(inodedep) 7740 struct inodedep *inodedep; 7741 { 7742 7743 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7744 if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 || 7745 !check_inodedep_free(inodedep)) 7746 return (0); 7747 if (inodedep->id_state & ONDEPLIST) 7748 LIST_REMOVE(inodedep, id_deps); 7749 LIST_REMOVE(inodedep, id_hash); 7750 WORKITEM_FREE(inodedep, D_INODEDEP); 7751 return (1); 7752 } 7753 7754 /* 7755 * Free the block referenced by a freework structure. The parent freeblks 7756 * structure is released and completed when the final cg bitmap reaches 7757 * the disk. This routine may be freeing a jnewblk which never made it to 7758 * disk in which case we do not have to wait as the operation is undone 7759 * in memory immediately. 7760 */ 7761 static void 7762 freework_freeblock(freework, key) 7763 struct freework *freework; 7764 u_long key; 7765 { 7766 struct freeblks *freeblks; 7767 struct jnewblk *jnewblk; 7768 struct ufsmount *ump; 7769 struct workhead wkhd; 7770 struct fs *fs; 7771 int bsize; 7772 int needj; 7773 7774 ump = VFSTOUFS(freework->fw_list.wk_mp); 7775 LOCK_OWNED(ump); 7776 /* 7777 * Handle partial truncate separately. 7778 */ 7779 if (freework->fw_indir) { 7780 complete_trunc_indir(freework); 7781 return; 7782 } 7783 freeblks = freework->fw_freeblks; 7784 fs = ump->um_fs; 7785 needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0; 7786 bsize = lfragtosize(fs, freework->fw_frags); 7787 LIST_INIT(&wkhd); 7788 /* 7789 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives 7790 * on the indirblk hashtable and prevents premature freeing. 7791 */ 7792 freework->fw_state |= DEPCOMPLETE; 7793 /* 7794 * SUJ needs to wait for the segment referencing freed indirect 7795 * blocks to expire so that we know the checker will not confuse 7796 * a re-allocated indirect block with its old contents. 7797 */ 7798 if (needj && freework->fw_lbn <= -UFS_NDADDR) 7799 indirblk_insert(freework); 7800 /* 7801 * If we are canceling an existing jnewblk pass it to the free 7802 * routine, otherwise pass the freeblk which will ultimately 7803 * release the freeblks. If we're not journaling, we can just 7804 * free the freeblks immediately. 7805 */ 7806 jnewblk = freework->fw_jnewblk; 7807 if (jnewblk != NULL) { 7808 cancel_jnewblk(jnewblk, &wkhd); 7809 needj = 0; 7810 } else if (needj) { 7811 freework->fw_state |= DELAYEDFREE; 7812 freeblks->fb_cgwait++; 7813 WORKLIST_INSERT(&wkhd, &freework->fw_list); 7814 } 7815 FREE_LOCK(ump); 7816 freeblks_free(ump, freeblks, btodb(bsize)); 7817 CTR4(KTR_SUJ, 7818 "freework_freeblock: ino %jd blkno %jd lbn %jd size %d", 7819 freeblks->fb_inum, freework->fw_blkno, freework->fw_lbn, bsize); 7820 ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, bsize, 7821 freeblks->fb_inum, freeblks->fb_vtype, &wkhd, key); 7822 ACQUIRE_LOCK(ump); 7823 /* 7824 * The jnewblk will be discarded and the bits in the map never 7825 * made it to disk. We can immediately free the freeblk. 7826 */ 7827 if (needj == 0) 7828 handle_written_freework(freework); 7829 } 7830 7831 /* 7832 * We enqueue freework items that need processing back on the freeblks and 7833 * add the freeblks to the worklist. This makes it easier to find all work 7834 * required to flush a truncation in process_truncates(). 7835 */ 7836 static void 7837 freework_enqueue(freework) 7838 struct freework *freework; 7839 { 7840 struct freeblks *freeblks; 7841 7842 freeblks = freework->fw_freeblks; 7843 if ((freework->fw_state & INPROGRESS) == 0) 7844 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 7845 if ((freeblks->fb_state & 7846 (ONWORKLIST | INPROGRESS | ALLCOMPLETE)) == ALLCOMPLETE && 7847 LIST_EMPTY(&freeblks->fb_jblkdephd)) 7848 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 7849 } 7850 7851 /* 7852 * Start, continue, or finish the process of freeing an indirect block tree. 7853 * The free operation may be paused at any point with fw_off containing the 7854 * offset to restart from. This enables us to implement some flow control 7855 * for large truncates which may fan out and generate a huge number of 7856 * dependencies. 7857 */ 7858 static void 7859 handle_workitem_indirblk(freework) 7860 struct freework *freework; 7861 { 7862 struct freeblks *freeblks; 7863 struct ufsmount *ump; 7864 struct fs *fs; 7865 7866 freeblks = freework->fw_freeblks; 7867 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7868 fs = ump->um_fs; 7869 if (freework->fw_state & DEPCOMPLETE) { 7870 handle_written_freework(freework); 7871 return; 7872 } 7873 if (freework->fw_off == NINDIR(fs)) { 7874 freework_freeblock(freework, SINGLETON_KEY); 7875 return; 7876 } 7877 freework->fw_state |= INPROGRESS; 7878 FREE_LOCK(ump); 7879 indir_trunc(freework, fsbtodb(fs, freework->fw_blkno), 7880 freework->fw_lbn); 7881 ACQUIRE_LOCK(ump); 7882 } 7883 7884 /* 7885 * Called when a freework structure attached to a cg buf is written. The 7886 * ref on either the parent or the freeblks structure is released and 7887 * the freeblks is added back to the worklist if there is more work to do. 7888 */ 7889 static void 7890 handle_written_freework(freework) 7891 struct freework *freework; 7892 { 7893 struct freeblks *freeblks; 7894 struct freework *parent; 7895 7896 freeblks = freework->fw_freeblks; 7897 parent = freework->fw_parent; 7898 if (freework->fw_state & DELAYEDFREE) 7899 freeblks->fb_cgwait--; 7900 freework->fw_state |= COMPLETE; 7901 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 7902 WORKITEM_FREE(freework, D_FREEWORK); 7903 if (parent) { 7904 if (--parent->fw_ref == 0) 7905 freework_enqueue(parent); 7906 return; 7907 } 7908 if (--freeblks->fb_ref != 0) 7909 return; 7910 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) == 7911 ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd)) 7912 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 7913 } 7914 7915 /* 7916 * This workitem routine performs the block de-allocation. 7917 * The workitem is added to the pending list after the updated 7918 * inode block has been written to disk. As mentioned above, 7919 * checks regarding the number of blocks de-allocated (compared 7920 * to the number of blocks allocated for the file) are also 7921 * performed in this function. 7922 */ 7923 static int 7924 handle_workitem_freeblocks(freeblks, flags) 7925 struct freeblks *freeblks; 7926 int flags; 7927 { 7928 struct freework *freework; 7929 struct newblk *newblk; 7930 struct allocindir *aip; 7931 struct ufsmount *ump; 7932 struct worklist *wk; 7933 u_long key; 7934 7935 KASSERT(LIST_EMPTY(&freeblks->fb_jblkdephd), 7936 ("handle_workitem_freeblocks: Journal entries not written.")); 7937 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7938 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum); 7939 ACQUIRE_LOCK(ump); 7940 while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) { 7941 WORKLIST_REMOVE(wk); 7942 switch (wk->wk_type) { 7943 case D_DIRREM: 7944 wk->wk_state |= COMPLETE; 7945 add_to_worklist(wk, 0); 7946 continue; 7947 7948 case D_ALLOCDIRECT: 7949 free_newblk(WK_NEWBLK(wk)); 7950 continue; 7951 7952 case D_ALLOCINDIR: 7953 aip = WK_ALLOCINDIR(wk); 7954 freework = NULL; 7955 if (aip->ai_state & DELAYEDFREE) { 7956 FREE_LOCK(ump); 7957 freework = newfreework(ump, freeblks, NULL, 7958 aip->ai_lbn, aip->ai_newblkno, 7959 ump->um_fs->fs_frag, 0, 0); 7960 ACQUIRE_LOCK(ump); 7961 } 7962 newblk = WK_NEWBLK(wk); 7963 if (newblk->nb_jnewblk) { 7964 freework->fw_jnewblk = newblk->nb_jnewblk; 7965 newblk->nb_jnewblk->jn_dep = &freework->fw_list; 7966 newblk->nb_jnewblk = NULL; 7967 } 7968 free_newblk(newblk); 7969 continue; 7970 7971 case D_FREEWORK: 7972 freework = WK_FREEWORK(wk); 7973 if (freework->fw_lbn <= -UFS_NDADDR) 7974 handle_workitem_indirblk(freework); 7975 else 7976 freework_freeblock(freework, key); 7977 continue; 7978 default: 7979 panic("handle_workitem_freeblocks: Unknown type %s", 7980 TYPENAME(wk->wk_type)); 7981 } 7982 } 7983 if (freeblks->fb_ref != 0) { 7984 freeblks->fb_state &= ~INPROGRESS; 7985 wake_worklist(&freeblks->fb_list); 7986 freeblks = NULL; 7987 } 7988 FREE_LOCK(ump); 7989 ffs_blkrelease_finish(ump, key); 7990 if (freeblks) 7991 return handle_complete_freeblocks(freeblks, flags); 7992 return (0); 7993 } 7994 7995 /* 7996 * Handle completion of block free via truncate. This allows fs_pending 7997 * to track the actual free block count more closely than if we only updated 7998 * it at the end. We must be careful to handle cases where the block count 7999 * on free was incorrect. 8000 */ 8001 static void 8002 freeblks_free(ump, freeblks, blocks) 8003 struct ufsmount *ump; 8004 struct freeblks *freeblks; 8005 int blocks; 8006 { 8007 struct fs *fs; 8008 ufs2_daddr_t remain; 8009 8010 UFS_LOCK(ump); 8011 remain = -freeblks->fb_chkcnt; 8012 freeblks->fb_chkcnt += blocks; 8013 if (remain > 0) { 8014 if (remain < blocks) 8015 blocks = remain; 8016 fs = ump->um_fs; 8017 fs->fs_pendingblocks -= blocks; 8018 } 8019 UFS_UNLOCK(ump); 8020 } 8021 8022 /* 8023 * Once all of the freework workitems are complete we can retire the 8024 * freeblocks dependency and any journal work awaiting completion. This 8025 * can not be called until all other dependencies are stable on disk. 8026 */ 8027 static int 8028 handle_complete_freeblocks(freeblks, flags) 8029 struct freeblks *freeblks; 8030 int flags; 8031 { 8032 struct inodedep *inodedep; 8033 struct inode *ip; 8034 struct vnode *vp; 8035 struct fs *fs; 8036 struct ufsmount *ump; 8037 ufs2_daddr_t spare; 8038 8039 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8040 fs = ump->um_fs; 8041 flags = LK_EXCLUSIVE | flags; 8042 spare = freeblks->fb_chkcnt; 8043 8044 /* 8045 * If we did not release the expected number of blocks we may have 8046 * to adjust the inode block count here. Only do so if it wasn't 8047 * a truncation to zero and the modrev still matches. 8048 */ 8049 if (spare && freeblks->fb_len != 0) { 8050 if (ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_inum, 8051 flags, &vp, FFSV_FORCEINSMQ) != 0) 8052 return (EBUSY); 8053 ip = VTOI(vp); 8054 if (DIP(ip, i_modrev) == freeblks->fb_modrev) { 8055 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare); 8056 ip->i_flag |= IN_CHANGE; 8057 /* 8058 * We must wait so this happens before the 8059 * journal is reclaimed. 8060 */ 8061 ffs_update(vp, 1); 8062 } 8063 vput(vp); 8064 } 8065 if (spare < 0) { 8066 UFS_LOCK(ump); 8067 fs->fs_pendingblocks += spare; 8068 UFS_UNLOCK(ump); 8069 } 8070 #ifdef QUOTA 8071 /* Handle spare. */ 8072 if (spare) 8073 quotaadj(freeblks->fb_quota, ump, -spare); 8074 quotarele(freeblks->fb_quota); 8075 #endif 8076 ACQUIRE_LOCK(ump); 8077 if (freeblks->fb_state & ONDEPLIST) { 8078 inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum, 8079 0, &inodedep); 8080 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next); 8081 freeblks->fb_state &= ~ONDEPLIST; 8082 if (TAILQ_EMPTY(&inodedep->id_freeblklst)) 8083 free_inodedep(inodedep); 8084 } 8085 /* 8086 * All of the freeblock deps must be complete prior to this call 8087 * so it's now safe to complete earlier outstanding journal entries. 8088 */ 8089 handle_jwork(&freeblks->fb_jwork); 8090 WORKITEM_FREE(freeblks, D_FREEBLKS); 8091 FREE_LOCK(ump); 8092 return (0); 8093 } 8094 8095 /* 8096 * Release blocks associated with the freeblks and stored in the indirect 8097 * block dbn. If level is greater than SINGLE, the block is an indirect block 8098 * and recursive calls to indirtrunc must be used to cleanse other indirect 8099 * blocks. 8100 * 8101 * This handles partial and complete truncation of blocks. Partial is noted 8102 * with goingaway == 0. In this case the freework is completed after the 8103 * zero'd indirects are written to disk. For full truncation the freework 8104 * is completed after the block is freed. 8105 */ 8106 static void 8107 indir_trunc(freework, dbn, lbn) 8108 struct freework *freework; 8109 ufs2_daddr_t dbn; 8110 ufs_lbn_t lbn; 8111 { 8112 struct freework *nfreework; 8113 struct workhead wkhd; 8114 struct freeblks *freeblks; 8115 struct buf *bp; 8116 struct fs *fs; 8117 struct indirdep *indirdep; 8118 struct ufsmount *ump; 8119 ufs1_daddr_t *bap1; 8120 ufs2_daddr_t nb, nnb, *bap2; 8121 ufs_lbn_t lbnadd, nlbn; 8122 u_long key; 8123 int nblocks, ufs1fmt, freedblocks; 8124 int goingaway, freedeps, needj, level, cnt, i; 8125 8126 freeblks = freework->fw_freeblks; 8127 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8128 fs = ump->um_fs; 8129 /* 8130 * Get buffer of block pointers to be freed. There are three cases: 8131 * 8132 * 1) Partial truncate caches the indirdep pointer in the freework 8133 * which provides us a back copy to the save bp which holds the 8134 * pointers we want to clear. When this completes the zero 8135 * pointers are written to the real copy. 8136 * 2) The indirect is being completely truncated, cancel_indirdep() 8137 * eliminated the real copy and placed the indirdep on the saved 8138 * copy. The indirdep and buf are discarded when this completes. 8139 * 3) The indirect was not in memory, we read a copy off of the disk 8140 * using the devvp and drop and invalidate the buffer when we're 8141 * done. 8142 */ 8143 goingaway = 1; 8144 indirdep = NULL; 8145 if (freework->fw_indir != NULL) { 8146 goingaway = 0; 8147 indirdep = freework->fw_indir; 8148 bp = indirdep->ir_savebp; 8149 if (bp == NULL || bp->b_blkno != dbn) 8150 panic("indir_trunc: Bad saved buf %p blkno %jd", 8151 bp, (intmax_t)dbn); 8152 } else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) { 8153 /* 8154 * The lock prevents the buf dep list from changing and 8155 * indirects on devvp should only ever have one dependency. 8156 */ 8157 indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep)); 8158 if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0) 8159 panic("indir_trunc: Bad indirdep %p from buf %p", 8160 indirdep, bp); 8161 } else if (bread(freeblks->fb_devvp, dbn, (int)fs->fs_bsize, 8162 NOCRED, &bp) != 0) { 8163 brelse(bp); 8164 return; 8165 } 8166 ACQUIRE_LOCK(ump); 8167 /* Protects against a race with complete_trunc_indir(). */ 8168 freework->fw_state &= ~INPROGRESS; 8169 /* 8170 * If we have an indirdep we need to enforce the truncation order 8171 * and discard it when it is complete. 8172 */ 8173 if (indirdep) { 8174 if (freework != TAILQ_FIRST(&indirdep->ir_trunc) && 8175 !TAILQ_EMPTY(&indirdep->ir_trunc)) { 8176 /* 8177 * Add the complete truncate to the list on the 8178 * indirdep to enforce in-order processing. 8179 */ 8180 if (freework->fw_indir == NULL) 8181 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, 8182 freework, fw_next); 8183 FREE_LOCK(ump); 8184 return; 8185 } 8186 /* 8187 * If we're goingaway, free the indirdep. Otherwise it will 8188 * linger until the write completes. 8189 */ 8190 if (goingaway) 8191 free_indirdep(indirdep); 8192 } 8193 FREE_LOCK(ump); 8194 /* Initialize pointers depending on block size. */ 8195 if (ump->um_fstype == UFS1) { 8196 bap1 = (ufs1_daddr_t *)bp->b_data; 8197 nb = bap1[freework->fw_off]; 8198 ufs1fmt = 1; 8199 bap2 = NULL; 8200 } else { 8201 bap2 = (ufs2_daddr_t *)bp->b_data; 8202 nb = bap2[freework->fw_off]; 8203 ufs1fmt = 0; 8204 bap1 = NULL; 8205 } 8206 level = lbn_level(lbn); 8207 needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0; 8208 lbnadd = lbn_offset(fs, level); 8209 nblocks = btodb(fs->fs_bsize); 8210 nfreework = freework; 8211 freedeps = 0; 8212 cnt = 0; 8213 /* 8214 * Reclaim blocks. Traverses into nested indirect levels and 8215 * arranges for the current level to be freed when subordinates 8216 * are free when journaling. 8217 */ 8218 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum); 8219 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) { 8220 if (i != NINDIR(fs) - 1) { 8221 if (ufs1fmt) 8222 nnb = bap1[i+1]; 8223 else 8224 nnb = bap2[i+1]; 8225 } else 8226 nnb = 0; 8227 if (nb == 0) 8228 continue; 8229 cnt++; 8230 if (level != 0) { 8231 nlbn = (lbn + 1) - (i * lbnadd); 8232 if (needj != 0) { 8233 nfreework = newfreework(ump, freeblks, freework, 8234 nlbn, nb, fs->fs_frag, 0, 0); 8235 freedeps++; 8236 } 8237 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn); 8238 } else { 8239 struct freedep *freedep; 8240 8241 /* 8242 * Attempt to aggregate freedep dependencies for 8243 * all blocks being released to the same CG. 8244 */ 8245 LIST_INIT(&wkhd); 8246 if (needj != 0 && 8247 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) { 8248 freedep = newfreedep(freework); 8249 WORKLIST_INSERT_UNLOCKED(&wkhd, 8250 &freedep->fd_list); 8251 freedeps++; 8252 } 8253 CTR3(KTR_SUJ, 8254 "indir_trunc: ino %jd blkno %jd size %d", 8255 freeblks->fb_inum, nb, fs->fs_bsize); 8256 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb, 8257 fs->fs_bsize, freeblks->fb_inum, 8258 freeblks->fb_vtype, &wkhd, key); 8259 } 8260 } 8261 ffs_blkrelease_finish(ump, key); 8262 if (goingaway) { 8263 bp->b_flags |= B_INVAL | B_NOCACHE; 8264 brelse(bp); 8265 } 8266 freedblocks = 0; 8267 if (level == 0) 8268 freedblocks = (nblocks * cnt); 8269 if (needj == 0) 8270 freedblocks += nblocks; 8271 freeblks_free(ump, freeblks, freedblocks); 8272 /* 8273 * If we are journaling set up the ref counts and offset so this 8274 * indirect can be completed when its children are free. 8275 */ 8276 if (needj) { 8277 ACQUIRE_LOCK(ump); 8278 freework->fw_off = i; 8279 freework->fw_ref += freedeps; 8280 freework->fw_ref -= NINDIR(fs) + 1; 8281 if (level == 0) 8282 freeblks->fb_cgwait += freedeps; 8283 if (freework->fw_ref == 0) 8284 freework_freeblock(freework, SINGLETON_KEY); 8285 FREE_LOCK(ump); 8286 return; 8287 } 8288 /* 8289 * If we're not journaling we can free the indirect now. 8290 */ 8291 dbn = dbtofsb(fs, dbn); 8292 CTR3(KTR_SUJ, 8293 "indir_trunc 2: ino %jd blkno %jd size %d", 8294 freeblks->fb_inum, dbn, fs->fs_bsize); 8295 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize, 8296 freeblks->fb_inum, freeblks->fb_vtype, NULL, SINGLETON_KEY); 8297 /* Non SUJ softdep does single-threaded truncations. */ 8298 if (freework->fw_blkno == dbn) { 8299 freework->fw_state |= ALLCOMPLETE; 8300 ACQUIRE_LOCK(ump); 8301 handle_written_freework(freework); 8302 FREE_LOCK(ump); 8303 } 8304 return; 8305 } 8306 8307 /* 8308 * Cancel an allocindir when it is removed via truncation. When bp is not 8309 * NULL the indirect never appeared on disk and is scheduled to be freed 8310 * independently of the indir so we can more easily track journal work. 8311 */ 8312 static void 8313 cancel_allocindir(aip, bp, freeblks, trunc) 8314 struct allocindir *aip; 8315 struct buf *bp; 8316 struct freeblks *freeblks; 8317 int trunc; 8318 { 8319 struct indirdep *indirdep; 8320 struct freefrag *freefrag; 8321 struct newblk *newblk; 8322 8323 newblk = (struct newblk *)aip; 8324 LIST_REMOVE(aip, ai_next); 8325 /* 8326 * We must eliminate the pointer in bp if it must be freed on its 8327 * own due to partial truncate or pending journal work. 8328 */ 8329 if (bp && (trunc || newblk->nb_jnewblk)) { 8330 /* 8331 * Clear the pointer and mark the aip to be freed 8332 * directly if it never existed on disk. 8333 */ 8334 aip->ai_state |= DELAYEDFREE; 8335 indirdep = aip->ai_indirdep; 8336 if (indirdep->ir_state & UFS1FMT) 8337 ((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8338 else 8339 ((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8340 } 8341 /* 8342 * When truncating the previous pointer will be freed via 8343 * savedbp. Eliminate the freefrag which would dup free. 8344 */ 8345 if (trunc && (freefrag = newblk->nb_freefrag) != NULL) { 8346 newblk->nb_freefrag = NULL; 8347 if (freefrag->ff_jdep) 8348 cancel_jfreefrag( 8349 WK_JFREEFRAG(freefrag->ff_jdep)); 8350 jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork); 8351 WORKITEM_FREE(freefrag, D_FREEFRAG); 8352 } 8353 /* 8354 * If the journal hasn't been written the jnewblk must be passed 8355 * to the call to ffs_blkfree that reclaims the space. We accomplish 8356 * this by leaving the journal dependency on the newblk to be freed 8357 * when a freework is created in handle_workitem_freeblocks(). 8358 */ 8359 cancel_newblk(newblk, NULL, &freeblks->fb_jwork); 8360 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 8361 } 8362 8363 /* 8364 * Create the mkdir dependencies for . and .. in a new directory. Link them 8365 * in to a newdirblk so any subsequent additions are tracked properly. The 8366 * caller is responsible for adding the mkdir1 dependency to the journal 8367 * and updating id_mkdiradd. This function returns with the per-filesystem 8368 * lock held. 8369 */ 8370 static struct mkdir * 8371 setup_newdir(dap, newinum, dinum, newdirbp, mkdirp) 8372 struct diradd *dap; 8373 ino_t newinum; 8374 ino_t dinum; 8375 struct buf *newdirbp; 8376 struct mkdir **mkdirp; 8377 { 8378 struct newblk *newblk; 8379 struct pagedep *pagedep; 8380 struct inodedep *inodedep; 8381 struct newdirblk *newdirblk; 8382 struct mkdir *mkdir1, *mkdir2; 8383 struct worklist *wk; 8384 struct jaddref *jaddref; 8385 struct ufsmount *ump; 8386 struct mount *mp; 8387 8388 mp = dap->da_list.wk_mp; 8389 ump = VFSTOUFS(mp); 8390 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK, 8391 M_SOFTDEP_FLAGS); 8392 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8393 LIST_INIT(&newdirblk->db_mkdir); 8394 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8395 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp); 8396 mkdir1->md_state = ATTACHED | MKDIR_BODY; 8397 mkdir1->md_diradd = dap; 8398 mkdir1->md_jaddref = NULL; 8399 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8400 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp); 8401 mkdir2->md_state = ATTACHED | MKDIR_PARENT; 8402 mkdir2->md_diradd = dap; 8403 mkdir2->md_jaddref = NULL; 8404 if (MOUNTEDSUJ(mp) == 0) { 8405 mkdir1->md_state |= DEPCOMPLETE; 8406 mkdir2->md_state |= DEPCOMPLETE; 8407 } 8408 /* 8409 * Dependency on "." and ".." being written to disk. 8410 */ 8411 mkdir1->md_buf = newdirbp; 8412 ACQUIRE_LOCK(VFSTOUFS(mp)); 8413 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir1, md_mkdirs); 8414 /* 8415 * We must link the pagedep, allocdirect, and newdirblk for 8416 * the initial file page so the pointer to the new directory 8417 * is not written until the directory contents are live and 8418 * any subsequent additions are not marked live until the 8419 * block is reachable via the inode. 8420 */ 8421 if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0) 8422 panic("setup_newdir: lost pagedep"); 8423 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list) 8424 if (wk->wk_type == D_ALLOCDIRECT) 8425 break; 8426 if (wk == NULL) 8427 panic("setup_newdir: lost allocdirect"); 8428 if (pagedep->pd_state & NEWBLOCK) 8429 panic("setup_newdir: NEWBLOCK already set"); 8430 newblk = WK_NEWBLK(wk); 8431 pagedep->pd_state |= NEWBLOCK; 8432 pagedep->pd_newdirblk = newdirblk; 8433 newdirblk->db_pagedep = pagedep; 8434 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8435 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list); 8436 /* 8437 * Look up the inodedep for the parent directory so that we 8438 * can link mkdir2 into the pending dotdot jaddref or 8439 * the inode write if there is none. If the inode is 8440 * ALLCOMPLETE and no jaddref is present all dependencies have 8441 * been satisfied and mkdir2 can be freed. 8442 */ 8443 inodedep_lookup(mp, dinum, 0, &inodedep); 8444 if (MOUNTEDSUJ(mp)) { 8445 if (inodedep == NULL) 8446 panic("setup_newdir: Lost parent."); 8447 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8448 inoreflst); 8449 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum && 8450 (jaddref->ja_state & MKDIR_PARENT), 8451 ("setup_newdir: bad dotdot jaddref %p", jaddref)); 8452 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8453 mkdir2->md_jaddref = jaddref; 8454 jaddref->ja_mkdir = mkdir2; 8455 } else if (inodedep == NULL || 8456 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 8457 dap->da_state &= ~MKDIR_PARENT; 8458 WORKITEM_FREE(mkdir2, D_MKDIR); 8459 mkdir2 = NULL; 8460 } else { 8461 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8462 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list); 8463 } 8464 *mkdirp = mkdir2; 8465 8466 return (mkdir1); 8467 } 8468 8469 /* 8470 * Directory entry addition dependencies. 8471 * 8472 * When adding a new directory entry, the inode (with its incremented link 8473 * count) must be written to disk before the directory entry's pointer to it. 8474 * Also, if the inode is newly allocated, the corresponding freemap must be 8475 * updated (on disk) before the directory entry's pointer. These requirements 8476 * are met via undo/redo on the directory entry's pointer, which consists 8477 * simply of the inode number. 8478 * 8479 * As directory entries are added and deleted, the free space within a 8480 * directory block can become fragmented. The ufs filesystem will compact 8481 * a fragmented directory block to make space for a new entry. When this 8482 * occurs, the offsets of previously added entries change. Any "diradd" 8483 * dependency structures corresponding to these entries must be updated with 8484 * the new offsets. 8485 */ 8486 8487 /* 8488 * This routine is called after the in-memory inode's link 8489 * count has been incremented, but before the directory entry's 8490 * pointer to the inode has been set. 8491 */ 8492 int 8493 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 8494 struct buf *bp; /* buffer containing directory block */ 8495 struct inode *dp; /* inode for directory */ 8496 off_t diroffset; /* offset of new entry in directory */ 8497 ino_t newinum; /* inode referenced by new directory entry */ 8498 struct buf *newdirbp; /* non-NULL => contents of new mkdir */ 8499 int isnewblk; /* entry is in a newly allocated block */ 8500 { 8501 int offset; /* offset of new entry within directory block */ 8502 ufs_lbn_t lbn; /* block in directory containing new entry */ 8503 struct fs *fs; 8504 struct diradd *dap; 8505 struct newblk *newblk; 8506 struct pagedep *pagedep; 8507 struct inodedep *inodedep; 8508 struct newdirblk *newdirblk; 8509 struct mkdir *mkdir1, *mkdir2; 8510 struct jaddref *jaddref; 8511 struct ufsmount *ump; 8512 struct mount *mp; 8513 int isindir; 8514 8515 mp = ITOVFS(dp); 8516 ump = VFSTOUFS(mp); 8517 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 8518 ("softdep_setup_directory_add called on non-softdep filesystem")); 8519 /* 8520 * Whiteouts have no dependencies. 8521 */ 8522 if (newinum == UFS_WINO) { 8523 if (newdirbp != NULL) 8524 bdwrite(newdirbp); 8525 return (0); 8526 } 8527 jaddref = NULL; 8528 mkdir1 = mkdir2 = NULL; 8529 fs = ump->um_fs; 8530 lbn = lblkno(fs, diroffset); 8531 offset = blkoff(fs, diroffset); 8532 dap = malloc(sizeof(struct diradd), M_DIRADD, 8533 M_SOFTDEP_FLAGS|M_ZERO); 8534 workitem_alloc(&dap->da_list, D_DIRADD, mp); 8535 dap->da_offset = offset; 8536 dap->da_newinum = newinum; 8537 dap->da_state = ATTACHED; 8538 LIST_INIT(&dap->da_jwork); 8539 isindir = bp->b_lblkno >= UFS_NDADDR; 8540 newdirblk = NULL; 8541 if (isnewblk && 8542 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) { 8543 newdirblk = malloc(sizeof(struct newdirblk), 8544 M_NEWDIRBLK, M_SOFTDEP_FLAGS); 8545 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8546 LIST_INIT(&newdirblk->db_mkdir); 8547 } 8548 /* 8549 * If we're creating a new directory setup the dependencies and set 8550 * the dap state to wait for them. Otherwise it's COMPLETE and 8551 * we can move on. 8552 */ 8553 if (newdirbp == NULL) { 8554 dap->da_state |= DEPCOMPLETE; 8555 ACQUIRE_LOCK(ump); 8556 } else { 8557 dap->da_state |= MKDIR_BODY | MKDIR_PARENT; 8558 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp, 8559 &mkdir2); 8560 } 8561 /* 8562 * Link into parent directory pagedep to await its being written. 8563 */ 8564 pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep); 8565 #ifdef DEBUG 8566 if (diradd_lookup(pagedep, offset) != NULL) 8567 panic("softdep_setup_directory_add: %p already at off %d\n", 8568 diradd_lookup(pagedep, offset), offset); 8569 #endif 8570 dap->da_pagedep = pagedep; 8571 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap, 8572 da_pdlist); 8573 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 8574 /* 8575 * If we're journaling, link the diradd into the jaddref so it 8576 * may be completed after the journal entry is written. Otherwise, 8577 * link the diradd into its inodedep. If the inode is not yet 8578 * written place it on the bufwait list, otherwise do the post-inode 8579 * write processing to put it on the id_pendinghd list. 8580 */ 8581 if (MOUNTEDSUJ(mp)) { 8582 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8583 inoreflst); 8584 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 8585 ("softdep_setup_directory_add: bad jaddref %p", jaddref)); 8586 jaddref->ja_diroff = diroffset; 8587 jaddref->ja_diradd = dap; 8588 add_to_journal(&jaddref->ja_list); 8589 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) 8590 diradd_inode_written(dap, inodedep); 8591 else 8592 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 8593 /* 8594 * Add the journal entries for . and .. links now that the primary 8595 * link is written. 8596 */ 8597 if (mkdir1 != NULL && MOUNTEDSUJ(mp)) { 8598 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 8599 inoreflst, if_deps); 8600 KASSERT(jaddref != NULL && 8601 jaddref->ja_ino == jaddref->ja_parent && 8602 (jaddref->ja_state & MKDIR_BODY), 8603 ("softdep_setup_directory_add: bad dot jaddref %p", 8604 jaddref)); 8605 mkdir1->md_jaddref = jaddref; 8606 jaddref->ja_mkdir = mkdir1; 8607 /* 8608 * It is important that the dotdot journal entry 8609 * is added prior to the dot entry since dot writes 8610 * both the dot and dotdot links. These both must 8611 * be added after the primary link for the journal 8612 * to remain consistent. 8613 */ 8614 add_to_journal(&mkdir2->md_jaddref->ja_list); 8615 add_to_journal(&jaddref->ja_list); 8616 } 8617 /* 8618 * If we are adding a new directory remember this diradd so that if 8619 * we rename it we can keep the dot and dotdot dependencies. If 8620 * we are adding a new name for an inode that has a mkdiradd we 8621 * must be in rename and we have to move the dot and dotdot 8622 * dependencies to this new name. The old name is being orphaned 8623 * soon. 8624 */ 8625 if (mkdir1 != NULL) { 8626 if (inodedep->id_mkdiradd != NULL) 8627 panic("softdep_setup_directory_add: Existing mkdir"); 8628 inodedep->id_mkdiradd = dap; 8629 } else if (inodedep->id_mkdiradd) 8630 merge_diradd(inodedep, dap); 8631 if (newdirblk != NULL) { 8632 /* 8633 * There is nothing to do if we are already tracking 8634 * this block. 8635 */ 8636 if ((pagedep->pd_state & NEWBLOCK) != 0) { 8637 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 8638 FREE_LOCK(ump); 8639 return (0); 8640 } 8641 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk) 8642 == 0) 8643 panic("softdep_setup_directory_add: lost entry"); 8644 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8645 pagedep->pd_state |= NEWBLOCK; 8646 pagedep->pd_newdirblk = newdirblk; 8647 newdirblk->db_pagedep = pagedep; 8648 FREE_LOCK(ump); 8649 /* 8650 * If we extended into an indirect signal direnter to sync. 8651 */ 8652 if (isindir) 8653 return (1); 8654 return (0); 8655 } 8656 FREE_LOCK(ump); 8657 return (0); 8658 } 8659 8660 /* 8661 * This procedure is called to change the offset of a directory 8662 * entry when compacting a directory block which must be owned 8663 * exclusively by the caller. Note that the actual entry movement 8664 * must be done in this procedure to ensure that no I/O completions 8665 * occur while the move is in progress. 8666 */ 8667 void 8668 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 8669 struct buf *bp; /* Buffer holding directory block. */ 8670 struct inode *dp; /* inode for directory */ 8671 caddr_t base; /* address of dp->i_offset */ 8672 caddr_t oldloc; /* address of old directory location */ 8673 caddr_t newloc; /* address of new directory location */ 8674 int entrysize; /* size of directory entry */ 8675 { 8676 int offset, oldoffset, newoffset; 8677 struct pagedep *pagedep; 8678 struct jmvref *jmvref; 8679 struct diradd *dap; 8680 struct direct *de; 8681 struct mount *mp; 8682 struct ufsmount *ump; 8683 ufs_lbn_t lbn; 8684 int flags; 8685 8686 mp = ITOVFS(dp); 8687 ump = VFSTOUFS(mp); 8688 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 8689 ("softdep_change_directoryentry_offset called on " 8690 "non-softdep filesystem")); 8691 de = (struct direct *)oldloc; 8692 jmvref = NULL; 8693 flags = 0; 8694 /* 8695 * Moves are always journaled as it would be too complex to 8696 * determine if any affected adds or removes are present in the 8697 * journal. 8698 */ 8699 if (MOUNTEDSUJ(mp)) { 8700 flags = DEPALLOC; 8701 jmvref = newjmvref(dp, de->d_ino, 8702 dp->i_offset + (oldloc - base), 8703 dp->i_offset + (newloc - base)); 8704 } 8705 lbn = lblkno(ump->um_fs, dp->i_offset); 8706 offset = blkoff(ump->um_fs, dp->i_offset); 8707 oldoffset = offset + (oldloc - base); 8708 newoffset = offset + (newloc - base); 8709 ACQUIRE_LOCK(ump); 8710 if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0) 8711 goto done; 8712 dap = diradd_lookup(pagedep, oldoffset); 8713 if (dap) { 8714 dap->da_offset = newoffset; 8715 newoffset = DIRADDHASH(newoffset); 8716 oldoffset = DIRADDHASH(oldoffset); 8717 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE && 8718 newoffset != oldoffset) { 8719 LIST_REMOVE(dap, da_pdlist); 8720 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset], 8721 dap, da_pdlist); 8722 } 8723 } 8724 done: 8725 if (jmvref) { 8726 jmvref->jm_pagedep = pagedep; 8727 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps); 8728 add_to_journal(&jmvref->jm_list); 8729 } 8730 bcopy(oldloc, newloc, entrysize); 8731 FREE_LOCK(ump); 8732 } 8733 8734 /* 8735 * Move the mkdir dependencies and journal work from one diradd to another 8736 * when renaming a directory. The new name must depend on the mkdir deps 8737 * completing as the old name did. Directories can only have one valid link 8738 * at a time so one must be canonical. 8739 */ 8740 static void 8741 merge_diradd(inodedep, newdap) 8742 struct inodedep *inodedep; 8743 struct diradd *newdap; 8744 { 8745 struct diradd *olddap; 8746 struct mkdir *mkdir, *nextmd; 8747 struct ufsmount *ump; 8748 short state; 8749 8750 olddap = inodedep->id_mkdiradd; 8751 inodedep->id_mkdiradd = newdap; 8752 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8753 newdap->da_state &= ~DEPCOMPLETE; 8754 ump = VFSTOUFS(inodedep->id_list.wk_mp); 8755 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 8756 mkdir = nextmd) { 8757 nextmd = LIST_NEXT(mkdir, md_mkdirs); 8758 if (mkdir->md_diradd != olddap) 8759 continue; 8760 mkdir->md_diradd = newdap; 8761 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY); 8762 newdap->da_state |= state; 8763 olddap->da_state &= ~state; 8764 if ((olddap->da_state & 8765 (MKDIR_PARENT | MKDIR_BODY)) == 0) 8766 break; 8767 } 8768 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 8769 panic("merge_diradd: unfound ref"); 8770 } 8771 /* 8772 * Any mkdir related journal items are not safe to be freed until 8773 * the new name is stable. 8774 */ 8775 jwork_move(&newdap->da_jwork, &olddap->da_jwork); 8776 olddap->da_state |= DEPCOMPLETE; 8777 complete_diradd(olddap); 8778 } 8779 8780 /* 8781 * Move the diradd to the pending list when all diradd dependencies are 8782 * complete. 8783 */ 8784 static void 8785 complete_diradd(dap) 8786 struct diradd *dap; 8787 { 8788 struct pagedep *pagedep; 8789 8790 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 8791 if (dap->da_state & DIRCHG) 8792 pagedep = dap->da_previous->dm_pagedep; 8793 else 8794 pagedep = dap->da_pagedep; 8795 LIST_REMOVE(dap, da_pdlist); 8796 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 8797 } 8798 } 8799 8800 /* 8801 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal 8802 * add entries and conditonally journal the remove. 8803 */ 8804 static void 8805 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref) 8806 struct diradd *dap; 8807 struct dirrem *dirrem; 8808 struct jremref *jremref; 8809 struct jremref *dotremref; 8810 struct jremref *dotdotremref; 8811 { 8812 struct inodedep *inodedep; 8813 struct jaddref *jaddref; 8814 struct inoref *inoref; 8815 struct ufsmount *ump; 8816 struct mkdir *mkdir; 8817 8818 /* 8819 * If no remove references were allocated we're on a non-journaled 8820 * filesystem and can skip the cancel step. 8821 */ 8822 if (jremref == NULL) { 8823 free_diradd(dap, NULL); 8824 return; 8825 } 8826 /* 8827 * Cancel the primary name an free it if it does not require 8828 * journaling. 8829 */ 8830 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum, 8831 0, &inodedep) != 0) { 8832 /* Abort the addref that reference this diradd. */ 8833 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 8834 if (inoref->if_list.wk_type != D_JADDREF) 8835 continue; 8836 jaddref = (struct jaddref *)inoref; 8837 if (jaddref->ja_diradd != dap) 8838 continue; 8839 if (cancel_jaddref(jaddref, inodedep, 8840 &dirrem->dm_jwork) == 0) { 8841 free_jremref(jremref); 8842 jremref = NULL; 8843 } 8844 break; 8845 } 8846 } 8847 /* 8848 * Cancel subordinate names and free them if they do not require 8849 * journaling. 8850 */ 8851 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8852 ump = VFSTOUFS(dap->da_list.wk_mp); 8853 LIST_FOREACH(mkdir, &ump->softdep_mkdirlisthd, md_mkdirs) { 8854 if (mkdir->md_diradd != dap) 8855 continue; 8856 if ((jaddref = mkdir->md_jaddref) == NULL) 8857 continue; 8858 mkdir->md_jaddref = NULL; 8859 if (mkdir->md_state & MKDIR_PARENT) { 8860 if (cancel_jaddref(jaddref, NULL, 8861 &dirrem->dm_jwork) == 0) { 8862 free_jremref(dotdotremref); 8863 dotdotremref = NULL; 8864 } 8865 } else { 8866 if (cancel_jaddref(jaddref, inodedep, 8867 &dirrem->dm_jwork) == 0) { 8868 free_jremref(dotremref); 8869 dotremref = NULL; 8870 } 8871 } 8872 } 8873 } 8874 8875 if (jremref) 8876 journal_jremref(dirrem, jremref, inodedep); 8877 if (dotremref) 8878 journal_jremref(dirrem, dotremref, inodedep); 8879 if (dotdotremref) 8880 journal_jremref(dirrem, dotdotremref, NULL); 8881 jwork_move(&dirrem->dm_jwork, &dap->da_jwork); 8882 free_diradd(dap, &dirrem->dm_jwork); 8883 } 8884 8885 /* 8886 * Free a diradd dependency structure. This routine must be called 8887 * with splbio interrupts blocked. 8888 */ 8889 static void 8890 free_diradd(dap, wkhd) 8891 struct diradd *dap; 8892 struct workhead *wkhd; 8893 { 8894 struct dirrem *dirrem; 8895 struct pagedep *pagedep; 8896 struct inodedep *inodedep; 8897 struct mkdir *mkdir, *nextmd; 8898 struct ufsmount *ump; 8899 8900 ump = VFSTOUFS(dap->da_list.wk_mp); 8901 LOCK_OWNED(ump); 8902 LIST_REMOVE(dap, da_pdlist); 8903 if (dap->da_state & ONWORKLIST) 8904 WORKLIST_REMOVE(&dap->da_list); 8905 if ((dap->da_state & DIRCHG) == 0) { 8906 pagedep = dap->da_pagedep; 8907 } else { 8908 dirrem = dap->da_previous; 8909 pagedep = dirrem->dm_pagedep; 8910 dirrem->dm_dirinum = pagedep->pd_ino; 8911 dirrem->dm_state |= COMPLETE; 8912 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 8913 add_to_worklist(&dirrem->dm_list, 0); 8914 } 8915 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum, 8916 0, &inodedep) != 0) 8917 if (inodedep->id_mkdiradd == dap) 8918 inodedep->id_mkdiradd = NULL; 8919 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8920 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 8921 mkdir = nextmd) { 8922 nextmd = LIST_NEXT(mkdir, md_mkdirs); 8923 if (mkdir->md_diradd != dap) 8924 continue; 8925 dap->da_state &= 8926 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 8927 LIST_REMOVE(mkdir, md_mkdirs); 8928 if (mkdir->md_state & ONWORKLIST) 8929 WORKLIST_REMOVE(&mkdir->md_list); 8930 if (mkdir->md_jaddref != NULL) 8931 panic("free_diradd: Unexpected jaddref"); 8932 WORKITEM_FREE(mkdir, D_MKDIR); 8933 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) 8934 break; 8935 } 8936 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 8937 panic("free_diradd: unfound ref"); 8938 } 8939 if (inodedep) 8940 free_inodedep(inodedep); 8941 /* 8942 * Free any journal segments waiting for the directory write. 8943 */ 8944 handle_jwork(&dap->da_jwork); 8945 WORKITEM_FREE(dap, D_DIRADD); 8946 } 8947 8948 /* 8949 * Directory entry removal dependencies. 8950 * 8951 * When removing a directory entry, the entry's inode pointer must be 8952 * zero'ed on disk before the corresponding inode's link count is decremented 8953 * (possibly freeing the inode for re-use). This dependency is handled by 8954 * updating the directory entry but delaying the inode count reduction until 8955 * after the directory block has been written to disk. After this point, the 8956 * inode count can be decremented whenever it is convenient. 8957 */ 8958 8959 /* 8960 * This routine should be called immediately after removing 8961 * a directory entry. The inode's link count should not be 8962 * decremented by the calling procedure -- the soft updates 8963 * code will do this task when it is safe. 8964 */ 8965 void 8966 softdep_setup_remove(bp, dp, ip, isrmdir) 8967 struct buf *bp; /* buffer containing directory block */ 8968 struct inode *dp; /* inode for the directory being modified */ 8969 struct inode *ip; /* inode for directory entry being removed */ 8970 int isrmdir; /* indicates if doing RMDIR */ 8971 { 8972 struct dirrem *dirrem, *prevdirrem; 8973 struct inodedep *inodedep; 8974 struct ufsmount *ump; 8975 int direct; 8976 8977 ump = ITOUMP(ip); 8978 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 8979 ("softdep_setup_remove called on non-softdep filesystem")); 8980 /* 8981 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want 8982 * newdirrem() to setup the full directory remove which requires 8983 * isrmdir > 1. 8984 */ 8985 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 8986 /* 8987 * Add the dirrem to the inodedep's pending remove list for quick 8988 * discovery later. 8989 */ 8990 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) 8991 panic("softdep_setup_remove: Lost inodedep."); 8992 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 8993 dirrem->dm_state |= ONDEPLIST; 8994 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 8995 8996 /* 8997 * If the COMPLETE flag is clear, then there were no active 8998 * entries and we want to roll back to a zeroed entry until 8999 * the new inode is committed to disk. If the COMPLETE flag is 9000 * set then we have deleted an entry that never made it to 9001 * disk. If the entry we deleted resulted from a name change, 9002 * then the old name still resides on disk. We cannot delete 9003 * its inode (returned to us in prevdirrem) until the zeroed 9004 * directory entry gets to disk. The new inode has never been 9005 * referenced on the disk, so can be deleted immediately. 9006 */ 9007 if ((dirrem->dm_state & COMPLETE) == 0) { 9008 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem, 9009 dm_next); 9010 FREE_LOCK(ump); 9011 } else { 9012 if (prevdirrem != NULL) 9013 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, 9014 prevdirrem, dm_next); 9015 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino; 9016 direct = LIST_EMPTY(&dirrem->dm_jremrefhd); 9017 FREE_LOCK(ump); 9018 if (direct) 9019 handle_workitem_remove(dirrem, 0); 9020 } 9021 } 9022 9023 /* 9024 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the 9025 * pd_pendinghd list of a pagedep. 9026 */ 9027 static struct diradd * 9028 diradd_lookup(pagedep, offset) 9029 struct pagedep *pagedep; 9030 int offset; 9031 { 9032 struct diradd *dap; 9033 9034 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist) 9035 if (dap->da_offset == offset) 9036 return (dap); 9037 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 9038 if (dap->da_offset == offset) 9039 return (dap); 9040 return (NULL); 9041 } 9042 9043 /* 9044 * Search for a .. diradd dependency in a directory that is being removed. 9045 * If the directory was renamed to a new parent we have a diradd rather 9046 * than a mkdir for the .. entry. We need to cancel it now before 9047 * it is found in truncate(). 9048 */ 9049 static struct jremref * 9050 cancel_diradd_dotdot(ip, dirrem, jremref) 9051 struct inode *ip; 9052 struct dirrem *dirrem; 9053 struct jremref *jremref; 9054 { 9055 struct pagedep *pagedep; 9056 struct diradd *dap; 9057 struct worklist *wk; 9058 9059 if (pagedep_lookup(ITOVFS(ip), NULL, ip->i_number, 0, 0, &pagedep) == 0) 9060 return (jremref); 9061 dap = diradd_lookup(pagedep, DOTDOT_OFFSET); 9062 if (dap == NULL) 9063 return (jremref); 9064 cancel_diradd(dap, dirrem, jremref, NULL, NULL); 9065 /* 9066 * Mark any journal work as belonging to the parent so it is freed 9067 * with the .. reference. 9068 */ 9069 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9070 wk->wk_state |= MKDIR_PARENT; 9071 return (NULL); 9072 } 9073 9074 /* 9075 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to 9076 * replace it with a dirrem/diradd pair as a result of re-parenting a 9077 * directory. This ensures that we don't simultaneously have a mkdir and 9078 * a diradd for the same .. entry. 9079 */ 9080 static struct jremref * 9081 cancel_mkdir_dotdot(ip, dirrem, jremref) 9082 struct inode *ip; 9083 struct dirrem *dirrem; 9084 struct jremref *jremref; 9085 { 9086 struct inodedep *inodedep; 9087 struct jaddref *jaddref; 9088 struct ufsmount *ump; 9089 struct mkdir *mkdir; 9090 struct diradd *dap; 9091 struct mount *mp; 9092 9093 mp = ITOVFS(ip); 9094 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9095 return (jremref); 9096 dap = inodedep->id_mkdiradd; 9097 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0) 9098 return (jremref); 9099 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9100 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 9101 mkdir = LIST_NEXT(mkdir, md_mkdirs)) 9102 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT) 9103 break; 9104 if (mkdir == NULL) 9105 panic("cancel_mkdir_dotdot: Unable to find mkdir\n"); 9106 if ((jaddref = mkdir->md_jaddref) != NULL) { 9107 mkdir->md_jaddref = NULL; 9108 jaddref->ja_state &= ~MKDIR_PARENT; 9109 if (inodedep_lookup(mp, jaddref->ja_ino, 0, &inodedep) == 0) 9110 panic("cancel_mkdir_dotdot: Lost parent inodedep"); 9111 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) { 9112 journal_jremref(dirrem, jremref, inodedep); 9113 jremref = NULL; 9114 } 9115 } 9116 if (mkdir->md_state & ONWORKLIST) 9117 WORKLIST_REMOVE(&mkdir->md_list); 9118 mkdir->md_state |= ALLCOMPLETE; 9119 complete_mkdir(mkdir); 9120 return (jremref); 9121 } 9122 9123 static void 9124 journal_jremref(dirrem, jremref, inodedep) 9125 struct dirrem *dirrem; 9126 struct jremref *jremref; 9127 struct inodedep *inodedep; 9128 { 9129 9130 if (inodedep == NULL) 9131 if (inodedep_lookup(jremref->jr_list.wk_mp, 9132 jremref->jr_ref.if_ino, 0, &inodedep) == 0) 9133 panic("journal_jremref: Lost inodedep"); 9134 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps); 9135 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 9136 add_to_journal(&jremref->jr_list); 9137 } 9138 9139 static void 9140 dirrem_journal(dirrem, jremref, dotremref, dotdotremref) 9141 struct dirrem *dirrem; 9142 struct jremref *jremref; 9143 struct jremref *dotremref; 9144 struct jremref *dotdotremref; 9145 { 9146 struct inodedep *inodedep; 9147 9148 9149 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0, 9150 &inodedep) == 0) 9151 panic("dirrem_journal: Lost inodedep"); 9152 journal_jremref(dirrem, jremref, inodedep); 9153 if (dotremref) 9154 journal_jremref(dirrem, dotremref, inodedep); 9155 if (dotdotremref) 9156 journal_jremref(dirrem, dotdotremref, NULL); 9157 } 9158 9159 /* 9160 * Allocate a new dirrem if appropriate and return it along with 9161 * its associated pagedep. Called without a lock, returns with lock. 9162 */ 9163 static struct dirrem * 9164 newdirrem(bp, dp, ip, isrmdir, prevdirremp) 9165 struct buf *bp; /* buffer containing directory block */ 9166 struct inode *dp; /* inode for the directory being modified */ 9167 struct inode *ip; /* inode for directory entry being removed */ 9168 int isrmdir; /* indicates if doing RMDIR */ 9169 struct dirrem **prevdirremp; /* previously referenced inode, if any */ 9170 { 9171 int offset; 9172 ufs_lbn_t lbn; 9173 struct diradd *dap; 9174 struct dirrem *dirrem; 9175 struct pagedep *pagedep; 9176 struct jremref *jremref; 9177 struct jremref *dotremref; 9178 struct jremref *dotdotremref; 9179 struct vnode *dvp; 9180 struct ufsmount *ump; 9181 9182 /* 9183 * Whiteouts have no deletion dependencies. 9184 */ 9185 if (ip == NULL) 9186 panic("newdirrem: whiteout"); 9187 dvp = ITOV(dp); 9188 ump = ITOUMP(dp); 9189 9190 /* 9191 * If the system is over its limit and our filesystem is 9192 * responsible for more than our share of that usage and 9193 * we are not a snapshot, request some inodedep cleanup. 9194 * Limiting the number of dirrem structures will also limit 9195 * the number of freefile and freeblks structures. 9196 */ 9197 ACQUIRE_LOCK(ump); 9198 if (!IS_SNAPSHOT(ip) && softdep_excess_items(ump, D_DIRREM)) 9199 schedule_cleanup(UFSTOVFS(ump)); 9200 else 9201 FREE_LOCK(ump); 9202 dirrem = malloc(sizeof(struct dirrem), M_DIRREM, M_SOFTDEP_FLAGS | 9203 M_ZERO); 9204 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount); 9205 LIST_INIT(&dirrem->dm_jremrefhd); 9206 LIST_INIT(&dirrem->dm_jwork); 9207 dirrem->dm_state = isrmdir ? RMDIR : 0; 9208 dirrem->dm_oldinum = ip->i_number; 9209 *prevdirremp = NULL; 9210 /* 9211 * Allocate remove reference structures to track journal write 9212 * dependencies. We will always have one for the link and 9213 * when doing directories we will always have one more for dot. 9214 * When renaming a directory we skip the dotdot link change so 9215 * this is not needed. 9216 */ 9217 jremref = dotremref = dotdotremref = NULL; 9218 if (DOINGSUJ(dvp)) { 9219 if (isrmdir) { 9220 jremref = newjremref(dirrem, dp, ip, dp->i_offset, 9221 ip->i_effnlink + 2); 9222 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET, 9223 ip->i_effnlink + 1); 9224 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET, 9225 dp->i_effnlink + 1); 9226 dotdotremref->jr_state |= MKDIR_PARENT; 9227 } else 9228 jremref = newjremref(dirrem, dp, ip, dp->i_offset, 9229 ip->i_effnlink + 1); 9230 } 9231 ACQUIRE_LOCK(ump); 9232 lbn = lblkno(ump->um_fs, dp->i_offset); 9233 offset = blkoff(ump->um_fs, dp->i_offset); 9234 pagedep_lookup(UFSTOVFS(ump), bp, dp->i_number, lbn, DEPALLOC, 9235 &pagedep); 9236 dirrem->dm_pagedep = pagedep; 9237 dirrem->dm_offset = offset; 9238 /* 9239 * If we're renaming a .. link to a new directory, cancel any 9240 * existing MKDIR_PARENT mkdir. If it has already been canceled 9241 * the jremref is preserved for any potential diradd in this 9242 * location. This can not coincide with a rmdir. 9243 */ 9244 if (dp->i_offset == DOTDOT_OFFSET) { 9245 if (isrmdir) 9246 panic("newdirrem: .. directory change during remove?"); 9247 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref); 9248 } 9249 /* 9250 * If we're removing a directory search for the .. dependency now and 9251 * cancel it. Any pending journal work will be added to the dirrem 9252 * to be completed when the workitem remove completes. 9253 */ 9254 if (isrmdir) 9255 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref); 9256 /* 9257 * Check for a diradd dependency for the same directory entry. 9258 * If present, then both dependencies become obsolete and can 9259 * be de-allocated. 9260 */ 9261 dap = diradd_lookup(pagedep, offset); 9262 if (dap == NULL) { 9263 /* 9264 * Link the jremref structures into the dirrem so they are 9265 * written prior to the pagedep. 9266 */ 9267 if (jremref) 9268 dirrem_journal(dirrem, jremref, dotremref, 9269 dotdotremref); 9270 return (dirrem); 9271 } 9272 /* 9273 * Must be ATTACHED at this point. 9274 */ 9275 if ((dap->da_state & ATTACHED) == 0) 9276 panic("newdirrem: not ATTACHED"); 9277 if (dap->da_newinum != ip->i_number) 9278 panic("newdirrem: inum %ju should be %ju", 9279 (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum); 9280 /* 9281 * If we are deleting a changed name that never made it to disk, 9282 * then return the dirrem describing the previous inode (which 9283 * represents the inode currently referenced from this entry on disk). 9284 */ 9285 if ((dap->da_state & DIRCHG) != 0) { 9286 *prevdirremp = dap->da_previous; 9287 dap->da_state &= ~DIRCHG; 9288 dap->da_pagedep = pagedep; 9289 } 9290 /* 9291 * We are deleting an entry that never made it to disk. 9292 * Mark it COMPLETE so we can delete its inode immediately. 9293 */ 9294 dirrem->dm_state |= COMPLETE; 9295 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref); 9296 #ifdef SUJ_DEBUG 9297 if (isrmdir == 0) { 9298 struct worklist *wk; 9299 9300 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9301 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT)) 9302 panic("bad wk %p (0x%X)\n", wk, wk->wk_state); 9303 } 9304 #endif 9305 9306 return (dirrem); 9307 } 9308 9309 /* 9310 * Directory entry change dependencies. 9311 * 9312 * Changing an existing directory entry requires that an add operation 9313 * be completed first followed by a deletion. The semantics for the addition 9314 * are identical to the description of adding a new entry above except 9315 * that the rollback is to the old inode number rather than zero. Once 9316 * the addition dependency is completed, the removal is done as described 9317 * in the removal routine above. 9318 */ 9319 9320 /* 9321 * This routine should be called immediately after changing 9322 * a directory entry. The inode's link count should not be 9323 * decremented by the calling procedure -- the soft updates 9324 * code will perform this task when it is safe. 9325 */ 9326 void 9327 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 9328 struct buf *bp; /* buffer containing directory block */ 9329 struct inode *dp; /* inode for the directory being modified */ 9330 struct inode *ip; /* inode for directory entry being removed */ 9331 ino_t newinum; /* new inode number for changed entry */ 9332 int isrmdir; /* indicates if doing RMDIR */ 9333 { 9334 int offset; 9335 struct diradd *dap = NULL; 9336 struct dirrem *dirrem, *prevdirrem; 9337 struct pagedep *pagedep; 9338 struct inodedep *inodedep; 9339 struct jaddref *jaddref; 9340 struct mount *mp; 9341 struct ufsmount *ump; 9342 9343 mp = ITOVFS(dp); 9344 ump = VFSTOUFS(mp); 9345 offset = blkoff(ump->um_fs, dp->i_offset); 9346 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 9347 ("softdep_setup_directory_change called on non-softdep filesystem")); 9348 9349 /* 9350 * Whiteouts do not need diradd dependencies. 9351 */ 9352 if (newinum != UFS_WINO) { 9353 dap = malloc(sizeof(struct diradd), 9354 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO); 9355 workitem_alloc(&dap->da_list, D_DIRADD, mp); 9356 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE; 9357 dap->da_offset = offset; 9358 dap->da_newinum = newinum; 9359 LIST_INIT(&dap->da_jwork); 9360 } 9361 9362 /* 9363 * Allocate a new dirrem and ACQUIRE_LOCK. 9364 */ 9365 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 9366 pagedep = dirrem->dm_pagedep; 9367 /* 9368 * The possible values for isrmdir: 9369 * 0 - non-directory file rename 9370 * 1 - directory rename within same directory 9371 * inum - directory rename to new directory of given inode number 9372 * When renaming to a new directory, we are both deleting and 9373 * creating a new directory entry, so the link count on the new 9374 * directory should not change. Thus we do not need the followup 9375 * dirrem which is usually done in handle_workitem_remove. We set 9376 * the DIRCHG flag to tell handle_workitem_remove to skip the 9377 * followup dirrem. 9378 */ 9379 if (isrmdir > 1) 9380 dirrem->dm_state |= DIRCHG; 9381 9382 /* 9383 * Whiteouts have no additional dependencies, 9384 * so just put the dirrem on the correct list. 9385 */ 9386 if (newinum == UFS_WINO) { 9387 if ((dirrem->dm_state & COMPLETE) == 0) { 9388 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem, 9389 dm_next); 9390 } else { 9391 dirrem->dm_dirinum = pagedep->pd_ino; 9392 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9393 add_to_worklist(&dirrem->dm_list, 0); 9394 } 9395 FREE_LOCK(ump); 9396 return; 9397 } 9398 /* 9399 * Add the dirrem to the inodedep's pending remove list for quick 9400 * discovery later. A valid nlinkdelta ensures that this lookup 9401 * will not fail. 9402 */ 9403 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9404 panic("softdep_setup_directory_change: Lost inodedep."); 9405 dirrem->dm_state |= ONDEPLIST; 9406 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9407 9408 /* 9409 * If the COMPLETE flag is clear, then there were no active 9410 * entries and we want to roll back to the previous inode until 9411 * the new inode is committed to disk. If the COMPLETE flag is 9412 * set, then we have deleted an entry that never made it to disk. 9413 * If the entry we deleted resulted from a name change, then the old 9414 * inode reference still resides on disk. Any rollback that we do 9415 * needs to be to that old inode (returned to us in prevdirrem). If 9416 * the entry we deleted resulted from a create, then there is 9417 * no entry on the disk, so we want to roll back to zero rather 9418 * than the uncommitted inode. In either of the COMPLETE cases we 9419 * want to immediately free the unwritten and unreferenced inode. 9420 */ 9421 if ((dirrem->dm_state & COMPLETE) == 0) { 9422 dap->da_previous = dirrem; 9423 } else { 9424 if (prevdirrem != NULL) { 9425 dap->da_previous = prevdirrem; 9426 } else { 9427 dap->da_state &= ~DIRCHG; 9428 dap->da_pagedep = pagedep; 9429 } 9430 dirrem->dm_dirinum = pagedep->pd_ino; 9431 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9432 add_to_worklist(&dirrem->dm_list, 0); 9433 } 9434 /* 9435 * Lookup the jaddref for this journal entry. We must finish 9436 * initializing it and make the diradd write dependent on it. 9437 * If we're not journaling, put it on the id_bufwait list if the 9438 * inode is not yet written. If it is written, do the post-inode 9439 * write processing to put it on the id_pendinghd list. 9440 */ 9441 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 9442 if (MOUNTEDSUJ(mp)) { 9443 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 9444 inoreflst); 9445 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 9446 ("softdep_setup_directory_change: bad jaddref %p", 9447 jaddref)); 9448 jaddref->ja_diroff = dp->i_offset; 9449 jaddref->ja_diradd = dap; 9450 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9451 dap, da_pdlist); 9452 add_to_journal(&jaddref->ja_list); 9453 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 9454 dap->da_state |= COMPLETE; 9455 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 9456 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 9457 } else { 9458 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9459 dap, da_pdlist); 9460 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 9461 } 9462 /* 9463 * If we're making a new name for a directory that has not been 9464 * committed when need to move the dot and dotdot references to 9465 * this new name. 9466 */ 9467 if (inodedep->id_mkdiradd && dp->i_offset != DOTDOT_OFFSET) 9468 merge_diradd(inodedep, dap); 9469 FREE_LOCK(ump); 9470 } 9471 9472 /* 9473 * Called whenever the link count on an inode is changed. 9474 * It creates an inode dependency so that the new reference(s) 9475 * to the inode cannot be committed to disk until the updated 9476 * inode has been written. 9477 */ 9478 void 9479 softdep_change_linkcnt(ip) 9480 struct inode *ip; /* the inode with the increased link count */ 9481 { 9482 struct inodedep *inodedep; 9483 struct ufsmount *ump; 9484 9485 ump = ITOUMP(ip); 9486 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9487 ("softdep_change_linkcnt called on non-softdep filesystem")); 9488 ACQUIRE_LOCK(ump); 9489 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep); 9490 if (ip->i_nlink < ip->i_effnlink) 9491 panic("softdep_change_linkcnt: bad delta"); 9492 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9493 FREE_LOCK(ump); 9494 } 9495 9496 /* 9497 * Attach a sbdep dependency to the superblock buf so that we can keep 9498 * track of the head of the linked list of referenced but unlinked inodes. 9499 */ 9500 void 9501 softdep_setup_sbupdate(ump, fs, bp) 9502 struct ufsmount *ump; 9503 struct fs *fs; 9504 struct buf *bp; 9505 { 9506 struct sbdep *sbdep; 9507 struct worklist *wk; 9508 9509 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9510 ("softdep_setup_sbupdate called on non-softdep filesystem")); 9511 LIST_FOREACH(wk, &bp->b_dep, wk_list) 9512 if (wk->wk_type == D_SBDEP) 9513 break; 9514 if (wk != NULL) 9515 return; 9516 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS); 9517 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump)); 9518 sbdep->sb_fs = fs; 9519 sbdep->sb_ump = ump; 9520 ACQUIRE_LOCK(ump); 9521 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list); 9522 FREE_LOCK(ump); 9523 } 9524 9525 /* 9526 * Return the first unlinked inodedep which is ready to be the head of the 9527 * list. The inodedep and all those after it must have valid next pointers. 9528 */ 9529 static struct inodedep * 9530 first_unlinked_inodedep(ump) 9531 struct ufsmount *ump; 9532 { 9533 struct inodedep *inodedep; 9534 struct inodedep *idp; 9535 9536 LOCK_OWNED(ump); 9537 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst); 9538 inodedep; inodedep = idp) { 9539 if ((inodedep->id_state & UNLINKNEXT) == 0) 9540 return (NULL); 9541 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9542 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0) 9543 break; 9544 if ((inodedep->id_state & UNLINKPREV) == 0) 9545 break; 9546 } 9547 return (inodedep); 9548 } 9549 9550 /* 9551 * Set the sujfree unlinked head pointer prior to writing a superblock. 9552 */ 9553 static void 9554 initiate_write_sbdep(sbdep) 9555 struct sbdep *sbdep; 9556 { 9557 struct inodedep *inodedep; 9558 struct fs *bpfs; 9559 struct fs *fs; 9560 9561 bpfs = sbdep->sb_fs; 9562 fs = sbdep->sb_ump->um_fs; 9563 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9564 if (inodedep) { 9565 fs->fs_sujfree = inodedep->id_ino; 9566 inodedep->id_state |= UNLINKPREV; 9567 } else 9568 fs->fs_sujfree = 0; 9569 bpfs->fs_sujfree = fs->fs_sujfree; 9570 } 9571 9572 /* 9573 * After a superblock is written determine whether it must be written again 9574 * due to a changing unlinked list head. 9575 */ 9576 static int 9577 handle_written_sbdep(sbdep, bp) 9578 struct sbdep *sbdep; 9579 struct buf *bp; 9580 { 9581 struct inodedep *inodedep; 9582 struct fs *fs; 9583 9584 LOCK_OWNED(sbdep->sb_ump); 9585 fs = sbdep->sb_fs; 9586 /* 9587 * If the superblock doesn't match the in-memory list start over. 9588 */ 9589 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9590 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) || 9591 (inodedep == NULL && fs->fs_sujfree != 0)) { 9592 bdirty(bp); 9593 return (1); 9594 } 9595 WORKITEM_FREE(sbdep, D_SBDEP); 9596 if (fs->fs_sujfree == 0) 9597 return (0); 9598 /* 9599 * Now that we have a record of this inode in stable store allow it 9600 * to be written to free up pending work. Inodes may see a lot of 9601 * write activity after they are unlinked which we must not hold up. 9602 */ 9603 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 9604 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS) 9605 panic("handle_written_sbdep: Bad inodedep %p (0x%X)", 9606 inodedep, inodedep->id_state); 9607 if (inodedep->id_state & UNLINKONLIST) 9608 break; 9609 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST; 9610 } 9611 9612 return (0); 9613 } 9614 9615 /* 9616 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list. 9617 */ 9618 static void 9619 unlinked_inodedep(mp, inodedep) 9620 struct mount *mp; 9621 struct inodedep *inodedep; 9622 { 9623 struct ufsmount *ump; 9624 9625 ump = VFSTOUFS(mp); 9626 LOCK_OWNED(ump); 9627 if (MOUNTEDSUJ(mp) == 0) 9628 return; 9629 ump->um_fs->fs_fmod = 1; 9630 if (inodedep->id_state & UNLINKED) 9631 panic("unlinked_inodedep: %p already unlinked\n", inodedep); 9632 inodedep->id_state |= UNLINKED; 9633 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked); 9634 } 9635 9636 /* 9637 * Remove an inodedep from the unlinked inodedep list. This may require 9638 * disk writes if the inode has made it that far. 9639 */ 9640 static void 9641 clear_unlinked_inodedep(inodedep) 9642 struct inodedep *inodedep; 9643 { 9644 struct ufs2_dinode *dip; 9645 struct ufsmount *ump; 9646 struct inodedep *idp; 9647 struct inodedep *idn; 9648 struct fs *fs; 9649 struct buf *bp; 9650 ino_t ino; 9651 ino_t nino; 9652 ino_t pino; 9653 int error; 9654 9655 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9656 fs = ump->um_fs; 9657 ino = inodedep->id_ino; 9658 error = 0; 9659 for (;;) { 9660 LOCK_OWNED(ump); 9661 KASSERT((inodedep->id_state & UNLINKED) != 0, 9662 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9663 inodedep)); 9664 /* 9665 * If nothing has yet been written simply remove us from 9666 * the in memory list and return. This is the most common 9667 * case where handle_workitem_remove() loses the final 9668 * reference. 9669 */ 9670 if ((inodedep->id_state & UNLINKLINKS) == 0) 9671 break; 9672 /* 9673 * If we have a NEXT pointer and no PREV pointer we can simply 9674 * clear NEXT's PREV and remove ourselves from the list. Be 9675 * careful not to clear PREV if the superblock points at 9676 * next as well. 9677 */ 9678 idn = TAILQ_NEXT(inodedep, id_unlinked); 9679 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) { 9680 if (idn && fs->fs_sujfree != idn->id_ino) 9681 idn->id_state &= ~UNLINKPREV; 9682 break; 9683 } 9684 /* 9685 * Here we have an inodedep which is actually linked into 9686 * the list. We must remove it by forcing a write to the 9687 * link before us, whether it be the superblock or an inode. 9688 * Unfortunately the list may change while we're waiting 9689 * on the buf lock for either resource so we must loop until 9690 * we lock the right one. If both the superblock and an 9691 * inode point to this inode we must clear the inode first 9692 * followed by the superblock. 9693 */ 9694 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9695 pino = 0; 9696 if (idp && (idp->id_state & UNLINKNEXT)) 9697 pino = idp->id_ino; 9698 FREE_LOCK(ump); 9699 if (pino == 0) { 9700 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9701 (int)fs->fs_sbsize, 0, 0, 0); 9702 } else { 9703 error = bread(ump->um_devvp, 9704 fsbtodb(fs, ino_to_fsba(fs, pino)), 9705 (int)fs->fs_bsize, NOCRED, &bp); 9706 if (error) 9707 brelse(bp); 9708 } 9709 ACQUIRE_LOCK(ump); 9710 if (error) 9711 break; 9712 /* If the list has changed restart the loop. */ 9713 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9714 nino = 0; 9715 if (idp && (idp->id_state & UNLINKNEXT)) 9716 nino = idp->id_ino; 9717 if (nino != pino || 9718 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) { 9719 FREE_LOCK(ump); 9720 brelse(bp); 9721 ACQUIRE_LOCK(ump); 9722 continue; 9723 } 9724 nino = 0; 9725 idn = TAILQ_NEXT(inodedep, id_unlinked); 9726 if (idn) 9727 nino = idn->id_ino; 9728 /* 9729 * Remove us from the in memory list. After this we cannot 9730 * access the inodedep. 9731 */ 9732 KASSERT((inodedep->id_state & UNLINKED) != 0, 9733 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9734 inodedep)); 9735 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9736 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9737 FREE_LOCK(ump); 9738 /* 9739 * The predecessor's next pointer is manually updated here 9740 * so that the NEXT flag is never cleared for an element 9741 * that is in the list. 9742 */ 9743 if (pino == 0) { 9744 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 9745 ffs_oldfscompat_write((struct fs *)bp->b_data, ump); 9746 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, 9747 bp); 9748 } else if (fs->fs_magic == FS_UFS1_MAGIC) { 9749 ((struct ufs1_dinode *)bp->b_data + 9750 ino_to_fsbo(fs, pino))->di_freelink = nino; 9751 } else { 9752 dip = (struct ufs2_dinode *)bp->b_data + 9753 ino_to_fsbo(fs, pino); 9754 dip->di_freelink = nino; 9755 } 9756 /* 9757 * If the bwrite fails we have no recourse to recover. The 9758 * filesystem is corrupted already. 9759 */ 9760 bwrite(bp); 9761 ACQUIRE_LOCK(ump); 9762 /* 9763 * If the superblock pointer still needs to be cleared force 9764 * a write here. 9765 */ 9766 if (fs->fs_sujfree == ino) { 9767 FREE_LOCK(ump); 9768 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9769 (int)fs->fs_sbsize, 0, 0, 0); 9770 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 9771 ffs_oldfscompat_write((struct fs *)bp->b_data, ump); 9772 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, 9773 bp); 9774 bwrite(bp); 9775 ACQUIRE_LOCK(ump); 9776 } 9777 9778 if (fs->fs_sujfree != ino) 9779 return; 9780 panic("clear_unlinked_inodedep: Failed to clear free head"); 9781 } 9782 if (inodedep->id_ino == fs->fs_sujfree) 9783 panic("clear_unlinked_inodedep: Freeing head of free list"); 9784 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9785 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9786 return; 9787 } 9788 9789 /* 9790 * This workitem decrements the inode's link count. 9791 * If the link count reaches zero, the file is removed. 9792 */ 9793 static int 9794 handle_workitem_remove(dirrem, flags) 9795 struct dirrem *dirrem; 9796 int flags; 9797 { 9798 struct inodedep *inodedep; 9799 struct workhead dotdotwk; 9800 struct worklist *wk; 9801 struct ufsmount *ump; 9802 struct mount *mp; 9803 struct vnode *vp; 9804 struct inode *ip; 9805 ino_t oldinum; 9806 9807 if (dirrem->dm_state & ONWORKLIST) 9808 panic("handle_workitem_remove: dirrem %p still on worklist", 9809 dirrem); 9810 oldinum = dirrem->dm_oldinum; 9811 mp = dirrem->dm_list.wk_mp; 9812 ump = VFSTOUFS(mp); 9813 flags |= LK_EXCLUSIVE; 9814 if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ) != 0) 9815 return (EBUSY); 9816 ip = VTOI(vp); 9817 ACQUIRE_LOCK(ump); 9818 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0) 9819 panic("handle_workitem_remove: lost inodedep"); 9820 if (dirrem->dm_state & ONDEPLIST) 9821 LIST_REMOVE(dirrem, dm_inonext); 9822 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 9823 ("handle_workitem_remove: Journal entries not written.")); 9824 9825 /* 9826 * Move all dependencies waiting on the remove to complete 9827 * from the dirrem to the inode inowait list to be completed 9828 * after the inode has been updated and written to disk. Any 9829 * marked MKDIR_PARENT are saved to be completed when the .. ref 9830 * is removed. 9831 */ 9832 LIST_INIT(&dotdotwk); 9833 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) { 9834 WORKLIST_REMOVE(wk); 9835 if (wk->wk_state & MKDIR_PARENT) { 9836 wk->wk_state &= ~MKDIR_PARENT; 9837 WORKLIST_INSERT(&dotdotwk, wk); 9838 continue; 9839 } 9840 WORKLIST_INSERT(&inodedep->id_inowait, wk); 9841 } 9842 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list); 9843 /* 9844 * Normal file deletion. 9845 */ 9846 if ((dirrem->dm_state & RMDIR) == 0) { 9847 ip->i_nlink--; 9848 DIP_SET(ip, i_nlink, ip->i_nlink); 9849 ip->i_flag |= IN_CHANGE; 9850 if (ip->i_nlink < ip->i_effnlink) 9851 panic("handle_workitem_remove: bad file delta"); 9852 if (ip->i_nlink == 0) 9853 unlinked_inodedep(mp, inodedep); 9854 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9855 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 9856 ("handle_workitem_remove: worklist not empty. %s", 9857 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type))); 9858 WORKITEM_FREE(dirrem, D_DIRREM); 9859 FREE_LOCK(ump); 9860 goto out; 9861 } 9862 /* 9863 * Directory deletion. Decrement reference count for both the 9864 * just deleted parent directory entry and the reference for ".". 9865 * Arrange to have the reference count on the parent decremented 9866 * to account for the loss of "..". 9867 */ 9868 ip->i_nlink -= 2; 9869 DIP_SET(ip, i_nlink, ip->i_nlink); 9870 ip->i_flag |= IN_CHANGE; 9871 if (ip->i_nlink < ip->i_effnlink) 9872 panic("handle_workitem_remove: bad dir delta"); 9873 if (ip->i_nlink == 0) 9874 unlinked_inodedep(mp, inodedep); 9875 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9876 /* 9877 * Rename a directory to a new parent. Since, we are both deleting 9878 * and creating a new directory entry, the link count on the new 9879 * directory should not change. Thus we skip the followup dirrem. 9880 */ 9881 if (dirrem->dm_state & DIRCHG) { 9882 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 9883 ("handle_workitem_remove: DIRCHG and worklist not empty.")); 9884 WORKITEM_FREE(dirrem, D_DIRREM); 9885 FREE_LOCK(ump); 9886 goto out; 9887 } 9888 dirrem->dm_state = ONDEPLIST; 9889 dirrem->dm_oldinum = dirrem->dm_dirinum; 9890 /* 9891 * Place the dirrem on the parent's diremhd list. 9892 */ 9893 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0) 9894 panic("handle_workitem_remove: lost dir inodedep"); 9895 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9896 /* 9897 * If the allocated inode has never been written to disk, then 9898 * the on-disk inode is zero'ed and we can remove the file 9899 * immediately. When journaling if the inode has been marked 9900 * unlinked and not DEPCOMPLETE we know it can never be written. 9901 */ 9902 inodedep_lookup(mp, oldinum, 0, &inodedep); 9903 if (inodedep == NULL || 9904 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED || 9905 check_inode_unwritten(inodedep)) { 9906 FREE_LOCK(ump); 9907 vput(vp); 9908 return handle_workitem_remove(dirrem, flags); 9909 } 9910 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list); 9911 FREE_LOCK(ump); 9912 ip->i_flag |= IN_CHANGE; 9913 out: 9914 ffs_update(vp, 0); 9915 vput(vp); 9916 return (0); 9917 } 9918 9919 /* 9920 * Inode de-allocation dependencies. 9921 * 9922 * When an inode's link count is reduced to zero, it can be de-allocated. We 9923 * found it convenient to postpone de-allocation until after the inode is 9924 * written to disk with its new link count (zero). At this point, all of the 9925 * on-disk inode's block pointers are nullified and, with careful dependency 9926 * list ordering, all dependencies related to the inode will be satisfied and 9927 * the corresponding dependency structures de-allocated. So, if/when the 9928 * inode is reused, there will be no mixing of old dependencies with new 9929 * ones. This artificial dependency is set up by the block de-allocation 9930 * procedure above (softdep_setup_freeblocks) and completed by the 9931 * following procedure. 9932 */ 9933 static void 9934 handle_workitem_freefile(freefile) 9935 struct freefile *freefile; 9936 { 9937 struct workhead wkhd; 9938 struct fs *fs; 9939 struct inodedep *idp; 9940 struct ufsmount *ump; 9941 int error; 9942 9943 ump = VFSTOUFS(freefile->fx_list.wk_mp); 9944 fs = ump->um_fs; 9945 #ifdef DEBUG 9946 ACQUIRE_LOCK(ump); 9947 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp); 9948 FREE_LOCK(ump); 9949 if (error) 9950 panic("handle_workitem_freefile: inodedep %p survived", idp); 9951 #endif 9952 UFS_LOCK(ump); 9953 fs->fs_pendinginodes -= 1; 9954 UFS_UNLOCK(ump); 9955 LIST_INIT(&wkhd); 9956 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list); 9957 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp, 9958 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0) 9959 softdep_error("handle_workitem_freefile", error); 9960 ACQUIRE_LOCK(ump); 9961 WORKITEM_FREE(freefile, D_FREEFILE); 9962 FREE_LOCK(ump); 9963 } 9964 9965 9966 /* 9967 * Helper function which unlinks marker element from work list and returns 9968 * the next element on the list. 9969 */ 9970 static __inline struct worklist * 9971 markernext(struct worklist *marker) 9972 { 9973 struct worklist *next; 9974 9975 next = LIST_NEXT(marker, wk_list); 9976 LIST_REMOVE(marker, wk_list); 9977 return next; 9978 } 9979 9980 /* 9981 * Disk writes. 9982 * 9983 * The dependency structures constructed above are most actively used when file 9984 * system blocks are written to disk. No constraints are placed on when a 9985 * block can be written, but unsatisfied update dependencies are made safe by 9986 * modifying (or replacing) the source memory for the duration of the disk 9987 * write. When the disk write completes, the memory block is again brought 9988 * up-to-date. 9989 * 9990 * In-core inode structure reclamation. 9991 * 9992 * Because there are a finite number of "in-core" inode structures, they are 9993 * reused regularly. By transferring all inode-related dependencies to the 9994 * in-memory inode block and indexing them separately (via "inodedep"s), we 9995 * can allow "in-core" inode structures to be reused at any time and avoid 9996 * any increase in contention. 9997 * 9998 * Called just before entering the device driver to initiate a new disk I/O. 9999 * The buffer must be locked, thus, no I/O completion operations can occur 10000 * while we are manipulating its associated dependencies. 10001 */ 10002 static void 10003 softdep_disk_io_initiation(bp) 10004 struct buf *bp; /* structure describing disk write to occur */ 10005 { 10006 struct worklist *wk; 10007 struct worklist marker; 10008 struct inodedep *inodedep; 10009 struct freeblks *freeblks; 10010 struct jblkdep *jblkdep; 10011 struct newblk *newblk; 10012 struct ufsmount *ump; 10013 10014 /* 10015 * We only care about write operations. There should never 10016 * be dependencies for reads. 10017 */ 10018 if (bp->b_iocmd != BIO_WRITE) 10019 panic("softdep_disk_io_initiation: not write"); 10020 10021 if (bp->b_vflags & BV_BKGRDINPROG) 10022 panic("softdep_disk_io_initiation: Writing buffer with " 10023 "background write in progress: %p", bp); 10024 10025 ump = softdep_bp_to_mp(bp); 10026 if (ump == NULL) 10027 return; 10028 10029 marker.wk_type = D_LAST + 1; /* Not a normal workitem */ 10030 PHOLD(curproc); /* Don't swap out kernel stack */ 10031 ACQUIRE_LOCK(ump); 10032 /* 10033 * Do any necessary pre-I/O processing. 10034 */ 10035 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL; 10036 wk = markernext(&marker)) { 10037 LIST_INSERT_AFTER(wk, &marker, wk_list); 10038 switch (wk->wk_type) { 10039 10040 case D_PAGEDEP: 10041 initiate_write_filepage(WK_PAGEDEP(wk), bp); 10042 continue; 10043 10044 case D_INODEDEP: 10045 inodedep = WK_INODEDEP(wk); 10046 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) 10047 initiate_write_inodeblock_ufs1(inodedep, bp); 10048 else 10049 initiate_write_inodeblock_ufs2(inodedep, bp); 10050 continue; 10051 10052 case D_INDIRDEP: 10053 initiate_write_indirdep(WK_INDIRDEP(wk), bp); 10054 continue; 10055 10056 case D_BMSAFEMAP: 10057 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp); 10058 continue; 10059 10060 case D_JSEG: 10061 WK_JSEG(wk)->js_buf = NULL; 10062 continue; 10063 10064 case D_FREEBLKS: 10065 freeblks = WK_FREEBLKS(wk); 10066 jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd); 10067 /* 10068 * We have to wait for the freeblks to be journaled 10069 * before we can write an inodeblock with updated 10070 * pointers. Be careful to arrange the marker so 10071 * we revisit the freeblks if it's not removed by 10072 * the first jwait(). 10073 */ 10074 if (jblkdep != NULL) { 10075 LIST_REMOVE(&marker, wk_list); 10076 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10077 jwait(&jblkdep->jb_list, MNT_WAIT); 10078 } 10079 continue; 10080 case D_ALLOCDIRECT: 10081 case D_ALLOCINDIR: 10082 /* 10083 * We have to wait for the jnewblk to be journaled 10084 * before we can write to a block if the contents 10085 * may be confused with an earlier file's indirect 10086 * at recovery time. Handle the marker as described 10087 * above. 10088 */ 10089 newblk = WK_NEWBLK(wk); 10090 if (newblk->nb_jnewblk != NULL && 10091 indirblk_lookup(newblk->nb_list.wk_mp, 10092 newblk->nb_newblkno)) { 10093 LIST_REMOVE(&marker, wk_list); 10094 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10095 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 10096 } 10097 continue; 10098 10099 case D_SBDEP: 10100 initiate_write_sbdep(WK_SBDEP(wk)); 10101 continue; 10102 10103 case D_MKDIR: 10104 case D_FREEWORK: 10105 case D_FREEDEP: 10106 case D_JSEGDEP: 10107 continue; 10108 10109 default: 10110 panic("handle_disk_io_initiation: Unexpected type %s", 10111 TYPENAME(wk->wk_type)); 10112 /* NOTREACHED */ 10113 } 10114 } 10115 FREE_LOCK(ump); 10116 PRELE(curproc); /* Allow swapout of kernel stack */ 10117 } 10118 10119 /* 10120 * Called from within the procedure above to deal with unsatisfied 10121 * allocation dependencies in a directory. The buffer must be locked, 10122 * thus, no I/O completion operations can occur while we are 10123 * manipulating its associated dependencies. 10124 */ 10125 static void 10126 initiate_write_filepage(pagedep, bp) 10127 struct pagedep *pagedep; 10128 struct buf *bp; 10129 { 10130 struct jremref *jremref; 10131 struct jmvref *jmvref; 10132 struct dirrem *dirrem; 10133 struct diradd *dap; 10134 struct direct *ep; 10135 int i; 10136 10137 if (pagedep->pd_state & IOSTARTED) { 10138 /* 10139 * This can only happen if there is a driver that does not 10140 * understand chaining. Here biodone will reissue the call 10141 * to strategy for the incomplete buffers. 10142 */ 10143 printf("initiate_write_filepage: already started\n"); 10144 return; 10145 } 10146 pagedep->pd_state |= IOSTARTED; 10147 /* 10148 * Wait for all journal remove dependencies to hit the disk. 10149 * We can not allow any potentially conflicting directory adds 10150 * to be visible before removes and rollback is too difficult. 10151 * The per-filesystem lock may be dropped and re-acquired, however 10152 * we hold the buf locked so the dependency can not go away. 10153 */ 10154 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) 10155 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) 10156 jwait(&jremref->jr_list, MNT_WAIT); 10157 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) 10158 jwait(&jmvref->jm_list, MNT_WAIT); 10159 for (i = 0; i < DAHASHSZ; i++) { 10160 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 10161 ep = (struct direct *) 10162 ((char *)bp->b_data + dap->da_offset); 10163 if (ep->d_ino != dap->da_newinum) 10164 panic("%s: dir inum %ju != new %ju", 10165 "initiate_write_filepage", 10166 (uintmax_t)ep->d_ino, 10167 (uintmax_t)dap->da_newinum); 10168 if (dap->da_state & DIRCHG) 10169 ep->d_ino = dap->da_previous->dm_oldinum; 10170 else 10171 ep->d_ino = 0; 10172 dap->da_state &= ~ATTACHED; 10173 dap->da_state |= UNDONE; 10174 } 10175 } 10176 } 10177 10178 /* 10179 * Version of initiate_write_inodeblock that handles UFS1 dinodes. 10180 * Note that any bug fixes made to this routine must be done in the 10181 * version found below. 10182 * 10183 * Called from within the procedure above to deal with unsatisfied 10184 * allocation dependencies in an inodeblock. The buffer must be 10185 * locked, thus, no I/O completion operations can occur while we 10186 * are manipulating its associated dependencies. 10187 */ 10188 static void 10189 initiate_write_inodeblock_ufs1(inodedep, bp) 10190 struct inodedep *inodedep; 10191 struct buf *bp; /* The inode block */ 10192 { 10193 struct allocdirect *adp, *lastadp; 10194 struct ufs1_dinode *dp; 10195 struct ufs1_dinode *sip; 10196 struct inoref *inoref; 10197 struct ufsmount *ump; 10198 struct fs *fs; 10199 ufs_lbn_t i; 10200 #ifdef INVARIANTS 10201 ufs_lbn_t prevlbn = 0; 10202 #endif 10203 int deplist; 10204 10205 if (inodedep->id_state & IOSTARTED) 10206 panic("initiate_write_inodeblock_ufs1: already started"); 10207 inodedep->id_state |= IOSTARTED; 10208 fs = inodedep->id_fs; 10209 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10210 LOCK_OWNED(ump); 10211 dp = (struct ufs1_dinode *)bp->b_data + 10212 ino_to_fsbo(fs, inodedep->id_ino); 10213 10214 /* 10215 * If we're on the unlinked list but have not yet written our 10216 * next pointer initialize it here. 10217 */ 10218 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10219 struct inodedep *inon; 10220 10221 inon = TAILQ_NEXT(inodedep, id_unlinked); 10222 dp->di_freelink = inon ? inon->id_ino : 0; 10223 } 10224 /* 10225 * If the bitmap is not yet written, then the allocated 10226 * inode cannot be written to disk. 10227 */ 10228 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10229 if (inodedep->id_savedino1 != NULL) 10230 panic("initiate_write_inodeblock_ufs1: I/O underway"); 10231 FREE_LOCK(ump); 10232 sip = malloc(sizeof(struct ufs1_dinode), 10233 M_SAVEDINO, M_SOFTDEP_FLAGS); 10234 ACQUIRE_LOCK(ump); 10235 inodedep->id_savedino1 = sip; 10236 *inodedep->id_savedino1 = *dp; 10237 bzero((caddr_t)dp, sizeof(struct ufs1_dinode)); 10238 dp->di_gen = inodedep->id_savedino1->di_gen; 10239 dp->di_freelink = inodedep->id_savedino1->di_freelink; 10240 return; 10241 } 10242 /* 10243 * If no dependencies, then there is nothing to roll back. 10244 */ 10245 inodedep->id_savedsize = dp->di_size; 10246 inodedep->id_savedextsize = 0; 10247 inodedep->id_savednlink = dp->di_nlink; 10248 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10249 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10250 return; 10251 /* 10252 * Revert the link count to that of the first unwritten journal entry. 10253 */ 10254 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10255 if (inoref) 10256 dp->di_nlink = inoref->if_nlink; 10257 /* 10258 * Set the dependencies to busy. 10259 */ 10260 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10261 adp = TAILQ_NEXT(adp, ad_next)) { 10262 #ifdef INVARIANTS 10263 if (deplist != 0 && prevlbn >= adp->ad_offset) 10264 panic("softdep_write_inodeblock: lbn order"); 10265 prevlbn = adp->ad_offset; 10266 if (adp->ad_offset < UFS_NDADDR && 10267 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10268 panic("initiate_write_inodeblock_ufs1: " 10269 "direct pointer #%jd mismatch %d != %jd", 10270 (intmax_t)adp->ad_offset, 10271 dp->di_db[adp->ad_offset], 10272 (intmax_t)adp->ad_newblkno); 10273 if (adp->ad_offset >= UFS_NDADDR && 10274 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno) 10275 panic("initiate_write_inodeblock_ufs1: " 10276 "indirect pointer #%jd mismatch %d != %jd", 10277 (intmax_t)adp->ad_offset - UFS_NDADDR, 10278 dp->di_ib[adp->ad_offset - UFS_NDADDR], 10279 (intmax_t)adp->ad_newblkno); 10280 deplist |= 1 << adp->ad_offset; 10281 if ((adp->ad_state & ATTACHED) == 0) 10282 panic("initiate_write_inodeblock_ufs1: " 10283 "Unknown state 0x%x", adp->ad_state); 10284 #endif /* INVARIANTS */ 10285 adp->ad_state &= ~ATTACHED; 10286 adp->ad_state |= UNDONE; 10287 } 10288 /* 10289 * The on-disk inode cannot claim to be any larger than the last 10290 * fragment that has been written. Otherwise, the on-disk inode 10291 * might have fragments that were not the last block in the file 10292 * which would corrupt the filesystem. 10293 */ 10294 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10295 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10296 if (adp->ad_offset >= UFS_NDADDR) 10297 break; 10298 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10299 /* keep going until hitting a rollback to a frag */ 10300 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10301 continue; 10302 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10303 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) { 10304 #ifdef INVARIANTS 10305 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10306 panic("initiate_write_inodeblock_ufs1: " 10307 "lost dep1"); 10308 #endif /* INVARIANTS */ 10309 dp->di_db[i] = 0; 10310 } 10311 for (i = 0; i < UFS_NIADDR; i++) { 10312 #ifdef INVARIANTS 10313 if (dp->di_ib[i] != 0 && 10314 (deplist & ((1 << UFS_NDADDR) << i)) == 0) 10315 panic("initiate_write_inodeblock_ufs1: " 10316 "lost dep2"); 10317 #endif /* INVARIANTS */ 10318 dp->di_ib[i] = 0; 10319 } 10320 return; 10321 } 10322 /* 10323 * If we have zero'ed out the last allocated block of the file, 10324 * roll back the size to the last currently allocated block. 10325 * We know that this last allocated block is a full-sized as 10326 * we already checked for fragments in the loop above. 10327 */ 10328 if (lastadp != NULL && 10329 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10330 for (i = lastadp->ad_offset; i >= 0; i--) 10331 if (dp->di_db[i] != 0) 10332 break; 10333 dp->di_size = (i + 1) * fs->fs_bsize; 10334 } 10335 /* 10336 * The only dependencies are for indirect blocks. 10337 * 10338 * The file size for indirect block additions is not guaranteed. 10339 * Such a guarantee would be non-trivial to achieve. The conventional 10340 * synchronous write implementation also does not make this guarantee. 10341 * Fsck should catch and fix discrepancies. Arguably, the file size 10342 * can be over-estimated without destroying integrity when the file 10343 * moves into the indirect blocks (i.e., is large). If we want to 10344 * postpone fsck, we are stuck with this argument. 10345 */ 10346 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10347 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0; 10348 } 10349 10350 /* 10351 * Version of initiate_write_inodeblock that handles UFS2 dinodes. 10352 * Note that any bug fixes made to this routine must be done in the 10353 * version found above. 10354 * 10355 * Called from within the procedure above to deal with unsatisfied 10356 * allocation dependencies in an inodeblock. The buffer must be 10357 * locked, thus, no I/O completion operations can occur while we 10358 * are manipulating its associated dependencies. 10359 */ 10360 static void 10361 initiate_write_inodeblock_ufs2(inodedep, bp) 10362 struct inodedep *inodedep; 10363 struct buf *bp; /* The inode block */ 10364 { 10365 struct allocdirect *adp, *lastadp; 10366 struct ufs2_dinode *dp; 10367 struct ufs2_dinode *sip; 10368 struct inoref *inoref; 10369 struct ufsmount *ump; 10370 struct fs *fs; 10371 ufs_lbn_t i; 10372 #ifdef INVARIANTS 10373 ufs_lbn_t prevlbn = 0; 10374 #endif 10375 int deplist; 10376 10377 if (inodedep->id_state & IOSTARTED) 10378 panic("initiate_write_inodeblock_ufs2: already started"); 10379 inodedep->id_state |= IOSTARTED; 10380 fs = inodedep->id_fs; 10381 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10382 LOCK_OWNED(ump); 10383 dp = (struct ufs2_dinode *)bp->b_data + 10384 ino_to_fsbo(fs, inodedep->id_ino); 10385 10386 /* 10387 * If we're on the unlinked list but have not yet written our 10388 * next pointer initialize it here. 10389 */ 10390 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10391 struct inodedep *inon; 10392 10393 inon = TAILQ_NEXT(inodedep, id_unlinked); 10394 dp->di_freelink = inon ? inon->id_ino : 0; 10395 } 10396 /* 10397 * If the bitmap is not yet written, then the allocated 10398 * inode cannot be written to disk. 10399 */ 10400 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10401 if (inodedep->id_savedino2 != NULL) 10402 panic("initiate_write_inodeblock_ufs2: I/O underway"); 10403 FREE_LOCK(ump); 10404 sip = malloc(sizeof(struct ufs2_dinode), 10405 M_SAVEDINO, M_SOFTDEP_FLAGS); 10406 ACQUIRE_LOCK(ump); 10407 inodedep->id_savedino2 = sip; 10408 *inodedep->id_savedino2 = *dp; 10409 bzero((caddr_t)dp, sizeof(struct ufs2_dinode)); 10410 dp->di_gen = inodedep->id_savedino2->di_gen; 10411 dp->di_freelink = inodedep->id_savedino2->di_freelink; 10412 return; 10413 } 10414 /* 10415 * If no dependencies, then there is nothing to roll back. 10416 */ 10417 inodedep->id_savedsize = dp->di_size; 10418 inodedep->id_savedextsize = dp->di_extsize; 10419 inodedep->id_savednlink = dp->di_nlink; 10420 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10421 TAILQ_EMPTY(&inodedep->id_extupdt) && 10422 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10423 return; 10424 /* 10425 * Revert the link count to that of the first unwritten journal entry. 10426 */ 10427 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10428 if (inoref) 10429 dp->di_nlink = inoref->if_nlink; 10430 10431 /* 10432 * Set the ext data dependencies to busy. 10433 */ 10434 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10435 adp = TAILQ_NEXT(adp, ad_next)) { 10436 #ifdef INVARIANTS 10437 if (deplist != 0 && prevlbn >= adp->ad_offset) 10438 panic("initiate_write_inodeblock_ufs2: lbn order"); 10439 prevlbn = adp->ad_offset; 10440 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno) 10441 panic("initiate_write_inodeblock_ufs2: " 10442 "ext pointer #%jd mismatch %jd != %jd", 10443 (intmax_t)adp->ad_offset, 10444 (intmax_t)dp->di_extb[adp->ad_offset], 10445 (intmax_t)adp->ad_newblkno); 10446 deplist |= 1 << adp->ad_offset; 10447 if ((adp->ad_state & ATTACHED) == 0) 10448 panic("initiate_write_inodeblock_ufs2: Unknown " 10449 "state 0x%x", adp->ad_state); 10450 #endif /* INVARIANTS */ 10451 adp->ad_state &= ~ATTACHED; 10452 adp->ad_state |= UNDONE; 10453 } 10454 /* 10455 * The on-disk inode cannot claim to be any larger than the last 10456 * fragment that has been written. Otherwise, the on-disk inode 10457 * might have fragments that were not the last block in the ext 10458 * data which would corrupt the filesystem. 10459 */ 10460 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10461 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10462 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno; 10463 /* keep going until hitting a rollback to a frag */ 10464 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10465 continue; 10466 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10467 for (i = adp->ad_offset + 1; i < UFS_NXADDR; i++) { 10468 #ifdef INVARIANTS 10469 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0) 10470 panic("initiate_write_inodeblock_ufs2: " 10471 "lost dep1"); 10472 #endif /* INVARIANTS */ 10473 dp->di_extb[i] = 0; 10474 } 10475 lastadp = NULL; 10476 break; 10477 } 10478 /* 10479 * If we have zero'ed out the last allocated block of the ext 10480 * data, roll back the size to the last currently allocated block. 10481 * We know that this last allocated block is a full-sized as 10482 * we already checked for fragments in the loop above. 10483 */ 10484 if (lastadp != NULL && 10485 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10486 for (i = lastadp->ad_offset; i >= 0; i--) 10487 if (dp->di_extb[i] != 0) 10488 break; 10489 dp->di_extsize = (i + 1) * fs->fs_bsize; 10490 } 10491 /* 10492 * Set the file data dependencies to busy. 10493 */ 10494 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10495 adp = TAILQ_NEXT(adp, ad_next)) { 10496 #ifdef INVARIANTS 10497 if (deplist != 0 && prevlbn >= adp->ad_offset) 10498 panic("softdep_write_inodeblock: lbn order"); 10499 if ((adp->ad_state & ATTACHED) == 0) 10500 panic("inodedep %p and adp %p not attached", inodedep, adp); 10501 prevlbn = adp->ad_offset; 10502 if (adp->ad_offset < UFS_NDADDR && 10503 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10504 panic("initiate_write_inodeblock_ufs2: " 10505 "direct pointer #%jd mismatch %jd != %jd", 10506 (intmax_t)adp->ad_offset, 10507 (intmax_t)dp->di_db[adp->ad_offset], 10508 (intmax_t)adp->ad_newblkno); 10509 if (adp->ad_offset >= UFS_NDADDR && 10510 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno) 10511 panic("initiate_write_inodeblock_ufs2: " 10512 "indirect pointer #%jd mismatch %jd != %jd", 10513 (intmax_t)adp->ad_offset - UFS_NDADDR, 10514 (intmax_t)dp->di_ib[adp->ad_offset - UFS_NDADDR], 10515 (intmax_t)adp->ad_newblkno); 10516 deplist |= 1 << adp->ad_offset; 10517 if ((adp->ad_state & ATTACHED) == 0) 10518 panic("initiate_write_inodeblock_ufs2: Unknown " 10519 "state 0x%x", adp->ad_state); 10520 #endif /* INVARIANTS */ 10521 adp->ad_state &= ~ATTACHED; 10522 adp->ad_state |= UNDONE; 10523 } 10524 /* 10525 * The on-disk inode cannot claim to be any larger than the last 10526 * fragment that has been written. Otherwise, the on-disk inode 10527 * might have fragments that were not the last block in the file 10528 * which would corrupt the filesystem. 10529 */ 10530 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10531 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10532 if (adp->ad_offset >= UFS_NDADDR) 10533 break; 10534 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10535 /* keep going until hitting a rollback to a frag */ 10536 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10537 continue; 10538 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10539 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) { 10540 #ifdef INVARIANTS 10541 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10542 panic("initiate_write_inodeblock_ufs2: " 10543 "lost dep2"); 10544 #endif /* INVARIANTS */ 10545 dp->di_db[i] = 0; 10546 } 10547 for (i = 0; i < UFS_NIADDR; i++) { 10548 #ifdef INVARIANTS 10549 if (dp->di_ib[i] != 0 && 10550 (deplist & ((1 << UFS_NDADDR) << i)) == 0) 10551 panic("initiate_write_inodeblock_ufs2: " 10552 "lost dep3"); 10553 #endif /* INVARIANTS */ 10554 dp->di_ib[i] = 0; 10555 } 10556 return; 10557 } 10558 /* 10559 * If we have zero'ed out the last allocated block of the file, 10560 * roll back the size to the last currently allocated block. 10561 * We know that this last allocated block is a full-sized as 10562 * we already checked for fragments in the loop above. 10563 */ 10564 if (lastadp != NULL && 10565 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10566 for (i = lastadp->ad_offset; i >= 0; i--) 10567 if (dp->di_db[i] != 0) 10568 break; 10569 dp->di_size = (i + 1) * fs->fs_bsize; 10570 } 10571 /* 10572 * The only dependencies are for indirect blocks. 10573 * 10574 * The file size for indirect block additions is not guaranteed. 10575 * Such a guarantee would be non-trivial to achieve. The conventional 10576 * synchronous write implementation also does not make this guarantee. 10577 * Fsck should catch and fix discrepancies. Arguably, the file size 10578 * can be over-estimated without destroying integrity when the file 10579 * moves into the indirect blocks (i.e., is large). If we want to 10580 * postpone fsck, we are stuck with this argument. 10581 */ 10582 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10583 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0; 10584 } 10585 10586 /* 10587 * Cancel an indirdep as a result of truncation. Release all of the 10588 * children allocindirs and place their journal work on the appropriate 10589 * list. 10590 */ 10591 static void 10592 cancel_indirdep(indirdep, bp, freeblks) 10593 struct indirdep *indirdep; 10594 struct buf *bp; 10595 struct freeblks *freeblks; 10596 { 10597 struct allocindir *aip; 10598 10599 /* 10600 * None of the indirect pointers will ever be visible, 10601 * so they can simply be tossed. GOINGAWAY ensures 10602 * that allocated pointers will be saved in the buffer 10603 * cache until they are freed. Note that they will 10604 * only be able to be found by their physical address 10605 * since the inode mapping the logical address will 10606 * be gone. The save buffer used for the safe copy 10607 * was allocated in setup_allocindir_phase2 using 10608 * the physical address so it could be used for this 10609 * purpose. Hence we swap the safe copy with the real 10610 * copy, allowing the safe copy to be freed and holding 10611 * on to the real copy for later use in indir_trunc. 10612 */ 10613 if (indirdep->ir_state & GOINGAWAY) 10614 panic("cancel_indirdep: already gone"); 10615 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 10616 indirdep->ir_state |= DEPCOMPLETE; 10617 LIST_REMOVE(indirdep, ir_next); 10618 } 10619 indirdep->ir_state |= GOINGAWAY; 10620 /* 10621 * Pass in bp for blocks still have journal writes 10622 * pending so we can cancel them on their own. 10623 */ 10624 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != NULL) 10625 cancel_allocindir(aip, bp, freeblks, 0); 10626 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) 10627 cancel_allocindir(aip, NULL, freeblks, 0); 10628 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) 10629 cancel_allocindir(aip, NULL, freeblks, 0); 10630 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) 10631 cancel_allocindir(aip, NULL, freeblks, 0); 10632 /* 10633 * If there are pending partial truncations we need to keep the 10634 * old block copy around until they complete. This is because 10635 * the current b_data is not a perfect superset of the available 10636 * blocks. 10637 */ 10638 if (TAILQ_EMPTY(&indirdep->ir_trunc)) 10639 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount); 10640 else 10641 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10642 WORKLIST_REMOVE(&indirdep->ir_list); 10643 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list); 10644 indirdep->ir_bp = NULL; 10645 indirdep->ir_freeblks = freeblks; 10646 } 10647 10648 /* 10649 * Free an indirdep once it no longer has new pointers to track. 10650 */ 10651 static void 10652 free_indirdep(indirdep) 10653 struct indirdep *indirdep; 10654 { 10655 10656 KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc), 10657 ("free_indirdep: Indir trunc list not empty.")); 10658 KASSERT(LIST_EMPTY(&indirdep->ir_completehd), 10659 ("free_indirdep: Complete head not empty.")); 10660 KASSERT(LIST_EMPTY(&indirdep->ir_writehd), 10661 ("free_indirdep: write head not empty.")); 10662 KASSERT(LIST_EMPTY(&indirdep->ir_donehd), 10663 ("free_indirdep: done head not empty.")); 10664 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd), 10665 ("free_indirdep: deplist head not empty.")); 10666 KASSERT((indirdep->ir_state & DEPCOMPLETE), 10667 ("free_indirdep: %p still on newblk list.", indirdep)); 10668 KASSERT(indirdep->ir_saveddata == NULL, 10669 ("free_indirdep: %p still has saved data.", indirdep)); 10670 if (indirdep->ir_state & ONWORKLIST) 10671 WORKLIST_REMOVE(&indirdep->ir_list); 10672 WORKITEM_FREE(indirdep, D_INDIRDEP); 10673 } 10674 10675 /* 10676 * Called before a write to an indirdep. This routine is responsible for 10677 * rolling back pointers to a safe state which includes only those 10678 * allocindirs which have been completed. 10679 */ 10680 static void 10681 initiate_write_indirdep(indirdep, bp) 10682 struct indirdep *indirdep; 10683 struct buf *bp; 10684 { 10685 struct ufsmount *ump; 10686 10687 indirdep->ir_state |= IOSTARTED; 10688 if (indirdep->ir_state & GOINGAWAY) 10689 panic("disk_io_initiation: indirdep gone"); 10690 /* 10691 * If there are no remaining dependencies, this will be writing 10692 * the real pointers. 10693 */ 10694 if (LIST_EMPTY(&indirdep->ir_deplisthd) && 10695 TAILQ_EMPTY(&indirdep->ir_trunc)) 10696 return; 10697 /* 10698 * Replace up-to-date version with safe version. 10699 */ 10700 if (indirdep->ir_saveddata == NULL) { 10701 ump = VFSTOUFS(indirdep->ir_list.wk_mp); 10702 LOCK_OWNED(ump); 10703 FREE_LOCK(ump); 10704 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 10705 M_SOFTDEP_FLAGS); 10706 ACQUIRE_LOCK(ump); 10707 } 10708 indirdep->ir_state &= ~ATTACHED; 10709 indirdep->ir_state |= UNDONE; 10710 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10711 bcopy(indirdep->ir_savebp->b_data, bp->b_data, 10712 bp->b_bcount); 10713 } 10714 10715 /* 10716 * Called when an inode has been cleared in a cg bitmap. This finally 10717 * eliminates any canceled jaddrefs 10718 */ 10719 void 10720 softdep_setup_inofree(mp, bp, ino, wkhd) 10721 struct mount *mp; 10722 struct buf *bp; 10723 ino_t ino; 10724 struct workhead *wkhd; 10725 { 10726 struct worklist *wk, *wkn; 10727 struct inodedep *inodedep; 10728 struct ufsmount *ump; 10729 uint8_t *inosused; 10730 struct cg *cgp; 10731 struct fs *fs; 10732 10733 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 10734 ("softdep_setup_inofree called on non-softdep filesystem")); 10735 ump = VFSTOUFS(mp); 10736 ACQUIRE_LOCK(ump); 10737 fs = ump->um_fs; 10738 cgp = (struct cg *)bp->b_data; 10739 inosused = cg_inosused(cgp); 10740 if (isset(inosused, ino % fs->fs_ipg)) 10741 panic("softdep_setup_inofree: inode %ju not freed.", 10742 (uintmax_t)ino); 10743 if (inodedep_lookup(mp, ino, 0, &inodedep)) 10744 panic("softdep_setup_inofree: ino %ju has existing inodedep %p", 10745 (uintmax_t)ino, inodedep); 10746 if (wkhd) { 10747 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) { 10748 if (wk->wk_type != D_JADDREF) 10749 continue; 10750 WORKLIST_REMOVE(wk); 10751 /* 10752 * We can free immediately even if the jaddref 10753 * isn't attached in a background write as now 10754 * the bitmaps are reconciled. 10755 */ 10756 wk->wk_state |= COMPLETE | ATTACHED; 10757 free_jaddref(WK_JADDREF(wk)); 10758 } 10759 jwork_move(&bp->b_dep, wkhd); 10760 } 10761 FREE_LOCK(ump); 10762 } 10763 10764 10765 /* 10766 * Called via ffs_blkfree() after a set of frags has been cleared from a cg 10767 * map. Any dependencies waiting for the write to clear are added to the 10768 * buf's list and any jnewblks that are being canceled are discarded 10769 * immediately. 10770 */ 10771 void 10772 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 10773 struct mount *mp; 10774 struct buf *bp; 10775 ufs2_daddr_t blkno; 10776 int frags; 10777 struct workhead *wkhd; 10778 { 10779 struct bmsafemap *bmsafemap; 10780 struct jnewblk *jnewblk; 10781 struct ufsmount *ump; 10782 struct worklist *wk; 10783 struct fs *fs; 10784 #ifdef SUJ_DEBUG 10785 uint8_t *blksfree; 10786 struct cg *cgp; 10787 ufs2_daddr_t jstart; 10788 ufs2_daddr_t jend; 10789 ufs2_daddr_t end; 10790 long bno; 10791 int i; 10792 #endif 10793 10794 CTR3(KTR_SUJ, 10795 "softdep_setup_blkfree: blkno %jd frags %d wk head %p", 10796 blkno, frags, wkhd); 10797 10798 ump = VFSTOUFS(mp); 10799 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 10800 ("softdep_setup_blkfree called on non-softdep filesystem")); 10801 ACQUIRE_LOCK(ump); 10802 /* Lookup the bmsafemap so we track when it is dirty. */ 10803 fs = ump->um_fs; 10804 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 10805 /* 10806 * Detach any jnewblks which have been canceled. They must linger 10807 * until the bitmap is cleared again by ffs_blkfree() to prevent 10808 * an unjournaled allocation from hitting the disk. 10809 */ 10810 if (wkhd) { 10811 while ((wk = LIST_FIRST(wkhd)) != NULL) { 10812 CTR2(KTR_SUJ, 10813 "softdep_setup_blkfree: blkno %jd wk type %d", 10814 blkno, wk->wk_type); 10815 WORKLIST_REMOVE(wk); 10816 if (wk->wk_type != D_JNEWBLK) { 10817 WORKLIST_INSERT(&bmsafemap->sm_freehd, wk); 10818 continue; 10819 } 10820 jnewblk = WK_JNEWBLK(wk); 10821 KASSERT(jnewblk->jn_state & GOINGAWAY, 10822 ("softdep_setup_blkfree: jnewblk not canceled.")); 10823 #ifdef SUJ_DEBUG 10824 /* 10825 * Assert that this block is free in the bitmap 10826 * before we discard the jnewblk. 10827 */ 10828 cgp = (struct cg *)bp->b_data; 10829 blksfree = cg_blksfree(cgp); 10830 bno = dtogd(fs, jnewblk->jn_blkno); 10831 for (i = jnewblk->jn_oldfrags; 10832 i < jnewblk->jn_frags; i++) { 10833 if (isset(blksfree, bno + i)) 10834 continue; 10835 panic("softdep_setup_blkfree: not free"); 10836 } 10837 #endif 10838 /* 10839 * Even if it's not attached we can free immediately 10840 * as the new bitmap is correct. 10841 */ 10842 wk->wk_state |= COMPLETE | ATTACHED; 10843 free_jnewblk(jnewblk); 10844 } 10845 } 10846 10847 #ifdef SUJ_DEBUG 10848 /* 10849 * Assert that we are not freeing a block which has an outstanding 10850 * allocation dependency. 10851 */ 10852 fs = VFSTOUFS(mp)->um_fs; 10853 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 10854 end = blkno + frags; 10855 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 10856 /* 10857 * Don't match against blocks that will be freed when the 10858 * background write is done. 10859 */ 10860 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) == 10861 (COMPLETE | DEPCOMPLETE)) 10862 continue; 10863 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags; 10864 jend = jnewblk->jn_blkno + jnewblk->jn_frags; 10865 if ((blkno >= jstart && blkno < jend) || 10866 (end > jstart && end <= jend)) { 10867 printf("state 0x%X %jd - %d %d dep %p\n", 10868 jnewblk->jn_state, jnewblk->jn_blkno, 10869 jnewblk->jn_oldfrags, jnewblk->jn_frags, 10870 jnewblk->jn_dep); 10871 panic("softdep_setup_blkfree: " 10872 "%jd-%jd(%d) overlaps with %jd-%jd", 10873 blkno, end, frags, jstart, jend); 10874 } 10875 } 10876 #endif 10877 FREE_LOCK(ump); 10878 } 10879 10880 /* 10881 * Revert a block allocation when the journal record that describes it 10882 * is not yet written. 10883 */ 10884 static int 10885 jnewblk_rollback(jnewblk, fs, cgp, blksfree) 10886 struct jnewblk *jnewblk; 10887 struct fs *fs; 10888 struct cg *cgp; 10889 uint8_t *blksfree; 10890 { 10891 ufs1_daddr_t fragno; 10892 long cgbno, bbase; 10893 int frags, blk; 10894 int i; 10895 10896 frags = 0; 10897 cgbno = dtogd(fs, jnewblk->jn_blkno); 10898 /* 10899 * We have to test which frags need to be rolled back. We may 10900 * be operating on a stale copy when doing background writes. 10901 */ 10902 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) 10903 if (isclr(blksfree, cgbno + i)) 10904 frags++; 10905 if (frags == 0) 10906 return (0); 10907 /* 10908 * This is mostly ffs_blkfree() sans some validation and 10909 * superblock updates. 10910 */ 10911 if (frags == fs->fs_frag) { 10912 fragno = fragstoblks(fs, cgbno); 10913 ffs_setblock(fs, blksfree, fragno); 10914 ffs_clusteracct(fs, cgp, fragno, 1); 10915 cgp->cg_cs.cs_nbfree++; 10916 } else { 10917 cgbno += jnewblk->jn_oldfrags; 10918 bbase = cgbno - fragnum(fs, cgbno); 10919 /* Decrement the old frags. */ 10920 blk = blkmap(fs, blksfree, bbase); 10921 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 10922 /* Deallocate the fragment */ 10923 for (i = 0; i < frags; i++) 10924 setbit(blksfree, cgbno + i); 10925 cgp->cg_cs.cs_nffree += frags; 10926 /* Add back in counts associated with the new frags */ 10927 blk = blkmap(fs, blksfree, bbase); 10928 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 10929 /* If a complete block has been reassembled, account for it. */ 10930 fragno = fragstoblks(fs, bbase); 10931 if (ffs_isblock(fs, blksfree, fragno)) { 10932 cgp->cg_cs.cs_nffree -= fs->fs_frag; 10933 ffs_clusteracct(fs, cgp, fragno, 1); 10934 cgp->cg_cs.cs_nbfree++; 10935 } 10936 } 10937 stat_jnewblk++; 10938 jnewblk->jn_state &= ~ATTACHED; 10939 jnewblk->jn_state |= UNDONE; 10940 10941 return (frags); 10942 } 10943 10944 static void 10945 initiate_write_bmsafemap(bmsafemap, bp) 10946 struct bmsafemap *bmsafemap; 10947 struct buf *bp; /* The cg block. */ 10948 { 10949 struct jaddref *jaddref; 10950 struct jnewblk *jnewblk; 10951 uint8_t *inosused; 10952 uint8_t *blksfree; 10953 struct cg *cgp; 10954 struct fs *fs; 10955 ino_t ino; 10956 10957 /* 10958 * If this is a background write, we did this at the time that 10959 * the copy was made, so do not need to do it again. 10960 */ 10961 if (bmsafemap->sm_state & IOSTARTED) 10962 return; 10963 bmsafemap->sm_state |= IOSTARTED; 10964 /* 10965 * Clear any inode allocations which are pending journal writes. 10966 */ 10967 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) { 10968 cgp = (struct cg *)bp->b_data; 10969 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 10970 inosused = cg_inosused(cgp); 10971 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) { 10972 ino = jaddref->ja_ino % fs->fs_ipg; 10973 if (isset(inosused, ino)) { 10974 if ((jaddref->ja_mode & IFMT) == IFDIR) 10975 cgp->cg_cs.cs_ndir--; 10976 cgp->cg_cs.cs_nifree++; 10977 clrbit(inosused, ino); 10978 jaddref->ja_state &= ~ATTACHED; 10979 jaddref->ja_state |= UNDONE; 10980 stat_jaddref++; 10981 } else 10982 panic("initiate_write_bmsafemap: inode %ju " 10983 "marked free", (uintmax_t)jaddref->ja_ino); 10984 } 10985 } 10986 /* 10987 * Clear any block allocations which are pending journal writes. 10988 */ 10989 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 10990 cgp = (struct cg *)bp->b_data; 10991 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 10992 blksfree = cg_blksfree(cgp); 10993 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 10994 if (jnewblk_rollback(jnewblk, fs, cgp, blksfree)) 10995 continue; 10996 panic("initiate_write_bmsafemap: block %jd " 10997 "marked free", jnewblk->jn_blkno); 10998 } 10999 } 11000 /* 11001 * Move allocation lists to the written lists so they can be 11002 * cleared once the block write is complete. 11003 */ 11004 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr, 11005 inodedep, id_deps); 11006 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 11007 newblk, nb_deps); 11008 LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist, 11009 wk_list); 11010 } 11011 11012 /* 11013 * This routine is called during the completion interrupt 11014 * service routine for a disk write (from the procedure called 11015 * by the device driver to inform the filesystem caches of 11016 * a request completion). It should be called early in this 11017 * procedure, before the block is made available to other 11018 * processes or other routines are called. 11019 * 11020 */ 11021 static void 11022 softdep_disk_write_complete(bp) 11023 struct buf *bp; /* describes the completed disk write */ 11024 { 11025 struct worklist *wk; 11026 struct worklist *owk; 11027 struct ufsmount *ump; 11028 struct workhead reattach; 11029 struct freeblks *freeblks; 11030 struct buf *sbp; 11031 11032 ump = softdep_bp_to_mp(bp); 11033 if (ump == NULL) 11034 return; 11035 11036 sbp = NULL; 11037 11038 /* 11039 * If an error occurred while doing the write, then the data 11040 * has not hit the disk and the dependencies cannot be processed. 11041 * But we do have to go through and roll forward any dependencies 11042 * that were rolled back before the disk write. 11043 */ 11044 ACQUIRE_LOCK(ump); 11045 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) { 11046 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 11047 switch (wk->wk_type) { 11048 11049 case D_PAGEDEP: 11050 handle_written_filepage(WK_PAGEDEP(wk), bp, 0); 11051 continue; 11052 11053 case D_INODEDEP: 11054 handle_written_inodeblock(WK_INODEDEP(wk), 11055 bp, 0); 11056 continue; 11057 11058 case D_BMSAFEMAP: 11059 handle_written_bmsafemap(WK_BMSAFEMAP(wk), 11060 bp, 0); 11061 continue; 11062 11063 case D_INDIRDEP: 11064 handle_written_indirdep(WK_INDIRDEP(wk), 11065 bp, &sbp, 0); 11066 continue; 11067 default: 11068 /* nothing to roll forward */ 11069 continue; 11070 } 11071 } 11072 FREE_LOCK(ump); 11073 return; 11074 } 11075 LIST_INIT(&reattach); 11076 11077 /* 11078 * Ump SU lock must not be released anywhere in this code segment. 11079 */ 11080 owk = NULL; 11081 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) { 11082 WORKLIST_REMOVE(wk); 11083 atomic_add_long(&dep_write[wk->wk_type], 1); 11084 if (wk == owk) 11085 panic("duplicate worklist: %p\n", wk); 11086 owk = wk; 11087 switch (wk->wk_type) { 11088 11089 case D_PAGEDEP: 11090 if (handle_written_filepage(WK_PAGEDEP(wk), bp, 11091 WRITESUCCEEDED)) 11092 WORKLIST_INSERT(&reattach, wk); 11093 continue; 11094 11095 case D_INODEDEP: 11096 if (handle_written_inodeblock(WK_INODEDEP(wk), bp, 11097 WRITESUCCEEDED)) 11098 WORKLIST_INSERT(&reattach, wk); 11099 continue; 11100 11101 case D_BMSAFEMAP: 11102 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp, 11103 WRITESUCCEEDED)) 11104 WORKLIST_INSERT(&reattach, wk); 11105 continue; 11106 11107 case D_MKDIR: 11108 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 11109 continue; 11110 11111 case D_ALLOCDIRECT: 11112 wk->wk_state |= COMPLETE; 11113 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL); 11114 continue; 11115 11116 case D_ALLOCINDIR: 11117 wk->wk_state |= COMPLETE; 11118 handle_allocindir_partdone(WK_ALLOCINDIR(wk)); 11119 continue; 11120 11121 case D_INDIRDEP: 11122 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp, 11123 WRITESUCCEEDED)) 11124 WORKLIST_INSERT(&reattach, wk); 11125 continue; 11126 11127 case D_FREEBLKS: 11128 wk->wk_state |= COMPLETE; 11129 freeblks = WK_FREEBLKS(wk); 11130 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE && 11131 LIST_EMPTY(&freeblks->fb_jblkdephd)) 11132 add_to_worklist(wk, WK_NODELAY); 11133 continue; 11134 11135 case D_FREEWORK: 11136 handle_written_freework(WK_FREEWORK(wk)); 11137 break; 11138 11139 case D_JSEGDEP: 11140 free_jsegdep(WK_JSEGDEP(wk)); 11141 continue; 11142 11143 case D_JSEG: 11144 handle_written_jseg(WK_JSEG(wk), bp); 11145 continue; 11146 11147 case D_SBDEP: 11148 if (handle_written_sbdep(WK_SBDEP(wk), bp)) 11149 WORKLIST_INSERT(&reattach, wk); 11150 continue; 11151 11152 case D_FREEDEP: 11153 free_freedep(WK_FREEDEP(wk)); 11154 continue; 11155 11156 default: 11157 panic("handle_disk_write_complete: Unknown type %s", 11158 TYPENAME(wk->wk_type)); 11159 /* NOTREACHED */ 11160 } 11161 } 11162 /* 11163 * Reattach any requests that must be redone. 11164 */ 11165 while ((wk = LIST_FIRST(&reattach)) != NULL) { 11166 WORKLIST_REMOVE(wk); 11167 WORKLIST_INSERT(&bp->b_dep, wk); 11168 } 11169 FREE_LOCK(ump); 11170 if (sbp) 11171 brelse(sbp); 11172 } 11173 11174 /* 11175 * Called from within softdep_disk_write_complete above. Note that 11176 * this routine is always called from interrupt level with further 11177 * splbio interrupts blocked. 11178 */ 11179 static void 11180 handle_allocdirect_partdone(adp, wkhd) 11181 struct allocdirect *adp; /* the completed allocdirect */ 11182 struct workhead *wkhd; /* Work to do when inode is writtne. */ 11183 { 11184 struct allocdirectlst *listhead; 11185 struct allocdirect *listadp; 11186 struct inodedep *inodedep; 11187 long bsize; 11188 11189 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11190 return; 11191 /* 11192 * The on-disk inode cannot claim to be any larger than the last 11193 * fragment that has been written. Otherwise, the on-disk inode 11194 * might have fragments that were not the last block in the file 11195 * which would corrupt the filesystem. Thus, we cannot free any 11196 * allocdirects after one whose ad_oldblkno claims a fragment as 11197 * these blocks must be rolled back to zero before writing the inode. 11198 * We check the currently active set of allocdirects in id_inoupdt 11199 * or id_extupdt as appropriate. 11200 */ 11201 inodedep = adp->ad_inodedep; 11202 bsize = inodedep->id_fs->fs_bsize; 11203 if (adp->ad_state & EXTDATA) 11204 listhead = &inodedep->id_extupdt; 11205 else 11206 listhead = &inodedep->id_inoupdt; 11207 TAILQ_FOREACH(listadp, listhead, ad_next) { 11208 /* found our block */ 11209 if (listadp == adp) 11210 break; 11211 /* continue if ad_oldlbn is not a fragment */ 11212 if (listadp->ad_oldsize == 0 || 11213 listadp->ad_oldsize == bsize) 11214 continue; 11215 /* hit a fragment */ 11216 return; 11217 } 11218 /* 11219 * If we have reached the end of the current list without 11220 * finding the just finished dependency, then it must be 11221 * on the future dependency list. Future dependencies cannot 11222 * be freed until they are moved to the current list. 11223 */ 11224 if (listadp == NULL) { 11225 #ifdef DEBUG 11226 if (adp->ad_state & EXTDATA) 11227 listhead = &inodedep->id_newextupdt; 11228 else 11229 listhead = &inodedep->id_newinoupdt; 11230 TAILQ_FOREACH(listadp, listhead, ad_next) 11231 /* found our block */ 11232 if (listadp == adp) 11233 break; 11234 if (listadp == NULL) 11235 panic("handle_allocdirect_partdone: lost dep"); 11236 #endif /* DEBUG */ 11237 return; 11238 } 11239 /* 11240 * If we have found the just finished dependency, then queue 11241 * it along with anything that follows it that is complete. 11242 * Since the pointer has not yet been written in the inode 11243 * as the dependency prevents it, place the allocdirect on the 11244 * bufwait list where it will be freed once the pointer is 11245 * valid. 11246 */ 11247 if (wkhd == NULL) 11248 wkhd = &inodedep->id_bufwait; 11249 for (; adp; adp = listadp) { 11250 listadp = TAILQ_NEXT(adp, ad_next); 11251 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11252 return; 11253 TAILQ_REMOVE(listhead, adp, ad_next); 11254 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list); 11255 } 11256 } 11257 11258 /* 11259 * Called from within softdep_disk_write_complete above. This routine 11260 * completes successfully written allocindirs. 11261 */ 11262 static void 11263 handle_allocindir_partdone(aip) 11264 struct allocindir *aip; /* the completed allocindir */ 11265 { 11266 struct indirdep *indirdep; 11267 11268 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE) 11269 return; 11270 indirdep = aip->ai_indirdep; 11271 LIST_REMOVE(aip, ai_next); 11272 /* 11273 * Don't set a pointer while the buffer is undergoing IO or while 11274 * we have active truncations. 11275 */ 11276 if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) { 11277 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next); 11278 return; 11279 } 11280 if (indirdep->ir_state & UFS1FMT) 11281 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11282 aip->ai_newblkno; 11283 else 11284 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11285 aip->ai_newblkno; 11286 /* 11287 * Await the pointer write before freeing the allocindir. 11288 */ 11289 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next); 11290 } 11291 11292 /* 11293 * Release segments held on a jwork list. 11294 */ 11295 static void 11296 handle_jwork(wkhd) 11297 struct workhead *wkhd; 11298 { 11299 struct worklist *wk; 11300 11301 while ((wk = LIST_FIRST(wkhd)) != NULL) { 11302 WORKLIST_REMOVE(wk); 11303 switch (wk->wk_type) { 11304 case D_JSEGDEP: 11305 free_jsegdep(WK_JSEGDEP(wk)); 11306 continue; 11307 case D_FREEDEP: 11308 free_freedep(WK_FREEDEP(wk)); 11309 continue; 11310 case D_FREEFRAG: 11311 rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep)); 11312 WORKITEM_FREE(wk, D_FREEFRAG); 11313 continue; 11314 case D_FREEWORK: 11315 handle_written_freework(WK_FREEWORK(wk)); 11316 continue; 11317 default: 11318 panic("handle_jwork: Unknown type %s\n", 11319 TYPENAME(wk->wk_type)); 11320 } 11321 } 11322 } 11323 11324 /* 11325 * Handle the bufwait list on an inode when it is safe to release items 11326 * held there. This normally happens after an inode block is written but 11327 * may be delayed and handled later if there are pending journal items that 11328 * are not yet safe to be released. 11329 */ 11330 static struct freefile * 11331 handle_bufwait(inodedep, refhd) 11332 struct inodedep *inodedep; 11333 struct workhead *refhd; 11334 { 11335 struct jaddref *jaddref; 11336 struct freefile *freefile; 11337 struct worklist *wk; 11338 11339 freefile = NULL; 11340 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) { 11341 WORKLIST_REMOVE(wk); 11342 switch (wk->wk_type) { 11343 case D_FREEFILE: 11344 /* 11345 * We defer adding freefile to the worklist 11346 * until all other additions have been made to 11347 * ensure that it will be done after all the 11348 * old blocks have been freed. 11349 */ 11350 if (freefile != NULL) 11351 panic("handle_bufwait: freefile"); 11352 freefile = WK_FREEFILE(wk); 11353 continue; 11354 11355 case D_MKDIR: 11356 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT); 11357 continue; 11358 11359 case D_DIRADD: 11360 diradd_inode_written(WK_DIRADD(wk), inodedep); 11361 continue; 11362 11363 case D_FREEFRAG: 11364 wk->wk_state |= COMPLETE; 11365 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE) 11366 add_to_worklist(wk, 0); 11367 continue; 11368 11369 case D_DIRREM: 11370 wk->wk_state |= COMPLETE; 11371 add_to_worklist(wk, 0); 11372 continue; 11373 11374 case D_ALLOCDIRECT: 11375 case D_ALLOCINDIR: 11376 free_newblk(WK_NEWBLK(wk)); 11377 continue; 11378 11379 case D_JNEWBLK: 11380 wk->wk_state |= COMPLETE; 11381 free_jnewblk(WK_JNEWBLK(wk)); 11382 continue; 11383 11384 /* 11385 * Save freed journal segments and add references on 11386 * the supplied list which will delay their release 11387 * until the cg bitmap is cleared on disk. 11388 */ 11389 case D_JSEGDEP: 11390 if (refhd == NULL) 11391 free_jsegdep(WK_JSEGDEP(wk)); 11392 else 11393 WORKLIST_INSERT(refhd, wk); 11394 continue; 11395 11396 case D_JADDREF: 11397 jaddref = WK_JADDREF(wk); 11398 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 11399 if_deps); 11400 /* 11401 * Transfer any jaddrefs to the list to be freed with 11402 * the bitmap if we're handling a removed file. 11403 */ 11404 if (refhd == NULL) { 11405 wk->wk_state |= COMPLETE; 11406 free_jaddref(jaddref); 11407 } else 11408 WORKLIST_INSERT(refhd, wk); 11409 continue; 11410 11411 default: 11412 panic("handle_bufwait: Unknown type %p(%s)", 11413 wk, TYPENAME(wk->wk_type)); 11414 /* NOTREACHED */ 11415 } 11416 } 11417 return (freefile); 11418 } 11419 /* 11420 * Called from within softdep_disk_write_complete above to restore 11421 * in-memory inode block contents to their most up-to-date state. Note 11422 * that this routine is always called from interrupt level with further 11423 * interrupts from this device blocked. 11424 * 11425 * If the write did not succeed, we will do all the roll-forward 11426 * operations, but we will not take the actions that will allow its 11427 * dependencies to be processed. 11428 */ 11429 static int 11430 handle_written_inodeblock(inodedep, bp, flags) 11431 struct inodedep *inodedep; 11432 struct buf *bp; /* buffer containing the inode block */ 11433 int flags; 11434 { 11435 struct freefile *freefile; 11436 struct allocdirect *adp, *nextadp; 11437 struct ufs1_dinode *dp1 = NULL; 11438 struct ufs2_dinode *dp2 = NULL; 11439 struct workhead wkhd; 11440 int hadchanges, fstype; 11441 ino_t freelink; 11442 11443 LIST_INIT(&wkhd); 11444 hadchanges = 0; 11445 freefile = NULL; 11446 if ((inodedep->id_state & IOSTARTED) == 0) 11447 panic("handle_written_inodeblock: not started"); 11448 inodedep->id_state &= ~IOSTARTED; 11449 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) { 11450 fstype = UFS1; 11451 dp1 = (struct ufs1_dinode *)bp->b_data + 11452 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11453 freelink = dp1->di_freelink; 11454 } else { 11455 fstype = UFS2; 11456 dp2 = (struct ufs2_dinode *)bp->b_data + 11457 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11458 freelink = dp2->di_freelink; 11459 } 11460 /* 11461 * Leave this inodeblock dirty until it's in the list. 11462 */ 11463 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED && 11464 (flags & WRITESUCCEEDED)) { 11465 struct inodedep *inon; 11466 11467 inon = TAILQ_NEXT(inodedep, id_unlinked); 11468 if ((inon == NULL && freelink == 0) || 11469 (inon && inon->id_ino == freelink)) { 11470 if (inon) 11471 inon->id_state |= UNLINKPREV; 11472 inodedep->id_state |= UNLINKNEXT; 11473 } 11474 hadchanges = 1; 11475 } 11476 /* 11477 * If we had to rollback the inode allocation because of 11478 * bitmaps being incomplete, then simply restore it. 11479 * Keep the block dirty so that it will not be reclaimed until 11480 * all associated dependencies have been cleared and the 11481 * corresponding updates written to disk. 11482 */ 11483 if (inodedep->id_savedino1 != NULL) { 11484 hadchanges = 1; 11485 if (fstype == UFS1) 11486 *dp1 = *inodedep->id_savedino1; 11487 else 11488 *dp2 = *inodedep->id_savedino2; 11489 free(inodedep->id_savedino1, M_SAVEDINO); 11490 inodedep->id_savedino1 = NULL; 11491 if ((bp->b_flags & B_DELWRI) == 0) 11492 stat_inode_bitmap++; 11493 bdirty(bp); 11494 /* 11495 * If the inode is clear here and GOINGAWAY it will never 11496 * be written. Process the bufwait and clear any pending 11497 * work which may include the freefile. 11498 */ 11499 if (inodedep->id_state & GOINGAWAY) 11500 goto bufwait; 11501 return (1); 11502 } 11503 if (flags & WRITESUCCEEDED) 11504 inodedep->id_state |= COMPLETE; 11505 /* 11506 * Roll forward anything that had to be rolled back before 11507 * the inode could be updated. 11508 */ 11509 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) { 11510 nextadp = TAILQ_NEXT(adp, ad_next); 11511 if (adp->ad_state & ATTACHED) 11512 panic("handle_written_inodeblock: new entry"); 11513 if (fstype == UFS1) { 11514 if (adp->ad_offset < UFS_NDADDR) { 11515 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11516 panic("%s %s #%jd mismatch %d != %jd", 11517 "handle_written_inodeblock:", 11518 "direct pointer", 11519 (intmax_t)adp->ad_offset, 11520 dp1->di_db[adp->ad_offset], 11521 (intmax_t)adp->ad_oldblkno); 11522 dp1->di_db[adp->ad_offset] = adp->ad_newblkno; 11523 } else { 11524 if (dp1->di_ib[adp->ad_offset - UFS_NDADDR] != 11525 0) 11526 panic("%s: %s #%jd allocated as %d", 11527 "handle_written_inodeblock", 11528 "indirect pointer", 11529 (intmax_t)adp->ad_offset - 11530 UFS_NDADDR, 11531 dp1->di_ib[adp->ad_offset - 11532 UFS_NDADDR]); 11533 dp1->di_ib[adp->ad_offset - UFS_NDADDR] = 11534 adp->ad_newblkno; 11535 } 11536 } else { 11537 if (adp->ad_offset < UFS_NDADDR) { 11538 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11539 panic("%s: %s #%jd %s %jd != %jd", 11540 "handle_written_inodeblock", 11541 "direct pointer", 11542 (intmax_t)adp->ad_offset, "mismatch", 11543 (intmax_t)dp2->di_db[adp->ad_offset], 11544 (intmax_t)adp->ad_oldblkno); 11545 dp2->di_db[adp->ad_offset] = adp->ad_newblkno; 11546 } else { 11547 if (dp2->di_ib[adp->ad_offset - UFS_NDADDR] != 11548 0) 11549 panic("%s: %s #%jd allocated as %jd", 11550 "handle_written_inodeblock", 11551 "indirect pointer", 11552 (intmax_t)adp->ad_offset - 11553 UFS_NDADDR, 11554 (intmax_t) 11555 dp2->di_ib[adp->ad_offset - 11556 UFS_NDADDR]); 11557 dp2->di_ib[adp->ad_offset - UFS_NDADDR] = 11558 adp->ad_newblkno; 11559 } 11560 } 11561 adp->ad_state &= ~UNDONE; 11562 adp->ad_state |= ATTACHED; 11563 hadchanges = 1; 11564 } 11565 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) { 11566 nextadp = TAILQ_NEXT(adp, ad_next); 11567 if (adp->ad_state & ATTACHED) 11568 panic("handle_written_inodeblock: new entry"); 11569 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno) 11570 panic("%s: direct pointers #%jd %s %jd != %jd", 11571 "handle_written_inodeblock", 11572 (intmax_t)adp->ad_offset, "mismatch", 11573 (intmax_t)dp2->di_extb[adp->ad_offset], 11574 (intmax_t)adp->ad_oldblkno); 11575 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno; 11576 adp->ad_state &= ~UNDONE; 11577 adp->ad_state |= ATTACHED; 11578 hadchanges = 1; 11579 } 11580 if (hadchanges && (bp->b_flags & B_DELWRI) == 0) 11581 stat_direct_blk_ptrs++; 11582 /* 11583 * Reset the file size to its most up-to-date value. 11584 */ 11585 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1) 11586 panic("handle_written_inodeblock: bad size"); 11587 if (inodedep->id_savednlink > UFS_LINK_MAX) 11588 panic("handle_written_inodeblock: Invalid link count " 11589 "%jd for inodedep %p", (uintmax_t)inodedep->id_savednlink, 11590 inodedep); 11591 if (fstype == UFS1) { 11592 if (dp1->di_nlink != inodedep->id_savednlink) { 11593 dp1->di_nlink = inodedep->id_savednlink; 11594 hadchanges = 1; 11595 } 11596 if (dp1->di_size != inodedep->id_savedsize) { 11597 dp1->di_size = inodedep->id_savedsize; 11598 hadchanges = 1; 11599 } 11600 } else { 11601 if (dp2->di_nlink != inodedep->id_savednlink) { 11602 dp2->di_nlink = inodedep->id_savednlink; 11603 hadchanges = 1; 11604 } 11605 if (dp2->di_size != inodedep->id_savedsize) { 11606 dp2->di_size = inodedep->id_savedsize; 11607 hadchanges = 1; 11608 } 11609 if (dp2->di_extsize != inodedep->id_savedextsize) { 11610 dp2->di_extsize = inodedep->id_savedextsize; 11611 hadchanges = 1; 11612 } 11613 } 11614 inodedep->id_savedsize = -1; 11615 inodedep->id_savedextsize = -1; 11616 inodedep->id_savednlink = -1; 11617 /* 11618 * If there were any rollbacks in the inode block, then it must be 11619 * marked dirty so that its will eventually get written back in 11620 * its correct form. 11621 */ 11622 if (hadchanges) 11623 bdirty(bp); 11624 bufwait: 11625 /* 11626 * If the write did not succeed, we have done all the roll-forward 11627 * operations, but we cannot take the actions that will allow its 11628 * dependencies to be processed. 11629 */ 11630 if ((flags & WRITESUCCEEDED) == 0) 11631 return (hadchanges); 11632 /* 11633 * Process any allocdirects that completed during the update. 11634 */ 11635 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 11636 handle_allocdirect_partdone(adp, &wkhd); 11637 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 11638 handle_allocdirect_partdone(adp, &wkhd); 11639 /* 11640 * Process deallocations that were held pending until the 11641 * inode had been written to disk. Freeing of the inode 11642 * is delayed until after all blocks have been freed to 11643 * avoid creation of new <vfsid, inum, lbn> triples 11644 * before the old ones have been deleted. Completely 11645 * unlinked inodes are not processed until the unlinked 11646 * inode list is written or the last reference is removed. 11647 */ 11648 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) { 11649 freefile = handle_bufwait(inodedep, NULL); 11650 if (freefile && !LIST_EMPTY(&wkhd)) { 11651 WORKLIST_INSERT(&wkhd, &freefile->fx_list); 11652 freefile = NULL; 11653 } 11654 } 11655 /* 11656 * Move rolled forward dependency completions to the bufwait list 11657 * now that those that were already written have been processed. 11658 */ 11659 if (!LIST_EMPTY(&wkhd) && hadchanges == 0) 11660 panic("handle_written_inodeblock: bufwait but no changes"); 11661 jwork_move(&inodedep->id_bufwait, &wkhd); 11662 11663 if (freefile != NULL) { 11664 /* 11665 * If the inode is goingaway it was never written. Fake up 11666 * the state here so free_inodedep() can succeed. 11667 */ 11668 if (inodedep->id_state & GOINGAWAY) 11669 inodedep->id_state |= COMPLETE | DEPCOMPLETE; 11670 if (free_inodedep(inodedep) == 0) 11671 panic("handle_written_inodeblock: live inodedep %p", 11672 inodedep); 11673 add_to_worklist(&freefile->fx_list, 0); 11674 return (0); 11675 } 11676 11677 /* 11678 * If no outstanding dependencies, free it. 11679 */ 11680 if (free_inodedep(inodedep) || 11681 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 && 11682 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 && 11683 TAILQ_FIRST(&inodedep->id_extupdt) == 0 && 11684 LIST_FIRST(&inodedep->id_bufwait) == 0)) 11685 return (0); 11686 return (hadchanges); 11687 } 11688 11689 /* 11690 * Perform needed roll-forwards and kick off any dependencies that 11691 * can now be processed. 11692 * 11693 * If the write did not succeed, we will do all the roll-forward 11694 * operations, but we will not take the actions that will allow its 11695 * dependencies to be processed. 11696 */ 11697 static int 11698 handle_written_indirdep(indirdep, bp, bpp, flags) 11699 struct indirdep *indirdep; 11700 struct buf *bp; 11701 struct buf **bpp; 11702 int flags; 11703 { 11704 struct allocindir *aip; 11705 struct buf *sbp; 11706 int chgs; 11707 11708 if (indirdep->ir_state & GOINGAWAY) 11709 panic("handle_written_indirdep: indirdep gone"); 11710 if ((indirdep->ir_state & IOSTARTED) == 0) 11711 panic("handle_written_indirdep: IO not started"); 11712 chgs = 0; 11713 /* 11714 * If there were rollbacks revert them here. 11715 */ 11716 if (indirdep->ir_saveddata) { 11717 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount); 11718 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11719 free(indirdep->ir_saveddata, M_INDIRDEP); 11720 indirdep->ir_saveddata = NULL; 11721 } 11722 chgs = 1; 11723 } 11724 indirdep->ir_state &= ~(UNDONE | IOSTARTED); 11725 indirdep->ir_state |= ATTACHED; 11726 /* 11727 * If the write did not succeed, we have done all the roll-forward 11728 * operations, but we cannot take the actions that will allow its 11729 * dependencies to be processed. 11730 */ 11731 if ((flags & WRITESUCCEEDED) == 0) { 11732 stat_indir_blk_ptrs++; 11733 bdirty(bp); 11734 return (1); 11735 } 11736 /* 11737 * Move allocindirs with written pointers to the completehd if 11738 * the indirdep's pointer is not yet written. Otherwise 11739 * free them here. 11740 */ 11741 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) { 11742 LIST_REMOVE(aip, ai_next); 11743 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 11744 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip, 11745 ai_next); 11746 newblk_freefrag(&aip->ai_block); 11747 continue; 11748 } 11749 free_newblk(&aip->ai_block); 11750 } 11751 /* 11752 * Move allocindirs that have finished dependency processing from 11753 * the done list to the write list after updating the pointers. 11754 */ 11755 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11756 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) { 11757 handle_allocindir_partdone(aip); 11758 if (aip == LIST_FIRST(&indirdep->ir_donehd)) 11759 panic("disk_write_complete: not gone"); 11760 chgs = 1; 11761 } 11762 } 11763 /* 11764 * Preserve the indirdep if there were any changes or if it is not 11765 * yet valid on disk. 11766 */ 11767 if (chgs) { 11768 stat_indir_blk_ptrs++; 11769 bdirty(bp); 11770 return (1); 11771 } 11772 /* 11773 * If there were no changes we can discard the savedbp and detach 11774 * ourselves from the buf. We are only carrying completed pointers 11775 * in this case. 11776 */ 11777 sbp = indirdep->ir_savebp; 11778 sbp->b_flags |= B_INVAL | B_NOCACHE; 11779 indirdep->ir_savebp = NULL; 11780 indirdep->ir_bp = NULL; 11781 if (*bpp != NULL) 11782 panic("handle_written_indirdep: bp already exists."); 11783 *bpp = sbp; 11784 /* 11785 * The indirdep may not be freed until its parent points at it. 11786 */ 11787 if (indirdep->ir_state & DEPCOMPLETE) 11788 free_indirdep(indirdep); 11789 11790 return (0); 11791 } 11792 11793 /* 11794 * Process a diradd entry after its dependent inode has been written. 11795 * This routine must be called with splbio interrupts blocked. 11796 */ 11797 static void 11798 diradd_inode_written(dap, inodedep) 11799 struct diradd *dap; 11800 struct inodedep *inodedep; 11801 { 11802 11803 dap->da_state |= COMPLETE; 11804 complete_diradd(dap); 11805 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 11806 } 11807 11808 /* 11809 * Returns true if the bmsafemap will have rollbacks when written. Must only 11810 * be called with the per-filesystem lock and the buf lock on the cg held. 11811 */ 11812 static int 11813 bmsafemap_backgroundwrite(bmsafemap, bp) 11814 struct bmsafemap *bmsafemap; 11815 struct buf *bp; 11816 { 11817 int dirty; 11818 11819 LOCK_OWNED(VFSTOUFS(bmsafemap->sm_list.wk_mp)); 11820 dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) | 11821 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd); 11822 /* 11823 * If we're initiating a background write we need to process the 11824 * rollbacks as they exist now, not as they exist when IO starts. 11825 * No other consumers will look at the contents of the shadowed 11826 * buf so this is safe to do here. 11827 */ 11828 if (bp->b_xflags & BX_BKGRDMARKER) 11829 initiate_write_bmsafemap(bmsafemap, bp); 11830 11831 return (dirty); 11832 } 11833 11834 /* 11835 * Re-apply an allocation when a cg write is complete. 11836 */ 11837 static int 11838 jnewblk_rollforward(jnewblk, fs, cgp, blksfree) 11839 struct jnewblk *jnewblk; 11840 struct fs *fs; 11841 struct cg *cgp; 11842 uint8_t *blksfree; 11843 { 11844 ufs1_daddr_t fragno; 11845 ufs2_daddr_t blkno; 11846 long cgbno, bbase; 11847 int frags, blk; 11848 int i; 11849 11850 frags = 0; 11851 cgbno = dtogd(fs, jnewblk->jn_blkno); 11852 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) { 11853 if (isclr(blksfree, cgbno + i)) 11854 panic("jnewblk_rollforward: re-allocated fragment"); 11855 frags++; 11856 } 11857 if (frags == fs->fs_frag) { 11858 blkno = fragstoblks(fs, cgbno); 11859 ffs_clrblock(fs, blksfree, (long)blkno); 11860 ffs_clusteracct(fs, cgp, blkno, -1); 11861 cgp->cg_cs.cs_nbfree--; 11862 } else { 11863 bbase = cgbno - fragnum(fs, cgbno); 11864 cgbno += jnewblk->jn_oldfrags; 11865 /* If a complete block had been reassembled, account for it. */ 11866 fragno = fragstoblks(fs, bbase); 11867 if (ffs_isblock(fs, blksfree, fragno)) { 11868 cgp->cg_cs.cs_nffree += fs->fs_frag; 11869 ffs_clusteracct(fs, cgp, fragno, -1); 11870 cgp->cg_cs.cs_nbfree--; 11871 } 11872 /* Decrement the old frags. */ 11873 blk = blkmap(fs, blksfree, bbase); 11874 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 11875 /* Allocate the fragment */ 11876 for (i = 0; i < frags; i++) 11877 clrbit(blksfree, cgbno + i); 11878 cgp->cg_cs.cs_nffree -= frags; 11879 /* Add back in counts associated with the new frags */ 11880 blk = blkmap(fs, blksfree, bbase); 11881 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 11882 } 11883 return (frags); 11884 } 11885 11886 /* 11887 * Complete a write to a bmsafemap structure. Roll forward any bitmap 11888 * changes if it's not a background write. Set all written dependencies 11889 * to DEPCOMPLETE and free the structure if possible. 11890 * 11891 * If the write did not succeed, we will do all the roll-forward 11892 * operations, but we will not take the actions that will allow its 11893 * dependencies to be processed. 11894 */ 11895 static int 11896 handle_written_bmsafemap(bmsafemap, bp, flags) 11897 struct bmsafemap *bmsafemap; 11898 struct buf *bp; 11899 int flags; 11900 { 11901 struct newblk *newblk; 11902 struct inodedep *inodedep; 11903 struct jaddref *jaddref, *jatmp; 11904 struct jnewblk *jnewblk, *jntmp; 11905 struct ufsmount *ump; 11906 uint8_t *inosused; 11907 uint8_t *blksfree; 11908 struct cg *cgp; 11909 struct fs *fs; 11910 ino_t ino; 11911 int foreground; 11912 int chgs; 11913 11914 if ((bmsafemap->sm_state & IOSTARTED) == 0) 11915 panic("handle_written_bmsafemap: Not started\n"); 11916 ump = VFSTOUFS(bmsafemap->sm_list.wk_mp); 11917 chgs = 0; 11918 bmsafemap->sm_state &= ~IOSTARTED; 11919 foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0; 11920 /* 11921 * If write was successful, release journal work that was waiting 11922 * on the write. Otherwise move the work back. 11923 */ 11924 if (flags & WRITESUCCEEDED) 11925 handle_jwork(&bmsafemap->sm_freewr); 11926 else 11927 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, 11928 worklist, wk_list); 11929 11930 /* 11931 * Restore unwritten inode allocation pending jaddref writes. 11932 */ 11933 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) { 11934 cgp = (struct cg *)bp->b_data; 11935 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11936 inosused = cg_inosused(cgp); 11937 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd, 11938 ja_bmdeps, jatmp) { 11939 if ((jaddref->ja_state & UNDONE) == 0) 11940 continue; 11941 ino = jaddref->ja_ino % fs->fs_ipg; 11942 if (isset(inosused, ino)) 11943 panic("handle_written_bmsafemap: " 11944 "re-allocated inode"); 11945 /* Do the roll-forward only if it's a real copy. */ 11946 if (foreground) { 11947 if ((jaddref->ja_mode & IFMT) == IFDIR) 11948 cgp->cg_cs.cs_ndir++; 11949 cgp->cg_cs.cs_nifree--; 11950 setbit(inosused, ino); 11951 chgs = 1; 11952 } 11953 jaddref->ja_state &= ~UNDONE; 11954 jaddref->ja_state |= ATTACHED; 11955 free_jaddref(jaddref); 11956 } 11957 } 11958 /* 11959 * Restore any block allocations which are pending journal writes. 11960 */ 11961 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 11962 cgp = (struct cg *)bp->b_data; 11963 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11964 blksfree = cg_blksfree(cgp); 11965 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps, 11966 jntmp) { 11967 if ((jnewblk->jn_state & UNDONE) == 0) 11968 continue; 11969 /* Do the roll-forward only if it's a real copy. */ 11970 if (foreground && 11971 jnewblk_rollforward(jnewblk, fs, cgp, blksfree)) 11972 chgs = 1; 11973 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK); 11974 jnewblk->jn_state |= ATTACHED; 11975 free_jnewblk(jnewblk); 11976 } 11977 } 11978 /* 11979 * If the write did not succeed, we have done all the roll-forward 11980 * operations, but we cannot take the actions that will allow its 11981 * dependencies to be processed. 11982 */ 11983 if ((flags & WRITESUCCEEDED) == 0) { 11984 LIST_CONCAT(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 11985 newblk, nb_deps); 11986 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, 11987 worklist, wk_list); 11988 if (foreground) 11989 bdirty(bp); 11990 return (1); 11991 } 11992 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) { 11993 newblk->nb_state |= DEPCOMPLETE; 11994 newblk->nb_state &= ~ONDEPLIST; 11995 newblk->nb_bmsafemap = NULL; 11996 LIST_REMOVE(newblk, nb_deps); 11997 if (newblk->nb_list.wk_type == D_ALLOCDIRECT) 11998 handle_allocdirect_partdone( 11999 WK_ALLOCDIRECT(&newblk->nb_list), NULL); 12000 else if (newblk->nb_list.wk_type == D_ALLOCINDIR) 12001 handle_allocindir_partdone( 12002 WK_ALLOCINDIR(&newblk->nb_list)); 12003 else if (newblk->nb_list.wk_type != D_NEWBLK) 12004 panic("handle_written_bmsafemap: Unexpected type: %s", 12005 TYPENAME(newblk->nb_list.wk_type)); 12006 } 12007 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) { 12008 inodedep->id_state |= DEPCOMPLETE; 12009 inodedep->id_state &= ~ONDEPLIST; 12010 LIST_REMOVE(inodedep, id_deps); 12011 inodedep->id_bmsafemap = NULL; 12012 } 12013 LIST_REMOVE(bmsafemap, sm_next); 12014 if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) && 12015 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) && 12016 LIST_EMPTY(&bmsafemap->sm_newblkhd) && 12017 LIST_EMPTY(&bmsafemap->sm_inodedephd) && 12018 LIST_EMPTY(&bmsafemap->sm_freehd)) { 12019 LIST_REMOVE(bmsafemap, sm_hash); 12020 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 12021 return (0); 12022 } 12023 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 12024 if (foreground) 12025 bdirty(bp); 12026 return (1); 12027 } 12028 12029 /* 12030 * Try to free a mkdir dependency. 12031 */ 12032 static void 12033 complete_mkdir(mkdir) 12034 struct mkdir *mkdir; 12035 { 12036 struct diradd *dap; 12037 12038 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE) 12039 return; 12040 LIST_REMOVE(mkdir, md_mkdirs); 12041 dap = mkdir->md_diradd; 12042 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 12043 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) { 12044 dap->da_state |= DEPCOMPLETE; 12045 complete_diradd(dap); 12046 } 12047 WORKITEM_FREE(mkdir, D_MKDIR); 12048 } 12049 12050 /* 12051 * Handle the completion of a mkdir dependency. 12052 */ 12053 static void 12054 handle_written_mkdir(mkdir, type) 12055 struct mkdir *mkdir; 12056 int type; 12057 { 12058 12059 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type) 12060 panic("handle_written_mkdir: bad type"); 12061 mkdir->md_state |= COMPLETE; 12062 complete_mkdir(mkdir); 12063 } 12064 12065 static int 12066 free_pagedep(pagedep) 12067 struct pagedep *pagedep; 12068 { 12069 int i; 12070 12071 if (pagedep->pd_state & NEWBLOCK) 12072 return (0); 12073 if (!LIST_EMPTY(&pagedep->pd_dirremhd)) 12074 return (0); 12075 for (i = 0; i < DAHASHSZ; i++) 12076 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i])) 12077 return (0); 12078 if (!LIST_EMPTY(&pagedep->pd_pendinghd)) 12079 return (0); 12080 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd)) 12081 return (0); 12082 if (pagedep->pd_state & ONWORKLIST) 12083 WORKLIST_REMOVE(&pagedep->pd_list); 12084 LIST_REMOVE(pagedep, pd_hash); 12085 WORKITEM_FREE(pagedep, D_PAGEDEP); 12086 12087 return (1); 12088 } 12089 12090 /* 12091 * Called from within softdep_disk_write_complete above. 12092 * A write operation was just completed. Removed inodes can 12093 * now be freed and associated block pointers may be committed. 12094 * Note that this routine is always called from interrupt level 12095 * with further interrupts from this device blocked. 12096 * 12097 * If the write did not succeed, we will do all the roll-forward 12098 * operations, but we will not take the actions that will allow its 12099 * dependencies to be processed. 12100 */ 12101 static int 12102 handle_written_filepage(pagedep, bp, flags) 12103 struct pagedep *pagedep; 12104 struct buf *bp; /* buffer containing the written page */ 12105 int flags; 12106 { 12107 struct dirrem *dirrem; 12108 struct diradd *dap, *nextdap; 12109 struct direct *ep; 12110 int i, chgs; 12111 12112 if ((pagedep->pd_state & IOSTARTED) == 0) 12113 panic("handle_written_filepage: not started"); 12114 pagedep->pd_state &= ~IOSTARTED; 12115 if ((flags & WRITESUCCEEDED) == 0) 12116 goto rollforward; 12117 /* 12118 * Process any directory removals that have been committed. 12119 */ 12120 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) { 12121 LIST_REMOVE(dirrem, dm_next); 12122 dirrem->dm_state |= COMPLETE; 12123 dirrem->dm_dirinum = pagedep->pd_ino; 12124 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 12125 ("handle_written_filepage: Journal entries not written.")); 12126 add_to_worklist(&dirrem->dm_list, 0); 12127 } 12128 /* 12129 * Free any directory additions that have been committed. 12130 * If it is a newly allocated block, we have to wait until 12131 * the on-disk directory inode claims the new block. 12132 */ 12133 if ((pagedep->pd_state & NEWBLOCK) == 0) 12134 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 12135 free_diradd(dap, NULL); 12136 rollforward: 12137 /* 12138 * Uncommitted directory entries must be restored. 12139 */ 12140 for (chgs = 0, i = 0; i < DAHASHSZ; i++) { 12141 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap; 12142 dap = nextdap) { 12143 nextdap = LIST_NEXT(dap, da_pdlist); 12144 if (dap->da_state & ATTACHED) 12145 panic("handle_written_filepage: attached"); 12146 ep = (struct direct *) 12147 ((char *)bp->b_data + dap->da_offset); 12148 ep->d_ino = dap->da_newinum; 12149 dap->da_state &= ~UNDONE; 12150 dap->da_state |= ATTACHED; 12151 chgs = 1; 12152 /* 12153 * If the inode referenced by the directory has 12154 * been written out, then the dependency can be 12155 * moved to the pending list. 12156 */ 12157 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 12158 LIST_REMOVE(dap, da_pdlist); 12159 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, 12160 da_pdlist); 12161 } 12162 } 12163 } 12164 /* 12165 * If there were any rollbacks in the directory, then it must be 12166 * marked dirty so that its will eventually get written back in 12167 * its correct form. 12168 */ 12169 if (chgs || (flags & WRITESUCCEEDED) == 0) { 12170 if ((bp->b_flags & B_DELWRI) == 0) 12171 stat_dir_entry++; 12172 bdirty(bp); 12173 return (1); 12174 } 12175 /* 12176 * If we are not waiting for a new directory block to be 12177 * claimed by its inode, then the pagedep will be freed. 12178 * Otherwise it will remain to track any new entries on 12179 * the page in case they are fsync'ed. 12180 */ 12181 free_pagedep(pagedep); 12182 return (0); 12183 } 12184 12185 /* 12186 * Writing back in-core inode structures. 12187 * 12188 * The filesystem only accesses an inode's contents when it occupies an 12189 * "in-core" inode structure. These "in-core" structures are separate from 12190 * the page frames used to cache inode blocks. Only the latter are 12191 * transferred to/from the disk. So, when the updated contents of the 12192 * "in-core" inode structure are copied to the corresponding in-memory inode 12193 * block, the dependencies are also transferred. The following procedure is 12194 * called when copying a dirty "in-core" inode to a cached inode block. 12195 */ 12196 12197 /* 12198 * Called when an inode is loaded from disk. If the effective link count 12199 * differed from the actual link count when it was last flushed, then we 12200 * need to ensure that the correct effective link count is put back. 12201 */ 12202 void 12203 softdep_load_inodeblock(ip) 12204 struct inode *ip; /* the "in_core" copy of the inode */ 12205 { 12206 struct inodedep *inodedep; 12207 struct ufsmount *ump; 12208 12209 ump = ITOUMP(ip); 12210 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 12211 ("softdep_load_inodeblock called on non-softdep filesystem")); 12212 /* 12213 * Check for alternate nlink count. 12214 */ 12215 ip->i_effnlink = ip->i_nlink; 12216 ACQUIRE_LOCK(ump); 12217 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) { 12218 FREE_LOCK(ump); 12219 return; 12220 } 12221 ip->i_effnlink -= inodedep->id_nlinkdelta; 12222 FREE_LOCK(ump); 12223 } 12224 12225 /* 12226 * This routine is called just before the "in-core" inode 12227 * information is to be copied to the in-memory inode block. 12228 * Recall that an inode block contains several inodes. If 12229 * the force flag is set, then the dependencies will be 12230 * cleared so that the update can always be made. Note that 12231 * the buffer is locked when this routine is called, so we 12232 * will never be in the middle of writing the inode block 12233 * to disk. 12234 */ 12235 void 12236 softdep_update_inodeblock(ip, bp, waitfor) 12237 struct inode *ip; /* the "in_core" copy of the inode */ 12238 struct buf *bp; /* the buffer containing the inode block */ 12239 int waitfor; /* nonzero => update must be allowed */ 12240 { 12241 struct inodedep *inodedep; 12242 struct inoref *inoref; 12243 struct ufsmount *ump; 12244 struct worklist *wk; 12245 struct mount *mp; 12246 struct buf *ibp; 12247 struct fs *fs; 12248 int error; 12249 12250 ump = ITOUMP(ip); 12251 mp = UFSTOVFS(ump); 12252 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 12253 ("softdep_update_inodeblock called on non-softdep filesystem")); 12254 fs = ump->um_fs; 12255 /* 12256 * Preserve the freelink that is on disk. clear_unlinked_inodedep() 12257 * does not have access to the in-core ip so must write directly into 12258 * the inode block buffer when setting freelink. 12259 */ 12260 if (fs->fs_magic == FS_UFS1_MAGIC) 12261 DIP_SET(ip, i_freelink, ((struct ufs1_dinode *)bp->b_data + 12262 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12263 else 12264 DIP_SET(ip, i_freelink, ((struct ufs2_dinode *)bp->b_data + 12265 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12266 /* 12267 * If the effective link count is not equal to the actual link 12268 * count, then we must track the difference in an inodedep while 12269 * the inode is (potentially) tossed out of the cache. Otherwise, 12270 * if there is no existing inodedep, then there are no dependencies 12271 * to track. 12272 */ 12273 ACQUIRE_LOCK(ump); 12274 again: 12275 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12276 FREE_LOCK(ump); 12277 if (ip->i_effnlink != ip->i_nlink) 12278 panic("softdep_update_inodeblock: bad link count"); 12279 return; 12280 } 12281 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink) 12282 panic("softdep_update_inodeblock: bad delta"); 12283 /* 12284 * If we're flushing all dependencies we must also move any waiting 12285 * for journal writes onto the bufwait list prior to I/O. 12286 */ 12287 if (waitfor) { 12288 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12289 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12290 == DEPCOMPLETE) { 12291 jwait(&inoref->if_list, MNT_WAIT); 12292 goto again; 12293 } 12294 } 12295 } 12296 /* 12297 * Changes have been initiated. Anything depending on these 12298 * changes cannot occur until this inode has been written. 12299 */ 12300 inodedep->id_state &= ~COMPLETE; 12301 if ((inodedep->id_state & ONWORKLIST) == 0) 12302 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list); 12303 /* 12304 * Any new dependencies associated with the incore inode must 12305 * now be moved to the list associated with the buffer holding 12306 * the in-memory copy of the inode. Once merged process any 12307 * allocdirects that are completed by the merger. 12308 */ 12309 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt); 12310 if (!TAILQ_EMPTY(&inodedep->id_inoupdt)) 12311 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt), 12312 NULL); 12313 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt); 12314 if (!TAILQ_EMPTY(&inodedep->id_extupdt)) 12315 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt), 12316 NULL); 12317 /* 12318 * Now that the inode has been pushed into the buffer, the 12319 * operations dependent on the inode being written to disk 12320 * can be moved to the id_bufwait so that they will be 12321 * processed when the buffer I/O completes. 12322 */ 12323 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) { 12324 WORKLIST_REMOVE(wk); 12325 WORKLIST_INSERT(&inodedep->id_bufwait, wk); 12326 } 12327 /* 12328 * Newly allocated inodes cannot be written until the bitmap 12329 * that allocates them have been written (indicated by 12330 * DEPCOMPLETE being set in id_state). If we are doing a 12331 * forced sync (e.g., an fsync on a file), we force the bitmap 12332 * to be written so that the update can be done. 12333 */ 12334 if (waitfor == 0) { 12335 FREE_LOCK(ump); 12336 return; 12337 } 12338 retry: 12339 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) { 12340 FREE_LOCK(ump); 12341 return; 12342 } 12343 ibp = inodedep->id_bmsafemap->sm_buf; 12344 ibp = getdirtybuf(ibp, LOCK_PTR(ump), MNT_WAIT); 12345 if (ibp == NULL) { 12346 /* 12347 * If ibp came back as NULL, the dependency could have been 12348 * freed while we slept. Look it up again, and check to see 12349 * that it has completed. 12350 */ 12351 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 12352 goto retry; 12353 FREE_LOCK(ump); 12354 return; 12355 } 12356 FREE_LOCK(ump); 12357 if ((error = bwrite(ibp)) != 0) 12358 softdep_error("softdep_update_inodeblock: bwrite", error); 12359 } 12360 12361 /* 12362 * Merge the a new inode dependency list (such as id_newinoupdt) into an 12363 * old inode dependency list (such as id_inoupdt). This routine must be 12364 * called with splbio interrupts blocked. 12365 */ 12366 static void 12367 merge_inode_lists(newlisthead, oldlisthead) 12368 struct allocdirectlst *newlisthead; 12369 struct allocdirectlst *oldlisthead; 12370 { 12371 struct allocdirect *listadp, *newadp; 12372 12373 newadp = TAILQ_FIRST(newlisthead); 12374 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) { 12375 if (listadp->ad_offset < newadp->ad_offset) { 12376 listadp = TAILQ_NEXT(listadp, ad_next); 12377 continue; 12378 } 12379 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12380 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next); 12381 if (listadp->ad_offset == newadp->ad_offset) { 12382 allocdirect_merge(oldlisthead, newadp, 12383 listadp); 12384 listadp = newadp; 12385 } 12386 newadp = TAILQ_FIRST(newlisthead); 12387 } 12388 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) { 12389 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12390 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next); 12391 } 12392 } 12393 12394 /* 12395 * If we are doing an fsync, then we must ensure that any directory 12396 * entries for the inode have been written after the inode gets to disk. 12397 */ 12398 int 12399 softdep_fsync(vp) 12400 struct vnode *vp; /* the "in_core" copy of the inode */ 12401 { 12402 struct inodedep *inodedep; 12403 struct pagedep *pagedep; 12404 struct inoref *inoref; 12405 struct ufsmount *ump; 12406 struct worklist *wk; 12407 struct diradd *dap; 12408 struct mount *mp; 12409 struct vnode *pvp; 12410 struct inode *ip; 12411 struct buf *bp; 12412 struct fs *fs; 12413 struct thread *td = curthread; 12414 int error, flushparent, pagedep_new_block; 12415 ino_t parentino; 12416 ufs_lbn_t lbn; 12417 12418 ip = VTOI(vp); 12419 mp = vp->v_mount; 12420 ump = VFSTOUFS(mp); 12421 fs = ump->um_fs; 12422 if (MOUNTEDSOFTDEP(mp) == 0) 12423 return (0); 12424 ACQUIRE_LOCK(ump); 12425 restart: 12426 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12427 FREE_LOCK(ump); 12428 return (0); 12429 } 12430 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12431 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12432 == DEPCOMPLETE) { 12433 jwait(&inoref->if_list, MNT_WAIT); 12434 goto restart; 12435 } 12436 } 12437 if (!LIST_EMPTY(&inodedep->id_inowait) || 12438 !TAILQ_EMPTY(&inodedep->id_extupdt) || 12439 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 12440 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 12441 !TAILQ_EMPTY(&inodedep->id_newinoupdt)) 12442 panic("softdep_fsync: pending ops %p", inodedep); 12443 for (error = 0, flushparent = 0; ; ) { 12444 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL) 12445 break; 12446 if (wk->wk_type != D_DIRADD) 12447 panic("softdep_fsync: Unexpected type %s", 12448 TYPENAME(wk->wk_type)); 12449 dap = WK_DIRADD(wk); 12450 /* 12451 * Flush our parent if this directory entry has a MKDIR_PARENT 12452 * dependency or is contained in a newly allocated block. 12453 */ 12454 if (dap->da_state & DIRCHG) 12455 pagedep = dap->da_previous->dm_pagedep; 12456 else 12457 pagedep = dap->da_pagedep; 12458 parentino = pagedep->pd_ino; 12459 lbn = pagedep->pd_lbn; 12460 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) 12461 panic("softdep_fsync: dirty"); 12462 if ((dap->da_state & MKDIR_PARENT) || 12463 (pagedep->pd_state & NEWBLOCK)) 12464 flushparent = 1; 12465 else 12466 flushparent = 0; 12467 /* 12468 * If we are being fsync'ed as part of vgone'ing this vnode, 12469 * then we will not be able to release and recover the 12470 * vnode below, so we just have to give up on writing its 12471 * directory entry out. It will eventually be written, just 12472 * not now, but then the user was not asking to have it 12473 * written, so we are not breaking any promises. 12474 */ 12475 if (vp->v_iflag & VI_DOOMED) 12476 break; 12477 /* 12478 * We prevent deadlock by always fetching inodes from the 12479 * root, moving down the directory tree. Thus, when fetching 12480 * our parent directory, we first try to get the lock. If 12481 * that fails, we must unlock ourselves before requesting 12482 * the lock on our parent. See the comment in ufs_lookup 12483 * for details on possible races. 12484 */ 12485 FREE_LOCK(ump); 12486 if (ffs_vgetf(mp, parentino, LK_NOWAIT | LK_EXCLUSIVE, &pvp, 12487 FFSV_FORCEINSMQ)) { 12488 error = vfs_busy(mp, MBF_NOWAIT); 12489 if (error != 0) { 12490 vfs_ref(mp); 12491 VOP_UNLOCK(vp, 0); 12492 error = vfs_busy(mp, 0); 12493 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 12494 vfs_rel(mp); 12495 if (error != 0) 12496 return (ENOENT); 12497 if (vp->v_iflag & VI_DOOMED) { 12498 vfs_unbusy(mp); 12499 return (ENOENT); 12500 } 12501 } 12502 VOP_UNLOCK(vp, 0); 12503 error = ffs_vgetf(mp, parentino, LK_EXCLUSIVE, 12504 &pvp, FFSV_FORCEINSMQ); 12505 vfs_unbusy(mp); 12506 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 12507 if (vp->v_iflag & VI_DOOMED) { 12508 if (error == 0) 12509 vput(pvp); 12510 error = ENOENT; 12511 } 12512 if (error != 0) 12513 return (error); 12514 } 12515 /* 12516 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps 12517 * that are contained in direct blocks will be resolved by 12518 * doing a ffs_update. Pagedeps contained in indirect blocks 12519 * may require a complete sync'ing of the directory. So, we 12520 * try the cheap and fast ffs_update first, and if that fails, 12521 * then we do the slower ffs_syncvnode of the directory. 12522 */ 12523 if (flushparent) { 12524 int locked; 12525 12526 if ((error = ffs_update(pvp, 1)) != 0) { 12527 vput(pvp); 12528 return (error); 12529 } 12530 ACQUIRE_LOCK(ump); 12531 locked = 1; 12532 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) { 12533 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) { 12534 if (wk->wk_type != D_DIRADD) 12535 panic("softdep_fsync: Unexpected type %s", 12536 TYPENAME(wk->wk_type)); 12537 dap = WK_DIRADD(wk); 12538 if (dap->da_state & DIRCHG) 12539 pagedep = dap->da_previous->dm_pagedep; 12540 else 12541 pagedep = dap->da_pagedep; 12542 pagedep_new_block = pagedep->pd_state & NEWBLOCK; 12543 FREE_LOCK(ump); 12544 locked = 0; 12545 if (pagedep_new_block && (error = 12546 ffs_syncvnode(pvp, MNT_WAIT, 0))) { 12547 vput(pvp); 12548 return (error); 12549 } 12550 } 12551 } 12552 if (locked) 12553 FREE_LOCK(ump); 12554 } 12555 /* 12556 * Flush directory page containing the inode's name. 12557 */ 12558 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred, 12559 &bp); 12560 if (error == 0) 12561 error = bwrite(bp); 12562 else 12563 brelse(bp); 12564 vput(pvp); 12565 if (error != 0) 12566 return (error); 12567 ACQUIRE_LOCK(ump); 12568 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 12569 break; 12570 } 12571 FREE_LOCK(ump); 12572 return (0); 12573 } 12574 12575 /* 12576 * Flush all the dirty bitmaps associated with the block device 12577 * before flushing the rest of the dirty blocks so as to reduce 12578 * the number of dependencies that will have to be rolled back. 12579 * 12580 * XXX Unused? 12581 */ 12582 void 12583 softdep_fsync_mountdev(vp) 12584 struct vnode *vp; 12585 { 12586 struct buf *bp, *nbp; 12587 struct worklist *wk; 12588 struct bufobj *bo; 12589 12590 if (!vn_isdisk(vp, NULL)) 12591 panic("softdep_fsync_mountdev: vnode not a disk"); 12592 bo = &vp->v_bufobj; 12593 restart: 12594 BO_LOCK(bo); 12595 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 12596 /* 12597 * If it is already scheduled, skip to the next buffer. 12598 */ 12599 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) 12600 continue; 12601 12602 if ((bp->b_flags & B_DELWRI) == 0) 12603 panic("softdep_fsync_mountdev: not dirty"); 12604 /* 12605 * We are only interested in bitmaps with outstanding 12606 * dependencies. 12607 */ 12608 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL || 12609 wk->wk_type != D_BMSAFEMAP || 12610 (bp->b_vflags & BV_BKGRDINPROG)) { 12611 BUF_UNLOCK(bp); 12612 continue; 12613 } 12614 BO_UNLOCK(bo); 12615 bremfree(bp); 12616 (void) bawrite(bp); 12617 goto restart; 12618 } 12619 drain_output(vp); 12620 BO_UNLOCK(bo); 12621 } 12622 12623 /* 12624 * Sync all cylinder groups that were dirty at the time this function is 12625 * called. Newly dirtied cgs will be inserted before the sentinel. This 12626 * is used to flush freedep activity that may be holding up writes to a 12627 * indirect block. 12628 */ 12629 static int 12630 sync_cgs(mp, waitfor) 12631 struct mount *mp; 12632 int waitfor; 12633 { 12634 struct bmsafemap *bmsafemap; 12635 struct bmsafemap *sentinel; 12636 struct ufsmount *ump; 12637 struct buf *bp; 12638 int error; 12639 12640 sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK); 12641 sentinel->sm_cg = -1; 12642 ump = VFSTOUFS(mp); 12643 error = 0; 12644 ACQUIRE_LOCK(ump); 12645 LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next); 12646 for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL; 12647 bmsafemap = LIST_NEXT(sentinel, sm_next)) { 12648 /* Skip sentinels and cgs with no work to release. */ 12649 if (bmsafemap->sm_cg == -1 || 12650 (LIST_EMPTY(&bmsafemap->sm_freehd) && 12651 LIST_EMPTY(&bmsafemap->sm_freewr))) { 12652 LIST_REMOVE(sentinel, sm_next); 12653 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 12654 continue; 12655 } 12656 /* 12657 * If we don't get the lock and we're waiting try again, if 12658 * not move on to the next buf and try to sync it. 12659 */ 12660 bp = getdirtybuf(bmsafemap->sm_buf, LOCK_PTR(ump), waitfor); 12661 if (bp == NULL && waitfor == MNT_WAIT) 12662 continue; 12663 LIST_REMOVE(sentinel, sm_next); 12664 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 12665 if (bp == NULL) 12666 continue; 12667 FREE_LOCK(ump); 12668 if (waitfor == MNT_NOWAIT) 12669 bawrite(bp); 12670 else 12671 error = bwrite(bp); 12672 ACQUIRE_LOCK(ump); 12673 if (error) 12674 break; 12675 } 12676 LIST_REMOVE(sentinel, sm_next); 12677 FREE_LOCK(ump); 12678 free(sentinel, M_BMSAFEMAP); 12679 return (error); 12680 } 12681 12682 /* 12683 * This routine is called when we are trying to synchronously flush a 12684 * file. This routine must eliminate any filesystem metadata dependencies 12685 * so that the syncing routine can succeed. 12686 */ 12687 int 12688 softdep_sync_metadata(struct vnode *vp) 12689 { 12690 struct inode *ip; 12691 int error; 12692 12693 ip = VTOI(vp); 12694 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 12695 ("softdep_sync_metadata called on non-softdep filesystem")); 12696 /* 12697 * Ensure that any direct block dependencies have been cleared, 12698 * truncations are started, and inode references are journaled. 12699 */ 12700 ACQUIRE_LOCK(VFSTOUFS(vp->v_mount)); 12701 /* 12702 * Write all journal records to prevent rollbacks on devvp. 12703 */ 12704 if (vp->v_type == VCHR) 12705 softdep_flushjournal(vp->v_mount); 12706 error = flush_inodedep_deps(vp, vp->v_mount, ip->i_number); 12707 /* 12708 * Ensure that all truncates are written so we won't find deps on 12709 * indirect blocks. 12710 */ 12711 process_truncates(vp); 12712 FREE_LOCK(VFSTOUFS(vp->v_mount)); 12713 12714 return (error); 12715 } 12716 12717 /* 12718 * This routine is called when we are attempting to sync a buf with 12719 * dependencies. If waitfor is MNT_NOWAIT it attempts to schedule any 12720 * other IO it can but returns EBUSY if the buffer is not yet able to 12721 * be written. Dependencies which will not cause rollbacks will always 12722 * return 0. 12723 */ 12724 int 12725 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 12726 { 12727 struct indirdep *indirdep; 12728 struct pagedep *pagedep; 12729 struct allocindir *aip; 12730 struct newblk *newblk; 12731 struct ufsmount *ump; 12732 struct buf *nbp; 12733 struct worklist *wk; 12734 int i, error; 12735 12736 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 12737 ("softdep_sync_buf called on non-softdep filesystem")); 12738 /* 12739 * For VCHR we just don't want to force flush any dependencies that 12740 * will cause rollbacks. 12741 */ 12742 if (vp->v_type == VCHR) { 12743 if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0)) 12744 return (EBUSY); 12745 return (0); 12746 } 12747 ump = VFSTOUFS(vp->v_mount); 12748 ACQUIRE_LOCK(ump); 12749 /* 12750 * As we hold the buffer locked, none of its dependencies 12751 * will disappear. 12752 */ 12753 error = 0; 12754 top: 12755 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 12756 switch (wk->wk_type) { 12757 12758 case D_ALLOCDIRECT: 12759 case D_ALLOCINDIR: 12760 newblk = WK_NEWBLK(wk); 12761 if (newblk->nb_jnewblk != NULL) { 12762 if (waitfor == MNT_NOWAIT) { 12763 error = EBUSY; 12764 goto out_unlock; 12765 } 12766 jwait(&newblk->nb_jnewblk->jn_list, waitfor); 12767 goto top; 12768 } 12769 if (newblk->nb_state & DEPCOMPLETE || 12770 waitfor == MNT_NOWAIT) 12771 continue; 12772 nbp = newblk->nb_bmsafemap->sm_buf; 12773 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 12774 if (nbp == NULL) 12775 goto top; 12776 FREE_LOCK(ump); 12777 if ((error = bwrite(nbp)) != 0) 12778 goto out; 12779 ACQUIRE_LOCK(ump); 12780 continue; 12781 12782 case D_INDIRDEP: 12783 indirdep = WK_INDIRDEP(wk); 12784 if (waitfor == MNT_NOWAIT) { 12785 if (!TAILQ_EMPTY(&indirdep->ir_trunc) || 12786 !LIST_EMPTY(&indirdep->ir_deplisthd)) { 12787 error = EBUSY; 12788 goto out_unlock; 12789 } 12790 } 12791 if (!TAILQ_EMPTY(&indirdep->ir_trunc)) 12792 panic("softdep_sync_buf: truncation pending."); 12793 restart: 12794 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 12795 newblk = (struct newblk *)aip; 12796 if (newblk->nb_jnewblk != NULL) { 12797 jwait(&newblk->nb_jnewblk->jn_list, 12798 waitfor); 12799 goto restart; 12800 } 12801 if (newblk->nb_state & DEPCOMPLETE) 12802 continue; 12803 nbp = newblk->nb_bmsafemap->sm_buf; 12804 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 12805 if (nbp == NULL) 12806 goto restart; 12807 FREE_LOCK(ump); 12808 if ((error = bwrite(nbp)) != 0) 12809 goto out; 12810 ACQUIRE_LOCK(ump); 12811 goto restart; 12812 } 12813 continue; 12814 12815 case D_PAGEDEP: 12816 /* 12817 * Only flush directory entries in synchronous passes. 12818 */ 12819 if (waitfor != MNT_WAIT) { 12820 error = EBUSY; 12821 goto out_unlock; 12822 } 12823 /* 12824 * While syncing snapshots, we must allow recursive 12825 * lookups. 12826 */ 12827 BUF_AREC(bp); 12828 /* 12829 * We are trying to sync a directory that may 12830 * have dependencies on both its own metadata 12831 * and/or dependencies on the inodes of any 12832 * recently allocated files. We walk its diradd 12833 * lists pushing out the associated inode. 12834 */ 12835 pagedep = WK_PAGEDEP(wk); 12836 for (i = 0; i < DAHASHSZ; i++) { 12837 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0) 12838 continue; 12839 if ((error = flush_pagedep_deps(vp, wk->wk_mp, 12840 &pagedep->pd_diraddhd[i]))) { 12841 BUF_NOREC(bp); 12842 goto out_unlock; 12843 } 12844 } 12845 BUF_NOREC(bp); 12846 continue; 12847 12848 case D_FREEWORK: 12849 case D_FREEDEP: 12850 case D_JSEGDEP: 12851 case D_JNEWBLK: 12852 continue; 12853 12854 default: 12855 panic("softdep_sync_buf: Unknown type %s", 12856 TYPENAME(wk->wk_type)); 12857 /* NOTREACHED */ 12858 } 12859 } 12860 out_unlock: 12861 FREE_LOCK(ump); 12862 out: 12863 return (error); 12864 } 12865 12866 /* 12867 * Flush the dependencies associated with an inodedep. 12868 * Called with splbio blocked. 12869 */ 12870 static int 12871 flush_inodedep_deps(vp, mp, ino) 12872 struct vnode *vp; 12873 struct mount *mp; 12874 ino_t ino; 12875 { 12876 struct inodedep *inodedep; 12877 struct inoref *inoref; 12878 struct ufsmount *ump; 12879 int error, waitfor; 12880 12881 /* 12882 * This work is done in two passes. The first pass grabs most 12883 * of the buffers and begins asynchronously writing them. The 12884 * only way to wait for these asynchronous writes is to sleep 12885 * on the filesystem vnode which may stay busy for a long time 12886 * if the filesystem is active. So, instead, we make a second 12887 * pass over the dependencies blocking on each write. In the 12888 * usual case we will be blocking against a write that we 12889 * initiated, so when it is done the dependency will have been 12890 * resolved. Thus the second pass is expected to end quickly. 12891 * We give a brief window at the top of the loop to allow 12892 * any pending I/O to complete. 12893 */ 12894 ump = VFSTOUFS(mp); 12895 LOCK_OWNED(ump); 12896 for (error = 0, waitfor = MNT_NOWAIT; ; ) { 12897 if (error) 12898 return (error); 12899 FREE_LOCK(ump); 12900 ACQUIRE_LOCK(ump); 12901 restart: 12902 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 12903 return (0); 12904 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12905 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12906 == DEPCOMPLETE) { 12907 jwait(&inoref->if_list, MNT_WAIT); 12908 goto restart; 12909 } 12910 } 12911 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) || 12912 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) || 12913 flush_deplist(&inodedep->id_extupdt, waitfor, &error) || 12914 flush_deplist(&inodedep->id_newextupdt, waitfor, &error)) 12915 continue; 12916 /* 12917 * If pass2, we are done, otherwise do pass 2. 12918 */ 12919 if (waitfor == MNT_WAIT) 12920 break; 12921 waitfor = MNT_WAIT; 12922 } 12923 /* 12924 * Try freeing inodedep in case all dependencies have been removed. 12925 */ 12926 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0) 12927 (void) free_inodedep(inodedep); 12928 return (0); 12929 } 12930 12931 /* 12932 * Flush an inode dependency list. 12933 * Called with splbio blocked. 12934 */ 12935 static int 12936 flush_deplist(listhead, waitfor, errorp) 12937 struct allocdirectlst *listhead; 12938 int waitfor; 12939 int *errorp; 12940 { 12941 struct allocdirect *adp; 12942 struct newblk *newblk; 12943 struct ufsmount *ump; 12944 struct buf *bp; 12945 12946 if ((adp = TAILQ_FIRST(listhead)) == NULL) 12947 return (0); 12948 ump = VFSTOUFS(adp->ad_list.wk_mp); 12949 LOCK_OWNED(ump); 12950 TAILQ_FOREACH(adp, listhead, ad_next) { 12951 newblk = (struct newblk *)adp; 12952 if (newblk->nb_jnewblk != NULL) { 12953 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 12954 return (1); 12955 } 12956 if (newblk->nb_state & DEPCOMPLETE) 12957 continue; 12958 bp = newblk->nb_bmsafemap->sm_buf; 12959 bp = getdirtybuf(bp, LOCK_PTR(ump), waitfor); 12960 if (bp == NULL) { 12961 if (waitfor == MNT_NOWAIT) 12962 continue; 12963 return (1); 12964 } 12965 FREE_LOCK(ump); 12966 if (waitfor == MNT_NOWAIT) 12967 bawrite(bp); 12968 else 12969 *errorp = bwrite(bp); 12970 ACQUIRE_LOCK(ump); 12971 return (1); 12972 } 12973 return (0); 12974 } 12975 12976 /* 12977 * Flush dependencies associated with an allocdirect block. 12978 */ 12979 static int 12980 flush_newblk_dep(vp, mp, lbn) 12981 struct vnode *vp; 12982 struct mount *mp; 12983 ufs_lbn_t lbn; 12984 { 12985 struct newblk *newblk; 12986 struct ufsmount *ump; 12987 struct bufobj *bo; 12988 struct inode *ip; 12989 struct buf *bp; 12990 ufs2_daddr_t blkno; 12991 int error; 12992 12993 error = 0; 12994 bo = &vp->v_bufobj; 12995 ip = VTOI(vp); 12996 blkno = DIP(ip, i_db[lbn]); 12997 if (blkno == 0) 12998 panic("flush_newblk_dep: Missing block"); 12999 ump = VFSTOUFS(mp); 13000 ACQUIRE_LOCK(ump); 13001 /* 13002 * Loop until all dependencies related to this block are satisfied. 13003 * We must be careful to restart after each sleep in case a write 13004 * completes some part of this process for us. 13005 */ 13006 for (;;) { 13007 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) { 13008 FREE_LOCK(ump); 13009 break; 13010 } 13011 if (newblk->nb_list.wk_type != D_ALLOCDIRECT) 13012 panic("flush_newblk_dep: Bad newblk %p", newblk); 13013 /* 13014 * Flush the journal. 13015 */ 13016 if (newblk->nb_jnewblk != NULL) { 13017 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 13018 continue; 13019 } 13020 /* 13021 * Write the bitmap dependency. 13022 */ 13023 if ((newblk->nb_state & DEPCOMPLETE) == 0) { 13024 bp = newblk->nb_bmsafemap->sm_buf; 13025 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 13026 if (bp == NULL) 13027 continue; 13028 FREE_LOCK(ump); 13029 error = bwrite(bp); 13030 if (error) 13031 break; 13032 ACQUIRE_LOCK(ump); 13033 continue; 13034 } 13035 /* 13036 * Write the buffer. 13037 */ 13038 FREE_LOCK(ump); 13039 BO_LOCK(bo); 13040 bp = gbincore(bo, lbn); 13041 if (bp != NULL) { 13042 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 13043 LK_INTERLOCK, BO_LOCKPTR(bo)); 13044 if (error == ENOLCK) { 13045 ACQUIRE_LOCK(ump); 13046 error = 0; 13047 continue; /* Slept, retry */ 13048 } 13049 if (error != 0) 13050 break; /* Failed */ 13051 if (bp->b_flags & B_DELWRI) { 13052 bremfree(bp); 13053 error = bwrite(bp); 13054 if (error) 13055 break; 13056 } else 13057 BUF_UNLOCK(bp); 13058 } else 13059 BO_UNLOCK(bo); 13060 /* 13061 * We have to wait for the direct pointers to 13062 * point at the newdirblk before the dependency 13063 * will go away. 13064 */ 13065 error = ffs_update(vp, 1); 13066 if (error) 13067 break; 13068 ACQUIRE_LOCK(ump); 13069 } 13070 return (error); 13071 } 13072 13073 /* 13074 * Eliminate a pagedep dependency by flushing out all its diradd dependencies. 13075 * Called with splbio blocked. 13076 */ 13077 static int 13078 flush_pagedep_deps(pvp, mp, diraddhdp) 13079 struct vnode *pvp; 13080 struct mount *mp; 13081 struct diraddhd *diraddhdp; 13082 { 13083 struct inodedep *inodedep; 13084 struct inoref *inoref; 13085 struct ufsmount *ump; 13086 struct diradd *dap; 13087 struct vnode *vp; 13088 int error = 0; 13089 struct buf *bp; 13090 ino_t inum; 13091 struct diraddhd unfinished; 13092 13093 LIST_INIT(&unfinished); 13094 ump = VFSTOUFS(mp); 13095 LOCK_OWNED(ump); 13096 restart: 13097 while ((dap = LIST_FIRST(diraddhdp)) != NULL) { 13098 /* 13099 * Flush ourselves if this directory entry 13100 * has a MKDIR_PARENT dependency. 13101 */ 13102 if (dap->da_state & MKDIR_PARENT) { 13103 FREE_LOCK(ump); 13104 if ((error = ffs_update(pvp, 1)) != 0) 13105 break; 13106 ACQUIRE_LOCK(ump); 13107 /* 13108 * If that cleared dependencies, go on to next. 13109 */ 13110 if (dap != LIST_FIRST(diraddhdp)) 13111 continue; 13112 /* 13113 * All MKDIR_PARENT dependencies and all the 13114 * NEWBLOCK pagedeps that are contained in direct 13115 * blocks were resolved by doing above ffs_update. 13116 * Pagedeps contained in indirect blocks may 13117 * require a complete sync'ing of the directory. 13118 * We are in the midst of doing a complete sync, 13119 * so if they are not resolved in this pass we 13120 * defer them for now as they will be sync'ed by 13121 * our caller shortly. 13122 */ 13123 LIST_REMOVE(dap, da_pdlist); 13124 LIST_INSERT_HEAD(&unfinished, dap, da_pdlist); 13125 continue; 13126 } 13127 /* 13128 * A newly allocated directory must have its "." and 13129 * ".." entries written out before its name can be 13130 * committed in its parent. 13131 */ 13132 inum = dap->da_newinum; 13133 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 13134 panic("flush_pagedep_deps: lost inode1"); 13135 /* 13136 * Wait for any pending journal adds to complete so we don't 13137 * cause rollbacks while syncing. 13138 */ 13139 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 13140 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 13141 == DEPCOMPLETE) { 13142 jwait(&inoref->if_list, MNT_WAIT); 13143 goto restart; 13144 } 13145 } 13146 if (dap->da_state & MKDIR_BODY) { 13147 FREE_LOCK(ump); 13148 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp, 13149 FFSV_FORCEINSMQ))) 13150 break; 13151 error = flush_newblk_dep(vp, mp, 0); 13152 /* 13153 * If we still have the dependency we might need to 13154 * update the vnode to sync the new link count to 13155 * disk. 13156 */ 13157 if (error == 0 && dap == LIST_FIRST(diraddhdp)) 13158 error = ffs_update(vp, 1); 13159 vput(vp); 13160 if (error != 0) 13161 break; 13162 ACQUIRE_LOCK(ump); 13163 /* 13164 * If that cleared dependencies, go on to next. 13165 */ 13166 if (dap != LIST_FIRST(diraddhdp)) 13167 continue; 13168 if (dap->da_state & MKDIR_BODY) { 13169 inodedep_lookup(UFSTOVFS(ump), inum, 0, 13170 &inodedep); 13171 panic("flush_pagedep_deps: MKDIR_BODY " 13172 "inodedep %p dap %p vp %p", 13173 inodedep, dap, vp); 13174 } 13175 } 13176 /* 13177 * Flush the inode on which the directory entry depends. 13178 * Having accounted for MKDIR_PARENT and MKDIR_BODY above, 13179 * the only remaining dependency is that the updated inode 13180 * count must get pushed to disk. The inode has already 13181 * been pushed into its inode buffer (via VOP_UPDATE) at 13182 * the time of the reference count change. So we need only 13183 * locate that buffer, ensure that there will be no rollback 13184 * caused by a bitmap dependency, then write the inode buffer. 13185 */ 13186 retry: 13187 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 13188 panic("flush_pagedep_deps: lost inode"); 13189 /* 13190 * If the inode still has bitmap dependencies, 13191 * push them to disk. 13192 */ 13193 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) { 13194 bp = inodedep->id_bmsafemap->sm_buf; 13195 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 13196 if (bp == NULL) 13197 goto retry; 13198 FREE_LOCK(ump); 13199 if ((error = bwrite(bp)) != 0) 13200 break; 13201 ACQUIRE_LOCK(ump); 13202 if (dap != LIST_FIRST(diraddhdp)) 13203 continue; 13204 } 13205 /* 13206 * If the inode is still sitting in a buffer waiting 13207 * to be written or waiting for the link count to be 13208 * adjusted update it here to flush it to disk. 13209 */ 13210 if (dap == LIST_FIRST(diraddhdp)) { 13211 FREE_LOCK(ump); 13212 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp, 13213 FFSV_FORCEINSMQ))) 13214 break; 13215 error = ffs_update(vp, 1); 13216 vput(vp); 13217 if (error) 13218 break; 13219 ACQUIRE_LOCK(ump); 13220 } 13221 /* 13222 * If we have failed to get rid of all the dependencies 13223 * then something is seriously wrong. 13224 */ 13225 if (dap == LIST_FIRST(diraddhdp)) { 13226 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep); 13227 panic("flush_pagedep_deps: failed to flush " 13228 "inodedep %p ino %ju dap %p", 13229 inodedep, (uintmax_t)inum, dap); 13230 } 13231 } 13232 if (error) 13233 ACQUIRE_LOCK(ump); 13234 while ((dap = LIST_FIRST(&unfinished)) != NULL) { 13235 LIST_REMOVE(dap, da_pdlist); 13236 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist); 13237 } 13238 return (error); 13239 } 13240 13241 /* 13242 * A large burst of file addition or deletion activity can drive the 13243 * memory load excessively high. First attempt to slow things down 13244 * using the techniques below. If that fails, this routine requests 13245 * the offending operations to fall back to running synchronously 13246 * until the memory load returns to a reasonable level. 13247 */ 13248 int 13249 softdep_slowdown(vp) 13250 struct vnode *vp; 13251 { 13252 struct ufsmount *ump; 13253 int jlow; 13254 int max_softdeps_hard; 13255 13256 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 13257 ("softdep_slowdown called on non-softdep filesystem")); 13258 ump = VFSTOUFS(vp->v_mount); 13259 ACQUIRE_LOCK(ump); 13260 jlow = 0; 13261 /* 13262 * Check for journal space if needed. 13263 */ 13264 if (DOINGSUJ(vp)) { 13265 if (journal_space(ump, 0) == 0) 13266 jlow = 1; 13267 } 13268 /* 13269 * If the system is under its limits and our filesystem is 13270 * not responsible for more than our share of the usage and 13271 * we are not low on journal space, then no need to slow down. 13272 */ 13273 max_softdeps_hard = max_softdeps * 11 / 10; 13274 if (dep_current[D_DIRREM] < max_softdeps_hard / 2 && 13275 dep_current[D_INODEDEP] < max_softdeps_hard && 13276 dep_current[D_INDIRDEP] < max_softdeps_hard / 1000 && 13277 dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0 && 13278 ump->softdep_curdeps[D_DIRREM] < 13279 (max_softdeps_hard / 2) / stat_flush_threads && 13280 ump->softdep_curdeps[D_INODEDEP] < 13281 max_softdeps_hard / stat_flush_threads && 13282 ump->softdep_curdeps[D_INDIRDEP] < 13283 (max_softdeps_hard / 1000) / stat_flush_threads && 13284 ump->softdep_curdeps[D_FREEBLKS] < 13285 max_softdeps_hard / stat_flush_threads) { 13286 FREE_LOCK(ump); 13287 return (0); 13288 } 13289 /* 13290 * If the journal is low or our filesystem is over its limit 13291 * then speedup the cleanup. 13292 */ 13293 if (ump->softdep_curdeps[D_INDIRDEP] < 13294 (max_softdeps_hard / 1000) / stat_flush_threads || jlow) 13295 softdep_speedup(ump); 13296 stat_sync_limit_hit += 1; 13297 FREE_LOCK(ump); 13298 /* 13299 * We only slow down the rate at which new dependencies are 13300 * generated if we are not using journaling. With journaling, 13301 * the cleanup should always be sufficient to keep things 13302 * under control. 13303 */ 13304 if (DOINGSUJ(vp)) 13305 return (0); 13306 return (1); 13307 } 13308 13309 /* 13310 * Called by the allocation routines when they are about to fail 13311 * in the hope that we can free up the requested resource (inodes 13312 * or disk space). 13313 * 13314 * First check to see if the work list has anything on it. If it has, 13315 * clean up entries until we successfully free the requested resource. 13316 * Because this process holds inodes locked, we cannot handle any remove 13317 * requests that might block on a locked inode as that could lead to 13318 * deadlock. If the worklist yields none of the requested resource, 13319 * start syncing out vnodes to free up the needed space. 13320 */ 13321 int 13322 softdep_request_cleanup(fs, vp, cred, resource) 13323 struct fs *fs; 13324 struct vnode *vp; 13325 struct ucred *cred; 13326 int resource; 13327 { 13328 struct ufsmount *ump; 13329 struct mount *mp; 13330 long starttime; 13331 ufs2_daddr_t needed; 13332 int error, failed_vnode; 13333 13334 /* 13335 * If we are being called because of a process doing a 13336 * copy-on-write, then it is not safe to process any 13337 * worklist items as we will recurse into the copyonwrite 13338 * routine. This will result in an incoherent snapshot. 13339 * If the vnode that we hold is a snapshot, we must avoid 13340 * handling other resources that could cause deadlock. 13341 */ 13342 if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp))) 13343 return (0); 13344 13345 if (resource == FLUSH_BLOCKS_WAIT) 13346 stat_cleanup_blkrequests += 1; 13347 else 13348 stat_cleanup_inorequests += 1; 13349 13350 mp = vp->v_mount; 13351 ump = VFSTOUFS(mp); 13352 mtx_assert(UFS_MTX(ump), MA_OWNED); 13353 UFS_UNLOCK(ump); 13354 error = ffs_update(vp, 1); 13355 if (error != 0 || MOUNTEDSOFTDEP(mp) == 0) { 13356 UFS_LOCK(ump); 13357 return (0); 13358 } 13359 /* 13360 * If we are in need of resources, start by cleaning up 13361 * any block removals associated with our inode. 13362 */ 13363 ACQUIRE_LOCK(ump); 13364 process_removes(vp); 13365 process_truncates(vp); 13366 FREE_LOCK(ump); 13367 /* 13368 * Now clean up at least as many resources as we will need. 13369 * 13370 * When requested to clean up inodes, the number that are needed 13371 * is set by the number of simultaneous writers (mnt_writeopcount) 13372 * plus a bit of slop (2) in case some more writers show up while 13373 * we are cleaning. 13374 * 13375 * When requested to free up space, the amount of space that 13376 * we need is enough blocks to allocate a full-sized segment 13377 * (fs_contigsumsize). The number of such segments that will 13378 * be needed is set by the number of simultaneous writers 13379 * (mnt_writeopcount) plus a bit of slop (2) in case some more 13380 * writers show up while we are cleaning. 13381 * 13382 * Additionally, if we are unpriviledged and allocating space, 13383 * we need to ensure that we clean up enough blocks to get the 13384 * needed number of blocks over the threshold of the minimum 13385 * number of blocks required to be kept free by the filesystem 13386 * (fs_minfree). 13387 */ 13388 if (resource == FLUSH_INODES_WAIT) { 13389 needed = vp->v_mount->mnt_writeopcount + 2; 13390 } else if (resource == FLUSH_BLOCKS_WAIT) { 13391 needed = (vp->v_mount->mnt_writeopcount + 2) * 13392 fs->fs_contigsumsize; 13393 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE, 0)) 13394 needed += fragstoblks(fs, 13395 roundup((fs->fs_dsize * fs->fs_minfree / 100) - 13396 fs->fs_cstotal.cs_nffree, fs->fs_frag)); 13397 } else { 13398 UFS_LOCK(ump); 13399 printf("softdep_request_cleanup: Unknown resource type %d\n", 13400 resource); 13401 return (0); 13402 } 13403 starttime = time_second; 13404 retry: 13405 if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 && 13406 fs->fs_cstotal.cs_nbfree <= needed) || 13407 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13408 fs->fs_cstotal.cs_nifree <= needed)) { 13409 ACQUIRE_LOCK(ump); 13410 if (ump->softdep_on_worklist > 0 && 13411 process_worklist_item(UFSTOVFS(ump), 13412 ump->softdep_on_worklist, LK_NOWAIT) != 0) 13413 stat_worklist_push += 1; 13414 FREE_LOCK(ump); 13415 } 13416 /* 13417 * If we still need resources and there are no more worklist 13418 * entries to process to obtain them, we have to start flushing 13419 * the dirty vnodes to force the release of additional requests 13420 * to the worklist that we can then process to reap addition 13421 * resources. We walk the vnodes associated with the mount point 13422 * until we get the needed worklist requests that we can reap. 13423 * 13424 * If there are several threads all needing to clean the same 13425 * mount point, only one is allowed to walk the mount list. 13426 * When several threads all try to walk the same mount list, 13427 * they end up competing with each other and often end up in 13428 * livelock. This approach ensures that forward progress is 13429 * made at the cost of occational ENOSPC errors being returned 13430 * that might otherwise have been avoided. 13431 */ 13432 error = 1; 13433 if ((resource == FLUSH_BLOCKS_WAIT && 13434 fs->fs_cstotal.cs_nbfree <= needed) || 13435 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13436 fs->fs_cstotal.cs_nifree <= needed)) { 13437 ACQUIRE_LOCK(ump); 13438 if ((ump->um_softdep->sd_flags & FLUSH_RC_ACTIVE) == 0) { 13439 ump->um_softdep->sd_flags |= FLUSH_RC_ACTIVE; 13440 FREE_LOCK(ump); 13441 failed_vnode = softdep_request_cleanup_flush(mp, ump); 13442 ACQUIRE_LOCK(ump); 13443 ump->um_softdep->sd_flags &= ~FLUSH_RC_ACTIVE; 13444 FREE_LOCK(ump); 13445 if (ump->softdep_on_worklist > 0) { 13446 stat_cleanup_retries += 1; 13447 if (!failed_vnode) 13448 goto retry; 13449 } 13450 } else { 13451 FREE_LOCK(ump); 13452 error = 0; 13453 } 13454 stat_cleanup_failures += 1; 13455 } 13456 if (time_second - starttime > stat_cleanup_high_delay) 13457 stat_cleanup_high_delay = time_second - starttime; 13458 UFS_LOCK(ump); 13459 return (error); 13460 } 13461 13462 /* 13463 * Scan the vnodes for the specified mount point flushing out any 13464 * vnodes that can be locked without waiting. Finally, try to flush 13465 * the device associated with the mount point if it can be locked 13466 * without waiting. 13467 * 13468 * We return 0 if we were able to lock every vnode in our scan. 13469 * If we had to skip one or more vnodes, we return 1. 13470 */ 13471 static int 13472 softdep_request_cleanup_flush(mp, ump) 13473 struct mount *mp; 13474 struct ufsmount *ump; 13475 { 13476 struct thread *td; 13477 struct vnode *lvp, *mvp; 13478 int failed_vnode; 13479 13480 failed_vnode = 0; 13481 td = curthread; 13482 MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) { 13483 if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) { 13484 VI_UNLOCK(lvp); 13485 continue; 13486 } 13487 if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT, 13488 td) != 0) { 13489 failed_vnode = 1; 13490 continue; 13491 } 13492 if (lvp->v_vflag & VV_NOSYNC) { /* unlinked */ 13493 vput(lvp); 13494 continue; 13495 } 13496 (void) ffs_syncvnode(lvp, MNT_NOWAIT, 0); 13497 vput(lvp); 13498 } 13499 lvp = ump->um_devvp; 13500 if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) { 13501 VOP_FSYNC(lvp, MNT_NOWAIT, td); 13502 VOP_UNLOCK(lvp, 0); 13503 } 13504 return (failed_vnode); 13505 } 13506 13507 static bool 13508 softdep_excess_items(struct ufsmount *ump, int item) 13509 { 13510 13511 KASSERT(item >= 0 && item < D_LAST, ("item %d", item)); 13512 return (dep_current[item] > max_softdeps && 13513 ump->softdep_curdeps[item] > max_softdeps / 13514 stat_flush_threads); 13515 } 13516 13517 static void 13518 schedule_cleanup(struct mount *mp) 13519 { 13520 struct ufsmount *ump; 13521 struct thread *td; 13522 13523 ump = VFSTOUFS(mp); 13524 LOCK_OWNED(ump); 13525 FREE_LOCK(ump); 13526 td = curthread; 13527 if ((td->td_pflags & TDP_KTHREAD) != 0 && 13528 (td->td_proc->p_flag2 & P2_AST_SU) == 0) { 13529 /* 13530 * No ast is delivered to kernel threads, so nobody 13531 * would deref the mp. Some kernel threads 13532 * explicitely check for AST, e.g. NFS daemon does 13533 * this in the serving loop. 13534 */ 13535 return; 13536 } 13537 if (td->td_su != NULL) 13538 vfs_rel(td->td_su); 13539 vfs_ref(mp); 13540 td->td_su = mp; 13541 thread_lock(td); 13542 td->td_flags |= TDF_ASTPENDING; 13543 thread_unlock(td); 13544 } 13545 13546 static void 13547 softdep_ast_cleanup_proc(struct thread *td) 13548 { 13549 struct mount *mp; 13550 struct ufsmount *ump; 13551 int error; 13552 bool req; 13553 13554 while ((mp = td->td_su) != NULL) { 13555 td->td_su = NULL; 13556 error = vfs_busy(mp, MBF_NOWAIT); 13557 vfs_rel(mp); 13558 if (error != 0) 13559 return; 13560 if (ffs_own_mount(mp) && MOUNTEDSOFTDEP(mp)) { 13561 ump = VFSTOUFS(mp); 13562 for (;;) { 13563 req = false; 13564 ACQUIRE_LOCK(ump); 13565 if (softdep_excess_items(ump, D_INODEDEP)) { 13566 req = true; 13567 request_cleanup(mp, FLUSH_INODES); 13568 } 13569 if (softdep_excess_items(ump, D_DIRREM)) { 13570 req = true; 13571 request_cleanup(mp, FLUSH_BLOCKS); 13572 } 13573 FREE_LOCK(ump); 13574 if (softdep_excess_items(ump, D_NEWBLK) || 13575 softdep_excess_items(ump, D_ALLOCDIRECT) || 13576 softdep_excess_items(ump, D_ALLOCINDIR)) { 13577 error = vn_start_write(NULL, &mp, 13578 V_WAIT); 13579 if (error == 0) { 13580 req = true; 13581 VFS_SYNC(mp, MNT_WAIT); 13582 vn_finished_write(mp); 13583 } 13584 } 13585 if ((td->td_pflags & TDP_KTHREAD) != 0 || !req) 13586 break; 13587 } 13588 } 13589 vfs_unbusy(mp); 13590 } 13591 if ((mp = td->td_su) != NULL) { 13592 td->td_su = NULL; 13593 vfs_rel(mp); 13594 } 13595 } 13596 13597 /* 13598 * If memory utilization has gotten too high, deliberately slow things 13599 * down and speed up the I/O processing. 13600 */ 13601 static int 13602 request_cleanup(mp, resource) 13603 struct mount *mp; 13604 int resource; 13605 { 13606 struct thread *td = curthread; 13607 struct ufsmount *ump; 13608 13609 ump = VFSTOUFS(mp); 13610 LOCK_OWNED(ump); 13611 /* 13612 * We never hold up the filesystem syncer or buf daemon. 13613 */ 13614 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF)) 13615 return (0); 13616 /* 13617 * First check to see if the work list has gotten backlogged. 13618 * If it has, co-opt this process to help clean up two entries. 13619 * Because this process may hold inodes locked, we cannot 13620 * handle any remove requests that might block on a locked 13621 * inode as that could lead to deadlock. We set TDP_SOFTDEP 13622 * to avoid recursively processing the worklist. 13623 */ 13624 if (ump->softdep_on_worklist > max_softdeps / 10) { 13625 td->td_pflags |= TDP_SOFTDEP; 13626 process_worklist_item(mp, 2, LK_NOWAIT); 13627 td->td_pflags &= ~TDP_SOFTDEP; 13628 stat_worklist_push += 2; 13629 return(1); 13630 } 13631 /* 13632 * Next, we attempt to speed up the syncer process. If that 13633 * is successful, then we allow the process to continue. 13634 */ 13635 if (softdep_speedup(ump) && 13636 resource != FLUSH_BLOCKS_WAIT && 13637 resource != FLUSH_INODES_WAIT) 13638 return(0); 13639 /* 13640 * If we are resource constrained on inode dependencies, try 13641 * flushing some dirty inodes. Otherwise, we are constrained 13642 * by file deletions, so try accelerating flushes of directories 13643 * with removal dependencies. We would like to do the cleanup 13644 * here, but we probably hold an inode locked at this point and 13645 * that might deadlock against one that we try to clean. So, 13646 * the best that we can do is request the syncer daemon to do 13647 * the cleanup for us. 13648 */ 13649 switch (resource) { 13650 13651 case FLUSH_INODES: 13652 case FLUSH_INODES_WAIT: 13653 ACQUIRE_GBLLOCK(&lk); 13654 stat_ino_limit_push += 1; 13655 req_clear_inodedeps += 1; 13656 FREE_GBLLOCK(&lk); 13657 stat_countp = &stat_ino_limit_hit; 13658 break; 13659 13660 case FLUSH_BLOCKS: 13661 case FLUSH_BLOCKS_WAIT: 13662 ACQUIRE_GBLLOCK(&lk); 13663 stat_blk_limit_push += 1; 13664 req_clear_remove += 1; 13665 FREE_GBLLOCK(&lk); 13666 stat_countp = &stat_blk_limit_hit; 13667 break; 13668 13669 default: 13670 panic("request_cleanup: unknown type"); 13671 } 13672 /* 13673 * Hopefully the syncer daemon will catch up and awaken us. 13674 * We wait at most tickdelay before proceeding in any case. 13675 */ 13676 ACQUIRE_GBLLOCK(&lk); 13677 FREE_LOCK(ump); 13678 proc_waiting += 1; 13679 if (callout_pending(&softdep_callout) == FALSE) 13680 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2, 13681 pause_timer, 0); 13682 13683 if ((td->td_pflags & TDP_KTHREAD) == 0) 13684 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0); 13685 proc_waiting -= 1; 13686 FREE_GBLLOCK(&lk); 13687 ACQUIRE_LOCK(ump); 13688 return (1); 13689 } 13690 13691 /* 13692 * Awaken processes pausing in request_cleanup and clear proc_waiting 13693 * to indicate that there is no longer a timer running. Pause_timer 13694 * will be called with the global softdep mutex (&lk) locked. 13695 */ 13696 static void 13697 pause_timer(arg) 13698 void *arg; 13699 { 13700 13701 GBLLOCK_OWNED(&lk); 13702 /* 13703 * The callout_ API has acquired mtx and will hold it around this 13704 * function call. 13705 */ 13706 *stat_countp += proc_waiting; 13707 wakeup(&proc_waiting); 13708 } 13709 13710 /* 13711 * If requested, try removing inode or removal dependencies. 13712 */ 13713 static void 13714 check_clear_deps(mp) 13715 struct mount *mp; 13716 { 13717 13718 /* 13719 * If we are suspended, it may be because of our using 13720 * too many inodedeps, so help clear them out. 13721 */ 13722 if (MOUNTEDSUJ(mp) && VFSTOUFS(mp)->softdep_jblocks->jb_suspended) 13723 clear_inodedeps(mp); 13724 /* 13725 * General requests for cleanup of backed up dependencies 13726 */ 13727 ACQUIRE_GBLLOCK(&lk); 13728 if (req_clear_inodedeps) { 13729 req_clear_inodedeps -= 1; 13730 FREE_GBLLOCK(&lk); 13731 clear_inodedeps(mp); 13732 ACQUIRE_GBLLOCK(&lk); 13733 wakeup(&proc_waiting); 13734 } 13735 if (req_clear_remove) { 13736 req_clear_remove -= 1; 13737 FREE_GBLLOCK(&lk); 13738 clear_remove(mp); 13739 ACQUIRE_GBLLOCK(&lk); 13740 wakeup(&proc_waiting); 13741 } 13742 FREE_GBLLOCK(&lk); 13743 } 13744 13745 /* 13746 * Flush out a directory with at least one removal dependency in an effort to 13747 * reduce the number of dirrem, freefile, and freeblks dependency structures. 13748 */ 13749 static void 13750 clear_remove(mp) 13751 struct mount *mp; 13752 { 13753 struct pagedep_hashhead *pagedephd; 13754 struct pagedep *pagedep; 13755 struct ufsmount *ump; 13756 struct vnode *vp; 13757 struct bufobj *bo; 13758 int error, cnt; 13759 ino_t ino; 13760 13761 ump = VFSTOUFS(mp); 13762 LOCK_OWNED(ump); 13763 13764 for (cnt = 0; cnt <= ump->pagedep_hash_size; cnt++) { 13765 pagedephd = &ump->pagedep_hashtbl[ump->pagedep_nextclean++]; 13766 if (ump->pagedep_nextclean > ump->pagedep_hash_size) 13767 ump->pagedep_nextclean = 0; 13768 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 13769 if (LIST_EMPTY(&pagedep->pd_dirremhd)) 13770 continue; 13771 ino = pagedep->pd_ino; 13772 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 13773 continue; 13774 FREE_LOCK(ump); 13775 13776 /* 13777 * Let unmount clear deps 13778 */ 13779 error = vfs_busy(mp, MBF_NOWAIT); 13780 if (error != 0) 13781 goto finish_write; 13782 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 13783 FFSV_FORCEINSMQ); 13784 vfs_unbusy(mp); 13785 if (error != 0) { 13786 softdep_error("clear_remove: vget", error); 13787 goto finish_write; 13788 } 13789 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 13790 softdep_error("clear_remove: fsync", error); 13791 bo = &vp->v_bufobj; 13792 BO_LOCK(bo); 13793 drain_output(vp); 13794 BO_UNLOCK(bo); 13795 vput(vp); 13796 finish_write: 13797 vn_finished_write(mp); 13798 ACQUIRE_LOCK(ump); 13799 return; 13800 } 13801 } 13802 } 13803 13804 /* 13805 * Clear out a block of dirty inodes in an effort to reduce 13806 * the number of inodedep dependency structures. 13807 */ 13808 static void 13809 clear_inodedeps(mp) 13810 struct mount *mp; 13811 { 13812 struct inodedep_hashhead *inodedephd; 13813 struct inodedep *inodedep; 13814 struct ufsmount *ump; 13815 struct vnode *vp; 13816 struct fs *fs; 13817 int error, cnt; 13818 ino_t firstino, lastino, ino; 13819 13820 ump = VFSTOUFS(mp); 13821 fs = ump->um_fs; 13822 LOCK_OWNED(ump); 13823 /* 13824 * Pick a random inode dependency to be cleared. 13825 * We will then gather up all the inodes in its block 13826 * that have dependencies and flush them out. 13827 */ 13828 for (cnt = 0; cnt <= ump->inodedep_hash_size; cnt++) { 13829 inodedephd = &ump->inodedep_hashtbl[ump->inodedep_nextclean++]; 13830 if (ump->inodedep_nextclean > ump->inodedep_hash_size) 13831 ump->inodedep_nextclean = 0; 13832 if ((inodedep = LIST_FIRST(inodedephd)) != NULL) 13833 break; 13834 } 13835 if (inodedep == NULL) 13836 return; 13837 /* 13838 * Find the last inode in the block with dependencies. 13839 */ 13840 firstino = rounddown2(inodedep->id_ino, INOPB(fs)); 13841 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--) 13842 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0) 13843 break; 13844 /* 13845 * Asynchronously push all but the last inode with dependencies. 13846 * Synchronously push the last inode with dependencies to ensure 13847 * that the inode block gets written to free up the inodedeps. 13848 */ 13849 for (ino = firstino; ino <= lastino; ino++) { 13850 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 13851 continue; 13852 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 13853 continue; 13854 FREE_LOCK(ump); 13855 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */ 13856 if (error != 0) { 13857 vn_finished_write(mp); 13858 ACQUIRE_LOCK(ump); 13859 return; 13860 } 13861 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 13862 FFSV_FORCEINSMQ)) != 0) { 13863 softdep_error("clear_inodedeps: vget", error); 13864 vfs_unbusy(mp); 13865 vn_finished_write(mp); 13866 ACQUIRE_LOCK(ump); 13867 return; 13868 } 13869 vfs_unbusy(mp); 13870 if (ino == lastino) { 13871 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0))) 13872 softdep_error("clear_inodedeps: fsync1", error); 13873 } else { 13874 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 13875 softdep_error("clear_inodedeps: fsync2", error); 13876 BO_LOCK(&vp->v_bufobj); 13877 drain_output(vp); 13878 BO_UNLOCK(&vp->v_bufobj); 13879 } 13880 vput(vp); 13881 vn_finished_write(mp); 13882 ACQUIRE_LOCK(ump); 13883 } 13884 } 13885 13886 void 13887 softdep_buf_append(bp, wkhd) 13888 struct buf *bp; 13889 struct workhead *wkhd; 13890 { 13891 struct worklist *wk; 13892 struct ufsmount *ump; 13893 13894 if ((wk = LIST_FIRST(wkhd)) == NULL) 13895 return; 13896 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 13897 ("softdep_buf_append called on non-softdep filesystem")); 13898 ump = VFSTOUFS(wk->wk_mp); 13899 ACQUIRE_LOCK(ump); 13900 while ((wk = LIST_FIRST(wkhd)) != NULL) { 13901 WORKLIST_REMOVE(wk); 13902 WORKLIST_INSERT(&bp->b_dep, wk); 13903 } 13904 FREE_LOCK(ump); 13905 13906 } 13907 13908 void 13909 softdep_inode_append(ip, cred, wkhd) 13910 struct inode *ip; 13911 struct ucred *cred; 13912 struct workhead *wkhd; 13913 { 13914 struct buf *bp; 13915 struct fs *fs; 13916 struct ufsmount *ump; 13917 int error; 13918 13919 ump = ITOUMP(ip); 13920 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 13921 ("softdep_inode_append called on non-softdep filesystem")); 13922 fs = ump->um_fs; 13923 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 13924 (int)fs->fs_bsize, cred, &bp); 13925 if (error) { 13926 bqrelse(bp); 13927 softdep_freework(wkhd); 13928 return; 13929 } 13930 softdep_buf_append(bp, wkhd); 13931 bqrelse(bp); 13932 } 13933 13934 void 13935 softdep_freework(wkhd) 13936 struct workhead *wkhd; 13937 { 13938 struct worklist *wk; 13939 struct ufsmount *ump; 13940 13941 if ((wk = LIST_FIRST(wkhd)) == NULL) 13942 return; 13943 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 13944 ("softdep_freework called on non-softdep filesystem")); 13945 ump = VFSTOUFS(wk->wk_mp); 13946 ACQUIRE_LOCK(ump); 13947 handle_jwork(wkhd); 13948 FREE_LOCK(ump); 13949 } 13950 13951 static struct ufsmount * 13952 softdep_bp_to_mp(bp) 13953 struct buf *bp; 13954 { 13955 struct mount *mp; 13956 struct vnode *vp; 13957 13958 if (LIST_EMPTY(&bp->b_dep)) 13959 return (NULL); 13960 vp = bp->b_vp; 13961 13962 /* 13963 * The ump mount point is stable after we get a correct 13964 * pointer, since bp is locked and this prevents unmount from 13965 * proceeding. But to get to it, we cannot dereference bp->b_dep 13966 * head wk_mp, because we do not yet own SU ump lock and 13967 * workitem might be freed while dereferenced. 13968 */ 13969 retry: 13970 if (vp->v_type == VCHR) { 13971 VI_LOCK(vp); 13972 mp = vp->v_type == VCHR ? vp->v_rdev->si_mountpt : NULL; 13973 VI_UNLOCK(vp); 13974 if (mp == NULL) 13975 goto retry; 13976 } else if (vp->v_type == VREG || vp->v_type == VDIR || 13977 vp->v_type == VLNK) { 13978 mp = vp->v_mount; 13979 } else { 13980 return (NULL); 13981 } 13982 return (VFSTOUFS(mp)); 13983 } 13984 13985 /* 13986 * Function to determine if the buffer has outstanding dependencies 13987 * that will cause a roll-back if the buffer is written. If wantcount 13988 * is set, return number of dependencies, otherwise just yes or no. 13989 */ 13990 static int 13991 softdep_count_dependencies(bp, wantcount) 13992 struct buf *bp; 13993 int wantcount; 13994 { 13995 struct worklist *wk; 13996 struct ufsmount *ump; 13997 struct bmsafemap *bmsafemap; 13998 struct freework *freework; 13999 struct inodedep *inodedep; 14000 struct indirdep *indirdep; 14001 struct freeblks *freeblks; 14002 struct allocindir *aip; 14003 struct pagedep *pagedep; 14004 struct dirrem *dirrem; 14005 struct newblk *newblk; 14006 struct mkdir *mkdir; 14007 struct diradd *dap; 14008 int i, retval; 14009 14010 ump = softdep_bp_to_mp(bp); 14011 if (ump == NULL) 14012 return (0); 14013 retval = 0; 14014 ACQUIRE_LOCK(ump); 14015 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 14016 switch (wk->wk_type) { 14017 14018 case D_INODEDEP: 14019 inodedep = WK_INODEDEP(wk); 14020 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 14021 /* bitmap allocation dependency */ 14022 retval += 1; 14023 if (!wantcount) 14024 goto out; 14025 } 14026 if (TAILQ_FIRST(&inodedep->id_inoupdt)) { 14027 /* direct block pointer dependency */ 14028 retval += 1; 14029 if (!wantcount) 14030 goto out; 14031 } 14032 if (TAILQ_FIRST(&inodedep->id_extupdt)) { 14033 /* direct block pointer dependency */ 14034 retval += 1; 14035 if (!wantcount) 14036 goto out; 14037 } 14038 if (TAILQ_FIRST(&inodedep->id_inoreflst)) { 14039 /* Add reference dependency. */ 14040 retval += 1; 14041 if (!wantcount) 14042 goto out; 14043 } 14044 continue; 14045 14046 case D_INDIRDEP: 14047 indirdep = WK_INDIRDEP(wk); 14048 14049 TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) { 14050 /* indirect truncation dependency */ 14051 retval += 1; 14052 if (!wantcount) 14053 goto out; 14054 } 14055 14056 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 14057 /* indirect block pointer dependency */ 14058 retval += 1; 14059 if (!wantcount) 14060 goto out; 14061 } 14062 continue; 14063 14064 case D_PAGEDEP: 14065 pagedep = WK_PAGEDEP(wk); 14066 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) { 14067 if (LIST_FIRST(&dirrem->dm_jremrefhd)) { 14068 /* Journal remove ref dependency. */ 14069 retval += 1; 14070 if (!wantcount) 14071 goto out; 14072 } 14073 } 14074 for (i = 0; i < DAHASHSZ; i++) { 14075 14076 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 14077 /* directory entry dependency */ 14078 retval += 1; 14079 if (!wantcount) 14080 goto out; 14081 } 14082 } 14083 continue; 14084 14085 case D_BMSAFEMAP: 14086 bmsafemap = WK_BMSAFEMAP(wk); 14087 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) { 14088 /* Add reference dependency. */ 14089 retval += 1; 14090 if (!wantcount) 14091 goto out; 14092 } 14093 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) { 14094 /* Allocate block dependency. */ 14095 retval += 1; 14096 if (!wantcount) 14097 goto out; 14098 } 14099 continue; 14100 14101 case D_FREEBLKS: 14102 freeblks = WK_FREEBLKS(wk); 14103 if (LIST_FIRST(&freeblks->fb_jblkdephd)) { 14104 /* Freeblk journal dependency. */ 14105 retval += 1; 14106 if (!wantcount) 14107 goto out; 14108 } 14109 continue; 14110 14111 case D_ALLOCDIRECT: 14112 case D_ALLOCINDIR: 14113 newblk = WK_NEWBLK(wk); 14114 if (newblk->nb_jnewblk) { 14115 /* Journal allocate dependency. */ 14116 retval += 1; 14117 if (!wantcount) 14118 goto out; 14119 } 14120 continue; 14121 14122 case D_MKDIR: 14123 mkdir = WK_MKDIR(wk); 14124 if (mkdir->md_jaddref) { 14125 /* Journal reference dependency. */ 14126 retval += 1; 14127 if (!wantcount) 14128 goto out; 14129 } 14130 continue; 14131 14132 case D_FREEWORK: 14133 case D_FREEDEP: 14134 case D_JSEGDEP: 14135 case D_JSEG: 14136 case D_SBDEP: 14137 /* never a dependency on these blocks */ 14138 continue; 14139 14140 default: 14141 panic("softdep_count_dependencies: Unexpected type %s", 14142 TYPENAME(wk->wk_type)); 14143 /* NOTREACHED */ 14144 } 14145 } 14146 out: 14147 FREE_LOCK(ump); 14148 return (retval); 14149 } 14150 14151 /* 14152 * Acquire exclusive access to a buffer. 14153 * Must be called with a locked mtx parameter. 14154 * Return acquired buffer or NULL on failure. 14155 */ 14156 static struct buf * 14157 getdirtybuf(bp, lock, waitfor) 14158 struct buf *bp; 14159 struct rwlock *lock; 14160 int waitfor; 14161 { 14162 int error; 14163 14164 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) { 14165 if (waitfor != MNT_WAIT) 14166 return (NULL); 14167 error = BUF_LOCK(bp, 14168 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock); 14169 /* 14170 * Even if we successfully acquire bp here, we have dropped 14171 * lock, which may violates our guarantee. 14172 */ 14173 if (error == 0) 14174 BUF_UNLOCK(bp); 14175 else if (error != ENOLCK) 14176 panic("getdirtybuf: inconsistent lock: %d", error); 14177 rw_wlock(lock); 14178 return (NULL); 14179 } 14180 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 14181 if (lock != BO_LOCKPTR(bp->b_bufobj) && waitfor == MNT_WAIT) { 14182 rw_wunlock(lock); 14183 BO_LOCK(bp->b_bufobj); 14184 BUF_UNLOCK(bp); 14185 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 14186 bp->b_vflags |= BV_BKGRDWAIT; 14187 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), 14188 PRIBIO | PDROP, "getbuf", 0); 14189 } else 14190 BO_UNLOCK(bp->b_bufobj); 14191 rw_wlock(lock); 14192 return (NULL); 14193 } 14194 BUF_UNLOCK(bp); 14195 if (waitfor != MNT_WAIT) 14196 return (NULL); 14197 #ifdef DEBUG_VFS_LOCKS 14198 if (bp->b_vp->v_type != VCHR) 14199 ASSERT_BO_WLOCKED(bp->b_bufobj); 14200 #endif 14201 bp->b_vflags |= BV_BKGRDWAIT; 14202 rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0); 14203 return (NULL); 14204 } 14205 if ((bp->b_flags & B_DELWRI) == 0) { 14206 BUF_UNLOCK(bp); 14207 return (NULL); 14208 } 14209 bremfree(bp); 14210 return (bp); 14211 } 14212 14213 14214 /* 14215 * Check if it is safe to suspend the file system now. On entry, 14216 * the vnode interlock for devvp should be held. Return 0 with 14217 * the mount interlock held if the file system can be suspended now, 14218 * otherwise return EAGAIN with the mount interlock held. 14219 */ 14220 int 14221 softdep_check_suspend(struct mount *mp, 14222 struct vnode *devvp, 14223 int softdep_depcnt, 14224 int softdep_accdepcnt, 14225 int secondary_writes, 14226 int secondary_accwrites) 14227 { 14228 struct bufobj *bo; 14229 struct ufsmount *ump; 14230 struct inodedep *inodedep; 14231 int error, unlinked; 14232 14233 bo = &devvp->v_bufobj; 14234 ASSERT_BO_WLOCKED(bo); 14235 14236 /* 14237 * If we are not running with soft updates, then we need only 14238 * deal with secondary writes as we try to suspend. 14239 */ 14240 if (MOUNTEDSOFTDEP(mp) == 0) { 14241 MNT_ILOCK(mp); 14242 while (mp->mnt_secondary_writes != 0) { 14243 BO_UNLOCK(bo); 14244 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 14245 (PUSER - 1) | PDROP, "secwr", 0); 14246 BO_LOCK(bo); 14247 MNT_ILOCK(mp); 14248 } 14249 14250 /* 14251 * Reasons for needing more work before suspend: 14252 * - Dirty buffers on devvp. 14253 * - Secondary writes occurred after start of vnode sync loop 14254 */ 14255 error = 0; 14256 if (bo->bo_numoutput > 0 || 14257 bo->bo_dirty.bv_cnt > 0 || 14258 secondary_writes != 0 || 14259 mp->mnt_secondary_writes != 0 || 14260 secondary_accwrites != mp->mnt_secondary_accwrites) 14261 error = EAGAIN; 14262 BO_UNLOCK(bo); 14263 return (error); 14264 } 14265 14266 /* 14267 * If we are running with soft updates, then we need to coordinate 14268 * with them as we try to suspend. 14269 */ 14270 ump = VFSTOUFS(mp); 14271 for (;;) { 14272 if (!TRY_ACQUIRE_LOCK(ump)) { 14273 BO_UNLOCK(bo); 14274 ACQUIRE_LOCK(ump); 14275 FREE_LOCK(ump); 14276 BO_LOCK(bo); 14277 continue; 14278 } 14279 MNT_ILOCK(mp); 14280 if (mp->mnt_secondary_writes != 0) { 14281 FREE_LOCK(ump); 14282 BO_UNLOCK(bo); 14283 msleep(&mp->mnt_secondary_writes, 14284 MNT_MTX(mp), 14285 (PUSER - 1) | PDROP, "secwr", 0); 14286 BO_LOCK(bo); 14287 continue; 14288 } 14289 break; 14290 } 14291 14292 unlinked = 0; 14293 if (MOUNTEDSUJ(mp)) { 14294 for (inodedep = TAILQ_FIRST(&ump->softdep_unlinked); 14295 inodedep != NULL; 14296 inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 14297 if ((inodedep->id_state & (UNLINKED | UNLINKLINKS | 14298 UNLINKONLIST)) != (UNLINKED | UNLINKLINKS | 14299 UNLINKONLIST) || 14300 !check_inodedep_free(inodedep)) 14301 continue; 14302 unlinked++; 14303 } 14304 } 14305 14306 /* 14307 * Reasons for needing more work before suspend: 14308 * - Dirty buffers on devvp. 14309 * - Softdep activity occurred after start of vnode sync loop 14310 * - Secondary writes occurred after start of vnode sync loop 14311 */ 14312 error = 0; 14313 if (bo->bo_numoutput > 0 || 14314 bo->bo_dirty.bv_cnt > 0 || 14315 softdep_depcnt != unlinked || 14316 ump->softdep_deps != unlinked || 14317 softdep_accdepcnt != ump->softdep_accdeps || 14318 secondary_writes != 0 || 14319 mp->mnt_secondary_writes != 0 || 14320 secondary_accwrites != mp->mnt_secondary_accwrites) 14321 error = EAGAIN; 14322 FREE_LOCK(ump); 14323 BO_UNLOCK(bo); 14324 return (error); 14325 } 14326 14327 14328 /* 14329 * Get the number of dependency structures for the file system, both 14330 * the current number and the total number allocated. These will 14331 * later be used to detect that softdep processing has occurred. 14332 */ 14333 void 14334 softdep_get_depcounts(struct mount *mp, 14335 int *softdep_depsp, 14336 int *softdep_accdepsp) 14337 { 14338 struct ufsmount *ump; 14339 14340 if (MOUNTEDSOFTDEP(mp) == 0) { 14341 *softdep_depsp = 0; 14342 *softdep_accdepsp = 0; 14343 return; 14344 } 14345 ump = VFSTOUFS(mp); 14346 ACQUIRE_LOCK(ump); 14347 *softdep_depsp = ump->softdep_deps; 14348 *softdep_accdepsp = ump->softdep_accdeps; 14349 FREE_LOCK(ump); 14350 } 14351 14352 /* 14353 * Wait for pending output on a vnode to complete. 14354 */ 14355 static void 14356 drain_output(vp) 14357 struct vnode *vp; 14358 { 14359 14360 ASSERT_VOP_LOCKED(vp, "drain_output"); 14361 (void)bufobj_wwait(&vp->v_bufobj, 0, 0); 14362 } 14363 14364 /* 14365 * Called whenever a buffer that is being invalidated or reallocated 14366 * contains dependencies. This should only happen if an I/O error has 14367 * occurred. The routine is called with the buffer locked. 14368 */ 14369 static void 14370 softdep_deallocate_dependencies(bp) 14371 struct buf *bp; 14372 { 14373 14374 if ((bp->b_ioflags & BIO_ERROR) == 0) 14375 panic("softdep_deallocate_dependencies: dangling deps"); 14376 if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL) 14377 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error); 14378 else 14379 printf("softdep_deallocate_dependencies: " 14380 "got error %d while accessing filesystem\n", bp->b_error); 14381 if (bp->b_error != ENXIO) 14382 panic("softdep_deallocate_dependencies: unrecovered I/O error"); 14383 } 14384 14385 /* 14386 * Function to handle asynchronous write errors in the filesystem. 14387 */ 14388 static void 14389 softdep_error(func, error) 14390 char *func; 14391 int error; 14392 { 14393 14394 /* XXX should do something better! */ 14395 printf("%s: got error %d while accessing filesystem\n", func, error); 14396 } 14397 14398 #ifdef DDB 14399 14400 static void 14401 inodedep_print(struct inodedep *inodedep, int verbose) 14402 { 14403 db_printf("%p fs %p st %x ino %jd inoblk %jd delta %jd nlink %jd" 14404 " saveino %p\n", 14405 inodedep, inodedep->id_fs, inodedep->id_state, 14406 (intmax_t)inodedep->id_ino, 14407 (intmax_t)fsbtodb(inodedep->id_fs, 14408 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)), 14409 (intmax_t)inodedep->id_nlinkdelta, 14410 (intmax_t)inodedep->id_savednlink, 14411 inodedep->id_savedino1); 14412 14413 if (verbose == 0) 14414 return; 14415 14416 db_printf("\tpendinghd %p, bufwait %p, inowait %p, inoreflst %p, " 14417 "mkdiradd %p\n", 14418 LIST_FIRST(&inodedep->id_pendinghd), 14419 LIST_FIRST(&inodedep->id_bufwait), 14420 LIST_FIRST(&inodedep->id_inowait), 14421 TAILQ_FIRST(&inodedep->id_inoreflst), 14422 inodedep->id_mkdiradd); 14423 db_printf("\tinoupdt %p, newinoupdt %p, extupdt %p, newextupdt %p\n", 14424 TAILQ_FIRST(&inodedep->id_inoupdt), 14425 TAILQ_FIRST(&inodedep->id_newinoupdt), 14426 TAILQ_FIRST(&inodedep->id_extupdt), 14427 TAILQ_FIRST(&inodedep->id_newextupdt)); 14428 } 14429 14430 DB_SHOW_COMMAND(inodedep, db_show_inodedep) 14431 { 14432 14433 if (have_addr == 0) { 14434 db_printf("Address required\n"); 14435 return; 14436 } 14437 inodedep_print((struct inodedep*)addr, 1); 14438 } 14439 14440 DB_SHOW_COMMAND(inodedeps, db_show_inodedeps) 14441 { 14442 struct inodedep_hashhead *inodedephd; 14443 struct inodedep *inodedep; 14444 struct ufsmount *ump; 14445 int cnt; 14446 14447 if (have_addr == 0) { 14448 db_printf("Address required\n"); 14449 return; 14450 } 14451 ump = (struct ufsmount *)addr; 14452 for (cnt = 0; cnt < ump->inodedep_hash_size; cnt++) { 14453 inodedephd = &ump->inodedep_hashtbl[cnt]; 14454 LIST_FOREACH(inodedep, inodedephd, id_hash) { 14455 inodedep_print(inodedep, 0); 14456 } 14457 } 14458 } 14459 14460 DB_SHOW_COMMAND(worklist, db_show_worklist) 14461 { 14462 struct worklist *wk; 14463 14464 if (have_addr == 0) { 14465 db_printf("Address required\n"); 14466 return; 14467 } 14468 wk = (struct worklist *)addr; 14469 printf("worklist: %p type %s state 0x%X\n", 14470 wk, TYPENAME(wk->wk_type), wk->wk_state); 14471 } 14472 14473 DB_SHOW_COMMAND(workhead, db_show_workhead) 14474 { 14475 struct workhead *wkhd; 14476 struct worklist *wk; 14477 int i; 14478 14479 if (have_addr == 0) { 14480 db_printf("Address required\n"); 14481 return; 14482 } 14483 wkhd = (struct workhead *)addr; 14484 wk = LIST_FIRST(wkhd); 14485 for (i = 0; i < 100 && wk != NULL; i++, wk = LIST_NEXT(wk, wk_list)) 14486 db_printf("worklist: %p type %s state 0x%X", 14487 wk, TYPENAME(wk->wk_type), wk->wk_state); 14488 if (i == 100) 14489 db_printf("workhead overflow"); 14490 printf("\n"); 14491 } 14492 14493 14494 DB_SHOW_COMMAND(mkdirs, db_show_mkdirs) 14495 { 14496 struct mkdirlist *mkdirlisthd; 14497 struct jaddref *jaddref; 14498 struct diradd *diradd; 14499 struct mkdir *mkdir; 14500 14501 if (have_addr == 0) { 14502 db_printf("Address required\n"); 14503 return; 14504 } 14505 mkdirlisthd = (struct mkdirlist *)addr; 14506 LIST_FOREACH(mkdir, mkdirlisthd, md_mkdirs) { 14507 diradd = mkdir->md_diradd; 14508 db_printf("mkdir: %p state 0x%X dap %p state 0x%X", 14509 mkdir, mkdir->md_state, diradd, diradd->da_state); 14510 if ((jaddref = mkdir->md_jaddref) != NULL) 14511 db_printf(" jaddref %p jaddref state 0x%X", 14512 jaddref, jaddref->ja_state); 14513 db_printf("\n"); 14514 } 14515 } 14516 14517 /* exported to ffs_vfsops.c */ 14518 extern void db_print_ffs(struct ufsmount *ump); 14519 void 14520 db_print_ffs(struct ufsmount *ump) 14521 { 14522 db_printf("mp %p %s devvp %p fs %p su_wl %d su_deps %d su_req %d\n", 14523 ump->um_mountp, ump->um_mountp->mnt_stat.f_mntonname, 14524 ump->um_devvp, ump->um_fs, ump->softdep_on_worklist, 14525 ump->softdep_deps, ump->softdep_req); 14526 } 14527 14528 #endif /* DDB */ 14529 14530 #endif /* SOFTUPDATES */ 14531