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 42 #include <sys/cdefs.h> 43 #include "opt_ffs.h" 44 #include "opt_quota.h" 45 #include "opt_ddb.h" 46 47 #include <sys/param.h> 48 #include <sys/kernel.h> 49 #include <sys/systm.h> 50 #include <sys/bio.h> 51 #include <sys/buf.h> 52 #include <sys/kdb.h> 53 #include <sys/kthread.h> 54 #include <sys/ktr.h> 55 #include <sys/limits.h> 56 #include <sys/lock.h> 57 #include <sys/malloc.h> 58 #include <sys/mount.h> 59 #include <sys/mutex.h> 60 #include <sys/namei.h> 61 #include <sys/priv.h> 62 #include <sys/proc.h> 63 #include <sys/racct.h> 64 #include <sys/rwlock.h> 65 #include <sys/stat.h> 66 #include <sys/sysctl.h> 67 #include <sys/syslog.h> 68 #include <sys/vnode.h> 69 #include <sys/conf.h> 70 71 #include <ufs/ufs/dir.h> 72 #include <ufs/ufs/extattr.h> 73 #include <ufs/ufs/quota.h> 74 #include <ufs/ufs/inode.h> 75 #include <ufs/ufs/ufsmount.h> 76 #include <ufs/ffs/fs.h> 77 #include <ufs/ffs/softdep.h> 78 #include <ufs/ffs/ffs_extern.h> 79 #include <ufs/ufs/ufs_extern.h> 80 81 #include <vm/vm.h> 82 #include <vm/vm_extern.h> 83 #include <vm/vm_object.h> 84 85 #include <geom/geom.h> 86 #include <geom/geom_vfs.h> 87 88 #include <ddb/ddb.h> 89 90 #define KTR_SUJ 0 /* Define to KTR_SPARE. */ 91 92 #ifndef SOFTUPDATES 93 94 int 95 softdep_flushfiles(struct mount *oldmnt, 96 int flags, 97 struct thread *td) 98 { 99 100 panic("softdep_flushfiles called"); 101 } 102 103 int 104 softdep_mount(struct vnode *devvp, 105 struct mount *mp, 106 struct fs *fs, 107 struct ucred *cred) 108 { 109 110 return (0); 111 } 112 113 void 114 softdep_initialize(void) 115 { 116 117 return; 118 } 119 120 void 121 softdep_uninitialize(void) 122 { 123 124 return; 125 } 126 127 void 128 softdep_unmount(struct mount *mp) 129 { 130 131 panic("softdep_unmount called"); 132 } 133 134 void 135 softdep_setup_sbupdate(struct ufsmount *ump, 136 struct fs *fs, 137 struct buf *bp) 138 { 139 140 panic("softdep_setup_sbupdate called"); 141 } 142 143 void 144 softdep_setup_inomapdep(struct buf *bp, 145 struct inode *ip, 146 ino_t newinum, 147 int mode) 148 { 149 150 panic("softdep_setup_inomapdep called"); 151 } 152 153 void 154 softdep_setup_blkmapdep(struct buf *bp, 155 struct mount *mp, 156 ufs2_daddr_t newblkno, 157 int frags, 158 int oldfrags) 159 { 160 161 panic("softdep_setup_blkmapdep called"); 162 } 163 164 void 165 softdep_setup_allocdirect(struct inode *ip, 166 ufs_lbn_t lbn, 167 ufs2_daddr_t newblkno, 168 ufs2_daddr_t oldblkno, 169 long newsize, 170 long oldsize, 171 struct buf *bp) 172 { 173 174 panic("softdep_setup_allocdirect called"); 175 } 176 177 void 178 softdep_setup_allocext(struct inode *ip, 179 ufs_lbn_t lbn, 180 ufs2_daddr_t newblkno, 181 ufs2_daddr_t oldblkno, 182 long newsize, 183 long oldsize, 184 struct buf *bp) 185 { 186 187 panic("softdep_setup_allocext called"); 188 } 189 190 void 191 softdep_setup_allocindir_page(struct inode *ip, 192 ufs_lbn_t lbn, 193 struct buf *bp, 194 int ptrno, 195 ufs2_daddr_t newblkno, 196 ufs2_daddr_t oldblkno, 197 struct buf *nbp) 198 { 199 200 panic("softdep_setup_allocindir_page called"); 201 } 202 203 void 204 softdep_setup_allocindir_meta(struct buf *nbp, 205 struct inode *ip, 206 struct buf *bp, 207 int ptrno, 208 ufs2_daddr_t newblkno) 209 { 210 211 panic("softdep_setup_allocindir_meta called"); 212 } 213 214 void 215 softdep_journal_freeblocks(struct inode *ip, 216 struct ucred *cred, 217 off_t length, 218 int flags) 219 { 220 221 panic("softdep_journal_freeblocks called"); 222 } 223 224 void 225 softdep_journal_fsync(struct inode *ip) 226 { 227 228 panic("softdep_journal_fsync called"); 229 } 230 231 void 232 softdep_setup_freeblocks(struct inode *ip, 233 off_t length, 234 int flags) 235 { 236 237 panic("softdep_setup_freeblocks called"); 238 } 239 240 void 241 softdep_freefile(struct vnode *pvp, 242 ino_t ino, 243 int mode) 244 { 245 246 panic("softdep_freefile called"); 247 } 248 249 int 250 softdep_setup_directory_add(struct buf *bp, 251 struct inode *dp, 252 off_t diroffset, 253 ino_t newinum, 254 struct buf *newdirbp, 255 int isnewblk) 256 { 257 258 panic("softdep_setup_directory_add called"); 259 } 260 261 void 262 softdep_change_directoryentry_offset(struct buf *bp, 263 struct inode *dp, 264 caddr_t base, 265 caddr_t oldloc, 266 caddr_t newloc, 267 int entrysize) 268 { 269 270 panic("softdep_change_directoryentry_offset called"); 271 } 272 273 void 274 softdep_setup_remove(struct buf *bp, 275 struct inode *dp, 276 struct inode *ip, 277 int isrmdir) 278 { 279 280 panic("softdep_setup_remove called"); 281 } 282 283 void 284 softdep_setup_directory_change(struct buf *bp, 285 struct inode *dp, 286 struct inode *ip, 287 ino_t newinum, 288 int isrmdir) 289 { 290 291 panic("softdep_setup_directory_change called"); 292 } 293 294 void 295 softdep_setup_blkfree(struct mount *mp, 296 struct buf *bp, 297 ufs2_daddr_t blkno, 298 int frags, 299 struct workhead *wkhd, 300 bool doingrecovery) 301 { 302 303 panic("%s called", __FUNCTION__); 304 } 305 306 void 307 softdep_setup_inofree(struct mount *mp, 308 struct buf *bp, 309 ino_t ino, 310 struct workhead *wkhd, 311 bool doingrecovery) 312 { 313 314 panic("%s called", __FUNCTION__); 315 } 316 317 void 318 softdep_setup_unlink(struct inode *dp, struct inode *ip) 319 { 320 321 panic("%s called", __FUNCTION__); 322 } 323 324 void 325 softdep_setup_link(struct inode *dp, struct inode *ip) 326 { 327 328 panic("%s called", __FUNCTION__); 329 } 330 331 void 332 softdep_revert_link(struct inode *dp, struct inode *ip) 333 { 334 335 panic("%s called", __FUNCTION__); 336 } 337 338 void 339 softdep_setup_rmdir(struct inode *dp, struct inode *ip) 340 { 341 342 panic("%s called", __FUNCTION__); 343 } 344 345 void 346 softdep_revert_rmdir(struct inode *dp, struct inode *ip) 347 { 348 349 panic("%s called", __FUNCTION__); 350 } 351 352 void 353 softdep_setup_create(struct inode *dp, struct inode *ip) 354 { 355 356 panic("%s called", __FUNCTION__); 357 } 358 359 void 360 softdep_revert_create(struct inode *dp, struct inode *ip) 361 { 362 363 panic("%s called", __FUNCTION__); 364 } 365 366 void 367 softdep_setup_mkdir(struct inode *dp, struct inode *ip) 368 { 369 370 panic("%s called", __FUNCTION__); 371 } 372 373 void 374 softdep_revert_mkdir(struct inode *dp, struct inode *ip) 375 { 376 377 panic("%s called", __FUNCTION__); 378 } 379 380 void 381 softdep_setup_dotdot_link(struct inode *dp, struct inode *ip) 382 { 383 384 panic("%s called", __FUNCTION__); 385 } 386 387 int 388 softdep_prealloc(struct vnode *vp, int waitok) 389 { 390 391 panic("%s called", __FUNCTION__); 392 } 393 394 int 395 softdep_journal_lookup(struct mount *mp, struct vnode **vpp) 396 { 397 398 return (ENOENT); 399 } 400 401 void 402 softdep_change_linkcnt(struct inode *ip) 403 { 404 405 panic("softdep_change_linkcnt called"); 406 } 407 408 void 409 softdep_load_inodeblock(struct inode *ip) 410 { 411 412 panic("softdep_load_inodeblock called"); 413 } 414 415 void 416 softdep_update_inodeblock(struct inode *ip, 417 struct buf *bp, 418 int waitfor) 419 { 420 421 panic("softdep_update_inodeblock called"); 422 } 423 424 int 425 softdep_fsync(struct vnode *vp) /* the "in_core" copy of the inode */ 426 { 427 428 return (0); 429 } 430 431 void 432 softdep_fsync_mountdev(struct vnode *vp) 433 { 434 435 return; 436 } 437 438 int 439 softdep_flushworklist(struct mount *oldmnt, 440 int *countp, 441 struct thread *td) 442 { 443 444 *countp = 0; 445 return (0); 446 } 447 448 int 449 softdep_sync_metadata(struct vnode *vp) 450 { 451 452 panic("softdep_sync_metadata called"); 453 } 454 455 int 456 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 457 { 458 459 panic("softdep_sync_buf called"); 460 } 461 462 int 463 softdep_slowdown(struct vnode *vp) 464 { 465 466 panic("softdep_slowdown called"); 467 } 468 469 int 470 softdep_request_cleanup(struct fs *fs, 471 struct vnode *vp, 472 struct ucred *cred, 473 int resource) 474 { 475 476 return (0); 477 } 478 479 int 480 softdep_check_suspend(struct mount *mp, 481 struct vnode *devvp, 482 int softdep_depcnt, 483 int softdep_accdepcnt, 484 int secondary_writes, 485 int secondary_accwrites) 486 { 487 struct bufobj *bo; 488 int error; 489 490 (void) softdep_depcnt, 491 (void) softdep_accdepcnt; 492 493 bo = &devvp->v_bufobj; 494 ASSERT_BO_WLOCKED(bo); 495 496 MNT_ILOCK(mp); 497 while (mp->mnt_secondary_writes != 0) { 498 BO_UNLOCK(bo); 499 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 500 (PUSER - 1) | PDROP, "secwr", 0); 501 BO_LOCK(bo); 502 MNT_ILOCK(mp); 503 } 504 505 /* 506 * Reasons for needing more work before suspend: 507 * - Dirty buffers on devvp. 508 * - Secondary writes occurred after start of vnode sync loop 509 */ 510 error = 0; 511 if (bo->bo_numoutput > 0 || 512 bo->bo_dirty.bv_cnt > 0 || 513 secondary_writes != 0 || 514 mp->mnt_secondary_writes != 0 || 515 secondary_accwrites != mp->mnt_secondary_accwrites) 516 error = EAGAIN; 517 BO_UNLOCK(bo); 518 return (error); 519 } 520 521 void 522 softdep_get_depcounts(struct mount *mp, 523 int *softdepactivep, 524 int *softdepactiveaccp) 525 { 526 (void) mp; 527 *softdepactivep = 0; 528 *softdepactiveaccp = 0; 529 } 530 531 void 532 softdep_buf_append(struct buf *bp, struct workhead *wkhd) 533 { 534 535 panic("softdep_buf_appendwork called"); 536 } 537 538 void 539 softdep_inode_append(struct inode *ip, 540 struct ucred *cred, 541 struct workhead *wkhd) 542 { 543 544 panic("softdep_inode_appendwork called"); 545 } 546 547 void 548 softdep_freework(struct workhead *wkhd) 549 { 550 551 panic("softdep_freework called"); 552 } 553 554 int 555 softdep_prerename(struct vnode *fdvp, 556 struct vnode *fvp, 557 struct vnode *tdvp, 558 struct vnode *tvp) 559 { 560 561 panic("softdep_prerename called"); 562 } 563 564 int 565 softdep_prelink(struct vnode *dvp, 566 struct vnode *vp, 567 struct componentname *cnp) 568 { 569 570 panic("softdep_prelink called"); 571 } 572 573 #else 574 575 FEATURE(softupdates, "FFS soft-updates support"); 576 577 static SYSCTL_NODE(_debug, OID_AUTO, softdep, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 578 "soft updates stats"); 579 static SYSCTL_NODE(_debug_softdep, OID_AUTO, total, 580 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 581 "total dependencies allocated"); 582 static SYSCTL_NODE(_debug_softdep, OID_AUTO, highuse, 583 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 584 "high use dependencies allocated"); 585 static SYSCTL_NODE(_debug_softdep, OID_AUTO, current, 586 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 587 "current dependencies allocated"); 588 static SYSCTL_NODE(_debug_softdep, OID_AUTO, write, 589 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 590 "current dependencies written"); 591 592 unsigned long dep_current[D_LAST + 1]; 593 unsigned long dep_highuse[D_LAST + 1]; 594 unsigned long dep_total[D_LAST + 1]; 595 unsigned long dep_write[D_LAST + 1]; 596 597 #define SOFTDEP_TYPE(type, str, long) \ 598 static MALLOC_DEFINE(M_ ## type, #str, long); \ 599 SYSCTL_ULONG(_debug_softdep_total, OID_AUTO, str, CTLFLAG_RD, \ 600 &dep_total[D_ ## type], 0, ""); \ 601 SYSCTL_ULONG(_debug_softdep_current, OID_AUTO, str, CTLFLAG_RD, \ 602 &dep_current[D_ ## type], 0, ""); \ 603 SYSCTL_ULONG(_debug_softdep_highuse, OID_AUTO, str, CTLFLAG_RD, \ 604 &dep_highuse[D_ ## type], 0, ""); \ 605 SYSCTL_ULONG(_debug_softdep_write, OID_AUTO, str, CTLFLAG_RD, \ 606 &dep_write[D_ ## type], 0, ""); 607 608 SOFTDEP_TYPE(PAGEDEP, pagedep, "File page dependencies"); 609 SOFTDEP_TYPE(INODEDEP, inodedep, "Inode dependencies"); 610 SOFTDEP_TYPE(BMSAFEMAP, bmsafemap, 611 "Block or frag allocated from cyl group map"); 612 SOFTDEP_TYPE(NEWBLK, newblk, "New block or frag allocation dependency"); 613 SOFTDEP_TYPE(ALLOCDIRECT, allocdirect, "Block or frag dependency for an inode"); 614 SOFTDEP_TYPE(INDIRDEP, indirdep, "Indirect block dependencies"); 615 SOFTDEP_TYPE(ALLOCINDIR, allocindir, "Block dependency for an indirect block"); 616 SOFTDEP_TYPE(FREEFRAG, freefrag, "Previously used frag for an inode"); 617 SOFTDEP_TYPE(FREEBLKS, freeblks, "Blocks freed from an inode"); 618 SOFTDEP_TYPE(FREEFILE, freefile, "Inode deallocated"); 619 SOFTDEP_TYPE(DIRADD, diradd, "New directory entry"); 620 SOFTDEP_TYPE(MKDIR, mkdir, "New directory"); 621 SOFTDEP_TYPE(DIRREM, dirrem, "Directory entry deleted"); 622 SOFTDEP_TYPE(NEWDIRBLK, newdirblk, "Unclaimed new directory block"); 623 SOFTDEP_TYPE(FREEWORK, freework, "free an inode block"); 624 SOFTDEP_TYPE(FREEDEP, freedep, "track a block free"); 625 SOFTDEP_TYPE(JADDREF, jaddref, "Journal inode ref add"); 626 SOFTDEP_TYPE(JREMREF, jremref, "Journal inode ref remove"); 627 SOFTDEP_TYPE(JMVREF, jmvref, "Journal inode ref move"); 628 SOFTDEP_TYPE(JNEWBLK, jnewblk, "Journal new block"); 629 SOFTDEP_TYPE(JFREEBLK, jfreeblk, "Journal free block"); 630 SOFTDEP_TYPE(JFREEFRAG, jfreefrag, "Journal free frag"); 631 SOFTDEP_TYPE(JSEG, jseg, "Journal segment"); 632 SOFTDEP_TYPE(JSEGDEP, jsegdep, "Journal segment complete"); 633 SOFTDEP_TYPE(SBDEP, sbdep, "Superblock write dependency"); 634 SOFTDEP_TYPE(JTRUNC, jtrunc, "Journal inode truncation"); 635 SOFTDEP_TYPE(JFSYNC, jfsync, "Journal fsync complete"); 636 637 static MALLOC_DEFINE(M_SENTINEL, "sentinel", "Worklist sentinel"); 638 639 static MALLOC_DEFINE(M_SAVEDINO, "savedino", "Saved inodes"); 640 static MALLOC_DEFINE(M_JBLOCKS, "jblocks", "Journal block locations"); 641 static MALLOC_DEFINE(M_MOUNTDATA, "softdep", "Softdep per-mount data"); 642 643 #define M_SOFTDEP_FLAGS (M_WAITOK) 644 645 /* 646 * translate from workitem type to memory type 647 * MUST match the defines above, such that memtype[D_XXX] == M_XXX 648 */ 649 static struct malloc_type *memtype[] = { 650 NULL, 651 M_PAGEDEP, 652 M_INODEDEP, 653 M_BMSAFEMAP, 654 M_NEWBLK, 655 M_ALLOCDIRECT, 656 M_INDIRDEP, 657 M_ALLOCINDIR, 658 M_FREEFRAG, 659 M_FREEBLKS, 660 M_FREEFILE, 661 M_DIRADD, 662 M_MKDIR, 663 M_DIRREM, 664 M_NEWDIRBLK, 665 M_FREEWORK, 666 M_FREEDEP, 667 M_JADDREF, 668 M_JREMREF, 669 M_JMVREF, 670 M_JNEWBLK, 671 M_JFREEBLK, 672 M_JFREEFRAG, 673 M_JSEG, 674 M_JSEGDEP, 675 M_SBDEP, 676 M_JTRUNC, 677 M_JFSYNC, 678 M_SENTINEL 679 }; 680 681 #define DtoM(type) (memtype[type]) 682 683 /* 684 * Names of malloc types. 685 */ 686 #define TYPENAME(type) \ 687 ((unsigned)(type) <= D_LAST && (unsigned)(type) >= D_FIRST ? \ 688 memtype[type]->ks_shortdesc : "???") 689 /* 690 * End system adaptation definitions. 691 */ 692 693 #define DOTDOT_OFFSET offsetof(struct dirtemplate, dotdot_ino) 694 #define DOT_OFFSET offsetof(struct dirtemplate, dot_ino) 695 696 /* 697 * Internal function prototypes. 698 */ 699 static void check_clear_deps(struct mount *); 700 static void softdep_error(char *, int); 701 static int softdep_prerename_vnode(struct ufsmount *, struct vnode *); 702 static int softdep_process_worklist(struct mount *, int); 703 static int softdep_waitidle(struct mount *, int); 704 static void drain_output(struct vnode *); 705 static struct buf *getdirtybuf(struct buf *, struct rwlock *, int); 706 static int check_inodedep_free(struct inodedep *); 707 static void clear_remove(struct mount *); 708 static void clear_inodedeps(struct mount *); 709 static void unlinked_inodedep(struct mount *, struct inodedep *); 710 static void clear_unlinked_inodedep(struct inodedep *); 711 static struct inodedep *first_unlinked_inodedep(struct ufsmount *); 712 static int flush_pagedep_deps(struct vnode *, struct mount *, 713 struct diraddhd *, struct buf *); 714 static int free_pagedep(struct pagedep *); 715 static int flush_newblk_dep(struct vnode *, struct mount *, ufs_lbn_t); 716 static int flush_inodedep_deps(struct vnode *, struct mount *, ino_t); 717 static int flush_deplist(struct allocdirectlst *, int, int *); 718 static int sync_cgs(struct mount *, int); 719 static int handle_written_filepage(struct pagedep *, struct buf *, int); 720 static int handle_written_sbdep(struct sbdep *, struct buf *); 721 static void initiate_write_sbdep(struct sbdep *); 722 static void diradd_inode_written(struct diradd *, struct inodedep *); 723 static int handle_written_indirdep(struct indirdep *, struct buf *, 724 struct buf**, int); 725 static int handle_written_inodeblock(struct inodedep *, struct buf *, int); 726 static int jnewblk_rollforward(struct jnewblk *, struct fs *, struct cg *, 727 uint8_t *); 728 static int handle_written_bmsafemap(struct bmsafemap *, struct buf *, int); 729 static void handle_written_jaddref(struct jaddref *); 730 static void handle_written_jremref(struct jremref *); 731 static void handle_written_jseg(struct jseg *, struct buf *); 732 static void handle_written_jnewblk(struct jnewblk *); 733 static void handle_written_jblkdep(struct jblkdep *); 734 static void handle_written_jfreefrag(struct jfreefrag *); 735 static void complete_jseg(struct jseg *); 736 static void complete_jsegs(struct jseg *); 737 static void jseg_write(struct ufsmount *ump, struct jseg *, uint8_t *); 738 static void jaddref_write(struct jaddref *, struct jseg *, uint8_t *); 739 static void jremref_write(struct jremref *, struct jseg *, uint8_t *); 740 static void jmvref_write(struct jmvref *, struct jseg *, uint8_t *); 741 static void jtrunc_write(struct jtrunc *, struct jseg *, uint8_t *); 742 static void jfsync_write(struct jfsync *, struct jseg *, uint8_t *data); 743 static void jnewblk_write(struct jnewblk *, struct jseg *, uint8_t *); 744 static void jfreeblk_write(struct jfreeblk *, struct jseg *, uint8_t *); 745 static void jfreefrag_write(struct jfreefrag *, struct jseg *, uint8_t *); 746 static inline void inoref_write(struct inoref *, struct jseg *, 747 struct jrefrec *); 748 static void handle_allocdirect_partdone(struct allocdirect *, 749 struct workhead *); 750 static struct jnewblk *cancel_newblk(struct newblk *, struct worklist *, 751 struct workhead *); 752 static void indirdep_complete(struct indirdep *); 753 static int indirblk_lookup(struct mount *, ufs2_daddr_t); 754 static void indirblk_insert(struct freework *); 755 static void indirblk_remove(struct freework *); 756 static void handle_allocindir_partdone(struct allocindir *); 757 static void initiate_write_filepage(struct pagedep *, struct buf *); 758 static void initiate_write_indirdep(struct indirdep*, struct buf *); 759 static void handle_written_mkdir(struct mkdir *, int); 760 static int jnewblk_rollback(struct jnewblk *, struct fs *, struct cg *, 761 uint8_t *); 762 static void initiate_write_bmsafemap(struct bmsafemap *, struct buf *); 763 static void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *); 764 static void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *); 765 static void handle_workitem_freefile(struct freefile *); 766 static int handle_workitem_remove(struct dirrem *, int); 767 static struct dirrem *newdirrem(struct buf *, struct inode *, 768 struct inode *, int, struct dirrem **); 769 static struct indirdep *indirdep_lookup(struct mount *, struct inode *, 770 struct buf *); 771 static void cancel_indirdep(struct indirdep *, struct buf *, 772 struct freeblks *); 773 static void free_indirdep(struct indirdep *); 774 static void free_diradd(struct diradd *, struct workhead *); 775 static void merge_diradd(struct inodedep *, struct diradd *); 776 static void complete_diradd(struct diradd *); 777 static struct diradd *diradd_lookup(struct pagedep *, int); 778 static struct jremref *cancel_diradd_dotdot(struct inode *, struct dirrem *, 779 struct jremref *); 780 static struct jremref *cancel_mkdir_dotdot(struct inode *, struct dirrem *, 781 struct jremref *); 782 static void cancel_diradd(struct diradd *, struct dirrem *, struct jremref *, 783 struct jremref *, struct jremref *); 784 static void dirrem_journal(struct dirrem *, struct jremref *, struct jremref *, 785 struct jremref *); 786 static void cancel_allocindir(struct allocindir *, struct buf *bp, 787 struct freeblks *, int); 788 static int setup_trunc_indir(struct freeblks *, struct inode *, 789 ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t); 790 static void complete_trunc_indir(struct freework *); 791 static void trunc_indirdep(struct indirdep *, struct freeblks *, struct buf *, 792 int); 793 static void complete_mkdir(struct mkdir *); 794 static void free_newdirblk(struct newdirblk *); 795 static void free_jremref(struct jremref *); 796 static void free_jaddref(struct jaddref *); 797 static void free_jsegdep(struct jsegdep *); 798 static void free_jsegs(struct jblocks *); 799 static void rele_jseg(struct jseg *); 800 static void free_jseg(struct jseg *, struct jblocks *); 801 static void free_jnewblk(struct jnewblk *); 802 static void free_jblkdep(struct jblkdep *); 803 static void free_jfreefrag(struct jfreefrag *); 804 static void free_freedep(struct freedep *); 805 static void journal_jremref(struct dirrem *, struct jremref *, 806 struct inodedep *); 807 static void cancel_jnewblk(struct jnewblk *, struct workhead *); 808 static int cancel_jaddref(struct jaddref *, struct inodedep *, 809 struct workhead *); 810 static void cancel_jfreefrag(struct jfreefrag *); 811 static inline void setup_freedirect(struct freeblks *, struct inode *, 812 int, int); 813 static inline void setup_freeext(struct freeblks *, struct inode *, int, int); 814 static inline void setup_freeindir(struct freeblks *, struct inode *, int, 815 ufs_lbn_t, int); 816 static inline struct freeblks *newfreeblks(struct mount *, struct inode *); 817 static void freeblks_free(struct ufsmount *, struct freeblks *, int); 818 static void indir_trunc(struct freework *, ufs2_daddr_t, ufs_lbn_t); 819 static ufs2_daddr_t blkcount(struct fs *, ufs2_daddr_t, off_t); 820 static int trunc_check_buf(struct buf *, int *, ufs_lbn_t, int, int); 821 static void trunc_dependencies(struct inode *, struct freeblks *, ufs_lbn_t, 822 int, int); 823 static void trunc_pages(struct inode *, off_t, ufs2_daddr_t, int); 824 static int cancel_pagedep(struct pagedep *, struct freeblks *, int); 825 static int deallocate_dependencies(struct buf *, struct freeblks *, int); 826 static void newblk_freefrag(struct newblk*); 827 static void free_newblk(struct newblk *); 828 static void cancel_allocdirect(struct allocdirectlst *, 829 struct allocdirect *, struct freeblks *); 830 static int check_inode_unwritten(struct inodedep *); 831 static int free_inodedep(struct inodedep *); 832 static void freework_freeblock(struct freework *, uint64_t); 833 static void freework_enqueue(struct freework *); 834 static int handle_workitem_freeblocks(struct freeblks *, int); 835 static int handle_complete_freeblocks(struct freeblks *, int); 836 static void handle_workitem_indirblk(struct freework *); 837 static void handle_written_freework(struct freework *); 838 static void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *); 839 static struct worklist *jnewblk_merge(struct worklist *, struct worklist *, 840 struct workhead *); 841 static struct freefrag *setup_allocindir_phase2(struct buf *, struct inode *, 842 struct inodedep *, struct allocindir *, ufs_lbn_t); 843 static struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t, 844 ufs2_daddr_t, ufs_lbn_t); 845 static void handle_workitem_freefrag(struct freefrag *); 846 static struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long, 847 ufs_lbn_t, uint64_t); 848 static void allocdirect_merge(struct allocdirectlst *, 849 struct allocdirect *, struct allocdirect *); 850 static struct freefrag *allocindir_merge(struct allocindir *, 851 struct allocindir *); 852 static int bmsafemap_find(struct bmsafemap_hashhead *, int, 853 struct bmsafemap **); 854 static struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *, 855 int cg, struct bmsafemap *); 856 static int newblk_find(struct newblk_hashhead *, ufs2_daddr_t, int, 857 struct newblk **); 858 static int newblk_lookup(struct mount *, ufs2_daddr_t, int, struct newblk **); 859 static int inodedep_find(struct inodedep_hashhead *, ino_t, 860 struct inodedep **); 861 static int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **); 862 static int pagedep_lookup(struct mount *, struct buf *bp, ino_t, ufs_lbn_t, 863 int, struct pagedep **); 864 static int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t, 865 struct pagedep **); 866 static void pause_timer(void *); 867 static int request_cleanup(struct mount *, int); 868 static int softdep_request_cleanup_flush(struct mount *, struct ufsmount *); 869 static void schedule_cleanup(struct mount *); 870 static void softdep_ast_cleanup_proc(struct thread *, int); 871 static struct ufsmount *softdep_bp_to_mp(struct buf *bp); 872 static int process_worklist_item(struct mount *, int, int); 873 static void process_removes(struct vnode *); 874 static void process_truncates(struct vnode *); 875 static void jwork_move(struct workhead *, struct workhead *); 876 static void jwork_insert(struct workhead *, struct jsegdep *); 877 static void add_to_worklist(struct worklist *, int); 878 static void wake_worklist(struct worklist *); 879 static void wait_worklist(struct worklist *, char *); 880 static void remove_from_worklist(struct worklist *); 881 static void softdep_flush(void *); 882 static void softdep_flushjournal(struct mount *); 883 static int softdep_speedup(struct ufsmount *); 884 static void worklist_speedup(struct mount *); 885 static int journal_mount(struct mount *, struct fs *, struct ucred *); 886 static void journal_unmount(struct ufsmount *); 887 static int journal_space(struct ufsmount *, int); 888 static void journal_suspend(struct ufsmount *); 889 static int journal_unsuspend(struct ufsmount *ump); 890 static void add_to_journal(struct worklist *); 891 static void remove_from_journal(struct worklist *); 892 static bool softdep_excess_items(struct ufsmount *, int); 893 static void softdep_process_journal(struct mount *, struct worklist *, int); 894 static struct jremref *newjremref(struct dirrem *, struct inode *, 895 struct inode *ip, off_t, nlink_t); 896 static struct jaddref *newjaddref(struct inode *, ino_t, off_t, int16_t, 897 uint16_t); 898 static inline void newinoref(struct inoref *, ino_t, ino_t, off_t, nlink_t, 899 uint16_t); 900 static inline struct jsegdep *inoref_jseg(struct inoref *); 901 static struct jmvref *newjmvref(struct inode *, ino_t, off_t, off_t); 902 static struct jfreeblk *newjfreeblk(struct freeblks *, ufs_lbn_t, 903 ufs2_daddr_t, int); 904 static void adjust_newfreework(struct freeblks *, int); 905 static struct jtrunc *newjtrunc(struct freeblks *, off_t, int); 906 static void move_newblock_dep(struct jaddref *, struct inodedep *); 907 static void cancel_jfreeblk(struct freeblks *, ufs2_daddr_t); 908 static struct jfreefrag *newjfreefrag(struct freefrag *, struct inode *, 909 ufs2_daddr_t, long, ufs_lbn_t); 910 static struct freework *newfreework(struct ufsmount *, struct freeblks *, 911 struct freework *, ufs_lbn_t, ufs2_daddr_t, int, int, int); 912 static int jwait(struct worklist *, int); 913 static struct inodedep *inodedep_lookup_ip(struct inode *); 914 static int bmsafemap_backgroundwrite(struct bmsafemap *, struct buf *); 915 static struct freefile *handle_bufwait(struct inodedep *, struct workhead *); 916 static void handle_jwork(struct workhead *); 917 static struct mkdir *setup_newdir(struct diradd *, ino_t, ino_t, struct buf *, 918 struct mkdir **); 919 static struct jblocks *jblocks_create(void); 920 static ufs2_daddr_t jblocks_alloc(struct jblocks *, int, int *); 921 static void jblocks_free(struct jblocks *, struct mount *, int); 922 static void jblocks_destroy(struct jblocks *); 923 static void jblocks_add(struct jblocks *, ufs2_daddr_t, int); 924 925 /* 926 * Exported softdep operations. 927 */ 928 static void softdep_disk_io_initiation(struct buf *); 929 static void softdep_disk_write_complete(struct buf *); 930 static void softdep_deallocate_dependencies(struct buf *); 931 static int softdep_count_dependencies(struct buf *bp, int); 932 933 /* 934 * Global lock over all of soft updates. 935 */ 936 static struct mtx lk; 937 MTX_SYSINIT(softdep_lock, &lk, "global softdep", MTX_DEF); 938 939 #define ACQUIRE_GBLLOCK(lk) mtx_lock(lk) 940 #define FREE_GBLLOCK(lk) mtx_unlock(lk) 941 #define GBLLOCK_OWNED(lk) mtx_assert((lk), MA_OWNED) 942 943 /* 944 * Per-filesystem soft-updates locking. 945 */ 946 #define LOCK_PTR(ump) (&(ump)->um_softdep->sd_fslock) 947 #define TRY_ACQUIRE_LOCK(ump) rw_try_wlock(&(ump)->um_softdep->sd_fslock) 948 #define ACQUIRE_LOCK(ump) rw_wlock(&(ump)->um_softdep->sd_fslock) 949 #define FREE_LOCK(ump) rw_wunlock(&(ump)->um_softdep->sd_fslock) 950 #define LOCK_OWNED(ump) rw_assert(&(ump)->um_softdep->sd_fslock, \ 951 RA_WLOCKED) 952 953 #define BUF_AREC(bp) lockallowrecurse(&(bp)->b_lock) 954 #define BUF_NOREC(bp) lockdisablerecurse(&(bp)->b_lock) 955 956 /* 957 * Worklist queue management. 958 * These routines require that the lock be held. 959 */ 960 #ifndef /* NOT */ INVARIANTS 961 #define WORKLIST_INSERT(head, item) do { \ 962 (item)->wk_state |= ONWORKLIST; \ 963 LIST_INSERT_HEAD(head, item, wk_list); \ 964 } while (0) 965 #define WORKLIST_REMOVE(item) do { \ 966 (item)->wk_state &= ~ONWORKLIST; \ 967 LIST_REMOVE(item, wk_list); \ 968 } while (0) 969 #define WORKLIST_INSERT_UNLOCKED WORKLIST_INSERT 970 #define WORKLIST_REMOVE_UNLOCKED WORKLIST_REMOVE 971 972 #else /* INVARIANTS */ 973 static void worklist_insert(struct workhead *, struct worklist *, int, 974 const char *, int); 975 static void worklist_remove(struct worklist *, int, const char *, int); 976 977 #define WORKLIST_INSERT(head, item) \ 978 worklist_insert(head, item, 1, __func__, __LINE__) 979 #define WORKLIST_INSERT_UNLOCKED(head, item)\ 980 worklist_insert(head, item, 0, __func__, __LINE__) 981 #define WORKLIST_REMOVE(item)\ 982 worklist_remove(item, 1, __func__, __LINE__) 983 #define WORKLIST_REMOVE_UNLOCKED(item)\ 984 worklist_remove(item, 0, __func__, __LINE__) 985 986 static void 987 worklist_insert(struct workhead *head, 988 struct worklist *item, 989 int locked, 990 const char *func, 991 int line) 992 { 993 994 if (locked) 995 LOCK_OWNED(VFSTOUFS(item->wk_mp)); 996 if (item->wk_state & ONWORKLIST) 997 panic("worklist_insert: %p %s(0x%X) already on list, " 998 "added in function %s at line %d", 999 item, TYPENAME(item->wk_type), item->wk_state, 1000 item->wk_func, item->wk_line); 1001 item->wk_state |= ONWORKLIST; 1002 item->wk_func = func; 1003 item->wk_line = line; 1004 LIST_INSERT_HEAD(head, item, wk_list); 1005 } 1006 1007 static void 1008 worklist_remove(struct worklist *item, 1009 int locked, 1010 const char *func, 1011 int line) 1012 { 1013 1014 if (locked) 1015 LOCK_OWNED(VFSTOUFS(item->wk_mp)); 1016 if ((item->wk_state & ONWORKLIST) == 0) 1017 panic("worklist_remove: %p %s(0x%X) not on list, " 1018 "removed in function %s at line %d", 1019 item, TYPENAME(item->wk_type), item->wk_state, 1020 item->wk_func, item->wk_line); 1021 item->wk_state &= ~ONWORKLIST; 1022 item->wk_func = func; 1023 item->wk_line = line; 1024 LIST_REMOVE(item, wk_list); 1025 } 1026 #endif /* INVARIANTS */ 1027 1028 /* 1029 * Merge two jsegdeps keeping only the oldest one as newer references 1030 * can't be discarded until after older references. 1031 */ 1032 static inline struct jsegdep * 1033 jsegdep_merge(struct jsegdep *one, struct jsegdep *two) 1034 { 1035 struct jsegdep *swp; 1036 1037 if (two == NULL) 1038 return (one); 1039 1040 if (one->jd_seg->js_seq > two->jd_seg->js_seq) { 1041 swp = one; 1042 one = two; 1043 two = swp; 1044 } 1045 WORKLIST_REMOVE(&two->jd_list); 1046 free_jsegdep(two); 1047 1048 return (one); 1049 } 1050 1051 /* 1052 * If two freedeps are compatible free one to reduce list size. 1053 */ 1054 static inline struct freedep * 1055 freedep_merge(struct freedep *one, struct freedep *two) 1056 { 1057 if (two == NULL) 1058 return (one); 1059 1060 if (one->fd_freework == two->fd_freework) { 1061 WORKLIST_REMOVE(&two->fd_list); 1062 free_freedep(two); 1063 } 1064 return (one); 1065 } 1066 1067 /* 1068 * Move journal work from one list to another. Duplicate freedeps and 1069 * jsegdeps are coalesced to keep the lists as small as possible. 1070 */ 1071 static void 1072 jwork_move(struct workhead *dst, struct workhead *src) 1073 { 1074 struct freedep *freedep; 1075 struct jsegdep *jsegdep; 1076 struct worklist *wkn; 1077 struct worklist *wk; 1078 1079 KASSERT(dst != src, 1080 ("jwork_move: dst == src")); 1081 freedep = NULL; 1082 jsegdep = NULL; 1083 LIST_FOREACH_SAFE(wk, dst, wk_list, wkn) { 1084 if (wk->wk_type == D_JSEGDEP) 1085 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1086 else if (wk->wk_type == D_FREEDEP) 1087 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1088 } 1089 1090 while ((wk = LIST_FIRST(src)) != NULL) { 1091 WORKLIST_REMOVE(wk); 1092 WORKLIST_INSERT(dst, wk); 1093 if (wk->wk_type == D_JSEGDEP) { 1094 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1095 continue; 1096 } 1097 if (wk->wk_type == D_FREEDEP) 1098 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1099 } 1100 } 1101 1102 static void 1103 jwork_insert(struct workhead *dst, struct jsegdep *jsegdep) 1104 { 1105 struct jsegdep *jsegdepn; 1106 struct worklist *wk; 1107 1108 LIST_FOREACH(wk, dst, wk_list) 1109 if (wk->wk_type == D_JSEGDEP) 1110 break; 1111 if (wk == NULL) { 1112 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1113 return; 1114 } 1115 jsegdepn = WK_JSEGDEP(wk); 1116 if (jsegdep->jd_seg->js_seq < jsegdepn->jd_seg->js_seq) { 1117 WORKLIST_REMOVE(wk); 1118 free_jsegdep(jsegdepn); 1119 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1120 } else 1121 free_jsegdep(jsegdep); 1122 } 1123 1124 /* 1125 * Routines for tracking and managing workitems. 1126 */ 1127 static void workitem_free(struct worklist *, int); 1128 static void workitem_alloc(struct worklist *, int, struct mount *); 1129 static void workitem_reassign(struct worklist *, int); 1130 1131 #define WORKITEM_FREE(item, type) \ 1132 workitem_free((struct worklist *)(item), (type)) 1133 #define WORKITEM_REASSIGN(item, type) \ 1134 workitem_reassign((struct worklist *)(item), (type)) 1135 1136 static void 1137 workitem_free(struct worklist *item, int type) 1138 { 1139 struct ufsmount *ump; 1140 1141 #ifdef INVARIANTS 1142 if (item->wk_state & ONWORKLIST) 1143 panic("workitem_free: %s(0x%X) still on list, " 1144 "added in function %s at line %d", 1145 TYPENAME(item->wk_type), item->wk_state, 1146 item->wk_func, item->wk_line); 1147 if (item->wk_type != type && type != D_NEWBLK) 1148 panic("workitem_free: type mismatch %s != %s", 1149 TYPENAME(item->wk_type), TYPENAME(type)); 1150 #endif 1151 if (item->wk_state & IOWAITING) 1152 wakeup(item); 1153 ump = VFSTOUFS(item->wk_mp); 1154 LOCK_OWNED(ump); 1155 KASSERT(ump->softdep_deps > 0, 1156 ("workitem_free: %s: softdep_deps going negative", 1157 ump->um_fs->fs_fsmnt)); 1158 if (--ump->softdep_deps == 0 && ump->softdep_req) 1159 wakeup(&ump->softdep_deps); 1160 KASSERT(dep_current[item->wk_type] > 0, 1161 ("workitem_free: %s: dep_current[%s] going negative", 1162 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1163 KASSERT(ump->softdep_curdeps[item->wk_type] > 0, 1164 ("workitem_free: %s: softdep_curdeps[%s] going negative", 1165 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1166 atomic_subtract_long(&dep_current[item->wk_type], 1); 1167 ump->softdep_curdeps[item->wk_type] -= 1; 1168 LIST_REMOVE(item, wk_all); 1169 free(item, DtoM(type)); 1170 } 1171 1172 static void 1173 workitem_alloc(struct worklist *item, 1174 int type, 1175 struct mount *mp) 1176 { 1177 struct ufsmount *ump; 1178 1179 item->wk_type = type; 1180 item->wk_mp = mp; 1181 item->wk_state = 0; 1182 1183 ump = VFSTOUFS(mp); 1184 ACQUIRE_GBLLOCK(&lk); 1185 dep_current[type]++; 1186 if (dep_current[type] > dep_highuse[type]) 1187 dep_highuse[type] = dep_current[type]; 1188 dep_total[type]++; 1189 FREE_GBLLOCK(&lk); 1190 ACQUIRE_LOCK(ump); 1191 ump->softdep_curdeps[type] += 1; 1192 ump->softdep_deps++; 1193 ump->softdep_accdeps++; 1194 LIST_INSERT_HEAD(&ump->softdep_alldeps[type], item, wk_all); 1195 FREE_LOCK(ump); 1196 } 1197 1198 static void 1199 workitem_reassign(struct worklist *item, int newtype) 1200 { 1201 struct ufsmount *ump; 1202 1203 ump = VFSTOUFS(item->wk_mp); 1204 LOCK_OWNED(ump); 1205 KASSERT(ump->softdep_curdeps[item->wk_type] > 0, 1206 ("workitem_reassign: %s: softdep_curdeps[%s] going negative", 1207 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1208 ump->softdep_curdeps[item->wk_type] -= 1; 1209 ump->softdep_curdeps[newtype] += 1; 1210 KASSERT(dep_current[item->wk_type] > 0, 1211 ("workitem_reassign: %s: dep_current[%s] going negative", 1212 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1213 ACQUIRE_GBLLOCK(&lk); 1214 dep_current[newtype]++; 1215 dep_current[item->wk_type]--; 1216 if (dep_current[newtype] > dep_highuse[newtype]) 1217 dep_highuse[newtype] = dep_current[newtype]; 1218 dep_total[newtype]++; 1219 FREE_GBLLOCK(&lk); 1220 item->wk_type = newtype; 1221 LIST_REMOVE(item, wk_all); 1222 LIST_INSERT_HEAD(&ump->softdep_alldeps[newtype], item, wk_all); 1223 } 1224 1225 /* 1226 * Workitem queue management 1227 */ 1228 static int max_softdeps; /* maximum number of structs before slowdown */ 1229 static int tickdelay = 2; /* number of ticks to pause during slowdown */ 1230 static int proc_waiting; /* tracks whether we have a timeout posted */ 1231 static int *stat_countp; /* statistic to count in proc_waiting timeout */ 1232 static struct callout softdep_callout; 1233 static int req_clear_inodedeps; /* syncer process flush some inodedeps */ 1234 static int req_clear_remove; /* syncer process flush some freeblks */ 1235 static int softdep_flushcache = 0; /* Should we do BIO_FLUSH? */ 1236 1237 /* 1238 * runtime statistics 1239 */ 1240 static int stat_flush_threads; /* number of softdep flushing threads */ 1241 static int stat_worklist_push; /* number of worklist cleanups */ 1242 static int stat_delayed_inact; /* number of delayed inactivation cleanups */ 1243 static int stat_blk_limit_push; /* number of times block limit neared */ 1244 static int stat_ino_limit_push; /* number of times inode limit neared */ 1245 static int stat_blk_limit_hit; /* number of times block slowdown imposed */ 1246 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */ 1247 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */ 1248 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */ 1249 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */ 1250 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */ 1251 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */ 1252 static int stat_jaddref; /* bufs redirtied as ino bitmap can not write */ 1253 static int stat_jnewblk; /* bufs redirtied as blk bitmap can not write */ 1254 static int stat_journal_min; /* Times hit journal min threshold */ 1255 static int stat_journal_low; /* Times hit journal low threshold */ 1256 static int stat_journal_wait; /* Times blocked in jwait(). */ 1257 static int stat_jwait_filepage; /* Times blocked in jwait() for filepage. */ 1258 static int stat_jwait_freeblks; /* Times blocked in jwait() for freeblks. */ 1259 static int stat_jwait_inode; /* Times blocked in jwait() for inodes. */ 1260 static int stat_jwait_newblk; /* Times blocked in jwait() for newblks. */ 1261 static int stat_cleanup_high_delay; /* Maximum cleanup delay (in ticks) */ 1262 static int stat_cleanup_blkrequests; /* Number of block cleanup requests */ 1263 static int stat_cleanup_inorequests; /* Number of inode cleanup requests */ 1264 static int stat_cleanup_retries; /* Number of cleanups that needed to flush */ 1265 static int stat_cleanup_failures; /* Number of cleanup requests that failed */ 1266 static int stat_emptyjblocks; /* Number of potentially empty journal blocks */ 1267 1268 SYSCTL_INT(_debug_softdep, OID_AUTO, max_softdeps, CTLFLAG_RW, 1269 &max_softdeps, 0, ""); 1270 SYSCTL_INT(_debug_softdep, OID_AUTO, tickdelay, CTLFLAG_RW, 1271 &tickdelay, 0, ""); 1272 SYSCTL_INT(_debug_softdep, OID_AUTO, flush_threads, CTLFLAG_RD, 1273 &stat_flush_threads, 0, ""); 1274 SYSCTL_INT(_debug_softdep, OID_AUTO, worklist_push, 1275 CTLFLAG_RW | CTLFLAG_STATS, &stat_worklist_push, 0,""); 1276 SYSCTL_INT(_debug_softdep, OID_AUTO, delayed_inactivations, CTLFLAG_RD, 1277 &stat_delayed_inact, 0, ""); 1278 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_push, 1279 CTLFLAG_RW | CTLFLAG_STATS, &stat_blk_limit_push, 0,""); 1280 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_push, 1281 CTLFLAG_RW | CTLFLAG_STATS, &stat_ino_limit_push, 0,""); 1282 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_hit, 1283 CTLFLAG_RW | CTLFLAG_STATS, &stat_blk_limit_hit, 0, ""); 1284 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_hit, 1285 CTLFLAG_RW | CTLFLAG_STATS, &stat_ino_limit_hit, 0, ""); 1286 SYSCTL_INT(_debug_softdep, OID_AUTO, sync_limit_hit, 1287 CTLFLAG_RW | CTLFLAG_STATS, &stat_sync_limit_hit, 0, ""); 1288 SYSCTL_INT(_debug_softdep, OID_AUTO, indir_blk_ptrs, 1289 CTLFLAG_RW | CTLFLAG_STATS, &stat_indir_blk_ptrs, 0, ""); 1290 SYSCTL_INT(_debug_softdep, OID_AUTO, inode_bitmap, 1291 CTLFLAG_RW | CTLFLAG_STATS, &stat_inode_bitmap, 0, ""); 1292 SYSCTL_INT(_debug_softdep, OID_AUTO, direct_blk_ptrs, 1293 CTLFLAG_RW | CTLFLAG_STATS, &stat_direct_blk_ptrs, 0, ""); 1294 SYSCTL_INT(_debug_softdep, OID_AUTO, dir_entry, 1295 CTLFLAG_RW | CTLFLAG_STATS, &stat_dir_entry, 0, ""); 1296 SYSCTL_INT(_debug_softdep, OID_AUTO, jaddref_rollback, 1297 CTLFLAG_RW | CTLFLAG_STATS, &stat_jaddref, 0, ""); 1298 SYSCTL_INT(_debug_softdep, OID_AUTO, jnewblk_rollback, 1299 CTLFLAG_RW | CTLFLAG_STATS, &stat_jnewblk, 0, ""); 1300 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_low, 1301 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_low, 0, ""); 1302 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_min, 1303 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_min, 0, ""); 1304 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_wait, 1305 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_wait, 0, ""); 1306 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_filepage, 1307 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_filepage, 0, ""); 1308 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_freeblks, 1309 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_freeblks, 0, ""); 1310 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_inode, 1311 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_inode, 0, ""); 1312 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_newblk, 1313 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_newblk, 0, ""); 1314 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_blkrequests, 1315 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_blkrequests, 0, ""); 1316 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_inorequests, 1317 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_inorequests, 0, ""); 1318 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_high_delay, 1319 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_high_delay, 0, ""); 1320 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_retries, 1321 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_retries, 0, ""); 1322 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_failures, 1323 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_failures, 0, ""); 1324 1325 SYSCTL_INT(_debug_softdep, OID_AUTO, flushcache, CTLFLAG_RW, 1326 &softdep_flushcache, 0, ""); 1327 SYSCTL_INT(_debug_softdep, OID_AUTO, emptyjblocks, CTLFLAG_RD, 1328 &stat_emptyjblocks, 0, ""); 1329 1330 SYSCTL_DECL(_vfs_ffs); 1331 1332 /* Whether to recompute the summary at mount time */ 1333 static int compute_summary_at_mount = 0; 1334 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW, 1335 &compute_summary_at_mount, 0, "Recompute summary at mount"); 1336 static int print_threads = 0; 1337 SYSCTL_INT(_debug_softdep, OID_AUTO, print_threads, CTLFLAG_RW, 1338 &print_threads, 0, "Notify flusher thread start/stop"); 1339 1340 /* List of all filesystems mounted with soft updates */ 1341 static TAILQ_HEAD(, mount_softdeps) softdepmounts; 1342 1343 static void 1344 get_parent_vp_unlock_bp(struct mount *mp, 1345 struct buf *bp, 1346 struct diraddhd *diraddhdp, 1347 struct diraddhd *unfinishedp) 1348 { 1349 struct diradd *dap; 1350 1351 /* 1352 * Requeue unfinished dependencies before 1353 * unlocking buffer, which could make 1354 * diraddhdp invalid. 1355 */ 1356 ACQUIRE_LOCK(VFSTOUFS(mp)); 1357 while ((dap = LIST_FIRST(unfinishedp)) != NULL) { 1358 LIST_REMOVE(dap, da_pdlist); 1359 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist); 1360 } 1361 FREE_LOCK(VFSTOUFS(mp)); 1362 1363 bp->b_vflags &= ~BV_SCANNED; 1364 BUF_NOREC(bp); 1365 BUF_UNLOCK(bp); 1366 } 1367 1368 /* 1369 * This function fetches inode inum on mount point mp. We already 1370 * hold a locked vnode vp, and might have a locked buffer bp belonging 1371 * to vp. 1372 1373 * We must not block on acquiring the new inode lock as we will get 1374 * into a lock-order reversal with the buffer lock and possibly get a 1375 * deadlock. Thus if we cannot instantiate the requested vnode 1376 * without sleeping on its lock, we must unlock the vnode and the 1377 * buffer before doing a blocking on the vnode lock. We return 1378 * ERELOOKUP if we have had to unlock either the vnode or the buffer so 1379 * that the caller can reassess its state. 1380 * 1381 * Top-level VFS code (for syscalls and other consumers, e.g. callers 1382 * of VOP_FSYNC() in syncer) check for ERELOOKUP and restart at safe 1383 * point. 1384 * 1385 * Since callers expect to operate on fully constructed vnode, we also 1386 * recheck v_data after relock, and return ENOENT if NULL. 1387 * 1388 * If unlocking bp, we must unroll dequeueing its unfinished 1389 * dependencies, and clear scan flag, before unlocking. If unlocking 1390 * vp while it is under deactivation, we re-queue deactivation. 1391 */ 1392 static int 1393 get_parent_vp(struct vnode *vp, 1394 struct mount *mp, 1395 ino_t inum, 1396 struct buf *bp, 1397 struct diraddhd *diraddhdp, 1398 struct diraddhd *unfinishedp, 1399 struct vnode **rvp) 1400 { 1401 struct vnode *pvp; 1402 int error; 1403 bool bplocked; 1404 1405 ASSERT_VOP_ELOCKED(vp, "child vnode must be locked"); 1406 for (bplocked = true, pvp = NULL;;) { 1407 error = ffs_vgetf(mp, inum, LK_EXCLUSIVE | LK_NOWAIT, &pvp, 1408 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP); 1409 if (error == 0) { 1410 /* 1411 * Since we could have unlocked vp, the inode 1412 * number could no longer indicate a 1413 * constructed node. In this case, we must 1414 * restart the syscall. 1415 */ 1416 if (VTOI(pvp)->i_mode == 0 || !bplocked) { 1417 if (bp != NULL && bplocked) 1418 get_parent_vp_unlock_bp(mp, bp, 1419 diraddhdp, unfinishedp); 1420 if (VTOI(pvp)->i_mode == 0) 1421 vgone(pvp); 1422 error = ERELOOKUP; 1423 goto out2; 1424 } 1425 goto out1; 1426 } 1427 if (bp != NULL && bplocked) { 1428 get_parent_vp_unlock_bp(mp, bp, diraddhdp, unfinishedp); 1429 bplocked = false; 1430 } 1431 1432 /* 1433 * Do not drop vnode lock while inactivating during 1434 * vunref. This would result in leaks of the VI flags 1435 * and reclaiming of non-truncated vnode. Instead, 1436 * re-schedule inactivation hoping that we would be 1437 * able to sync inode later. 1438 */ 1439 if ((vp->v_iflag & VI_DOINGINACT) != 0 && 1440 (vp->v_vflag & VV_UNREF) != 0) { 1441 VI_LOCK(vp); 1442 vp->v_iflag |= VI_OWEINACT; 1443 VI_UNLOCK(vp); 1444 return (ERELOOKUP); 1445 } 1446 1447 VOP_UNLOCK(vp); 1448 error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &pvp, 1449 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP); 1450 if (error != 0) { 1451 MPASS(error != ERELOOKUP); 1452 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1453 break; 1454 } 1455 if (VTOI(pvp)->i_mode == 0) { 1456 vgone(pvp); 1457 vput(pvp); 1458 pvp = NULL; 1459 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1460 error = ERELOOKUP; 1461 break; 1462 } 1463 error = vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT); 1464 if (error == 0) 1465 break; 1466 vput(pvp); 1467 pvp = NULL; 1468 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1469 if (vp->v_data == NULL) { 1470 error = ENOENT; 1471 break; 1472 } 1473 } 1474 if (bp != NULL) { 1475 MPASS(!bplocked); 1476 error = ERELOOKUP; 1477 } 1478 out2: 1479 if (error != 0 && pvp != NULL) { 1480 vput(pvp); 1481 pvp = NULL; 1482 } 1483 out1: 1484 *rvp = pvp; 1485 ASSERT_VOP_ELOCKED(vp, "child vnode must be locked on return"); 1486 return (error); 1487 } 1488 1489 /* 1490 * This function cleans the worklist for a filesystem. 1491 * Each filesystem running with soft dependencies gets its own 1492 * thread to run in this function. The thread is started up in 1493 * softdep_mount and shutdown in softdep_unmount. They show up 1494 * as part of the kernel "bufdaemon" process whose process 1495 * entry is available in bufdaemonproc. 1496 */ 1497 static int searchfailed; 1498 extern struct proc *bufdaemonproc; 1499 static void 1500 softdep_flush(void *addr) 1501 { 1502 struct mount *mp; 1503 struct thread *td; 1504 struct ufsmount *ump; 1505 int cleanups; 1506 1507 td = curthread; 1508 td->td_pflags |= TDP_NORUNNINGBUF; 1509 mp = (struct mount *)addr; 1510 ump = VFSTOUFS(mp); 1511 atomic_add_int(&stat_flush_threads, 1); 1512 ACQUIRE_LOCK(ump); 1513 ump->softdep_flags &= ~FLUSH_STARTING; 1514 wakeup(&ump->softdep_flushtd); 1515 FREE_LOCK(ump); 1516 if (print_threads) { 1517 if (stat_flush_threads == 1) 1518 printf("Running %s at pid %d\n", bufdaemonproc->p_comm, 1519 bufdaemonproc->p_pid); 1520 printf("Start thread %s\n", td->td_name); 1521 } 1522 for (;;) { 1523 while (softdep_process_worklist(mp, 0) > 0 || 1524 (MOUNTEDSUJ(mp) && 1525 VFSTOUFS(mp)->softdep_jblocks->jb_suspended)) 1526 kthread_suspend_check(); 1527 ACQUIRE_LOCK(ump); 1528 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1529 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, 1530 "sdflush", hz / 2); 1531 ump->softdep_flags &= ~FLUSH_CLEANUP; 1532 /* 1533 * Check to see if we are done and need to exit. 1534 */ 1535 if ((ump->softdep_flags & FLUSH_EXIT) == 0) { 1536 FREE_LOCK(ump); 1537 continue; 1538 } 1539 ump->softdep_flags &= ~FLUSH_EXIT; 1540 cleanups = ump->um_softdep->sd_cleanups; 1541 FREE_LOCK(ump); 1542 wakeup(&ump->softdep_flags); 1543 if (print_threads) { 1544 printf("Stop thread %s: searchfailed %d, " 1545 "did cleanups %d\n", 1546 td->td_name, searchfailed, cleanups); 1547 } 1548 atomic_subtract_int(&stat_flush_threads, 1); 1549 kthread_exit(); 1550 panic("kthread_exit failed\n"); 1551 } 1552 } 1553 1554 static void 1555 worklist_speedup(struct mount *mp) 1556 { 1557 struct ufsmount *ump; 1558 1559 ump = VFSTOUFS(mp); 1560 LOCK_OWNED(ump); 1561 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1562 ump->softdep_flags |= FLUSH_CLEANUP; 1563 wakeup(&ump->softdep_flushtd); 1564 } 1565 1566 static void 1567 softdep_send_speedup(struct ufsmount *ump, 1568 off_t shortage, 1569 uint64_t flags) 1570 { 1571 struct buf *bp; 1572 1573 if ((ump->um_flags & UM_CANSPEEDUP) == 0) 1574 return; 1575 1576 bp = malloc(sizeof(*bp), M_TRIM, M_WAITOK | M_ZERO); 1577 bp->b_iocmd = BIO_SPEEDUP; 1578 bp->b_ioflags = flags; 1579 bp->b_bcount = omin(shortage, LONG_MAX); 1580 g_vfs_strategy(ump->um_bo, bp); 1581 bufwait(bp); 1582 free(bp, M_TRIM); 1583 } 1584 1585 static int 1586 softdep_speedup(struct ufsmount *ump) 1587 { 1588 struct ufsmount *altump; 1589 struct mount_softdeps *sdp; 1590 1591 LOCK_OWNED(ump); 1592 worklist_speedup(ump->um_mountp); 1593 bd_speedup(); 1594 /* 1595 * If we have global shortages, then we need other 1596 * filesystems to help with the cleanup. Here we wakeup a 1597 * flusher thread for a filesystem that is over its fair 1598 * share of resources. 1599 */ 1600 if (req_clear_inodedeps || req_clear_remove) { 1601 ACQUIRE_GBLLOCK(&lk); 1602 TAILQ_FOREACH(sdp, &softdepmounts, sd_next) { 1603 if ((altump = sdp->sd_ump) == ump) 1604 continue; 1605 if (((req_clear_inodedeps && 1606 altump->softdep_curdeps[D_INODEDEP] > 1607 max_softdeps / stat_flush_threads) || 1608 (req_clear_remove && 1609 altump->softdep_curdeps[D_DIRREM] > 1610 (max_softdeps / 2) / stat_flush_threads)) && 1611 TRY_ACQUIRE_LOCK(altump)) 1612 break; 1613 } 1614 if (sdp == NULL) { 1615 searchfailed++; 1616 FREE_GBLLOCK(&lk); 1617 } else { 1618 /* 1619 * Move to the end of the list so we pick a 1620 * different one on out next try. 1621 */ 1622 TAILQ_REMOVE(&softdepmounts, sdp, sd_next); 1623 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next); 1624 FREE_GBLLOCK(&lk); 1625 if ((altump->softdep_flags & 1626 (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1627 altump->softdep_flags |= FLUSH_CLEANUP; 1628 altump->um_softdep->sd_cleanups++; 1629 wakeup(&altump->softdep_flushtd); 1630 FREE_LOCK(altump); 1631 } 1632 } 1633 return (speedup_syncer()); 1634 } 1635 1636 /* 1637 * Add an item to the end of the work queue. 1638 * This routine requires that the lock be held. 1639 * This is the only routine that adds items to the list. 1640 * The following routine is the only one that removes items 1641 * and does so in order from first to last. 1642 */ 1643 1644 #define WK_HEAD 0x0001 /* Add to HEAD. */ 1645 #define WK_NODELAY 0x0002 /* Process immediately. */ 1646 1647 static void 1648 add_to_worklist(struct worklist *wk, int flags) 1649 { 1650 struct ufsmount *ump; 1651 1652 ump = VFSTOUFS(wk->wk_mp); 1653 LOCK_OWNED(ump); 1654 if (wk->wk_state & ONWORKLIST) 1655 panic("add_to_worklist: %s(0x%X) already on list", 1656 TYPENAME(wk->wk_type), wk->wk_state); 1657 wk->wk_state |= ONWORKLIST; 1658 if (ump->softdep_on_worklist == 0) { 1659 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1660 ump->softdep_worklist_tail = wk; 1661 } else if (flags & WK_HEAD) { 1662 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1663 } else { 1664 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list); 1665 ump->softdep_worklist_tail = wk; 1666 } 1667 ump->softdep_on_worklist += 1; 1668 if (flags & WK_NODELAY) 1669 worklist_speedup(wk->wk_mp); 1670 } 1671 1672 /* 1673 * Remove the item to be processed. If we are removing the last 1674 * item on the list, we need to recalculate the tail pointer. 1675 */ 1676 static void 1677 remove_from_worklist(struct worklist *wk) 1678 { 1679 struct ufsmount *ump; 1680 1681 ump = VFSTOUFS(wk->wk_mp); 1682 if (ump->softdep_worklist_tail == wk) 1683 ump->softdep_worklist_tail = 1684 (struct worklist *)wk->wk_list.le_prev; 1685 WORKLIST_REMOVE(wk); 1686 ump->softdep_on_worklist -= 1; 1687 } 1688 1689 static void 1690 wake_worklist(struct worklist *wk) 1691 { 1692 if (wk->wk_state & IOWAITING) { 1693 wk->wk_state &= ~IOWAITING; 1694 wakeup(wk); 1695 } 1696 } 1697 1698 static void 1699 wait_worklist(struct worklist *wk, char *wmesg) 1700 { 1701 struct ufsmount *ump; 1702 1703 ump = VFSTOUFS(wk->wk_mp); 1704 wk->wk_state |= IOWAITING; 1705 msleep(wk, LOCK_PTR(ump), PVM, wmesg, 0); 1706 } 1707 1708 /* 1709 * Process that runs once per second to handle items in the background queue. 1710 * 1711 * Note that we ensure that everything is done in the order in which they 1712 * appear in the queue. The code below depends on this property to ensure 1713 * that blocks of a file are freed before the inode itself is freed. This 1714 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated 1715 * until all the old ones have been purged from the dependency lists. 1716 */ 1717 static int 1718 softdep_process_worklist(struct mount *mp, int full) 1719 { 1720 int cnt, matchcnt; 1721 struct ufsmount *ump; 1722 long starttime; 1723 1724 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp")); 1725 ump = VFSTOUFS(mp); 1726 if (ump->um_softdep == NULL) 1727 return (0); 1728 matchcnt = 0; 1729 ACQUIRE_LOCK(ump); 1730 starttime = time_second; 1731 softdep_process_journal(mp, NULL, full ? MNT_WAIT : 0); 1732 check_clear_deps(mp); 1733 while (ump->softdep_on_worklist > 0) { 1734 if ((cnt = process_worklist_item(mp, 10, LK_NOWAIT)) == 0) 1735 break; 1736 else 1737 matchcnt += cnt; 1738 check_clear_deps(mp); 1739 /* 1740 * We do not generally want to stop for buffer space, but if 1741 * we are really being a buffer hog, we will stop and wait. 1742 */ 1743 if (should_yield()) { 1744 FREE_LOCK(ump); 1745 kern_yield(PRI_USER); 1746 bwillwrite(); 1747 ACQUIRE_LOCK(ump); 1748 } 1749 /* 1750 * Never allow processing to run for more than one 1751 * second. This gives the syncer thread the opportunity 1752 * to pause if appropriate. 1753 */ 1754 if (!full && starttime != time_second) 1755 break; 1756 } 1757 if (full == 0) 1758 journal_unsuspend(ump); 1759 FREE_LOCK(ump); 1760 return (matchcnt); 1761 } 1762 1763 /* 1764 * Process all removes associated with a vnode if we are running out of 1765 * journal space. Any other process which attempts to flush these will 1766 * be unable as we have the vnodes locked. 1767 */ 1768 static void 1769 process_removes(struct vnode *vp) 1770 { 1771 struct inodedep *inodedep; 1772 struct dirrem *dirrem; 1773 struct ufsmount *ump; 1774 struct mount *mp; 1775 ino_t inum; 1776 1777 mp = vp->v_mount; 1778 ump = VFSTOUFS(mp); 1779 LOCK_OWNED(ump); 1780 inum = VTOI(vp)->i_number; 1781 for (;;) { 1782 top: 1783 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1784 return; 1785 LIST_FOREACH(dirrem, &inodedep->id_dirremhd, dm_inonext) { 1786 /* 1787 * If another thread is trying to lock this vnode 1788 * it will fail but we must wait for it to do so 1789 * before we can proceed. 1790 */ 1791 if (dirrem->dm_state & INPROGRESS) { 1792 wait_worklist(&dirrem->dm_list, "pwrwait"); 1793 goto top; 1794 } 1795 if ((dirrem->dm_state & (COMPLETE | ONWORKLIST)) == 1796 (COMPLETE | ONWORKLIST)) 1797 break; 1798 } 1799 if (dirrem == NULL) 1800 return; 1801 remove_from_worklist(&dirrem->dm_list); 1802 FREE_LOCK(ump); 1803 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1804 panic("process_removes: suspended filesystem"); 1805 handle_workitem_remove(dirrem, 0); 1806 vn_finished_secondary_write(mp); 1807 ACQUIRE_LOCK(ump); 1808 } 1809 } 1810 1811 /* 1812 * Process all truncations associated with a vnode if we are running out 1813 * of journal space. This is called when the vnode lock is already held 1814 * and no other process can clear the truncation. This function returns 1815 * a value greater than zero if it did any work. 1816 */ 1817 static void 1818 process_truncates(struct vnode *vp) 1819 { 1820 struct inodedep *inodedep; 1821 struct freeblks *freeblks; 1822 struct ufsmount *ump; 1823 struct mount *mp; 1824 ino_t inum; 1825 int cgwait; 1826 1827 mp = vp->v_mount; 1828 ump = VFSTOUFS(mp); 1829 LOCK_OWNED(ump); 1830 inum = VTOI(vp)->i_number; 1831 for (;;) { 1832 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1833 return; 1834 cgwait = 0; 1835 TAILQ_FOREACH(freeblks, &inodedep->id_freeblklst, fb_next) { 1836 /* Journal entries not yet written. */ 1837 if (!LIST_EMPTY(&freeblks->fb_jblkdephd)) { 1838 jwait(&LIST_FIRST( 1839 &freeblks->fb_jblkdephd)->jb_list, 1840 MNT_WAIT); 1841 break; 1842 } 1843 /* Another thread is executing this item. */ 1844 if (freeblks->fb_state & INPROGRESS) { 1845 wait_worklist(&freeblks->fb_list, "ptrwait"); 1846 break; 1847 } 1848 /* Freeblks is waiting on a inode write. */ 1849 if ((freeblks->fb_state & COMPLETE) == 0) { 1850 FREE_LOCK(ump); 1851 ffs_update(vp, 1); 1852 ACQUIRE_LOCK(ump); 1853 break; 1854 } 1855 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST)) == 1856 (ALLCOMPLETE | ONWORKLIST)) { 1857 remove_from_worklist(&freeblks->fb_list); 1858 freeblks->fb_state |= INPROGRESS; 1859 FREE_LOCK(ump); 1860 if (vn_start_secondary_write(NULL, &mp, 1861 V_NOWAIT)) 1862 panic("process_truncates: " 1863 "suspended filesystem"); 1864 handle_workitem_freeblocks(freeblks, 0); 1865 vn_finished_secondary_write(mp); 1866 ACQUIRE_LOCK(ump); 1867 break; 1868 } 1869 if (freeblks->fb_cgwait) 1870 cgwait++; 1871 } 1872 if (cgwait) { 1873 FREE_LOCK(ump); 1874 sync_cgs(mp, MNT_WAIT); 1875 ffs_sync_snap(mp, MNT_WAIT); 1876 ACQUIRE_LOCK(ump); 1877 continue; 1878 } 1879 if (freeblks == NULL) 1880 break; 1881 } 1882 return; 1883 } 1884 1885 /* 1886 * Process one item on the worklist. 1887 */ 1888 static int 1889 process_worklist_item(struct mount *mp, 1890 int target, 1891 int flags) 1892 { 1893 struct worklist sentinel; 1894 struct worklist *wk; 1895 struct ufsmount *ump; 1896 int matchcnt; 1897 int error; 1898 1899 KASSERT(mp != NULL, ("process_worklist_item: NULL mp")); 1900 /* 1901 * If we are being called because of a process doing a 1902 * copy-on-write, then it is not safe to write as we may 1903 * recurse into the copy-on-write routine. 1904 */ 1905 if (curthread->td_pflags & TDP_COWINPROGRESS) 1906 return (-1); 1907 ump = VFSTOUFS(mp); 1908 LOCK_OWNED(ump); 1909 matchcnt = 0; 1910 sentinel.wk_mp = NULL; 1911 sentinel.wk_type = D_SENTINEL; 1912 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, &sentinel, wk_list); 1913 for (wk = LIST_NEXT(&sentinel, wk_list); wk != NULL; 1914 wk = LIST_NEXT(&sentinel, wk_list)) { 1915 if (wk->wk_type == D_SENTINEL) { 1916 LIST_REMOVE(&sentinel, wk_list); 1917 LIST_INSERT_AFTER(wk, &sentinel, wk_list); 1918 continue; 1919 } 1920 if (wk->wk_state & INPROGRESS) 1921 panic("process_worklist_item: %p already in progress.", 1922 wk); 1923 wk->wk_state |= INPROGRESS; 1924 remove_from_worklist(wk); 1925 FREE_LOCK(ump); 1926 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1927 panic("process_worklist_item: suspended filesystem"); 1928 switch (wk->wk_type) { 1929 case D_DIRREM: 1930 /* removal of a directory entry */ 1931 error = handle_workitem_remove(WK_DIRREM(wk), flags); 1932 break; 1933 1934 case D_FREEBLKS: 1935 /* releasing blocks and/or fragments from a file */ 1936 error = handle_workitem_freeblocks(WK_FREEBLKS(wk), 1937 flags); 1938 break; 1939 1940 case D_FREEFRAG: 1941 /* releasing a fragment when replaced as a file grows */ 1942 handle_workitem_freefrag(WK_FREEFRAG(wk)); 1943 error = 0; 1944 break; 1945 1946 case D_FREEFILE: 1947 /* releasing an inode when its link count drops to 0 */ 1948 handle_workitem_freefile(WK_FREEFILE(wk)); 1949 error = 0; 1950 break; 1951 1952 default: 1953 panic("%s_process_worklist: Unknown type %s", 1954 "softdep", TYPENAME(wk->wk_type)); 1955 /* NOTREACHED */ 1956 } 1957 vn_finished_secondary_write(mp); 1958 ACQUIRE_LOCK(ump); 1959 if (error == 0) { 1960 if (++matchcnt == target) 1961 break; 1962 continue; 1963 } 1964 /* 1965 * We have to retry the worklist item later. Wake up any 1966 * waiters who may be able to complete it immediately and 1967 * add the item back to the head so we don't try to execute 1968 * it again. 1969 */ 1970 wk->wk_state &= ~INPROGRESS; 1971 wake_worklist(wk); 1972 add_to_worklist(wk, WK_HEAD); 1973 } 1974 /* Sentinal could've become the tail from remove_from_worklist. */ 1975 if (ump->softdep_worklist_tail == &sentinel) 1976 ump->softdep_worklist_tail = 1977 (struct worklist *)sentinel.wk_list.le_prev; 1978 LIST_REMOVE(&sentinel, wk_list); 1979 return (matchcnt); 1980 } 1981 1982 /* 1983 * Move dependencies from one buffer to another. 1984 */ 1985 int 1986 softdep_move_dependencies(struct buf *oldbp, struct buf *newbp) 1987 { 1988 struct worklist *wk, *wktail; 1989 struct ufsmount *ump; 1990 int dirty; 1991 1992 if ((wk = LIST_FIRST(&oldbp->b_dep)) == NULL) 1993 return (0); 1994 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 1995 ("softdep_move_dependencies called on non-softdep filesystem")); 1996 dirty = 0; 1997 wktail = NULL; 1998 ump = VFSTOUFS(wk->wk_mp); 1999 ACQUIRE_LOCK(ump); 2000 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) { 2001 LIST_REMOVE(wk, wk_list); 2002 if (wk->wk_type == D_BMSAFEMAP && 2003 bmsafemap_backgroundwrite(WK_BMSAFEMAP(wk), newbp)) 2004 dirty = 1; 2005 if (wktail == NULL) 2006 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list); 2007 else 2008 LIST_INSERT_AFTER(wktail, wk, wk_list); 2009 wktail = wk; 2010 } 2011 FREE_LOCK(ump); 2012 2013 return (dirty); 2014 } 2015 2016 /* 2017 * Purge the work list of all items associated with a particular mount point. 2018 */ 2019 int 2020 softdep_flushworklist(struct mount *oldmnt, 2021 int *countp, 2022 struct thread *td) 2023 { 2024 struct vnode *devvp; 2025 struct ufsmount *ump; 2026 int count, error; 2027 2028 /* 2029 * Alternately flush the block device associated with the mount 2030 * point and process any dependencies that the flushing 2031 * creates. We continue until no more worklist dependencies 2032 * are found. 2033 */ 2034 *countp = 0; 2035 error = 0; 2036 ump = VFSTOUFS(oldmnt); 2037 devvp = ump->um_devvp; 2038 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) { 2039 *countp += count; 2040 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 2041 error = VOP_FSYNC(devvp, MNT_WAIT, td); 2042 VOP_UNLOCK(devvp); 2043 if (error != 0) 2044 break; 2045 } 2046 return (error); 2047 } 2048 2049 #define SU_WAITIDLE_RETRIES 20 2050 static int 2051 softdep_waitidle(struct mount *mp, int flags __unused) 2052 { 2053 struct ufsmount *ump; 2054 struct vnode *devvp; 2055 struct thread *td; 2056 int error, i; 2057 2058 ump = VFSTOUFS(mp); 2059 KASSERT(ump->um_softdep != NULL, 2060 ("softdep_waitidle called on non-softdep filesystem")); 2061 devvp = ump->um_devvp; 2062 td = curthread; 2063 error = 0; 2064 ACQUIRE_LOCK(ump); 2065 for (i = 0; i < SU_WAITIDLE_RETRIES && ump->softdep_deps != 0; i++) { 2066 ump->softdep_req = 1; 2067 KASSERT((flags & FORCECLOSE) == 0 || 2068 ump->softdep_on_worklist == 0, 2069 ("softdep_waitidle: work added after flush")); 2070 msleep(&ump->softdep_deps, LOCK_PTR(ump), PVM | PDROP, 2071 "softdeps", 10 * hz); 2072 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 2073 error = VOP_FSYNC(devvp, MNT_WAIT, td); 2074 VOP_UNLOCK(devvp); 2075 ACQUIRE_LOCK(ump); 2076 if (error != 0) 2077 break; 2078 } 2079 ump->softdep_req = 0; 2080 if (i == SU_WAITIDLE_RETRIES && error == 0 && ump->softdep_deps != 0) { 2081 error = EBUSY; 2082 printf("softdep_waitidle: Failed to flush worklist for %p\n", 2083 mp); 2084 } 2085 FREE_LOCK(ump); 2086 return (error); 2087 } 2088 2089 /* 2090 * Flush all vnodes and worklist items associated with a specified mount point. 2091 */ 2092 int 2093 softdep_flushfiles(struct mount *oldmnt, 2094 int flags, 2095 struct thread *td) 2096 { 2097 struct ufsmount *ump __unused; 2098 #ifdef QUOTA 2099 int i; 2100 #endif 2101 int error, early, depcount, loopcnt, retry_flush_count, retry; 2102 int morework; 2103 2104 ump = VFSTOUFS(oldmnt); 2105 KASSERT(ump->um_softdep != NULL, 2106 ("softdep_flushfiles called on non-softdep filesystem")); 2107 loopcnt = 10; 2108 retry_flush_count = 3; 2109 retry_flush: 2110 error = 0; 2111 2112 /* 2113 * Alternately flush the vnodes associated with the mount 2114 * point and process any dependencies that the flushing 2115 * creates. In theory, this loop can happen at most twice, 2116 * but we give it a few extra just to be sure. 2117 */ 2118 for (; loopcnt > 0; loopcnt--) { 2119 /* 2120 * Do another flush in case any vnodes were brought in 2121 * as part of the cleanup operations. 2122 */ 2123 early = retry_flush_count == 1 || (oldmnt->mnt_kern_flag & 2124 MNTK_UNMOUNT) == 0 ? 0 : EARLYFLUSH; 2125 if ((error = ffs_flushfiles(oldmnt, flags | early, td)) != 0) 2126 break; 2127 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 || 2128 depcount == 0) 2129 break; 2130 } 2131 /* 2132 * If we are unmounting then it is an error to fail. If we 2133 * are simply trying to downgrade to read-only, then filesystem 2134 * activity can keep us busy forever, so we just fail with EBUSY. 2135 */ 2136 if (loopcnt == 0) { 2137 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) 2138 panic("softdep_flushfiles: looping"); 2139 error = EBUSY; 2140 } 2141 if (!error) 2142 error = softdep_waitidle(oldmnt, flags); 2143 if (!error) { 2144 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) { 2145 retry = 0; 2146 MNT_ILOCK(oldmnt); 2147 morework = oldmnt->mnt_nvnodelistsize > 0; 2148 #ifdef QUOTA 2149 UFS_LOCK(ump); 2150 for (i = 0; i < MAXQUOTAS; i++) { 2151 if (ump->um_quotas[i] != NULLVP) 2152 morework = 1; 2153 } 2154 UFS_UNLOCK(ump); 2155 #endif 2156 if (morework) { 2157 if (--retry_flush_count > 0) { 2158 retry = 1; 2159 loopcnt = 3; 2160 } else 2161 error = EBUSY; 2162 } 2163 MNT_IUNLOCK(oldmnt); 2164 if (retry) 2165 goto retry_flush; 2166 } 2167 } 2168 return (error); 2169 } 2170 2171 /* 2172 * Structure hashing. 2173 * 2174 * There are four types of structures that can be looked up: 2175 * 1) pagedep structures identified by mount point, inode number, 2176 * and logical block. 2177 * 2) inodedep structures identified by mount point and inode number. 2178 * 3) newblk structures identified by mount point and 2179 * physical block number. 2180 * 4) bmsafemap structures identified by mount point and 2181 * cylinder group number. 2182 * 2183 * The "pagedep" and "inodedep" dependency structures are hashed 2184 * separately from the file blocks and inodes to which they correspond. 2185 * This separation helps when the in-memory copy of an inode or 2186 * file block must be replaced. It also obviates the need to access 2187 * an inode or file page when simply updating (or de-allocating) 2188 * dependency structures. Lookup of newblk structures is needed to 2189 * find newly allocated blocks when trying to associate them with 2190 * their allocdirect or allocindir structure. 2191 * 2192 * The lookup routines optionally create and hash a new instance when 2193 * an existing entry is not found. The bmsafemap lookup routine always 2194 * allocates a new structure if an existing one is not found. 2195 */ 2196 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */ 2197 2198 /* 2199 * Structures and routines associated with pagedep caching. 2200 */ 2201 #define PAGEDEP_HASH(ump, inum, lbn) \ 2202 (&(ump)->pagedep_hashtbl[((inum) + (lbn)) & (ump)->pagedep_hash_size]) 2203 2204 static int 2205 pagedep_find(struct pagedep_hashhead *pagedephd, 2206 ino_t ino, 2207 ufs_lbn_t lbn, 2208 struct pagedep **pagedeppp) 2209 { 2210 struct pagedep *pagedep; 2211 2212 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 2213 if (ino == pagedep->pd_ino && lbn == pagedep->pd_lbn) { 2214 *pagedeppp = pagedep; 2215 return (1); 2216 } 2217 } 2218 *pagedeppp = NULL; 2219 return (0); 2220 } 2221 /* 2222 * Look up a pagedep. Return 1 if found, 0 otherwise. 2223 * If not found, allocate if DEPALLOC flag is passed. 2224 * Found or allocated entry is returned in pagedeppp. 2225 */ 2226 static int 2227 pagedep_lookup(struct mount *mp, 2228 struct buf *bp, 2229 ino_t ino, 2230 ufs_lbn_t lbn, 2231 int flags, 2232 struct pagedep **pagedeppp) 2233 { 2234 struct pagedep *pagedep; 2235 struct pagedep_hashhead *pagedephd; 2236 struct worklist *wk; 2237 struct ufsmount *ump; 2238 int ret; 2239 int i; 2240 2241 ump = VFSTOUFS(mp); 2242 LOCK_OWNED(ump); 2243 if (bp) { 2244 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 2245 if (wk->wk_type == D_PAGEDEP) { 2246 *pagedeppp = WK_PAGEDEP(wk); 2247 return (1); 2248 } 2249 } 2250 } 2251 pagedephd = PAGEDEP_HASH(ump, ino, lbn); 2252 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp); 2253 if (ret) { 2254 if (((*pagedeppp)->pd_state & ONWORKLIST) == 0 && bp) 2255 WORKLIST_INSERT(&bp->b_dep, &(*pagedeppp)->pd_list); 2256 return (1); 2257 } 2258 if ((flags & DEPALLOC) == 0) 2259 return (0); 2260 FREE_LOCK(ump); 2261 pagedep = malloc(sizeof(struct pagedep), 2262 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO); 2263 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp); 2264 ACQUIRE_LOCK(ump); 2265 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp); 2266 if (*pagedeppp) { 2267 /* 2268 * This should never happen since we only create pagedeps 2269 * with the vnode lock held. Could be an assert. 2270 */ 2271 WORKITEM_FREE(pagedep, D_PAGEDEP); 2272 return (ret); 2273 } 2274 pagedep->pd_ino = ino; 2275 pagedep->pd_lbn = lbn; 2276 LIST_INIT(&pagedep->pd_dirremhd); 2277 LIST_INIT(&pagedep->pd_pendinghd); 2278 for (i = 0; i < DAHASHSZ; i++) 2279 LIST_INIT(&pagedep->pd_diraddhd[i]); 2280 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash); 2281 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list); 2282 *pagedeppp = pagedep; 2283 return (0); 2284 } 2285 2286 /* 2287 * Structures and routines associated with inodedep caching. 2288 */ 2289 #define INODEDEP_HASH(ump, inum) \ 2290 (&(ump)->inodedep_hashtbl[(inum) & (ump)->inodedep_hash_size]) 2291 2292 static int 2293 inodedep_find(struct inodedep_hashhead *inodedephd, 2294 ino_t inum, 2295 struct inodedep **inodedeppp) 2296 { 2297 struct inodedep *inodedep; 2298 2299 LIST_FOREACH(inodedep, inodedephd, id_hash) 2300 if (inum == inodedep->id_ino) 2301 break; 2302 if (inodedep) { 2303 *inodedeppp = inodedep; 2304 return (1); 2305 } 2306 *inodedeppp = NULL; 2307 2308 return (0); 2309 } 2310 /* 2311 * Look up an inodedep. Return 1 if found, 0 if not found. 2312 * If not found, allocate if DEPALLOC flag is passed. 2313 * Found or allocated entry is returned in inodedeppp. 2314 */ 2315 static int 2316 inodedep_lookup(struct mount *mp, 2317 ino_t inum, 2318 int flags, 2319 struct inodedep **inodedeppp) 2320 { 2321 struct inodedep *inodedep; 2322 struct inodedep_hashhead *inodedephd; 2323 struct ufsmount *ump; 2324 struct fs *fs; 2325 2326 ump = VFSTOUFS(mp); 2327 LOCK_OWNED(ump); 2328 fs = ump->um_fs; 2329 inodedephd = INODEDEP_HASH(ump, inum); 2330 2331 if (inodedep_find(inodedephd, inum, inodedeppp)) 2332 return (1); 2333 if ((flags & DEPALLOC) == 0) 2334 return (0); 2335 /* 2336 * If the system is over its limit and our filesystem is 2337 * responsible for more than our share of that usage and 2338 * we are not in a rush, request some inodedep cleanup. 2339 */ 2340 if (softdep_excess_items(ump, D_INODEDEP)) 2341 schedule_cleanup(mp); 2342 else 2343 FREE_LOCK(ump); 2344 inodedep = malloc(sizeof(struct inodedep), 2345 M_INODEDEP, M_SOFTDEP_FLAGS); 2346 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp); 2347 ACQUIRE_LOCK(ump); 2348 if (inodedep_find(inodedephd, inum, inodedeppp)) { 2349 WORKITEM_FREE(inodedep, D_INODEDEP); 2350 return (1); 2351 } 2352 inodedep->id_fs = fs; 2353 inodedep->id_ino = inum; 2354 inodedep->id_state = ALLCOMPLETE; 2355 inodedep->id_nlinkdelta = 0; 2356 inodedep->id_nlinkwrote = -1; 2357 inodedep->id_savedino1 = NULL; 2358 inodedep->id_savedsize = -1; 2359 inodedep->id_savedextsize = -1; 2360 inodedep->id_savednlink = -1; 2361 inodedep->id_bmsafemap = NULL; 2362 inodedep->id_mkdiradd = NULL; 2363 LIST_INIT(&inodedep->id_dirremhd); 2364 LIST_INIT(&inodedep->id_pendinghd); 2365 LIST_INIT(&inodedep->id_inowait); 2366 LIST_INIT(&inodedep->id_bufwait); 2367 TAILQ_INIT(&inodedep->id_inoreflst); 2368 TAILQ_INIT(&inodedep->id_inoupdt); 2369 TAILQ_INIT(&inodedep->id_newinoupdt); 2370 TAILQ_INIT(&inodedep->id_extupdt); 2371 TAILQ_INIT(&inodedep->id_newextupdt); 2372 TAILQ_INIT(&inodedep->id_freeblklst); 2373 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash); 2374 *inodedeppp = inodedep; 2375 return (0); 2376 } 2377 2378 /* 2379 * Structures and routines associated with newblk caching. 2380 */ 2381 #define NEWBLK_HASH(ump, inum) \ 2382 (&(ump)->newblk_hashtbl[(inum) & (ump)->newblk_hash_size]) 2383 2384 static int 2385 newblk_find(struct newblk_hashhead *newblkhd, 2386 ufs2_daddr_t newblkno, 2387 int flags, 2388 struct newblk **newblkpp) 2389 { 2390 struct newblk *newblk; 2391 2392 LIST_FOREACH(newblk, newblkhd, nb_hash) { 2393 if (newblkno != newblk->nb_newblkno) 2394 continue; 2395 /* 2396 * If we're creating a new dependency don't match those that 2397 * have already been converted to allocdirects. This is for 2398 * a frag extend. 2399 */ 2400 if ((flags & DEPALLOC) && newblk->nb_list.wk_type != D_NEWBLK) 2401 continue; 2402 break; 2403 } 2404 if (newblk) { 2405 *newblkpp = newblk; 2406 return (1); 2407 } 2408 *newblkpp = NULL; 2409 return (0); 2410 } 2411 2412 /* 2413 * Look up a newblk. Return 1 if found, 0 if not found. 2414 * If not found, allocate if DEPALLOC flag is passed. 2415 * Found or allocated entry is returned in newblkpp. 2416 */ 2417 static int 2418 newblk_lookup(struct mount *mp, 2419 ufs2_daddr_t newblkno, 2420 int flags, 2421 struct newblk **newblkpp) 2422 { 2423 struct newblk *newblk; 2424 struct newblk_hashhead *newblkhd; 2425 struct ufsmount *ump; 2426 2427 ump = VFSTOUFS(mp); 2428 LOCK_OWNED(ump); 2429 newblkhd = NEWBLK_HASH(ump, newblkno); 2430 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) 2431 return (1); 2432 if ((flags & DEPALLOC) == 0) 2433 return (0); 2434 if (softdep_excess_items(ump, D_NEWBLK) || 2435 softdep_excess_items(ump, D_ALLOCDIRECT) || 2436 softdep_excess_items(ump, D_ALLOCINDIR)) 2437 schedule_cleanup(mp); 2438 else 2439 FREE_LOCK(ump); 2440 newblk = malloc(sizeof(union allblk), M_NEWBLK, 2441 M_SOFTDEP_FLAGS | M_ZERO); 2442 workitem_alloc(&newblk->nb_list, D_NEWBLK, mp); 2443 ACQUIRE_LOCK(ump); 2444 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) { 2445 WORKITEM_FREE(newblk, D_NEWBLK); 2446 return (1); 2447 } 2448 newblk->nb_freefrag = NULL; 2449 LIST_INIT(&newblk->nb_indirdeps); 2450 LIST_INIT(&newblk->nb_newdirblk); 2451 LIST_INIT(&newblk->nb_jwork); 2452 newblk->nb_state = ATTACHED; 2453 newblk->nb_newblkno = newblkno; 2454 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash); 2455 *newblkpp = newblk; 2456 return (0); 2457 } 2458 2459 /* 2460 * Structures and routines associated with freed indirect block caching. 2461 */ 2462 #define INDIR_HASH(ump, blkno) \ 2463 (&(ump)->indir_hashtbl[(blkno) & (ump)->indir_hash_size]) 2464 2465 /* 2466 * Lookup an indirect block in the indir hash table. The freework is 2467 * removed and potentially freed. The caller must do a blocking journal 2468 * write before writing to the blkno. 2469 */ 2470 static int 2471 indirblk_lookup(struct mount *mp, ufs2_daddr_t blkno) 2472 { 2473 struct freework *freework; 2474 struct indir_hashhead *wkhd; 2475 struct ufsmount *ump; 2476 2477 ump = VFSTOUFS(mp); 2478 wkhd = INDIR_HASH(ump, blkno); 2479 TAILQ_FOREACH(freework, wkhd, fw_next) { 2480 if (freework->fw_blkno != blkno) 2481 continue; 2482 indirblk_remove(freework); 2483 return (1); 2484 } 2485 return (0); 2486 } 2487 2488 /* 2489 * Insert an indirect block represented by freework into the indirblk 2490 * hash table so that it may prevent the block from being re-used prior 2491 * to the journal being written. 2492 */ 2493 static void 2494 indirblk_insert(struct freework *freework) 2495 { 2496 struct jblocks *jblocks; 2497 struct jseg *jseg; 2498 struct ufsmount *ump; 2499 2500 ump = VFSTOUFS(freework->fw_list.wk_mp); 2501 jblocks = ump->softdep_jblocks; 2502 jseg = TAILQ_LAST(&jblocks->jb_segs, jseglst); 2503 if (jseg == NULL) 2504 return; 2505 2506 LIST_INSERT_HEAD(&jseg->js_indirs, freework, fw_segs); 2507 TAILQ_INSERT_HEAD(INDIR_HASH(ump, freework->fw_blkno), freework, 2508 fw_next); 2509 freework->fw_state &= ~DEPCOMPLETE; 2510 } 2511 2512 static void 2513 indirblk_remove(struct freework *freework) 2514 { 2515 struct ufsmount *ump; 2516 2517 ump = VFSTOUFS(freework->fw_list.wk_mp); 2518 LIST_REMOVE(freework, fw_segs); 2519 TAILQ_REMOVE(INDIR_HASH(ump, freework->fw_blkno), freework, fw_next); 2520 freework->fw_state |= DEPCOMPLETE; 2521 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 2522 WORKITEM_FREE(freework, D_FREEWORK); 2523 } 2524 2525 /* 2526 * Executed during filesystem system initialization before 2527 * mounting any filesystems. 2528 */ 2529 void 2530 softdep_initialize(void) 2531 { 2532 2533 TAILQ_INIT(&softdepmounts); 2534 #ifdef __LP64__ 2535 max_softdeps = desiredvnodes * 4; 2536 #else 2537 max_softdeps = desiredvnodes * 2; 2538 #endif 2539 2540 /* initialise bioops hack */ 2541 bioops.io_start = softdep_disk_io_initiation; 2542 bioops.io_complete = softdep_disk_write_complete; 2543 bioops.io_deallocate = softdep_deallocate_dependencies; 2544 bioops.io_countdeps = softdep_count_dependencies; 2545 ast_register(TDA_UFS, ASTR_KCLEAR | ASTR_ASTF_REQUIRED, 0, 2546 softdep_ast_cleanup_proc); 2547 2548 /* Initialize the callout with an mtx. */ 2549 callout_init_mtx(&softdep_callout, &lk, 0); 2550 } 2551 2552 /* 2553 * Executed after all filesystems have been unmounted during 2554 * filesystem module unload. 2555 */ 2556 void 2557 softdep_uninitialize(void) 2558 { 2559 2560 /* clear bioops hack */ 2561 bioops.io_start = NULL; 2562 bioops.io_complete = NULL; 2563 bioops.io_deallocate = NULL; 2564 bioops.io_countdeps = NULL; 2565 ast_deregister(TDA_UFS); 2566 2567 callout_drain(&softdep_callout); 2568 } 2569 2570 /* 2571 * Called at mount time to notify the dependency code that a 2572 * filesystem wishes to use it. 2573 */ 2574 int 2575 softdep_mount(struct vnode *devvp, 2576 struct mount *mp, 2577 struct fs *fs, 2578 struct ucred *cred) 2579 { 2580 struct csum_total cstotal; 2581 struct mount_softdeps *sdp; 2582 struct ufsmount *ump; 2583 struct cg *cgp; 2584 struct buf *bp; 2585 uint64_t cyl, i; 2586 int error; 2587 2588 ump = VFSTOUFS(mp); 2589 2590 sdp = malloc(sizeof(struct mount_softdeps), M_MOUNTDATA, 2591 M_WAITOK | M_ZERO); 2592 rw_init(&sdp->sd_fslock, "SUrw"); 2593 sdp->sd_ump = ump; 2594 LIST_INIT(&sdp->sd_workitem_pending); 2595 LIST_INIT(&sdp->sd_journal_pending); 2596 TAILQ_INIT(&sdp->sd_unlinked); 2597 LIST_INIT(&sdp->sd_dirtycg); 2598 sdp->sd_worklist_tail = NULL; 2599 sdp->sd_on_worklist = 0; 2600 sdp->sd_deps = 0; 2601 LIST_INIT(&sdp->sd_mkdirlisthd); 2602 sdp->sd_pdhash = hashinit(desiredvnodes / 5, M_PAGEDEP, 2603 &sdp->sd_pdhashsize); 2604 sdp->sd_pdnextclean = 0; 2605 sdp->sd_idhash = hashinit(desiredvnodes, M_INODEDEP, 2606 &sdp->sd_idhashsize); 2607 sdp->sd_idnextclean = 0; 2608 sdp->sd_newblkhash = hashinit(max_softdeps / 2, M_NEWBLK, 2609 &sdp->sd_newblkhashsize); 2610 sdp->sd_bmhash = hashinit(1024, M_BMSAFEMAP, &sdp->sd_bmhashsize); 2611 i = 1 << (ffs(desiredvnodes / 10) - 1); 2612 sdp->sd_indirhash = malloc(i * sizeof(struct indir_hashhead), 2613 M_FREEWORK, M_WAITOK); 2614 sdp->sd_indirhashsize = i - 1; 2615 for (i = 0; i <= sdp->sd_indirhashsize; i++) 2616 TAILQ_INIT(&sdp->sd_indirhash[i]); 2617 for (i = 0; i <= D_LAST; i++) 2618 LIST_INIT(&sdp->sd_alldeps[i]); 2619 ACQUIRE_GBLLOCK(&lk); 2620 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next); 2621 FREE_GBLLOCK(&lk); 2622 2623 ump->um_softdep = sdp; 2624 MNT_ILOCK(mp); 2625 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP; 2626 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) { 2627 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) | 2628 MNTK_SOFTDEP | MNTK_NOASYNC; 2629 } 2630 MNT_IUNLOCK(mp); 2631 2632 if ((fs->fs_flags & FS_SUJ) && 2633 (error = journal_mount(mp, fs, cred)) != 0) { 2634 printf("Failed to start journal: %d\n", error); 2635 softdep_unmount(mp); 2636 return (error); 2637 } 2638 /* 2639 * Start our flushing thread in the bufdaemon process. 2640 */ 2641 ACQUIRE_LOCK(ump); 2642 ump->softdep_flags |= FLUSH_STARTING; 2643 FREE_LOCK(ump); 2644 kproc_kthread_add(&softdep_flush, mp, &bufdaemonproc, 2645 &ump->softdep_flushtd, 0, 0, "softdepflush", "%s worker", 2646 mp->mnt_stat.f_mntonname); 2647 ACQUIRE_LOCK(ump); 2648 while ((ump->softdep_flags & FLUSH_STARTING) != 0) { 2649 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, "sdstart", 2650 hz / 2); 2651 } 2652 FREE_LOCK(ump); 2653 /* 2654 * When doing soft updates, the counters in the 2655 * superblock may have gotten out of sync. Recomputation 2656 * can take a long time and can be deferred for background 2657 * fsck. However, the old behavior of scanning the cylinder 2658 * groups and recalculating them at mount time is available 2659 * by setting vfs.ffs.compute_summary_at_mount to one. 2660 */ 2661 if (compute_summary_at_mount == 0 || fs->fs_clean != 0) 2662 return (0); 2663 bzero(&cstotal, sizeof cstotal); 2664 for (cyl = 0; cyl < fs->fs_ncg; cyl++) { 2665 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)), 2666 fs->fs_cgsize, cred, &bp)) != 0) { 2667 brelse(bp); 2668 softdep_unmount(mp); 2669 return (error); 2670 } 2671 cgp = (struct cg *)bp->b_data; 2672 cstotal.cs_nffree += cgp->cg_cs.cs_nffree; 2673 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree; 2674 cstotal.cs_nifree += cgp->cg_cs.cs_nifree; 2675 cstotal.cs_ndir += cgp->cg_cs.cs_ndir; 2676 fs->fs_cs(fs, cyl) = cgp->cg_cs; 2677 brelse(bp); 2678 } 2679 #ifdef INVARIANTS 2680 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal)) 2681 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt); 2682 #endif 2683 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal); 2684 return (0); 2685 } 2686 2687 void 2688 softdep_unmount(struct mount *mp) 2689 { 2690 struct ufsmount *ump; 2691 struct mount_softdeps *ums; 2692 2693 ump = VFSTOUFS(mp); 2694 KASSERT(ump->um_softdep != NULL, 2695 ("softdep_unmount called on non-softdep filesystem")); 2696 MNT_ILOCK(mp); 2697 mp->mnt_flag &= ~MNT_SOFTDEP; 2698 if ((mp->mnt_flag & MNT_SUJ) == 0) { 2699 MNT_IUNLOCK(mp); 2700 } else { 2701 mp->mnt_flag &= ~MNT_SUJ; 2702 MNT_IUNLOCK(mp); 2703 journal_unmount(ump); 2704 } 2705 /* 2706 * Shut down our flushing thread. Check for NULL is if 2707 * softdep_mount errors out before the thread has been created. 2708 */ 2709 if (ump->softdep_flushtd != NULL) { 2710 ACQUIRE_LOCK(ump); 2711 ump->softdep_flags |= FLUSH_EXIT; 2712 wakeup(&ump->softdep_flushtd); 2713 while ((ump->softdep_flags & FLUSH_EXIT) != 0) { 2714 msleep(&ump->softdep_flags, LOCK_PTR(ump), PVM, 2715 "sdwait", 0); 2716 } 2717 KASSERT((ump->softdep_flags & FLUSH_EXIT) == 0, 2718 ("Thread shutdown failed")); 2719 FREE_LOCK(ump); 2720 } 2721 2722 /* 2723 * We are no longer have softdep structure attached to ump. 2724 */ 2725 ums = ump->um_softdep; 2726 ACQUIRE_GBLLOCK(&lk); 2727 TAILQ_REMOVE(&softdepmounts, ums, sd_next); 2728 FREE_GBLLOCK(&lk); 2729 ump->um_softdep = NULL; 2730 2731 KASSERT(ums->sd_on_journal == 0, 2732 ("ump %p ums %p on_journal %d", ump, ums, ums->sd_on_journal)); 2733 KASSERT(ums->sd_on_worklist == 0, 2734 ("ump %p ums %p on_worklist %d", ump, ums, ums->sd_on_worklist)); 2735 KASSERT(ums->sd_deps == 0, 2736 ("ump %p ums %p deps %d", ump, ums, ums->sd_deps)); 2737 2738 /* 2739 * Free up our resources. 2740 */ 2741 rw_destroy(&ums->sd_fslock); 2742 hashdestroy(ums->sd_pdhash, M_PAGEDEP, ums->sd_pdhashsize); 2743 hashdestroy(ums->sd_idhash, M_INODEDEP, ums->sd_idhashsize); 2744 hashdestroy(ums->sd_newblkhash, M_NEWBLK, ums->sd_newblkhashsize); 2745 hashdestroy(ums->sd_bmhash, M_BMSAFEMAP, ums->sd_bmhashsize); 2746 free(ums->sd_indirhash, M_FREEWORK); 2747 #ifdef INVARIANTS 2748 for (int i = 0; i <= D_LAST; i++) { 2749 KASSERT(ums->sd_curdeps[i] == 0, 2750 ("Unmount %s: Dep type %s != 0 (%jd)", ump->um_fs->fs_fsmnt, 2751 TYPENAME(i), (intmax_t)ums->sd_curdeps[i])); 2752 KASSERT(LIST_EMPTY(&ums->sd_alldeps[i]), 2753 ("Unmount %s: Dep type %s not empty (%p)", 2754 ump->um_fs->fs_fsmnt, 2755 TYPENAME(i), LIST_FIRST(&ums->sd_alldeps[i]))); 2756 } 2757 #endif 2758 free(ums, M_MOUNTDATA); 2759 } 2760 2761 static struct jblocks * 2762 jblocks_create(void) 2763 { 2764 struct jblocks *jblocks; 2765 2766 jblocks = malloc(sizeof(*jblocks), M_JBLOCKS, M_WAITOK | M_ZERO); 2767 TAILQ_INIT(&jblocks->jb_segs); 2768 jblocks->jb_avail = 10; 2769 jblocks->jb_extent = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2770 M_JBLOCKS, M_WAITOK | M_ZERO); 2771 2772 return (jblocks); 2773 } 2774 2775 static ufs2_daddr_t 2776 jblocks_alloc(struct jblocks *jblocks, 2777 int bytes, 2778 int *actual) 2779 { 2780 ufs2_daddr_t daddr; 2781 struct jextent *jext; 2782 int freecnt; 2783 int blocks; 2784 2785 blocks = bytes / DEV_BSIZE; 2786 jext = &jblocks->jb_extent[jblocks->jb_head]; 2787 freecnt = jext->je_blocks - jblocks->jb_off; 2788 if (freecnt == 0) { 2789 jblocks->jb_off = 0; 2790 if (++jblocks->jb_head > jblocks->jb_used) 2791 jblocks->jb_head = 0; 2792 jext = &jblocks->jb_extent[jblocks->jb_head]; 2793 freecnt = jext->je_blocks; 2794 } 2795 if (freecnt > blocks) 2796 freecnt = blocks; 2797 *actual = freecnt * DEV_BSIZE; 2798 daddr = jext->je_daddr + jblocks->jb_off; 2799 jblocks->jb_off += freecnt; 2800 jblocks->jb_free -= freecnt; 2801 2802 return (daddr); 2803 } 2804 2805 static void 2806 jblocks_free(struct jblocks *jblocks, 2807 struct mount *mp, 2808 int bytes) 2809 { 2810 2811 LOCK_OWNED(VFSTOUFS(mp)); 2812 jblocks->jb_free += bytes / DEV_BSIZE; 2813 if (jblocks->jb_suspended) 2814 worklist_speedup(mp); 2815 wakeup(jblocks); 2816 } 2817 2818 static void 2819 jblocks_destroy(struct jblocks *jblocks) 2820 { 2821 2822 if (jblocks->jb_extent) 2823 free(jblocks->jb_extent, M_JBLOCKS); 2824 free(jblocks, M_JBLOCKS); 2825 } 2826 2827 static void 2828 jblocks_add(struct jblocks *jblocks, 2829 ufs2_daddr_t daddr, 2830 int blocks) 2831 { 2832 struct jextent *jext; 2833 2834 jblocks->jb_blocks += blocks; 2835 jblocks->jb_free += blocks; 2836 jext = &jblocks->jb_extent[jblocks->jb_used]; 2837 /* Adding the first block. */ 2838 if (jext->je_daddr == 0) { 2839 jext->je_daddr = daddr; 2840 jext->je_blocks = blocks; 2841 return; 2842 } 2843 /* Extending the last extent. */ 2844 if (jext->je_daddr + jext->je_blocks == daddr) { 2845 jext->je_blocks += blocks; 2846 return; 2847 } 2848 /* Adding a new extent. */ 2849 if (++jblocks->jb_used == jblocks->jb_avail) { 2850 jblocks->jb_avail *= 2; 2851 jext = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2852 M_JBLOCKS, M_WAITOK | M_ZERO); 2853 memcpy(jext, jblocks->jb_extent, 2854 sizeof(struct jextent) * jblocks->jb_used); 2855 free(jblocks->jb_extent, M_JBLOCKS); 2856 jblocks->jb_extent = jext; 2857 } 2858 jext = &jblocks->jb_extent[jblocks->jb_used]; 2859 jext->je_daddr = daddr; 2860 jext->je_blocks = blocks; 2861 return; 2862 } 2863 2864 int 2865 softdep_journal_lookup(struct mount *mp, struct vnode **vpp) 2866 { 2867 struct componentname cnp; 2868 struct vnode *dvp; 2869 ino_t sujournal; 2870 int error; 2871 2872 error = VFS_VGET(mp, UFS_ROOTINO, LK_EXCLUSIVE, &dvp); 2873 if (error) 2874 return (error); 2875 bzero(&cnp, sizeof(cnp)); 2876 cnp.cn_nameiop = LOOKUP; 2877 cnp.cn_flags = ISLASTCN; 2878 cnp.cn_cred = curthread->td_ucred; 2879 cnp.cn_pnbuf = SUJ_FILE; 2880 cnp.cn_nameptr = SUJ_FILE; 2881 cnp.cn_namelen = strlen(SUJ_FILE); 2882 error = ufs_lookup_ino(dvp, NULL, &cnp, &sujournal); 2883 vput(dvp); 2884 if (error != 0) 2885 return (error); 2886 error = VFS_VGET(mp, sujournal, LK_EXCLUSIVE, vpp); 2887 return (error); 2888 } 2889 2890 /* 2891 * Open and verify the journal file. 2892 */ 2893 static int 2894 journal_mount(struct mount *mp, 2895 struct fs *fs, 2896 struct ucred *cred) 2897 { 2898 struct jblocks *jblocks; 2899 struct ufsmount *ump; 2900 struct vnode *vp; 2901 struct inode *ip; 2902 ufs2_daddr_t blkno; 2903 int bcount; 2904 int error; 2905 int i; 2906 2907 ump = VFSTOUFS(mp); 2908 ump->softdep_journal_tail = NULL; 2909 ump->softdep_on_journal = 0; 2910 ump->softdep_accdeps = 0; 2911 ump->softdep_req = 0; 2912 ump->softdep_jblocks = NULL; 2913 error = softdep_journal_lookup(mp, &vp); 2914 if (error != 0) { 2915 printf("Failed to find journal. Use tunefs to create one\n"); 2916 return (error); 2917 } 2918 ip = VTOI(vp); 2919 if (ip->i_size < SUJ_MIN) { 2920 error = ENOSPC; 2921 goto out; 2922 } 2923 bcount = lblkno(fs, ip->i_size); /* Only use whole blocks. */ 2924 jblocks = jblocks_create(); 2925 for (i = 0; i < bcount; i++) { 2926 error = ufs_bmaparray(vp, i, &blkno, NULL, NULL, NULL); 2927 if (error) 2928 break; 2929 jblocks_add(jblocks, blkno, fsbtodb(fs, fs->fs_frag)); 2930 } 2931 if (error) { 2932 jblocks_destroy(jblocks); 2933 goto out; 2934 } 2935 jblocks->jb_low = jblocks->jb_free / 3; /* Reserve 33%. */ 2936 jblocks->jb_min = jblocks->jb_free / 10; /* Suspend at 10%. */ 2937 ump->softdep_jblocks = jblocks; 2938 2939 MNT_ILOCK(mp); 2940 mp->mnt_flag |= MNT_SUJ; 2941 MNT_IUNLOCK(mp); 2942 2943 /* 2944 * Only validate the journal contents if the 2945 * filesystem is clean, otherwise we write the logs 2946 * but they'll never be used. If the filesystem was 2947 * still dirty when we mounted it the journal is 2948 * invalid and a new journal can only be valid if it 2949 * starts from a clean mount. 2950 */ 2951 if (fs->fs_clean) { 2952 DIP_SET(ip, i_modrev, fs->fs_mtime); 2953 ip->i_flags |= IN_MODIFIED; 2954 ffs_update(vp, 1); 2955 } 2956 out: 2957 vput(vp); 2958 return (error); 2959 } 2960 2961 static void 2962 journal_unmount(struct ufsmount *ump) 2963 { 2964 2965 if (ump->softdep_jblocks) 2966 jblocks_destroy(ump->softdep_jblocks); 2967 ump->softdep_jblocks = NULL; 2968 } 2969 2970 /* 2971 * Called when a journal record is ready to be written. Space is allocated 2972 * and the journal entry is created when the journal is flushed to stable 2973 * store. 2974 */ 2975 static void 2976 add_to_journal(struct worklist *wk) 2977 { 2978 struct ufsmount *ump; 2979 2980 ump = VFSTOUFS(wk->wk_mp); 2981 LOCK_OWNED(ump); 2982 if (wk->wk_state & ONWORKLIST) 2983 panic("add_to_journal: %s(0x%X) already on list", 2984 TYPENAME(wk->wk_type), wk->wk_state); 2985 wk->wk_state |= ONWORKLIST | DEPCOMPLETE; 2986 if (LIST_EMPTY(&ump->softdep_journal_pending)) { 2987 ump->softdep_jblocks->jb_age = ticks; 2988 LIST_INSERT_HEAD(&ump->softdep_journal_pending, wk, wk_list); 2989 } else 2990 LIST_INSERT_AFTER(ump->softdep_journal_tail, wk, wk_list); 2991 ump->softdep_journal_tail = wk; 2992 ump->softdep_on_journal += 1; 2993 } 2994 2995 /* 2996 * Remove an arbitrary item for the journal worklist maintain the tail 2997 * pointer. This happens when a new operation obviates the need to 2998 * journal an old operation. 2999 */ 3000 static void 3001 remove_from_journal(struct worklist *wk) 3002 { 3003 struct ufsmount *ump; 3004 3005 ump = VFSTOUFS(wk->wk_mp); 3006 LOCK_OWNED(ump); 3007 #ifdef INVARIANTS 3008 { 3009 struct worklist *wkn; 3010 3011 LIST_FOREACH(wkn, &ump->softdep_journal_pending, wk_list) 3012 if (wkn == wk) 3013 break; 3014 if (wkn == NULL) 3015 panic("remove_from_journal: %p is not in journal", wk); 3016 } 3017 #endif 3018 /* 3019 * We emulate a TAILQ to save space in most structures which do not 3020 * require TAILQ semantics. Here we must update the tail position 3021 * when removing the tail which is not the final entry. This works 3022 * only if the worklist linkage are at the beginning of the structure. 3023 */ 3024 if (ump->softdep_journal_tail == wk) 3025 ump->softdep_journal_tail = 3026 (struct worklist *)wk->wk_list.le_prev; 3027 WORKLIST_REMOVE(wk); 3028 ump->softdep_on_journal -= 1; 3029 } 3030 3031 /* 3032 * Check for journal space as well as dependency limits so the prelink 3033 * code can throttle both journaled and non-journaled filesystems. 3034 * Threshold is 0 for low and 1 for min. 3035 */ 3036 static int 3037 journal_space(struct ufsmount *ump, int thresh) 3038 { 3039 struct jblocks *jblocks; 3040 int limit, avail; 3041 3042 jblocks = ump->softdep_jblocks; 3043 if (jblocks == NULL) 3044 return (1); 3045 /* 3046 * We use a tighter restriction here to prevent request_cleanup() 3047 * running in threads from running into locks we currently hold. 3048 * We have to be over the limit and our filesystem has to be 3049 * responsible for more than our share of that usage. 3050 */ 3051 limit = (max_softdeps / 10) * 9; 3052 if (dep_current[D_INODEDEP] > limit && 3053 ump->softdep_curdeps[D_INODEDEP] > limit / stat_flush_threads) 3054 return (0); 3055 if (thresh) 3056 thresh = jblocks->jb_min; 3057 else 3058 thresh = jblocks->jb_low; 3059 avail = (ump->softdep_on_journal * JREC_SIZE) / DEV_BSIZE; 3060 avail = jblocks->jb_free - avail; 3061 3062 return (avail > thresh); 3063 } 3064 3065 static void 3066 journal_suspend(struct ufsmount *ump) 3067 { 3068 struct jblocks *jblocks; 3069 struct mount *mp; 3070 bool set; 3071 3072 mp = UFSTOVFS(ump); 3073 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) 3074 return; 3075 3076 jblocks = ump->softdep_jblocks; 3077 vfs_op_enter(mp); 3078 set = false; 3079 MNT_ILOCK(mp); 3080 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) { 3081 stat_journal_min++; 3082 mp->mnt_kern_flag |= MNTK_SUSPEND; 3083 mp->mnt_susp_owner = ump->softdep_flushtd; 3084 set = true; 3085 } 3086 jblocks->jb_suspended = 1; 3087 MNT_IUNLOCK(mp); 3088 if (!set) 3089 vfs_op_exit(mp); 3090 } 3091 3092 static int 3093 journal_unsuspend(struct ufsmount *ump) 3094 { 3095 struct jblocks *jblocks; 3096 struct mount *mp; 3097 3098 mp = UFSTOVFS(ump); 3099 jblocks = ump->softdep_jblocks; 3100 3101 if (jblocks != NULL && jblocks->jb_suspended && 3102 journal_space(ump, jblocks->jb_min)) { 3103 jblocks->jb_suspended = 0; 3104 FREE_LOCK(ump); 3105 mp->mnt_susp_owner = curthread; 3106 vfs_write_resume(mp, 0); 3107 ACQUIRE_LOCK(ump); 3108 return (1); 3109 } 3110 return (0); 3111 } 3112 3113 static void 3114 journal_check_space(struct ufsmount *ump) 3115 { 3116 struct mount *mp; 3117 3118 LOCK_OWNED(ump); 3119 3120 if (journal_space(ump, 0) == 0) { 3121 softdep_speedup(ump); 3122 mp = UFSTOVFS(ump); 3123 FREE_LOCK(ump); 3124 VFS_SYNC(mp, MNT_NOWAIT); 3125 ffs_sbupdate(ump, MNT_WAIT, 0); 3126 ACQUIRE_LOCK(ump); 3127 if (journal_space(ump, 1) == 0) 3128 journal_suspend(ump); 3129 } 3130 } 3131 3132 /* 3133 * Called before any allocation function to be certain that there is 3134 * sufficient space in the journal prior to creating any new records. 3135 * Since in the case of block allocation we may have multiple locked 3136 * buffers at the time of the actual allocation we can not block 3137 * when the journal records are created. Doing so would create a deadlock 3138 * if any of these buffers needed to be flushed to reclaim space. Instead 3139 * we require a sufficiently large amount of available space such that 3140 * each thread in the system could have passed this allocation check and 3141 * still have sufficient free space. With 20% of a minimum journal size 3142 * of 1MB we have 6553 records available. 3143 */ 3144 int 3145 softdep_prealloc(struct vnode *vp, int waitok) 3146 { 3147 struct ufsmount *ump; 3148 3149 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 3150 ("softdep_prealloc called on non-softdep filesystem")); 3151 /* 3152 * Nothing to do if we are not running journaled soft updates. 3153 * If we currently hold the snapshot lock, we must avoid 3154 * handling other resources that could cause deadlock. Do not 3155 * touch quotas vnode since it is typically recursed with 3156 * other vnode locks held. 3157 */ 3158 if (DOINGSUJ(vp) == 0 || IS_SNAPSHOT(VTOI(vp)) || 3159 (vp->v_vflag & VV_SYSTEM) != 0) 3160 return (0); 3161 ump = VFSTOUFS(vp->v_mount); 3162 ACQUIRE_LOCK(ump); 3163 if (journal_space(ump, 0)) { 3164 FREE_LOCK(ump); 3165 return (0); 3166 } 3167 stat_journal_low++; 3168 FREE_LOCK(ump); 3169 if (waitok == MNT_NOWAIT) 3170 return (ENOSPC); 3171 /* 3172 * Attempt to sync this vnode once to flush any journal 3173 * work attached to it. 3174 */ 3175 if ((curthread->td_pflags & TDP_COWINPROGRESS) == 0) 3176 ffs_syncvnode(vp, waitok, 0); 3177 ACQUIRE_LOCK(ump); 3178 process_removes(vp); 3179 process_truncates(vp); 3180 journal_check_space(ump); 3181 FREE_LOCK(ump); 3182 3183 return (0); 3184 } 3185 3186 /* 3187 * Try hard to sync all data and metadata for the vnode, and workitems 3188 * flushing which might conflict with the vnode lock. This is a 3189 * helper for softdep_prerename(). 3190 */ 3191 static int 3192 softdep_prerename_vnode(struct ufsmount *ump, struct vnode *vp) 3193 { 3194 int error; 3195 3196 ASSERT_VOP_ELOCKED(vp, "prehandle"); 3197 if (vp->v_data == NULL) 3198 return (0); 3199 error = VOP_FSYNC(vp, MNT_WAIT, curthread); 3200 if (error != 0) 3201 return (error); 3202 ACQUIRE_LOCK(ump); 3203 process_removes(vp); 3204 process_truncates(vp); 3205 FREE_LOCK(ump); 3206 return (0); 3207 } 3208 3209 /* 3210 * Must be called from VOP_RENAME() after all vnodes are locked. 3211 * Ensures that there is enough journal space for rename. It is 3212 * sufficiently different from softdep_prelink() by having to handle 3213 * four vnodes. 3214 */ 3215 int 3216 softdep_prerename(struct vnode *fdvp, 3217 struct vnode *fvp, 3218 struct vnode *tdvp, 3219 struct vnode *tvp) 3220 { 3221 struct ufsmount *ump; 3222 int error; 3223 3224 ump = VFSTOUFS(fdvp->v_mount); 3225 3226 if (journal_space(ump, 0)) 3227 return (0); 3228 3229 VOP_UNLOCK(tdvp); 3230 VOP_UNLOCK(fvp); 3231 if (tvp != NULL && tvp != tdvp) 3232 VOP_UNLOCK(tvp); 3233 3234 error = softdep_prerename_vnode(ump, fdvp); 3235 VOP_UNLOCK(fdvp); 3236 if (error != 0) 3237 return (error); 3238 3239 VOP_LOCK(fvp, LK_EXCLUSIVE | LK_RETRY); 3240 error = softdep_prerename_vnode(ump, fvp); 3241 VOP_UNLOCK(fvp); 3242 if (error != 0) 3243 return (error); 3244 3245 if (tdvp != fdvp) { 3246 VOP_LOCK(tdvp, LK_EXCLUSIVE | LK_RETRY); 3247 error = softdep_prerename_vnode(ump, tdvp); 3248 VOP_UNLOCK(tdvp); 3249 if (error != 0) 3250 return (error); 3251 } 3252 3253 if (tvp != fvp && tvp != NULL) { 3254 VOP_LOCK(tvp, LK_EXCLUSIVE | LK_RETRY); 3255 error = softdep_prerename_vnode(ump, tvp); 3256 VOP_UNLOCK(tvp); 3257 if (error != 0) 3258 return (error); 3259 } 3260 3261 ACQUIRE_LOCK(ump); 3262 softdep_speedup(ump); 3263 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT); 3264 journal_check_space(ump); 3265 FREE_LOCK(ump); 3266 return (ERELOOKUP); 3267 } 3268 3269 /* 3270 * Before adjusting a link count on a vnode verify that we have sufficient 3271 * journal space. If not, process operations that depend on the currently 3272 * locked pair of vnodes to try to flush space as the syncer, buf daemon, 3273 * and softdep flush threads can not acquire these locks to reclaim space. 3274 * 3275 * Returns 0 if all owned locks are still valid and were not dropped 3276 * in the process, in other case it returns either an error from sync, 3277 * or ERELOOKUP if any of the locks were re-acquired. In the later 3278 * case, the state of the vnodes cannot be relied upon and our VFS 3279 * syscall must be restarted at top level from the lookup. 3280 */ 3281 int 3282 softdep_prelink(struct vnode *dvp, 3283 struct vnode *vp, 3284 struct componentname *cnp) 3285 { 3286 struct ufsmount *ump; 3287 struct nameidata *ndp; 3288 3289 ASSERT_VOP_ELOCKED(dvp, "prelink dvp"); 3290 if (vp != NULL) 3291 ASSERT_VOP_ELOCKED(vp, "prelink vp"); 3292 ump = VFSTOUFS(dvp->v_mount); 3293 3294 /* 3295 * Nothing to do if we have sufficient journal space. We skip 3296 * flushing when vp is a snapshot to avoid deadlock where 3297 * another thread is trying to update the inodeblock for dvp 3298 * and is waiting on snaplk that vp holds. 3299 */ 3300 if (journal_space(ump, 0) || (vp != NULL && IS_SNAPSHOT(VTOI(vp)))) 3301 return (0); 3302 3303 /* 3304 * Check if the journal space consumption can in theory be 3305 * accounted on dvp and vp. If the vnodes metadata was not 3306 * changed comparing with the previous round-trip into 3307 * softdep_prelink(), as indicated by the seqc generation 3308 * recorded in the nameidata, then there is no point in 3309 * starting the sync. 3310 */ 3311 ndp = __containerof(cnp, struct nameidata, ni_cnd); 3312 if (!seqc_in_modify(ndp->ni_dvp_seqc) && 3313 vn_seqc_consistent(dvp, ndp->ni_dvp_seqc) && 3314 (vp == NULL || (!seqc_in_modify(ndp->ni_vp_seqc) && 3315 vn_seqc_consistent(vp, ndp->ni_vp_seqc)))) 3316 return (0); 3317 3318 stat_journal_low++; 3319 if (vp != NULL) { 3320 VOP_UNLOCK(dvp); 3321 ffs_syncvnode(vp, MNT_NOWAIT, 0); 3322 vn_lock_pair(dvp, false, LK_EXCLUSIVE, vp, true, LK_EXCLUSIVE); 3323 if (dvp->v_data == NULL) 3324 goto out; 3325 } 3326 if (vp != NULL) 3327 VOP_UNLOCK(vp); 3328 ffs_syncvnode(dvp, MNT_WAIT, 0); 3329 /* Process vp before dvp as it may create .. removes. */ 3330 if (vp != NULL) { 3331 VOP_UNLOCK(dvp); 3332 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3333 if (vp->v_data == NULL) { 3334 vn_lock_pair(dvp, false, LK_EXCLUSIVE, vp, true, 3335 LK_EXCLUSIVE); 3336 goto out; 3337 } 3338 ACQUIRE_LOCK(ump); 3339 process_removes(vp); 3340 process_truncates(vp); 3341 FREE_LOCK(ump); 3342 VOP_UNLOCK(vp); 3343 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 3344 if (dvp->v_data == NULL) { 3345 vn_lock_pair(dvp, true, LK_EXCLUSIVE, vp, false, 3346 LK_EXCLUSIVE); 3347 goto out; 3348 } 3349 } 3350 3351 ACQUIRE_LOCK(ump); 3352 process_removes(dvp); 3353 process_truncates(dvp); 3354 VOP_UNLOCK(dvp); 3355 softdep_speedup(ump); 3356 3357 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT); 3358 journal_check_space(ump); 3359 FREE_LOCK(ump); 3360 3361 vn_lock_pair(dvp, false, LK_EXCLUSIVE, vp, false, LK_EXCLUSIVE); 3362 out: 3363 ndp->ni_dvp_seqc = vn_seqc_read_any(dvp); 3364 if (vp != NULL) 3365 ndp->ni_vp_seqc = vn_seqc_read_any(vp); 3366 return (ERELOOKUP); 3367 } 3368 3369 static void 3370 jseg_write(struct ufsmount *ump, 3371 struct jseg *jseg, 3372 uint8_t *data) 3373 { 3374 struct jsegrec *rec; 3375 3376 rec = (struct jsegrec *)data; 3377 rec->jsr_seq = jseg->js_seq; 3378 rec->jsr_oldest = jseg->js_oldseq; 3379 rec->jsr_cnt = jseg->js_cnt; 3380 rec->jsr_blocks = jseg->js_size / ump->um_devvp->v_bufobj.bo_bsize; 3381 rec->jsr_crc = 0; 3382 rec->jsr_time = ump->um_fs->fs_mtime; 3383 } 3384 3385 static inline void 3386 inoref_write(struct inoref *inoref, 3387 struct jseg *jseg, 3388 struct jrefrec *rec) 3389 { 3390 3391 inoref->if_jsegdep->jd_seg = jseg; 3392 rec->jr_ino = inoref->if_ino; 3393 rec->jr_parent = inoref->if_parent; 3394 rec->jr_nlink = inoref->if_nlink; 3395 rec->jr_mode = inoref->if_mode; 3396 rec->jr_diroff = inoref->if_diroff; 3397 } 3398 3399 static void 3400 jaddref_write(struct jaddref *jaddref, 3401 struct jseg *jseg, 3402 uint8_t *data) 3403 { 3404 struct jrefrec *rec; 3405 3406 rec = (struct jrefrec *)data; 3407 rec->jr_op = JOP_ADDREF; 3408 inoref_write(&jaddref->ja_ref, jseg, rec); 3409 } 3410 3411 static void 3412 jremref_write(struct jremref *jremref, 3413 struct jseg *jseg, 3414 uint8_t *data) 3415 { 3416 struct jrefrec *rec; 3417 3418 rec = (struct jrefrec *)data; 3419 rec->jr_op = JOP_REMREF; 3420 inoref_write(&jremref->jr_ref, jseg, rec); 3421 } 3422 3423 static void 3424 jmvref_write(struct jmvref *jmvref, 3425 struct jseg *jseg, 3426 uint8_t *data) 3427 { 3428 struct jmvrec *rec; 3429 3430 rec = (struct jmvrec *)data; 3431 rec->jm_op = JOP_MVREF; 3432 rec->jm_ino = jmvref->jm_ino; 3433 rec->jm_parent = jmvref->jm_parent; 3434 rec->jm_oldoff = jmvref->jm_oldoff; 3435 rec->jm_newoff = jmvref->jm_newoff; 3436 } 3437 3438 static void 3439 jnewblk_write(struct jnewblk *jnewblk, 3440 struct jseg *jseg, 3441 uint8_t *data) 3442 { 3443 struct jblkrec *rec; 3444 3445 jnewblk->jn_jsegdep->jd_seg = jseg; 3446 rec = (struct jblkrec *)data; 3447 rec->jb_op = JOP_NEWBLK; 3448 rec->jb_ino = jnewblk->jn_ino; 3449 rec->jb_blkno = jnewblk->jn_blkno; 3450 rec->jb_lbn = jnewblk->jn_lbn; 3451 rec->jb_frags = jnewblk->jn_frags; 3452 rec->jb_oldfrags = jnewblk->jn_oldfrags; 3453 } 3454 3455 static void 3456 jfreeblk_write(struct jfreeblk *jfreeblk, 3457 struct jseg *jseg, 3458 uint8_t *data) 3459 { 3460 struct jblkrec *rec; 3461 3462 jfreeblk->jf_dep.jb_jsegdep->jd_seg = jseg; 3463 rec = (struct jblkrec *)data; 3464 rec->jb_op = JOP_FREEBLK; 3465 rec->jb_ino = jfreeblk->jf_ino; 3466 rec->jb_blkno = jfreeblk->jf_blkno; 3467 rec->jb_lbn = jfreeblk->jf_lbn; 3468 rec->jb_frags = jfreeblk->jf_frags; 3469 rec->jb_oldfrags = 0; 3470 } 3471 3472 static void 3473 jfreefrag_write(struct jfreefrag *jfreefrag, 3474 struct jseg *jseg, 3475 uint8_t *data) 3476 { 3477 struct jblkrec *rec; 3478 3479 jfreefrag->fr_jsegdep->jd_seg = jseg; 3480 rec = (struct jblkrec *)data; 3481 rec->jb_op = JOP_FREEBLK; 3482 rec->jb_ino = jfreefrag->fr_ino; 3483 rec->jb_blkno = jfreefrag->fr_blkno; 3484 rec->jb_lbn = jfreefrag->fr_lbn; 3485 rec->jb_frags = jfreefrag->fr_frags; 3486 rec->jb_oldfrags = 0; 3487 } 3488 3489 static void 3490 jtrunc_write(struct jtrunc *jtrunc, 3491 struct jseg *jseg, 3492 uint8_t *data) 3493 { 3494 struct jtrncrec *rec; 3495 3496 jtrunc->jt_dep.jb_jsegdep->jd_seg = jseg; 3497 rec = (struct jtrncrec *)data; 3498 rec->jt_op = JOP_TRUNC; 3499 rec->jt_ino = jtrunc->jt_ino; 3500 rec->jt_size = jtrunc->jt_size; 3501 rec->jt_extsize = jtrunc->jt_extsize; 3502 } 3503 3504 static void 3505 jfsync_write(struct jfsync *jfsync, 3506 struct jseg *jseg, 3507 uint8_t *data) 3508 { 3509 struct jtrncrec *rec; 3510 3511 rec = (struct jtrncrec *)data; 3512 rec->jt_op = JOP_SYNC; 3513 rec->jt_ino = jfsync->jfs_ino; 3514 rec->jt_size = jfsync->jfs_size; 3515 rec->jt_extsize = jfsync->jfs_extsize; 3516 } 3517 3518 static void 3519 softdep_flushjournal(struct mount *mp) 3520 { 3521 struct jblocks *jblocks; 3522 struct ufsmount *ump; 3523 3524 if (MOUNTEDSUJ(mp) == 0) 3525 return; 3526 ump = VFSTOUFS(mp); 3527 jblocks = ump->softdep_jblocks; 3528 ACQUIRE_LOCK(ump); 3529 while (ump->softdep_on_journal) { 3530 jblocks->jb_needseg = 1; 3531 softdep_process_journal(mp, NULL, MNT_WAIT); 3532 } 3533 FREE_LOCK(ump); 3534 } 3535 3536 static void softdep_synchronize_completed(struct bio *); 3537 static void softdep_synchronize(struct bio *, struct ufsmount *, void *); 3538 3539 static void 3540 softdep_synchronize_completed(struct bio *bp) 3541 { 3542 struct jseg *oldest; 3543 struct jseg *jseg; 3544 struct ufsmount *ump; 3545 3546 /* 3547 * caller1 marks the last segment written before we issued the 3548 * synchronize cache. 3549 */ 3550 jseg = bp->bio_caller1; 3551 if (jseg == NULL) { 3552 g_destroy_bio(bp); 3553 return; 3554 } 3555 ump = VFSTOUFS(jseg->js_list.wk_mp); 3556 ACQUIRE_LOCK(ump); 3557 oldest = NULL; 3558 /* 3559 * Mark all the journal entries waiting on the synchronize cache 3560 * as completed so they may continue on. 3561 */ 3562 while (jseg != NULL && (jseg->js_state & COMPLETE) == 0) { 3563 jseg->js_state |= COMPLETE; 3564 oldest = jseg; 3565 jseg = TAILQ_PREV(jseg, jseglst, js_next); 3566 } 3567 /* 3568 * Restart deferred journal entry processing from the oldest 3569 * completed jseg. 3570 */ 3571 if (oldest) 3572 complete_jsegs(oldest); 3573 3574 FREE_LOCK(ump); 3575 g_destroy_bio(bp); 3576 } 3577 3578 /* 3579 * Send BIO_FLUSH/SYNCHRONIZE CACHE to the device to enforce write ordering 3580 * barriers. The journal must be written prior to any blocks that depend 3581 * on it and the journal can not be released until the blocks have be 3582 * written. This code handles both barriers simultaneously. 3583 */ 3584 static void 3585 softdep_synchronize(struct bio *bp, 3586 struct ufsmount *ump, 3587 void *caller1) 3588 { 3589 3590 bp->bio_cmd = BIO_FLUSH; 3591 bp->bio_flags |= BIO_ORDERED; 3592 bp->bio_data = NULL; 3593 bp->bio_offset = ump->um_cp->provider->mediasize; 3594 bp->bio_length = 0; 3595 bp->bio_done = softdep_synchronize_completed; 3596 bp->bio_caller1 = caller1; 3597 g_io_request(bp, ump->um_cp); 3598 } 3599 3600 /* 3601 * Flush some journal records to disk. 3602 */ 3603 static void 3604 softdep_process_journal(struct mount *mp, 3605 struct worklist *needwk, 3606 int flags) 3607 { 3608 struct jblocks *jblocks; 3609 struct ufsmount *ump; 3610 struct worklist *wk; 3611 struct jseg *jseg; 3612 struct buf *bp; 3613 struct bio *bio; 3614 uint8_t *data; 3615 struct fs *fs; 3616 int shouldflush; 3617 int segwritten; 3618 int jrecmin; /* Minimum records per block. */ 3619 int jrecmax; /* Maximum records per block. */ 3620 int size; 3621 int cnt; 3622 int off; 3623 int devbsize; 3624 3625 ump = VFSTOUFS(mp); 3626 if (ump->um_softdep == NULL || ump->um_softdep->sd_jblocks == NULL) 3627 return; 3628 shouldflush = softdep_flushcache; 3629 bio = NULL; 3630 jseg = NULL; 3631 LOCK_OWNED(ump); 3632 fs = ump->um_fs; 3633 jblocks = ump->softdep_jblocks; 3634 devbsize = ump->um_devvp->v_bufobj.bo_bsize; 3635 /* 3636 * We write anywhere between a disk block and fs block. The upper 3637 * bound is picked to prevent buffer cache fragmentation and limit 3638 * processing time per I/O. 3639 */ 3640 jrecmin = (devbsize / JREC_SIZE) - 1; /* -1 for seg header */ 3641 jrecmax = (fs->fs_bsize / devbsize) * jrecmin; 3642 segwritten = 0; 3643 for (;;) { 3644 cnt = ump->softdep_on_journal; 3645 /* 3646 * Criteria for writing a segment: 3647 * 1) We have a full block. 3648 * 2) We're called from jwait() and haven't found the 3649 * journal item yet. 3650 * 3) Always write if needseg is set. 3651 * 4) If we are called from process_worklist and have 3652 * not yet written anything we write a partial block 3653 * to enforce a 1 second maximum latency on journal 3654 * entries. 3655 */ 3656 if (cnt < (jrecmax - 1) && needwk == NULL && 3657 jblocks->jb_needseg == 0 && (segwritten || cnt == 0)) 3658 break; 3659 cnt++; 3660 /* 3661 * Verify some free journal space. softdep_prealloc() should 3662 * guarantee that we don't run out so this is indicative of 3663 * a problem with the flow control. Try to recover 3664 * gracefully in any event. 3665 */ 3666 while (jblocks->jb_free == 0) { 3667 if (flags != MNT_WAIT) 3668 break; 3669 printf("softdep: Out of journal space!\n"); 3670 softdep_speedup(ump); 3671 msleep(jblocks, LOCK_PTR(ump), PRIBIO, "jblocks", hz); 3672 } 3673 FREE_LOCK(ump); 3674 jseg = malloc(sizeof(*jseg), M_JSEG, M_SOFTDEP_FLAGS); 3675 workitem_alloc(&jseg->js_list, D_JSEG, mp); 3676 LIST_INIT(&jseg->js_entries); 3677 LIST_INIT(&jseg->js_indirs); 3678 jseg->js_state = ATTACHED; 3679 if (shouldflush == 0) 3680 jseg->js_state |= COMPLETE; 3681 else if (bio == NULL) 3682 bio = g_alloc_bio(); 3683 jseg->js_jblocks = jblocks; 3684 bp = geteblk(fs->fs_bsize, 0); 3685 ACQUIRE_LOCK(ump); 3686 /* 3687 * If there was a race while we were allocating the block 3688 * and jseg the entry we care about was likely written. 3689 * We bail out in both the WAIT and NOWAIT case and assume 3690 * the caller will loop if the entry it cares about is 3691 * not written. 3692 */ 3693 cnt = ump->softdep_on_journal; 3694 if (cnt + jblocks->jb_needseg == 0 || jblocks->jb_free == 0) { 3695 bp->b_flags |= B_INVAL | B_NOCACHE; 3696 WORKITEM_FREE(jseg, D_JSEG); 3697 FREE_LOCK(ump); 3698 brelse(bp); 3699 ACQUIRE_LOCK(ump); 3700 break; 3701 } 3702 /* 3703 * Calculate the disk block size required for the available 3704 * records rounded to the min size. 3705 */ 3706 if (cnt == 0) 3707 size = devbsize; 3708 else if (cnt < jrecmax) 3709 size = howmany(cnt, jrecmin) * devbsize; 3710 else 3711 size = fs->fs_bsize; 3712 /* 3713 * Allocate a disk block for this journal data and account 3714 * for truncation of the requested size if enough contiguous 3715 * space was not available. 3716 */ 3717 bp->b_blkno = jblocks_alloc(jblocks, size, &size); 3718 bp->b_lblkno = bp->b_blkno; 3719 bp->b_offset = bp->b_blkno * DEV_BSIZE; 3720 bp->b_bcount = size; 3721 bp->b_flags &= ~B_INVAL; 3722 bp->b_flags |= B_VALIDSUSPWRT | B_NOCOPY; 3723 /* 3724 * Initialize our jseg with cnt records. Assign the next 3725 * sequence number to it and link it in-order. 3726 */ 3727 cnt = MIN(cnt, (size / devbsize) * jrecmin); 3728 jseg->js_buf = bp; 3729 jseg->js_cnt = cnt; 3730 jseg->js_refs = cnt + 1; /* Self ref. */ 3731 jseg->js_size = size; 3732 jseg->js_seq = jblocks->jb_nextseq++; 3733 if (jblocks->jb_oldestseg == NULL) 3734 jblocks->jb_oldestseg = jseg; 3735 jseg->js_oldseq = jblocks->jb_oldestseg->js_seq; 3736 TAILQ_INSERT_TAIL(&jblocks->jb_segs, jseg, js_next); 3737 if (jblocks->jb_writeseg == NULL) 3738 jblocks->jb_writeseg = jseg; 3739 /* 3740 * Start filling in records from the pending list. 3741 */ 3742 data = bp->b_data; 3743 off = 0; 3744 3745 /* 3746 * Always put a header on the first block. 3747 * XXX As with below, there might not be a chance to get 3748 * into the loop. Ensure that something valid is written. 3749 */ 3750 jseg_write(ump, jseg, data); 3751 off += JREC_SIZE; 3752 data = bp->b_data + off; 3753 3754 /* 3755 * XXX Something is wrong here. There's no work to do, 3756 * but we need to perform and I/O and allow it to complete 3757 * anyways. 3758 */ 3759 if (LIST_EMPTY(&ump->softdep_journal_pending)) 3760 stat_emptyjblocks++; 3761 3762 while ((wk = LIST_FIRST(&ump->softdep_journal_pending)) 3763 != NULL) { 3764 if (cnt == 0) 3765 break; 3766 /* Place a segment header on every device block. */ 3767 if ((off % devbsize) == 0) { 3768 jseg_write(ump, jseg, data); 3769 off += JREC_SIZE; 3770 data = bp->b_data + off; 3771 } 3772 if (wk == needwk) 3773 needwk = NULL; 3774 remove_from_journal(wk); 3775 wk->wk_state |= INPROGRESS; 3776 WORKLIST_INSERT(&jseg->js_entries, wk); 3777 switch (wk->wk_type) { 3778 case D_JADDREF: 3779 jaddref_write(WK_JADDREF(wk), jseg, data); 3780 break; 3781 case D_JREMREF: 3782 jremref_write(WK_JREMREF(wk), jseg, data); 3783 break; 3784 case D_JMVREF: 3785 jmvref_write(WK_JMVREF(wk), jseg, data); 3786 break; 3787 case D_JNEWBLK: 3788 jnewblk_write(WK_JNEWBLK(wk), jseg, data); 3789 break; 3790 case D_JFREEBLK: 3791 jfreeblk_write(WK_JFREEBLK(wk), jseg, data); 3792 break; 3793 case D_JFREEFRAG: 3794 jfreefrag_write(WK_JFREEFRAG(wk), jseg, data); 3795 break; 3796 case D_JTRUNC: 3797 jtrunc_write(WK_JTRUNC(wk), jseg, data); 3798 break; 3799 case D_JFSYNC: 3800 jfsync_write(WK_JFSYNC(wk), jseg, data); 3801 break; 3802 default: 3803 panic("process_journal: Unknown type %s", 3804 TYPENAME(wk->wk_type)); 3805 /* NOTREACHED */ 3806 } 3807 off += JREC_SIZE; 3808 data = bp->b_data + off; 3809 cnt--; 3810 } 3811 3812 /* Clear any remaining space so we don't leak kernel data */ 3813 if (size > off) 3814 bzero(data, size - off); 3815 3816 /* 3817 * Write this one buffer and continue. 3818 */ 3819 segwritten = 1; 3820 jblocks->jb_needseg = 0; 3821 WORKLIST_INSERT(&bp->b_dep, &jseg->js_list); 3822 FREE_LOCK(ump); 3823 bp->b_xflags |= BX_CVTENXIO; 3824 pbgetvp(ump->um_devvp, bp); 3825 /* 3826 * We only do the blocking wait once we find the journal 3827 * entry we're looking for. 3828 */ 3829 if (needwk == NULL && flags == MNT_WAIT) 3830 bwrite(bp); 3831 else 3832 bawrite(bp); 3833 ACQUIRE_LOCK(ump); 3834 } 3835 /* 3836 * If we wrote a segment issue a synchronize cache so the journal 3837 * is reflected on disk before the data is written. Since reclaiming 3838 * journal space also requires writing a journal record this 3839 * process also enforces a barrier before reclamation. 3840 */ 3841 if (segwritten && shouldflush) { 3842 softdep_synchronize(bio, ump, 3843 TAILQ_LAST(&jblocks->jb_segs, jseglst)); 3844 } else if (bio) 3845 g_destroy_bio(bio); 3846 /* 3847 * If we've suspended the filesystem because we ran out of journal 3848 * space either try to sync it here to make some progress or 3849 * unsuspend it if we already have. 3850 */ 3851 if (flags == 0 && jblocks->jb_suspended) { 3852 if (journal_unsuspend(ump)) 3853 return; 3854 FREE_LOCK(ump); 3855 VFS_SYNC(mp, MNT_NOWAIT); 3856 ffs_sbupdate(ump, MNT_WAIT, 0); 3857 ACQUIRE_LOCK(ump); 3858 } 3859 } 3860 3861 /* 3862 * Complete a jseg, allowing all dependencies awaiting journal writes 3863 * to proceed. Each journal dependency also attaches a jsegdep to dependent 3864 * structures so that the journal segment can be freed to reclaim space. 3865 */ 3866 static void 3867 complete_jseg(struct jseg *jseg) 3868 { 3869 struct worklist *wk; 3870 struct jmvref *jmvref; 3871 #ifdef INVARIANTS 3872 int i = 0; 3873 #endif 3874 3875 while ((wk = LIST_FIRST(&jseg->js_entries)) != NULL) { 3876 WORKLIST_REMOVE(wk); 3877 wk->wk_state &= ~INPROGRESS; 3878 wk->wk_state |= COMPLETE; 3879 KASSERT(i++ < jseg->js_cnt, 3880 ("handle_written_jseg: overflow %d >= %d", 3881 i - 1, jseg->js_cnt)); 3882 switch (wk->wk_type) { 3883 case D_JADDREF: 3884 handle_written_jaddref(WK_JADDREF(wk)); 3885 break; 3886 case D_JREMREF: 3887 handle_written_jremref(WK_JREMREF(wk)); 3888 break; 3889 case D_JMVREF: 3890 rele_jseg(jseg); /* No jsegdep. */ 3891 jmvref = WK_JMVREF(wk); 3892 LIST_REMOVE(jmvref, jm_deps); 3893 if ((jmvref->jm_pagedep->pd_state & ONWORKLIST) == 0) 3894 free_pagedep(jmvref->jm_pagedep); 3895 WORKITEM_FREE(jmvref, D_JMVREF); 3896 break; 3897 case D_JNEWBLK: 3898 handle_written_jnewblk(WK_JNEWBLK(wk)); 3899 break; 3900 case D_JFREEBLK: 3901 handle_written_jblkdep(&WK_JFREEBLK(wk)->jf_dep); 3902 break; 3903 case D_JTRUNC: 3904 handle_written_jblkdep(&WK_JTRUNC(wk)->jt_dep); 3905 break; 3906 case D_JFSYNC: 3907 rele_jseg(jseg); /* No jsegdep. */ 3908 WORKITEM_FREE(wk, D_JFSYNC); 3909 break; 3910 case D_JFREEFRAG: 3911 handle_written_jfreefrag(WK_JFREEFRAG(wk)); 3912 break; 3913 default: 3914 panic("handle_written_jseg: Unknown type %s", 3915 TYPENAME(wk->wk_type)); 3916 /* NOTREACHED */ 3917 } 3918 } 3919 /* Release the self reference so the structure may be freed. */ 3920 rele_jseg(jseg); 3921 } 3922 3923 /* 3924 * Determine which jsegs are ready for completion processing. Waits for 3925 * synchronize cache to complete as well as forcing in-order completion 3926 * of journal entries. 3927 */ 3928 static void 3929 complete_jsegs(struct jseg *jseg) 3930 { 3931 struct jblocks *jblocks; 3932 struct jseg *jsegn; 3933 3934 jblocks = jseg->js_jblocks; 3935 /* 3936 * Don't allow out of order completions. If this isn't the first 3937 * block wait for it to write before we're done. 3938 */ 3939 if (jseg != jblocks->jb_writeseg) 3940 return; 3941 /* Iterate through available jsegs processing their entries. */ 3942 while (jseg && (jseg->js_state & ALLCOMPLETE) == ALLCOMPLETE) { 3943 jblocks->jb_oldestwrseq = jseg->js_oldseq; 3944 jsegn = TAILQ_NEXT(jseg, js_next); 3945 complete_jseg(jseg); 3946 jseg = jsegn; 3947 } 3948 jblocks->jb_writeseg = jseg; 3949 /* 3950 * Attempt to free jsegs now that oldestwrseq may have advanced. 3951 */ 3952 free_jsegs(jblocks); 3953 } 3954 3955 /* 3956 * Mark a jseg as DEPCOMPLETE and throw away the buffer. Attempt to handle 3957 * the final completions. 3958 */ 3959 static void 3960 handle_written_jseg(struct jseg *jseg, struct buf *bp) 3961 { 3962 3963 if (jseg->js_refs == 0) 3964 panic("handle_written_jseg: No self-reference on %p", jseg); 3965 jseg->js_state |= DEPCOMPLETE; 3966 /* 3967 * We'll never need this buffer again, set flags so it will be 3968 * discarded. 3969 */ 3970 bp->b_flags |= B_INVAL | B_NOCACHE; 3971 pbrelvp(bp); 3972 complete_jsegs(jseg); 3973 } 3974 3975 static inline struct jsegdep * 3976 inoref_jseg(struct inoref *inoref) 3977 { 3978 struct jsegdep *jsegdep; 3979 3980 jsegdep = inoref->if_jsegdep; 3981 inoref->if_jsegdep = NULL; 3982 3983 return (jsegdep); 3984 } 3985 3986 /* 3987 * Called once a jremref has made it to stable store. The jremref is marked 3988 * complete and we attempt to free it. Any pagedeps writes sleeping waiting 3989 * for the jremref to complete will be awoken by free_jremref. 3990 */ 3991 static void 3992 handle_written_jremref(struct jremref *jremref) 3993 { 3994 struct inodedep *inodedep; 3995 struct jsegdep *jsegdep; 3996 struct dirrem *dirrem; 3997 3998 /* Grab the jsegdep. */ 3999 jsegdep = inoref_jseg(&jremref->jr_ref); 4000 /* 4001 * Remove us from the inoref list. 4002 */ 4003 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 4004 0, &inodedep) == 0) 4005 panic("handle_written_jremref: Lost inodedep"); 4006 TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 4007 /* 4008 * Complete the dirrem. 4009 */ 4010 dirrem = jremref->jr_dirrem; 4011 jremref->jr_dirrem = NULL; 4012 LIST_REMOVE(jremref, jr_deps); 4013 jsegdep->jd_state |= jremref->jr_state & MKDIR_PARENT; 4014 jwork_insert(&dirrem->dm_jwork, jsegdep); 4015 if (LIST_EMPTY(&dirrem->dm_jremrefhd) && 4016 (dirrem->dm_state & COMPLETE) != 0) 4017 add_to_worklist(&dirrem->dm_list, 0); 4018 free_jremref(jremref); 4019 } 4020 4021 /* 4022 * Called once a jaddref has made it to stable store. The dependency is 4023 * marked complete and any dependent structures are added to the inode 4024 * bufwait list to be completed as soon as it is written. If a bitmap write 4025 * depends on this entry we move the inode into the inodedephd of the 4026 * bmsafemap dependency and attempt to remove the jaddref from the bmsafemap. 4027 */ 4028 static void 4029 handle_written_jaddref(struct jaddref *jaddref) 4030 { 4031 struct jsegdep *jsegdep; 4032 struct inodedep *inodedep; 4033 struct diradd *diradd; 4034 struct mkdir *mkdir; 4035 4036 /* Grab the jsegdep. */ 4037 jsegdep = inoref_jseg(&jaddref->ja_ref); 4038 mkdir = NULL; 4039 diradd = NULL; 4040 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 4041 0, &inodedep) == 0) 4042 panic("handle_written_jaddref: Lost inodedep."); 4043 if (jaddref->ja_diradd == NULL) 4044 panic("handle_written_jaddref: No dependency"); 4045 if (jaddref->ja_diradd->da_list.wk_type == D_DIRADD) { 4046 diradd = jaddref->ja_diradd; 4047 WORKLIST_INSERT(&inodedep->id_bufwait, &diradd->da_list); 4048 } else if (jaddref->ja_state & MKDIR_PARENT) { 4049 mkdir = jaddref->ja_mkdir; 4050 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir->md_list); 4051 } else if (jaddref->ja_state & MKDIR_BODY) 4052 mkdir = jaddref->ja_mkdir; 4053 else 4054 panic("handle_written_jaddref: Unknown dependency %p", 4055 jaddref->ja_diradd); 4056 jaddref->ja_diradd = NULL; /* also clears ja_mkdir */ 4057 /* 4058 * Remove us from the inode list. 4059 */ 4060 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps); 4061 /* 4062 * The mkdir may be waiting on the jaddref to clear before freeing. 4063 */ 4064 if (mkdir) { 4065 KASSERT(mkdir->md_list.wk_type == D_MKDIR, 4066 ("handle_written_jaddref: Incorrect type for mkdir %s", 4067 TYPENAME(mkdir->md_list.wk_type))); 4068 mkdir->md_jaddref = NULL; 4069 diradd = mkdir->md_diradd; 4070 mkdir->md_state |= DEPCOMPLETE; 4071 complete_mkdir(mkdir); 4072 } 4073 jwork_insert(&diradd->da_jwork, jsegdep); 4074 if (jaddref->ja_state & NEWBLOCK) { 4075 inodedep->id_state |= ONDEPLIST; 4076 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_inodedephd, 4077 inodedep, id_deps); 4078 } 4079 free_jaddref(jaddref); 4080 } 4081 4082 /* 4083 * Called once a jnewblk journal is written. The allocdirect or allocindir 4084 * is placed in the bmsafemap to await notification of a written bitmap. If 4085 * the operation was canceled we add the segdep to the appropriate 4086 * dependency to free the journal space once the canceling operation 4087 * completes. 4088 */ 4089 static void 4090 handle_written_jnewblk(struct jnewblk *jnewblk) 4091 { 4092 struct bmsafemap *bmsafemap; 4093 struct freefrag *freefrag; 4094 struct freework *freework; 4095 struct jsegdep *jsegdep; 4096 struct newblk *newblk; 4097 4098 /* Grab the jsegdep. */ 4099 jsegdep = jnewblk->jn_jsegdep; 4100 jnewblk->jn_jsegdep = NULL; 4101 if (jnewblk->jn_dep == NULL) 4102 panic("handle_written_jnewblk: No dependency for the segdep."); 4103 switch (jnewblk->jn_dep->wk_type) { 4104 case D_NEWBLK: 4105 case D_ALLOCDIRECT: 4106 case D_ALLOCINDIR: 4107 /* 4108 * Add the written block to the bmsafemap so it can 4109 * be notified when the bitmap is on disk. 4110 */ 4111 newblk = WK_NEWBLK(jnewblk->jn_dep); 4112 newblk->nb_jnewblk = NULL; 4113 if ((newblk->nb_state & GOINGAWAY) == 0) { 4114 bmsafemap = newblk->nb_bmsafemap; 4115 newblk->nb_state |= ONDEPLIST; 4116 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, 4117 nb_deps); 4118 } 4119 jwork_insert(&newblk->nb_jwork, jsegdep); 4120 break; 4121 case D_FREEFRAG: 4122 /* 4123 * A newblock being removed by a freefrag when replaced by 4124 * frag extension. 4125 */ 4126 freefrag = WK_FREEFRAG(jnewblk->jn_dep); 4127 freefrag->ff_jdep = NULL; 4128 jwork_insert(&freefrag->ff_jwork, jsegdep); 4129 break; 4130 case D_FREEWORK: 4131 /* 4132 * A direct block was removed by truncate. 4133 */ 4134 freework = WK_FREEWORK(jnewblk->jn_dep); 4135 freework->fw_jnewblk = NULL; 4136 jwork_insert(&freework->fw_freeblks->fb_jwork, jsegdep); 4137 break; 4138 default: 4139 panic("handle_written_jnewblk: Unknown type %d.", 4140 jnewblk->jn_dep->wk_type); 4141 } 4142 jnewblk->jn_dep = NULL; 4143 free_jnewblk(jnewblk); 4144 } 4145 4146 /* 4147 * Cancel a jfreefrag that won't be needed, probably due to colliding with 4148 * an in-flight allocation that has not yet been committed. Divorce us 4149 * from the freefrag and mark it DEPCOMPLETE so that it may be added 4150 * to the worklist. 4151 */ 4152 static void 4153 cancel_jfreefrag(struct jfreefrag *jfreefrag) 4154 { 4155 struct freefrag *freefrag; 4156 4157 if (jfreefrag->fr_jsegdep) { 4158 free_jsegdep(jfreefrag->fr_jsegdep); 4159 jfreefrag->fr_jsegdep = NULL; 4160 } 4161 freefrag = jfreefrag->fr_freefrag; 4162 jfreefrag->fr_freefrag = NULL; 4163 free_jfreefrag(jfreefrag); 4164 freefrag->ff_state |= DEPCOMPLETE; 4165 CTR1(KTR_SUJ, "cancel_jfreefrag: blkno %jd", freefrag->ff_blkno); 4166 } 4167 4168 /* 4169 * Free a jfreefrag when the parent freefrag is rendered obsolete. 4170 */ 4171 static void 4172 free_jfreefrag(struct jfreefrag *jfreefrag) 4173 { 4174 4175 if (jfreefrag->fr_state & INPROGRESS) 4176 WORKLIST_REMOVE(&jfreefrag->fr_list); 4177 else if (jfreefrag->fr_state & ONWORKLIST) 4178 remove_from_journal(&jfreefrag->fr_list); 4179 if (jfreefrag->fr_freefrag != NULL) 4180 panic("free_jfreefrag: Still attached to a freefrag."); 4181 WORKITEM_FREE(jfreefrag, D_JFREEFRAG); 4182 } 4183 4184 /* 4185 * Called when the journal write for a jfreefrag completes. The parent 4186 * freefrag is added to the worklist if this completes its dependencies. 4187 */ 4188 static void 4189 handle_written_jfreefrag(struct jfreefrag *jfreefrag) 4190 { 4191 struct jsegdep *jsegdep; 4192 struct freefrag *freefrag; 4193 4194 /* Grab the jsegdep. */ 4195 jsegdep = jfreefrag->fr_jsegdep; 4196 jfreefrag->fr_jsegdep = NULL; 4197 freefrag = jfreefrag->fr_freefrag; 4198 if (freefrag == NULL) 4199 panic("handle_written_jfreefrag: No freefrag."); 4200 freefrag->ff_state |= DEPCOMPLETE; 4201 freefrag->ff_jdep = NULL; 4202 jwork_insert(&freefrag->ff_jwork, jsegdep); 4203 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 4204 add_to_worklist(&freefrag->ff_list, 0); 4205 jfreefrag->fr_freefrag = NULL; 4206 free_jfreefrag(jfreefrag); 4207 } 4208 4209 /* 4210 * Called when the journal write for a jfreeblk completes. The jfreeblk 4211 * is removed from the freeblks list of pending journal writes and the 4212 * jsegdep is moved to the freeblks jwork to be completed when all blocks 4213 * have been reclaimed. 4214 */ 4215 static void 4216 handle_written_jblkdep(struct jblkdep *jblkdep) 4217 { 4218 struct freeblks *freeblks; 4219 struct jsegdep *jsegdep; 4220 4221 /* Grab the jsegdep. */ 4222 jsegdep = jblkdep->jb_jsegdep; 4223 jblkdep->jb_jsegdep = NULL; 4224 freeblks = jblkdep->jb_freeblks; 4225 LIST_REMOVE(jblkdep, jb_deps); 4226 jwork_insert(&freeblks->fb_jwork, jsegdep); 4227 /* 4228 * If the freeblks is all journaled, we can add it to the worklist. 4229 */ 4230 if (LIST_EMPTY(&freeblks->fb_jblkdephd) && 4231 (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 4232 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 4233 4234 free_jblkdep(jblkdep); 4235 } 4236 4237 static struct jsegdep * 4238 newjsegdep(struct worklist *wk) 4239 { 4240 struct jsegdep *jsegdep; 4241 4242 jsegdep = malloc(sizeof(*jsegdep), M_JSEGDEP, M_SOFTDEP_FLAGS); 4243 workitem_alloc(&jsegdep->jd_list, D_JSEGDEP, wk->wk_mp); 4244 jsegdep->jd_seg = NULL; 4245 4246 return (jsegdep); 4247 } 4248 4249 static struct jmvref * 4250 newjmvref(struct inode *dp, 4251 ino_t ino, 4252 off_t oldoff, 4253 off_t newoff) 4254 { 4255 struct jmvref *jmvref; 4256 4257 jmvref = malloc(sizeof(*jmvref), M_JMVREF, M_SOFTDEP_FLAGS); 4258 workitem_alloc(&jmvref->jm_list, D_JMVREF, ITOVFS(dp)); 4259 jmvref->jm_list.wk_state = ATTACHED | DEPCOMPLETE; 4260 jmvref->jm_parent = dp->i_number; 4261 jmvref->jm_ino = ino; 4262 jmvref->jm_oldoff = oldoff; 4263 jmvref->jm_newoff = newoff; 4264 4265 return (jmvref); 4266 } 4267 4268 /* 4269 * Allocate a new jremref that tracks the removal of ip from dp with the 4270 * directory entry offset of diroff. Mark the entry as ATTACHED and 4271 * DEPCOMPLETE as we have all the information required for the journal write 4272 * and the directory has already been removed from the buffer. The caller 4273 * is responsible for linking the jremref into the pagedep and adding it 4274 * to the journal to write. The MKDIR_PARENT flag is set if we're doing 4275 * a DOTDOT addition so handle_workitem_remove() can properly assign 4276 * the jsegdep when we're done. 4277 */ 4278 static struct jremref * 4279 newjremref(struct dirrem *dirrem, 4280 struct inode *dp, 4281 struct inode *ip, 4282 off_t diroff, 4283 nlink_t nlink) 4284 { 4285 struct jremref *jremref; 4286 4287 jremref = malloc(sizeof(*jremref), M_JREMREF, M_SOFTDEP_FLAGS); 4288 workitem_alloc(&jremref->jr_list, D_JREMREF, ITOVFS(dp)); 4289 jremref->jr_state = ATTACHED; 4290 newinoref(&jremref->jr_ref, ip->i_number, dp->i_number, diroff, 4291 nlink, ip->i_mode); 4292 jremref->jr_dirrem = dirrem; 4293 4294 return (jremref); 4295 } 4296 4297 static inline void 4298 newinoref(struct inoref *inoref, 4299 ino_t ino, 4300 ino_t parent, 4301 off_t diroff, 4302 nlink_t nlink, 4303 uint16_t mode) 4304 { 4305 4306 inoref->if_jsegdep = newjsegdep(&inoref->if_list); 4307 inoref->if_diroff = diroff; 4308 inoref->if_ino = ino; 4309 inoref->if_parent = parent; 4310 inoref->if_nlink = nlink; 4311 inoref->if_mode = mode; 4312 } 4313 4314 /* 4315 * Allocate a new jaddref to track the addition of ino to dp at diroff. The 4316 * directory offset may not be known until later. The caller is responsible 4317 * adding the entry to the journal when this information is available. nlink 4318 * should be the link count prior to the addition and mode is only required 4319 * to have the correct FMT. 4320 */ 4321 static struct jaddref * 4322 newjaddref(struct inode *dp, 4323 ino_t ino, 4324 off_t diroff, 4325 int16_t nlink, 4326 uint16_t mode) 4327 { 4328 struct jaddref *jaddref; 4329 4330 jaddref = malloc(sizeof(*jaddref), M_JADDREF, M_SOFTDEP_FLAGS); 4331 workitem_alloc(&jaddref->ja_list, D_JADDREF, ITOVFS(dp)); 4332 jaddref->ja_state = ATTACHED; 4333 jaddref->ja_mkdir = NULL; 4334 newinoref(&jaddref->ja_ref, ino, dp->i_number, diroff, nlink, mode); 4335 4336 return (jaddref); 4337 } 4338 4339 /* 4340 * Create a new free dependency for a freework. The caller is responsible 4341 * for adjusting the reference count when it has the lock held. The freedep 4342 * will track an outstanding bitmap write that will ultimately clear the 4343 * freework to continue. 4344 */ 4345 static struct freedep * 4346 newfreedep(struct freework *freework) 4347 { 4348 struct freedep *freedep; 4349 4350 freedep = malloc(sizeof(*freedep), M_FREEDEP, M_SOFTDEP_FLAGS); 4351 workitem_alloc(&freedep->fd_list, D_FREEDEP, freework->fw_list.wk_mp); 4352 freedep->fd_freework = freework; 4353 4354 return (freedep); 4355 } 4356 4357 /* 4358 * Free a freedep structure once the buffer it is linked to is written. If 4359 * this is the last reference to the freework schedule it for completion. 4360 */ 4361 static void 4362 free_freedep(struct freedep *freedep) 4363 { 4364 struct freework *freework; 4365 4366 freework = freedep->fd_freework; 4367 freework->fw_freeblks->fb_cgwait--; 4368 if (--freework->fw_ref == 0) 4369 freework_enqueue(freework); 4370 WORKITEM_FREE(freedep, D_FREEDEP); 4371 } 4372 4373 /* 4374 * Allocate a new freework structure that may be a level in an indirect 4375 * when parent is not NULL or a top level block when it is. The top level 4376 * freework structures are allocated without the per-filesystem lock held 4377 * and before the freeblks is visible outside of softdep_setup_freeblocks(). 4378 */ 4379 static struct freework * 4380 newfreework(struct ufsmount *ump, 4381 struct freeblks *freeblks, 4382 struct freework *parent, 4383 ufs_lbn_t lbn, 4384 ufs2_daddr_t nb, 4385 int frags, 4386 int off, 4387 int journal) 4388 { 4389 struct freework *freework; 4390 4391 freework = malloc(sizeof(*freework), M_FREEWORK, M_SOFTDEP_FLAGS); 4392 workitem_alloc(&freework->fw_list, D_FREEWORK, freeblks->fb_list.wk_mp); 4393 freework->fw_state = ATTACHED; 4394 freework->fw_jnewblk = NULL; 4395 freework->fw_freeblks = freeblks; 4396 freework->fw_parent = parent; 4397 freework->fw_lbn = lbn; 4398 freework->fw_blkno = nb; 4399 freework->fw_frags = frags; 4400 freework->fw_indir = NULL; 4401 freework->fw_ref = (MOUNTEDSUJ(UFSTOVFS(ump)) == 0 || 4402 lbn >= -UFS_NXADDR) ? 0 : NINDIR(ump->um_fs) + 1; 4403 freework->fw_start = freework->fw_off = off; 4404 if (journal) 4405 newjfreeblk(freeblks, lbn, nb, frags); 4406 if (parent == NULL) { 4407 ACQUIRE_LOCK(ump); 4408 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 4409 freeblks->fb_ref++; 4410 FREE_LOCK(ump); 4411 } 4412 4413 return (freework); 4414 } 4415 4416 /* 4417 * Eliminate a jfreeblk for a block that does not need journaling. 4418 */ 4419 static void 4420 cancel_jfreeblk(struct freeblks *freeblks, ufs2_daddr_t blkno) 4421 { 4422 struct jfreeblk *jfreeblk; 4423 struct jblkdep *jblkdep; 4424 4425 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) { 4426 if (jblkdep->jb_list.wk_type != D_JFREEBLK) 4427 continue; 4428 jfreeblk = WK_JFREEBLK(&jblkdep->jb_list); 4429 if (jfreeblk->jf_blkno == blkno) 4430 break; 4431 } 4432 if (jblkdep == NULL) 4433 return; 4434 CTR1(KTR_SUJ, "cancel_jfreeblk: blkno %jd", blkno); 4435 free_jsegdep(jblkdep->jb_jsegdep); 4436 LIST_REMOVE(jblkdep, jb_deps); 4437 WORKITEM_FREE(jfreeblk, D_JFREEBLK); 4438 } 4439 4440 /* 4441 * Allocate a new jfreeblk to journal top level block pointer when truncating 4442 * a file. The caller must add this to the worklist when the per-filesystem 4443 * lock is held. 4444 */ 4445 static struct jfreeblk * 4446 newjfreeblk(struct freeblks *freeblks, 4447 ufs_lbn_t lbn, 4448 ufs2_daddr_t blkno, 4449 int frags) 4450 { 4451 struct jfreeblk *jfreeblk; 4452 4453 jfreeblk = malloc(sizeof(*jfreeblk), M_JFREEBLK, M_SOFTDEP_FLAGS); 4454 workitem_alloc(&jfreeblk->jf_dep.jb_list, D_JFREEBLK, 4455 freeblks->fb_list.wk_mp); 4456 jfreeblk->jf_dep.jb_jsegdep = newjsegdep(&jfreeblk->jf_dep.jb_list); 4457 jfreeblk->jf_dep.jb_freeblks = freeblks; 4458 jfreeblk->jf_ino = freeblks->fb_inum; 4459 jfreeblk->jf_lbn = lbn; 4460 jfreeblk->jf_blkno = blkno; 4461 jfreeblk->jf_frags = frags; 4462 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jfreeblk->jf_dep, jb_deps); 4463 4464 return (jfreeblk); 4465 } 4466 4467 /* 4468 * The journal is only prepared to handle full-size block numbers, so we 4469 * have to adjust the record to reflect the change to a full-size block. 4470 * For example, suppose we have a block made up of fragments 8-15 and 4471 * want to free its last two fragments. We are given a request that says: 4472 * FREEBLK ino=5, blkno=14, lbn=0, frags=2, oldfrags=0 4473 * where frags are the number of fragments to free and oldfrags are the 4474 * number of fragments to keep. To block align it, we have to change it to 4475 * have a valid full-size blkno, so it becomes: 4476 * FREEBLK ino=5, blkno=8, lbn=0, frags=2, oldfrags=6 4477 */ 4478 static void 4479 adjust_newfreework(struct freeblks *freeblks, int frag_offset) 4480 { 4481 struct jfreeblk *jfreeblk; 4482 4483 KASSERT((LIST_FIRST(&freeblks->fb_jblkdephd) != NULL && 4484 LIST_FIRST(&freeblks->fb_jblkdephd)->jb_list.wk_type == D_JFREEBLK), 4485 ("adjust_newfreework: Missing freeblks dependency")); 4486 4487 jfreeblk = WK_JFREEBLK(LIST_FIRST(&freeblks->fb_jblkdephd)); 4488 jfreeblk->jf_blkno -= frag_offset; 4489 jfreeblk->jf_frags += frag_offset; 4490 } 4491 4492 /* 4493 * Allocate a new jtrunc to track a partial truncation. 4494 */ 4495 static struct jtrunc * 4496 newjtrunc(struct freeblks *freeblks, 4497 off_t size, 4498 int extsize) 4499 { 4500 struct jtrunc *jtrunc; 4501 4502 jtrunc = malloc(sizeof(*jtrunc), M_JTRUNC, M_SOFTDEP_FLAGS); 4503 workitem_alloc(&jtrunc->jt_dep.jb_list, D_JTRUNC, 4504 freeblks->fb_list.wk_mp); 4505 jtrunc->jt_dep.jb_jsegdep = newjsegdep(&jtrunc->jt_dep.jb_list); 4506 jtrunc->jt_dep.jb_freeblks = freeblks; 4507 jtrunc->jt_ino = freeblks->fb_inum; 4508 jtrunc->jt_size = size; 4509 jtrunc->jt_extsize = extsize; 4510 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jtrunc->jt_dep, jb_deps); 4511 4512 return (jtrunc); 4513 } 4514 4515 /* 4516 * If we're canceling a new bitmap we have to search for another ref 4517 * to move into the bmsafemap dep. This might be better expressed 4518 * with another structure. 4519 */ 4520 static void 4521 move_newblock_dep(struct jaddref *jaddref, struct inodedep *inodedep) 4522 { 4523 struct inoref *inoref; 4524 struct jaddref *jaddrefn; 4525 4526 jaddrefn = NULL; 4527 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4528 inoref = TAILQ_NEXT(inoref, if_deps)) { 4529 if ((jaddref->ja_state & NEWBLOCK) && 4530 inoref->if_list.wk_type == D_JADDREF) { 4531 jaddrefn = (struct jaddref *)inoref; 4532 break; 4533 } 4534 } 4535 if (jaddrefn == NULL) 4536 return; 4537 jaddrefn->ja_state &= ~(ATTACHED | UNDONE); 4538 jaddrefn->ja_state |= jaddref->ja_state & 4539 (ATTACHED | UNDONE | NEWBLOCK); 4540 jaddref->ja_state &= ~(ATTACHED | UNDONE | NEWBLOCK); 4541 jaddref->ja_state |= ATTACHED; 4542 LIST_REMOVE(jaddref, ja_bmdeps); 4543 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_jaddrefhd, jaddrefn, 4544 ja_bmdeps); 4545 } 4546 4547 /* 4548 * Cancel a jaddref either before it has been written or while it is being 4549 * written. This happens when a link is removed before the add reaches 4550 * the disk. The jaddref dependency is kept linked into the bmsafemap 4551 * and inode to prevent the link count or bitmap from reaching the disk 4552 * until handle_workitem_remove() re-adjusts the counts and bitmaps as 4553 * required. 4554 * 4555 * Returns 1 if the canceled addref requires journaling of the remove and 4556 * 0 otherwise. 4557 */ 4558 static int 4559 cancel_jaddref(struct jaddref *jaddref, 4560 struct inodedep *inodedep, 4561 struct workhead *wkhd) 4562 { 4563 struct inoref *inoref; 4564 struct jsegdep *jsegdep; 4565 int needsj; 4566 4567 KASSERT((jaddref->ja_state & COMPLETE) == 0, 4568 ("cancel_jaddref: Canceling complete jaddref")); 4569 if (jaddref->ja_state & (INPROGRESS | COMPLETE)) 4570 needsj = 1; 4571 else 4572 needsj = 0; 4573 if (inodedep == NULL) 4574 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 4575 0, &inodedep) == 0) 4576 panic("cancel_jaddref: Lost inodedep"); 4577 /* 4578 * We must adjust the nlink of any reference operation that follows 4579 * us so that it is consistent with the in-memory reference. This 4580 * ensures that inode nlink rollbacks always have the correct link. 4581 */ 4582 if (needsj == 0) { 4583 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4584 inoref = TAILQ_NEXT(inoref, if_deps)) { 4585 if (inoref->if_state & GOINGAWAY) 4586 break; 4587 inoref->if_nlink--; 4588 } 4589 } 4590 jsegdep = inoref_jseg(&jaddref->ja_ref); 4591 if (jaddref->ja_state & NEWBLOCK) 4592 move_newblock_dep(jaddref, inodedep); 4593 wake_worklist(&jaddref->ja_list); 4594 jaddref->ja_mkdir = NULL; 4595 if (jaddref->ja_state & INPROGRESS) { 4596 jaddref->ja_state &= ~INPROGRESS; 4597 WORKLIST_REMOVE(&jaddref->ja_list); 4598 jwork_insert(wkhd, jsegdep); 4599 } else { 4600 free_jsegdep(jsegdep); 4601 if (jaddref->ja_state & DEPCOMPLETE) 4602 remove_from_journal(&jaddref->ja_list); 4603 } 4604 jaddref->ja_state |= (GOINGAWAY | DEPCOMPLETE); 4605 /* 4606 * Leave NEWBLOCK jaddrefs on the inodedep so handle_workitem_remove 4607 * can arrange for them to be freed with the bitmap. Otherwise we 4608 * no longer need this addref attached to the inoreflst and it 4609 * will incorrectly adjust nlink if we leave it. 4610 */ 4611 if ((jaddref->ja_state & NEWBLOCK) == 0) { 4612 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 4613 if_deps); 4614 jaddref->ja_state |= COMPLETE; 4615 free_jaddref(jaddref); 4616 return (needsj); 4617 } 4618 /* 4619 * Leave the head of the list for jsegdeps for fast merging. 4620 */ 4621 if (LIST_FIRST(wkhd) != NULL) { 4622 jaddref->ja_state |= ONWORKLIST; 4623 LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list); 4624 } else 4625 WORKLIST_INSERT(wkhd, &jaddref->ja_list); 4626 4627 return (needsj); 4628 } 4629 4630 /* 4631 * Attempt to free a jaddref structure when some work completes. This 4632 * should only succeed once the entry is written and all dependencies have 4633 * been notified. 4634 */ 4635 static void 4636 free_jaddref(struct jaddref *jaddref) 4637 { 4638 4639 if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE) 4640 return; 4641 if (jaddref->ja_ref.if_jsegdep) 4642 panic("free_jaddref: segdep attached to jaddref %p(0x%X)\n", 4643 jaddref, jaddref->ja_state); 4644 if (jaddref->ja_state & NEWBLOCK) 4645 LIST_REMOVE(jaddref, ja_bmdeps); 4646 if (jaddref->ja_state & (INPROGRESS | ONWORKLIST)) 4647 panic("free_jaddref: Bad state %p(0x%X)", 4648 jaddref, jaddref->ja_state); 4649 if (jaddref->ja_mkdir != NULL) 4650 panic("free_jaddref: Work pending, 0x%X\n", jaddref->ja_state); 4651 WORKITEM_FREE(jaddref, D_JADDREF); 4652 } 4653 4654 /* 4655 * Free a jremref structure once it has been written or discarded. 4656 */ 4657 static void 4658 free_jremref(struct jremref *jremref) 4659 { 4660 4661 if (jremref->jr_ref.if_jsegdep) 4662 free_jsegdep(jremref->jr_ref.if_jsegdep); 4663 if (jremref->jr_state & INPROGRESS) 4664 panic("free_jremref: IO still pending"); 4665 WORKITEM_FREE(jremref, D_JREMREF); 4666 } 4667 4668 /* 4669 * Free a jnewblk structure. 4670 */ 4671 static void 4672 free_jnewblk(struct jnewblk *jnewblk) 4673 { 4674 4675 if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE) 4676 return; 4677 LIST_REMOVE(jnewblk, jn_deps); 4678 if (jnewblk->jn_dep != NULL) 4679 panic("free_jnewblk: Dependency still attached."); 4680 WORKITEM_FREE(jnewblk, D_JNEWBLK); 4681 } 4682 4683 /* 4684 * Cancel a jnewblk which has been been made redundant by frag extension. 4685 */ 4686 static void 4687 cancel_jnewblk(struct jnewblk *jnewblk, struct workhead *wkhd) 4688 { 4689 struct jsegdep *jsegdep; 4690 4691 CTR1(KTR_SUJ, "cancel_jnewblk: blkno %jd", jnewblk->jn_blkno); 4692 jsegdep = jnewblk->jn_jsegdep; 4693 if (jnewblk->jn_jsegdep == NULL || jnewblk->jn_dep == NULL) 4694 panic("cancel_jnewblk: Invalid state"); 4695 jnewblk->jn_jsegdep = NULL; 4696 jnewblk->jn_dep = NULL; 4697 jnewblk->jn_state |= GOINGAWAY; 4698 if (jnewblk->jn_state & INPROGRESS) { 4699 jnewblk->jn_state &= ~INPROGRESS; 4700 WORKLIST_REMOVE(&jnewblk->jn_list); 4701 jwork_insert(wkhd, jsegdep); 4702 } else { 4703 free_jsegdep(jsegdep); 4704 remove_from_journal(&jnewblk->jn_list); 4705 } 4706 wake_worklist(&jnewblk->jn_list); 4707 WORKLIST_INSERT(wkhd, &jnewblk->jn_list); 4708 } 4709 4710 static void 4711 free_jblkdep(struct jblkdep *jblkdep) 4712 { 4713 4714 if (jblkdep->jb_list.wk_type == D_JFREEBLK) 4715 WORKITEM_FREE(jblkdep, D_JFREEBLK); 4716 else if (jblkdep->jb_list.wk_type == D_JTRUNC) 4717 WORKITEM_FREE(jblkdep, D_JTRUNC); 4718 else 4719 panic("free_jblkdep: Unexpected type %s", 4720 TYPENAME(jblkdep->jb_list.wk_type)); 4721 } 4722 4723 /* 4724 * Free a single jseg once it is no longer referenced in memory or on 4725 * disk. Reclaim journal blocks and dependencies waiting for the segment 4726 * to disappear. 4727 */ 4728 static void 4729 free_jseg(struct jseg *jseg, struct jblocks *jblocks) 4730 { 4731 struct freework *freework; 4732 4733 /* 4734 * Free freework structures that were lingering to indicate freed 4735 * indirect blocks that forced journal write ordering on reallocate. 4736 */ 4737 while ((freework = LIST_FIRST(&jseg->js_indirs)) != NULL) 4738 indirblk_remove(freework); 4739 if (jblocks->jb_oldestseg == jseg) 4740 jblocks->jb_oldestseg = TAILQ_NEXT(jseg, js_next); 4741 TAILQ_REMOVE(&jblocks->jb_segs, jseg, js_next); 4742 jblocks_free(jblocks, jseg->js_list.wk_mp, jseg->js_size); 4743 KASSERT(LIST_EMPTY(&jseg->js_entries), 4744 ("free_jseg: Freed jseg has valid entries.")); 4745 WORKITEM_FREE(jseg, D_JSEG); 4746 } 4747 4748 /* 4749 * Free all jsegs that meet the criteria for being reclaimed and update 4750 * oldestseg. 4751 */ 4752 static void 4753 free_jsegs(struct jblocks *jblocks) 4754 { 4755 struct jseg *jseg; 4756 4757 /* 4758 * Free only those jsegs which have none allocated before them to 4759 * preserve the journal space ordering. 4760 */ 4761 while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) { 4762 /* 4763 * Only reclaim space when nothing depends on this journal 4764 * set and another set has written that it is no longer 4765 * valid. 4766 */ 4767 if (jseg->js_refs != 0) { 4768 jblocks->jb_oldestseg = jseg; 4769 return; 4770 } 4771 if ((jseg->js_state & ALLCOMPLETE) != ALLCOMPLETE) 4772 break; 4773 if (jseg->js_seq > jblocks->jb_oldestwrseq) 4774 break; 4775 /* 4776 * We can free jsegs that didn't write entries when 4777 * oldestwrseq == js_seq. 4778 */ 4779 if (jseg->js_seq == jblocks->jb_oldestwrseq && 4780 jseg->js_cnt != 0) 4781 break; 4782 free_jseg(jseg, jblocks); 4783 } 4784 /* 4785 * If we exited the loop above we still must discover the 4786 * oldest valid segment. 4787 */ 4788 if (jseg) 4789 for (jseg = jblocks->jb_oldestseg; jseg != NULL; 4790 jseg = TAILQ_NEXT(jseg, js_next)) 4791 if (jseg->js_refs != 0) 4792 break; 4793 jblocks->jb_oldestseg = jseg; 4794 /* 4795 * The journal has no valid records but some jsegs may still be 4796 * waiting on oldestwrseq to advance. We force a small record 4797 * out to permit these lingering records to be reclaimed. 4798 */ 4799 if (jblocks->jb_oldestseg == NULL && !TAILQ_EMPTY(&jblocks->jb_segs)) 4800 jblocks->jb_needseg = 1; 4801 } 4802 4803 /* 4804 * Release one reference to a jseg and free it if the count reaches 0. This 4805 * should eventually reclaim journal space as well. 4806 */ 4807 static void 4808 rele_jseg(struct jseg *jseg) 4809 { 4810 4811 KASSERT(jseg->js_refs > 0, 4812 ("free_jseg: Invalid refcnt %d", jseg->js_refs)); 4813 if (--jseg->js_refs != 0) 4814 return; 4815 free_jsegs(jseg->js_jblocks); 4816 } 4817 4818 /* 4819 * Release a jsegdep and decrement the jseg count. 4820 */ 4821 static void 4822 free_jsegdep(struct jsegdep *jsegdep) 4823 { 4824 4825 if (jsegdep->jd_seg) 4826 rele_jseg(jsegdep->jd_seg); 4827 WORKITEM_FREE(jsegdep, D_JSEGDEP); 4828 } 4829 4830 /* 4831 * Wait for a journal item to make it to disk. Initiate journal processing 4832 * if required. 4833 */ 4834 static int 4835 jwait(struct worklist *wk, int waitfor) 4836 { 4837 4838 LOCK_OWNED(VFSTOUFS(wk->wk_mp)); 4839 /* 4840 * Blocking journal waits cause slow synchronous behavior. Record 4841 * stats on the frequency of these blocking operations. 4842 */ 4843 if (waitfor == MNT_WAIT) { 4844 stat_journal_wait++; 4845 switch (wk->wk_type) { 4846 case D_JREMREF: 4847 case D_JMVREF: 4848 stat_jwait_filepage++; 4849 break; 4850 case D_JTRUNC: 4851 case D_JFREEBLK: 4852 stat_jwait_freeblks++; 4853 break; 4854 case D_JNEWBLK: 4855 stat_jwait_newblk++; 4856 break; 4857 case D_JADDREF: 4858 stat_jwait_inode++; 4859 break; 4860 default: 4861 break; 4862 } 4863 } 4864 /* 4865 * If IO has not started we process the journal. We can't mark the 4866 * worklist item as IOWAITING because we drop the lock while 4867 * processing the journal and the worklist entry may be freed after 4868 * this point. The caller may call back in and re-issue the request. 4869 */ 4870 if ((wk->wk_state & INPROGRESS) == 0) { 4871 softdep_process_journal(wk->wk_mp, wk, waitfor); 4872 if (waitfor != MNT_WAIT) 4873 return (EBUSY); 4874 return (0); 4875 } 4876 if (waitfor != MNT_WAIT) 4877 return (EBUSY); 4878 wait_worklist(wk, "jwait"); 4879 return (0); 4880 } 4881 4882 /* 4883 * Lookup an inodedep based on an inode pointer and set the nlinkdelta as 4884 * appropriate. This is a convenience function to reduce duplicate code 4885 * for the setup and revert functions below. 4886 */ 4887 static struct inodedep * 4888 inodedep_lookup_ip(struct inode *ip) 4889 { 4890 struct inodedep *inodedep; 4891 4892 KASSERT(ip->i_nlink >= ip->i_effnlink, 4893 ("inodedep_lookup_ip: bad delta")); 4894 (void) inodedep_lookup(ITOVFS(ip), ip->i_number, DEPALLOC, 4895 &inodedep); 4896 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 4897 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 4898 4899 return (inodedep); 4900 } 4901 4902 /* 4903 * Called prior to creating a new inode and linking it to a directory. The 4904 * jaddref structure must already be allocated by softdep_setup_inomapdep 4905 * and it is discovered here so we can initialize the mode and update 4906 * nlinkdelta. 4907 */ 4908 void 4909 softdep_setup_create(struct inode *dp, struct inode *ip) 4910 { 4911 struct inodedep *inodedep; 4912 struct jaddref *jaddref __diagused; 4913 struct vnode *dvp; 4914 4915 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4916 ("softdep_setup_create called on non-softdep filesystem")); 4917 KASSERT(ip->i_nlink == 1, 4918 ("softdep_setup_create: Invalid link count.")); 4919 dvp = ITOV(dp); 4920 ACQUIRE_LOCK(ITOUMP(dp)); 4921 inodedep = inodedep_lookup_ip(ip); 4922 if (DOINGSUJ(dvp)) { 4923 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4924 inoreflst); 4925 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 4926 ("softdep_setup_create: No addref structure present.")); 4927 } 4928 FREE_LOCK(ITOUMP(dp)); 4929 } 4930 4931 /* 4932 * Create a jaddref structure to track the addition of a DOTDOT link when 4933 * we are reparenting an inode as part of a rename. This jaddref will be 4934 * found by softdep_setup_directory_change. Adjusts nlinkdelta for 4935 * non-journaling softdep. 4936 */ 4937 void 4938 softdep_setup_dotdot_link(struct inode *dp, struct inode *ip) 4939 { 4940 struct inodedep *inodedep; 4941 struct jaddref *jaddref; 4942 struct vnode *dvp; 4943 4944 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4945 ("softdep_setup_dotdot_link called on non-softdep filesystem")); 4946 dvp = ITOV(dp); 4947 jaddref = NULL; 4948 /* 4949 * We don't set MKDIR_PARENT as this is not tied to a mkdir and 4950 * is used as a normal link would be. 4951 */ 4952 if (DOINGSUJ(dvp)) 4953 jaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 4954 dp->i_effnlink - 1, dp->i_mode); 4955 ACQUIRE_LOCK(ITOUMP(dp)); 4956 inodedep = inodedep_lookup_ip(dp); 4957 if (jaddref) 4958 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4959 if_deps); 4960 FREE_LOCK(ITOUMP(dp)); 4961 } 4962 4963 /* 4964 * Create a jaddref structure to track a new link to an inode. The directory 4965 * offset is not known until softdep_setup_directory_add or 4966 * softdep_setup_directory_change. Adjusts nlinkdelta for non-journaling 4967 * softdep. 4968 */ 4969 void 4970 softdep_setup_link(struct inode *dp, struct inode *ip) 4971 { 4972 struct inodedep *inodedep; 4973 struct jaddref *jaddref; 4974 struct vnode *dvp; 4975 4976 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 4977 ("softdep_setup_link called on non-softdep filesystem")); 4978 dvp = ITOV(dp); 4979 jaddref = NULL; 4980 if (DOINGSUJ(dvp)) 4981 jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1, 4982 ip->i_mode); 4983 ACQUIRE_LOCK(ITOUMP(dp)); 4984 inodedep = inodedep_lookup_ip(ip); 4985 if (jaddref) 4986 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4987 if_deps); 4988 FREE_LOCK(ITOUMP(dp)); 4989 } 4990 4991 /* 4992 * Called to create the jaddref structures to track . and .. references as 4993 * well as lookup and further initialize the incomplete jaddref created 4994 * by softdep_setup_inomapdep when the inode was allocated. Adjusts 4995 * nlinkdelta for non-journaling softdep. 4996 */ 4997 void 4998 softdep_setup_mkdir(struct inode *dp, struct inode *ip) 4999 { 5000 struct inodedep *inodedep; 5001 struct jaddref *dotdotaddref; 5002 struct jaddref *dotaddref; 5003 struct jaddref *jaddref; 5004 struct vnode *dvp; 5005 5006 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5007 ("softdep_setup_mkdir called on non-softdep filesystem")); 5008 dvp = ITOV(dp); 5009 dotaddref = dotdotaddref = NULL; 5010 if (DOINGSUJ(dvp)) { 5011 dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1, 5012 ip->i_mode); 5013 dotaddref->ja_state |= MKDIR_BODY; 5014 dotdotaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 5015 dp->i_effnlink - 1, dp->i_mode); 5016 dotdotaddref->ja_state |= MKDIR_PARENT; 5017 } 5018 ACQUIRE_LOCK(ITOUMP(dp)); 5019 inodedep = inodedep_lookup_ip(ip); 5020 if (DOINGSUJ(dvp)) { 5021 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 5022 inoreflst); 5023 KASSERT(jaddref != NULL, 5024 ("softdep_setup_mkdir: No addref structure present.")); 5025 KASSERT(jaddref->ja_parent == dp->i_number, 5026 ("softdep_setup_mkdir: bad parent %ju", 5027 (uintmax_t)jaddref->ja_parent)); 5028 TAILQ_INSERT_BEFORE(&jaddref->ja_ref, &dotaddref->ja_ref, 5029 if_deps); 5030 } 5031 inodedep = inodedep_lookup_ip(dp); 5032 if (DOINGSUJ(dvp)) 5033 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, 5034 &dotdotaddref->ja_ref, if_deps); 5035 FREE_LOCK(ITOUMP(dp)); 5036 } 5037 5038 /* 5039 * Called to track nlinkdelta of the inode and parent directories prior to 5040 * unlinking a directory. 5041 */ 5042 void 5043 softdep_setup_rmdir(struct inode *dp, struct inode *ip) 5044 { 5045 5046 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5047 ("softdep_setup_rmdir called on non-softdep filesystem")); 5048 ACQUIRE_LOCK(ITOUMP(dp)); 5049 (void) inodedep_lookup_ip(ip); 5050 (void) inodedep_lookup_ip(dp); 5051 FREE_LOCK(ITOUMP(dp)); 5052 } 5053 5054 /* 5055 * Called to track nlinkdelta of the inode and parent directories prior to 5056 * unlink. 5057 */ 5058 void 5059 softdep_setup_unlink(struct inode *dp, struct inode *ip) 5060 { 5061 5062 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5063 ("softdep_setup_unlink called on non-softdep filesystem")); 5064 ACQUIRE_LOCK(ITOUMP(dp)); 5065 (void) inodedep_lookup_ip(ip); 5066 (void) inodedep_lookup_ip(dp); 5067 FREE_LOCK(ITOUMP(dp)); 5068 } 5069 5070 /* 5071 * Called to release the journal structures created by a failed non-directory 5072 * creation. Adjusts nlinkdelta for non-journaling softdep. 5073 */ 5074 void 5075 softdep_revert_create(struct inode *dp, struct inode *ip) 5076 { 5077 struct inodedep *inodedep; 5078 struct jaddref *jaddref; 5079 struct vnode *dvp; 5080 5081 KASSERT(MOUNTEDSOFTDEP(ITOVFS((dp))) != 0, 5082 ("softdep_revert_create called on non-softdep filesystem")); 5083 dvp = ITOV(dp); 5084 ACQUIRE_LOCK(ITOUMP(dp)); 5085 inodedep = inodedep_lookup_ip(ip); 5086 if (DOINGSUJ(dvp)) { 5087 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 5088 inoreflst); 5089 KASSERT(jaddref->ja_parent == dp->i_number, 5090 ("softdep_revert_create: addref parent mismatch")); 5091 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 5092 } 5093 FREE_LOCK(ITOUMP(dp)); 5094 } 5095 5096 /* 5097 * Called to release the journal structures created by a failed link 5098 * addition. Adjusts nlinkdelta for non-journaling softdep. 5099 */ 5100 void 5101 softdep_revert_link(struct inode *dp, struct inode *ip) 5102 { 5103 struct inodedep *inodedep; 5104 struct jaddref *jaddref; 5105 struct vnode *dvp; 5106 5107 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5108 ("softdep_revert_link called on non-softdep filesystem")); 5109 dvp = ITOV(dp); 5110 ACQUIRE_LOCK(ITOUMP(dp)); 5111 inodedep = inodedep_lookup_ip(ip); 5112 if (DOINGSUJ(dvp)) { 5113 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 5114 inoreflst); 5115 KASSERT(jaddref->ja_parent == dp->i_number, 5116 ("softdep_revert_link: addref parent mismatch")); 5117 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 5118 } 5119 FREE_LOCK(ITOUMP(dp)); 5120 } 5121 5122 /* 5123 * Called to release the journal structures created by a failed mkdir 5124 * attempt. Adjusts nlinkdelta for non-journaling softdep. 5125 */ 5126 void 5127 softdep_revert_mkdir(struct inode *dp, struct inode *ip) 5128 { 5129 struct inodedep *inodedep; 5130 struct jaddref *jaddref; 5131 struct jaddref *dotaddref; 5132 struct vnode *dvp; 5133 5134 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5135 ("softdep_revert_mkdir called on non-softdep filesystem")); 5136 dvp = ITOV(dp); 5137 5138 ACQUIRE_LOCK(ITOUMP(dp)); 5139 inodedep = inodedep_lookup_ip(dp); 5140 if (DOINGSUJ(dvp)) { 5141 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 5142 inoreflst); 5143 KASSERT(jaddref->ja_parent == ip->i_number, 5144 ("softdep_revert_mkdir: dotdot addref parent mismatch")); 5145 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 5146 } 5147 inodedep = inodedep_lookup_ip(ip); 5148 if (DOINGSUJ(dvp)) { 5149 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 5150 inoreflst); 5151 KASSERT(jaddref->ja_parent == dp->i_number, 5152 ("softdep_revert_mkdir: addref parent mismatch")); 5153 dotaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 5154 inoreflst, if_deps); 5155 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 5156 KASSERT(dotaddref->ja_parent == ip->i_number, 5157 ("softdep_revert_mkdir: dot addref parent mismatch")); 5158 cancel_jaddref(dotaddref, inodedep, &inodedep->id_inowait); 5159 } 5160 FREE_LOCK(ITOUMP(dp)); 5161 } 5162 5163 /* 5164 * Called to correct nlinkdelta after a failed rmdir. 5165 */ 5166 void 5167 softdep_revert_rmdir(struct inode *dp, struct inode *ip) 5168 { 5169 5170 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0, 5171 ("softdep_revert_rmdir called on non-softdep filesystem")); 5172 ACQUIRE_LOCK(ITOUMP(dp)); 5173 (void) inodedep_lookup_ip(ip); 5174 (void) inodedep_lookup_ip(dp); 5175 FREE_LOCK(ITOUMP(dp)); 5176 } 5177 5178 /* 5179 * Protecting the freemaps (or bitmaps). 5180 * 5181 * To eliminate the need to execute fsck before mounting a filesystem 5182 * after a power failure, one must (conservatively) guarantee that the 5183 * on-disk copy of the bitmaps never indicate that a live inode or block is 5184 * free. So, when a block or inode is allocated, the bitmap should be 5185 * updated (on disk) before any new pointers. When a block or inode is 5186 * freed, the bitmap should not be updated until all pointers have been 5187 * reset. The latter dependency is handled by the delayed de-allocation 5188 * approach described below for block and inode de-allocation. The former 5189 * dependency is handled by calling the following procedure when a block or 5190 * inode is allocated. When an inode is allocated an "inodedep" is created 5191 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk. 5192 * Each "inodedep" is also inserted into the hash indexing structure so 5193 * that any additional link additions can be made dependent on the inode 5194 * allocation. 5195 * 5196 * The ufs filesystem maintains a number of free block counts (e.g., per 5197 * cylinder group, per cylinder and per <cylinder, rotational position> pair) 5198 * in addition to the bitmaps. These counts are used to improve efficiency 5199 * during allocation and therefore must be consistent with the bitmaps. 5200 * There is no convenient way to guarantee post-crash consistency of these 5201 * counts with simple update ordering, for two main reasons: (1) The counts 5202 * and bitmaps for a single cylinder group block are not in the same disk 5203 * sector. If a disk write is interrupted (e.g., by power failure), one may 5204 * be written and the other not. (2) Some of the counts are located in the 5205 * superblock rather than the cylinder group block. So, we focus our soft 5206 * updates implementation on protecting the bitmaps. When mounting a 5207 * filesystem, we recompute the auxiliary counts from the bitmaps. 5208 */ 5209 5210 /* 5211 * Called just after updating the cylinder group block to allocate an inode. 5212 */ 5213 void 5214 softdep_setup_inomapdep( 5215 struct buf *bp, /* buffer for cylgroup block with inode map */ 5216 struct inode *ip, /* inode related to allocation */ 5217 ino_t newinum, /* new inode number being allocated */ 5218 int mode) 5219 { 5220 struct inodedep *inodedep; 5221 struct bmsafemap *bmsafemap; 5222 struct jaddref *jaddref; 5223 struct mount *mp; 5224 struct fs *fs; 5225 5226 mp = ITOVFS(ip); 5227 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5228 ("softdep_setup_inomapdep called on non-softdep filesystem")); 5229 fs = VFSTOUFS(mp)->um_fs; 5230 jaddref = NULL; 5231 5232 /* 5233 * Allocate the journal reference add structure so that the bitmap 5234 * can be dependent on it. 5235 */ 5236 if (MOUNTEDSUJ(mp)) { 5237 jaddref = newjaddref(ip, newinum, 0, 0, mode); 5238 jaddref->ja_state |= NEWBLOCK; 5239 } 5240 5241 /* 5242 * Create a dependency for the newly allocated inode. 5243 * Panic if it already exists as something is seriously wrong. 5244 * Otherwise add it to the dependency list for the buffer holding 5245 * the cylinder group map from which it was allocated. 5246 * 5247 * We have to preallocate a bmsafemap entry in case it is needed 5248 * in bmsafemap_lookup since once we allocate the inodedep, we 5249 * have to finish initializing it before we can FREE_LOCK(). 5250 * By preallocating, we avoid FREE_LOCK() while doing a malloc 5251 * in bmsafemap_lookup. We cannot call bmsafemap_lookup before 5252 * creating the inodedep as it can be freed during the time 5253 * that we FREE_LOCK() while allocating the inodedep. We must 5254 * call workitem_alloc() before entering the locked section as 5255 * it also acquires the lock and we must avoid trying doing so 5256 * recursively. 5257 */ 5258 bmsafemap = malloc(sizeof(struct bmsafemap), 5259 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5260 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5261 ACQUIRE_LOCK(ITOUMP(ip)); 5262 if ((inodedep_lookup(mp, newinum, DEPALLOC, &inodedep))) 5263 panic("softdep_setup_inomapdep: dependency %p for new" 5264 "inode already exists", inodedep); 5265 bmsafemap = bmsafemap_lookup(mp, bp, ino_to_cg(fs, newinum), bmsafemap); 5266 if (jaddref) { 5267 LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps); 5268 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 5269 if_deps); 5270 } else { 5271 inodedep->id_state |= ONDEPLIST; 5272 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps); 5273 } 5274 inodedep->id_bmsafemap = bmsafemap; 5275 inodedep->id_state &= ~DEPCOMPLETE; 5276 FREE_LOCK(ITOUMP(ip)); 5277 } 5278 5279 /* 5280 * Called just after updating the cylinder group block to 5281 * allocate block or fragment. 5282 */ 5283 void 5284 softdep_setup_blkmapdep( 5285 struct buf *bp, /* buffer for cylgroup block with block map */ 5286 struct mount *mp, /* filesystem doing allocation */ 5287 ufs2_daddr_t newblkno, /* number of newly allocated block */ 5288 int frags, /* Number of fragments. */ 5289 int oldfrags) /* Previous number of fragments for extend. */ 5290 { 5291 struct newblk *newblk; 5292 struct bmsafemap *bmsafemap; 5293 struct jnewblk *jnewblk; 5294 struct ufsmount *ump; 5295 struct fs *fs; 5296 5297 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5298 ("softdep_setup_blkmapdep called on non-softdep filesystem")); 5299 ump = VFSTOUFS(mp); 5300 fs = ump->um_fs; 5301 jnewblk = NULL; 5302 /* 5303 * Create a dependency for the newly allocated block. 5304 * Add it to the dependency list for the buffer holding 5305 * the cylinder group map from which it was allocated. 5306 */ 5307 if (MOUNTEDSUJ(mp)) { 5308 jnewblk = malloc(sizeof(*jnewblk), M_JNEWBLK, M_SOFTDEP_FLAGS); 5309 workitem_alloc(&jnewblk->jn_list, D_JNEWBLK, mp); 5310 jnewblk->jn_jsegdep = newjsegdep(&jnewblk->jn_list); 5311 jnewblk->jn_state = ATTACHED; 5312 jnewblk->jn_blkno = newblkno; 5313 jnewblk->jn_frags = frags; 5314 jnewblk->jn_oldfrags = oldfrags; 5315 #ifdef INVARIANTS 5316 { 5317 struct cg *cgp; 5318 uint8_t *blksfree; 5319 long bno; 5320 int i; 5321 5322 cgp = (struct cg *)bp->b_data; 5323 blksfree = cg_blksfree(cgp); 5324 bno = dtogd(fs, jnewblk->jn_blkno); 5325 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; 5326 i++) { 5327 if (isset(blksfree, bno + i)) 5328 panic("softdep_setup_blkmapdep: " 5329 "free fragment %d from %d-%d " 5330 "state 0x%X dep %p", i, 5331 jnewblk->jn_oldfrags, 5332 jnewblk->jn_frags, 5333 jnewblk->jn_state, 5334 jnewblk->jn_dep); 5335 } 5336 } 5337 #endif 5338 } 5339 5340 CTR3(KTR_SUJ, 5341 "softdep_setup_blkmapdep: blkno %jd frags %d oldfrags %d", 5342 newblkno, frags, oldfrags); 5343 ACQUIRE_LOCK(ump); 5344 if (newblk_lookup(mp, newblkno, DEPALLOC, &newblk) != 0) 5345 panic("softdep_setup_blkmapdep: found block"); 5346 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp, 5347 dtog(fs, newblkno), NULL); 5348 if (jnewblk) { 5349 jnewblk->jn_dep = (struct worklist *)newblk; 5350 LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps); 5351 } else { 5352 newblk->nb_state |= ONDEPLIST; 5353 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps); 5354 } 5355 newblk->nb_bmsafemap = bmsafemap; 5356 newblk->nb_jnewblk = jnewblk; 5357 FREE_LOCK(ump); 5358 } 5359 5360 #define BMSAFEMAP_HASH(ump, cg) \ 5361 (&(ump)->bmsafemap_hashtbl[(cg) & (ump)->bmsafemap_hash_size]) 5362 5363 static int 5364 bmsafemap_find( 5365 struct bmsafemap_hashhead *bmsafemaphd, 5366 int cg, 5367 struct bmsafemap **bmsafemapp) 5368 { 5369 struct bmsafemap *bmsafemap; 5370 5371 LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash) 5372 if (bmsafemap->sm_cg == cg) 5373 break; 5374 if (bmsafemap) { 5375 *bmsafemapp = bmsafemap; 5376 return (1); 5377 } 5378 *bmsafemapp = NULL; 5379 5380 return (0); 5381 } 5382 5383 /* 5384 * Find the bmsafemap associated with a cylinder group buffer. 5385 * If none exists, create one. The buffer must be locked when 5386 * this routine is called and this routine must be called with 5387 * the softdep lock held. To avoid giving up the lock while 5388 * allocating a new bmsafemap, a preallocated bmsafemap may be 5389 * provided. If it is provided but not needed, it is freed. 5390 */ 5391 static struct bmsafemap * 5392 bmsafemap_lookup(struct mount *mp, 5393 struct buf *bp, 5394 int cg, 5395 struct bmsafemap *newbmsafemap) 5396 { 5397 struct bmsafemap_hashhead *bmsafemaphd; 5398 struct bmsafemap *bmsafemap, *collision; 5399 struct worklist *wk; 5400 struct ufsmount *ump; 5401 5402 ump = VFSTOUFS(mp); 5403 LOCK_OWNED(ump); 5404 KASSERT(bp != NULL, ("bmsafemap_lookup: missing buffer")); 5405 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 5406 if (wk->wk_type == D_BMSAFEMAP) { 5407 if (newbmsafemap) 5408 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5409 return (WK_BMSAFEMAP(wk)); 5410 } 5411 } 5412 bmsafemaphd = BMSAFEMAP_HASH(ump, cg); 5413 if (bmsafemap_find(bmsafemaphd, cg, &bmsafemap) == 1) { 5414 if (newbmsafemap) 5415 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5416 return (bmsafemap); 5417 } 5418 if (newbmsafemap) { 5419 bmsafemap = newbmsafemap; 5420 } else { 5421 FREE_LOCK(ump); 5422 bmsafemap = malloc(sizeof(struct bmsafemap), 5423 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5424 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5425 ACQUIRE_LOCK(ump); 5426 } 5427 bmsafemap->sm_buf = bp; 5428 LIST_INIT(&bmsafemap->sm_inodedephd); 5429 LIST_INIT(&bmsafemap->sm_inodedepwr); 5430 LIST_INIT(&bmsafemap->sm_newblkhd); 5431 LIST_INIT(&bmsafemap->sm_newblkwr); 5432 LIST_INIT(&bmsafemap->sm_jaddrefhd); 5433 LIST_INIT(&bmsafemap->sm_jnewblkhd); 5434 LIST_INIT(&bmsafemap->sm_freehd); 5435 LIST_INIT(&bmsafemap->sm_freewr); 5436 if (bmsafemap_find(bmsafemaphd, cg, &collision) == 1) { 5437 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 5438 return (collision); 5439 } 5440 bmsafemap->sm_cg = cg; 5441 LIST_INSERT_HEAD(bmsafemaphd, bmsafemap, sm_hash); 5442 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 5443 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list); 5444 return (bmsafemap); 5445 } 5446 5447 /* 5448 * Direct block allocation dependencies. 5449 * 5450 * When a new block is allocated, the corresponding disk locations must be 5451 * initialized (with zeros or new data) before the on-disk inode points to 5452 * them. Also, the freemap from which the block was allocated must be 5453 * updated (on disk) before the inode's pointer. These two dependencies are 5454 * independent of each other and are needed for all file blocks and indirect 5455 * blocks that are pointed to directly by the inode. Just before the 5456 * "in-core" version of the inode is updated with a newly allocated block 5457 * number, a procedure (below) is called to setup allocation dependency 5458 * structures. These structures are removed when the corresponding 5459 * dependencies are satisfied or when the block allocation becomes obsolete 5460 * (i.e., the file is deleted, the block is de-allocated, or the block is a 5461 * fragment that gets upgraded). All of these cases are handled in 5462 * procedures described later. 5463 * 5464 * When a file extension causes a fragment to be upgraded, either to a larger 5465 * fragment or to a full block, the on-disk location may change (if the 5466 * previous fragment could not simply be extended). In this case, the old 5467 * fragment must be de-allocated, but not until after the inode's pointer has 5468 * been updated. In most cases, this is handled by later procedures, which 5469 * will construct a "freefrag" structure to be added to the workitem queue 5470 * when the inode update is complete (or obsolete). The main exception to 5471 * this is when an allocation occurs while a pending allocation dependency 5472 * (for the same block pointer) remains. This case is handled in the main 5473 * allocation dependency setup procedure by immediately freeing the 5474 * unreferenced fragments. 5475 */ 5476 void 5477 softdep_setup_allocdirect( 5478 struct inode *ip, /* inode to which block is being added */ 5479 ufs_lbn_t off, /* block pointer within inode */ 5480 ufs2_daddr_t newblkno, /* disk block number being added */ 5481 ufs2_daddr_t oldblkno, /* previous block number, 0 unless frag */ 5482 long newsize, /* size of new block */ 5483 long oldsize, /* size of new block */ 5484 struct buf *bp) /* bp for allocated block */ 5485 { 5486 struct allocdirect *adp, *oldadp; 5487 struct allocdirectlst *adphead; 5488 struct freefrag *freefrag; 5489 struct inodedep *inodedep; 5490 struct pagedep *pagedep; 5491 struct jnewblk *jnewblk; 5492 struct newblk *newblk; 5493 struct mount *mp; 5494 ufs_lbn_t lbn; 5495 5496 lbn = bp->b_lblkno; 5497 mp = ITOVFS(ip); 5498 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5499 ("softdep_setup_allocdirect called on non-softdep filesystem")); 5500 if (oldblkno && oldblkno != newblkno) 5501 /* 5502 * The usual case is that a smaller fragment that 5503 * was just allocated has been replaced with a bigger 5504 * fragment or a full-size block. If it is marked as 5505 * B_DELWRI, the current contents have not been written 5506 * to disk. It is possible that the block was written 5507 * earlier, but very uncommon. If the block has never 5508 * been written, there is no need to send a BIO_DELETE 5509 * for it when it is freed. The gain from avoiding the 5510 * TRIMs for the common case of unwritten blocks far 5511 * exceeds the cost of the write amplification for the 5512 * uncommon case of failing to send a TRIM for a block 5513 * that had been written. 5514 */ 5515 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn, 5516 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY); 5517 else 5518 freefrag = NULL; 5519 5520 CTR6(KTR_SUJ, 5521 "softdep_setup_allocdirect: ino %d blkno %jd oldblkno %jd " 5522 "off %jd newsize %ld oldsize %d", 5523 ip->i_number, newblkno, oldblkno, off, newsize, oldsize); 5524 ACQUIRE_LOCK(ITOUMP(ip)); 5525 if (off >= UFS_NDADDR) { 5526 if (lbn > 0) 5527 panic("softdep_setup_allocdirect: bad lbn %jd, off %jd", 5528 lbn, off); 5529 /* allocating an indirect block */ 5530 if (oldblkno != 0) 5531 panic("softdep_setup_allocdirect: non-zero indir"); 5532 } else { 5533 if (off != lbn) 5534 panic("softdep_setup_allocdirect: lbn %jd != off %jd", 5535 lbn, off); 5536 /* 5537 * Allocating a direct block. 5538 * 5539 * If we are allocating a directory block, then we must 5540 * allocate an associated pagedep to track additions and 5541 * deletions. 5542 */ 5543 if ((ip->i_mode & IFMT) == IFDIR) 5544 pagedep_lookup(mp, bp, ip->i_number, off, DEPALLOC, 5545 &pagedep); 5546 } 5547 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5548 panic("softdep_setup_allocdirect: lost block"); 5549 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5550 ("softdep_setup_allocdirect: newblk already initialized")); 5551 /* 5552 * Convert the newblk to an allocdirect. 5553 */ 5554 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT); 5555 adp = (struct allocdirect *)newblk; 5556 newblk->nb_freefrag = freefrag; 5557 adp->ad_offset = off; 5558 adp->ad_oldblkno = oldblkno; 5559 adp->ad_newsize = newsize; 5560 adp->ad_oldsize = oldsize; 5561 5562 /* 5563 * Finish initializing the journal. 5564 */ 5565 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5566 jnewblk->jn_ino = ip->i_number; 5567 jnewblk->jn_lbn = lbn; 5568 add_to_journal(&jnewblk->jn_list); 5569 } 5570 if (freefrag && freefrag->ff_jdep != NULL && 5571 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5572 add_to_journal(freefrag->ff_jdep); 5573 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 5574 adp->ad_inodedep = inodedep; 5575 5576 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5577 /* 5578 * The list of allocdirects must be kept in sorted and ascending 5579 * order so that the rollback routines can quickly determine the 5580 * first uncommitted block (the size of the file stored on disk 5581 * ends at the end of the lowest committed fragment, or if there 5582 * are no fragments, at the end of the highest committed block). 5583 * Since files generally grow, the typical case is that the new 5584 * block is to be added at the end of the list. We speed this 5585 * special case by checking against the last allocdirect in the 5586 * list before laboriously traversing the list looking for the 5587 * insertion point. 5588 */ 5589 adphead = &inodedep->id_newinoupdt; 5590 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5591 if (oldadp == NULL || oldadp->ad_offset <= off) { 5592 /* insert at end of list */ 5593 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5594 if (oldadp != NULL && oldadp->ad_offset == off) 5595 allocdirect_merge(adphead, adp, oldadp); 5596 FREE_LOCK(ITOUMP(ip)); 5597 return; 5598 } 5599 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5600 if (oldadp->ad_offset >= off) 5601 break; 5602 } 5603 if (oldadp == NULL) 5604 panic("softdep_setup_allocdirect: lost entry"); 5605 /* insert in middle of list */ 5606 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5607 if (oldadp->ad_offset == off) 5608 allocdirect_merge(adphead, adp, oldadp); 5609 5610 FREE_LOCK(ITOUMP(ip)); 5611 } 5612 5613 /* 5614 * Merge a newer and older journal record to be stored either in a 5615 * newblock or freefrag. This handles aggregating journal records for 5616 * fragment allocation into a second record as well as replacing a 5617 * journal free with an aborted journal allocation. A segment for the 5618 * oldest record will be placed on wkhd if it has been written. If not 5619 * the segment for the newer record will suffice. 5620 */ 5621 static struct worklist * 5622 jnewblk_merge(struct worklist *new, 5623 struct worklist *old, 5624 struct workhead *wkhd) 5625 { 5626 struct jnewblk *njnewblk; 5627 struct jnewblk *jnewblk; 5628 5629 /* Handle NULLs to simplify callers. */ 5630 if (new == NULL) 5631 return (old); 5632 if (old == NULL) 5633 return (new); 5634 /* Replace a jfreefrag with a jnewblk. */ 5635 if (new->wk_type == D_JFREEFRAG) { 5636 if (WK_JNEWBLK(old)->jn_blkno != WK_JFREEFRAG(new)->fr_blkno) 5637 panic("jnewblk_merge: blkno mismatch: %p, %p", 5638 old, new); 5639 cancel_jfreefrag(WK_JFREEFRAG(new)); 5640 return (old); 5641 } 5642 if (old->wk_type != D_JNEWBLK || new->wk_type != D_JNEWBLK) 5643 panic("jnewblk_merge: Bad type: old %d new %d\n", 5644 old->wk_type, new->wk_type); 5645 /* 5646 * Handle merging of two jnewblk records that describe 5647 * different sets of fragments in the same block. 5648 */ 5649 jnewblk = WK_JNEWBLK(old); 5650 njnewblk = WK_JNEWBLK(new); 5651 if (jnewblk->jn_blkno != njnewblk->jn_blkno) 5652 panic("jnewblk_merge: Merging disparate blocks."); 5653 /* 5654 * The record may be rolled back in the cg. 5655 */ 5656 if (jnewblk->jn_state & UNDONE) { 5657 jnewblk->jn_state &= ~UNDONE; 5658 njnewblk->jn_state |= UNDONE; 5659 njnewblk->jn_state &= ~ATTACHED; 5660 } 5661 /* 5662 * We modify the newer addref and free the older so that if neither 5663 * has been written the most up-to-date copy will be on disk. If 5664 * both have been written but rolled back we only temporarily need 5665 * one of them to fix the bits when the cg write completes. 5666 */ 5667 jnewblk->jn_state |= ATTACHED | COMPLETE; 5668 njnewblk->jn_oldfrags = jnewblk->jn_oldfrags; 5669 cancel_jnewblk(jnewblk, wkhd); 5670 WORKLIST_REMOVE(&jnewblk->jn_list); 5671 free_jnewblk(jnewblk); 5672 return (new); 5673 } 5674 5675 /* 5676 * Replace an old allocdirect dependency with a newer one. 5677 */ 5678 static void 5679 allocdirect_merge( 5680 struct allocdirectlst *adphead, /* head of list holding allocdirects */ 5681 struct allocdirect *newadp, /* allocdirect being added */ 5682 struct allocdirect *oldadp) /* existing allocdirect being checked */ 5683 { 5684 struct worklist *wk; 5685 struct freefrag *freefrag; 5686 5687 freefrag = NULL; 5688 LOCK_OWNED(VFSTOUFS(newadp->ad_list.wk_mp)); 5689 if (newadp->ad_oldblkno != oldadp->ad_newblkno || 5690 newadp->ad_oldsize != oldadp->ad_newsize || 5691 newadp->ad_offset >= UFS_NDADDR) 5692 panic("%s %jd != new %jd || old size %ld != new %ld", 5693 "allocdirect_merge: old blkno", 5694 (intmax_t)newadp->ad_oldblkno, 5695 (intmax_t)oldadp->ad_newblkno, 5696 newadp->ad_oldsize, oldadp->ad_newsize); 5697 newadp->ad_oldblkno = oldadp->ad_oldblkno; 5698 newadp->ad_oldsize = oldadp->ad_oldsize; 5699 /* 5700 * If the old dependency had a fragment to free or had never 5701 * previously had a block allocated, then the new dependency 5702 * can immediately post its freefrag and adopt the old freefrag. 5703 * This action is done by swapping the freefrag dependencies. 5704 * The new dependency gains the old one's freefrag, and the 5705 * old one gets the new one and then immediately puts it on 5706 * the worklist when it is freed by free_newblk. It is 5707 * not possible to do this swap when the old dependency had a 5708 * non-zero size but no previous fragment to free. This condition 5709 * arises when the new block is an extension of the old block. 5710 * Here, the first part of the fragment allocated to the new 5711 * dependency is part of the block currently claimed on disk by 5712 * the old dependency, so cannot legitimately be freed until the 5713 * conditions for the new dependency are fulfilled. 5714 */ 5715 freefrag = newadp->ad_freefrag; 5716 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) { 5717 newadp->ad_freefrag = oldadp->ad_freefrag; 5718 oldadp->ad_freefrag = freefrag; 5719 } 5720 /* 5721 * If we are tracking a new directory-block allocation, 5722 * move it from the old allocdirect to the new allocdirect. 5723 */ 5724 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) { 5725 WORKLIST_REMOVE(wk); 5726 if (!LIST_EMPTY(&oldadp->ad_newdirblk)) 5727 panic("allocdirect_merge: extra newdirblk"); 5728 WORKLIST_INSERT(&newadp->ad_newdirblk, wk); 5729 } 5730 TAILQ_REMOVE(adphead, oldadp, ad_next); 5731 /* 5732 * We need to move any journal dependencies over to the freefrag 5733 * that releases this block if it exists. Otherwise we are 5734 * extending an existing block and we'll wait until that is 5735 * complete to release the journal space and extend the 5736 * new journal to cover this old space as well. 5737 */ 5738 if (freefrag == NULL) { 5739 if (oldadp->ad_newblkno != newadp->ad_newblkno) 5740 panic("allocdirect_merge: %jd != %jd", 5741 oldadp->ad_newblkno, newadp->ad_newblkno); 5742 newadp->ad_block.nb_jnewblk = (struct jnewblk *) 5743 jnewblk_merge(&newadp->ad_block.nb_jnewblk->jn_list, 5744 &oldadp->ad_block.nb_jnewblk->jn_list, 5745 &newadp->ad_block.nb_jwork); 5746 oldadp->ad_block.nb_jnewblk = NULL; 5747 cancel_newblk(&oldadp->ad_block, NULL, 5748 &newadp->ad_block.nb_jwork); 5749 } else { 5750 wk = (struct worklist *) cancel_newblk(&oldadp->ad_block, 5751 &freefrag->ff_list, &freefrag->ff_jwork); 5752 freefrag->ff_jdep = jnewblk_merge(freefrag->ff_jdep, wk, 5753 &freefrag->ff_jwork); 5754 } 5755 free_newblk(&oldadp->ad_block); 5756 } 5757 5758 /* 5759 * Allocate a jfreefrag structure to journal a single block free. 5760 */ 5761 static struct jfreefrag * 5762 newjfreefrag(struct freefrag *freefrag, 5763 struct inode *ip, 5764 ufs2_daddr_t blkno, 5765 long size, 5766 ufs_lbn_t lbn) 5767 { 5768 struct jfreefrag *jfreefrag; 5769 struct fs *fs; 5770 5771 fs = ITOFS(ip); 5772 jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG, 5773 M_SOFTDEP_FLAGS); 5774 workitem_alloc(&jfreefrag->fr_list, D_JFREEFRAG, ITOVFS(ip)); 5775 jfreefrag->fr_jsegdep = newjsegdep(&jfreefrag->fr_list); 5776 jfreefrag->fr_state = ATTACHED | DEPCOMPLETE; 5777 jfreefrag->fr_ino = ip->i_number; 5778 jfreefrag->fr_lbn = lbn; 5779 jfreefrag->fr_blkno = blkno; 5780 jfreefrag->fr_frags = numfrags(fs, size); 5781 jfreefrag->fr_freefrag = freefrag; 5782 5783 return (jfreefrag); 5784 } 5785 5786 /* 5787 * Allocate a new freefrag structure. 5788 */ 5789 static struct freefrag * 5790 newfreefrag(struct inode *ip, 5791 ufs2_daddr_t blkno, 5792 long size, 5793 ufs_lbn_t lbn, 5794 uint64_t key) 5795 { 5796 struct freefrag *freefrag; 5797 struct ufsmount *ump; 5798 struct fs *fs; 5799 5800 CTR4(KTR_SUJ, "newfreefrag: ino %d blkno %jd size %ld lbn %jd", 5801 ip->i_number, blkno, size, lbn); 5802 ump = ITOUMP(ip); 5803 fs = ump->um_fs; 5804 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag) 5805 panic("newfreefrag: frag size"); 5806 freefrag = malloc(sizeof(struct freefrag), 5807 M_FREEFRAG, M_SOFTDEP_FLAGS); 5808 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ump)); 5809 freefrag->ff_state = ATTACHED; 5810 LIST_INIT(&freefrag->ff_jwork); 5811 freefrag->ff_inum = ip->i_number; 5812 freefrag->ff_vtype = ITOV(ip)->v_type; 5813 freefrag->ff_blkno = blkno; 5814 freefrag->ff_fragsize = size; 5815 freefrag->ff_key = key; 5816 5817 if (MOUNTEDSUJ(UFSTOVFS(ump))) { 5818 freefrag->ff_jdep = (struct worklist *) 5819 newjfreefrag(freefrag, ip, blkno, size, lbn); 5820 } else { 5821 freefrag->ff_state |= DEPCOMPLETE; 5822 freefrag->ff_jdep = NULL; 5823 } 5824 5825 return (freefrag); 5826 } 5827 5828 /* 5829 * This workitem de-allocates fragments that were replaced during 5830 * file block allocation. 5831 */ 5832 static void 5833 handle_workitem_freefrag(struct freefrag *freefrag) 5834 { 5835 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp); 5836 struct workhead wkhd; 5837 5838 CTR3(KTR_SUJ, 5839 "handle_workitem_freefrag: ino %d blkno %jd size %ld", 5840 freefrag->ff_inum, freefrag->ff_blkno, freefrag->ff_fragsize); 5841 /* 5842 * It would be illegal to add new completion items to the 5843 * freefrag after it was schedule to be done so it must be 5844 * safe to modify the list head here. 5845 */ 5846 LIST_INIT(&wkhd); 5847 ACQUIRE_LOCK(ump); 5848 LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list); 5849 /* 5850 * If the journal has not been written we must cancel it here. 5851 */ 5852 if (freefrag->ff_jdep) { 5853 if (freefrag->ff_jdep->wk_type != D_JNEWBLK) 5854 panic("handle_workitem_freefrag: Unexpected type %d\n", 5855 freefrag->ff_jdep->wk_type); 5856 cancel_jnewblk(WK_JNEWBLK(freefrag->ff_jdep), &wkhd); 5857 } 5858 FREE_LOCK(ump); 5859 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno, 5860 freefrag->ff_fragsize, freefrag->ff_inum, freefrag->ff_vtype, 5861 &wkhd, freefrag->ff_key); 5862 ACQUIRE_LOCK(ump); 5863 WORKITEM_FREE(freefrag, D_FREEFRAG); 5864 FREE_LOCK(ump); 5865 } 5866 5867 /* 5868 * Set up a dependency structure for an external attributes data block. 5869 * This routine follows much of the structure of softdep_setup_allocdirect. 5870 * See the description of softdep_setup_allocdirect above for details. 5871 */ 5872 void 5873 softdep_setup_allocext( 5874 struct inode *ip, 5875 ufs_lbn_t off, 5876 ufs2_daddr_t newblkno, 5877 ufs2_daddr_t oldblkno, 5878 long newsize, 5879 long oldsize, 5880 struct buf *bp) 5881 { 5882 struct allocdirect *adp, *oldadp; 5883 struct allocdirectlst *adphead; 5884 struct freefrag *freefrag; 5885 struct inodedep *inodedep; 5886 struct jnewblk *jnewblk; 5887 struct newblk *newblk; 5888 struct mount *mp; 5889 struct ufsmount *ump; 5890 ufs_lbn_t lbn; 5891 5892 mp = ITOVFS(ip); 5893 ump = VFSTOUFS(mp); 5894 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5895 ("softdep_setup_allocext called on non-softdep filesystem")); 5896 KASSERT(off < UFS_NXADDR, 5897 ("softdep_setup_allocext: lbn %lld > UFS_NXADDR", (long long)off)); 5898 5899 lbn = bp->b_lblkno; 5900 if (oldblkno && oldblkno != newblkno) 5901 /* 5902 * The usual case is that a smaller fragment that 5903 * was just allocated has been replaced with a bigger 5904 * fragment or a full-size block. If it is marked as 5905 * B_DELWRI, the current contents have not been written 5906 * to disk. It is possible that the block was written 5907 * earlier, but very uncommon. If the block has never 5908 * been written, there is no need to send a BIO_DELETE 5909 * for it when it is freed. The gain from avoiding the 5910 * TRIMs for the common case of unwritten blocks far 5911 * exceeds the cost of the write amplification for the 5912 * uncommon case of failing to send a TRIM for a block 5913 * that had been written. 5914 */ 5915 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn, 5916 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY); 5917 else 5918 freefrag = NULL; 5919 5920 ACQUIRE_LOCK(ump); 5921 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5922 panic("softdep_setup_allocext: lost block"); 5923 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5924 ("softdep_setup_allocext: newblk already initialized")); 5925 /* 5926 * Convert the newblk to an allocdirect. 5927 */ 5928 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT); 5929 adp = (struct allocdirect *)newblk; 5930 newblk->nb_freefrag = freefrag; 5931 adp->ad_offset = off; 5932 adp->ad_oldblkno = oldblkno; 5933 adp->ad_newsize = newsize; 5934 adp->ad_oldsize = oldsize; 5935 adp->ad_state |= EXTDATA; 5936 5937 /* 5938 * Finish initializing the journal. 5939 */ 5940 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5941 jnewblk->jn_ino = ip->i_number; 5942 jnewblk->jn_lbn = lbn; 5943 add_to_journal(&jnewblk->jn_list); 5944 } 5945 if (freefrag && freefrag->ff_jdep != NULL && 5946 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5947 add_to_journal(freefrag->ff_jdep); 5948 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 5949 adp->ad_inodedep = inodedep; 5950 5951 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5952 /* 5953 * The list of allocdirects must be kept in sorted and ascending 5954 * order so that the rollback routines can quickly determine the 5955 * first uncommitted block (the size of the file stored on disk 5956 * ends at the end of the lowest committed fragment, or if there 5957 * are no fragments, at the end of the highest committed block). 5958 * Since files generally grow, the typical case is that the new 5959 * block is to be added at the end of the list. We speed this 5960 * special case by checking against the last allocdirect in the 5961 * list before laboriously traversing the list looking for the 5962 * insertion point. 5963 */ 5964 adphead = &inodedep->id_newextupdt; 5965 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5966 if (oldadp == NULL || oldadp->ad_offset <= off) { 5967 /* insert at end of list */ 5968 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5969 if (oldadp != NULL && oldadp->ad_offset == off) 5970 allocdirect_merge(adphead, adp, oldadp); 5971 FREE_LOCK(ump); 5972 return; 5973 } 5974 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5975 if (oldadp->ad_offset >= off) 5976 break; 5977 } 5978 if (oldadp == NULL) 5979 panic("softdep_setup_allocext: lost entry"); 5980 /* insert in middle of list */ 5981 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5982 if (oldadp->ad_offset == off) 5983 allocdirect_merge(adphead, adp, oldadp); 5984 FREE_LOCK(ump); 5985 } 5986 5987 /* 5988 * Indirect block allocation dependencies. 5989 * 5990 * The same dependencies that exist for a direct block also exist when 5991 * a new block is allocated and pointed to by an entry in a block of 5992 * indirect pointers. The undo/redo states described above are also 5993 * used here. Because an indirect block contains many pointers that 5994 * may have dependencies, a second copy of the entire in-memory indirect 5995 * block is kept. The buffer cache copy is always completely up-to-date. 5996 * The second copy, which is used only as a source for disk writes, 5997 * contains only the safe pointers (i.e., those that have no remaining 5998 * update dependencies). The second copy is freed when all pointers 5999 * are safe. The cache is not allowed to replace indirect blocks with 6000 * pending update dependencies. If a buffer containing an indirect 6001 * block with dependencies is written, these routines will mark it 6002 * dirty again. It can only be successfully written once all the 6003 * dependencies are removed. The ffs_fsync routine in conjunction with 6004 * softdep_sync_metadata work together to get all the dependencies 6005 * removed so that a file can be successfully written to disk. Three 6006 * procedures are used when setting up indirect block pointer 6007 * dependencies. The division is necessary because of the organization 6008 * of the "balloc" routine and because of the distinction between file 6009 * pages and file metadata blocks. 6010 */ 6011 6012 /* 6013 * Allocate a new allocindir structure. 6014 */ 6015 static struct allocindir * 6016 newallocindir( 6017 struct inode *ip, /* inode for file being extended */ 6018 int ptrno, /* offset of pointer in indirect block */ 6019 ufs2_daddr_t newblkno, /* disk block number being added */ 6020 ufs2_daddr_t oldblkno, /* previous block number, 0 if none */ 6021 ufs_lbn_t lbn) 6022 { 6023 struct newblk *newblk; 6024 struct allocindir *aip; 6025 struct freefrag *freefrag; 6026 struct jnewblk *jnewblk; 6027 6028 if (oldblkno) 6029 freefrag = newfreefrag(ip, oldblkno, ITOFS(ip)->fs_bsize, lbn, 6030 SINGLETON_KEY); 6031 else 6032 freefrag = NULL; 6033 ACQUIRE_LOCK(ITOUMP(ip)); 6034 if (newblk_lookup(ITOVFS(ip), newblkno, 0, &newblk) == 0) 6035 panic("new_allocindir: lost block"); 6036 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 6037 ("newallocindir: newblk already initialized")); 6038 WORKITEM_REASSIGN(newblk, D_ALLOCINDIR); 6039 newblk->nb_freefrag = freefrag; 6040 aip = (struct allocindir *)newblk; 6041 aip->ai_offset = ptrno; 6042 aip->ai_oldblkno = oldblkno; 6043 aip->ai_lbn = lbn; 6044 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 6045 jnewblk->jn_ino = ip->i_number; 6046 jnewblk->jn_lbn = lbn; 6047 add_to_journal(&jnewblk->jn_list); 6048 } 6049 if (freefrag && freefrag->ff_jdep != NULL && 6050 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 6051 add_to_journal(freefrag->ff_jdep); 6052 return (aip); 6053 } 6054 6055 /* 6056 * Called just before setting an indirect block pointer 6057 * to a newly allocated file page. 6058 */ 6059 void 6060 softdep_setup_allocindir_page( 6061 struct inode *ip, /* inode for file being extended */ 6062 ufs_lbn_t lbn, /* allocated block number within file */ 6063 struct buf *bp, /* buffer with indirect blk referencing page */ 6064 int ptrno, /* offset of pointer in indirect block */ 6065 ufs2_daddr_t newblkno, /* disk block number being added */ 6066 ufs2_daddr_t oldblkno, /* previous block number, 0 if none */ 6067 struct buf *nbp) /* buffer holding allocated page */ 6068 { 6069 struct inodedep *inodedep; 6070 struct freefrag *freefrag; 6071 struct allocindir *aip; 6072 struct pagedep *pagedep; 6073 struct mount *mp; 6074 struct ufsmount *ump; 6075 6076 mp = ITOVFS(ip); 6077 ump = VFSTOUFS(mp); 6078 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6079 ("softdep_setup_allocindir_page called on non-softdep filesystem")); 6080 KASSERT(lbn == nbp->b_lblkno, 6081 ("softdep_setup_allocindir_page: lbn %jd != lblkno %jd", 6082 lbn, bp->b_lblkno)); 6083 CTR4(KTR_SUJ, 6084 "softdep_setup_allocindir_page: ino %d blkno %jd oldblkno %jd " 6085 "lbn %jd", ip->i_number, newblkno, oldblkno, lbn); 6086 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page"); 6087 aip = newallocindir(ip, ptrno, newblkno, oldblkno, lbn); 6088 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6089 /* 6090 * If we are allocating a directory page, then we must 6091 * allocate an associated pagedep to track additions and 6092 * deletions. 6093 */ 6094 if ((ip->i_mode & IFMT) == IFDIR) 6095 pagedep_lookup(mp, nbp, ip->i_number, lbn, DEPALLOC, &pagedep); 6096 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 6097 freefrag = setup_allocindir_phase2(bp, ip, inodedep, aip, lbn); 6098 FREE_LOCK(ump); 6099 if (freefrag) 6100 handle_workitem_freefrag(freefrag); 6101 } 6102 6103 /* 6104 * Called just before setting an indirect block pointer to a 6105 * newly allocated indirect block. 6106 */ 6107 void 6108 softdep_setup_allocindir_meta( 6109 struct buf *nbp, /* newly allocated indirect block */ 6110 struct inode *ip, /* inode for file being extended */ 6111 struct buf *bp, /* indirect block referencing allocated block */ 6112 int ptrno, /* offset of pointer in indirect block */ 6113 ufs2_daddr_t newblkno) /* disk block number being added */ 6114 { 6115 struct inodedep *inodedep; 6116 struct allocindir *aip; 6117 struct ufsmount *ump; 6118 ufs_lbn_t lbn; 6119 6120 ump = ITOUMP(ip); 6121 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 6122 ("softdep_setup_allocindir_meta called on non-softdep filesystem")); 6123 CTR3(KTR_SUJ, 6124 "softdep_setup_allocindir_meta: ino %d blkno %jd ptrno %d", 6125 ip->i_number, newblkno, ptrno); 6126 lbn = nbp->b_lblkno; 6127 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta"); 6128 aip = newallocindir(ip, ptrno, newblkno, 0, lbn); 6129 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep); 6130 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 6131 if (setup_allocindir_phase2(bp, ip, inodedep, aip, lbn)) 6132 panic("softdep_setup_allocindir_meta: Block already existed"); 6133 FREE_LOCK(ump); 6134 } 6135 6136 static void 6137 indirdep_complete(struct indirdep *indirdep) 6138 { 6139 struct allocindir *aip; 6140 6141 LIST_REMOVE(indirdep, ir_next); 6142 indirdep->ir_state |= DEPCOMPLETE; 6143 6144 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) { 6145 LIST_REMOVE(aip, ai_next); 6146 free_newblk(&aip->ai_block); 6147 } 6148 /* 6149 * If this indirdep is not attached to a buf it was simply waiting 6150 * on completion to clear completehd. free_indirdep() asserts 6151 * that nothing is dangling. 6152 */ 6153 if ((indirdep->ir_state & ONWORKLIST) == 0) 6154 free_indirdep(indirdep); 6155 } 6156 6157 static struct indirdep * 6158 indirdep_lookup(struct mount *mp, 6159 struct inode *ip, 6160 struct buf *bp) 6161 { 6162 struct indirdep *indirdep, *newindirdep; 6163 struct newblk *newblk; 6164 struct ufsmount *ump; 6165 struct worklist *wk; 6166 struct fs *fs; 6167 ufs2_daddr_t blkno; 6168 6169 ump = VFSTOUFS(mp); 6170 LOCK_OWNED(ump); 6171 indirdep = NULL; 6172 newindirdep = NULL; 6173 fs = ump->um_fs; 6174 for (;;) { 6175 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 6176 if (wk->wk_type != D_INDIRDEP) 6177 continue; 6178 indirdep = WK_INDIRDEP(wk); 6179 break; 6180 } 6181 /* Found on the buffer worklist, no new structure to free. */ 6182 if (indirdep != NULL && newindirdep == NULL) 6183 return (indirdep); 6184 if (indirdep != NULL && newindirdep != NULL) 6185 panic("indirdep_lookup: simultaneous create"); 6186 /* None found on the buffer and a new structure is ready. */ 6187 if (indirdep == NULL && newindirdep != NULL) 6188 break; 6189 /* None found and no new structure available. */ 6190 FREE_LOCK(ump); 6191 newindirdep = malloc(sizeof(struct indirdep), 6192 M_INDIRDEP, M_SOFTDEP_FLAGS); 6193 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, mp); 6194 newindirdep->ir_state = ATTACHED; 6195 if (I_IS_UFS1(ip)) 6196 newindirdep->ir_state |= UFS1FMT; 6197 TAILQ_INIT(&newindirdep->ir_trunc); 6198 newindirdep->ir_saveddata = NULL; 6199 LIST_INIT(&newindirdep->ir_deplisthd); 6200 LIST_INIT(&newindirdep->ir_donehd); 6201 LIST_INIT(&newindirdep->ir_writehd); 6202 LIST_INIT(&newindirdep->ir_completehd); 6203 if (bp->b_blkno == bp->b_lblkno) { 6204 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp, 6205 NULL, NULL); 6206 bp->b_blkno = blkno; 6207 } 6208 newindirdep->ir_freeblks = NULL; 6209 newindirdep->ir_savebp = 6210 getblk(ump->um_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0); 6211 newindirdep->ir_bp = bp; 6212 BUF_KERNPROC(newindirdep->ir_savebp); 6213 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount); 6214 ACQUIRE_LOCK(ump); 6215 } 6216 indirdep = newindirdep; 6217 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list); 6218 /* 6219 * If the block is not yet allocated we don't set DEPCOMPLETE so 6220 * that we don't free dependencies until the pointers are valid. 6221 * This could search b_dep for D_ALLOCDIRECT/D_ALLOCINDIR rather 6222 * than using the hash. 6223 */ 6224 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk)) 6225 LIST_INSERT_HEAD(&newblk->nb_indirdeps, indirdep, ir_next); 6226 else 6227 indirdep->ir_state |= DEPCOMPLETE; 6228 return (indirdep); 6229 } 6230 6231 /* 6232 * Called to finish the allocation of the "aip" allocated 6233 * by one of the two routines above. 6234 */ 6235 static struct freefrag * 6236 setup_allocindir_phase2( 6237 struct buf *bp, /* in-memory copy of the indirect block */ 6238 struct inode *ip, /* inode for file being extended */ 6239 struct inodedep *inodedep, /* Inodedep for ip */ 6240 struct allocindir *aip, /* allocindir allocated by the above routines */ 6241 ufs_lbn_t lbn) /* Logical block number for this block. */ 6242 { 6243 struct fs *fs __diagused; 6244 struct indirdep *indirdep; 6245 struct allocindir *oldaip; 6246 struct freefrag *freefrag; 6247 struct mount *mp; 6248 struct ufsmount *ump; 6249 6250 mp = ITOVFS(ip); 6251 ump = VFSTOUFS(mp); 6252 LOCK_OWNED(ump); 6253 fs = ump->um_fs; 6254 if (bp->b_lblkno >= 0) 6255 panic("setup_allocindir_phase2: not indir blk"); 6256 KASSERT(aip->ai_offset >= 0 && aip->ai_offset < NINDIR(fs), 6257 ("setup_allocindir_phase2: Bad offset %d", aip->ai_offset)); 6258 indirdep = indirdep_lookup(mp, ip, bp); 6259 KASSERT(indirdep->ir_savebp != NULL, 6260 ("setup_allocindir_phase2 NULL ir_savebp")); 6261 aip->ai_indirdep = indirdep; 6262 /* 6263 * Check for an unwritten dependency for this indirect offset. If 6264 * there is, merge the old dependency into the new one. This happens 6265 * as a result of reallocblk only. 6266 */ 6267 freefrag = NULL; 6268 if (aip->ai_oldblkno != 0) { 6269 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) { 6270 if (oldaip->ai_offset == aip->ai_offset) { 6271 freefrag = allocindir_merge(aip, oldaip); 6272 goto done; 6273 } 6274 } 6275 LIST_FOREACH(oldaip, &indirdep->ir_donehd, ai_next) { 6276 if (oldaip->ai_offset == aip->ai_offset) { 6277 freefrag = allocindir_merge(aip, oldaip); 6278 goto done; 6279 } 6280 } 6281 } 6282 done: 6283 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next); 6284 return (freefrag); 6285 } 6286 6287 /* 6288 * Merge two allocindirs which refer to the same block. Move newblock 6289 * dependencies and setup the freefrags appropriately. 6290 */ 6291 static struct freefrag * 6292 allocindir_merge( 6293 struct allocindir *aip, 6294 struct allocindir *oldaip) 6295 { 6296 struct freefrag *freefrag; 6297 struct worklist *wk; 6298 6299 if (oldaip->ai_newblkno != aip->ai_oldblkno) 6300 panic("allocindir_merge: blkno"); 6301 aip->ai_oldblkno = oldaip->ai_oldblkno; 6302 freefrag = aip->ai_freefrag; 6303 aip->ai_freefrag = oldaip->ai_freefrag; 6304 oldaip->ai_freefrag = NULL; 6305 KASSERT(freefrag != NULL, ("setup_allocindir_phase2: No freefrag")); 6306 /* 6307 * If we are tracking a new directory-block allocation, 6308 * move it from the old allocindir to the new allocindir. 6309 */ 6310 if ((wk = LIST_FIRST(&oldaip->ai_newdirblk)) != NULL) { 6311 WORKLIST_REMOVE(wk); 6312 if (!LIST_EMPTY(&oldaip->ai_newdirblk)) 6313 panic("allocindir_merge: extra newdirblk"); 6314 WORKLIST_INSERT(&aip->ai_newdirblk, wk); 6315 } 6316 /* 6317 * We can skip journaling for this freefrag and just complete 6318 * any pending journal work for the allocindir that is being 6319 * removed after the freefrag completes. 6320 */ 6321 if (freefrag->ff_jdep) 6322 cancel_jfreefrag(WK_JFREEFRAG(freefrag->ff_jdep)); 6323 LIST_REMOVE(oldaip, ai_next); 6324 freefrag->ff_jdep = (struct worklist *)cancel_newblk(&oldaip->ai_block, 6325 &freefrag->ff_list, &freefrag->ff_jwork); 6326 free_newblk(&oldaip->ai_block); 6327 6328 return (freefrag); 6329 } 6330 6331 static inline void 6332 setup_freedirect( 6333 struct freeblks *freeblks, 6334 struct inode *ip, 6335 int i, 6336 int needj) 6337 { 6338 struct ufsmount *ump; 6339 ufs2_daddr_t blkno; 6340 int frags; 6341 6342 blkno = DIP(ip, i_db[i]); 6343 if (blkno == 0) 6344 return; 6345 DIP_SET(ip, i_db[i], 0); 6346 ump = ITOUMP(ip); 6347 frags = sblksize(ump->um_fs, ip->i_size, i); 6348 frags = numfrags(ump->um_fs, frags); 6349 newfreework(ump, freeblks, NULL, i, blkno, frags, 0, needj); 6350 } 6351 6352 static inline void 6353 setup_freeext( 6354 struct freeblks *freeblks, 6355 struct inode *ip, 6356 int i, 6357 int needj) 6358 { 6359 struct ufsmount *ump; 6360 ufs2_daddr_t blkno; 6361 int frags; 6362 6363 blkno = ip->i_din2->di_extb[i]; 6364 if (blkno == 0) 6365 return; 6366 ip->i_din2->di_extb[i] = 0; 6367 ump = ITOUMP(ip); 6368 frags = sblksize(ump->um_fs, ip->i_din2->di_extsize, i); 6369 frags = numfrags(ump->um_fs, frags); 6370 newfreework(ump, freeblks, NULL, -1 - i, blkno, frags, 0, needj); 6371 } 6372 6373 static inline void 6374 setup_freeindir( 6375 struct freeblks *freeblks, 6376 struct inode *ip, 6377 int i, 6378 ufs_lbn_t lbn, 6379 int needj) 6380 { 6381 struct ufsmount *ump; 6382 ufs2_daddr_t blkno; 6383 6384 blkno = DIP(ip, i_ib[i]); 6385 if (blkno == 0) 6386 return; 6387 DIP_SET(ip, i_ib[i], 0); 6388 ump = ITOUMP(ip); 6389 newfreework(ump, freeblks, NULL, lbn, blkno, ump->um_fs->fs_frag, 6390 0, needj); 6391 } 6392 6393 static inline struct freeblks * 6394 newfreeblks(struct mount *mp, struct inode *ip) 6395 { 6396 struct freeblks *freeblks; 6397 6398 freeblks = malloc(sizeof(struct freeblks), 6399 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO); 6400 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp); 6401 LIST_INIT(&freeblks->fb_jblkdephd); 6402 LIST_INIT(&freeblks->fb_jwork); 6403 freeblks->fb_ref = 0; 6404 freeblks->fb_cgwait = 0; 6405 freeblks->fb_state = ATTACHED; 6406 freeblks->fb_uid = ip->i_uid; 6407 freeblks->fb_inum = ip->i_number; 6408 freeblks->fb_vtype = ITOV(ip)->v_type; 6409 freeblks->fb_modrev = DIP(ip, i_modrev); 6410 freeblks->fb_devvp = ITODEVVP(ip); 6411 freeblks->fb_chkcnt = 0; 6412 freeblks->fb_len = 0; 6413 6414 return (freeblks); 6415 } 6416 6417 static void 6418 trunc_indirdep( 6419 struct indirdep *indirdep, 6420 struct freeblks *freeblks, 6421 struct buf *bp, 6422 int off) 6423 { 6424 struct allocindir *aip, *aipn; 6425 6426 /* 6427 * The first set of allocindirs won't be in savedbp. 6428 */ 6429 LIST_FOREACH_SAFE(aip, &indirdep->ir_deplisthd, ai_next, aipn) 6430 if (aip->ai_offset > off) 6431 cancel_allocindir(aip, bp, freeblks, 1); 6432 LIST_FOREACH_SAFE(aip, &indirdep->ir_donehd, ai_next, aipn) 6433 if (aip->ai_offset > off) 6434 cancel_allocindir(aip, bp, freeblks, 1); 6435 /* 6436 * These will exist in savedbp. 6437 */ 6438 LIST_FOREACH_SAFE(aip, &indirdep->ir_writehd, ai_next, aipn) 6439 if (aip->ai_offset > off) 6440 cancel_allocindir(aip, NULL, freeblks, 0); 6441 LIST_FOREACH_SAFE(aip, &indirdep->ir_completehd, ai_next, aipn) 6442 if (aip->ai_offset > off) 6443 cancel_allocindir(aip, NULL, freeblks, 0); 6444 } 6445 6446 /* 6447 * Follow the chain of indirects down to lastlbn creating a freework 6448 * structure for each. This will be used to start indir_trunc() at 6449 * the right offset and create the journal records for the parrtial 6450 * truncation. A second step will handle the truncated dependencies. 6451 */ 6452 static int 6453 setup_trunc_indir( 6454 struct freeblks *freeblks, 6455 struct inode *ip, 6456 ufs_lbn_t lbn, 6457 ufs_lbn_t lastlbn, 6458 ufs2_daddr_t blkno) 6459 { 6460 struct indirdep *indirdep; 6461 struct indirdep *indirn; 6462 struct freework *freework; 6463 struct newblk *newblk; 6464 struct mount *mp; 6465 struct ufsmount *ump; 6466 struct buf *bp; 6467 uint8_t *start; 6468 uint8_t *end; 6469 ufs_lbn_t lbnadd; 6470 int level; 6471 int error; 6472 int off; 6473 6474 freework = NULL; 6475 if (blkno == 0) 6476 return (0); 6477 mp = freeblks->fb_list.wk_mp; 6478 ump = VFSTOUFS(mp); 6479 /* 6480 * Here, calls to VOP_BMAP() will fail. However, we already have 6481 * the on-disk address, so we just pass it to bread() instead of 6482 * having bread() attempt to calculate it using VOP_BMAP(). 6483 */ 6484 error = ffs_breadz(ump, ITOV(ip), lbn, blkptrtodb(ump, blkno), 6485 (int)mp->mnt_stat.f_iosize, NULL, NULL, 0, NOCRED, 0, NULL, &bp); 6486 if (error) 6487 return (error); 6488 level = lbn_level(lbn); 6489 lbnadd = lbn_offset(ump->um_fs, level); 6490 /* 6491 * Compute the offset of the last block we want to keep. Store 6492 * in the freework the first block we want to completely free. 6493 */ 6494 off = (lastlbn - -(lbn + level)) / lbnadd; 6495 if (off + 1 == NINDIR(ump->um_fs)) 6496 goto nowork; 6497 freework = newfreework(ump, freeblks, NULL, lbn, blkno, 0, off + 1, 0); 6498 /* 6499 * Link the freework into the indirdep. This will prevent any new 6500 * allocations from proceeding until we are finished with the 6501 * truncate and the block is written. 6502 */ 6503 ACQUIRE_LOCK(ump); 6504 indirdep = indirdep_lookup(mp, ip, bp); 6505 if (indirdep->ir_freeblks) 6506 panic("setup_trunc_indir: indirdep already truncated."); 6507 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, freework, fw_next); 6508 freework->fw_indir = indirdep; 6509 /* 6510 * Cancel any allocindirs that will not make it to disk. 6511 * We have to do this for all copies of the indirdep that 6512 * live on this newblk. 6513 */ 6514 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 6515 if (newblk_lookup(mp, dbtofsb(ump->um_fs, bp->b_blkno), 0, 6516 &newblk) == 0) 6517 panic("setup_trunc_indir: lost block"); 6518 LIST_FOREACH(indirn, &newblk->nb_indirdeps, ir_next) 6519 trunc_indirdep(indirn, freeblks, bp, off); 6520 } else 6521 trunc_indirdep(indirdep, freeblks, bp, off); 6522 FREE_LOCK(ump); 6523 /* 6524 * Creation is protected by the buf lock. The saveddata is only 6525 * needed if a full truncation follows a partial truncation but it 6526 * is difficult to allocate in that case so we fetch it anyway. 6527 */ 6528 if (indirdep->ir_saveddata == NULL) 6529 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 6530 M_SOFTDEP_FLAGS); 6531 nowork: 6532 /* Fetch the blkno of the child and the zero start offset. */ 6533 if (I_IS_UFS1(ip)) { 6534 blkno = ((ufs1_daddr_t *)bp->b_data)[off]; 6535 start = (uint8_t *)&((ufs1_daddr_t *)bp->b_data)[off+1]; 6536 } else { 6537 blkno = ((ufs2_daddr_t *)bp->b_data)[off]; 6538 start = (uint8_t *)&((ufs2_daddr_t *)bp->b_data)[off+1]; 6539 } 6540 if (freework) { 6541 /* Zero the truncated pointers. */ 6542 end = bp->b_data + bp->b_bcount; 6543 bzero(start, end - start); 6544 bdwrite(bp); 6545 } else 6546 bqrelse(bp); 6547 if (level == 0) 6548 return (0); 6549 lbn++; /* adjust level */ 6550 lbn -= (off * lbnadd); 6551 return setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno); 6552 } 6553 6554 /* 6555 * Complete the partial truncation of an indirect block setup by 6556 * setup_trunc_indir(). This zeros the truncated pointers in the saved 6557 * copy and writes them to disk before the freeblks is allowed to complete. 6558 */ 6559 static void 6560 complete_trunc_indir(struct freework *freework) 6561 { 6562 struct freework *fwn; 6563 struct indirdep *indirdep; 6564 struct ufsmount *ump; 6565 struct buf *bp; 6566 uintptr_t start; 6567 int count; 6568 6569 ump = VFSTOUFS(freework->fw_list.wk_mp); 6570 LOCK_OWNED(ump); 6571 indirdep = freework->fw_indir; 6572 for (;;) { 6573 bp = indirdep->ir_bp; 6574 /* See if the block was discarded. */ 6575 if (bp == NULL) 6576 break; 6577 /* Inline part of getdirtybuf(). We dont want bremfree. */ 6578 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) 6579 break; 6580 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 6581 LOCK_PTR(ump)) == 0) 6582 BUF_UNLOCK(bp); 6583 ACQUIRE_LOCK(ump); 6584 } 6585 freework->fw_state |= DEPCOMPLETE; 6586 TAILQ_REMOVE(&indirdep->ir_trunc, freework, fw_next); 6587 /* 6588 * Zero the pointers in the saved copy. 6589 */ 6590 if (indirdep->ir_state & UFS1FMT) 6591 start = sizeof(ufs1_daddr_t); 6592 else 6593 start = sizeof(ufs2_daddr_t); 6594 start *= freework->fw_start; 6595 count = indirdep->ir_savebp->b_bcount - start; 6596 start += (uintptr_t)indirdep->ir_savebp->b_data; 6597 bzero((char *)start, count); 6598 /* 6599 * We need to start the next truncation in the list if it has not 6600 * been started yet. 6601 */ 6602 fwn = TAILQ_FIRST(&indirdep->ir_trunc); 6603 if (fwn != NULL) { 6604 if (fwn->fw_freeblks == indirdep->ir_freeblks) 6605 TAILQ_REMOVE(&indirdep->ir_trunc, fwn, fw_next); 6606 if ((fwn->fw_state & ONWORKLIST) == 0) 6607 freework_enqueue(fwn); 6608 } 6609 /* 6610 * If bp is NULL the block was fully truncated, restore 6611 * the saved block list otherwise free it if it is no 6612 * longer needed. 6613 */ 6614 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 6615 if (bp == NULL) 6616 bcopy(indirdep->ir_saveddata, 6617 indirdep->ir_savebp->b_data, 6618 indirdep->ir_savebp->b_bcount); 6619 free(indirdep->ir_saveddata, M_INDIRDEP); 6620 indirdep->ir_saveddata = NULL; 6621 } 6622 /* 6623 * When bp is NULL there is a full truncation pending. We 6624 * must wait for this full truncation to be journaled before 6625 * we can release this freework because the disk pointers will 6626 * never be written as zero. 6627 */ 6628 if (bp == NULL) { 6629 if (LIST_EMPTY(&indirdep->ir_freeblks->fb_jblkdephd)) 6630 handle_written_freework(freework); 6631 else 6632 WORKLIST_INSERT(&indirdep->ir_freeblks->fb_freeworkhd, 6633 &freework->fw_list); 6634 if (fwn == NULL) { 6635 freework->fw_indir = (void *)0x0000deadbeef0000; 6636 bp = indirdep->ir_savebp; 6637 indirdep->ir_savebp = NULL; 6638 free_indirdep(indirdep); 6639 FREE_LOCK(ump); 6640 brelse(bp); 6641 ACQUIRE_LOCK(ump); 6642 } 6643 } else { 6644 /* Complete when the real copy is written. */ 6645 WORKLIST_INSERT(&bp->b_dep, &freework->fw_list); 6646 BUF_UNLOCK(bp); 6647 } 6648 } 6649 6650 /* 6651 * Calculate the number of blocks we are going to release where datablocks 6652 * is the current total and length is the new file size. 6653 */ 6654 static ufs2_daddr_t 6655 blkcount(struct fs *fs, 6656 ufs2_daddr_t datablocks, 6657 off_t length) 6658 { 6659 off_t totblks, numblks; 6660 6661 totblks = 0; 6662 numblks = howmany(length, fs->fs_bsize); 6663 if (numblks <= UFS_NDADDR) { 6664 totblks = howmany(length, fs->fs_fsize); 6665 goto out; 6666 } 6667 totblks = blkstofrags(fs, numblks); 6668 numblks -= UFS_NDADDR; 6669 /* 6670 * Count all single, then double, then triple indirects required. 6671 * Subtracting one indirects worth of blocks for each pass 6672 * acknowledges one of each pointed to by the inode. 6673 */ 6674 for (;;) { 6675 totblks += blkstofrags(fs, howmany(numblks, NINDIR(fs))); 6676 numblks -= NINDIR(fs); 6677 if (numblks <= 0) 6678 break; 6679 numblks = howmany(numblks, NINDIR(fs)); 6680 } 6681 out: 6682 totblks = fsbtodb(fs, totblks); 6683 /* 6684 * Handle sparse files. We can't reclaim more blocks than the inode 6685 * references. We will correct it later in handle_complete_freeblks() 6686 * when we know the real count. 6687 */ 6688 if (totblks > datablocks) 6689 return (0); 6690 return (datablocks - totblks); 6691 } 6692 6693 /* 6694 * Handle freeblocks for journaled softupdate filesystems. 6695 * 6696 * Contrary to normal softupdates, we must preserve the block pointers in 6697 * indirects until their subordinates are free. This is to avoid journaling 6698 * every block that is freed which may consume more space than the journal 6699 * itself. The recovery program will see the free block journals at the 6700 * base of the truncated area and traverse them to reclaim space. The 6701 * pointers in the inode may be cleared immediately after the journal 6702 * records are written because each direct and indirect pointer in the 6703 * inode is recorded in a journal. This permits full truncation to proceed 6704 * asynchronously. The write order is journal -> inode -> cgs -> indirects. 6705 * 6706 * The algorithm is as follows: 6707 * 1) Traverse the in-memory state and create journal entries to release 6708 * the relevant blocks and full indirect trees. 6709 * 2) Traverse the indirect block chain adding partial truncation freework 6710 * records to indirects in the path to lastlbn. The freework will 6711 * prevent new allocation dependencies from being satisfied in this 6712 * indirect until the truncation completes. 6713 * 3) Read and lock the inode block, performing an update with the new size 6714 * and pointers. This prevents truncated data from becoming valid on 6715 * disk through step 4. 6716 * 4) Reap unsatisfied dependencies that are beyond the truncated area, 6717 * eliminate journal work for those records that do not require it. 6718 * 5) Schedule the journal records to be written followed by the inode block. 6719 * 6) Allocate any necessary frags for the end of file. 6720 * 7) Zero any partially truncated blocks. 6721 * 6722 * From this truncation proceeds asynchronously using the freework and 6723 * indir_trunc machinery. The file will not be extended again into a 6724 * partially truncated indirect block until all work is completed but 6725 * the normal dependency mechanism ensures that it is rolled back/forward 6726 * as appropriate. Further truncation may occur without delay and is 6727 * serialized in indir_trunc(). 6728 */ 6729 void 6730 softdep_journal_freeblocks( 6731 struct inode *ip, /* The inode whose length is to be reduced */ 6732 struct ucred *cred, 6733 off_t length, /* The new length for the file */ 6734 int flags) /* IO_EXT and/or IO_NORMAL */ 6735 { 6736 struct freeblks *freeblks, *fbn; 6737 struct worklist *wk, *wkn; 6738 struct inodedep *inodedep; 6739 struct jblkdep *jblkdep; 6740 struct allocdirect *adp, *adpn; 6741 struct ufsmount *ump; 6742 struct fs *fs; 6743 struct buf *bp; 6744 struct vnode *vp; 6745 struct mount *mp; 6746 daddr_t dbn; 6747 ufs2_daddr_t extblocks, datablocks; 6748 ufs_lbn_t tmpval, lbn, lastlbn; 6749 int frags, lastoff, iboff, allocblock, needj, error, i; 6750 6751 ump = ITOUMP(ip); 6752 mp = UFSTOVFS(ump); 6753 fs = ump->um_fs; 6754 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6755 ("softdep_journal_freeblocks called on non-softdep filesystem")); 6756 vp = ITOV(ip); 6757 needj = 1; 6758 iboff = -1; 6759 allocblock = 0; 6760 extblocks = 0; 6761 datablocks = 0; 6762 frags = 0; 6763 freeblks = newfreeblks(mp, ip); 6764 ACQUIRE_LOCK(ump); 6765 /* 6766 * If we're truncating a removed file that will never be written 6767 * we don't need to journal the block frees. The canceled journals 6768 * for the allocations will suffice. 6769 */ 6770 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6771 if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED && 6772 length == 0) 6773 needj = 0; 6774 CTR3(KTR_SUJ, "softdep_journal_freeblks: ip %d length %ld needj %d", 6775 ip->i_number, length, needj); 6776 FREE_LOCK(ump); 6777 /* 6778 * Calculate the lbn that we are truncating to. This results in -1 6779 * if we're truncating the 0 bytes. So it is the last lbn we want 6780 * to keep, not the first lbn we want to truncate. 6781 */ 6782 lastlbn = lblkno(fs, length + fs->fs_bsize - 1) - 1; 6783 lastoff = blkoff(fs, length); 6784 /* 6785 * Compute frags we are keeping in lastlbn. 0 means all. 6786 */ 6787 if (lastlbn >= 0 && lastlbn < UFS_NDADDR) { 6788 frags = fragroundup(fs, lastoff); 6789 /* adp offset of last valid allocdirect. */ 6790 iboff = lastlbn; 6791 } else if (lastlbn > 0) 6792 iboff = UFS_NDADDR; 6793 if (fs->fs_magic == FS_UFS2_MAGIC) 6794 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 6795 /* 6796 * Handle normal data blocks and indirects. This section saves 6797 * values used after the inode update to complete frag and indirect 6798 * truncation. 6799 */ 6800 if ((flags & IO_NORMAL) != 0) { 6801 /* 6802 * Handle truncation of whole direct and indirect blocks. 6803 */ 6804 for (i = iboff + 1; i < UFS_NDADDR; i++) 6805 setup_freedirect(freeblks, ip, i, needj); 6806 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR; 6807 i < UFS_NIADDR; 6808 i++, lbn += tmpval, tmpval *= NINDIR(fs)) { 6809 /* Release a whole indirect tree. */ 6810 if (lbn > lastlbn) { 6811 setup_freeindir(freeblks, ip, i, -lbn -i, 6812 needj); 6813 continue; 6814 } 6815 iboff = i + UFS_NDADDR; 6816 /* 6817 * Traverse partially truncated indirect tree. 6818 */ 6819 if (lbn <= lastlbn && lbn + tmpval - 1 > lastlbn) 6820 setup_trunc_indir(freeblks, ip, -lbn - i, 6821 lastlbn, DIP(ip, i_ib[i])); 6822 } 6823 /* 6824 * Handle partial truncation to a frag boundary. 6825 */ 6826 if (frags) { 6827 ufs2_daddr_t blkno; 6828 long oldfrags; 6829 6830 oldfrags = blksize(fs, ip, lastlbn); 6831 blkno = DIP(ip, i_db[lastlbn]); 6832 if (blkno && oldfrags != frags) { 6833 oldfrags -= frags; 6834 oldfrags = numfrags(fs, oldfrags); 6835 blkno += numfrags(fs, frags); 6836 newfreework(ump, freeblks, NULL, lastlbn, 6837 blkno, oldfrags, 0, needj); 6838 if (needj) 6839 adjust_newfreework(freeblks, 6840 numfrags(fs, frags)); 6841 } else if (blkno == 0) 6842 allocblock = 1; 6843 } 6844 /* 6845 * Add a journal record for partial truncate if we are 6846 * handling indirect blocks. Non-indirects need no extra 6847 * journaling. 6848 */ 6849 if (length != 0 && lastlbn >= UFS_NDADDR) { 6850 UFS_INODE_SET_FLAG(ip, IN_TRUNCATED); 6851 newjtrunc(freeblks, length, 0); 6852 } 6853 ip->i_size = length; 6854 DIP_SET(ip, i_size, ip->i_size); 6855 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE); 6856 datablocks = DIP(ip, i_blocks) - extblocks; 6857 if (length != 0) 6858 datablocks = blkcount(fs, datablocks, length); 6859 freeblks->fb_len = length; 6860 } 6861 if ((flags & IO_EXT) != 0) { 6862 for (i = 0; i < UFS_NXADDR; i++) 6863 setup_freeext(freeblks, ip, i, needj); 6864 ip->i_din2->di_extsize = 0; 6865 datablocks += extblocks; 6866 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE); 6867 } 6868 #ifdef QUOTA 6869 /* Reference the quotas in case the block count is wrong in the end. */ 6870 quotaref(vp, freeblks->fb_quota); 6871 (void) chkdq(ip, -datablocks, NOCRED, FORCE); 6872 #endif 6873 freeblks->fb_chkcnt = -datablocks; 6874 UFS_LOCK(ump); 6875 fs->fs_pendingblocks += datablocks; 6876 UFS_UNLOCK(ump); 6877 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 6878 /* 6879 * Handle truncation of incomplete alloc direct dependencies. We 6880 * hold the inode block locked to prevent incomplete dependencies 6881 * from reaching the disk while we are eliminating those that 6882 * have been truncated. This is a partially inlined ffs_update(). 6883 */ 6884 ufs_itimes(vp); 6885 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED); 6886 dbn = fsbtodb(fs, ino_to_fsba(fs, ip->i_number)); 6887 error = ffs_breadz(ump, ump->um_devvp, dbn, dbn, (int)fs->fs_bsize, 6888 NULL, NULL, 0, cred, 0, NULL, &bp); 6889 if (error) { 6890 softdep_error("softdep_journal_freeblocks", error); 6891 return; 6892 } 6893 if (bp->b_bufsize == fs->fs_bsize) 6894 bp->b_flags |= B_CLUSTEROK; 6895 softdep_update_inodeblock(ip, bp, 0); 6896 if (ump->um_fstype == UFS1) { 6897 *((struct ufs1_dinode *)bp->b_data + 6898 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1; 6899 } else { 6900 ffs_update_dinode_ckhash(fs, ip->i_din2); 6901 *((struct ufs2_dinode *)bp->b_data + 6902 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2; 6903 } 6904 ACQUIRE_LOCK(ump); 6905 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6906 if ((inodedep->id_state & IOSTARTED) != 0) 6907 panic("softdep_setup_freeblocks: inode busy"); 6908 /* 6909 * Add the freeblks structure to the list of operations that 6910 * must await the zero'ed inode being written to disk. If we 6911 * still have a bitmap dependency (needj), then the inode 6912 * has never been written to disk, so we can process the 6913 * freeblks below once we have deleted the dependencies. 6914 */ 6915 if (needj) 6916 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 6917 else 6918 freeblks->fb_state |= COMPLETE; 6919 if ((flags & IO_NORMAL) != 0) { 6920 TAILQ_FOREACH_SAFE(adp, &inodedep->id_inoupdt, ad_next, adpn) { 6921 if (adp->ad_offset > iboff) 6922 cancel_allocdirect(&inodedep->id_inoupdt, adp, 6923 freeblks); 6924 /* 6925 * Truncate the allocdirect. We could eliminate 6926 * or modify journal records as well. 6927 */ 6928 else if (adp->ad_offset == iboff && frags) 6929 adp->ad_newsize = frags; 6930 } 6931 } 6932 if ((flags & IO_EXT) != 0) 6933 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 6934 cancel_allocdirect(&inodedep->id_extupdt, adp, 6935 freeblks); 6936 /* 6937 * Scan the bufwait list for newblock dependencies that will never 6938 * make it to disk. 6939 */ 6940 LIST_FOREACH_SAFE(wk, &inodedep->id_bufwait, wk_list, wkn) { 6941 if (wk->wk_type != D_ALLOCDIRECT) 6942 continue; 6943 adp = WK_ALLOCDIRECT(wk); 6944 if (((flags & IO_NORMAL) != 0 && (adp->ad_offset > iboff)) || 6945 ((flags & IO_EXT) != 0 && (adp->ad_state & EXTDATA))) { 6946 cancel_jfreeblk(freeblks, adp->ad_newblkno); 6947 cancel_newblk(WK_NEWBLK(wk), NULL, &freeblks->fb_jwork); 6948 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 6949 } 6950 } 6951 /* 6952 * Add journal work. 6953 */ 6954 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) 6955 add_to_journal(&jblkdep->jb_list); 6956 FREE_LOCK(ump); 6957 bdwrite(bp); 6958 /* 6959 * Truncate dependency structures beyond length. 6960 */ 6961 trunc_dependencies(ip, freeblks, lastlbn, frags, flags); 6962 /* 6963 * This is only set when we need to allocate a fragment because 6964 * none existed at the end of a frag-sized file. It handles only 6965 * allocating a new, zero filled block. 6966 */ 6967 if (allocblock) { 6968 ip->i_size = length - lastoff; 6969 DIP_SET(ip, i_size, ip->i_size); 6970 error = UFS_BALLOC(vp, length - 1, 1, cred, BA_CLRBUF, &bp); 6971 if (error != 0) { 6972 softdep_error("softdep_journal_freeblks", error); 6973 return; 6974 } 6975 ip->i_size = length; 6976 DIP_SET(ip, i_size, length); 6977 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE); 6978 allocbuf(bp, frags); 6979 ffs_update(vp, 0); 6980 bawrite(bp); 6981 } else if (lastoff != 0 && vp->v_type != VDIR) { 6982 int size; 6983 6984 /* 6985 * Zero the end of a truncated frag or block. 6986 */ 6987 size = sblksize(fs, length, lastlbn); 6988 error = bread(vp, lastlbn, size, cred, &bp); 6989 if (error == 0) { 6990 bzero((char *)bp->b_data + lastoff, size - lastoff); 6991 bawrite(bp); 6992 } else if (!ffs_fsfail_cleanup(ump, error)) { 6993 softdep_error("softdep_journal_freeblks", error); 6994 return; 6995 } 6996 } 6997 ACQUIRE_LOCK(ump); 6998 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 6999 TAILQ_INSERT_TAIL(&inodedep->id_freeblklst, freeblks, fb_next); 7000 freeblks->fb_state |= DEPCOMPLETE | ONDEPLIST; 7001 /* 7002 * We zero earlier truncations so they don't erroneously 7003 * update i_blocks. 7004 */ 7005 if (freeblks->fb_len == 0 && (flags & IO_NORMAL) != 0) 7006 TAILQ_FOREACH(fbn, &inodedep->id_freeblklst, fb_next) 7007 fbn->fb_len = 0; 7008 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE && 7009 LIST_EMPTY(&freeblks->fb_jblkdephd)) 7010 freeblks->fb_state |= INPROGRESS; 7011 else 7012 freeblks = NULL; 7013 FREE_LOCK(ump); 7014 if (freeblks) 7015 handle_workitem_freeblocks(freeblks, 0); 7016 trunc_pages(ip, length, extblocks, flags); 7017 7018 } 7019 7020 /* 7021 * Flush a JOP_SYNC to the journal. 7022 */ 7023 void 7024 softdep_journal_fsync(struct inode *ip) 7025 { 7026 struct jfsync *jfsync; 7027 struct ufsmount *ump; 7028 7029 ump = ITOUMP(ip); 7030 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 7031 ("softdep_journal_fsync called on non-softdep filesystem")); 7032 if ((ip->i_flag & IN_TRUNCATED) == 0) 7033 return; 7034 ip->i_flag &= ~IN_TRUNCATED; 7035 jfsync = malloc(sizeof(*jfsync), M_JFSYNC, M_SOFTDEP_FLAGS | M_ZERO); 7036 workitem_alloc(&jfsync->jfs_list, D_JFSYNC, UFSTOVFS(ump)); 7037 jfsync->jfs_size = ip->i_size; 7038 jfsync->jfs_ino = ip->i_number; 7039 ACQUIRE_LOCK(ump); 7040 add_to_journal(&jfsync->jfs_list); 7041 jwait(&jfsync->jfs_list, MNT_WAIT); 7042 FREE_LOCK(ump); 7043 } 7044 7045 /* 7046 * Block de-allocation dependencies. 7047 * 7048 * When blocks are de-allocated, the on-disk pointers must be nullified before 7049 * the blocks are made available for use by other files. (The true 7050 * requirement is that old pointers must be nullified before new on-disk 7051 * pointers are set. We chose this slightly more stringent requirement to 7052 * reduce complexity.) Our implementation handles this dependency by updating 7053 * the inode (or indirect block) appropriately but delaying the actual block 7054 * de-allocation (i.e., freemap and free space count manipulation) until 7055 * after the updated versions reach stable storage. After the disk is 7056 * updated, the blocks can be safely de-allocated whenever it is convenient. 7057 * This implementation handles only the common case of reducing a file's 7058 * length to zero. Other cases are handled by the conventional synchronous 7059 * write approach. 7060 * 7061 * The ffs implementation with which we worked double-checks 7062 * the state of the block pointers and file size as it reduces 7063 * a file's length. Some of this code is replicated here in our 7064 * soft updates implementation. The freeblks->fb_chkcnt field is 7065 * used to transfer a part of this information to the procedure 7066 * that eventually de-allocates the blocks. 7067 * 7068 * This routine should be called from the routine that shortens 7069 * a file's length, before the inode's size or block pointers 7070 * are modified. It will save the block pointer information for 7071 * later release and zero the inode so that the calling routine 7072 * can release it. 7073 */ 7074 void 7075 softdep_setup_freeblocks( 7076 struct inode *ip, /* The inode whose length is to be reduced */ 7077 off_t length, /* The new length for the file */ 7078 int flags) /* IO_EXT and/or IO_NORMAL */ 7079 { 7080 struct ufs1_dinode *dp1; 7081 struct ufs2_dinode *dp2; 7082 struct freeblks *freeblks; 7083 struct inodedep *inodedep; 7084 struct allocdirect *adp; 7085 struct ufsmount *ump; 7086 struct buf *bp; 7087 struct fs *fs; 7088 ufs2_daddr_t extblocks, datablocks; 7089 struct mount *mp; 7090 int i, delay, error; 7091 ufs_lbn_t tmpval; 7092 ufs_lbn_t lbn; 7093 7094 ump = ITOUMP(ip); 7095 mp = UFSTOVFS(ump); 7096 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 7097 ("softdep_setup_freeblocks called on non-softdep filesystem")); 7098 CTR2(KTR_SUJ, "softdep_setup_freeblks: ip %d length %ld", 7099 ip->i_number, length); 7100 KASSERT(length == 0, ("softdep_setup_freeblocks: non-zero length")); 7101 fs = ump->um_fs; 7102 if ((error = bread(ump->um_devvp, 7103 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 7104 (int)fs->fs_bsize, NOCRED, &bp)) != 0) { 7105 if (!ffs_fsfail_cleanup(ump, error)) 7106 softdep_error("softdep_setup_freeblocks", error); 7107 return; 7108 } 7109 freeblks = newfreeblks(mp, ip); 7110 extblocks = 0; 7111 datablocks = 0; 7112 if (fs->fs_magic == FS_UFS2_MAGIC) 7113 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 7114 if ((flags & IO_NORMAL) != 0) { 7115 for (i = 0; i < UFS_NDADDR; i++) 7116 setup_freedirect(freeblks, ip, i, 0); 7117 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR; 7118 i < UFS_NIADDR; 7119 i++, lbn += tmpval, tmpval *= NINDIR(fs)) 7120 setup_freeindir(freeblks, ip, i, -lbn -i, 0); 7121 ip->i_size = 0; 7122 DIP_SET(ip, i_size, 0); 7123 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE); 7124 datablocks = DIP(ip, i_blocks) - extblocks; 7125 } 7126 if ((flags & IO_EXT) != 0) { 7127 for (i = 0; i < UFS_NXADDR; i++) 7128 setup_freeext(freeblks, ip, i, 0); 7129 ip->i_din2->di_extsize = 0; 7130 datablocks += extblocks; 7131 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE); 7132 } 7133 #ifdef QUOTA 7134 /* Reference the quotas in case the block count is wrong in the end. */ 7135 quotaref(ITOV(ip), freeblks->fb_quota); 7136 (void) chkdq(ip, -datablocks, NOCRED, FORCE); 7137 #endif 7138 freeblks->fb_chkcnt = -datablocks; 7139 UFS_LOCK(ump); 7140 fs->fs_pendingblocks += datablocks; 7141 UFS_UNLOCK(ump); 7142 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 7143 /* 7144 * Push the zero'ed inode to its disk buffer so that we are free 7145 * to delete its dependencies below. Once the dependencies are gone 7146 * the buffer can be safely released. 7147 */ 7148 if (ump->um_fstype == UFS1) { 7149 dp1 = ((struct ufs1_dinode *)bp->b_data + 7150 ino_to_fsbo(fs, ip->i_number)); 7151 ip->i_din1->di_freelink = dp1->di_freelink; 7152 *dp1 = *ip->i_din1; 7153 } else { 7154 dp2 = ((struct ufs2_dinode *)bp->b_data + 7155 ino_to_fsbo(fs, ip->i_number)); 7156 ip->i_din2->di_freelink = dp2->di_freelink; 7157 ffs_update_dinode_ckhash(fs, ip->i_din2); 7158 *dp2 = *ip->i_din2; 7159 } 7160 /* 7161 * Find and eliminate any inode dependencies. 7162 */ 7163 ACQUIRE_LOCK(ump); 7164 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 7165 if ((inodedep->id_state & IOSTARTED) != 0) 7166 panic("softdep_setup_freeblocks: inode busy"); 7167 /* 7168 * Add the freeblks structure to the list of operations that 7169 * must await the zero'ed inode being written to disk. If we 7170 * still have a bitmap dependency (delay == 0), then the inode 7171 * has never been written to disk, so we can process the 7172 * freeblks below once we have deleted the dependencies. 7173 */ 7174 delay = (inodedep->id_state & DEPCOMPLETE); 7175 if (delay) 7176 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 7177 else 7178 freeblks->fb_state |= COMPLETE; 7179 /* 7180 * Because the file length has been truncated to zero, any 7181 * pending block allocation dependency structures associated 7182 * with this inode are obsolete and can simply be de-allocated. 7183 * We must first merge the two dependency lists to get rid of 7184 * any duplicate freefrag structures, then purge the merged list. 7185 * If we still have a bitmap dependency, then the inode has never 7186 * been written to disk, so we can free any fragments without delay. 7187 */ 7188 if (flags & IO_NORMAL) { 7189 merge_inode_lists(&inodedep->id_newinoupdt, 7190 &inodedep->id_inoupdt); 7191 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 7192 cancel_allocdirect(&inodedep->id_inoupdt, adp, 7193 freeblks); 7194 } 7195 if (flags & IO_EXT) { 7196 merge_inode_lists(&inodedep->id_newextupdt, 7197 &inodedep->id_extupdt); 7198 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 7199 cancel_allocdirect(&inodedep->id_extupdt, adp, 7200 freeblks); 7201 } 7202 FREE_LOCK(ump); 7203 bdwrite(bp); 7204 trunc_dependencies(ip, freeblks, -1, 0, flags); 7205 ACQUIRE_LOCK(ump); 7206 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 7207 (void) free_inodedep(inodedep); 7208 freeblks->fb_state |= DEPCOMPLETE; 7209 /* 7210 * If the inode with zeroed block pointers is now on disk 7211 * we can start freeing blocks. 7212 */ 7213 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 7214 freeblks->fb_state |= INPROGRESS; 7215 else 7216 freeblks = NULL; 7217 FREE_LOCK(ump); 7218 if (freeblks) 7219 handle_workitem_freeblocks(freeblks, 0); 7220 trunc_pages(ip, length, extblocks, flags); 7221 } 7222 7223 /* 7224 * Eliminate pages from the page cache that back parts of this inode and 7225 * adjust the vnode pager's idea of our size. This prevents stale data 7226 * from hanging around in the page cache. 7227 */ 7228 static void 7229 trunc_pages( 7230 struct inode *ip, 7231 off_t length, 7232 ufs2_daddr_t extblocks, 7233 int flags) 7234 { 7235 struct vnode *vp; 7236 struct fs *fs; 7237 ufs_lbn_t lbn; 7238 off_t end, extend; 7239 7240 vp = ITOV(ip); 7241 fs = ITOFS(ip); 7242 extend = OFF_TO_IDX(lblktosize(fs, -extblocks)); 7243 if ((flags & IO_EXT) != 0) 7244 vn_pages_remove(vp, extend, 0); 7245 if ((flags & IO_NORMAL) == 0) 7246 return; 7247 BO_LOCK(&vp->v_bufobj); 7248 drain_output(vp); 7249 BO_UNLOCK(&vp->v_bufobj); 7250 /* 7251 * The vnode pager eliminates file pages we eliminate indirects 7252 * below. 7253 */ 7254 vnode_pager_setsize(vp, length); 7255 /* 7256 * Calculate the end based on the last indirect we want to keep. If 7257 * the block extends into indirects we can just use the negative of 7258 * its lbn. Doubles and triples exist at lower numbers so we must 7259 * be careful not to remove those, if they exist. double and triple 7260 * indirect lbns do not overlap with others so it is not important 7261 * to verify how many levels are required. 7262 */ 7263 lbn = lblkno(fs, length); 7264 if (lbn >= UFS_NDADDR) { 7265 /* Calculate the virtual lbn of the triple indirect. */ 7266 lbn = -lbn - (UFS_NIADDR - 1); 7267 end = OFF_TO_IDX(lblktosize(fs, lbn)); 7268 } else 7269 end = extend; 7270 vn_pages_remove(vp, OFF_TO_IDX(OFF_MAX), end); 7271 } 7272 7273 /* 7274 * See if the buf bp is in the range eliminated by truncation. 7275 */ 7276 static int 7277 trunc_check_buf( 7278 struct buf *bp, 7279 int *blkoffp, 7280 ufs_lbn_t lastlbn, 7281 int lastoff, 7282 int flags) 7283 { 7284 ufs_lbn_t lbn; 7285 7286 *blkoffp = 0; 7287 /* Only match ext/normal blocks as appropriate. */ 7288 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) || 7289 ((flags & IO_NORMAL) == 0 && (bp->b_xflags & BX_ALTDATA) == 0)) 7290 return (0); 7291 /* ALTDATA is always a full truncation. */ 7292 if ((bp->b_xflags & BX_ALTDATA) != 0) 7293 return (1); 7294 /* -1 is full truncation. */ 7295 if (lastlbn == -1) 7296 return (1); 7297 /* 7298 * If this is a partial truncate we only want those 7299 * blocks and indirect blocks that cover the range 7300 * we're after. 7301 */ 7302 lbn = bp->b_lblkno; 7303 if (lbn < 0) 7304 lbn = -(lbn + lbn_level(lbn)); 7305 if (lbn < lastlbn) 7306 return (0); 7307 /* Here we only truncate lblkno if it's partial. */ 7308 if (lbn == lastlbn) { 7309 if (lastoff == 0) 7310 return (0); 7311 *blkoffp = lastoff; 7312 } 7313 return (1); 7314 } 7315 7316 /* 7317 * Eliminate any dependencies that exist in memory beyond lblkno:off 7318 */ 7319 static void 7320 trunc_dependencies( 7321 struct inode *ip, 7322 struct freeblks *freeblks, 7323 ufs_lbn_t lastlbn, 7324 int lastoff, 7325 int flags) 7326 { 7327 struct bufobj *bo; 7328 struct vnode *vp; 7329 struct buf *bp; 7330 int blkoff; 7331 7332 /* 7333 * We must wait for any I/O in progress to finish so that 7334 * all potential buffers on the dirty list will be visible. 7335 * Once they are all there, walk the list and get rid of 7336 * any dependencies. 7337 */ 7338 vp = ITOV(ip); 7339 bo = &vp->v_bufobj; 7340 BO_LOCK(bo); 7341 drain_output(vp); 7342 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) 7343 bp->b_vflags &= ~BV_SCANNED; 7344 restart: 7345 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) { 7346 if (bp->b_vflags & BV_SCANNED) 7347 continue; 7348 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 7349 bp->b_vflags |= BV_SCANNED; 7350 continue; 7351 } 7352 KASSERT(bp->b_bufobj == bo, ("Wrong object in buffer")); 7353 if ((bp = getdirtybuf(bp, BO_LOCKPTR(bo), MNT_WAIT)) == NULL) 7354 goto restart; 7355 BO_UNLOCK(bo); 7356 if (deallocate_dependencies(bp, freeblks, blkoff)) 7357 bqrelse(bp); 7358 else 7359 brelse(bp); 7360 BO_LOCK(bo); 7361 goto restart; 7362 } 7363 /* 7364 * Now do the work of vtruncbuf while also matching indirect blocks. 7365 */ 7366 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) 7367 bp->b_vflags &= ~BV_SCANNED; 7368 cleanrestart: 7369 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) { 7370 if (bp->b_vflags & BV_SCANNED) 7371 continue; 7372 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 7373 bp->b_vflags |= BV_SCANNED; 7374 continue; 7375 } 7376 if (BUF_LOCK(bp, 7377 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 7378 BO_LOCKPTR(bo)) == ENOLCK) { 7379 BO_LOCK(bo); 7380 goto cleanrestart; 7381 } 7382 BO_LOCK(bo); 7383 bp->b_vflags |= BV_SCANNED; 7384 BO_UNLOCK(bo); 7385 bremfree(bp); 7386 if (blkoff != 0) { 7387 allocbuf(bp, blkoff); 7388 bqrelse(bp); 7389 } else { 7390 bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF; 7391 brelse(bp); 7392 } 7393 BO_LOCK(bo); 7394 goto cleanrestart; 7395 } 7396 drain_output(vp); 7397 BO_UNLOCK(bo); 7398 } 7399 7400 static int 7401 cancel_pagedep( 7402 struct pagedep *pagedep, 7403 struct freeblks *freeblks, 7404 int blkoff) 7405 { 7406 struct jremref *jremref; 7407 struct jmvref *jmvref; 7408 struct dirrem *dirrem, *tmp; 7409 int i; 7410 7411 /* 7412 * Copy any directory remove dependencies to the list 7413 * to be processed after the freeblks proceeds. If 7414 * directory entry never made it to disk they 7415 * can be dumped directly onto the work list. 7416 */ 7417 LIST_FOREACH_SAFE(dirrem, &pagedep->pd_dirremhd, dm_next, tmp) { 7418 /* Skip this directory removal if it is intended to remain. */ 7419 if (dirrem->dm_offset < blkoff) 7420 continue; 7421 /* 7422 * If there are any dirrems we wait for the journal write 7423 * to complete and then restart the buf scan as the lock 7424 * has been dropped. 7425 */ 7426 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) { 7427 jwait(&jremref->jr_list, MNT_WAIT); 7428 return (ERESTART); 7429 } 7430 LIST_REMOVE(dirrem, dm_next); 7431 dirrem->dm_dirinum = pagedep->pd_ino; 7432 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list); 7433 } 7434 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) { 7435 jwait(&jmvref->jm_list, MNT_WAIT); 7436 return (ERESTART); 7437 } 7438 /* 7439 * When we're partially truncating a pagedep we just want to flush 7440 * journal entries and return. There can not be any adds in the 7441 * truncated portion of the directory and newblk must remain if 7442 * part of the block remains. 7443 */ 7444 if (blkoff != 0) { 7445 struct diradd *dap; 7446 7447 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 7448 if (dap->da_offset > blkoff) 7449 panic("cancel_pagedep: diradd %p off %d > %d", 7450 dap, dap->da_offset, blkoff); 7451 for (i = 0; i < DAHASHSZ; i++) 7452 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) 7453 if (dap->da_offset > blkoff) 7454 panic("cancel_pagedep: diradd %p off %d > %d", 7455 dap, dap->da_offset, blkoff); 7456 return (0); 7457 } 7458 /* 7459 * There should be no directory add dependencies present 7460 * as the directory could not be truncated until all 7461 * children were removed. 7462 */ 7463 KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL, 7464 ("deallocate_dependencies: pendinghd != NULL")); 7465 for (i = 0; i < DAHASHSZ; i++) 7466 KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL, 7467 ("deallocate_dependencies: diraddhd != NULL")); 7468 if ((pagedep->pd_state & NEWBLOCK) != 0) 7469 free_newdirblk(pagedep->pd_newdirblk); 7470 if (free_pagedep(pagedep) == 0) 7471 panic("Failed to free pagedep %p", pagedep); 7472 return (0); 7473 } 7474 7475 /* 7476 * Reclaim any dependency structures from a buffer that is about to 7477 * be reallocated to a new vnode. The buffer must be locked, thus, 7478 * no I/O completion operations can occur while we are manipulating 7479 * its associated dependencies. The mutex is held so that other I/O's 7480 * associated with related dependencies do not occur. 7481 */ 7482 static int 7483 deallocate_dependencies( 7484 struct buf *bp, 7485 struct freeblks *freeblks, 7486 int off) 7487 { 7488 struct indirdep *indirdep; 7489 struct pagedep *pagedep; 7490 struct worklist *wk, *wkn; 7491 struct ufsmount *ump; 7492 7493 ump = softdep_bp_to_mp(bp); 7494 if (ump == NULL) 7495 goto done; 7496 ACQUIRE_LOCK(ump); 7497 LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) { 7498 switch (wk->wk_type) { 7499 case D_INDIRDEP: 7500 indirdep = WK_INDIRDEP(wk); 7501 if (bp->b_lblkno >= 0 || 7502 bp->b_blkno != indirdep->ir_savebp->b_lblkno) 7503 panic("deallocate_dependencies: not indir"); 7504 cancel_indirdep(indirdep, bp, freeblks); 7505 continue; 7506 7507 case D_PAGEDEP: 7508 pagedep = WK_PAGEDEP(wk); 7509 if (cancel_pagedep(pagedep, freeblks, off)) { 7510 FREE_LOCK(ump); 7511 return (ERESTART); 7512 } 7513 continue; 7514 7515 case D_ALLOCINDIR: 7516 /* 7517 * Simply remove the allocindir, we'll find it via 7518 * the indirdep where we can clear pointers if 7519 * needed. 7520 */ 7521 WORKLIST_REMOVE(wk); 7522 continue; 7523 7524 case D_FREEWORK: 7525 /* 7526 * A truncation is waiting for the zero'd pointers 7527 * to be written. It can be freed when the freeblks 7528 * is journaled. 7529 */ 7530 WORKLIST_REMOVE(wk); 7531 wk->wk_state |= ONDEPLIST; 7532 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 7533 break; 7534 7535 case D_ALLOCDIRECT: 7536 if (off != 0) 7537 continue; 7538 /* FALLTHROUGH */ 7539 default: 7540 panic("deallocate_dependencies: Unexpected type %s", 7541 TYPENAME(wk->wk_type)); 7542 /* NOTREACHED */ 7543 } 7544 } 7545 FREE_LOCK(ump); 7546 done: 7547 /* 7548 * Don't throw away this buf, we were partially truncating and 7549 * some deps may always remain. 7550 */ 7551 if (off) { 7552 allocbuf(bp, off); 7553 bp->b_vflags |= BV_SCANNED; 7554 return (EBUSY); 7555 } 7556 bp->b_flags |= B_INVAL | B_NOCACHE; 7557 7558 return (0); 7559 } 7560 7561 /* 7562 * An allocdirect is being canceled due to a truncate. We must make sure 7563 * the journal entry is released in concert with the blkfree that releases 7564 * the storage. Completed journal entries must not be released until the 7565 * space is no longer pointed to by the inode or in the bitmap. 7566 */ 7567 static void 7568 cancel_allocdirect( 7569 struct allocdirectlst *adphead, 7570 struct allocdirect *adp, 7571 struct freeblks *freeblks) 7572 { 7573 struct freework *freework; 7574 struct newblk *newblk; 7575 struct worklist *wk; 7576 7577 TAILQ_REMOVE(adphead, adp, ad_next); 7578 newblk = (struct newblk *)adp; 7579 freework = NULL; 7580 /* 7581 * Find the correct freework structure. 7582 */ 7583 LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) { 7584 if (wk->wk_type != D_FREEWORK) 7585 continue; 7586 freework = WK_FREEWORK(wk); 7587 if (freework->fw_blkno == newblk->nb_newblkno) 7588 break; 7589 } 7590 if (freework == NULL) 7591 panic("cancel_allocdirect: Freework not found"); 7592 /* 7593 * If a newblk exists at all we still have the journal entry that 7594 * initiated the allocation so we do not need to journal the free. 7595 */ 7596 cancel_jfreeblk(freeblks, freework->fw_blkno); 7597 /* 7598 * If the journal hasn't been written the jnewblk must be passed 7599 * to the call to ffs_blkfree that reclaims the space. We accomplish 7600 * this by linking the journal dependency into the freework to be 7601 * freed when freework_freeblock() is called. If the journal has 7602 * been written we can simply reclaim the journal space when the 7603 * freeblks work is complete. 7604 */ 7605 freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list, 7606 &freeblks->fb_jwork); 7607 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 7608 } 7609 7610 /* 7611 * Cancel a new block allocation. May be an indirect or direct block. We 7612 * remove it from various lists and return any journal record that needs to 7613 * be resolved by the caller. 7614 * 7615 * A special consideration is made for indirects which were never pointed 7616 * at on disk and will never be found once this block is released. 7617 */ 7618 static struct jnewblk * 7619 cancel_newblk( 7620 struct newblk *newblk, 7621 struct worklist *wk, 7622 struct workhead *wkhd) 7623 { 7624 struct jnewblk *jnewblk; 7625 7626 CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno); 7627 7628 newblk->nb_state |= GOINGAWAY; 7629 /* 7630 * Previously we traversed the completedhd on each indirdep 7631 * attached to this newblk to cancel them and gather journal 7632 * work. Since we need only the oldest journal segment and 7633 * the lowest point on the tree will always have the oldest 7634 * journal segment we are free to release the segments 7635 * of any subordinates and may leave the indirdep list to 7636 * indirdep_complete() when this newblk is freed. 7637 */ 7638 if (newblk->nb_state & ONDEPLIST) { 7639 newblk->nb_state &= ~ONDEPLIST; 7640 LIST_REMOVE(newblk, nb_deps); 7641 } 7642 if (newblk->nb_state & ONWORKLIST) 7643 WORKLIST_REMOVE(&newblk->nb_list); 7644 /* 7645 * If the journal entry hasn't been written we save a pointer to 7646 * the dependency that frees it until it is written or the 7647 * superseding operation completes. 7648 */ 7649 jnewblk = newblk->nb_jnewblk; 7650 if (jnewblk != NULL && wk != NULL) { 7651 newblk->nb_jnewblk = NULL; 7652 jnewblk->jn_dep = wk; 7653 } 7654 if (!LIST_EMPTY(&newblk->nb_jwork)) 7655 jwork_move(wkhd, &newblk->nb_jwork); 7656 /* 7657 * When truncating we must free the newdirblk early to remove 7658 * the pagedep from the hash before returning. 7659 */ 7660 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7661 free_newdirblk(WK_NEWDIRBLK(wk)); 7662 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7663 panic("cancel_newblk: extra newdirblk"); 7664 7665 return (jnewblk); 7666 } 7667 7668 /* 7669 * Schedule the freefrag associated with a newblk to be released once 7670 * the pointers are written and the previous block is no longer needed. 7671 */ 7672 static void 7673 newblk_freefrag(struct newblk *newblk) 7674 { 7675 struct freefrag *freefrag; 7676 7677 if (newblk->nb_freefrag == NULL) 7678 return; 7679 freefrag = newblk->nb_freefrag; 7680 newblk->nb_freefrag = NULL; 7681 freefrag->ff_state |= COMPLETE; 7682 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 7683 add_to_worklist(&freefrag->ff_list, 0); 7684 } 7685 7686 /* 7687 * Free a newblk. Generate a new freefrag work request if appropriate. 7688 * This must be called after the inode pointer and any direct block pointers 7689 * are valid or fully removed via truncate or frag extension. 7690 */ 7691 static void 7692 free_newblk(struct newblk *newblk) 7693 { 7694 struct indirdep *indirdep; 7695 struct worklist *wk; 7696 7697 KASSERT(newblk->nb_jnewblk == NULL, 7698 ("free_newblk: jnewblk %p still attached", newblk->nb_jnewblk)); 7699 KASSERT(newblk->nb_list.wk_type != D_NEWBLK, 7700 ("free_newblk: unclaimed newblk")); 7701 LOCK_OWNED(VFSTOUFS(newblk->nb_list.wk_mp)); 7702 newblk_freefrag(newblk); 7703 if (newblk->nb_state & ONDEPLIST) 7704 LIST_REMOVE(newblk, nb_deps); 7705 if (newblk->nb_state & ONWORKLIST) 7706 WORKLIST_REMOVE(&newblk->nb_list); 7707 LIST_REMOVE(newblk, nb_hash); 7708 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7709 free_newdirblk(WK_NEWDIRBLK(wk)); 7710 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7711 panic("free_newblk: extra newdirblk"); 7712 while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL) 7713 indirdep_complete(indirdep); 7714 handle_jwork(&newblk->nb_jwork); 7715 WORKITEM_FREE(newblk, D_NEWBLK); 7716 } 7717 7718 /* 7719 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep. 7720 */ 7721 static void 7722 free_newdirblk(struct newdirblk *newdirblk) 7723 { 7724 struct pagedep *pagedep; 7725 struct diradd *dap; 7726 struct worklist *wk; 7727 7728 LOCK_OWNED(VFSTOUFS(newdirblk->db_list.wk_mp)); 7729 WORKLIST_REMOVE(&newdirblk->db_list); 7730 /* 7731 * If the pagedep is still linked onto the directory buffer 7732 * dependency chain, then some of the entries on the 7733 * pd_pendinghd list may not be committed to disk yet. In 7734 * this case, we will simply clear the NEWBLOCK flag and 7735 * let the pd_pendinghd list be processed when the pagedep 7736 * is next written. If the pagedep is no longer on the buffer 7737 * dependency chain, then all the entries on the pd_pending 7738 * list are committed to disk and we can free them here. 7739 */ 7740 pagedep = newdirblk->db_pagedep; 7741 pagedep->pd_state &= ~NEWBLOCK; 7742 if ((pagedep->pd_state & ONWORKLIST) == 0) { 7743 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 7744 free_diradd(dap, NULL); 7745 /* 7746 * If no dependencies remain, the pagedep will be freed. 7747 */ 7748 free_pagedep(pagedep); 7749 } 7750 /* Should only ever be one item in the list. */ 7751 while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) { 7752 WORKLIST_REMOVE(wk); 7753 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 7754 } 7755 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 7756 } 7757 7758 /* 7759 * Prepare an inode to be freed. The actual free operation is not 7760 * done until the zero'ed inode has been written to disk. 7761 */ 7762 void 7763 softdep_freefile( 7764 struct vnode *pvp, 7765 ino_t ino, 7766 int mode) 7767 { 7768 struct inode *ip = VTOI(pvp); 7769 struct inodedep *inodedep; 7770 struct freefile *freefile; 7771 struct freeblks *freeblks; 7772 struct ufsmount *ump; 7773 7774 ump = ITOUMP(ip); 7775 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 7776 ("softdep_freefile called on non-softdep filesystem")); 7777 /* 7778 * This sets up the inode de-allocation dependency. 7779 */ 7780 freefile = malloc(sizeof(struct freefile), 7781 M_FREEFILE, M_SOFTDEP_FLAGS); 7782 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount); 7783 freefile->fx_mode = mode; 7784 freefile->fx_oldinum = ino; 7785 freefile->fx_devvp = ump->um_devvp; 7786 LIST_INIT(&freefile->fx_jwork); 7787 UFS_LOCK(ump); 7788 ump->um_fs->fs_pendinginodes += 1; 7789 UFS_UNLOCK(ump); 7790 7791 /* 7792 * If the inodedep does not exist, then the zero'ed inode has 7793 * been written to disk. If the allocated inode has never been 7794 * written to disk, then the on-disk inode is zero'ed. In either 7795 * case we can free the file immediately. If the journal was 7796 * canceled before being written the inode will never make it to 7797 * disk and we must send the canceled journal entrys to 7798 * ffs_freefile() to be cleared in conjunction with the bitmap. 7799 * Any blocks waiting on the inode to write can be safely freed 7800 * here as it will never been written. 7801 */ 7802 ACQUIRE_LOCK(ump); 7803 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7804 if (inodedep) { 7805 /* 7806 * Clear out freeblks that no longer need to reference 7807 * this inode. 7808 */ 7809 while ((freeblks = 7810 TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) { 7811 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, 7812 fb_next); 7813 freeblks->fb_state &= ~ONDEPLIST; 7814 } 7815 /* 7816 * Remove this inode from the unlinked list. 7817 */ 7818 if (inodedep->id_state & UNLINKED) { 7819 /* 7820 * Save the journal work to be freed with the bitmap 7821 * before we clear UNLINKED. Otherwise it can be lost 7822 * if the inode block is written. 7823 */ 7824 handle_bufwait(inodedep, &freefile->fx_jwork); 7825 clear_unlinked_inodedep(inodedep); 7826 /* 7827 * Re-acquire inodedep as we've dropped the 7828 * per-filesystem lock in clear_unlinked_inodedep(). 7829 */ 7830 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7831 } 7832 } 7833 if (inodedep == NULL || check_inode_unwritten(inodedep)) { 7834 FREE_LOCK(ump); 7835 handle_workitem_freefile(freefile); 7836 return; 7837 } 7838 if ((inodedep->id_state & DEPCOMPLETE) == 0) 7839 inodedep->id_state |= GOINGAWAY; 7840 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list); 7841 FREE_LOCK(ump); 7842 if (ip->i_number == ino) 7843 UFS_INODE_SET_FLAG(ip, IN_MODIFIED); 7844 } 7845 7846 /* 7847 * Check to see if an inode has never been written to disk. If 7848 * so free the inodedep and return success, otherwise return failure. 7849 * 7850 * If we still have a bitmap dependency, then the inode has never 7851 * been written to disk. Drop the dependency as it is no longer 7852 * necessary since the inode is being deallocated. We set the 7853 * ALLCOMPLETE flags since the bitmap now properly shows that the 7854 * inode is not allocated. Even if the inode is actively being 7855 * written, it has been rolled back to its zero'ed state, so we 7856 * are ensured that a zero inode is what is on the disk. For short 7857 * lived files, this change will usually result in removing all the 7858 * dependencies from the inode so that it can be freed immediately. 7859 */ 7860 static int 7861 check_inode_unwritten(struct inodedep *inodedep) 7862 { 7863 7864 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7865 7866 if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 || 7867 !LIST_EMPTY(&inodedep->id_dirremhd) || 7868 !LIST_EMPTY(&inodedep->id_pendinghd) || 7869 !LIST_EMPTY(&inodedep->id_bufwait) || 7870 !LIST_EMPTY(&inodedep->id_inowait) || 7871 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 7872 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 7873 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 7874 !TAILQ_EMPTY(&inodedep->id_extupdt) || 7875 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 7876 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 7877 inodedep->id_mkdiradd != NULL || 7878 inodedep->id_nlinkdelta != 0) 7879 return (0); 7880 /* 7881 * Another process might be in initiate_write_inodeblock_ufs[12] 7882 * trying to allocate memory without holding "Softdep Lock". 7883 */ 7884 if ((inodedep->id_state & IOSTARTED) != 0 && 7885 inodedep->id_savedino1 == NULL) 7886 return (0); 7887 7888 if (inodedep->id_state & ONDEPLIST) 7889 LIST_REMOVE(inodedep, id_deps); 7890 inodedep->id_state &= ~ONDEPLIST; 7891 inodedep->id_state |= ALLCOMPLETE; 7892 inodedep->id_bmsafemap = NULL; 7893 if (inodedep->id_state & ONWORKLIST) 7894 WORKLIST_REMOVE(&inodedep->id_list); 7895 if (inodedep->id_savedino1 != NULL) { 7896 free(inodedep->id_savedino1, M_SAVEDINO); 7897 inodedep->id_savedino1 = NULL; 7898 } 7899 if (free_inodedep(inodedep) == 0) 7900 panic("check_inode_unwritten: busy inode"); 7901 return (1); 7902 } 7903 7904 static int 7905 check_inodedep_free(struct inodedep *inodedep) 7906 { 7907 7908 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7909 if ((inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE || 7910 !LIST_EMPTY(&inodedep->id_dirremhd) || 7911 !LIST_EMPTY(&inodedep->id_pendinghd) || 7912 !LIST_EMPTY(&inodedep->id_bufwait) || 7913 !LIST_EMPTY(&inodedep->id_inowait) || 7914 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 7915 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 7916 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 7917 !TAILQ_EMPTY(&inodedep->id_extupdt) || 7918 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 7919 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 7920 inodedep->id_mkdiradd != NULL || 7921 inodedep->id_nlinkdelta != 0 || 7922 inodedep->id_savedino1 != NULL) 7923 return (0); 7924 return (1); 7925 } 7926 7927 /* 7928 * Try to free an inodedep structure. Return 1 if it could be freed. 7929 */ 7930 static int 7931 free_inodedep(struct inodedep *inodedep) 7932 { 7933 7934 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7935 if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 || 7936 !check_inodedep_free(inodedep)) 7937 return (0); 7938 if (inodedep->id_state & ONDEPLIST) 7939 LIST_REMOVE(inodedep, id_deps); 7940 LIST_REMOVE(inodedep, id_hash); 7941 WORKITEM_FREE(inodedep, D_INODEDEP); 7942 return (1); 7943 } 7944 7945 /* 7946 * Free the block referenced by a freework structure. The parent freeblks 7947 * structure is released and completed when the final cg bitmap reaches 7948 * the disk. This routine may be freeing a jnewblk which never made it to 7949 * disk in which case we do not have to wait as the operation is undone 7950 * in memory immediately. 7951 */ 7952 static void 7953 freework_freeblock(struct freework *freework, uint64_t key) 7954 { 7955 struct freeblks *freeblks; 7956 struct jnewblk *jnewblk; 7957 struct ufsmount *ump; 7958 struct workhead wkhd; 7959 struct fs *fs; 7960 int bsize; 7961 int needj; 7962 7963 ump = VFSTOUFS(freework->fw_list.wk_mp); 7964 LOCK_OWNED(ump); 7965 /* 7966 * Handle partial truncate separately. 7967 */ 7968 if (freework->fw_indir) { 7969 complete_trunc_indir(freework); 7970 return; 7971 } 7972 freeblks = freework->fw_freeblks; 7973 fs = ump->um_fs; 7974 needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0; 7975 bsize = lfragtosize(fs, freework->fw_frags); 7976 LIST_INIT(&wkhd); 7977 /* 7978 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives 7979 * on the indirblk hashtable and prevents premature freeing. 7980 */ 7981 freework->fw_state |= DEPCOMPLETE; 7982 /* 7983 * SUJ needs to wait for the segment referencing freed indirect 7984 * blocks to expire so that we know the checker will not confuse 7985 * a re-allocated indirect block with its old contents. 7986 */ 7987 if (needj && freework->fw_lbn <= -UFS_NDADDR) 7988 indirblk_insert(freework); 7989 /* 7990 * If we are canceling an existing jnewblk pass it to the free 7991 * routine, otherwise pass the freeblk which will ultimately 7992 * release the freeblks. If we're not journaling, we can just 7993 * free the freeblks immediately. 7994 */ 7995 jnewblk = freework->fw_jnewblk; 7996 if (jnewblk != NULL) { 7997 cancel_jnewblk(jnewblk, &wkhd); 7998 needj = 0; 7999 } else if (needj) { 8000 freework->fw_state |= DELAYEDFREE; 8001 freeblks->fb_cgwait++; 8002 WORKLIST_INSERT(&wkhd, &freework->fw_list); 8003 } 8004 FREE_LOCK(ump); 8005 freeblks_free(ump, freeblks, btodb(bsize)); 8006 CTR4(KTR_SUJ, 8007 "freework_freeblock: ino %jd blkno %jd lbn %jd size %d", 8008 freeblks->fb_inum, freework->fw_blkno, freework->fw_lbn, bsize); 8009 ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, bsize, 8010 freeblks->fb_inum, freeblks->fb_vtype, &wkhd, key); 8011 ACQUIRE_LOCK(ump); 8012 /* 8013 * The jnewblk will be discarded and the bits in the map never 8014 * made it to disk. We can immediately free the freeblk. 8015 */ 8016 if (needj == 0) 8017 handle_written_freework(freework); 8018 } 8019 8020 /* 8021 * We enqueue freework items that need processing back on the freeblks and 8022 * add the freeblks to the worklist. This makes it easier to find all work 8023 * required to flush a truncation in process_truncates(). 8024 */ 8025 static void 8026 freework_enqueue(struct freework *freework) 8027 { 8028 struct freeblks *freeblks; 8029 8030 freeblks = freework->fw_freeblks; 8031 if ((freework->fw_state & INPROGRESS) == 0) 8032 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 8033 if ((freeblks->fb_state & 8034 (ONWORKLIST | INPROGRESS | ALLCOMPLETE)) == ALLCOMPLETE && 8035 LIST_EMPTY(&freeblks->fb_jblkdephd)) 8036 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 8037 } 8038 8039 /* 8040 * Start, continue, or finish the process of freeing an indirect block tree. 8041 * The free operation may be paused at any point with fw_off containing the 8042 * offset to restart from. This enables us to implement some flow control 8043 * for large truncates which may fan out and generate a huge number of 8044 * dependencies. 8045 */ 8046 static void 8047 handle_workitem_indirblk(struct freework *freework) 8048 { 8049 struct freeblks *freeblks; 8050 struct ufsmount *ump; 8051 struct fs *fs; 8052 8053 freeblks = freework->fw_freeblks; 8054 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8055 fs = ump->um_fs; 8056 if (freework->fw_state & DEPCOMPLETE) { 8057 handle_written_freework(freework); 8058 return; 8059 } 8060 if (freework->fw_off == NINDIR(fs)) { 8061 freework_freeblock(freework, SINGLETON_KEY); 8062 return; 8063 } 8064 freework->fw_state |= INPROGRESS; 8065 FREE_LOCK(ump); 8066 indir_trunc(freework, fsbtodb(fs, freework->fw_blkno), 8067 freework->fw_lbn); 8068 ACQUIRE_LOCK(ump); 8069 } 8070 8071 /* 8072 * Called when a freework structure attached to a cg buf is written. The 8073 * ref on either the parent or the freeblks structure is released and 8074 * the freeblks is added back to the worklist if there is more work to do. 8075 */ 8076 static void 8077 handle_written_freework(struct freework *freework) 8078 { 8079 struct freeblks *freeblks; 8080 struct freework *parent; 8081 8082 freeblks = freework->fw_freeblks; 8083 parent = freework->fw_parent; 8084 if (freework->fw_state & DELAYEDFREE) 8085 freeblks->fb_cgwait--; 8086 freework->fw_state |= COMPLETE; 8087 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 8088 WORKITEM_FREE(freework, D_FREEWORK); 8089 if (parent) { 8090 if (--parent->fw_ref == 0) 8091 freework_enqueue(parent); 8092 return; 8093 } 8094 if (--freeblks->fb_ref != 0) 8095 return; 8096 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) == 8097 ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd)) 8098 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 8099 } 8100 8101 /* 8102 * This workitem routine performs the block de-allocation. 8103 * The workitem is added to the pending list after the updated 8104 * inode block has been written to disk. As mentioned above, 8105 * checks regarding the number of blocks de-allocated (compared 8106 * to the number of blocks allocated for the file) are also 8107 * performed in this function. 8108 */ 8109 static int 8110 handle_workitem_freeblocks(struct freeblks *freeblks, int flags) 8111 { 8112 struct freework *freework; 8113 struct newblk *newblk; 8114 struct allocindir *aip; 8115 struct ufsmount *ump; 8116 struct worklist *wk; 8117 uint64_t key; 8118 8119 KASSERT(LIST_EMPTY(&freeblks->fb_jblkdephd), 8120 ("handle_workitem_freeblocks: Journal entries not written.")); 8121 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8122 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum); 8123 ACQUIRE_LOCK(ump); 8124 while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) { 8125 WORKLIST_REMOVE(wk); 8126 switch (wk->wk_type) { 8127 case D_DIRREM: 8128 wk->wk_state |= COMPLETE; 8129 add_to_worklist(wk, 0); 8130 continue; 8131 8132 case D_ALLOCDIRECT: 8133 free_newblk(WK_NEWBLK(wk)); 8134 continue; 8135 8136 case D_ALLOCINDIR: 8137 aip = WK_ALLOCINDIR(wk); 8138 freework = NULL; 8139 if (aip->ai_state & DELAYEDFREE) { 8140 FREE_LOCK(ump); 8141 freework = newfreework(ump, freeblks, NULL, 8142 aip->ai_lbn, aip->ai_newblkno, 8143 ump->um_fs->fs_frag, 0, 0); 8144 ACQUIRE_LOCK(ump); 8145 } 8146 newblk = WK_NEWBLK(wk); 8147 if (newblk->nb_jnewblk) { 8148 freework->fw_jnewblk = newblk->nb_jnewblk; 8149 newblk->nb_jnewblk->jn_dep = &freework->fw_list; 8150 newblk->nb_jnewblk = NULL; 8151 } 8152 free_newblk(newblk); 8153 continue; 8154 8155 case D_FREEWORK: 8156 freework = WK_FREEWORK(wk); 8157 if (freework->fw_lbn <= -UFS_NDADDR) 8158 handle_workitem_indirblk(freework); 8159 else 8160 freework_freeblock(freework, key); 8161 continue; 8162 default: 8163 panic("handle_workitem_freeblocks: Unknown type %s", 8164 TYPENAME(wk->wk_type)); 8165 } 8166 } 8167 if (freeblks->fb_ref != 0) { 8168 freeblks->fb_state &= ~INPROGRESS; 8169 wake_worklist(&freeblks->fb_list); 8170 freeblks = NULL; 8171 } 8172 FREE_LOCK(ump); 8173 ffs_blkrelease_finish(ump, key); 8174 if (freeblks) 8175 return handle_complete_freeblocks(freeblks, flags); 8176 return (0); 8177 } 8178 8179 /* 8180 * Handle completion of block free via truncate. This allows fs_pending 8181 * to track the actual free block count more closely than if we only updated 8182 * it at the end. We must be careful to handle cases where the block count 8183 * on free was incorrect. 8184 */ 8185 static void 8186 freeblks_free(struct ufsmount *ump, 8187 struct freeblks *freeblks, 8188 int blocks) 8189 { 8190 struct fs *fs; 8191 ufs2_daddr_t remain; 8192 8193 UFS_LOCK(ump); 8194 remain = -freeblks->fb_chkcnt; 8195 freeblks->fb_chkcnt += blocks; 8196 if (remain > 0) { 8197 if (remain < blocks) 8198 blocks = remain; 8199 fs = ump->um_fs; 8200 fs->fs_pendingblocks -= blocks; 8201 } 8202 UFS_UNLOCK(ump); 8203 } 8204 8205 /* 8206 * Once all of the freework workitems are complete we can retire the 8207 * freeblocks dependency and any journal work awaiting completion. This 8208 * can not be called until all other dependencies are stable on disk. 8209 */ 8210 static int 8211 handle_complete_freeblocks(struct freeblks *freeblks, int flags) 8212 { 8213 struct inodedep *inodedep; 8214 struct inode *ip; 8215 struct vnode *vp; 8216 struct fs *fs; 8217 struct ufsmount *ump; 8218 ufs2_daddr_t spare; 8219 8220 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8221 fs = ump->um_fs; 8222 flags = LK_EXCLUSIVE | flags; 8223 spare = freeblks->fb_chkcnt; 8224 8225 /* 8226 * If we did not release the expected number of blocks we may have 8227 * to adjust the inode block count here. Only do so if it wasn't 8228 * a truncation to zero and the modrev still matches. 8229 */ 8230 if (spare && freeblks->fb_len != 0) { 8231 if (ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_inum, 8232 flags, &vp, FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP) != 0) 8233 return (EBUSY); 8234 ip = VTOI(vp); 8235 if (ip->i_mode == 0) { 8236 vgone(vp); 8237 } else if (DIP(ip, i_modrev) == freeblks->fb_modrev) { 8238 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare); 8239 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 8240 /* 8241 * We must wait so this happens before the 8242 * journal is reclaimed. 8243 */ 8244 ffs_update(vp, 1); 8245 } 8246 vput(vp); 8247 } 8248 if (spare < 0) { 8249 UFS_LOCK(ump); 8250 fs->fs_pendingblocks += spare; 8251 UFS_UNLOCK(ump); 8252 } 8253 #ifdef QUOTA 8254 /* Handle spare. */ 8255 if (spare) 8256 quotaadj(freeblks->fb_quota, ump, -spare); 8257 quotarele(freeblks->fb_quota); 8258 #endif 8259 ACQUIRE_LOCK(ump); 8260 if (freeblks->fb_state & ONDEPLIST) { 8261 inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum, 8262 0, &inodedep); 8263 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next); 8264 freeblks->fb_state &= ~ONDEPLIST; 8265 if (TAILQ_EMPTY(&inodedep->id_freeblklst)) 8266 free_inodedep(inodedep); 8267 } 8268 /* 8269 * All of the freeblock deps must be complete prior to this call 8270 * so it's now safe to complete earlier outstanding journal entries. 8271 */ 8272 handle_jwork(&freeblks->fb_jwork); 8273 WORKITEM_FREE(freeblks, D_FREEBLKS); 8274 FREE_LOCK(ump); 8275 return (0); 8276 } 8277 8278 /* 8279 * Release blocks associated with the freeblks and stored in the indirect 8280 * block dbn. If level is greater than SINGLE, the block is an indirect block 8281 * and recursive calls to indirtrunc must be used to cleanse other indirect 8282 * blocks. 8283 * 8284 * This handles partial and complete truncation of blocks. Partial is noted 8285 * with goingaway == 0. In this case the freework is completed after the 8286 * zero'd indirects are written to disk. For full truncation the freework 8287 * is completed after the block is freed. 8288 */ 8289 static void 8290 indir_trunc(struct freework *freework, 8291 ufs2_daddr_t dbn, 8292 ufs_lbn_t lbn) 8293 { 8294 struct freework *nfreework; 8295 struct workhead wkhd; 8296 struct freeblks *freeblks; 8297 struct buf *bp; 8298 struct fs *fs; 8299 struct indirdep *indirdep; 8300 struct mount *mp; 8301 struct ufsmount *ump; 8302 ufs1_daddr_t *bap1; 8303 ufs2_daddr_t nb, nnb, *bap2; 8304 ufs_lbn_t lbnadd, nlbn; 8305 uint64_t key; 8306 int nblocks, ufs1fmt, freedblocks; 8307 int goingaway, freedeps, needj, level, cnt, i, error; 8308 8309 freeblks = freework->fw_freeblks; 8310 mp = freeblks->fb_list.wk_mp; 8311 ump = VFSTOUFS(mp); 8312 fs = ump->um_fs; 8313 /* 8314 * Get buffer of block pointers to be freed. There are three cases: 8315 * 8316 * 1) Partial truncate caches the indirdep pointer in the freework 8317 * which provides us a back copy to the save bp which holds the 8318 * pointers we want to clear. When this completes the zero 8319 * pointers are written to the real copy. 8320 * 2) The indirect is being completely truncated, cancel_indirdep() 8321 * eliminated the real copy and placed the indirdep on the saved 8322 * copy. The indirdep and buf are discarded when this completes. 8323 * 3) The indirect was not in memory, we read a copy off of the disk 8324 * using the devvp and drop and invalidate the buffer when we're 8325 * done. 8326 */ 8327 goingaway = 1; 8328 indirdep = NULL; 8329 if (freework->fw_indir != NULL) { 8330 goingaway = 0; 8331 indirdep = freework->fw_indir; 8332 bp = indirdep->ir_savebp; 8333 if (bp == NULL || bp->b_blkno != dbn) 8334 panic("indir_trunc: Bad saved buf %p blkno %jd", 8335 bp, (intmax_t)dbn); 8336 } else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) { 8337 /* 8338 * The lock prevents the buf dep list from changing and 8339 * indirects on devvp should only ever have one dependency. 8340 */ 8341 indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep)); 8342 if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0) 8343 panic("indir_trunc: Bad indirdep %p from buf %p", 8344 indirdep, bp); 8345 } else { 8346 error = ffs_breadz(ump, freeblks->fb_devvp, dbn, dbn, 8347 (int)fs->fs_bsize, NULL, NULL, 0, NOCRED, 0, NULL, &bp); 8348 if (error) 8349 return; 8350 } 8351 ACQUIRE_LOCK(ump); 8352 /* Protects against a race with complete_trunc_indir(). */ 8353 freework->fw_state &= ~INPROGRESS; 8354 /* 8355 * If we have an indirdep we need to enforce the truncation order 8356 * and discard it when it is complete. 8357 */ 8358 if (indirdep) { 8359 if (freework != TAILQ_FIRST(&indirdep->ir_trunc) && 8360 !TAILQ_EMPTY(&indirdep->ir_trunc)) { 8361 /* 8362 * Add the complete truncate to the list on the 8363 * indirdep to enforce in-order processing. 8364 */ 8365 if (freework->fw_indir == NULL) 8366 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, 8367 freework, fw_next); 8368 FREE_LOCK(ump); 8369 return; 8370 } 8371 /* 8372 * If we're goingaway, free the indirdep. Otherwise it will 8373 * linger until the write completes. 8374 */ 8375 if (goingaway) { 8376 KASSERT(indirdep->ir_savebp == bp, 8377 ("indir_trunc: losing ir_savebp %p", 8378 indirdep->ir_savebp)); 8379 indirdep->ir_savebp = NULL; 8380 free_indirdep(indirdep); 8381 } 8382 } 8383 FREE_LOCK(ump); 8384 /* Initialize pointers depending on block size. */ 8385 if (ump->um_fstype == UFS1) { 8386 bap1 = (ufs1_daddr_t *)bp->b_data; 8387 nb = bap1[freework->fw_off]; 8388 ufs1fmt = 1; 8389 bap2 = NULL; 8390 } else { 8391 bap2 = (ufs2_daddr_t *)bp->b_data; 8392 nb = bap2[freework->fw_off]; 8393 ufs1fmt = 0; 8394 bap1 = NULL; 8395 } 8396 level = lbn_level(lbn); 8397 needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0; 8398 lbnadd = lbn_offset(fs, level); 8399 nblocks = btodb(fs->fs_bsize); 8400 nfreework = freework; 8401 freedeps = 0; 8402 cnt = 0; 8403 /* 8404 * Reclaim blocks. Traverses into nested indirect levels and 8405 * arranges for the current level to be freed when subordinates 8406 * are free when journaling. 8407 */ 8408 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum); 8409 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) { 8410 if (UFS_CHECK_BLKNO(mp, freeblks->fb_inum, nb, 8411 fs->fs_bsize) != 0) 8412 nb = 0; 8413 if (i != NINDIR(fs) - 1) { 8414 if (ufs1fmt) 8415 nnb = bap1[i+1]; 8416 else 8417 nnb = bap2[i+1]; 8418 } else 8419 nnb = 0; 8420 if (nb == 0) 8421 continue; 8422 cnt++; 8423 if (level != 0) { 8424 nlbn = (lbn + 1) - (i * lbnadd); 8425 if (needj != 0) { 8426 nfreework = newfreework(ump, freeblks, freework, 8427 nlbn, nb, fs->fs_frag, 0, 0); 8428 freedeps++; 8429 } 8430 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn); 8431 } else { 8432 struct freedep *freedep; 8433 8434 /* 8435 * Attempt to aggregate freedep dependencies for 8436 * all blocks being released to the same CG. 8437 */ 8438 LIST_INIT(&wkhd); 8439 if (needj != 0 && 8440 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) { 8441 freedep = newfreedep(freework); 8442 WORKLIST_INSERT_UNLOCKED(&wkhd, 8443 &freedep->fd_list); 8444 freedeps++; 8445 } 8446 CTR3(KTR_SUJ, 8447 "indir_trunc: ino %jd blkno %jd size %d", 8448 freeblks->fb_inum, nb, fs->fs_bsize); 8449 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb, 8450 fs->fs_bsize, freeblks->fb_inum, 8451 freeblks->fb_vtype, &wkhd, key); 8452 } 8453 } 8454 ffs_blkrelease_finish(ump, key); 8455 if (goingaway) { 8456 bp->b_flags |= B_INVAL | B_NOCACHE; 8457 brelse(bp); 8458 } 8459 freedblocks = 0; 8460 if (level == 0) 8461 freedblocks = (nblocks * cnt); 8462 if (needj == 0) 8463 freedblocks += nblocks; 8464 freeblks_free(ump, freeblks, freedblocks); 8465 /* 8466 * If we are journaling set up the ref counts and offset so this 8467 * indirect can be completed when its children are free. 8468 */ 8469 if (needj) { 8470 ACQUIRE_LOCK(ump); 8471 freework->fw_off = i; 8472 freework->fw_ref += freedeps; 8473 freework->fw_ref -= NINDIR(fs) + 1; 8474 if (level == 0) 8475 freeblks->fb_cgwait += freedeps; 8476 if (freework->fw_ref == 0) 8477 freework_freeblock(freework, SINGLETON_KEY); 8478 FREE_LOCK(ump); 8479 return; 8480 } 8481 /* 8482 * If we're not journaling we can free the indirect now. 8483 */ 8484 dbn = dbtofsb(fs, dbn); 8485 CTR3(KTR_SUJ, 8486 "indir_trunc 2: ino %jd blkno %jd size %d", 8487 freeblks->fb_inum, dbn, fs->fs_bsize); 8488 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize, 8489 freeblks->fb_inum, freeblks->fb_vtype, NULL, SINGLETON_KEY); 8490 /* Non SUJ softdep does single-threaded truncations. */ 8491 if (freework->fw_blkno == dbn) { 8492 freework->fw_state |= ALLCOMPLETE; 8493 ACQUIRE_LOCK(ump); 8494 handle_written_freework(freework); 8495 FREE_LOCK(ump); 8496 } 8497 return; 8498 } 8499 8500 /* 8501 * Cancel an allocindir when it is removed via truncation. When bp is not 8502 * NULL the indirect never appeared on disk and is scheduled to be freed 8503 * independently of the indir so we can more easily track journal work. 8504 */ 8505 static void 8506 cancel_allocindir( 8507 struct allocindir *aip, 8508 struct buf *bp, 8509 struct freeblks *freeblks, 8510 int trunc) 8511 { 8512 struct indirdep *indirdep; 8513 struct freefrag *freefrag; 8514 struct newblk *newblk; 8515 8516 newblk = (struct newblk *)aip; 8517 LIST_REMOVE(aip, ai_next); 8518 /* 8519 * We must eliminate the pointer in bp if it must be freed on its 8520 * own due to partial truncate or pending journal work. 8521 */ 8522 if (bp && (trunc || newblk->nb_jnewblk)) { 8523 /* 8524 * Clear the pointer and mark the aip to be freed 8525 * directly if it never existed on disk. 8526 */ 8527 aip->ai_state |= DELAYEDFREE; 8528 indirdep = aip->ai_indirdep; 8529 if (indirdep->ir_state & UFS1FMT) 8530 ((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8531 else 8532 ((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8533 } 8534 /* 8535 * When truncating the previous pointer will be freed via 8536 * savedbp. Eliminate the freefrag which would dup free. 8537 */ 8538 if (trunc && (freefrag = newblk->nb_freefrag) != NULL) { 8539 newblk->nb_freefrag = NULL; 8540 if (freefrag->ff_jdep) 8541 cancel_jfreefrag( 8542 WK_JFREEFRAG(freefrag->ff_jdep)); 8543 jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork); 8544 WORKITEM_FREE(freefrag, D_FREEFRAG); 8545 } 8546 /* 8547 * If the journal hasn't been written the jnewblk must be passed 8548 * to the call to ffs_blkfree that reclaims the space. We accomplish 8549 * this by leaving the journal dependency on the newblk to be freed 8550 * when a freework is created in handle_workitem_freeblocks(). 8551 */ 8552 cancel_newblk(newblk, NULL, &freeblks->fb_jwork); 8553 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 8554 } 8555 8556 /* 8557 * Create the mkdir dependencies for . and .. in a new directory. Link them 8558 * in to a newdirblk so any subsequent additions are tracked properly. The 8559 * caller is responsible for adding the mkdir1 dependency to the journal 8560 * and updating id_mkdiradd. This function returns with the per-filesystem 8561 * lock held. 8562 */ 8563 static struct mkdir * 8564 setup_newdir( 8565 struct diradd *dap, 8566 ino_t newinum, 8567 ino_t dinum, 8568 struct buf *newdirbp, 8569 struct mkdir **mkdirp) 8570 { 8571 struct newblk *newblk; 8572 struct pagedep *pagedep; 8573 struct inodedep *inodedep; 8574 struct newdirblk *newdirblk; 8575 struct mkdir *mkdir1, *mkdir2; 8576 struct worklist *wk; 8577 struct jaddref *jaddref; 8578 struct ufsmount *ump; 8579 struct mount *mp; 8580 8581 mp = dap->da_list.wk_mp; 8582 ump = VFSTOUFS(mp); 8583 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK, 8584 M_SOFTDEP_FLAGS); 8585 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8586 LIST_INIT(&newdirblk->db_mkdir); 8587 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8588 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp); 8589 mkdir1->md_state = ATTACHED | MKDIR_BODY; 8590 mkdir1->md_diradd = dap; 8591 mkdir1->md_jaddref = NULL; 8592 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8593 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp); 8594 mkdir2->md_state = ATTACHED | MKDIR_PARENT; 8595 mkdir2->md_diradd = dap; 8596 mkdir2->md_jaddref = NULL; 8597 if (MOUNTEDSUJ(mp) == 0) { 8598 mkdir1->md_state |= DEPCOMPLETE; 8599 mkdir2->md_state |= DEPCOMPLETE; 8600 } 8601 /* 8602 * Dependency on "." and ".." being written to disk. 8603 */ 8604 mkdir1->md_buf = newdirbp; 8605 ACQUIRE_LOCK(VFSTOUFS(mp)); 8606 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir1, md_mkdirs); 8607 /* 8608 * We must link the pagedep, allocdirect, and newdirblk for 8609 * the initial file page so the pointer to the new directory 8610 * is not written until the directory contents are live and 8611 * any subsequent additions are not marked live until the 8612 * block is reachable via the inode. 8613 */ 8614 if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0) 8615 panic("setup_newdir: lost pagedep"); 8616 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list) 8617 if (wk->wk_type == D_ALLOCDIRECT) 8618 break; 8619 if (wk == NULL) 8620 panic("setup_newdir: lost allocdirect"); 8621 if (pagedep->pd_state & NEWBLOCK) 8622 panic("setup_newdir: NEWBLOCK already set"); 8623 newblk = WK_NEWBLK(wk); 8624 pagedep->pd_state |= NEWBLOCK; 8625 pagedep->pd_newdirblk = newdirblk; 8626 newdirblk->db_pagedep = pagedep; 8627 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8628 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list); 8629 /* 8630 * Look up the inodedep for the parent directory so that we 8631 * can link mkdir2 into the pending dotdot jaddref or 8632 * the inode write if there is none. If the inode is 8633 * ALLCOMPLETE and no jaddref is present all dependencies have 8634 * been satisfied and mkdir2 can be freed. 8635 */ 8636 inodedep_lookup(mp, dinum, 0, &inodedep); 8637 if (MOUNTEDSUJ(mp)) { 8638 if (inodedep == NULL) 8639 panic("setup_newdir: Lost parent."); 8640 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8641 inoreflst); 8642 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum && 8643 (jaddref->ja_state & MKDIR_PARENT), 8644 ("setup_newdir: bad dotdot jaddref %p", jaddref)); 8645 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8646 mkdir2->md_jaddref = jaddref; 8647 jaddref->ja_mkdir = mkdir2; 8648 } else if (inodedep == NULL || 8649 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 8650 dap->da_state &= ~MKDIR_PARENT; 8651 WORKITEM_FREE(mkdir2, D_MKDIR); 8652 mkdir2 = NULL; 8653 } else { 8654 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8655 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list); 8656 } 8657 *mkdirp = mkdir2; 8658 8659 return (mkdir1); 8660 } 8661 8662 /* 8663 * Directory entry addition dependencies. 8664 * 8665 * When adding a new directory entry, the inode (with its incremented link 8666 * count) must be written to disk before the directory entry's pointer to it. 8667 * Also, if the inode is newly allocated, the corresponding freemap must be 8668 * updated (on disk) before the directory entry's pointer. These requirements 8669 * are met via undo/redo on the directory entry's pointer, which consists 8670 * simply of the inode number. 8671 * 8672 * As directory entries are added and deleted, the free space within a 8673 * directory block can become fragmented. The ufs filesystem will compact 8674 * a fragmented directory block to make space for a new entry. When this 8675 * occurs, the offsets of previously added entries change. Any "diradd" 8676 * dependency structures corresponding to these entries must be updated with 8677 * the new offsets. 8678 */ 8679 8680 /* 8681 * This routine is called after the in-memory inode's link 8682 * count has been incremented, but before the directory entry's 8683 * pointer to the inode has been set. 8684 */ 8685 int 8686 softdep_setup_directory_add( 8687 struct buf *bp, /* buffer containing directory block */ 8688 struct inode *dp, /* inode for directory */ 8689 off_t diroffset, /* offset of new entry in directory */ 8690 ino_t newinum, /* inode referenced by new directory entry */ 8691 struct buf *newdirbp, /* non-NULL => contents of new mkdir */ 8692 int isnewblk) /* entry is in a newly allocated block */ 8693 { 8694 int offset; /* offset of new entry within directory block */ 8695 ufs_lbn_t lbn; /* block in directory containing new entry */ 8696 struct fs *fs; 8697 struct diradd *dap; 8698 struct newblk *newblk; 8699 struct pagedep *pagedep; 8700 struct inodedep *inodedep; 8701 struct newdirblk *newdirblk; 8702 struct mkdir *mkdir1, *mkdir2; 8703 struct jaddref *jaddref; 8704 struct ufsmount *ump; 8705 struct mount *mp; 8706 int isindir; 8707 8708 mp = ITOVFS(dp); 8709 ump = VFSTOUFS(mp); 8710 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 8711 ("softdep_setup_directory_add called on non-softdep filesystem")); 8712 /* 8713 * Whiteouts have no dependencies. 8714 */ 8715 if (newinum == UFS_WINO) { 8716 if (newdirbp != NULL) 8717 bdwrite(newdirbp); 8718 return (0); 8719 } 8720 jaddref = NULL; 8721 mkdir1 = mkdir2 = NULL; 8722 fs = ump->um_fs; 8723 lbn = lblkno(fs, diroffset); 8724 offset = blkoff(fs, diroffset); 8725 dap = malloc(sizeof(struct diradd), M_DIRADD, 8726 M_SOFTDEP_FLAGS|M_ZERO); 8727 workitem_alloc(&dap->da_list, D_DIRADD, mp); 8728 dap->da_offset = offset; 8729 dap->da_newinum = newinum; 8730 dap->da_state = ATTACHED; 8731 LIST_INIT(&dap->da_jwork); 8732 isindir = bp->b_lblkno >= UFS_NDADDR; 8733 newdirblk = NULL; 8734 if (isnewblk && 8735 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) { 8736 newdirblk = malloc(sizeof(struct newdirblk), 8737 M_NEWDIRBLK, M_SOFTDEP_FLAGS); 8738 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8739 LIST_INIT(&newdirblk->db_mkdir); 8740 } 8741 /* 8742 * If we're creating a new directory setup the dependencies and set 8743 * the dap state to wait for them. Otherwise it's COMPLETE and 8744 * we can move on. 8745 */ 8746 if (newdirbp == NULL) { 8747 dap->da_state |= DEPCOMPLETE; 8748 ACQUIRE_LOCK(ump); 8749 } else { 8750 dap->da_state |= MKDIR_BODY | MKDIR_PARENT; 8751 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp, 8752 &mkdir2); 8753 } 8754 /* 8755 * Link into parent directory pagedep to await its being written. 8756 */ 8757 pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep); 8758 #ifdef INVARIANTS 8759 if (diradd_lookup(pagedep, offset) != NULL) 8760 panic("softdep_setup_directory_add: %p already at off %d\n", 8761 diradd_lookup(pagedep, offset), offset); 8762 #endif 8763 dap->da_pagedep = pagedep; 8764 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap, 8765 da_pdlist); 8766 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 8767 /* 8768 * If we're journaling, link the diradd into the jaddref so it 8769 * may be completed after the journal entry is written. Otherwise, 8770 * link the diradd into its inodedep. If the inode is not yet 8771 * written place it on the bufwait list, otherwise do the post-inode 8772 * write processing to put it on the id_pendinghd list. 8773 */ 8774 if (MOUNTEDSUJ(mp)) { 8775 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8776 inoreflst); 8777 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 8778 ("softdep_setup_directory_add: bad jaddref %p", jaddref)); 8779 jaddref->ja_diroff = diroffset; 8780 jaddref->ja_diradd = dap; 8781 add_to_journal(&jaddref->ja_list); 8782 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) 8783 diradd_inode_written(dap, inodedep); 8784 else 8785 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 8786 /* 8787 * Add the journal entries for . and .. links now that the primary 8788 * link is written. 8789 */ 8790 if (mkdir1 != NULL && MOUNTEDSUJ(mp)) { 8791 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 8792 inoreflst, if_deps); 8793 KASSERT(jaddref != NULL && 8794 jaddref->ja_ino == jaddref->ja_parent && 8795 (jaddref->ja_state & MKDIR_BODY), 8796 ("softdep_setup_directory_add: bad dot jaddref %p", 8797 jaddref)); 8798 mkdir1->md_jaddref = jaddref; 8799 jaddref->ja_mkdir = mkdir1; 8800 /* 8801 * It is important that the dotdot journal entry 8802 * is added prior to the dot entry since dot writes 8803 * both the dot and dotdot links. These both must 8804 * be added after the primary link for the journal 8805 * to remain consistent. 8806 */ 8807 add_to_journal(&mkdir2->md_jaddref->ja_list); 8808 add_to_journal(&jaddref->ja_list); 8809 } 8810 /* 8811 * If we are adding a new directory remember this diradd so that if 8812 * we rename it we can keep the dot and dotdot dependencies. If 8813 * we are adding a new name for an inode that has a mkdiradd we 8814 * must be in rename and we have to move the dot and dotdot 8815 * dependencies to this new name. The old name is being orphaned 8816 * soon. 8817 */ 8818 if (mkdir1 != NULL) { 8819 if (inodedep->id_mkdiradd != NULL) 8820 panic("softdep_setup_directory_add: Existing mkdir"); 8821 inodedep->id_mkdiradd = dap; 8822 } else if (inodedep->id_mkdiradd) 8823 merge_diradd(inodedep, dap); 8824 if (newdirblk != NULL) { 8825 /* 8826 * There is nothing to do if we are already tracking 8827 * this block. 8828 */ 8829 if ((pagedep->pd_state & NEWBLOCK) != 0) { 8830 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 8831 FREE_LOCK(ump); 8832 return (0); 8833 } 8834 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk) 8835 == 0) 8836 panic("softdep_setup_directory_add: lost entry"); 8837 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8838 pagedep->pd_state |= NEWBLOCK; 8839 pagedep->pd_newdirblk = newdirblk; 8840 newdirblk->db_pagedep = pagedep; 8841 FREE_LOCK(ump); 8842 /* 8843 * If we extended into an indirect signal direnter to sync. 8844 */ 8845 if (isindir) 8846 return (1); 8847 return (0); 8848 } 8849 FREE_LOCK(ump); 8850 return (0); 8851 } 8852 8853 /* 8854 * This procedure is called to change the offset of a directory 8855 * entry when compacting a directory block which must be owned 8856 * exclusively by the caller. Note that the actual entry movement 8857 * must be done in this procedure to ensure that no I/O completions 8858 * occur while the move is in progress. 8859 */ 8860 void 8861 softdep_change_directoryentry_offset( 8862 struct buf *bp, /* Buffer holding directory block. */ 8863 struct inode *dp, /* inode for directory */ 8864 caddr_t base, /* address of dp->i_offset */ 8865 caddr_t oldloc, /* address of old directory location */ 8866 caddr_t newloc, /* address of new directory location */ 8867 int entrysize) /* size of directory entry */ 8868 { 8869 int offset, oldoffset, newoffset; 8870 struct pagedep *pagedep; 8871 struct jmvref *jmvref; 8872 struct diradd *dap; 8873 struct direct *de; 8874 struct mount *mp; 8875 struct ufsmount *ump; 8876 ufs_lbn_t lbn; 8877 int flags; 8878 8879 mp = ITOVFS(dp); 8880 ump = VFSTOUFS(mp); 8881 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 8882 ("softdep_change_directoryentry_offset called on " 8883 "non-softdep filesystem")); 8884 de = (struct direct *)oldloc; 8885 jmvref = NULL; 8886 flags = 0; 8887 /* 8888 * Moves are always journaled as it would be too complex to 8889 * determine if any affected adds or removes are present in the 8890 * journal. 8891 */ 8892 if (MOUNTEDSUJ(mp)) { 8893 flags = DEPALLOC; 8894 jmvref = newjmvref(dp, de->d_ino, 8895 I_OFFSET(dp) + (oldloc - base), 8896 I_OFFSET(dp) + (newloc - base)); 8897 } 8898 lbn = lblkno(ump->um_fs, I_OFFSET(dp)); 8899 offset = blkoff(ump->um_fs, I_OFFSET(dp)); 8900 oldoffset = offset + (oldloc - base); 8901 newoffset = offset + (newloc - base); 8902 ACQUIRE_LOCK(ump); 8903 if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0) 8904 goto done; 8905 dap = diradd_lookup(pagedep, oldoffset); 8906 if (dap) { 8907 dap->da_offset = newoffset; 8908 newoffset = DIRADDHASH(newoffset); 8909 oldoffset = DIRADDHASH(oldoffset); 8910 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE && 8911 newoffset != oldoffset) { 8912 LIST_REMOVE(dap, da_pdlist); 8913 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset], 8914 dap, da_pdlist); 8915 } 8916 } 8917 done: 8918 if (jmvref) { 8919 jmvref->jm_pagedep = pagedep; 8920 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps); 8921 add_to_journal(&jmvref->jm_list); 8922 } 8923 bcopy(oldloc, newloc, entrysize); 8924 FREE_LOCK(ump); 8925 } 8926 8927 /* 8928 * Move the mkdir dependencies and journal work from one diradd to another 8929 * when renaming a directory. The new name must depend on the mkdir deps 8930 * completing as the old name did. Directories can only have one valid link 8931 * at a time so one must be canonical. 8932 */ 8933 static void 8934 merge_diradd(struct inodedep *inodedep, struct diradd *newdap) 8935 { 8936 struct diradd *olddap; 8937 struct mkdir *mkdir, *nextmd; 8938 struct ufsmount *ump; 8939 short state; 8940 8941 olddap = inodedep->id_mkdiradd; 8942 inodedep->id_mkdiradd = newdap; 8943 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8944 newdap->da_state &= ~DEPCOMPLETE; 8945 ump = VFSTOUFS(inodedep->id_list.wk_mp); 8946 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 8947 mkdir = nextmd) { 8948 nextmd = LIST_NEXT(mkdir, md_mkdirs); 8949 if (mkdir->md_diradd != olddap) 8950 continue; 8951 mkdir->md_diradd = newdap; 8952 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY); 8953 newdap->da_state |= state; 8954 olddap->da_state &= ~state; 8955 if ((olddap->da_state & 8956 (MKDIR_PARENT | MKDIR_BODY)) == 0) 8957 break; 8958 } 8959 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 8960 panic("merge_diradd: unfound ref"); 8961 } 8962 /* 8963 * Any mkdir related journal items are not safe to be freed until 8964 * the new name is stable. 8965 */ 8966 jwork_move(&newdap->da_jwork, &olddap->da_jwork); 8967 olddap->da_state |= DEPCOMPLETE; 8968 complete_diradd(olddap); 8969 } 8970 8971 /* 8972 * Move the diradd to the pending list when all diradd dependencies are 8973 * complete. 8974 */ 8975 static void 8976 complete_diradd(struct diradd *dap) 8977 { 8978 struct pagedep *pagedep; 8979 8980 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 8981 if (dap->da_state & DIRCHG) 8982 pagedep = dap->da_previous->dm_pagedep; 8983 else 8984 pagedep = dap->da_pagedep; 8985 LIST_REMOVE(dap, da_pdlist); 8986 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 8987 } 8988 } 8989 8990 /* 8991 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal 8992 * add entries and conditionally journal the remove. 8993 */ 8994 static void 8995 cancel_diradd( 8996 struct diradd *dap, 8997 struct dirrem *dirrem, 8998 struct jremref *jremref, 8999 struct jremref *dotremref, 9000 struct jremref *dotdotremref) 9001 { 9002 struct inodedep *inodedep; 9003 struct jaddref *jaddref; 9004 struct inoref *inoref; 9005 struct ufsmount *ump; 9006 struct mkdir *mkdir; 9007 9008 /* 9009 * If no remove references were allocated we're on a non-journaled 9010 * filesystem and can skip the cancel step. 9011 */ 9012 if (jremref == NULL) { 9013 free_diradd(dap, NULL); 9014 return; 9015 } 9016 /* 9017 * Cancel the primary name an free it if it does not require 9018 * journaling. 9019 */ 9020 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum, 9021 0, &inodedep) != 0) { 9022 /* Abort the addref that reference this diradd. */ 9023 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 9024 if (inoref->if_list.wk_type != D_JADDREF) 9025 continue; 9026 jaddref = (struct jaddref *)inoref; 9027 if (jaddref->ja_diradd != dap) 9028 continue; 9029 if (cancel_jaddref(jaddref, inodedep, 9030 &dirrem->dm_jwork) == 0) { 9031 free_jremref(jremref); 9032 jremref = NULL; 9033 } 9034 break; 9035 } 9036 } 9037 /* 9038 * Cancel subordinate names and free them if they do not require 9039 * journaling. 9040 */ 9041 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 9042 ump = VFSTOUFS(dap->da_list.wk_mp); 9043 LIST_FOREACH(mkdir, &ump->softdep_mkdirlisthd, md_mkdirs) { 9044 if (mkdir->md_diradd != dap) 9045 continue; 9046 if ((jaddref = mkdir->md_jaddref) == NULL) 9047 continue; 9048 mkdir->md_jaddref = NULL; 9049 if (mkdir->md_state & MKDIR_PARENT) { 9050 if (cancel_jaddref(jaddref, NULL, 9051 &dirrem->dm_jwork) == 0) { 9052 free_jremref(dotdotremref); 9053 dotdotremref = NULL; 9054 } 9055 } else { 9056 if (cancel_jaddref(jaddref, inodedep, 9057 &dirrem->dm_jwork) == 0) { 9058 free_jremref(dotremref); 9059 dotremref = NULL; 9060 } 9061 } 9062 } 9063 } 9064 9065 if (jremref) 9066 journal_jremref(dirrem, jremref, inodedep); 9067 if (dotremref) 9068 journal_jremref(dirrem, dotremref, inodedep); 9069 if (dotdotremref) 9070 journal_jremref(dirrem, dotdotremref, NULL); 9071 jwork_move(&dirrem->dm_jwork, &dap->da_jwork); 9072 free_diradd(dap, &dirrem->dm_jwork); 9073 } 9074 9075 /* 9076 * Free a diradd dependency structure. 9077 */ 9078 static void 9079 free_diradd(struct diradd *dap, struct workhead *wkhd) 9080 { 9081 struct dirrem *dirrem; 9082 struct pagedep *pagedep; 9083 struct inodedep *inodedep; 9084 struct mkdir *mkdir, *nextmd; 9085 struct ufsmount *ump; 9086 9087 ump = VFSTOUFS(dap->da_list.wk_mp); 9088 LOCK_OWNED(ump); 9089 LIST_REMOVE(dap, da_pdlist); 9090 if (dap->da_state & ONWORKLIST) 9091 WORKLIST_REMOVE(&dap->da_list); 9092 if ((dap->da_state & DIRCHG) == 0) { 9093 pagedep = dap->da_pagedep; 9094 } else { 9095 dirrem = dap->da_previous; 9096 pagedep = dirrem->dm_pagedep; 9097 dirrem->dm_dirinum = pagedep->pd_ino; 9098 dirrem->dm_state |= COMPLETE; 9099 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9100 add_to_worklist(&dirrem->dm_list, 0); 9101 } 9102 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum, 9103 0, &inodedep) != 0) 9104 if (inodedep->id_mkdiradd == dap) 9105 inodedep->id_mkdiradd = NULL; 9106 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 9107 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 9108 mkdir = nextmd) { 9109 nextmd = LIST_NEXT(mkdir, md_mkdirs); 9110 if (mkdir->md_diradd != dap) 9111 continue; 9112 dap->da_state &= 9113 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 9114 LIST_REMOVE(mkdir, md_mkdirs); 9115 if (mkdir->md_state & ONWORKLIST) 9116 WORKLIST_REMOVE(&mkdir->md_list); 9117 if (mkdir->md_jaddref != NULL) 9118 panic("free_diradd: Unexpected jaddref"); 9119 WORKITEM_FREE(mkdir, D_MKDIR); 9120 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) 9121 break; 9122 } 9123 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 9124 panic("free_diradd: unfound ref"); 9125 } 9126 if (inodedep) 9127 free_inodedep(inodedep); 9128 /* 9129 * Free any journal segments waiting for the directory write. 9130 */ 9131 handle_jwork(&dap->da_jwork); 9132 WORKITEM_FREE(dap, D_DIRADD); 9133 } 9134 9135 /* 9136 * Directory entry removal dependencies. 9137 * 9138 * When removing a directory entry, the entry's inode pointer must be 9139 * zero'ed on disk before the corresponding inode's link count is decremented 9140 * (possibly freeing the inode for re-use). This dependency is handled by 9141 * updating the directory entry but delaying the inode count reduction until 9142 * after the directory block has been written to disk. After this point, the 9143 * inode count can be decremented whenever it is convenient. 9144 */ 9145 9146 /* 9147 * This routine should be called immediately after removing 9148 * a directory entry. The inode's link count should not be 9149 * decremented by the calling procedure -- the soft updates 9150 * code will do this task when it is safe. 9151 */ 9152 void 9153 softdep_setup_remove( 9154 struct buf *bp, /* buffer containing directory block */ 9155 struct inode *dp, /* inode for the directory being modified */ 9156 struct inode *ip, /* inode for directory entry being removed */ 9157 int isrmdir) /* indicates if doing RMDIR */ 9158 { 9159 struct dirrem *dirrem, *prevdirrem; 9160 struct inodedep *inodedep; 9161 struct ufsmount *ump; 9162 int direct; 9163 9164 ump = ITOUMP(ip); 9165 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9166 ("softdep_setup_remove called on non-softdep filesystem")); 9167 /* 9168 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want 9169 * newdirrem() to setup the full directory remove which requires 9170 * isrmdir > 1. 9171 */ 9172 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 9173 /* 9174 * Add the dirrem to the inodedep's pending remove list for quick 9175 * discovery later. 9176 */ 9177 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) 9178 panic("softdep_setup_remove: Lost inodedep."); 9179 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 9180 dirrem->dm_state |= ONDEPLIST; 9181 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9182 9183 /* 9184 * If the COMPLETE flag is clear, then there were no active 9185 * entries and we want to roll back to a zeroed entry until 9186 * the new inode is committed to disk. If the COMPLETE flag is 9187 * set then we have deleted an entry that never made it to 9188 * disk. If the entry we deleted resulted from a name change, 9189 * then the old name still resides on disk. We cannot delete 9190 * its inode (returned to us in prevdirrem) until the zeroed 9191 * directory entry gets to disk. The new inode has never been 9192 * referenced on the disk, so can be deleted immediately. 9193 */ 9194 if ((dirrem->dm_state & COMPLETE) == 0) { 9195 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem, 9196 dm_next); 9197 FREE_LOCK(ump); 9198 } else { 9199 if (prevdirrem != NULL) 9200 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, 9201 prevdirrem, dm_next); 9202 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino; 9203 direct = LIST_EMPTY(&dirrem->dm_jremrefhd); 9204 FREE_LOCK(ump); 9205 if (direct) 9206 handle_workitem_remove(dirrem, 0); 9207 } 9208 } 9209 9210 /* 9211 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the 9212 * pd_pendinghd list of a pagedep. 9213 */ 9214 static struct diradd * 9215 diradd_lookup(struct pagedep *pagedep, int offset) 9216 { 9217 struct diradd *dap; 9218 9219 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist) 9220 if (dap->da_offset == offset) 9221 return (dap); 9222 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 9223 if (dap->da_offset == offset) 9224 return (dap); 9225 return (NULL); 9226 } 9227 9228 /* 9229 * Search for a .. diradd dependency in a directory that is being removed. 9230 * If the directory was renamed to a new parent we have a diradd rather 9231 * than a mkdir for the .. entry. We need to cancel it now before 9232 * it is found in truncate(). 9233 */ 9234 static struct jremref * 9235 cancel_diradd_dotdot(struct inode *ip, 9236 struct dirrem *dirrem, 9237 struct jremref *jremref) 9238 { 9239 struct pagedep *pagedep; 9240 struct diradd *dap; 9241 struct worklist *wk; 9242 9243 if (pagedep_lookup(ITOVFS(ip), NULL, ip->i_number, 0, 0, &pagedep) == 0) 9244 return (jremref); 9245 dap = diradd_lookup(pagedep, DOTDOT_OFFSET); 9246 if (dap == NULL) 9247 return (jremref); 9248 cancel_diradd(dap, dirrem, jremref, NULL, NULL); 9249 /* 9250 * Mark any journal work as belonging to the parent so it is freed 9251 * with the .. reference. 9252 */ 9253 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9254 wk->wk_state |= MKDIR_PARENT; 9255 return (NULL); 9256 } 9257 9258 /* 9259 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to 9260 * replace it with a dirrem/diradd pair as a result of re-parenting a 9261 * directory. This ensures that we don't simultaneously have a mkdir and 9262 * a diradd for the same .. entry. 9263 */ 9264 static struct jremref * 9265 cancel_mkdir_dotdot(struct inode *ip, 9266 struct dirrem *dirrem, 9267 struct jremref *jremref) 9268 { 9269 struct inodedep *inodedep; 9270 struct jaddref *jaddref; 9271 struct ufsmount *ump; 9272 struct mkdir *mkdir; 9273 struct diradd *dap; 9274 struct mount *mp; 9275 9276 mp = ITOVFS(ip); 9277 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9278 return (jremref); 9279 dap = inodedep->id_mkdiradd; 9280 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0) 9281 return (jremref); 9282 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9283 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 9284 mkdir = LIST_NEXT(mkdir, md_mkdirs)) 9285 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT) 9286 break; 9287 if (mkdir == NULL) 9288 panic("cancel_mkdir_dotdot: Unable to find mkdir\n"); 9289 if ((jaddref = mkdir->md_jaddref) != NULL) { 9290 mkdir->md_jaddref = NULL; 9291 jaddref->ja_state &= ~MKDIR_PARENT; 9292 if (inodedep_lookup(mp, jaddref->ja_ino, 0, &inodedep) == 0) 9293 panic("cancel_mkdir_dotdot: Lost parent inodedep"); 9294 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) { 9295 journal_jremref(dirrem, jremref, inodedep); 9296 jremref = NULL; 9297 } 9298 } 9299 if (mkdir->md_state & ONWORKLIST) 9300 WORKLIST_REMOVE(&mkdir->md_list); 9301 mkdir->md_state |= ALLCOMPLETE; 9302 complete_mkdir(mkdir); 9303 return (jremref); 9304 } 9305 9306 static void 9307 journal_jremref(struct dirrem *dirrem, 9308 struct jremref *jremref, 9309 struct inodedep *inodedep) 9310 { 9311 9312 if (inodedep == NULL) 9313 if (inodedep_lookup(jremref->jr_list.wk_mp, 9314 jremref->jr_ref.if_ino, 0, &inodedep) == 0) 9315 panic("journal_jremref: Lost inodedep"); 9316 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps); 9317 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 9318 add_to_journal(&jremref->jr_list); 9319 } 9320 9321 static void 9322 dirrem_journal( 9323 struct dirrem *dirrem, 9324 struct jremref *jremref, 9325 struct jremref *dotremref, 9326 struct jremref *dotdotremref) 9327 { 9328 struct inodedep *inodedep; 9329 9330 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0, 9331 &inodedep) == 0) 9332 panic("dirrem_journal: Lost inodedep"); 9333 journal_jremref(dirrem, jremref, inodedep); 9334 if (dotremref) 9335 journal_jremref(dirrem, dotremref, inodedep); 9336 if (dotdotremref) 9337 journal_jremref(dirrem, dotdotremref, NULL); 9338 } 9339 9340 /* 9341 * Allocate a new dirrem if appropriate and return it along with 9342 * its associated pagedep. Called without a lock, returns with lock. 9343 */ 9344 static struct dirrem * 9345 newdirrem( 9346 struct buf *bp, /* buffer containing directory block */ 9347 struct inode *dp, /* inode for the directory being modified */ 9348 struct inode *ip, /* inode for directory entry being removed */ 9349 int isrmdir, /* indicates if doing RMDIR */ 9350 struct dirrem **prevdirremp) /* previously referenced inode, if any */ 9351 { 9352 int offset; 9353 ufs_lbn_t lbn; 9354 struct diradd *dap; 9355 struct dirrem *dirrem; 9356 struct pagedep *pagedep; 9357 struct jremref *jremref; 9358 struct jremref *dotremref; 9359 struct jremref *dotdotremref; 9360 struct vnode *dvp; 9361 struct ufsmount *ump; 9362 9363 /* 9364 * Whiteouts have no deletion dependencies. 9365 */ 9366 if (ip == NULL) 9367 panic("newdirrem: whiteout"); 9368 dvp = ITOV(dp); 9369 ump = ITOUMP(dp); 9370 9371 /* 9372 * If the system is over its limit and our filesystem is 9373 * responsible for more than our share of that usage and 9374 * we are not a snapshot, request some inodedep cleanup. 9375 * Limiting the number of dirrem structures will also limit 9376 * the number of freefile and freeblks structures. 9377 */ 9378 ACQUIRE_LOCK(ump); 9379 if (!IS_SNAPSHOT(ip) && softdep_excess_items(ump, D_DIRREM)) 9380 schedule_cleanup(UFSTOVFS(ump)); 9381 else 9382 FREE_LOCK(ump); 9383 dirrem = malloc(sizeof(struct dirrem), M_DIRREM, M_SOFTDEP_FLAGS | 9384 M_ZERO); 9385 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount); 9386 LIST_INIT(&dirrem->dm_jremrefhd); 9387 LIST_INIT(&dirrem->dm_jwork); 9388 dirrem->dm_state = isrmdir ? RMDIR : 0; 9389 dirrem->dm_oldinum = ip->i_number; 9390 *prevdirremp = NULL; 9391 /* 9392 * Allocate remove reference structures to track journal write 9393 * dependencies. We will always have one for the link and 9394 * when doing directories we will always have one more for dot. 9395 * When renaming a directory we skip the dotdot link change so 9396 * this is not needed. 9397 */ 9398 jremref = dotremref = dotdotremref = NULL; 9399 if (DOINGSUJ(dvp)) { 9400 if (isrmdir) { 9401 jremref = newjremref(dirrem, dp, ip, I_OFFSET(dp), 9402 ip->i_effnlink + 2); 9403 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET, 9404 ip->i_effnlink + 1); 9405 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET, 9406 dp->i_effnlink + 1); 9407 dotdotremref->jr_state |= MKDIR_PARENT; 9408 } else 9409 jremref = newjremref(dirrem, dp, ip, I_OFFSET(dp), 9410 ip->i_effnlink + 1); 9411 } 9412 ACQUIRE_LOCK(ump); 9413 lbn = lblkno(ump->um_fs, I_OFFSET(dp)); 9414 offset = blkoff(ump->um_fs, I_OFFSET(dp)); 9415 pagedep_lookup(UFSTOVFS(ump), bp, dp->i_number, lbn, DEPALLOC, 9416 &pagedep); 9417 dirrem->dm_pagedep = pagedep; 9418 dirrem->dm_offset = offset; 9419 /* 9420 * If we're renaming a .. link to a new directory, cancel any 9421 * existing MKDIR_PARENT mkdir. If it has already been canceled 9422 * the jremref is preserved for any potential diradd in this 9423 * location. This can not coincide with a rmdir. 9424 */ 9425 if (I_OFFSET(dp) == DOTDOT_OFFSET) { 9426 if (isrmdir) 9427 panic("newdirrem: .. directory change during remove?"); 9428 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref); 9429 } 9430 /* 9431 * If we're removing a directory search for the .. dependency now and 9432 * cancel it. Any pending journal work will be added to the dirrem 9433 * to be completed when the workitem remove completes. 9434 */ 9435 if (isrmdir) 9436 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref); 9437 /* 9438 * Check for a diradd dependency for the same directory entry. 9439 * If present, then both dependencies become obsolete and can 9440 * be de-allocated. 9441 */ 9442 dap = diradd_lookup(pagedep, offset); 9443 if (dap == NULL) { 9444 /* 9445 * Link the jremref structures into the dirrem so they are 9446 * written prior to the pagedep. 9447 */ 9448 if (jremref) 9449 dirrem_journal(dirrem, jremref, dotremref, 9450 dotdotremref); 9451 return (dirrem); 9452 } 9453 /* 9454 * Must be ATTACHED at this point. 9455 */ 9456 if ((dap->da_state & ATTACHED) == 0) 9457 panic("newdirrem: not ATTACHED"); 9458 if (dap->da_newinum != ip->i_number) 9459 panic("newdirrem: inum %ju should be %ju", 9460 (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum); 9461 /* 9462 * If we are deleting a changed name that never made it to disk, 9463 * then return the dirrem describing the previous inode (which 9464 * represents the inode currently referenced from this entry on disk). 9465 */ 9466 if ((dap->da_state & DIRCHG) != 0) { 9467 *prevdirremp = dap->da_previous; 9468 dap->da_state &= ~DIRCHG; 9469 dap->da_pagedep = pagedep; 9470 } 9471 /* 9472 * We are deleting an entry that never made it to disk. 9473 * Mark it COMPLETE so we can delete its inode immediately. 9474 */ 9475 dirrem->dm_state |= COMPLETE; 9476 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref); 9477 #ifdef INVARIANTS 9478 if (isrmdir == 0) { 9479 struct worklist *wk; 9480 9481 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9482 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT)) 9483 panic("bad wk %p (0x%X)\n", wk, wk->wk_state); 9484 } 9485 #endif 9486 9487 return (dirrem); 9488 } 9489 9490 /* 9491 * Directory entry change dependencies. 9492 * 9493 * Changing an existing directory entry requires that an add operation 9494 * be completed first followed by a deletion. The semantics for the addition 9495 * are identical to the description of adding a new entry above except 9496 * that the rollback is to the old inode number rather than zero. Once 9497 * the addition dependency is completed, the removal is done as described 9498 * in the removal routine above. 9499 */ 9500 9501 /* 9502 * This routine should be called immediately after changing 9503 * a directory entry. The inode's link count should not be 9504 * decremented by the calling procedure -- the soft updates 9505 * code will perform this task when it is safe. 9506 */ 9507 void 9508 softdep_setup_directory_change( 9509 struct buf *bp, /* buffer containing directory block */ 9510 struct inode *dp, /* inode for the directory being modified */ 9511 struct inode *ip, /* inode for directory entry being removed */ 9512 ino_t newinum, /* new inode number for changed entry */ 9513 int isrmdir) /* indicates if doing RMDIR */ 9514 { 9515 int offset; 9516 struct diradd *dap = NULL; 9517 struct dirrem *dirrem, *prevdirrem; 9518 struct pagedep *pagedep; 9519 struct inodedep *inodedep; 9520 struct jaddref *jaddref; 9521 struct mount *mp; 9522 struct ufsmount *ump; 9523 9524 mp = ITOVFS(dp); 9525 ump = VFSTOUFS(mp); 9526 offset = blkoff(ump->um_fs, I_OFFSET(dp)); 9527 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 9528 ("softdep_setup_directory_change called on non-softdep filesystem")); 9529 9530 /* 9531 * Whiteouts do not need diradd dependencies. 9532 */ 9533 if (newinum != UFS_WINO) { 9534 dap = malloc(sizeof(struct diradd), 9535 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO); 9536 workitem_alloc(&dap->da_list, D_DIRADD, mp); 9537 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE; 9538 dap->da_offset = offset; 9539 dap->da_newinum = newinum; 9540 LIST_INIT(&dap->da_jwork); 9541 } 9542 9543 /* 9544 * Allocate a new dirrem and ACQUIRE_LOCK. 9545 */ 9546 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 9547 pagedep = dirrem->dm_pagedep; 9548 /* 9549 * The possible values for isrmdir: 9550 * 0 - non-directory file rename 9551 * 1 - directory rename within same directory 9552 * inum - directory rename to new directory of given inode number 9553 * When renaming to a new directory, we are both deleting and 9554 * creating a new directory entry, so the link count on the new 9555 * directory should not change. Thus we do not need the followup 9556 * dirrem which is usually done in handle_workitem_remove. We set 9557 * the DIRCHG flag to tell handle_workitem_remove to skip the 9558 * followup dirrem. 9559 */ 9560 if (isrmdir > 1) 9561 dirrem->dm_state |= DIRCHG; 9562 9563 /* 9564 * Whiteouts have no additional dependencies, 9565 * so just put the dirrem on the correct list. 9566 */ 9567 if (newinum == UFS_WINO) { 9568 if ((dirrem->dm_state & COMPLETE) == 0) { 9569 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem, 9570 dm_next); 9571 } else { 9572 dirrem->dm_dirinum = pagedep->pd_ino; 9573 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9574 add_to_worklist(&dirrem->dm_list, 0); 9575 } 9576 FREE_LOCK(ump); 9577 return; 9578 } 9579 /* 9580 * Add the dirrem to the inodedep's pending remove list for quick 9581 * discovery later. A valid nlinkdelta ensures that this lookup 9582 * will not fail. 9583 */ 9584 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9585 panic("softdep_setup_directory_change: Lost inodedep."); 9586 dirrem->dm_state |= ONDEPLIST; 9587 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9588 9589 /* 9590 * If the COMPLETE flag is clear, then there were no active 9591 * entries and we want to roll back to the previous inode until 9592 * the new inode is committed to disk. If the COMPLETE flag is 9593 * set, then we have deleted an entry that never made it to disk. 9594 * If the entry we deleted resulted from a name change, then the old 9595 * inode reference still resides on disk. Any rollback that we do 9596 * needs to be to that old inode (returned to us in prevdirrem). If 9597 * the entry we deleted resulted from a create, then there is 9598 * no entry on the disk, so we want to roll back to zero rather 9599 * than the uncommitted inode. In either of the COMPLETE cases we 9600 * want to immediately free the unwritten and unreferenced inode. 9601 */ 9602 if ((dirrem->dm_state & COMPLETE) == 0) { 9603 dap->da_previous = dirrem; 9604 } else { 9605 if (prevdirrem != NULL) { 9606 dap->da_previous = prevdirrem; 9607 } else { 9608 dap->da_state &= ~DIRCHG; 9609 dap->da_pagedep = pagedep; 9610 } 9611 dirrem->dm_dirinum = pagedep->pd_ino; 9612 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9613 add_to_worklist(&dirrem->dm_list, 0); 9614 } 9615 /* 9616 * Lookup the jaddref for this journal entry. We must finish 9617 * initializing it and make the diradd write dependent on it. 9618 * If we're not journaling, put it on the id_bufwait list if the 9619 * inode is not yet written. If it is written, do the post-inode 9620 * write processing to put it on the id_pendinghd list. 9621 */ 9622 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 9623 if (MOUNTEDSUJ(mp)) { 9624 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 9625 inoreflst); 9626 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 9627 ("softdep_setup_directory_change: bad jaddref %p", 9628 jaddref)); 9629 jaddref->ja_diroff = I_OFFSET(dp); 9630 jaddref->ja_diradd = dap; 9631 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9632 dap, da_pdlist); 9633 add_to_journal(&jaddref->ja_list); 9634 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 9635 dap->da_state |= COMPLETE; 9636 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 9637 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 9638 } else { 9639 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9640 dap, da_pdlist); 9641 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 9642 } 9643 /* 9644 * If we're making a new name for a directory that has not been 9645 * committed when need to move the dot and dotdot references to 9646 * this new name. 9647 */ 9648 if (inodedep->id_mkdiradd && I_OFFSET(dp) != DOTDOT_OFFSET) 9649 merge_diradd(inodedep, dap); 9650 FREE_LOCK(ump); 9651 } 9652 9653 /* 9654 * Called whenever the link count on an inode is changed. 9655 * It creates an inode dependency so that the new reference(s) 9656 * to the inode cannot be committed to disk until the updated 9657 * inode has been written. 9658 */ 9659 void 9660 softdep_change_linkcnt( 9661 struct inode *ip) /* the inode with the increased link count */ 9662 { 9663 struct inodedep *inodedep; 9664 struct ufsmount *ump; 9665 9666 ump = ITOUMP(ip); 9667 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9668 ("softdep_change_linkcnt called on non-softdep filesystem")); 9669 ACQUIRE_LOCK(ump); 9670 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep); 9671 if (ip->i_nlink < ip->i_effnlink) 9672 panic("softdep_change_linkcnt: bad delta"); 9673 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9674 FREE_LOCK(ump); 9675 } 9676 9677 /* 9678 * Attach a sbdep dependency to the superblock buf so that we can keep 9679 * track of the head of the linked list of referenced but unlinked inodes. 9680 */ 9681 void 9682 softdep_setup_sbupdate( 9683 struct ufsmount *ump, 9684 struct fs *fs, 9685 struct buf *bp) 9686 { 9687 struct sbdep *sbdep; 9688 struct worklist *wk; 9689 9690 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9691 ("softdep_setup_sbupdate called on non-softdep filesystem")); 9692 LIST_FOREACH(wk, &bp->b_dep, wk_list) 9693 if (wk->wk_type == D_SBDEP) 9694 break; 9695 if (wk != NULL) 9696 return; 9697 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS); 9698 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump)); 9699 sbdep->sb_fs = fs; 9700 sbdep->sb_ump = ump; 9701 ACQUIRE_LOCK(ump); 9702 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list); 9703 FREE_LOCK(ump); 9704 } 9705 9706 /* 9707 * Return the first unlinked inodedep which is ready to be the head of the 9708 * list. The inodedep and all those after it must have valid next pointers. 9709 */ 9710 static struct inodedep * 9711 first_unlinked_inodedep(struct ufsmount *ump) 9712 { 9713 struct inodedep *inodedep; 9714 struct inodedep *idp; 9715 9716 LOCK_OWNED(ump); 9717 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst); 9718 inodedep; inodedep = idp) { 9719 if ((inodedep->id_state & UNLINKNEXT) == 0) 9720 return (NULL); 9721 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9722 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0) 9723 break; 9724 if ((inodedep->id_state & UNLINKPREV) == 0) 9725 break; 9726 } 9727 return (inodedep); 9728 } 9729 9730 /* 9731 * Set the sujfree unlinked head pointer prior to writing a superblock. 9732 */ 9733 static void 9734 initiate_write_sbdep(struct sbdep *sbdep) 9735 { 9736 struct inodedep *inodedep; 9737 struct fs *bpfs; 9738 struct fs *fs; 9739 9740 bpfs = sbdep->sb_fs; 9741 fs = sbdep->sb_ump->um_fs; 9742 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9743 if (inodedep) { 9744 fs->fs_sujfree = inodedep->id_ino; 9745 inodedep->id_state |= UNLINKPREV; 9746 } else 9747 fs->fs_sujfree = 0; 9748 bpfs->fs_sujfree = fs->fs_sujfree; 9749 /* 9750 * Because we have made changes to the superblock, we need to 9751 * recompute its check-hash. 9752 */ 9753 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs); 9754 } 9755 9756 /* 9757 * After a superblock is written determine whether it must be written again 9758 * due to a changing unlinked list head. 9759 */ 9760 static int 9761 handle_written_sbdep(struct sbdep *sbdep, struct buf *bp) 9762 { 9763 struct inodedep *inodedep; 9764 struct fs *fs; 9765 9766 LOCK_OWNED(sbdep->sb_ump); 9767 fs = sbdep->sb_fs; 9768 /* 9769 * If the superblock doesn't match the in-memory list start over. 9770 */ 9771 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9772 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) || 9773 (inodedep == NULL && fs->fs_sujfree != 0)) { 9774 bdirty(bp); 9775 return (1); 9776 } 9777 WORKITEM_FREE(sbdep, D_SBDEP); 9778 if (fs->fs_sujfree == 0) 9779 return (0); 9780 /* 9781 * Now that we have a record of this inode in stable store allow it 9782 * to be written to free up pending work. Inodes may see a lot of 9783 * write activity after they are unlinked which we must not hold up. 9784 */ 9785 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 9786 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS) 9787 panic("handle_written_sbdep: Bad inodedep %p (0x%X)", 9788 inodedep, inodedep->id_state); 9789 if (inodedep->id_state & UNLINKONLIST) 9790 break; 9791 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST; 9792 } 9793 9794 return (0); 9795 } 9796 9797 /* 9798 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list. 9799 */ 9800 static void 9801 unlinked_inodedep( struct mount *mp, struct inodedep *inodedep) 9802 { 9803 struct ufsmount *ump; 9804 9805 ump = VFSTOUFS(mp); 9806 LOCK_OWNED(ump); 9807 if (MOUNTEDSUJ(mp) == 0) 9808 return; 9809 ump->um_fs->fs_fmod = 1; 9810 if (inodedep->id_state & UNLINKED) 9811 panic("unlinked_inodedep: %p already unlinked\n", inodedep); 9812 inodedep->id_state |= UNLINKED; 9813 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked); 9814 } 9815 9816 /* 9817 * Remove an inodedep from the unlinked inodedep list. This may require 9818 * disk writes if the inode has made it that far. 9819 */ 9820 static void 9821 clear_unlinked_inodedep( struct inodedep *inodedep) 9822 { 9823 struct ufs2_dinode *dip; 9824 struct ufsmount *ump; 9825 struct inodedep *idp; 9826 struct inodedep *idn; 9827 struct fs *fs, *bpfs; 9828 struct buf *bp; 9829 daddr_t dbn; 9830 ino_t ino; 9831 ino_t nino; 9832 ino_t pino; 9833 int error; 9834 9835 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9836 fs = ump->um_fs; 9837 ino = inodedep->id_ino; 9838 error = 0; 9839 for (;;) { 9840 LOCK_OWNED(ump); 9841 KASSERT((inodedep->id_state & UNLINKED) != 0, 9842 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9843 inodedep)); 9844 /* 9845 * If nothing has yet been written simply remove us from 9846 * the in memory list and return. This is the most common 9847 * case where handle_workitem_remove() loses the final 9848 * reference. 9849 */ 9850 if ((inodedep->id_state & UNLINKLINKS) == 0) 9851 break; 9852 /* 9853 * If we have a NEXT pointer and no PREV pointer we can simply 9854 * clear NEXT's PREV and remove ourselves from the list. Be 9855 * careful not to clear PREV if the superblock points at 9856 * next as well. 9857 */ 9858 idn = TAILQ_NEXT(inodedep, id_unlinked); 9859 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) { 9860 if (idn && fs->fs_sujfree != idn->id_ino) 9861 idn->id_state &= ~UNLINKPREV; 9862 break; 9863 } 9864 /* 9865 * Here we have an inodedep which is actually linked into 9866 * the list. We must remove it by forcing a write to the 9867 * link before us, whether it be the superblock or an inode. 9868 * Unfortunately the list may change while we're waiting 9869 * on the buf lock for either resource so we must loop until 9870 * we lock the right one. If both the superblock and an 9871 * inode point to this inode we must clear the inode first 9872 * followed by the superblock. 9873 */ 9874 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9875 pino = 0; 9876 if (idp && (idp->id_state & UNLINKNEXT)) 9877 pino = idp->id_ino; 9878 FREE_LOCK(ump); 9879 if (pino == 0) { 9880 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9881 (int)fs->fs_sbsize, 0, 0, 0); 9882 } else { 9883 dbn = fsbtodb(fs, ino_to_fsba(fs, pino)); 9884 error = ffs_breadz(ump, ump->um_devvp, dbn, dbn, 9885 (int)fs->fs_bsize, NULL, NULL, 0, NOCRED, 0, NULL, 9886 &bp); 9887 } 9888 ACQUIRE_LOCK(ump); 9889 if (error) 9890 break; 9891 /* If the list has changed restart the loop. */ 9892 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9893 nino = 0; 9894 if (idp && (idp->id_state & UNLINKNEXT)) 9895 nino = idp->id_ino; 9896 if (nino != pino || 9897 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) { 9898 FREE_LOCK(ump); 9899 brelse(bp); 9900 ACQUIRE_LOCK(ump); 9901 continue; 9902 } 9903 nino = 0; 9904 idn = TAILQ_NEXT(inodedep, id_unlinked); 9905 if (idn) 9906 nino = idn->id_ino; 9907 /* 9908 * Remove us from the in memory list. After this we cannot 9909 * access the inodedep. 9910 */ 9911 KASSERT((inodedep->id_state & UNLINKED) != 0, 9912 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9913 inodedep)); 9914 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9915 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9916 FREE_LOCK(ump); 9917 /* 9918 * The predecessor's next pointer is manually updated here 9919 * so that the NEXT flag is never cleared for an element 9920 * that is in the list. 9921 */ 9922 if (pino == 0) { 9923 bcopy((caddr_t)fs, bp->b_data, (uint64_t)fs->fs_sbsize); 9924 bpfs = (struct fs *)bp->b_data; 9925 ffs_oldfscompat_write(bpfs, ump); 9926 softdep_setup_sbupdate(ump, bpfs, bp); 9927 /* 9928 * Because we may have made changes to the superblock, 9929 * we need to recompute its check-hash. 9930 */ 9931 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs); 9932 } else if (fs->fs_magic == FS_UFS1_MAGIC) { 9933 ((struct ufs1_dinode *)bp->b_data + 9934 ino_to_fsbo(fs, pino))->di_freelink = nino; 9935 } else { 9936 dip = (struct ufs2_dinode *)bp->b_data + 9937 ino_to_fsbo(fs, pino); 9938 dip->di_freelink = nino; 9939 ffs_update_dinode_ckhash(fs, dip); 9940 } 9941 /* 9942 * If the bwrite fails we have no recourse to recover. The 9943 * filesystem is corrupted already. 9944 */ 9945 bwrite(bp); 9946 ACQUIRE_LOCK(ump); 9947 /* 9948 * If the superblock pointer still needs to be cleared force 9949 * a write here. 9950 */ 9951 if (fs->fs_sujfree == ino) { 9952 FREE_LOCK(ump); 9953 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9954 (int)fs->fs_sbsize, 0, 0, 0); 9955 bcopy((caddr_t)fs, bp->b_data, (uint64_t)fs->fs_sbsize); 9956 bpfs = (struct fs *)bp->b_data; 9957 ffs_oldfscompat_write(bpfs, ump); 9958 softdep_setup_sbupdate(ump, bpfs, bp); 9959 /* 9960 * Because we may have made changes to the superblock, 9961 * we need to recompute its check-hash. 9962 */ 9963 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs); 9964 bwrite(bp); 9965 ACQUIRE_LOCK(ump); 9966 } 9967 9968 if (fs->fs_sujfree != ino) 9969 return; 9970 panic("clear_unlinked_inodedep: Failed to clear free head"); 9971 } 9972 if (inodedep->id_ino == fs->fs_sujfree) 9973 panic("clear_unlinked_inodedep: Freeing head of free list"); 9974 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9975 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9976 return; 9977 } 9978 9979 /* 9980 * This workitem decrements the inode's link count. 9981 * If the link count reaches zero, the file is removed. 9982 */ 9983 static int 9984 handle_workitem_remove(struct dirrem *dirrem, int flags) 9985 { 9986 struct inodedep *inodedep; 9987 struct workhead dotdotwk; 9988 struct worklist *wk; 9989 struct ufsmount *ump; 9990 struct mount *mp; 9991 struct vnode *vp; 9992 struct inode *ip; 9993 ino_t oldinum; 9994 9995 if (dirrem->dm_state & ONWORKLIST) 9996 panic("handle_workitem_remove: dirrem %p still on worklist", 9997 dirrem); 9998 oldinum = dirrem->dm_oldinum; 9999 mp = dirrem->dm_list.wk_mp; 10000 ump = VFSTOUFS(mp); 10001 flags |= LK_EXCLUSIVE; 10002 if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ | 10003 FFSV_FORCEINODEDEP) != 0) 10004 return (EBUSY); 10005 ip = VTOI(vp); 10006 MPASS(ip->i_mode != 0); 10007 ACQUIRE_LOCK(ump); 10008 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0) 10009 panic("handle_workitem_remove: lost inodedep"); 10010 if (dirrem->dm_state & ONDEPLIST) 10011 LIST_REMOVE(dirrem, dm_inonext); 10012 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 10013 ("handle_workitem_remove: Journal entries not written.")); 10014 10015 /* 10016 * Move all dependencies waiting on the remove to complete 10017 * from the dirrem to the inode inowait list to be completed 10018 * after the inode has been updated and written to disk. 10019 * 10020 * Any marked MKDIR_PARENT are saved to be completed when the 10021 * dotdot ref is removed unless DIRCHG is specified. For 10022 * directory change operations there will be no further 10023 * directory writes and the jsegdeps need to be moved along 10024 * with the rest to be completed when the inode is free or 10025 * stable in the inode free list. 10026 */ 10027 LIST_INIT(&dotdotwk); 10028 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) { 10029 WORKLIST_REMOVE(wk); 10030 if ((dirrem->dm_state & DIRCHG) == 0 && 10031 wk->wk_state & MKDIR_PARENT) { 10032 wk->wk_state &= ~MKDIR_PARENT; 10033 WORKLIST_INSERT(&dotdotwk, wk); 10034 continue; 10035 } 10036 WORKLIST_INSERT(&inodedep->id_inowait, wk); 10037 } 10038 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list); 10039 /* 10040 * Normal file deletion. 10041 */ 10042 if ((dirrem->dm_state & RMDIR) == 0) { 10043 ip->i_nlink--; 10044 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: file ino " 10045 "%ju negative i_nlink %d", (intmax_t)ip->i_number, 10046 ip->i_nlink)); 10047 DIP_SET_NLINK(ip, ip->i_nlink); 10048 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 10049 if (ip->i_nlink < ip->i_effnlink) 10050 panic("handle_workitem_remove: bad file delta"); 10051 if (ip->i_nlink == 0) 10052 unlinked_inodedep(mp, inodedep); 10053 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 10054 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 10055 ("handle_workitem_remove: worklist not empty. %s", 10056 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type))); 10057 WORKITEM_FREE(dirrem, D_DIRREM); 10058 FREE_LOCK(ump); 10059 goto out; 10060 } 10061 /* 10062 * Directory deletion. Decrement reference count for both the 10063 * just deleted parent directory entry and the reference for ".". 10064 * Arrange to have the reference count on the parent decremented 10065 * to account for the loss of "..". 10066 */ 10067 ip->i_nlink -= 2; 10068 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: directory ino " 10069 "%ju negative i_nlink %d", (intmax_t)ip->i_number, ip->i_nlink)); 10070 DIP_SET_NLINK(ip, ip->i_nlink); 10071 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 10072 if (ip->i_nlink < ip->i_effnlink) 10073 panic("handle_workitem_remove: bad dir delta"); 10074 if (ip->i_nlink == 0) 10075 unlinked_inodedep(mp, inodedep); 10076 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 10077 /* 10078 * Rename a directory to a new parent. Since, we are both deleting 10079 * and creating a new directory entry, the link count on the new 10080 * directory should not change. Thus we skip the followup dirrem. 10081 */ 10082 if (dirrem->dm_state & DIRCHG) { 10083 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 10084 ("handle_workitem_remove: DIRCHG and worklist not empty.")); 10085 WORKITEM_FREE(dirrem, D_DIRREM); 10086 FREE_LOCK(ump); 10087 goto out; 10088 } 10089 dirrem->dm_state = ONDEPLIST; 10090 dirrem->dm_oldinum = dirrem->dm_dirinum; 10091 /* 10092 * Place the dirrem on the parent's diremhd list. 10093 */ 10094 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0) 10095 panic("handle_workitem_remove: lost dir inodedep"); 10096 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 10097 /* 10098 * If the allocated inode has never been written to disk, then 10099 * the on-disk inode is zero'ed and we can remove the file 10100 * immediately. When journaling if the inode has been marked 10101 * unlinked and not DEPCOMPLETE we know it can never be written. 10102 */ 10103 inodedep_lookup(mp, oldinum, 0, &inodedep); 10104 if (inodedep == NULL || 10105 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED || 10106 check_inode_unwritten(inodedep)) { 10107 FREE_LOCK(ump); 10108 vput(vp); 10109 return handle_workitem_remove(dirrem, flags); 10110 } 10111 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list); 10112 FREE_LOCK(ump); 10113 UFS_INODE_SET_FLAG(ip, IN_CHANGE); 10114 out: 10115 ffs_update(vp, 0); 10116 vput(vp); 10117 return (0); 10118 } 10119 10120 /* 10121 * Inode de-allocation dependencies. 10122 * 10123 * When an inode's link count is reduced to zero, it can be de-allocated. We 10124 * found it convenient to postpone de-allocation until after the inode is 10125 * written to disk with its new link count (zero). At this point, all of the 10126 * on-disk inode's block pointers are nullified and, with careful dependency 10127 * list ordering, all dependencies related to the inode will be satisfied and 10128 * the corresponding dependency structures de-allocated. So, if/when the 10129 * inode is reused, there will be no mixing of old dependencies with new 10130 * ones. This artificial dependency is set up by the block de-allocation 10131 * procedure above (softdep_setup_freeblocks) and completed by the 10132 * following procedure. 10133 */ 10134 static void 10135 handle_workitem_freefile(struct freefile *freefile) 10136 { 10137 struct workhead wkhd; 10138 struct fs *fs; 10139 struct ufsmount *ump; 10140 int error; 10141 #ifdef INVARIANTS 10142 struct inodedep *idp; 10143 #endif 10144 10145 ump = VFSTOUFS(freefile->fx_list.wk_mp); 10146 fs = ump->um_fs; 10147 #ifdef INVARIANTS 10148 ACQUIRE_LOCK(ump); 10149 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp); 10150 FREE_LOCK(ump); 10151 if (error) 10152 panic("handle_workitem_freefile: inodedep %p survived", idp); 10153 #endif 10154 UFS_LOCK(ump); 10155 fs->fs_pendinginodes -= 1; 10156 UFS_UNLOCK(ump); 10157 LIST_INIT(&wkhd); 10158 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list); 10159 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp, 10160 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0) 10161 softdep_error("handle_workitem_freefile", error); 10162 ACQUIRE_LOCK(ump); 10163 WORKITEM_FREE(freefile, D_FREEFILE); 10164 FREE_LOCK(ump); 10165 } 10166 10167 /* 10168 * Helper function which unlinks marker element from work list and returns 10169 * the next element on the list. 10170 */ 10171 static __inline struct worklist * 10172 markernext(struct worklist *marker) 10173 { 10174 struct worklist *next; 10175 10176 next = LIST_NEXT(marker, wk_list); 10177 LIST_REMOVE(marker, wk_list); 10178 return next; 10179 } 10180 10181 /* 10182 * Disk writes. 10183 * 10184 * The dependency structures constructed above are most actively used when file 10185 * system blocks are written to disk. No constraints are placed on when a 10186 * block can be written, but unsatisfied update dependencies are made safe by 10187 * modifying (or replacing) the source memory for the duration of the disk 10188 * write. When the disk write completes, the memory block is again brought 10189 * up-to-date. 10190 * 10191 * In-core inode structure reclamation. 10192 * 10193 * Because there are a finite number of "in-core" inode structures, they are 10194 * reused regularly. By transferring all inode-related dependencies to the 10195 * in-memory inode block and indexing them separately (via "inodedep"s), we 10196 * can allow "in-core" inode structures to be reused at any time and avoid 10197 * any increase in contention. 10198 * 10199 * Called just before entering the device driver to initiate a new disk I/O. 10200 * The buffer must be locked, thus, no I/O completion operations can occur 10201 * while we are manipulating its associated dependencies. 10202 */ 10203 static void 10204 softdep_disk_io_initiation( 10205 struct buf *bp) /* structure describing disk write to occur */ 10206 { 10207 struct worklist *wk; 10208 struct worklist marker; 10209 struct inodedep *inodedep; 10210 struct freeblks *freeblks; 10211 struct jblkdep *jblkdep; 10212 struct newblk *newblk; 10213 struct ufsmount *ump; 10214 10215 /* 10216 * We only care about write operations. There should never 10217 * be dependencies for reads. 10218 */ 10219 if (bp->b_iocmd != BIO_WRITE) 10220 panic("softdep_disk_io_initiation: not write"); 10221 10222 if (bp->b_vflags & BV_BKGRDINPROG) 10223 panic("softdep_disk_io_initiation: Writing buffer with " 10224 "background write in progress: %p", bp); 10225 10226 ump = softdep_bp_to_mp(bp); 10227 if (ump == NULL) 10228 return; 10229 10230 marker.wk_type = D_LAST + 1; /* Not a normal workitem */ 10231 ACQUIRE_LOCK(ump); 10232 /* 10233 * Do any necessary pre-I/O processing. 10234 */ 10235 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL; 10236 wk = markernext(&marker)) { 10237 LIST_INSERT_AFTER(wk, &marker, wk_list); 10238 switch (wk->wk_type) { 10239 case D_PAGEDEP: 10240 initiate_write_filepage(WK_PAGEDEP(wk), bp); 10241 continue; 10242 10243 case D_INODEDEP: 10244 inodedep = WK_INODEDEP(wk); 10245 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) 10246 initiate_write_inodeblock_ufs1(inodedep, bp); 10247 else 10248 initiate_write_inodeblock_ufs2(inodedep, bp); 10249 continue; 10250 10251 case D_INDIRDEP: 10252 initiate_write_indirdep(WK_INDIRDEP(wk), bp); 10253 continue; 10254 10255 case D_BMSAFEMAP: 10256 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp); 10257 continue; 10258 10259 case D_JSEG: 10260 WK_JSEG(wk)->js_buf = NULL; 10261 continue; 10262 10263 case D_FREEBLKS: 10264 freeblks = WK_FREEBLKS(wk); 10265 jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd); 10266 /* 10267 * We have to wait for the freeblks to be journaled 10268 * before we can write an inodeblock with updated 10269 * pointers. Be careful to arrange the marker so 10270 * we revisit the freeblks if it's not removed by 10271 * the first jwait(). 10272 */ 10273 if (jblkdep != NULL) { 10274 LIST_REMOVE(&marker, wk_list); 10275 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10276 jwait(&jblkdep->jb_list, MNT_WAIT); 10277 } 10278 continue; 10279 case D_ALLOCDIRECT: 10280 case D_ALLOCINDIR: 10281 /* 10282 * We have to wait for the jnewblk to be journaled 10283 * before we can write to a block if the contents 10284 * may be confused with an earlier file's indirect 10285 * at recovery time. Handle the marker as described 10286 * above. 10287 */ 10288 newblk = WK_NEWBLK(wk); 10289 if (newblk->nb_jnewblk != NULL && 10290 indirblk_lookup(newblk->nb_list.wk_mp, 10291 newblk->nb_newblkno)) { 10292 LIST_REMOVE(&marker, wk_list); 10293 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10294 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 10295 } 10296 continue; 10297 10298 case D_SBDEP: 10299 initiate_write_sbdep(WK_SBDEP(wk)); 10300 continue; 10301 10302 case D_MKDIR: 10303 case D_FREEWORK: 10304 case D_FREEDEP: 10305 case D_JSEGDEP: 10306 continue; 10307 10308 default: 10309 panic("handle_disk_io_initiation: Unexpected type %s", 10310 TYPENAME(wk->wk_type)); 10311 /* NOTREACHED */ 10312 } 10313 } 10314 FREE_LOCK(ump); 10315 } 10316 10317 /* 10318 * Called from within the procedure above to deal with unsatisfied 10319 * allocation dependencies in a directory. The buffer must be locked, 10320 * thus, no I/O completion operations can occur while we are 10321 * manipulating its associated dependencies. 10322 */ 10323 static void 10324 initiate_write_filepage(struct pagedep *pagedep, struct buf *bp) 10325 { 10326 struct jremref *jremref; 10327 struct jmvref *jmvref; 10328 struct dirrem *dirrem; 10329 struct diradd *dap; 10330 struct direct *ep; 10331 int i; 10332 10333 if (pagedep->pd_state & IOSTARTED) { 10334 /* 10335 * This can only happen if there is a driver that does not 10336 * understand chaining. Here biodone will reissue the call 10337 * to strategy for the incomplete buffers. 10338 */ 10339 printf("initiate_write_filepage: already started\n"); 10340 return; 10341 } 10342 pagedep->pd_state |= IOSTARTED; 10343 /* 10344 * Wait for all journal remove dependencies to hit the disk. 10345 * We can not allow any potentially conflicting directory adds 10346 * to be visible before removes and rollback is too difficult. 10347 * The per-filesystem lock may be dropped and re-acquired, however 10348 * we hold the buf locked so the dependency can not go away. 10349 */ 10350 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) 10351 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) 10352 jwait(&jremref->jr_list, MNT_WAIT); 10353 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) 10354 jwait(&jmvref->jm_list, MNT_WAIT); 10355 for (i = 0; i < DAHASHSZ; i++) { 10356 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 10357 ep = (struct direct *) 10358 ((char *)bp->b_data + dap->da_offset); 10359 if (ep->d_ino != dap->da_newinum) 10360 panic("%s: dir inum %ju != new %ju", 10361 "initiate_write_filepage", 10362 (uintmax_t)ep->d_ino, 10363 (uintmax_t)dap->da_newinum); 10364 if (dap->da_state & DIRCHG) 10365 ep->d_ino = dap->da_previous->dm_oldinum; 10366 else 10367 ep->d_ino = 0; 10368 dap->da_state &= ~ATTACHED; 10369 dap->da_state |= UNDONE; 10370 } 10371 } 10372 } 10373 10374 /* 10375 * Version of initiate_write_inodeblock that handles UFS1 dinodes. 10376 * Note that any bug fixes made to this routine must be done in the 10377 * version found below. 10378 * 10379 * Called from within the procedure above to deal with unsatisfied 10380 * allocation dependencies in an inodeblock. The buffer must be 10381 * locked, thus, no I/O completion operations can occur while we 10382 * are manipulating its associated dependencies. 10383 */ 10384 static void 10385 initiate_write_inodeblock_ufs1( 10386 struct inodedep *inodedep, 10387 struct buf *bp) /* The inode block */ 10388 { 10389 struct allocdirect *adp, *lastadp; 10390 struct ufs1_dinode *dp; 10391 struct ufs1_dinode *sip; 10392 struct inoref *inoref; 10393 struct ufsmount *ump; 10394 struct fs *fs; 10395 ufs_lbn_t i; 10396 #ifdef INVARIANTS 10397 ufs_lbn_t prevlbn = 0; 10398 #endif 10399 int deplist __diagused; 10400 10401 if (inodedep->id_state & IOSTARTED) 10402 panic("initiate_write_inodeblock_ufs1: already started"); 10403 inodedep->id_state |= IOSTARTED; 10404 fs = inodedep->id_fs; 10405 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10406 LOCK_OWNED(ump); 10407 dp = (struct ufs1_dinode *)bp->b_data + 10408 ino_to_fsbo(fs, inodedep->id_ino); 10409 10410 /* 10411 * If we're on the unlinked list but have not yet written our 10412 * next pointer initialize it here. 10413 */ 10414 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10415 struct inodedep *inon; 10416 10417 inon = TAILQ_NEXT(inodedep, id_unlinked); 10418 dp->di_freelink = inon ? inon->id_ino : 0; 10419 } 10420 /* 10421 * If the bitmap is not yet written, then the allocated 10422 * inode cannot be written to disk. 10423 */ 10424 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10425 if (inodedep->id_savedino1 != NULL) 10426 panic("initiate_write_inodeblock_ufs1: I/O underway"); 10427 FREE_LOCK(ump); 10428 sip = malloc(sizeof(struct ufs1_dinode), 10429 M_SAVEDINO, M_SOFTDEP_FLAGS); 10430 ACQUIRE_LOCK(ump); 10431 inodedep->id_savedino1 = sip; 10432 *inodedep->id_savedino1 = *dp; 10433 bzero((caddr_t)dp, sizeof(struct ufs1_dinode)); 10434 dp->di_gen = inodedep->id_savedino1->di_gen; 10435 dp->di_freelink = inodedep->id_savedino1->di_freelink; 10436 return; 10437 } 10438 /* 10439 * If no dependencies, then there is nothing to roll back. 10440 */ 10441 inodedep->id_savedsize = dp->di_size; 10442 inodedep->id_savedextsize = 0; 10443 inodedep->id_savednlink = dp->di_nlink; 10444 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10445 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10446 return; 10447 /* 10448 * Revert the link count to that of the first unwritten journal entry. 10449 */ 10450 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10451 if (inoref) 10452 dp->di_nlink = inoref->if_nlink; 10453 /* 10454 * Set the dependencies to busy. 10455 */ 10456 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10457 adp = TAILQ_NEXT(adp, ad_next)) { 10458 #ifdef INVARIANTS 10459 if (deplist != 0 && prevlbn >= adp->ad_offset) 10460 panic("softdep_write_inodeblock: lbn order"); 10461 prevlbn = adp->ad_offset; 10462 if (adp->ad_offset < UFS_NDADDR && 10463 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10464 panic("initiate_write_inodeblock_ufs1: " 10465 "direct pointer #%jd mismatch %d != %jd", 10466 (intmax_t)adp->ad_offset, 10467 dp->di_db[adp->ad_offset], 10468 (intmax_t)adp->ad_newblkno); 10469 if (adp->ad_offset >= UFS_NDADDR && 10470 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno) 10471 panic("initiate_write_inodeblock_ufs1: " 10472 "indirect pointer #%jd mismatch %d != %jd", 10473 (intmax_t)adp->ad_offset - UFS_NDADDR, 10474 dp->di_ib[adp->ad_offset - UFS_NDADDR], 10475 (intmax_t)adp->ad_newblkno); 10476 deplist |= 1 << adp->ad_offset; 10477 if ((adp->ad_state & ATTACHED) == 0) 10478 panic("initiate_write_inodeblock_ufs1: " 10479 "Unknown state 0x%x", adp->ad_state); 10480 #endif /* INVARIANTS */ 10481 adp->ad_state &= ~ATTACHED; 10482 adp->ad_state |= UNDONE; 10483 } 10484 /* 10485 * The on-disk inode cannot claim to be any larger than the last 10486 * fragment that has been written. Otherwise, the on-disk inode 10487 * might have fragments that were not the last block in the file 10488 * which would corrupt the filesystem. 10489 */ 10490 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10491 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10492 if (adp->ad_offset >= UFS_NDADDR) 10493 break; 10494 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10495 /* keep going until hitting a rollback to a frag */ 10496 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10497 continue; 10498 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10499 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) { 10500 #ifdef INVARIANTS 10501 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10502 panic("initiate_write_inodeblock_ufs1: " 10503 "lost dep1"); 10504 #endif /* INVARIANTS */ 10505 dp->di_db[i] = 0; 10506 } 10507 for (i = 0; i < UFS_NIADDR; i++) { 10508 #ifdef INVARIANTS 10509 if (dp->di_ib[i] != 0 && 10510 (deplist & ((1 << UFS_NDADDR) << i)) == 0) 10511 panic("initiate_write_inodeblock_ufs1: " 10512 "lost dep2"); 10513 #endif /* INVARIANTS */ 10514 dp->di_ib[i] = 0; 10515 } 10516 return; 10517 } 10518 /* 10519 * If we have zero'ed out the last allocated block of the file, 10520 * roll back the size to the last currently allocated block. 10521 * We know that this last allocated block is a full-sized as 10522 * we already checked for fragments in the loop above. 10523 */ 10524 if (lastadp != NULL && 10525 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10526 for (i = lastadp->ad_offset; i >= 0; i--) 10527 if (dp->di_db[i] != 0) 10528 break; 10529 dp->di_size = (i + 1) * fs->fs_bsize; 10530 } 10531 /* 10532 * The only dependencies are for indirect blocks. 10533 * 10534 * The file size for indirect block additions is not guaranteed. 10535 * Such a guarantee would be non-trivial to achieve. The conventional 10536 * synchronous write implementation also does not make this guarantee. 10537 * Fsck should catch and fix discrepancies. Arguably, the file size 10538 * can be over-estimated without destroying integrity when the file 10539 * moves into the indirect blocks (i.e., is large). If we want to 10540 * postpone fsck, we are stuck with this argument. 10541 */ 10542 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10543 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0; 10544 } 10545 10546 /* 10547 * Version of initiate_write_inodeblock that handles UFS2 dinodes. 10548 * Note that any bug fixes made to this routine must be done in the 10549 * version found above. 10550 * 10551 * Called from within the procedure above to deal with unsatisfied 10552 * allocation dependencies in an inodeblock. The buffer must be 10553 * locked, thus, no I/O completion operations can occur while we 10554 * are manipulating its associated dependencies. 10555 */ 10556 static void 10557 initiate_write_inodeblock_ufs2( 10558 struct inodedep *inodedep, 10559 struct buf *bp) /* The inode block */ 10560 { 10561 struct allocdirect *adp, *lastadp; 10562 struct ufs2_dinode *dp; 10563 struct ufs2_dinode *sip; 10564 struct inoref *inoref; 10565 struct ufsmount *ump; 10566 struct fs *fs; 10567 ufs_lbn_t i; 10568 #ifdef INVARIANTS 10569 ufs_lbn_t prevlbn = 0; 10570 #endif 10571 int deplist __diagused; 10572 10573 if (inodedep->id_state & IOSTARTED) 10574 panic("initiate_write_inodeblock_ufs2: already started"); 10575 inodedep->id_state |= IOSTARTED; 10576 fs = inodedep->id_fs; 10577 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10578 LOCK_OWNED(ump); 10579 dp = (struct ufs2_dinode *)bp->b_data + 10580 ino_to_fsbo(fs, inodedep->id_ino); 10581 10582 /* 10583 * If we're on the unlinked list but have not yet written our 10584 * next pointer initialize it here. 10585 */ 10586 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10587 struct inodedep *inon; 10588 10589 inon = TAILQ_NEXT(inodedep, id_unlinked); 10590 dp->di_freelink = inon ? inon->id_ino : 0; 10591 ffs_update_dinode_ckhash(fs, dp); 10592 } 10593 /* 10594 * If the bitmap is not yet written, then the allocated 10595 * inode cannot be written to disk. 10596 */ 10597 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10598 if (inodedep->id_savedino2 != NULL) 10599 panic("initiate_write_inodeblock_ufs2: I/O underway"); 10600 FREE_LOCK(ump); 10601 sip = malloc(sizeof(struct ufs2_dinode), 10602 M_SAVEDINO, M_SOFTDEP_FLAGS); 10603 ACQUIRE_LOCK(ump); 10604 inodedep->id_savedino2 = sip; 10605 *inodedep->id_savedino2 = *dp; 10606 bzero((caddr_t)dp, sizeof(struct ufs2_dinode)); 10607 dp->di_gen = inodedep->id_savedino2->di_gen; 10608 dp->di_freelink = inodedep->id_savedino2->di_freelink; 10609 return; 10610 } 10611 /* 10612 * If no dependencies, then there is nothing to roll back. 10613 */ 10614 inodedep->id_savedsize = dp->di_size; 10615 inodedep->id_savedextsize = dp->di_extsize; 10616 inodedep->id_savednlink = dp->di_nlink; 10617 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10618 TAILQ_EMPTY(&inodedep->id_extupdt) && 10619 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10620 return; 10621 /* 10622 * Revert the link count to that of the first unwritten journal entry. 10623 */ 10624 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10625 if (inoref) 10626 dp->di_nlink = inoref->if_nlink; 10627 10628 /* 10629 * Set the ext data dependencies to busy. 10630 */ 10631 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10632 adp = TAILQ_NEXT(adp, ad_next)) { 10633 #ifdef INVARIANTS 10634 if (deplist != 0 && prevlbn >= adp->ad_offset) 10635 panic("initiate_write_inodeblock_ufs2: lbn order"); 10636 prevlbn = adp->ad_offset; 10637 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno) 10638 panic("initiate_write_inodeblock_ufs2: " 10639 "ext pointer #%jd mismatch %jd != %jd", 10640 (intmax_t)adp->ad_offset, 10641 (intmax_t)dp->di_extb[adp->ad_offset], 10642 (intmax_t)adp->ad_newblkno); 10643 deplist |= 1 << adp->ad_offset; 10644 if ((adp->ad_state & ATTACHED) == 0) 10645 panic("initiate_write_inodeblock_ufs2: Unknown " 10646 "state 0x%x", adp->ad_state); 10647 #endif /* INVARIANTS */ 10648 adp->ad_state &= ~ATTACHED; 10649 adp->ad_state |= UNDONE; 10650 } 10651 /* 10652 * The on-disk inode cannot claim to be any larger than the last 10653 * fragment that has been written. Otherwise, the on-disk inode 10654 * might have fragments that were not the last block in the ext 10655 * data which would corrupt the filesystem. 10656 */ 10657 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10658 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10659 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno; 10660 /* keep going until hitting a rollback to a frag */ 10661 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10662 continue; 10663 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10664 for (i = adp->ad_offset + 1; i < UFS_NXADDR; i++) { 10665 #ifdef INVARIANTS 10666 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0) 10667 panic("initiate_write_inodeblock_ufs2: " 10668 "lost dep1"); 10669 #endif /* INVARIANTS */ 10670 dp->di_extb[i] = 0; 10671 } 10672 lastadp = NULL; 10673 break; 10674 } 10675 /* 10676 * If we have zero'ed out the last allocated block of the ext 10677 * data, roll back the size to the last currently allocated block. 10678 * We know that this last allocated block is a full-sized as 10679 * we already checked for fragments in the loop above. 10680 */ 10681 if (lastadp != NULL && 10682 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10683 for (i = lastadp->ad_offset; i >= 0; i--) 10684 if (dp->di_extb[i] != 0) 10685 break; 10686 dp->di_extsize = (i + 1) * fs->fs_bsize; 10687 } 10688 /* 10689 * Set the file data dependencies to busy. 10690 */ 10691 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10692 adp = TAILQ_NEXT(adp, ad_next)) { 10693 #ifdef INVARIANTS 10694 if (deplist != 0 && prevlbn >= adp->ad_offset) 10695 panic("softdep_write_inodeblock: lbn order"); 10696 if ((adp->ad_state & ATTACHED) == 0) 10697 panic("inodedep %p and adp %p not attached", inodedep, adp); 10698 prevlbn = adp->ad_offset; 10699 if (!ffs_fsfail_cleanup(ump, 0) && 10700 adp->ad_offset < UFS_NDADDR && 10701 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10702 panic("initiate_write_inodeblock_ufs2: " 10703 "direct pointer #%jd mismatch %jd != %jd", 10704 (intmax_t)adp->ad_offset, 10705 (intmax_t)dp->di_db[adp->ad_offset], 10706 (intmax_t)adp->ad_newblkno); 10707 if (!ffs_fsfail_cleanup(ump, 0) && 10708 adp->ad_offset >= UFS_NDADDR && 10709 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno) 10710 panic("initiate_write_inodeblock_ufs2: " 10711 "indirect pointer #%jd mismatch %jd != %jd", 10712 (intmax_t)adp->ad_offset - UFS_NDADDR, 10713 (intmax_t)dp->di_ib[adp->ad_offset - UFS_NDADDR], 10714 (intmax_t)adp->ad_newblkno); 10715 deplist |= 1 << adp->ad_offset; 10716 if ((adp->ad_state & ATTACHED) == 0) 10717 panic("initiate_write_inodeblock_ufs2: Unknown " 10718 "state 0x%x", adp->ad_state); 10719 #endif /* INVARIANTS */ 10720 adp->ad_state &= ~ATTACHED; 10721 adp->ad_state |= UNDONE; 10722 } 10723 /* 10724 * The on-disk inode cannot claim to be any larger than the last 10725 * fragment that has been written. Otherwise, the on-disk inode 10726 * might have fragments that were not the last block in the file 10727 * which would corrupt the filesystem. 10728 */ 10729 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10730 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10731 if (adp->ad_offset >= UFS_NDADDR) 10732 break; 10733 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10734 /* keep going until hitting a rollback to a frag */ 10735 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10736 continue; 10737 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10738 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) { 10739 #ifdef INVARIANTS 10740 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10741 panic("initiate_write_inodeblock_ufs2: " 10742 "lost dep2"); 10743 #endif /* INVARIANTS */ 10744 dp->di_db[i] = 0; 10745 } 10746 for (i = 0; i < UFS_NIADDR; i++) { 10747 #ifdef INVARIANTS 10748 if (dp->di_ib[i] != 0 && 10749 (deplist & ((1 << UFS_NDADDR) << i)) == 0) 10750 panic("initiate_write_inodeblock_ufs2: " 10751 "lost dep3"); 10752 #endif /* INVARIANTS */ 10753 dp->di_ib[i] = 0; 10754 } 10755 ffs_update_dinode_ckhash(fs, dp); 10756 return; 10757 } 10758 /* 10759 * If we have zero'ed out the last allocated block of the file, 10760 * roll back the size to the last currently allocated block. 10761 * We know that this last allocated block is a full-sized as 10762 * we already checked for fragments in the loop above. 10763 */ 10764 if (lastadp != NULL && 10765 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10766 for (i = lastadp->ad_offset; i >= 0; i--) 10767 if (dp->di_db[i] != 0) 10768 break; 10769 dp->di_size = (i + 1) * fs->fs_bsize; 10770 } 10771 /* 10772 * The only dependencies are for indirect blocks. 10773 * 10774 * The file size for indirect block additions is not guaranteed. 10775 * Such a guarantee would be non-trivial to achieve. The conventional 10776 * synchronous write implementation also does not make this guarantee. 10777 * Fsck should catch and fix discrepancies. Arguably, the file size 10778 * can be over-estimated without destroying integrity when the file 10779 * moves into the indirect blocks (i.e., is large). If we want to 10780 * postpone fsck, we are stuck with this argument. 10781 */ 10782 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10783 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0; 10784 ffs_update_dinode_ckhash(fs, dp); 10785 } 10786 10787 /* 10788 * Cancel an indirdep as a result of truncation. Release all of the 10789 * children allocindirs and place their journal work on the appropriate 10790 * list. 10791 */ 10792 static void 10793 cancel_indirdep( 10794 struct indirdep *indirdep, 10795 struct buf *bp, 10796 struct freeblks *freeblks) 10797 { 10798 struct allocindir *aip; 10799 10800 /* 10801 * None of the indirect pointers will ever be visible, 10802 * so they can simply be tossed. GOINGAWAY ensures 10803 * that allocated pointers will be saved in the buffer 10804 * cache until they are freed. Note that they will 10805 * only be able to be found by their physical address 10806 * since the inode mapping the logical address will 10807 * be gone. The save buffer used for the safe copy 10808 * was allocated in setup_allocindir_phase2 using 10809 * the physical address so it could be used for this 10810 * purpose. Hence we swap the safe copy with the real 10811 * copy, allowing the safe copy to be freed and holding 10812 * on to the real copy for later use in indir_trunc. 10813 */ 10814 if (indirdep->ir_state & GOINGAWAY) 10815 panic("cancel_indirdep: already gone"); 10816 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 10817 indirdep->ir_state |= DEPCOMPLETE; 10818 LIST_REMOVE(indirdep, ir_next); 10819 } 10820 indirdep->ir_state |= GOINGAWAY; 10821 /* 10822 * Pass in bp for blocks still have journal writes 10823 * pending so we can cancel them on their own. 10824 */ 10825 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != NULL) 10826 cancel_allocindir(aip, bp, freeblks, 0); 10827 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) 10828 cancel_allocindir(aip, NULL, freeblks, 0); 10829 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) 10830 cancel_allocindir(aip, NULL, freeblks, 0); 10831 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) 10832 cancel_allocindir(aip, NULL, freeblks, 0); 10833 /* 10834 * If there are pending partial truncations we need to keep the 10835 * old block copy around until they complete. This is because 10836 * the current b_data is not a perfect superset of the available 10837 * blocks. 10838 */ 10839 if (TAILQ_EMPTY(&indirdep->ir_trunc)) 10840 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount); 10841 else 10842 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10843 WORKLIST_REMOVE(&indirdep->ir_list); 10844 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list); 10845 indirdep->ir_bp = NULL; 10846 indirdep->ir_freeblks = freeblks; 10847 } 10848 10849 /* 10850 * Free an indirdep once it no longer has new pointers to track. 10851 */ 10852 static void 10853 free_indirdep(struct indirdep *indirdep) 10854 { 10855 10856 KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc), 10857 ("free_indirdep: Indir trunc list not empty.")); 10858 KASSERT(LIST_EMPTY(&indirdep->ir_completehd), 10859 ("free_indirdep: Complete head not empty.")); 10860 KASSERT(LIST_EMPTY(&indirdep->ir_writehd), 10861 ("free_indirdep: write head not empty.")); 10862 KASSERT(LIST_EMPTY(&indirdep->ir_donehd), 10863 ("free_indirdep: done head not empty.")); 10864 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd), 10865 ("free_indirdep: deplist head not empty.")); 10866 KASSERT((indirdep->ir_state & DEPCOMPLETE), 10867 ("free_indirdep: %p still on newblk list.", indirdep)); 10868 KASSERT(indirdep->ir_saveddata == NULL, 10869 ("free_indirdep: %p still has saved data.", indirdep)); 10870 KASSERT(indirdep->ir_savebp == NULL, 10871 ("free_indirdep: %p still has savebp buffer.", indirdep)); 10872 if (indirdep->ir_state & ONWORKLIST) 10873 WORKLIST_REMOVE(&indirdep->ir_list); 10874 WORKITEM_FREE(indirdep, D_INDIRDEP); 10875 } 10876 10877 /* 10878 * Called before a write to an indirdep. This routine is responsible for 10879 * rolling back pointers to a safe state which includes only those 10880 * allocindirs which have been completed. 10881 */ 10882 static void 10883 initiate_write_indirdep(struct indirdep *indirdep, struct buf *bp) 10884 { 10885 struct ufsmount *ump; 10886 10887 indirdep->ir_state |= IOSTARTED; 10888 if (indirdep->ir_state & GOINGAWAY) 10889 panic("disk_io_initiation: indirdep gone"); 10890 /* 10891 * If there are no remaining dependencies, this will be writing 10892 * the real pointers. 10893 */ 10894 if (LIST_EMPTY(&indirdep->ir_deplisthd) && 10895 TAILQ_EMPTY(&indirdep->ir_trunc)) 10896 return; 10897 /* 10898 * Replace up-to-date version with safe version. 10899 */ 10900 if (indirdep->ir_saveddata == NULL) { 10901 ump = VFSTOUFS(indirdep->ir_list.wk_mp); 10902 LOCK_OWNED(ump); 10903 FREE_LOCK(ump); 10904 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 10905 M_SOFTDEP_FLAGS); 10906 ACQUIRE_LOCK(ump); 10907 } 10908 indirdep->ir_state &= ~ATTACHED; 10909 indirdep->ir_state |= UNDONE; 10910 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10911 bcopy(indirdep->ir_savebp->b_data, bp->b_data, 10912 bp->b_bcount); 10913 } 10914 10915 /* 10916 * Called when an inode has been cleared in a cg bitmap. This finally 10917 * eliminates any canceled jaddrefs 10918 */ 10919 void 10920 softdep_setup_inofree(struct mount *mp, 10921 struct buf *bp, 10922 ino_t ino, 10923 struct workhead *wkhd, 10924 bool doingrecovery) 10925 { 10926 struct worklist *wk, *wkn; 10927 struct ufsmount *ump; 10928 #ifdef INVARIANTS 10929 struct inodedep *inodedep; 10930 #endif 10931 10932 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 10933 ("softdep_setup_inofree called on non-softdep filesystem")); 10934 ump = VFSTOUFS(mp); 10935 ACQUIRE_LOCK(ump); 10936 KASSERT(doingrecovery || ffs_fsfail_cleanup(ump, 0) || 10937 isclr(cg_inosused((struct cg *)bp->b_data), 10938 ino % ump->um_fs->fs_ipg), 10939 ("softdep_setup_inofree: inode %ju not freed.", (uintmax_t)ino)); 10940 KASSERT(inodedep_lookup(mp, ino, 0, &inodedep) == 0, 10941 ("softdep_setup_inofree: ino %ju has existing inodedep %p", 10942 (uintmax_t)ino, inodedep)); 10943 if (wkhd) { 10944 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) { 10945 if (wk->wk_type != D_JADDREF) 10946 continue; 10947 WORKLIST_REMOVE(wk); 10948 /* 10949 * We can free immediately even if the jaddref 10950 * isn't attached in a background write as now 10951 * the bitmaps are reconciled. 10952 */ 10953 wk->wk_state |= COMPLETE | ATTACHED; 10954 free_jaddref(WK_JADDREF(wk)); 10955 } 10956 jwork_move(&bp->b_dep, wkhd); 10957 } 10958 FREE_LOCK(ump); 10959 } 10960 10961 /* 10962 * Called via ffs_blkfree() after a set of frags has been cleared from a cg 10963 * map. Any dependencies waiting for the write to clear are added to the 10964 * buf's list and any jnewblks that are being canceled are discarded 10965 * immediately. 10966 */ 10967 void 10968 softdep_setup_blkfree( 10969 struct mount *mp, 10970 struct buf *bp, 10971 ufs2_daddr_t blkno, 10972 int frags, 10973 struct workhead *wkhd, 10974 bool doingrecovery) 10975 { 10976 struct bmsafemap *bmsafemap; 10977 struct jnewblk *jnewblk; 10978 struct ufsmount *ump; 10979 struct worklist *wk; 10980 struct fs *fs; 10981 #ifdef INVARIANTS 10982 uint8_t *blksfree; 10983 struct cg *cgp; 10984 ufs2_daddr_t jstart; 10985 ufs2_daddr_t jend; 10986 ufs2_daddr_t end; 10987 long bno; 10988 int i; 10989 #endif 10990 10991 CTR3(KTR_SUJ, 10992 "softdep_setup_blkfree: blkno %jd frags %d wk head %p", 10993 blkno, frags, wkhd); 10994 10995 ump = VFSTOUFS(mp); 10996 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 10997 ("softdep_setup_blkfree called on non-softdep filesystem")); 10998 ACQUIRE_LOCK(ump); 10999 /* Lookup the bmsafemap so we track when it is dirty. */ 11000 fs = ump->um_fs; 11001 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 11002 /* 11003 * Detach any jnewblks which have been canceled. They must linger 11004 * until the bitmap is cleared again by ffs_blkfree() to prevent 11005 * an unjournaled allocation from hitting the disk. 11006 */ 11007 if (wkhd) { 11008 while ((wk = LIST_FIRST(wkhd)) != NULL) { 11009 CTR2(KTR_SUJ, 11010 "softdep_setup_blkfree: blkno %jd wk type %d", 11011 blkno, wk->wk_type); 11012 WORKLIST_REMOVE(wk); 11013 if (wk->wk_type != D_JNEWBLK) { 11014 WORKLIST_INSERT(&bmsafemap->sm_freehd, wk); 11015 continue; 11016 } 11017 jnewblk = WK_JNEWBLK(wk); 11018 KASSERT(jnewblk->jn_state & GOINGAWAY, 11019 ("softdep_setup_blkfree: jnewblk not canceled.")); 11020 #ifdef INVARIANTS 11021 if (!doingrecovery && !ffs_fsfail_cleanup(ump, 0)) { 11022 /* 11023 * Assert that this block is free in the 11024 * bitmap before we discard the jnewblk. 11025 */ 11026 cgp = (struct cg *)bp->b_data; 11027 blksfree = cg_blksfree(cgp); 11028 bno = dtogd(fs, jnewblk->jn_blkno); 11029 for (i = jnewblk->jn_oldfrags; 11030 i < jnewblk->jn_frags; i++) { 11031 if (isset(blksfree, bno + i)) 11032 continue; 11033 panic("softdep_setup_blkfree: block " 11034 "%ju not freed.", 11035 (uintmax_t)jnewblk->jn_blkno); 11036 } 11037 } 11038 #endif 11039 /* 11040 * Even if it's not attached we can free immediately 11041 * as the new bitmap is correct. 11042 */ 11043 wk->wk_state |= COMPLETE | ATTACHED; 11044 free_jnewblk(jnewblk); 11045 } 11046 } 11047 11048 #ifdef INVARIANTS 11049 /* 11050 * Assert that we are not freeing a block which has an outstanding 11051 * allocation dependency. 11052 */ 11053 fs = VFSTOUFS(mp)->um_fs; 11054 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 11055 end = blkno + frags; 11056 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 11057 /* 11058 * Don't match against blocks that will be freed when the 11059 * background write is done. 11060 */ 11061 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) == 11062 (COMPLETE | DEPCOMPLETE)) 11063 continue; 11064 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags; 11065 jend = jnewblk->jn_blkno + jnewblk->jn_frags; 11066 if ((blkno >= jstart && blkno < jend) || 11067 (end > jstart && end <= jend)) { 11068 printf("state 0x%X %jd - %d %d dep %p\n", 11069 jnewblk->jn_state, jnewblk->jn_blkno, 11070 jnewblk->jn_oldfrags, jnewblk->jn_frags, 11071 jnewblk->jn_dep); 11072 panic("softdep_setup_blkfree: " 11073 "%jd-%jd(%d) overlaps with %jd-%jd", 11074 blkno, end, frags, jstart, jend); 11075 } 11076 } 11077 #endif 11078 FREE_LOCK(ump); 11079 } 11080 11081 /* 11082 * Revert a block allocation when the journal record that describes it 11083 * is not yet written. 11084 */ 11085 static int 11086 jnewblk_rollback( 11087 struct jnewblk *jnewblk, 11088 struct fs *fs, 11089 struct cg *cgp, 11090 uint8_t *blksfree) 11091 { 11092 ufs1_daddr_t fragno; 11093 long cgbno, bbase; 11094 int frags, blk; 11095 int i; 11096 11097 frags = 0; 11098 cgbno = dtogd(fs, jnewblk->jn_blkno); 11099 /* 11100 * We have to test which frags need to be rolled back. We may 11101 * be operating on a stale copy when doing background writes. 11102 */ 11103 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) 11104 if (isclr(blksfree, cgbno + i)) 11105 frags++; 11106 if (frags == 0) 11107 return (0); 11108 /* 11109 * This is mostly ffs_blkfree() sans some validation and 11110 * superblock updates. 11111 */ 11112 if (frags == fs->fs_frag) { 11113 fragno = fragstoblks(fs, cgbno); 11114 ffs_setblock(fs, blksfree, fragno); 11115 ffs_clusteracct(fs, cgp, fragno, 1); 11116 cgp->cg_cs.cs_nbfree++; 11117 } else { 11118 cgbno += jnewblk->jn_oldfrags; 11119 bbase = cgbno - fragnum(fs, cgbno); 11120 /* Decrement the old frags. */ 11121 blk = blkmap(fs, blksfree, bbase); 11122 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 11123 /* Deallocate the fragment */ 11124 for (i = 0; i < frags; i++) 11125 setbit(blksfree, cgbno + i); 11126 cgp->cg_cs.cs_nffree += frags; 11127 /* Add back in counts associated with the new frags */ 11128 blk = blkmap(fs, blksfree, bbase); 11129 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 11130 /* If a complete block has been reassembled, account for it. */ 11131 fragno = fragstoblks(fs, bbase); 11132 if (ffs_isblock(fs, blksfree, fragno)) { 11133 cgp->cg_cs.cs_nffree -= fs->fs_frag; 11134 ffs_clusteracct(fs, cgp, fragno, 1); 11135 cgp->cg_cs.cs_nbfree++; 11136 } 11137 } 11138 stat_jnewblk++; 11139 jnewblk->jn_state &= ~ATTACHED; 11140 jnewblk->jn_state |= UNDONE; 11141 11142 return (frags); 11143 } 11144 11145 static void 11146 initiate_write_bmsafemap( 11147 struct bmsafemap *bmsafemap, 11148 struct buf *bp) /* The cg block. */ 11149 { 11150 struct jaddref *jaddref; 11151 struct jnewblk *jnewblk; 11152 uint8_t *inosused; 11153 uint8_t *blksfree; 11154 struct cg *cgp; 11155 struct fs *fs; 11156 ino_t ino; 11157 11158 /* 11159 * If this is a background write, we did this at the time that 11160 * the copy was made, so do not need to do it again. 11161 */ 11162 if (bmsafemap->sm_state & IOSTARTED) 11163 return; 11164 bmsafemap->sm_state |= IOSTARTED; 11165 /* 11166 * Clear any inode allocations which are pending journal writes. 11167 */ 11168 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) { 11169 cgp = (struct cg *)bp->b_data; 11170 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11171 inosused = cg_inosused(cgp); 11172 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) { 11173 ino = jaddref->ja_ino % fs->fs_ipg; 11174 if (isset(inosused, ino)) { 11175 if ((jaddref->ja_mode & IFMT) == IFDIR) 11176 cgp->cg_cs.cs_ndir--; 11177 cgp->cg_cs.cs_nifree++; 11178 clrbit(inosused, ino); 11179 jaddref->ja_state &= ~ATTACHED; 11180 jaddref->ja_state |= UNDONE; 11181 stat_jaddref++; 11182 } else 11183 panic("initiate_write_bmsafemap: inode %ju " 11184 "marked free", (uintmax_t)jaddref->ja_ino); 11185 } 11186 } 11187 /* 11188 * Clear any block allocations which are pending journal writes. 11189 */ 11190 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 11191 cgp = (struct cg *)bp->b_data; 11192 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11193 blksfree = cg_blksfree(cgp); 11194 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 11195 if (jnewblk_rollback(jnewblk, fs, cgp, blksfree)) 11196 continue; 11197 panic("initiate_write_bmsafemap: block %jd " 11198 "marked free", jnewblk->jn_blkno); 11199 } 11200 } 11201 /* 11202 * Move allocation lists to the written lists so they can be 11203 * cleared once the block write is complete. 11204 */ 11205 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr, 11206 inodedep, id_deps); 11207 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 11208 newblk, nb_deps); 11209 LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist, 11210 wk_list); 11211 } 11212 11213 void 11214 softdep_handle_error(struct buf *bp) 11215 { 11216 struct ufsmount *ump; 11217 11218 ump = softdep_bp_to_mp(bp); 11219 if (ump == NULL) 11220 return; 11221 11222 if (ffs_fsfail_cleanup(ump, bp->b_error)) { 11223 /* 11224 * No future writes will succeed, so the on-disk image is safe. 11225 * Pretend that this write succeeded so that the softdep state 11226 * will be cleaned up naturally. 11227 */ 11228 bp->b_ioflags &= ~BIO_ERROR; 11229 bp->b_error = 0; 11230 } 11231 } 11232 11233 /* 11234 * This routine is called during the completion interrupt 11235 * service routine for a disk write (from the procedure called 11236 * by the device driver to inform the filesystem caches of 11237 * a request completion). It should be called early in this 11238 * procedure, before the block is made available to other 11239 * processes or other routines are called. 11240 * 11241 */ 11242 static void 11243 softdep_disk_write_complete( 11244 struct buf *bp) /* describes the completed disk write */ 11245 { 11246 struct worklist *wk; 11247 struct worklist *owk; 11248 struct ufsmount *ump; 11249 struct workhead reattach; 11250 struct freeblks *freeblks; 11251 struct buf *sbp; 11252 11253 ump = softdep_bp_to_mp(bp); 11254 KASSERT(LIST_EMPTY(&bp->b_dep) || ump != NULL, 11255 ("softdep_disk_write_complete: softdep_bp_to_mp returned NULL " 11256 "with outstanding dependencies for buffer %p", bp)); 11257 if (ump == NULL) 11258 return; 11259 if ((bp->b_ioflags & BIO_ERROR) != 0) 11260 softdep_handle_error(bp); 11261 /* 11262 * If an error occurred while doing the write, then the data 11263 * has not hit the disk and the dependencies cannot be processed. 11264 * But we do have to go through and roll forward any dependencies 11265 * that were rolled back before the disk write. 11266 */ 11267 sbp = NULL; 11268 ACQUIRE_LOCK(ump); 11269 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) { 11270 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 11271 switch (wk->wk_type) { 11272 case D_PAGEDEP: 11273 handle_written_filepage(WK_PAGEDEP(wk), bp, 0); 11274 continue; 11275 11276 case D_INODEDEP: 11277 handle_written_inodeblock(WK_INODEDEP(wk), 11278 bp, 0); 11279 continue; 11280 11281 case D_BMSAFEMAP: 11282 handle_written_bmsafemap(WK_BMSAFEMAP(wk), 11283 bp, 0); 11284 continue; 11285 11286 case D_INDIRDEP: 11287 handle_written_indirdep(WK_INDIRDEP(wk), 11288 bp, &sbp, 0); 11289 continue; 11290 default: 11291 /* nothing to roll forward */ 11292 continue; 11293 } 11294 } 11295 FREE_LOCK(ump); 11296 if (sbp) 11297 brelse(sbp); 11298 return; 11299 } 11300 LIST_INIT(&reattach); 11301 11302 /* 11303 * Ump SU lock must not be released anywhere in this code segment. 11304 */ 11305 owk = NULL; 11306 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) { 11307 WORKLIST_REMOVE(wk); 11308 atomic_add_long(&dep_write[wk->wk_type], 1); 11309 if (wk == owk) 11310 panic("duplicate worklist: %p\n", wk); 11311 owk = wk; 11312 switch (wk->wk_type) { 11313 case D_PAGEDEP: 11314 if (handle_written_filepage(WK_PAGEDEP(wk), bp, 11315 WRITESUCCEEDED)) 11316 WORKLIST_INSERT(&reattach, wk); 11317 continue; 11318 11319 case D_INODEDEP: 11320 if (handle_written_inodeblock(WK_INODEDEP(wk), bp, 11321 WRITESUCCEEDED)) 11322 WORKLIST_INSERT(&reattach, wk); 11323 continue; 11324 11325 case D_BMSAFEMAP: 11326 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp, 11327 WRITESUCCEEDED)) 11328 WORKLIST_INSERT(&reattach, wk); 11329 continue; 11330 11331 case D_MKDIR: 11332 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 11333 continue; 11334 11335 case D_ALLOCDIRECT: 11336 wk->wk_state |= COMPLETE; 11337 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL); 11338 continue; 11339 11340 case D_ALLOCINDIR: 11341 wk->wk_state |= COMPLETE; 11342 handle_allocindir_partdone(WK_ALLOCINDIR(wk)); 11343 continue; 11344 11345 case D_INDIRDEP: 11346 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp, 11347 WRITESUCCEEDED)) 11348 WORKLIST_INSERT(&reattach, wk); 11349 continue; 11350 11351 case D_FREEBLKS: 11352 wk->wk_state |= COMPLETE; 11353 freeblks = WK_FREEBLKS(wk); 11354 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE && 11355 LIST_EMPTY(&freeblks->fb_jblkdephd)) 11356 add_to_worklist(wk, WK_NODELAY); 11357 continue; 11358 11359 case D_FREEWORK: 11360 handle_written_freework(WK_FREEWORK(wk)); 11361 break; 11362 11363 case D_JSEGDEP: 11364 free_jsegdep(WK_JSEGDEP(wk)); 11365 continue; 11366 11367 case D_JSEG: 11368 handle_written_jseg(WK_JSEG(wk), bp); 11369 continue; 11370 11371 case D_SBDEP: 11372 if (handle_written_sbdep(WK_SBDEP(wk), bp)) 11373 WORKLIST_INSERT(&reattach, wk); 11374 continue; 11375 11376 case D_FREEDEP: 11377 free_freedep(WK_FREEDEP(wk)); 11378 continue; 11379 11380 default: 11381 panic("handle_disk_write_complete: Unknown type %s", 11382 TYPENAME(wk->wk_type)); 11383 /* NOTREACHED */ 11384 } 11385 } 11386 /* 11387 * Reattach any requests that must be redone. 11388 */ 11389 while ((wk = LIST_FIRST(&reattach)) != NULL) { 11390 WORKLIST_REMOVE(wk); 11391 WORKLIST_INSERT(&bp->b_dep, wk); 11392 } 11393 FREE_LOCK(ump); 11394 if (sbp) 11395 brelse(sbp); 11396 } 11397 11398 /* 11399 * Called from within softdep_disk_write_complete above. 11400 */ 11401 static void 11402 handle_allocdirect_partdone( 11403 struct allocdirect *adp, /* the completed allocdirect */ 11404 struct workhead *wkhd) /* Work to do when inode is writtne. */ 11405 { 11406 struct allocdirectlst *listhead; 11407 struct allocdirect *listadp; 11408 struct inodedep *inodedep; 11409 long bsize; 11410 11411 LOCK_OWNED(VFSTOUFS(adp->ad_block.nb_list.wk_mp)); 11412 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11413 return; 11414 /* 11415 * The on-disk inode cannot claim to be any larger than the last 11416 * fragment that has been written. Otherwise, the on-disk inode 11417 * might have fragments that were not the last block in the file 11418 * which would corrupt the filesystem. Thus, we cannot free any 11419 * allocdirects after one whose ad_oldblkno claims a fragment as 11420 * these blocks must be rolled back to zero before writing the inode. 11421 * We check the currently active set of allocdirects in id_inoupdt 11422 * or id_extupdt as appropriate. 11423 */ 11424 inodedep = adp->ad_inodedep; 11425 bsize = inodedep->id_fs->fs_bsize; 11426 if (adp->ad_state & EXTDATA) 11427 listhead = &inodedep->id_extupdt; 11428 else 11429 listhead = &inodedep->id_inoupdt; 11430 TAILQ_FOREACH(listadp, listhead, ad_next) { 11431 /* found our block */ 11432 if (listadp == adp) 11433 break; 11434 /* continue if ad_oldlbn is not a fragment */ 11435 if (listadp->ad_oldsize == 0 || 11436 listadp->ad_oldsize == bsize) 11437 continue; 11438 /* hit a fragment */ 11439 return; 11440 } 11441 /* 11442 * If we have reached the end of the current list without 11443 * finding the just finished dependency, then it must be 11444 * on the future dependency list. Future dependencies cannot 11445 * be freed until they are moved to the current list. 11446 */ 11447 if (listadp == NULL) { 11448 #ifdef INVARIANTS 11449 if (adp->ad_state & EXTDATA) 11450 listhead = &inodedep->id_newextupdt; 11451 else 11452 listhead = &inodedep->id_newinoupdt; 11453 TAILQ_FOREACH(listadp, listhead, ad_next) 11454 /* found our block */ 11455 if (listadp == adp) 11456 break; 11457 if (listadp == NULL) 11458 panic("handle_allocdirect_partdone: lost dep"); 11459 #endif /* INVARIANTS */ 11460 return; 11461 } 11462 /* 11463 * If we have found the just finished dependency, then queue 11464 * it along with anything that follows it that is complete. 11465 * Since the pointer has not yet been written in the inode 11466 * as the dependency prevents it, place the allocdirect on the 11467 * bufwait list where it will be freed once the pointer is 11468 * valid. 11469 */ 11470 if (wkhd == NULL) 11471 wkhd = &inodedep->id_bufwait; 11472 for (; adp; adp = listadp) { 11473 listadp = TAILQ_NEXT(adp, ad_next); 11474 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11475 return; 11476 TAILQ_REMOVE(listhead, adp, ad_next); 11477 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list); 11478 } 11479 } 11480 11481 /* 11482 * Called from within softdep_disk_write_complete above. This routine 11483 * completes successfully written allocindirs. 11484 */ 11485 static void 11486 handle_allocindir_partdone( 11487 struct allocindir *aip) /* the completed allocindir */ 11488 { 11489 struct indirdep *indirdep; 11490 11491 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE) 11492 return; 11493 indirdep = aip->ai_indirdep; 11494 LIST_REMOVE(aip, ai_next); 11495 /* 11496 * Don't set a pointer while the buffer is undergoing IO or while 11497 * we have active truncations. 11498 */ 11499 if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) { 11500 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next); 11501 return; 11502 } 11503 if (indirdep->ir_state & UFS1FMT) 11504 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11505 aip->ai_newblkno; 11506 else 11507 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11508 aip->ai_newblkno; 11509 /* 11510 * Await the pointer write before freeing the allocindir. 11511 */ 11512 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next); 11513 } 11514 11515 /* 11516 * Release segments held on a jwork list. 11517 */ 11518 static void 11519 handle_jwork(struct workhead *wkhd) 11520 { 11521 struct worklist *wk; 11522 11523 while ((wk = LIST_FIRST(wkhd)) != NULL) { 11524 WORKLIST_REMOVE(wk); 11525 switch (wk->wk_type) { 11526 case D_JSEGDEP: 11527 free_jsegdep(WK_JSEGDEP(wk)); 11528 continue; 11529 case D_FREEDEP: 11530 free_freedep(WK_FREEDEP(wk)); 11531 continue; 11532 case D_FREEFRAG: 11533 rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep)); 11534 WORKITEM_FREE(wk, D_FREEFRAG); 11535 continue; 11536 case D_FREEWORK: 11537 handle_written_freework(WK_FREEWORK(wk)); 11538 continue; 11539 default: 11540 panic("handle_jwork: Unknown type %s\n", 11541 TYPENAME(wk->wk_type)); 11542 } 11543 } 11544 } 11545 11546 /* 11547 * Handle the bufwait list on an inode when it is safe to release items 11548 * held there. This normally happens after an inode block is written but 11549 * may be delayed and handled later if there are pending journal items that 11550 * are not yet safe to be released. 11551 */ 11552 static struct freefile * 11553 handle_bufwait( 11554 struct inodedep *inodedep, 11555 struct workhead *refhd) 11556 { 11557 struct jaddref *jaddref; 11558 struct freefile *freefile; 11559 struct worklist *wk; 11560 11561 freefile = NULL; 11562 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) { 11563 WORKLIST_REMOVE(wk); 11564 switch (wk->wk_type) { 11565 case D_FREEFILE: 11566 /* 11567 * We defer adding freefile to the worklist 11568 * until all other additions have been made to 11569 * ensure that it will be done after all the 11570 * old blocks have been freed. 11571 */ 11572 if (freefile != NULL) 11573 panic("handle_bufwait: freefile"); 11574 freefile = WK_FREEFILE(wk); 11575 continue; 11576 11577 case D_MKDIR: 11578 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT); 11579 continue; 11580 11581 case D_DIRADD: 11582 diradd_inode_written(WK_DIRADD(wk), inodedep); 11583 continue; 11584 11585 case D_FREEFRAG: 11586 wk->wk_state |= COMPLETE; 11587 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE) 11588 add_to_worklist(wk, 0); 11589 continue; 11590 11591 case D_DIRREM: 11592 wk->wk_state |= COMPLETE; 11593 add_to_worklist(wk, 0); 11594 continue; 11595 11596 case D_ALLOCDIRECT: 11597 case D_ALLOCINDIR: 11598 free_newblk(WK_NEWBLK(wk)); 11599 continue; 11600 11601 case D_JNEWBLK: 11602 wk->wk_state |= COMPLETE; 11603 free_jnewblk(WK_JNEWBLK(wk)); 11604 continue; 11605 11606 /* 11607 * Save freed journal segments and add references on 11608 * the supplied list which will delay their release 11609 * until the cg bitmap is cleared on disk. 11610 */ 11611 case D_JSEGDEP: 11612 if (refhd == NULL) 11613 free_jsegdep(WK_JSEGDEP(wk)); 11614 else 11615 WORKLIST_INSERT(refhd, wk); 11616 continue; 11617 11618 case D_JADDREF: 11619 jaddref = WK_JADDREF(wk); 11620 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 11621 if_deps); 11622 /* 11623 * Transfer any jaddrefs to the list to be freed with 11624 * the bitmap if we're handling a removed file. 11625 */ 11626 if (refhd == NULL) { 11627 wk->wk_state |= COMPLETE; 11628 free_jaddref(jaddref); 11629 } else 11630 WORKLIST_INSERT(refhd, wk); 11631 continue; 11632 11633 default: 11634 panic("handle_bufwait: Unknown type %p(%s)", 11635 wk, TYPENAME(wk->wk_type)); 11636 /* NOTREACHED */ 11637 } 11638 } 11639 return (freefile); 11640 } 11641 /* 11642 * Called from within softdep_disk_write_complete above to restore 11643 * in-memory inode block contents to their most up-to-date state. Note 11644 * that this routine is always called from interrupt level with further 11645 * interrupts from this device blocked. 11646 * 11647 * If the write did not succeed, we will do all the roll-forward 11648 * operations, but we will not take the actions that will allow its 11649 * dependencies to be processed. 11650 */ 11651 static int 11652 handle_written_inodeblock( 11653 struct inodedep *inodedep, 11654 struct buf *bp, /* buffer containing the inode block */ 11655 int flags) 11656 { 11657 struct freefile *freefile; 11658 struct allocdirect *adp, *nextadp; 11659 struct ufs1_dinode *dp1 = NULL; 11660 struct ufs2_dinode *dp2 = NULL; 11661 struct workhead wkhd; 11662 int hadchanges, fstype; 11663 ino_t freelink; 11664 11665 LIST_INIT(&wkhd); 11666 hadchanges = 0; 11667 freefile = NULL; 11668 if ((inodedep->id_state & IOSTARTED) == 0) 11669 panic("handle_written_inodeblock: not started"); 11670 inodedep->id_state &= ~IOSTARTED; 11671 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) { 11672 fstype = UFS1; 11673 dp1 = (struct ufs1_dinode *)bp->b_data + 11674 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11675 freelink = dp1->di_freelink; 11676 } else { 11677 fstype = UFS2; 11678 dp2 = (struct ufs2_dinode *)bp->b_data + 11679 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11680 freelink = dp2->di_freelink; 11681 } 11682 /* 11683 * Leave this inodeblock dirty until it's in the list. 11684 */ 11685 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED && 11686 (flags & WRITESUCCEEDED)) { 11687 struct inodedep *inon; 11688 11689 inon = TAILQ_NEXT(inodedep, id_unlinked); 11690 if ((inon == NULL && freelink == 0) || 11691 (inon && inon->id_ino == freelink)) { 11692 if (inon) 11693 inon->id_state |= UNLINKPREV; 11694 inodedep->id_state |= UNLINKNEXT; 11695 } 11696 hadchanges = 1; 11697 } 11698 /* 11699 * If we had to rollback the inode allocation because of 11700 * bitmaps being incomplete, then simply restore it. 11701 * Keep the block dirty so that it will not be reclaimed until 11702 * all associated dependencies have been cleared and the 11703 * corresponding updates written to disk. 11704 */ 11705 if (inodedep->id_savedino1 != NULL) { 11706 hadchanges = 1; 11707 if (fstype == UFS1) 11708 *dp1 = *inodedep->id_savedino1; 11709 else 11710 *dp2 = *inodedep->id_savedino2; 11711 free(inodedep->id_savedino1, M_SAVEDINO); 11712 inodedep->id_savedino1 = NULL; 11713 if ((bp->b_flags & B_DELWRI) == 0) 11714 stat_inode_bitmap++; 11715 bdirty(bp); 11716 /* 11717 * If the inode is clear here and GOINGAWAY it will never 11718 * be written. Process the bufwait and clear any pending 11719 * work which may include the freefile. 11720 */ 11721 if (inodedep->id_state & GOINGAWAY) 11722 goto bufwait; 11723 return (1); 11724 } 11725 if (flags & WRITESUCCEEDED) 11726 inodedep->id_state |= COMPLETE; 11727 /* 11728 * Roll forward anything that had to be rolled back before 11729 * the inode could be updated. 11730 */ 11731 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) { 11732 nextadp = TAILQ_NEXT(adp, ad_next); 11733 if (adp->ad_state & ATTACHED) 11734 panic("handle_written_inodeblock: new entry"); 11735 if (fstype == UFS1) { 11736 if (adp->ad_offset < UFS_NDADDR) { 11737 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11738 panic("%s %s #%jd mismatch %d != %jd", 11739 "handle_written_inodeblock:", 11740 "direct pointer", 11741 (intmax_t)adp->ad_offset, 11742 dp1->di_db[adp->ad_offset], 11743 (intmax_t)adp->ad_oldblkno); 11744 dp1->di_db[adp->ad_offset] = adp->ad_newblkno; 11745 } else { 11746 if (dp1->di_ib[adp->ad_offset - UFS_NDADDR] != 11747 0) 11748 panic("%s: %s #%jd allocated as %d", 11749 "handle_written_inodeblock", 11750 "indirect pointer", 11751 (intmax_t)adp->ad_offset - 11752 UFS_NDADDR, 11753 dp1->di_ib[adp->ad_offset - 11754 UFS_NDADDR]); 11755 dp1->di_ib[adp->ad_offset - UFS_NDADDR] = 11756 adp->ad_newblkno; 11757 } 11758 } else { 11759 if (adp->ad_offset < UFS_NDADDR) { 11760 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11761 panic("%s: %s #%jd %s %jd != %jd", 11762 "handle_written_inodeblock", 11763 "direct pointer", 11764 (intmax_t)adp->ad_offset, "mismatch", 11765 (intmax_t)dp2->di_db[adp->ad_offset], 11766 (intmax_t)adp->ad_oldblkno); 11767 dp2->di_db[adp->ad_offset] = adp->ad_newblkno; 11768 } else { 11769 if (dp2->di_ib[adp->ad_offset - UFS_NDADDR] != 11770 0) 11771 panic("%s: %s #%jd allocated as %jd", 11772 "handle_written_inodeblock", 11773 "indirect pointer", 11774 (intmax_t)adp->ad_offset - 11775 UFS_NDADDR, 11776 (intmax_t) 11777 dp2->di_ib[adp->ad_offset - 11778 UFS_NDADDR]); 11779 dp2->di_ib[adp->ad_offset - UFS_NDADDR] = 11780 adp->ad_newblkno; 11781 } 11782 } 11783 adp->ad_state &= ~UNDONE; 11784 adp->ad_state |= ATTACHED; 11785 hadchanges = 1; 11786 } 11787 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) { 11788 nextadp = TAILQ_NEXT(adp, ad_next); 11789 if (adp->ad_state & ATTACHED) 11790 panic("handle_written_inodeblock: new entry"); 11791 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno) 11792 panic("%s: direct pointers #%jd %s %jd != %jd", 11793 "handle_written_inodeblock", 11794 (intmax_t)adp->ad_offset, "mismatch", 11795 (intmax_t)dp2->di_extb[adp->ad_offset], 11796 (intmax_t)adp->ad_oldblkno); 11797 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno; 11798 adp->ad_state &= ~UNDONE; 11799 adp->ad_state |= ATTACHED; 11800 hadchanges = 1; 11801 } 11802 if (hadchanges && (bp->b_flags & B_DELWRI) == 0) 11803 stat_direct_blk_ptrs++; 11804 /* 11805 * Reset the file size to its most up-to-date value. 11806 */ 11807 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1) 11808 panic("handle_written_inodeblock: bad size"); 11809 if (inodedep->id_savednlink > UFS_LINK_MAX) 11810 panic("handle_written_inodeblock: Invalid link count " 11811 "%jd for inodedep %p", (uintmax_t)inodedep->id_savednlink, 11812 inodedep); 11813 if (fstype == UFS1) { 11814 if (dp1->di_nlink != inodedep->id_savednlink) { 11815 dp1->di_nlink = inodedep->id_savednlink; 11816 hadchanges = 1; 11817 } 11818 if (dp1->di_size != inodedep->id_savedsize) { 11819 dp1->di_size = inodedep->id_savedsize; 11820 hadchanges = 1; 11821 } 11822 } else { 11823 if (dp2->di_nlink != inodedep->id_savednlink) { 11824 dp2->di_nlink = inodedep->id_savednlink; 11825 hadchanges = 1; 11826 } 11827 if (dp2->di_size != inodedep->id_savedsize) { 11828 dp2->di_size = inodedep->id_savedsize; 11829 hadchanges = 1; 11830 } 11831 if (dp2->di_extsize != inodedep->id_savedextsize) { 11832 dp2->di_extsize = inodedep->id_savedextsize; 11833 hadchanges = 1; 11834 } 11835 } 11836 inodedep->id_savedsize = -1; 11837 inodedep->id_savedextsize = -1; 11838 inodedep->id_savednlink = -1; 11839 /* 11840 * If there were any rollbacks in the inode block, then it must be 11841 * marked dirty so that its will eventually get written back in 11842 * its correct form. 11843 */ 11844 if (hadchanges) { 11845 if (fstype == UFS2) 11846 ffs_update_dinode_ckhash(inodedep->id_fs, dp2); 11847 bdirty(bp); 11848 } 11849 bufwait: 11850 /* 11851 * If the write did not succeed, we have done all the roll-forward 11852 * operations, but we cannot take the actions that will allow its 11853 * dependencies to be processed. 11854 */ 11855 if ((flags & WRITESUCCEEDED) == 0) 11856 return (hadchanges); 11857 /* 11858 * Process any allocdirects that completed during the update. 11859 */ 11860 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 11861 handle_allocdirect_partdone(adp, &wkhd); 11862 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 11863 handle_allocdirect_partdone(adp, &wkhd); 11864 /* 11865 * Process deallocations that were held pending until the 11866 * inode had been written to disk. Freeing of the inode 11867 * is delayed until after all blocks have been freed to 11868 * avoid creation of new <vfsid, inum, lbn> triples 11869 * before the old ones have been deleted. Completely 11870 * unlinked inodes are not processed until the unlinked 11871 * inode list is written or the last reference is removed. 11872 */ 11873 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) { 11874 freefile = handle_bufwait(inodedep, NULL); 11875 if (freefile && !LIST_EMPTY(&wkhd)) { 11876 WORKLIST_INSERT(&wkhd, &freefile->fx_list); 11877 freefile = NULL; 11878 } 11879 } 11880 /* 11881 * Move rolled forward dependency completions to the bufwait list 11882 * now that those that were already written have been processed. 11883 */ 11884 if (!LIST_EMPTY(&wkhd) && hadchanges == 0) 11885 panic("handle_written_inodeblock: bufwait but no changes"); 11886 jwork_move(&inodedep->id_bufwait, &wkhd); 11887 11888 if (freefile != NULL) { 11889 /* 11890 * If the inode is goingaway it was never written. Fake up 11891 * the state here so free_inodedep() can succeed. 11892 */ 11893 if (inodedep->id_state & GOINGAWAY) 11894 inodedep->id_state |= COMPLETE | DEPCOMPLETE; 11895 if (free_inodedep(inodedep) == 0) 11896 panic("handle_written_inodeblock: live inodedep %p", 11897 inodedep); 11898 add_to_worklist(&freefile->fx_list, 0); 11899 return (0); 11900 } 11901 11902 /* 11903 * If no outstanding dependencies, free it. 11904 */ 11905 if (free_inodedep(inodedep) || 11906 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 && 11907 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 && 11908 TAILQ_FIRST(&inodedep->id_extupdt) == 0 && 11909 LIST_FIRST(&inodedep->id_bufwait) == 0)) 11910 return (0); 11911 return (hadchanges); 11912 } 11913 11914 /* 11915 * Perform needed roll-forwards and kick off any dependencies that 11916 * can now be processed. 11917 * 11918 * If the write did not succeed, we will do all the roll-forward 11919 * operations, but we will not take the actions that will allow its 11920 * dependencies to be processed. 11921 */ 11922 static int 11923 handle_written_indirdep( 11924 struct indirdep *indirdep, 11925 struct buf *bp, 11926 struct buf **bpp, 11927 int flags) 11928 { 11929 struct allocindir *aip; 11930 struct buf *sbp; 11931 int chgs; 11932 11933 if (indirdep->ir_state & GOINGAWAY) 11934 panic("handle_written_indirdep: indirdep gone"); 11935 if ((indirdep->ir_state & IOSTARTED) == 0) 11936 panic("handle_written_indirdep: IO not started"); 11937 chgs = 0; 11938 /* 11939 * If there were rollbacks revert them here. 11940 */ 11941 if (indirdep->ir_saveddata) { 11942 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount); 11943 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11944 free(indirdep->ir_saveddata, M_INDIRDEP); 11945 indirdep->ir_saveddata = NULL; 11946 } 11947 chgs = 1; 11948 } 11949 indirdep->ir_state &= ~(UNDONE | IOSTARTED); 11950 indirdep->ir_state |= ATTACHED; 11951 /* 11952 * If the write did not succeed, we have done all the roll-forward 11953 * operations, but we cannot take the actions that will allow its 11954 * dependencies to be processed. 11955 */ 11956 if ((flags & WRITESUCCEEDED) == 0) { 11957 stat_indir_blk_ptrs++; 11958 bdirty(bp); 11959 return (1); 11960 } 11961 /* 11962 * Move allocindirs with written pointers to the completehd if 11963 * the indirdep's pointer is not yet written. Otherwise 11964 * free them here. 11965 */ 11966 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) { 11967 LIST_REMOVE(aip, ai_next); 11968 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 11969 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip, 11970 ai_next); 11971 newblk_freefrag(&aip->ai_block); 11972 continue; 11973 } 11974 free_newblk(&aip->ai_block); 11975 } 11976 /* 11977 * Move allocindirs that have finished dependency processing from 11978 * the done list to the write list after updating the pointers. 11979 */ 11980 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11981 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) { 11982 handle_allocindir_partdone(aip); 11983 if (aip == LIST_FIRST(&indirdep->ir_donehd)) 11984 panic("disk_write_complete: not gone"); 11985 chgs = 1; 11986 } 11987 } 11988 /* 11989 * Preserve the indirdep if there were any changes or if it is not 11990 * yet valid on disk. 11991 */ 11992 if (chgs) { 11993 stat_indir_blk_ptrs++; 11994 bdirty(bp); 11995 return (1); 11996 } 11997 /* 11998 * If there were no changes we can discard the savedbp and detach 11999 * ourselves from the buf. We are only carrying completed pointers 12000 * in this case. 12001 */ 12002 sbp = indirdep->ir_savebp; 12003 sbp->b_flags |= B_INVAL | B_NOCACHE; 12004 indirdep->ir_savebp = NULL; 12005 indirdep->ir_bp = NULL; 12006 if (*bpp != NULL) 12007 panic("handle_written_indirdep: bp already exists."); 12008 *bpp = sbp; 12009 /* 12010 * The indirdep may not be freed until its parent points at it. 12011 */ 12012 if (indirdep->ir_state & DEPCOMPLETE) 12013 free_indirdep(indirdep); 12014 12015 return (0); 12016 } 12017 12018 /* 12019 * Process a diradd entry after its dependent inode has been written. 12020 */ 12021 static void 12022 diradd_inode_written( 12023 struct diradd *dap, 12024 struct inodedep *inodedep) 12025 { 12026 12027 LOCK_OWNED(VFSTOUFS(dap->da_list.wk_mp)); 12028 dap->da_state |= COMPLETE; 12029 complete_diradd(dap); 12030 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 12031 } 12032 12033 /* 12034 * Returns true if the bmsafemap will have rollbacks when written. Must only 12035 * be called with the per-filesystem lock and the buf lock on the cg held. 12036 */ 12037 static int 12038 bmsafemap_backgroundwrite( 12039 struct bmsafemap *bmsafemap, 12040 struct buf *bp) 12041 { 12042 int dirty; 12043 12044 LOCK_OWNED(VFSTOUFS(bmsafemap->sm_list.wk_mp)); 12045 dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) | 12046 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd); 12047 /* 12048 * If we're initiating a background write we need to process the 12049 * rollbacks as they exist now, not as they exist when IO starts. 12050 * No other consumers will look at the contents of the shadowed 12051 * buf so this is safe to do here. 12052 */ 12053 if (bp->b_xflags & BX_BKGRDMARKER) 12054 initiate_write_bmsafemap(bmsafemap, bp); 12055 12056 return (dirty); 12057 } 12058 12059 /* 12060 * Re-apply an allocation when a cg write is complete. 12061 */ 12062 static int 12063 jnewblk_rollforward( 12064 struct jnewblk *jnewblk, 12065 struct fs *fs, 12066 struct cg *cgp, 12067 uint8_t *blksfree) 12068 { 12069 ufs1_daddr_t fragno; 12070 ufs2_daddr_t blkno; 12071 long cgbno, bbase; 12072 int frags, blk; 12073 int i; 12074 12075 frags = 0; 12076 cgbno = dtogd(fs, jnewblk->jn_blkno); 12077 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) { 12078 if (isclr(blksfree, cgbno + i)) 12079 panic("jnewblk_rollforward: re-allocated fragment"); 12080 frags++; 12081 } 12082 if (frags == fs->fs_frag) { 12083 blkno = fragstoblks(fs, cgbno); 12084 ffs_clrblock(fs, blksfree, (long)blkno); 12085 ffs_clusteracct(fs, cgp, blkno, -1); 12086 cgp->cg_cs.cs_nbfree--; 12087 } else { 12088 bbase = cgbno - fragnum(fs, cgbno); 12089 cgbno += jnewblk->jn_oldfrags; 12090 /* If a complete block had been reassembled, account for it. */ 12091 fragno = fragstoblks(fs, bbase); 12092 if (ffs_isblock(fs, blksfree, fragno)) { 12093 cgp->cg_cs.cs_nffree += fs->fs_frag; 12094 ffs_clusteracct(fs, cgp, fragno, -1); 12095 cgp->cg_cs.cs_nbfree--; 12096 } 12097 /* Decrement the old frags. */ 12098 blk = blkmap(fs, blksfree, bbase); 12099 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 12100 /* Allocate the fragment */ 12101 for (i = 0; i < frags; i++) 12102 clrbit(blksfree, cgbno + i); 12103 cgp->cg_cs.cs_nffree -= frags; 12104 /* Add back in counts associated with the new frags */ 12105 blk = blkmap(fs, blksfree, bbase); 12106 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 12107 } 12108 return (frags); 12109 } 12110 12111 /* 12112 * Complete a write to a bmsafemap structure. Roll forward any bitmap 12113 * changes if it's not a background write. Set all written dependencies 12114 * to DEPCOMPLETE and free the structure if possible. 12115 * 12116 * If the write did not succeed, we will do all the roll-forward 12117 * operations, but we will not take the actions that will allow its 12118 * dependencies to be processed. 12119 */ 12120 static int 12121 handle_written_bmsafemap( 12122 struct bmsafemap *bmsafemap, 12123 struct buf *bp, 12124 int flags) 12125 { 12126 struct newblk *newblk; 12127 struct inodedep *inodedep; 12128 struct jaddref *jaddref, *jatmp; 12129 struct jnewblk *jnewblk, *jntmp; 12130 struct ufsmount *ump; 12131 uint8_t *inosused; 12132 uint8_t *blksfree; 12133 struct cg *cgp; 12134 struct fs *fs; 12135 ino_t ino; 12136 int foreground; 12137 int chgs; 12138 12139 if ((bmsafemap->sm_state & IOSTARTED) == 0) 12140 panic("handle_written_bmsafemap: Not started\n"); 12141 ump = VFSTOUFS(bmsafemap->sm_list.wk_mp); 12142 chgs = 0; 12143 bmsafemap->sm_state &= ~IOSTARTED; 12144 foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0; 12145 /* 12146 * If write was successful, release journal work that was waiting 12147 * on the write. Otherwise move the work back. 12148 */ 12149 if (flags & WRITESUCCEEDED) 12150 handle_jwork(&bmsafemap->sm_freewr); 12151 else 12152 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, 12153 worklist, wk_list); 12154 12155 /* 12156 * Restore unwritten inode allocation pending jaddref writes. 12157 */ 12158 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) { 12159 cgp = (struct cg *)bp->b_data; 12160 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 12161 inosused = cg_inosused(cgp); 12162 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd, 12163 ja_bmdeps, jatmp) { 12164 if ((jaddref->ja_state & UNDONE) == 0) 12165 continue; 12166 ino = jaddref->ja_ino % fs->fs_ipg; 12167 if (isset(inosused, ino)) 12168 panic("handle_written_bmsafemap: " 12169 "re-allocated inode"); 12170 /* Do the roll-forward only if it's a real copy. */ 12171 if (foreground) { 12172 if ((jaddref->ja_mode & IFMT) == IFDIR) 12173 cgp->cg_cs.cs_ndir++; 12174 cgp->cg_cs.cs_nifree--; 12175 setbit(inosused, ino); 12176 chgs = 1; 12177 } 12178 jaddref->ja_state &= ~UNDONE; 12179 jaddref->ja_state |= ATTACHED; 12180 free_jaddref(jaddref); 12181 } 12182 } 12183 /* 12184 * Restore any block allocations which are pending journal writes. 12185 */ 12186 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 12187 cgp = (struct cg *)bp->b_data; 12188 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 12189 blksfree = cg_blksfree(cgp); 12190 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps, 12191 jntmp) { 12192 if ((jnewblk->jn_state & UNDONE) == 0) 12193 continue; 12194 /* Do the roll-forward only if it's a real copy. */ 12195 if (foreground && 12196 jnewblk_rollforward(jnewblk, fs, cgp, blksfree)) 12197 chgs = 1; 12198 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK); 12199 jnewblk->jn_state |= ATTACHED; 12200 free_jnewblk(jnewblk); 12201 } 12202 } 12203 /* 12204 * If the write did not succeed, we have done all the roll-forward 12205 * operations, but we cannot take the actions that will allow its 12206 * dependencies to be processed. 12207 */ 12208 if ((flags & WRITESUCCEEDED) == 0) { 12209 LIST_CONCAT(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 12210 newblk, nb_deps); 12211 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, 12212 worklist, wk_list); 12213 if (foreground) 12214 bdirty(bp); 12215 return (1); 12216 } 12217 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) { 12218 newblk->nb_state |= DEPCOMPLETE; 12219 newblk->nb_state &= ~ONDEPLIST; 12220 newblk->nb_bmsafemap = NULL; 12221 LIST_REMOVE(newblk, nb_deps); 12222 if (newblk->nb_list.wk_type == D_ALLOCDIRECT) 12223 handle_allocdirect_partdone( 12224 WK_ALLOCDIRECT(&newblk->nb_list), NULL); 12225 else if (newblk->nb_list.wk_type == D_ALLOCINDIR) 12226 handle_allocindir_partdone( 12227 WK_ALLOCINDIR(&newblk->nb_list)); 12228 else if (newblk->nb_list.wk_type != D_NEWBLK) 12229 panic("handle_written_bmsafemap: Unexpected type: %s", 12230 TYPENAME(newblk->nb_list.wk_type)); 12231 } 12232 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) { 12233 inodedep->id_state |= DEPCOMPLETE; 12234 inodedep->id_state &= ~ONDEPLIST; 12235 LIST_REMOVE(inodedep, id_deps); 12236 inodedep->id_bmsafemap = NULL; 12237 } 12238 LIST_REMOVE(bmsafemap, sm_next); 12239 if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) && 12240 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) && 12241 LIST_EMPTY(&bmsafemap->sm_newblkhd) && 12242 LIST_EMPTY(&bmsafemap->sm_inodedephd) && 12243 LIST_EMPTY(&bmsafemap->sm_freehd)) { 12244 LIST_REMOVE(bmsafemap, sm_hash); 12245 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 12246 return (0); 12247 } 12248 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 12249 if (foreground) 12250 bdirty(bp); 12251 return (1); 12252 } 12253 12254 /* 12255 * Try to free a mkdir dependency. 12256 */ 12257 static void 12258 complete_mkdir(struct mkdir *mkdir) 12259 { 12260 struct diradd *dap; 12261 12262 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE) 12263 return; 12264 LIST_REMOVE(mkdir, md_mkdirs); 12265 dap = mkdir->md_diradd; 12266 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 12267 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) { 12268 dap->da_state |= DEPCOMPLETE; 12269 complete_diradd(dap); 12270 } 12271 WORKITEM_FREE(mkdir, D_MKDIR); 12272 } 12273 12274 /* 12275 * Handle the completion of a mkdir dependency. 12276 */ 12277 static void 12278 handle_written_mkdir(struct mkdir *mkdir, int type) 12279 { 12280 12281 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type) 12282 panic("handle_written_mkdir: bad type"); 12283 mkdir->md_state |= COMPLETE; 12284 complete_mkdir(mkdir); 12285 } 12286 12287 static int 12288 free_pagedep(struct pagedep *pagedep) 12289 { 12290 int i; 12291 12292 if (pagedep->pd_state & NEWBLOCK) 12293 return (0); 12294 if (!LIST_EMPTY(&pagedep->pd_dirremhd)) 12295 return (0); 12296 for (i = 0; i < DAHASHSZ; i++) 12297 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i])) 12298 return (0); 12299 if (!LIST_EMPTY(&pagedep->pd_pendinghd)) 12300 return (0); 12301 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd)) 12302 return (0); 12303 if (pagedep->pd_state & ONWORKLIST) 12304 WORKLIST_REMOVE(&pagedep->pd_list); 12305 LIST_REMOVE(pagedep, pd_hash); 12306 WORKITEM_FREE(pagedep, D_PAGEDEP); 12307 12308 return (1); 12309 } 12310 12311 /* 12312 * Called from within softdep_disk_write_complete above. 12313 * A write operation was just completed. Removed inodes can 12314 * now be freed and associated block pointers may be committed. 12315 * Note that this routine is always called from interrupt level 12316 * with further interrupts from this device blocked. 12317 * 12318 * If the write did not succeed, we will do all the roll-forward 12319 * operations, but we will not take the actions that will allow its 12320 * dependencies to be processed. 12321 */ 12322 static int 12323 handle_written_filepage( 12324 struct pagedep *pagedep, 12325 struct buf *bp, /* buffer containing the written page */ 12326 int flags) 12327 { 12328 struct dirrem *dirrem; 12329 struct diradd *dap, *nextdap; 12330 struct direct *ep; 12331 int i, chgs; 12332 12333 if ((pagedep->pd_state & IOSTARTED) == 0) 12334 panic("handle_written_filepage: not started"); 12335 pagedep->pd_state &= ~IOSTARTED; 12336 if ((flags & WRITESUCCEEDED) == 0) 12337 goto rollforward; 12338 /* 12339 * Process any directory removals that have been committed. 12340 */ 12341 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) { 12342 LIST_REMOVE(dirrem, dm_next); 12343 dirrem->dm_state |= COMPLETE; 12344 dirrem->dm_dirinum = pagedep->pd_ino; 12345 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 12346 ("handle_written_filepage: Journal entries not written.")); 12347 add_to_worklist(&dirrem->dm_list, 0); 12348 } 12349 /* 12350 * Free any directory additions that have been committed. 12351 * If it is a newly allocated block, we have to wait until 12352 * the on-disk directory inode claims the new block. 12353 */ 12354 if ((pagedep->pd_state & NEWBLOCK) == 0) 12355 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 12356 free_diradd(dap, NULL); 12357 rollforward: 12358 /* 12359 * Uncommitted directory entries must be restored. 12360 */ 12361 for (chgs = 0, i = 0; i < DAHASHSZ; i++) { 12362 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap; 12363 dap = nextdap) { 12364 nextdap = LIST_NEXT(dap, da_pdlist); 12365 if (dap->da_state & ATTACHED) 12366 panic("handle_written_filepage: attached"); 12367 ep = (struct direct *) 12368 ((char *)bp->b_data + dap->da_offset); 12369 ep->d_ino = dap->da_newinum; 12370 dap->da_state &= ~UNDONE; 12371 dap->da_state |= ATTACHED; 12372 chgs = 1; 12373 /* 12374 * If the inode referenced by the directory has 12375 * been written out, then the dependency can be 12376 * moved to the pending list. 12377 */ 12378 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 12379 LIST_REMOVE(dap, da_pdlist); 12380 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, 12381 da_pdlist); 12382 } 12383 } 12384 } 12385 /* 12386 * If there were any rollbacks in the directory, then it must be 12387 * marked dirty so that its will eventually get written back in 12388 * its correct form. 12389 */ 12390 if (chgs || (flags & WRITESUCCEEDED) == 0) { 12391 if ((bp->b_flags & B_DELWRI) == 0) 12392 stat_dir_entry++; 12393 bdirty(bp); 12394 return (1); 12395 } 12396 /* 12397 * If we are not waiting for a new directory block to be 12398 * claimed by its inode, then the pagedep will be freed. 12399 * Otherwise it will remain to track any new entries on 12400 * the page in case they are fsync'ed. 12401 */ 12402 free_pagedep(pagedep); 12403 return (0); 12404 } 12405 12406 /* 12407 * Writing back in-core inode structures. 12408 * 12409 * The filesystem only accesses an inode's contents when it occupies an 12410 * "in-core" inode structure. These "in-core" structures are separate from 12411 * the page frames used to cache inode blocks. Only the latter are 12412 * transferred to/from the disk. So, when the updated contents of the 12413 * "in-core" inode structure are copied to the corresponding in-memory inode 12414 * block, the dependencies are also transferred. The following procedure is 12415 * called when copying a dirty "in-core" inode to a cached inode block. 12416 */ 12417 12418 /* 12419 * Called when an inode is loaded from disk. If the effective link count 12420 * differed from the actual link count when it was last flushed, then we 12421 * need to ensure that the correct effective link count is put back. 12422 */ 12423 void 12424 softdep_load_inodeblock( 12425 struct inode *ip) /* the "in_core" copy of the inode */ 12426 { 12427 struct inodedep *inodedep; 12428 struct ufsmount *ump; 12429 12430 ump = ITOUMP(ip); 12431 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 12432 ("softdep_load_inodeblock called on non-softdep filesystem")); 12433 /* 12434 * Check for alternate nlink count. 12435 */ 12436 ip->i_effnlink = ip->i_nlink; 12437 ACQUIRE_LOCK(ump); 12438 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) { 12439 FREE_LOCK(ump); 12440 return; 12441 } 12442 if (ip->i_nlink != inodedep->id_nlinkwrote && 12443 inodedep->id_nlinkwrote != -1) { 12444 KASSERT(ip->i_nlink == 0 && 12445 (ump->um_flags & UM_FSFAIL_CLEANUP) != 0, 12446 ("read bad i_nlink value")); 12447 ip->i_effnlink = ip->i_nlink = inodedep->id_nlinkwrote; 12448 } 12449 ip->i_effnlink -= inodedep->id_nlinkdelta; 12450 KASSERT(ip->i_effnlink >= 0, 12451 ("softdep_load_inodeblock: negative i_effnlink")); 12452 FREE_LOCK(ump); 12453 } 12454 12455 /* 12456 * This routine is called just before the "in-core" inode 12457 * information is to be copied to the in-memory inode block. 12458 * Recall that an inode block contains several inodes. If 12459 * the force flag is set, then the dependencies will be 12460 * cleared so that the update can always be made. Note that 12461 * the buffer is locked when this routine is called, so we 12462 * will never be in the middle of writing the inode block 12463 * to disk. 12464 */ 12465 void 12466 softdep_update_inodeblock( 12467 struct inode *ip, /* the "in_core" copy of the inode */ 12468 struct buf *bp, /* the buffer containing the inode block */ 12469 int waitfor) /* nonzero => update must be allowed */ 12470 { 12471 struct inodedep *inodedep; 12472 struct inoref *inoref; 12473 struct ufsmount *ump; 12474 struct worklist *wk; 12475 struct mount *mp; 12476 struct buf *ibp; 12477 struct fs *fs; 12478 int error; 12479 12480 ump = ITOUMP(ip); 12481 mp = UFSTOVFS(ump); 12482 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 12483 ("softdep_update_inodeblock called on non-softdep filesystem")); 12484 fs = ump->um_fs; 12485 /* 12486 * If the effective link count is not equal to the actual link 12487 * count, then we must track the difference in an inodedep while 12488 * the inode is (potentially) tossed out of the cache. Otherwise, 12489 * if there is no existing inodedep, then there are no dependencies 12490 * to track. 12491 */ 12492 ACQUIRE_LOCK(ump); 12493 again: 12494 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12495 FREE_LOCK(ump); 12496 if (ip->i_effnlink != ip->i_nlink) 12497 panic("softdep_update_inodeblock: bad link count"); 12498 return; 12499 } 12500 /* 12501 * Preserve the freelink that is on disk. clear_unlinked_inodedep() 12502 * does not have access to the in-core ip so must write directly into 12503 * the inode block buffer when setting freelink. 12504 */ 12505 if ((inodedep->id_state & UNLINKED) != 0) { 12506 if (fs->fs_magic == FS_UFS1_MAGIC) 12507 DIP_SET(ip, i_freelink, 12508 ((struct ufs1_dinode *)bp->b_data + 12509 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12510 else 12511 DIP_SET(ip, i_freelink, 12512 ((struct ufs2_dinode *)bp->b_data + 12513 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12514 } 12515 KASSERT(ip->i_nlink >= inodedep->id_nlinkdelta, 12516 ("softdep_update_inodeblock inconsistent ip %p i_nlink %d " 12517 "inodedep %p id_nlinkdelta %jd", 12518 ip, ip->i_nlink, inodedep, (intmax_t)inodedep->id_nlinkdelta)); 12519 inodedep->id_nlinkwrote = ip->i_nlink; 12520 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink) 12521 panic("softdep_update_inodeblock: bad delta"); 12522 /* 12523 * If we're flushing all dependencies we must also move any waiting 12524 * for journal writes onto the bufwait list prior to I/O. 12525 */ 12526 if (waitfor) { 12527 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12528 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12529 == DEPCOMPLETE) { 12530 jwait(&inoref->if_list, MNT_WAIT); 12531 goto again; 12532 } 12533 } 12534 } 12535 /* 12536 * Changes have been initiated. Anything depending on these 12537 * changes cannot occur until this inode has been written. 12538 */ 12539 inodedep->id_state &= ~COMPLETE; 12540 if ((inodedep->id_state & ONWORKLIST) == 0) 12541 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list); 12542 /* 12543 * Any new dependencies associated with the incore inode must 12544 * now be moved to the list associated with the buffer holding 12545 * the in-memory copy of the inode. Once merged process any 12546 * allocdirects that are completed by the merger. 12547 */ 12548 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt); 12549 if (!TAILQ_EMPTY(&inodedep->id_inoupdt)) 12550 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt), 12551 NULL); 12552 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt); 12553 if (!TAILQ_EMPTY(&inodedep->id_extupdt)) 12554 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt), 12555 NULL); 12556 /* 12557 * Now that the inode has been pushed into the buffer, the 12558 * operations dependent on the inode being written to disk 12559 * can be moved to the id_bufwait so that they will be 12560 * processed when the buffer I/O completes. 12561 */ 12562 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) { 12563 WORKLIST_REMOVE(wk); 12564 WORKLIST_INSERT(&inodedep->id_bufwait, wk); 12565 } 12566 /* 12567 * Newly allocated inodes cannot be written until the bitmap 12568 * that allocates them have been written (indicated by 12569 * DEPCOMPLETE being set in id_state). If we are doing a 12570 * forced sync (e.g., an fsync on a file), we force the bitmap 12571 * to be written so that the update can be done. 12572 */ 12573 if (waitfor == 0) { 12574 FREE_LOCK(ump); 12575 return; 12576 } 12577 retry: 12578 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) { 12579 FREE_LOCK(ump); 12580 return; 12581 } 12582 ibp = inodedep->id_bmsafemap->sm_buf; 12583 ibp = getdirtybuf(ibp, LOCK_PTR(ump), MNT_WAIT); 12584 if (ibp == NULL) { 12585 /* 12586 * If ibp came back as NULL, the dependency could have been 12587 * freed while we slept. Look it up again, and check to see 12588 * that it has completed. 12589 */ 12590 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 12591 goto retry; 12592 FREE_LOCK(ump); 12593 return; 12594 } 12595 FREE_LOCK(ump); 12596 if ((error = bwrite(ibp)) != 0) 12597 softdep_error("softdep_update_inodeblock: bwrite", error); 12598 } 12599 12600 /* 12601 * Merge the a new inode dependency list (such as id_newinoupdt) into an 12602 * old inode dependency list (such as id_inoupdt). 12603 */ 12604 static void 12605 merge_inode_lists( 12606 struct allocdirectlst *newlisthead, 12607 struct allocdirectlst *oldlisthead) 12608 { 12609 struct allocdirect *listadp, *newadp; 12610 12611 newadp = TAILQ_FIRST(newlisthead); 12612 if (newadp != NULL) 12613 LOCK_OWNED(VFSTOUFS(newadp->ad_block.nb_list.wk_mp)); 12614 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) { 12615 if (listadp->ad_offset < newadp->ad_offset) { 12616 listadp = TAILQ_NEXT(listadp, ad_next); 12617 continue; 12618 } 12619 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12620 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next); 12621 if (listadp->ad_offset == newadp->ad_offset) { 12622 allocdirect_merge(oldlisthead, newadp, 12623 listadp); 12624 listadp = newadp; 12625 } 12626 newadp = TAILQ_FIRST(newlisthead); 12627 } 12628 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) { 12629 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12630 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next); 12631 } 12632 } 12633 12634 /* 12635 * If we are doing an fsync, then we must ensure that any directory 12636 * entries for the inode have been written after the inode gets to disk. 12637 */ 12638 int 12639 softdep_fsync( 12640 struct vnode *vp) /* the "in_core" copy of the inode */ 12641 { 12642 struct inodedep *inodedep; 12643 struct pagedep *pagedep; 12644 struct inoref *inoref; 12645 struct ufsmount *ump; 12646 struct worklist *wk; 12647 struct diradd *dap; 12648 struct mount *mp; 12649 struct vnode *pvp; 12650 struct inode *ip; 12651 struct buf *bp; 12652 struct fs *fs; 12653 struct thread *td = curthread; 12654 int error, flushparent, pagedep_new_block; 12655 ino_t parentino; 12656 ufs_lbn_t lbn; 12657 12658 ip = VTOI(vp); 12659 mp = vp->v_mount; 12660 ump = VFSTOUFS(mp); 12661 fs = ump->um_fs; 12662 if (MOUNTEDSOFTDEP(mp) == 0) 12663 return (0); 12664 ACQUIRE_LOCK(ump); 12665 restart: 12666 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12667 FREE_LOCK(ump); 12668 return (0); 12669 } 12670 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12671 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12672 == DEPCOMPLETE) { 12673 jwait(&inoref->if_list, MNT_WAIT); 12674 goto restart; 12675 } 12676 } 12677 if (!LIST_EMPTY(&inodedep->id_inowait) || 12678 !TAILQ_EMPTY(&inodedep->id_extupdt) || 12679 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 12680 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 12681 !TAILQ_EMPTY(&inodedep->id_newinoupdt)) 12682 panic("softdep_fsync: pending ops %p", inodedep); 12683 for (error = 0, flushparent = 0; ; ) { 12684 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL) 12685 break; 12686 if (wk->wk_type != D_DIRADD) 12687 panic("softdep_fsync: Unexpected type %s", 12688 TYPENAME(wk->wk_type)); 12689 dap = WK_DIRADD(wk); 12690 /* 12691 * Flush our parent if this directory entry has a MKDIR_PARENT 12692 * dependency or is contained in a newly allocated block. 12693 */ 12694 if (dap->da_state & DIRCHG) 12695 pagedep = dap->da_previous->dm_pagedep; 12696 else 12697 pagedep = dap->da_pagedep; 12698 parentino = pagedep->pd_ino; 12699 lbn = pagedep->pd_lbn; 12700 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) 12701 panic("softdep_fsync: dirty"); 12702 if ((dap->da_state & MKDIR_PARENT) || 12703 (pagedep->pd_state & NEWBLOCK)) 12704 flushparent = 1; 12705 else 12706 flushparent = 0; 12707 /* 12708 * If we are being fsync'ed as part of vgone'ing this vnode, 12709 * then we will not be able to release and recover the 12710 * vnode below, so we just have to give up on writing its 12711 * directory entry out. It will eventually be written, just 12712 * not now, but then the user was not asking to have it 12713 * written, so we are not breaking any promises. 12714 */ 12715 if (VN_IS_DOOMED(vp)) 12716 break; 12717 /* 12718 * We prevent deadlock by always fetching inodes from the 12719 * root, moving down the directory tree. Thus, when fetching 12720 * our parent directory, we first try to get the lock. If 12721 * that fails, we must unlock ourselves before requesting 12722 * the lock on our parent. See the comment in ufs_lookup 12723 * for details on possible races. 12724 */ 12725 FREE_LOCK(ump); 12726 error = get_parent_vp(vp, mp, parentino, NULL, NULL, NULL, 12727 &pvp); 12728 if (error == ERELOOKUP) 12729 error = 0; 12730 if (error != 0) 12731 return (error); 12732 /* 12733 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps 12734 * that are contained in direct blocks will be resolved by 12735 * doing a ffs_update. Pagedeps contained in indirect blocks 12736 * may require a complete sync'ing of the directory. So, we 12737 * try the cheap and fast ffs_update first, and if that fails, 12738 * then we do the slower ffs_syncvnode of the directory. 12739 */ 12740 if (flushparent) { 12741 int locked; 12742 12743 if ((error = ffs_update(pvp, 1)) != 0) { 12744 vput(pvp); 12745 return (error); 12746 } 12747 ACQUIRE_LOCK(ump); 12748 locked = 1; 12749 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) { 12750 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) { 12751 if (wk->wk_type != D_DIRADD) 12752 panic("softdep_fsync: Unexpected type %s", 12753 TYPENAME(wk->wk_type)); 12754 dap = WK_DIRADD(wk); 12755 if (dap->da_state & DIRCHG) 12756 pagedep = dap->da_previous->dm_pagedep; 12757 else 12758 pagedep = dap->da_pagedep; 12759 pagedep_new_block = pagedep->pd_state & NEWBLOCK; 12760 FREE_LOCK(ump); 12761 locked = 0; 12762 if (pagedep_new_block) { 12763 VOP_UNLOCK(vp); 12764 error = ffs_syncvnode(pvp, 12765 MNT_WAIT, 0); 12766 if (error == 0) 12767 error = ERELOOKUP; 12768 vput(pvp); 12769 vn_lock(vp, LK_EXCLUSIVE | 12770 LK_RETRY); 12771 return (error); 12772 } 12773 } 12774 } 12775 if (locked) 12776 FREE_LOCK(ump); 12777 } 12778 /* 12779 * Flush directory page containing the inode's name. 12780 */ 12781 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred, 12782 &bp); 12783 if (error == 0) 12784 error = bwrite(bp); 12785 else 12786 brelse(bp); 12787 vput(pvp); 12788 if (!ffs_fsfail_cleanup(ump, error)) 12789 return (error); 12790 ACQUIRE_LOCK(ump); 12791 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 12792 break; 12793 } 12794 FREE_LOCK(ump); 12795 return (0); 12796 } 12797 12798 /* 12799 * Flush all the dirty bitmaps associated with the block device 12800 * before flushing the rest of the dirty blocks so as to reduce 12801 * the number of dependencies that will have to be rolled back. 12802 * 12803 * XXX Unused? 12804 */ 12805 void 12806 softdep_fsync_mountdev(struct vnode *vp) 12807 { 12808 struct buf *bp, *nbp; 12809 struct worklist *wk; 12810 struct bufobj *bo; 12811 12812 if (!vn_isdisk(vp)) 12813 panic("softdep_fsync_mountdev: vnode not a disk"); 12814 bo = &vp->v_bufobj; 12815 restart: 12816 BO_LOCK(bo); 12817 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 12818 /* 12819 * If it is already scheduled, skip to the next buffer. 12820 */ 12821 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) 12822 continue; 12823 12824 if ((bp->b_flags & B_DELWRI) == 0) 12825 panic("softdep_fsync_mountdev: not dirty"); 12826 /* 12827 * We are only interested in bitmaps with outstanding 12828 * dependencies. 12829 */ 12830 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL || 12831 wk->wk_type != D_BMSAFEMAP || 12832 (bp->b_vflags & BV_BKGRDINPROG)) { 12833 BUF_UNLOCK(bp); 12834 continue; 12835 } 12836 BO_UNLOCK(bo); 12837 bremfree(bp); 12838 (void) bawrite(bp); 12839 goto restart; 12840 } 12841 drain_output(vp); 12842 BO_UNLOCK(bo); 12843 } 12844 12845 /* 12846 * Sync all cylinder groups that were dirty at the time this function is 12847 * called. Newly dirtied cgs will be inserted before the sentinel. This 12848 * is used to flush freedep activity that may be holding up writes to a 12849 * indirect block. 12850 */ 12851 static int 12852 sync_cgs(struct mount *mp, int waitfor) 12853 { 12854 struct bmsafemap *bmsafemap; 12855 struct bmsafemap *sentinel; 12856 struct ufsmount *ump; 12857 struct buf *bp; 12858 int error; 12859 12860 sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK); 12861 sentinel->sm_cg = -1; 12862 ump = VFSTOUFS(mp); 12863 error = 0; 12864 ACQUIRE_LOCK(ump); 12865 LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next); 12866 for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL; 12867 bmsafemap = LIST_NEXT(sentinel, sm_next)) { 12868 /* Skip sentinels and cgs with no work to release. */ 12869 if (bmsafemap->sm_cg == -1 || 12870 (LIST_EMPTY(&bmsafemap->sm_freehd) && 12871 LIST_EMPTY(&bmsafemap->sm_freewr))) { 12872 LIST_REMOVE(sentinel, sm_next); 12873 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 12874 continue; 12875 } 12876 /* 12877 * If we don't get the lock and we're waiting try again, if 12878 * not move on to the next buf and try to sync it. 12879 */ 12880 bp = getdirtybuf(bmsafemap->sm_buf, LOCK_PTR(ump), waitfor); 12881 if (bp == NULL && waitfor == MNT_WAIT) 12882 continue; 12883 LIST_REMOVE(sentinel, sm_next); 12884 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 12885 if (bp == NULL) 12886 continue; 12887 FREE_LOCK(ump); 12888 if (waitfor == MNT_NOWAIT) 12889 bawrite(bp); 12890 else 12891 error = bwrite(bp); 12892 ACQUIRE_LOCK(ump); 12893 if (error) 12894 break; 12895 } 12896 LIST_REMOVE(sentinel, sm_next); 12897 FREE_LOCK(ump); 12898 free(sentinel, M_BMSAFEMAP); 12899 return (error); 12900 } 12901 12902 /* 12903 * This routine is called when we are trying to synchronously flush a 12904 * file. This routine must eliminate any filesystem metadata dependencies 12905 * so that the syncing routine can succeed. 12906 */ 12907 int 12908 softdep_sync_metadata(struct vnode *vp) 12909 { 12910 struct inode *ip; 12911 int error; 12912 12913 ip = VTOI(vp); 12914 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 12915 ("softdep_sync_metadata called on non-softdep filesystem")); 12916 /* 12917 * Ensure that any direct block dependencies have been cleared, 12918 * truncations are started, and inode references are journaled. 12919 */ 12920 ACQUIRE_LOCK(VFSTOUFS(vp->v_mount)); 12921 /* 12922 * Write all journal records to prevent rollbacks on devvp. 12923 */ 12924 if (vp->v_type == VCHR) 12925 softdep_flushjournal(vp->v_mount); 12926 error = flush_inodedep_deps(vp, vp->v_mount, ip->i_number); 12927 /* 12928 * Ensure that all truncates are written so we won't find deps on 12929 * indirect blocks. 12930 */ 12931 process_truncates(vp); 12932 FREE_LOCK(VFSTOUFS(vp->v_mount)); 12933 12934 return (error); 12935 } 12936 12937 /* 12938 * This routine is called when we are attempting to sync a buf with 12939 * dependencies. If waitfor is MNT_NOWAIT it attempts to schedule any 12940 * other IO it can but returns EBUSY if the buffer is not yet able to 12941 * be written. Dependencies which will not cause rollbacks will always 12942 * return 0. 12943 */ 12944 int 12945 softdep_sync_buf(struct vnode *vp, 12946 struct buf *bp, 12947 int waitfor) 12948 { 12949 struct indirdep *indirdep; 12950 struct pagedep *pagedep; 12951 struct allocindir *aip; 12952 struct newblk *newblk; 12953 struct ufsmount *ump; 12954 struct buf *nbp; 12955 struct worklist *wk; 12956 int i, error; 12957 12958 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 12959 ("softdep_sync_buf called on non-softdep filesystem")); 12960 /* 12961 * For VCHR we just don't want to force flush any dependencies that 12962 * will cause rollbacks. 12963 */ 12964 if (vp->v_type == VCHR) { 12965 if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0)) 12966 return (EBUSY); 12967 return (0); 12968 } 12969 ump = VFSTOUFS(vp->v_mount); 12970 ACQUIRE_LOCK(ump); 12971 /* 12972 * As we hold the buffer locked, none of its dependencies 12973 * will disappear. 12974 */ 12975 error = 0; 12976 top: 12977 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 12978 switch (wk->wk_type) { 12979 case D_ALLOCDIRECT: 12980 case D_ALLOCINDIR: 12981 newblk = WK_NEWBLK(wk); 12982 if (newblk->nb_jnewblk != NULL) { 12983 if (waitfor == MNT_NOWAIT) { 12984 error = EBUSY; 12985 goto out_unlock; 12986 } 12987 jwait(&newblk->nb_jnewblk->jn_list, waitfor); 12988 goto top; 12989 } 12990 if (newblk->nb_state & DEPCOMPLETE || 12991 waitfor == MNT_NOWAIT) 12992 continue; 12993 nbp = newblk->nb_bmsafemap->sm_buf; 12994 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 12995 if (nbp == NULL) 12996 goto top; 12997 FREE_LOCK(ump); 12998 if ((error = bwrite(nbp)) != 0) 12999 goto out; 13000 ACQUIRE_LOCK(ump); 13001 continue; 13002 13003 case D_INDIRDEP: 13004 indirdep = WK_INDIRDEP(wk); 13005 if (waitfor == MNT_NOWAIT) { 13006 if (!TAILQ_EMPTY(&indirdep->ir_trunc) || 13007 !LIST_EMPTY(&indirdep->ir_deplisthd)) { 13008 error = EBUSY; 13009 goto out_unlock; 13010 } 13011 } 13012 if (!TAILQ_EMPTY(&indirdep->ir_trunc)) 13013 panic("softdep_sync_buf: truncation pending."); 13014 restart: 13015 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 13016 newblk = (struct newblk *)aip; 13017 if (newblk->nb_jnewblk != NULL) { 13018 jwait(&newblk->nb_jnewblk->jn_list, 13019 waitfor); 13020 goto restart; 13021 } 13022 if (newblk->nb_state & DEPCOMPLETE) 13023 continue; 13024 nbp = newblk->nb_bmsafemap->sm_buf; 13025 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 13026 if (nbp == NULL) 13027 goto restart; 13028 FREE_LOCK(ump); 13029 if ((error = bwrite(nbp)) != 0) 13030 goto out; 13031 ACQUIRE_LOCK(ump); 13032 goto restart; 13033 } 13034 continue; 13035 13036 case D_PAGEDEP: 13037 /* 13038 * Only flush directory entries in synchronous passes. 13039 */ 13040 if (waitfor != MNT_WAIT) { 13041 error = EBUSY; 13042 goto out_unlock; 13043 } 13044 /* 13045 * While syncing snapshots, we must allow recursive 13046 * lookups. 13047 */ 13048 BUF_AREC(bp); 13049 /* 13050 * We are trying to sync a directory that may 13051 * have dependencies on both its own metadata 13052 * and/or dependencies on the inodes of any 13053 * recently allocated files. We walk its diradd 13054 * lists pushing out the associated inode. 13055 */ 13056 pagedep = WK_PAGEDEP(wk); 13057 for (i = 0; i < DAHASHSZ; i++) { 13058 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0) 13059 continue; 13060 error = flush_pagedep_deps(vp, wk->wk_mp, 13061 &pagedep->pd_diraddhd[i], bp); 13062 if (error != 0) { 13063 if (error != ERELOOKUP) 13064 BUF_NOREC(bp); 13065 goto out_unlock; 13066 } 13067 } 13068 BUF_NOREC(bp); 13069 continue; 13070 13071 case D_FREEWORK: 13072 case D_FREEDEP: 13073 case D_JSEGDEP: 13074 case D_JNEWBLK: 13075 continue; 13076 13077 default: 13078 panic("softdep_sync_buf: Unknown type %s", 13079 TYPENAME(wk->wk_type)); 13080 /* NOTREACHED */ 13081 } 13082 } 13083 out_unlock: 13084 FREE_LOCK(ump); 13085 out: 13086 return (error); 13087 } 13088 13089 /* 13090 * Flush the dependencies associated with an inodedep. 13091 */ 13092 static int 13093 flush_inodedep_deps( 13094 struct vnode *vp, 13095 struct mount *mp, 13096 ino_t ino) 13097 { 13098 struct inodedep *inodedep; 13099 struct inoref *inoref; 13100 struct ufsmount *ump; 13101 int error, waitfor; 13102 13103 /* 13104 * This work is done in two passes. The first pass grabs most 13105 * of the buffers and begins asynchronously writing them. The 13106 * only way to wait for these asynchronous writes is to sleep 13107 * on the filesystem vnode which may stay busy for a long time 13108 * if the filesystem is active. So, instead, we make a second 13109 * pass over the dependencies blocking on each write. In the 13110 * usual case we will be blocking against a write that we 13111 * initiated, so when it is done the dependency will have been 13112 * resolved. Thus the second pass is expected to end quickly. 13113 * We give a brief window at the top of the loop to allow 13114 * any pending I/O to complete. 13115 */ 13116 ump = VFSTOUFS(mp); 13117 LOCK_OWNED(ump); 13118 for (error = 0, waitfor = MNT_NOWAIT; ; ) { 13119 if (error) 13120 return (error); 13121 FREE_LOCK(ump); 13122 ACQUIRE_LOCK(ump); 13123 restart: 13124 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 13125 return (0); 13126 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 13127 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 13128 == DEPCOMPLETE) { 13129 jwait(&inoref->if_list, MNT_WAIT); 13130 goto restart; 13131 } 13132 } 13133 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) || 13134 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) || 13135 flush_deplist(&inodedep->id_extupdt, waitfor, &error) || 13136 flush_deplist(&inodedep->id_newextupdt, waitfor, &error)) 13137 continue; 13138 /* 13139 * If pass2, we are done, otherwise do pass 2. 13140 */ 13141 if (waitfor == MNT_WAIT) 13142 break; 13143 waitfor = MNT_WAIT; 13144 } 13145 /* 13146 * Try freeing inodedep in case all dependencies have been removed. 13147 */ 13148 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0) 13149 (void) free_inodedep(inodedep); 13150 return (0); 13151 } 13152 13153 /* 13154 * Flush an inode dependency list. 13155 */ 13156 static int 13157 flush_deplist( 13158 struct allocdirectlst *listhead, 13159 int waitfor, 13160 int *errorp) 13161 { 13162 struct allocdirect *adp; 13163 struct newblk *newblk; 13164 struct ufsmount *ump; 13165 struct buf *bp; 13166 13167 if ((adp = TAILQ_FIRST(listhead)) == NULL) 13168 return (0); 13169 ump = VFSTOUFS(adp->ad_list.wk_mp); 13170 LOCK_OWNED(ump); 13171 TAILQ_FOREACH(adp, listhead, ad_next) { 13172 newblk = (struct newblk *)adp; 13173 if (newblk->nb_jnewblk != NULL) { 13174 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 13175 return (1); 13176 } 13177 if (newblk->nb_state & DEPCOMPLETE) 13178 continue; 13179 bp = newblk->nb_bmsafemap->sm_buf; 13180 bp = getdirtybuf(bp, LOCK_PTR(ump), waitfor); 13181 if (bp == NULL) { 13182 if (waitfor == MNT_NOWAIT) 13183 continue; 13184 return (1); 13185 } 13186 FREE_LOCK(ump); 13187 if (waitfor == MNT_NOWAIT) 13188 bawrite(bp); 13189 else 13190 *errorp = bwrite(bp); 13191 ACQUIRE_LOCK(ump); 13192 return (1); 13193 } 13194 return (0); 13195 } 13196 13197 /* 13198 * Flush dependencies associated with an allocdirect block. 13199 */ 13200 static int 13201 flush_newblk_dep( 13202 struct vnode *vp, 13203 struct mount *mp, 13204 ufs_lbn_t lbn) 13205 { 13206 struct newblk *newblk; 13207 struct ufsmount *ump; 13208 struct bufobj *bo; 13209 struct inode *ip; 13210 struct buf *bp; 13211 ufs2_daddr_t blkno; 13212 int error; 13213 13214 error = 0; 13215 bo = &vp->v_bufobj; 13216 ip = VTOI(vp); 13217 blkno = DIP(ip, i_db[lbn]); 13218 if (blkno == 0) 13219 panic("flush_newblk_dep: Missing block"); 13220 ump = VFSTOUFS(mp); 13221 ACQUIRE_LOCK(ump); 13222 /* 13223 * Loop until all dependencies related to this block are satisfied. 13224 * We must be careful to restart after each sleep in case a write 13225 * completes some part of this process for us. 13226 */ 13227 for (;;) { 13228 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) { 13229 FREE_LOCK(ump); 13230 break; 13231 } 13232 if (newblk->nb_list.wk_type != D_ALLOCDIRECT) 13233 panic("flush_newblk_dep: Bad newblk %p", newblk); 13234 /* 13235 * Flush the journal. 13236 */ 13237 if (newblk->nb_jnewblk != NULL) { 13238 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 13239 continue; 13240 } 13241 /* 13242 * Write the bitmap dependency. 13243 */ 13244 if ((newblk->nb_state & DEPCOMPLETE) == 0) { 13245 bp = newblk->nb_bmsafemap->sm_buf; 13246 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 13247 if (bp == NULL) 13248 continue; 13249 FREE_LOCK(ump); 13250 error = bwrite(bp); 13251 if (error) 13252 break; 13253 ACQUIRE_LOCK(ump); 13254 continue; 13255 } 13256 /* 13257 * Write the buffer. 13258 */ 13259 FREE_LOCK(ump); 13260 BO_LOCK(bo); 13261 bp = gbincore(bo, lbn); 13262 if (bp != NULL) { 13263 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 13264 LK_INTERLOCK, BO_LOCKPTR(bo)); 13265 if (error == ENOLCK) { 13266 ACQUIRE_LOCK(ump); 13267 error = 0; 13268 continue; /* Slept, retry */ 13269 } 13270 if (error != 0) 13271 break; /* Failed */ 13272 if (bp->b_flags & B_DELWRI) { 13273 bremfree(bp); 13274 error = bwrite(bp); 13275 if (error) 13276 break; 13277 } else 13278 BUF_UNLOCK(bp); 13279 } else 13280 BO_UNLOCK(bo); 13281 /* 13282 * We have to wait for the direct pointers to 13283 * point at the newdirblk before the dependency 13284 * will go away. 13285 */ 13286 error = ffs_update(vp, 1); 13287 if (error) 13288 break; 13289 ACQUIRE_LOCK(ump); 13290 } 13291 return (error); 13292 } 13293 13294 /* 13295 * Eliminate a pagedep dependency by flushing out all its diradd dependencies. 13296 */ 13297 static int 13298 flush_pagedep_deps( 13299 struct vnode *pvp, 13300 struct mount *mp, 13301 struct diraddhd *diraddhdp, 13302 struct buf *locked_bp) 13303 { 13304 struct inodedep *inodedep; 13305 struct inoref *inoref; 13306 struct ufsmount *ump; 13307 struct diradd *dap; 13308 struct vnode *vp; 13309 int error = 0; 13310 struct buf *bp; 13311 ino_t inum; 13312 struct diraddhd unfinished; 13313 13314 LIST_INIT(&unfinished); 13315 ump = VFSTOUFS(mp); 13316 LOCK_OWNED(ump); 13317 restart: 13318 while ((dap = LIST_FIRST(diraddhdp)) != NULL) { 13319 /* 13320 * Flush ourselves if this directory entry 13321 * has a MKDIR_PARENT dependency. 13322 */ 13323 if (dap->da_state & MKDIR_PARENT) { 13324 FREE_LOCK(ump); 13325 if ((error = ffs_update(pvp, 1)) != 0) 13326 break; 13327 ACQUIRE_LOCK(ump); 13328 /* 13329 * If that cleared dependencies, go on to next. 13330 */ 13331 if (dap != LIST_FIRST(diraddhdp)) 13332 continue; 13333 /* 13334 * All MKDIR_PARENT dependencies and all the 13335 * NEWBLOCK pagedeps that are contained in direct 13336 * blocks were resolved by doing above ffs_update. 13337 * Pagedeps contained in indirect blocks may 13338 * require a complete sync'ing of the directory. 13339 * We are in the midst of doing a complete sync, 13340 * so if they are not resolved in this pass we 13341 * defer them for now as they will be sync'ed by 13342 * our caller shortly. 13343 */ 13344 LIST_REMOVE(dap, da_pdlist); 13345 LIST_INSERT_HEAD(&unfinished, dap, da_pdlist); 13346 continue; 13347 } 13348 /* 13349 * A newly allocated directory must have its "." and 13350 * ".." entries written out before its name can be 13351 * committed in its parent. 13352 */ 13353 inum = dap->da_newinum; 13354 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 13355 panic("flush_pagedep_deps: lost inode1"); 13356 /* 13357 * Wait for any pending journal adds to complete so we don't 13358 * cause rollbacks while syncing. 13359 */ 13360 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 13361 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 13362 == DEPCOMPLETE) { 13363 jwait(&inoref->if_list, MNT_WAIT); 13364 goto restart; 13365 } 13366 } 13367 if (dap->da_state & MKDIR_BODY) { 13368 FREE_LOCK(ump); 13369 error = get_parent_vp(pvp, mp, inum, locked_bp, 13370 diraddhdp, &unfinished, &vp); 13371 if (error != 0) 13372 break; 13373 error = flush_newblk_dep(vp, mp, 0); 13374 /* 13375 * If we still have the dependency we might need to 13376 * update the vnode to sync the new link count to 13377 * disk. 13378 */ 13379 if (error == 0 && dap == LIST_FIRST(diraddhdp)) 13380 error = ffs_update(vp, 1); 13381 vput(vp); 13382 if (error != 0) 13383 break; 13384 ACQUIRE_LOCK(ump); 13385 /* 13386 * If that cleared dependencies, go on to next. 13387 */ 13388 if (dap != LIST_FIRST(diraddhdp)) 13389 continue; 13390 if (dap->da_state & MKDIR_BODY) { 13391 inodedep_lookup(UFSTOVFS(ump), inum, 0, 13392 &inodedep); 13393 panic("flush_pagedep_deps: MKDIR_BODY " 13394 "inodedep %p dap %p vp %p", 13395 inodedep, dap, vp); 13396 } 13397 } 13398 /* 13399 * Flush the inode on which the directory entry depends. 13400 * Having accounted for MKDIR_PARENT and MKDIR_BODY above, 13401 * the only remaining dependency is that the updated inode 13402 * count must get pushed to disk. The inode has already 13403 * been pushed into its inode buffer (via VOP_UPDATE) at 13404 * the time of the reference count change. So we need only 13405 * locate that buffer, ensure that there will be no rollback 13406 * caused by a bitmap dependency, then write the inode buffer. 13407 */ 13408 retry: 13409 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 13410 panic("flush_pagedep_deps: lost inode"); 13411 /* 13412 * If the inode still has bitmap dependencies, 13413 * push them to disk. 13414 */ 13415 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) { 13416 bp = inodedep->id_bmsafemap->sm_buf; 13417 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 13418 if (bp == NULL) 13419 goto retry; 13420 FREE_LOCK(ump); 13421 if ((error = bwrite(bp)) != 0) 13422 break; 13423 ACQUIRE_LOCK(ump); 13424 if (dap != LIST_FIRST(diraddhdp)) 13425 continue; 13426 } 13427 /* 13428 * If the inode is still sitting in a buffer waiting 13429 * to be written or waiting for the link count to be 13430 * adjusted update it here to flush it to disk. 13431 */ 13432 if (dap == LIST_FIRST(diraddhdp)) { 13433 FREE_LOCK(ump); 13434 error = get_parent_vp(pvp, mp, inum, locked_bp, 13435 diraddhdp, &unfinished, &vp); 13436 if (error != 0) 13437 break; 13438 error = ffs_update(vp, 1); 13439 vput(vp); 13440 if (error) 13441 break; 13442 ACQUIRE_LOCK(ump); 13443 } 13444 /* 13445 * If we have failed to get rid of all the dependencies 13446 * then something is seriously wrong. 13447 */ 13448 if (dap == LIST_FIRST(diraddhdp)) { 13449 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep); 13450 panic("flush_pagedep_deps: failed to flush " 13451 "inodedep %p ino %ju dap %p", 13452 inodedep, (uintmax_t)inum, dap); 13453 } 13454 } 13455 if (error) 13456 ACQUIRE_LOCK(ump); 13457 while ((dap = LIST_FIRST(&unfinished)) != NULL) { 13458 LIST_REMOVE(dap, da_pdlist); 13459 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist); 13460 } 13461 return (error); 13462 } 13463 13464 /* 13465 * A large burst of file addition or deletion activity can drive the 13466 * memory load excessively high. First attempt to slow things down 13467 * using the techniques below. If that fails, this routine requests 13468 * the offending operations to fall back to running synchronously 13469 * until the memory load returns to a reasonable level. 13470 */ 13471 int 13472 softdep_slowdown(struct vnode *vp) 13473 { 13474 struct ufsmount *ump; 13475 int jlow; 13476 int max_softdeps_hard; 13477 13478 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 13479 ("softdep_slowdown called on non-softdep filesystem")); 13480 ump = VFSTOUFS(vp->v_mount); 13481 ACQUIRE_LOCK(ump); 13482 jlow = 0; 13483 /* 13484 * Check for journal space if needed. 13485 */ 13486 if (DOINGSUJ(vp)) { 13487 if (journal_space(ump, 0) == 0) 13488 jlow = 1; 13489 } 13490 /* 13491 * If the system is under its limits and our filesystem is 13492 * not responsible for more than our share of the usage and 13493 * we are not low on journal space, then no need to slow down. 13494 */ 13495 max_softdeps_hard = max_softdeps * 11 / 10; 13496 if (dep_current[D_DIRREM] < max_softdeps_hard / 2 && 13497 dep_current[D_INODEDEP] < max_softdeps_hard && 13498 dep_current[D_INDIRDEP] < max_softdeps_hard / 1000 && 13499 dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0 && 13500 ump->softdep_curdeps[D_DIRREM] < 13501 (max_softdeps_hard / 2) / stat_flush_threads && 13502 ump->softdep_curdeps[D_INODEDEP] < 13503 max_softdeps_hard / stat_flush_threads && 13504 ump->softdep_curdeps[D_INDIRDEP] < 13505 (max_softdeps_hard / 1000) / stat_flush_threads && 13506 ump->softdep_curdeps[D_FREEBLKS] < 13507 max_softdeps_hard / stat_flush_threads) { 13508 FREE_LOCK(ump); 13509 return (0); 13510 } 13511 /* 13512 * If the journal is low or our filesystem is over its limit 13513 * then speedup the cleanup. 13514 */ 13515 if (ump->softdep_curdeps[D_INDIRDEP] < 13516 (max_softdeps_hard / 1000) / stat_flush_threads || jlow) 13517 softdep_speedup(ump); 13518 stat_sync_limit_hit += 1; 13519 FREE_LOCK(ump); 13520 /* 13521 * We only slow down the rate at which new dependencies are 13522 * generated if we are not using journaling. With journaling, 13523 * the cleanup should always be sufficient to keep things 13524 * under control. 13525 */ 13526 if (DOINGSUJ(vp)) 13527 return (0); 13528 return (1); 13529 } 13530 13531 static int 13532 softdep_request_cleanup_filter(struct vnode *vp, void *arg __unused) 13533 { 13534 return ((vp->v_iflag & VI_OWEINACT) != 0 && vp->v_usecount == 0 && 13535 ((vp->v_vflag & VV_NOSYNC) != 0 || VTOI(vp)->i_effnlink == 0)); 13536 } 13537 13538 static void 13539 softdep_request_cleanup_inactivate(struct mount *mp) 13540 { 13541 struct vnode *vp, *mvp; 13542 int error; 13543 13544 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, softdep_request_cleanup_filter, 13545 NULL) { 13546 vholdl(vp); 13547 vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY); 13548 VI_LOCK(vp); 13549 if (IS_UFS(vp) && vp->v_usecount == 0) { 13550 while ((vp->v_iflag & VI_OWEINACT) != 0) { 13551 error = vinactive(vp); 13552 if (error != 0 && error != ERELOOKUP) 13553 break; 13554 } 13555 atomic_add_int(&stat_delayed_inact, 1); 13556 } 13557 VOP_UNLOCK(vp); 13558 vdropl(vp); 13559 } 13560 } 13561 13562 /* 13563 * Called by the allocation routines when they are about to fail 13564 * in the hope that we can free up the requested resource (inodes 13565 * or disk space). 13566 * 13567 * First check to see if the work list has anything on it. If it has, 13568 * clean up entries until we successfully free the requested resource. 13569 * Because this process holds inodes locked, we cannot handle any remove 13570 * requests that might block on a locked inode as that could lead to 13571 * deadlock. If the worklist yields none of the requested resource, 13572 * start syncing out vnodes to free up the needed space. 13573 */ 13574 int 13575 softdep_request_cleanup( 13576 struct fs *fs, 13577 struct vnode *vp, 13578 struct ucred *cred, 13579 int resource) 13580 { 13581 struct ufsmount *ump; 13582 struct mount *mp; 13583 long starttime; 13584 ufs2_daddr_t needed; 13585 int error, failed_vnode; 13586 13587 /* 13588 * If we are being called because of a process doing a 13589 * copy-on-write, then it is not safe to process any 13590 * worklist items as we will recurse into the copyonwrite 13591 * routine. This will result in an incoherent snapshot. 13592 * If the vnode that we hold is a snapshot, we must avoid 13593 * handling other resources that could cause deadlock. 13594 */ 13595 if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp))) 13596 return (0); 13597 13598 if (resource == FLUSH_BLOCKS_WAIT) 13599 stat_cleanup_blkrequests += 1; 13600 else 13601 stat_cleanup_inorequests += 1; 13602 13603 mp = vp->v_mount; 13604 ump = VFSTOUFS(mp); 13605 mtx_assert(UFS_MTX(ump), MA_OWNED); 13606 UFS_UNLOCK(ump); 13607 error = ffs_update(vp, 1); 13608 if (error != 0 || MOUNTEDSOFTDEP(mp) == 0) { 13609 UFS_LOCK(ump); 13610 return (0); 13611 } 13612 /* 13613 * If we are in need of resources, start by cleaning up 13614 * any block removals associated with our inode. 13615 */ 13616 ACQUIRE_LOCK(ump); 13617 process_removes(vp); 13618 process_truncates(vp); 13619 FREE_LOCK(ump); 13620 /* 13621 * Now clean up at least as many resources as we will need. 13622 * 13623 * When requested to clean up inodes, the number that are needed 13624 * is set by the number of simultaneous writers (mnt_writeopcount) 13625 * plus a bit of slop (2) in case some more writers show up while 13626 * we are cleaning. 13627 * 13628 * When requested to free up space, the amount of space that 13629 * we need is enough blocks to allocate a full-sized segment 13630 * (fs_contigsumsize). The number of such segments that will 13631 * be needed is set by the number of simultaneous writers 13632 * (mnt_writeopcount) plus a bit of slop (2) in case some more 13633 * writers show up while we are cleaning. 13634 * 13635 * Additionally, if we are unpriviledged and allocating space, 13636 * we need to ensure that we clean up enough blocks to get the 13637 * needed number of blocks over the threshold of the minimum 13638 * number of blocks required to be kept free by the filesystem 13639 * (fs_minfree). 13640 */ 13641 if (resource == FLUSH_INODES_WAIT) { 13642 needed = vfs_mount_fetch_counter(vp->v_mount, 13643 MNT_COUNT_WRITEOPCOUNT) + 2; 13644 } else if (resource == FLUSH_BLOCKS_WAIT) { 13645 needed = (vfs_mount_fetch_counter(vp->v_mount, 13646 MNT_COUNT_WRITEOPCOUNT) + 2) * fs->fs_contigsumsize; 13647 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE)) 13648 needed += fragstoblks(fs, 13649 roundup((fs->fs_dsize * fs->fs_minfree / 100) - 13650 fs->fs_cstotal.cs_nffree, fs->fs_frag)); 13651 } else { 13652 printf("softdep_request_cleanup: Unknown resource type %d\n", 13653 resource); 13654 UFS_LOCK(ump); 13655 return (0); 13656 } 13657 starttime = time_second; 13658 retry: 13659 if (resource == FLUSH_BLOCKS_WAIT && 13660 fs->fs_cstotal.cs_nbfree <= needed) 13661 softdep_send_speedup(ump, needed * fs->fs_bsize, 13662 BIO_SPEEDUP_TRIM); 13663 if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 && 13664 fs->fs_cstotal.cs_nbfree <= needed) || 13665 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13666 fs->fs_cstotal.cs_nifree <= needed)) { 13667 ACQUIRE_LOCK(ump); 13668 if (ump->softdep_on_worklist > 0 && 13669 process_worklist_item(UFSTOVFS(ump), 13670 ump->softdep_on_worklist, LK_NOWAIT) != 0) 13671 stat_worklist_push += 1; 13672 FREE_LOCK(ump); 13673 } 13674 13675 /* 13676 * Check that there are vnodes pending inactivation. As they 13677 * have been unlinked, inactivating them will free up their 13678 * inodes. 13679 */ 13680 ACQUIRE_LOCK(ump); 13681 if (resource == FLUSH_INODES_WAIT && 13682 fs->fs_cstotal.cs_nifree <= needed && 13683 fs->fs_pendinginodes <= needed) { 13684 if ((ump->um_softdep->sd_flags & FLUSH_DI_ACTIVE) == 0) { 13685 ump->um_softdep->sd_flags |= FLUSH_DI_ACTIVE; 13686 FREE_LOCK(ump); 13687 softdep_request_cleanup_inactivate(mp); 13688 ACQUIRE_LOCK(ump); 13689 ump->um_softdep->sd_flags &= ~FLUSH_DI_ACTIVE; 13690 wakeup(&ump->um_softdep->sd_flags); 13691 } else { 13692 while ((ump->um_softdep->sd_flags & 13693 FLUSH_DI_ACTIVE) != 0) { 13694 msleep(&ump->um_softdep->sd_flags, 13695 LOCK_PTR(ump), PVM, "ffsvina", hz); 13696 } 13697 } 13698 } 13699 FREE_LOCK(ump); 13700 13701 /* 13702 * If we still need resources and there are no more worklist 13703 * entries to process to obtain them, we have to start flushing 13704 * the dirty vnodes to force the release of additional requests 13705 * to the worklist that we can then process to reap addition 13706 * resources. We walk the vnodes associated with the mount point 13707 * until we get the needed worklist requests that we can reap. 13708 * 13709 * If there are several threads all needing to clean the same 13710 * mount point, only one is allowed to walk the mount list. 13711 * When several threads all try to walk the same mount list, 13712 * they end up competing with each other and often end up in 13713 * livelock. This approach ensures that forward progress is 13714 * made at the cost of occational ENOSPC errors being returned 13715 * that might otherwise have been avoided. 13716 */ 13717 error = 1; 13718 if ((resource == FLUSH_BLOCKS_WAIT && 13719 fs->fs_cstotal.cs_nbfree <= needed) || 13720 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13721 fs->fs_cstotal.cs_nifree <= needed)) { 13722 ACQUIRE_LOCK(ump); 13723 if ((ump->um_softdep->sd_flags & FLUSH_RC_ACTIVE) == 0) { 13724 ump->um_softdep->sd_flags |= FLUSH_RC_ACTIVE; 13725 FREE_LOCK(ump); 13726 failed_vnode = softdep_request_cleanup_flush(mp, ump); 13727 ACQUIRE_LOCK(ump); 13728 ump->um_softdep->sd_flags &= ~FLUSH_RC_ACTIVE; 13729 wakeup(&ump->um_softdep->sd_flags); 13730 FREE_LOCK(ump); 13731 if (ump->softdep_on_worklist > 0) { 13732 stat_cleanup_retries += 1; 13733 if (!failed_vnode) 13734 goto retry; 13735 } 13736 } else { 13737 while ((ump->um_softdep->sd_flags & 13738 FLUSH_RC_ACTIVE) != 0) { 13739 msleep(&ump->um_softdep->sd_flags, 13740 LOCK_PTR(ump), PVM, "ffsrca", hz); 13741 } 13742 FREE_LOCK(ump); 13743 error = 0; 13744 } 13745 stat_cleanup_failures += 1; 13746 } 13747 if (time_second - starttime > stat_cleanup_high_delay) 13748 stat_cleanup_high_delay = time_second - starttime; 13749 UFS_LOCK(ump); 13750 return (error); 13751 } 13752 13753 /* 13754 * Scan the vnodes for the specified mount point flushing out any 13755 * vnodes that can be locked without waiting. Finally, try to flush 13756 * the device associated with the mount point if it can be locked 13757 * without waiting. 13758 * 13759 * We return 0 if we were able to lock every vnode in our scan. 13760 * If we had to skip one or more vnodes, we return 1. 13761 */ 13762 static int 13763 softdep_request_cleanup_flush(struct mount *mp, struct ufsmount *ump) 13764 { 13765 struct thread *td; 13766 struct vnode *lvp, *mvp; 13767 int failed_vnode; 13768 13769 failed_vnode = 0; 13770 td = curthread; 13771 MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) { 13772 if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) { 13773 VI_UNLOCK(lvp); 13774 continue; 13775 } 13776 if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT) != 0) { 13777 failed_vnode = 1; 13778 continue; 13779 } 13780 if (lvp->v_vflag & VV_NOSYNC) { /* unlinked */ 13781 vput(lvp); 13782 continue; 13783 } 13784 (void) ffs_syncvnode(lvp, MNT_NOWAIT, 0); 13785 vput(lvp); 13786 } 13787 lvp = ump->um_devvp; 13788 if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) { 13789 VOP_FSYNC(lvp, MNT_NOWAIT, td); 13790 VOP_UNLOCK(lvp); 13791 } 13792 return (failed_vnode); 13793 } 13794 13795 static bool 13796 softdep_excess_items(struct ufsmount *ump, int item) 13797 { 13798 13799 KASSERT(item >= 0 && item < D_LAST, ("item %d", item)); 13800 return (dep_current[item] > max_softdeps && 13801 ump->softdep_curdeps[item] > max_softdeps / 13802 stat_flush_threads); 13803 } 13804 13805 static void 13806 schedule_cleanup(struct mount *mp) 13807 { 13808 struct ufsmount *ump; 13809 struct thread *td; 13810 13811 ump = VFSTOUFS(mp); 13812 LOCK_OWNED(ump); 13813 FREE_LOCK(ump); 13814 td = curthread; 13815 if ((td->td_pflags & TDP_KTHREAD) != 0 && 13816 (td->td_proc->p_flag2 & P2_AST_SU) == 0) { 13817 /* 13818 * No ast is delivered to kernel threads, so nobody 13819 * would deref the mp. Some kernel threads 13820 * explicitly check for AST, e.g. NFS daemon does 13821 * this in the serving loop. 13822 */ 13823 return; 13824 } 13825 if (td->td_su != NULL) 13826 vfs_rel(td->td_su); 13827 vfs_ref(mp); 13828 td->td_su = mp; 13829 ast_sched(td, TDA_UFS); 13830 } 13831 13832 static void 13833 softdep_ast_cleanup_proc(struct thread *td, int ast __unused) 13834 { 13835 struct mount *mp; 13836 struct ufsmount *ump; 13837 int error; 13838 bool req; 13839 13840 while ((mp = td->td_su) != NULL) { 13841 td->td_su = NULL; 13842 error = vfs_busy(mp, MBF_NOWAIT); 13843 vfs_rel(mp); 13844 if (error != 0) 13845 return; 13846 if (ffs_own_mount(mp) && MOUNTEDSOFTDEP(mp)) { 13847 ump = VFSTOUFS(mp); 13848 for (;;) { 13849 req = false; 13850 ACQUIRE_LOCK(ump); 13851 if (softdep_excess_items(ump, D_INODEDEP)) { 13852 req = true; 13853 request_cleanup(mp, FLUSH_INODES); 13854 } 13855 if (softdep_excess_items(ump, D_DIRREM)) { 13856 req = true; 13857 request_cleanup(mp, FLUSH_BLOCKS); 13858 } 13859 FREE_LOCK(ump); 13860 if (softdep_excess_items(ump, D_NEWBLK) || 13861 softdep_excess_items(ump, D_ALLOCDIRECT) || 13862 softdep_excess_items(ump, D_ALLOCINDIR)) { 13863 error = vn_start_write(NULL, &mp, 13864 V_WAIT); 13865 if (error == 0) { 13866 req = true; 13867 VFS_SYNC(mp, MNT_WAIT); 13868 vn_finished_write(mp); 13869 } 13870 } 13871 if ((td->td_pflags & TDP_KTHREAD) != 0 || !req) 13872 break; 13873 } 13874 } 13875 vfs_unbusy(mp); 13876 } 13877 if ((mp = td->td_su) != NULL) { 13878 td->td_su = NULL; 13879 vfs_rel(mp); 13880 } 13881 } 13882 13883 /* 13884 * If memory utilization has gotten too high, deliberately slow things 13885 * down and speed up the I/O processing. 13886 */ 13887 static int 13888 request_cleanup(struct mount *mp, int resource) 13889 { 13890 struct thread *td = curthread; 13891 struct ufsmount *ump; 13892 13893 ump = VFSTOUFS(mp); 13894 LOCK_OWNED(ump); 13895 /* 13896 * We never hold up the filesystem syncer or buf daemon. 13897 */ 13898 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF)) 13899 return (0); 13900 /* 13901 * First check to see if the work list has gotten backlogged. 13902 * If it has, co-opt this process to help clean up two entries. 13903 * Because this process may hold inodes locked, we cannot 13904 * handle any remove requests that might block on a locked 13905 * inode as that could lead to deadlock. We set TDP_SOFTDEP 13906 * to avoid recursively processing the worklist. 13907 */ 13908 if (ump->softdep_on_worklist > max_softdeps / 10) { 13909 td->td_pflags |= TDP_SOFTDEP; 13910 process_worklist_item(mp, 2, LK_NOWAIT); 13911 td->td_pflags &= ~TDP_SOFTDEP; 13912 stat_worklist_push += 2; 13913 return(1); 13914 } 13915 /* 13916 * Next, we attempt to speed up the syncer process. If that 13917 * is successful, then we allow the process to continue. 13918 */ 13919 if (softdep_speedup(ump) && 13920 resource != FLUSH_BLOCKS_WAIT && 13921 resource != FLUSH_INODES_WAIT) 13922 return(0); 13923 /* 13924 * If we are resource constrained on inode dependencies, try 13925 * flushing some dirty inodes. Otherwise, we are constrained 13926 * by file deletions, so try accelerating flushes of directories 13927 * with removal dependencies. We would like to do the cleanup 13928 * here, but we probably hold an inode locked at this point and 13929 * that might deadlock against one that we try to clean. So, 13930 * the best that we can do is request the syncer daemon to do 13931 * the cleanup for us. 13932 */ 13933 switch (resource) { 13934 case FLUSH_INODES: 13935 case FLUSH_INODES_WAIT: 13936 ACQUIRE_GBLLOCK(&lk); 13937 stat_ino_limit_push += 1; 13938 req_clear_inodedeps += 1; 13939 FREE_GBLLOCK(&lk); 13940 stat_countp = &stat_ino_limit_hit; 13941 break; 13942 13943 case FLUSH_BLOCKS: 13944 case FLUSH_BLOCKS_WAIT: 13945 ACQUIRE_GBLLOCK(&lk); 13946 stat_blk_limit_push += 1; 13947 req_clear_remove += 1; 13948 FREE_GBLLOCK(&lk); 13949 stat_countp = &stat_blk_limit_hit; 13950 break; 13951 13952 default: 13953 panic("request_cleanup: unknown type"); 13954 } 13955 /* 13956 * Hopefully the syncer daemon will catch up and awaken us. 13957 * We wait at most tickdelay before proceeding in any case. 13958 */ 13959 ACQUIRE_GBLLOCK(&lk); 13960 FREE_LOCK(ump); 13961 proc_waiting += 1; 13962 if (callout_pending(&softdep_callout) == FALSE) 13963 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2, 13964 pause_timer, 0); 13965 13966 if ((td->td_pflags & TDP_KTHREAD) == 0) 13967 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0); 13968 proc_waiting -= 1; 13969 FREE_GBLLOCK(&lk); 13970 ACQUIRE_LOCK(ump); 13971 return (1); 13972 } 13973 13974 /* 13975 * Awaken processes pausing in request_cleanup and clear proc_waiting 13976 * to indicate that there is no longer a timer running. Pause_timer 13977 * will be called with the global softdep mutex (&lk) locked. 13978 */ 13979 static void 13980 pause_timer(void *arg) 13981 { 13982 13983 GBLLOCK_OWNED(&lk); 13984 /* 13985 * The callout_ API has acquired mtx and will hold it around this 13986 * function call. 13987 */ 13988 *stat_countp += proc_waiting; 13989 wakeup(&proc_waiting); 13990 } 13991 13992 /* 13993 * If requested, try removing inode or removal dependencies. 13994 */ 13995 static void 13996 check_clear_deps(struct mount *mp) 13997 { 13998 struct ufsmount *ump; 13999 bool suj_susp; 14000 14001 /* 14002 * Tell the lower layers that any TRIM or WRITE transactions that have 14003 * been delayed for performance reasons should proceed to help alleviate 14004 * the shortage faster. The race between checking req_* and the softdep 14005 * mutex (lk) is fine since this is an advisory operation that at most 14006 * causes deferred work to be done sooner. 14007 */ 14008 ump = VFSTOUFS(mp); 14009 suj_susp = ump->um_softdep->sd_jblocks != NULL && 14010 ump->softdep_jblocks->jb_suspended; 14011 if (req_clear_remove || req_clear_inodedeps || suj_susp) { 14012 FREE_LOCK(ump); 14013 softdep_send_speedup(ump, 0, BIO_SPEEDUP_TRIM | BIO_SPEEDUP_WRITE); 14014 ACQUIRE_LOCK(ump); 14015 } 14016 14017 /* 14018 * If we are suspended, it may be because of our using 14019 * too many inodedeps, so help clear them out. 14020 */ 14021 if (suj_susp) 14022 clear_inodedeps(mp); 14023 14024 /* 14025 * General requests for cleanup of backed up dependencies 14026 */ 14027 ACQUIRE_GBLLOCK(&lk); 14028 if (req_clear_inodedeps) { 14029 req_clear_inodedeps -= 1; 14030 FREE_GBLLOCK(&lk); 14031 clear_inodedeps(mp); 14032 ACQUIRE_GBLLOCK(&lk); 14033 wakeup(&proc_waiting); 14034 } 14035 if (req_clear_remove) { 14036 req_clear_remove -= 1; 14037 FREE_GBLLOCK(&lk); 14038 clear_remove(mp); 14039 ACQUIRE_GBLLOCK(&lk); 14040 wakeup(&proc_waiting); 14041 } 14042 FREE_GBLLOCK(&lk); 14043 } 14044 14045 /* 14046 * Flush out a directory with at least one removal dependency in an effort to 14047 * reduce the number of dirrem, freefile, and freeblks dependency structures. 14048 */ 14049 static void 14050 clear_remove(struct mount *mp) 14051 { 14052 struct pagedep_hashhead *pagedephd; 14053 struct pagedep *pagedep; 14054 struct ufsmount *ump; 14055 struct vnode *vp; 14056 struct bufobj *bo; 14057 int error, cnt; 14058 ino_t ino; 14059 14060 ump = VFSTOUFS(mp); 14061 LOCK_OWNED(ump); 14062 14063 for (cnt = 0; cnt <= ump->pagedep_hash_size; cnt++) { 14064 pagedephd = &ump->pagedep_hashtbl[ump->pagedep_nextclean++]; 14065 if (ump->pagedep_nextclean > ump->pagedep_hash_size) 14066 ump->pagedep_nextclean = 0; 14067 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 14068 if (LIST_EMPTY(&pagedep->pd_dirremhd)) 14069 continue; 14070 ino = pagedep->pd_ino; 14071 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 14072 continue; 14073 FREE_LOCK(ump); 14074 14075 /* 14076 * Let unmount clear deps 14077 */ 14078 error = vfs_busy(mp, MBF_NOWAIT); 14079 if (error != 0) 14080 goto finish_write; 14081 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 14082 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP); 14083 vfs_unbusy(mp); 14084 if (error != 0) { 14085 softdep_error("clear_remove: vget", error); 14086 goto finish_write; 14087 } 14088 MPASS(VTOI(vp)->i_mode != 0); 14089 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 14090 softdep_error("clear_remove: fsync", error); 14091 bo = &vp->v_bufobj; 14092 BO_LOCK(bo); 14093 drain_output(vp); 14094 BO_UNLOCK(bo); 14095 vput(vp); 14096 finish_write: 14097 vn_finished_write(mp); 14098 ACQUIRE_LOCK(ump); 14099 return; 14100 } 14101 } 14102 } 14103 14104 /* 14105 * Clear out a block of dirty inodes in an effort to reduce 14106 * the number of inodedep dependency structures. 14107 */ 14108 static void 14109 clear_inodedeps(struct mount *mp) 14110 { 14111 struct inodedep_hashhead *inodedephd; 14112 struct inodedep *inodedep; 14113 struct ufsmount *ump; 14114 struct vnode *vp; 14115 struct fs *fs; 14116 int error, cnt; 14117 ino_t firstino, lastino, ino; 14118 14119 ump = VFSTOUFS(mp); 14120 fs = ump->um_fs; 14121 LOCK_OWNED(ump); 14122 /* 14123 * Pick a random inode dependency to be cleared. 14124 * We will then gather up all the inodes in its block 14125 * that have dependencies and flush them out. 14126 */ 14127 for (cnt = 0; cnt <= ump->inodedep_hash_size; cnt++) { 14128 inodedephd = &ump->inodedep_hashtbl[ump->inodedep_nextclean++]; 14129 if (ump->inodedep_nextclean > ump->inodedep_hash_size) 14130 ump->inodedep_nextclean = 0; 14131 if ((inodedep = LIST_FIRST(inodedephd)) != NULL) 14132 break; 14133 } 14134 if (inodedep == NULL) 14135 return; 14136 /* 14137 * Find the last inode in the block with dependencies. 14138 */ 14139 firstino = rounddown2(inodedep->id_ino, INOPB(fs)); 14140 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--) 14141 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0) 14142 break; 14143 /* 14144 * Asynchronously push all but the last inode with dependencies. 14145 * Synchronously push the last inode with dependencies to ensure 14146 * that the inode block gets written to free up the inodedeps. 14147 */ 14148 for (ino = firstino; ino <= lastino; ino++) { 14149 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 14150 continue; 14151 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 14152 continue; 14153 FREE_LOCK(ump); 14154 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */ 14155 if (error != 0) { 14156 vn_finished_write(mp); 14157 ACQUIRE_LOCK(ump); 14158 return; 14159 } 14160 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 14161 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP)) != 0) { 14162 softdep_error("clear_inodedeps: vget", error); 14163 vfs_unbusy(mp); 14164 vn_finished_write(mp); 14165 ACQUIRE_LOCK(ump); 14166 return; 14167 } 14168 vfs_unbusy(mp); 14169 if (VTOI(vp)->i_mode == 0) { 14170 vgone(vp); 14171 } else if (ino == lastino) { 14172 do { 14173 error = ffs_syncvnode(vp, MNT_WAIT, 0); 14174 } while (error == ERELOOKUP); 14175 if (error != 0) 14176 softdep_error("clear_inodedeps: fsync1", error); 14177 } else { 14178 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 14179 softdep_error("clear_inodedeps: fsync2", error); 14180 BO_LOCK(&vp->v_bufobj); 14181 drain_output(vp); 14182 BO_UNLOCK(&vp->v_bufobj); 14183 } 14184 vput(vp); 14185 vn_finished_write(mp); 14186 ACQUIRE_LOCK(ump); 14187 } 14188 } 14189 14190 void 14191 softdep_buf_append(struct buf *bp, struct workhead *wkhd) 14192 { 14193 struct worklist *wk; 14194 struct ufsmount *ump; 14195 14196 if ((wk = LIST_FIRST(wkhd)) == NULL) 14197 return; 14198 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 14199 ("softdep_buf_append called on non-softdep filesystem")); 14200 ump = VFSTOUFS(wk->wk_mp); 14201 ACQUIRE_LOCK(ump); 14202 while ((wk = LIST_FIRST(wkhd)) != NULL) { 14203 WORKLIST_REMOVE(wk); 14204 WORKLIST_INSERT(&bp->b_dep, wk); 14205 } 14206 FREE_LOCK(ump); 14207 14208 } 14209 14210 void 14211 softdep_inode_append( 14212 struct inode *ip, 14213 struct ucred *cred, 14214 struct workhead *wkhd) 14215 { 14216 struct buf *bp; 14217 struct fs *fs; 14218 struct ufsmount *ump; 14219 int error; 14220 14221 ump = ITOUMP(ip); 14222 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 14223 ("softdep_inode_append called on non-softdep filesystem")); 14224 fs = ump->um_fs; 14225 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 14226 (int)fs->fs_bsize, cred, &bp); 14227 if (error) { 14228 bqrelse(bp); 14229 softdep_freework(wkhd); 14230 return; 14231 } 14232 softdep_buf_append(bp, wkhd); 14233 bqrelse(bp); 14234 } 14235 14236 void 14237 softdep_freework(struct workhead *wkhd) 14238 { 14239 struct worklist *wk; 14240 struct ufsmount *ump; 14241 14242 if ((wk = LIST_FIRST(wkhd)) == NULL) 14243 return; 14244 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 14245 ("softdep_freework called on non-softdep filesystem")); 14246 ump = VFSTOUFS(wk->wk_mp); 14247 ACQUIRE_LOCK(ump); 14248 handle_jwork(wkhd); 14249 FREE_LOCK(ump); 14250 } 14251 14252 static struct ufsmount * 14253 softdep_bp_to_mp(struct buf *bp) 14254 { 14255 struct mount *mp; 14256 struct vnode *vp; 14257 14258 if (LIST_EMPTY(&bp->b_dep)) 14259 return (NULL); 14260 vp = bp->b_vp; 14261 KASSERT(vp != NULL, 14262 ("%s, buffer with dependencies lacks vnode", __func__)); 14263 14264 /* 14265 * The ump mount point is stable after we get a correct 14266 * pointer, since bp is locked and this prevents unmount from 14267 * proceeding. But to get to it, we cannot dereference bp->b_dep 14268 * head wk_mp, because we do not yet own SU ump lock and 14269 * workitem might be freed while dereferenced. 14270 */ 14271 retry: 14272 switch (vp->v_type) { 14273 case VCHR: 14274 VI_LOCK(vp); 14275 mp = vp->v_type == VCHR ? vp->v_rdev->si_mountpt : NULL; 14276 VI_UNLOCK(vp); 14277 if (mp == NULL) 14278 goto retry; 14279 break; 14280 case VREG: 14281 case VDIR: 14282 case VLNK: 14283 case VFIFO: 14284 case VSOCK: 14285 mp = vp->v_mount; 14286 break; 14287 case VBLK: 14288 vn_printf(vp, "softdep_bp_to_mp: unexpected block device\n"); 14289 /* FALLTHROUGH */ 14290 case VNON: 14291 case VBAD: 14292 case VMARKER: 14293 mp = NULL; 14294 break; 14295 default: 14296 vn_printf(vp, "unknown vnode type"); 14297 mp = NULL; 14298 break; 14299 } 14300 return (VFSTOUFS(mp)); 14301 } 14302 14303 /* 14304 * Function to determine if the buffer has outstanding dependencies 14305 * that will cause a roll-back if the buffer is written. If wantcount 14306 * is set, return number of dependencies, otherwise just yes or no. 14307 */ 14308 static int 14309 softdep_count_dependencies(struct buf *bp, int wantcount) 14310 { 14311 struct worklist *wk; 14312 struct ufsmount *ump; 14313 struct bmsafemap *bmsafemap; 14314 struct freework *freework; 14315 struct inodedep *inodedep; 14316 struct indirdep *indirdep; 14317 struct freeblks *freeblks; 14318 struct allocindir *aip; 14319 struct pagedep *pagedep; 14320 struct dirrem *dirrem; 14321 struct newblk *newblk; 14322 struct mkdir *mkdir; 14323 struct diradd *dap; 14324 int i, retval; 14325 14326 ump = softdep_bp_to_mp(bp); 14327 if (ump == NULL) 14328 return (0); 14329 retval = 0; 14330 ACQUIRE_LOCK(ump); 14331 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 14332 switch (wk->wk_type) { 14333 case D_INODEDEP: 14334 inodedep = WK_INODEDEP(wk); 14335 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 14336 /* bitmap allocation dependency */ 14337 retval += 1; 14338 if (!wantcount) 14339 goto out; 14340 } 14341 if (TAILQ_FIRST(&inodedep->id_inoupdt)) { 14342 /* direct block pointer dependency */ 14343 retval += 1; 14344 if (!wantcount) 14345 goto out; 14346 } 14347 if (TAILQ_FIRST(&inodedep->id_extupdt)) { 14348 /* direct block pointer dependency */ 14349 retval += 1; 14350 if (!wantcount) 14351 goto out; 14352 } 14353 if (TAILQ_FIRST(&inodedep->id_inoreflst)) { 14354 /* Add reference dependency. */ 14355 retval += 1; 14356 if (!wantcount) 14357 goto out; 14358 } 14359 continue; 14360 14361 case D_INDIRDEP: 14362 indirdep = WK_INDIRDEP(wk); 14363 14364 TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) { 14365 /* indirect truncation dependency */ 14366 retval += 1; 14367 if (!wantcount) 14368 goto out; 14369 } 14370 14371 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 14372 /* indirect block pointer dependency */ 14373 retval += 1; 14374 if (!wantcount) 14375 goto out; 14376 } 14377 continue; 14378 14379 case D_PAGEDEP: 14380 pagedep = WK_PAGEDEP(wk); 14381 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) { 14382 if (LIST_FIRST(&dirrem->dm_jremrefhd)) { 14383 /* Journal remove ref dependency. */ 14384 retval += 1; 14385 if (!wantcount) 14386 goto out; 14387 } 14388 } 14389 for (i = 0; i < DAHASHSZ; i++) { 14390 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 14391 /* directory entry dependency */ 14392 retval += 1; 14393 if (!wantcount) 14394 goto out; 14395 } 14396 } 14397 continue; 14398 14399 case D_BMSAFEMAP: 14400 bmsafemap = WK_BMSAFEMAP(wk); 14401 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) { 14402 /* Add reference dependency. */ 14403 retval += 1; 14404 if (!wantcount) 14405 goto out; 14406 } 14407 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) { 14408 /* Allocate block dependency. */ 14409 retval += 1; 14410 if (!wantcount) 14411 goto out; 14412 } 14413 continue; 14414 14415 case D_FREEBLKS: 14416 freeblks = WK_FREEBLKS(wk); 14417 if (LIST_FIRST(&freeblks->fb_jblkdephd)) { 14418 /* Freeblk journal dependency. */ 14419 retval += 1; 14420 if (!wantcount) 14421 goto out; 14422 } 14423 continue; 14424 14425 case D_ALLOCDIRECT: 14426 case D_ALLOCINDIR: 14427 newblk = WK_NEWBLK(wk); 14428 if (newblk->nb_jnewblk) { 14429 /* Journal allocate dependency. */ 14430 retval += 1; 14431 if (!wantcount) 14432 goto out; 14433 } 14434 continue; 14435 14436 case D_MKDIR: 14437 mkdir = WK_MKDIR(wk); 14438 if (mkdir->md_jaddref) { 14439 /* Journal reference dependency. */ 14440 retval += 1; 14441 if (!wantcount) 14442 goto out; 14443 } 14444 continue; 14445 14446 case D_FREEWORK: 14447 case D_FREEDEP: 14448 case D_JSEGDEP: 14449 case D_JSEG: 14450 case D_SBDEP: 14451 /* never a dependency on these blocks */ 14452 continue; 14453 14454 default: 14455 panic("softdep_count_dependencies: Unexpected type %s", 14456 TYPENAME(wk->wk_type)); 14457 /* NOTREACHED */ 14458 } 14459 } 14460 out: 14461 FREE_LOCK(ump); 14462 return (retval); 14463 } 14464 14465 /* 14466 * Acquire exclusive access to a buffer. 14467 * Must be called with a locked mtx parameter. 14468 * Return acquired buffer or NULL on failure. 14469 */ 14470 static struct buf * 14471 getdirtybuf(struct buf *bp, 14472 struct rwlock *lock, 14473 int waitfor) 14474 { 14475 int error; 14476 14477 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) { 14478 if (waitfor != MNT_WAIT) 14479 return (NULL); 14480 error = BUF_LOCK(bp, 14481 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock); 14482 /* 14483 * Even if we successfully acquire bp here, we have dropped 14484 * lock, which may violates our guarantee. 14485 */ 14486 if (error == 0) 14487 BUF_UNLOCK(bp); 14488 else if (error != ENOLCK) 14489 panic("getdirtybuf: inconsistent lock: %d", error); 14490 rw_wlock(lock); 14491 return (NULL); 14492 } 14493 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 14494 if (lock != BO_LOCKPTR(bp->b_bufobj) && waitfor == MNT_WAIT) { 14495 rw_wunlock(lock); 14496 BO_LOCK(bp->b_bufobj); 14497 BUF_UNLOCK(bp); 14498 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 14499 bp->b_vflags |= BV_BKGRDWAIT; 14500 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), 14501 PRIBIO | PDROP, "getbuf", 0); 14502 } else 14503 BO_UNLOCK(bp->b_bufobj); 14504 rw_wlock(lock); 14505 return (NULL); 14506 } 14507 BUF_UNLOCK(bp); 14508 if (waitfor != MNT_WAIT) 14509 return (NULL); 14510 #ifdef DEBUG_VFS_LOCKS 14511 if (bp->b_vp->v_type != VCHR) 14512 ASSERT_BO_WLOCKED(bp->b_bufobj); 14513 #endif 14514 bp->b_vflags |= BV_BKGRDWAIT; 14515 rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0); 14516 return (NULL); 14517 } 14518 if ((bp->b_flags & B_DELWRI) == 0) { 14519 BUF_UNLOCK(bp); 14520 return (NULL); 14521 } 14522 bremfree(bp); 14523 return (bp); 14524 } 14525 14526 /* 14527 * Check if it is safe to suspend the file system now. On entry, 14528 * the vnode interlock for devvp should be held. Return 0 with 14529 * the mount interlock held if the file system can be suspended now, 14530 * otherwise return EAGAIN with the mount interlock held. 14531 */ 14532 int 14533 softdep_check_suspend(struct mount *mp, 14534 struct vnode *devvp, 14535 int softdep_depcnt, 14536 int softdep_accdepcnt, 14537 int secondary_writes, 14538 int secondary_accwrites) 14539 { 14540 struct buf *bp; 14541 struct bufobj *bo; 14542 struct ufsmount *ump; 14543 struct inodedep *inodedep; 14544 struct indirdep *indirdep; 14545 struct worklist *wk, *nextwk; 14546 int error, unlinked; 14547 14548 bo = &devvp->v_bufobj; 14549 ASSERT_BO_WLOCKED(bo); 14550 14551 /* 14552 * If we are not running with soft updates, then we need only 14553 * deal with secondary writes as we try to suspend. 14554 */ 14555 if (MOUNTEDSOFTDEP(mp) == 0) { 14556 MNT_ILOCK(mp); 14557 while (mp->mnt_secondary_writes != 0) { 14558 BO_UNLOCK(bo); 14559 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 14560 (PUSER - 1) | PDROP, "secwr", 0); 14561 BO_LOCK(bo); 14562 MNT_ILOCK(mp); 14563 } 14564 14565 /* 14566 * Reasons for needing more work before suspend: 14567 * - Dirty buffers on devvp. 14568 * - Secondary writes occurred after start of vnode sync loop 14569 */ 14570 error = 0; 14571 if (bo->bo_numoutput > 0 || 14572 bo->bo_dirty.bv_cnt > 0 || 14573 secondary_writes != 0 || 14574 mp->mnt_secondary_writes != 0 || 14575 secondary_accwrites != mp->mnt_secondary_accwrites) 14576 error = EAGAIN; 14577 BO_UNLOCK(bo); 14578 return (error); 14579 } 14580 14581 /* 14582 * If we are running with soft updates, then we need to coordinate 14583 * with them as we try to suspend. 14584 */ 14585 ump = VFSTOUFS(mp); 14586 for (;;) { 14587 if (!TRY_ACQUIRE_LOCK(ump)) { 14588 BO_UNLOCK(bo); 14589 ACQUIRE_LOCK(ump); 14590 FREE_LOCK(ump); 14591 BO_LOCK(bo); 14592 continue; 14593 } 14594 MNT_ILOCK(mp); 14595 if (mp->mnt_secondary_writes != 0) { 14596 FREE_LOCK(ump); 14597 BO_UNLOCK(bo); 14598 msleep(&mp->mnt_secondary_writes, 14599 MNT_MTX(mp), 14600 (PUSER - 1) | PDROP, "secwr", 0); 14601 BO_LOCK(bo); 14602 continue; 14603 } 14604 break; 14605 } 14606 14607 unlinked = 0; 14608 if (MOUNTEDSUJ(mp)) { 14609 for (inodedep = TAILQ_FIRST(&ump->softdep_unlinked); 14610 inodedep != NULL; 14611 inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 14612 if ((inodedep->id_state & (UNLINKED | UNLINKLINKS | 14613 UNLINKONLIST)) != (UNLINKED | UNLINKLINKS | 14614 UNLINKONLIST) || 14615 !check_inodedep_free(inodedep)) 14616 continue; 14617 unlinked++; 14618 } 14619 } 14620 14621 /* 14622 * XXX Check for orphaned indirdep dependency structures. 14623 * 14624 * During forcible unmount after a disk failure there is a 14625 * bug that causes one or more indirdep dependency structures 14626 * to fail to be deallocated. We check for them here and clean 14627 * them up so that the unmount can succeed. 14628 */ 14629 if ((ump->um_flags & UM_FSFAIL_CLEANUP) != 0 && ump->softdep_deps > 0 && 14630 ump->softdep_deps == ump->softdep_curdeps[D_INDIRDEP]) { 14631 LIST_FOREACH_SAFE(wk, &ump->softdep_alldeps[D_INDIRDEP], 14632 wk_all, nextwk) { 14633 indirdep = WK_INDIRDEP(wk); 14634 if ((indirdep->ir_state & (GOINGAWAY | DEPCOMPLETE)) != 14635 (GOINGAWAY | DEPCOMPLETE) || 14636 !TAILQ_EMPTY(&indirdep->ir_trunc) || 14637 !LIST_EMPTY(&indirdep->ir_completehd) || 14638 !LIST_EMPTY(&indirdep->ir_writehd) || 14639 !LIST_EMPTY(&indirdep->ir_donehd) || 14640 !LIST_EMPTY(&indirdep->ir_deplisthd) || 14641 indirdep->ir_saveddata != NULL || 14642 indirdep->ir_savebp == NULL) { 14643 printf("%s: skipping orphaned indirdep %p\n", 14644 __FUNCTION__, indirdep); 14645 continue; 14646 } 14647 printf("%s: freeing orphaned indirdep %p\n", 14648 __FUNCTION__, indirdep); 14649 bp = indirdep->ir_savebp; 14650 indirdep->ir_savebp = NULL; 14651 free_indirdep(indirdep); 14652 FREE_LOCK(ump); 14653 brelse(bp); 14654 while (!TRY_ACQUIRE_LOCK(ump)) { 14655 BO_UNLOCK(bo); 14656 ACQUIRE_LOCK(ump); 14657 FREE_LOCK(ump); 14658 BO_LOCK(bo); 14659 } 14660 } 14661 } 14662 14663 /* 14664 * Reasons for needing more work before suspend: 14665 * - Dirty buffers on devvp. 14666 * - Dependency structures still exist 14667 * - Softdep activity occurred after start of vnode sync loop 14668 * - Secondary writes occurred after start of vnode sync loop 14669 */ 14670 error = 0; 14671 if (bo->bo_numoutput > 0 || 14672 bo->bo_dirty.bv_cnt > 0 || 14673 softdep_depcnt != unlinked || 14674 ump->softdep_deps != unlinked || 14675 softdep_accdepcnt != ump->softdep_accdeps || 14676 secondary_writes != 0 || 14677 mp->mnt_secondary_writes != 0 || 14678 secondary_accwrites != mp->mnt_secondary_accwrites) 14679 error = EAGAIN; 14680 FREE_LOCK(ump); 14681 BO_UNLOCK(bo); 14682 return (error); 14683 } 14684 14685 /* 14686 * Get the number of dependency structures for the file system, both 14687 * the current number and the total number allocated. These will 14688 * later be used to detect that softdep processing has occurred. 14689 */ 14690 void 14691 softdep_get_depcounts(struct mount *mp, 14692 int *softdep_depsp, 14693 int *softdep_accdepsp) 14694 { 14695 struct ufsmount *ump; 14696 14697 if (MOUNTEDSOFTDEP(mp) == 0) { 14698 *softdep_depsp = 0; 14699 *softdep_accdepsp = 0; 14700 return; 14701 } 14702 ump = VFSTOUFS(mp); 14703 ACQUIRE_LOCK(ump); 14704 *softdep_depsp = ump->softdep_deps; 14705 *softdep_accdepsp = ump->softdep_accdeps; 14706 FREE_LOCK(ump); 14707 } 14708 14709 /* 14710 * Wait for pending output on a vnode to complete. 14711 */ 14712 static void 14713 drain_output(struct vnode *vp) 14714 { 14715 14716 ASSERT_VOP_LOCKED(vp, "drain_output"); 14717 (void)bufobj_wwait(&vp->v_bufobj, 0, 0); 14718 } 14719 14720 /* 14721 * Called whenever a buffer that is being invalidated or reallocated 14722 * contains dependencies. This should only happen if an I/O error has 14723 * occurred. The routine is called with the buffer locked. 14724 */ 14725 static void 14726 softdep_deallocate_dependencies(struct buf *bp) 14727 { 14728 14729 if ((bp->b_ioflags & BIO_ERROR) == 0) 14730 panic("softdep_deallocate_dependencies: dangling deps"); 14731 if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL) 14732 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error); 14733 else 14734 printf("softdep_deallocate_dependencies: " 14735 "got error %d while accessing filesystem\n", bp->b_error); 14736 if (bp->b_error != ENXIO) 14737 panic("softdep_deallocate_dependencies: unrecovered I/O error"); 14738 } 14739 14740 /* 14741 * Function to handle asynchronous write errors in the filesystem. 14742 */ 14743 static void 14744 softdep_error(char *func, int error) 14745 { 14746 14747 /* XXX should do something better! */ 14748 printf("%s: got error %d while accessing filesystem\n", func, error); 14749 } 14750 14751 #ifdef DDB 14752 14753 /* exported to ffs_vfsops.c */ 14754 extern void db_print_ffs(struct ufsmount *ump); 14755 void 14756 db_print_ffs(struct ufsmount *ump) 14757 { 14758 db_printf("mp %p (%s) devvp %p\n", ump->um_mountp, 14759 ump->um_mountp->mnt_stat.f_mntonname, ump->um_devvp); 14760 db_printf(" fs %p ", ump->um_fs); 14761 14762 if (ump->um_softdep != NULL) { 14763 db_printf("su_wl %d su_deps %d su_req %d\n", 14764 ump->softdep_on_worklist, ump->softdep_deps, 14765 ump->softdep_req); 14766 } else { 14767 db_printf("su disabled\n"); 14768 } 14769 } 14770 14771 static void 14772 worklist_print(struct worklist *wk, int verbose) 14773 { 14774 14775 if (!verbose) { 14776 db_printf("%s: %p state 0x%b\n", TYPENAME(wk->wk_type), wk, 14777 wk->wk_state, PRINT_SOFTDEP_FLAGS); 14778 return; 14779 } 14780 db_printf("worklist: %p type %s state 0x%b next %p\n ", wk, 14781 TYPENAME(wk->wk_type), wk->wk_state, PRINT_SOFTDEP_FLAGS, 14782 LIST_NEXT(wk, wk_list)); 14783 db_print_ffs(VFSTOUFS(wk->wk_mp)); 14784 } 14785 14786 static void 14787 inodedep_print(struct inodedep *inodedep, int verbose) 14788 { 14789 14790 worklist_print(&inodedep->id_list, 0); 14791 db_printf(" fs %p ino %jd inoblk %jd delta %jd nlink %jd\n", 14792 inodedep->id_fs, 14793 (intmax_t)inodedep->id_ino, 14794 (intmax_t)fsbtodb(inodedep->id_fs, 14795 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)), 14796 (intmax_t)inodedep->id_nlinkdelta, 14797 (intmax_t)inodedep->id_savednlink); 14798 14799 if (verbose == 0) 14800 return; 14801 14802 db_printf(" bmsafemap %p, mkdiradd %p, inoreflst %p\n", 14803 inodedep->id_bmsafemap, 14804 inodedep->id_mkdiradd, 14805 TAILQ_FIRST(&inodedep->id_inoreflst)); 14806 db_printf(" dirremhd %p, pendinghd %p, bufwait %p\n", 14807 LIST_FIRST(&inodedep->id_dirremhd), 14808 LIST_FIRST(&inodedep->id_pendinghd), 14809 LIST_FIRST(&inodedep->id_bufwait)); 14810 db_printf(" inowait %p, inoupdt %p, newinoupdt %p\n", 14811 LIST_FIRST(&inodedep->id_inowait), 14812 TAILQ_FIRST(&inodedep->id_inoupdt), 14813 TAILQ_FIRST(&inodedep->id_newinoupdt)); 14814 db_printf(" extupdt %p, newextupdt %p, freeblklst %p\n", 14815 TAILQ_FIRST(&inodedep->id_extupdt), 14816 TAILQ_FIRST(&inodedep->id_newextupdt), 14817 TAILQ_FIRST(&inodedep->id_freeblklst)); 14818 db_printf(" saveino %p, savedsize %jd, savedextsize %jd\n", 14819 inodedep->id_savedino1, 14820 (intmax_t)inodedep->id_savedsize, 14821 (intmax_t)inodedep->id_savedextsize); 14822 } 14823 14824 static void 14825 newblk_print(struct newblk *nbp) 14826 { 14827 14828 worklist_print(&nbp->nb_list, 0); 14829 db_printf(" newblkno %jd\n", (intmax_t)nbp->nb_newblkno); 14830 db_printf(" jnewblk %p, bmsafemap %p, freefrag %p\n", 14831 &nbp->nb_jnewblk, 14832 &nbp->nb_bmsafemap, 14833 &nbp->nb_freefrag); 14834 db_printf(" indirdeps %p, newdirblk %p, jwork %p\n", 14835 LIST_FIRST(&nbp->nb_indirdeps), 14836 LIST_FIRST(&nbp->nb_newdirblk), 14837 LIST_FIRST(&nbp->nb_jwork)); 14838 } 14839 14840 static void 14841 allocdirect_print(struct allocdirect *adp) 14842 { 14843 14844 newblk_print(&adp->ad_block); 14845 db_printf(" oldblkno %jd, oldsize %ld, newsize %ld\n", 14846 adp->ad_oldblkno, adp->ad_oldsize, adp->ad_newsize); 14847 db_printf(" offset %d, inodedep %p\n", 14848 adp->ad_offset, adp->ad_inodedep); 14849 } 14850 14851 static void 14852 allocindir_print(struct allocindir *aip) 14853 { 14854 14855 newblk_print(&aip->ai_block); 14856 db_printf(" oldblkno %jd, lbn %jd\n", 14857 (intmax_t)aip->ai_oldblkno, (intmax_t)aip->ai_lbn); 14858 db_printf(" offset %d, indirdep %p\n", 14859 aip->ai_offset, aip->ai_indirdep); 14860 } 14861 14862 static void 14863 mkdir_print(struct mkdir *mkdir) 14864 { 14865 14866 worklist_print(&mkdir->md_list, 0); 14867 db_printf(" diradd %p, jaddref %p, buf %p\n", 14868 mkdir->md_diradd, mkdir->md_jaddref, mkdir->md_buf); 14869 } 14870 14871 DB_SHOW_COMMAND(sd_inodedep, db_show_sd_inodedep) 14872 { 14873 14874 if (have_addr == 0) { 14875 db_printf("inodedep address required\n"); 14876 return; 14877 } 14878 inodedep_print((struct inodedep*)addr, 1); 14879 } 14880 14881 DB_SHOW_COMMAND(sd_allinodedeps, db_show_sd_allinodedeps) 14882 { 14883 struct inodedep_hashhead *inodedephd; 14884 struct inodedep *inodedep; 14885 struct ufsmount *ump; 14886 int cnt; 14887 14888 if (have_addr == 0) { 14889 db_printf("ufsmount address required\n"); 14890 return; 14891 } 14892 ump = (struct ufsmount *)addr; 14893 for (cnt = 0; cnt < ump->inodedep_hash_size; cnt++) { 14894 inodedephd = &ump->inodedep_hashtbl[cnt]; 14895 LIST_FOREACH(inodedep, inodedephd, id_hash) { 14896 inodedep_print(inodedep, 0); 14897 } 14898 } 14899 } 14900 14901 DB_SHOW_COMMAND(sd_worklist, db_show_sd_worklist) 14902 { 14903 14904 if (have_addr == 0) { 14905 db_printf("worklist address required\n"); 14906 return; 14907 } 14908 worklist_print((struct worklist *)addr, 1); 14909 } 14910 14911 DB_SHOW_COMMAND(sd_workhead, db_show_sd_workhead) 14912 { 14913 struct worklist *wk; 14914 struct workhead *wkhd; 14915 14916 if (have_addr == 0) { 14917 db_printf("worklist address required " 14918 "(for example value in bp->b_dep)\n"); 14919 return; 14920 } 14921 /* 14922 * We often do not have the address of the worklist head but 14923 * instead a pointer to its first entry (e.g., we have the 14924 * contents of bp->b_dep rather than &bp->b_dep). But the back 14925 * pointer of bp->b_dep will point at the head of the list, so 14926 * we cheat and use that instead. If we are in the middle of 14927 * a list we will still get the same result, so nothing 14928 * unexpected will result. 14929 */ 14930 wk = (struct worklist *)addr; 14931 if (wk == NULL) 14932 return; 14933 wkhd = (struct workhead *)wk->wk_list.le_prev; 14934 LIST_FOREACH(wk, wkhd, wk_list) { 14935 switch(wk->wk_type) { 14936 case D_INODEDEP: 14937 inodedep_print(WK_INODEDEP(wk), 0); 14938 continue; 14939 case D_ALLOCDIRECT: 14940 allocdirect_print(WK_ALLOCDIRECT(wk)); 14941 continue; 14942 case D_ALLOCINDIR: 14943 allocindir_print(WK_ALLOCINDIR(wk)); 14944 continue; 14945 case D_MKDIR: 14946 mkdir_print(WK_MKDIR(wk)); 14947 continue; 14948 default: 14949 worklist_print(wk, 0); 14950 continue; 14951 } 14952 } 14953 } 14954 14955 DB_SHOW_COMMAND(sd_mkdir, db_show_sd_mkdir) 14956 { 14957 if (have_addr == 0) { 14958 db_printf("mkdir address required\n"); 14959 return; 14960 } 14961 mkdir_print((struct mkdir *)addr); 14962 } 14963 14964 DB_SHOW_COMMAND(sd_mkdir_list, db_show_sd_mkdir_list) 14965 { 14966 struct mkdirlist *mkdirlisthd; 14967 struct mkdir *mkdir; 14968 14969 if (have_addr == 0) { 14970 db_printf("mkdir listhead address required\n"); 14971 return; 14972 } 14973 mkdirlisthd = (struct mkdirlist *)addr; 14974 LIST_FOREACH(mkdir, mkdirlisthd, md_mkdirs) { 14975 mkdir_print(mkdir); 14976 if (mkdir->md_diradd != NULL) { 14977 db_printf(" "); 14978 worklist_print(&mkdir->md_diradd->da_list, 0); 14979 } 14980 if (mkdir->md_jaddref != NULL) { 14981 db_printf(" "); 14982 worklist_print(&mkdir->md_jaddref->ja_list, 0); 14983 } 14984 } 14985 } 14986 14987 DB_SHOW_COMMAND(sd_allocdirect, db_show_sd_allocdirect) 14988 { 14989 if (have_addr == 0) { 14990 db_printf("allocdirect address required\n"); 14991 return; 14992 } 14993 allocdirect_print((struct allocdirect *)addr); 14994 } 14995 14996 DB_SHOW_COMMAND(sd_allocindir, db_show_sd_allocindir) 14997 { 14998 if (have_addr == 0) { 14999 db_printf("allocindir address required\n"); 15000 return; 15001 } 15002 allocindir_print((struct allocindir *)addr); 15003 } 15004 15005 #endif /* DDB */ 15006 15007 #endif /* SOFTUPDATES */ 15008