1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright 1998, 2000 Marshall Kirk McKusick. 5 * Copyright 2009, 2010 Jeffrey W. Roberson <jeff@FreeBSD.org> 6 * All rights reserved. 7 * 8 * The soft updates code is derived from the appendix of a University 9 * of Michigan technical report (Gregory R. Ganger and Yale N. Patt, 10 * "Soft Updates: A Solution to the Metadata Update Problem in File 11 * Systems", CSE-TR-254-95, August 1995). 12 * 13 * Further information about soft updates can be obtained from: 14 * 15 * Marshall Kirk McKusick http://www.mckusick.com/softdep/ 16 * 1614 Oxford Street mckusick@mckusick.com 17 * Berkeley, CA 94709-1608 +1-510-843-9542 18 * USA 19 * 20 * Redistribution and use in source and binary forms, with or without 21 * modification, are permitted provided that the following conditions 22 * are met: 23 * 24 * 1. Redistributions of source code must retain the above copyright 25 * notice, this list of conditions and the following disclaimer. 26 * 2. Redistributions in binary form must reproduce the above copyright 27 * notice, this list of conditions and the following disclaimer in the 28 * documentation and/or other materials provided with the distribution. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 33 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 35 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 36 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 37 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 38 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 39 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * from: @(#)ffs_softdep.c 9.59 (McKusick) 6/21/00 42 */ 43 44 #include <sys/cdefs.h> 45 __FBSDID("$FreeBSD$"); 46 47 #include "opt_ffs.h" 48 #include "opt_quota.h" 49 #include "opt_ddb.h" 50 51 #include <sys/param.h> 52 #include <sys/kernel.h> 53 #include <sys/systm.h> 54 #include <sys/bio.h> 55 #include <sys/buf.h> 56 #include <sys/kdb.h> 57 #include <sys/kthread.h> 58 #include <sys/ktr.h> 59 #include <sys/limits.h> 60 #include <sys/lock.h> 61 #include <sys/malloc.h> 62 #include <sys/mount.h> 63 #include <sys/mutex.h> 64 #include <sys/namei.h> 65 #include <sys/priv.h> 66 #include <sys/proc.h> 67 #include <sys/racct.h> 68 #include <sys/rwlock.h> 69 #include <sys/stat.h> 70 #include <sys/sysctl.h> 71 #include <sys/syslog.h> 72 #include <sys/vnode.h> 73 #include <sys/conf.h> 74 75 #include <ufs/ufs/dir.h> 76 #include <ufs/ufs/extattr.h> 77 #include <ufs/ufs/quota.h> 78 #include <ufs/ufs/inode.h> 79 #include <ufs/ufs/ufsmount.h> 80 #include <ufs/ffs/fs.h> 81 #include <ufs/ffs/softdep.h> 82 #include <ufs/ffs/ffs_extern.h> 83 #include <ufs/ufs/ufs_extern.h> 84 85 #include <vm/vm.h> 86 #include <vm/vm_extern.h> 87 #include <vm/vm_object.h> 88 89 #include <geom/geom.h> 90 #include <geom/geom_vfs.h> 91 92 #include <ddb/ddb.h> 93 94 #define KTR_SUJ 0 /* Define to KTR_SPARE. */ 95 96 #ifndef SOFTUPDATES 97 98 int 99 softdep_flushfiles(struct mount *oldmnt, 100 int flags, 101 struct thread *td) 102 { 103 104 panic("softdep_flushfiles called"); 105 } 106 107 int 108 softdep_mount(struct vnode *devvp, 109 struct mount *mp, 110 struct fs *fs, 111 struct ucred *cred) 112 { 113 114 return (0); 115 } 116 117 void 118 softdep_initialize(void) 119 { 120 121 return; 122 } 123 124 void 125 softdep_uninitialize(void) 126 { 127 128 return; 129 } 130 131 void 132 softdep_unmount(struct mount *mp) 133 { 134 135 panic("softdep_unmount called"); 136 } 137 138 void 139 softdep_setup_sbupdate(struct ufsmount *ump, 140 struct fs *fs, 141 struct buf *bp) 142 { 143 144 panic("softdep_setup_sbupdate called"); 145 } 146 147 void 148 softdep_setup_inomapdep(struct buf *bp, 149 struct inode *ip, 150 ino_t newinum, 151 int mode) 152 { 153 154 panic("softdep_setup_inomapdep called"); 155 } 156 157 void 158 softdep_setup_blkmapdep(struct buf *bp, 159 struct mount *mp, 160 ufs2_daddr_t newblkno, 161 int frags, 162 int oldfrags) 163 { 164 165 panic("softdep_setup_blkmapdep called"); 166 } 167 168 void 169 softdep_setup_allocdirect(struct inode *ip, 170 ufs_lbn_t lbn, 171 ufs2_daddr_t newblkno, 172 ufs2_daddr_t oldblkno, 173 long newsize, 174 long oldsize, 175 struct buf *bp) 176 { 177 178 panic("softdep_setup_allocdirect called"); 179 } 180 181 void 182 softdep_setup_allocext(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_allocext called"); 192 } 193 194 void 195 softdep_setup_allocindir_page(struct inode *ip, 196 ufs_lbn_t lbn, 197 struct buf *bp, 198 int ptrno, 199 ufs2_daddr_t newblkno, 200 ufs2_daddr_t oldblkno, 201 struct buf *nbp) 202 { 203 204 panic("softdep_setup_allocindir_page called"); 205 } 206 207 void 208 softdep_setup_allocindir_meta(struct buf *nbp, 209 struct inode *ip, 210 struct buf *bp, 211 int ptrno, 212 ufs2_daddr_t newblkno) 213 { 214 215 panic("softdep_setup_allocindir_meta called"); 216 } 217 218 void 219 softdep_journal_freeblocks(struct inode *ip, 220 struct ucred *cred, 221 off_t length, 222 int flags) 223 { 224 225 panic("softdep_journal_freeblocks called"); 226 } 227 228 void 229 softdep_journal_fsync(struct inode *ip) 230 { 231 232 panic("softdep_journal_fsync called"); 233 } 234 235 void 236 softdep_setup_freeblocks(struct inode *ip, 237 off_t length, 238 int flags) 239 { 240 241 panic("softdep_setup_freeblocks called"); 242 } 243 244 void 245 softdep_freefile(struct vnode *pvp, 246 ino_t ino, 247 int mode) 248 { 249 250 panic("softdep_freefile called"); 251 } 252 253 int 254 softdep_setup_directory_add(struct buf *bp, 255 struct inode *dp, 256 off_t diroffset, 257 ino_t newinum, 258 struct buf *newdirbp, 259 int isnewblk) 260 { 261 262 panic("softdep_setup_directory_add called"); 263 } 264 265 void 266 softdep_change_directoryentry_offset(struct buf *bp, 267 struct inode *dp, 268 caddr_t base, 269 caddr_t oldloc, 270 caddr_t newloc, 271 int entrysize) 272 { 273 274 panic("softdep_change_directoryentry_offset called"); 275 } 276 277 void 278 softdep_setup_remove(struct buf *bp, 279 struct inode *dp, 280 struct inode *ip, 281 int isrmdir) 282 { 283 284 panic("softdep_setup_remove called"); 285 } 286 287 void 288 softdep_setup_directory_change(struct buf *bp, 289 struct inode *dp, 290 struct inode *ip, 291 ino_t newinum, 292 int isrmdir) 293 { 294 295 panic("softdep_setup_directory_change called"); 296 } 297 298 void 299 softdep_setup_blkfree(struct mount *mp, 300 struct buf *bp, 301 ufs2_daddr_t blkno, 302 int frags, 303 struct workhead *wkhd, 304 bool doingrecovery) 305 { 306 307 panic("%s called", __FUNCTION__); 308 } 309 310 void 311 softdep_setup_inofree(struct mount *mp, 312 struct buf *bp, 313 ino_t ino, 314 struct workhead *wkhd, 315 bool doingrecovery) 316 { 317 318 panic("%s called", __FUNCTION__); 319 } 320 321 void 322 softdep_setup_unlink(struct inode *dp, struct inode *ip) 323 { 324 325 panic("%s called", __FUNCTION__); 326 } 327 328 void 329 softdep_setup_link(struct inode *dp, struct inode *ip) 330 { 331 332 panic("%s called", __FUNCTION__); 333 } 334 335 void 336 softdep_revert_link(struct inode *dp, struct inode *ip) 337 { 338 339 panic("%s called", __FUNCTION__); 340 } 341 342 void 343 softdep_setup_rmdir(struct inode *dp, struct inode *ip) 344 { 345 346 panic("%s called", __FUNCTION__); 347 } 348 349 void 350 softdep_revert_rmdir(struct inode *dp, struct inode *ip) 351 { 352 353 panic("%s called", __FUNCTION__); 354 } 355 356 void 357 softdep_setup_create(struct inode *dp, struct inode *ip) 358 { 359 360 panic("%s called", __FUNCTION__); 361 } 362 363 void 364 softdep_revert_create(struct inode *dp, struct inode *ip) 365 { 366 367 panic("%s called", __FUNCTION__); 368 } 369 370 void 371 softdep_setup_mkdir(struct inode *dp, struct inode *ip) 372 { 373 374 panic("%s called", __FUNCTION__); 375 } 376 377 void 378 softdep_revert_mkdir(struct inode *dp, struct inode *ip) 379 { 380 381 panic("%s called", __FUNCTION__); 382 } 383 384 void 385 softdep_setup_dotdot_link(struct inode *dp, struct inode *ip) 386 { 387 388 panic("%s called", __FUNCTION__); 389 } 390 391 int 392 softdep_prealloc(struct vnode *vp, int waitok) 393 { 394 395 panic("%s called", __FUNCTION__); 396 } 397 398 int 399 softdep_journal_lookup(struct mount *mp, struct vnode **vpp) 400 { 401 402 return (ENOENT); 403 } 404 405 void 406 softdep_change_linkcnt(struct inode *ip) 407 { 408 409 panic("softdep_change_linkcnt called"); 410 } 411 412 void 413 softdep_load_inodeblock(struct inode *ip) 414 { 415 416 panic("softdep_load_inodeblock called"); 417 } 418 419 void 420 softdep_update_inodeblock(struct inode *ip, 421 struct buf *bp, 422 int waitfor) 423 { 424 425 panic("softdep_update_inodeblock called"); 426 } 427 428 int 429 softdep_fsync(struct vnode *vp) /* the "in_core" copy of the inode */ 430 { 431 432 return (0); 433 } 434 435 void 436 softdep_fsync_mountdev(struct vnode *vp) 437 { 438 439 return; 440 } 441 442 int 443 softdep_flushworklist(struct mount *oldmnt, 444 int *countp, 445 struct thread *td) 446 { 447 448 *countp = 0; 449 return (0); 450 } 451 452 int 453 softdep_sync_metadata(struct vnode *vp) 454 { 455 456 panic("softdep_sync_metadata called"); 457 } 458 459 int 460 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 461 { 462 463 panic("softdep_sync_buf called"); 464 } 465 466 int 467 softdep_slowdown(struct vnode *vp) 468 { 469 470 panic("softdep_slowdown called"); 471 } 472 473 int 474 softdep_request_cleanup(struct fs *fs, 475 struct vnode *vp, 476 struct ucred *cred, 477 int resource) 478 { 479 480 return (0); 481 } 482 483 int 484 softdep_check_suspend(struct mount *mp, 485 struct vnode *devvp, 486 int softdep_depcnt, 487 int softdep_accdepcnt, 488 int secondary_writes, 489 int secondary_accwrites) 490 { 491 struct bufobj *bo; 492 int error; 493 494 (void) softdep_depcnt, 495 (void) softdep_accdepcnt; 496 497 bo = &devvp->v_bufobj; 498 ASSERT_BO_WLOCKED(bo); 499 500 MNT_ILOCK(mp); 501 while (mp->mnt_secondary_writes != 0) { 502 BO_UNLOCK(bo); 503 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 504 (PUSER - 1) | PDROP, "secwr", 0); 505 BO_LOCK(bo); 506 MNT_ILOCK(mp); 507 } 508 509 /* 510 * Reasons for needing more work before suspend: 511 * - Dirty buffers on devvp. 512 * - Secondary writes occurred after start of vnode sync loop 513 */ 514 error = 0; 515 if (bo->bo_numoutput > 0 || 516 bo->bo_dirty.bv_cnt > 0 || 517 secondary_writes != 0 || 518 mp->mnt_secondary_writes != 0 || 519 secondary_accwrites != mp->mnt_secondary_accwrites) 520 error = EAGAIN; 521 BO_UNLOCK(bo); 522 return (error); 523 } 524 525 void 526 softdep_get_depcounts(struct mount *mp, 527 int *softdepactivep, 528 int *softdepactiveaccp) 529 { 530 (void) mp; 531 *softdepactivep = 0; 532 *softdepactiveaccp = 0; 533 } 534 535 void 536 softdep_buf_append(struct buf *bp, struct workhead *wkhd) 537 { 538 539 panic("softdep_buf_appendwork called"); 540 } 541 542 void 543 softdep_inode_append(struct inode *ip, 544 struct ucred *cred, 545 struct workhead *wkhd) 546 { 547 548 panic("softdep_inode_appendwork called"); 549 } 550 551 void 552 softdep_freework(struct workhead *wkhd) 553 { 554 555 panic("softdep_freework called"); 556 } 557 558 int 559 softdep_prerename(struct vnode *fdvp, 560 struct vnode *fvp, 561 struct vnode *tdvp, 562 struct vnode *tvp) 563 { 564 565 panic("softdep_prerename called"); 566 } 567 568 int 569 softdep_prelink(struct vnode *dvp, 570 struct vnode *vp, 571 struct componentname *cnp) 572 { 573 574 panic("softdep_prelink called"); 575 } 576 577 #else 578 579 FEATURE(softupdates, "FFS soft-updates support"); 580 581 static SYSCTL_NODE(_debug, OID_AUTO, softdep, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 582 "soft updates stats"); 583 static SYSCTL_NODE(_debug_softdep, OID_AUTO, total, 584 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 585 "total dependencies allocated"); 586 static SYSCTL_NODE(_debug_softdep, OID_AUTO, highuse, 587 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 588 "high use dependencies allocated"); 589 static SYSCTL_NODE(_debug_softdep, OID_AUTO, current, 590 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 591 "current dependencies allocated"); 592 static SYSCTL_NODE(_debug_softdep, OID_AUTO, write, 593 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 594 "current dependencies written"); 595 596 unsigned long dep_current[D_LAST + 1]; 597 unsigned long dep_highuse[D_LAST + 1]; 598 unsigned long dep_total[D_LAST + 1]; 599 unsigned long dep_write[D_LAST + 1]; 600 601 #define SOFTDEP_TYPE(type, str, long) \ 602 static MALLOC_DEFINE(M_ ## type, #str, long); \ 603 SYSCTL_ULONG(_debug_softdep_total, OID_AUTO, str, CTLFLAG_RD, \ 604 &dep_total[D_ ## type], 0, ""); \ 605 SYSCTL_ULONG(_debug_softdep_current, OID_AUTO, str, CTLFLAG_RD, \ 606 &dep_current[D_ ## type], 0, ""); \ 607 SYSCTL_ULONG(_debug_softdep_highuse, OID_AUTO, str, CTLFLAG_RD, \ 608 &dep_highuse[D_ ## type], 0, ""); \ 609 SYSCTL_ULONG(_debug_softdep_write, OID_AUTO, str, CTLFLAG_RD, \ 610 &dep_write[D_ ## type], 0, ""); 611 612 SOFTDEP_TYPE(PAGEDEP, pagedep, "File page dependencies"); 613 SOFTDEP_TYPE(INODEDEP, inodedep, "Inode dependencies"); 614 SOFTDEP_TYPE(BMSAFEMAP, bmsafemap, 615 "Block or frag allocated from cyl group map"); 616 SOFTDEP_TYPE(NEWBLK, newblk, "New block or frag allocation dependency"); 617 SOFTDEP_TYPE(ALLOCDIRECT, allocdirect, "Block or frag dependency for an inode"); 618 SOFTDEP_TYPE(INDIRDEP, indirdep, "Indirect block dependencies"); 619 SOFTDEP_TYPE(ALLOCINDIR, allocindir, "Block dependency for an indirect block"); 620 SOFTDEP_TYPE(FREEFRAG, freefrag, "Previously used frag for an inode"); 621 SOFTDEP_TYPE(FREEBLKS, freeblks, "Blocks freed from an inode"); 622 SOFTDEP_TYPE(FREEFILE, freefile, "Inode deallocated"); 623 SOFTDEP_TYPE(DIRADD, diradd, "New directory entry"); 624 SOFTDEP_TYPE(MKDIR, mkdir, "New directory"); 625 SOFTDEP_TYPE(DIRREM, dirrem, "Directory entry deleted"); 626 SOFTDEP_TYPE(NEWDIRBLK, newdirblk, "Unclaimed new directory block"); 627 SOFTDEP_TYPE(FREEWORK, freework, "free an inode block"); 628 SOFTDEP_TYPE(FREEDEP, freedep, "track a block free"); 629 SOFTDEP_TYPE(JADDREF, jaddref, "Journal inode ref add"); 630 SOFTDEP_TYPE(JREMREF, jremref, "Journal inode ref remove"); 631 SOFTDEP_TYPE(JMVREF, jmvref, "Journal inode ref move"); 632 SOFTDEP_TYPE(JNEWBLK, jnewblk, "Journal new block"); 633 SOFTDEP_TYPE(JFREEBLK, jfreeblk, "Journal free block"); 634 SOFTDEP_TYPE(JFREEFRAG, jfreefrag, "Journal free frag"); 635 SOFTDEP_TYPE(JSEG, jseg, "Journal segment"); 636 SOFTDEP_TYPE(JSEGDEP, jsegdep, "Journal segment complete"); 637 SOFTDEP_TYPE(SBDEP, sbdep, "Superblock write dependency"); 638 SOFTDEP_TYPE(JTRUNC, jtrunc, "Journal inode truncation"); 639 SOFTDEP_TYPE(JFSYNC, jfsync, "Journal fsync complete"); 640 641 static MALLOC_DEFINE(M_SENTINEL, "sentinel", "Worklist sentinel"); 642 643 static MALLOC_DEFINE(M_SAVEDINO, "savedino", "Saved inodes"); 644 static MALLOC_DEFINE(M_JBLOCKS, "jblocks", "Journal block locations"); 645 static MALLOC_DEFINE(M_MOUNTDATA, "softdep", "Softdep per-mount data"); 646 647 #define M_SOFTDEP_FLAGS (M_WAITOK) 648 649 /* 650 * translate from workitem type to memory type 651 * MUST match the defines above, such that memtype[D_XXX] == M_XXX 652 */ 653 static struct malloc_type *memtype[] = { 654 NULL, 655 M_PAGEDEP, 656 M_INODEDEP, 657 M_BMSAFEMAP, 658 M_NEWBLK, 659 M_ALLOCDIRECT, 660 M_INDIRDEP, 661 M_ALLOCINDIR, 662 M_FREEFRAG, 663 M_FREEBLKS, 664 M_FREEFILE, 665 M_DIRADD, 666 M_MKDIR, 667 M_DIRREM, 668 M_NEWDIRBLK, 669 M_FREEWORK, 670 M_FREEDEP, 671 M_JADDREF, 672 M_JREMREF, 673 M_JMVREF, 674 M_JNEWBLK, 675 M_JFREEBLK, 676 M_JFREEFRAG, 677 M_JSEG, 678 M_JSEGDEP, 679 M_SBDEP, 680 M_JTRUNC, 681 M_JFSYNC, 682 M_SENTINEL 683 }; 684 685 #define DtoM(type) (memtype[type]) 686 687 /* 688 * Names of malloc types. 689 */ 690 #define TYPENAME(type) \ 691 ((unsigned)(type) <= D_LAST && (unsigned)(type) >= D_FIRST ? \ 692 memtype[type]->ks_shortdesc : "???") 693 /* 694 * End system adaptation definitions. 695 */ 696 697 #define DOTDOT_OFFSET offsetof(struct dirtemplate, dotdot_ino) 698 #define DOT_OFFSET offsetof(struct dirtemplate, dot_ino) 699 700 /* 701 * Internal function prototypes. 702 */ 703 static void check_clear_deps(struct mount *); 704 static void softdep_error(char *, int); 705 static int softdep_prerename_vnode(struct ufsmount *, struct vnode *); 706 static int softdep_process_worklist(struct mount *, int); 707 static int softdep_waitidle(struct mount *, int); 708 static void drain_output(struct vnode *); 709 static struct buf *getdirtybuf(struct buf *, struct rwlock *, int); 710 static int check_inodedep_free(struct inodedep *); 711 static void clear_remove(struct mount *); 712 static void clear_inodedeps(struct mount *); 713 static void unlinked_inodedep(struct mount *, struct inodedep *); 714 static void clear_unlinked_inodedep(struct inodedep *); 715 static struct inodedep *first_unlinked_inodedep(struct ufsmount *); 716 static int flush_pagedep_deps(struct vnode *, struct mount *, 717 struct diraddhd *, struct buf *); 718 static int free_pagedep(struct pagedep *); 719 static int flush_newblk_dep(struct vnode *, struct mount *, ufs_lbn_t); 720 static int flush_inodedep_deps(struct vnode *, struct mount *, ino_t); 721 static int flush_deplist(struct allocdirectlst *, int, int *); 722 static int sync_cgs(struct mount *, int); 723 static int handle_written_filepage(struct pagedep *, struct buf *, int); 724 static int handle_written_sbdep(struct sbdep *, struct buf *); 725 static void initiate_write_sbdep(struct sbdep *); 726 static void diradd_inode_written(struct diradd *, struct inodedep *); 727 static int handle_written_indirdep(struct indirdep *, struct buf *, 728 struct buf**, int); 729 static int handle_written_inodeblock(struct inodedep *, struct buf *, int); 730 static int jnewblk_rollforward(struct jnewblk *, struct fs *, struct cg *, 731 uint8_t *); 732 static int handle_written_bmsafemap(struct bmsafemap *, struct buf *, int); 733 static void handle_written_jaddref(struct jaddref *); 734 static void handle_written_jremref(struct jremref *); 735 static void handle_written_jseg(struct jseg *, struct buf *); 736 static void handle_written_jnewblk(struct jnewblk *); 737 static void handle_written_jblkdep(struct jblkdep *); 738 static void handle_written_jfreefrag(struct jfreefrag *); 739 static void complete_jseg(struct jseg *); 740 static void complete_jsegs(struct jseg *); 741 static void jseg_write(struct ufsmount *ump, struct jseg *, uint8_t *); 742 static void jaddref_write(struct jaddref *, struct jseg *, uint8_t *); 743 static void jremref_write(struct jremref *, struct jseg *, uint8_t *); 744 static void jmvref_write(struct jmvref *, struct jseg *, uint8_t *); 745 static void jtrunc_write(struct jtrunc *, struct jseg *, uint8_t *); 746 static void jfsync_write(struct jfsync *, struct jseg *, uint8_t *data); 747 static void jnewblk_write(struct jnewblk *, struct jseg *, uint8_t *); 748 static void jfreeblk_write(struct jfreeblk *, struct jseg *, uint8_t *); 749 static void jfreefrag_write(struct jfreefrag *, struct jseg *, uint8_t *); 750 static inline void inoref_write(struct inoref *, struct jseg *, 751 struct jrefrec *); 752 static void handle_allocdirect_partdone(struct allocdirect *, 753 struct workhead *); 754 static struct jnewblk *cancel_newblk(struct newblk *, struct worklist *, 755 struct workhead *); 756 static void indirdep_complete(struct indirdep *); 757 static int indirblk_lookup(struct mount *, ufs2_daddr_t); 758 static void indirblk_insert(struct freework *); 759 static void indirblk_remove(struct freework *); 760 static void handle_allocindir_partdone(struct allocindir *); 761 static void initiate_write_filepage(struct pagedep *, struct buf *); 762 static void initiate_write_indirdep(struct indirdep*, struct buf *); 763 static void handle_written_mkdir(struct mkdir *, int); 764 static int jnewblk_rollback(struct jnewblk *, struct fs *, struct cg *, 765 uint8_t *); 766 static void initiate_write_bmsafemap(struct bmsafemap *, struct buf *); 767 static void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *); 768 static void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *); 769 static void handle_workitem_freefile(struct freefile *); 770 static int handle_workitem_remove(struct dirrem *, int); 771 static struct dirrem *newdirrem(struct buf *, struct inode *, 772 struct inode *, int, struct dirrem **); 773 static struct indirdep *indirdep_lookup(struct mount *, struct inode *, 774 struct buf *); 775 static void cancel_indirdep(struct indirdep *, struct buf *, 776 struct freeblks *); 777 static void free_indirdep(struct indirdep *); 778 static void free_diradd(struct diradd *, struct workhead *); 779 static void merge_diradd(struct inodedep *, struct diradd *); 780 static void complete_diradd(struct diradd *); 781 static struct diradd *diradd_lookup(struct pagedep *, int); 782 static struct jremref *cancel_diradd_dotdot(struct inode *, struct dirrem *, 783 struct jremref *); 784 static struct jremref *cancel_mkdir_dotdot(struct inode *, struct dirrem *, 785 struct jremref *); 786 static void cancel_diradd(struct diradd *, struct dirrem *, struct jremref *, 787 struct jremref *, struct jremref *); 788 static void dirrem_journal(struct dirrem *, struct jremref *, struct jremref *, 789 struct jremref *); 790 static void cancel_allocindir(struct allocindir *, struct buf *bp, 791 struct freeblks *, int); 792 static int setup_trunc_indir(struct freeblks *, struct inode *, 793 ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t); 794 static void complete_trunc_indir(struct freework *); 795 static void trunc_indirdep(struct indirdep *, struct freeblks *, struct buf *, 796 int); 797 static void complete_mkdir(struct mkdir *); 798 static void free_newdirblk(struct newdirblk *); 799 static void free_jremref(struct jremref *); 800 static void free_jaddref(struct jaddref *); 801 static void free_jsegdep(struct jsegdep *); 802 static void free_jsegs(struct jblocks *); 803 static void rele_jseg(struct jseg *); 804 static void free_jseg(struct jseg *, struct jblocks *); 805 static void free_jnewblk(struct jnewblk *); 806 static void free_jblkdep(struct jblkdep *); 807 static void free_jfreefrag(struct jfreefrag *); 808 static void free_freedep(struct freedep *); 809 static void journal_jremref(struct dirrem *, struct jremref *, 810 struct inodedep *); 811 static void cancel_jnewblk(struct jnewblk *, struct workhead *); 812 static int cancel_jaddref(struct jaddref *, struct inodedep *, 813 struct workhead *); 814 static void cancel_jfreefrag(struct jfreefrag *); 815 static inline void setup_freedirect(struct freeblks *, struct inode *, 816 int, int); 817 static inline void setup_freeext(struct freeblks *, struct inode *, int, int); 818 static inline void setup_freeindir(struct freeblks *, struct inode *, int, 819 ufs_lbn_t, int); 820 static inline struct freeblks *newfreeblks(struct mount *, struct inode *); 821 static void freeblks_free(struct ufsmount *, struct freeblks *, int); 822 static void indir_trunc(struct freework *, ufs2_daddr_t, ufs_lbn_t); 823 static ufs2_daddr_t blkcount(struct fs *, ufs2_daddr_t, off_t); 824 static int trunc_check_buf(struct buf *, int *, ufs_lbn_t, int, int); 825 static void trunc_dependencies(struct inode *, struct freeblks *, ufs_lbn_t, 826 int, int); 827 static void trunc_pages(struct inode *, off_t, ufs2_daddr_t, int); 828 static int cancel_pagedep(struct pagedep *, struct freeblks *, int); 829 static int deallocate_dependencies(struct buf *, struct freeblks *, int); 830 static void newblk_freefrag(struct newblk*); 831 static void free_newblk(struct newblk *); 832 static void cancel_allocdirect(struct allocdirectlst *, 833 struct allocdirect *, struct freeblks *); 834 static int check_inode_unwritten(struct inodedep *); 835 static int free_inodedep(struct inodedep *); 836 static void freework_freeblock(struct freework *, uint64_t); 837 static void freework_enqueue(struct freework *); 838 static int handle_workitem_freeblocks(struct freeblks *, int); 839 static int handle_complete_freeblocks(struct freeblks *, int); 840 static void handle_workitem_indirblk(struct freework *); 841 static void handle_written_freework(struct freework *); 842 static void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *); 843 static struct worklist *jnewblk_merge(struct worklist *, struct worklist *, 844 struct workhead *); 845 static struct freefrag *setup_allocindir_phase2(struct buf *, struct inode *, 846 struct inodedep *, struct allocindir *, ufs_lbn_t); 847 static struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t, 848 ufs2_daddr_t, ufs_lbn_t); 849 static void handle_workitem_freefrag(struct freefrag *); 850 static struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long, 851 ufs_lbn_t, uint64_t); 852 static void allocdirect_merge(struct allocdirectlst *, 853 struct allocdirect *, struct allocdirect *); 854 static struct freefrag *allocindir_merge(struct allocindir *, 855 struct allocindir *); 856 static int bmsafemap_find(struct bmsafemap_hashhead *, int, 857 struct bmsafemap **); 858 static struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *, 859 int cg, struct bmsafemap *); 860 static int newblk_find(struct newblk_hashhead *, ufs2_daddr_t, int, 861 struct newblk **); 862 static int newblk_lookup(struct mount *, ufs2_daddr_t, int, struct newblk **); 863 static int inodedep_find(struct inodedep_hashhead *, ino_t, 864 struct inodedep **); 865 static int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **); 866 static int pagedep_lookup(struct mount *, struct buf *bp, ino_t, ufs_lbn_t, 867 int, struct pagedep **); 868 static int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t, 869 struct pagedep **); 870 static void pause_timer(void *); 871 static int request_cleanup(struct mount *, int); 872 static int softdep_request_cleanup_flush(struct mount *, struct ufsmount *); 873 static void schedule_cleanup(struct mount *); 874 static void softdep_ast_cleanup_proc(struct thread *, int); 875 static struct ufsmount *softdep_bp_to_mp(struct buf *bp); 876 static int process_worklist_item(struct mount *, int, int); 877 static void process_removes(struct vnode *); 878 static void process_truncates(struct vnode *); 879 static void jwork_move(struct workhead *, struct workhead *); 880 static void jwork_insert(struct workhead *, struct jsegdep *); 881 static void add_to_worklist(struct worklist *, int); 882 static void wake_worklist(struct worklist *); 883 static void wait_worklist(struct worklist *, char *); 884 static void remove_from_worklist(struct worklist *); 885 static void softdep_flush(void *); 886 static void softdep_flushjournal(struct mount *); 887 static int softdep_speedup(struct ufsmount *); 888 static void worklist_speedup(struct mount *); 889 static int journal_mount(struct mount *, struct fs *, struct ucred *); 890 static void journal_unmount(struct ufsmount *); 891 static int journal_space(struct ufsmount *, int); 892 static void journal_suspend(struct ufsmount *); 893 static int journal_unsuspend(struct ufsmount *ump); 894 static void add_to_journal(struct worklist *); 895 static void remove_from_journal(struct worklist *); 896 static bool softdep_excess_items(struct ufsmount *, int); 897 static void softdep_process_journal(struct mount *, struct worklist *, int); 898 static struct jremref *newjremref(struct dirrem *, struct inode *, 899 struct inode *ip, off_t, nlink_t); 900 static struct jaddref *newjaddref(struct inode *, ino_t, off_t, int16_t, 901 uint16_t); 902 static inline void newinoref(struct inoref *, ino_t, ino_t, off_t, nlink_t, 903 uint16_t); 904 static inline struct jsegdep *inoref_jseg(struct inoref *); 905 static struct jmvref *newjmvref(struct inode *, ino_t, off_t, off_t); 906 static struct jfreeblk *newjfreeblk(struct freeblks *, ufs_lbn_t, 907 ufs2_daddr_t, int); 908 static void adjust_newfreework(struct freeblks *, int); 909 static struct jtrunc *newjtrunc(struct freeblks *, off_t, int); 910 static void move_newblock_dep(struct jaddref *, struct inodedep *); 911 static void cancel_jfreeblk(struct freeblks *, ufs2_daddr_t); 912 static struct jfreefrag *newjfreefrag(struct freefrag *, struct inode *, 913 ufs2_daddr_t, long, ufs_lbn_t); 914 static struct freework *newfreework(struct ufsmount *, struct freeblks *, 915 struct freework *, ufs_lbn_t, ufs2_daddr_t, int, int, int); 916 static int jwait(struct worklist *, int); 917 static struct inodedep *inodedep_lookup_ip(struct inode *); 918 static int bmsafemap_backgroundwrite(struct bmsafemap *, struct buf *); 919 static struct freefile *handle_bufwait(struct inodedep *, struct workhead *); 920 static void handle_jwork(struct workhead *); 921 static struct mkdir *setup_newdir(struct diradd *, ino_t, ino_t, struct buf *, 922 struct mkdir **); 923 static struct jblocks *jblocks_create(void); 924 static ufs2_daddr_t jblocks_alloc(struct jblocks *, int, int *); 925 static void jblocks_free(struct jblocks *, struct mount *, int); 926 static void jblocks_destroy(struct jblocks *); 927 static void jblocks_add(struct jblocks *, ufs2_daddr_t, int); 928 929 /* 930 * Exported softdep operations. 931 */ 932 static void softdep_disk_io_initiation(struct buf *); 933 static void softdep_disk_write_complete(struct buf *); 934 static void softdep_deallocate_dependencies(struct buf *); 935 static int softdep_count_dependencies(struct buf *bp, int); 936 937 /* 938 * Global lock over all of soft updates. 939 */ 940 static struct mtx lk; 941 MTX_SYSINIT(softdep_lock, &lk, "global softdep", MTX_DEF); 942 943 #define ACQUIRE_GBLLOCK(lk) mtx_lock(lk) 944 #define FREE_GBLLOCK(lk) mtx_unlock(lk) 945 #define GBLLOCK_OWNED(lk) mtx_assert((lk), MA_OWNED) 946 947 /* 948 * Per-filesystem soft-updates locking. 949 */ 950 #define LOCK_PTR(ump) (&(ump)->um_softdep->sd_fslock) 951 #define TRY_ACQUIRE_LOCK(ump) rw_try_wlock(&(ump)->um_softdep->sd_fslock) 952 #define ACQUIRE_LOCK(ump) rw_wlock(&(ump)->um_softdep->sd_fslock) 953 #define FREE_LOCK(ump) rw_wunlock(&(ump)->um_softdep->sd_fslock) 954 #define LOCK_OWNED(ump) rw_assert(&(ump)->um_softdep->sd_fslock, \ 955 RA_WLOCKED) 956 957 #define BUF_AREC(bp) lockallowrecurse(&(bp)->b_lock) 958 #define BUF_NOREC(bp) lockdisablerecurse(&(bp)->b_lock) 959 960 /* 961 * Worklist queue management. 962 * These routines require that the lock be held. 963 */ 964 #ifndef /* NOT */ INVARIANTS 965 #define WORKLIST_INSERT(head, item) do { \ 966 (item)->wk_state |= ONWORKLIST; \ 967 LIST_INSERT_HEAD(head, item, wk_list); \ 968 } while (0) 969 #define WORKLIST_REMOVE(item) do { \ 970 (item)->wk_state &= ~ONWORKLIST; \ 971 LIST_REMOVE(item, wk_list); \ 972 } while (0) 973 #define WORKLIST_INSERT_UNLOCKED WORKLIST_INSERT 974 #define WORKLIST_REMOVE_UNLOCKED WORKLIST_REMOVE 975 976 #else /* INVARIANTS */ 977 static void worklist_insert(struct workhead *, struct worklist *, int, 978 const char *, int); 979 static void worklist_remove(struct worklist *, int, const char *, int); 980 981 #define WORKLIST_INSERT(head, item) \ 982 worklist_insert(head, item, 1, __func__, __LINE__) 983 #define WORKLIST_INSERT_UNLOCKED(head, item)\ 984 worklist_insert(head, item, 0, __func__, __LINE__) 985 #define WORKLIST_REMOVE(item)\ 986 worklist_remove(item, 1, __func__, __LINE__) 987 #define WORKLIST_REMOVE_UNLOCKED(item)\ 988 worklist_remove(item, 0, __func__, __LINE__) 989 990 static void 991 worklist_insert(struct workhead *head, 992 struct worklist *item, 993 int locked, 994 const char *func, 995 int line) 996 { 997 998 if (locked) 999 LOCK_OWNED(VFSTOUFS(item->wk_mp)); 1000 if (item->wk_state & ONWORKLIST) 1001 panic("worklist_insert: %p %s(0x%X) already on list, " 1002 "added in function %s at line %d", 1003 item, TYPENAME(item->wk_type), item->wk_state, 1004 item->wk_func, item->wk_line); 1005 item->wk_state |= ONWORKLIST; 1006 item->wk_func = func; 1007 item->wk_line = line; 1008 LIST_INSERT_HEAD(head, item, wk_list); 1009 } 1010 1011 static void 1012 worklist_remove(struct worklist *item, 1013 int locked, 1014 const char *func, 1015 int line) 1016 { 1017 1018 if (locked) 1019 LOCK_OWNED(VFSTOUFS(item->wk_mp)); 1020 if ((item->wk_state & ONWORKLIST) == 0) 1021 panic("worklist_remove: %p %s(0x%X) not on list, " 1022 "removed in function %s at line %d", 1023 item, TYPENAME(item->wk_type), item->wk_state, 1024 item->wk_func, item->wk_line); 1025 item->wk_state &= ~ONWORKLIST; 1026 item->wk_func = func; 1027 item->wk_line = line; 1028 LIST_REMOVE(item, wk_list); 1029 } 1030 #endif /* INVARIANTS */ 1031 1032 /* 1033 * Merge two jsegdeps keeping only the oldest one as newer references 1034 * can't be discarded until after older references. 1035 */ 1036 static inline struct jsegdep * 1037 jsegdep_merge(struct jsegdep *one, struct jsegdep *two) 1038 { 1039 struct jsegdep *swp; 1040 1041 if (two == NULL) 1042 return (one); 1043 1044 if (one->jd_seg->js_seq > two->jd_seg->js_seq) { 1045 swp = one; 1046 one = two; 1047 two = swp; 1048 } 1049 WORKLIST_REMOVE(&two->jd_list); 1050 free_jsegdep(two); 1051 1052 return (one); 1053 } 1054 1055 /* 1056 * If two freedeps are compatible free one to reduce list size. 1057 */ 1058 static inline struct freedep * 1059 freedep_merge(struct freedep *one, struct freedep *two) 1060 { 1061 if (two == NULL) 1062 return (one); 1063 1064 if (one->fd_freework == two->fd_freework) { 1065 WORKLIST_REMOVE(&two->fd_list); 1066 free_freedep(two); 1067 } 1068 return (one); 1069 } 1070 1071 /* 1072 * Move journal work from one list to another. Duplicate freedeps and 1073 * jsegdeps are coalesced to keep the lists as small as possible. 1074 */ 1075 static void 1076 jwork_move(struct workhead *dst, struct workhead *src) 1077 { 1078 struct freedep *freedep; 1079 struct jsegdep *jsegdep; 1080 struct worklist *wkn; 1081 struct worklist *wk; 1082 1083 KASSERT(dst != src, 1084 ("jwork_move: dst == src")); 1085 freedep = NULL; 1086 jsegdep = NULL; 1087 LIST_FOREACH_SAFE(wk, dst, wk_list, wkn) { 1088 if (wk->wk_type == D_JSEGDEP) 1089 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1090 else if (wk->wk_type == D_FREEDEP) 1091 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1092 } 1093 1094 while ((wk = LIST_FIRST(src)) != NULL) { 1095 WORKLIST_REMOVE(wk); 1096 WORKLIST_INSERT(dst, wk); 1097 if (wk->wk_type == D_JSEGDEP) { 1098 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1099 continue; 1100 } 1101 if (wk->wk_type == D_FREEDEP) 1102 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1103 } 1104 } 1105 1106 static void 1107 jwork_insert(struct workhead *dst, struct jsegdep *jsegdep) 1108 { 1109 struct jsegdep *jsegdepn; 1110 struct worklist *wk; 1111 1112 LIST_FOREACH(wk, dst, wk_list) 1113 if (wk->wk_type == D_JSEGDEP) 1114 break; 1115 if (wk == NULL) { 1116 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1117 return; 1118 } 1119 jsegdepn = WK_JSEGDEP(wk); 1120 if (jsegdep->jd_seg->js_seq < jsegdepn->jd_seg->js_seq) { 1121 WORKLIST_REMOVE(wk); 1122 free_jsegdep(jsegdepn); 1123 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1124 } else 1125 free_jsegdep(jsegdep); 1126 } 1127 1128 /* 1129 * Routines for tracking and managing workitems. 1130 */ 1131 static void workitem_free(struct worklist *, int); 1132 static void workitem_alloc(struct worklist *, int, struct mount *); 1133 static void workitem_reassign(struct worklist *, int); 1134 1135 #define WORKITEM_FREE(item, type) \ 1136 workitem_free((struct worklist *)(item), (type)) 1137 #define WORKITEM_REASSIGN(item, type) \ 1138 workitem_reassign((struct worklist *)(item), (type)) 1139 1140 static void 1141 workitem_free(struct worklist *item, int type) 1142 { 1143 struct ufsmount *ump; 1144 1145 #ifdef INVARIANTS 1146 if (item->wk_state & ONWORKLIST) 1147 panic("workitem_free: %s(0x%X) still on list, " 1148 "added in function %s at line %d", 1149 TYPENAME(item->wk_type), item->wk_state, 1150 item->wk_func, item->wk_line); 1151 if (item->wk_type != type && type != D_NEWBLK) 1152 panic("workitem_free: type mismatch %s != %s", 1153 TYPENAME(item->wk_type), TYPENAME(type)); 1154 #endif 1155 if (item->wk_state & IOWAITING) 1156 wakeup(item); 1157 ump = VFSTOUFS(item->wk_mp); 1158 LOCK_OWNED(ump); 1159 KASSERT(ump->softdep_deps > 0, 1160 ("workitem_free: %s: softdep_deps going negative", 1161 ump->um_fs->fs_fsmnt)); 1162 if (--ump->softdep_deps == 0 && ump->softdep_req) 1163 wakeup(&ump->softdep_deps); 1164 KASSERT(dep_current[item->wk_type] > 0, 1165 ("workitem_free: %s: dep_current[%s] going negative", 1166 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1167 KASSERT(ump->softdep_curdeps[item->wk_type] > 0, 1168 ("workitem_free: %s: softdep_curdeps[%s] going negative", 1169 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1170 atomic_subtract_long(&dep_current[item->wk_type], 1); 1171 ump->softdep_curdeps[item->wk_type] -= 1; 1172 LIST_REMOVE(item, wk_all); 1173 free(item, DtoM(type)); 1174 } 1175 1176 static void 1177 workitem_alloc(struct worklist *item, 1178 int type, 1179 struct mount *mp) 1180 { 1181 struct ufsmount *ump; 1182 1183 item->wk_type = type; 1184 item->wk_mp = mp; 1185 item->wk_state = 0; 1186 1187 ump = VFSTOUFS(mp); 1188 ACQUIRE_GBLLOCK(&lk); 1189 dep_current[type]++; 1190 if (dep_current[type] > dep_highuse[type]) 1191 dep_highuse[type] = dep_current[type]; 1192 dep_total[type]++; 1193 FREE_GBLLOCK(&lk); 1194 ACQUIRE_LOCK(ump); 1195 ump->softdep_curdeps[type] += 1; 1196 ump->softdep_deps++; 1197 ump->softdep_accdeps++; 1198 LIST_INSERT_HEAD(&ump->softdep_alldeps[type], item, wk_all); 1199 FREE_LOCK(ump); 1200 } 1201 1202 static void 1203 workitem_reassign(struct worklist *item, int newtype) 1204 { 1205 struct ufsmount *ump; 1206 1207 ump = VFSTOUFS(item->wk_mp); 1208 LOCK_OWNED(ump); 1209 KASSERT(ump->softdep_curdeps[item->wk_type] > 0, 1210 ("workitem_reassign: %s: softdep_curdeps[%s] going negative", 1211 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1212 ump->softdep_curdeps[item->wk_type] -= 1; 1213 ump->softdep_curdeps[newtype] += 1; 1214 KASSERT(dep_current[item->wk_type] > 0, 1215 ("workitem_reassign: %s: dep_current[%s] going negative", 1216 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1217 ACQUIRE_GBLLOCK(&lk); 1218 dep_current[newtype]++; 1219 dep_current[item->wk_type]--; 1220 if (dep_current[newtype] > dep_highuse[newtype]) 1221 dep_highuse[newtype] = dep_current[newtype]; 1222 dep_total[newtype]++; 1223 FREE_GBLLOCK(&lk); 1224 item->wk_type = newtype; 1225 LIST_REMOVE(item, wk_all); 1226 LIST_INSERT_HEAD(&ump->softdep_alldeps[newtype], item, wk_all); 1227 } 1228 1229 /* 1230 * Workitem queue management 1231 */ 1232 static int max_softdeps; /* maximum number of structs before slowdown */ 1233 static int tickdelay = 2; /* number of ticks to pause during slowdown */ 1234 static int proc_waiting; /* tracks whether we have a timeout posted */ 1235 static int *stat_countp; /* statistic to count in proc_waiting timeout */ 1236 static struct callout softdep_callout; 1237 static int req_clear_inodedeps; /* syncer process flush some inodedeps */ 1238 static int req_clear_remove; /* syncer process flush some freeblks */ 1239 static int softdep_flushcache = 0; /* Should we do BIO_FLUSH? */ 1240 1241 /* 1242 * runtime statistics 1243 */ 1244 static int stat_flush_threads; /* number of softdep flushing threads */ 1245 static int stat_worklist_push; /* number of worklist cleanups */ 1246 static int stat_delayed_inact; /* number of delayed inactivation cleanups */ 1247 static int stat_blk_limit_push; /* number of times block limit neared */ 1248 static int stat_ino_limit_push; /* number of times inode limit neared */ 1249 static int stat_blk_limit_hit; /* number of times block slowdown imposed */ 1250 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */ 1251 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */ 1252 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */ 1253 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */ 1254 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */ 1255 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */ 1256 static int stat_jaddref; /* bufs redirtied as ino bitmap can not write */ 1257 static int stat_jnewblk; /* bufs redirtied as blk bitmap can not write */ 1258 static int stat_journal_min; /* Times hit journal min threshold */ 1259 static int stat_journal_low; /* Times hit journal low threshold */ 1260 static int stat_journal_wait; /* Times blocked in jwait(). */ 1261 static int stat_jwait_filepage; /* Times blocked in jwait() for filepage. */ 1262 static int stat_jwait_freeblks; /* Times blocked in jwait() for freeblks. */ 1263 static int stat_jwait_inode; /* Times blocked in jwait() for inodes. */ 1264 static int stat_jwait_newblk; /* Times blocked in jwait() for newblks. */ 1265 static int stat_cleanup_high_delay; /* Maximum cleanup delay (in ticks) */ 1266 static int stat_cleanup_blkrequests; /* Number of block cleanup requests */ 1267 static int stat_cleanup_inorequests; /* Number of inode cleanup requests */ 1268 static int stat_cleanup_retries; /* Number of cleanups that needed to flush */ 1269 static int stat_cleanup_failures; /* Number of cleanup requests that failed */ 1270 static int stat_emptyjblocks; /* Number of potentially empty journal blocks */ 1271 1272 SYSCTL_INT(_debug_softdep, OID_AUTO, max_softdeps, CTLFLAG_RW, 1273 &max_softdeps, 0, ""); 1274 SYSCTL_INT(_debug_softdep, OID_AUTO, tickdelay, CTLFLAG_RW, 1275 &tickdelay, 0, ""); 1276 SYSCTL_INT(_debug_softdep, OID_AUTO, flush_threads, CTLFLAG_RD, 1277 &stat_flush_threads, 0, ""); 1278 SYSCTL_INT(_debug_softdep, OID_AUTO, worklist_push, 1279 CTLFLAG_RW | CTLFLAG_STATS, &stat_worklist_push, 0,""); 1280 SYSCTL_INT(_debug_softdep, OID_AUTO, delayed_inactivations, CTLFLAG_RD, 1281 &stat_delayed_inact, 0, ""); 1282 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_push, 1283 CTLFLAG_RW | CTLFLAG_STATS, &stat_blk_limit_push, 0,""); 1284 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_push, 1285 CTLFLAG_RW | CTLFLAG_STATS, &stat_ino_limit_push, 0,""); 1286 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_hit, 1287 CTLFLAG_RW | CTLFLAG_STATS, &stat_blk_limit_hit, 0, ""); 1288 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_hit, 1289 CTLFLAG_RW | CTLFLAG_STATS, &stat_ino_limit_hit, 0, ""); 1290 SYSCTL_INT(_debug_softdep, OID_AUTO, sync_limit_hit, 1291 CTLFLAG_RW | CTLFLAG_STATS, &stat_sync_limit_hit, 0, ""); 1292 SYSCTL_INT(_debug_softdep, OID_AUTO, indir_blk_ptrs, 1293 CTLFLAG_RW | CTLFLAG_STATS, &stat_indir_blk_ptrs, 0, ""); 1294 SYSCTL_INT(_debug_softdep, OID_AUTO, inode_bitmap, 1295 CTLFLAG_RW | CTLFLAG_STATS, &stat_inode_bitmap, 0, ""); 1296 SYSCTL_INT(_debug_softdep, OID_AUTO, direct_blk_ptrs, 1297 CTLFLAG_RW | CTLFLAG_STATS, &stat_direct_blk_ptrs, 0, ""); 1298 SYSCTL_INT(_debug_softdep, OID_AUTO, dir_entry, 1299 CTLFLAG_RW | CTLFLAG_STATS, &stat_dir_entry, 0, ""); 1300 SYSCTL_INT(_debug_softdep, OID_AUTO, jaddref_rollback, 1301 CTLFLAG_RW | CTLFLAG_STATS, &stat_jaddref, 0, ""); 1302 SYSCTL_INT(_debug_softdep, OID_AUTO, jnewblk_rollback, 1303 CTLFLAG_RW | CTLFLAG_STATS, &stat_jnewblk, 0, ""); 1304 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_low, 1305 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_low, 0, ""); 1306 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_min, 1307 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_min, 0, ""); 1308 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_wait, 1309 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_wait, 0, ""); 1310 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_filepage, 1311 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_filepage, 0, ""); 1312 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_freeblks, 1313 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_freeblks, 0, ""); 1314 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_inode, 1315 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_inode, 0, ""); 1316 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_newblk, 1317 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_newblk, 0, ""); 1318 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_blkrequests, 1319 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_blkrequests, 0, ""); 1320 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_inorequests, 1321 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_inorequests, 0, ""); 1322 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_high_delay, 1323 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_high_delay, 0, ""); 1324 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_retries, 1325 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_retries, 0, ""); 1326 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_failures, 1327 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_failures, 0, ""); 1328 1329 SYSCTL_INT(_debug_softdep, OID_AUTO, flushcache, CTLFLAG_RW, 1330 &softdep_flushcache, 0, ""); 1331 SYSCTL_INT(_debug_softdep, OID_AUTO, emptyjblocks, CTLFLAG_RD, 1332 &stat_emptyjblocks, 0, ""); 1333 1334 SYSCTL_DECL(_vfs_ffs); 1335 1336 /* Whether to recompute the summary at mount time */ 1337 static int compute_summary_at_mount = 0; 1338 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW, 1339 &compute_summary_at_mount, 0, "Recompute summary at mount"); 1340 static int print_threads = 0; 1341 SYSCTL_INT(_debug_softdep, OID_AUTO, print_threads, CTLFLAG_RW, 1342 &print_threads, 0, "Notify flusher thread start/stop"); 1343 1344 /* List of all filesystems mounted with soft updates */ 1345 static TAILQ_HEAD(, mount_softdeps) softdepmounts; 1346 1347 static void 1348 get_parent_vp_unlock_bp(struct mount *mp, 1349 struct buf *bp, 1350 struct diraddhd *diraddhdp, 1351 struct diraddhd *unfinishedp) 1352 { 1353 struct diradd *dap; 1354 1355 /* 1356 * Requeue unfinished dependencies before 1357 * unlocking buffer, which could make 1358 * diraddhdp invalid. 1359 */ 1360 ACQUIRE_LOCK(VFSTOUFS(mp)); 1361 while ((dap = LIST_FIRST(unfinishedp)) != NULL) { 1362 LIST_REMOVE(dap, da_pdlist); 1363 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist); 1364 } 1365 FREE_LOCK(VFSTOUFS(mp)); 1366 1367 bp->b_vflags &= ~BV_SCANNED; 1368 BUF_NOREC(bp); 1369 BUF_UNLOCK(bp); 1370 } 1371 1372 /* 1373 * This function fetches inode inum on mount point mp. We already 1374 * hold a locked vnode vp, and might have a locked buffer bp belonging 1375 * to vp. 1376 1377 * We must not block on acquiring the new inode lock as we will get 1378 * into a lock-order reversal with the buffer lock and possibly get a 1379 * deadlock. Thus if we cannot instantiate the requested vnode 1380 * without sleeping on its lock, we must unlock the vnode and the 1381 * buffer before doing a blocking on the vnode lock. We return 1382 * ERELOOKUP if we have had to unlock either the vnode or the buffer so 1383 * that the caller can reassess its state. 1384 * 1385 * Top-level VFS code (for syscalls and other consumers, e.g. callers 1386 * of VOP_FSYNC() in syncer) check for ERELOOKUP and restart at safe 1387 * point. 1388 * 1389 * Since callers expect to operate on fully constructed vnode, we also 1390 * recheck v_data after relock, and return ENOENT if NULL. 1391 * 1392 * If unlocking bp, we must unroll dequeueing its unfinished 1393 * dependencies, and clear scan flag, before unlocking. If unlocking 1394 * vp while it is under deactivation, we re-queue deactivation. 1395 */ 1396 static int 1397 get_parent_vp(struct vnode *vp, 1398 struct mount *mp, 1399 ino_t inum, 1400 struct buf *bp, 1401 struct diraddhd *diraddhdp, 1402 struct diraddhd *unfinishedp, 1403 struct vnode **rvp) 1404 { 1405 struct vnode *pvp; 1406 int error; 1407 bool bplocked; 1408 1409 ASSERT_VOP_ELOCKED(vp, "child vnode must be locked"); 1410 for (bplocked = true, pvp = NULL;;) { 1411 error = ffs_vgetf(mp, inum, LK_EXCLUSIVE | LK_NOWAIT, &pvp, 1412 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP); 1413 if (error == 0) { 1414 /* 1415 * Since we could have unlocked vp, the inode 1416 * number could no longer indicate a 1417 * constructed node. In this case, we must 1418 * restart the syscall. 1419 */ 1420 if (VTOI(pvp)->i_mode == 0 || !bplocked) { 1421 if (bp != NULL && bplocked) 1422 get_parent_vp_unlock_bp(mp, bp, 1423 diraddhdp, unfinishedp); 1424 if (VTOI(pvp)->i_mode == 0) 1425 vgone(pvp); 1426 error = ERELOOKUP; 1427 goto out2; 1428 } 1429 goto out1; 1430 } 1431 if (bp != NULL && bplocked) { 1432 get_parent_vp_unlock_bp(mp, bp, diraddhdp, unfinishedp); 1433 bplocked = false; 1434 } 1435 1436 /* 1437 * Do not drop vnode lock while inactivating during 1438 * vunref. This would result in leaks of the VI flags 1439 * and reclaiming of non-truncated vnode. Instead, 1440 * re-schedule inactivation hoping that we would be 1441 * able to sync inode later. 1442 */ 1443 if ((vp->v_iflag & VI_DOINGINACT) != 0 && 1444 (vp->v_vflag & VV_UNREF) != 0) { 1445 VI_LOCK(vp); 1446 vp->v_iflag |= VI_OWEINACT; 1447 VI_UNLOCK(vp); 1448 return (ERELOOKUP); 1449 } 1450 1451 VOP_UNLOCK(vp); 1452 error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &pvp, 1453 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP); 1454 if (error != 0) { 1455 MPASS(error != ERELOOKUP); 1456 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1457 break; 1458 } 1459 if (VTOI(pvp)->i_mode == 0) { 1460 vgone(pvp); 1461 vput(pvp); 1462 pvp = NULL; 1463 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1464 error = ERELOOKUP; 1465 break; 1466 } 1467 error = vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT); 1468 if (error == 0) 1469 break; 1470 vput(pvp); 1471 pvp = NULL; 1472 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1473 if (vp->v_data == NULL) { 1474 error = ENOENT; 1475 break; 1476 } 1477 } 1478 if (bp != NULL) { 1479 MPASS(!bplocked); 1480 error = ERELOOKUP; 1481 } 1482 out2: 1483 if (error != 0 && pvp != NULL) { 1484 vput(pvp); 1485 pvp = NULL; 1486 } 1487 out1: 1488 *rvp = pvp; 1489 ASSERT_VOP_ELOCKED(vp, "child vnode must be locked on return"); 1490 return (error); 1491 } 1492 1493 /* 1494 * This function cleans the worklist for a filesystem. 1495 * Each filesystem running with soft dependencies gets its own 1496 * thread to run in this function. The thread is started up in 1497 * softdep_mount and shutdown in softdep_unmount. They show up 1498 * as part of the kernel "bufdaemon" process whose process 1499 * entry is available in bufdaemonproc. 1500 */ 1501 static int searchfailed; 1502 extern struct proc *bufdaemonproc; 1503 static void 1504 softdep_flush(void *addr) 1505 { 1506 struct mount *mp; 1507 struct thread *td; 1508 struct ufsmount *ump; 1509 int cleanups; 1510 1511 td = curthread; 1512 td->td_pflags |= TDP_NORUNNINGBUF; 1513 mp = (struct mount *)addr; 1514 ump = VFSTOUFS(mp); 1515 atomic_add_int(&stat_flush_threads, 1); 1516 ACQUIRE_LOCK(ump); 1517 ump->softdep_flags &= ~FLUSH_STARTING; 1518 wakeup(&ump->softdep_flushtd); 1519 FREE_LOCK(ump); 1520 if (print_threads) { 1521 if (stat_flush_threads == 1) 1522 printf("Running %s at pid %d\n", bufdaemonproc->p_comm, 1523 bufdaemonproc->p_pid); 1524 printf("Start thread %s\n", td->td_name); 1525 } 1526 for (;;) { 1527 while (softdep_process_worklist(mp, 0) > 0 || 1528 (MOUNTEDSUJ(mp) && 1529 VFSTOUFS(mp)->softdep_jblocks->jb_suspended)) 1530 kthread_suspend_check(); 1531 ACQUIRE_LOCK(ump); 1532 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1533 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, 1534 "sdflush", hz / 2); 1535 ump->softdep_flags &= ~FLUSH_CLEANUP; 1536 /* 1537 * Check to see if we are done and need to exit. 1538 */ 1539 if ((ump->softdep_flags & FLUSH_EXIT) == 0) { 1540 FREE_LOCK(ump); 1541 continue; 1542 } 1543 ump->softdep_flags &= ~FLUSH_EXIT; 1544 cleanups = ump->um_softdep->sd_cleanups; 1545 FREE_LOCK(ump); 1546 wakeup(&ump->softdep_flags); 1547 if (print_threads) { 1548 printf("Stop thread %s: searchfailed %d, " 1549 "did cleanups %d\n", 1550 td->td_name, searchfailed, cleanups); 1551 } 1552 atomic_subtract_int(&stat_flush_threads, 1); 1553 kthread_exit(); 1554 panic("kthread_exit failed\n"); 1555 } 1556 } 1557 1558 static void 1559 worklist_speedup(struct mount *mp) 1560 { 1561 struct ufsmount *ump; 1562 1563 ump = VFSTOUFS(mp); 1564 LOCK_OWNED(ump); 1565 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1566 ump->softdep_flags |= FLUSH_CLEANUP; 1567 wakeup(&ump->softdep_flushtd); 1568 } 1569 1570 static void 1571 softdep_send_speedup(struct ufsmount *ump, 1572 off_t shortage, 1573 uint64_t flags) 1574 { 1575 struct buf *bp; 1576 1577 if ((ump->um_flags & UM_CANSPEEDUP) == 0) 1578 return; 1579 1580 bp = malloc(sizeof(*bp), M_TRIM, M_WAITOK | M_ZERO); 1581 bp->b_iocmd = BIO_SPEEDUP; 1582 bp->b_ioflags = flags; 1583 bp->b_bcount = omin(shortage, LONG_MAX); 1584 g_vfs_strategy(ump->um_bo, bp); 1585 bufwait(bp); 1586 free(bp, M_TRIM); 1587 } 1588 1589 static int 1590 softdep_speedup(struct ufsmount *ump) 1591 { 1592 struct ufsmount *altump; 1593 struct mount_softdeps *sdp; 1594 1595 LOCK_OWNED(ump); 1596 worklist_speedup(ump->um_mountp); 1597 bd_speedup(); 1598 /* 1599 * If we have global shortages, then we need other 1600 * filesystems to help with the cleanup. Here we wakeup a 1601 * flusher thread for a filesystem that is over its fair 1602 * share of resources. 1603 */ 1604 if (req_clear_inodedeps || req_clear_remove) { 1605 ACQUIRE_GBLLOCK(&lk); 1606 TAILQ_FOREACH(sdp, &softdepmounts, sd_next) { 1607 if ((altump = sdp->sd_ump) == ump) 1608 continue; 1609 if (((req_clear_inodedeps && 1610 altump->softdep_curdeps[D_INODEDEP] > 1611 max_softdeps / stat_flush_threads) || 1612 (req_clear_remove && 1613 altump->softdep_curdeps[D_DIRREM] > 1614 (max_softdeps / 2) / stat_flush_threads)) && 1615 TRY_ACQUIRE_LOCK(altump)) 1616 break; 1617 } 1618 if (sdp == NULL) { 1619 searchfailed++; 1620 FREE_GBLLOCK(&lk); 1621 } else { 1622 /* 1623 * Move to the end of the list so we pick a 1624 * different one on out next try. 1625 */ 1626 TAILQ_REMOVE(&softdepmounts, sdp, sd_next); 1627 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next); 1628 FREE_GBLLOCK(&lk); 1629 if ((altump->softdep_flags & 1630 (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1631 altump->softdep_flags |= FLUSH_CLEANUP; 1632 altump->um_softdep->sd_cleanups++; 1633 wakeup(&altump->softdep_flushtd); 1634 FREE_LOCK(altump); 1635 } 1636 } 1637 return (speedup_syncer()); 1638 } 1639 1640 /* 1641 * Add an item to the end of the work queue. 1642 * This routine requires that the lock be held. 1643 * This is the only routine that adds items to the list. 1644 * The following routine is the only one that removes items 1645 * and does so in order from first to last. 1646 */ 1647 1648 #define WK_HEAD 0x0001 /* Add to HEAD. */ 1649 #define WK_NODELAY 0x0002 /* Process immediately. */ 1650 1651 static void 1652 add_to_worklist(struct worklist *wk, int flags) 1653 { 1654 struct ufsmount *ump; 1655 1656 ump = VFSTOUFS(wk->wk_mp); 1657 LOCK_OWNED(ump); 1658 if (wk->wk_state & ONWORKLIST) 1659 panic("add_to_worklist: %s(0x%X) already on list", 1660 TYPENAME(wk->wk_type), wk->wk_state); 1661 wk->wk_state |= ONWORKLIST; 1662 if (ump->softdep_on_worklist == 0) { 1663 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1664 ump->softdep_worklist_tail = wk; 1665 } else if (flags & WK_HEAD) { 1666 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1667 } else { 1668 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list); 1669 ump->softdep_worklist_tail = wk; 1670 } 1671 ump->softdep_on_worklist += 1; 1672 if (flags & WK_NODELAY) 1673 worklist_speedup(wk->wk_mp); 1674 } 1675 1676 /* 1677 * Remove the item to be processed. If we are removing the last 1678 * item on the list, we need to recalculate the tail pointer. 1679 */ 1680 static void 1681 remove_from_worklist(struct worklist *wk) 1682 { 1683 struct ufsmount *ump; 1684 1685 ump = VFSTOUFS(wk->wk_mp); 1686 if (ump->softdep_worklist_tail == wk) 1687 ump->softdep_worklist_tail = 1688 (struct worklist *)wk->wk_list.le_prev; 1689 WORKLIST_REMOVE(wk); 1690 ump->softdep_on_worklist -= 1; 1691 } 1692 1693 static void 1694 wake_worklist(struct worklist *wk) 1695 { 1696 if (wk->wk_state & IOWAITING) { 1697 wk->wk_state &= ~IOWAITING; 1698 wakeup(wk); 1699 } 1700 } 1701 1702 static void 1703 wait_worklist(struct worklist *wk, char *wmesg) 1704 { 1705 struct ufsmount *ump; 1706 1707 ump = VFSTOUFS(wk->wk_mp); 1708 wk->wk_state |= IOWAITING; 1709 msleep(wk, LOCK_PTR(ump), PVM, wmesg, 0); 1710 } 1711 1712 /* 1713 * Process that runs once per second to handle items in the background queue. 1714 * 1715 * Note that we ensure that everything is done in the order in which they 1716 * appear in the queue. The code below depends on this property to ensure 1717 * that blocks of a file are freed before the inode itself is freed. This 1718 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated 1719 * until all the old ones have been purged from the dependency lists. 1720 */ 1721 static int 1722 softdep_process_worklist(struct mount *mp, int full) 1723 { 1724 int cnt, matchcnt; 1725 struct ufsmount *ump; 1726 long starttime; 1727 1728 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp")); 1729 ump = VFSTOUFS(mp); 1730 if (ump->um_softdep == NULL) 1731 return (0); 1732 matchcnt = 0; 1733 ACQUIRE_LOCK(ump); 1734 starttime = time_second; 1735 softdep_process_journal(mp, NULL, full ? MNT_WAIT : 0); 1736 check_clear_deps(mp); 1737 while (ump->softdep_on_worklist > 0) { 1738 if ((cnt = process_worklist_item(mp, 10, LK_NOWAIT)) == 0) 1739 break; 1740 else 1741 matchcnt += cnt; 1742 check_clear_deps(mp); 1743 /* 1744 * We do not generally want to stop for buffer space, but if 1745 * we are really being a buffer hog, we will stop and wait. 1746 */ 1747 if (should_yield()) { 1748 FREE_LOCK(ump); 1749 kern_yield(PRI_USER); 1750 bwillwrite(); 1751 ACQUIRE_LOCK(ump); 1752 } 1753 /* 1754 * Never allow processing to run for more than one 1755 * second. This gives the syncer thread the opportunity 1756 * to pause if appropriate. 1757 */ 1758 if (!full && starttime != time_second) 1759 break; 1760 } 1761 if (full == 0) 1762 journal_unsuspend(ump); 1763 FREE_LOCK(ump); 1764 return (matchcnt); 1765 } 1766 1767 /* 1768 * Process all removes associated with a vnode if we are running out of 1769 * journal space. Any other process which attempts to flush these will 1770 * be unable as we have the vnodes locked. 1771 */ 1772 static void 1773 process_removes(struct vnode *vp) 1774 { 1775 struct inodedep *inodedep; 1776 struct dirrem *dirrem; 1777 struct ufsmount *ump; 1778 struct mount *mp; 1779 ino_t inum; 1780 1781 mp = vp->v_mount; 1782 ump = VFSTOUFS(mp); 1783 LOCK_OWNED(ump); 1784 inum = VTOI(vp)->i_number; 1785 for (;;) { 1786 top: 1787 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1788 return; 1789 LIST_FOREACH(dirrem, &inodedep->id_dirremhd, dm_inonext) { 1790 /* 1791 * If another thread is trying to lock this vnode 1792 * it will fail but we must wait for it to do so 1793 * before we can proceed. 1794 */ 1795 if (dirrem->dm_state & INPROGRESS) { 1796 wait_worklist(&dirrem->dm_list, "pwrwait"); 1797 goto top; 1798 } 1799 if ((dirrem->dm_state & (COMPLETE | ONWORKLIST)) == 1800 (COMPLETE | ONWORKLIST)) 1801 break; 1802 } 1803 if (dirrem == NULL) 1804 return; 1805 remove_from_worklist(&dirrem->dm_list); 1806 FREE_LOCK(ump); 1807 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1808 panic("process_removes: suspended filesystem"); 1809 handle_workitem_remove(dirrem, 0); 1810 vn_finished_secondary_write(mp); 1811 ACQUIRE_LOCK(ump); 1812 } 1813 } 1814 1815 /* 1816 * Process all truncations associated with a vnode if we are running out 1817 * of journal space. This is called when the vnode lock is already held 1818 * and no other process can clear the truncation. This function returns 1819 * a value greater than zero if it did any work. 1820 */ 1821 static void 1822 process_truncates(struct vnode *vp) 1823 { 1824 struct inodedep *inodedep; 1825 struct freeblks *freeblks; 1826 struct ufsmount *ump; 1827 struct mount *mp; 1828 ino_t inum; 1829 int cgwait; 1830 1831 mp = vp->v_mount; 1832 ump = VFSTOUFS(mp); 1833 LOCK_OWNED(ump); 1834 inum = VTOI(vp)->i_number; 1835 for (;;) { 1836 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1837 return; 1838 cgwait = 0; 1839 TAILQ_FOREACH(freeblks, &inodedep->id_freeblklst, fb_next) { 1840 /* Journal entries not yet written. */ 1841 if (!LIST_EMPTY(&freeblks->fb_jblkdephd)) { 1842 jwait(&LIST_FIRST( 1843 &freeblks->fb_jblkdephd)->jb_list, 1844 MNT_WAIT); 1845 break; 1846 } 1847 /* Another thread is executing this item. */ 1848 if (freeblks->fb_state & INPROGRESS) { 1849 wait_worklist(&freeblks->fb_list, "ptrwait"); 1850 break; 1851 } 1852 /* Freeblks is waiting on a inode write. */ 1853 if ((freeblks->fb_state & COMPLETE) == 0) { 1854 FREE_LOCK(ump); 1855 ffs_update(vp, 1); 1856 ACQUIRE_LOCK(ump); 1857 break; 1858 } 1859 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST)) == 1860 (ALLCOMPLETE | ONWORKLIST)) { 1861 remove_from_worklist(&freeblks->fb_list); 1862 freeblks->fb_state |= INPROGRESS; 1863 FREE_LOCK(ump); 1864 if (vn_start_secondary_write(NULL, &mp, 1865 V_NOWAIT)) 1866 panic("process_truncates: " 1867 "suspended filesystem"); 1868 handle_workitem_freeblocks(freeblks, 0); 1869 vn_finished_secondary_write(mp); 1870 ACQUIRE_LOCK(ump); 1871 break; 1872 } 1873 if (freeblks->fb_cgwait) 1874 cgwait++; 1875 } 1876 if (cgwait) { 1877 FREE_LOCK(ump); 1878 sync_cgs(mp, MNT_WAIT); 1879 ffs_sync_snap(mp, MNT_WAIT); 1880 ACQUIRE_LOCK(ump); 1881 continue; 1882 } 1883 if (freeblks == NULL) 1884 break; 1885 } 1886 return; 1887 } 1888 1889 /* 1890 * Process one item on the worklist. 1891 */ 1892 static int 1893 process_worklist_item(struct mount *mp, 1894 int target, 1895 int flags) 1896 { 1897 struct worklist sentinel; 1898 struct worklist *wk; 1899 struct ufsmount *ump; 1900 int matchcnt; 1901 int error; 1902 1903 KASSERT(mp != NULL, ("process_worklist_item: NULL mp")); 1904 /* 1905 * If we are being called because of a process doing a 1906 * copy-on-write, then it is not safe to write as we may 1907 * recurse into the copy-on-write routine. 1908 */ 1909 if (curthread->td_pflags & TDP_COWINPROGRESS) 1910 return (-1); 1911 PHOLD(curproc); /* Don't let the stack go away. */ 1912 ump = VFSTOUFS(mp); 1913 LOCK_OWNED(ump); 1914 matchcnt = 0; 1915 sentinel.wk_mp = NULL; 1916 sentinel.wk_type = D_SENTINEL; 1917 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, &sentinel, wk_list); 1918 for (wk = LIST_NEXT(&sentinel, wk_list); wk != NULL; 1919 wk = LIST_NEXT(&sentinel, wk_list)) { 1920 if (wk->wk_type == D_SENTINEL) { 1921 LIST_REMOVE(&sentinel, wk_list); 1922 LIST_INSERT_AFTER(wk, &sentinel, wk_list); 1923 continue; 1924 } 1925 if (wk->wk_state & INPROGRESS) 1926 panic("process_worklist_item: %p already in progress.", 1927 wk); 1928 wk->wk_state |= INPROGRESS; 1929 remove_from_worklist(wk); 1930 FREE_LOCK(ump); 1931 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1932 panic("process_worklist_item: suspended filesystem"); 1933 switch (wk->wk_type) { 1934 case D_DIRREM: 1935 /* removal of a directory entry */ 1936 error = handle_workitem_remove(WK_DIRREM(wk), flags); 1937 break; 1938 1939 case D_FREEBLKS: 1940 /* releasing blocks and/or fragments from a file */ 1941 error = handle_workitem_freeblocks(WK_FREEBLKS(wk), 1942 flags); 1943 break; 1944 1945 case D_FREEFRAG: 1946 /* releasing a fragment when replaced as a file grows */ 1947 handle_workitem_freefrag(WK_FREEFRAG(wk)); 1948 error = 0; 1949 break; 1950 1951 case D_FREEFILE: 1952 /* releasing an inode when its link count drops to 0 */ 1953 handle_workitem_freefile(WK_FREEFILE(wk)); 1954 error = 0; 1955 break; 1956 1957 default: 1958 panic("%s_process_worklist: Unknown type %s", 1959 "softdep", TYPENAME(wk->wk_type)); 1960 /* NOTREACHED */ 1961 } 1962 vn_finished_secondary_write(mp); 1963 ACQUIRE_LOCK(ump); 1964 if (error == 0) { 1965 if (++matchcnt == target) 1966 break; 1967 continue; 1968 } 1969 /* 1970 * We have to retry the worklist item later. Wake up any 1971 * waiters who may be able to complete it immediately and 1972 * add the item back to the head so we don't try to execute 1973 * it again. 1974 */ 1975 wk->wk_state &= ~INPROGRESS; 1976 wake_worklist(wk); 1977 add_to_worklist(wk, WK_HEAD); 1978 } 1979 /* Sentinal could've become the tail from remove_from_worklist. */ 1980 if (ump->softdep_worklist_tail == &sentinel) 1981 ump->softdep_worklist_tail = 1982 (struct worklist *)sentinel.wk_list.le_prev; 1983 LIST_REMOVE(&sentinel, wk_list); 1984 PRELE(curproc); 1985 return (matchcnt); 1986 } 1987 1988 /* 1989 * Move dependencies from one buffer to another. 1990 */ 1991 int 1992 softdep_move_dependencies(struct buf *oldbp, struct buf *newbp) 1993 { 1994 struct worklist *wk, *wktail; 1995 struct ufsmount *ump; 1996 int dirty; 1997 1998 if ((wk = LIST_FIRST(&oldbp->b_dep)) == NULL) 1999 return (0); 2000 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 2001 ("softdep_move_dependencies called on non-softdep filesystem")); 2002 dirty = 0; 2003 wktail = NULL; 2004 ump = VFSTOUFS(wk->wk_mp); 2005 ACQUIRE_LOCK(ump); 2006 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) { 2007 LIST_REMOVE(wk, wk_list); 2008 if (wk->wk_type == D_BMSAFEMAP && 2009 bmsafemap_backgroundwrite(WK_BMSAFEMAP(wk), newbp)) 2010 dirty = 1; 2011 if (wktail == NULL) 2012 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list); 2013 else 2014 LIST_INSERT_AFTER(wktail, wk, wk_list); 2015 wktail = wk; 2016 } 2017 FREE_LOCK(ump); 2018 2019 return (dirty); 2020 } 2021 2022 /* 2023 * Purge the work list of all items associated with a particular mount point. 2024 */ 2025 int 2026 softdep_flushworklist(struct mount *oldmnt, 2027 int *countp, 2028 struct thread *td) 2029 { 2030 struct vnode *devvp; 2031 struct ufsmount *ump; 2032 int count, error; 2033 2034 /* 2035 * Alternately flush the block device associated with the mount 2036 * point and process any dependencies that the flushing 2037 * creates. We continue until no more worklist dependencies 2038 * are found. 2039 */ 2040 *countp = 0; 2041 error = 0; 2042 ump = VFSTOUFS(oldmnt); 2043 devvp = ump->um_devvp; 2044 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) { 2045 *countp += count; 2046 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 2047 error = VOP_FSYNC(devvp, MNT_WAIT, td); 2048 VOP_UNLOCK(devvp); 2049 if (error != 0) 2050 break; 2051 } 2052 return (error); 2053 } 2054 2055 #define SU_WAITIDLE_RETRIES 20 2056 static int 2057 softdep_waitidle(struct mount *mp, int flags __unused) 2058 { 2059 struct ufsmount *ump; 2060 struct vnode *devvp; 2061 struct thread *td; 2062 int error, i; 2063 2064 ump = VFSTOUFS(mp); 2065 KASSERT(ump->um_softdep != NULL, 2066 ("softdep_waitidle called on non-softdep filesystem")); 2067 devvp = ump->um_devvp; 2068 td = curthread; 2069 error = 0; 2070 ACQUIRE_LOCK(ump); 2071 for (i = 0; i < SU_WAITIDLE_RETRIES && ump->softdep_deps != 0; i++) { 2072 ump->softdep_req = 1; 2073 KASSERT((flags & FORCECLOSE) == 0 || 2074 ump->softdep_on_worklist == 0, 2075 ("softdep_waitidle: work added after flush")); 2076 msleep(&ump->softdep_deps, LOCK_PTR(ump), PVM | PDROP, 2077 "softdeps", 10 * hz); 2078 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 2079 error = VOP_FSYNC(devvp, MNT_WAIT, td); 2080 VOP_UNLOCK(devvp); 2081 ACQUIRE_LOCK(ump); 2082 if (error != 0) 2083 break; 2084 } 2085 ump->softdep_req = 0; 2086 if (i == SU_WAITIDLE_RETRIES && error == 0 && ump->softdep_deps != 0) { 2087 error = EBUSY; 2088 printf("softdep_waitidle: Failed to flush worklist for %p\n", 2089 mp); 2090 } 2091 FREE_LOCK(ump); 2092 return (error); 2093 } 2094 2095 /* 2096 * Flush all vnodes and worklist items associated with a specified mount point. 2097 */ 2098 int 2099 softdep_flushfiles(struct mount *oldmnt, 2100 int flags, 2101 struct thread *td) 2102 { 2103 struct ufsmount *ump __unused; 2104 #ifdef QUOTA 2105 int i; 2106 #endif 2107 int error, early, depcount, loopcnt, retry_flush_count, retry; 2108 int morework; 2109 2110 ump = VFSTOUFS(oldmnt); 2111 KASSERT(ump->um_softdep != NULL, 2112 ("softdep_flushfiles called on non-softdep filesystem")); 2113 loopcnt = 10; 2114 retry_flush_count = 3; 2115 retry_flush: 2116 error = 0; 2117 2118 /* 2119 * Alternately flush the vnodes associated with the mount 2120 * point and process any dependencies that the flushing 2121 * creates. In theory, this loop can happen at most twice, 2122 * but we give it a few extra just to be sure. 2123 */ 2124 for (; loopcnt > 0; loopcnt--) { 2125 /* 2126 * Do another flush in case any vnodes were brought in 2127 * as part of the cleanup operations. 2128 */ 2129 early = retry_flush_count == 1 || (oldmnt->mnt_kern_flag & 2130 MNTK_UNMOUNT) == 0 ? 0 : EARLYFLUSH; 2131 if ((error = ffs_flushfiles(oldmnt, flags | early, td)) != 0) 2132 break; 2133 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 || 2134 depcount == 0) 2135 break; 2136 } 2137 /* 2138 * If we are unmounting then it is an error to fail. If we 2139 * are simply trying to downgrade to read-only, then filesystem 2140 * activity can keep us busy forever, so we just fail with EBUSY. 2141 */ 2142 if (loopcnt == 0) { 2143 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) 2144 panic("softdep_flushfiles: looping"); 2145 error = EBUSY; 2146 } 2147 if (!error) 2148 error = softdep_waitidle(oldmnt, flags); 2149 if (!error) { 2150 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) { 2151 retry = 0; 2152 MNT_ILOCK(oldmnt); 2153 morework = oldmnt->mnt_nvnodelistsize > 0; 2154 #ifdef QUOTA 2155 UFS_LOCK(ump); 2156 for (i = 0; i < MAXQUOTAS; i++) { 2157 if (ump->um_quotas[i] != NULLVP) 2158 morework = 1; 2159 } 2160 UFS_UNLOCK(ump); 2161 #endif 2162 if (morework) { 2163 if (--retry_flush_count > 0) { 2164 retry = 1; 2165 loopcnt = 3; 2166 } else 2167 error = EBUSY; 2168 } 2169 MNT_IUNLOCK(oldmnt); 2170 if (retry) 2171 goto retry_flush; 2172 } 2173 } 2174 return (error); 2175 } 2176 2177 /* 2178 * Structure hashing. 2179 * 2180 * There are four types of structures that can be looked up: 2181 * 1) pagedep structures identified by mount point, inode number, 2182 * and logical block. 2183 * 2) inodedep structures identified by mount point and inode number. 2184 * 3) newblk structures identified by mount point and 2185 * physical block number. 2186 * 4) bmsafemap structures identified by mount point and 2187 * cylinder group number. 2188 * 2189 * The "pagedep" and "inodedep" dependency structures are hashed 2190 * separately from the file blocks and inodes to which they correspond. 2191 * This separation helps when the in-memory copy of an inode or 2192 * file block must be replaced. It also obviates the need to access 2193 * an inode or file page when simply updating (or de-allocating) 2194 * dependency structures. Lookup of newblk structures is needed to 2195 * find newly allocated blocks when trying to associate them with 2196 * their allocdirect or allocindir structure. 2197 * 2198 * The lookup routines optionally create and hash a new instance when 2199 * an existing entry is not found. The bmsafemap lookup routine always 2200 * allocates a new structure if an existing one is not found. 2201 */ 2202 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */ 2203 2204 /* 2205 * Structures and routines associated with pagedep caching. 2206 */ 2207 #define PAGEDEP_HASH(ump, inum, lbn) \ 2208 (&(ump)->pagedep_hashtbl[((inum) + (lbn)) & (ump)->pagedep_hash_size]) 2209 2210 static int 2211 pagedep_find(struct pagedep_hashhead *pagedephd, 2212 ino_t ino, 2213 ufs_lbn_t lbn, 2214 struct pagedep **pagedeppp) 2215 { 2216 struct pagedep *pagedep; 2217 2218 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 2219 if (ino == pagedep->pd_ino && lbn == pagedep->pd_lbn) { 2220 *pagedeppp = pagedep; 2221 return (1); 2222 } 2223 } 2224 *pagedeppp = NULL; 2225 return (0); 2226 } 2227 /* 2228 * Look up a pagedep. Return 1 if found, 0 otherwise. 2229 * If not found, allocate if DEPALLOC flag is passed. 2230 * Found or allocated entry is returned in pagedeppp. 2231 */ 2232 static int 2233 pagedep_lookup(struct mount *mp, 2234 struct buf *bp, 2235 ino_t ino, 2236 ufs_lbn_t lbn, 2237 int flags, 2238 struct pagedep **pagedeppp) 2239 { 2240 struct pagedep *pagedep; 2241 struct pagedep_hashhead *pagedephd; 2242 struct worklist *wk; 2243 struct ufsmount *ump; 2244 int ret; 2245 int i; 2246 2247 ump = VFSTOUFS(mp); 2248 LOCK_OWNED(ump); 2249 if (bp) { 2250 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 2251 if (wk->wk_type == D_PAGEDEP) { 2252 *pagedeppp = WK_PAGEDEP(wk); 2253 return (1); 2254 } 2255 } 2256 } 2257 pagedephd = PAGEDEP_HASH(ump, ino, lbn); 2258 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp); 2259 if (ret) { 2260 if (((*pagedeppp)->pd_state & ONWORKLIST) == 0 && bp) 2261 WORKLIST_INSERT(&bp->b_dep, &(*pagedeppp)->pd_list); 2262 return (1); 2263 } 2264 if ((flags & DEPALLOC) == 0) 2265 return (0); 2266 FREE_LOCK(ump); 2267 pagedep = malloc(sizeof(struct pagedep), 2268 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO); 2269 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp); 2270 ACQUIRE_LOCK(ump); 2271 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp); 2272 if (*pagedeppp) { 2273 /* 2274 * This should never happen since we only create pagedeps 2275 * with the vnode lock held. Could be an assert. 2276 */ 2277 WORKITEM_FREE(pagedep, D_PAGEDEP); 2278 return (ret); 2279 } 2280 pagedep->pd_ino = ino; 2281 pagedep->pd_lbn = lbn; 2282 LIST_INIT(&pagedep->pd_dirremhd); 2283 LIST_INIT(&pagedep->pd_pendinghd); 2284 for (i = 0; i < DAHASHSZ; i++) 2285 LIST_INIT(&pagedep->pd_diraddhd[i]); 2286 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash); 2287 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list); 2288 *pagedeppp = pagedep; 2289 return (0); 2290 } 2291 2292 /* 2293 * Structures and routines associated with inodedep caching. 2294 */ 2295 #define INODEDEP_HASH(ump, inum) \ 2296 (&(ump)->inodedep_hashtbl[(inum) & (ump)->inodedep_hash_size]) 2297 2298 static int 2299 inodedep_find(struct inodedep_hashhead *inodedephd, 2300 ino_t inum, 2301 struct inodedep **inodedeppp) 2302 { 2303 struct inodedep *inodedep; 2304 2305 LIST_FOREACH(inodedep, inodedephd, id_hash) 2306 if (inum == inodedep->id_ino) 2307 break; 2308 if (inodedep) { 2309 *inodedeppp = inodedep; 2310 return (1); 2311 } 2312 *inodedeppp = NULL; 2313 2314 return (0); 2315 } 2316 /* 2317 * Look up an inodedep. Return 1 if found, 0 if not found. 2318 * If not found, allocate if DEPALLOC flag is passed. 2319 * Found or allocated entry is returned in inodedeppp. 2320 */ 2321 static int 2322 inodedep_lookup(struct mount *mp, 2323 ino_t inum, 2324 int flags, 2325 struct inodedep **inodedeppp) 2326 { 2327 struct inodedep *inodedep; 2328 struct inodedep_hashhead *inodedephd; 2329 struct ufsmount *ump; 2330 struct fs *fs; 2331 2332 ump = VFSTOUFS(mp); 2333 LOCK_OWNED(ump); 2334 fs = ump->um_fs; 2335 inodedephd = INODEDEP_HASH(ump, inum); 2336 2337 if (inodedep_find(inodedephd, inum, inodedeppp)) 2338 return (1); 2339 if ((flags & DEPALLOC) == 0) 2340 return (0); 2341 /* 2342 * If the system is over its limit and our filesystem is 2343 * responsible for more than our share of that usage and 2344 * we are not in a rush, request some inodedep cleanup. 2345 */ 2346 if (softdep_excess_items(ump, D_INODEDEP)) 2347 schedule_cleanup(mp); 2348 else 2349 FREE_LOCK(ump); 2350 inodedep = malloc(sizeof(struct inodedep), 2351 M_INODEDEP, M_SOFTDEP_FLAGS); 2352 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp); 2353 ACQUIRE_LOCK(ump); 2354 if (inodedep_find(inodedephd, inum, inodedeppp)) { 2355 WORKITEM_FREE(inodedep, D_INODEDEP); 2356 return (1); 2357 } 2358 inodedep->id_fs = fs; 2359 inodedep->id_ino = inum; 2360 inodedep->id_state = ALLCOMPLETE; 2361 inodedep->id_nlinkdelta = 0; 2362 inodedep->id_nlinkwrote = -1; 2363 inodedep->id_savedino1 = NULL; 2364 inodedep->id_savedsize = -1; 2365 inodedep->id_savedextsize = -1; 2366 inodedep->id_savednlink = -1; 2367 inodedep->id_bmsafemap = NULL; 2368 inodedep->id_mkdiradd = NULL; 2369 LIST_INIT(&inodedep->id_dirremhd); 2370 LIST_INIT(&inodedep->id_pendinghd); 2371 LIST_INIT(&inodedep->id_inowait); 2372 LIST_INIT(&inodedep->id_bufwait); 2373 TAILQ_INIT(&inodedep->id_inoreflst); 2374 TAILQ_INIT(&inodedep->id_inoupdt); 2375 TAILQ_INIT(&inodedep->id_newinoupdt); 2376 TAILQ_INIT(&inodedep->id_extupdt); 2377 TAILQ_INIT(&inodedep->id_newextupdt); 2378 TAILQ_INIT(&inodedep->id_freeblklst); 2379 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash); 2380 *inodedeppp = inodedep; 2381 return (0); 2382 } 2383 2384 /* 2385 * Structures and routines associated with newblk caching. 2386 */ 2387 #define NEWBLK_HASH(ump, inum) \ 2388 (&(ump)->newblk_hashtbl[(inum) & (ump)->newblk_hash_size]) 2389 2390 static int 2391 newblk_find(struct newblk_hashhead *newblkhd, 2392 ufs2_daddr_t newblkno, 2393 int flags, 2394 struct newblk **newblkpp) 2395 { 2396 struct newblk *newblk; 2397 2398 LIST_FOREACH(newblk, newblkhd, nb_hash) { 2399 if (newblkno != newblk->nb_newblkno) 2400 continue; 2401 /* 2402 * If we're creating a new dependency don't match those that 2403 * have already been converted to allocdirects. This is for 2404 * a frag extend. 2405 */ 2406 if ((flags & DEPALLOC) && newblk->nb_list.wk_type != D_NEWBLK) 2407 continue; 2408 break; 2409 } 2410 if (newblk) { 2411 *newblkpp = newblk; 2412 return (1); 2413 } 2414 *newblkpp = NULL; 2415 return (0); 2416 } 2417 2418 /* 2419 * Look up a newblk. Return 1 if found, 0 if not found. 2420 * If not found, allocate if DEPALLOC flag is passed. 2421 * Found or allocated entry is returned in newblkpp. 2422 */ 2423 static int 2424 newblk_lookup(struct mount *mp, 2425 ufs2_daddr_t newblkno, 2426 int flags, 2427 struct newblk **newblkpp) 2428 { 2429 struct newblk *newblk; 2430 struct newblk_hashhead *newblkhd; 2431 struct ufsmount *ump; 2432 2433 ump = VFSTOUFS(mp); 2434 LOCK_OWNED(ump); 2435 newblkhd = NEWBLK_HASH(ump, newblkno); 2436 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) 2437 return (1); 2438 if ((flags & DEPALLOC) == 0) 2439 return (0); 2440 if (softdep_excess_items(ump, D_NEWBLK) || 2441 softdep_excess_items(ump, D_ALLOCDIRECT) || 2442 softdep_excess_items(ump, D_ALLOCINDIR)) 2443 schedule_cleanup(mp); 2444 else 2445 FREE_LOCK(ump); 2446 newblk = malloc(sizeof(union allblk), M_NEWBLK, 2447 M_SOFTDEP_FLAGS | M_ZERO); 2448 workitem_alloc(&newblk->nb_list, D_NEWBLK, mp); 2449 ACQUIRE_LOCK(ump); 2450 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) { 2451 WORKITEM_FREE(newblk, D_NEWBLK); 2452 return (1); 2453 } 2454 newblk->nb_freefrag = NULL; 2455 LIST_INIT(&newblk->nb_indirdeps); 2456 LIST_INIT(&newblk->nb_newdirblk); 2457 LIST_INIT(&newblk->nb_jwork); 2458 newblk->nb_state = ATTACHED; 2459 newblk->nb_newblkno = newblkno; 2460 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash); 2461 *newblkpp = newblk; 2462 return (0); 2463 } 2464 2465 /* 2466 * Structures and routines associated with freed indirect block caching. 2467 */ 2468 #define INDIR_HASH(ump, blkno) \ 2469 (&(ump)->indir_hashtbl[(blkno) & (ump)->indir_hash_size]) 2470 2471 /* 2472 * Lookup an indirect block in the indir hash table. The freework is 2473 * removed and potentially freed. The caller must do a blocking journal 2474 * write before writing to the blkno. 2475 */ 2476 static int 2477 indirblk_lookup(struct mount *mp, ufs2_daddr_t blkno) 2478 { 2479 struct freework *freework; 2480 struct indir_hashhead *wkhd; 2481 struct ufsmount *ump; 2482 2483 ump = VFSTOUFS(mp); 2484 wkhd = INDIR_HASH(ump, blkno); 2485 TAILQ_FOREACH(freework, wkhd, fw_next) { 2486 if (freework->fw_blkno != blkno) 2487 continue; 2488 indirblk_remove(freework); 2489 return (1); 2490 } 2491 return (0); 2492 } 2493 2494 /* 2495 * Insert an indirect block represented by freework into the indirblk 2496 * hash table so that it may prevent the block from being re-used prior 2497 * to the journal being written. 2498 */ 2499 static void 2500 indirblk_insert(struct freework *freework) 2501 { 2502 struct jblocks *jblocks; 2503 struct jseg *jseg; 2504 struct ufsmount *ump; 2505 2506 ump = VFSTOUFS(freework->fw_list.wk_mp); 2507 jblocks = ump->softdep_jblocks; 2508 jseg = TAILQ_LAST(&jblocks->jb_segs, jseglst); 2509 if (jseg == NULL) 2510 return; 2511 2512 LIST_INSERT_HEAD(&jseg->js_indirs, freework, fw_segs); 2513 TAILQ_INSERT_HEAD(INDIR_HASH(ump, freework->fw_blkno), freework, 2514 fw_next); 2515 freework->fw_state &= ~DEPCOMPLETE; 2516 } 2517 2518 static void 2519 indirblk_remove(struct freework *freework) 2520 { 2521 struct ufsmount *ump; 2522 2523 ump = VFSTOUFS(freework->fw_list.wk_mp); 2524 LIST_REMOVE(freework, fw_segs); 2525 TAILQ_REMOVE(INDIR_HASH(ump, freework->fw_blkno), freework, fw_next); 2526 freework->fw_state |= DEPCOMPLETE; 2527 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 2528 WORKITEM_FREE(freework, D_FREEWORK); 2529 } 2530 2531 /* 2532 * Executed during filesystem system initialization before 2533 * mounting any filesystems. 2534 */ 2535 void 2536 softdep_initialize(void) 2537 { 2538 2539 TAILQ_INIT(&softdepmounts); 2540 #ifdef __LP64__ 2541 max_softdeps = desiredvnodes * 4; 2542 #else 2543 max_softdeps = desiredvnodes * 2; 2544 #endif 2545 2546 /* initialise bioops hack */ 2547 bioops.io_start = softdep_disk_io_initiation; 2548 bioops.io_complete = softdep_disk_write_complete; 2549 bioops.io_deallocate = softdep_deallocate_dependencies; 2550 bioops.io_countdeps = softdep_count_dependencies; 2551 ast_register(TDA_UFS, ASTR_KCLEAR | ASTR_ASTF_REQUIRED, 0, 2552 softdep_ast_cleanup_proc); 2553 2554 /* Initialize the callout with an mtx. */ 2555 callout_init_mtx(&softdep_callout, &lk, 0); 2556 } 2557 2558 /* 2559 * Executed after all filesystems have been unmounted during 2560 * filesystem module unload. 2561 */ 2562 void 2563 softdep_uninitialize(void) 2564 { 2565 2566 /* clear bioops hack */ 2567 bioops.io_start = NULL; 2568 bioops.io_complete = NULL; 2569 bioops.io_deallocate = NULL; 2570 bioops.io_countdeps = NULL; 2571 ast_deregister(TDA_UFS); 2572 2573 callout_drain(&softdep_callout); 2574 } 2575 2576 /* 2577 * Called at mount time to notify the dependency code that a 2578 * filesystem wishes to use it. 2579 */ 2580 int 2581 softdep_mount(struct vnode *devvp, 2582 struct mount *mp, 2583 struct fs *fs, 2584 struct ucred *cred) 2585 { 2586 struct csum_total cstotal; 2587 struct mount_softdeps *sdp; 2588 struct ufsmount *ump; 2589 struct cg *cgp; 2590 struct buf *bp; 2591 uint64_t cyl, i; 2592 int error; 2593 2594 ump = VFSTOUFS(mp); 2595 2596 sdp = malloc(sizeof(struct mount_softdeps), M_MOUNTDATA, 2597 M_WAITOK | M_ZERO); 2598 rw_init(&sdp->sd_fslock, "SUrw"); 2599 sdp->sd_ump = ump; 2600 LIST_INIT(&sdp->sd_workitem_pending); 2601 LIST_INIT(&sdp->sd_journal_pending); 2602 TAILQ_INIT(&sdp->sd_unlinked); 2603 LIST_INIT(&sdp->sd_dirtycg); 2604 sdp->sd_worklist_tail = NULL; 2605 sdp->sd_on_worklist = 0; 2606 sdp->sd_deps = 0; 2607 LIST_INIT(&sdp->sd_mkdirlisthd); 2608 sdp->sd_pdhash = hashinit(desiredvnodes / 5, M_PAGEDEP, 2609 &sdp->sd_pdhashsize); 2610 sdp->sd_pdnextclean = 0; 2611 sdp->sd_idhash = hashinit(desiredvnodes, M_INODEDEP, 2612 &sdp->sd_idhashsize); 2613 sdp->sd_idnextclean = 0; 2614 sdp->sd_newblkhash = hashinit(max_softdeps / 2, M_NEWBLK, 2615 &sdp->sd_newblkhashsize); 2616 sdp->sd_bmhash = hashinit(1024, M_BMSAFEMAP, &sdp->sd_bmhashsize); 2617 i = 1 << (ffs(desiredvnodes / 10) - 1); 2618 sdp->sd_indirhash = malloc(i * sizeof(struct indir_hashhead), 2619 M_FREEWORK, M_WAITOK); 2620 sdp->sd_indirhashsize = i - 1; 2621 for (i = 0; i <= sdp->sd_indirhashsize; i++) 2622 TAILQ_INIT(&sdp->sd_indirhash[i]); 2623 for (i = 0; i <= D_LAST; i++) 2624 LIST_INIT(&sdp->sd_alldeps[i]); 2625 ACQUIRE_GBLLOCK(&lk); 2626 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next); 2627 FREE_GBLLOCK(&lk); 2628 2629 ump->um_softdep = sdp; 2630 MNT_ILOCK(mp); 2631 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP; 2632 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) { 2633 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) | 2634 MNTK_SOFTDEP | MNTK_NOASYNC; 2635 } 2636 MNT_IUNLOCK(mp); 2637 2638 if ((fs->fs_flags & FS_SUJ) && 2639 (error = journal_mount(mp, fs, cred)) != 0) { 2640 printf("Failed to start journal: %d\n", error); 2641 softdep_unmount(mp); 2642 return (error); 2643 } 2644 /* 2645 * Start our flushing thread in the bufdaemon process. 2646 */ 2647 ACQUIRE_LOCK(ump); 2648 ump->softdep_flags |= FLUSH_STARTING; 2649 FREE_LOCK(ump); 2650 kproc_kthread_add(&softdep_flush, mp, &bufdaemonproc, 2651 &ump->softdep_flushtd, 0, 0, "softdepflush", "%s worker", 2652 mp->mnt_stat.f_mntonname); 2653 ACQUIRE_LOCK(ump); 2654 while ((ump->softdep_flags & FLUSH_STARTING) != 0) { 2655 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, "sdstart", 2656 hz / 2); 2657 } 2658 FREE_LOCK(ump); 2659 /* 2660 * When doing soft updates, the counters in the 2661 * superblock may have gotten out of sync. Recomputation 2662 * can take a long time and can be deferred for background 2663 * fsck. However, the old behavior of scanning the cylinder 2664 * groups and recalculating them at mount time is available 2665 * by setting vfs.ffs.compute_summary_at_mount to one. 2666 */ 2667 if (compute_summary_at_mount == 0 || fs->fs_clean != 0) 2668 return (0); 2669 bzero(&cstotal, sizeof cstotal); 2670 for (cyl = 0; cyl < fs->fs_ncg; cyl++) { 2671 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)), 2672 fs->fs_cgsize, cred, &bp)) != 0) { 2673 brelse(bp); 2674 softdep_unmount(mp); 2675 return (error); 2676 } 2677 cgp = (struct cg *)bp->b_data; 2678 cstotal.cs_nffree += cgp->cg_cs.cs_nffree; 2679 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree; 2680 cstotal.cs_nifree += cgp->cg_cs.cs_nifree; 2681 cstotal.cs_ndir += cgp->cg_cs.cs_ndir; 2682 fs->fs_cs(fs, cyl) = cgp->cg_cs; 2683 brelse(bp); 2684 } 2685 #ifdef INVARIANTS 2686 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal)) 2687 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt); 2688 #endif 2689 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal); 2690 return (0); 2691 } 2692 2693 void 2694 softdep_unmount(struct mount *mp) 2695 { 2696 struct ufsmount *ump; 2697 struct mount_softdeps *ums; 2698 2699 ump = VFSTOUFS(mp); 2700 KASSERT(ump->um_softdep != NULL, 2701 ("softdep_unmount called on non-softdep filesystem")); 2702 MNT_ILOCK(mp); 2703 mp->mnt_flag &= ~MNT_SOFTDEP; 2704 if ((mp->mnt_flag & MNT_SUJ) == 0) { 2705 MNT_IUNLOCK(mp); 2706 } else { 2707 mp->mnt_flag &= ~MNT_SUJ; 2708 MNT_IUNLOCK(mp); 2709 journal_unmount(ump); 2710 } 2711 /* 2712 * Shut down our flushing thread. Check for NULL is if 2713 * softdep_mount errors out before the thread has been created. 2714 */ 2715 if (ump->softdep_flushtd != NULL) { 2716 ACQUIRE_LOCK(ump); 2717 ump->softdep_flags |= FLUSH_EXIT; 2718 wakeup(&ump->softdep_flushtd); 2719 while ((ump->softdep_flags & FLUSH_EXIT) != 0) { 2720 msleep(&ump->softdep_flags, LOCK_PTR(ump), PVM, 2721 "sdwait", 0); 2722 } 2723 KASSERT((ump->softdep_flags & FLUSH_EXIT) == 0, 2724 ("Thread shutdown failed")); 2725 FREE_LOCK(ump); 2726 } 2727 2728 /* 2729 * We are no longer have softdep structure attached to ump. 2730 */ 2731 ums = ump->um_softdep; 2732 ACQUIRE_GBLLOCK(&lk); 2733 TAILQ_REMOVE(&softdepmounts, ums, sd_next); 2734 FREE_GBLLOCK(&lk); 2735 ump->um_softdep = NULL; 2736 2737 KASSERT(ums->sd_on_journal == 0, 2738 ("ump %p ums %p on_journal %d", ump, ums, ums->sd_on_journal)); 2739 KASSERT(ums->sd_on_worklist == 0, 2740 ("ump %p ums %p on_worklist %d", ump, ums, ums->sd_on_worklist)); 2741 KASSERT(ums->sd_deps == 0, 2742 ("ump %p ums %p deps %d", ump, ums, ums->sd_deps)); 2743 2744 /* 2745 * Free up our resources. 2746 */ 2747 rw_destroy(&ums->sd_fslock); 2748 hashdestroy(ums->sd_pdhash, M_PAGEDEP, ums->sd_pdhashsize); 2749 hashdestroy(ums->sd_idhash, M_INODEDEP, ums->sd_idhashsize); 2750 hashdestroy(ums->sd_newblkhash, M_NEWBLK, ums->sd_newblkhashsize); 2751 hashdestroy(ums->sd_bmhash, M_BMSAFEMAP, ums->sd_bmhashsize); 2752 free(ums->sd_indirhash, M_FREEWORK); 2753 #ifdef INVARIANTS 2754 for (int i = 0; i <= D_LAST; i++) { 2755 KASSERT(ums->sd_curdeps[i] == 0, 2756 ("Unmount %s: Dep type %s != 0 (%jd)", ump->um_fs->fs_fsmnt, 2757 TYPENAME(i), (intmax_t)ums->sd_curdeps[i])); 2758 KASSERT(LIST_EMPTY(&ums->sd_alldeps[i]), 2759 ("Unmount %s: Dep type %s not empty (%p)", 2760 ump->um_fs->fs_fsmnt, 2761 TYPENAME(i), LIST_FIRST(&ums->sd_alldeps[i]))); 2762 } 2763 #endif 2764 free(ums, M_MOUNTDATA); 2765 } 2766 2767 static struct jblocks * 2768 jblocks_create(void) 2769 { 2770 struct jblocks *jblocks; 2771 2772 jblocks = malloc(sizeof(*jblocks), M_JBLOCKS, M_WAITOK | M_ZERO); 2773 TAILQ_INIT(&jblocks->jb_segs); 2774 jblocks->jb_avail = 10; 2775 jblocks->jb_extent = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2776 M_JBLOCKS, M_WAITOK | M_ZERO); 2777 2778 return (jblocks); 2779 } 2780 2781 static ufs2_daddr_t 2782 jblocks_alloc(struct jblocks *jblocks, 2783 int bytes, 2784 int *actual) 2785 { 2786 ufs2_daddr_t daddr; 2787 struct jextent *jext; 2788 int freecnt; 2789 int blocks; 2790 2791 blocks = bytes / DEV_BSIZE; 2792 jext = &jblocks->jb_extent[jblocks->jb_head]; 2793 freecnt = jext->je_blocks - jblocks->jb_off; 2794 if (freecnt == 0) { 2795 jblocks->jb_off = 0; 2796 if (++jblocks->jb_head > jblocks->jb_used) 2797 jblocks->jb_head = 0; 2798 jext = &jblocks->jb_extent[jblocks->jb_head]; 2799 freecnt = jext->je_blocks; 2800 } 2801 if (freecnt > blocks) 2802 freecnt = blocks; 2803 *actual = freecnt * DEV_BSIZE; 2804 daddr = jext->je_daddr + jblocks->jb_off; 2805 jblocks->jb_off += freecnt; 2806 jblocks->jb_free -= freecnt; 2807 2808 return (daddr); 2809 } 2810 2811 static void 2812 jblocks_free(struct jblocks *jblocks, 2813 struct mount *mp, 2814 int bytes) 2815 { 2816 2817 LOCK_OWNED(VFSTOUFS(mp)); 2818 jblocks->jb_free += bytes / DEV_BSIZE; 2819 if (jblocks->jb_suspended) 2820 worklist_speedup(mp); 2821 wakeup(jblocks); 2822 } 2823 2824 static void 2825 jblocks_destroy(struct jblocks *jblocks) 2826 { 2827 2828 if (jblocks->jb_extent) 2829 free(jblocks->jb_extent, M_JBLOCKS); 2830 free(jblocks, M_JBLOCKS); 2831 } 2832 2833 static void 2834 jblocks_add(struct jblocks *jblocks, 2835 ufs2_daddr_t daddr, 2836 int blocks) 2837 { 2838 struct jextent *jext; 2839 2840 jblocks->jb_blocks += blocks; 2841 jblocks->jb_free += blocks; 2842 jext = &jblocks->jb_extent[jblocks->jb_used]; 2843 /* Adding the first block. */ 2844 if (jext->je_daddr == 0) { 2845 jext->je_daddr = daddr; 2846 jext->je_blocks = blocks; 2847 return; 2848 } 2849 /* Extending the last extent. */ 2850 if (jext->je_daddr + jext->je_blocks == daddr) { 2851 jext->je_blocks += blocks; 2852 return; 2853 } 2854 /* Adding a new extent. */ 2855 if (++jblocks->jb_used == jblocks->jb_avail) { 2856 jblocks->jb_avail *= 2; 2857 jext = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2858 M_JBLOCKS, M_WAITOK | M_ZERO); 2859 memcpy(jext, jblocks->jb_extent, 2860 sizeof(struct jextent) * jblocks->jb_used); 2861 free(jblocks->jb_extent, M_JBLOCKS); 2862 jblocks->jb_extent = jext; 2863 } 2864 jext = &jblocks->jb_extent[jblocks->jb_used]; 2865 jext->je_daddr = daddr; 2866 jext->je_blocks = blocks; 2867 return; 2868 } 2869 2870 int 2871 softdep_journal_lookup(struct mount *mp, struct vnode **vpp) 2872 { 2873 struct componentname cnp; 2874 struct vnode *dvp; 2875 ino_t sujournal; 2876 int error; 2877 2878 error = VFS_VGET(mp, UFS_ROOTINO, LK_EXCLUSIVE, &dvp); 2879 if (error) 2880 return (error); 2881 bzero(&cnp, sizeof(cnp)); 2882 cnp.cn_nameiop = LOOKUP; 2883 cnp.cn_flags = ISLASTCN; 2884 cnp.cn_cred = curthread->td_ucred; 2885 cnp.cn_pnbuf = SUJ_FILE; 2886 cnp.cn_nameptr = SUJ_FILE; 2887 cnp.cn_namelen = strlen(SUJ_FILE); 2888 error = ufs_lookup_ino(dvp, NULL, &cnp, &sujournal); 2889 vput(dvp); 2890 if (error != 0) 2891 return (error); 2892 error = VFS_VGET(mp, sujournal, LK_EXCLUSIVE, vpp); 2893 return (error); 2894 } 2895 2896 /* 2897 * Open and verify the journal file. 2898 */ 2899 static int 2900 journal_mount(struct mount *mp, 2901 struct fs *fs, 2902 struct ucred *cred) 2903 { 2904 struct jblocks *jblocks; 2905 struct ufsmount *ump; 2906 struct vnode *vp; 2907 struct inode *ip; 2908 ufs2_daddr_t blkno; 2909 int bcount; 2910 int error; 2911 int i; 2912 2913 ump = VFSTOUFS(mp); 2914 ump->softdep_journal_tail = NULL; 2915 ump->softdep_on_journal = 0; 2916 ump->softdep_accdeps = 0; 2917 ump->softdep_req = 0; 2918 ump->softdep_jblocks = NULL; 2919 error = softdep_journal_lookup(mp, &vp); 2920 if (error != 0) { 2921 printf("Failed to find journal. Use tunefs to create one\n"); 2922 return (error); 2923 } 2924 ip = VTOI(vp); 2925 if (ip->i_size < SUJ_MIN) { 2926 error = ENOSPC; 2927 goto out; 2928 } 2929 bcount = lblkno(fs, ip->i_size); /* Only use whole blocks. */ 2930 jblocks = jblocks_create(); 2931 for (i = 0; i < bcount; i++) { 2932 error = ufs_bmaparray(vp, i, &blkno, NULL, NULL, NULL); 2933 if (error) 2934 break; 2935 jblocks_add(jblocks, blkno, fsbtodb(fs, fs->fs_frag)); 2936 } 2937 if (error) { 2938 jblocks_destroy(jblocks); 2939 goto out; 2940 } 2941 jblocks->jb_low = jblocks->jb_free / 3; /* Reserve 33%. */ 2942 jblocks->jb_min = jblocks->jb_free / 10; /* Suspend at 10%. */ 2943 ump->softdep_jblocks = jblocks; 2944 2945 MNT_ILOCK(mp); 2946 mp->mnt_flag |= MNT_SUJ; 2947 MNT_IUNLOCK(mp); 2948 2949 /* 2950 * Only validate the journal contents if the 2951 * filesystem is clean, otherwise we write the logs 2952 * but they'll never be used. If the filesystem was 2953 * still dirty when we mounted it the journal is 2954 * invalid and a new journal can only be valid if it 2955 * starts from a clean mount. 2956 */ 2957 if (fs->fs_clean) { 2958 DIP_SET(ip, i_modrev, fs->fs_mtime); 2959 ip->i_flags |= IN_MODIFIED; 2960 ffs_update(vp, 1); 2961 } 2962 out: 2963 vput(vp); 2964 return (error); 2965 } 2966 2967 static void 2968 journal_unmount(struct ufsmount *ump) 2969 { 2970 2971 if (ump->softdep_jblocks) 2972 jblocks_destroy(ump->softdep_jblocks); 2973 ump->softdep_jblocks = NULL; 2974 } 2975 2976 /* 2977 * Called when a journal record is ready to be written. Space is allocated 2978 * and the journal entry is created when the journal is flushed to stable 2979 * store. 2980 */ 2981 static void 2982 add_to_journal(struct worklist *wk) 2983 { 2984 struct ufsmount *ump; 2985 2986 ump = VFSTOUFS(wk->wk_mp); 2987 LOCK_OWNED(ump); 2988 if (wk->wk_state & ONWORKLIST) 2989 panic("add_to_journal: %s(0x%X) already on list", 2990 TYPENAME(wk->wk_type), wk->wk_state); 2991 wk->wk_state |= ONWORKLIST | DEPCOMPLETE; 2992 if (LIST_EMPTY(&ump->softdep_journal_pending)) { 2993 ump->softdep_jblocks->jb_age = ticks; 2994 LIST_INSERT_HEAD(&ump->softdep_journal_pending, wk, wk_list); 2995 } else 2996 LIST_INSERT_AFTER(ump->softdep_journal_tail, wk, wk_list); 2997 ump->softdep_journal_tail = wk; 2998 ump->softdep_on_journal += 1; 2999 } 3000 3001 /* 3002 * Remove an arbitrary item for the journal worklist maintain the tail 3003 * pointer. This happens when a new operation obviates the need to 3004 * journal an old operation. 3005 */ 3006 static void 3007 remove_from_journal(struct worklist *wk) 3008 { 3009 struct ufsmount *ump; 3010 3011 ump = VFSTOUFS(wk->wk_mp); 3012 LOCK_OWNED(ump); 3013 #ifdef INVARIANTS 3014 { 3015 struct worklist *wkn; 3016 3017 LIST_FOREACH(wkn, &ump->softdep_journal_pending, wk_list) 3018 if (wkn == wk) 3019 break; 3020 if (wkn == NULL) 3021 panic("remove_from_journal: %p is not in journal", wk); 3022 } 3023 #endif 3024 /* 3025 * We emulate a TAILQ to save space in most structures which do not 3026 * require TAILQ semantics. Here we must update the tail position 3027 * when removing the tail which is not the final entry. This works 3028 * only if the worklist linkage are at the beginning of the structure. 3029 */ 3030 if (ump->softdep_journal_tail == wk) 3031 ump->softdep_journal_tail = 3032 (struct worklist *)wk->wk_list.le_prev; 3033 WORKLIST_REMOVE(wk); 3034 ump->softdep_on_journal -= 1; 3035 } 3036 3037 /* 3038 * Check for journal space as well as dependency limits so the prelink 3039 * code can throttle both journaled and non-journaled filesystems. 3040 * Threshold is 0 for low and 1 for min. 3041 */ 3042 static int 3043 journal_space(struct ufsmount *ump, int thresh) 3044 { 3045 struct jblocks *jblocks; 3046 int limit, avail; 3047 3048 jblocks = ump->softdep_jblocks; 3049 if (jblocks == NULL) 3050 return (1); 3051 /* 3052 * We use a tighter restriction here to prevent request_cleanup() 3053 * running in threads from running into locks we currently hold. 3054 * We have to be over the limit and our filesystem has to be 3055 * responsible for more than our share of that usage. 3056 */ 3057 limit = (max_softdeps / 10) * 9; 3058 if (dep_current[D_INODEDEP] > limit && 3059 ump->softdep_curdeps[D_INODEDEP] > limit / stat_flush_threads) 3060 return (0); 3061 if (thresh) 3062 thresh = jblocks->jb_min; 3063 else 3064 thresh = jblocks->jb_low; 3065 avail = (ump->softdep_on_journal * JREC_SIZE) / DEV_BSIZE; 3066 avail = jblocks->jb_free - avail; 3067 3068 return (avail > thresh); 3069 } 3070 3071 static void 3072 journal_suspend(struct ufsmount *ump) 3073 { 3074 struct jblocks *jblocks; 3075 struct mount *mp; 3076 bool set; 3077 3078 mp = UFSTOVFS(ump); 3079 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) 3080 return; 3081 3082 jblocks = ump->softdep_jblocks; 3083 vfs_op_enter(mp); 3084 set = false; 3085 MNT_ILOCK(mp); 3086 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) { 3087 stat_journal_min++; 3088 mp->mnt_kern_flag |= MNTK_SUSPEND; 3089 mp->mnt_susp_owner = ump->softdep_flushtd; 3090 set = true; 3091 } 3092 jblocks->jb_suspended = 1; 3093 MNT_IUNLOCK(mp); 3094 if (!set) 3095 vfs_op_exit(mp); 3096 } 3097 3098 static int 3099 journal_unsuspend(struct ufsmount *ump) 3100 { 3101 struct jblocks *jblocks; 3102 struct mount *mp; 3103 3104 mp = UFSTOVFS(ump); 3105 jblocks = ump->softdep_jblocks; 3106 3107 if (jblocks != NULL && jblocks->jb_suspended && 3108 journal_space(ump, jblocks->jb_min)) { 3109 jblocks->jb_suspended = 0; 3110 FREE_LOCK(ump); 3111 mp->mnt_susp_owner = curthread; 3112 vfs_write_resume(mp, 0); 3113 ACQUIRE_LOCK(ump); 3114 return (1); 3115 } 3116 return (0); 3117 } 3118 3119 static void 3120 journal_check_space(struct ufsmount *ump) 3121 { 3122 struct mount *mp; 3123 3124 LOCK_OWNED(ump); 3125 3126 if (journal_space(ump, 0) == 0) { 3127 softdep_speedup(ump); 3128 mp = UFSTOVFS(ump); 3129 FREE_LOCK(ump); 3130 VFS_SYNC(mp, MNT_NOWAIT); 3131 ffs_sbupdate(ump, MNT_WAIT, 0); 3132 ACQUIRE_LOCK(ump); 3133 if (journal_space(ump, 1) == 0) 3134 journal_suspend(ump); 3135 } 3136 } 3137 3138 /* 3139 * Called before any allocation function to be certain that there is 3140 * sufficient space in the journal prior to creating any new records. 3141 * Since in the case of block allocation we may have multiple locked 3142 * buffers at the time of the actual allocation we can not block 3143 * when the journal records are created. Doing so would create a deadlock 3144 * if any of these buffers needed to be flushed to reclaim space. Instead 3145 * we require a sufficiently large amount of available space such that 3146 * each thread in the system could have passed this allocation check and 3147 * still have sufficient free space. With 20% of a minimum journal size 3148 * of 1MB we have 6553 records available. 3149 */ 3150 int 3151 softdep_prealloc(struct vnode *vp, int waitok) 3152 { 3153 struct ufsmount *ump; 3154 3155 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 3156 ("softdep_prealloc called on non-softdep filesystem")); 3157 /* 3158 * Nothing to do if we are not running journaled soft updates. 3159 * If we currently hold the snapshot lock, we must avoid 3160 * handling other resources that could cause deadlock. Do not 3161 * touch quotas vnode since it is typically recursed with 3162 * other vnode locks held. 3163 */ 3164 if (DOINGSUJ(vp) == 0 || IS_SNAPSHOT(VTOI(vp)) || 3165 (vp->v_vflag & VV_SYSTEM) != 0) 3166 return (0); 3167 ump = VFSTOUFS(vp->v_mount); 3168 ACQUIRE_LOCK(ump); 3169 if (journal_space(ump, 0)) { 3170 FREE_LOCK(ump); 3171 return (0); 3172 } 3173 stat_journal_low++; 3174 FREE_LOCK(ump); 3175 if (waitok == MNT_NOWAIT) 3176 return (ENOSPC); 3177 /* 3178 * Attempt to sync this vnode once to flush any journal 3179 * work attached to it. 3180 */ 3181 if ((curthread->td_pflags & TDP_COWINPROGRESS) == 0) 3182 ffs_syncvnode(vp, waitok, 0); 3183 ACQUIRE_LOCK(ump); 3184 process_removes(vp); 3185 process_truncates(vp); 3186 journal_check_space(ump); 3187 FREE_LOCK(ump); 3188 3189 return (0); 3190 } 3191 3192 /* 3193 * Try hard to sync all data and metadata for the vnode, and workitems 3194 * flushing which might conflict with the vnode lock. This is a 3195 * helper for softdep_prerename(). 3196 */ 3197 static int 3198 softdep_prerename_vnode(struct ufsmount *ump, struct vnode *vp) 3199 { 3200 int error; 3201 3202 ASSERT_VOP_ELOCKED(vp, "prehandle"); 3203 if (vp->v_data == NULL) 3204 return (0); 3205 error = VOP_FSYNC(vp, MNT_WAIT, curthread); 3206 if (error != 0) 3207 return (error); 3208 ACQUIRE_LOCK(ump); 3209 process_removes(vp); 3210 process_truncates(vp); 3211 FREE_LOCK(ump); 3212 return (0); 3213 } 3214 3215 /* 3216 * Must be called from VOP_RENAME() after all vnodes are locked. 3217 * Ensures that there is enough journal space for rename. It is 3218 * sufficiently different from softdep_prelink() by having to handle 3219 * four vnodes. 3220 */ 3221 int 3222 softdep_prerename(struct vnode *fdvp, 3223 struct vnode *fvp, 3224 struct vnode *tdvp, 3225 struct vnode *tvp) 3226 { 3227 struct ufsmount *ump; 3228 int error; 3229 3230 ump = VFSTOUFS(fdvp->v_mount); 3231 3232 if (journal_space(ump, 0)) 3233 return (0); 3234 3235 VOP_UNLOCK(tdvp); 3236 VOP_UNLOCK(fvp); 3237 if (tvp != NULL && tvp != tdvp) 3238 VOP_UNLOCK(tvp); 3239 3240 error = softdep_prerename_vnode(ump, fdvp); 3241 VOP_UNLOCK(fdvp); 3242 if (error != 0) 3243 return (error); 3244 3245 VOP_LOCK(fvp, LK_EXCLUSIVE | LK_RETRY); 3246 error = softdep_prerename_vnode(ump, fvp); 3247 VOP_UNLOCK(fvp); 3248 if (error != 0) 3249 return (error); 3250 3251 if (tdvp != fdvp) { 3252 VOP_LOCK(tdvp, LK_EXCLUSIVE | LK_RETRY); 3253 error = softdep_prerename_vnode(ump, tdvp); 3254 VOP_UNLOCK(tdvp); 3255 if (error != 0) 3256 return (error); 3257 } 3258 3259 if (tvp != fvp && tvp != NULL) { 3260 VOP_LOCK(tvp, LK_EXCLUSIVE | LK_RETRY); 3261 error = softdep_prerename_vnode(ump, tvp); 3262 VOP_UNLOCK(tvp); 3263 if (error != 0) 3264 return (error); 3265 } 3266 3267 ACQUIRE_LOCK(ump); 3268 softdep_speedup(ump); 3269 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT); 3270 journal_check_space(ump); 3271 FREE_LOCK(ump); 3272 return (ERELOOKUP); 3273 } 3274 3275 /* 3276 * Before adjusting a link count on a vnode verify that we have sufficient 3277 * journal space. If not, process operations that depend on the currently 3278 * locked pair of vnodes to try to flush space as the syncer, buf daemon, 3279 * and softdep flush threads can not acquire these locks to reclaim space. 3280 * 3281 * Returns 0 if all owned locks are still valid and were not dropped 3282 * in the process, in other case it returns either an error from sync, 3283 * or ERELOOKUP if any of the locks were re-acquired. In the later 3284 * case, the state of the vnodes cannot be relied upon and our VFS 3285 * syscall must be restarted at top level from the lookup. 3286 */ 3287 int 3288 softdep_prelink(struct vnode *dvp, 3289 struct vnode *vp, 3290 struct componentname *cnp) 3291 { 3292 struct ufsmount *ump; 3293 struct nameidata *ndp; 3294 3295 ASSERT_VOP_ELOCKED(dvp, "prelink dvp"); 3296 if (vp != NULL) 3297 ASSERT_VOP_ELOCKED(vp, "prelink vp"); 3298 ump = VFSTOUFS(dvp->v_mount); 3299 3300 /* 3301 * Nothing to do if we have sufficient journal space. We skip 3302 * flushing when vp is a snapshot to avoid deadlock where 3303 * another thread is trying to update the inodeblock for dvp 3304 * and is waiting on snaplk that vp holds. 3305 */ 3306 if (journal_space(ump, 0) || (vp != NULL && IS_SNAPSHOT(VTOI(vp)))) 3307 return (0); 3308 3309 /* 3310 * Check if the journal space consumption can in theory be 3311 * accounted on dvp and vp. If the vnodes metadata was not 3312 * changed comparing with the previous round-trip into 3313 * softdep_prelink(), as indicated by the seqc generation 3314 * recorded in the nameidata, then there is no point in 3315 * starting the sync. 3316 */ 3317 ndp = __containerof(cnp, struct nameidata, ni_cnd); 3318 if (!seqc_in_modify(ndp->ni_dvp_seqc) && 3319 vn_seqc_consistent(dvp, ndp->ni_dvp_seqc) && 3320 (vp == NULL || (!seqc_in_modify(ndp->ni_vp_seqc) && 3321 vn_seqc_consistent(vp, ndp->ni_vp_seqc)))) 3322 return (0); 3323 3324 stat_journal_low++; 3325 if (vp != NULL) { 3326 VOP_UNLOCK(dvp); 3327 ffs_syncvnode(vp, MNT_NOWAIT, 0); 3328 vn_lock_pair(dvp, false, LK_EXCLUSIVE, vp, true, LK_EXCLUSIVE); 3329 if (dvp->v_data == NULL) 3330 goto out; 3331 } 3332 if (vp != NULL) 3333 VOP_UNLOCK(vp); 3334 ffs_syncvnode(dvp, MNT_WAIT, 0); 3335 /* Process vp before dvp as it may create .. removes. */ 3336 if (vp != NULL) { 3337 VOP_UNLOCK(dvp); 3338 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3339 if (vp->v_data == NULL) { 3340 vn_lock_pair(dvp, false, LK_EXCLUSIVE, vp, true, 3341 LK_EXCLUSIVE); 3342 goto out; 3343 } 3344 ACQUIRE_LOCK(ump); 3345 process_removes(vp); 3346 process_truncates(vp); 3347 FREE_LOCK(ump); 3348 VOP_UNLOCK(vp); 3349 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 3350 if (dvp->v_data == NULL) { 3351 vn_lock_pair(dvp, true, LK_EXCLUSIVE, vp, false, 3352 LK_EXCLUSIVE); 3353 goto out; 3354 } 3355 } 3356 3357 ACQUIRE_LOCK(ump); 3358 process_removes(dvp); 3359 process_truncates(dvp); 3360 VOP_UNLOCK(dvp); 3361 softdep_speedup(ump); 3362 3363 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT); 3364 journal_check_space(ump); 3365 FREE_LOCK(ump); 3366 3367 vn_lock_pair(dvp, false, LK_EXCLUSIVE, vp, false, LK_EXCLUSIVE); 3368 out: 3369 ndp->ni_dvp_seqc = vn_seqc_read_any(dvp); 3370 if (vp != NULL) 3371 ndp->ni_vp_seqc = vn_seqc_read_any(vp); 3372 return (ERELOOKUP); 3373 } 3374 3375 static void 3376 jseg_write(struct ufsmount *ump, 3377 struct jseg *jseg, 3378 uint8_t *data) 3379 { 3380 struct jsegrec *rec; 3381 3382 rec = (struct jsegrec *)data; 3383 rec->jsr_seq = jseg->js_seq; 3384 rec->jsr_oldest = jseg->js_oldseq; 3385 rec->jsr_cnt = jseg->js_cnt; 3386 rec->jsr_blocks = jseg->js_size / ump->um_devvp->v_bufobj.bo_bsize; 3387 rec->jsr_crc = 0; 3388 rec->jsr_time = ump->um_fs->fs_mtime; 3389 } 3390 3391 static inline void 3392 inoref_write(struct inoref *inoref, 3393 struct jseg *jseg, 3394 struct jrefrec *rec) 3395 { 3396 3397 inoref->if_jsegdep->jd_seg = jseg; 3398 rec->jr_ino = inoref->if_ino; 3399 rec->jr_parent = inoref->if_parent; 3400 rec->jr_nlink = inoref->if_nlink; 3401 rec->jr_mode = inoref->if_mode; 3402 rec->jr_diroff = inoref->if_diroff; 3403 } 3404 3405 static void 3406 jaddref_write(struct jaddref *jaddref, 3407 struct jseg *jseg, 3408 uint8_t *data) 3409 { 3410 struct jrefrec *rec; 3411 3412 rec = (struct jrefrec *)data; 3413 rec->jr_op = JOP_ADDREF; 3414 inoref_write(&jaddref->ja_ref, jseg, rec); 3415 } 3416 3417 static void 3418 jremref_write(struct jremref *jremref, 3419 struct jseg *jseg, 3420 uint8_t *data) 3421 { 3422 struct jrefrec *rec; 3423 3424 rec = (struct jrefrec *)data; 3425 rec->jr_op = JOP_REMREF; 3426 inoref_write(&jremref->jr_ref, jseg, rec); 3427 } 3428 3429 static void 3430 jmvref_write(struct jmvref *jmvref, 3431 struct jseg *jseg, 3432 uint8_t *data) 3433 { 3434 struct jmvrec *rec; 3435 3436 rec = (struct jmvrec *)data; 3437 rec->jm_op = JOP_MVREF; 3438 rec->jm_ino = jmvref->jm_ino; 3439 rec->jm_parent = jmvref->jm_parent; 3440 rec->jm_oldoff = jmvref->jm_oldoff; 3441 rec->jm_newoff = jmvref->jm_newoff; 3442 } 3443 3444 static void 3445 jnewblk_write(struct jnewblk *jnewblk, 3446 struct jseg *jseg, 3447 uint8_t *data) 3448 { 3449 struct jblkrec *rec; 3450 3451 jnewblk->jn_jsegdep->jd_seg = jseg; 3452 rec = (struct jblkrec *)data; 3453 rec->jb_op = JOP_NEWBLK; 3454 rec->jb_ino = jnewblk->jn_ino; 3455 rec->jb_blkno = jnewblk->jn_blkno; 3456 rec->jb_lbn = jnewblk->jn_lbn; 3457 rec->jb_frags = jnewblk->jn_frags; 3458 rec->jb_oldfrags = jnewblk->jn_oldfrags; 3459 } 3460 3461 static void 3462 jfreeblk_write(struct jfreeblk *jfreeblk, 3463 struct jseg *jseg, 3464 uint8_t *data) 3465 { 3466 struct jblkrec *rec; 3467 3468 jfreeblk->jf_dep.jb_jsegdep->jd_seg = jseg; 3469 rec = (struct jblkrec *)data; 3470 rec->jb_op = JOP_FREEBLK; 3471 rec->jb_ino = jfreeblk->jf_ino; 3472 rec->jb_blkno = jfreeblk->jf_blkno; 3473 rec->jb_lbn = jfreeblk->jf_lbn; 3474 rec->jb_frags = jfreeblk->jf_frags; 3475 rec->jb_oldfrags = 0; 3476 } 3477 3478 static void 3479 jfreefrag_write(struct jfreefrag *jfreefrag, 3480 struct jseg *jseg, 3481 uint8_t *data) 3482 { 3483 struct jblkrec *rec; 3484 3485 jfreefrag->fr_jsegdep->jd_seg = jseg; 3486 rec = (struct jblkrec *)data; 3487 rec->jb_op = JOP_FREEBLK; 3488 rec->jb_ino = jfreefrag->fr_ino; 3489 rec->jb_blkno = jfreefrag->fr_blkno; 3490 rec->jb_lbn = jfreefrag->fr_lbn; 3491 rec->jb_frags = jfreefrag->fr_frags; 3492 rec->jb_oldfrags = 0; 3493 } 3494 3495 static void 3496 jtrunc_write(struct jtrunc *jtrunc, 3497 struct jseg *jseg, 3498 uint8_t *data) 3499 { 3500 struct jtrncrec *rec; 3501 3502 jtrunc->jt_dep.jb_jsegdep->jd_seg = jseg; 3503 rec = (struct jtrncrec *)data; 3504 rec->jt_op = JOP_TRUNC; 3505 rec->jt_ino = jtrunc->jt_ino; 3506 rec->jt_size = jtrunc->jt_size; 3507 rec->jt_extsize = jtrunc->jt_extsize; 3508 } 3509 3510 static void 3511 jfsync_write(struct jfsync *jfsync, 3512 struct jseg *jseg, 3513 uint8_t *data) 3514 { 3515 struct jtrncrec *rec; 3516 3517 rec = (struct jtrncrec *)data; 3518 rec->jt_op = JOP_SYNC; 3519 rec->jt_ino = jfsync->jfs_ino; 3520 rec->jt_size = jfsync->jfs_size; 3521 rec->jt_extsize = jfsync->jfs_extsize; 3522 } 3523 3524 static void 3525 softdep_flushjournal(struct mount *mp) 3526 { 3527 struct jblocks *jblocks; 3528 struct ufsmount *ump; 3529 3530 if (MOUNTEDSUJ(mp) == 0) 3531 return; 3532 ump = VFSTOUFS(mp); 3533 jblocks = ump->softdep_jblocks; 3534 ACQUIRE_LOCK(ump); 3535 while (ump->softdep_on_journal) { 3536 jblocks->jb_needseg = 1; 3537 softdep_process_journal(mp, NULL, MNT_WAIT); 3538 } 3539 FREE_LOCK(ump); 3540 } 3541 3542 static void softdep_synchronize_completed(struct bio *); 3543 static void softdep_synchronize(struct bio *, struct ufsmount *, void *); 3544 3545 static void 3546 softdep_synchronize_completed(struct bio *bp) 3547 { 3548 struct jseg *oldest; 3549 struct jseg *jseg; 3550 struct ufsmount *ump; 3551 3552 /* 3553 * caller1 marks the last segment written before we issued the 3554 * synchronize cache. 3555 */ 3556 jseg = bp->bio_caller1; 3557 if (jseg == NULL) { 3558 g_destroy_bio(bp); 3559 return; 3560 } 3561 ump = VFSTOUFS(jseg->js_list.wk_mp); 3562 ACQUIRE_LOCK(ump); 3563 oldest = NULL; 3564 /* 3565 * Mark all the journal entries waiting on the synchronize cache 3566 * as completed so they may continue on. 3567 */ 3568 while (jseg != NULL && (jseg->js_state & COMPLETE) == 0) { 3569 jseg->js_state |= COMPLETE; 3570 oldest = jseg; 3571 jseg = TAILQ_PREV(jseg, jseglst, js_next); 3572 } 3573 /* 3574 * Restart deferred journal entry processing from the oldest 3575 * completed jseg. 3576 */ 3577 if (oldest) 3578 complete_jsegs(oldest); 3579 3580 FREE_LOCK(ump); 3581 g_destroy_bio(bp); 3582 } 3583 3584 /* 3585 * Send BIO_FLUSH/SYNCHRONIZE CACHE to the device to enforce write ordering 3586 * barriers. The journal must be written prior to any blocks that depend 3587 * on it and the journal can not be released until the blocks have be 3588 * written. This code handles both barriers simultaneously. 3589 */ 3590 static void 3591 softdep_synchronize(struct bio *bp, 3592 struct ufsmount *ump, 3593 void *caller1) 3594 { 3595 3596 bp->bio_cmd = BIO_FLUSH; 3597 bp->bio_flags |= BIO_ORDERED; 3598 bp->bio_data = NULL; 3599 bp->bio_offset = ump->um_cp->provider->mediasize; 3600 bp->bio_length = 0; 3601 bp->bio_done = softdep_synchronize_completed; 3602 bp->bio_caller1 = caller1; 3603 g_io_request(bp, ump->um_cp); 3604 } 3605 3606 /* 3607 * Flush some journal records to disk. 3608 */ 3609 static void 3610 softdep_process_journal(struct mount *mp, 3611 struct worklist *needwk, 3612 int flags) 3613 { 3614 struct jblocks *jblocks; 3615 struct ufsmount *ump; 3616 struct worklist *wk; 3617 struct jseg *jseg; 3618 struct buf *bp; 3619 struct bio *bio; 3620 uint8_t *data; 3621 struct fs *fs; 3622 int shouldflush; 3623 int segwritten; 3624 int jrecmin; /* Minimum records per block. */ 3625 int jrecmax; /* Maximum records per block. */ 3626 int size; 3627 int cnt; 3628 int off; 3629 int devbsize; 3630 3631 ump = VFSTOUFS(mp); 3632 if (ump->um_softdep == NULL || ump->um_softdep->sd_jblocks == NULL) 3633 return; 3634 shouldflush = softdep_flushcache; 3635 bio = NULL; 3636 jseg = NULL; 3637 LOCK_OWNED(ump); 3638 fs = ump->um_fs; 3639 jblocks = ump->softdep_jblocks; 3640 devbsize = ump->um_devvp->v_bufobj.bo_bsize; 3641 /* 3642 * We write anywhere between a disk block and fs block. The upper 3643 * bound is picked to prevent buffer cache fragmentation and limit 3644 * processing time per I/O. 3645 */ 3646 jrecmin = (devbsize / JREC_SIZE) - 1; /* -1 for seg header */ 3647 jrecmax = (fs->fs_bsize / devbsize) * jrecmin; 3648 segwritten = 0; 3649 for (;;) { 3650 cnt = ump->softdep_on_journal; 3651 /* 3652 * Criteria for writing a segment: 3653 * 1) We have a full block. 3654 * 2) We're called from jwait() and haven't found the 3655 * journal item yet. 3656 * 3) Always write if needseg is set. 3657 * 4) If we are called from process_worklist and have 3658 * not yet written anything we write a partial block 3659 * to enforce a 1 second maximum latency on journal 3660 * entries. 3661 */ 3662 if (cnt < (jrecmax - 1) && needwk == NULL && 3663 jblocks->jb_needseg == 0 && (segwritten || cnt == 0)) 3664 break; 3665 cnt++; 3666 /* 3667 * Verify some free journal space. softdep_prealloc() should 3668 * guarantee that we don't run out so this is indicative of 3669 * a problem with the flow control. Try to recover 3670 * gracefully in any event. 3671 */ 3672 while (jblocks->jb_free == 0) { 3673 if (flags != MNT_WAIT) 3674 break; 3675 printf("softdep: Out of journal space!\n"); 3676 softdep_speedup(ump); 3677 msleep(jblocks, LOCK_PTR(ump), PRIBIO, "jblocks", hz); 3678 } 3679 FREE_LOCK(ump); 3680 jseg = malloc(sizeof(*jseg), M_JSEG, M_SOFTDEP_FLAGS); 3681 workitem_alloc(&jseg->js_list, D_JSEG, mp); 3682 LIST_INIT(&jseg->js_entries); 3683 LIST_INIT(&jseg->js_indirs); 3684 jseg->js_state = ATTACHED; 3685 if (shouldflush == 0) 3686 jseg->js_state |= COMPLETE; 3687 else if (bio == NULL) 3688 bio = g_alloc_bio(); 3689 jseg->js_jblocks = jblocks; 3690 bp = geteblk(fs->fs_bsize, 0); 3691 ACQUIRE_LOCK(ump); 3692 /* 3693 * If there was a race while we were allocating the block 3694 * and jseg the entry we care about was likely written. 3695 * We bail out in both the WAIT and NOWAIT case and assume 3696 * the caller will loop if the entry it cares about is 3697 * not written. 3698 */ 3699 cnt = ump->softdep_on_journal; 3700 if (cnt + jblocks->jb_needseg == 0 || jblocks->jb_free == 0) { 3701 bp->b_flags |= B_INVAL | B_NOCACHE; 3702 WORKITEM_FREE(jseg, D_JSEG); 3703 FREE_LOCK(ump); 3704 brelse(bp); 3705 ACQUIRE_LOCK(ump); 3706 break; 3707 } 3708 /* 3709 * Calculate the disk block size required for the available 3710 * records rounded to the min size. 3711 */ 3712 if (cnt == 0) 3713 size = devbsize; 3714 else if (cnt < jrecmax) 3715 size = howmany(cnt, jrecmin) * devbsize; 3716 else 3717 size = fs->fs_bsize; 3718 /* 3719 * Allocate a disk block for this journal data and account 3720 * for truncation of the requested size if enough contiguous 3721 * space was not available. 3722 */ 3723 bp->b_blkno = jblocks_alloc(jblocks, size, &size); 3724 bp->b_lblkno = bp->b_blkno; 3725 bp->b_offset = bp->b_blkno * DEV_BSIZE; 3726 bp->b_bcount = size; 3727 bp->b_flags &= ~B_INVAL; 3728 bp->b_flags |= B_VALIDSUSPWRT | B_NOCOPY; 3729 /* 3730 * Initialize our jseg with cnt records. Assign the next 3731 * sequence number to it and link it in-order. 3732 */ 3733 cnt = MIN(cnt, (size / devbsize) * jrecmin); 3734 jseg->js_buf = bp; 3735 jseg->js_cnt = cnt; 3736 jseg->js_refs = cnt + 1; /* Self ref. */ 3737 jseg->js_size = size; 3738 jseg->js_seq = jblocks->jb_nextseq++; 3739 if (jblocks->jb_oldestseg == NULL) 3740 jblocks->jb_oldestseg = jseg; 3741 jseg->js_oldseq = jblocks->jb_oldestseg->js_seq; 3742 TAILQ_INSERT_TAIL(&jblocks->jb_segs, jseg, js_next); 3743 if (jblocks->jb_writeseg == NULL) 3744 jblocks->jb_writeseg = jseg; 3745 /* 3746 * Start filling in records from the pending list. 3747 */ 3748 data = bp->b_data; 3749 off = 0; 3750 3751 /* 3752 * Always put a header on the first block. 3753 * XXX As with below, there might not be a chance to get 3754 * into the loop. Ensure that something valid is written. 3755 */ 3756 jseg_write(ump, jseg, data); 3757 off += JREC_SIZE; 3758 data = bp->b_data + off; 3759 3760 /* 3761 * XXX Something is wrong here. There's no work to do, 3762 * but we need to perform and I/O and allow it to complete 3763 * anyways. 3764 */ 3765 if (LIST_EMPTY(&ump->softdep_journal_pending)) 3766 stat_emptyjblocks++; 3767 3768 while ((wk = LIST_FIRST(&ump->softdep_journal_pending)) 3769 != NULL) { 3770 if (cnt == 0) 3771 break; 3772 /* Place a segment header on every device block. */ 3773 if ((off % devbsize) == 0) { 3774 jseg_write(ump, jseg, data); 3775 off += JREC_SIZE; 3776 data = bp->b_data + off; 3777 } 3778 if (wk == needwk) 3779 needwk = NULL; 3780 remove_from_journal(wk); 3781 wk->wk_state |= INPROGRESS; 3782 WORKLIST_INSERT(&jseg->js_entries, wk); 3783 switch (wk->wk_type) { 3784 case D_JADDREF: 3785 jaddref_write(WK_JADDREF(wk), jseg, data); 3786 break; 3787 case D_JREMREF: 3788 jremref_write(WK_JREMREF(wk), jseg, data); 3789 break; 3790 case D_JMVREF: 3791 jmvref_write(WK_JMVREF(wk), jseg, data); 3792 break; 3793 case D_JNEWBLK: 3794 jnewblk_write(WK_JNEWBLK(wk), jseg, data); 3795 break; 3796 case D_JFREEBLK: 3797 jfreeblk_write(WK_JFREEBLK(wk), jseg, data); 3798 break; 3799 case D_JFREEFRAG: 3800 jfreefrag_write(WK_JFREEFRAG(wk), jseg, data); 3801 break; 3802 case D_JTRUNC: 3803 jtrunc_write(WK_JTRUNC(wk), jseg, data); 3804 break; 3805 case D_JFSYNC: 3806 jfsync_write(WK_JFSYNC(wk), jseg, data); 3807 break; 3808 default: 3809 panic("process_journal: Unknown type %s", 3810 TYPENAME(wk->wk_type)); 3811 /* NOTREACHED */ 3812 } 3813 off += JREC_SIZE; 3814 data = bp->b_data + off; 3815 cnt--; 3816 } 3817 3818 /* Clear any remaining space so we don't leak kernel data */ 3819 if (size > off) 3820 bzero(data, size - off); 3821 3822 /* 3823 * Write this one buffer and continue. 3824 */ 3825 segwritten = 1; 3826 jblocks->jb_needseg = 0; 3827 WORKLIST_INSERT(&bp->b_dep, &jseg->js_list); 3828 FREE_LOCK(ump); 3829 bp->b_xflags |= BX_CVTENXIO; 3830 pbgetvp(ump->um_devvp, bp); 3831 /* 3832 * We only do the blocking wait once we find the journal 3833 * entry we're looking for. 3834 */ 3835 if (needwk == NULL && flags == MNT_WAIT) 3836 bwrite(bp); 3837 else 3838 bawrite(bp); 3839 ACQUIRE_LOCK(ump); 3840 } 3841 /* 3842 * If we wrote a segment issue a synchronize cache so the journal 3843 * is reflected on disk before the data is written. Since reclaiming 3844 * journal space also requires writing a journal record this 3845 * process also enforces a barrier before reclamation. 3846 */ 3847 if (segwritten && shouldflush) { 3848 softdep_synchronize(bio, ump, 3849 TAILQ_LAST(&jblocks->jb_segs, jseglst)); 3850 } else if (bio) 3851 g_destroy_bio(bio); 3852 /* 3853 * If we've suspended the filesystem because we ran out of journal 3854 * space either try to sync it here to make some progress or 3855 * unsuspend it if we already have. 3856 */ 3857 if (flags == 0 && jblocks->jb_suspended) { 3858 if (journal_unsuspend(ump)) 3859 return; 3860 FREE_LOCK(ump); 3861 VFS_SYNC(mp, MNT_NOWAIT); 3862 ffs_sbupdate(ump, MNT_WAIT, 0); 3863 ACQUIRE_LOCK(ump); 3864 } 3865 } 3866 3867 /* 3868 * Complete a jseg, allowing all dependencies awaiting journal writes 3869 * to proceed. Each journal dependency also attaches a jsegdep to dependent 3870 * structures so that the journal segment can be freed to reclaim space. 3871 */ 3872 static void 3873 complete_jseg(struct jseg *jseg) 3874 { 3875 struct worklist *wk; 3876 struct jmvref *jmvref; 3877 #ifdef INVARIANTS 3878 int i = 0; 3879 #endif 3880 3881 while ((wk = LIST_FIRST(&jseg->js_entries)) != NULL) { 3882 WORKLIST_REMOVE(wk); 3883 wk->wk_state &= ~INPROGRESS; 3884 wk->wk_state |= COMPLETE; 3885 KASSERT(i++ < jseg->js_cnt, 3886 ("handle_written_jseg: overflow %d >= %d", 3887 i - 1, jseg->js_cnt)); 3888 switch (wk->wk_type) { 3889 case D_JADDREF: 3890 handle_written_jaddref(WK_JADDREF(wk)); 3891 break; 3892 case D_JREMREF: 3893 handle_written_jremref(WK_JREMREF(wk)); 3894 break; 3895 case D_JMVREF: 3896 rele_jseg(jseg); /* No jsegdep. */ 3897 jmvref = WK_JMVREF(wk); 3898 LIST_REMOVE(jmvref, jm_deps); 3899 if ((jmvref->jm_pagedep->pd_state & ONWORKLIST) == 0) 3900 free_pagedep(jmvref->jm_pagedep); 3901 WORKITEM_FREE(jmvref, D_JMVREF); 3902 break; 3903 case D_JNEWBLK: 3904 handle_written_jnewblk(WK_JNEWBLK(wk)); 3905 break; 3906 case D_JFREEBLK: 3907 handle_written_jblkdep(&WK_JFREEBLK(wk)->jf_dep); 3908 break; 3909 case D_JTRUNC: 3910 handle_written_jblkdep(&WK_JTRUNC(wk)->jt_dep); 3911 break; 3912 case D_JFSYNC: 3913 rele_jseg(jseg); /* No jsegdep. */ 3914 WORKITEM_FREE(wk, D_JFSYNC); 3915 break; 3916 case D_JFREEFRAG: 3917 handle_written_jfreefrag(WK_JFREEFRAG(wk)); 3918 break; 3919 default: 3920 panic("handle_written_jseg: Unknown type %s", 3921 TYPENAME(wk->wk_type)); 3922 /* NOTREACHED */ 3923 } 3924 } 3925 /* Release the self reference so the structure may be freed. */ 3926 rele_jseg(jseg); 3927 } 3928 3929 /* 3930 * Determine which jsegs are ready for completion processing. Waits for 3931 * synchronize cache to complete as well as forcing in-order completion 3932 * of journal entries. 3933 */ 3934 static void 3935 complete_jsegs(struct jseg *jseg) 3936 { 3937 struct jblocks *jblocks; 3938 struct jseg *jsegn; 3939 3940 jblocks = jseg->js_jblocks; 3941 /* 3942 * Don't allow out of order completions. If this isn't the first 3943 * block wait for it to write before we're done. 3944 */ 3945 if (jseg != jblocks->jb_writeseg) 3946 return; 3947 /* Iterate through available jsegs processing their entries. */ 3948 while (jseg && (jseg->js_state & ALLCOMPLETE) == ALLCOMPLETE) { 3949 jblocks->jb_oldestwrseq = jseg->js_oldseq; 3950 jsegn = TAILQ_NEXT(jseg, js_next); 3951 complete_jseg(jseg); 3952 jseg = jsegn; 3953 } 3954 jblocks->jb_writeseg = jseg; 3955 /* 3956 * Attempt to free jsegs now that oldestwrseq may have advanced. 3957 */ 3958 free_jsegs(jblocks); 3959 } 3960 3961 /* 3962 * Mark a jseg as DEPCOMPLETE and throw away the buffer. Attempt to handle 3963 * the final completions. 3964 */ 3965 static void 3966 handle_written_jseg(struct jseg *jseg, struct buf *bp) 3967 { 3968 3969 if (jseg->js_refs == 0) 3970 panic("handle_written_jseg: No self-reference on %p", jseg); 3971 jseg->js_state |= DEPCOMPLETE; 3972 /* 3973 * We'll never need this buffer again, set flags so it will be 3974 * discarded. 3975 */ 3976 bp->b_flags |= B_INVAL | B_NOCACHE; 3977 pbrelvp(bp); 3978 complete_jsegs(jseg); 3979 } 3980 3981 static inline struct jsegdep * 3982 inoref_jseg(struct inoref *inoref) 3983 { 3984 struct jsegdep *jsegdep; 3985 3986 jsegdep = inoref->if_jsegdep; 3987 inoref->if_jsegdep = NULL; 3988 3989 return (jsegdep); 3990 } 3991 3992 /* 3993 * Called once a jremref has made it to stable store. The jremref is marked 3994 * complete and we attempt to free it. Any pagedeps writes sleeping waiting 3995 * for the jremref to complete will be awoken by free_jremref. 3996 */ 3997 static void 3998 handle_written_jremref(struct jremref *jremref) 3999 { 4000 struct inodedep *inodedep; 4001 struct jsegdep *jsegdep; 4002 struct dirrem *dirrem; 4003 4004 /* Grab the jsegdep. */ 4005 jsegdep = inoref_jseg(&jremref->jr_ref); 4006 /* 4007 * Remove us from the inoref list. 4008 */ 4009 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 4010 0, &inodedep) == 0) 4011 panic("handle_written_jremref: Lost inodedep"); 4012 TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 4013 /* 4014 * Complete the dirrem. 4015 */ 4016 dirrem = jremref->jr_dirrem; 4017 jremref->jr_dirrem = NULL; 4018 LIST_REMOVE(jremref, jr_deps); 4019 jsegdep->jd_state |= jremref->jr_state & MKDIR_PARENT; 4020 jwork_insert(&dirrem->dm_jwork, jsegdep); 4021 if (LIST_EMPTY(&dirrem->dm_jremrefhd) && 4022 (dirrem->dm_state & COMPLETE) != 0) 4023 add_to_worklist(&dirrem->dm_list, 0); 4024 free_jremref(jremref); 4025 } 4026 4027 /* 4028 * Called once a jaddref has made it to stable store. The dependency is 4029 * marked complete and any dependent structures are added to the inode 4030 * bufwait list to be completed as soon as it is written. If a bitmap write 4031 * depends on this entry we move the inode into the inodedephd of the 4032 * bmsafemap dependency and attempt to remove the jaddref from the bmsafemap. 4033 */ 4034 static void 4035 handle_written_jaddref(struct jaddref *jaddref) 4036 { 4037 struct jsegdep *jsegdep; 4038 struct inodedep *inodedep; 4039 struct diradd *diradd; 4040 struct mkdir *mkdir; 4041 4042 /* Grab the jsegdep. */ 4043 jsegdep = inoref_jseg(&jaddref->ja_ref); 4044 mkdir = NULL; 4045 diradd = NULL; 4046 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 4047 0, &inodedep) == 0) 4048 panic("handle_written_jaddref: Lost inodedep."); 4049 if (jaddref->ja_diradd == NULL) 4050 panic("handle_written_jaddref: No dependency"); 4051 if (jaddref->ja_diradd->da_list.wk_type == D_DIRADD) { 4052 diradd = jaddref->ja_diradd; 4053 WORKLIST_INSERT(&inodedep->id_bufwait, &diradd->da_list); 4054 } else if (jaddref->ja_state & MKDIR_PARENT) { 4055 mkdir = jaddref->ja_mkdir; 4056 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir->md_list); 4057 } else if (jaddref->ja_state & MKDIR_BODY) 4058 mkdir = jaddref->ja_mkdir; 4059 else 4060 panic("handle_written_jaddref: Unknown dependency %p", 4061 jaddref->ja_diradd); 4062 jaddref->ja_diradd = NULL; /* also clears ja_mkdir */ 4063 /* 4064 * Remove us from the inode list. 4065 */ 4066 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps); 4067 /* 4068 * The mkdir may be waiting on the jaddref to clear before freeing. 4069 */ 4070 if (mkdir) { 4071 KASSERT(mkdir->md_list.wk_type == D_MKDIR, 4072 ("handle_written_jaddref: Incorrect type for mkdir %s", 4073 TYPENAME(mkdir->md_list.wk_type))); 4074 mkdir->md_jaddref = NULL; 4075 diradd = mkdir->md_diradd; 4076 mkdir->md_state |= DEPCOMPLETE; 4077 complete_mkdir(mkdir); 4078 } 4079 jwork_insert(&diradd->da_jwork, jsegdep); 4080 if (jaddref->ja_state & NEWBLOCK) { 4081 inodedep->id_state |= ONDEPLIST; 4082 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_inodedephd, 4083 inodedep, id_deps); 4084 } 4085 free_jaddref(jaddref); 4086 } 4087 4088 /* 4089 * Called once a jnewblk journal is written. The allocdirect or allocindir 4090 * is placed in the bmsafemap to await notification of a written bitmap. If 4091 * the operation was canceled we add the segdep to the appropriate 4092 * dependency to free the journal space once the canceling operation 4093 * completes. 4094 */ 4095 static void 4096 handle_written_jnewblk(struct jnewblk *jnewblk) 4097 { 4098 struct bmsafemap *bmsafemap; 4099 struct freefrag *freefrag; 4100 struct freework *freework; 4101 struct jsegdep *jsegdep; 4102 struct newblk *newblk; 4103 4104 /* Grab the jsegdep. */ 4105 jsegdep = jnewblk->jn_jsegdep; 4106 jnewblk->jn_jsegdep = NULL; 4107 if (jnewblk->jn_dep == NULL) 4108 panic("handle_written_jnewblk: No dependency for the segdep."); 4109 switch (jnewblk->jn_dep->wk_type) { 4110 case D_NEWBLK: 4111 case D_ALLOCDIRECT: 4112 case D_ALLOCINDIR: 4113 /* 4114 * Add the written block to the bmsafemap so it can 4115 * be notified when the bitmap is on disk. 4116 */ 4117 newblk = WK_NEWBLK(jnewblk->jn_dep); 4118 newblk->nb_jnewblk = NULL; 4119 if ((newblk->nb_state & GOINGAWAY) == 0) { 4120 bmsafemap = newblk->nb_bmsafemap; 4121 newblk->nb_state |= ONDEPLIST; 4122 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, 4123 nb_deps); 4124 } 4125 jwork_insert(&newblk->nb_jwork, jsegdep); 4126 break; 4127 case D_FREEFRAG: 4128 /* 4129 * A newblock being removed by a freefrag when replaced by 4130 * frag extension. 4131 */ 4132 freefrag = WK_FREEFRAG(jnewblk->jn_dep); 4133 freefrag->ff_jdep = NULL; 4134 jwork_insert(&freefrag->ff_jwork, jsegdep); 4135 break; 4136 case D_FREEWORK: 4137 /* 4138 * A direct block was removed by truncate. 4139 */ 4140 freework = WK_FREEWORK(jnewblk->jn_dep); 4141 freework->fw_jnewblk = NULL; 4142 jwork_insert(&freework->fw_freeblks->fb_jwork, jsegdep); 4143 break; 4144 default: 4145 panic("handle_written_jnewblk: Unknown type %d.", 4146 jnewblk->jn_dep->wk_type); 4147 } 4148 jnewblk->jn_dep = NULL; 4149 free_jnewblk(jnewblk); 4150 } 4151 4152 /* 4153 * Cancel a jfreefrag that won't be needed, probably due to colliding with 4154 * an in-flight allocation that has not yet been committed. Divorce us 4155 * from the freefrag and mark it DEPCOMPLETE so that it may be added 4156 * to the worklist. 4157 */ 4158 static void 4159 cancel_jfreefrag(struct jfreefrag *jfreefrag) 4160 { 4161 struct freefrag *freefrag; 4162 4163 if (jfreefrag->fr_jsegdep) { 4164 free_jsegdep(jfreefrag->fr_jsegdep); 4165 jfreefrag->fr_jsegdep = NULL; 4166 } 4167 freefrag = jfreefrag->fr_freefrag; 4168 jfreefrag->fr_freefrag = NULL; 4169 free_jfreefrag(jfreefrag); 4170 freefrag->ff_state |= DEPCOMPLETE; 4171 CTR1(KTR_SUJ, "cancel_jfreefrag: blkno %jd", freefrag->ff_blkno); 4172 } 4173 4174 /* 4175 * Free a jfreefrag when the parent freefrag is rendered obsolete. 4176 */ 4177 static void 4178 free_jfreefrag(struct jfreefrag *jfreefrag) 4179 { 4180 4181 if (jfreefrag->fr_state & INPROGRESS) 4182 WORKLIST_REMOVE(&jfreefrag->fr_list); 4183 else if (jfreefrag->fr_state & ONWORKLIST) 4184 remove_from_journal(&jfreefrag->fr_list); 4185 if (jfreefrag->fr_freefrag != NULL) 4186 panic("free_jfreefrag: Still attached to a freefrag."); 4187 WORKITEM_FREE(jfreefrag, D_JFREEFRAG); 4188 } 4189 4190 /* 4191 * Called when the journal write for a jfreefrag completes. The parent 4192 * freefrag is added to the worklist if this completes its dependencies. 4193 */ 4194 static void 4195 handle_written_jfreefrag(struct jfreefrag *jfreefrag) 4196 { 4197 struct jsegdep *jsegdep; 4198 struct freefrag *freefrag; 4199 4200 /* Grab the jsegdep. */ 4201 jsegdep = jfreefrag->fr_jsegdep; 4202 jfreefrag->fr_jsegdep = NULL; 4203 freefrag = jfreefrag->fr_freefrag; 4204 if (freefrag == NULL) 4205 panic("handle_written_jfreefrag: No freefrag."); 4206 freefrag->ff_state |= DEPCOMPLETE; 4207 freefrag->ff_jdep = NULL; 4208 jwork_insert(&freefrag->ff_jwork, jsegdep); 4209 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 4210 add_to_worklist(&freefrag->ff_list, 0); 4211 jfreefrag->fr_freefrag = NULL; 4212 free_jfreefrag(jfreefrag); 4213 } 4214 4215 /* 4216 * Called when the journal write for a jfreeblk completes. The jfreeblk 4217 * is removed from the freeblks list of pending journal writes and the 4218 * jsegdep is moved to the freeblks jwork to be completed when all blocks 4219 * have been reclaimed. 4220 */ 4221 static void 4222 handle_written_jblkdep(struct jblkdep *jblkdep) 4223 { 4224 struct freeblks *freeblks; 4225 struct jsegdep *jsegdep; 4226 4227 /* Grab the jsegdep. */ 4228 jsegdep = jblkdep->jb_jsegdep; 4229 jblkdep->jb_jsegdep = NULL; 4230 freeblks = jblkdep->jb_freeblks; 4231 LIST_REMOVE(jblkdep, jb_deps); 4232 jwork_insert(&freeblks->fb_jwork, jsegdep); 4233 /* 4234 * If the freeblks is all journaled, we can add it to the worklist. 4235 */ 4236 if (LIST_EMPTY(&freeblks->fb_jblkdephd) && 4237 (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 4238 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 4239 4240 free_jblkdep(jblkdep); 4241 } 4242 4243 static struct jsegdep * 4244 newjsegdep(struct worklist *wk) 4245 { 4246 struct jsegdep *jsegdep; 4247 4248 jsegdep = malloc(sizeof(*jsegdep), M_JSEGDEP, M_SOFTDEP_FLAGS); 4249 workitem_alloc(&jsegdep->jd_list, D_JSEGDEP, wk->wk_mp); 4250 jsegdep->jd_seg = NULL; 4251 4252 return (jsegdep); 4253 } 4254 4255 static struct jmvref * 4256 newjmvref(struct inode *dp, 4257 ino_t ino, 4258 off_t oldoff, 4259 off_t newoff) 4260 { 4261 struct jmvref *jmvref; 4262 4263 jmvref = malloc(sizeof(*jmvref), M_JMVREF, M_SOFTDEP_FLAGS); 4264 workitem_alloc(&jmvref->jm_list, D_JMVREF, ITOVFS(dp)); 4265 jmvref->jm_list.wk_state = ATTACHED | DEPCOMPLETE; 4266 jmvref->jm_parent = dp->i_number; 4267 jmvref->jm_ino = ino; 4268 jmvref->jm_oldoff = oldoff; 4269 jmvref->jm_newoff = newoff; 4270 4271 return (jmvref); 4272 } 4273 4274 /* 4275 * Allocate a new jremref that tracks the removal of ip from dp with the 4276 * directory entry offset of diroff. Mark the entry as ATTACHED and 4277 * DEPCOMPLETE as we have all the information required for the journal write 4278 * and the directory has already been removed from the buffer. The caller 4279 * is responsible for linking the jremref into the pagedep and adding it 4280 * to the journal to write. The MKDIR_PARENT flag is set if we're doing 4281 * a DOTDOT addition so handle_workitem_remove() can properly assign 4282 * the jsegdep when we're done. 4283 */ 4284 static struct jremref * 4285 newjremref(struct dirrem *dirrem, 4286 struct inode *dp, 4287 struct inode *ip, 4288 off_t diroff, 4289 nlink_t nlink) 4290 { 4291 struct jremref *jremref; 4292 4293 jremref = malloc(sizeof(*jremref), M_JREMREF, M_SOFTDEP_FLAGS); 4294 workitem_alloc(&jremref->jr_list, D_JREMREF, ITOVFS(dp)); 4295 jremref->jr_state = ATTACHED; 4296 newinoref(&jremref->jr_ref, ip->i_number, dp->i_number, diroff, 4297 nlink, ip->i_mode); 4298 jremref->jr_dirrem = dirrem; 4299 4300 return (jremref); 4301 } 4302 4303 static inline void 4304 newinoref(struct inoref *inoref, 4305 ino_t ino, 4306 ino_t parent, 4307 off_t diroff, 4308 nlink_t nlink, 4309 uint16_t mode) 4310 { 4311 4312 inoref->if_jsegdep = newjsegdep(&inoref->if_list); 4313 inoref->if_diroff = diroff; 4314 inoref->if_ino = ino; 4315 inoref->if_parent = parent; 4316 inoref->if_nlink = nlink; 4317 inoref->if_mode = mode; 4318 } 4319 4320 /* 4321 * Allocate a new jaddref to track the addition of ino to dp at diroff. The 4322 * directory offset may not be known until later. The caller is responsible 4323 * adding the entry to the journal when this information is available. nlink 4324 * should be the link count prior to the addition and mode is only required 4325 * to have the correct FMT. 4326 */ 4327 static struct jaddref * 4328 newjaddref(struct inode *dp, 4329 ino_t ino, 4330 off_t diroff, 4331 int16_t nlink, 4332 uint16_t mode) 4333 { 4334 struct jaddref *jaddref; 4335 4336 jaddref = malloc(sizeof(*jaddref), M_JADDREF, M_SOFTDEP_FLAGS); 4337 workitem_alloc(&jaddref->ja_list, D_JADDREF, ITOVFS(dp)); 4338 jaddref->ja_state = ATTACHED; 4339 jaddref->ja_mkdir = NULL; 4340 newinoref(&jaddref->ja_ref, ino, dp->i_number, diroff, nlink, mode); 4341 4342 return (jaddref); 4343 } 4344 4345 /* 4346 * Create a new free dependency for a freework. The caller is responsible 4347 * for adjusting the reference count when it has the lock held. The freedep 4348 * will track an outstanding bitmap write that will ultimately clear the 4349 * freework to continue. 4350 */ 4351 static struct freedep * 4352 newfreedep(struct freework *freework) 4353 { 4354 struct freedep *freedep; 4355 4356 freedep = malloc(sizeof(*freedep), M_FREEDEP, M_SOFTDEP_FLAGS); 4357 workitem_alloc(&freedep->fd_list, D_FREEDEP, freework->fw_list.wk_mp); 4358 freedep->fd_freework = freework; 4359 4360 return (freedep); 4361 } 4362 4363 /* 4364 * Free a freedep structure once the buffer it is linked to is written. If 4365 * this is the last reference to the freework schedule it for completion. 4366 */ 4367 static void 4368 free_freedep(struct freedep *freedep) 4369 { 4370 struct freework *freework; 4371 4372 freework = freedep->fd_freework; 4373 freework->fw_freeblks->fb_cgwait--; 4374 if (--freework->fw_ref == 0) 4375 freework_enqueue(freework); 4376 WORKITEM_FREE(freedep, D_FREEDEP); 4377 } 4378 4379 /* 4380 * Allocate a new freework structure that may be a level in an indirect 4381 * when parent is not NULL or a top level block when it is. The top level 4382 * freework structures are allocated without the per-filesystem lock held 4383 * and before the freeblks is visible outside of softdep_setup_freeblocks(). 4384 */ 4385 static struct freework * 4386 newfreework(struct ufsmount *ump, 4387 struct freeblks *freeblks, 4388 struct freework *parent, 4389 ufs_lbn_t lbn, 4390 ufs2_daddr_t nb, 4391 int frags, 4392 int off, 4393 int journal) 4394 { 4395 struct freework *freework; 4396 4397 freework = malloc(sizeof(*freework), M_FREEWORK, M_SOFTDEP_FLAGS); 4398 workitem_alloc(&freework->fw_list, D_FREEWORK, freeblks->fb_list.wk_mp); 4399 freework->fw_state = ATTACHED; 4400 freework->fw_jnewblk = NULL; 4401 freework->fw_freeblks = freeblks; 4402 freework->fw_parent = parent; 4403 freework->fw_lbn = lbn; 4404 freework->fw_blkno = nb; 4405 freework->fw_frags = frags; 4406 freework->fw_indir = NULL; 4407 freework->fw_ref = (MOUNTEDSUJ(UFSTOVFS(ump)) == 0 || 4408 lbn >= -UFS_NXADDR) ? 0 : NINDIR(ump->um_fs) + 1; 4409 freework->fw_start = freework->fw_off = off; 4410 if (journal) 4411 newjfreeblk(freeblks, lbn, nb, frags); 4412 if (parent == NULL) { 4413 ACQUIRE_LOCK(ump); 4414 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 4415 freeblks->fb_ref++; 4416 FREE_LOCK(ump); 4417 } 4418 4419 return (freework); 4420 } 4421 4422 /* 4423 * Eliminate a jfreeblk for a block that does not need journaling. 4424 */ 4425 static void 4426 cancel_jfreeblk(struct freeblks *freeblks, ufs2_daddr_t blkno) 4427 { 4428 struct jfreeblk *jfreeblk; 4429 struct jblkdep *jblkdep; 4430 4431 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) { 4432 if (jblkdep->jb_list.wk_type != D_JFREEBLK) 4433 continue; 4434 jfreeblk = WK_JFREEBLK(&jblkdep->jb_list); 4435 if (jfreeblk->jf_blkno == blkno) 4436 break; 4437 } 4438 if (jblkdep == NULL) 4439 return; 4440 CTR1(KTR_SUJ, "cancel_jfreeblk: blkno %jd", blkno); 4441 free_jsegdep(jblkdep->jb_jsegdep); 4442 LIST_REMOVE(jblkdep, jb_deps); 4443 WORKITEM_FREE(jfreeblk, D_JFREEBLK); 4444 } 4445 4446 /* 4447 * Allocate a new jfreeblk to journal top level block pointer when truncating 4448 * a file. The caller must add this to the worklist when the per-filesystem 4449 * lock is held. 4450 */ 4451 static struct jfreeblk * 4452 newjfreeblk(struct freeblks *freeblks, 4453 ufs_lbn_t lbn, 4454 ufs2_daddr_t blkno, 4455 int frags) 4456 { 4457 struct jfreeblk *jfreeblk; 4458 4459 jfreeblk = malloc(sizeof(*jfreeblk), M_JFREEBLK, M_SOFTDEP_FLAGS); 4460 workitem_alloc(&jfreeblk->jf_dep.jb_list, D_JFREEBLK, 4461 freeblks->fb_list.wk_mp); 4462 jfreeblk->jf_dep.jb_jsegdep = newjsegdep(&jfreeblk->jf_dep.jb_list); 4463 jfreeblk->jf_dep.jb_freeblks = freeblks; 4464 jfreeblk->jf_ino = freeblks->fb_inum; 4465 jfreeblk->jf_lbn = lbn; 4466 jfreeblk->jf_blkno = blkno; 4467 jfreeblk->jf_frags = frags; 4468 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jfreeblk->jf_dep, jb_deps); 4469 4470 return (jfreeblk); 4471 } 4472 4473 /* 4474 * The journal is only prepared to handle full-size block numbers, so we 4475 * have to adjust the record to reflect the change to a full-size block. 4476 * For example, suppose we have a block made up of fragments 8-15 and 4477 * want to free its last two fragments. We are given a request that says: 4478 * FREEBLK ino=5, blkno=14, lbn=0, frags=2, oldfrags=0 4479 * where frags are the number of fragments to free and oldfrags are the 4480 * number of fragments to keep. To block align it, we have to change it to 4481 * have a valid full-size blkno, so it becomes: 4482 * FREEBLK ino=5, blkno=8, lbn=0, frags=2, oldfrags=6 4483 */ 4484 static void 4485 adjust_newfreework(struct freeblks *freeblks, int frag_offset) 4486 { 4487 struct jfreeblk *jfreeblk; 4488 4489 KASSERT((LIST_FIRST(&freeblks->fb_jblkdephd) != NULL && 4490 LIST_FIRST(&freeblks->fb_jblkdephd)->jb_list.wk_type == D_JFREEBLK), 4491 ("adjust_newfreework: Missing freeblks dependency")); 4492 4493 jfreeblk = WK_JFREEBLK(LIST_FIRST(&freeblks->fb_jblkdephd)); 4494 jfreeblk->jf_blkno -= frag_offset; 4495 jfreeblk->jf_frags += frag_offset; 4496 } 4497 4498 /* 4499 * Allocate a new jtrunc to track a partial truncation. 4500 */ 4501 static struct jtrunc * 4502 newjtrunc(struct freeblks *freeblks, 4503 off_t size, 4504 int extsize) 4505 { 4506 struct jtrunc *jtrunc; 4507 4508 jtrunc = malloc(sizeof(*jtrunc), M_JTRUNC, M_SOFTDEP_FLAGS); 4509 workitem_alloc(&jtrunc->jt_dep.jb_list, D_JTRUNC, 4510 freeblks->fb_list.wk_mp); 4511 jtrunc->jt_dep.jb_jsegdep = newjsegdep(&jtrunc->jt_dep.jb_list); 4512 jtrunc->jt_dep.jb_freeblks = freeblks; 4513 jtrunc->jt_ino = freeblks->fb_inum; 4514 jtrunc->jt_size = size; 4515 jtrunc->jt_extsize = extsize; 4516 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jtrunc->jt_dep, jb_deps); 4517 4518 return (jtrunc); 4519 } 4520 4521 /* 4522 * If we're canceling a new bitmap we have to search for another ref 4523 * to move into the bmsafemap dep. This might be better expressed 4524 * with another structure. 4525 */ 4526 static void 4527 move_newblock_dep(struct jaddref *jaddref, struct inodedep *inodedep) 4528 { 4529 struct inoref *inoref; 4530 struct jaddref *jaddrefn; 4531 4532 jaddrefn = NULL; 4533 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4534 inoref = TAILQ_NEXT(inoref, if_deps)) { 4535 if ((jaddref->ja_state & NEWBLOCK) && 4536 inoref->if_list.wk_type == D_JADDREF) { 4537 jaddrefn = (struct jaddref *)inoref; 4538 break; 4539 } 4540 } 4541 if (jaddrefn == NULL) 4542 return; 4543 jaddrefn->ja_state &= ~(ATTACHED | UNDONE); 4544 jaddrefn->ja_state |= jaddref->ja_state & 4545 (ATTACHED | UNDONE | NEWBLOCK); 4546 jaddref->ja_state &= ~(ATTACHED | UNDONE | NEWBLOCK); 4547 jaddref->ja_state |= ATTACHED; 4548 LIST_REMOVE(jaddref, ja_bmdeps); 4549 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_jaddrefhd, jaddrefn, 4550 ja_bmdeps); 4551 } 4552 4553 /* 4554 * Cancel a jaddref either before it has been written or while it is being 4555 * written. This happens when a link is removed before the add reaches 4556 * the disk. The jaddref dependency is kept linked into the bmsafemap 4557 * and inode to prevent the link count or bitmap from reaching the disk 4558 * until handle_workitem_remove() re-adjusts the counts and bitmaps as 4559 * required. 4560 * 4561 * Returns 1 if the canceled addref requires journaling of the remove and 4562 * 0 otherwise. 4563 */ 4564 static int 4565 cancel_jaddref(struct jaddref *jaddref, 4566 struct inodedep *inodedep, 4567 struct workhead *wkhd) 4568 { 4569 struct inoref *inoref; 4570 struct jsegdep *jsegdep; 4571 int needsj; 4572 4573 KASSERT((jaddref->ja_state & COMPLETE) == 0, 4574 ("cancel_jaddref: Canceling complete jaddref")); 4575 if (jaddref->ja_state & (INPROGRESS | COMPLETE)) 4576 needsj = 1; 4577 else 4578 needsj = 0; 4579 if (inodedep == NULL) 4580 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 4581 0, &inodedep) == 0) 4582 panic("cancel_jaddref: Lost inodedep"); 4583 /* 4584 * We must adjust the nlink of any reference operation that follows 4585 * us so that it is consistent with the in-memory reference. This 4586 * ensures that inode nlink rollbacks always have the correct link. 4587 */ 4588 if (needsj == 0) { 4589 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4590 inoref = TAILQ_NEXT(inoref, if_deps)) { 4591 if (inoref->if_state & GOINGAWAY) 4592 break; 4593 inoref->if_nlink--; 4594 } 4595 } 4596 jsegdep = inoref_jseg(&jaddref->ja_ref); 4597 if (jaddref->ja_state & NEWBLOCK) 4598 move_newblock_dep(jaddref, inodedep); 4599 wake_worklist(&jaddref->ja_list); 4600 jaddref->ja_mkdir = NULL; 4601 if (jaddref->ja_state & INPROGRESS) { 4602 jaddref->ja_state &= ~INPROGRESS; 4603 WORKLIST_REMOVE(&jaddref->ja_list); 4604 jwork_insert(wkhd, jsegdep); 4605 } else { 4606 free_jsegdep(jsegdep); 4607 if (jaddref->ja_state & DEPCOMPLETE) 4608 remove_from_journal(&jaddref->ja_list); 4609 } 4610 jaddref->ja_state |= (GOINGAWAY | DEPCOMPLETE); 4611 /* 4612 * Leave NEWBLOCK jaddrefs on the inodedep so handle_workitem_remove 4613 * can arrange for them to be freed with the bitmap. Otherwise we 4614 * no longer need this addref attached to the inoreflst and it 4615 * will incorrectly adjust nlink if we leave it. 4616 */ 4617 if ((jaddref->ja_state & NEWBLOCK) == 0) { 4618 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 4619 if_deps); 4620 jaddref->ja_state |= COMPLETE; 4621 free_jaddref(jaddref); 4622 return (needsj); 4623 } 4624 /* 4625 * Leave the head of the list for jsegdeps for fast merging. 4626 */ 4627 if (LIST_FIRST(wkhd) != NULL) { 4628 jaddref->ja_state |= ONWORKLIST; 4629 LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list); 4630 } else 4631 WORKLIST_INSERT(wkhd, &jaddref->ja_list); 4632 4633 return (needsj); 4634 } 4635 4636 /* 4637 * Attempt to free a jaddref structure when some work completes. This 4638 * should only succeed once the entry is written and all dependencies have 4639 * been notified. 4640 */ 4641 static void 4642 free_jaddref(struct jaddref *jaddref) 4643 { 4644 4645 if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE) 4646 return; 4647 if (jaddref->ja_ref.if_jsegdep) 4648 panic("free_jaddref: segdep attached to jaddref %p(0x%X)\n", 4649 jaddref, jaddref->ja_state); 4650 if (jaddref->ja_state & NEWBLOCK) 4651 LIST_REMOVE(jaddref, ja_bmdeps); 4652 if (jaddref->ja_state & (INPROGRESS | ONWORKLIST)) 4653 panic("free_jaddref: Bad state %p(0x%X)", 4654 jaddref, jaddref->ja_state); 4655 if (jaddref->ja_mkdir != NULL) 4656 panic("free_jaddref: Work pending, 0x%X\n", jaddref->ja_state); 4657 WORKITEM_FREE(jaddref, D_JADDREF); 4658 } 4659 4660 /* 4661 * Free a jremref structure once it has been written or discarded. 4662 */ 4663 static void 4664 free_jremref(struct jremref *jremref) 4665 { 4666 4667 if (jremref->jr_ref.if_jsegdep) 4668 free_jsegdep(jremref->jr_ref.if_jsegdep); 4669 if (jremref->jr_state & INPROGRESS) 4670 panic("free_jremref: IO still pending"); 4671 WORKITEM_FREE(jremref, D_JREMREF); 4672 } 4673 4674 /* 4675 * Free a jnewblk structure. 4676 */ 4677 static void 4678 free_jnewblk(struct jnewblk *jnewblk) 4679 { 4680 4681 if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE) 4682 return; 4683 LIST_REMOVE(jnewblk, jn_deps); 4684 if (jnewblk->jn_dep != NULL) 4685 panic("free_jnewblk: Dependency still attached."); 4686 WORKITEM_FREE(jnewblk, D_JNEWBLK); 4687 } 4688 4689 /* 4690 * Cancel a jnewblk which has been been made redundant by frag extension. 4691 */ 4692 static void 4693 cancel_jnewblk(struct jnewblk *jnewblk, struct workhead *wkhd) 4694 { 4695 struct jsegdep *jsegdep; 4696 4697 CTR1(KTR_SUJ, "cancel_jnewblk: blkno %jd", jnewblk->jn_blkno); 4698 jsegdep = jnewblk->jn_jsegdep; 4699 if (jnewblk->jn_jsegdep == NULL || jnewblk->jn_dep == NULL) 4700 panic("cancel_jnewblk: Invalid state"); 4701 jnewblk->jn_jsegdep = NULL; 4702 jnewblk->jn_dep = NULL; 4703 jnewblk->jn_state |= GOINGAWAY; 4704 if (jnewblk->jn_state & INPROGRESS) { 4705 jnewblk->jn_state &= ~INPROGRESS; 4706 WORKLIST_REMOVE(&jnewblk->jn_list); 4707 jwork_insert(wkhd, jsegdep); 4708 } else { 4709 free_jsegdep(jsegdep); 4710 remove_from_journal(&jnewblk->jn_list); 4711 } 4712 wake_worklist(&jnewblk->jn_list); 4713 WORKLIST_INSERT(wkhd, &jnewblk->jn_list); 4714 } 4715 4716 static void 4717 free_jblkdep(struct jblkdep *jblkdep) 4718 { 4719 4720 if (jblkdep->jb_list.wk_type == D_JFREEBLK) 4721 WORKITEM_FREE(jblkdep, D_JFREEBLK); 4722 else if (jblkdep->jb_list.wk_type == D_JTRUNC) 4723 WORKITEM_FREE(jblkdep, D_JTRUNC); 4724 else 4725 panic("free_jblkdep: Unexpected type %s", 4726 TYPENAME(jblkdep->jb_list.wk_type)); 4727 } 4728 4729 /* 4730 * Free a single jseg once it is no longer referenced in memory or on 4731 * disk. Reclaim journal blocks and dependencies waiting for the segment 4732 * to disappear. 4733 */ 4734 static void 4735 free_jseg(struct jseg *jseg, struct jblocks *jblocks) 4736 { 4737 struct freework *freework; 4738 4739 /* 4740 * Free freework structures that were lingering to indicate freed 4741 * indirect blocks that forced journal write ordering on reallocate. 4742 */ 4743 while ((freework = LIST_FIRST(&jseg->js_indirs)) != NULL) 4744 indirblk_remove(freework); 4745 if (jblocks->jb_oldestseg == jseg) 4746 jblocks->jb_oldestseg = TAILQ_NEXT(jseg, js_next); 4747 TAILQ_REMOVE(&jblocks->jb_segs, jseg, js_next); 4748 jblocks_free(jblocks, jseg->js_list.wk_mp, jseg->js_size); 4749 KASSERT(LIST_EMPTY(&jseg->js_entries), 4750 ("free_jseg: Freed jseg has valid entries.")); 4751 WORKITEM_FREE(jseg, D_JSEG); 4752 } 4753 4754 /* 4755 * Free all jsegs that meet the criteria for being reclaimed and update 4756 * oldestseg. 4757 */ 4758 static void 4759 free_jsegs(struct jblocks *jblocks) 4760 { 4761 struct jseg *jseg; 4762 4763 /* 4764 * Free only those jsegs which have none allocated before them to 4765 * preserve the journal space ordering. 4766 */ 4767 while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) { 4768 /* 4769 * Only reclaim space when nothing depends on this journal 4770 * set and another set has written that it is no longer 4771 * valid. 4772 */ 4773 if (jseg->js_refs != 0) { 4774 jblocks->jb_oldestseg = jseg; 4775 return; 4776 } 4777 if ((jseg->js_state & ALLCOMPLETE) != ALLCOMPLETE) 4778 break; 4779 if (jseg->js_seq > jblocks->jb_oldestwrseq) 4780 break; 4781 /* 4782 * We can free jsegs that didn't write entries when 4783 * oldestwrseq == js_seq. 4784 */ 4785 if (jseg->js_seq == jblocks->jb_oldestwrseq && 4786 jseg->js_cnt != 0) 4787 break; 4788 free_jseg(jseg, jblocks); 4789 } 4790 /* 4791 * If we exited the loop above we still must discover the 4792 * oldest valid segment. 4793 */ 4794 if (jseg) 4795 for (jseg = jblocks->jb_oldestseg; jseg != NULL; 4796 jseg = TAILQ_NEXT(jseg, js_next)) 4797 if (jseg->js_refs != 0) 4798 break; 4799 jblocks->jb_oldestseg = jseg; 4800 /* 4801 * The journal has no valid records but some jsegs may still be 4802 * waiting on oldestwrseq to advance. We force a small record 4803 * out to permit these lingering records to be reclaimed. 4804 */ 4805 if (jblocks->jb_oldestseg == NULL && !TAILQ_EMPTY(&jblocks->jb_segs)) 4806 jblocks->jb_needseg = 1; 4807 } 4808 4809 /* 4810 * Release one reference to a jseg and free it if the count reaches 0. This 4811 * should eventually reclaim journal space as well. 4812 */ 4813 static void 4814 rele_jseg(struct jseg *jseg) 4815 { 4816 4817 KASSERT(jseg->js_refs > 0, 4818 ("free_jseg: Invalid refcnt %d", jseg->js_refs)); 4819 if (--jseg->js_refs != 0) 4820 return; 4821 free_jsegs(jseg->js_jblocks); 4822 } 4823 4824 /* 4825 * Release a jsegdep and decrement the jseg count. 4826 */ 4827 static void 4828 free_jsegdep(struct jsegdep *jsegdep) 4829 { 4830 4831 if (jsegdep->jd_seg) 4832 rele_jseg(jsegdep->jd_seg); 4833 WORKITEM_FREE(jsegdep, D_JSEGDEP); 4834 } 4835 4836 /* 4837 * Wait for a journal item to make it to disk. Initiate journal processing 4838 * if required. 4839 */ 4840 static int 4841 jwait(struct worklist *wk, int waitfor) 4842 { 4843 4844 LOCK_OWNED(VFSTOUFS(wk->wk_mp)); 4845 /* 4846 * Blocking journal waits cause slow synchronous behavior. Record 4847 * stats on the frequency of these blocking operations. 4848 */ 4849 if (waitfor == MNT_WAIT) { 4850 stat_journal_wait++; 4851 switch (wk->wk_type) { 4852 case D_JREMREF: 4853 case D_JMVREF: 4854 stat_jwait_filepage++; 4855 break; 4856 case D_JTRUNC: 4857 case D_JFREEBLK: 4858 stat_jwait_freeblks++; 4859 break; 4860 case D_JNEWBLK: 4861 stat_jwait_newblk++; 4862 break; 4863 case D_JADDREF: 4864 stat_jwait_inode++; 4865 break; 4866 default: 4867 break; 4868 } 4869 } 4870 /* 4871 * If IO has not started we process the journal. We can't mark the 4872 * worklist item as IOWAITING because we drop the lock while 4873 * processing the journal and the worklist entry may be freed after 4874 * this point. The caller may call back in and re-issue the request. 4875 */ 4876 if ((wk->wk_state & INPROGRESS) == 0) { 4877 softdep_process_journal(wk->wk_mp, wk, waitfor); 4878 if (waitfor != MNT_WAIT) 4879 return (EBUSY); 4880 return (0); 4881 } 4882 if (waitfor != MNT_WAIT) 4883 return (EBUSY); 4884 wait_worklist(wk, "jwait"); 4885 return (0); 4886 } 4887 4888 /* 4889 * Lookup an inodedep based on an inode pointer and set the nlinkdelta as 4890 * appropriate. This is a convenience function to reduce duplicate code 4891 * for the setup and revert functions below. 4892 */ 4893 static struct inodedep * 4894 inodedep_lookup_ip(struct inode *ip) 4895 { 4896 struct inodedep *inodedep; 4897 4898 KASSERT(ip->i_nlink >= ip->i_effnlink, 4899 ("inodedep_lookup_ip: bad delta")); 4900 (void) inodedep_lookup(ITOVFS(ip), ip->i_number, DEPALLOC, 4901 &inodedep); 4902 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 4903 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 4904 4905 return (inodedep); 4906 } 4907 4908 /* 4909 * Called prior to creating a new inode and linking it to a directory. The 4910 * jaddref structure must already be allocated by softdep_setup_inomapdep 4911 * and it is discovered here so we can initialize the mode and update 4912 * nlinkdelta. 4913 */ 4914 void 4915 softdep_setup_create(struct inode *dp, struct inode *ip) 4916 { 4917 struct inodedep *inodedep; 4918 struct jaddref *jaddref __diagused; 4919 struct vnode *dvp; 4920 4921 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4922 ("softdep_setup_create called on non-softdep filesystem")); 4923 KASSERT(ip->i_nlink == 1, 4924 ("softdep_setup_create: Invalid link count.")); 4925 dvp = ITOV(dp); 4926 ACQUIRE_LOCK(ITOUMP(dp)); 4927 inodedep = inodedep_lookup_ip(ip); 4928 if (DOINGSUJ(dvp)) { 4929 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4930 inoreflst); 4931 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 4932 ("softdep_setup_create: No addref structure present.")); 4933 } 4934 FREE_LOCK(ITOUMP(dp)); 4935 } 4936 4937 /* 4938 * Create a jaddref structure to track the addition of a DOTDOT link when 4939 * we are reparenting an inode as part of a rename. This jaddref will be 4940 * found by softdep_setup_directory_change. Adjusts nlinkdelta for 4941 * non-journaling softdep. 4942 */ 4943 void 4944 softdep_setup_dotdot_link(struct inode *dp, struct inode *ip) 4945 { 4946 struct inodedep *inodedep; 4947 struct jaddref *jaddref; 4948 struct vnode *dvp; 4949 4950 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4951 ("softdep_setup_dotdot_link called on non-softdep filesystem")); 4952 dvp = ITOV(dp); 4953 jaddref = NULL; 4954 /* 4955 * We don't set MKDIR_PARENT as this is not tied to a mkdir and 4956 * is used as a normal link would be. 4957 */ 4958 if (DOINGSUJ(dvp)) 4959 jaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 4960 dp->i_effnlink - 1, dp->i_mode); 4961 ACQUIRE_LOCK(ITOUMP(dp)); 4962 inodedep = inodedep_lookup_ip(dp); 4963 if (jaddref) 4964 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4965 if_deps); 4966 FREE_LOCK(ITOUMP(dp)); 4967 } 4968 4969 /* 4970 * Create a jaddref structure to track a new link to an inode. The directory 4971 * offset is not known until softdep_setup_directory_add or 4972 * softdep_setup_directory_change. Adjusts nlinkdelta for non-journaling 4973 * softdep. 4974 */ 4975 void 4976 softdep_setup_link(struct inode *dp, struct inode *ip) 4977 { 4978 struct inodedep *inodedep; 4979 struct jaddref *jaddref; 4980 struct vnode *dvp; 4981 4982 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4983 ("softdep_setup_link called on non-softdep filesystem")); 4984 dvp = ITOV(dp); 4985 jaddref = NULL; 4986 if (DOINGSUJ(dvp)) 4987 jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1, 4988 ip->i_mode); 4989 ACQUIRE_LOCK(ITOUMP(dp)); 4990 inodedep = inodedep_lookup_ip(ip); 4991 if (jaddref) 4992 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4993 if_deps); 4994 FREE_LOCK(ITOUMP(dp)); 4995 } 4996 4997 /* 4998 * Called to create the jaddref structures to track . and .. references as 4999 * well as lookup and further initialize the incomplete jaddref created 5000 * by softdep_setup_inomapdep when the inode was allocated. Adjusts 5001 * nlinkdelta for non-journaling softdep. 5002 */ 5003 void 5004 softdep_setup_mkdir(struct inode *dp, struct inode *ip) 5005 { 5006 struct inodedep *inodedep; 5007 struct jaddref *dotdotaddref; 5008 struct jaddref *dotaddref; 5009 struct jaddref *jaddref; 5010 struct vnode *dvp; 5011 5012 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5013 ("softdep_setup_mkdir called on non-softdep filesystem")); 5014 dvp = ITOV(dp); 5015 dotaddref = dotdotaddref = NULL; 5016 if (DOINGSUJ(dvp)) { 5017 dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1, 5018 ip->i_mode); 5019 dotaddref->ja_state |= MKDIR_BODY; 5020 dotdotaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 5021 dp->i_effnlink - 1, dp->i_mode); 5022 dotdotaddref->ja_state |= MKDIR_PARENT; 5023 } 5024 ACQUIRE_LOCK(ITOUMP(dp)); 5025 inodedep = inodedep_lookup_ip(ip); 5026 if (DOINGSUJ(dvp)) { 5027 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 5028 inoreflst); 5029 KASSERT(jaddref != NULL, 5030 ("softdep_setup_mkdir: No addref structure present.")); 5031 KASSERT(jaddref->ja_parent == dp->i_number, 5032 ("softdep_setup_mkdir: bad parent %ju", 5033 (uintmax_t)jaddref->ja_parent)); 5034 TAILQ_INSERT_BEFORE(&jaddref->ja_ref, &dotaddref->ja_ref, 5035 if_deps); 5036 } 5037 inodedep = inodedep_lookup_ip(dp); 5038 if (DOINGSUJ(dvp)) 5039 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, 5040 &dotdotaddref->ja_ref, if_deps); 5041 FREE_LOCK(ITOUMP(dp)); 5042 } 5043 5044 /* 5045 * Called to track nlinkdelta of the inode and parent directories prior to 5046 * unlinking a directory. 5047 */ 5048 void 5049 softdep_setup_rmdir(struct inode *dp, struct inode *ip) 5050 { 5051 5052 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5053 ("softdep_setup_rmdir called on non-softdep filesystem")); 5054 ACQUIRE_LOCK(ITOUMP(dp)); 5055 (void) inodedep_lookup_ip(ip); 5056 (void) inodedep_lookup_ip(dp); 5057 FREE_LOCK(ITOUMP(dp)); 5058 } 5059 5060 /* 5061 * Called to track nlinkdelta of the inode and parent directories prior to 5062 * unlink. 5063 */ 5064 void 5065 softdep_setup_unlink(struct inode *dp, struct inode *ip) 5066 { 5067 5068 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5069 ("softdep_setup_unlink called on non-softdep filesystem")); 5070 ACQUIRE_LOCK(ITOUMP(dp)); 5071 (void) inodedep_lookup_ip(ip); 5072 (void) inodedep_lookup_ip(dp); 5073 FREE_LOCK(ITOUMP(dp)); 5074 } 5075 5076 /* 5077 * Called to release the journal structures created by a failed non-directory 5078 * creation. Adjusts nlinkdelta for non-journaling softdep. 5079 */ 5080 void 5081 softdep_revert_create(struct inode *dp, struct inode *ip) 5082 { 5083 struct inodedep *inodedep; 5084 struct jaddref *jaddref; 5085 struct vnode *dvp; 5086 5087 KASSERT(MOUNTEDSOFTDEP(ITOVFS((dp))) != 0, 5088 ("softdep_revert_create called on non-softdep filesystem")); 5089 dvp = ITOV(dp); 5090 ACQUIRE_LOCK(ITOUMP(dp)); 5091 inodedep = inodedep_lookup_ip(ip); 5092 if (DOINGSUJ(dvp)) { 5093 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 5094 inoreflst); 5095 KASSERT(jaddref->ja_parent == dp->i_number, 5096 ("softdep_revert_create: addref parent mismatch")); 5097 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 5098 } 5099 FREE_LOCK(ITOUMP(dp)); 5100 } 5101 5102 /* 5103 * Called to release the journal structures created by a failed link 5104 * addition. Adjusts nlinkdelta for non-journaling softdep. 5105 */ 5106 void 5107 softdep_revert_link(struct inode *dp, struct inode *ip) 5108 { 5109 struct inodedep *inodedep; 5110 struct jaddref *jaddref; 5111 struct vnode *dvp; 5112 5113 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5114 ("softdep_revert_link called on non-softdep filesystem")); 5115 dvp = ITOV(dp); 5116 ACQUIRE_LOCK(ITOUMP(dp)); 5117 inodedep = inodedep_lookup_ip(ip); 5118 if (DOINGSUJ(dvp)) { 5119 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 5120 inoreflst); 5121 KASSERT(jaddref->ja_parent == dp->i_number, 5122 ("softdep_revert_link: addref parent mismatch")); 5123 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 5124 } 5125 FREE_LOCK(ITOUMP(dp)); 5126 } 5127 5128 /* 5129 * Called to release the journal structures created by a failed mkdir 5130 * attempt. Adjusts nlinkdelta for non-journaling softdep. 5131 */ 5132 void 5133 softdep_revert_mkdir(struct inode *dp, struct inode *ip) 5134 { 5135 struct inodedep *inodedep; 5136 struct jaddref *jaddref; 5137 struct jaddref *dotaddref; 5138 struct vnode *dvp; 5139 5140 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5141 ("softdep_revert_mkdir called on non-softdep filesystem")); 5142 dvp = ITOV(dp); 5143 5144 ACQUIRE_LOCK(ITOUMP(dp)); 5145 inodedep = inodedep_lookup_ip(dp); 5146 if (DOINGSUJ(dvp)) { 5147 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 5148 inoreflst); 5149 KASSERT(jaddref->ja_parent == ip->i_number, 5150 ("softdep_revert_mkdir: dotdot addref parent mismatch")); 5151 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 5152 } 5153 inodedep = inodedep_lookup_ip(ip); 5154 if (DOINGSUJ(dvp)) { 5155 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 5156 inoreflst); 5157 KASSERT(jaddref->ja_parent == dp->i_number, 5158 ("softdep_revert_mkdir: addref parent mismatch")); 5159 dotaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 5160 inoreflst, if_deps); 5161 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 5162 KASSERT(dotaddref->ja_parent == ip->i_number, 5163 ("softdep_revert_mkdir: dot addref parent mismatch")); 5164 cancel_jaddref(dotaddref, inodedep, &inodedep->id_inowait); 5165 } 5166 FREE_LOCK(ITOUMP(dp)); 5167 } 5168 5169 /* 5170 * Called to correct nlinkdelta after a failed rmdir. 5171 */ 5172 void 5173 softdep_revert_rmdir(struct inode *dp, struct inode *ip) 5174 { 5175 5176 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5177 ("softdep_revert_rmdir called on non-softdep filesystem")); 5178 ACQUIRE_LOCK(ITOUMP(dp)); 5179 (void) inodedep_lookup_ip(ip); 5180 (void) inodedep_lookup_ip(dp); 5181 FREE_LOCK(ITOUMP(dp)); 5182 } 5183 5184 /* 5185 * Protecting the freemaps (or bitmaps). 5186 * 5187 * To eliminate the need to execute fsck before mounting a filesystem 5188 * after a power failure, one must (conservatively) guarantee that the 5189 * on-disk copy of the bitmaps never indicate that a live inode or block is 5190 * free. So, when a block or inode is allocated, the bitmap should be 5191 * updated (on disk) before any new pointers. When a block or inode is 5192 * freed, the bitmap should not be updated until all pointers have been 5193 * reset. The latter dependency is handled by the delayed de-allocation 5194 * approach described below for block and inode de-allocation. The former 5195 * dependency is handled by calling the following procedure when a block or 5196 * inode is allocated. When an inode is allocated an "inodedep" is created 5197 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk. 5198 * Each "inodedep" is also inserted into the hash indexing structure so 5199 * that any additional link additions can be made dependent on the inode 5200 * allocation. 5201 * 5202 * The ufs filesystem maintains a number of free block counts (e.g., per 5203 * cylinder group, per cylinder and per <cylinder, rotational position> pair) 5204 * in addition to the bitmaps. These counts are used to improve efficiency 5205 * during allocation and therefore must be consistent with the bitmaps. 5206 * There is no convenient way to guarantee post-crash consistency of these 5207 * counts with simple update ordering, for two main reasons: (1) The counts 5208 * and bitmaps for a single cylinder group block are not in the same disk 5209 * sector. If a disk write is interrupted (e.g., by power failure), one may 5210 * be written and the other not. (2) Some of the counts are located in the 5211 * superblock rather than the cylinder group block. So, we focus our soft 5212 * updates implementation on protecting the bitmaps. When mounting a 5213 * filesystem, we recompute the auxiliary counts from the bitmaps. 5214 */ 5215 5216 /* 5217 * Called just after updating the cylinder group block to allocate an inode. 5218 */ 5219 void 5220 softdep_setup_inomapdep( 5221 struct buf *bp, /* buffer for cylgroup block with inode map */ 5222 struct inode *ip, /* inode related to allocation */ 5223 ino_t newinum, /* new inode number being allocated */ 5224 int mode) 5225 { 5226 struct inodedep *inodedep; 5227 struct bmsafemap *bmsafemap; 5228 struct jaddref *jaddref; 5229 struct mount *mp; 5230 struct fs *fs; 5231 5232 mp = ITOVFS(ip); 5233 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5234 ("softdep_setup_inomapdep called on non-softdep filesystem")); 5235 fs = VFSTOUFS(mp)->um_fs; 5236 jaddref = NULL; 5237 5238 /* 5239 * Allocate the journal reference add structure so that the bitmap 5240 * can be dependent on it. 5241 */ 5242 if (MOUNTEDSUJ(mp)) { 5243 jaddref = newjaddref(ip, newinum, 0, 0, mode); 5244 jaddref->ja_state |= NEWBLOCK; 5245 } 5246 5247 /* 5248 * Create a dependency for the newly allocated inode. 5249 * Panic if it already exists as something is seriously wrong. 5250 * Otherwise add it to the dependency list for the buffer holding 5251 * the cylinder group map from which it was allocated. 5252 * 5253 * We have to preallocate a bmsafemap entry in case it is needed 5254 * in bmsafemap_lookup since once we allocate the inodedep, we 5255 * have to finish initializing it before we can FREE_LOCK(). 5256 * By preallocating, we avoid FREE_LOCK() while doing a malloc 5257 * in bmsafemap_lookup. We cannot call bmsafemap_lookup before 5258 * creating the inodedep as it can be freed during the time 5259 * that we FREE_LOCK() while allocating the inodedep. We must 5260 * call workitem_alloc() before entering the locked section as 5261 * it also acquires the lock and we must avoid trying doing so 5262 * recursively. 5263 */ 5264 bmsafemap = malloc(sizeof(struct bmsafemap), 5265 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5266 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5267 ACQUIRE_LOCK(ITOUMP(ip)); 5268 if ((inodedep_lookup(mp, newinum, DEPALLOC, &inodedep))) 5269 panic("softdep_setup_inomapdep: dependency %p for new" 5270 "inode already exists", inodedep); 5271 bmsafemap = bmsafemap_lookup(mp, bp, ino_to_cg(fs, newinum), bmsafemap); 5272 if (jaddref) { 5273 LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps); 5274 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 5275 if_deps); 5276 } else { 5277 inodedep->id_state |= ONDEPLIST; 5278 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps); 5279 } 5280 inodedep->id_bmsafemap = bmsafemap; 5281 inodedep->id_state &= ~DEPCOMPLETE; 5282 FREE_LOCK(ITOUMP(ip)); 5283 } 5284 5285 /* 5286 * Called just after updating the cylinder group block to 5287 * allocate block or fragment. 5288 */ 5289 void 5290 softdep_setup_blkmapdep( 5291 struct buf *bp, /* buffer for cylgroup block with block map */ 5292 struct mount *mp, /* filesystem doing allocation */ 5293 ufs2_daddr_t newblkno, /* number of newly allocated block */ 5294 int frags, /* Number of fragments. */ 5295 int oldfrags) /* Previous number of fragments for extend. */ 5296 { 5297 struct newblk *newblk; 5298 struct bmsafemap *bmsafemap; 5299 struct jnewblk *jnewblk; 5300 struct ufsmount *ump; 5301 struct fs *fs; 5302 5303 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5304 ("softdep_setup_blkmapdep called on non-softdep filesystem")); 5305 ump = VFSTOUFS(mp); 5306 fs = ump->um_fs; 5307 jnewblk = NULL; 5308 /* 5309 * Create a dependency for the newly allocated block. 5310 * Add it to the dependency list for the buffer holding 5311 * the cylinder group map from which it was allocated. 5312 */ 5313 if (MOUNTEDSUJ(mp)) { 5314 jnewblk = malloc(sizeof(*jnewblk), M_JNEWBLK, M_SOFTDEP_FLAGS); 5315 workitem_alloc(&jnewblk->jn_list, D_JNEWBLK, mp); 5316 jnewblk->jn_jsegdep = newjsegdep(&jnewblk->jn_list); 5317 jnewblk->jn_state = ATTACHED; 5318 jnewblk->jn_blkno = newblkno; 5319 jnewblk->jn_frags = frags; 5320 jnewblk->jn_oldfrags = oldfrags; 5321 #ifdef INVARIANTS 5322 { 5323 struct cg *cgp; 5324 uint8_t *blksfree; 5325 long bno; 5326 int i; 5327 5328 cgp = (struct cg *)bp->b_data; 5329 blksfree = cg_blksfree(cgp); 5330 bno = dtogd(fs, jnewblk->jn_blkno); 5331 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; 5332 i++) { 5333 if (isset(blksfree, bno + i)) 5334 panic("softdep_setup_blkmapdep: " 5335 "free fragment %d from %d-%d " 5336 "state 0x%X dep %p", i, 5337 jnewblk->jn_oldfrags, 5338 jnewblk->jn_frags, 5339 jnewblk->jn_state, 5340 jnewblk->jn_dep); 5341 } 5342 } 5343 #endif 5344 } 5345 5346 CTR3(KTR_SUJ, 5347 "softdep_setup_blkmapdep: blkno %jd frags %d oldfrags %d", 5348 newblkno, frags, oldfrags); 5349 ACQUIRE_LOCK(ump); 5350 if (newblk_lookup(mp, newblkno, DEPALLOC, &newblk) != 0) 5351 panic("softdep_setup_blkmapdep: found block"); 5352 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp, 5353 dtog(fs, newblkno), NULL); 5354 if (jnewblk) { 5355 jnewblk->jn_dep = (struct worklist *)newblk; 5356 LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps); 5357 } else { 5358 newblk->nb_state |= ONDEPLIST; 5359 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps); 5360 } 5361 newblk->nb_bmsafemap = bmsafemap; 5362 newblk->nb_jnewblk = jnewblk; 5363 FREE_LOCK(ump); 5364 } 5365 5366 #define BMSAFEMAP_HASH(ump, cg) \ 5367 (&(ump)->bmsafemap_hashtbl[(cg) & (ump)->bmsafemap_hash_size]) 5368 5369 static int 5370 bmsafemap_find( 5371 struct bmsafemap_hashhead *bmsafemaphd, 5372 int cg, 5373 struct bmsafemap **bmsafemapp) 5374 { 5375 struct bmsafemap *bmsafemap; 5376 5377 LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash) 5378 if (bmsafemap->sm_cg == cg) 5379 break; 5380 if (bmsafemap) { 5381 *bmsafemapp = bmsafemap; 5382 return (1); 5383 } 5384 *bmsafemapp = NULL; 5385 5386 return (0); 5387 } 5388 5389 /* 5390 * Find the bmsafemap associated with a cylinder group buffer. 5391 * If none exists, create one. The buffer must be locked when 5392 * this routine is called and this routine must be called with 5393 * the softdep lock held. To avoid giving up the lock while 5394 * allocating a new bmsafemap, a preallocated bmsafemap may be 5395 * provided. If it is provided but not needed, it is freed. 5396 */ 5397 static struct bmsafemap * 5398 bmsafemap_lookup(struct mount *mp, 5399 struct buf *bp, 5400 int cg, 5401 struct bmsafemap *newbmsafemap) 5402 { 5403 struct bmsafemap_hashhead *bmsafemaphd; 5404 struct bmsafemap *bmsafemap, *collision; 5405 struct worklist *wk; 5406 struct ufsmount *ump; 5407 5408 ump = VFSTOUFS(mp); 5409 LOCK_OWNED(ump); 5410 KASSERT(bp != NULL, ("bmsafemap_lookup: missing buffer")); 5411 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 5412 if (wk->wk_type == D_BMSAFEMAP) { 5413 if (newbmsafemap) 5414 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5415 return (WK_BMSAFEMAP(wk)); 5416 } 5417 } 5418 bmsafemaphd = BMSAFEMAP_HASH(ump, cg); 5419 if (bmsafemap_find(bmsafemaphd, cg, &bmsafemap) == 1) { 5420 if (newbmsafemap) 5421 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5422 return (bmsafemap); 5423 } 5424 if (newbmsafemap) { 5425 bmsafemap = newbmsafemap; 5426 } else { 5427 FREE_LOCK(ump); 5428 bmsafemap = malloc(sizeof(struct bmsafemap), 5429 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5430 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5431 ACQUIRE_LOCK(ump); 5432 } 5433 bmsafemap->sm_buf = bp; 5434 LIST_INIT(&bmsafemap->sm_inodedephd); 5435 LIST_INIT(&bmsafemap->sm_inodedepwr); 5436 LIST_INIT(&bmsafemap->sm_newblkhd); 5437 LIST_INIT(&bmsafemap->sm_newblkwr); 5438 LIST_INIT(&bmsafemap->sm_jaddrefhd); 5439 LIST_INIT(&bmsafemap->sm_jnewblkhd); 5440 LIST_INIT(&bmsafemap->sm_freehd); 5441 LIST_INIT(&bmsafemap->sm_freewr); 5442 if (bmsafemap_find(bmsafemaphd, cg, &collision) == 1) { 5443 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 5444 return (collision); 5445 } 5446 bmsafemap->sm_cg = cg; 5447 LIST_INSERT_HEAD(bmsafemaphd, bmsafemap, sm_hash); 5448 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 5449 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list); 5450 return (bmsafemap); 5451 } 5452 5453 /* 5454 * Direct block allocation dependencies. 5455 * 5456 * When a new block is allocated, the corresponding disk locations must be 5457 * initialized (with zeros or new data) before the on-disk inode points to 5458 * them. Also, the freemap from which the block was allocated must be 5459 * updated (on disk) before the inode's pointer. These two dependencies are 5460 * independent of each other and are needed for all file blocks and indirect 5461 * blocks that are pointed to directly by the inode. Just before the 5462 * "in-core" version of the inode is updated with a newly allocated block 5463 * number, a procedure (below) is called to setup allocation dependency 5464 * structures. These structures are removed when the corresponding 5465 * dependencies are satisfied or when the block allocation becomes obsolete 5466 * (i.e., the file is deleted, the block is de-allocated, or the block is a 5467 * fragment that gets upgraded). All of these cases are handled in 5468 * procedures described later. 5469 * 5470 * When a file extension causes a fragment to be upgraded, either to a larger 5471 * fragment or to a full block, the on-disk location may change (if the 5472 * previous fragment could not simply be extended). In this case, the old 5473 * fragment must be de-allocated, but not until after the inode's pointer has 5474 * been updated. In most cases, this is handled by later procedures, which 5475 * will construct a "freefrag" structure to be added to the workitem queue 5476 * when the inode update is complete (or obsolete). The main exception to 5477 * this is when an allocation occurs while a pending allocation dependency 5478 * (for the same block pointer) remains. This case is handled in the main 5479 * allocation dependency setup procedure by immediately freeing the 5480 * unreferenced fragments. 5481 */ 5482 void 5483 softdep_setup_allocdirect( 5484 struct inode *ip, /* inode to which block is being added */ 5485 ufs_lbn_t off, /* block pointer within inode */ 5486 ufs2_daddr_t newblkno, /* disk block number being added */ 5487 ufs2_daddr_t oldblkno, /* previous block number, 0 unless frag */ 5488 long newsize, /* size of new block */ 5489 long oldsize, /* size of new block */ 5490 struct buf *bp) /* bp for allocated block */ 5491 { 5492 struct allocdirect *adp, *oldadp; 5493 struct allocdirectlst *adphead; 5494 struct freefrag *freefrag; 5495 struct inodedep *inodedep; 5496 struct pagedep *pagedep; 5497 struct jnewblk *jnewblk; 5498 struct newblk *newblk; 5499 struct mount *mp; 5500 ufs_lbn_t lbn; 5501 5502 lbn = bp->b_lblkno; 5503 mp = ITOVFS(ip); 5504 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5505 ("softdep_setup_allocdirect called on non-softdep filesystem")); 5506 if (oldblkno && oldblkno != newblkno) 5507 /* 5508 * The usual case is that a smaller fragment that 5509 * was just allocated has been replaced with a bigger 5510 * fragment or a full-size block. If it is marked as 5511 * B_DELWRI, the current contents have not been written 5512 * to disk. It is possible that the block was written 5513 * earlier, but very uncommon. If the block has never 5514 * been written, there is no need to send a BIO_DELETE 5515 * for it when it is freed. The gain from avoiding the 5516 * TRIMs for the common case of unwritten blocks far 5517 * exceeds the cost of the write amplification for the 5518 * uncommon case of failing to send a TRIM for a block 5519 * that had been written. 5520 */ 5521 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn, 5522 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY); 5523 else 5524 freefrag = NULL; 5525 5526 CTR6(KTR_SUJ, 5527 "softdep_setup_allocdirect: ino %d blkno %jd oldblkno %jd " 5528 "off %jd newsize %ld oldsize %d", 5529 ip->i_number, newblkno, oldblkno, off, newsize, oldsize); 5530 ACQUIRE_LOCK(ITOUMP(ip)); 5531 if (off >= UFS_NDADDR) { 5532 if (lbn > 0) 5533 panic("softdep_setup_allocdirect: bad lbn %jd, off %jd", 5534 lbn, off); 5535 /* allocating an indirect block */ 5536 if (oldblkno != 0) 5537 panic("softdep_setup_allocdirect: non-zero indir"); 5538 } else { 5539 if (off != lbn) 5540 panic("softdep_setup_allocdirect: lbn %jd != off %jd", 5541 lbn, off); 5542 /* 5543 * Allocating a direct block. 5544 * 5545 * If we are allocating a directory block, then we must 5546 * allocate an associated pagedep to track additions and 5547 * deletions. 5548 */ 5549 if ((ip->i_mode & IFMT) == IFDIR) 5550 pagedep_lookup(mp, bp, ip->i_number, off, DEPALLOC, 5551 &pagedep); 5552 } 5553 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5554 panic("softdep_setup_allocdirect: lost block"); 5555 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5556 ("softdep_setup_allocdirect: newblk already initialized")); 5557 /* 5558 * Convert the newblk to an allocdirect. 5559 */ 5560 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT); 5561 adp = (struct allocdirect *)newblk; 5562 newblk->nb_freefrag = freefrag; 5563 adp->ad_offset = off; 5564 adp->ad_oldblkno = oldblkno; 5565 adp->ad_newsize = newsize; 5566 adp->ad_oldsize = oldsize; 5567 5568 /* 5569 * Finish initializing the journal. 5570 */ 5571 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5572 jnewblk->jn_ino = ip->i_number; 5573 jnewblk->jn_lbn = lbn; 5574 add_to_journal(&jnewblk->jn_list); 5575 } 5576 if (freefrag && freefrag->ff_jdep != NULL && 5577 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5578 add_to_journal(freefrag->ff_jdep); 5579 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 5580 adp->ad_inodedep = inodedep; 5581 5582 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5583 /* 5584 * The list of allocdirects must be kept in sorted and ascending 5585 * order so that the rollback routines can quickly determine the 5586 * first uncommitted block (the size of the file stored on disk 5587 * ends at the end of the lowest committed fragment, or if there 5588 * are no fragments, at the end of the highest committed block). 5589 * Since files generally grow, the typical case is that the new 5590 * block is to be added at the end of the list. We speed this 5591 * special case by checking against the last allocdirect in the 5592 * list before laboriously traversing the list looking for the 5593 * insertion point. 5594 */ 5595 adphead = &inodedep->id_newinoupdt; 5596 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5597 if (oldadp == NULL || oldadp->ad_offset <= off) { 5598 /* insert at end of list */ 5599 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5600 if (oldadp != NULL && oldadp->ad_offset == off) 5601 allocdirect_merge(adphead, adp, oldadp); 5602 FREE_LOCK(ITOUMP(ip)); 5603 return; 5604 } 5605 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5606 if (oldadp->ad_offset >= off) 5607 break; 5608 } 5609 if (oldadp == NULL) 5610 panic("softdep_setup_allocdirect: lost entry"); 5611 /* insert in middle of list */ 5612 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5613 if (oldadp->ad_offset == off) 5614 allocdirect_merge(adphead, adp, oldadp); 5615 5616 FREE_LOCK(ITOUMP(ip)); 5617 } 5618 5619 /* 5620 * Merge a newer and older journal record to be stored either in a 5621 * newblock or freefrag. This handles aggregating journal records for 5622 * fragment allocation into a second record as well as replacing a 5623 * journal free with an aborted journal allocation. A segment for the 5624 * oldest record will be placed on wkhd if it has been written. If not 5625 * the segment for the newer record will suffice. 5626 */ 5627 static struct worklist * 5628 jnewblk_merge(struct worklist *new, 5629 struct worklist *old, 5630 struct workhead *wkhd) 5631 { 5632 struct jnewblk *njnewblk; 5633 struct jnewblk *jnewblk; 5634 5635 /* Handle NULLs to simplify callers. */ 5636 if (new == NULL) 5637 return (old); 5638 if (old == NULL) 5639 return (new); 5640 /* Replace a jfreefrag with a jnewblk. */ 5641 if (new->wk_type == D_JFREEFRAG) { 5642 if (WK_JNEWBLK(old)->jn_blkno != WK_JFREEFRAG(new)->fr_blkno) 5643 panic("jnewblk_merge: blkno mismatch: %p, %p", 5644 old, new); 5645 cancel_jfreefrag(WK_JFREEFRAG(new)); 5646 return (old); 5647 } 5648 if (old->wk_type != D_JNEWBLK || new->wk_type != D_JNEWBLK) 5649 panic("jnewblk_merge: Bad type: old %d new %d\n", 5650 old->wk_type, new->wk_type); 5651 /* 5652 * Handle merging of two jnewblk records that describe 5653 * different sets of fragments in the same block. 5654 */ 5655 jnewblk = WK_JNEWBLK(old); 5656 njnewblk = WK_JNEWBLK(new); 5657 if (jnewblk->jn_blkno != njnewblk->jn_blkno) 5658 panic("jnewblk_merge: Merging disparate blocks."); 5659 /* 5660 * The record may be rolled back in the cg. 5661 */ 5662 if (jnewblk->jn_state & UNDONE) { 5663 jnewblk->jn_state &= ~UNDONE; 5664 njnewblk->jn_state |= UNDONE; 5665 njnewblk->jn_state &= ~ATTACHED; 5666 } 5667 /* 5668 * We modify the newer addref and free the older so that if neither 5669 * has been written the most up-to-date copy will be on disk. If 5670 * both have been written but rolled back we only temporarily need 5671 * one of them to fix the bits when the cg write completes. 5672 */ 5673 jnewblk->jn_state |= ATTACHED | COMPLETE; 5674 njnewblk->jn_oldfrags = jnewblk->jn_oldfrags; 5675 cancel_jnewblk(jnewblk, wkhd); 5676 WORKLIST_REMOVE(&jnewblk->jn_list); 5677 free_jnewblk(jnewblk); 5678 return (new); 5679 } 5680 5681 /* 5682 * Replace an old allocdirect dependency with a newer one. 5683 */ 5684 static void 5685 allocdirect_merge( 5686 struct allocdirectlst *adphead, /* head of list holding allocdirects */ 5687 struct allocdirect *newadp, /* allocdirect being added */ 5688 struct allocdirect *oldadp) /* existing allocdirect being checked */ 5689 { 5690 struct worklist *wk; 5691 struct freefrag *freefrag; 5692 5693 freefrag = NULL; 5694 LOCK_OWNED(VFSTOUFS(newadp->ad_list.wk_mp)); 5695 if (newadp->ad_oldblkno != oldadp->ad_newblkno || 5696 newadp->ad_oldsize != oldadp->ad_newsize || 5697 newadp->ad_offset >= UFS_NDADDR) 5698 panic("%s %jd != new %jd || old size %ld != new %ld", 5699 "allocdirect_merge: old blkno", 5700 (intmax_t)newadp->ad_oldblkno, 5701 (intmax_t)oldadp->ad_newblkno, 5702 newadp->ad_oldsize, oldadp->ad_newsize); 5703 newadp->ad_oldblkno = oldadp->ad_oldblkno; 5704 newadp->ad_oldsize = oldadp->ad_oldsize; 5705 /* 5706 * If the old dependency had a fragment to free or had never 5707 * previously had a block allocated, then the new dependency 5708 * can immediately post its freefrag and adopt the old freefrag. 5709 * This action is done by swapping the freefrag dependencies. 5710 * The new dependency gains the old one's freefrag, and the 5711 * old one gets the new one and then immediately puts it on 5712 * the worklist when it is freed by free_newblk. It is 5713 * not possible to do this swap when the old dependency had a 5714 * non-zero size but no previous fragment to free. This condition 5715 * arises when the new block is an extension of the old block. 5716 * Here, the first part of the fragment allocated to the new 5717 * dependency is part of the block currently claimed on disk by 5718 * the old dependency, so cannot legitimately be freed until the 5719 * conditions for the new dependency are fulfilled. 5720 */ 5721 freefrag = newadp->ad_freefrag; 5722 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) { 5723 newadp->ad_freefrag = oldadp->ad_freefrag; 5724 oldadp->ad_freefrag = freefrag; 5725 } 5726 /* 5727 * If we are tracking a new directory-block allocation, 5728 * move it from the old allocdirect to the new allocdirect. 5729 */ 5730 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) { 5731 WORKLIST_REMOVE(wk); 5732 if (!LIST_EMPTY(&oldadp->ad_newdirblk)) 5733 panic("allocdirect_merge: extra newdirblk"); 5734 WORKLIST_INSERT(&newadp->ad_newdirblk, wk); 5735 } 5736 TAILQ_REMOVE(adphead, oldadp, ad_next); 5737 /* 5738 * We need to move any journal dependencies over to the freefrag 5739 * that releases this block if it exists. Otherwise we are 5740 * extending an existing block and we'll wait until that is 5741 * complete to release the journal space and extend the 5742 * new journal to cover this old space as well. 5743 */ 5744 if (freefrag == NULL) { 5745 if (oldadp->ad_newblkno != newadp->ad_newblkno) 5746 panic("allocdirect_merge: %jd != %jd", 5747 oldadp->ad_newblkno, newadp->ad_newblkno); 5748 newadp->ad_block.nb_jnewblk = (struct jnewblk *) 5749 jnewblk_merge(&newadp->ad_block.nb_jnewblk->jn_list, 5750 &oldadp->ad_block.nb_jnewblk->jn_list, 5751 &newadp->ad_block.nb_jwork); 5752 oldadp->ad_block.nb_jnewblk = NULL; 5753 cancel_newblk(&oldadp->ad_block, NULL, 5754 &newadp->ad_block.nb_jwork); 5755 } else { 5756 wk = (struct worklist *) cancel_newblk(&oldadp->ad_block, 5757 &freefrag->ff_list, &freefrag->ff_jwork); 5758 freefrag->ff_jdep = jnewblk_merge(freefrag->ff_jdep, wk, 5759 &freefrag->ff_jwork); 5760 } 5761 free_newblk(&oldadp->ad_block); 5762 } 5763 5764 /* 5765 * Allocate a jfreefrag structure to journal a single block free. 5766 */ 5767 static struct jfreefrag * 5768 newjfreefrag(struct freefrag *freefrag, 5769 struct inode *ip, 5770 ufs2_daddr_t blkno, 5771 long size, 5772 ufs_lbn_t lbn) 5773 { 5774 struct jfreefrag *jfreefrag; 5775 struct fs *fs; 5776 5777 fs = ITOFS(ip); 5778 jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG, 5779 M_SOFTDEP_FLAGS); 5780 workitem_alloc(&jfreefrag->fr_list, D_JFREEFRAG, ITOVFS(ip)); 5781 jfreefrag->fr_jsegdep = newjsegdep(&jfreefrag->fr_list); 5782 jfreefrag->fr_state = ATTACHED | DEPCOMPLETE; 5783 jfreefrag->fr_ino = ip->i_number; 5784 jfreefrag->fr_lbn = lbn; 5785 jfreefrag->fr_blkno = blkno; 5786 jfreefrag->fr_frags = numfrags(fs, size); 5787 jfreefrag->fr_freefrag = freefrag; 5788 5789 return (jfreefrag); 5790 } 5791 5792 /* 5793 * Allocate a new freefrag structure. 5794 */ 5795 static struct freefrag * 5796 newfreefrag(struct inode *ip, 5797 ufs2_daddr_t blkno, 5798 long size, 5799 ufs_lbn_t lbn, 5800 uint64_t key) 5801 { 5802 struct freefrag *freefrag; 5803 struct ufsmount *ump; 5804 struct fs *fs; 5805 5806 CTR4(KTR_SUJ, "newfreefrag: ino %d blkno %jd size %ld lbn %jd", 5807 ip->i_number, blkno, size, lbn); 5808 ump = ITOUMP(ip); 5809 fs = ump->um_fs; 5810 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag) 5811 panic("newfreefrag: frag size"); 5812 freefrag = malloc(sizeof(struct freefrag), 5813 M_FREEFRAG, M_SOFTDEP_FLAGS); 5814 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ump)); 5815 freefrag->ff_state = ATTACHED; 5816 LIST_INIT(&freefrag->ff_jwork); 5817 freefrag->ff_inum = ip->i_number; 5818 freefrag->ff_vtype = ITOV(ip)->v_type; 5819 freefrag->ff_blkno = blkno; 5820 freefrag->ff_fragsize = size; 5821 freefrag->ff_key = key; 5822 5823 if (MOUNTEDSUJ(UFSTOVFS(ump))) { 5824 freefrag->ff_jdep = (struct worklist *) 5825 newjfreefrag(freefrag, ip, blkno, size, lbn); 5826 } else { 5827 freefrag->ff_state |= DEPCOMPLETE; 5828 freefrag->ff_jdep = NULL; 5829 } 5830 5831 return (freefrag); 5832 } 5833 5834 /* 5835 * This workitem de-allocates fragments that were replaced during 5836 * file block allocation. 5837 */ 5838 static void 5839 handle_workitem_freefrag(struct freefrag *freefrag) 5840 { 5841 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp); 5842 struct workhead wkhd; 5843 5844 CTR3(KTR_SUJ, 5845 "handle_workitem_freefrag: ino %d blkno %jd size %ld", 5846 freefrag->ff_inum, freefrag->ff_blkno, freefrag->ff_fragsize); 5847 /* 5848 * It would be illegal to add new completion items to the 5849 * freefrag after it was schedule to be done so it must be 5850 * safe to modify the list head here. 5851 */ 5852 LIST_INIT(&wkhd); 5853 ACQUIRE_LOCK(ump); 5854 LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list); 5855 /* 5856 * If the journal has not been written we must cancel it here. 5857 */ 5858 if (freefrag->ff_jdep) { 5859 if (freefrag->ff_jdep->wk_type != D_JNEWBLK) 5860 panic("handle_workitem_freefrag: Unexpected type %d\n", 5861 freefrag->ff_jdep->wk_type); 5862 cancel_jnewblk(WK_JNEWBLK(freefrag->ff_jdep), &wkhd); 5863 } 5864 FREE_LOCK(ump); 5865 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno, 5866 freefrag->ff_fragsize, freefrag->ff_inum, freefrag->ff_vtype, 5867 &wkhd, freefrag->ff_key); 5868 ACQUIRE_LOCK(ump); 5869 WORKITEM_FREE(freefrag, D_FREEFRAG); 5870 FREE_LOCK(ump); 5871 } 5872 5873 /* 5874 * Set up a dependency structure for an external attributes data block. 5875 * This routine follows much of the structure of softdep_setup_allocdirect. 5876 * See the description of softdep_setup_allocdirect above for details. 5877 */ 5878 void 5879 softdep_setup_allocext( 5880 struct inode *ip, 5881 ufs_lbn_t off, 5882 ufs2_daddr_t newblkno, 5883 ufs2_daddr_t oldblkno, 5884 long newsize, 5885 long oldsize, 5886 struct buf *bp) 5887 { 5888 struct allocdirect *adp, *oldadp; 5889 struct allocdirectlst *adphead; 5890 struct freefrag *freefrag; 5891 struct inodedep *inodedep; 5892 struct jnewblk *jnewblk; 5893 struct newblk *newblk; 5894 struct mount *mp; 5895 struct ufsmount *ump; 5896 ufs_lbn_t lbn; 5897 5898 mp = ITOVFS(ip); 5899 ump = VFSTOUFS(mp); 5900 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5901 ("softdep_setup_allocext called on non-softdep filesystem")); 5902 KASSERT(off < UFS_NXADDR, 5903 ("softdep_setup_allocext: lbn %lld > UFS_NXADDR", (long long)off)); 5904 5905 lbn = bp->b_lblkno; 5906 if (oldblkno && oldblkno != newblkno) 5907 /* 5908 * The usual case is that a smaller fragment that 5909 * was just allocated has been replaced with a bigger 5910 * fragment or a full-size block. If it is marked as 5911 * B_DELWRI, the current contents have not been written 5912 * to disk. It is possible that the block was written 5913 * earlier, but very uncommon. If the block has never 5914 * been written, there is no need to send a BIO_DELETE 5915 * for it when it is freed. The gain from avoiding the 5916 * TRIMs for the common case of unwritten blocks far 5917 * exceeds the cost of the write amplification for the 5918 * uncommon case of failing to send a TRIM for a block 5919 * that had been written. 5920 */ 5921 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn, 5922 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY); 5923 else 5924 freefrag = NULL; 5925 5926 ACQUIRE_LOCK(ump); 5927 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5928 panic("softdep_setup_allocext: lost block"); 5929 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5930 ("softdep_setup_allocext: newblk already initialized")); 5931 /* 5932 * Convert the newblk to an allocdirect. 5933 */ 5934 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT); 5935 adp = (struct allocdirect *)newblk; 5936 newblk->nb_freefrag = freefrag; 5937 adp->ad_offset = off; 5938 adp->ad_oldblkno = oldblkno; 5939 adp->ad_newsize = newsize; 5940 adp->ad_oldsize = oldsize; 5941 adp->ad_state |= EXTDATA; 5942 5943 /* 5944 * Finish initializing the journal. 5945 */ 5946 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5947 jnewblk->jn_ino = ip->i_number; 5948 jnewblk->jn_lbn = lbn; 5949 add_to_journal(&jnewblk->jn_list); 5950 } 5951 if (freefrag && freefrag->ff_jdep != NULL && 5952 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5953 add_to_journal(freefrag->ff_jdep); 5954 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 5955 adp->ad_inodedep = inodedep; 5956 5957 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5958 /* 5959 * The list of allocdirects must be kept in sorted and ascending 5960 * order so that the rollback routines can quickly determine the 5961 * first uncommitted block (the size of the file stored on disk 5962 * ends at the end of the lowest committed fragment, or if there 5963 * are no fragments, at the end of the highest committed block). 5964 * Since files generally grow, the typical case is that the new 5965 * block is to be added at the end of the list. We speed this 5966 * special case by checking against the last allocdirect in the 5967 * list before laboriously traversing the list looking for the 5968 * insertion point. 5969 */ 5970 adphead = &inodedep->id_newextupdt; 5971 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5972 if (oldadp == NULL || oldadp->ad_offset <= off) { 5973 /* insert at end of list */ 5974 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5975 if (oldadp != NULL && oldadp->ad_offset == off) 5976 allocdirect_merge(adphead, adp, oldadp); 5977 FREE_LOCK(ump); 5978 return; 5979 } 5980 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5981 if (oldadp->ad_offset >= off) 5982 break; 5983 } 5984 if (oldadp == NULL) 5985 panic("softdep_setup_allocext: lost entry"); 5986 /* insert in middle of list */ 5987 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5988 if (oldadp->ad_offset == off) 5989 allocdirect_merge(adphead, adp, oldadp); 5990 FREE_LOCK(ump); 5991 } 5992 5993 /* 5994 * Indirect block allocation dependencies. 5995 * 5996 * The same dependencies that exist for a direct block also exist when 5997 * a new block is allocated and pointed to by an entry in a block of 5998 * indirect pointers. The undo/redo states described above are also 5999 * used here. Because an indirect block contains many pointers that 6000 * may have dependencies, a second copy of the entire in-memory indirect 6001 * block is kept. The buffer cache copy is always completely up-to-date. 6002 * The second copy, which is used only as a source for disk writes, 6003 * contains only the safe pointers (i.e., those that have no remaining 6004 * update dependencies). The second copy is freed when all pointers 6005 * are safe. The cache is not allowed to replace indirect blocks with 6006 * pending update dependencies. If a buffer containing an indirect 6007 * block with dependencies is written, these routines will mark it 6008 * dirty again. It can only be successfully written once all the 6009 * dependencies are removed. The ffs_fsync routine in conjunction with 6010 * softdep_sync_metadata work together to get all the dependencies 6011 * removed so that a file can be successfully written to disk. Three 6012 * procedures are used when setting up indirect block pointer 6013 * dependencies. The division is necessary because of the organization 6014 * of the "balloc" routine and because of the distinction between file 6015 * pages and file metadata blocks. 6016 */ 6017 6018 /* 6019 * Allocate a new allocindir structure. 6020 */ 6021 static struct allocindir * 6022 newallocindir( 6023 struct inode *ip, /* inode for file being extended */ 6024 int ptrno, /* offset of pointer in indirect block */ 6025 ufs2_daddr_t newblkno, /* disk block number being added */ 6026 ufs2_daddr_t oldblkno, /* previous block number, 0 if none */ 6027 ufs_lbn_t lbn) 6028 { 6029 struct newblk *newblk; 6030 struct allocindir *aip; 6031 struct freefrag *freefrag; 6032 struct jnewblk *jnewblk; 6033 6034 if (oldblkno) 6035 freefrag = newfreefrag(ip, oldblkno, ITOFS(ip)->fs_bsize, lbn, 6036 SINGLETON_KEY); 6037 else 6038 freefrag = NULL; 6039 ACQUIRE_LOCK(ITOUMP(ip)); 6040 if (newblk_lookup(ITOVFS(ip), newblkno, 0, &newblk) == 0) 6041 panic("new_allocindir: lost block"); 6042 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 6043 ("newallocindir: newblk already initialized")); 6044 WORKITEM_REASSIGN(newblk, D_ALLOCINDIR); 6045 newblk->nb_freefrag = freefrag; 6046 aip = (struct allocindir *)newblk; 6047 aip->ai_offset = ptrno; 6048 aip->ai_oldblkno = oldblkno; 6049 aip->ai_lbn = lbn; 6050 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 6051 jnewblk->jn_ino = ip->i_number; 6052 jnewblk->jn_lbn = lbn; 6053 add_to_journal(&jnewblk->jn_list); 6054 } 6055 if (freefrag && freefrag->ff_jdep != NULL && 6056 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 6057 add_to_journal(freefrag->ff_jdep); 6058 return (aip); 6059 } 6060 6061 /* 6062 * Called just before setting an indirect block pointer 6063 * to a newly allocated file page. 6064 */ 6065 void 6066 softdep_setup_allocindir_page( 6067 struct inode *ip, /* inode for file being extended */ 6068 ufs_lbn_t lbn, /* allocated block number within file */ 6069 struct buf *bp, /* buffer with indirect blk referencing page */ 6070 int ptrno, /* offset of pointer in indirect block */ 6071 ufs2_daddr_t newblkno, /* disk block number being added */ 6072 ufs2_daddr_t oldblkno, /* previous block number, 0 if none */ 6073 struct buf *nbp) /* buffer holding allocated page */ 6074 { 6075 struct inodedep *inodedep; 6076 struct freefrag *freefrag; 6077 struct allocindir *aip; 6078 struct pagedep *pagedep; 6079 struct mount *mp; 6080 struct ufsmount *ump; 6081 6082 mp = ITOVFS(ip); 6083 ump = VFSTOUFS(mp); 6084 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6085 ("softdep_setup_allocindir_page called on non-softdep filesystem")); 6086 KASSERT(lbn == nbp->b_lblkno, 6087 ("softdep_setup_allocindir_page: lbn %jd != lblkno %jd", 6088 lbn, bp->b_lblkno)); 6089 CTR4(KTR_SUJ, 6090 "softdep_setup_allocindir_page: ino %d blkno %jd oldblkno %jd " 6091 "lbn %jd", ip->i_number, newblkno, oldblkno, lbn); 6092 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page"); 6093 aip = newallocindir(ip, ptrno, newblkno, oldblkno, lbn); 6094 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6095 /* 6096 * If we are allocating a directory page, then we must 6097 * allocate an associated pagedep to track additions and 6098 * deletions. 6099 */ 6100 if ((ip->i_mode & IFMT) == IFDIR) 6101 pagedep_lookup(mp, nbp, ip->i_number, lbn, DEPALLOC, &pagedep); 6102 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 6103 freefrag = setup_allocindir_phase2(bp, ip, inodedep, aip, lbn); 6104 FREE_LOCK(ump); 6105 if (freefrag) 6106 handle_workitem_freefrag(freefrag); 6107 } 6108 6109 /* 6110 * Called just before setting an indirect block pointer to a 6111 * newly allocated indirect block. 6112 */ 6113 void 6114 softdep_setup_allocindir_meta( 6115 struct buf *nbp, /* newly allocated indirect block */ 6116 struct inode *ip, /* inode for file being extended */ 6117 struct buf *bp, /* indirect block referencing allocated block */ 6118 int ptrno, /* offset of pointer in indirect block */ 6119 ufs2_daddr_t newblkno) /* disk block number being added */ 6120 { 6121 struct inodedep *inodedep; 6122 struct allocindir *aip; 6123 struct ufsmount *ump; 6124 ufs_lbn_t lbn; 6125 6126 ump = ITOUMP(ip); 6127 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 6128 ("softdep_setup_allocindir_meta called on non-softdep filesystem")); 6129 CTR3(KTR_SUJ, 6130 "softdep_setup_allocindir_meta: ino %d blkno %jd ptrno %d", 6131 ip->i_number, newblkno, ptrno); 6132 lbn = nbp->b_lblkno; 6133 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta"); 6134 aip = newallocindir(ip, ptrno, newblkno, 0, lbn); 6135 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep); 6136 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 6137 if (setup_allocindir_phase2(bp, ip, inodedep, aip, lbn)) 6138 panic("softdep_setup_allocindir_meta: Block already existed"); 6139 FREE_LOCK(ump); 6140 } 6141 6142 static void 6143 indirdep_complete(struct indirdep *indirdep) 6144 { 6145 struct allocindir *aip; 6146 6147 LIST_REMOVE(indirdep, ir_next); 6148 indirdep->ir_state |= DEPCOMPLETE; 6149 6150 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) { 6151 LIST_REMOVE(aip, ai_next); 6152 free_newblk(&aip->ai_block); 6153 } 6154 /* 6155 * If this indirdep is not attached to a buf it was simply waiting 6156 * on completion to clear completehd. free_indirdep() asserts 6157 * that nothing is dangling. 6158 */ 6159 if ((indirdep->ir_state & ONWORKLIST) == 0) 6160 free_indirdep(indirdep); 6161 } 6162 6163 static struct indirdep * 6164 indirdep_lookup(struct mount *mp, 6165 struct inode *ip, 6166 struct buf *bp) 6167 { 6168 struct indirdep *indirdep, *newindirdep; 6169 struct newblk *newblk; 6170 struct ufsmount *ump; 6171 struct worklist *wk; 6172 struct fs *fs; 6173 ufs2_daddr_t blkno; 6174 6175 ump = VFSTOUFS(mp); 6176 LOCK_OWNED(ump); 6177 indirdep = NULL; 6178 newindirdep = NULL; 6179 fs = ump->um_fs; 6180 for (;;) { 6181 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 6182 if (wk->wk_type != D_INDIRDEP) 6183 continue; 6184 indirdep = WK_INDIRDEP(wk); 6185 break; 6186 } 6187 /* Found on the buffer worklist, no new structure to free. */ 6188 if (indirdep != NULL && newindirdep == NULL) 6189 return (indirdep); 6190 if (indirdep != NULL && newindirdep != NULL) 6191 panic("indirdep_lookup: simultaneous create"); 6192 /* None found on the buffer and a new structure is ready. */ 6193 if (indirdep == NULL && newindirdep != NULL) 6194 break; 6195 /* None found and no new structure available. */ 6196 FREE_LOCK(ump); 6197 newindirdep = malloc(sizeof(struct indirdep), 6198 M_INDIRDEP, M_SOFTDEP_FLAGS); 6199 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, mp); 6200 newindirdep->ir_state = ATTACHED; 6201 if (I_IS_UFS1(ip)) 6202 newindirdep->ir_state |= UFS1FMT; 6203 TAILQ_INIT(&newindirdep->ir_trunc); 6204 newindirdep->ir_saveddata = NULL; 6205 LIST_INIT(&newindirdep->ir_deplisthd); 6206 LIST_INIT(&newindirdep->ir_donehd); 6207 LIST_INIT(&newindirdep->ir_writehd); 6208 LIST_INIT(&newindirdep->ir_completehd); 6209 if (bp->b_blkno == bp->b_lblkno) { 6210 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp, 6211 NULL, NULL); 6212 bp->b_blkno = blkno; 6213 } 6214 newindirdep->ir_freeblks = NULL; 6215 newindirdep->ir_savebp = 6216 getblk(ump->um_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0); 6217 newindirdep->ir_bp = bp; 6218 BUF_KERNPROC(newindirdep->ir_savebp); 6219 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount); 6220 ACQUIRE_LOCK(ump); 6221 } 6222 indirdep = newindirdep; 6223 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list); 6224 /* 6225 * If the block is not yet allocated we don't set DEPCOMPLETE so 6226 * that we don't free dependencies until the pointers are valid. 6227 * This could search b_dep for D_ALLOCDIRECT/D_ALLOCINDIR rather 6228 * than using the hash. 6229 */ 6230 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk)) 6231 LIST_INSERT_HEAD(&newblk->nb_indirdeps, indirdep, ir_next); 6232 else 6233 indirdep->ir_state |= DEPCOMPLETE; 6234 return (indirdep); 6235 } 6236 6237 /* 6238 * Called to finish the allocation of the "aip" allocated 6239 * by one of the two routines above. 6240 */ 6241 static struct freefrag * 6242 setup_allocindir_phase2( 6243 struct buf *bp, /* in-memory copy of the indirect block */ 6244 struct inode *ip, /* inode for file being extended */ 6245 struct inodedep *inodedep, /* Inodedep for ip */ 6246 struct allocindir *aip, /* allocindir allocated by the above routines */ 6247 ufs_lbn_t lbn) /* Logical block number for this block. */ 6248 { 6249 struct fs *fs __diagused; 6250 struct indirdep *indirdep; 6251 struct allocindir *oldaip; 6252 struct freefrag *freefrag; 6253 struct mount *mp; 6254 struct ufsmount *ump; 6255 6256 mp = ITOVFS(ip); 6257 ump = VFSTOUFS(mp); 6258 LOCK_OWNED(ump); 6259 fs = ump->um_fs; 6260 if (bp->b_lblkno >= 0) 6261 panic("setup_allocindir_phase2: not indir blk"); 6262 KASSERT(aip->ai_offset >= 0 && aip->ai_offset < NINDIR(fs), 6263 ("setup_allocindir_phase2: Bad offset %d", aip->ai_offset)); 6264 indirdep = indirdep_lookup(mp, ip, bp); 6265 KASSERT(indirdep->ir_savebp != NULL, 6266 ("setup_allocindir_phase2 NULL ir_savebp")); 6267 aip->ai_indirdep = indirdep; 6268 /* 6269 * Check for an unwritten dependency for this indirect offset. If 6270 * there is, merge the old dependency into the new one. This happens 6271 * as a result of reallocblk only. 6272 */ 6273 freefrag = NULL; 6274 if (aip->ai_oldblkno != 0) { 6275 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) { 6276 if (oldaip->ai_offset == aip->ai_offset) { 6277 freefrag = allocindir_merge(aip, oldaip); 6278 goto done; 6279 } 6280 } 6281 LIST_FOREACH(oldaip, &indirdep->ir_donehd, ai_next) { 6282 if (oldaip->ai_offset == aip->ai_offset) { 6283 freefrag = allocindir_merge(aip, oldaip); 6284 goto done; 6285 } 6286 } 6287 } 6288 done: 6289 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next); 6290 return (freefrag); 6291 } 6292 6293 /* 6294 * Merge two allocindirs which refer to the same block. Move newblock 6295 * dependencies and setup the freefrags appropriately. 6296 */ 6297 static struct freefrag * 6298 allocindir_merge( 6299 struct allocindir *aip, 6300 struct allocindir *oldaip) 6301 { 6302 struct freefrag *freefrag; 6303 struct worklist *wk; 6304 6305 if (oldaip->ai_newblkno != aip->ai_oldblkno) 6306 panic("allocindir_merge: blkno"); 6307 aip->ai_oldblkno = oldaip->ai_oldblkno; 6308 freefrag = aip->ai_freefrag; 6309 aip->ai_freefrag = oldaip->ai_freefrag; 6310 oldaip->ai_freefrag = NULL; 6311 KASSERT(freefrag != NULL, ("setup_allocindir_phase2: No freefrag")); 6312 /* 6313 * If we are tracking a new directory-block allocation, 6314 * move it from the old allocindir to the new allocindir. 6315 */ 6316 if ((wk = LIST_FIRST(&oldaip->ai_newdirblk)) != NULL) { 6317 WORKLIST_REMOVE(wk); 6318 if (!LIST_EMPTY(&oldaip->ai_newdirblk)) 6319 panic("allocindir_merge: extra newdirblk"); 6320 WORKLIST_INSERT(&aip->ai_newdirblk, wk); 6321 } 6322 /* 6323 * We can skip journaling for this freefrag and just complete 6324 * any pending journal work for the allocindir that is being 6325 * removed after the freefrag completes. 6326 */ 6327 if (freefrag->ff_jdep) 6328 cancel_jfreefrag(WK_JFREEFRAG(freefrag->ff_jdep)); 6329 LIST_REMOVE(oldaip, ai_next); 6330 freefrag->ff_jdep = (struct worklist *)cancel_newblk(&oldaip->ai_block, 6331 &freefrag->ff_list, &freefrag->ff_jwork); 6332 free_newblk(&oldaip->ai_block); 6333 6334 return (freefrag); 6335 } 6336 6337 static inline void 6338 setup_freedirect( 6339 struct freeblks *freeblks, 6340 struct inode *ip, 6341 int i, 6342 int needj) 6343 { 6344 struct ufsmount *ump; 6345 ufs2_daddr_t blkno; 6346 int frags; 6347 6348 blkno = DIP(ip, i_db[i]); 6349 if (blkno == 0) 6350 return; 6351 DIP_SET(ip, i_db[i], 0); 6352 ump = ITOUMP(ip); 6353 frags = sblksize(ump->um_fs, ip->i_size, i); 6354 frags = numfrags(ump->um_fs, frags); 6355 newfreework(ump, freeblks, NULL, i, blkno, frags, 0, needj); 6356 } 6357 6358 static inline void 6359 setup_freeext( 6360 struct freeblks *freeblks, 6361 struct inode *ip, 6362 int i, 6363 int needj) 6364 { 6365 struct ufsmount *ump; 6366 ufs2_daddr_t blkno; 6367 int frags; 6368 6369 blkno = ip->i_din2->di_extb[i]; 6370 if (blkno == 0) 6371 return; 6372 ip->i_din2->di_extb[i] = 0; 6373 ump = ITOUMP(ip); 6374 frags = sblksize(ump->um_fs, ip->i_din2->di_extsize, i); 6375 frags = numfrags(ump->um_fs, frags); 6376 newfreework(ump, freeblks, NULL, -1 - i, blkno, frags, 0, needj); 6377 } 6378 6379 static inline void 6380 setup_freeindir( 6381 struct freeblks *freeblks, 6382 struct inode *ip, 6383 int i, 6384 ufs_lbn_t lbn, 6385 int needj) 6386 { 6387 struct ufsmount *ump; 6388 ufs2_daddr_t blkno; 6389 6390 blkno = DIP(ip, i_ib[i]); 6391 if (blkno == 0) 6392 return; 6393 DIP_SET(ip, i_ib[i], 0); 6394 ump = ITOUMP(ip); 6395 newfreework(ump, freeblks, NULL, lbn, blkno, ump->um_fs->fs_frag, 6396 0, needj); 6397 } 6398 6399 static inline struct freeblks * 6400 newfreeblks(struct mount *mp, struct inode *ip) 6401 { 6402 struct freeblks *freeblks; 6403 6404 freeblks = malloc(sizeof(struct freeblks), 6405 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO); 6406 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp); 6407 LIST_INIT(&freeblks->fb_jblkdephd); 6408 LIST_INIT(&freeblks->fb_jwork); 6409 freeblks->fb_ref = 0; 6410 freeblks->fb_cgwait = 0; 6411 freeblks->fb_state = ATTACHED; 6412 freeblks->fb_uid = ip->i_uid; 6413 freeblks->fb_inum = ip->i_number; 6414 freeblks->fb_vtype = ITOV(ip)->v_type; 6415 freeblks->fb_modrev = DIP(ip, i_modrev); 6416 freeblks->fb_devvp = ITODEVVP(ip); 6417 freeblks->fb_chkcnt = 0; 6418 freeblks->fb_len = 0; 6419 6420 return (freeblks); 6421 } 6422 6423 static void 6424 trunc_indirdep( 6425 struct indirdep *indirdep, 6426 struct freeblks *freeblks, 6427 struct buf *bp, 6428 int off) 6429 { 6430 struct allocindir *aip, *aipn; 6431 6432 /* 6433 * The first set of allocindirs won't be in savedbp. 6434 */ 6435 LIST_FOREACH_SAFE(aip, &indirdep->ir_deplisthd, ai_next, aipn) 6436 if (aip->ai_offset > off) 6437 cancel_allocindir(aip, bp, freeblks, 1); 6438 LIST_FOREACH_SAFE(aip, &indirdep->ir_donehd, ai_next, aipn) 6439 if (aip->ai_offset > off) 6440 cancel_allocindir(aip, bp, freeblks, 1); 6441 /* 6442 * These will exist in savedbp. 6443 */ 6444 LIST_FOREACH_SAFE(aip, &indirdep->ir_writehd, ai_next, aipn) 6445 if (aip->ai_offset > off) 6446 cancel_allocindir(aip, NULL, freeblks, 0); 6447 LIST_FOREACH_SAFE(aip, &indirdep->ir_completehd, ai_next, aipn) 6448 if (aip->ai_offset > off) 6449 cancel_allocindir(aip, NULL, freeblks, 0); 6450 } 6451 6452 /* 6453 * Follow the chain of indirects down to lastlbn creating a freework 6454 * structure for each. This will be used to start indir_trunc() at 6455 * the right offset and create the journal records for the parrtial 6456 * truncation. A second step will handle the truncated dependencies. 6457 */ 6458 static int 6459 setup_trunc_indir( 6460 struct freeblks *freeblks, 6461 struct inode *ip, 6462 ufs_lbn_t lbn, 6463 ufs_lbn_t lastlbn, 6464 ufs2_daddr_t blkno) 6465 { 6466 struct indirdep *indirdep; 6467 struct indirdep *indirn; 6468 struct freework *freework; 6469 struct newblk *newblk; 6470 struct mount *mp; 6471 struct ufsmount *ump; 6472 struct buf *bp; 6473 uint8_t *start; 6474 uint8_t *end; 6475 ufs_lbn_t lbnadd; 6476 int level; 6477 int error; 6478 int off; 6479 6480 freework = NULL; 6481 if (blkno == 0) 6482 return (0); 6483 mp = freeblks->fb_list.wk_mp; 6484 ump = VFSTOUFS(mp); 6485 /* 6486 * Here, calls to VOP_BMAP() will fail. However, we already have 6487 * the on-disk address, so we just pass it to bread() instead of 6488 * having bread() attempt to calculate it using VOP_BMAP(). 6489 */ 6490 error = ffs_breadz(ump, ITOV(ip), lbn, blkptrtodb(ump, blkno), 6491 (int)mp->mnt_stat.f_iosize, NULL, NULL, 0, NOCRED, 0, NULL, &bp); 6492 if (error) 6493 return (error); 6494 level = lbn_level(lbn); 6495 lbnadd = lbn_offset(ump->um_fs, level); 6496 /* 6497 * Compute the offset of the last block we want to keep. Store 6498 * in the freework the first block we want to completely free. 6499 */ 6500 off = (lastlbn - -(lbn + level)) / lbnadd; 6501 if (off + 1 == NINDIR(ump->um_fs)) 6502 goto nowork; 6503 freework = newfreework(ump, freeblks, NULL, lbn, blkno, 0, off + 1, 0); 6504 /* 6505 * Link the freework into the indirdep. This will prevent any new 6506 * allocations from proceeding until we are finished with the 6507 * truncate and the block is written. 6508 */ 6509 ACQUIRE_LOCK(ump); 6510 indirdep = indirdep_lookup(mp, ip, bp); 6511 if (indirdep->ir_freeblks) 6512 panic("setup_trunc_indir: indirdep already truncated."); 6513 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, freework, fw_next); 6514 freework->fw_indir = indirdep; 6515 /* 6516 * Cancel any allocindirs that will not make it to disk. 6517 * We have to do this for all copies of the indirdep that 6518 * live on this newblk. 6519 */ 6520 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 6521 if (newblk_lookup(mp, dbtofsb(ump->um_fs, bp->b_blkno), 0, 6522 &newblk) == 0) 6523 panic("setup_trunc_indir: lost block"); 6524 LIST_FOREACH(indirn, &newblk->nb_indirdeps, ir_next) 6525 trunc_indirdep(indirn, freeblks, bp, off); 6526 } else 6527 trunc_indirdep(indirdep, freeblks, bp, off); 6528 FREE_LOCK(ump); 6529 /* 6530 * Creation is protected by the buf lock. The saveddata is only 6531 * needed if a full truncation follows a partial truncation but it 6532 * is difficult to allocate in that case so we fetch it anyway. 6533 */ 6534 if (indirdep->ir_saveddata == NULL) 6535 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 6536 M_SOFTDEP_FLAGS); 6537 nowork: 6538 /* Fetch the blkno of the child and the zero start offset. */ 6539 if (I_IS_UFS1(ip)) { 6540 blkno = ((ufs1_daddr_t *)bp->b_data)[off]; 6541 start = (uint8_t *)&((ufs1_daddr_t *)bp->b_data)[off+1]; 6542 } else { 6543 blkno = ((ufs2_daddr_t *)bp->b_data)[off]; 6544 start = (uint8_t *)&((ufs2_daddr_t *)bp->b_data)[off+1]; 6545 } 6546 if (freework) { 6547 /* Zero the truncated pointers. */ 6548 end = bp->b_data + bp->b_bcount; 6549 bzero(start, end - start); 6550 bdwrite(bp); 6551 } else 6552 bqrelse(bp); 6553 if (level == 0) 6554 return (0); 6555 lbn++; /* adjust level */ 6556 lbn -= (off * lbnadd); 6557 return setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno); 6558 } 6559 6560 /* 6561 * Complete the partial truncation of an indirect block setup by 6562 * setup_trunc_indir(). This zeros the truncated pointers in the saved 6563 * copy and writes them to disk before the freeblks is allowed to complete. 6564 */ 6565 static void 6566 complete_trunc_indir(struct freework *freework) 6567 { 6568 struct freework *fwn; 6569 struct indirdep *indirdep; 6570 struct ufsmount *ump; 6571 struct buf *bp; 6572 uintptr_t start; 6573 int count; 6574 6575 ump = VFSTOUFS(freework->fw_list.wk_mp); 6576 LOCK_OWNED(ump); 6577 indirdep = freework->fw_indir; 6578 for (;;) { 6579 bp = indirdep->ir_bp; 6580 /* See if the block was discarded. */ 6581 if (bp == NULL) 6582 break; 6583 /* Inline part of getdirtybuf(). We dont want bremfree. */ 6584 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) 6585 break; 6586 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 6587 LOCK_PTR(ump)) == 0) 6588 BUF_UNLOCK(bp); 6589 ACQUIRE_LOCK(ump); 6590 } 6591 freework->fw_state |= DEPCOMPLETE; 6592 TAILQ_REMOVE(&indirdep->ir_trunc, freework, fw_next); 6593 /* 6594 * Zero the pointers in the saved copy. 6595 */ 6596 if (indirdep->ir_state & UFS1FMT) 6597 start = sizeof(ufs1_daddr_t); 6598 else 6599 start = sizeof(ufs2_daddr_t); 6600 start *= freework->fw_start; 6601 count = indirdep->ir_savebp->b_bcount - start; 6602 start += (uintptr_t)indirdep->ir_savebp->b_data; 6603 bzero((char *)start, count); 6604 /* 6605 * We need to start the next truncation in the list if it has not 6606 * been started yet. 6607 */ 6608 fwn = TAILQ_FIRST(&indirdep->ir_trunc); 6609 if (fwn != NULL) { 6610 if (fwn->fw_freeblks == indirdep->ir_freeblks) 6611 TAILQ_REMOVE(&indirdep->ir_trunc, fwn, fw_next); 6612 if ((fwn->fw_state & ONWORKLIST) == 0) 6613 freework_enqueue(fwn); 6614 } 6615 /* 6616 * If bp is NULL the block was fully truncated, restore 6617 * the saved block list otherwise free it if it is no 6618 * longer needed. 6619 */ 6620 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 6621 if (bp == NULL) 6622 bcopy(indirdep->ir_saveddata, 6623 indirdep->ir_savebp->b_data, 6624 indirdep->ir_savebp->b_bcount); 6625 free(indirdep->ir_saveddata, M_INDIRDEP); 6626 indirdep->ir_saveddata = NULL; 6627 } 6628 /* 6629 * When bp is NULL there is a full truncation pending. We 6630 * must wait for this full truncation to be journaled before 6631 * we can release this freework because the disk pointers will 6632 * never be written as zero. 6633 */ 6634 if (bp == NULL) { 6635 if (LIST_EMPTY(&indirdep->ir_freeblks->fb_jblkdephd)) 6636 handle_written_freework(freework); 6637 else 6638 WORKLIST_INSERT(&indirdep->ir_freeblks->fb_freeworkhd, 6639 &freework->fw_list); 6640 if (fwn == NULL) { 6641 freework->fw_indir = (void *)0x0000deadbeef0000; 6642 bp = indirdep->ir_savebp; 6643 indirdep->ir_savebp = NULL; 6644 free_indirdep(indirdep); 6645 FREE_LOCK(ump); 6646 brelse(bp); 6647 ACQUIRE_LOCK(ump); 6648 } 6649 } else { 6650 /* Complete when the real copy is written. */ 6651 WORKLIST_INSERT(&bp->b_dep, &freework->fw_list); 6652 BUF_UNLOCK(bp); 6653 } 6654 } 6655 6656 /* 6657 * Calculate the number of blocks we are going to release where datablocks 6658 * is the current total and length is the new file size. 6659 */ 6660 static ufs2_daddr_t 6661 blkcount(struct fs *fs, 6662 ufs2_daddr_t datablocks, 6663 off_t length) 6664 { 6665 off_t totblks, numblks; 6666 6667 totblks = 0; 6668 numblks = howmany(length, fs->fs_bsize); 6669 if (numblks <= UFS_NDADDR) { 6670 totblks = howmany(length, fs->fs_fsize); 6671 goto out; 6672 } 6673 totblks = blkstofrags(fs, numblks); 6674 numblks -= UFS_NDADDR; 6675 /* 6676 * Count all single, then double, then triple indirects required. 6677 * Subtracting one indirects worth of blocks for each pass 6678 * acknowledges one of each pointed to by the inode. 6679 */ 6680 for (;;) { 6681 totblks += blkstofrags(fs, howmany(numblks, NINDIR(fs))); 6682 numblks -= NINDIR(fs); 6683 if (numblks <= 0) 6684 break; 6685 numblks = howmany(numblks, NINDIR(fs)); 6686 } 6687 out: 6688 totblks = fsbtodb(fs, totblks); 6689 /* 6690 * Handle sparse files. We can't reclaim more blocks than the inode 6691 * references. We will correct it later in handle_complete_freeblks() 6692 * when we know the real count. 6693 */ 6694 if (totblks > datablocks) 6695 return (0); 6696 return (datablocks - totblks); 6697 } 6698 6699 /* 6700 * Handle freeblocks for journaled softupdate filesystems. 6701 * 6702 * Contrary to normal softupdates, we must preserve the block pointers in 6703 * indirects until their subordinates are free. This is to avoid journaling 6704 * every block that is freed which may consume more space than the journal 6705 * itself. The recovery program will see the free block journals at the 6706 * base of the truncated area and traverse them to reclaim space. The 6707 * pointers in the inode may be cleared immediately after the journal 6708 * records are written because each direct and indirect pointer in the 6709 * inode is recorded in a journal. This permits full truncation to proceed 6710 * asynchronously. The write order is journal -> inode -> cgs -> indirects. 6711 * 6712 * The algorithm is as follows: 6713 * 1) Traverse the in-memory state and create journal entries to release 6714 * the relevant blocks and full indirect trees. 6715 * 2) Traverse the indirect block chain adding partial truncation freework 6716 * records to indirects in the path to lastlbn. The freework will 6717 * prevent new allocation dependencies from being satisfied in this 6718 * indirect until the truncation completes. 6719 * 3) Read and lock the inode block, performing an update with the new size 6720 * and pointers. This prevents truncated data from becoming valid on 6721 * disk through step 4. 6722 * 4) Reap unsatisfied dependencies that are beyond the truncated area, 6723 * eliminate journal work for those records that do not require it. 6724 * 5) Schedule the journal records to be written followed by the inode block. 6725 * 6) Allocate any necessary frags for the end of file. 6726 * 7) Zero any partially truncated blocks. 6727 * 6728 * From this truncation proceeds asynchronously using the freework and 6729 * indir_trunc machinery. The file will not be extended again into a 6730 * partially truncated indirect block until all work is completed but 6731 * the normal dependency mechanism ensures that it is rolled back/forward 6732 * as appropriate. Further truncation may occur without delay and is 6733 * serialized in indir_trunc(). 6734 */ 6735 void 6736 softdep_journal_freeblocks( 6737 struct inode *ip, /* The inode whose length is to be reduced */ 6738 struct ucred *cred, 6739 off_t length, /* The new length for the file */ 6740 int flags) /* IO_EXT and/or IO_NORMAL */ 6741 { 6742 struct freeblks *freeblks, *fbn; 6743 struct worklist *wk, *wkn; 6744 struct inodedep *inodedep; 6745 struct jblkdep *jblkdep; 6746 struct allocdirect *adp, *adpn; 6747 struct ufsmount *ump; 6748 struct fs *fs; 6749 struct buf *bp; 6750 struct vnode *vp; 6751 struct mount *mp; 6752 daddr_t dbn; 6753 ufs2_daddr_t extblocks, datablocks; 6754 ufs_lbn_t tmpval, lbn, lastlbn; 6755 int frags, lastoff, iboff, allocblock, needj, error, i; 6756 6757 ump = ITOUMP(ip); 6758 mp = UFSTOVFS(ump); 6759 fs = ump->um_fs; 6760 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6761 ("softdep_journal_freeblocks called on non-softdep filesystem")); 6762 vp = ITOV(ip); 6763 needj = 1; 6764 iboff = -1; 6765 allocblock = 0; 6766 extblocks = 0; 6767 datablocks = 0; 6768 frags = 0; 6769 freeblks = newfreeblks(mp, ip); 6770 ACQUIRE_LOCK(ump); 6771 /* 6772 * If we're truncating a removed file that will never be written 6773 * we don't need to journal the block frees. The canceled journals 6774 * for the allocations will suffice. 6775 */ 6776 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6777 if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED && 6778 length == 0) 6779 needj = 0; 6780 CTR3(KTR_SUJ, "softdep_journal_freeblks: ip %d length %ld needj %d", 6781 ip->i_number, length, needj); 6782 FREE_LOCK(ump); 6783 /* 6784 * Calculate the lbn that we are truncating to. This results in -1 6785 * if we're truncating the 0 bytes. So it is the last lbn we want 6786 * to keep, not the first lbn we want to truncate. 6787 */ 6788 lastlbn = lblkno(fs, length + fs->fs_bsize - 1) - 1; 6789 lastoff = blkoff(fs, length); 6790 /* 6791 * Compute frags we are keeping in lastlbn. 0 means all. 6792 */ 6793 if (lastlbn >= 0 && lastlbn < UFS_NDADDR) { 6794 frags = fragroundup(fs, lastoff); 6795 /* adp offset of last valid allocdirect. */ 6796 iboff = lastlbn; 6797 } else if (lastlbn > 0) 6798 iboff = UFS_NDADDR; 6799 if (fs->fs_magic == FS_UFS2_MAGIC) 6800 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 6801 /* 6802 * Handle normal data blocks and indirects. This section saves 6803 * values used after the inode update to complete frag and indirect 6804 * truncation. 6805 */ 6806 if ((flags & IO_NORMAL) != 0) { 6807 /* 6808 * Handle truncation of whole direct and indirect blocks. 6809 */ 6810 for (i = iboff + 1; i < UFS_NDADDR; i++) 6811 setup_freedirect(freeblks, ip, i, needj); 6812 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR; 6813 i < UFS_NIADDR; 6814 i++, lbn += tmpval, tmpval *= NINDIR(fs)) { 6815 /* Release a whole indirect tree. */ 6816 if (lbn > lastlbn) { 6817 setup_freeindir(freeblks, ip, i, -lbn -i, 6818 needj); 6819 continue; 6820 } 6821 iboff = i + UFS_NDADDR; 6822 /* 6823 * Traverse partially truncated indirect tree. 6824 */ 6825 if (lbn <= lastlbn && lbn + tmpval - 1 > lastlbn) 6826 setup_trunc_indir(freeblks, ip, -lbn - i, 6827 lastlbn, DIP(ip, i_ib[i])); 6828 } 6829 /* 6830 * Handle partial truncation to a frag boundary. 6831 */ 6832 if (frags) { 6833 ufs2_daddr_t blkno; 6834 long oldfrags; 6835 6836 oldfrags = blksize(fs, ip, lastlbn); 6837 blkno = DIP(ip, i_db[lastlbn]); 6838 if (blkno && oldfrags != frags) { 6839 oldfrags -= frags; 6840 oldfrags = numfrags(fs, oldfrags); 6841 blkno += numfrags(fs, frags); 6842 newfreework(ump, freeblks, NULL, lastlbn, 6843 blkno, oldfrags, 0, needj); 6844 if (needj) 6845 adjust_newfreework(freeblks, 6846 numfrags(fs, frags)); 6847 } else if (blkno == 0) 6848 allocblock = 1; 6849 } 6850 /* 6851 * Add a journal record for partial truncate if we are 6852 * handling indirect blocks. Non-indirects need no extra 6853 * journaling. 6854 */ 6855 if (length != 0 && lastlbn >= UFS_NDADDR) { 6856 UFS_INODE_SET_FLAG(ip, IN_TRUNCATED); 6857 newjtrunc(freeblks, length, 0); 6858 } 6859 ip->i_size = length; 6860 DIP_SET(ip, i_size, ip->i_size); 6861 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE); 6862 datablocks = DIP(ip, i_blocks) - extblocks; 6863 if (length != 0) 6864 datablocks = blkcount(fs, datablocks, length); 6865 freeblks->fb_len = length; 6866 } 6867 if ((flags & IO_EXT) != 0) { 6868 for (i = 0; i < UFS_NXADDR; i++) 6869 setup_freeext(freeblks, ip, i, needj); 6870 ip->i_din2->di_extsize = 0; 6871 datablocks += extblocks; 6872 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE); 6873 } 6874 #ifdef QUOTA 6875 /* Reference the quotas in case the block count is wrong in the end. */ 6876 quotaref(vp, freeblks->fb_quota); 6877 (void) chkdq(ip, -datablocks, NOCRED, FORCE); 6878 #endif 6879 freeblks->fb_chkcnt = -datablocks; 6880 UFS_LOCK(ump); 6881 fs->fs_pendingblocks += datablocks; 6882 UFS_UNLOCK(ump); 6883 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 6884 /* 6885 * Handle truncation of incomplete alloc direct dependencies. We 6886 * hold the inode block locked to prevent incomplete dependencies 6887 * from reaching the disk while we are eliminating those that 6888 * have been truncated. This is a partially inlined ffs_update(). 6889 */ 6890 ufs_itimes(vp); 6891 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED); 6892 dbn = fsbtodb(fs, ino_to_fsba(fs, ip->i_number)); 6893 error = ffs_breadz(ump, ump->um_devvp, dbn, dbn, (int)fs->fs_bsize, 6894 NULL, NULL, 0, cred, 0, NULL, &bp); 6895 if (error) { 6896 softdep_error("softdep_journal_freeblocks", error); 6897 return; 6898 } 6899 if (bp->b_bufsize == fs->fs_bsize) 6900 bp->b_flags |= B_CLUSTEROK; 6901 softdep_update_inodeblock(ip, bp, 0); 6902 if (ump->um_fstype == UFS1) { 6903 *((struct ufs1_dinode *)bp->b_data + 6904 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1; 6905 } else { 6906 ffs_update_dinode_ckhash(fs, ip->i_din2); 6907 *((struct ufs2_dinode *)bp->b_data + 6908 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2; 6909 } 6910 ACQUIRE_LOCK(ump); 6911 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6912 if ((inodedep->id_state & IOSTARTED) != 0) 6913 panic("softdep_setup_freeblocks: inode busy"); 6914 /* 6915 * Add the freeblks structure to the list of operations that 6916 * must await the zero'ed inode being written to disk. If we 6917 * still have a bitmap dependency (needj), then the inode 6918 * has never been written to disk, so we can process the 6919 * freeblks below once we have deleted the dependencies. 6920 */ 6921 if (needj) 6922 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 6923 else 6924 freeblks->fb_state |= COMPLETE; 6925 if ((flags & IO_NORMAL) != 0) { 6926 TAILQ_FOREACH_SAFE(adp, &inodedep->id_inoupdt, ad_next, adpn) { 6927 if (adp->ad_offset > iboff) 6928 cancel_allocdirect(&inodedep->id_inoupdt, adp, 6929 freeblks); 6930 /* 6931 * Truncate the allocdirect. We could eliminate 6932 * or modify journal records as well. 6933 */ 6934 else if (adp->ad_offset == iboff && frags) 6935 adp->ad_newsize = frags; 6936 } 6937 } 6938 if ((flags & IO_EXT) != 0) 6939 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 6940 cancel_allocdirect(&inodedep->id_extupdt, adp, 6941 freeblks); 6942 /* 6943 * Scan the bufwait list for newblock dependencies that will never 6944 * make it to disk. 6945 */ 6946 LIST_FOREACH_SAFE(wk, &inodedep->id_bufwait, wk_list, wkn) { 6947 if (wk->wk_type != D_ALLOCDIRECT) 6948 continue; 6949 adp = WK_ALLOCDIRECT(wk); 6950 if (((flags & IO_NORMAL) != 0 && (adp->ad_offset > iboff)) || 6951 ((flags & IO_EXT) != 0 && (adp->ad_state & EXTDATA))) { 6952 cancel_jfreeblk(freeblks, adp->ad_newblkno); 6953 cancel_newblk(WK_NEWBLK(wk), NULL, &freeblks->fb_jwork); 6954 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 6955 } 6956 } 6957 /* 6958 * Add journal work. 6959 */ 6960 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) 6961 add_to_journal(&jblkdep->jb_list); 6962 FREE_LOCK(ump); 6963 bdwrite(bp); 6964 /* 6965 * Truncate dependency structures beyond length. 6966 */ 6967 trunc_dependencies(ip, freeblks, lastlbn, frags, flags); 6968 /* 6969 * This is only set when we need to allocate a fragment because 6970 * none existed at the end of a frag-sized file. It handles only 6971 * allocating a new, zero filled block. 6972 */ 6973 if (allocblock) { 6974 ip->i_size = length - lastoff; 6975 DIP_SET(ip, i_size, ip->i_size); 6976 error = UFS_BALLOC(vp, length - 1, 1, cred, BA_CLRBUF, &bp); 6977 if (error != 0) { 6978 softdep_error("softdep_journal_freeblks", error); 6979 return; 6980 } 6981 ip->i_size = length; 6982 DIP_SET(ip, i_size, length); 6983 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE); 6984 allocbuf(bp, frags); 6985 ffs_update(vp, 0); 6986 bawrite(bp); 6987 } else if (lastoff != 0 && vp->v_type != VDIR) { 6988 int size; 6989 6990 /* 6991 * Zero the end of a truncated frag or block. 6992 */ 6993 size = sblksize(fs, length, lastlbn); 6994 error = bread(vp, lastlbn, size, cred, &bp); 6995 if (error == 0) { 6996 bzero((char *)bp->b_data + lastoff, size - lastoff); 6997 bawrite(bp); 6998 } else if (!ffs_fsfail_cleanup(ump, error)) { 6999 softdep_error("softdep_journal_freeblks", error); 7000 return; 7001 } 7002 } 7003 ACQUIRE_LOCK(ump); 7004 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 7005 TAILQ_INSERT_TAIL(&inodedep->id_freeblklst, freeblks, fb_next); 7006 freeblks->fb_state |= DEPCOMPLETE | ONDEPLIST; 7007 /* 7008 * We zero earlier truncations so they don't erroneously 7009 * update i_blocks. 7010 */ 7011 if (freeblks->fb_len == 0 && (flags & IO_NORMAL) != 0) 7012 TAILQ_FOREACH(fbn, &inodedep->id_freeblklst, fb_next) 7013 fbn->fb_len = 0; 7014 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE && 7015 LIST_EMPTY(&freeblks->fb_jblkdephd)) 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 /* 7027 * Flush a JOP_SYNC to the journal. 7028 */ 7029 void 7030 softdep_journal_fsync(struct inode *ip) 7031 { 7032 struct jfsync *jfsync; 7033 struct ufsmount *ump; 7034 7035 ump = ITOUMP(ip); 7036 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 7037 ("softdep_journal_fsync called on non-softdep filesystem")); 7038 if ((ip->i_flag & IN_TRUNCATED) == 0) 7039 return; 7040 ip->i_flag &= ~IN_TRUNCATED; 7041 jfsync = malloc(sizeof(*jfsync), M_JFSYNC, M_SOFTDEP_FLAGS | M_ZERO); 7042 workitem_alloc(&jfsync->jfs_list, D_JFSYNC, UFSTOVFS(ump)); 7043 jfsync->jfs_size = ip->i_size; 7044 jfsync->jfs_ino = ip->i_number; 7045 ACQUIRE_LOCK(ump); 7046 add_to_journal(&jfsync->jfs_list); 7047 jwait(&jfsync->jfs_list, MNT_WAIT); 7048 FREE_LOCK(ump); 7049 } 7050 7051 /* 7052 * Block de-allocation dependencies. 7053 * 7054 * When blocks are de-allocated, the on-disk pointers must be nullified before 7055 * the blocks are made available for use by other files. (The true 7056 * requirement is that old pointers must be nullified before new on-disk 7057 * pointers are set. We chose this slightly more stringent requirement to 7058 * reduce complexity.) Our implementation handles this dependency by updating 7059 * the inode (or indirect block) appropriately but delaying the actual block 7060 * de-allocation (i.e., freemap and free space count manipulation) until 7061 * after the updated versions reach stable storage. After the disk is 7062 * updated, the blocks can be safely de-allocated whenever it is convenient. 7063 * This implementation handles only the common case of reducing a file's 7064 * length to zero. Other cases are handled by the conventional synchronous 7065 * write approach. 7066 * 7067 * The ffs implementation with which we worked double-checks 7068 * the state of the block pointers and file size as it reduces 7069 * a file's length. Some of this code is replicated here in our 7070 * soft updates implementation. The freeblks->fb_chkcnt field is 7071 * used to transfer a part of this information to the procedure 7072 * that eventually de-allocates the blocks. 7073 * 7074 * This routine should be called from the routine that shortens 7075 * a file's length, before the inode's size or block pointers 7076 * are modified. It will save the block pointer information for 7077 * later release and zero the inode so that the calling routine 7078 * can release it. 7079 */ 7080 void 7081 softdep_setup_freeblocks( 7082 struct inode *ip, /* The inode whose length is to be reduced */ 7083 off_t length, /* The new length for the file */ 7084 int flags) /* IO_EXT and/or IO_NORMAL */ 7085 { 7086 struct ufs1_dinode *dp1; 7087 struct ufs2_dinode *dp2; 7088 struct freeblks *freeblks; 7089 struct inodedep *inodedep; 7090 struct allocdirect *adp; 7091 struct ufsmount *ump; 7092 struct buf *bp; 7093 struct fs *fs; 7094 ufs2_daddr_t extblocks, datablocks; 7095 struct mount *mp; 7096 int i, delay, error; 7097 ufs_lbn_t tmpval; 7098 ufs_lbn_t lbn; 7099 7100 ump = ITOUMP(ip); 7101 mp = UFSTOVFS(ump); 7102 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 7103 ("softdep_setup_freeblocks called on non-softdep filesystem")); 7104 CTR2(KTR_SUJ, "softdep_setup_freeblks: ip %d length %ld", 7105 ip->i_number, length); 7106 KASSERT(length == 0, ("softdep_setup_freeblocks: non-zero length")); 7107 fs = ump->um_fs; 7108 if ((error = bread(ump->um_devvp, 7109 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 7110 (int)fs->fs_bsize, NOCRED, &bp)) != 0) { 7111 if (!ffs_fsfail_cleanup(ump, error)) 7112 softdep_error("softdep_setup_freeblocks", error); 7113 return; 7114 } 7115 freeblks = newfreeblks(mp, ip); 7116 extblocks = 0; 7117 datablocks = 0; 7118 if (fs->fs_magic == FS_UFS2_MAGIC) 7119 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 7120 if ((flags & IO_NORMAL) != 0) { 7121 for (i = 0; i < UFS_NDADDR; i++) 7122 setup_freedirect(freeblks, ip, i, 0); 7123 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR; 7124 i < UFS_NIADDR; 7125 i++, lbn += tmpval, tmpval *= NINDIR(fs)) 7126 setup_freeindir(freeblks, ip, i, -lbn -i, 0); 7127 ip->i_size = 0; 7128 DIP_SET(ip, i_size, 0); 7129 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE); 7130 datablocks = DIP(ip, i_blocks) - extblocks; 7131 } 7132 if ((flags & IO_EXT) != 0) { 7133 for (i = 0; i < UFS_NXADDR; i++) 7134 setup_freeext(freeblks, ip, i, 0); 7135 ip->i_din2->di_extsize = 0; 7136 datablocks += extblocks; 7137 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE); 7138 } 7139 #ifdef QUOTA 7140 /* Reference the quotas in case the block count is wrong in the end. */ 7141 quotaref(ITOV(ip), freeblks->fb_quota); 7142 (void) chkdq(ip, -datablocks, NOCRED, FORCE); 7143 #endif 7144 freeblks->fb_chkcnt = -datablocks; 7145 UFS_LOCK(ump); 7146 fs->fs_pendingblocks += datablocks; 7147 UFS_UNLOCK(ump); 7148 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 7149 /* 7150 * Push the zero'ed inode to its disk buffer so that we are free 7151 * to delete its dependencies below. Once the dependencies are gone 7152 * the buffer can be safely released. 7153 */ 7154 if (ump->um_fstype == UFS1) { 7155 dp1 = ((struct ufs1_dinode *)bp->b_data + 7156 ino_to_fsbo(fs, ip->i_number)); 7157 ip->i_din1->di_freelink = dp1->di_freelink; 7158 *dp1 = *ip->i_din1; 7159 } else { 7160 dp2 = ((struct ufs2_dinode *)bp->b_data + 7161 ino_to_fsbo(fs, ip->i_number)); 7162 ip->i_din2->di_freelink = dp2->di_freelink; 7163 ffs_update_dinode_ckhash(fs, ip->i_din2); 7164 *dp2 = *ip->i_din2; 7165 } 7166 /* 7167 * Find and eliminate any inode dependencies. 7168 */ 7169 ACQUIRE_LOCK(ump); 7170 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 7171 if ((inodedep->id_state & IOSTARTED) != 0) 7172 panic("softdep_setup_freeblocks: inode busy"); 7173 /* 7174 * Add the freeblks structure to the list of operations that 7175 * must await the zero'ed inode being written to disk. If we 7176 * still have a bitmap dependency (delay == 0), then the inode 7177 * has never been written to disk, so we can process the 7178 * freeblks below once we have deleted the dependencies. 7179 */ 7180 delay = (inodedep->id_state & DEPCOMPLETE); 7181 if (delay) 7182 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 7183 else 7184 freeblks->fb_state |= COMPLETE; 7185 /* 7186 * Because the file length has been truncated to zero, any 7187 * pending block allocation dependency structures associated 7188 * with this inode are obsolete and can simply be de-allocated. 7189 * We must first merge the two dependency lists to get rid of 7190 * any duplicate freefrag structures, then purge the merged list. 7191 * If we still have a bitmap dependency, then the inode has never 7192 * been written to disk, so we can free any fragments without delay. 7193 */ 7194 if (flags & IO_NORMAL) { 7195 merge_inode_lists(&inodedep->id_newinoupdt, 7196 &inodedep->id_inoupdt); 7197 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 7198 cancel_allocdirect(&inodedep->id_inoupdt, adp, 7199 freeblks); 7200 } 7201 if (flags & IO_EXT) { 7202 merge_inode_lists(&inodedep->id_newextupdt, 7203 &inodedep->id_extupdt); 7204 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 7205 cancel_allocdirect(&inodedep->id_extupdt, adp, 7206 freeblks); 7207 } 7208 FREE_LOCK(ump); 7209 bdwrite(bp); 7210 trunc_dependencies(ip, freeblks, -1, 0, flags); 7211 ACQUIRE_LOCK(ump); 7212 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 7213 (void) free_inodedep(inodedep); 7214 freeblks->fb_state |= DEPCOMPLETE; 7215 /* 7216 * If the inode with zeroed block pointers is now on disk 7217 * we can start freeing blocks. 7218 */ 7219 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 7220 freeblks->fb_state |= INPROGRESS; 7221 else 7222 freeblks = NULL; 7223 FREE_LOCK(ump); 7224 if (freeblks) 7225 handle_workitem_freeblocks(freeblks, 0); 7226 trunc_pages(ip, length, extblocks, flags); 7227 } 7228 7229 /* 7230 * Eliminate pages from the page cache that back parts of this inode and 7231 * adjust the vnode pager's idea of our size. This prevents stale data 7232 * from hanging around in the page cache. 7233 */ 7234 static void 7235 trunc_pages( 7236 struct inode *ip, 7237 off_t length, 7238 ufs2_daddr_t extblocks, 7239 int flags) 7240 { 7241 struct vnode *vp; 7242 struct fs *fs; 7243 ufs_lbn_t lbn; 7244 off_t end, extend; 7245 7246 vp = ITOV(ip); 7247 fs = ITOFS(ip); 7248 extend = OFF_TO_IDX(lblktosize(fs, -extblocks)); 7249 if ((flags & IO_EXT) != 0) 7250 vn_pages_remove(vp, extend, 0); 7251 if ((flags & IO_NORMAL) == 0) 7252 return; 7253 BO_LOCK(&vp->v_bufobj); 7254 drain_output(vp); 7255 BO_UNLOCK(&vp->v_bufobj); 7256 /* 7257 * The vnode pager eliminates file pages we eliminate indirects 7258 * below. 7259 */ 7260 vnode_pager_setsize(vp, length); 7261 /* 7262 * Calculate the end based on the last indirect we want to keep. If 7263 * the block extends into indirects we can just use the negative of 7264 * its lbn. Doubles and triples exist at lower numbers so we must 7265 * be careful not to remove those, if they exist. double and triple 7266 * indirect lbns do not overlap with others so it is not important 7267 * to verify how many levels are required. 7268 */ 7269 lbn = lblkno(fs, length); 7270 if (lbn >= UFS_NDADDR) { 7271 /* Calculate the virtual lbn of the triple indirect. */ 7272 lbn = -lbn - (UFS_NIADDR - 1); 7273 end = OFF_TO_IDX(lblktosize(fs, lbn)); 7274 } else 7275 end = extend; 7276 vn_pages_remove(vp, OFF_TO_IDX(OFF_MAX), end); 7277 } 7278 7279 /* 7280 * See if the buf bp is in the range eliminated by truncation. 7281 */ 7282 static int 7283 trunc_check_buf( 7284 struct buf *bp, 7285 int *blkoffp, 7286 ufs_lbn_t lastlbn, 7287 int lastoff, 7288 int flags) 7289 { 7290 ufs_lbn_t lbn; 7291 7292 *blkoffp = 0; 7293 /* Only match ext/normal blocks as appropriate. */ 7294 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) || 7295 ((flags & IO_NORMAL) == 0 && (bp->b_xflags & BX_ALTDATA) == 0)) 7296 return (0); 7297 /* ALTDATA is always a full truncation. */ 7298 if ((bp->b_xflags & BX_ALTDATA) != 0) 7299 return (1); 7300 /* -1 is full truncation. */ 7301 if (lastlbn == -1) 7302 return (1); 7303 /* 7304 * If this is a partial truncate we only want those 7305 * blocks and indirect blocks that cover the range 7306 * we're after. 7307 */ 7308 lbn = bp->b_lblkno; 7309 if (lbn < 0) 7310 lbn = -(lbn + lbn_level(lbn)); 7311 if (lbn < lastlbn) 7312 return (0); 7313 /* Here we only truncate lblkno if it's partial. */ 7314 if (lbn == lastlbn) { 7315 if (lastoff == 0) 7316 return (0); 7317 *blkoffp = lastoff; 7318 } 7319 return (1); 7320 } 7321 7322 /* 7323 * Eliminate any dependencies that exist in memory beyond lblkno:off 7324 */ 7325 static void 7326 trunc_dependencies( 7327 struct inode *ip, 7328 struct freeblks *freeblks, 7329 ufs_lbn_t lastlbn, 7330 int lastoff, 7331 int flags) 7332 { 7333 struct bufobj *bo; 7334 struct vnode *vp; 7335 struct buf *bp; 7336 int blkoff; 7337 7338 /* 7339 * We must wait for any I/O in progress to finish so that 7340 * all potential buffers on the dirty list will be visible. 7341 * Once they are all there, walk the list and get rid of 7342 * any dependencies. 7343 */ 7344 vp = ITOV(ip); 7345 bo = &vp->v_bufobj; 7346 BO_LOCK(bo); 7347 drain_output(vp); 7348 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) 7349 bp->b_vflags &= ~BV_SCANNED; 7350 restart: 7351 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) { 7352 if (bp->b_vflags & BV_SCANNED) 7353 continue; 7354 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 7355 bp->b_vflags |= BV_SCANNED; 7356 continue; 7357 } 7358 KASSERT(bp->b_bufobj == bo, ("Wrong object in buffer")); 7359 if ((bp = getdirtybuf(bp, BO_LOCKPTR(bo), MNT_WAIT)) == NULL) 7360 goto restart; 7361 BO_UNLOCK(bo); 7362 if (deallocate_dependencies(bp, freeblks, blkoff)) 7363 bqrelse(bp); 7364 else 7365 brelse(bp); 7366 BO_LOCK(bo); 7367 goto restart; 7368 } 7369 /* 7370 * Now do the work of vtruncbuf while also matching indirect blocks. 7371 */ 7372 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) 7373 bp->b_vflags &= ~BV_SCANNED; 7374 cleanrestart: 7375 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) { 7376 if (bp->b_vflags & BV_SCANNED) 7377 continue; 7378 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 7379 bp->b_vflags |= BV_SCANNED; 7380 continue; 7381 } 7382 if (BUF_LOCK(bp, 7383 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 7384 BO_LOCKPTR(bo)) == ENOLCK) { 7385 BO_LOCK(bo); 7386 goto cleanrestart; 7387 } 7388 BO_LOCK(bo); 7389 bp->b_vflags |= BV_SCANNED; 7390 BO_UNLOCK(bo); 7391 bremfree(bp); 7392 if (blkoff != 0) { 7393 allocbuf(bp, blkoff); 7394 bqrelse(bp); 7395 } else { 7396 bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF; 7397 brelse(bp); 7398 } 7399 BO_LOCK(bo); 7400 goto cleanrestart; 7401 } 7402 drain_output(vp); 7403 BO_UNLOCK(bo); 7404 } 7405 7406 static int 7407 cancel_pagedep( 7408 struct pagedep *pagedep, 7409 struct freeblks *freeblks, 7410 int blkoff) 7411 { 7412 struct jremref *jremref; 7413 struct jmvref *jmvref; 7414 struct dirrem *dirrem, *tmp; 7415 int i; 7416 7417 /* 7418 * Copy any directory remove dependencies to the list 7419 * to be processed after the freeblks proceeds. If 7420 * directory entry never made it to disk they 7421 * can be dumped directly onto the work list. 7422 */ 7423 LIST_FOREACH_SAFE(dirrem, &pagedep->pd_dirremhd, dm_next, tmp) { 7424 /* Skip this directory removal if it is intended to remain. */ 7425 if (dirrem->dm_offset < blkoff) 7426 continue; 7427 /* 7428 * If there are any dirrems we wait for the journal write 7429 * to complete and then restart the buf scan as the lock 7430 * has been dropped. 7431 */ 7432 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) { 7433 jwait(&jremref->jr_list, MNT_WAIT); 7434 return (ERESTART); 7435 } 7436 LIST_REMOVE(dirrem, dm_next); 7437 dirrem->dm_dirinum = pagedep->pd_ino; 7438 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list); 7439 } 7440 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) { 7441 jwait(&jmvref->jm_list, MNT_WAIT); 7442 return (ERESTART); 7443 } 7444 /* 7445 * When we're partially truncating a pagedep we just want to flush 7446 * journal entries and return. There can not be any adds in the 7447 * truncated portion of the directory and newblk must remain if 7448 * part of the block remains. 7449 */ 7450 if (blkoff != 0) { 7451 struct diradd *dap; 7452 7453 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 7454 if (dap->da_offset > blkoff) 7455 panic("cancel_pagedep: diradd %p off %d > %d", 7456 dap, dap->da_offset, blkoff); 7457 for (i = 0; i < DAHASHSZ; i++) 7458 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) 7459 if (dap->da_offset > blkoff) 7460 panic("cancel_pagedep: diradd %p off %d > %d", 7461 dap, dap->da_offset, blkoff); 7462 return (0); 7463 } 7464 /* 7465 * There should be no directory add dependencies present 7466 * as the directory could not be truncated until all 7467 * children were removed. 7468 */ 7469 KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL, 7470 ("deallocate_dependencies: pendinghd != NULL")); 7471 for (i = 0; i < DAHASHSZ; i++) 7472 KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL, 7473 ("deallocate_dependencies: diraddhd != NULL")); 7474 if ((pagedep->pd_state & NEWBLOCK) != 0) 7475 free_newdirblk(pagedep->pd_newdirblk); 7476 if (free_pagedep(pagedep) == 0) 7477 panic("Failed to free pagedep %p", pagedep); 7478 return (0); 7479 } 7480 7481 /* 7482 * Reclaim any dependency structures from a buffer that is about to 7483 * be reallocated to a new vnode. The buffer must be locked, thus, 7484 * no I/O completion operations can occur while we are manipulating 7485 * its associated dependencies. The mutex is held so that other I/O's 7486 * associated with related dependencies do not occur. 7487 */ 7488 static int 7489 deallocate_dependencies( 7490 struct buf *bp, 7491 struct freeblks *freeblks, 7492 int off) 7493 { 7494 struct indirdep *indirdep; 7495 struct pagedep *pagedep; 7496 struct worklist *wk, *wkn; 7497 struct ufsmount *ump; 7498 7499 ump = softdep_bp_to_mp(bp); 7500 if (ump == NULL) 7501 goto done; 7502 ACQUIRE_LOCK(ump); 7503 LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) { 7504 switch (wk->wk_type) { 7505 case D_INDIRDEP: 7506 indirdep = WK_INDIRDEP(wk); 7507 if (bp->b_lblkno >= 0 || 7508 bp->b_blkno != indirdep->ir_savebp->b_lblkno) 7509 panic("deallocate_dependencies: not indir"); 7510 cancel_indirdep(indirdep, bp, freeblks); 7511 continue; 7512 7513 case D_PAGEDEP: 7514 pagedep = WK_PAGEDEP(wk); 7515 if (cancel_pagedep(pagedep, freeblks, off)) { 7516 FREE_LOCK(ump); 7517 return (ERESTART); 7518 } 7519 continue; 7520 7521 case D_ALLOCINDIR: 7522 /* 7523 * Simply remove the allocindir, we'll find it via 7524 * the indirdep where we can clear pointers if 7525 * needed. 7526 */ 7527 WORKLIST_REMOVE(wk); 7528 continue; 7529 7530 case D_FREEWORK: 7531 /* 7532 * A truncation is waiting for the zero'd pointers 7533 * to be written. It can be freed when the freeblks 7534 * is journaled. 7535 */ 7536 WORKLIST_REMOVE(wk); 7537 wk->wk_state |= ONDEPLIST; 7538 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 7539 break; 7540 7541 case D_ALLOCDIRECT: 7542 if (off != 0) 7543 continue; 7544 /* FALLTHROUGH */ 7545 default: 7546 panic("deallocate_dependencies: Unexpected type %s", 7547 TYPENAME(wk->wk_type)); 7548 /* NOTREACHED */ 7549 } 7550 } 7551 FREE_LOCK(ump); 7552 done: 7553 /* 7554 * Don't throw away this buf, we were partially truncating and 7555 * some deps may always remain. 7556 */ 7557 if (off) { 7558 allocbuf(bp, off); 7559 bp->b_vflags |= BV_SCANNED; 7560 return (EBUSY); 7561 } 7562 bp->b_flags |= B_INVAL | B_NOCACHE; 7563 7564 return (0); 7565 } 7566 7567 /* 7568 * An allocdirect is being canceled due to a truncate. We must make sure 7569 * the journal entry is released in concert with the blkfree that releases 7570 * the storage. Completed journal entries must not be released until the 7571 * space is no longer pointed to by the inode or in the bitmap. 7572 */ 7573 static void 7574 cancel_allocdirect( 7575 struct allocdirectlst *adphead, 7576 struct allocdirect *adp, 7577 struct freeblks *freeblks) 7578 { 7579 struct freework *freework; 7580 struct newblk *newblk; 7581 struct worklist *wk; 7582 7583 TAILQ_REMOVE(adphead, adp, ad_next); 7584 newblk = (struct newblk *)adp; 7585 freework = NULL; 7586 /* 7587 * Find the correct freework structure. 7588 */ 7589 LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) { 7590 if (wk->wk_type != D_FREEWORK) 7591 continue; 7592 freework = WK_FREEWORK(wk); 7593 if (freework->fw_blkno == newblk->nb_newblkno) 7594 break; 7595 } 7596 if (freework == NULL) 7597 panic("cancel_allocdirect: Freework not found"); 7598 /* 7599 * If a newblk exists at all we still have the journal entry that 7600 * initiated the allocation so we do not need to journal the free. 7601 */ 7602 cancel_jfreeblk(freeblks, freework->fw_blkno); 7603 /* 7604 * If the journal hasn't been written the jnewblk must be passed 7605 * to the call to ffs_blkfree that reclaims the space. We accomplish 7606 * this by linking the journal dependency into the freework to be 7607 * freed when freework_freeblock() is called. If the journal has 7608 * been written we can simply reclaim the journal space when the 7609 * freeblks work is complete. 7610 */ 7611 freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list, 7612 &freeblks->fb_jwork); 7613 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 7614 } 7615 7616 /* 7617 * Cancel a new block allocation. May be an indirect or direct block. We 7618 * remove it from various lists and return any journal record that needs to 7619 * be resolved by the caller. 7620 * 7621 * A special consideration is made for indirects which were never pointed 7622 * at on disk and will never be found once this block is released. 7623 */ 7624 static struct jnewblk * 7625 cancel_newblk( 7626 struct newblk *newblk, 7627 struct worklist *wk, 7628 struct workhead *wkhd) 7629 { 7630 struct jnewblk *jnewblk; 7631 7632 CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno); 7633 7634 newblk->nb_state |= GOINGAWAY; 7635 /* 7636 * Previously we traversed the completedhd on each indirdep 7637 * attached to this newblk to cancel them and gather journal 7638 * work. Since we need only the oldest journal segment and 7639 * the lowest point on the tree will always have the oldest 7640 * journal segment we are free to release the segments 7641 * of any subordinates and may leave the indirdep list to 7642 * indirdep_complete() when this newblk is freed. 7643 */ 7644 if (newblk->nb_state & ONDEPLIST) { 7645 newblk->nb_state &= ~ONDEPLIST; 7646 LIST_REMOVE(newblk, nb_deps); 7647 } 7648 if (newblk->nb_state & ONWORKLIST) 7649 WORKLIST_REMOVE(&newblk->nb_list); 7650 /* 7651 * If the journal entry hasn't been written we save a pointer to 7652 * the dependency that frees it until it is written or the 7653 * superseding operation completes. 7654 */ 7655 jnewblk = newblk->nb_jnewblk; 7656 if (jnewblk != NULL && wk != NULL) { 7657 newblk->nb_jnewblk = NULL; 7658 jnewblk->jn_dep = wk; 7659 } 7660 if (!LIST_EMPTY(&newblk->nb_jwork)) 7661 jwork_move(wkhd, &newblk->nb_jwork); 7662 /* 7663 * When truncating we must free the newdirblk early to remove 7664 * the pagedep from the hash before returning. 7665 */ 7666 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7667 free_newdirblk(WK_NEWDIRBLK(wk)); 7668 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7669 panic("cancel_newblk: extra newdirblk"); 7670 7671 return (jnewblk); 7672 } 7673 7674 /* 7675 * Schedule the freefrag associated with a newblk to be released once 7676 * the pointers are written and the previous block is no longer needed. 7677 */ 7678 static void 7679 newblk_freefrag(struct newblk *newblk) 7680 { 7681 struct freefrag *freefrag; 7682 7683 if (newblk->nb_freefrag == NULL) 7684 return; 7685 freefrag = newblk->nb_freefrag; 7686 newblk->nb_freefrag = NULL; 7687 freefrag->ff_state |= COMPLETE; 7688 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 7689 add_to_worklist(&freefrag->ff_list, 0); 7690 } 7691 7692 /* 7693 * Free a newblk. Generate a new freefrag work request if appropriate. 7694 * This must be called after the inode pointer and any direct block pointers 7695 * are valid or fully removed via truncate or frag extension. 7696 */ 7697 static void 7698 free_newblk(struct newblk *newblk) 7699 { 7700 struct indirdep *indirdep; 7701 struct worklist *wk; 7702 7703 KASSERT(newblk->nb_jnewblk == NULL, 7704 ("free_newblk: jnewblk %p still attached", newblk->nb_jnewblk)); 7705 KASSERT(newblk->nb_list.wk_type != D_NEWBLK, 7706 ("free_newblk: unclaimed newblk")); 7707 LOCK_OWNED(VFSTOUFS(newblk->nb_list.wk_mp)); 7708 newblk_freefrag(newblk); 7709 if (newblk->nb_state & ONDEPLIST) 7710 LIST_REMOVE(newblk, nb_deps); 7711 if (newblk->nb_state & ONWORKLIST) 7712 WORKLIST_REMOVE(&newblk->nb_list); 7713 LIST_REMOVE(newblk, nb_hash); 7714 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7715 free_newdirblk(WK_NEWDIRBLK(wk)); 7716 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7717 panic("free_newblk: extra newdirblk"); 7718 while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL) 7719 indirdep_complete(indirdep); 7720 handle_jwork(&newblk->nb_jwork); 7721 WORKITEM_FREE(newblk, D_NEWBLK); 7722 } 7723 7724 /* 7725 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep. 7726 */ 7727 static void 7728 free_newdirblk(struct newdirblk *newdirblk) 7729 { 7730 struct pagedep *pagedep; 7731 struct diradd *dap; 7732 struct worklist *wk; 7733 7734 LOCK_OWNED(VFSTOUFS(newdirblk->db_list.wk_mp)); 7735 WORKLIST_REMOVE(&newdirblk->db_list); 7736 /* 7737 * If the pagedep is still linked onto the directory buffer 7738 * dependency chain, then some of the entries on the 7739 * pd_pendinghd list may not be committed to disk yet. In 7740 * this case, we will simply clear the NEWBLOCK flag and 7741 * let the pd_pendinghd list be processed when the pagedep 7742 * is next written. If the pagedep is no longer on the buffer 7743 * dependency chain, then all the entries on the pd_pending 7744 * list are committed to disk and we can free them here. 7745 */ 7746 pagedep = newdirblk->db_pagedep; 7747 pagedep->pd_state &= ~NEWBLOCK; 7748 if ((pagedep->pd_state & ONWORKLIST) == 0) { 7749 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 7750 free_diradd(dap, NULL); 7751 /* 7752 * If no dependencies remain, the pagedep will be freed. 7753 */ 7754 free_pagedep(pagedep); 7755 } 7756 /* Should only ever be one item in the list. */ 7757 while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) { 7758 WORKLIST_REMOVE(wk); 7759 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 7760 } 7761 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 7762 } 7763 7764 /* 7765 * Prepare an inode to be freed. The actual free operation is not 7766 * done until the zero'ed inode has been written to disk. 7767 */ 7768 void 7769 softdep_freefile( 7770 struct vnode *pvp, 7771 ino_t ino, 7772 int mode) 7773 { 7774 struct inode *ip = VTOI(pvp); 7775 struct inodedep *inodedep; 7776 struct freefile *freefile; 7777 struct freeblks *freeblks; 7778 struct ufsmount *ump; 7779 7780 ump = ITOUMP(ip); 7781 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 7782 ("softdep_freefile called on non-softdep filesystem")); 7783 /* 7784 * This sets up the inode de-allocation dependency. 7785 */ 7786 freefile = malloc(sizeof(struct freefile), 7787 M_FREEFILE, M_SOFTDEP_FLAGS); 7788 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount); 7789 freefile->fx_mode = mode; 7790 freefile->fx_oldinum = ino; 7791 freefile->fx_devvp = ump->um_devvp; 7792 LIST_INIT(&freefile->fx_jwork); 7793 UFS_LOCK(ump); 7794 ump->um_fs->fs_pendinginodes += 1; 7795 UFS_UNLOCK(ump); 7796 7797 /* 7798 * If the inodedep does not exist, then the zero'ed inode has 7799 * been written to disk. If the allocated inode has never been 7800 * written to disk, then the on-disk inode is zero'ed. In either 7801 * case we can free the file immediately. If the journal was 7802 * canceled before being written the inode will never make it to 7803 * disk and we must send the canceled journal entrys to 7804 * ffs_freefile() to be cleared in conjunction with the bitmap. 7805 * Any blocks waiting on the inode to write can be safely freed 7806 * here as it will never been written. 7807 */ 7808 ACQUIRE_LOCK(ump); 7809 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7810 if (inodedep) { 7811 /* 7812 * Clear out freeblks that no longer need to reference 7813 * this inode. 7814 */ 7815 while ((freeblks = 7816 TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) { 7817 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, 7818 fb_next); 7819 freeblks->fb_state &= ~ONDEPLIST; 7820 } 7821 /* 7822 * Remove this inode from the unlinked list. 7823 */ 7824 if (inodedep->id_state & UNLINKED) { 7825 /* 7826 * Save the journal work to be freed with the bitmap 7827 * before we clear UNLINKED. Otherwise it can be lost 7828 * if the inode block is written. 7829 */ 7830 handle_bufwait(inodedep, &freefile->fx_jwork); 7831 clear_unlinked_inodedep(inodedep); 7832 /* 7833 * Re-acquire inodedep as we've dropped the 7834 * per-filesystem lock in clear_unlinked_inodedep(). 7835 */ 7836 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7837 } 7838 } 7839 if (inodedep == NULL || check_inode_unwritten(inodedep)) { 7840 FREE_LOCK(ump); 7841 handle_workitem_freefile(freefile); 7842 return; 7843 } 7844 if ((inodedep->id_state & DEPCOMPLETE) == 0) 7845 inodedep->id_state |= GOINGAWAY; 7846 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list); 7847 FREE_LOCK(ump); 7848 if (ip->i_number == ino) 7849 UFS_INODE_SET_FLAG(ip, IN_MODIFIED); 7850 } 7851 7852 /* 7853 * Check to see if an inode has never been written to disk. If 7854 * so free the inodedep and return success, otherwise return failure. 7855 * 7856 * If we still have a bitmap dependency, then the inode has never 7857 * been written to disk. Drop the dependency as it is no longer 7858 * necessary since the inode is being deallocated. We set the 7859 * ALLCOMPLETE flags since the bitmap now properly shows that the 7860 * inode is not allocated. Even if the inode is actively being 7861 * written, it has been rolled back to its zero'ed state, so we 7862 * are ensured that a zero inode is what is on the disk. For short 7863 * lived files, this change will usually result in removing all the 7864 * dependencies from the inode so that it can be freed immediately. 7865 */ 7866 static int 7867 check_inode_unwritten(struct inodedep *inodedep) 7868 { 7869 7870 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7871 7872 if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 || 7873 !LIST_EMPTY(&inodedep->id_dirremhd) || 7874 !LIST_EMPTY(&inodedep->id_pendinghd) || 7875 !LIST_EMPTY(&inodedep->id_bufwait) || 7876 !LIST_EMPTY(&inodedep->id_inowait) || 7877 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 7878 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 7879 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 7880 !TAILQ_EMPTY(&inodedep->id_extupdt) || 7881 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 7882 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 7883 inodedep->id_mkdiradd != NULL || 7884 inodedep->id_nlinkdelta != 0) 7885 return (0); 7886 /* 7887 * Another process might be in initiate_write_inodeblock_ufs[12] 7888 * trying to allocate memory without holding "Softdep Lock". 7889 */ 7890 if ((inodedep->id_state & IOSTARTED) != 0 && 7891 inodedep->id_savedino1 == NULL) 7892 return (0); 7893 7894 if (inodedep->id_state & ONDEPLIST) 7895 LIST_REMOVE(inodedep, id_deps); 7896 inodedep->id_state &= ~ONDEPLIST; 7897 inodedep->id_state |= ALLCOMPLETE; 7898 inodedep->id_bmsafemap = NULL; 7899 if (inodedep->id_state & ONWORKLIST) 7900 WORKLIST_REMOVE(&inodedep->id_list); 7901 if (inodedep->id_savedino1 != NULL) { 7902 free(inodedep->id_savedino1, M_SAVEDINO); 7903 inodedep->id_savedino1 = NULL; 7904 } 7905 if (free_inodedep(inodedep) == 0) 7906 panic("check_inode_unwritten: busy inode"); 7907 return (1); 7908 } 7909 7910 static int 7911 check_inodedep_free(struct inodedep *inodedep) 7912 { 7913 7914 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7915 if ((inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE || 7916 !LIST_EMPTY(&inodedep->id_dirremhd) || 7917 !LIST_EMPTY(&inodedep->id_pendinghd) || 7918 !LIST_EMPTY(&inodedep->id_bufwait) || 7919 !LIST_EMPTY(&inodedep->id_inowait) || 7920 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 7921 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 7922 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 7923 !TAILQ_EMPTY(&inodedep->id_extupdt) || 7924 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 7925 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 7926 inodedep->id_mkdiradd != NULL || 7927 inodedep->id_nlinkdelta != 0 || 7928 inodedep->id_savedino1 != NULL) 7929 return (0); 7930 return (1); 7931 } 7932 7933 /* 7934 * Try to free an inodedep structure. Return 1 if it could be freed. 7935 */ 7936 static int 7937 free_inodedep(struct inodedep *inodedep) 7938 { 7939 7940 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7941 if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 || 7942 !check_inodedep_free(inodedep)) 7943 return (0); 7944 if (inodedep->id_state & ONDEPLIST) 7945 LIST_REMOVE(inodedep, id_deps); 7946 LIST_REMOVE(inodedep, id_hash); 7947 WORKITEM_FREE(inodedep, D_INODEDEP); 7948 return (1); 7949 } 7950 7951 /* 7952 * Free the block referenced by a freework structure. The parent freeblks 7953 * structure is released and completed when the final cg bitmap reaches 7954 * the disk. This routine may be freeing a jnewblk which never made it to 7955 * disk in which case we do not have to wait as the operation is undone 7956 * in memory immediately. 7957 */ 7958 static void 7959 freework_freeblock(struct freework *freework, uint64_t key) 7960 { 7961 struct freeblks *freeblks; 7962 struct jnewblk *jnewblk; 7963 struct ufsmount *ump; 7964 struct workhead wkhd; 7965 struct fs *fs; 7966 int bsize; 7967 int needj; 7968 7969 ump = VFSTOUFS(freework->fw_list.wk_mp); 7970 LOCK_OWNED(ump); 7971 /* 7972 * Handle partial truncate separately. 7973 */ 7974 if (freework->fw_indir) { 7975 complete_trunc_indir(freework); 7976 return; 7977 } 7978 freeblks = freework->fw_freeblks; 7979 fs = ump->um_fs; 7980 needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0; 7981 bsize = lfragtosize(fs, freework->fw_frags); 7982 LIST_INIT(&wkhd); 7983 /* 7984 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives 7985 * on the indirblk hashtable and prevents premature freeing. 7986 */ 7987 freework->fw_state |= DEPCOMPLETE; 7988 /* 7989 * SUJ needs to wait for the segment referencing freed indirect 7990 * blocks to expire so that we know the checker will not confuse 7991 * a re-allocated indirect block with its old contents. 7992 */ 7993 if (needj && freework->fw_lbn <= -UFS_NDADDR) 7994 indirblk_insert(freework); 7995 /* 7996 * If we are canceling an existing jnewblk pass it to the free 7997 * routine, otherwise pass the freeblk which will ultimately 7998 * release the freeblks. If we're not journaling, we can just 7999 * free the freeblks immediately. 8000 */ 8001 jnewblk = freework->fw_jnewblk; 8002 if (jnewblk != NULL) { 8003 cancel_jnewblk(jnewblk, &wkhd); 8004 needj = 0; 8005 } else if (needj) { 8006 freework->fw_state |= DELAYEDFREE; 8007 freeblks->fb_cgwait++; 8008 WORKLIST_INSERT(&wkhd, &freework->fw_list); 8009 } 8010 FREE_LOCK(ump); 8011 freeblks_free(ump, freeblks, btodb(bsize)); 8012 CTR4(KTR_SUJ, 8013 "freework_freeblock: ino %jd blkno %jd lbn %jd size %d", 8014 freeblks->fb_inum, freework->fw_blkno, freework->fw_lbn, bsize); 8015 ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, bsize, 8016 freeblks->fb_inum, freeblks->fb_vtype, &wkhd, key); 8017 ACQUIRE_LOCK(ump); 8018 /* 8019 * The jnewblk will be discarded and the bits in the map never 8020 * made it to disk. We can immediately free the freeblk. 8021 */ 8022 if (needj == 0) 8023 handle_written_freework(freework); 8024 } 8025 8026 /* 8027 * We enqueue freework items that need processing back on the freeblks and 8028 * add the freeblks to the worklist. This makes it easier to find all work 8029 * required to flush a truncation in process_truncates(). 8030 */ 8031 static void 8032 freework_enqueue(struct freework *freework) 8033 { 8034 struct freeblks *freeblks; 8035 8036 freeblks = freework->fw_freeblks; 8037 if ((freework->fw_state & INPROGRESS) == 0) 8038 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 8039 if ((freeblks->fb_state & 8040 (ONWORKLIST | INPROGRESS | ALLCOMPLETE)) == ALLCOMPLETE && 8041 LIST_EMPTY(&freeblks->fb_jblkdephd)) 8042 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 8043 } 8044 8045 /* 8046 * Start, continue, or finish the process of freeing an indirect block tree. 8047 * The free operation may be paused at any point with fw_off containing the 8048 * offset to restart from. This enables us to implement some flow control 8049 * for large truncates which may fan out and generate a huge number of 8050 * dependencies. 8051 */ 8052 static void 8053 handle_workitem_indirblk(struct freework *freework) 8054 { 8055 struct freeblks *freeblks; 8056 struct ufsmount *ump; 8057 struct fs *fs; 8058 8059 freeblks = freework->fw_freeblks; 8060 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8061 fs = ump->um_fs; 8062 if (freework->fw_state & DEPCOMPLETE) { 8063 handle_written_freework(freework); 8064 return; 8065 } 8066 if (freework->fw_off == NINDIR(fs)) { 8067 freework_freeblock(freework, SINGLETON_KEY); 8068 return; 8069 } 8070 freework->fw_state |= INPROGRESS; 8071 FREE_LOCK(ump); 8072 indir_trunc(freework, fsbtodb(fs, freework->fw_blkno), 8073 freework->fw_lbn); 8074 ACQUIRE_LOCK(ump); 8075 } 8076 8077 /* 8078 * Called when a freework structure attached to a cg buf is written. The 8079 * ref on either the parent or the freeblks structure is released and 8080 * the freeblks is added back to the worklist if there is more work to do. 8081 */ 8082 static void 8083 handle_written_freework(struct freework *freework) 8084 { 8085 struct freeblks *freeblks; 8086 struct freework *parent; 8087 8088 freeblks = freework->fw_freeblks; 8089 parent = freework->fw_parent; 8090 if (freework->fw_state & DELAYEDFREE) 8091 freeblks->fb_cgwait--; 8092 freework->fw_state |= COMPLETE; 8093 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 8094 WORKITEM_FREE(freework, D_FREEWORK); 8095 if (parent) { 8096 if (--parent->fw_ref == 0) 8097 freework_enqueue(parent); 8098 return; 8099 } 8100 if (--freeblks->fb_ref != 0) 8101 return; 8102 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) == 8103 ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd)) 8104 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 8105 } 8106 8107 /* 8108 * This workitem routine performs the block de-allocation. 8109 * The workitem is added to the pending list after the updated 8110 * inode block has been written to disk. As mentioned above, 8111 * checks regarding the number of blocks de-allocated (compared 8112 * to the number of blocks allocated for the file) are also 8113 * performed in this function. 8114 */ 8115 static int 8116 handle_workitem_freeblocks(struct freeblks *freeblks, int flags) 8117 { 8118 struct freework *freework; 8119 struct newblk *newblk; 8120 struct allocindir *aip; 8121 struct ufsmount *ump; 8122 struct worklist *wk; 8123 uint64_t key; 8124 8125 KASSERT(LIST_EMPTY(&freeblks->fb_jblkdephd), 8126 ("handle_workitem_freeblocks: Journal entries not written.")); 8127 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8128 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum); 8129 ACQUIRE_LOCK(ump); 8130 while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) { 8131 WORKLIST_REMOVE(wk); 8132 switch (wk->wk_type) { 8133 case D_DIRREM: 8134 wk->wk_state |= COMPLETE; 8135 add_to_worklist(wk, 0); 8136 continue; 8137 8138 case D_ALLOCDIRECT: 8139 free_newblk(WK_NEWBLK(wk)); 8140 continue; 8141 8142 case D_ALLOCINDIR: 8143 aip = WK_ALLOCINDIR(wk); 8144 freework = NULL; 8145 if (aip->ai_state & DELAYEDFREE) { 8146 FREE_LOCK(ump); 8147 freework = newfreework(ump, freeblks, NULL, 8148 aip->ai_lbn, aip->ai_newblkno, 8149 ump->um_fs->fs_frag, 0, 0); 8150 ACQUIRE_LOCK(ump); 8151 } 8152 newblk = WK_NEWBLK(wk); 8153 if (newblk->nb_jnewblk) { 8154 freework->fw_jnewblk = newblk->nb_jnewblk; 8155 newblk->nb_jnewblk->jn_dep = &freework->fw_list; 8156 newblk->nb_jnewblk = NULL; 8157 } 8158 free_newblk(newblk); 8159 continue; 8160 8161 case D_FREEWORK: 8162 freework = WK_FREEWORK(wk); 8163 if (freework->fw_lbn <= -UFS_NDADDR) 8164 handle_workitem_indirblk(freework); 8165 else 8166 freework_freeblock(freework, key); 8167 continue; 8168 default: 8169 panic("handle_workitem_freeblocks: Unknown type %s", 8170 TYPENAME(wk->wk_type)); 8171 } 8172 } 8173 if (freeblks->fb_ref != 0) { 8174 freeblks->fb_state &= ~INPROGRESS; 8175 wake_worklist(&freeblks->fb_list); 8176 freeblks = NULL; 8177 } 8178 FREE_LOCK(ump); 8179 ffs_blkrelease_finish(ump, key); 8180 if (freeblks) 8181 return handle_complete_freeblocks(freeblks, flags); 8182 return (0); 8183 } 8184 8185 /* 8186 * Handle completion of block free via truncate. This allows fs_pending 8187 * to track the actual free block count more closely than if we only updated 8188 * it at the end. We must be careful to handle cases where the block count 8189 * on free was incorrect. 8190 */ 8191 static void 8192 freeblks_free(struct ufsmount *ump, 8193 struct freeblks *freeblks, 8194 int blocks) 8195 { 8196 struct fs *fs; 8197 ufs2_daddr_t remain; 8198 8199 UFS_LOCK(ump); 8200 remain = -freeblks->fb_chkcnt; 8201 freeblks->fb_chkcnt += blocks; 8202 if (remain > 0) { 8203 if (remain < blocks) 8204 blocks = remain; 8205 fs = ump->um_fs; 8206 fs->fs_pendingblocks -= blocks; 8207 } 8208 UFS_UNLOCK(ump); 8209 } 8210 8211 /* 8212 * Once all of the freework workitems are complete we can retire the 8213 * freeblocks dependency and any journal work awaiting completion. This 8214 * can not be called until all other dependencies are stable on disk. 8215 */ 8216 static int 8217 handle_complete_freeblocks(struct freeblks *freeblks, int flags) 8218 { 8219 struct inodedep *inodedep; 8220 struct inode *ip; 8221 struct vnode *vp; 8222 struct fs *fs; 8223 struct ufsmount *ump; 8224 ufs2_daddr_t spare; 8225 8226 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8227 fs = ump->um_fs; 8228 flags = LK_EXCLUSIVE | flags; 8229 spare = freeblks->fb_chkcnt; 8230 8231 /* 8232 * If we did not release the expected number of blocks we may have 8233 * to adjust the inode block count here. Only do so if it wasn't 8234 * a truncation to zero and the modrev still matches. 8235 */ 8236 if (spare && freeblks->fb_len != 0) { 8237 if (ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_inum, 8238 flags, &vp, FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP) != 0) 8239 return (EBUSY); 8240 ip = VTOI(vp); 8241 if (ip->i_mode == 0) { 8242 vgone(vp); 8243 } else if (DIP(ip, i_modrev) == freeblks->fb_modrev) { 8244 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare); 8245 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 8246 /* 8247 * We must wait so this happens before the 8248 * journal is reclaimed. 8249 */ 8250 ffs_update(vp, 1); 8251 } 8252 vput(vp); 8253 } 8254 if (spare < 0) { 8255 UFS_LOCK(ump); 8256 fs->fs_pendingblocks += spare; 8257 UFS_UNLOCK(ump); 8258 } 8259 #ifdef QUOTA 8260 /* Handle spare. */ 8261 if (spare) 8262 quotaadj(freeblks->fb_quota, ump, -spare); 8263 quotarele(freeblks->fb_quota); 8264 #endif 8265 ACQUIRE_LOCK(ump); 8266 if (freeblks->fb_state & ONDEPLIST) { 8267 inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum, 8268 0, &inodedep); 8269 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next); 8270 freeblks->fb_state &= ~ONDEPLIST; 8271 if (TAILQ_EMPTY(&inodedep->id_freeblklst)) 8272 free_inodedep(inodedep); 8273 } 8274 /* 8275 * All of the freeblock deps must be complete prior to this call 8276 * so it's now safe to complete earlier outstanding journal entries. 8277 */ 8278 handle_jwork(&freeblks->fb_jwork); 8279 WORKITEM_FREE(freeblks, D_FREEBLKS); 8280 FREE_LOCK(ump); 8281 return (0); 8282 } 8283 8284 /* 8285 * Release blocks associated with the freeblks and stored in the indirect 8286 * block dbn. If level is greater than SINGLE, the block is an indirect block 8287 * and recursive calls to indirtrunc must be used to cleanse other indirect 8288 * blocks. 8289 * 8290 * This handles partial and complete truncation of blocks. Partial is noted 8291 * with goingaway == 0. In this case the freework is completed after the 8292 * zero'd indirects are written to disk. For full truncation the freework 8293 * is completed after the block is freed. 8294 */ 8295 static void 8296 indir_trunc(struct freework *freework, 8297 ufs2_daddr_t dbn, 8298 ufs_lbn_t lbn) 8299 { 8300 struct freework *nfreework; 8301 struct workhead wkhd; 8302 struct freeblks *freeblks; 8303 struct buf *bp; 8304 struct fs *fs; 8305 struct indirdep *indirdep; 8306 struct mount *mp; 8307 struct ufsmount *ump; 8308 ufs1_daddr_t *bap1; 8309 ufs2_daddr_t nb, nnb, *bap2; 8310 ufs_lbn_t lbnadd, nlbn; 8311 uint64_t key; 8312 int nblocks, ufs1fmt, freedblocks; 8313 int goingaway, freedeps, needj, level, cnt, i, error; 8314 8315 freeblks = freework->fw_freeblks; 8316 mp = freeblks->fb_list.wk_mp; 8317 ump = VFSTOUFS(mp); 8318 fs = ump->um_fs; 8319 /* 8320 * Get buffer of block pointers to be freed. There are three cases: 8321 * 8322 * 1) Partial truncate caches the indirdep pointer in the freework 8323 * which provides us a back copy to the save bp which holds the 8324 * pointers we want to clear. When this completes the zero 8325 * pointers are written to the real copy. 8326 * 2) The indirect is being completely truncated, cancel_indirdep() 8327 * eliminated the real copy and placed the indirdep on the saved 8328 * copy. The indirdep and buf are discarded when this completes. 8329 * 3) The indirect was not in memory, we read a copy off of the disk 8330 * using the devvp and drop and invalidate the buffer when we're 8331 * done. 8332 */ 8333 goingaway = 1; 8334 indirdep = NULL; 8335 if (freework->fw_indir != NULL) { 8336 goingaway = 0; 8337 indirdep = freework->fw_indir; 8338 bp = indirdep->ir_savebp; 8339 if (bp == NULL || bp->b_blkno != dbn) 8340 panic("indir_trunc: Bad saved buf %p blkno %jd", 8341 bp, (intmax_t)dbn); 8342 } else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) { 8343 /* 8344 * The lock prevents the buf dep list from changing and 8345 * indirects on devvp should only ever have one dependency. 8346 */ 8347 indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep)); 8348 if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0) 8349 panic("indir_trunc: Bad indirdep %p from buf %p", 8350 indirdep, bp); 8351 } else { 8352 error = ffs_breadz(ump, freeblks->fb_devvp, dbn, dbn, 8353 (int)fs->fs_bsize, NULL, NULL, 0, NOCRED, 0, NULL, &bp); 8354 if (error) 8355 return; 8356 } 8357 ACQUIRE_LOCK(ump); 8358 /* Protects against a race with complete_trunc_indir(). */ 8359 freework->fw_state &= ~INPROGRESS; 8360 /* 8361 * If we have an indirdep we need to enforce the truncation order 8362 * and discard it when it is complete. 8363 */ 8364 if (indirdep) { 8365 if (freework != TAILQ_FIRST(&indirdep->ir_trunc) && 8366 !TAILQ_EMPTY(&indirdep->ir_trunc)) { 8367 /* 8368 * Add the complete truncate to the list on the 8369 * indirdep to enforce in-order processing. 8370 */ 8371 if (freework->fw_indir == NULL) 8372 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, 8373 freework, fw_next); 8374 FREE_LOCK(ump); 8375 return; 8376 } 8377 /* 8378 * If we're goingaway, free the indirdep. Otherwise it will 8379 * linger until the write completes. 8380 */ 8381 if (goingaway) { 8382 KASSERT(indirdep->ir_savebp == bp, 8383 ("indir_trunc: losing ir_savebp %p", 8384 indirdep->ir_savebp)); 8385 indirdep->ir_savebp = NULL; 8386 free_indirdep(indirdep); 8387 } 8388 } 8389 FREE_LOCK(ump); 8390 /* Initialize pointers depending on block size. */ 8391 if (ump->um_fstype == UFS1) { 8392 bap1 = (ufs1_daddr_t *)bp->b_data; 8393 nb = bap1[freework->fw_off]; 8394 ufs1fmt = 1; 8395 bap2 = NULL; 8396 } else { 8397 bap2 = (ufs2_daddr_t *)bp->b_data; 8398 nb = bap2[freework->fw_off]; 8399 ufs1fmt = 0; 8400 bap1 = NULL; 8401 } 8402 level = lbn_level(lbn); 8403 needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0; 8404 lbnadd = lbn_offset(fs, level); 8405 nblocks = btodb(fs->fs_bsize); 8406 nfreework = freework; 8407 freedeps = 0; 8408 cnt = 0; 8409 /* 8410 * Reclaim blocks. Traverses into nested indirect levels and 8411 * arranges for the current level to be freed when subordinates 8412 * are free when journaling. 8413 */ 8414 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum); 8415 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) { 8416 if (UFS_CHECK_BLKNO(mp, freeblks->fb_inum, nb, 8417 fs->fs_bsize) != 0) 8418 nb = 0; 8419 if (i != NINDIR(fs) - 1) { 8420 if (ufs1fmt) 8421 nnb = bap1[i+1]; 8422 else 8423 nnb = bap2[i+1]; 8424 } else 8425 nnb = 0; 8426 if (nb == 0) 8427 continue; 8428 cnt++; 8429 if (level != 0) { 8430 nlbn = (lbn + 1) - (i * lbnadd); 8431 if (needj != 0) { 8432 nfreework = newfreework(ump, freeblks, freework, 8433 nlbn, nb, fs->fs_frag, 0, 0); 8434 freedeps++; 8435 } 8436 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn); 8437 } else { 8438 struct freedep *freedep; 8439 8440 /* 8441 * Attempt to aggregate freedep dependencies for 8442 * all blocks being released to the same CG. 8443 */ 8444 LIST_INIT(&wkhd); 8445 if (needj != 0 && 8446 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) { 8447 freedep = newfreedep(freework); 8448 WORKLIST_INSERT_UNLOCKED(&wkhd, 8449 &freedep->fd_list); 8450 freedeps++; 8451 } 8452 CTR3(KTR_SUJ, 8453 "indir_trunc: ino %jd blkno %jd size %d", 8454 freeblks->fb_inum, nb, fs->fs_bsize); 8455 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb, 8456 fs->fs_bsize, freeblks->fb_inum, 8457 freeblks->fb_vtype, &wkhd, key); 8458 } 8459 } 8460 ffs_blkrelease_finish(ump, key); 8461 if (goingaway) { 8462 bp->b_flags |= B_INVAL | B_NOCACHE; 8463 brelse(bp); 8464 } 8465 freedblocks = 0; 8466 if (level == 0) 8467 freedblocks = (nblocks * cnt); 8468 if (needj == 0) 8469 freedblocks += nblocks; 8470 freeblks_free(ump, freeblks, freedblocks); 8471 /* 8472 * If we are journaling set up the ref counts and offset so this 8473 * indirect can be completed when its children are free. 8474 */ 8475 if (needj) { 8476 ACQUIRE_LOCK(ump); 8477 freework->fw_off = i; 8478 freework->fw_ref += freedeps; 8479 freework->fw_ref -= NINDIR(fs) + 1; 8480 if (level == 0) 8481 freeblks->fb_cgwait += freedeps; 8482 if (freework->fw_ref == 0) 8483 freework_freeblock(freework, SINGLETON_KEY); 8484 FREE_LOCK(ump); 8485 return; 8486 } 8487 /* 8488 * If we're not journaling we can free the indirect now. 8489 */ 8490 dbn = dbtofsb(fs, dbn); 8491 CTR3(KTR_SUJ, 8492 "indir_trunc 2: ino %jd blkno %jd size %d", 8493 freeblks->fb_inum, dbn, fs->fs_bsize); 8494 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize, 8495 freeblks->fb_inum, freeblks->fb_vtype, NULL, SINGLETON_KEY); 8496 /* Non SUJ softdep does single-threaded truncations. */ 8497 if (freework->fw_blkno == dbn) { 8498 freework->fw_state |= ALLCOMPLETE; 8499 ACQUIRE_LOCK(ump); 8500 handle_written_freework(freework); 8501 FREE_LOCK(ump); 8502 } 8503 return; 8504 } 8505 8506 /* 8507 * Cancel an allocindir when it is removed via truncation. When bp is not 8508 * NULL the indirect never appeared on disk and is scheduled to be freed 8509 * independently of the indir so we can more easily track journal work. 8510 */ 8511 static void 8512 cancel_allocindir( 8513 struct allocindir *aip, 8514 struct buf *bp, 8515 struct freeblks *freeblks, 8516 int trunc) 8517 { 8518 struct indirdep *indirdep; 8519 struct freefrag *freefrag; 8520 struct newblk *newblk; 8521 8522 newblk = (struct newblk *)aip; 8523 LIST_REMOVE(aip, ai_next); 8524 /* 8525 * We must eliminate the pointer in bp if it must be freed on its 8526 * own due to partial truncate or pending journal work. 8527 */ 8528 if (bp && (trunc || newblk->nb_jnewblk)) { 8529 /* 8530 * Clear the pointer and mark the aip to be freed 8531 * directly if it never existed on disk. 8532 */ 8533 aip->ai_state |= DELAYEDFREE; 8534 indirdep = aip->ai_indirdep; 8535 if (indirdep->ir_state & UFS1FMT) 8536 ((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8537 else 8538 ((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8539 } 8540 /* 8541 * When truncating the previous pointer will be freed via 8542 * savedbp. Eliminate the freefrag which would dup free. 8543 */ 8544 if (trunc && (freefrag = newblk->nb_freefrag) != NULL) { 8545 newblk->nb_freefrag = NULL; 8546 if (freefrag->ff_jdep) 8547 cancel_jfreefrag( 8548 WK_JFREEFRAG(freefrag->ff_jdep)); 8549 jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork); 8550 WORKITEM_FREE(freefrag, D_FREEFRAG); 8551 } 8552 /* 8553 * If the journal hasn't been written the jnewblk must be passed 8554 * to the call to ffs_blkfree that reclaims the space. We accomplish 8555 * this by leaving the journal dependency on the newblk to be freed 8556 * when a freework is created in handle_workitem_freeblocks(). 8557 */ 8558 cancel_newblk(newblk, NULL, &freeblks->fb_jwork); 8559 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 8560 } 8561 8562 /* 8563 * Create the mkdir dependencies for . and .. in a new directory. Link them 8564 * in to a newdirblk so any subsequent additions are tracked properly. The 8565 * caller is responsible for adding the mkdir1 dependency to the journal 8566 * and updating id_mkdiradd. This function returns with the per-filesystem 8567 * lock held. 8568 */ 8569 static struct mkdir * 8570 setup_newdir( 8571 struct diradd *dap, 8572 ino_t newinum, 8573 ino_t dinum, 8574 struct buf *newdirbp, 8575 struct mkdir **mkdirp) 8576 { 8577 struct newblk *newblk; 8578 struct pagedep *pagedep; 8579 struct inodedep *inodedep; 8580 struct newdirblk *newdirblk; 8581 struct mkdir *mkdir1, *mkdir2; 8582 struct worklist *wk; 8583 struct jaddref *jaddref; 8584 struct ufsmount *ump; 8585 struct mount *mp; 8586 8587 mp = dap->da_list.wk_mp; 8588 ump = VFSTOUFS(mp); 8589 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK, 8590 M_SOFTDEP_FLAGS); 8591 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8592 LIST_INIT(&newdirblk->db_mkdir); 8593 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8594 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp); 8595 mkdir1->md_state = ATTACHED | MKDIR_BODY; 8596 mkdir1->md_diradd = dap; 8597 mkdir1->md_jaddref = NULL; 8598 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8599 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp); 8600 mkdir2->md_state = ATTACHED | MKDIR_PARENT; 8601 mkdir2->md_diradd = dap; 8602 mkdir2->md_jaddref = NULL; 8603 if (MOUNTEDSUJ(mp) == 0) { 8604 mkdir1->md_state |= DEPCOMPLETE; 8605 mkdir2->md_state |= DEPCOMPLETE; 8606 } 8607 /* 8608 * Dependency on "." and ".." being written to disk. 8609 */ 8610 mkdir1->md_buf = newdirbp; 8611 ACQUIRE_LOCK(VFSTOUFS(mp)); 8612 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir1, md_mkdirs); 8613 /* 8614 * We must link the pagedep, allocdirect, and newdirblk for 8615 * the initial file page so the pointer to the new directory 8616 * is not written until the directory contents are live and 8617 * any subsequent additions are not marked live until the 8618 * block is reachable via the inode. 8619 */ 8620 if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0) 8621 panic("setup_newdir: lost pagedep"); 8622 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list) 8623 if (wk->wk_type == D_ALLOCDIRECT) 8624 break; 8625 if (wk == NULL) 8626 panic("setup_newdir: lost allocdirect"); 8627 if (pagedep->pd_state & NEWBLOCK) 8628 panic("setup_newdir: NEWBLOCK already set"); 8629 newblk = WK_NEWBLK(wk); 8630 pagedep->pd_state |= NEWBLOCK; 8631 pagedep->pd_newdirblk = newdirblk; 8632 newdirblk->db_pagedep = pagedep; 8633 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8634 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list); 8635 /* 8636 * Look up the inodedep for the parent directory so that we 8637 * can link mkdir2 into the pending dotdot jaddref or 8638 * the inode write if there is none. If the inode is 8639 * ALLCOMPLETE and no jaddref is present all dependencies have 8640 * been satisfied and mkdir2 can be freed. 8641 */ 8642 inodedep_lookup(mp, dinum, 0, &inodedep); 8643 if (MOUNTEDSUJ(mp)) { 8644 if (inodedep == NULL) 8645 panic("setup_newdir: Lost parent."); 8646 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8647 inoreflst); 8648 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum && 8649 (jaddref->ja_state & MKDIR_PARENT), 8650 ("setup_newdir: bad dotdot jaddref %p", jaddref)); 8651 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8652 mkdir2->md_jaddref = jaddref; 8653 jaddref->ja_mkdir = mkdir2; 8654 } else if (inodedep == NULL || 8655 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 8656 dap->da_state &= ~MKDIR_PARENT; 8657 WORKITEM_FREE(mkdir2, D_MKDIR); 8658 mkdir2 = NULL; 8659 } else { 8660 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8661 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list); 8662 } 8663 *mkdirp = mkdir2; 8664 8665 return (mkdir1); 8666 } 8667 8668 /* 8669 * Directory entry addition dependencies. 8670 * 8671 * When adding a new directory entry, the inode (with its incremented link 8672 * count) must be written to disk before the directory entry's pointer to it. 8673 * Also, if the inode is newly allocated, the corresponding freemap must be 8674 * updated (on disk) before the directory entry's pointer. These requirements 8675 * are met via undo/redo on the directory entry's pointer, which consists 8676 * simply of the inode number. 8677 * 8678 * As directory entries are added and deleted, the free space within a 8679 * directory block can become fragmented. The ufs filesystem will compact 8680 * a fragmented directory block to make space for a new entry. When this 8681 * occurs, the offsets of previously added entries change. Any "diradd" 8682 * dependency structures corresponding to these entries must be updated with 8683 * the new offsets. 8684 */ 8685 8686 /* 8687 * This routine is called after the in-memory inode's link 8688 * count has been incremented, but before the directory entry's 8689 * pointer to the inode has been set. 8690 */ 8691 int 8692 softdep_setup_directory_add( 8693 struct buf *bp, /* buffer containing directory block */ 8694 struct inode *dp, /* inode for directory */ 8695 off_t diroffset, /* offset of new entry in directory */ 8696 ino_t newinum, /* inode referenced by new directory entry */ 8697 struct buf *newdirbp, /* non-NULL => contents of new mkdir */ 8698 int isnewblk) /* entry is in a newly allocated block */ 8699 { 8700 int offset; /* offset of new entry within directory block */ 8701 ufs_lbn_t lbn; /* block in directory containing new entry */ 8702 struct fs *fs; 8703 struct diradd *dap; 8704 struct newblk *newblk; 8705 struct pagedep *pagedep; 8706 struct inodedep *inodedep; 8707 struct newdirblk *newdirblk; 8708 struct mkdir *mkdir1, *mkdir2; 8709 struct jaddref *jaddref; 8710 struct ufsmount *ump; 8711 struct mount *mp; 8712 int isindir; 8713 8714 mp = ITOVFS(dp); 8715 ump = VFSTOUFS(mp); 8716 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 8717 ("softdep_setup_directory_add called on non-softdep filesystem")); 8718 /* 8719 * Whiteouts have no dependencies. 8720 */ 8721 if (newinum == UFS_WINO) { 8722 if (newdirbp != NULL) 8723 bdwrite(newdirbp); 8724 return (0); 8725 } 8726 jaddref = NULL; 8727 mkdir1 = mkdir2 = NULL; 8728 fs = ump->um_fs; 8729 lbn = lblkno(fs, diroffset); 8730 offset = blkoff(fs, diroffset); 8731 dap = malloc(sizeof(struct diradd), M_DIRADD, 8732 M_SOFTDEP_FLAGS|M_ZERO); 8733 workitem_alloc(&dap->da_list, D_DIRADD, mp); 8734 dap->da_offset = offset; 8735 dap->da_newinum = newinum; 8736 dap->da_state = ATTACHED; 8737 LIST_INIT(&dap->da_jwork); 8738 isindir = bp->b_lblkno >= UFS_NDADDR; 8739 newdirblk = NULL; 8740 if (isnewblk && 8741 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) { 8742 newdirblk = malloc(sizeof(struct newdirblk), 8743 M_NEWDIRBLK, M_SOFTDEP_FLAGS); 8744 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8745 LIST_INIT(&newdirblk->db_mkdir); 8746 } 8747 /* 8748 * If we're creating a new directory setup the dependencies and set 8749 * the dap state to wait for them. Otherwise it's COMPLETE and 8750 * we can move on. 8751 */ 8752 if (newdirbp == NULL) { 8753 dap->da_state |= DEPCOMPLETE; 8754 ACQUIRE_LOCK(ump); 8755 } else { 8756 dap->da_state |= MKDIR_BODY | MKDIR_PARENT; 8757 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp, 8758 &mkdir2); 8759 } 8760 /* 8761 * Link into parent directory pagedep to await its being written. 8762 */ 8763 pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep); 8764 #ifdef INVARIANTS 8765 if (diradd_lookup(pagedep, offset) != NULL) 8766 panic("softdep_setup_directory_add: %p already at off %d\n", 8767 diradd_lookup(pagedep, offset), offset); 8768 #endif 8769 dap->da_pagedep = pagedep; 8770 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap, 8771 da_pdlist); 8772 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 8773 /* 8774 * If we're journaling, link the diradd into the jaddref so it 8775 * may be completed after the journal entry is written. Otherwise, 8776 * link the diradd into its inodedep. If the inode is not yet 8777 * written place it on the bufwait list, otherwise do the post-inode 8778 * write processing to put it on the id_pendinghd list. 8779 */ 8780 if (MOUNTEDSUJ(mp)) { 8781 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8782 inoreflst); 8783 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 8784 ("softdep_setup_directory_add: bad jaddref %p", jaddref)); 8785 jaddref->ja_diroff = diroffset; 8786 jaddref->ja_diradd = dap; 8787 add_to_journal(&jaddref->ja_list); 8788 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) 8789 diradd_inode_written(dap, inodedep); 8790 else 8791 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 8792 /* 8793 * Add the journal entries for . and .. links now that the primary 8794 * link is written. 8795 */ 8796 if (mkdir1 != NULL && MOUNTEDSUJ(mp)) { 8797 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 8798 inoreflst, if_deps); 8799 KASSERT(jaddref != NULL && 8800 jaddref->ja_ino == jaddref->ja_parent && 8801 (jaddref->ja_state & MKDIR_BODY), 8802 ("softdep_setup_directory_add: bad dot jaddref %p", 8803 jaddref)); 8804 mkdir1->md_jaddref = jaddref; 8805 jaddref->ja_mkdir = mkdir1; 8806 /* 8807 * It is important that the dotdot journal entry 8808 * is added prior to the dot entry since dot writes 8809 * both the dot and dotdot links. These both must 8810 * be added after the primary link for the journal 8811 * to remain consistent. 8812 */ 8813 add_to_journal(&mkdir2->md_jaddref->ja_list); 8814 add_to_journal(&jaddref->ja_list); 8815 } 8816 /* 8817 * If we are adding a new directory remember this diradd so that if 8818 * we rename it we can keep the dot and dotdot dependencies. If 8819 * we are adding a new name for an inode that has a mkdiradd we 8820 * must be in rename and we have to move the dot and dotdot 8821 * dependencies to this new name. The old name is being orphaned 8822 * soon. 8823 */ 8824 if (mkdir1 != NULL) { 8825 if (inodedep->id_mkdiradd != NULL) 8826 panic("softdep_setup_directory_add: Existing mkdir"); 8827 inodedep->id_mkdiradd = dap; 8828 } else if (inodedep->id_mkdiradd) 8829 merge_diradd(inodedep, dap); 8830 if (newdirblk != NULL) { 8831 /* 8832 * There is nothing to do if we are already tracking 8833 * this block. 8834 */ 8835 if ((pagedep->pd_state & NEWBLOCK) != 0) { 8836 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 8837 FREE_LOCK(ump); 8838 return (0); 8839 } 8840 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk) 8841 == 0) 8842 panic("softdep_setup_directory_add: lost entry"); 8843 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8844 pagedep->pd_state |= NEWBLOCK; 8845 pagedep->pd_newdirblk = newdirblk; 8846 newdirblk->db_pagedep = pagedep; 8847 FREE_LOCK(ump); 8848 /* 8849 * If we extended into an indirect signal direnter to sync. 8850 */ 8851 if (isindir) 8852 return (1); 8853 return (0); 8854 } 8855 FREE_LOCK(ump); 8856 return (0); 8857 } 8858 8859 /* 8860 * This procedure is called to change the offset of a directory 8861 * entry when compacting a directory block which must be owned 8862 * exclusively by the caller. Note that the actual entry movement 8863 * must be done in this procedure to ensure that no I/O completions 8864 * occur while the move is in progress. 8865 */ 8866 void 8867 softdep_change_directoryentry_offset( 8868 struct buf *bp, /* Buffer holding directory block. */ 8869 struct inode *dp, /* inode for directory */ 8870 caddr_t base, /* address of dp->i_offset */ 8871 caddr_t oldloc, /* address of old directory location */ 8872 caddr_t newloc, /* address of new directory location */ 8873 int entrysize) /* size of directory entry */ 8874 { 8875 int offset, oldoffset, newoffset; 8876 struct pagedep *pagedep; 8877 struct jmvref *jmvref; 8878 struct diradd *dap; 8879 struct direct *de; 8880 struct mount *mp; 8881 struct ufsmount *ump; 8882 ufs_lbn_t lbn; 8883 int flags; 8884 8885 mp = ITOVFS(dp); 8886 ump = VFSTOUFS(mp); 8887 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 8888 ("softdep_change_directoryentry_offset called on " 8889 "non-softdep filesystem")); 8890 de = (struct direct *)oldloc; 8891 jmvref = NULL; 8892 flags = 0; 8893 /* 8894 * Moves are always journaled as it would be too complex to 8895 * determine if any affected adds or removes are present in the 8896 * journal. 8897 */ 8898 if (MOUNTEDSUJ(mp)) { 8899 flags = DEPALLOC; 8900 jmvref = newjmvref(dp, de->d_ino, 8901 I_OFFSET(dp) + (oldloc - base), 8902 I_OFFSET(dp) + (newloc - base)); 8903 } 8904 lbn = lblkno(ump->um_fs, I_OFFSET(dp)); 8905 offset = blkoff(ump->um_fs, I_OFFSET(dp)); 8906 oldoffset = offset + (oldloc - base); 8907 newoffset = offset + (newloc - base); 8908 ACQUIRE_LOCK(ump); 8909 if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0) 8910 goto done; 8911 dap = diradd_lookup(pagedep, oldoffset); 8912 if (dap) { 8913 dap->da_offset = newoffset; 8914 newoffset = DIRADDHASH(newoffset); 8915 oldoffset = DIRADDHASH(oldoffset); 8916 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE && 8917 newoffset != oldoffset) { 8918 LIST_REMOVE(dap, da_pdlist); 8919 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset], 8920 dap, da_pdlist); 8921 } 8922 } 8923 done: 8924 if (jmvref) { 8925 jmvref->jm_pagedep = pagedep; 8926 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps); 8927 add_to_journal(&jmvref->jm_list); 8928 } 8929 bcopy(oldloc, newloc, entrysize); 8930 FREE_LOCK(ump); 8931 } 8932 8933 /* 8934 * Move the mkdir dependencies and journal work from one diradd to another 8935 * when renaming a directory. The new name must depend on the mkdir deps 8936 * completing as the old name did. Directories can only have one valid link 8937 * at a time so one must be canonical. 8938 */ 8939 static void 8940 merge_diradd(struct inodedep *inodedep, struct diradd *newdap) 8941 { 8942 struct diradd *olddap; 8943 struct mkdir *mkdir, *nextmd; 8944 struct ufsmount *ump; 8945 short state; 8946 8947 olddap = inodedep->id_mkdiradd; 8948 inodedep->id_mkdiradd = newdap; 8949 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8950 newdap->da_state &= ~DEPCOMPLETE; 8951 ump = VFSTOUFS(inodedep->id_list.wk_mp); 8952 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 8953 mkdir = nextmd) { 8954 nextmd = LIST_NEXT(mkdir, md_mkdirs); 8955 if (mkdir->md_diradd != olddap) 8956 continue; 8957 mkdir->md_diradd = newdap; 8958 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY); 8959 newdap->da_state |= state; 8960 olddap->da_state &= ~state; 8961 if ((olddap->da_state & 8962 (MKDIR_PARENT | MKDIR_BODY)) == 0) 8963 break; 8964 } 8965 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 8966 panic("merge_diradd: unfound ref"); 8967 } 8968 /* 8969 * Any mkdir related journal items are not safe to be freed until 8970 * the new name is stable. 8971 */ 8972 jwork_move(&newdap->da_jwork, &olddap->da_jwork); 8973 olddap->da_state |= DEPCOMPLETE; 8974 complete_diradd(olddap); 8975 } 8976 8977 /* 8978 * Move the diradd to the pending list when all diradd dependencies are 8979 * complete. 8980 */ 8981 static void 8982 complete_diradd(struct diradd *dap) 8983 { 8984 struct pagedep *pagedep; 8985 8986 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 8987 if (dap->da_state & DIRCHG) 8988 pagedep = dap->da_previous->dm_pagedep; 8989 else 8990 pagedep = dap->da_pagedep; 8991 LIST_REMOVE(dap, da_pdlist); 8992 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 8993 } 8994 } 8995 8996 /* 8997 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal 8998 * add entries and conditionally journal the remove. 8999 */ 9000 static void 9001 cancel_diradd( 9002 struct diradd *dap, 9003 struct dirrem *dirrem, 9004 struct jremref *jremref, 9005 struct jremref *dotremref, 9006 struct jremref *dotdotremref) 9007 { 9008 struct inodedep *inodedep; 9009 struct jaddref *jaddref; 9010 struct inoref *inoref; 9011 struct ufsmount *ump; 9012 struct mkdir *mkdir; 9013 9014 /* 9015 * If no remove references were allocated we're on a non-journaled 9016 * filesystem and can skip the cancel step. 9017 */ 9018 if (jremref == NULL) { 9019 free_diradd(dap, NULL); 9020 return; 9021 } 9022 /* 9023 * Cancel the primary name an free it if it does not require 9024 * journaling. 9025 */ 9026 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum, 9027 0, &inodedep) != 0) { 9028 /* Abort the addref that reference this diradd. */ 9029 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 9030 if (inoref->if_list.wk_type != D_JADDREF) 9031 continue; 9032 jaddref = (struct jaddref *)inoref; 9033 if (jaddref->ja_diradd != dap) 9034 continue; 9035 if (cancel_jaddref(jaddref, inodedep, 9036 &dirrem->dm_jwork) == 0) { 9037 free_jremref(jremref); 9038 jremref = NULL; 9039 } 9040 break; 9041 } 9042 } 9043 /* 9044 * Cancel subordinate names and free them if they do not require 9045 * journaling. 9046 */ 9047 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 9048 ump = VFSTOUFS(dap->da_list.wk_mp); 9049 LIST_FOREACH(mkdir, &ump->softdep_mkdirlisthd, md_mkdirs) { 9050 if (mkdir->md_diradd != dap) 9051 continue; 9052 if ((jaddref = mkdir->md_jaddref) == NULL) 9053 continue; 9054 mkdir->md_jaddref = NULL; 9055 if (mkdir->md_state & MKDIR_PARENT) { 9056 if (cancel_jaddref(jaddref, NULL, 9057 &dirrem->dm_jwork) == 0) { 9058 free_jremref(dotdotremref); 9059 dotdotremref = NULL; 9060 } 9061 } else { 9062 if (cancel_jaddref(jaddref, inodedep, 9063 &dirrem->dm_jwork) == 0) { 9064 free_jremref(dotremref); 9065 dotremref = NULL; 9066 } 9067 } 9068 } 9069 } 9070 9071 if (jremref) 9072 journal_jremref(dirrem, jremref, inodedep); 9073 if (dotremref) 9074 journal_jremref(dirrem, dotremref, inodedep); 9075 if (dotdotremref) 9076 journal_jremref(dirrem, dotdotremref, NULL); 9077 jwork_move(&dirrem->dm_jwork, &dap->da_jwork); 9078 free_diradd(dap, &dirrem->dm_jwork); 9079 } 9080 9081 /* 9082 * Free a diradd dependency structure. 9083 */ 9084 static void 9085 free_diradd(struct diradd *dap, struct workhead *wkhd) 9086 { 9087 struct dirrem *dirrem; 9088 struct pagedep *pagedep; 9089 struct inodedep *inodedep; 9090 struct mkdir *mkdir, *nextmd; 9091 struct ufsmount *ump; 9092 9093 ump = VFSTOUFS(dap->da_list.wk_mp); 9094 LOCK_OWNED(ump); 9095 LIST_REMOVE(dap, da_pdlist); 9096 if (dap->da_state & ONWORKLIST) 9097 WORKLIST_REMOVE(&dap->da_list); 9098 if ((dap->da_state & DIRCHG) == 0) { 9099 pagedep = dap->da_pagedep; 9100 } else { 9101 dirrem = dap->da_previous; 9102 pagedep = dirrem->dm_pagedep; 9103 dirrem->dm_dirinum = pagedep->pd_ino; 9104 dirrem->dm_state |= COMPLETE; 9105 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9106 add_to_worklist(&dirrem->dm_list, 0); 9107 } 9108 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum, 9109 0, &inodedep) != 0) 9110 if (inodedep->id_mkdiradd == dap) 9111 inodedep->id_mkdiradd = NULL; 9112 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 9113 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 9114 mkdir = nextmd) { 9115 nextmd = LIST_NEXT(mkdir, md_mkdirs); 9116 if (mkdir->md_diradd != dap) 9117 continue; 9118 dap->da_state &= 9119 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 9120 LIST_REMOVE(mkdir, md_mkdirs); 9121 if (mkdir->md_state & ONWORKLIST) 9122 WORKLIST_REMOVE(&mkdir->md_list); 9123 if (mkdir->md_jaddref != NULL) 9124 panic("free_diradd: Unexpected jaddref"); 9125 WORKITEM_FREE(mkdir, D_MKDIR); 9126 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) 9127 break; 9128 } 9129 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 9130 panic("free_diradd: unfound ref"); 9131 } 9132 if (inodedep) 9133 free_inodedep(inodedep); 9134 /* 9135 * Free any journal segments waiting for the directory write. 9136 */ 9137 handle_jwork(&dap->da_jwork); 9138 WORKITEM_FREE(dap, D_DIRADD); 9139 } 9140 9141 /* 9142 * Directory entry removal dependencies. 9143 * 9144 * When removing a directory entry, the entry's inode pointer must be 9145 * zero'ed on disk before the corresponding inode's link count is decremented 9146 * (possibly freeing the inode for re-use). This dependency is handled by 9147 * updating the directory entry but delaying the inode count reduction until 9148 * after the directory block has been written to disk. After this point, the 9149 * inode count can be decremented whenever it is convenient. 9150 */ 9151 9152 /* 9153 * This routine should be called immediately after removing 9154 * a directory entry. The inode's link count should not be 9155 * decremented by the calling procedure -- the soft updates 9156 * code will do this task when it is safe. 9157 */ 9158 void 9159 softdep_setup_remove( 9160 struct buf *bp, /* buffer containing directory block */ 9161 struct inode *dp, /* inode for the directory being modified */ 9162 struct inode *ip, /* inode for directory entry being removed */ 9163 int isrmdir) /* indicates if doing RMDIR */ 9164 { 9165 struct dirrem *dirrem, *prevdirrem; 9166 struct inodedep *inodedep; 9167 struct ufsmount *ump; 9168 int direct; 9169 9170 ump = ITOUMP(ip); 9171 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9172 ("softdep_setup_remove called on non-softdep filesystem")); 9173 /* 9174 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want 9175 * newdirrem() to setup the full directory remove which requires 9176 * isrmdir > 1. 9177 */ 9178 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 9179 /* 9180 * Add the dirrem to the inodedep's pending remove list for quick 9181 * discovery later. 9182 */ 9183 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) 9184 panic("softdep_setup_remove: Lost inodedep."); 9185 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 9186 dirrem->dm_state |= ONDEPLIST; 9187 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9188 9189 /* 9190 * If the COMPLETE flag is clear, then there were no active 9191 * entries and we want to roll back to a zeroed entry until 9192 * the new inode is committed to disk. If the COMPLETE flag is 9193 * set then we have deleted an entry that never made it to 9194 * disk. If the entry we deleted resulted from a name change, 9195 * then the old name still resides on disk. We cannot delete 9196 * its inode (returned to us in prevdirrem) until the zeroed 9197 * directory entry gets to disk. The new inode has never been 9198 * referenced on the disk, so can be deleted immediately. 9199 */ 9200 if ((dirrem->dm_state & COMPLETE) == 0) { 9201 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem, 9202 dm_next); 9203 FREE_LOCK(ump); 9204 } else { 9205 if (prevdirrem != NULL) 9206 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, 9207 prevdirrem, dm_next); 9208 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino; 9209 direct = LIST_EMPTY(&dirrem->dm_jremrefhd); 9210 FREE_LOCK(ump); 9211 if (direct) 9212 handle_workitem_remove(dirrem, 0); 9213 } 9214 } 9215 9216 /* 9217 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the 9218 * pd_pendinghd list of a pagedep. 9219 */ 9220 static struct diradd * 9221 diradd_lookup(struct pagedep *pagedep, int offset) 9222 { 9223 struct diradd *dap; 9224 9225 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist) 9226 if (dap->da_offset == offset) 9227 return (dap); 9228 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 9229 if (dap->da_offset == offset) 9230 return (dap); 9231 return (NULL); 9232 } 9233 9234 /* 9235 * Search for a .. diradd dependency in a directory that is being removed. 9236 * If the directory was renamed to a new parent we have a diradd rather 9237 * than a mkdir for the .. entry. We need to cancel it now before 9238 * it is found in truncate(). 9239 */ 9240 static struct jremref * 9241 cancel_diradd_dotdot(struct inode *ip, 9242 struct dirrem *dirrem, 9243 struct jremref *jremref) 9244 { 9245 struct pagedep *pagedep; 9246 struct diradd *dap; 9247 struct worklist *wk; 9248 9249 if (pagedep_lookup(ITOVFS(ip), NULL, ip->i_number, 0, 0, &pagedep) == 0) 9250 return (jremref); 9251 dap = diradd_lookup(pagedep, DOTDOT_OFFSET); 9252 if (dap == NULL) 9253 return (jremref); 9254 cancel_diradd(dap, dirrem, jremref, NULL, NULL); 9255 /* 9256 * Mark any journal work as belonging to the parent so it is freed 9257 * with the .. reference. 9258 */ 9259 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9260 wk->wk_state |= MKDIR_PARENT; 9261 return (NULL); 9262 } 9263 9264 /* 9265 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to 9266 * replace it with a dirrem/diradd pair as a result of re-parenting a 9267 * directory. This ensures that we don't simultaneously have a mkdir and 9268 * a diradd for the same .. entry. 9269 */ 9270 static struct jremref * 9271 cancel_mkdir_dotdot(struct inode *ip, 9272 struct dirrem *dirrem, 9273 struct jremref *jremref) 9274 { 9275 struct inodedep *inodedep; 9276 struct jaddref *jaddref; 9277 struct ufsmount *ump; 9278 struct mkdir *mkdir; 9279 struct diradd *dap; 9280 struct mount *mp; 9281 9282 mp = ITOVFS(ip); 9283 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9284 return (jremref); 9285 dap = inodedep->id_mkdiradd; 9286 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0) 9287 return (jremref); 9288 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9289 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 9290 mkdir = LIST_NEXT(mkdir, md_mkdirs)) 9291 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT) 9292 break; 9293 if (mkdir == NULL) 9294 panic("cancel_mkdir_dotdot: Unable to find mkdir\n"); 9295 if ((jaddref = mkdir->md_jaddref) != NULL) { 9296 mkdir->md_jaddref = NULL; 9297 jaddref->ja_state &= ~MKDIR_PARENT; 9298 if (inodedep_lookup(mp, jaddref->ja_ino, 0, &inodedep) == 0) 9299 panic("cancel_mkdir_dotdot: Lost parent inodedep"); 9300 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) { 9301 journal_jremref(dirrem, jremref, inodedep); 9302 jremref = NULL; 9303 } 9304 } 9305 if (mkdir->md_state & ONWORKLIST) 9306 WORKLIST_REMOVE(&mkdir->md_list); 9307 mkdir->md_state |= ALLCOMPLETE; 9308 complete_mkdir(mkdir); 9309 return (jremref); 9310 } 9311 9312 static void 9313 journal_jremref(struct dirrem *dirrem, 9314 struct jremref *jremref, 9315 struct inodedep *inodedep) 9316 { 9317 9318 if (inodedep == NULL) 9319 if (inodedep_lookup(jremref->jr_list.wk_mp, 9320 jremref->jr_ref.if_ino, 0, &inodedep) == 0) 9321 panic("journal_jremref: Lost inodedep"); 9322 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps); 9323 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 9324 add_to_journal(&jremref->jr_list); 9325 } 9326 9327 static void 9328 dirrem_journal( 9329 struct dirrem *dirrem, 9330 struct jremref *jremref, 9331 struct jremref *dotremref, 9332 struct jremref *dotdotremref) 9333 { 9334 struct inodedep *inodedep; 9335 9336 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0, 9337 &inodedep) == 0) 9338 panic("dirrem_journal: Lost inodedep"); 9339 journal_jremref(dirrem, jremref, inodedep); 9340 if (dotremref) 9341 journal_jremref(dirrem, dotremref, inodedep); 9342 if (dotdotremref) 9343 journal_jremref(dirrem, dotdotremref, NULL); 9344 } 9345 9346 /* 9347 * Allocate a new dirrem if appropriate and return it along with 9348 * its associated pagedep. Called without a lock, returns with lock. 9349 */ 9350 static struct dirrem * 9351 newdirrem( 9352 struct buf *bp, /* buffer containing directory block */ 9353 struct inode *dp, /* inode for the directory being modified */ 9354 struct inode *ip, /* inode for directory entry being removed */ 9355 int isrmdir, /* indicates if doing RMDIR */ 9356 struct dirrem **prevdirremp) /* previously referenced inode, if any */ 9357 { 9358 int offset; 9359 ufs_lbn_t lbn; 9360 struct diradd *dap; 9361 struct dirrem *dirrem; 9362 struct pagedep *pagedep; 9363 struct jremref *jremref; 9364 struct jremref *dotremref; 9365 struct jremref *dotdotremref; 9366 struct vnode *dvp; 9367 struct ufsmount *ump; 9368 9369 /* 9370 * Whiteouts have no deletion dependencies. 9371 */ 9372 if (ip == NULL) 9373 panic("newdirrem: whiteout"); 9374 dvp = ITOV(dp); 9375 ump = ITOUMP(dp); 9376 9377 /* 9378 * If the system is over its limit and our filesystem is 9379 * responsible for more than our share of that usage and 9380 * we are not a snapshot, request some inodedep cleanup. 9381 * Limiting the number of dirrem structures will also limit 9382 * the number of freefile and freeblks structures. 9383 */ 9384 ACQUIRE_LOCK(ump); 9385 if (!IS_SNAPSHOT(ip) && softdep_excess_items(ump, D_DIRREM)) 9386 schedule_cleanup(UFSTOVFS(ump)); 9387 else 9388 FREE_LOCK(ump); 9389 dirrem = malloc(sizeof(struct dirrem), M_DIRREM, M_SOFTDEP_FLAGS | 9390 M_ZERO); 9391 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount); 9392 LIST_INIT(&dirrem->dm_jremrefhd); 9393 LIST_INIT(&dirrem->dm_jwork); 9394 dirrem->dm_state = isrmdir ? RMDIR : 0; 9395 dirrem->dm_oldinum = ip->i_number; 9396 *prevdirremp = NULL; 9397 /* 9398 * Allocate remove reference structures to track journal write 9399 * dependencies. We will always have one for the link and 9400 * when doing directories we will always have one more for dot. 9401 * When renaming a directory we skip the dotdot link change so 9402 * this is not needed. 9403 */ 9404 jremref = dotremref = dotdotremref = NULL; 9405 if (DOINGSUJ(dvp)) { 9406 if (isrmdir) { 9407 jremref = newjremref(dirrem, dp, ip, I_OFFSET(dp), 9408 ip->i_effnlink + 2); 9409 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET, 9410 ip->i_effnlink + 1); 9411 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET, 9412 dp->i_effnlink + 1); 9413 dotdotremref->jr_state |= MKDIR_PARENT; 9414 } else 9415 jremref = newjremref(dirrem, dp, ip, I_OFFSET(dp), 9416 ip->i_effnlink + 1); 9417 } 9418 ACQUIRE_LOCK(ump); 9419 lbn = lblkno(ump->um_fs, I_OFFSET(dp)); 9420 offset = blkoff(ump->um_fs, I_OFFSET(dp)); 9421 pagedep_lookup(UFSTOVFS(ump), bp, dp->i_number, lbn, DEPALLOC, 9422 &pagedep); 9423 dirrem->dm_pagedep = pagedep; 9424 dirrem->dm_offset = offset; 9425 /* 9426 * If we're renaming a .. link to a new directory, cancel any 9427 * existing MKDIR_PARENT mkdir. If it has already been canceled 9428 * the jremref is preserved for any potential diradd in this 9429 * location. This can not coincide with a rmdir. 9430 */ 9431 if (I_OFFSET(dp) == DOTDOT_OFFSET) { 9432 if (isrmdir) 9433 panic("newdirrem: .. directory change during remove?"); 9434 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref); 9435 } 9436 /* 9437 * If we're removing a directory search for the .. dependency now and 9438 * cancel it. Any pending journal work will be added to the dirrem 9439 * to be completed when the workitem remove completes. 9440 */ 9441 if (isrmdir) 9442 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref); 9443 /* 9444 * Check for a diradd dependency for the same directory entry. 9445 * If present, then both dependencies become obsolete and can 9446 * be de-allocated. 9447 */ 9448 dap = diradd_lookup(pagedep, offset); 9449 if (dap == NULL) { 9450 /* 9451 * Link the jremref structures into the dirrem so they are 9452 * written prior to the pagedep. 9453 */ 9454 if (jremref) 9455 dirrem_journal(dirrem, jremref, dotremref, 9456 dotdotremref); 9457 return (dirrem); 9458 } 9459 /* 9460 * Must be ATTACHED at this point. 9461 */ 9462 if ((dap->da_state & ATTACHED) == 0) 9463 panic("newdirrem: not ATTACHED"); 9464 if (dap->da_newinum != ip->i_number) 9465 panic("newdirrem: inum %ju should be %ju", 9466 (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum); 9467 /* 9468 * If we are deleting a changed name that never made it to disk, 9469 * then return the dirrem describing the previous inode (which 9470 * represents the inode currently referenced from this entry on disk). 9471 */ 9472 if ((dap->da_state & DIRCHG) != 0) { 9473 *prevdirremp = dap->da_previous; 9474 dap->da_state &= ~DIRCHG; 9475 dap->da_pagedep = pagedep; 9476 } 9477 /* 9478 * We are deleting an entry that never made it to disk. 9479 * Mark it COMPLETE so we can delete its inode immediately. 9480 */ 9481 dirrem->dm_state |= COMPLETE; 9482 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref); 9483 #ifdef INVARIANTS 9484 if (isrmdir == 0) { 9485 struct worklist *wk; 9486 9487 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9488 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT)) 9489 panic("bad wk %p (0x%X)\n", wk, wk->wk_state); 9490 } 9491 #endif 9492 9493 return (dirrem); 9494 } 9495 9496 /* 9497 * Directory entry change dependencies. 9498 * 9499 * Changing an existing directory entry requires that an add operation 9500 * be completed first followed by a deletion. The semantics for the addition 9501 * are identical to the description of adding a new entry above except 9502 * that the rollback is to the old inode number rather than zero. Once 9503 * the addition dependency is completed, the removal is done as described 9504 * in the removal routine above. 9505 */ 9506 9507 /* 9508 * This routine should be called immediately after changing 9509 * a directory entry. The inode's link count should not be 9510 * decremented by the calling procedure -- the soft updates 9511 * code will perform this task when it is safe. 9512 */ 9513 void 9514 softdep_setup_directory_change( 9515 struct buf *bp, /* buffer containing directory block */ 9516 struct inode *dp, /* inode for the directory being modified */ 9517 struct inode *ip, /* inode for directory entry being removed */ 9518 ino_t newinum, /* new inode number for changed entry */ 9519 int isrmdir) /* indicates if doing RMDIR */ 9520 { 9521 int offset; 9522 struct diradd *dap = NULL; 9523 struct dirrem *dirrem, *prevdirrem; 9524 struct pagedep *pagedep; 9525 struct inodedep *inodedep; 9526 struct jaddref *jaddref; 9527 struct mount *mp; 9528 struct ufsmount *ump; 9529 9530 mp = ITOVFS(dp); 9531 ump = VFSTOUFS(mp); 9532 offset = blkoff(ump->um_fs, I_OFFSET(dp)); 9533 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 9534 ("softdep_setup_directory_change called on non-softdep filesystem")); 9535 9536 /* 9537 * Whiteouts do not need diradd dependencies. 9538 */ 9539 if (newinum != UFS_WINO) { 9540 dap = malloc(sizeof(struct diradd), 9541 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO); 9542 workitem_alloc(&dap->da_list, D_DIRADD, mp); 9543 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE; 9544 dap->da_offset = offset; 9545 dap->da_newinum = newinum; 9546 LIST_INIT(&dap->da_jwork); 9547 } 9548 9549 /* 9550 * Allocate a new dirrem and ACQUIRE_LOCK. 9551 */ 9552 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 9553 pagedep = dirrem->dm_pagedep; 9554 /* 9555 * The possible values for isrmdir: 9556 * 0 - non-directory file rename 9557 * 1 - directory rename within same directory 9558 * inum - directory rename to new directory of given inode number 9559 * When renaming to a new directory, we are both deleting and 9560 * creating a new directory entry, so the link count on the new 9561 * directory should not change. Thus we do not need the followup 9562 * dirrem which is usually done in handle_workitem_remove. We set 9563 * the DIRCHG flag to tell handle_workitem_remove to skip the 9564 * followup dirrem. 9565 */ 9566 if (isrmdir > 1) 9567 dirrem->dm_state |= DIRCHG; 9568 9569 /* 9570 * Whiteouts have no additional dependencies, 9571 * so just put the dirrem on the correct list. 9572 */ 9573 if (newinum == UFS_WINO) { 9574 if ((dirrem->dm_state & COMPLETE) == 0) { 9575 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem, 9576 dm_next); 9577 } else { 9578 dirrem->dm_dirinum = pagedep->pd_ino; 9579 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9580 add_to_worklist(&dirrem->dm_list, 0); 9581 } 9582 FREE_LOCK(ump); 9583 return; 9584 } 9585 /* 9586 * Add the dirrem to the inodedep's pending remove list for quick 9587 * discovery later. A valid nlinkdelta ensures that this lookup 9588 * will not fail. 9589 */ 9590 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9591 panic("softdep_setup_directory_change: Lost inodedep."); 9592 dirrem->dm_state |= ONDEPLIST; 9593 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9594 9595 /* 9596 * If the COMPLETE flag is clear, then there were no active 9597 * entries and we want to roll back to the previous inode until 9598 * the new inode is committed to disk. If the COMPLETE flag is 9599 * set, then we have deleted an entry that never made it to disk. 9600 * If the entry we deleted resulted from a name change, then the old 9601 * inode reference still resides on disk. Any rollback that we do 9602 * needs to be to that old inode (returned to us in prevdirrem). If 9603 * the entry we deleted resulted from a create, then there is 9604 * no entry on the disk, so we want to roll back to zero rather 9605 * than the uncommitted inode. In either of the COMPLETE cases we 9606 * want to immediately free the unwritten and unreferenced inode. 9607 */ 9608 if ((dirrem->dm_state & COMPLETE) == 0) { 9609 dap->da_previous = dirrem; 9610 } else { 9611 if (prevdirrem != NULL) { 9612 dap->da_previous = prevdirrem; 9613 } else { 9614 dap->da_state &= ~DIRCHG; 9615 dap->da_pagedep = pagedep; 9616 } 9617 dirrem->dm_dirinum = pagedep->pd_ino; 9618 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9619 add_to_worklist(&dirrem->dm_list, 0); 9620 } 9621 /* 9622 * Lookup the jaddref for this journal entry. We must finish 9623 * initializing it and make the diradd write dependent on it. 9624 * If we're not journaling, put it on the id_bufwait list if the 9625 * inode is not yet written. If it is written, do the post-inode 9626 * write processing to put it on the id_pendinghd list. 9627 */ 9628 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 9629 if (MOUNTEDSUJ(mp)) { 9630 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 9631 inoreflst); 9632 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 9633 ("softdep_setup_directory_change: bad jaddref %p", 9634 jaddref)); 9635 jaddref->ja_diroff = I_OFFSET(dp); 9636 jaddref->ja_diradd = dap; 9637 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9638 dap, da_pdlist); 9639 add_to_journal(&jaddref->ja_list); 9640 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 9641 dap->da_state |= COMPLETE; 9642 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 9643 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 9644 } else { 9645 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9646 dap, da_pdlist); 9647 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 9648 } 9649 /* 9650 * If we're making a new name for a directory that has not been 9651 * committed when need to move the dot and dotdot references to 9652 * this new name. 9653 */ 9654 if (inodedep->id_mkdiradd && I_OFFSET(dp) != DOTDOT_OFFSET) 9655 merge_diradd(inodedep, dap); 9656 FREE_LOCK(ump); 9657 } 9658 9659 /* 9660 * Called whenever the link count on an inode is changed. 9661 * It creates an inode dependency so that the new reference(s) 9662 * to the inode cannot be committed to disk until the updated 9663 * inode has been written. 9664 */ 9665 void 9666 softdep_change_linkcnt( 9667 struct inode *ip) /* the inode with the increased link count */ 9668 { 9669 struct inodedep *inodedep; 9670 struct ufsmount *ump; 9671 9672 ump = ITOUMP(ip); 9673 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9674 ("softdep_change_linkcnt called on non-softdep filesystem")); 9675 ACQUIRE_LOCK(ump); 9676 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep); 9677 if (ip->i_nlink < ip->i_effnlink) 9678 panic("softdep_change_linkcnt: bad delta"); 9679 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9680 FREE_LOCK(ump); 9681 } 9682 9683 /* 9684 * Attach a sbdep dependency to the superblock buf so that we can keep 9685 * track of the head of the linked list of referenced but unlinked inodes. 9686 */ 9687 void 9688 softdep_setup_sbupdate( 9689 struct ufsmount *ump, 9690 struct fs *fs, 9691 struct buf *bp) 9692 { 9693 struct sbdep *sbdep; 9694 struct worklist *wk; 9695 9696 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9697 ("softdep_setup_sbupdate called on non-softdep filesystem")); 9698 LIST_FOREACH(wk, &bp->b_dep, wk_list) 9699 if (wk->wk_type == D_SBDEP) 9700 break; 9701 if (wk != NULL) 9702 return; 9703 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS); 9704 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump)); 9705 sbdep->sb_fs = fs; 9706 sbdep->sb_ump = ump; 9707 ACQUIRE_LOCK(ump); 9708 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list); 9709 FREE_LOCK(ump); 9710 } 9711 9712 /* 9713 * Return the first unlinked inodedep which is ready to be the head of the 9714 * list. The inodedep and all those after it must have valid next pointers. 9715 */ 9716 static struct inodedep * 9717 first_unlinked_inodedep(struct ufsmount *ump) 9718 { 9719 struct inodedep *inodedep; 9720 struct inodedep *idp; 9721 9722 LOCK_OWNED(ump); 9723 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst); 9724 inodedep; inodedep = idp) { 9725 if ((inodedep->id_state & UNLINKNEXT) == 0) 9726 return (NULL); 9727 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9728 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0) 9729 break; 9730 if ((inodedep->id_state & UNLINKPREV) == 0) 9731 break; 9732 } 9733 return (inodedep); 9734 } 9735 9736 /* 9737 * Set the sujfree unlinked head pointer prior to writing a superblock. 9738 */ 9739 static void 9740 initiate_write_sbdep(struct sbdep *sbdep) 9741 { 9742 struct inodedep *inodedep; 9743 struct fs *bpfs; 9744 struct fs *fs; 9745 9746 bpfs = sbdep->sb_fs; 9747 fs = sbdep->sb_ump->um_fs; 9748 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9749 if (inodedep) { 9750 fs->fs_sujfree = inodedep->id_ino; 9751 inodedep->id_state |= UNLINKPREV; 9752 } else 9753 fs->fs_sujfree = 0; 9754 bpfs->fs_sujfree = fs->fs_sujfree; 9755 /* 9756 * Because we have made changes to the superblock, we need to 9757 * recompute its check-hash. 9758 */ 9759 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs); 9760 } 9761 9762 /* 9763 * After a superblock is written determine whether it must be written again 9764 * due to a changing unlinked list head. 9765 */ 9766 static int 9767 handle_written_sbdep(struct sbdep *sbdep, struct buf *bp) 9768 { 9769 struct inodedep *inodedep; 9770 struct fs *fs; 9771 9772 LOCK_OWNED(sbdep->sb_ump); 9773 fs = sbdep->sb_fs; 9774 /* 9775 * If the superblock doesn't match the in-memory list start over. 9776 */ 9777 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9778 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) || 9779 (inodedep == NULL && fs->fs_sujfree != 0)) { 9780 bdirty(bp); 9781 return (1); 9782 } 9783 WORKITEM_FREE(sbdep, D_SBDEP); 9784 if (fs->fs_sujfree == 0) 9785 return (0); 9786 /* 9787 * Now that we have a record of this inode in stable store allow it 9788 * to be written to free up pending work. Inodes may see a lot of 9789 * write activity after they are unlinked which we must not hold up. 9790 */ 9791 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 9792 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS) 9793 panic("handle_written_sbdep: Bad inodedep %p (0x%X)", 9794 inodedep, inodedep->id_state); 9795 if (inodedep->id_state & UNLINKONLIST) 9796 break; 9797 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST; 9798 } 9799 9800 return (0); 9801 } 9802 9803 /* 9804 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list. 9805 */ 9806 static void 9807 unlinked_inodedep( struct mount *mp, struct inodedep *inodedep) 9808 { 9809 struct ufsmount *ump; 9810 9811 ump = VFSTOUFS(mp); 9812 LOCK_OWNED(ump); 9813 if (MOUNTEDSUJ(mp) == 0) 9814 return; 9815 ump->um_fs->fs_fmod = 1; 9816 if (inodedep->id_state & UNLINKED) 9817 panic("unlinked_inodedep: %p already unlinked\n", inodedep); 9818 inodedep->id_state |= UNLINKED; 9819 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked); 9820 } 9821 9822 /* 9823 * Remove an inodedep from the unlinked inodedep list. This may require 9824 * disk writes if the inode has made it that far. 9825 */ 9826 static void 9827 clear_unlinked_inodedep( struct inodedep *inodedep) 9828 { 9829 struct ufs2_dinode *dip; 9830 struct ufsmount *ump; 9831 struct inodedep *idp; 9832 struct inodedep *idn; 9833 struct fs *fs, *bpfs; 9834 struct buf *bp; 9835 daddr_t dbn; 9836 ino_t ino; 9837 ino_t nino; 9838 ino_t pino; 9839 int error; 9840 9841 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9842 fs = ump->um_fs; 9843 ino = inodedep->id_ino; 9844 error = 0; 9845 for (;;) { 9846 LOCK_OWNED(ump); 9847 KASSERT((inodedep->id_state & UNLINKED) != 0, 9848 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9849 inodedep)); 9850 /* 9851 * If nothing has yet been written simply remove us from 9852 * the in memory list and return. This is the most common 9853 * case where handle_workitem_remove() loses the final 9854 * reference. 9855 */ 9856 if ((inodedep->id_state & UNLINKLINKS) == 0) 9857 break; 9858 /* 9859 * If we have a NEXT pointer and no PREV pointer we can simply 9860 * clear NEXT's PREV and remove ourselves from the list. Be 9861 * careful not to clear PREV if the superblock points at 9862 * next as well. 9863 */ 9864 idn = TAILQ_NEXT(inodedep, id_unlinked); 9865 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) { 9866 if (idn && fs->fs_sujfree != idn->id_ino) 9867 idn->id_state &= ~UNLINKPREV; 9868 break; 9869 } 9870 /* 9871 * Here we have an inodedep which is actually linked into 9872 * the list. We must remove it by forcing a write to the 9873 * link before us, whether it be the superblock or an inode. 9874 * Unfortunately the list may change while we're waiting 9875 * on the buf lock for either resource so we must loop until 9876 * we lock the right one. If both the superblock and an 9877 * inode point to this inode we must clear the inode first 9878 * followed by the superblock. 9879 */ 9880 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9881 pino = 0; 9882 if (idp && (idp->id_state & UNLINKNEXT)) 9883 pino = idp->id_ino; 9884 FREE_LOCK(ump); 9885 if (pino == 0) { 9886 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9887 (int)fs->fs_sbsize, 0, 0, 0); 9888 } else { 9889 dbn = fsbtodb(fs, ino_to_fsba(fs, pino)); 9890 error = ffs_breadz(ump, ump->um_devvp, dbn, dbn, 9891 (int)fs->fs_bsize, NULL, NULL, 0, NOCRED, 0, NULL, 9892 &bp); 9893 } 9894 ACQUIRE_LOCK(ump); 9895 if (error) 9896 break; 9897 /* If the list has changed restart the loop. */ 9898 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9899 nino = 0; 9900 if (idp && (idp->id_state & UNLINKNEXT)) 9901 nino = idp->id_ino; 9902 if (nino != pino || 9903 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) { 9904 FREE_LOCK(ump); 9905 brelse(bp); 9906 ACQUIRE_LOCK(ump); 9907 continue; 9908 } 9909 nino = 0; 9910 idn = TAILQ_NEXT(inodedep, id_unlinked); 9911 if (idn) 9912 nino = idn->id_ino; 9913 /* 9914 * Remove us from the in memory list. After this we cannot 9915 * access the inodedep. 9916 */ 9917 KASSERT((inodedep->id_state & UNLINKED) != 0, 9918 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9919 inodedep)); 9920 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9921 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9922 FREE_LOCK(ump); 9923 /* 9924 * The predecessor's next pointer is manually updated here 9925 * so that the NEXT flag is never cleared for an element 9926 * that is in the list. 9927 */ 9928 if (pino == 0) { 9929 bcopy((caddr_t)fs, bp->b_data, (uint64_t)fs->fs_sbsize); 9930 bpfs = (struct fs *)bp->b_data; 9931 ffs_oldfscompat_write(bpfs, ump); 9932 softdep_setup_sbupdate(ump, bpfs, bp); 9933 /* 9934 * Because we may have made changes to the superblock, 9935 * we need to recompute its check-hash. 9936 */ 9937 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs); 9938 } else if (fs->fs_magic == FS_UFS1_MAGIC) { 9939 ((struct ufs1_dinode *)bp->b_data + 9940 ino_to_fsbo(fs, pino))->di_freelink = nino; 9941 } else { 9942 dip = (struct ufs2_dinode *)bp->b_data + 9943 ino_to_fsbo(fs, pino); 9944 dip->di_freelink = nino; 9945 ffs_update_dinode_ckhash(fs, dip); 9946 } 9947 /* 9948 * If the bwrite fails we have no recourse to recover. The 9949 * filesystem is corrupted already. 9950 */ 9951 bwrite(bp); 9952 ACQUIRE_LOCK(ump); 9953 /* 9954 * If the superblock pointer still needs to be cleared force 9955 * a write here. 9956 */ 9957 if (fs->fs_sujfree == ino) { 9958 FREE_LOCK(ump); 9959 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9960 (int)fs->fs_sbsize, 0, 0, 0); 9961 bcopy((caddr_t)fs, bp->b_data, (uint64_t)fs->fs_sbsize); 9962 bpfs = (struct fs *)bp->b_data; 9963 ffs_oldfscompat_write(bpfs, ump); 9964 softdep_setup_sbupdate(ump, bpfs, bp); 9965 /* 9966 * Because we may have made changes to the superblock, 9967 * we need to recompute its check-hash. 9968 */ 9969 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs); 9970 bwrite(bp); 9971 ACQUIRE_LOCK(ump); 9972 } 9973 9974 if (fs->fs_sujfree != ino) 9975 return; 9976 panic("clear_unlinked_inodedep: Failed to clear free head"); 9977 } 9978 if (inodedep->id_ino == fs->fs_sujfree) 9979 panic("clear_unlinked_inodedep: Freeing head of free list"); 9980 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9981 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9982 return; 9983 } 9984 9985 /* 9986 * This workitem decrements the inode's link count. 9987 * If the link count reaches zero, the file is removed. 9988 */ 9989 static int 9990 handle_workitem_remove(struct dirrem *dirrem, int flags) 9991 { 9992 struct inodedep *inodedep; 9993 struct workhead dotdotwk; 9994 struct worklist *wk; 9995 struct ufsmount *ump; 9996 struct mount *mp; 9997 struct vnode *vp; 9998 struct inode *ip; 9999 ino_t oldinum; 10000 10001 if (dirrem->dm_state & ONWORKLIST) 10002 panic("handle_workitem_remove: dirrem %p still on worklist", 10003 dirrem); 10004 oldinum = dirrem->dm_oldinum; 10005 mp = dirrem->dm_list.wk_mp; 10006 ump = VFSTOUFS(mp); 10007 flags |= LK_EXCLUSIVE; 10008 if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ | 10009 FFSV_FORCEINODEDEP) != 0) 10010 return (EBUSY); 10011 ip = VTOI(vp); 10012 MPASS(ip->i_mode != 0); 10013 ACQUIRE_LOCK(ump); 10014 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0) 10015 panic("handle_workitem_remove: lost inodedep"); 10016 if (dirrem->dm_state & ONDEPLIST) 10017 LIST_REMOVE(dirrem, dm_inonext); 10018 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 10019 ("handle_workitem_remove: Journal entries not written.")); 10020 10021 /* 10022 * Move all dependencies waiting on the remove to complete 10023 * from the dirrem to the inode inowait list to be completed 10024 * after the inode has been updated and written to disk. 10025 * 10026 * Any marked MKDIR_PARENT are saved to be completed when the 10027 * dotdot ref is removed unless DIRCHG is specified. For 10028 * directory change operations there will be no further 10029 * directory writes and the jsegdeps need to be moved along 10030 * with the rest to be completed when the inode is free or 10031 * stable in the inode free list. 10032 */ 10033 LIST_INIT(&dotdotwk); 10034 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) { 10035 WORKLIST_REMOVE(wk); 10036 if ((dirrem->dm_state & DIRCHG) == 0 && 10037 wk->wk_state & MKDIR_PARENT) { 10038 wk->wk_state &= ~MKDIR_PARENT; 10039 WORKLIST_INSERT(&dotdotwk, wk); 10040 continue; 10041 } 10042 WORKLIST_INSERT(&inodedep->id_inowait, wk); 10043 } 10044 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list); 10045 /* 10046 * Normal file deletion. 10047 */ 10048 if ((dirrem->dm_state & RMDIR) == 0) { 10049 ip->i_nlink--; 10050 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: file ino " 10051 "%ju negative i_nlink %d", (intmax_t)ip->i_number, 10052 ip->i_nlink)); 10053 DIP_SET(ip, i_nlink, ip->i_nlink); 10054 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 10055 if (ip->i_nlink < ip->i_effnlink) 10056 panic("handle_workitem_remove: bad file delta"); 10057 if (ip->i_nlink == 0) 10058 unlinked_inodedep(mp, inodedep); 10059 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 10060 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 10061 ("handle_workitem_remove: worklist not empty. %s", 10062 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type))); 10063 WORKITEM_FREE(dirrem, D_DIRREM); 10064 FREE_LOCK(ump); 10065 goto out; 10066 } 10067 /* 10068 * Directory deletion. Decrement reference count for both the 10069 * just deleted parent directory entry and the reference for ".". 10070 * Arrange to have the reference count on the parent decremented 10071 * to account for the loss of "..". 10072 */ 10073 ip->i_nlink -= 2; 10074 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: directory ino " 10075 "%ju negative i_nlink %d", (intmax_t)ip->i_number, ip->i_nlink)); 10076 DIP_SET(ip, i_nlink, ip->i_nlink); 10077 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 10078 if (ip->i_nlink < ip->i_effnlink) 10079 panic("handle_workitem_remove: bad dir delta"); 10080 if (ip->i_nlink == 0) 10081 unlinked_inodedep(mp, inodedep); 10082 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 10083 /* 10084 * Rename a directory to a new parent. Since, we are both deleting 10085 * and creating a new directory entry, the link count on the new 10086 * directory should not change. Thus we skip the followup dirrem. 10087 */ 10088 if (dirrem->dm_state & DIRCHG) { 10089 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 10090 ("handle_workitem_remove: DIRCHG and worklist not empty.")); 10091 WORKITEM_FREE(dirrem, D_DIRREM); 10092 FREE_LOCK(ump); 10093 goto out; 10094 } 10095 dirrem->dm_state = ONDEPLIST; 10096 dirrem->dm_oldinum = dirrem->dm_dirinum; 10097 /* 10098 * Place the dirrem on the parent's diremhd list. 10099 */ 10100 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0) 10101 panic("handle_workitem_remove: lost dir inodedep"); 10102 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 10103 /* 10104 * If the allocated inode has never been written to disk, then 10105 * the on-disk inode is zero'ed and we can remove the file 10106 * immediately. When journaling if the inode has been marked 10107 * unlinked and not DEPCOMPLETE we know it can never be written. 10108 */ 10109 inodedep_lookup(mp, oldinum, 0, &inodedep); 10110 if (inodedep == NULL || 10111 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED || 10112 check_inode_unwritten(inodedep)) { 10113 FREE_LOCK(ump); 10114 vput(vp); 10115 return handle_workitem_remove(dirrem, flags); 10116 } 10117 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list); 10118 FREE_LOCK(ump); 10119 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 10120 out: 10121 ffs_update(vp, 0); 10122 vput(vp); 10123 return (0); 10124 } 10125 10126 /* 10127 * Inode de-allocation dependencies. 10128 * 10129 * When an inode's link count is reduced to zero, it can be de-allocated. We 10130 * found it convenient to postpone de-allocation until after the inode is 10131 * written to disk with its new link count (zero). At this point, all of the 10132 * on-disk inode's block pointers are nullified and, with careful dependency 10133 * list ordering, all dependencies related to the inode will be satisfied and 10134 * the corresponding dependency structures de-allocated. So, if/when the 10135 * inode is reused, there will be no mixing of old dependencies with new 10136 * ones. This artificial dependency is set up by the block de-allocation 10137 * procedure above (softdep_setup_freeblocks) and completed by the 10138 * following procedure. 10139 */ 10140 static void 10141 handle_workitem_freefile(struct freefile *freefile) 10142 { 10143 struct workhead wkhd; 10144 struct fs *fs; 10145 struct ufsmount *ump; 10146 int error; 10147 #ifdef INVARIANTS 10148 struct inodedep *idp; 10149 #endif 10150 10151 ump = VFSTOUFS(freefile->fx_list.wk_mp); 10152 fs = ump->um_fs; 10153 #ifdef INVARIANTS 10154 ACQUIRE_LOCK(ump); 10155 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp); 10156 FREE_LOCK(ump); 10157 if (error) 10158 panic("handle_workitem_freefile: inodedep %p survived", idp); 10159 #endif 10160 UFS_LOCK(ump); 10161 fs->fs_pendinginodes -= 1; 10162 UFS_UNLOCK(ump); 10163 LIST_INIT(&wkhd); 10164 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list); 10165 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp, 10166 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0) 10167 softdep_error("handle_workitem_freefile", error); 10168 ACQUIRE_LOCK(ump); 10169 WORKITEM_FREE(freefile, D_FREEFILE); 10170 FREE_LOCK(ump); 10171 } 10172 10173 /* 10174 * Helper function which unlinks marker element from work list and returns 10175 * the next element on the list. 10176 */ 10177 static __inline struct worklist * 10178 markernext(struct worklist *marker) 10179 { 10180 struct worklist *next; 10181 10182 next = LIST_NEXT(marker, wk_list); 10183 LIST_REMOVE(marker, wk_list); 10184 return next; 10185 } 10186 10187 /* 10188 * Disk writes. 10189 * 10190 * The dependency structures constructed above are most actively used when file 10191 * system blocks are written to disk. No constraints are placed on when a 10192 * block can be written, but unsatisfied update dependencies are made safe by 10193 * modifying (or replacing) the source memory for the duration of the disk 10194 * write. When the disk write completes, the memory block is again brought 10195 * up-to-date. 10196 * 10197 * In-core inode structure reclamation. 10198 * 10199 * Because there are a finite number of "in-core" inode structures, they are 10200 * reused regularly. By transferring all inode-related dependencies to the 10201 * in-memory inode block and indexing them separately (via "inodedep"s), we 10202 * can allow "in-core" inode structures to be reused at any time and avoid 10203 * any increase in contention. 10204 * 10205 * Called just before entering the device driver to initiate a new disk I/O. 10206 * The buffer must be locked, thus, no I/O completion operations can occur 10207 * while we are manipulating its associated dependencies. 10208 */ 10209 static void 10210 softdep_disk_io_initiation( 10211 struct buf *bp) /* structure describing disk write to occur */ 10212 { 10213 struct worklist *wk; 10214 struct worklist marker; 10215 struct inodedep *inodedep; 10216 struct freeblks *freeblks; 10217 struct jblkdep *jblkdep; 10218 struct newblk *newblk; 10219 struct ufsmount *ump; 10220 10221 /* 10222 * We only care about write operations. There should never 10223 * be dependencies for reads. 10224 */ 10225 if (bp->b_iocmd != BIO_WRITE) 10226 panic("softdep_disk_io_initiation: not write"); 10227 10228 if (bp->b_vflags & BV_BKGRDINPROG) 10229 panic("softdep_disk_io_initiation: Writing buffer with " 10230 "background write in progress: %p", bp); 10231 10232 ump = softdep_bp_to_mp(bp); 10233 if (ump == NULL) 10234 return; 10235 10236 marker.wk_type = D_LAST + 1; /* Not a normal workitem */ 10237 PHOLD(curproc); /* Don't swap out kernel stack */ 10238 ACQUIRE_LOCK(ump); 10239 /* 10240 * Do any necessary pre-I/O processing. 10241 */ 10242 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL; 10243 wk = markernext(&marker)) { 10244 LIST_INSERT_AFTER(wk, &marker, wk_list); 10245 switch (wk->wk_type) { 10246 case D_PAGEDEP: 10247 initiate_write_filepage(WK_PAGEDEP(wk), bp); 10248 continue; 10249 10250 case D_INODEDEP: 10251 inodedep = WK_INODEDEP(wk); 10252 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) 10253 initiate_write_inodeblock_ufs1(inodedep, bp); 10254 else 10255 initiate_write_inodeblock_ufs2(inodedep, bp); 10256 continue; 10257 10258 case D_INDIRDEP: 10259 initiate_write_indirdep(WK_INDIRDEP(wk), bp); 10260 continue; 10261 10262 case D_BMSAFEMAP: 10263 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp); 10264 continue; 10265 10266 case D_JSEG: 10267 WK_JSEG(wk)->js_buf = NULL; 10268 continue; 10269 10270 case D_FREEBLKS: 10271 freeblks = WK_FREEBLKS(wk); 10272 jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd); 10273 /* 10274 * We have to wait for the freeblks to be journaled 10275 * before we can write an inodeblock with updated 10276 * pointers. Be careful to arrange the marker so 10277 * we revisit the freeblks if it's not removed by 10278 * the first jwait(). 10279 */ 10280 if (jblkdep != NULL) { 10281 LIST_REMOVE(&marker, wk_list); 10282 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10283 jwait(&jblkdep->jb_list, MNT_WAIT); 10284 } 10285 continue; 10286 case D_ALLOCDIRECT: 10287 case D_ALLOCINDIR: 10288 /* 10289 * We have to wait for the jnewblk to be journaled 10290 * before we can write to a block if the contents 10291 * may be confused with an earlier file's indirect 10292 * at recovery time. Handle the marker as described 10293 * above. 10294 */ 10295 newblk = WK_NEWBLK(wk); 10296 if (newblk->nb_jnewblk != NULL && 10297 indirblk_lookup(newblk->nb_list.wk_mp, 10298 newblk->nb_newblkno)) { 10299 LIST_REMOVE(&marker, wk_list); 10300 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10301 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 10302 } 10303 continue; 10304 10305 case D_SBDEP: 10306 initiate_write_sbdep(WK_SBDEP(wk)); 10307 continue; 10308 10309 case D_MKDIR: 10310 case D_FREEWORK: 10311 case D_FREEDEP: 10312 case D_JSEGDEP: 10313 continue; 10314 10315 default: 10316 panic("handle_disk_io_initiation: Unexpected type %s", 10317 TYPENAME(wk->wk_type)); 10318 /* NOTREACHED */ 10319 } 10320 } 10321 FREE_LOCK(ump); 10322 PRELE(curproc); /* Allow swapout of kernel stack */ 10323 } 10324 10325 /* 10326 * Called from within the procedure above to deal with unsatisfied 10327 * allocation dependencies in a directory. The buffer must be locked, 10328 * thus, no I/O completion operations can occur while we are 10329 * manipulating its associated dependencies. 10330 */ 10331 static void 10332 initiate_write_filepage(struct pagedep *pagedep, struct buf *bp) 10333 { 10334 struct jremref *jremref; 10335 struct jmvref *jmvref; 10336 struct dirrem *dirrem; 10337 struct diradd *dap; 10338 struct direct *ep; 10339 int i; 10340 10341 if (pagedep->pd_state & IOSTARTED) { 10342 /* 10343 * This can only happen if there is a driver that does not 10344 * understand chaining. Here biodone will reissue the call 10345 * to strategy for the incomplete buffers. 10346 */ 10347 printf("initiate_write_filepage: already started\n"); 10348 return; 10349 } 10350 pagedep->pd_state |= IOSTARTED; 10351 /* 10352 * Wait for all journal remove dependencies to hit the disk. 10353 * We can not allow any potentially conflicting directory adds 10354 * to be visible before removes and rollback is too difficult. 10355 * The per-filesystem lock may be dropped and re-acquired, however 10356 * we hold the buf locked so the dependency can not go away. 10357 */ 10358 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) 10359 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) 10360 jwait(&jremref->jr_list, MNT_WAIT); 10361 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) 10362 jwait(&jmvref->jm_list, MNT_WAIT); 10363 for (i = 0; i < DAHASHSZ; i++) { 10364 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 10365 ep = (struct direct *) 10366 ((char *)bp->b_data + dap->da_offset); 10367 if (ep->d_ino != dap->da_newinum) 10368 panic("%s: dir inum %ju != new %ju", 10369 "initiate_write_filepage", 10370 (uintmax_t)ep->d_ino, 10371 (uintmax_t)dap->da_newinum); 10372 if (dap->da_state & DIRCHG) 10373 ep->d_ino = dap->da_previous->dm_oldinum; 10374 else 10375 ep->d_ino = 0; 10376 dap->da_state &= ~ATTACHED; 10377 dap->da_state |= UNDONE; 10378 } 10379 } 10380 } 10381 10382 /* 10383 * Version of initiate_write_inodeblock that handles UFS1 dinodes. 10384 * Note that any bug fixes made to this routine must be done in the 10385 * version found below. 10386 * 10387 * Called from within the procedure above to deal with unsatisfied 10388 * allocation dependencies in an inodeblock. The buffer must be 10389 * locked, thus, no I/O completion operations can occur while we 10390 * are manipulating its associated dependencies. 10391 */ 10392 static void 10393 initiate_write_inodeblock_ufs1( 10394 struct inodedep *inodedep, 10395 struct buf *bp) /* The inode block */ 10396 { 10397 struct allocdirect *adp, *lastadp; 10398 struct ufs1_dinode *dp; 10399 struct ufs1_dinode *sip; 10400 struct inoref *inoref; 10401 struct ufsmount *ump; 10402 struct fs *fs; 10403 ufs_lbn_t i; 10404 #ifdef INVARIANTS 10405 ufs_lbn_t prevlbn = 0; 10406 #endif 10407 int deplist __diagused; 10408 10409 if (inodedep->id_state & IOSTARTED) 10410 panic("initiate_write_inodeblock_ufs1: already started"); 10411 inodedep->id_state |= IOSTARTED; 10412 fs = inodedep->id_fs; 10413 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10414 LOCK_OWNED(ump); 10415 dp = (struct ufs1_dinode *)bp->b_data + 10416 ino_to_fsbo(fs, inodedep->id_ino); 10417 10418 /* 10419 * If we're on the unlinked list but have not yet written our 10420 * next pointer initialize it here. 10421 */ 10422 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10423 struct inodedep *inon; 10424 10425 inon = TAILQ_NEXT(inodedep, id_unlinked); 10426 dp->di_freelink = inon ? inon->id_ino : 0; 10427 } 10428 /* 10429 * If the bitmap is not yet written, then the allocated 10430 * inode cannot be written to disk. 10431 */ 10432 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10433 if (inodedep->id_savedino1 != NULL) 10434 panic("initiate_write_inodeblock_ufs1: I/O underway"); 10435 FREE_LOCK(ump); 10436 sip = malloc(sizeof(struct ufs1_dinode), 10437 M_SAVEDINO, M_SOFTDEP_FLAGS); 10438 ACQUIRE_LOCK(ump); 10439 inodedep->id_savedino1 = sip; 10440 *inodedep->id_savedino1 = *dp; 10441 bzero((caddr_t)dp, sizeof(struct ufs1_dinode)); 10442 dp->di_gen = inodedep->id_savedino1->di_gen; 10443 dp->di_freelink = inodedep->id_savedino1->di_freelink; 10444 return; 10445 } 10446 /* 10447 * If no dependencies, then there is nothing to roll back. 10448 */ 10449 inodedep->id_savedsize = dp->di_size; 10450 inodedep->id_savedextsize = 0; 10451 inodedep->id_savednlink = dp->di_nlink; 10452 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10453 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10454 return; 10455 /* 10456 * Revert the link count to that of the first unwritten journal entry. 10457 */ 10458 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10459 if (inoref) 10460 dp->di_nlink = inoref->if_nlink; 10461 /* 10462 * Set the dependencies to busy. 10463 */ 10464 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10465 adp = TAILQ_NEXT(adp, ad_next)) { 10466 #ifdef INVARIANTS 10467 if (deplist != 0 && prevlbn >= adp->ad_offset) 10468 panic("softdep_write_inodeblock: lbn order"); 10469 prevlbn = adp->ad_offset; 10470 if (adp->ad_offset < UFS_NDADDR && 10471 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10472 panic("initiate_write_inodeblock_ufs1: " 10473 "direct pointer #%jd mismatch %d != %jd", 10474 (intmax_t)adp->ad_offset, 10475 dp->di_db[adp->ad_offset], 10476 (intmax_t)adp->ad_newblkno); 10477 if (adp->ad_offset >= UFS_NDADDR && 10478 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno) 10479 panic("initiate_write_inodeblock_ufs1: " 10480 "indirect pointer #%jd mismatch %d != %jd", 10481 (intmax_t)adp->ad_offset - UFS_NDADDR, 10482 dp->di_ib[adp->ad_offset - UFS_NDADDR], 10483 (intmax_t)adp->ad_newblkno); 10484 deplist |= 1 << adp->ad_offset; 10485 if ((adp->ad_state & ATTACHED) == 0) 10486 panic("initiate_write_inodeblock_ufs1: " 10487 "Unknown state 0x%x", adp->ad_state); 10488 #endif /* INVARIANTS */ 10489 adp->ad_state &= ~ATTACHED; 10490 adp->ad_state |= UNDONE; 10491 } 10492 /* 10493 * The on-disk inode cannot claim to be any larger than the last 10494 * fragment that has been written. Otherwise, the on-disk inode 10495 * might have fragments that were not the last block in the file 10496 * which would corrupt the filesystem. 10497 */ 10498 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10499 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10500 if (adp->ad_offset >= UFS_NDADDR) 10501 break; 10502 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10503 /* keep going until hitting a rollback to a frag */ 10504 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10505 continue; 10506 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10507 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) { 10508 #ifdef INVARIANTS 10509 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10510 panic("initiate_write_inodeblock_ufs1: " 10511 "lost dep1"); 10512 #endif /* INVARIANTS */ 10513 dp->di_db[i] = 0; 10514 } 10515 for (i = 0; i < UFS_NIADDR; i++) { 10516 #ifdef INVARIANTS 10517 if (dp->di_ib[i] != 0 && 10518 (deplist & ((1 << UFS_NDADDR) << i)) == 0) 10519 panic("initiate_write_inodeblock_ufs1: " 10520 "lost dep2"); 10521 #endif /* INVARIANTS */ 10522 dp->di_ib[i] = 0; 10523 } 10524 return; 10525 } 10526 /* 10527 * If we have zero'ed out the last allocated block of the file, 10528 * roll back the size to the last currently allocated block. 10529 * We know that this last allocated block is a full-sized as 10530 * we already checked for fragments in the loop above. 10531 */ 10532 if (lastadp != NULL && 10533 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10534 for (i = lastadp->ad_offset; i >= 0; i--) 10535 if (dp->di_db[i] != 0) 10536 break; 10537 dp->di_size = (i + 1) * fs->fs_bsize; 10538 } 10539 /* 10540 * The only dependencies are for indirect blocks. 10541 * 10542 * The file size for indirect block additions is not guaranteed. 10543 * Such a guarantee would be non-trivial to achieve. The conventional 10544 * synchronous write implementation also does not make this guarantee. 10545 * Fsck should catch and fix discrepancies. Arguably, the file size 10546 * can be over-estimated without destroying integrity when the file 10547 * moves into the indirect blocks (i.e., is large). If we want to 10548 * postpone fsck, we are stuck with this argument. 10549 */ 10550 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10551 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0; 10552 } 10553 10554 /* 10555 * Version of initiate_write_inodeblock that handles UFS2 dinodes. 10556 * Note that any bug fixes made to this routine must be done in the 10557 * version found above. 10558 * 10559 * Called from within the procedure above to deal with unsatisfied 10560 * allocation dependencies in an inodeblock. The buffer must be 10561 * locked, thus, no I/O completion operations can occur while we 10562 * are manipulating its associated dependencies. 10563 */ 10564 static void 10565 initiate_write_inodeblock_ufs2( 10566 struct inodedep *inodedep, 10567 struct buf *bp) /* The inode block */ 10568 { 10569 struct allocdirect *adp, *lastadp; 10570 struct ufs2_dinode *dp; 10571 struct ufs2_dinode *sip; 10572 struct inoref *inoref; 10573 struct ufsmount *ump; 10574 struct fs *fs; 10575 ufs_lbn_t i; 10576 #ifdef INVARIANTS 10577 ufs_lbn_t prevlbn = 0; 10578 #endif 10579 int deplist __diagused; 10580 10581 if (inodedep->id_state & IOSTARTED) 10582 panic("initiate_write_inodeblock_ufs2: already started"); 10583 inodedep->id_state |= IOSTARTED; 10584 fs = inodedep->id_fs; 10585 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10586 LOCK_OWNED(ump); 10587 dp = (struct ufs2_dinode *)bp->b_data + 10588 ino_to_fsbo(fs, inodedep->id_ino); 10589 10590 /* 10591 * If we're on the unlinked list but have not yet written our 10592 * next pointer initialize it here. 10593 */ 10594 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10595 struct inodedep *inon; 10596 10597 inon = TAILQ_NEXT(inodedep, id_unlinked); 10598 dp->di_freelink = inon ? inon->id_ino : 0; 10599 ffs_update_dinode_ckhash(fs, dp); 10600 } 10601 /* 10602 * If the bitmap is not yet written, then the allocated 10603 * inode cannot be written to disk. 10604 */ 10605 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10606 if (inodedep->id_savedino2 != NULL) 10607 panic("initiate_write_inodeblock_ufs2: I/O underway"); 10608 FREE_LOCK(ump); 10609 sip = malloc(sizeof(struct ufs2_dinode), 10610 M_SAVEDINO, M_SOFTDEP_FLAGS); 10611 ACQUIRE_LOCK(ump); 10612 inodedep->id_savedino2 = sip; 10613 *inodedep->id_savedino2 = *dp; 10614 bzero((caddr_t)dp, sizeof(struct ufs2_dinode)); 10615 dp->di_gen = inodedep->id_savedino2->di_gen; 10616 dp->di_freelink = inodedep->id_savedino2->di_freelink; 10617 return; 10618 } 10619 /* 10620 * If no dependencies, then there is nothing to roll back. 10621 */ 10622 inodedep->id_savedsize = dp->di_size; 10623 inodedep->id_savedextsize = dp->di_extsize; 10624 inodedep->id_savednlink = dp->di_nlink; 10625 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10626 TAILQ_EMPTY(&inodedep->id_extupdt) && 10627 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10628 return; 10629 /* 10630 * Revert the link count to that of the first unwritten journal entry. 10631 */ 10632 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10633 if (inoref) 10634 dp->di_nlink = inoref->if_nlink; 10635 10636 /* 10637 * Set the ext data dependencies to busy. 10638 */ 10639 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10640 adp = TAILQ_NEXT(adp, ad_next)) { 10641 #ifdef INVARIANTS 10642 if (deplist != 0 && prevlbn >= adp->ad_offset) 10643 panic("initiate_write_inodeblock_ufs2: lbn order"); 10644 prevlbn = adp->ad_offset; 10645 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno) 10646 panic("initiate_write_inodeblock_ufs2: " 10647 "ext pointer #%jd mismatch %jd != %jd", 10648 (intmax_t)adp->ad_offset, 10649 (intmax_t)dp->di_extb[adp->ad_offset], 10650 (intmax_t)adp->ad_newblkno); 10651 deplist |= 1 << adp->ad_offset; 10652 if ((adp->ad_state & ATTACHED) == 0) 10653 panic("initiate_write_inodeblock_ufs2: Unknown " 10654 "state 0x%x", adp->ad_state); 10655 #endif /* INVARIANTS */ 10656 adp->ad_state &= ~ATTACHED; 10657 adp->ad_state |= UNDONE; 10658 } 10659 /* 10660 * The on-disk inode cannot claim to be any larger than the last 10661 * fragment that has been written. Otherwise, the on-disk inode 10662 * might have fragments that were not the last block in the ext 10663 * data which would corrupt the filesystem. 10664 */ 10665 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10666 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10667 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno; 10668 /* keep going until hitting a rollback to a frag */ 10669 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10670 continue; 10671 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10672 for (i = adp->ad_offset + 1; i < UFS_NXADDR; i++) { 10673 #ifdef INVARIANTS 10674 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0) 10675 panic("initiate_write_inodeblock_ufs2: " 10676 "lost dep1"); 10677 #endif /* INVARIANTS */ 10678 dp->di_extb[i] = 0; 10679 } 10680 lastadp = NULL; 10681 break; 10682 } 10683 /* 10684 * If we have zero'ed out the last allocated block of the ext 10685 * data, roll back the size to the last currently allocated block. 10686 * We know that this last allocated block is a full-sized as 10687 * we already checked for fragments in the loop above. 10688 */ 10689 if (lastadp != NULL && 10690 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10691 for (i = lastadp->ad_offset; i >= 0; i--) 10692 if (dp->di_extb[i] != 0) 10693 break; 10694 dp->di_extsize = (i + 1) * fs->fs_bsize; 10695 } 10696 /* 10697 * Set the file data dependencies to busy. 10698 */ 10699 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10700 adp = TAILQ_NEXT(adp, ad_next)) { 10701 #ifdef INVARIANTS 10702 if (deplist != 0 && prevlbn >= adp->ad_offset) 10703 panic("softdep_write_inodeblock: lbn order"); 10704 if ((adp->ad_state & ATTACHED) == 0) 10705 panic("inodedep %p and adp %p not attached", inodedep, adp); 10706 prevlbn = adp->ad_offset; 10707 if (!ffs_fsfail_cleanup(ump, 0) && 10708 adp->ad_offset < UFS_NDADDR && 10709 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10710 panic("initiate_write_inodeblock_ufs2: " 10711 "direct pointer #%jd mismatch %jd != %jd", 10712 (intmax_t)adp->ad_offset, 10713 (intmax_t)dp->di_db[adp->ad_offset], 10714 (intmax_t)adp->ad_newblkno); 10715 if (!ffs_fsfail_cleanup(ump, 0) && 10716 adp->ad_offset >= UFS_NDADDR && 10717 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno) 10718 panic("initiate_write_inodeblock_ufs2: " 10719 "indirect pointer #%jd mismatch %jd != %jd", 10720 (intmax_t)adp->ad_offset - UFS_NDADDR, 10721 (intmax_t)dp->di_ib[adp->ad_offset - UFS_NDADDR], 10722 (intmax_t)adp->ad_newblkno); 10723 deplist |= 1 << adp->ad_offset; 10724 if ((adp->ad_state & ATTACHED) == 0) 10725 panic("initiate_write_inodeblock_ufs2: Unknown " 10726 "state 0x%x", adp->ad_state); 10727 #endif /* INVARIANTS */ 10728 adp->ad_state &= ~ATTACHED; 10729 adp->ad_state |= UNDONE; 10730 } 10731 /* 10732 * The on-disk inode cannot claim to be any larger than the last 10733 * fragment that has been written. Otherwise, the on-disk inode 10734 * might have fragments that were not the last block in the file 10735 * which would corrupt the filesystem. 10736 */ 10737 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10738 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10739 if (adp->ad_offset >= UFS_NDADDR) 10740 break; 10741 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10742 /* keep going until hitting a rollback to a frag */ 10743 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10744 continue; 10745 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10746 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) { 10747 #ifdef INVARIANTS 10748 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10749 panic("initiate_write_inodeblock_ufs2: " 10750 "lost dep2"); 10751 #endif /* INVARIANTS */ 10752 dp->di_db[i] = 0; 10753 } 10754 for (i = 0; i < UFS_NIADDR; i++) { 10755 #ifdef INVARIANTS 10756 if (dp->di_ib[i] != 0 && 10757 (deplist & ((1 << UFS_NDADDR) << i)) == 0) 10758 panic("initiate_write_inodeblock_ufs2: " 10759 "lost dep3"); 10760 #endif /* INVARIANTS */ 10761 dp->di_ib[i] = 0; 10762 } 10763 ffs_update_dinode_ckhash(fs, dp); 10764 return; 10765 } 10766 /* 10767 * If we have zero'ed out the last allocated block of the file, 10768 * roll back the size to the last currently allocated block. 10769 * We know that this last allocated block is a full-sized as 10770 * we already checked for fragments in the loop above. 10771 */ 10772 if (lastadp != NULL && 10773 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10774 for (i = lastadp->ad_offset; i >= 0; i--) 10775 if (dp->di_db[i] != 0) 10776 break; 10777 dp->di_size = (i + 1) * fs->fs_bsize; 10778 } 10779 /* 10780 * The only dependencies are for indirect blocks. 10781 * 10782 * The file size for indirect block additions is not guaranteed. 10783 * Such a guarantee would be non-trivial to achieve. The conventional 10784 * synchronous write implementation also does not make this guarantee. 10785 * Fsck should catch and fix discrepancies. Arguably, the file size 10786 * can be over-estimated without destroying integrity when the file 10787 * moves into the indirect blocks (i.e., is large). If we want to 10788 * postpone fsck, we are stuck with this argument. 10789 */ 10790 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10791 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0; 10792 ffs_update_dinode_ckhash(fs, dp); 10793 } 10794 10795 /* 10796 * Cancel an indirdep as a result of truncation. Release all of the 10797 * children allocindirs and place their journal work on the appropriate 10798 * list. 10799 */ 10800 static void 10801 cancel_indirdep( 10802 struct indirdep *indirdep, 10803 struct buf *bp, 10804 struct freeblks *freeblks) 10805 { 10806 struct allocindir *aip; 10807 10808 /* 10809 * None of the indirect pointers will ever be visible, 10810 * so they can simply be tossed. GOINGAWAY ensures 10811 * that allocated pointers will be saved in the buffer 10812 * cache until they are freed. Note that they will 10813 * only be able to be found by their physical address 10814 * since the inode mapping the logical address will 10815 * be gone. The save buffer used for the safe copy 10816 * was allocated in setup_allocindir_phase2 using 10817 * the physical address so it could be used for this 10818 * purpose. Hence we swap the safe copy with the real 10819 * copy, allowing the safe copy to be freed and holding 10820 * on to the real copy for later use in indir_trunc. 10821 */ 10822 if (indirdep->ir_state & GOINGAWAY) 10823 panic("cancel_indirdep: already gone"); 10824 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 10825 indirdep->ir_state |= DEPCOMPLETE; 10826 LIST_REMOVE(indirdep, ir_next); 10827 } 10828 indirdep->ir_state |= GOINGAWAY; 10829 /* 10830 * Pass in bp for blocks still have journal writes 10831 * pending so we can cancel them on their own. 10832 */ 10833 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != NULL) 10834 cancel_allocindir(aip, bp, freeblks, 0); 10835 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) 10836 cancel_allocindir(aip, NULL, freeblks, 0); 10837 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) 10838 cancel_allocindir(aip, NULL, freeblks, 0); 10839 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) 10840 cancel_allocindir(aip, NULL, freeblks, 0); 10841 /* 10842 * If there are pending partial truncations we need to keep the 10843 * old block copy around until they complete. This is because 10844 * the current b_data is not a perfect superset of the available 10845 * blocks. 10846 */ 10847 if (TAILQ_EMPTY(&indirdep->ir_trunc)) 10848 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount); 10849 else 10850 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10851 WORKLIST_REMOVE(&indirdep->ir_list); 10852 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list); 10853 indirdep->ir_bp = NULL; 10854 indirdep->ir_freeblks = freeblks; 10855 } 10856 10857 /* 10858 * Free an indirdep once it no longer has new pointers to track. 10859 */ 10860 static void 10861 free_indirdep(struct indirdep *indirdep) 10862 { 10863 10864 KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc), 10865 ("free_indirdep: Indir trunc list not empty.")); 10866 KASSERT(LIST_EMPTY(&indirdep->ir_completehd), 10867 ("free_indirdep: Complete head not empty.")); 10868 KASSERT(LIST_EMPTY(&indirdep->ir_writehd), 10869 ("free_indirdep: write head not empty.")); 10870 KASSERT(LIST_EMPTY(&indirdep->ir_donehd), 10871 ("free_indirdep: done head not empty.")); 10872 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd), 10873 ("free_indirdep: deplist head not empty.")); 10874 KASSERT((indirdep->ir_state & DEPCOMPLETE), 10875 ("free_indirdep: %p still on newblk list.", indirdep)); 10876 KASSERT(indirdep->ir_saveddata == NULL, 10877 ("free_indirdep: %p still has saved data.", indirdep)); 10878 KASSERT(indirdep->ir_savebp == NULL, 10879 ("free_indirdep: %p still has savebp buffer.", indirdep)); 10880 if (indirdep->ir_state & ONWORKLIST) 10881 WORKLIST_REMOVE(&indirdep->ir_list); 10882 WORKITEM_FREE(indirdep, D_INDIRDEP); 10883 } 10884 10885 /* 10886 * Called before a write to an indirdep. This routine is responsible for 10887 * rolling back pointers to a safe state which includes only those 10888 * allocindirs which have been completed. 10889 */ 10890 static void 10891 initiate_write_indirdep(struct indirdep *indirdep, struct buf *bp) 10892 { 10893 struct ufsmount *ump; 10894 10895 indirdep->ir_state |= IOSTARTED; 10896 if (indirdep->ir_state & GOINGAWAY) 10897 panic("disk_io_initiation: indirdep gone"); 10898 /* 10899 * If there are no remaining dependencies, this will be writing 10900 * the real pointers. 10901 */ 10902 if (LIST_EMPTY(&indirdep->ir_deplisthd) && 10903 TAILQ_EMPTY(&indirdep->ir_trunc)) 10904 return; 10905 /* 10906 * Replace up-to-date version with safe version. 10907 */ 10908 if (indirdep->ir_saveddata == NULL) { 10909 ump = VFSTOUFS(indirdep->ir_list.wk_mp); 10910 LOCK_OWNED(ump); 10911 FREE_LOCK(ump); 10912 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 10913 M_SOFTDEP_FLAGS); 10914 ACQUIRE_LOCK(ump); 10915 } 10916 indirdep->ir_state &= ~ATTACHED; 10917 indirdep->ir_state |= UNDONE; 10918 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10919 bcopy(indirdep->ir_savebp->b_data, bp->b_data, 10920 bp->b_bcount); 10921 } 10922 10923 /* 10924 * Called when an inode has been cleared in a cg bitmap. This finally 10925 * eliminates any canceled jaddrefs 10926 */ 10927 void 10928 softdep_setup_inofree(struct mount *mp, 10929 struct buf *bp, 10930 ino_t ino, 10931 struct workhead *wkhd, 10932 bool doingrecovery) 10933 { 10934 struct worklist *wk, *wkn; 10935 struct ufsmount *ump; 10936 #ifdef INVARIANTS 10937 struct inodedep *inodedep; 10938 #endif 10939 10940 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 10941 ("softdep_setup_inofree called on non-softdep filesystem")); 10942 ump = VFSTOUFS(mp); 10943 ACQUIRE_LOCK(ump); 10944 KASSERT(doingrecovery || ffs_fsfail_cleanup(ump, 0) || 10945 isclr(cg_inosused((struct cg *)bp->b_data), 10946 ino % ump->um_fs->fs_ipg), 10947 ("softdep_setup_inofree: inode %ju not freed.", (uintmax_t)ino)); 10948 KASSERT(inodedep_lookup(mp, ino, 0, &inodedep) == 0, 10949 ("softdep_setup_inofree: ino %ju has existing inodedep %p", 10950 (uintmax_t)ino, inodedep)); 10951 if (wkhd) { 10952 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) { 10953 if (wk->wk_type != D_JADDREF) 10954 continue; 10955 WORKLIST_REMOVE(wk); 10956 /* 10957 * We can free immediately even if the jaddref 10958 * isn't attached in a background write as now 10959 * the bitmaps are reconciled. 10960 */ 10961 wk->wk_state |= COMPLETE | ATTACHED; 10962 free_jaddref(WK_JADDREF(wk)); 10963 } 10964 jwork_move(&bp->b_dep, wkhd); 10965 } 10966 FREE_LOCK(ump); 10967 } 10968 10969 /* 10970 * Called via ffs_blkfree() after a set of frags has been cleared from a cg 10971 * map. Any dependencies waiting for the write to clear are added to the 10972 * buf's list and any jnewblks that are being canceled are discarded 10973 * immediately. 10974 */ 10975 void 10976 softdep_setup_blkfree( 10977 struct mount *mp, 10978 struct buf *bp, 10979 ufs2_daddr_t blkno, 10980 int frags, 10981 struct workhead *wkhd, 10982 bool doingrecovery) 10983 { 10984 struct bmsafemap *bmsafemap; 10985 struct jnewblk *jnewblk; 10986 struct ufsmount *ump; 10987 struct worklist *wk; 10988 struct fs *fs; 10989 #ifdef INVARIANTS 10990 uint8_t *blksfree; 10991 struct cg *cgp; 10992 ufs2_daddr_t jstart; 10993 ufs2_daddr_t jend; 10994 ufs2_daddr_t end; 10995 long bno; 10996 int i; 10997 #endif 10998 10999 CTR3(KTR_SUJ, 11000 "softdep_setup_blkfree: blkno %jd frags %d wk head %p", 11001 blkno, frags, wkhd); 11002 11003 ump = VFSTOUFS(mp); 11004 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 11005 ("softdep_setup_blkfree called on non-softdep filesystem")); 11006 ACQUIRE_LOCK(ump); 11007 /* Lookup the bmsafemap so we track when it is dirty. */ 11008 fs = ump->um_fs; 11009 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 11010 /* 11011 * Detach any jnewblks which have been canceled. They must linger 11012 * until the bitmap is cleared again by ffs_blkfree() to prevent 11013 * an unjournaled allocation from hitting the disk. 11014 */ 11015 if (wkhd) { 11016 while ((wk = LIST_FIRST(wkhd)) != NULL) { 11017 CTR2(KTR_SUJ, 11018 "softdep_setup_blkfree: blkno %jd wk type %d", 11019 blkno, wk->wk_type); 11020 WORKLIST_REMOVE(wk); 11021 if (wk->wk_type != D_JNEWBLK) { 11022 WORKLIST_INSERT(&bmsafemap->sm_freehd, wk); 11023 continue; 11024 } 11025 jnewblk = WK_JNEWBLK(wk); 11026 KASSERT(jnewblk->jn_state & GOINGAWAY, 11027 ("softdep_setup_blkfree: jnewblk not canceled.")); 11028 #ifdef INVARIANTS 11029 if (!doingrecovery && !ffs_fsfail_cleanup(ump, 0)) { 11030 /* 11031 * Assert that this block is free in the 11032 * bitmap before we discard the jnewblk. 11033 */ 11034 cgp = (struct cg *)bp->b_data; 11035 blksfree = cg_blksfree(cgp); 11036 bno = dtogd(fs, jnewblk->jn_blkno); 11037 for (i = jnewblk->jn_oldfrags; 11038 i < jnewblk->jn_frags; i++) { 11039 if (isset(blksfree, bno + i)) 11040 continue; 11041 panic("softdep_setup_blkfree: block " 11042 "%ju not freed.", 11043 (uintmax_t)jnewblk->jn_blkno); 11044 } 11045 } 11046 #endif 11047 /* 11048 * Even if it's not attached we can free immediately 11049 * as the new bitmap is correct. 11050 */ 11051 wk->wk_state |= COMPLETE | ATTACHED; 11052 free_jnewblk(jnewblk); 11053 } 11054 } 11055 11056 #ifdef INVARIANTS 11057 /* 11058 * Assert that we are not freeing a block which has an outstanding 11059 * allocation dependency. 11060 */ 11061 fs = VFSTOUFS(mp)->um_fs; 11062 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 11063 end = blkno + frags; 11064 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 11065 /* 11066 * Don't match against blocks that will be freed when the 11067 * background write is done. 11068 */ 11069 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) == 11070 (COMPLETE | DEPCOMPLETE)) 11071 continue; 11072 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags; 11073 jend = jnewblk->jn_blkno + jnewblk->jn_frags; 11074 if ((blkno >= jstart && blkno < jend) || 11075 (end > jstart && end <= jend)) { 11076 printf("state 0x%X %jd - %d %d dep %p\n", 11077 jnewblk->jn_state, jnewblk->jn_blkno, 11078 jnewblk->jn_oldfrags, jnewblk->jn_frags, 11079 jnewblk->jn_dep); 11080 panic("softdep_setup_blkfree: " 11081 "%jd-%jd(%d) overlaps with %jd-%jd", 11082 blkno, end, frags, jstart, jend); 11083 } 11084 } 11085 #endif 11086 FREE_LOCK(ump); 11087 } 11088 11089 /* 11090 * Revert a block allocation when the journal record that describes it 11091 * is not yet written. 11092 */ 11093 static int 11094 jnewblk_rollback( 11095 struct jnewblk *jnewblk, 11096 struct fs *fs, 11097 struct cg *cgp, 11098 uint8_t *blksfree) 11099 { 11100 ufs1_daddr_t fragno; 11101 long cgbno, bbase; 11102 int frags, blk; 11103 int i; 11104 11105 frags = 0; 11106 cgbno = dtogd(fs, jnewblk->jn_blkno); 11107 /* 11108 * We have to test which frags need to be rolled back. We may 11109 * be operating on a stale copy when doing background writes. 11110 */ 11111 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) 11112 if (isclr(blksfree, cgbno + i)) 11113 frags++; 11114 if (frags == 0) 11115 return (0); 11116 /* 11117 * This is mostly ffs_blkfree() sans some validation and 11118 * superblock updates. 11119 */ 11120 if (frags == fs->fs_frag) { 11121 fragno = fragstoblks(fs, cgbno); 11122 ffs_setblock(fs, blksfree, fragno); 11123 ffs_clusteracct(fs, cgp, fragno, 1); 11124 cgp->cg_cs.cs_nbfree++; 11125 } else { 11126 cgbno += jnewblk->jn_oldfrags; 11127 bbase = cgbno - fragnum(fs, cgbno); 11128 /* Decrement the old frags. */ 11129 blk = blkmap(fs, blksfree, bbase); 11130 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 11131 /* Deallocate the fragment */ 11132 for (i = 0; i < frags; i++) 11133 setbit(blksfree, cgbno + i); 11134 cgp->cg_cs.cs_nffree += frags; 11135 /* Add back in counts associated with the new frags */ 11136 blk = blkmap(fs, blksfree, bbase); 11137 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 11138 /* If a complete block has been reassembled, account for it. */ 11139 fragno = fragstoblks(fs, bbase); 11140 if (ffs_isblock(fs, blksfree, fragno)) { 11141 cgp->cg_cs.cs_nffree -= fs->fs_frag; 11142 ffs_clusteracct(fs, cgp, fragno, 1); 11143 cgp->cg_cs.cs_nbfree++; 11144 } 11145 } 11146 stat_jnewblk++; 11147 jnewblk->jn_state &= ~ATTACHED; 11148 jnewblk->jn_state |= UNDONE; 11149 11150 return (frags); 11151 } 11152 11153 static void 11154 initiate_write_bmsafemap( 11155 struct bmsafemap *bmsafemap, 11156 struct buf *bp) /* The cg block. */ 11157 { 11158 struct jaddref *jaddref; 11159 struct jnewblk *jnewblk; 11160 uint8_t *inosused; 11161 uint8_t *blksfree; 11162 struct cg *cgp; 11163 struct fs *fs; 11164 ino_t ino; 11165 11166 /* 11167 * If this is a background write, we did this at the time that 11168 * the copy was made, so do not need to do it again. 11169 */ 11170 if (bmsafemap->sm_state & IOSTARTED) 11171 return; 11172 bmsafemap->sm_state |= IOSTARTED; 11173 /* 11174 * Clear any inode allocations which are pending journal writes. 11175 */ 11176 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) { 11177 cgp = (struct cg *)bp->b_data; 11178 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11179 inosused = cg_inosused(cgp); 11180 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) { 11181 ino = jaddref->ja_ino % fs->fs_ipg; 11182 if (isset(inosused, ino)) { 11183 if ((jaddref->ja_mode & IFMT) == IFDIR) 11184 cgp->cg_cs.cs_ndir--; 11185 cgp->cg_cs.cs_nifree++; 11186 clrbit(inosused, ino); 11187 jaddref->ja_state &= ~ATTACHED; 11188 jaddref->ja_state |= UNDONE; 11189 stat_jaddref++; 11190 } else 11191 panic("initiate_write_bmsafemap: inode %ju " 11192 "marked free", (uintmax_t)jaddref->ja_ino); 11193 } 11194 } 11195 /* 11196 * Clear any block allocations which are pending journal writes. 11197 */ 11198 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 11199 cgp = (struct cg *)bp->b_data; 11200 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11201 blksfree = cg_blksfree(cgp); 11202 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 11203 if (jnewblk_rollback(jnewblk, fs, cgp, blksfree)) 11204 continue; 11205 panic("initiate_write_bmsafemap: block %jd " 11206 "marked free", jnewblk->jn_blkno); 11207 } 11208 } 11209 /* 11210 * Move allocation lists to the written lists so they can be 11211 * cleared once the block write is complete. 11212 */ 11213 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr, 11214 inodedep, id_deps); 11215 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 11216 newblk, nb_deps); 11217 LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist, 11218 wk_list); 11219 } 11220 11221 void 11222 softdep_handle_error(struct buf *bp) 11223 { 11224 struct ufsmount *ump; 11225 11226 ump = softdep_bp_to_mp(bp); 11227 if (ump == NULL) 11228 return; 11229 11230 if (ffs_fsfail_cleanup(ump, bp->b_error)) { 11231 /* 11232 * No future writes will succeed, so the on-disk image is safe. 11233 * Pretend that this write succeeded so that the softdep state 11234 * will be cleaned up naturally. 11235 */ 11236 bp->b_ioflags &= ~BIO_ERROR; 11237 bp->b_error = 0; 11238 } 11239 } 11240 11241 /* 11242 * This routine is called during the completion interrupt 11243 * service routine for a disk write (from the procedure called 11244 * by the device driver to inform the filesystem caches of 11245 * a request completion). It should be called early in this 11246 * procedure, before the block is made available to other 11247 * processes or other routines are called. 11248 * 11249 */ 11250 static void 11251 softdep_disk_write_complete( 11252 struct buf *bp) /* describes the completed disk write */ 11253 { 11254 struct worklist *wk; 11255 struct worklist *owk; 11256 struct ufsmount *ump; 11257 struct workhead reattach; 11258 struct freeblks *freeblks; 11259 struct buf *sbp; 11260 11261 ump = softdep_bp_to_mp(bp); 11262 KASSERT(LIST_EMPTY(&bp->b_dep) || ump != NULL, 11263 ("softdep_disk_write_complete: softdep_bp_to_mp returned NULL " 11264 "with outstanding dependencies for buffer %p", bp)); 11265 if (ump == NULL) 11266 return; 11267 if ((bp->b_ioflags & BIO_ERROR) != 0) 11268 softdep_handle_error(bp); 11269 /* 11270 * If an error occurred while doing the write, then the data 11271 * has not hit the disk and the dependencies cannot be processed. 11272 * But we do have to go through and roll forward any dependencies 11273 * that were rolled back before the disk write. 11274 */ 11275 sbp = NULL; 11276 ACQUIRE_LOCK(ump); 11277 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) { 11278 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 11279 switch (wk->wk_type) { 11280 case D_PAGEDEP: 11281 handle_written_filepage(WK_PAGEDEP(wk), bp, 0); 11282 continue; 11283 11284 case D_INODEDEP: 11285 handle_written_inodeblock(WK_INODEDEP(wk), 11286 bp, 0); 11287 continue; 11288 11289 case D_BMSAFEMAP: 11290 handle_written_bmsafemap(WK_BMSAFEMAP(wk), 11291 bp, 0); 11292 continue; 11293 11294 case D_INDIRDEP: 11295 handle_written_indirdep(WK_INDIRDEP(wk), 11296 bp, &sbp, 0); 11297 continue; 11298 default: 11299 /* nothing to roll forward */ 11300 continue; 11301 } 11302 } 11303 FREE_LOCK(ump); 11304 if (sbp) 11305 brelse(sbp); 11306 return; 11307 } 11308 LIST_INIT(&reattach); 11309 11310 /* 11311 * Ump SU lock must not be released anywhere in this code segment. 11312 */ 11313 owk = NULL; 11314 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) { 11315 WORKLIST_REMOVE(wk); 11316 atomic_add_long(&dep_write[wk->wk_type], 1); 11317 if (wk == owk) 11318 panic("duplicate worklist: %p\n", wk); 11319 owk = wk; 11320 switch (wk->wk_type) { 11321 case D_PAGEDEP: 11322 if (handle_written_filepage(WK_PAGEDEP(wk), bp, 11323 WRITESUCCEEDED)) 11324 WORKLIST_INSERT(&reattach, wk); 11325 continue; 11326 11327 case D_INODEDEP: 11328 if (handle_written_inodeblock(WK_INODEDEP(wk), bp, 11329 WRITESUCCEEDED)) 11330 WORKLIST_INSERT(&reattach, wk); 11331 continue; 11332 11333 case D_BMSAFEMAP: 11334 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp, 11335 WRITESUCCEEDED)) 11336 WORKLIST_INSERT(&reattach, wk); 11337 continue; 11338 11339 case D_MKDIR: 11340 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 11341 continue; 11342 11343 case D_ALLOCDIRECT: 11344 wk->wk_state |= COMPLETE; 11345 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL); 11346 continue; 11347 11348 case D_ALLOCINDIR: 11349 wk->wk_state |= COMPLETE; 11350 handle_allocindir_partdone(WK_ALLOCINDIR(wk)); 11351 continue; 11352 11353 case D_INDIRDEP: 11354 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp, 11355 WRITESUCCEEDED)) 11356 WORKLIST_INSERT(&reattach, wk); 11357 continue; 11358 11359 case D_FREEBLKS: 11360 wk->wk_state |= COMPLETE; 11361 freeblks = WK_FREEBLKS(wk); 11362 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE && 11363 LIST_EMPTY(&freeblks->fb_jblkdephd)) 11364 add_to_worklist(wk, WK_NODELAY); 11365 continue; 11366 11367 case D_FREEWORK: 11368 handle_written_freework(WK_FREEWORK(wk)); 11369 break; 11370 11371 case D_JSEGDEP: 11372 free_jsegdep(WK_JSEGDEP(wk)); 11373 continue; 11374 11375 case D_JSEG: 11376 handle_written_jseg(WK_JSEG(wk), bp); 11377 continue; 11378 11379 case D_SBDEP: 11380 if (handle_written_sbdep(WK_SBDEP(wk), bp)) 11381 WORKLIST_INSERT(&reattach, wk); 11382 continue; 11383 11384 case D_FREEDEP: 11385 free_freedep(WK_FREEDEP(wk)); 11386 continue; 11387 11388 default: 11389 panic("handle_disk_write_complete: Unknown type %s", 11390 TYPENAME(wk->wk_type)); 11391 /* NOTREACHED */ 11392 } 11393 } 11394 /* 11395 * Reattach any requests that must be redone. 11396 */ 11397 while ((wk = LIST_FIRST(&reattach)) != NULL) { 11398 WORKLIST_REMOVE(wk); 11399 WORKLIST_INSERT(&bp->b_dep, wk); 11400 } 11401 FREE_LOCK(ump); 11402 if (sbp) 11403 brelse(sbp); 11404 } 11405 11406 /* 11407 * Called from within softdep_disk_write_complete above. 11408 */ 11409 static void 11410 handle_allocdirect_partdone( 11411 struct allocdirect *adp, /* the completed allocdirect */ 11412 struct workhead *wkhd) /* Work to do when inode is writtne. */ 11413 { 11414 struct allocdirectlst *listhead; 11415 struct allocdirect *listadp; 11416 struct inodedep *inodedep; 11417 long bsize; 11418 11419 LOCK_OWNED(VFSTOUFS(adp->ad_block.nb_list.wk_mp)); 11420 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11421 return; 11422 /* 11423 * The on-disk inode cannot claim to be any larger than the last 11424 * fragment that has been written. Otherwise, the on-disk inode 11425 * might have fragments that were not the last block in the file 11426 * which would corrupt the filesystem. Thus, we cannot free any 11427 * allocdirects after one whose ad_oldblkno claims a fragment as 11428 * these blocks must be rolled back to zero before writing the inode. 11429 * We check the currently active set of allocdirects in id_inoupdt 11430 * or id_extupdt as appropriate. 11431 */ 11432 inodedep = adp->ad_inodedep; 11433 bsize = inodedep->id_fs->fs_bsize; 11434 if (adp->ad_state & EXTDATA) 11435 listhead = &inodedep->id_extupdt; 11436 else 11437 listhead = &inodedep->id_inoupdt; 11438 TAILQ_FOREACH(listadp, listhead, ad_next) { 11439 /* found our block */ 11440 if (listadp == adp) 11441 break; 11442 /* continue if ad_oldlbn is not a fragment */ 11443 if (listadp->ad_oldsize == 0 || 11444 listadp->ad_oldsize == bsize) 11445 continue; 11446 /* hit a fragment */ 11447 return; 11448 } 11449 /* 11450 * If we have reached the end of the current list without 11451 * finding the just finished dependency, then it must be 11452 * on the future dependency list. Future dependencies cannot 11453 * be freed until they are moved to the current list. 11454 */ 11455 if (listadp == NULL) { 11456 #ifdef INVARIANTS 11457 if (adp->ad_state & EXTDATA) 11458 listhead = &inodedep->id_newextupdt; 11459 else 11460 listhead = &inodedep->id_newinoupdt; 11461 TAILQ_FOREACH(listadp, listhead, ad_next) 11462 /* found our block */ 11463 if (listadp == adp) 11464 break; 11465 if (listadp == NULL) 11466 panic("handle_allocdirect_partdone: lost dep"); 11467 #endif /* INVARIANTS */ 11468 return; 11469 } 11470 /* 11471 * If we have found the just finished dependency, then queue 11472 * it along with anything that follows it that is complete. 11473 * Since the pointer has not yet been written in the inode 11474 * as the dependency prevents it, place the allocdirect on the 11475 * bufwait list where it will be freed once the pointer is 11476 * valid. 11477 */ 11478 if (wkhd == NULL) 11479 wkhd = &inodedep->id_bufwait; 11480 for (; adp; adp = listadp) { 11481 listadp = TAILQ_NEXT(adp, ad_next); 11482 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11483 return; 11484 TAILQ_REMOVE(listhead, adp, ad_next); 11485 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list); 11486 } 11487 } 11488 11489 /* 11490 * Called from within softdep_disk_write_complete above. This routine 11491 * completes successfully written allocindirs. 11492 */ 11493 static void 11494 handle_allocindir_partdone( 11495 struct allocindir *aip) /* the completed allocindir */ 11496 { 11497 struct indirdep *indirdep; 11498 11499 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE) 11500 return; 11501 indirdep = aip->ai_indirdep; 11502 LIST_REMOVE(aip, ai_next); 11503 /* 11504 * Don't set a pointer while the buffer is undergoing IO or while 11505 * we have active truncations. 11506 */ 11507 if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) { 11508 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next); 11509 return; 11510 } 11511 if (indirdep->ir_state & UFS1FMT) 11512 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11513 aip->ai_newblkno; 11514 else 11515 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11516 aip->ai_newblkno; 11517 /* 11518 * Await the pointer write before freeing the allocindir. 11519 */ 11520 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next); 11521 } 11522 11523 /* 11524 * Release segments held on a jwork list. 11525 */ 11526 static void 11527 handle_jwork(struct workhead *wkhd) 11528 { 11529 struct worklist *wk; 11530 11531 while ((wk = LIST_FIRST(wkhd)) != NULL) { 11532 WORKLIST_REMOVE(wk); 11533 switch (wk->wk_type) { 11534 case D_JSEGDEP: 11535 free_jsegdep(WK_JSEGDEP(wk)); 11536 continue; 11537 case D_FREEDEP: 11538 free_freedep(WK_FREEDEP(wk)); 11539 continue; 11540 case D_FREEFRAG: 11541 rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep)); 11542 WORKITEM_FREE(wk, D_FREEFRAG); 11543 continue; 11544 case D_FREEWORK: 11545 handle_written_freework(WK_FREEWORK(wk)); 11546 continue; 11547 default: 11548 panic("handle_jwork: Unknown type %s\n", 11549 TYPENAME(wk->wk_type)); 11550 } 11551 } 11552 } 11553 11554 /* 11555 * Handle the bufwait list on an inode when it is safe to release items 11556 * held there. This normally happens after an inode block is written but 11557 * may be delayed and handled later if there are pending journal items that 11558 * are not yet safe to be released. 11559 */ 11560 static struct freefile * 11561 handle_bufwait( 11562 struct inodedep *inodedep, 11563 struct workhead *refhd) 11564 { 11565 struct jaddref *jaddref; 11566 struct freefile *freefile; 11567 struct worklist *wk; 11568 11569 freefile = NULL; 11570 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) { 11571 WORKLIST_REMOVE(wk); 11572 switch (wk->wk_type) { 11573 case D_FREEFILE: 11574 /* 11575 * We defer adding freefile to the worklist 11576 * until all other additions have been made to 11577 * ensure that it will be done after all the 11578 * old blocks have been freed. 11579 */ 11580 if (freefile != NULL) 11581 panic("handle_bufwait: freefile"); 11582 freefile = WK_FREEFILE(wk); 11583 continue; 11584 11585 case D_MKDIR: 11586 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT); 11587 continue; 11588 11589 case D_DIRADD: 11590 diradd_inode_written(WK_DIRADD(wk), inodedep); 11591 continue; 11592 11593 case D_FREEFRAG: 11594 wk->wk_state |= COMPLETE; 11595 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE) 11596 add_to_worklist(wk, 0); 11597 continue; 11598 11599 case D_DIRREM: 11600 wk->wk_state |= COMPLETE; 11601 add_to_worklist(wk, 0); 11602 continue; 11603 11604 case D_ALLOCDIRECT: 11605 case D_ALLOCINDIR: 11606 free_newblk(WK_NEWBLK(wk)); 11607 continue; 11608 11609 case D_JNEWBLK: 11610 wk->wk_state |= COMPLETE; 11611 free_jnewblk(WK_JNEWBLK(wk)); 11612 continue; 11613 11614 /* 11615 * Save freed journal segments and add references on 11616 * the supplied list which will delay their release 11617 * until the cg bitmap is cleared on disk. 11618 */ 11619 case D_JSEGDEP: 11620 if (refhd == NULL) 11621 free_jsegdep(WK_JSEGDEP(wk)); 11622 else 11623 WORKLIST_INSERT(refhd, wk); 11624 continue; 11625 11626 case D_JADDREF: 11627 jaddref = WK_JADDREF(wk); 11628 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 11629 if_deps); 11630 /* 11631 * Transfer any jaddrefs to the list to be freed with 11632 * the bitmap if we're handling a removed file. 11633 */ 11634 if (refhd == NULL) { 11635 wk->wk_state |= COMPLETE; 11636 free_jaddref(jaddref); 11637 } else 11638 WORKLIST_INSERT(refhd, wk); 11639 continue; 11640 11641 default: 11642 panic("handle_bufwait: Unknown type %p(%s)", 11643 wk, TYPENAME(wk->wk_type)); 11644 /* NOTREACHED */ 11645 } 11646 } 11647 return (freefile); 11648 } 11649 /* 11650 * Called from within softdep_disk_write_complete above to restore 11651 * in-memory inode block contents to their most up-to-date state. Note 11652 * that this routine is always called from interrupt level with further 11653 * interrupts from this device blocked. 11654 * 11655 * If the write did not succeed, we will do all the roll-forward 11656 * operations, but we will not take the actions that will allow its 11657 * dependencies to be processed. 11658 */ 11659 static int 11660 handle_written_inodeblock( 11661 struct inodedep *inodedep, 11662 struct buf *bp, /* buffer containing the inode block */ 11663 int flags) 11664 { 11665 struct freefile *freefile; 11666 struct allocdirect *adp, *nextadp; 11667 struct ufs1_dinode *dp1 = NULL; 11668 struct ufs2_dinode *dp2 = NULL; 11669 struct workhead wkhd; 11670 int hadchanges, fstype; 11671 ino_t freelink; 11672 11673 LIST_INIT(&wkhd); 11674 hadchanges = 0; 11675 freefile = NULL; 11676 if ((inodedep->id_state & IOSTARTED) == 0) 11677 panic("handle_written_inodeblock: not started"); 11678 inodedep->id_state &= ~IOSTARTED; 11679 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) { 11680 fstype = UFS1; 11681 dp1 = (struct ufs1_dinode *)bp->b_data + 11682 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11683 freelink = dp1->di_freelink; 11684 } else { 11685 fstype = UFS2; 11686 dp2 = (struct ufs2_dinode *)bp->b_data + 11687 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11688 freelink = dp2->di_freelink; 11689 } 11690 /* 11691 * Leave this inodeblock dirty until it's in the list. 11692 */ 11693 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED && 11694 (flags & WRITESUCCEEDED)) { 11695 struct inodedep *inon; 11696 11697 inon = TAILQ_NEXT(inodedep, id_unlinked); 11698 if ((inon == NULL && freelink == 0) || 11699 (inon && inon->id_ino == freelink)) { 11700 if (inon) 11701 inon->id_state |= UNLINKPREV; 11702 inodedep->id_state |= UNLINKNEXT; 11703 } 11704 hadchanges = 1; 11705 } 11706 /* 11707 * If we had to rollback the inode allocation because of 11708 * bitmaps being incomplete, then simply restore it. 11709 * Keep the block dirty so that it will not be reclaimed until 11710 * all associated dependencies have been cleared and the 11711 * corresponding updates written to disk. 11712 */ 11713 if (inodedep->id_savedino1 != NULL) { 11714 hadchanges = 1; 11715 if (fstype == UFS1) 11716 *dp1 = *inodedep->id_savedino1; 11717 else 11718 *dp2 = *inodedep->id_savedino2; 11719 free(inodedep->id_savedino1, M_SAVEDINO); 11720 inodedep->id_savedino1 = NULL; 11721 if ((bp->b_flags & B_DELWRI) == 0) 11722 stat_inode_bitmap++; 11723 bdirty(bp); 11724 /* 11725 * If the inode is clear here and GOINGAWAY it will never 11726 * be written. Process the bufwait and clear any pending 11727 * work which may include the freefile. 11728 */ 11729 if (inodedep->id_state & GOINGAWAY) 11730 goto bufwait; 11731 return (1); 11732 } 11733 if (flags & WRITESUCCEEDED) 11734 inodedep->id_state |= COMPLETE; 11735 /* 11736 * Roll forward anything that had to be rolled back before 11737 * the inode could be updated. 11738 */ 11739 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) { 11740 nextadp = TAILQ_NEXT(adp, ad_next); 11741 if (adp->ad_state & ATTACHED) 11742 panic("handle_written_inodeblock: new entry"); 11743 if (fstype == UFS1) { 11744 if (adp->ad_offset < UFS_NDADDR) { 11745 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11746 panic("%s %s #%jd mismatch %d != %jd", 11747 "handle_written_inodeblock:", 11748 "direct pointer", 11749 (intmax_t)adp->ad_offset, 11750 dp1->di_db[adp->ad_offset], 11751 (intmax_t)adp->ad_oldblkno); 11752 dp1->di_db[adp->ad_offset] = adp->ad_newblkno; 11753 } else { 11754 if (dp1->di_ib[adp->ad_offset - UFS_NDADDR] != 11755 0) 11756 panic("%s: %s #%jd allocated as %d", 11757 "handle_written_inodeblock", 11758 "indirect pointer", 11759 (intmax_t)adp->ad_offset - 11760 UFS_NDADDR, 11761 dp1->di_ib[adp->ad_offset - 11762 UFS_NDADDR]); 11763 dp1->di_ib[adp->ad_offset - UFS_NDADDR] = 11764 adp->ad_newblkno; 11765 } 11766 } else { 11767 if (adp->ad_offset < UFS_NDADDR) { 11768 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11769 panic("%s: %s #%jd %s %jd != %jd", 11770 "handle_written_inodeblock", 11771 "direct pointer", 11772 (intmax_t)adp->ad_offset, "mismatch", 11773 (intmax_t)dp2->di_db[adp->ad_offset], 11774 (intmax_t)adp->ad_oldblkno); 11775 dp2->di_db[adp->ad_offset] = adp->ad_newblkno; 11776 } else { 11777 if (dp2->di_ib[adp->ad_offset - UFS_NDADDR] != 11778 0) 11779 panic("%s: %s #%jd allocated as %jd", 11780 "handle_written_inodeblock", 11781 "indirect pointer", 11782 (intmax_t)adp->ad_offset - 11783 UFS_NDADDR, 11784 (intmax_t) 11785 dp2->di_ib[adp->ad_offset - 11786 UFS_NDADDR]); 11787 dp2->di_ib[adp->ad_offset - UFS_NDADDR] = 11788 adp->ad_newblkno; 11789 } 11790 } 11791 adp->ad_state &= ~UNDONE; 11792 adp->ad_state |= ATTACHED; 11793 hadchanges = 1; 11794 } 11795 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) { 11796 nextadp = TAILQ_NEXT(adp, ad_next); 11797 if (adp->ad_state & ATTACHED) 11798 panic("handle_written_inodeblock: new entry"); 11799 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno) 11800 panic("%s: direct pointers #%jd %s %jd != %jd", 11801 "handle_written_inodeblock", 11802 (intmax_t)adp->ad_offset, "mismatch", 11803 (intmax_t)dp2->di_extb[adp->ad_offset], 11804 (intmax_t)adp->ad_oldblkno); 11805 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno; 11806 adp->ad_state &= ~UNDONE; 11807 adp->ad_state |= ATTACHED; 11808 hadchanges = 1; 11809 } 11810 if (hadchanges && (bp->b_flags & B_DELWRI) == 0) 11811 stat_direct_blk_ptrs++; 11812 /* 11813 * Reset the file size to its most up-to-date value. 11814 */ 11815 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1) 11816 panic("handle_written_inodeblock: bad size"); 11817 if (inodedep->id_savednlink > UFS_LINK_MAX) 11818 panic("handle_written_inodeblock: Invalid link count " 11819 "%jd for inodedep %p", (uintmax_t)inodedep->id_savednlink, 11820 inodedep); 11821 if (fstype == UFS1) { 11822 if (dp1->di_nlink != inodedep->id_savednlink) { 11823 dp1->di_nlink = inodedep->id_savednlink; 11824 hadchanges = 1; 11825 } 11826 if (dp1->di_size != inodedep->id_savedsize) { 11827 dp1->di_size = inodedep->id_savedsize; 11828 hadchanges = 1; 11829 } 11830 } else { 11831 if (dp2->di_nlink != inodedep->id_savednlink) { 11832 dp2->di_nlink = inodedep->id_savednlink; 11833 hadchanges = 1; 11834 } 11835 if (dp2->di_size != inodedep->id_savedsize) { 11836 dp2->di_size = inodedep->id_savedsize; 11837 hadchanges = 1; 11838 } 11839 if (dp2->di_extsize != inodedep->id_savedextsize) { 11840 dp2->di_extsize = inodedep->id_savedextsize; 11841 hadchanges = 1; 11842 } 11843 } 11844 inodedep->id_savedsize = -1; 11845 inodedep->id_savedextsize = -1; 11846 inodedep->id_savednlink = -1; 11847 /* 11848 * If there were any rollbacks in the inode block, then it must be 11849 * marked dirty so that its will eventually get written back in 11850 * its correct form. 11851 */ 11852 if (hadchanges) { 11853 if (fstype == UFS2) 11854 ffs_update_dinode_ckhash(inodedep->id_fs, dp2); 11855 bdirty(bp); 11856 } 11857 bufwait: 11858 /* 11859 * If the write did not succeed, we have done all the roll-forward 11860 * operations, but we cannot take the actions that will allow its 11861 * dependencies to be processed. 11862 */ 11863 if ((flags & WRITESUCCEEDED) == 0) 11864 return (hadchanges); 11865 /* 11866 * Process any allocdirects that completed during the update. 11867 */ 11868 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 11869 handle_allocdirect_partdone(adp, &wkhd); 11870 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 11871 handle_allocdirect_partdone(adp, &wkhd); 11872 /* 11873 * Process deallocations that were held pending until the 11874 * inode had been written to disk. Freeing of the inode 11875 * is delayed until after all blocks have been freed to 11876 * avoid creation of new <vfsid, inum, lbn> triples 11877 * before the old ones have been deleted. Completely 11878 * unlinked inodes are not processed until the unlinked 11879 * inode list is written or the last reference is removed. 11880 */ 11881 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) { 11882 freefile = handle_bufwait(inodedep, NULL); 11883 if (freefile && !LIST_EMPTY(&wkhd)) { 11884 WORKLIST_INSERT(&wkhd, &freefile->fx_list); 11885 freefile = NULL; 11886 } 11887 } 11888 /* 11889 * Move rolled forward dependency completions to the bufwait list 11890 * now that those that were already written have been processed. 11891 */ 11892 if (!LIST_EMPTY(&wkhd) && hadchanges == 0) 11893 panic("handle_written_inodeblock: bufwait but no changes"); 11894 jwork_move(&inodedep->id_bufwait, &wkhd); 11895 11896 if (freefile != NULL) { 11897 /* 11898 * If the inode is goingaway it was never written. Fake up 11899 * the state here so free_inodedep() can succeed. 11900 */ 11901 if (inodedep->id_state & GOINGAWAY) 11902 inodedep->id_state |= COMPLETE | DEPCOMPLETE; 11903 if (free_inodedep(inodedep) == 0) 11904 panic("handle_written_inodeblock: live inodedep %p", 11905 inodedep); 11906 add_to_worklist(&freefile->fx_list, 0); 11907 return (0); 11908 } 11909 11910 /* 11911 * If no outstanding dependencies, free it. 11912 */ 11913 if (free_inodedep(inodedep) || 11914 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 && 11915 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 && 11916 TAILQ_FIRST(&inodedep->id_extupdt) == 0 && 11917 LIST_FIRST(&inodedep->id_bufwait) == 0)) 11918 return (0); 11919 return (hadchanges); 11920 } 11921 11922 /* 11923 * Perform needed roll-forwards and kick off any dependencies that 11924 * can now be processed. 11925 * 11926 * If the write did not succeed, we will do all the roll-forward 11927 * operations, but we will not take the actions that will allow its 11928 * dependencies to be processed. 11929 */ 11930 static int 11931 handle_written_indirdep( 11932 struct indirdep *indirdep, 11933 struct buf *bp, 11934 struct buf **bpp, 11935 int flags) 11936 { 11937 struct allocindir *aip; 11938 struct buf *sbp; 11939 int chgs; 11940 11941 if (indirdep->ir_state & GOINGAWAY) 11942 panic("handle_written_indirdep: indirdep gone"); 11943 if ((indirdep->ir_state & IOSTARTED) == 0) 11944 panic("handle_written_indirdep: IO not started"); 11945 chgs = 0; 11946 /* 11947 * If there were rollbacks revert them here. 11948 */ 11949 if (indirdep->ir_saveddata) { 11950 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount); 11951 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11952 free(indirdep->ir_saveddata, M_INDIRDEP); 11953 indirdep->ir_saveddata = NULL; 11954 } 11955 chgs = 1; 11956 } 11957 indirdep->ir_state &= ~(UNDONE | IOSTARTED); 11958 indirdep->ir_state |= ATTACHED; 11959 /* 11960 * If the write did not succeed, we have done all the roll-forward 11961 * operations, but we cannot take the actions that will allow its 11962 * dependencies to be processed. 11963 */ 11964 if ((flags & WRITESUCCEEDED) == 0) { 11965 stat_indir_blk_ptrs++; 11966 bdirty(bp); 11967 return (1); 11968 } 11969 /* 11970 * Move allocindirs with written pointers to the completehd if 11971 * the indirdep's pointer is not yet written. Otherwise 11972 * free them here. 11973 */ 11974 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) { 11975 LIST_REMOVE(aip, ai_next); 11976 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 11977 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip, 11978 ai_next); 11979 newblk_freefrag(&aip->ai_block); 11980 continue; 11981 } 11982 free_newblk(&aip->ai_block); 11983 } 11984 /* 11985 * Move allocindirs that have finished dependency processing from 11986 * the done list to the write list after updating the pointers. 11987 */ 11988 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11989 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) { 11990 handle_allocindir_partdone(aip); 11991 if (aip == LIST_FIRST(&indirdep->ir_donehd)) 11992 panic("disk_write_complete: not gone"); 11993 chgs = 1; 11994 } 11995 } 11996 /* 11997 * Preserve the indirdep if there were any changes or if it is not 11998 * yet valid on disk. 11999 */ 12000 if (chgs) { 12001 stat_indir_blk_ptrs++; 12002 bdirty(bp); 12003 return (1); 12004 } 12005 /* 12006 * If there were no changes we can discard the savedbp and detach 12007 * ourselves from the buf. We are only carrying completed pointers 12008 * in this case. 12009 */ 12010 sbp = indirdep->ir_savebp; 12011 sbp->b_flags |= B_INVAL | B_NOCACHE; 12012 indirdep->ir_savebp = NULL; 12013 indirdep->ir_bp = NULL; 12014 if (*bpp != NULL) 12015 panic("handle_written_indirdep: bp already exists."); 12016 *bpp = sbp; 12017 /* 12018 * The indirdep may not be freed until its parent points at it. 12019 */ 12020 if (indirdep->ir_state & DEPCOMPLETE) 12021 free_indirdep(indirdep); 12022 12023 return (0); 12024 } 12025 12026 /* 12027 * Process a diradd entry after its dependent inode has been written. 12028 */ 12029 static void 12030 diradd_inode_written( 12031 struct diradd *dap, 12032 struct inodedep *inodedep) 12033 { 12034 12035 LOCK_OWNED(VFSTOUFS(dap->da_list.wk_mp)); 12036 dap->da_state |= COMPLETE; 12037 complete_diradd(dap); 12038 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 12039 } 12040 12041 /* 12042 * Returns true if the bmsafemap will have rollbacks when written. Must only 12043 * be called with the per-filesystem lock and the buf lock on the cg held. 12044 */ 12045 static int 12046 bmsafemap_backgroundwrite( 12047 struct bmsafemap *bmsafemap, 12048 struct buf *bp) 12049 { 12050 int dirty; 12051 12052 LOCK_OWNED(VFSTOUFS(bmsafemap->sm_list.wk_mp)); 12053 dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) | 12054 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd); 12055 /* 12056 * If we're initiating a background write we need to process the 12057 * rollbacks as they exist now, not as they exist when IO starts. 12058 * No other consumers will look at the contents of the shadowed 12059 * buf so this is safe to do here. 12060 */ 12061 if (bp->b_xflags & BX_BKGRDMARKER) 12062 initiate_write_bmsafemap(bmsafemap, bp); 12063 12064 return (dirty); 12065 } 12066 12067 /* 12068 * Re-apply an allocation when a cg write is complete. 12069 */ 12070 static int 12071 jnewblk_rollforward( 12072 struct jnewblk *jnewblk, 12073 struct fs *fs, 12074 struct cg *cgp, 12075 uint8_t *blksfree) 12076 { 12077 ufs1_daddr_t fragno; 12078 ufs2_daddr_t blkno; 12079 long cgbno, bbase; 12080 int frags, blk; 12081 int i; 12082 12083 frags = 0; 12084 cgbno = dtogd(fs, jnewblk->jn_blkno); 12085 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) { 12086 if (isclr(blksfree, cgbno + i)) 12087 panic("jnewblk_rollforward: re-allocated fragment"); 12088 frags++; 12089 } 12090 if (frags == fs->fs_frag) { 12091 blkno = fragstoblks(fs, cgbno); 12092 ffs_clrblock(fs, blksfree, (long)blkno); 12093 ffs_clusteracct(fs, cgp, blkno, -1); 12094 cgp->cg_cs.cs_nbfree--; 12095 } else { 12096 bbase = cgbno - fragnum(fs, cgbno); 12097 cgbno += jnewblk->jn_oldfrags; 12098 /* If a complete block had been reassembled, account for it. */ 12099 fragno = fragstoblks(fs, bbase); 12100 if (ffs_isblock(fs, blksfree, fragno)) { 12101 cgp->cg_cs.cs_nffree += fs->fs_frag; 12102 ffs_clusteracct(fs, cgp, fragno, -1); 12103 cgp->cg_cs.cs_nbfree--; 12104 } 12105 /* Decrement the old frags. */ 12106 blk = blkmap(fs, blksfree, bbase); 12107 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 12108 /* Allocate the fragment */ 12109 for (i = 0; i < frags; i++) 12110 clrbit(blksfree, cgbno + i); 12111 cgp->cg_cs.cs_nffree -= frags; 12112 /* Add back in counts associated with the new frags */ 12113 blk = blkmap(fs, blksfree, bbase); 12114 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 12115 } 12116 return (frags); 12117 } 12118 12119 /* 12120 * Complete a write to a bmsafemap structure. Roll forward any bitmap 12121 * changes if it's not a background write. Set all written dependencies 12122 * to DEPCOMPLETE and free the structure if possible. 12123 * 12124 * If the write did not succeed, we will do all the roll-forward 12125 * operations, but we will not take the actions that will allow its 12126 * dependencies to be processed. 12127 */ 12128 static int 12129 handle_written_bmsafemap( 12130 struct bmsafemap *bmsafemap, 12131 struct buf *bp, 12132 int flags) 12133 { 12134 struct newblk *newblk; 12135 struct inodedep *inodedep; 12136 struct jaddref *jaddref, *jatmp; 12137 struct jnewblk *jnewblk, *jntmp; 12138 struct ufsmount *ump; 12139 uint8_t *inosused; 12140 uint8_t *blksfree; 12141 struct cg *cgp; 12142 struct fs *fs; 12143 ino_t ino; 12144 int foreground; 12145 int chgs; 12146 12147 if ((bmsafemap->sm_state & IOSTARTED) == 0) 12148 panic("handle_written_bmsafemap: Not started\n"); 12149 ump = VFSTOUFS(bmsafemap->sm_list.wk_mp); 12150 chgs = 0; 12151 bmsafemap->sm_state &= ~IOSTARTED; 12152 foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0; 12153 /* 12154 * If write was successful, release journal work that was waiting 12155 * on the write. Otherwise move the work back. 12156 */ 12157 if (flags & WRITESUCCEEDED) 12158 handle_jwork(&bmsafemap->sm_freewr); 12159 else 12160 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, 12161 worklist, wk_list); 12162 12163 /* 12164 * Restore unwritten inode allocation pending jaddref writes. 12165 */ 12166 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) { 12167 cgp = (struct cg *)bp->b_data; 12168 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 12169 inosused = cg_inosused(cgp); 12170 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd, 12171 ja_bmdeps, jatmp) { 12172 if ((jaddref->ja_state & UNDONE) == 0) 12173 continue; 12174 ino = jaddref->ja_ino % fs->fs_ipg; 12175 if (isset(inosused, ino)) 12176 panic("handle_written_bmsafemap: " 12177 "re-allocated inode"); 12178 /* Do the roll-forward only if it's a real copy. */ 12179 if (foreground) { 12180 if ((jaddref->ja_mode & IFMT) == IFDIR) 12181 cgp->cg_cs.cs_ndir++; 12182 cgp->cg_cs.cs_nifree--; 12183 setbit(inosused, ino); 12184 chgs = 1; 12185 } 12186 jaddref->ja_state &= ~UNDONE; 12187 jaddref->ja_state |= ATTACHED; 12188 free_jaddref(jaddref); 12189 } 12190 } 12191 /* 12192 * Restore any block allocations which are pending journal writes. 12193 */ 12194 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 12195 cgp = (struct cg *)bp->b_data; 12196 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 12197 blksfree = cg_blksfree(cgp); 12198 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps, 12199 jntmp) { 12200 if ((jnewblk->jn_state & UNDONE) == 0) 12201 continue; 12202 /* Do the roll-forward only if it's a real copy. */ 12203 if (foreground && 12204 jnewblk_rollforward(jnewblk, fs, cgp, blksfree)) 12205 chgs = 1; 12206 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK); 12207 jnewblk->jn_state |= ATTACHED; 12208 free_jnewblk(jnewblk); 12209 } 12210 } 12211 /* 12212 * If the write did not succeed, we have done all the roll-forward 12213 * operations, but we cannot take the actions that will allow its 12214 * dependencies to be processed. 12215 */ 12216 if ((flags & WRITESUCCEEDED) == 0) { 12217 LIST_CONCAT(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 12218 newblk, nb_deps); 12219 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, 12220 worklist, wk_list); 12221 if (foreground) 12222 bdirty(bp); 12223 return (1); 12224 } 12225 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) { 12226 newblk->nb_state |= DEPCOMPLETE; 12227 newblk->nb_state &= ~ONDEPLIST; 12228 newblk->nb_bmsafemap = NULL; 12229 LIST_REMOVE(newblk, nb_deps); 12230 if (newblk->nb_list.wk_type == D_ALLOCDIRECT) 12231 handle_allocdirect_partdone( 12232 WK_ALLOCDIRECT(&newblk->nb_list), NULL); 12233 else if (newblk->nb_list.wk_type == D_ALLOCINDIR) 12234 handle_allocindir_partdone( 12235 WK_ALLOCINDIR(&newblk->nb_list)); 12236 else if (newblk->nb_list.wk_type != D_NEWBLK) 12237 panic("handle_written_bmsafemap: Unexpected type: %s", 12238 TYPENAME(newblk->nb_list.wk_type)); 12239 } 12240 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) { 12241 inodedep->id_state |= DEPCOMPLETE; 12242 inodedep->id_state &= ~ONDEPLIST; 12243 LIST_REMOVE(inodedep, id_deps); 12244 inodedep->id_bmsafemap = NULL; 12245 } 12246 LIST_REMOVE(bmsafemap, sm_next); 12247 if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) && 12248 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) && 12249 LIST_EMPTY(&bmsafemap->sm_newblkhd) && 12250 LIST_EMPTY(&bmsafemap->sm_inodedephd) && 12251 LIST_EMPTY(&bmsafemap->sm_freehd)) { 12252 LIST_REMOVE(bmsafemap, sm_hash); 12253 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 12254 return (0); 12255 } 12256 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 12257 if (foreground) 12258 bdirty(bp); 12259 return (1); 12260 } 12261 12262 /* 12263 * Try to free a mkdir dependency. 12264 */ 12265 static void 12266 complete_mkdir(struct mkdir *mkdir) 12267 { 12268 struct diradd *dap; 12269 12270 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE) 12271 return; 12272 LIST_REMOVE(mkdir, md_mkdirs); 12273 dap = mkdir->md_diradd; 12274 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 12275 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) { 12276 dap->da_state |= DEPCOMPLETE; 12277 complete_diradd(dap); 12278 } 12279 WORKITEM_FREE(mkdir, D_MKDIR); 12280 } 12281 12282 /* 12283 * Handle the completion of a mkdir dependency. 12284 */ 12285 static void 12286 handle_written_mkdir(struct mkdir *mkdir, int type) 12287 { 12288 12289 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type) 12290 panic("handle_written_mkdir: bad type"); 12291 mkdir->md_state |= COMPLETE; 12292 complete_mkdir(mkdir); 12293 } 12294 12295 static int 12296 free_pagedep(struct pagedep *pagedep) 12297 { 12298 int i; 12299 12300 if (pagedep->pd_state & NEWBLOCK) 12301 return (0); 12302 if (!LIST_EMPTY(&pagedep->pd_dirremhd)) 12303 return (0); 12304 for (i = 0; i < DAHASHSZ; i++) 12305 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i])) 12306 return (0); 12307 if (!LIST_EMPTY(&pagedep->pd_pendinghd)) 12308 return (0); 12309 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd)) 12310 return (0); 12311 if (pagedep->pd_state & ONWORKLIST) 12312 WORKLIST_REMOVE(&pagedep->pd_list); 12313 LIST_REMOVE(pagedep, pd_hash); 12314 WORKITEM_FREE(pagedep, D_PAGEDEP); 12315 12316 return (1); 12317 } 12318 12319 /* 12320 * Called from within softdep_disk_write_complete above. 12321 * A write operation was just completed. Removed inodes can 12322 * now be freed and associated block pointers may be committed. 12323 * Note that this routine is always called from interrupt level 12324 * with further interrupts from this device blocked. 12325 * 12326 * If the write did not succeed, we will do all the roll-forward 12327 * operations, but we will not take the actions that will allow its 12328 * dependencies to be processed. 12329 */ 12330 static int 12331 handle_written_filepage( 12332 struct pagedep *pagedep, 12333 struct buf *bp, /* buffer containing the written page */ 12334 int flags) 12335 { 12336 struct dirrem *dirrem; 12337 struct diradd *dap, *nextdap; 12338 struct direct *ep; 12339 int i, chgs; 12340 12341 if ((pagedep->pd_state & IOSTARTED) == 0) 12342 panic("handle_written_filepage: not started"); 12343 pagedep->pd_state &= ~IOSTARTED; 12344 if ((flags & WRITESUCCEEDED) == 0) 12345 goto rollforward; 12346 /* 12347 * Process any directory removals that have been committed. 12348 */ 12349 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) { 12350 LIST_REMOVE(dirrem, dm_next); 12351 dirrem->dm_state |= COMPLETE; 12352 dirrem->dm_dirinum = pagedep->pd_ino; 12353 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 12354 ("handle_written_filepage: Journal entries not written.")); 12355 add_to_worklist(&dirrem->dm_list, 0); 12356 } 12357 /* 12358 * Free any directory additions that have been committed. 12359 * If it is a newly allocated block, we have to wait until 12360 * the on-disk directory inode claims the new block. 12361 */ 12362 if ((pagedep->pd_state & NEWBLOCK) == 0) 12363 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 12364 free_diradd(dap, NULL); 12365 rollforward: 12366 /* 12367 * Uncommitted directory entries must be restored. 12368 */ 12369 for (chgs = 0, i = 0; i < DAHASHSZ; i++) { 12370 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap; 12371 dap = nextdap) { 12372 nextdap = LIST_NEXT(dap, da_pdlist); 12373 if (dap->da_state & ATTACHED) 12374 panic("handle_written_filepage: attached"); 12375 ep = (struct direct *) 12376 ((char *)bp->b_data + dap->da_offset); 12377 ep->d_ino = dap->da_newinum; 12378 dap->da_state &= ~UNDONE; 12379 dap->da_state |= ATTACHED; 12380 chgs = 1; 12381 /* 12382 * If the inode referenced by the directory has 12383 * been written out, then the dependency can be 12384 * moved to the pending list. 12385 */ 12386 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 12387 LIST_REMOVE(dap, da_pdlist); 12388 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, 12389 da_pdlist); 12390 } 12391 } 12392 } 12393 /* 12394 * If there were any rollbacks in the directory, then it must be 12395 * marked dirty so that its will eventually get written back in 12396 * its correct form. 12397 */ 12398 if (chgs || (flags & WRITESUCCEEDED) == 0) { 12399 if ((bp->b_flags & B_DELWRI) == 0) 12400 stat_dir_entry++; 12401 bdirty(bp); 12402 return (1); 12403 } 12404 /* 12405 * If we are not waiting for a new directory block to be 12406 * claimed by its inode, then the pagedep will be freed. 12407 * Otherwise it will remain to track any new entries on 12408 * the page in case they are fsync'ed. 12409 */ 12410 free_pagedep(pagedep); 12411 return (0); 12412 } 12413 12414 /* 12415 * Writing back in-core inode structures. 12416 * 12417 * The filesystem only accesses an inode's contents when it occupies an 12418 * "in-core" inode structure. These "in-core" structures are separate from 12419 * the page frames used to cache inode blocks. Only the latter are 12420 * transferred to/from the disk. So, when the updated contents of the 12421 * "in-core" inode structure are copied to the corresponding in-memory inode 12422 * block, the dependencies are also transferred. The following procedure is 12423 * called when copying a dirty "in-core" inode to a cached inode block. 12424 */ 12425 12426 /* 12427 * Called when an inode is loaded from disk. If the effective link count 12428 * differed from the actual link count when it was last flushed, then we 12429 * need to ensure that the correct effective link count is put back. 12430 */ 12431 void 12432 softdep_load_inodeblock( 12433 struct inode *ip) /* the "in_core" copy of the inode */ 12434 { 12435 struct inodedep *inodedep; 12436 struct ufsmount *ump; 12437 12438 ump = ITOUMP(ip); 12439 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 12440 ("softdep_load_inodeblock called on non-softdep filesystem")); 12441 /* 12442 * Check for alternate nlink count. 12443 */ 12444 ip->i_effnlink = ip->i_nlink; 12445 ACQUIRE_LOCK(ump); 12446 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) { 12447 FREE_LOCK(ump); 12448 return; 12449 } 12450 if (ip->i_nlink != inodedep->id_nlinkwrote && 12451 inodedep->id_nlinkwrote != -1) { 12452 KASSERT(ip->i_nlink == 0 && 12453 (ump->um_flags & UM_FSFAIL_CLEANUP) != 0, 12454 ("read bad i_nlink value")); 12455 ip->i_effnlink = ip->i_nlink = inodedep->id_nlinkwrote; 12456 } 12457 ip->i_effnlink -= inodedep->id_nlinkdelta; 12458 KASSERT(ip->i_effnlink >= 0, 12459 ("softdep_load_inodeblock: negative i_effnlink")); 12460 FREE_LOCK(ump); 12461 } 12462 12463 /* 12464 * This routine is called just before the "in-core" inode 12465 * information is to be copied to the in-memory inode block. 12466 * Recall that an inode block contains several inodes. If 12467 * the force flag is set, then the dependencies will be 12468 * cleared so that the update can always be made. Note that 12469 * the buffer is locked when this routine is called, so we 12470 * will never be in the middle of writing the inode block 12471 * to disk. 12472 */ 12473 void 12474 softdep_update_inodeblock( 12475 struct inode *ip, /* the "in_core" copy of the inode */ 12476 struct buf *bp, /* the buffer containing the inode block */ 12477 int waitfor) /* nonzero => update must be allowed */ 12478 { 12479 struct inodedep *inodedep; 12480 struct inoref *inoref; 12481 struct ufsmount *ump; 12482 struct worklist *wk; 12483 struct mount *mp; 12484 struct buf *ibp; 12485 struct fs *fs; 12486 int error; 12487 12488 ump = ITOUMP(ip); 12489 mp = UFSTOVFS(ump); 12490 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 12491 ("softdep_update_inodeblock called on non-softdep filesystem")); 12492 fs = ump->um_fs; 12493 /* 12494 * If the effective link count is not equal to the actual link 12495 * count, then we must track the difference in an inodedep while 12496 * the inode is (potentially) tossed out of the cache. Otherwise, 12497 * if there is no existing inodedep, then there are no dependencies 12498 * to track. 12499 */ 12500 ACQUIRE_LOCK(ump); 12501 again: 12502 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12503 FREE_LOCK(ump); 12504 if (ip->i_effnlink != ip->i_nlink) 12505 panic("softdep_update_inodeblock: bad link count"); 12506 return; 12507 } 12508 /* 12509 * Preserve the freelink that is on disk. clear_unlinked_inodedep() 12510 * does not have access to the in-core ip so must write directly into 12511 * the inode block buffer when setting freelink. 12512 */ 12513 if ((inodedep->id_state & UNLINKED) != 0) { 12514 if (fs->fs_magic == FS_UFS1_MAGIC) 12515 DIP_SET(ip, i_freelink, 12516 ((struct ufs1_dinode *)bp->b_data + 12517 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12518 else 12519 DIP_SET(ip, i_freelink, 12520 ((struct ufs2_dinode *)bp->b_data + 12521 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12522 } 12523 KASSERT(ip->i_nlink >= inodedep->id_nlinkdelta, 12524 ("softdep_update_inodeblock inconsistent ip %p i_nlink %d " 12525 "inodedep %p id_nlinkdelta %jd", 12526 ip, ip->i_nlink, inodedep, (intmax_t)inodedep->id_nlinkdelta)); 12527 inodedep->id_nlinkwrote = ip->i_nlink; 12528 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink) 12529 panic("softdep_update_inodeblock: bad delta"); 12530 /* 12531 * If we're flushing all dependencies we must also move any waiting 12532 * for journal writes onto the bufwait list prior to I/O. 12533 */ 12534 if (waitfor) { 12535 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12536 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12537 == DEPCOMPLETE) { 12538 jwait(&inoref->if_list, MNT_WAIT); 12539 goto again; 12540 } 12541 } 12542 } 12543 /* 12544 * Changes have been initiated. Anything depending on these 12545 * changes cannot occur until this inode has been written. 12546 */ 12547 inodedep->id_state &= ~COMPLETE; 12548 if ((inodedep->id_state & ONWORKLIST) == 0) 12549 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list); 12550 /* 12551 * Any new dependencies associated with the incore inode must 12552 * now be moved to the list associated with the buffer holding 12553 * the in-memory copy of the inode. Once merged process any 12554 * allocdirects that are completed by the merger. 12555 */ 12556 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt); 12557 if (!TAILQ_EMPTY(&inodedep->id_inoupdt)) 12558 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt), 12559 NULL); 12560 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt); 12561 if (!TAILQ_EMPTY(&inodedep->id_extupdt)) 12562 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt), 12563 NULL); 12564 /* 12565 * Now that the inode has been pushed into the buffer, the 12566 * operations dependent on the inode being written to disk 12567 * can be moved to the id_bufwait so that they will be 12568 * processed when the buffer I/O completes. 12569 */ 12570 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) { 12571 WORKLIST_REMOVE(wk); 12572 WORKLIST_INSERT(&inodedep->id_bufwait, wk); 12573 } 12574 /* 12575 * Newly allocated inodes cannot be written until the bitmap 12576 * that allocates them have been written (indicated by 12577 * DEPCOMPLETE being set in id_state). If we are doing a 12578 * forced sync (e.g., an fsync on a file), we force the bitmap 12579 * to be written so that the update can be done. 12580 */ 12581 if (waitfor == 0) { 12582 FREE_LOCK(ump); 12583 return; 12584 } 12585 retry: 12586 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) { 12587 FREE_LOCK(ump); 12588 return; 12589 } 12590 ibp = inodedep->id_bmsafemap->sm_buf; 12591 ibp = getdirtybuf(ibp, LOCK_PTR(ump), MNT_WAIT); 12592 if (ibp == NULL) { 12593 /* 12594 * If ibp came back as NULL, the dependency could have been 12595 * freed while we slept. Look it up again, and check to see 12596 * that it has completed. 12597 */ 12598 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 12599 goto retry; 12600 FREE_LOCK(ump); 12601 return; 12602 } 12603 FREE_LOCK(ump); 12604 if ((error = bwrite(ibp)) != 0) 12605 softdep_error("softdep_update_inodeblock: bwrite", error); 12606 } 12607 12608 /* 12609 * Merge the a new inode dependency list (such as id_newinoupdt) into an 12610 * old inode dependency list (such as id_inoupdt). 12611 */ 12612 static void 12613 merge_inode_lists( 12614 struct allocdirectlst *newlisthead, 12615 struct allocdirectlst *oldlisthead) 12616 { 12617 struct allocdirect *listadp, *newadp; 12618 12619 newadp = TAILQ_FIRST(newlisthead); 12620 if (newadp != NULL) 12621 LOCK_OWNED(VFSTOUFS(newadp->ad_block.nb_list.wk_mp)); 12622 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) { 12623 if (listadp->ad_offset < newadp->ad_offset) { 12624 listadp = TAILQ_NEXT(listadp, ad_next); 12625 continue; 12626 } 12627 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12628 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next); 12629 if (listadp->ad_offset == newadp->ad_offset) { 12630 allocdirect_merge(oldlisthead, newadp, 12631 listadp); 12632 listadp = newadp; 12633 } 12634 newadp = TAILQ_FIRST(newlisthead); 12635 } 12636 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) { 12637 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12638 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next); 12639 } 12640 } 12641 12642 /* 12643 * If we are doing an fsync, then we must ensure that any directory 12644 * entries for the inode have been written after the inode gets to disk. 12645 */ 12646 int 12647 softdep_fsync( 12648 struct vnode *vp) /* the "in_core" copy of the inode */ 12649 { 12650 struct inodedep *inodedep; 12651 struct pagedep *pagedep; 12652 struct inoref *inoref; 12653 struct ufsmount *ump; 12654 struct worklist *wk; 12655 struct diradd *dap; 12656 struct mount *mp; 12657 struct vnode *pvp; 12658 struct inode *ip; 12659 struct buf *bp; 12660 struct fs *fs; 12661 struct thread *td = curthread; 12662 int error, flushparent, pagedep_new_block; 12663 ino_t parentino; 12664 ufs_lbn_t lbn; 12665 12666 ip = VTOI(vp); 12667 mp = vp->v_mount; 12668 ump = VFSTOUFS(mp); 12669 fs = ump->um_fs; 12670 if (MOUNTEDSOFTDEP(mp) == 0) 12671 return (0); 12672 ACQUIRE_LOCK(ump); 12673 restart: 12674 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12675 FREE_LOCK(ump); 12676 return (0); 12677 } 12678 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12679 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12680 == DEPCOMPLETE) { 12681 jwait(&inoref->if_list, MNT_WAIT); 12682 goto restart; 12683 } 12684 } 12685 if (!LIST_EMPTY(&inodedep->id_inowait) || 12686 !TAILQ_EMPTY(&inodedep->id_extupdt) || 12687 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 12688 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 12689 !TAILQ_EMPTY(&inodedep->id_newinoupdt)) 12690 panic("softdep_fsync: pending ops %p", inodedep); 12691 for (error = 0, flushparent = 0; ; ) { 12692 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL) 12693 break; 12694 if (wk->wk_type != D_DIRADD) 12695 panic("softdep_fsync: Unexpected type %s", 12696 TYPENAME(wk->wk_type)); 12697 dap = WK_DIRADD(wk); 12698 /* 12699 * Flush our parent if this directory entry has a MKDIR_PARENT 12700 * dependency or is contained in a newly allocated block. 12701 */ 12702 if (dap->da_state & DIRCHG) 12703 pagedep = dap->da_previous->dm_pagedep; 12704 else 12705 pagedep = dap->da_pagedep; 12706 parentino = pagedep->pd_ino; 12707 lbn = pagedep->pd_lbn; 12708 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) 12709 panic("softdep_fsync: dirty"); 12710 if ((dap->da_state & MKDIR_PARENT) || 12711 (pagedep->pd_state & NEWBLOCK)) 12712 flushparent = 1; 12713 else 12714 flushparent = 0; 12715 /* 12716 * If we are being fsync'ed as part of vgone'ing this vnode, 12717 * then we will not be able to release and recover the 12718 * vnode below, so we just have to give up on writing its 12719 * directory entry out. It will eventually be written, just 12720 * not now, but then the user was not asking to have it 12721 * written, so we are not breaking any promises. 12722 */ 12723 if (VN_IS_DOOMED(vp)) 12724 break; 12725 /* 12726 * We prevent deadlock by always fetching inodes from the 12727 * root, moving down the directory tree. Thus, when fetching 12728 * our parent directory, we first try to get the lock. If 12729 * that fails, we must unlock ourselves before requesting 12730 * the lock on our parent. See the comment in ufs_lookup 12731 * for details on possible races. 12732 */ 12733 FREE_LOCK(ump); 12734 error = get_parent_vp(vp, mp, parentino, NULL, NULL, NULL, 12735 &pvp); 12736 if (error == ERELOOKUP) 12737 error = 0; 12738 if (error != 0) 12739 return (error); 12740 /* 12741 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps 12742 * that are contained in direct blocks will be resolved by 12743 * doing a ffs_update. Pagedeps contained in indirect blocks 12744 * may require a complete sync'ing of the directory. So, we 12745 * try the cheap and fast ffs_update first, and if that fails, 12746 * then we do the slower ffs_syncvnode of the directory. 12747 */ 12748 if (flushparent) { 12749 int locked; 12750 12751 if ((error = ffs_update(pvp, 1)) != 0) { 12752 vput(pvp); 12753 return (error); 12754 } 12755 ACQUIRE_LOCK(ump); 12756 locked = 1; 12757 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) { 12758 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) { 12759 if (wk->wk_type != D_DIRADD) 12760 panic("softdep_fsync: Unexpected type %s", 12761 TYPENAME(wk->wk_type)); 12762 dap = WK_DIRADD(wk); 12763 if (dap->da_state & DIRCHG) 12764 pagedep = dap->da_previous->dm_pagedep; 12765 else 12766 pagedep = dap->da_pagedep; 12767 pagedep_new_block = pagedep->pd_state & NEWBLOCK; 12768 FREE_LOCK(ump); 12769 locked = 0; 12770 if (pagedep_new_block) { 12771 VOP_UNLOCK(vp); 12772 error = ffs_syncvnode(pvp, 12773 MNT_WAIT, 0); 12774 if (error == 0) 12775 error = ERELOOKUP; 12776 vput(pvp); 12777 vn_lock(vp, LK_EXCLUSIVE | 12778 LK_RETRY); 12779 return (error); 12780 } 12781 } 12782 } 12783 if (locked) 12784 FREE_LOCK(ump); 12785 } 12786 /* 12787 * Flush directory page containing the inode's name. 12788 */ 12789 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred, 12790 &bp); 12791 if (error == 0) 12792 error = bwrite(bp); 12793 else 12794 brelse(bp); 12795 vput(pvp); 12796 if (!ffs_fsfail_cleanup(ump, error)) 12797 return (error); 12798 ACQUIRE_LOCK(ump); 12799 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 12800 break; 12801 } 12802 FREE_LOCK(ump); 12803 return (0); 12804 } 12805 12806 /* 12807 * Flush all the dirty bitmaps associated with the block device 12808 * before flushing the rest of the dirty blocks so as to reduce 12809 * the number of dependencies that will have to be rolled back. 12810 * 12811 * XXX Unused? 12812 */ 12813 void 12814 softdep_fsync_mountdev(struct vnode *vp) 12815 { 12816 struct buf *bp, *nbp; 12817 struct worklist *wk; 12818 struct bufobj *bo; 12819 12820 if (!vn_isdisk(vp)) 12821 panic("softdep_fsync_mountdev: vnode not a disk"); 12822 bo = &vp->v_bufobj; 12823 restart: 12824 BO_LOCK(bo); 12825 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 12826 /* 12827 * If it is already scheduled, skip to the next buffer. 12828 */ 12829 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) 12830 continue; 12831 12832 if ((bp->b_flags & B_DELWRI) == 0) 12833 panic("softdep_fsync_mountdev: not dirty"); 12834 /* 12835 * We are only interested in bitmaps with outstanding 12836 * dependencies. 12837 */ 12838 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL || 12839 wk->wk_type != D_BMSAFEMAP || 12840 (bp->b_vflags & BV_BKGRDINPROG)) { 12841 BUF_UNLOCK(bp); 12842 continue; 12843 } 12844 BO_UNLOCK(bo); 12845 bremfree(bp); 12846 (void) bawrite(bp); 12847 goto restart; 12848 } 12849 drain_output(vp); 12850 BO_UNLOCK(bo); 12851 } 12852 12853 /* 12854 * Sync all cylinder groups that were dirty at the time this function is 12855 * called. Newly dirtied cgs will be inserted before the sentinel. This 12856 * is used to flush freedep activity that may be holding up writes to a 12857 * indirect block. 12858 */ 12859 static int 12860 sync_cgs(struct mount *mp, int waitfor) 12861 { 12862 struct bmsafemap *bmsafemap; 12863 struct bmsafemap *sentinel; 12864 struct ufsmount *ump; 12865 struct buf *bp; 12866 int error; 12867 12868 sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK); 12869 sentinel->sm_cg = -1; 12870 ump = VFSTOUFS(mp); 12871 error = 0; 12872 ACQUIRE_LOCK(ump); 12873 LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next); 12874 for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL; 12875 bmsafemap = LIST_NEXT(sentinel, sm_next)) { 12876 /* Skip sentinels and cgs with no work to release. */ 12877 if (bmsafemap->sm_cg == -1 || 12878 (LIST_EMPTY(&bmsafemap->sm_freehd) && 12879 LIST_EMPTY(&bmsafemap->sm_freewr))) { 12880 LIST_REMOVE(sentinel, sm_next); 12881 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 12882 continue; 12883 } 12884 /* 12885 * If we don't get the lock and we're waiting try again, if 12886 * not move on to the next buf and try to sync it. 12887 */ 12888 bp = getdirtybuf(bmsafemap->sm_buf, LOCK_PTR(ump), waitfor); 12889 if (bp == NULL && waitfor == MNT_WAIT) 12890 continue; 12891 LIST_REMOVE(sentinel, sm_next); 12892 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 12893 if (bp == NULL) 12894 continue; 12895 FREE_LOCK(ump); 12896 if (waitfor == MNT_NOWAIT) 12897 bawrite(bp); 12898 else 12899 error = bwrite(bp); 12900 ACQUIRE_LOCK(ump); 12901 if (error) 12902 break; 12903 } 12904 LIST_REMOVE(sentinel, sm_next); 12905 FREE_LOCK(ump); 12906 free(sentinel, M_BMSAFEMAP); 12907 return (error); 12908 } 12909 12910 /* 12911 * This routine is called when we are trying to synchronously flush a 12912 * file. This routine must eliminate any filesystem metadata dependencies 12913 * so that the syncing routine can succeed. 12914 */ 12915 int 12916 softdep_sync_metadata(struct vnode *vp) 12917 { 12918 struct inode *ip; 12919 int error; 12920 12921 ip = VTOI(vp); 12922 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 12923 ("softdep_sync_metadata called on non-softdep filesystem")); 12924 /* 12925 * Ensure that any direct block dependencies have been cleared, 12926 * truncations are started, and inode references are journaled. 12927 */ 12928 ACQUIRE_LOCK(VFSTOUFS(vp->v_mount)); 12929 /* 12930 * Write all journal records to prevent rollbacks on devvp. 12931 */ 12932 if (vp->v_type == VCHR) 12933 softdep_flushjournal(vp->v_mount); 12934 error = flush_inodedep_deps(vp, vp->v_mount, ip->i_number); 12935 /* 12936 * Ensure that all truncates are written so we won't find deps on 12937 * indirect blocks. 12938 */ 12939 process_truncates(vp); 12940 FREE_LOCK(VFSTOUFS(vp->v_mount)); 12941 12942 return (error); 12943 } 12944 12945 /* 12946 * This routine is called when we are attempting to sync a buf with 12947 * dependencies. If waitfor is MNT_NOWAIT it attempts to schedule any 12948 * other IO it can but returns EBUSY if the buffer is not yet able to 12949 * be written. Dependencies which will not cause rollbacks will always 12950 * return 0. 12951 */ 12952 int 12953 softdep_sync_buf(struct vnode *vp, 12954 struct buf *bp, 12955 int waitfor) 12956 { 12957 struct indirdep *indirdep; 12958 struct pagedep *pagedep; 12959 struct allocindir *aip; 12960 struct newblk *newblk; 12961 struct ufsmount *ump; 12962 struct buf *nbp; 12963 struct worklist *wk; 12964 int i, error; 12965 12966 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 12967 ("softdep_sync_buf called on non-softdep filesystem")); 12968 /* 12969 * For VCHR we just don't want to force flush any dependencies that 12970 * will cause rollbacks. 12971 */ 12972 if (vp->v_type == VCHR) { 12973 if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0)) 12974 return (EBUSY); 12975 return (0); 12976 } 12977 ump = VFSTOUFS(vp->v_mount); 12978 ACQUIRE_LOCK(ump); 12979 /* 12980 * As we hold the buffer locked, none of its dependencies 12981 * will disappear. 12982 */ 12983 error = 0; 12984 top: 12985 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 12986 switch (wk->wk_type) { 12987 case D_ALLOCDIRECT: 12988 case D_ALLOCINDIR: 12989 newblk = WK_NEWBLK(wk); 12990 if (newblk->nb_jnewblk != NULL) { 12991 if (waitfor == MNT_NOWAIT) { 12992 error = EBUSY; 12993 goto out_unlock; 12994 } 12995 jwait(&newblk->nb_jnewblk->jn_list, waitfor); 12996 goto top; 12997 } 12998 if (newblk->nb_state & DEPCOMPLETE || 12999 waitfor == MNT_NOWAIT) 13000 continue; 13001 nbp = newblk->nb_bmsafemap->sm_buf; 13002 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 13003 if (nbp == NULL) 13004 goto top; 13005 FREE_LOCK(ump); 13006 if ((error = bwrite(nbp)) != 0) 13007 goto out; 13008 ACQUIRE_LOCK(ump); 13009 continue; 13010 13011 case D_INDIRDEP: 13012 indirdep = WK_INDIRDEP(wk); 13013 if (waitfor == MNT_NOWAIT) { 13014 if (!TAILQ_EMPTY(&indirdep->ir_trunc) || 13015 !LIST_EMPTY(&indirdep->ir_deplisthd)) { 13016 error = EBUSY; 13017 goto out_unlock; 13018 } 13019 } 13020 if (!TAILQ_EMPTY(&indirdep->ir_trunc)) 13021 panic("softdep_sync_buf: truncation pending."); 13022 restart: 13023 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 13024 newblk = (struct newblk *)aip; 13025 if (newblk->nb_jnewblk != NULL) { 13026 jwait(&newblk->nb_jnewblk->jn_list, 13027 waitfor); 13028 goto restart; 13029 } 13030 if (newblk->nb_state & DEPCOMPLETE) 13031 continue; 13032 nbp = newblk->nb_bmsafemap->sm_buf; 13033 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 13034 if (nbp == NULL) 13035 goto restart; 13036 FREE_LOCK(ump); 13037 if ((error = bwrite(nbp)) != 0) 13038 goto out; 13039 ACQUIRE_LOCK(ump); 13040 goto restart; 13041 } 13042 continue; 13043 13044 case D_PAGEDEP: 13045 /* 13046 * Only flush directory entries in synchronous passes. 13047 */ 13048 if (waitfor != MNT_WAIT) { 13049 error = EBUSY; 13050 goto out_unlock; 13051 } 13052 /* 13053 * While syncing snapshots, we must allow recursive 13054 * lookups. 13055 */ 13056 BUF_AREC(bp); 13057 /* 13058 * We are trying to sync a directory that may 13059 * have dependencies on both its own metadata 13060 * and/or dependencies on the inodes of any 13061 * recently allocated files. We walk its diradd 13062 * lists pushing out the associated inode. 13063 */ 13064 pagedep = WK_PAGEDEP(wk); 13065 for (i = 0; i < DAHASHSZ; i++) { 13066 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0) 13067 continue; 13068 error = flush_pagedep_deps(vp, wk->wk_mp, 13069 &pagedep->pd_diraddhd[i], bp); 13070 if (error != 0) { 13071 if (error != ERELOOKUP) 13072 BUF_NOREC(bp); 13073 goto out_unlock; 13074 } 13075 } 13076 BUF_NOREC(bp); 13077 continue; 13078 13079 case D_FREEWORK: 13080 case D_FREEDEP: 13081 case D_JSEGDEP: 13082 case D_JNEWBLK: 13083 continue; 13084 13085 default: 13086 panic("softdep_sync_buf: Unknown type %s", 13087 TYPENAME(wk->wk_type)); 13088 /* NOTREACHED */ 13089 } 13090 } 13091 out_unlock: 13092 FREE_LOCK(ump); 13093 out: 13094 return (error); 13095 } 13096 13097 /* 13098 * Flush the dependencies associated with an inodedep. 13099 */ 13100 static int 13101 flush_inodedep_deps( 13102 struct vnode *vp, 13103 struct mount *mp, 13104 ino_t ino) 13105 { 13106 struct inodedep *inodedep; 13107 struct inoref *inoref; 13108 struct ufsmount *ump; 13109 int error, waitfor; 13110 13111 /* 13112 * This work is done in two passes. The first pass grabs most 13113 * of the buffers and begins asynchronously writing them. The 13114 * only way to wait for these asynchronous writes is to sleep 13115 * on the filesystem vnode which may stay busy for a long time 13116 * if the filesystem is active. So, instead, we make a second 13117 * pass over the dependencies blocking on each write. In the 13118 * usual case we will be blocking against a write that we 13119 * initiated, so when it is done the dependency will have been 13120 * resolved. Thus the second pass is expected to end quickly. 13121 * We give a brief window at the top of the loop to allow 13122 * any pending I/O to complete. 13123 */ 13124 ump = VFSTOUFS(mp); 13125 LOCK_OWNED(ump); 13126 for (error = 0, waitfor = MNT_NOWAIT; ; ) { 13127 if (error) 13128 return (error); 13129 FREE_LOCK(ump); 13130 ACQUIRE_LOCK(ump); 13131 restart: 13132 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 13133 return (0); 13134 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 13135 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 13136 == DEPCOMPLETE) { 13137 jwait(&inoref->if_list, MNT_WAIT); 13138 goto restart; 13139 } 13140 } 13141 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) || 13142 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) || 13143 flush_deplist(&inodedep->id_extupdt, waitfor, &error) || 13144 flush_deplist(&inodedep->id_newextupdt, waitfor, &error)) 13145 continue; 13146 /* 13147 * If pass2, we are done, otherwise do pass 2. 13148 */ 13149 if (waitfor == MNT_WAIT) 13150 break; 13151 waitfor = MNT_WAIT; 13152 } 13153 /* 13154 * Try freeing inodedep in case all dependencies have been removed. 13155 */ 13156 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0) 13157 (void) free_inodedep(inodedep); 13158 return (0); 13159 } 13160 13161 /* 13162 * Flush an inode dependency list. 13163 */ 13164 static int 13165 flush_deplist( 13166 struct allocdirectlst *listhead, 13167 int waitfor, 13168 int *errorp) 13169 { 13170 struct allocdirect *adp; 13171 struct newblk *newblk; 13172 struct ufsmount *ump; 13173 struct buf *bp; 13174 13175 if ((adp = TAILQ_FIRST(listhead)) == NULL) 13176 return (0); 13177 ump = VFSTOUFS(adp->ad_list.wk_mp); 13178 LOCK_OWNED(ump); 13179 TAILQ_FOREACH(adp, listhead, ad_next) { 13180 newblk = (struct newblk *)adp; 13181 if (newblk->nb_jnewblk != NULL) { 13182 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 13183 return (1); 13184 } 13185 if (newblk->nb_state & DEPCOMPLETE) 13186 continue; 13187 bp = newblk->nb_bmsafemap->sm_buf; 13188 bp = getdirtybuf(bp, LOCK_PTR(ump), waitfor); 13189 if (bp == NULL) { 13190 if (waitfor == MNT_NOWAIT) 13191 continue; 13192 return (1); 13193 } 13194 FREE_LOCK(ump); 13195 if (waitfor == MNT_NOWAIT) 13196 bawrite(bp); 13197 else 13198 *errorp = bwrite(bp); 13199 ACQUIRE_LOCK(ump); 13200 return (1); 13201 } 13202 return (0); 13203 } 13204 13205 /* 13206 * Flush dependencies associated with an allocdirect block. 13207 */ 13208 static int 13209 flush_newblk_dep( 13210 struct vnode *vp, 13211 struct mount *mp, 13212 ufs_lbn_t lbn) 13213 { 13214 struct newblk *newblk; 13215 struct ufsmount *ump; 13216 struct bufobj *bo; 13217 struct inode *ip; 13218 struct buf *bp; 13219 ufs2_daddr_t blkno; 13220 int error; 13221 13222 error = 0; 13223 bo = &vp->v_bufobj; 13224 ip = VTOI(vp); 13225 blkno = DIP(ip, i_db[lbn]); 13226 if (blkno == 0) 13227 panic("flush_newblk_dep: Missing block"); 13228 ump = VFSTOUFS(mp); 13229 ACQUIRE_LOCK(ump); 13230 /* 13231 * Loop until all dependencies related to this block are satisfied. 13232 * We must be careful to restart after each sleep in case a write 13233 * completes some part of this process for us. 13234 */ 13235 for (;;) { 13236 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) { 13237 FREE_LOCK(ump); 13238 break; 13239 } 13240 if (newblk->nb_list.wk_type != D_ALLOCDIRECT) 13241 panic("flush_newblk_dep: Bad newblk %p", newblk); 13242 /* 13243 * Flush the journal. 13244 */ 13245 if (newblk->nb_jnewblk != NULL) { 13246 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 13247 continue; 13248 } 13249 /* 13250 * Write the bitmap dependency. 13251 */ 13252 if ((newblk->nb_state & DEPCOMPLETE) == 0) { 13253 bp = newblk->nb_bmsafemap->sm_buf; 13254 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 13255 if (bp == NULL) 13256 continue; 13257 FREE_LOCK(ump); 13258 error = bwrite(bp); 13259 if (error) 13260 break; 13261 ACQUIRE_LOCK(ump); 13262 continue; 13263 } 13264 /* 13265 * Write the buffer. 13266 */ 13267 FREE_LOCK(ump); 13268 BO_LOCK(bo); 13269 bp = gbincore(bo, lbn); 13270 if (bp != NULL) { 13271 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 13272 LK_INTERLOCK, BO_LOCKPTR(bo)); 13273 if (error == ENOLCK) { 13274 ACQUIRE_LOCK(ump); 13275 error = 0; 13276 continue; /* Slept, retry */ 13277 } 13278 if (error != 0) 13279 break; /* Failed */ 13280 if (bp->b_flags & B_DELWRI) { 13281 bremfree(bp); 13282 error = bwrite(bp); 13283 if (error) 13284 break; 13285 } else 13286 BUF_UNLOCK(bp); 13287 } else 13288 BO_UNLOCK(bo); 13289 /* 13290 * We have to wait for the direct pointers to 13291 * point at the newdirblk before the dependency 13292 * will go away. 13293 */ 13294 error = ffs_update(vp, 1); 13295 if (error) 13296 break; 13297 ACQUIRE_LOCK(ump); 13298 } 13299 return (error); 13300 } 13301 13302 /* 13303 * Eliminate a pagedep dependency by flushing out all its diradd dependencies. 13304 */ 13305 static int 13306 flush_pagedep_deps( 13307 struct vnode *pvp, 13308 struct mount *mp, 13309 struct diraddhd *diraddhdp, 13310 struct buf *locked_bp) 13311 { 13312 struct inodedep *inodedep; 13313 struct inoref *inoref; 13314 struct ufsmount *ump; 13315 struct diradd *dap; 13316 struct vnode *vp; 13317 int error = 0; 13318 struct buf *bp; 13319 ino_t inum; 13320 struct diraddhd unfinished; 13321 13322 LIST_INIT(&unfinished); 13323 ump = VFSTOUFS(mp); 13324 LOCK_OWNED(ump); 13325 restart: 13326 while ((dap = LIST_FIRST(diraddhdp)) != NULL) { 13327 /* 13328 * Flush ourselves if this directory entry 13329 * has a MKDIR_PARENT dependency. 13330 */ 13331 if (dap->da_state & MKDIR_PARENT) { 13332 FREE_LOCK(ump); 13333 if ((error = ffs_update(pvp, 1)) != 0) 13334 break; 13335 ACQUIRE_LOCK(ump); 13336 /* 13337 * If that cleared dependencies, go on to next. 13338 */ 13339 if (dap != LIST_FIRST(diraddhdp)) 13340 continue; 13341 /* 13342 * All MKDIR_PARENT dependencies and all the 13343 * NEWBLOCK pagedeps that are contained in direct 13344 * blocks were resolved by doing above ffs_update. 13345 * Pagedeps contained in indirect blocks may 13346 * require a complete sync'ing of the directory. 13347 * We are in the midst of doing a complete sync, 13348 * so if they are not resolved in this pass we 13349 * defer them for now as they will be sync'ed by 13350 * our caller shortly. 13351 */ 13352 LIST_REMOVE(dap, da_pdlist); 13353 LIST_INSERT_HEAD(&unfinished, dap, da_pdlist); 13354 continue; 13355 } 13356 /* 13357 * A newly allocated directory must have its "." and 13358 * ".." entries written out before its name can be 13359 * committed in its parent. 13360 */ 13361 inum = dap->da_newinum; 13362 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 13363 panic("flush_pagedep_deps: lost inode1"); 13364 /* 13365 * Wait for any pending journal adds to complete so we don't 13366 * cause rollbacks while syncing. 13367 */ 13368 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 13369 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 13370 == DEPCOMPLETE) { 13371 jwait(&inoref->if_list, MNT_WAIT); 13372 goto restart; 13373 } 13374 } 13375 if (dap->da_state & MKDIR_BODY) { 13376 FREE_LOCK(ump); 13377 error = get_parent_vp(pvp, mp, inum, locked_bp, 13378 diraddhdp, &unfinished, &vp); 13379 if (error != 0) 13380 break; 13381 error = flush_newblk_dep(vp, mp, 0); 13382 /* 13383 * If we still have the dependency we might need to 13384 * update the vnode to sync the new link count to 13385 * disk. 13386 */ 13387 if (error == 0 && dap == LIST_FIRST(diraddhdp)) 13388 error = ffs_update(vp, 1); 13389 vput(vp); 13390 if (error != 0) 13391 break; 13392 ACQUIRE_LOCK(ump); 13393 /* 13394 * If that cleared dependencies, go on to next. 13395 */ 13396 if (dap != LIST_FIRST(diraddhdp)) 13397 continue; 13398 if (dap->da_state & MKDIR_BODY) { 13399 inodedep_lookup(UFSTOVFS(ump), inum, 0, 13400 &inodedep); 13401 panic("flush_pagedep_deps: MKDIR_BODY " 13402 "inodedep %p dap %p vp %p", 13403 inodedep, dap, vp); 13404 } 13405 } 13406 /* 13407 * Flush the inode on which the directory entry depends. 13408 * Having accounted for MKDIR_PARENT and MKDIR_BODY above, 13409 * the only remaining dependency is that the updated inode 13410 * count must get pushed to disk. The inode has already 13411 * been pushed into its inode buffer (via VOP_UPDATE) at 13412 * the time of the reference count change. So we need only 13413 * locate that buffer, ensure that there will be no rollback 13414 * caused by a bitmap dependency, then write the inode buffer. 13415 */ 13416 retry: 13417 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 13418 panic("flush_pagedep_deps: lost inode"); 13419 /* 13420 * If the inode still has bitmap dependencies, 13421 * push them to disk. 13422 */ 13423 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) { 13424 bp = inodedep->id_bmsafemap->sm_buf; 13425 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 13426 if (bp == NULL) 13427 goto retry; 13428 FREE_LOCK(ump); 13429 if ((error = bwrite(bp)) != 0) 13430 break; 13431 ACQUIRE_LOCK(ump); 13432 if (dap != LIST_FIRST(diraddhdp)) 13433 continue; 13434 } 13435 /* 13436 * If the inode is still sitting in a buffer waiting 13437 * to be written or waiting for the link count to be 13438 * adjusted update it here to flush it to disk. 13439 */ 13440 if (dap == LIST_FIRST(diraddhdp)) { 13441 FREE_LOCK(ump); 13442 error = get_parent_vp(pvp, mp, inum, locked_bp, 13443 diraddhdp, &unfinished, &vp); 13444 if (error != 0) 13445 break; 13446 error = ffs_update(vp, 1); 13447 vput(vp); 13448 if (error) 13449 break; 13450 ACQUIRE_LOCK(ump); 13451 } 13452 /* 13453 * If we have failed to get rid of all the dependencies 13454 * then something is seriously wrong. 13455 */ 13456 if (dap == LIST_FIRST(diraddhdp)) { 13457 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep); 13458 panic("flush_pagedep_deps: failed to flush " 13459 "inodedep %p ino %ju dap %p", 13460 inodedep, (uintmax_t)inum, dap); 13461 } 13462 } 13463 if (error) 13464 ACQUIRE_LOCK(ump); 13465 while ((dap = LIST_FIRST(&unfinished)) != NULL) { 13466 LIST_REMOVE(dap, da_pdlist); 13467 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist); 13468 } 13469 return (error); 13470 } 13471 13472 /* 13473 * A large burst of file addition or deletion activity can drive the 13474 * memory load excessively high. First attempt to slow things down 13475 * using the techniques below. If that fails, this routine requests 13476 * the offending operations to fall back to running synchronously 13477 * until the memory load returns to a reasonable level. 13478 */ 13479 int 13480 softdep_slowdown(struct vnode *vp) 13481 { 13482 struct ufsmount *ump; 13483 int jlow; 13484 int max_softdeps_hard; 13485 13486 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 13487 ("softdep_slowdown called on non-softdep filesystem")); 13488 ump = VFSTOUFS(vp->v_mount); 13489 ACQUIRE_LOCK(ump); 13490 jlow = 0; 13491 /* 13492 * Check for journal space if needed. 13493 */ 13494 if (DOINGSUJ(vp)) { 13495 if (journal_space(ump, 0) == 0) 13496 jlow = 1; 13497 } 13498 /* 13499 * If the system is under its limits and our filesystem is 13500 * not responsible for more than our share of the usage and 13501 * we are not low on journal space, then no need to slow down. 13502 */ 13503 max_softdeps_hard = max_softdeps * 11 / 10; 13504 if (dep_current[D_DIRREM] < max_softdeps_hard / 2 && 13505 dep_current[D_INODEDEP] < max_softdeps_hard && 13506 dep_current[D_INDIRDEP] < max_softdeps_hard / 1000 && 13507 dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0 && 13508 ump->softdep_curdeps[D_DIRREM] < 13509 (max_softdeps_hard / 2) / stat_flush_threads && 13510 ump->softdep_curdeps[D_INODEDEP] < 13511 max_softdeps_hard / stat_flush_threads && 13512 ump->softdep_curdeps[D_INDIRDEP] < 13513 (max_softdeps_hard / 1000) / stat_flush_threads && 13514 ump->softdep_curdeps[D_FREEBLKS] < 13515 max_softdeps_hard / stat_flush_threads) { 13516 FREE_LOCK(ump); 13517 return (0); 13518 } 13519 /* 13520 * If the journal is low or our filesystem is over its limit 13521 * then speedup the cleanup. 13522 */ 13523 if (ump->softdep_curdeps[D_INDIRDEP] < 13524 (max_softdeps_hard / 1000) / stat_flush_threads || jlow) 13525 softdep_speedup(ump); 13526 stat_sync_limit_hit += 1; 13527 FREE_LOCK(ump); 13528 /* 13529 * We only slow down the rate at which new dependencies are 13530 * generated if we are not using journaling. With journaling, 13531 * the cleanup should always be sufficient to keep things 13532 * under control. 13533 */ 13534 if (DOINGSUJ(vp)) 13535 return (0); 13536 return (1); 13537 } 13538 13539 static int 13540 softdep_request_cleanup_filter(struct vnode *vp, void *arg __unused) 13541 { 13542 return ((vp->v_iflag & VI_OWEINACT) != 0 && vp->v_usecount == 0 && 13543 ((vp->v_vflag & VV_NOSYNC) != 0 || VTOI(vp)->i_effnlink == 0)); 13544 } 13545 13546 static void 13547 softdep_request_cleanup_inactivate(struct mount *mp) 13548 { 13549 struct vnode *vp, *mvp; 13550 int error; 13551 13552 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, softdep_request_cleanup_filter, 13553 NULL) { 13554 vholdl(vp); 13555 vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY); 13556 VI_LOCK(vp); 13557 if (IS_UFS(vp) && vp->v_usecount == 0) { 13558 while ((vp->v_iflag & VI_OWEINACT) != 0) { 13559 error = vinactive(vp); 13560 if (error != 0 && error != ERELOOKUP) 13561 break; 13562 } 13563 atomic_add_int(&stat_delayed_inact, 1); 13564 } 13565 VOP_UNLOCK(vp); 13566 vdropl(vp); 13567 } 13568 } 13569 13570 /* 13571 * Called by the allocation routines when they are about to fail 13572 * in the hope that we can free up the requested resource (inodes 13573 * or disk space). 13574 * 13575 * First check to see if the work list has anything on it. If it has, 13576 * clean up entries until we successfully free the requested resource. 13577 * Because this process holds inodes locked, we cannot handle any remove 13578 * requests that might block on a locked inode as that could lead to 13579 * deadlock. If the worklist yields none of the requested resource, 13580 * start syncing out vnodes to free up the needed space. 13581 */ 13582 int 13583 softdep_request_cleanup( 13584 struct fs *fs, 13585 struct vnode *vp, 13586 struct ucred *cred, 13587 int resource) 13588 { 13589 struct ufsmount *ump; 13590 struct mount *mp; 13591 long starttime; 13592 ufs2_daddr_t needed; 13593 int error, failed_vnode; 13594 13595 /* 13596 * If we are being called because of a process doing a 13597 * copy-on-write, then it is not safe to process any 13598 * worklist items as we will recurse into the copyonwrite 13599 * routine. This will result in an incoherent snapshot. 13600 * If the vnode that we hold is a snapshot, we must avoid 13601 * handling other resources that could cause deadlock. 13602 */ 13603 if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp))) 13604 return (0); 13605 13606 if (resource == FLUSH_BLOCKS_WAIT) 13607 stat_cleanup_blkrequests += 1; 13608 else 13609 stat_cleanup_inorequests += 1; 13610 13611 mp = vp->v_mount; 13612 ump = VFSTOUFS(mp); 13613 mtx_assert(UFS_MTX(ump), MA_OWNED); 13614 UFS_UNLOCK(ump); 13615 error = ffs_update(vp, 1); 13616 if (error != 0 || MOUNTEDSOFTDEP(mp) == 0) { 13617 UFS_LOCK(ump); 13618 return (0); 13619 } 13620 /* 13621 * If we are in need of resources, start by cleaning up 13622 * any block removals associated with our inode. 13623 */ 13624 ACQUIRE_LOCK(ump); 13625 process_removes(vp); 13626 process_truncates(vp); 13627 FREE_LOCK(ump); 13628 /* 13629 * Now clean up at least as many resources as we will need. 13630 * 13631 * When requested to clean up inodes, the number that are needed 13632 * is set by the number of simultaneous writers (mnt_writeopcount) 13633 * plus a bit of slop (2) in case some more writers show up while 13634 * we are cleaning. 13635 * 13636 * When requested to free up space, the amount of space that 13637 * we need is enough blocks to allocate a full-sized segment 13638 * (fs_contigsumsize). The number of such segments that will 13639 * be needed is set by the number of simultaneous writers 13640 * (mnt_writeopcount) plus a bit of slop (2) in case some more 13641 * writers show up while we are cleaning. 13642 * 13643 * Additionally, if we are unpriviledged and allocating space, 13644 * we need to ensure that we clean up enough blocks to get the 13645 * needed number of blocks over the threshold of the minimum 13646 * number of blocks required to be kept free by the filesystem 13647 * (fs_minfree). 13648 */ 13649 if (resource == FLUSH_INODES_WAIT) { 13650 needed = vfs_mount_fetch_counter(vp->v_mount, 13651 MNT_COUNT_WRITEOPCOUNT) + 2; 13652 } else if (resource == FLUSH_BLOCKS_WAIT) { 13653 needed = (vfs_mount_fetch_counter(vp->v_mount, 13654 MNT_COUNT_WRITEOPCOUNT) + 2) * fs->fs_contigsumsize; 13655 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE)) 13656 needed += fragstoblks(fs, 13657 roundup((fs->fs_dsize * fs->fs_minfree / 100) - 13658 fs->fs_cstotal.cs_nffree, fs->fs_frag)); 13659 } else { 13660 printf("softdep_request_cleanup: Unknown resource type %d\n", 13661 resource); 13662 UFS_LOCK(ump); 13663 return (0); 13664 } 13665 starttime = time_second; 13666 retry: 13667 if (resource == FLUSH_BLOCKS_WAIT && 13668 fs->fs_cstotal.cs_nbfree <= needed) 13669 softdep_send_speedup(ump, needed * fs->fs_bsize, 13670 BIO_SPEEDUP_TRIM); 13671 if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 && 13672 fs->fs_cstotal.cs_nbfree <= needed) || 13673 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13674 fs->fs_cstotal.cs_nifree <= needed)) { 13675 ACQUIRE_LOCK(ump); 13676 if (ump->softdep_on_worklist > 0 && 13677 process_worklist_item(UFSTOVFS(ump), 13678 ump->softdep_on_worklist, LK_NOWAIT) != 0) 13679 stat_worklist_push += 1; 13680 FREE_LOCK(ump); 13681 } 13682 13683 /* 13684 * Check that there are vnodes pending inactivation. As they 13685 * have been unlinked, inactivating them will free up their 13686 * inodes. 13687 */ 13688 ACQUIRE_LOCK(ump); 13689 if (resource == FLUSH_INODES_WAIT && 13690 fs->fs_cstotal.cs_nifree <= needed && 13691 fs->fs_pendinginodes <= needed) { 13692 if ((ump->um_softdep->sd_flags & FLUSH_DI_ACTIVE) == 0) { 13693 ump->um_softdep->sd_flags |= FLUSH_DI_ACTIVE; 13694 FREE_LOCK(ump); 13695 softdep_request_cleanup_inactivate(mp); 13696 ACQUIRE_LOCK(ump); 13697 ump->um_softdep->sd_flags &= ~FLUSH_DI_ACTIVE; 13698 wakeup(&ump->um_softdep->sd_flags); 13699 } else { 13700 while ((ump->um_softdep->sd_flags & 13701 FLUSH_DI_ACTIVE) != 0) { 13702 msleep(&ump->um_softdep->sd_flags, 13703 LOCK_PTR(ump), PVM, "ffsvina", hz); 13704 } 13705 } 13706 } 13707 FREE_LOCK(ump); 13708 13709 /* 13710 * If we still need resources and there are no more worklist 13711 * entries to process to obtain them, we have to start flushing 13712 * the dirty vnodes to force the release of additional requests 13713 * to the worklist that we can then process to reap addition 13714 * resources. We walk the vnodes associated with the mount point 13715 * until we get the needed worklist requests that we can reap. 13716 * 13717 * If there are several threads all needing to clean the same 13718 * mount point, only one is allowed to walk the mount list. 13719 * When several threads all try to walk the same mount list, 13720 * they end up competing with each other and often end up in 13721 * livelock. This approach ensures that forward progress is 13722 * made at the cost of occational ENOSPC errors being returned 13723 * that might otherwise have been avoided. 13724 */ 13725 error = 1; 13726 if ((resource == FLUSH_BLOCKS_WAIT && 13727 fs->fs_cstotal.cs_nbfree <= needed) || 13728 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13729 fs->fs_cstotal.cs_nifree <= needed)) { 13730 ACQUIRE_LOCK(ump); 13731 if ((ump->um_softdep->sd_flags & FLUSH_RC_ACTIVE) == 0) { 13732 ump->um_softdep->sd_flags |= FLUSH_RC_ACTIVE; 13733 FREE_LOCK(ump); 13734 failed_vnode = softdep_request_cleanup_flush(mp, ump); 13735 ACQUIRE_LOCK(ump); 13736 ump->um_softdep->sd_flags &= ~FLUSH_RC_ACTIVE; 13737 wakeup(&ump->um_softdep->sd_flags); 13738 FREE_LOCK(ump); 13739 if (ump->softdep_on_worklist > 0) { 13740 stat_cleanup_retries += 1; 13741 if (!failed_vnode) 13742 goto retry; 13743 } 13744 } else { 13745 while ((ump->um_softdep->sd_flags & 13746 FLUSH_RC_ACTIVE) != 0) { 13747 msleep(&ump->um_softdep->sd_flags, 13748 LOCK_PTR(ump), PVM, "ffsrca", hz); 13749 } 13750 FREE_LOCK(ump); 13751 error = 0; 13752 } 13753 stat_cleanup_failures += 1; 13754 } 13755 if (time_second - starttime > stat_cleanup_high_delay) 13756 stat_cleanup_high_delay = time_second - starttime; 13757 UFS_LOCK(ump); 13758 return (error); 13759 } 13760 13761 /* 13762 * Scan the vnodes for the specified mount point flushing out any 13763 * vnodes that can be locked without waiting. Finally, try to flush 13764 * the device associated with the mount point if it can be locked 13765 * without waiting. 13766 * 13767 * We return 0 if we were able to lock every vnode in our scan. 13768 * If we had to skip one or more vnodes, we return 1. 13769 */ 13770 static int 13771 softdep_request_cleanup_flush(struct mount *mp, struct ufsmount *ump) 13772 { 13773 struct thread *td; 13774 struct vnode *lvp, *mvp; 13775 int failed_vnode; 13776 13777 failed_vnode = 0; 13778 td = curthread; 13779 MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) { 13780 if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) { 13781 VI_UNLOCK(lvp); 13782 continue; 13783 } 13784 if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT) != 0) { 13785 failed_vnode = 1; 13786 continue; 13787 } 13788 if (lvp->v_vflag & VV_NOSYNC) { /* unlinked */ 13789 vput(lvp); 13790 continue; 13791 } 13792 (void) ffs_syncvnode(lvp, MNT_NOWAIT, 0); 13793 vput(lvp); 13794 } 13795 lvp = ump->um_devvp; 13796 if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) { 13797 VOP_FSYNC(lvp, MNT_NOWAIT, td); 13798 VOP_UNLOCK(lvp); 13799 } 13800 return (failed_vnode); 13801 } 13802 13803 static bool 13804 softdep_excess_items(struct ufsmount *ump, int item) 13805 { 13806 13807 KASSERT(item >= 0 && item < D_LAST, ("item %d", item)); 13808 return (dep_current[item] > max_softdeps && 13809 ump->softdep_curdeps[item] > max_softdeps / 13810 stat_flush_threads); 13811 } 13812 13813 static void 13814 schedule_cleanup(struct mount *mp) 13815 { 13816 struct ufsmount *ump; 13817 struct thread *td; 13818 13819 ump = VFSTOUFS(mp); 13820 LOCK_OWNED(ump); 13821 FREE_LOCK(ump); 13822 td = curthread; 13823 if ((td->td_pflags & TDP_KTHREAD) != 0 && 13824 (td->td_proc->p_flag2 & P2_AST_SU) == 0) { 13825 /* 13826 * No ast is delivered to kernel threads, so nobody 13827 * would deref the mp. Some kernel threads 13828 * explicitly check for AST, e.g. NFS daemon does 13829 * this in the serving loop. 13830 */ 13831 return; 13832 } 13833 if (td->td_su != NULL) 13834 vfs_rel(td->td_su); 13835 vfs_ref(mp); 13836 td->td_su = mp; 13837 ast_sched(td, TDA_UFS); 13838 } 13839 13840 static void 13841 softdep_ast_cleanup_proc(struct thread *td, int ast __unused) 13842 { 13843 struct mount *mp; 13844 struct ufsmount *ump; 13845 int error; 13846 bool req; 13847 13848 while ((mp = td->td_su) != NULL) { 13849 td->td_su = NULL; 13850 error = vfs_busy(mp, MBF_NOWAIT); 13851 vfs_rel(mp); 13852 if (error != 0) 13853 return; 13854 if (ffs_own_mount(mp) && MOUNTEDSOFTDEP(mp)) { 13855 ump = VFSTOUFS(mp); 13856 for (;;) { 13857 req = false; 13858 ACQUIRE_LOCK(ump); 13859 if (softdep_excess_items(ump, D_INODEDEP)) { 13860 req = true; 13861 request_cleanup(mp, FLUSH_INODES); 13862 } 13863 if (softdep_excess_items(ump, D_DIRREM)) { 13864 req = true; 13865 request_cleanup(mp, FLUSH_BLOCKS); 13866 } 13867 FREE_LOCK(ump); 13868 if (softdep_excess_items(ump, D_NEWBLK) || 13869 softdep_excess_items(ump, D_ALLOCDIRECT) || 13870 softdep_excess_items(ump, D_ALLOCINDIR)) { 13871 error = vn_start_write(NULL, &mp, 13872 V_WAIT); 13873 if (error == 0) { 13874 req = true; 13875 VFS_SYNC(mp, MNT_WAIT); 13876 vn_finished_write(mp); 13877 } 13878 } 13879 if ((td->td_pflags & TDP_KTHREAD) != 0 || !req) 13880 break; 13881 } 13882 } 13883 vfs_unbusy(mp); 13884 } 13885 if ((mp = td->td_su) != NULL) { 13886 td->td_su = NULL; 13887 vfs_rel(mp); 13888 } 13889 } 13890 13891 /* 13892 * If memory utilization has gotten too high, deliberately slow things 13893 * down and speed up the I/O processing. 13894 */ 13895 static int 13896 request_cleanup(struct mount *mp, int resource) 13897 { 13898 struct thread *td = curthread; 13899 struct ufsmount *ump; 13900 13901 ump = VFSTOUFS(mp); 13902 LOCK_OWNED(ump); 13903 /* 13904 * We never hold up the filesystem syncer or buf daemon. 13905 */ 13906 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF)) 13907 return (0); 13908 /* 13909 * First check to see if the work list has gotten backlogged. 13910 * If it has, co-opt this process to help clean up two entries. 13911 * Because this process may hold inodes locked, we cannot 13912 * handle any remove requests that might block on a locked 13913 * inode as that could lead to deadlock. We set TDP_SOFTDEP 13914 * to avoid recursively processing the worklist. 13915 */ 13916 if (ump->softdep_on_worklist > max_softdeps / 10) { 13917 td->td_pflags |= TDP_SOFTDEP; 13918 process_worklist_item(mp, 2, LK_NOWAIT); 13919 td->td_pflags &= ~TDP_SOFTDEP; 13920 stat_worklist_push += 2; 13921 return(1); 13922 } 13923 /* 13924 * Next, we attempt to speed up the syncer process. If that 13925 * is successful, then we allow the process to continue. 13926 */ 13927 if (softdep_speedup(ump) && 13928 resource != FLUSH_BLOCKS_WAIT && 13929 resource != FLUSH_INODES_WAIT) 13930 return(0); 13931 /* 13932 * If we are resource constrained on inode dependencies, try 13933 * flushing some dirty inodes. Otherwise, we are constrained 13934 * by file deletions, so try accelerating flushes of directories 13935 * with removal dependencies. We would like to do the cleanup 13936 * here, but we probably hold an inode locked at this point and 13937 * that might deadlock against one that we try to clean. So, 13938 * the best that we can do is request the syncer daemon to do 13939 * the cleanup for us. 13940 */ 13941 switch (resource) { 13942 case FLUSH_INODES: 13943 case FLUSH_INODES_WAIT: 13944 ACQUIRE_GBLLOCK(&lk); 13945 stat_ino_limit_push += 1; 13946 req_clear_inodedeps += 1; 13947 FREE_GBLLOCK(&lk); 13948 stat_countp = &stat_ino_limit_hit; 13949 break; 13950 13951 case FLUSH_BLOCKS: 13952 case FLUSH_BLOCKS_WAIT: 13953 ACQUIRE_GBLLOCK(&lk); 13954 stat_blk_limit_push += 1; 13955 req_clear_remove += 1; 13956 FREE_GBLLOCK(&lk); 13957 stat_countp = &stat_blk_limit_hit; 13958 break; 13959 13960 default: 13961 panic("request_cleanup: unknown type"); 13962 } 13963 /* 13964 * Hopefully the syncer daemon will catch up and awaken us. 13965 * We wait at most tickdelay before proceeding in any case. 13966 */ 13967 ACQUIRE_GBLLOCK(&lk); 13968 FREE_LOCK(ump); 13969 proc_waiting += 1; 13970 if (callout_pending(&softdep_callout) == FALSE) 13971 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2, 13972 pause_timer, 0); 13973 13974 if ((td->td_pflags & TDP_KTHREAD) == 0) 13975 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0); 13976 proc_waiting -= 1; 13977 FREE_GBLLOCK(&lk); 13978 ACQUIRE_LOCK(ump); 13979 return (1); 13980 } 13981 13982 /* 13983 * Awaken processes pausing in request_cleanup and clear proc_waiting 13984 * to indicate that there is no longer a timer running. Pause_timer 13985 * will be called with the global softdep mutex (&lk) locked. 13986 */ 13987 static void 13988 pause_timer(void *arg) 13989 { 13990 13991 GBLLOCK_OWNED(&lk); 13992 /* 13993 * The callout_ API has acquired mtx and will hold it around this 13994 * function call. 13995 */ 13996 *stat_countp += proc_waiting; 13997 wakeup(&proc_waiting); 13998 } 13999 14000 /* 14001 * If requested, try removing inode or removal dependencies. 14002 */ 14003 static void 14004 check_clear_deps(struct mount *mp) 14005 { 14006 struct ufsmount *ump; 14007 bool suj_susp; 14008 14009 /* 14010 * Tell the lower layers that any TRIM or WRITE transactions that have 14011 * been delayed for performance reasons should proceed to help alleviate 14012 * the shortage faster. The race between checking req_* and the softdep 14013 * mutex (lk) is fine since this is an advisory operation that at most 14014 * causes deferred work to be done sooner. 14015 */ 14016 ump = VFSTOUFS(mp); 14017 suj_susp = ump->um_softdep->sd_jblocks != NULL && 14018 ump->softdep_jblocks->jb_suspended; 14019 if (req_clear_remove || req_clear_inodedeps || suj_susp) { 14020 FREE_LOCK(ump); 14021 softdep_send_speedup(ump, 0, BIO_SPEEDUP_TRIM | BIO_SPEEDUP_WRITE); 14022 ACQUIRE_LOCK(ump); 14023 } 14024 14025 /* 14026 * If we are suspended, it may be because of our using 14027 * too many inodedeps, so help clear them out. 14028 */ 14029 if (suj_susp) 14030 clear_inodedeps(mp); 14031 14032 /* 14033 * General requests for cleanup of backed up dependencies 14034 */ 14035 ACQUIRE_GBLLOCK(&lk); 14036 if (req_clear_inodedeps) { 14037 req_clear_inodedeps -= 1; 14038 FREE_GBLLOCK(&lk); 14039 clear_inodedeps(mp); 14040 ACQUIRE_GBLLOCK(&lk); 14041 wakeup(&proc_waiting); 14042 } 14043 if (req_clear_remove) { 14044 req_clear_remove -= 1; 14045 FREE_GBLLOCK(&lk); 14046 clear_remove(mp); 14047 ACQUIRE_GBLLOCK(&lk); 14048 wakeup(&proc_waiting); 14049 } 14050 FREE_GBLLOCK(&lk); 14051 } 14052 14053 /* 14054 * Flush out a directory with at least one removal dependency in an effort to 14055 * reduce the number of dirrem, freefile, and freeblks dependency structures. 14056 */ 14057 static void 14058 clear_remove(struct mount *mp) 14059 { 14060 struct pagedep_hashhead *pagedephd; 14061 struct pagedep *pagedep; 14062 struct ufsmount *ump; 14063 struct vnode *vp; 14064 struct bufobj *bo; 14065 int error, cnt; 14066 ino_t ino; 14067 14068 ump = VFSTOUFS(mp); 14069 LOCK_OWNED(ump); 14070 14071 for (cnt = 0; cnt <= ump->pagedep_hash_size; cnt++) { 14072 pagedephd = &ump->pagedep_hashtbl[ump->pagedep_nextclean++]; 14073 if (ump->pagedep_nextclean > ump->pagedep_hash_size) 14074 ump->pagedep_nextclean = 0; 14075 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 14076 if (LIST_EMPTY(&pagedep->pd_dirremhd)) 14077 continue; 14078 ino = pagedep->pd_ino; 14079 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 14080 continue; 14081 FREE_LOCK(ump); 14082 14083 /* 14084 * Let unmount clear deps 14085 */ 14086 error = vfs_busy(mp, MBF_NOWAIT); 14087 if (error != 0) 14088 goto finish_write; 14089 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 14090 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP); 14091 vfs_unbusy(mp); 14092 if (error != 0) { 14093 softdep_error("clear_remove: vget", error); 14094 goto finish_write; 14095 } 14096 MPASS(VTOI(vp)->i_mode != 0); 14097 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 14098 softdep_error("clear_remove: fsync", error); 14099 bo = &vp->v_bufobj; 14100 BO_LOCK(bo); 14101 drain_output(vp); 14102 BO_UNLOCK(bo); 14103 vput(vp); 14104 finish_write: 14105 vn_finished_write(mp); 14106 ACQUIRE_LOCK(ump); 14107 return; 14108 } 14109 } 14110 } 14111 14112 /* 14113 * Clear out a block of dirty inodes in an effort to reduce 14114 * the number of inodedep dependency structures. 14115 */ 14116 static void 14117 clear_inodedeps(struct mount *mp) 14118 { 14119 struct inodedep_hashhead *inodedephd; 14120 struct inodedep *inodedep; 14121 struct ufsmount *ump; 14122 struct vnode *vp; 14123 struct fs *fs; 14124 int error, cnt; 14125 ino_t firstino, lastino, ino; 14126 14127 ump = VFSTOUFS(mp); 14128 fs = ump->um_fs; 14129 LOCK_OWNED(ump); 14130 /* 14131 * Pick a random inode dependency to be cleared. 14132 * We will then gather up all the inodes in its block 14133 * that have dependencies and flush them out. 14134 */ 14135 for (cnt = 0; cnt <= ump->inodedep_hash_size; cnt++) { 14136 inodedephd = &ump->inodedep_hashtbl[ump->inodedep_nextclean++]; 14137 if (ump->inodedep_nextclean > ump->inodedep_hash_size) 14138 ump->inodedep_nextclean = 0; 14139 if ((inodedep = LIST_FIRST(inodedephd)) != NULL) 14140 break; 14141 } 14142 if (inodedep == NULL) 14143 return; 14144 /* 14145 * Find the last inode in the block with dependencies. 14146 */ 14147 firstino = rounddown2(inodedep->id_ino, INOPB(fs)); 14148 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--) 14149 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0) 14150 break; 14151 /* 14152 * Asynchronously push all but the last inode with dependencies. 14153 * Synchronously push the last inode with dependencies to ensure 14154 * that the inode block gets written to free up the inodedeps. 14155 */ 14156 for (ino = firstino; ino <= lastino; ino++) { 14157 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 14158 continue; 14159 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 14160 continue; 14161 FREE_LOCK(ump); 14162 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */ 14163 if (error != 0) { 14164 vn_finished_write(mp); 14165 ACQUIRE_LOCK(ump); 14166 return; 14167 } 14168 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 14169 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP)) != 0) { 14170 softdep_error("clear_inodedeps: vget", error); 14171 vfs_unbusy(mp); 14172 vn_finished_write(mp); 14173 ACQUIRE_LOCK(ump); 14174 return; 14175 } 14176 vfs_unbusy(mp); 14177 if (VTOI(vp)->i_mode == 0) { 14178 vgone(vp); 14179 } else if (ino == lastino) { 14180 do { 14181 error = ffs_syncvnode(vp, MNT_WAIT, 0); 14182 } while (error == ERELOOKUP); 14183 if (error != 0) 14184 softdep_error("clear_inodedeps: fsync1", error); 14185 } else { 14186 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 14187 softdep_error("clear_inodedeps: fsync2", error); 14188 BO_LOCK(&vp->v_bufobj); 14189 drain_output(vp); 14190 BO_UNLOCK(&vp->v_bufobj); 14191 } 14192 vput(vp); 14193 vn_finished_write(mp); 14194 ACQUIRE_LOCK(ump); 14195 } 14196 } 14197 14198 void 14199 softdep_buf_append(struct buf *bp, struct workhead *wkhd) 14200 { 14201 struct worklist *wk; 14202 struct ufsmount *ump; 14203 14204 if ((wk = LIST_FIRST(wkhd)) == NULL) 14205 return; 14206 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 14207 ("softdep_buf_append called on non-softdep filesystem")); 14208 ump = VFSTOUFS(wk->wk_mp); 14209 ACQUIRE_LOCK(ump); 14210 while ((wk = LIST_FIRST(wkhd)) != NULL) { 14211 WORKLIST_REMOVE(wk); 14212 WORKLIST_INSERT(&bp->b_dep, wk); 14213 } 14214 FREE_LOCK(ump); 14215 14216 } 14217 14218 void 14219 softdep_inode_append( 14220 struct inode *ip, 14221 struct ucred *cred, 14222 struct workhead *wkhd) 14223 { 14224 struct buf *bp; 14225 struct fs *fs; 14226 struct ufsmount *ump; 14227 int error; 14228 14229 ump = ITOUMP(ip); 14230 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 14231 ("softdep_inode_append called on non-softdep filesystem")); 14232 fs = ump->um_fs; 14233 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 14234 (int)fs->fs_bsize, cred, &bp); 14235 if (error) { 14236 bqrelse(bp); 14237 softdep_freework(wkhd); 14238 return; 14239 } 14240 softdep_buf_append(bp, wkhd); 14241 bqrelse(bp); 14242 } 14243 14244 void 14245 softdep_freework(struct workhead *wkhd) 14246 { 14247 struct worklist *wk; 14248 struct ufsmount *ump; 14249 14250 if ((wk = LIST_FIRST(wkhd)) == NULL) 14251 return; 14252 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 14253 ("softdep_freework called on non-softdep filesystem")); 14254 ump = VFSTOUFS(wk->wk_mp); 14255 ACQUIRE_LOCK(ump); 14256 handle_jwork(wkhd); 14257 FREE_LOCK(ump); 14258 } 14259 14260 static struct ufsmount * 14261 softdep_bp_to_mp(struct buf *bp) 14262 { 14263 struct mount *mp; 14264 struct vnode *vp; 14265 14266 if (LIST_EMPTY(&bp->b_dep)) 14267 return (NULL); 14268 vp = bp->b_vp; 14269 KASSERT(vp != NULL, 14270 ("%s, buffer with dependencies lacks vnode", __func__)); 14271 14272 /* 14273 * The ump mount point is stable after we get a correct 14274 * pointer, since bp is locked and this prevents unmount from 14275 * proceeding. But to get to it, we cannot dereference bp->b_dep 14276 * head wk_mp, because we do not yet own SU ump lock and 14277 * workitem might be freed while dereferenced. 14278 */ 14279 retry: 14280 switch (vp->v_type) { 14281 case VCHR: 14282 VI_LOCK(vp); 14283 mp = vp->v_type == VCHR ? vp->v_rdev->si_mountpt : NULL; 14284 VI_UNLOCK(vp); 14285 if (mp == NULL) 14286 goto retry; 14287 break; 14288 case VREG: 14289 case VDIR: 14290 case VLNK: 14291 case VFIFO: 14292 case VSOCK: 14293 mp = vp->v_mount; 14294 break; 14295 case VBLK: 14296 vn_printf(vp, "softdep_bp_to_mp: unexpected block device\n"); 14297 /* FALLTHROUGH */ 14298 case VNON: 14299 case VBAD: 14300 case VMARKER: 14301 mp = NULL; 14302 break; 14303 default: 14304 vn_printf(vp, "unknown vnode type"); 14305 mp = NULL; 14306 break; 14307 } 14308 return (VFSTOUFS(mp)); 14309 } 14310 14311 /* 14312 * Function to determine if the buffer has outstanding dependencies 14313 * that will cause a roll-back if the buffer is written. If wantcount 14314 * is set, return number of dependencies, otherwise just yes or no. 14315 */ 14316 static int 14317 softdep_count_dependencies(struct buf *bp, int wantcount) 14318 { 14319 struct worklist *wk; 14320 struct ufsmount *ump; 14321 struct bmsafemap *bmsafemap; 14322 struct freework *freework; 14323 struct inodedep *inodedep; 14324 struct indirdep *indirdep; 14325 struct freeblks *freeblks; 14326 struct allocindir *aip; 14327 struct pagedep *pagedep; 14328 struct dirrem *dirrem; 14329 struct newblk *newblk; 14330 struct mkdir *mkdir; 14331 struct diradd *dap; 14332 int i, retval; 14333 14334 ump = softdep_bp_to_mp(bp); 14335 if (ump == NULL) 14336 return (0); 14337 retval = 0; 14338 ACQUIRE_LOCK(ump); 14339 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 14340 switch (wk->wk_type) { 14341 case D_INODEDEP: 14342 inodedep = WK_INODEDEP(wk); 14343 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 14344 /* bitmap allocation dependency */ 14345 retval += 1; 14346 if (!wantcount) 14347 goto out; 14348 } 14349 if (TAILQ_FIRST(&inodedep->id_inoupdt)) { 14350 /* direct block pointer dependency */ 14351 retval += 1; 14352 if (!wantcount) 14353 goto out; 14354 } 14355 if (TAILQ_FIRST(&inodedep->id_extupdt)) { 14356 /* direct block pointer dependency */ 14357 retval += 1; 14358 if (!wantcount) 14359 goto out; 14360 } 14361 if (TAILQ_FIRST(&inodedep->id_inoreflst)) { 14362 /* Add reference dependency. */ 14363 retval += 1; 14364 if (!wantcount) 14365 goto out; 14366 } 14367 continue; 14368 14369 case D_INDIRDEP: 14370 indirdep = WK_INDIRDEP(wk); 14371 14372 TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) { 14373 /* indirect truncation dependency */ 14374 retval += 1; 14375 if (!wantcount) 14376 goto out; 14377 } 14378 14379 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 14380 /* indirect block pointer dependency */ 14381 retval += 1; 14382 if (!wantcount) 14383 goto out; 14384 } 14385 continue; 14386 14387 case D_PAGEDEP: 14388 pagedep = WK_PAGEDEP(wk); 14389 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) { 14390 if (LIST_FIRST(&dirrem->dm_jremrefhd)) { 14391 /* Journal remove ref dependency. */ 14392 retval += 1; 14393 if (!wantcount) 14394 goto out; 14395 } 14396 } 14397 for (i = 0; i < DAHASHSZ; i++) { 14398 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 14399 /* directory entry dependency */ 14400 retval += 1; 14401 if (!wantcount) 14402 goto out; 14403 } 14404 } 14405 continue; 14406 14407 case D_BMSAFEMAP: 14408 bmsafemap = WK_BMSAFEMAP(wk); 14409 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) { 14410 /* Add reference dependency. */ 14411 retval += 1; 14412 if (!wantcount) 14413 goto out; 14414 } 14415 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) { 14416 /* Allocate block dependency. */ 14417 retval += 1; 14418 if (!wantcount) 14419 goto out; 14420 } 14421 continue; 14422 14423 case D_FREEBLKS: 14424 freeblks = WK_FREEBLKS(wk); 14425 if (LIST_FIRST(&freeblks->fb_jblkdephd)) { 14426 /* Freeblk journal dependency. */ 14427 retval += 1; 14428 if (!wantcount) 14429 goto out; 14430 } 14431 continue; 14432 14433 case D_ALLOCDIRECT: 14434 case D_ALLOCINDIR: 14435 newblk = WK_NEWBLK(wk); 14436 if (newblk->nb_jnewblk) { 14437 /* Journal allocate dependency. */ 14438 retval += 1; 14439 if (!wantcount) 14440 goto out; 14441 } 14442 continue; 14443 14444 case D_MKDIR: 14445 mkdir = WK_MKDIR(wk); 14446 if (mkdir->md_jaddref) { 14447 /* Journal reference dependency. */ 14448 retval += 1; 14449 if (!wantcount) 14450 goto out; 14451 } 14452 continue; 14453 14454 case D_FREEWORK: 14455 case D_FREEDEP: 14456 case D_JSEGDEP: 14457 case D_JSEG: 14458 case D_SBDEP: 14459 /* never a dependency on these blocks */ 14460 continue; 14461 14462 default: 14463 panic("softdep_count_dependencies: Unexpected type %s", 14464 TYPENAME(wk->wk_type)); 14465 /* NOTREACHED */ 14466 } 14467 } 14468 out: 14469 FREE_LOCK(ump); 14470 return (retval); 14471 } 14472 14473 /* 14474 * Acquire exclusive access to a buffer. 14475 * Must be called with a locked mtx parameter. 14476 * Return acquired buffer or NULL on failure. 14477 */ 14478 static struct buf * 14479 getdirtybuf(struct buf *bp, 14480 struct rwlock *lock, 14481 int waitfor) 14482 { 14483 int error; 14484 14485 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) { 14486 if (waitfor != MNT_WAIT) 14487 return (NULL); 14488 error = BUF_LOCK(bp, 14489 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock); 14490 /* 14491 * Even if we successfully acquire bp here, we have dropped 14492 * lock, which may violates our guarantee. 14493 */ 14494 if (error == 0) 14495 BUF_UNLOCK(bp); 14496 else if (error != ENOLCK) 14497 panic("getdirtybuf: inconsistent lock: %d", error); 14498 rw_wlock(lock); 14499 return (NULL); 14500 } 14501 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 14502 if (lock != BO_LOCKPTR(bp->b_bufobj) && waitfor == MNT_WAIT) { 14503 rw_wunlock(lock); 14504 BO_LOCK(bp->b_bufobj); 14505 BUF_UNLOCK(bp); 14506 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 14507 bp->b_vflags |= BV_BKGRDWAIT; 14508 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), 14509 PRIBIO | PDROP, "getbuf", 0); 14510 } else 14511 BO_UNLOCK(bp->b_bufobj); 14512 rw_wlock(lock); 14513 return (NULL); 14514 } 14515 BUF_UNLOCK(bp); 14516 if (waitfor != MNT_WAIT) 14517 return (NULL); 14518 #ifdef DEBUG_VFS_LOCKS 14519 if (bp->b_vp->v_type != VCHR) 14520 ASSERT_BO_WLOCKED(bp->b_bufobj); 14521 #endif 14522 bp->b_vflags |= BV_BKGRDWAIT; 14523 rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0); 14524 return (NULL); 14525 } 14526 if ((bp->b_flags & B_DELWRI) == 0) { 14527 BUF_UNLOCK(bp); 14528 return (NULL); 14529 } 14530 bremfree(bp); 14531 return (bp); 14532 } 14533 14534 /* 14535 * Check if it is safe to suspend the file system now. On entry, 14536 * the vnode interlock for devvp should be held. Return 0 with 14537 * the mount interlock held if the file system can be suspended now, 14538 * otherwise return EAGAIN with the mount interlock held. 14539 */ 14540 int 14541 softdep_check_suspend(struct mount *mp, 14542 struct vnode *devvp, 14543 int softdep_depcnt, 14544 int softdep_accdepcnt, 14545 int secondary_writes, 14546 int secondary_accwrites) 14547 { 14548 struct buf *bp; 14549 struct bufobj *bo; 14550 struct ufsmount *ump; 14551 struct inodedep *inodedep; 14552 struct indirdep *indirdep; 14553 struct worklist *wk, *nextwk; 14554 int error, unlinked; 14555 14556 bo = &devvp->v_bufobj; 14557 ASSERT_BO_WLOCKED(bo); 14558 14559 /* 14560 * If we are not running with soft updates, then we need only 14561 * deal with secondary writes as we try to suspend. 14562 */ 14563 if (MOUNTEDSOFTDEP(mp) == 0) { 14564 MNT_ILOCK(mp); 14565 while (mp->mnt_secondary_writes != 0) { 14566 BO_UNLOCK(bo); 14567 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 14568 (PUSER - 1) | PDROP, "secwr", 0); 14569 BO_LOCK(bo); 14570 MNT_ILOCK(mp); 14571 } 14572 14573 /* 14574 * Reasons for needing more work before suspend: 14575 * - Dirty buffers on devvp. 14576 * - Secondary writes occurred after start of vnode sync loop 14577 */ 14578 error = 0; 14579 if (bo->bo_numoutput > 0 || 14580 bo->bo_dirty.bv_cnt > 0 || 14581 secondary_writes != 0 || 14582 mp->mnt_secondary_writes != 0 || 14583 secondary_accwrites != mp->mnt_secondary_accwrites) 14584 error = EAGAIN; 14585 BO_UNLOCK(bo); 14586 return (error); 14587 } 14588 14589 /* 14590 * If we are running with soft updates, then we need to coordinate 14591 * with them as we try to suspend. 14592 */ 14593 ump = VFSTOUFS(mp); 14594 for (;;) { 14595 if (!TRY_ACQUIRE_LOCK(ump)) { 14596 BO_UNLOCK(bo); 14597 ACQUIRE_LOCK(ump); 14598 FREE_LOCK(ump); 14599 BO_LOCK(bo); 14600 continue; 14601 } 14602 MNT_ILOCK(mp); 14603 if (mp->mnt_secondary_writes != 0) { 14604 FREE_LOCK(ump); 14605 BO_UNLOCK(bo); 14606 msleep(&mp->mnt_secondary_writes, 14607 MNT_MTX(mp), 14608 (PUSER - 1) | PDROP, "secwr", 0); 14609 BO_LOCK(bo); 14610 continue; 14611 } 14612 break; 14613 } 14614 14615 unlinked = 0; 14616 if (MOUNTEDSUJ(mp)) { 14617 for (inodedep = TAILQ_FIRST(&ump->softdep_unlinked); 14618 inodedep != NULL; 14619 inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 14620 if ((inodedep->id_state & (UNLINKED | UNLINKLINKS | 14621 UNLINKONLIST)) != (UNLINKED | UNLINKLINKS | 14622 UNLINKONLIST) || 14623 !check_inodedep_free(inodedep)) 14624 continue; 14625 unlinked++; 14626 } 14627 } 14628 14629 /* 14630 * XXX Check for orphaned indirdep dependency structures. 14631 * 14632 * During forcible unmount after a disk failure there is a 14633 * bug that causes one or more indirdep dependency structures 14634 * to fail to be deallocated. We check for them here and clean 14635 * them up so that the unmount can succeed. 14636 */ 14637 if ((ump->um_flags & UM_FSFAIL_CLEANUP) != 0 && ump->softdep_deps > 0 && 14638 ump->softdep_deps == ump->softdep_curdeps[D_INDIRDEP]) { 14639 LIST_FOREACH_SAFE(wk, &ump->softdep_alldeps[D_INDIRDEP], 14640 wk_all, nextwk) { 14641 indirdep = WK_INDIRDEP(wk); 14642 if ((indirdep->ir_state & (GOINGAWAY | DEPCOMPLETE)) != 14643 (GOINGAWAY | DEPCOMPLETE) || 14644 !TAILQ_EMPTY(&indirdep->ir_trunc) || 14645 !LIST_EMPTY(&indirdep->ir_completehd) || 14646 !LIST_EMPTY(&indirdep->ir_writehd) || 14647 !LIST_EMPTY(&indirdep->ir_donehd) || 14648 !LIST_EMPTY(&indirdep->ir_deplisthd) || 14649 indirdep->ir_saveddata != NULL || 14650 indirdep->ir_savebp == NULL) { 14651 printf("%s: skipping orphaned indirdep %p\n", 14652 __FUNCTION__, indirdep); 14653 continue; 14654 } 14655 printf("%s: freeing orphaned indirdep %p\n", 14656 __FUNCTION__, indirdep); 14657 bp = indirdep->ir_savebp; 14658 indirdep->ir_savebp = NULL; 14659 free_indirdep(indirdep); 14660 FREE_LOCK(ump); 14661 brelse(bp); 14662 while (!TRY_ACQUIRE_LOCK(ump)) { 14663 BO_UNLOCK(bo); 14664 ACQUIRE_LOCK(ump); 14665 FREE_LOCK(ump); 14666 BO_LOCK(bo); 14667 } 14668 } 14669 } 14670 14671 /* 14672 * Reasons for needing more work before suspend: 14673 * - Dirty buffers on devvp. 14674 * - Dependency structures still exist 14675 * - Softdep activity occurred after start of vnode sync loop 14676 * - Secondary writes occurred after start of vnode sync loop 14677 */ 14678 error = 0; 14679 if (bo->bo_numoutput > 0 || 14680 bo->bo_dirty.bv_cnt > 0 || 14681 softdep_depcnt != unlinked || 14682 ump->softdep_deps != unlinked || 14683 softdep_accdepcnt != ump->softdep_accdeps || 14684 secondary_writes != 0 || 14685 mp->mnt_secondary_writes != 0 || 14686 secondary_accwrites != mp->mnt_secondary_accwrites) 14687 error = EAGAIN; 14688 FREE_LOCK(ump); 14689 BO_UNLOCK(bo); 14690 return (error); 14691 } 14692 14693 /* 14694 * Get the number of dependency structures for the file system, both 14695 * the current number and the total number allocated. These will 14696 * later be used to detect that softdep processing has occurred. 14697 */ 14698 void 14699 softdep_get_depcounts(struct mount *mp, 14700 int *softdep_depsp, 14701 int *softdep_accdepsp) 14702 { 14703 struct ufsmount *ump; 14704 14705 if (MOUNTEDSOFTDEP(mp) == 0) { 14706 *softdep_depsp = 0; 14707 *softdep_accdepsp = 0; 14708 return; 14709 } 14710 ump = VFSTOUFS(mp); 14711 ACQUIRE_LOCK(ump); 14712 *softdep_depsp = ump->softdep_deps; 14713 *softdep_accdepsp = ump->softdep_accdeps; 14714 FREE_LOCK(ump); 14715 } 14716 14717 /* 14718 * Wait for pending output on a vnode to complete. 14719 */ 14720 static void 14721 drain_output(struct vnode *vp) 14722 { 14723 14724 ASSERT_VOP_LOCKED(vp, "drain_output"); 14725 (void)bufobj_wwait(&vp->v_bufobj, 0, 0); 14726 } 14727 14728 /* 14729 * Called whenever a buffer that is being invalidated or reallocated 14730 * contains dependencies. This should only happen if an I/O error has 14731 * occurred. The routine is called with the buffer locked. 14732 */ 14733 static void 14734 softdep_deallocate_dependencies(struct buf *bp) 14735 { 14736 14737 if ((bp->b_ioflags & BIO_ERROR) == 0) 14738 panic("softdep_deallocate_dependencies: dangling deps"); 14739 if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL) 14740 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error); 14741 else 14742 printf("softdep_deallocate_dependencies: " 14743 "got error %d while accessing filesystem\n", bp->b_error); 14744 if (bp->b_error != ENXIO) 14745 panic("softdep_deallocate_dependencies: unrecovered I/O error"); 14746 } 14747 14748 /* 14749 * Function to handle asynchronous write errors in the filesystem. 14750 */ 14751 static void 14752 softdep_error(char *func, int error) 14753 { 14754 14755 /* XXX should do something better! */ 14756 printf("%s: got error %d while accessing filesystem\n", func, error); 14757 } 14758 14759 #ifdef DDB 14760 14761 /* exported to ffs_vfsops.c */ 14762 extern void db_print_ffs(struct ufsmount *ump); 14763 void 14764 db_print_ffs(struct ufsmount *ump) 14765 { 14766 db_printf("mp %p (%s) devvp %p\n", ump->um_mountp, 14767 ump->um_mountp->mnt_stat.f_mntonname, ump->um_devvp); 14768 db_printf(" fs %p ", ump->um_fs); 14769 14770 if (ump->um_softdep != NULL) { 14771 db_printf("su_wl %d su_deps %d su_req %d\n", 14772 ump->softdep_on_worklist, ump->softdep_deps, 14773 ump->softdep_req); 14774 } else { 14775 db_printf("su disabled\n"); 14776 } 14777 } 14778 14779 static void 14780 worklist_print(struct worklist *wk, int verbose) 14781 { 14782 14783 if (!verbose) { 14784 db_printf("%s: %p state 0x%b\n", TYPENAME(wk->wk_type), wk, 14785 wk->wk_state, PRINT_SOFTDEP_FLAGS); 14786 return; 14787 } 14788 db_printf("worklist: %p type %s state 0x%b next %p\n ", wk, 14789 TYPENAME(wk->wk_type), wk->wk_state, PRINT_SOFTDEP_FLAGS, 14790 LIST_NEXT(wk, wk_list)); 14791 db_print_ffs(VFSTOUFS(wk->wk_mp)); 14792 } 14793 14794 static void 14795 inodedep_print(struct inodedep *inodedep, int verbose) 14796 { 14797 14798 worklist_print(&inodedep->id_list, 0); 14799 db_printf(" fs %p ino %jd inoblk %jd delta %jd nlink %jd\n", 14800 inodedep->id_fs, 14801 (intmax_t)inodedep->id_ino, 14802 (intmax_t)fsbtodb(inodedep->id_fs, 14803 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)), 14804 (intmax_t)inodedep->id_nlinkdelta, 14805 (intmax_t)inodedep->id_savednlink); 14806 14807 if (verbose == 0) 14808 return; 14809 14810 db_printf(" bmsafemap %p, mkdiradd %p, inoreflst %p\n", 14811 inodedep->id_bmsafemap, 14812 inodedep->id_mkdiradd, 14813 TAILQ_FIRST(&inodedep->id_inoreflst)); 14814 db_printf(" dirremhd %p, pendinghd %p, bufwait %p\n", 14815 LIST_FIRST(&inodedep->id_dirremhd), 14816 LIST_FIRST(&inodedep->id_pendinghd), 14817 LIST_FIRST(&inodedep->id_bufwait)); 14818 db_printf(" inowait %p, inoupdt %p, newinoupdt %p\n", 14819 LIST_FIRST(&inodedep->id_inowait), 14820 TAILQ_FIRST(&inodedep->id_inoupdt), 14821 TAILQ_FIRST(&inodedep->id_newinoupdt)); 14822 db_printf(" extupdt %p, newextupdt %p, freeblklst %p\n", 14823 TAILQ_FIRST(&inodedep->id_extupdt), 14824 TAILQ_FIRST(&inodedep->id_newextupdt), 14825 TAILQ_FIRST(&inodedep->id_freeblklst)); 14826 db_printf(" saveino %p, savedsize %jd, savedextsize %jd\n", 14827 inodedep->id_savedino1, 14828 (intmax_t)inodedep->id_savedsize, 14829 (intmax_t)inodedep->id_savedextsize); 14830 } 14831 14832 static void 14833 newblk_print(struct newblk *nbp) 14834 { 14835 14836 worklist_print(&nbp->nb_list, 0); 14837 db_printf(" newblkno %jd\n", (intmax_t)nbp->nb_newblkno); 14838 db_printf(" jnewblk %p, bmsafemap %p, freefrag %p\n", 14839 &nbp->nb_jnewblk, 14840 &nbp->nb_bmsafemap, 14841 &nbp->nb_freefrag); 14842 db_printf(" indirdeps %p, newdirblk %p, jwork %p\n", 14843 LIST_FIRST(&nbp->nb_indirdeps), 14844 LIST_FIRST(&nbp->nb_newdirblk), 14845 LIST_FIRST(&nbp->nb_jwork)); 14846 } 14847 14848 static void 14849 allocdirect_print(struct allocdirect *adp) 14850 { 14851 14852 newblk_print(&adp->ad_block); 14853 db_printf(" oldblkno %jd, oldsize %ld, newsize %ld\n", 14854 adp->ad_oldblkno, adp->ad_oldsize, adp->ad_newsize); 14855 db_printf(" offset %d, inodedep %p\n", 14856 adp->ad_offset, adp->ad_inodedep); 14857 } 14858 14859 static void 14860 allocindir_print(struct allocindir *aip) 14861 { 14862 14863 newblk_print(&aip->ai_block); 14864 db_printf(" oldblkno %jd, lbn %jd\n", 14865 (intmax_t)aip->ai_oldblkno, (intmax_t)aip->ai_lbn); 14866 db_printf(" offset %d, indirdep %p\n", 14867 aip->ai_offset, aip->ai_indirdep); 14868 } 14869 14870 static void 14871 mkdir_print(struct mkdir *mkdir) 14872 { 14873 14874 worklist_print(&mkdir->md_list, 0); 14875 db_printf(" diradd %p, jaddref %p, buf %p\n", 14876 mkdir->md_diradd, mkdir->md_jaddref, mkdir->md_buf); 14877 } 14878 14879 DB_SHOW_COMMAND(sd_inodedep, db_show_sd_inodedep) 14880 { 14881 14882 if (have_addr == 0) { 14883 db_printf("inodedep address required\n"); 14884 return; 14885 } 14886 inodedep_print((struct inodedep*)addr, 1); 14887 } 14888 14889 DB_SHOW_COMMAND(sd_allinodedeps, db_show_sd_allinodedeps) 14890 { 14891 struct inodedep_hashhead *inodedephd; 14892 struct inodedep *inodedep; 14893 struct ufsmount *ump; 14894 int cnt; 14895 14896 if (have_addr == 0) { 14897 db_printf("ufsmount address required\n"); 14898 return; 14899 } 14900 ump = (struct ufsmount *)addr; 14901 for (cnt = 0; cnt < ump->inodedep_hash_size; cnt++) { 14902 inodedephd = &ump->inodedep_hashtbl[cnt]; 14903 LIST_FOREACH(inodedep, inodedephd, id_hash) { 14904 inodedep_print(inodedep, 0); 14905 } 14906 } 14907 } 14908 14909 DB_SHOW_COMMAND(sd_worklist, db_show_sd_worklist) 14910 { 14911 14912 if (have_addr == 0) { 14913 db_printf("worklist address required\n"); 14914 return; 14915 } 14916 worklist_print((struct worklist *)addr, 1); 14917 } 14918 14919 DB_SHOW_COMMAND(sd_workhead, db_show_sd_workhead) 14920 { 14921 struct worklist *wk; 14922 struct workhead *wkhd; 14923 14924 if (have_addr == 0) { 14925 db_printf("worklist address required " 14926 "(for example value in bp->b_dep)\n"); 14927 return; 14928 } 14929 /* 14930 * We often do not have the address of the worklist head but 14931 * instead a pointer to its first entry (e.g., we have the 14932 * contents of bp->b_dep rather than &bp->b_dep). But the back 14933 * pointer of bp->b_dep will point at the head of the list, so 14934 * we cheat and use that instead. If we are in the middle of 14935 * a list we will still get the same result, so nothing 14936 * unexpected will result. 14937 */ 14938 wk = (struct worklist *)addr; 14939 if (wk == NULL) 14940 return; 14941 wkhd = (struct workhead *)wk->wk_list.le_prev; 14942 LIST_FOREACH(wk, wkhd, wk_list) { 14943 switch(wk->wk_type) { 14944 case D_INODEDEP: 14945 inodedep_print(WK_INODEDEP(wk), 0); 14946 continue; 14947 case D_ALLOCDIRECT: 14948 allocdirect_print(WK_ALLOCDIRECT(wk)); 14949 continue; 14950 case D_ALLOCINDIR: 14951 allocindir_print(WK_ALLOCINDIR(wk)); 14952 continue; 14953 case D_MKDIR: 14954 mkdir_print(WK_MKDIR(wk)); 14955 continue; 14956 default: 14957 worklist_print(wk, 0); 14958 continue; 14959 } 14960 } 14961 } 14962 14963 DB_SHOW_COMMAND(sd_mkdir, db_show_sd_mkdir) 14964 { 14965 if (have_addr == 0) { 14966 db_printf("mkdir address required\n"); 14967 return; 14968 } 14969 mkdir_print((struct mkdir *)addr); 14970 } 14971 14972 DB_SHOW_COMMAND(sd_mkdir_list, db_show_sd_mkdir_list) 14973 { 14974 struct mkdirlist *mkdirlisthd; 14975 struct mkdir *mkdir; 14976 14977 if (have_addr == 0) { 14978 db_printf("mkdir listhead address required\n"); 14979 return; 14980 } 14981 mkdirlisthd = (struct mkdirlist *)addr; 14982 LIST_FOREACH(mkdir, mkdirlisthd, md_mkdirs) { 14983 mkdir_print(mkdir); 14984 if (mkdir->md_diradd != NULL) { 14985 db_printf(" "); 14986 worklist_print(&mkdir->md_diradd->da_list, 0); 14987 } 14988 if (mkdir->md_jaddref != NULL) { 14989 db_printf(" "); 14990 worklist_print(&mkdir->md_jaddref->ja_list, 0); 14991 } 14992 } 14993 } 14994 14995 DB_SHOW_COMMAND(sd_allocdirect, db_show_sd_allocdirect) 14996 { 14997 if (have_addr == 0) { 14998 db_printf("allocdirect address required\n"); 14999 return; 15000 } 15001 allocdirect_print((struct allocdirect *)addr); 15002 } 15003 15004 DB_SHOW_COMMAND(sd_allocindir, db_show_sd_allocindir) 15005 { 15006 if (have_addr == 0) { 15007 db_printf("allocindir address required\n"); 15008 return; 15009 } 15010 allocindir_print((struct allocindir *)addr); 15011 } 15012 15013 #endif /* DDB */ 15014 15015 #endif /* SOFTUPDATES */ 15016