ffs_softdep.c revision 284021
1/*- 2 * Copyright 1998, 2000 Marshall Kirk McKusick. 3 * Copyright 2009, 2010 Jeffrey W. Roberson <jeff@FreeBSD.org> 4 * All rights reserved. 5 * 6 * The soft updates code is derived from the appendix of a University 7 * of Michigan technical report (Gregory R. Ganger and Yale N. Patt, 8 * "Soft Updates: A Solution to the Metadata Update Problem in File 9 * Systems", CSE-TR-254-95, August 1995). 10 * 11 * Further information about soft updates can be obtained from: 12 * 13 * Marshall Kirk McKusick http://www.mckusick.com/softdep/ 14 * 1614 Oxford Street mckusick@mckusick.com 15 * Berkeley, CA 94709-1608 +1-510-843-9542 16 * USA 17 * 18 * Redistribution and use in source and binary forms, with or without 19 * modification, are permitted provided that the following conditions 20 * are met: 21 * 22 * 1. Redistributions of source code must retain the above copyright 23 * notice, this list of conditions and the following disclaimer. 24 * 2. Redistributions in binary form must reproduce the above copyright 25 * notice, this list of conditions and the following disclaimer in the 26 * documentation and/or other materials provided with the distribution. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 31 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 * 39 * from: @(#)ffs_softdep.c 9.59 (McKusick) 6/21/00 40 */ 41 42#include <sys/cdefs.h> 43__FBSDID("$FreeBSD: stable/10/sys/ufs/ffs/ffs_softdep.c 284021 2015-06-05 08:36:25Z kib $"); 44 45#include "opt_ffs.h" 46#include "opt_quota.h" 47#include "opt_ddb.h" 48 49/* 50 * For now we want the safety net that the DEBUG flag provides. 51 */ 52#ifndef DEBUG 53#define DEBUG 54#endif 55 56#include <sys/param.h> 57#include <sys/kernel.h> 58#include <sys/systm.h> 59#include <sys/bio.h> 60#include <sys/buf.h> 61#include <sys/kdb.h> 62#include <sys/kthread.h> 63#include <sys/ktr.h> 64#include <sys/limits.h> 65#include <sys/lock.h> 66#include <sys/malloc.h> 67#include <sys/mount.h> 68#include <sys/mutex.h> 69#include <sys/namei.h> 70#include <sys/priv.h> 71#include <sys/proc.h> 72#include <sys/rwlock.h> 73#include <sys/stat.h> 74#include <sys/sysctl.h> 75#include <sys/syslog.h> 76#include <sys/vnode.h> 77#include <sys/conf.h> 78 79#include <ufs/ufs/dir.h> 80#include <ufs/ufs/extattr.h> 81#include <ufs/ufs/quota.h> 82#include <ufs/ufs/inode.h> 83#include <ufs/ufs/ufsmount.h> 84#include <ufs/ffs/fs.h> 85#include <ufs/ffs/softdep.h> 86#include <ufs/ffs/ffs_extern.h> 87#include <ufs/ufs/ufs_extern.h> 88 89#include <vm/vm.h> 90#include <vm/vm_extern.h> 91#include <vm/vm_object.h> 92 93#include <geom/geom.h> 94 95#include <ddb/ddb.h> 96 97#define KTR_SUJ 0 /* Define to KTR_SPARE. */ 98 99#ifndef SOFTUPDATES 100 101int 102softdep_flushfiles(oldmnt, flags, td) 103 struct mount *oldmnt; 104 int flags; 105 struct thread *td; 106{ 107 108 panic("softdep_flushfiles called"); 109} 110 111int 112softdep_mount(devvp, mp, fs, cred) 113 struct vnode *devvp; 114 struct mount *mp; 115 struct fs *fs; 116 struct ucred *cred; 117{ 118 119 return (0); 120} 121 122void 123softdep_initialize() 124{ 125 126 return; 127} 128 129void 130softdep_uninitialize() 131{ 132 133 return; 134} 135 136void 137softdep_unmount(mp) 138 struct mount *mp; 139{ 140 141 panic("softdep_unmount called"); 142} 143 144void 145softdep_setup_sbupdate(ump, fs, bp) 146 struct ufsmount *ump; 147 struct fs *fs; 148 struct buf *bp; 149{ 150 151 panic("softdep_setup_sbupdate called"); 152} 153 154void 155softdep_setup_inomapdep(bp, ip, newinum, mode) 156 struct buf *bp; 157 struct inode *ip; 158 ino_t newinum; 159 int mode; 160{ 161 162 panic("softdep_setup_inomapdep called"); 163} 164 165void 166softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags) 167 struct buf *bp; 168 struct mount *mp; 169 ufs2_daddr_t newblkno; 170 int frags; 171 int oldfrags; 172{ 173 174 panic("softdep_setup_blkmapdep called"); 175} 176 177void 178softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 179 struct inode *ip; 180 ufs_lbn_t lbn; 181 ufs2_daddr_t newblkno; 182 ufs2_daddr_t oldblkno; 183 long newsize; 184 long oldsize; 185 struct buf *bp; 186{ 187 188 panic("softdep_setup_allocdirect called"); 189} 190 191void 192softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 193 struct inode *ip; 194 ufs_lbn_t lbn; 195 ufs2_daddr_t newblkno; 196 ufs2_daddr_t oldblkno; 197 long newsize; 198 long oldsize; 199 struct buf *bp; 200{ 201 202 panic("softdep_setup_allocext called"); 203} 204 205void 206softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 207 struct inode *ip; 208 ufs_lbn_t lbn; 209 struct buf *bp; 210 int ptrno; 211 ufs2_daddr_t newblkno; 212 ufs2_daddr_t oldblkno; 213 struct buf *nbp; 214{ 215 216 panic("softdep_setup_allocindir_page called"); 217} 218 219void 220softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 221 struct buf *nbp; 222 struct inode *ip; 223 struct buf *bp; 224 int ptrno; 225 ufs2_daddr_t newblkno; 226{ 227 228 panic("softdep_setup_allocindir_meta called"); 229} 230 231void 232softdep_journal_freeblocks(ip, cred, length, flags) 233 struct inode *ip; 234 struct ucred *cred; 235 off_t length; 236 int flags; 237{ 238 239 panic("softdep_journal_freeblocks called"); 240} 241 242void 243softdep_journal_fsync(ip) 244 struct inode *ip; 245{ 246 247 panic("softdep_journal_fsync called"); 248} 249 250void 251softdep_setup_freeblocks(ip, length, flags) 252 struct inode *ip; 253 off_t length; 254 int flags; 255{ 256 257 panic("softdep_setup_freeblocks called"); 258} 259 260void 261softdep_freefile(pvp, ino, mode) 262 struct vnode *pvp; 263 ino_t ino; 264 int mode; 265{ 266 267 panic("softdep_freefile called"); 268} 269 270int 271softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 272 struct buf *bp; 273 struct inode *dp; 274 off_t diroffset; 275 ino_t newinum; 276 struct buf *newdirbp; 277 int isnewblk; 278{ 279 280 panic("softdep_setup_directory_add called"); 281} 282 283void 284softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 285 struct buf *bp; 286 struct inode *dp; 287 caddr_t base; 288 caddr_t oldloc; 289 caddr_t newloc; 290 int entrysize; 291{ 292 293 panic("softdep_change_directoryentry_offset called"); 294} 295 296void 297softdep_setup_remove(bp, dp, ip, isrmdir) 298 struct buf *bp; 299 struct inode *dp; 300 struct inode *ip; 301 int isrmdir; 302{ 303 304 panic("softdep_setup_remove called"); 305} 306 307void 308softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 309 struct buf *bp; 310 struct inode *dp; 311 struct inode *ip; 312 ino_t newinum; 313 int isrmdir; 314{ 315 316 panic("softdep_setup_directory_change called"); 317} 318 319void 320softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 321 struct mount *mp; 322 struct buf *bp; 323 ufs2_daddr_t blkno; 324 int frags; 325 struct workhead *wkhd; 326{ 327 328 panic("%s called", __FUNCTION__); 329} 330 331void 332softdep_setup_inofree(mp, bp, ino, wkhd) 333 struct mount *mp; 334 struct buf *bp; 335 ino_t ino; 336 struct workhead *wkhd; 337{ 338 339 panic("%s called", __FUNCTION__); 340} 341 342void 343softdep_setup_unlink(dp, ip) 344 struct inode *dp; 345 struct inode *ip; 346{ 347 348 panic("%s called", __FUNCTION__); 349} 350 351void 352softdep_setup_link(dp, ip) 353 struct inode *dp; 354 struct inode *ip; 355{ 356 357 panic("%s called", __FUNCTION__); 358} 359 360void 361softdep_revert_link(dp, ip) 362 struct inode *dp; 363 struct inode *ip; 364{ 365 366 panic("%s called", __FUNCTION__); 367} 368 369void 370softdep_setup_rmdir(dp, ip) 371 struct inode *dp; 372 struct inode *ip; 373{ 374 375 panic("%s called", __FUNCTION__); 376} 377 378void 379softdep_revert_rmdir(dp, ip) 380 struct inode *dp; 381 struct inode *ip; 382{ 383 384 panic("%s called", __FUNCTION__); 385} 386 387void 388softdep_setup_create(dp, ip) 389 struct inode *dp; 390 struct inode *ip; 391{ 392 393 panic("%s called", __FUNCTION__); 394} 395 396void 397softdep_revert_create(dp, ip) 398 struct inode *dp; 399 struct inode *ip; 400{ 401 402 panic("%s called", __FUNCTION__); 403} 404 405void 406softdep_setup_mkdir(dp, ip) 407 struct inode *dp; 408 struct inode *ip; 409{ 410 411 panic("%s called", __FUNCTION__); 412} 413 414void 415softdep_revert_mkdir(dp, ip) 416 struct inode *dp; 417 struct inode *ip; 418{ 419 420 panic("%s called", __FUNCTION__); 421} 422 423void 424softdep_setup_dotdot_link(dp, ip) 425 struct inode *dp; 426 struct inode *ip; 427{ 428 429 panic("%s called", __FUNCTION__); 430} 431 432int 433softdep_prealloc(vp, waitok) 434 struct vnode *vp; 435 int waitok; 436{ 437 438 panic("%s called", __FUNCTION__); 439} 440 441int 442softdep_journal_lookup(mp, vpp) 443 struct mount *mp; 444 struct vnode **vpp; 445{ 446 447 return (ENOENT); 448} 449 450void 451softdep_change_linkcnt(ip) 452 struct inode *ip; 453{ 454 455 panic("softdep_change_linkcnt called"); 456} 457 458void 459softdep_load_inodeblock(ip) 460 struct inode *ip; 461{ 462 463 panic("softdep_load_inodeblock called"); 464} 465 466void 467softdep_update_inodeblock(ip, bp, waitfor) 468 struct inode *ip; 469 struct buf *bp; 470 int waitfor; 471{ 472 473 panic("softdep_update_inodeblock called"); 474} 475 476int 477softdep_fsync(vp) 478 struct vnode *vp; /* the "in_core" copy of the inode */ 479{ 480 481 return (0); 482} 483 484void 485softdep_fsync_mountdev(vp) 486 struct vnode *vp; 487{ 488 489 return; 490} 491 492int 493softdep_flushworklist(oldmnt, countp, td) 494 struct mount *oldmnt; 495 int *countp; 496 struct thread *td; 497{ 498 499 *countp = 0; 500 return (0); 501} 502 503int 504softdep_sync_metadata(struct vnode *vp) 505{ 506 507 panic("softdep_sync_metadata called"); 508} 509 510int 511softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 512{ 513 514 panic("softdep_sync_buf called"); 515} 516 517int 518softdep_slowdown(vp) 519 struct vnode *vp; 520{ 521 522 panic("softdep_slowdown called"); 523} 524 525int 526softdep_request_cleanup(fs, vp, cred, resource) 527 struct fs *fs; 528 struct vnode *vp; 529 struct ucred *cred; 530 int resource; 531{ 532 533 return (0); 534} 535 536int 537softdep_check_suspend(struct mount *mp, 538 struct vnode *devvp, 539 int softdep_depcnt, 540 int softdep_accdepcnt, 541 int secondary_writes, 542 int secondary_accwrites) 543{ 544 struct bufobj *bo; 545 int error; 546 547 (void) softdep_depcnt, 548 (void) softdep_accdepcnt; 549 550 bo = &devvp->v_bufobj; 551 ASSERT_BO_WLOCKED(bo); 552 553 MNT_ILOCK(mp); 554 while (mp->mnt_secondary_writes != 0) { 555 BO_UNLOCK(bo); 556 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 557 (PUSER - 1) | PDROP, "secwr", 0); 558 BO_LOCK(bo); 559 MNT_ILOCK(mp); 560 } 561 562 /* 563 * Reasons for needing more work before suspend: 564 * - Dirty buffers on devvp. 565 * - Secondary writes occurred after start of vnode sync loop 566 */ 567 error = 0; 568 if (bo->bo_numoutput > 0 || 569 bo->bo_dirty.bv_cnt > 0 || 570 secondary_writes != 0 || 571 mp->mnt_secondary_writes != 0 || 572 secondary_accwrites != mp->mnt_secondary_accwrites) 573 error = EAGAIN; 574 BO_UNLOCK(bo); 575 return (error); 576} 577 578void 579softdep_get_depcounts(struct mount *mp, 580 int *softdepactivep, 581 int *softdepactiveaccp) 582{ 583 (void) mp; 584 *softdepactivep = 0; 585 *softdepactiveaccp = 0; 586} 587 588void 589softdep_buf_append(bp, wkhd) 590 struct buf *bp; 591 struct workhead *wkhd; 592{ 593 594 panic("softdep_buf_appendwork called"); 595} 596 597void 598softdep_inode_append(ip, cred, wkhd) 599 struct inode *ip; 600 struct ucred *cred; 601 struct workhead *wkhd; 602{ 603 604 panic("softdep_inode_appendwork called"); 605} 606 607void 608softdep_freework(wkhd) 609 struct workhead *wkhd; 610{ 611 612 panic("softdep_freework called"); 613} 614 615#else 616 617FEATURE(softupdates, "FFS soft-updates support"); 618 619static SYSCTL_NODE(_debug, OID_AUTO, softdep, CTLFLAG_RW, 0, 620 "soft updates stats"); 621static SYSCTL_NODE(_debug_softdep, OID_AUTO, total, CTLFLAG_RW, 0, 622 "total dependencies allocated"); 623static SYSCTL_NODE(_debug_softdep, OID_AUTO, highuse, CTLFLAG_RW, 0, 624 "high use dependencies allocated"); 625static SYSCTL_NODE(_debug_softdep, OID_AUTO, current, CTLFLAG_RW, 0, 626 "current dependencies allocated"); 627static SYSCTL_NODE(_debug_softdep, OID_AUTO, write, CTLFLAG_RW, 0, 628 "current dependencies written"); 629 630unsigned long dep_current[D_LAST + 1]; 631unsigned long dep_highuse[D_LAST + 1]; 632unsigned long dep_total[D_LAST + 1]; 633unsigned long dep_write[D_LAST + 1]; 634 635#define SOFTDEP_TYPE(type, str, long) \ 636 static MALLOC_DEFINE(M_ ## type, #str, long); \ 637 SYSCTL_ULONG(_debug_softdep_total, OID_AUTO, str, CTLFLAG_RD, \ 638 &dep_total[D_ ## type], 0, ""); \ 639 SYSCTL_ULONG(_debug_softdep_current, OID_AUTO, str, CTLFLAG_RD, \ 640 &dep_current[D_ ## type], 0, ""); \ 641 SYSCTL_ULONG(_debug_softdep_highuse, OID_AUTO, str, CTLFLAG_RD, \ 642 &dep_highuse[D_ ## type], 0, ""); \ 643 SYSCTL_ULONG(_debug_softdep_write, OID_AUTO, str, CTLFLAG_RD, \ 644 &dep_write[D_ ## type], 0, ""); 645 646SOFTDEP_TYPE(PAGEDEP, pagedep, "File page dependencies"); 647SOFTDEP_TYPE(INODEDEP, inodedep, "Inode dependencies"); 648SOFTDEP_TYPE(BMSAFEMAP, bmsafemap, 649 "Block or frag allocated from cyl group map"); 650SOFTDEP_TYPE(NEWBLK, newblk, "New block or frag allocation dependency"); 651SOFTDEP_TYPE(ALLOCDIRECT, allocdirect, "Block or frag dependency for an inode"); 652SOFTDEP_TYPE(INDIRDEP, indirdep, "Indirect block dependencies"); 653SOFTDEP_TYPE(ALLOCINDIR, allocindir, "Block dependency for an indirect block"); 654SOFTDEP_TYPE(FREEFRAG, freefrag, "Previously used frag for an inode"); 655SOFTDEP_TYPE(FREEBLKS, freeblks, "Blocks freed from an inode"); 656SOFTDEP_TYPE(FREEFILE, freefile, "Inode deallocated"); 657SOFTDEP_TYPE(DIRADD, diradd, "New directory entry"); 658SOFTDEP_TYPE(MKDIR, mkdir, "New directory"); 659SOFTDEP_TYPE(DIRREM, dirrem, "Directory entry deleted"); 660SOFTDEP_TYPE(NEWDIRBLK, newdirblk, "Unclaimed new directory block"); 661SOFTDEP_TYPE(FREEWORK, freework, "free an inode block"); 662SOFTDEP_TYPE(FREEDEP, freedep, "track a block free"); 663SOFTDEP_TYPE(JADDREF, jaddref, "Journal inode ref add"); 664SOFTDEP_TYPE(JREMREF, jremref, "Journal inode ref remove"); 665SOFTDEP_TYPE(JMVREF, jmvref, "Journal inode ref move"); 666SOFTDEP_TYPE(JNEWBLK, jnewblk, "Journal new block"); 667SOFTDEP_TYPE(JFREEBLK, jfreeblk, "Journal free block"); 668SOFTDEP_TYPE(JFREEFRAG, jfreefrag, "Journal free frag"); 669SOFTDEP_TYPE(JSEG, jseg, "Journal segment"); 670SOFTDEP_TYPE(JSEGDEP, jsegdep, "Journal segment complete"); 671SOFTDEP_TYPE(SBDEP, sbdep, "Superblock write dependency"); 672SOFTDEP_TYPE(JTRUNC, jtrunc, "Journal inode truncation"); 673SOFTDEP_TYPE(JFSYNC, jfsync, "Journal fsync complete"); 674 675static MALLOC_DEFINE(M_SENTINEL, "sentinel", "Worklist sentinel"); 676 677static MALLOC_DEFINE(M_SAVEDINO, "savedino", "Saved inodes"); 678static MALLOC_DEFINE(M_JBLOCKS, "jblocks", "Journal block locations"); 679static MALLOC_DEFINE(M_MOUNTDATA, "softdep", "Softdep per-mount data"); 680 681#define M_SOFTDEP_FLAGS (M_WAITOK) 682 683/* 684 * translate from workitem type to memory type 685 * MUST match the defines above, such that memtype[D_XXX] == M_XXX 686 */ 687static struct malloc_type *memtype[] = { 688 M_PAGEDEP, 689 M_INODEDEP, 690 M_BMSAFEMAP, 691 M_NEWBLK, 692 M_ALLOCDIRECT, 693 M_INDIRDEP, 694 M_ALLOCINDIR, 695 M_FREEFRAG, 696 M_FREEBLKS, 697 M_FREEFILE, 698 M_DIRADD, 699 M_MKDIR, 700 M_DIRREM, 701 M_NEWDIRBLK, 702 M_FREEWORK, 703 M_FREEDEP, 704 M_JADDREF, 705 M_JREMREF, 706 M_JMVREF, 707 M_JNEWBLK, 708 M_JFREEBLK, 709 M_JFREEFRAG, 710 M_JSEG, 711 M_JSEGDEP, 712 M_SBDEP, 713 M_JTRUNC, 714 M_JFSYNC, 715 M_SENTINEL 716}; 717 718#define DtoM(type) (memtype[type]) 719 720/* 721 * Names of malloc types. 722 */ 723#define TYPENAME(type) \ 724 ((unsigned)(type) <= D_LAST ? memtype[type]->ks_shortdesc : "???") 725/* 726 * End system adaptation definitions. 727 */ 728 729#define DOTDOT_OFFSET offsetof(struct dirtemplate, dotdot_ino) 730#define DOT_OFFSET offsetof(struct dirtemplate, dot_ino) 731 732/* 733 * Internal function prototypes. 734 */ 735static void check_clear_deps(struct mount *); 736static void softdep_error(char *, int); 737static int softdep_process_worklist(struct mount *, int); 738static int softdep_waitidle(struct mount *, int); 739static void drain_output(struct vnode *); 740static struct buf *getdirtybuf(struct buf *, struct rwlock *, int); 741static int check_inodedep_free(struct inodedep *); 742static void clear_remove(struct mount *); 743static void clear_inodedeps(struct mount *); 744static void unlinked_inodedep(struct mount *, struct inodedep *); 745static void clear_unlinked_inodedep(struct inodedep *); 746static struct inodedep *first_unlinked_inodedep(struct ufsmount *); 747static int flush_pagedep_deps(struct vnode *, struct mount *, 748 struct diraddhd *); 749static int free_pagedep(struct pagedep *); 750static int flush_newblk_dep(struct vnode *, struct mount *, ufs_lbn_t); 751static int flush_inodedep_deps(struct vnode *, struct mount *, ino_t); 752static int flush_deplist(struct allocdirectlst *, int, int *); 753static int sync_cgs(struct mount *, int); 754static int handle_written_filepage(struct pagedep *, struct buf *); 755static int handle_written_sbdep(struct sbdep *, struct buf *); 756static void initiate_write_sbdep(struct sbdep *); 757static void diradd_inode_written(struct diradd *, struct inodedep *); 758static int handle_written_indirdep(struct indirdep *, struct buf *, 759 struct buf**); 760static int handle_written_inodeblock(struct inodedep *, struct buf *); 761static int jnewblk_rollforward(struct jnewblk *, struct fs *, struct cg *, 762 uint8_t *); 763static int handle_written_bmsafemap(struct bmsafemap *, struct buf *); 764static void handle_written_jaddref(struct jaddref *); 765static void handle_written_jremref(struct jremref *); 766static void handle_written_jseg(struct jseg *, struct buf *); 767static void handle_written_jnewblk(struct jnewblk *); 768static void handle_written_jblkdep(struct jblkdep *); 769static void handle_written_jfreefrag(struct jfreefrag *); 770static void complete_jseg(struct jseg *); 771static void complete_jsegs(struct jseg *); 772static void jseg_write(struct ufsmount *ump, struct jseg *, uint8_t *); 773static void jaddref_write(struct jaddref *, struct jseg *, uint8_t *); 774static void jremref_write(struct jremref *, struct jseg *, uint8_t *); 775static void jmvref_write(struct jmvref *, struct jseg *, uint8_t *); 776static void jtrunc_write(struct jtrunc *, struct jseg *, uint8_t *); 777static void jfsync_write(struct jfsync *, struct jseg *, uint8_t *data); 778static void jnewblk_write(struct jnewblk *, struct jseg *, uint8_t *); 779static void jfreeblk_write(struct jfreeblk *, struct jseg *, uint8_t *); 780static void jfreefrag_write(struct jfreefrag *, struct jseg *, uint8_t *); 781static inline void inoref_write(struct inoref *, struct jseg *, 782 struct jrefrec *); 783static void handle_allocdirect_partdone(struct allocdirect *, 784 struct workhead *); 785static struct jnewblk *cancel_newblk(struct newblk *, struct worklist *, 786 struct workhead *); 787static void indirdep_complete(struct indirdep *); 788static int indirblk_lookup(struct mount *, ufs2_daddr_t); 789static void indirblk_insert(struct freework *); 790static void indirblk_remove(struct freework *); 791static void handle_allocindir_partdone(struct allocindir *); 792static void initiate_write_filepage(struct pagedep *, struct buf *); 793static void initiate_write_indirdep(struct indirdep*, struct buf *); 794static void handle_written_mkdir(struct mkdir *, int); 795static int jnewblk_rollback(struct jnewblk *, struct fs *, struct cg *, 796 uint8_t *); 797static void initiate_write_bmsafemap(struct bmsafemap *, struct buf *); 798static void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *); 799static void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *); 800static void handle_workitem_freefile(struct freefile *); 801static int handle_workitem_remove(struct dirrem *, int); 802static struct dirrem *newdirrem(struct buf *, struct inode *, 803 struct inode *, int, struct dirrem **); 804static struct indirdep *indirdep_lookup(struct mount *, struct inode *, 805 struct buf *); 806static void cancel_indirdep(struct indirdep *, struct buf *, 807 struct freeblks *); 808static void free_indirdep(struct indirdep *); 809static void free_diradd(struct diradd *, struct workhead *); 810static void merge_diradd(struct inodedep *, struct diradd *); 811static void complete_diradd(struct diradd *); 812static struct diradd *diradd_lookup(struct pagedep *, int); 813static struct jremref *cancel_diradd_dotdot(struct inode *, struct dirrem *, 814 struct jremref *); 815static struct jremref *cancel_mkdir_dotdot(struct inode *, struct dirrem *, 816 struct jremref *); 817static void cancel_diradd(struct diradd *, struct dirrem *, struct jremref *, 818 struct jremref *, struct jremref *); 819static void dirrem_journal(struct dirrem *, struct jremref *, struct jremref *, 820 struct jremref *); 821static void cancel_allocindir(struct allocindir *, struct buf *bp, 822 struct freeblks *, int); 823static int setup_trunc_indir(struct freeblks *, struct inode *, 824 ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t); 825static void complete_trunc_indir(struct freework *); 826static void trunc_indirdep(struct indirdep *, struct freeblks *, struct buf *, 827 int); 828static void complete_mkdir(struct mkdir *); 829static void free_newdirblk(struct newdirblk *); 830static void free_jremref(struct jremref *); 831static void free_jaddref(struct jaddref *); 832static void free_jsegdep(struct jsegdep *); 833static void free_jsegs(struct jblocks *); 834static void rele_jseg(struct jseg *); 835static void free_jseg(struct jseg *, struct jblocks *); 836static void free_jnewblk(struct jnewblk *); 837static void free_jblkdep(struct jblkdep *); 838static void free_jfreefrag(struct jfreefrag *); 839static void free_freedep(struct freedep *); 840static void journal_jremref(struct dirrem *, struct jremref *, 841 struct inodedep *); 842static void cancel_jnewblk(struct jnewblk *, struct workhead *); 843static int cancel_jaddref(struct jaddref *, struct inodedep *, 844 struct workhead *); 845static void cancel_jfreefrag(struct jfreefrag *); 846static inline void setup_freedirect(struct freeblks *, struct inode *, 847 int, int); 848static inline void setup_freeext(struct freeblks *, struct inode *, int, int); 849static inline void setup_freeindir(struct freeblks *, struct inode *, int, 850 ufs_lbn_t, int); 851static inline struct freeblks *newfreeblks(struct mount *, struct inode *); 852static void freeblks_free(struct ufsmount *, struct freeblks *, int); 853static void indir_trunc(struct freework *, ufs2_daddr_t, ufs_lbn_t); 854static ufs2_daddr_t blkcount(struct fs *, ufs2_daddr_t, off_t); 855static int trunc_check_buf(struct buf *, int *, ufs_lbn_t, int, int); 856static void trunc_dependencies(struct inode *, struct freeblks *, ufs_lbn_t, 857 int, int); 858static void trunc_pages(struct inode *, off_t, ufs2_daddr_t, int); 859static int cancel_pagedep(struct pagedep *, struct freeblks *, int); 860static int deallocate_dependencies(struct buf *, struct freeblks *, int); 861static void newblk_freefrag(struct newblk*); 862static void free_newblk(struct newblk *); 863static void cancel_allocdirect(struct allocdirectlst *, 864 struct allocdirect *, struct freeblks *); 865static int check_inode_unwritten(struct inodedep *); 866static int free_inodedep(struct inodedep *); 867static void freework_freeblock(struct freework *); 868static void freework_enqueue(struct freework *); 869static int handle_workitem_freeblocks(struct freeblks *, int); 870static int handle_complete_freeblocks(struct freeblks *, int); 871static void handle_workitem_indirblk(struct freework *); 872static void handle_written_freework(struct freework *); 873static void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *); 874static struct worklist *jnewblk_merge(struct worklist *, struct worklist *, 875 struct workhead *); 876static struct freefrag *setup_allocindir_phase2(struct buf *, struct inode *, 877 struct inodedep *, struct allocindir *, ufs_lbn_t); 878static struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t, 879 ufs2_daddr_t, ufs_lbn_t); 880static void handle_workitem_freefrag(struct freefrag *); 881static struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long, 882 ufs_lbn_t); 883static void allocdirect_merge(struct allocdirectlst *, 884 struct allocdirect *, struct allocdirect *); 885static struct freefrag *allocindir_merge(struct allocindir *, 886 struct allocindir *); 887static int bmsafemap_find(struct bmsafemap_hashhead *, int, 888 struct bmsafemap **); 889static struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *, 890 int cg, struct bmsafemap *); 891static int newblk_find(struct newblk_hashhead *, ufs2_daddr_t, int, 892 struct newblk **); 893static int newblk_lookup(struct mount *, ufs2_daddr_t, int, struct newblk **); 894static int inodedep_find(struct inodedep_hashhead *, ino_t, 895 struct inodedep **); 896static int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **); 897static int pagedep_lookup(struct mount *, struct buf *bp, ino_t, ufs_lbn_t, 898 int, struct pagedep **); 899static int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t, 900 struct pagedep **); 901static void pause_timer(void *); 902static int request_cleanup(struct mount *, int); 903static int process_worklist_item(struct mount *, int, int); 904static void process_removes(struct vnode *); 905static void process_truncates(struct vnode *); 906static void jwork_move(struct workhead *, struct workhead *); 907static void jwork_insert(struct workhead *, struct jsegdep *); 908static void add_to_worklist(struct worklist *, int); 909static void wake_worklist(struct worklist *); 910static void wait_worklist(struct worklist *, char *); 911static void remove_from_worklist(struct worklist *); 912static void softdep_flush(void *); 913static void softdep_flushjournal(struct mount *); 914static int softdep_speedup(struct ufsmount *); 915static void worklist_speedup(struct mount *); 916static int journal_mount(struct mount *, struct fs *, struct ucred *); 917static void journal_unmount(struct ufsmount *); 918static int journal_space(struct ufsmount *, int); 919static void journal_suspend(struct ufsmount *); 920static int journal_unsuspend(struct ufsmount *ump); 921static void softdep_prelink(struct vnode *, struct vnode *); 922static void add_to_journal(struct worklist *); 923static void remove_from_journal(struct worklist *); 924static void softdep_process_journal(struct mount *, struct worklist *, int); 925static struct jremref *newjremref(struct dirrem *, struct inode *, 926 struct inode *ip, off_t, nlink_t); 927static struct jaddref *newjaddref(struct inode *, ino_t, off_t, int16_t, 928 uint16_t); 929static inline void newinoref(struct inoref *, ino_t, ino_t, off_t, nlink_t, 930 uint16_t); 931static inline struct jsegdep *inoref_jseg(struct inoref *); 932static struct jmvref *newjmvref(struct inode *, ino_t, off_t, off_t); 933static struct jfreeblk *newjfreeblk(struct freeblks *, ufs_lbn_t, 934 ufs2_daddr_t, int); 935static void adjust_newfreework(struct freeblks *, int); 936static struct jtrunc *newjtrunc(struct freeblks *, off_t, int); 937static void move_newblock_dep(struct jaddref *, struct inodedep *); 938static void cancel_jfreeblk(struct freeblks *, ufs2_daddr_t); 939static struct jfreefrag *newjfreefrag(struct freefrag *, struct inode *, 940 ufs2_daddr_t, long, ufs_lbn_t); 941static struct freework *newfreework(struct ufsmount *, struct freeblks *, 942 struct freework *, ufs_lbn_t, ufs2_daddr_t, int, int, int); 943static int jwait(struct worklist *, int); 944static struct inodedep *inodedep_lookup_ip(struct inode *); 945static int bmsafemap_backgroundwrite(struct bmsafemap *, struct buf *); 946static struct freefile *handle_bufwait(struct inodedep *, struct workhead *); 947static void handle_jwork(struct workhead *); 948static struct mkdir *setup_newdir(struct diradd *, ino_t, ino_t, struct buf *, 949 struct mkdir **); 950static struct jblocks *jblocks_create(void); 951static ufs2_daddr_t jblocks_alloc(struct jblocks *, int, int *); 952static void jblocks_free(struct jblocks *, struct mount *, int); 953static void jblocks_destroy(struct jblocks *); 954static void jblocks_add(struct jblocks *, ufs2_daddr_t, int); 955 956/* 957 * Exported softdep operations. 958 */ 959static void softdep_disk_io_initiation(struct buf *); 960static void softdep_disk_write_complete(struct buf *); 961static void softdep_deallocate_dependencies(struct buf *); 962static int softdep_count_dependencies(struct buf *bp, int); 963 964/* 965 * Global lock over all of soft updates. 966 */ 967static struct mtx lk; 968MTX_SYSINIT(softdep_lock, &lk, "Global Softdep Lock", MTX_DEF); 969 970#define ACQUIRE_GBLLOCK(lk) mtx_lock(lk) 971#define FREE_GBLLOCK(lk) mtx_unlock(lk) 972#define GBLLOCK_OWNED(lk) mtx_assert((lk), MA_OWNED) 973 974/* 975 * Per-filesystem soft-updates locking. 976 */ 977#define LOCK_PTR(ump) (&(ump)->um_softdep->sd_fslock) 978#define TRY_ACQUIRE_LOCK(ump) rw_try_wlock(&(ump)->um_softdep->sd_fslock) 979#define ACQUIRE_LOCK(ump) rw_wlock(&(ump)->um_softdep->sd_fslock) 980#define FREE_LOCK(ump) rw_wunlock(&(ump)->um_softdep->sd_fslock) 981#define LOCK_OWNED(ump) rw_assert(&(ump)->um_softdep->sd_fslock, \ 982 RA_WLOCKED) 983 984#define BUF_AREC(bp) lockallowrecurse(&(bp)->b_lock) 985#define BUF_NOREC(bp) lockdisablerecurse(&(bp)->b_lock) 986 987/* 988 * Worklist queue management. 989 * These routines require that the lock be held. 990 */ 991#ifndef /* NOT */ DEBUG 992#define WORKLIST_INSERT(head, item) do { \ 993 (item)->wk_state |= ONWORKLIST; \ 994 LIST_INSERT_HEAD(head, item, wk_list); \ 995} while (0) 996#define WORKLIST_REMOVE(item) do { \ 997 (item)->wk_state &= ~ONWORKLIST; \ 998 LIST_REMOVE(item, wk_list); \ 999} while (0) 1000#define WORKLIST_INSERT_UNLOCKED WORKLIST_INSERT 1001#define WORKLIST_REMOVE_UNLOCKED WORKLIST_REMOVE 1002 1003#else /* DEBUG */ 1004static void worklist_insert(struct workhead *, struct worklist *, int); 1005static void worklist_remove(struct worklist *, int); 1006 1007#define WORKLIST_INSERT(head, item) worklist_insert(head, item, 1) 1008#define WORKLIST_INSERT_UNLOCKED(head, item) worklist_insert(head, item, 0) 1009#define WORKLIST_REMOVE(item) worklist_remove(item, 1) 1010#define WORKLIST_REMOVE_UNLOCKED(item) worklist_remove(item, 0) 1011 1012static void 1013worklist_insert(head, item, locked) 1014 struct workhead *head; 1015 struct worklist *item; 1016 int locked; 1017{ 1018 1019 if (locked) 1020 LOCK_OWNED(VFSTOUFS(item->wk_mp)); 1021 if (item->wk_state & ONWORKLIST) 1022 panic("worklist_insert: %p %s(0x%X) already on list", 1023 item, TYPENAME(item->wk_type), item->wk_state); 1024 item->wk_state |= ONWORKLIST; 1025 LIST_INSERT_HEAD(head, item, wk_list); 1026} 1027 1028static void 1029worklist_remove(item, locked) 1030 struct worklist *item; 1031 int locked; 1032{ 1033 1034 if (locked) 1035 LOCK_OWNED(VFSTOUFS(item->wk_mp)); 1036 if ((item->wk_state & ONWORKLIST) == 0) 1037 panic("worklist_remove: %p %s(0x%X) not on list", 1038 item, TYPENAME(item->wk_type), item->wk_state); 1039 item->wk_state &= ~ONWORKLIST; 1040 LIST_REMOVE(item, wk_list); 1041} 1042#endif /* DEBUG */ 1043 1044/* 1045 * Merge two jsegdeps keeping only the oldest one as newer references 1046 * can't be discarded until after older references. 1047 */ 1048static inline struct jsegdep * 1049jsegdep_merge(struct jsegdep *one, struct jsegdep *two) 1050{ 1051 struct jsegdep *swp; 1052 1053 if (two == NULL) 1054 return (one); 1055 1056 if (one->jd_seg->js_seq > two->jd_seg->js_seq) { 1057 swp = one; 1058 one = two; 1059 two = swp; 1060 } 1061 WORKLIST_REMOVE(&two->jd_list); 1062 free_jsegdep(two); 1063 1064 return (one); 1065} 1066 1067/* 1068 * If two freedeps are compatible free one to reduce list size. 1069 */ 1070static inline struct freedep * 1071freedep_merge(struct freedep *one, struct freedep *two) 1072{ 1073 if (two == NULL) 1074 return (one); 1075 1076 if (one->fd_freework == two->fd_freework) { 1077 WORKLIST_REMOVE(&two->fd_list); 1078 free_freedep(two); 1079 } 1080 return (one); 1081} 1082 1083/* 1084 * Move journal work from one list to another. Duplicate freedeps and 1085 * jsegdeps are coalesced to keep the lists as small as possible. 1086 */ 1087static void 1088jwork_move(dst, src) 1089 struct workhead *dst; 1090 struct workhead *src; 1091{ 1092 struct freedep *freedep; 1093 struct jsegdep *jsegdep; 1094 struct worklist *wkn; 1095 struct worklist *wk; 1096 1097 KASSERT(dst != src, 1098 ("jwork_move: dst == src")); 1099 freedep = NULL; 1100 jsegdep = NULL; 1101 LIST_FOREACH_SAFE(wk, dst, wk_list, wkn) { 1102 if (wk->wk_type == D_JSEGDEP) 1103 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1104 if (wk->wk_type == D_FREEDEP) 1105 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1106 } 1107 1108 while ((wk = LIST_FIRST(src)) != NULL) { 1109 WORKLIST_REMOVE(wk); 1110 WORKLIST_INSERT(dst, wk); 1111 if (wk->wk_type == D_JSEGDEP) { 1112 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1113 continue; 1114 } 1115 if (wk->wk_type == D_FREEDEP) 1116 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1117 } 1118} 1119 1120static void 1121jwork_insert(dst, jsegdep) 1122 struct workhead *dst; 1123 struct jsegdep *jsegdep; 1124{ 1125 struct jsegdep *jsegdepn; 1126 struct worklist *wk; 1127 1128 LIST_FOREACH(wk, dst, wk_list) 1129 if (wk->wk_type == D_JSEGDEP) 1130 break; 1131 if (wk == NULL) { 1132 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1133 return; 1134 } 1135 jsegdepn = WK_JSEGDEP(wk); 1136 if (jsegdep->jd_seg->js_seq < jsegdepn->jd_seg->js_seq) { 1137 WORKLIST_REMOVE(wk); 1138 free_jsegdep(jsegdepn); 1139 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1140 } else 1141 free_jsegdep(jsegdep); 1142} 1143 1144/* 1145 * Routines for tracking and managing workitems. 1146 */ 1147static void workitem_free(struct worklist *, int); 1148static void workitem_alloc(struct worklist *, int, struct mount *); 1149static void workitem_reassign(struct worklist *, int); 1150 1151#define WORKITEM_FREE(item, type) \ 1152 workitem_free((struct worklist *)(item), (type)) 1153#define WORKITEM_REASSIGN(item, type) \ 1154 workitem_reassign((struct worklist *)(item), (type)) 1155 1156static void 1157workitem_free(item, type) 1158 struct worklist *item; 1159 int type; 1160{ 1161 struct ufsmount *ump; 1162 1163#ifdef DEBUG 1164 if (item->wk_state & ONWORKLIST) 1165 panic("workitem_free: %s(0x%X) still on list", 1166 TYPENAME(item->wk_type), item->wk_state); 1167 if (item->wk_type != type && type != D_NEWBLK) 1168 panic("workitem_free: type mismatch %s != %s", 1169 TYPENAME(item->wk_type), TYPENAME(type)); 1170#endif 1171 if (item->wk_state & IOWAITING) 1172 wakeup(item); 1173 ump = VFSTOUFS(item->wk_mp); 1174 LOCK_OWNED(ump); 1175 KASSERT(ump->softdep_deps > 0, 1176 ("workitem_free: %s: softdep_deps going negative", 1177 ump->um_fs->fs_fsmnt)); 1178 if (--ump->softdep_deps == 0 && ump->softdep_req) 1179 wakeup(&ump->softdep_deps); 1180 KASSERT(dep_current[item->wk_type] > 0, 1181 ("workitem_free: %s: dep_current[%s] going negative", 1182 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1183 KASSERT(ump->softdep_curdeps[item->wk_type] > 0, 1184 ("workitem_free: %s: softdep_curdeps[%s] going negative", 1185 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1186 atomic_subtract_long(&dep_current[item->wk_type], 1); 1187 ump->softdep_curdeps[item->wk_type] -= 1; 1188 free(item, DtoM(type)); 1189} 1190 1191static void 1192workitem_alloc(item, type, mp) 1193 struct worklist *item; 1194 int type; 1195 struct mount *mp; 1196{ 1197 struct ufsmount *ump; 1198 1199 item->wk_type = type; 1200 item->wk_mp = mp; 1201 item->wk_state = 0; 1202 1203 ump = VFSTOUFS(mp); 1204 ACQUIRE_GBLLOCK(&lk); 1205 dep_current[type]++; 1206 if (dep_current[type] > dep_highuse[type]) 1207 dep_highuse[type] = dep_current[type]; 1208 dep_total[type]++; 1209 FREE_GBLLOCK(&lk); 1210 ACQUIRE_LOCK(ump); 1211 ump->softdep_curdeps[type] += 1; 1212 ump->softdep_deps++; 1213 ump->softdep_accdeps++; 1214 FREE_LOCK(ump); 1215} 1216 1217static void 1218workitem_reassign(item, newtype) 1219 struct worklist *item; 1220 int newtype; 1221{ 1222 struct ufsmount *ump; 1223 1224 ump = VFSTOUFS(item->wk_mp); 1225 LOCK_OWNED(ump); 1226 KASSERT(ump->softdep_curdeps[item->wk_type] > 0, 1227 ("workitem_reassign: %s: softdep_curdeps[%s] going negative", 1228 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1229 ump->softdep_curdeps[item->wk_type] -= 1; 1230 ump->softdep_curdeps[newtype] += 1; 1231 KASSERT(dep_current[item->wk_type] > 0, 1232 ("workitem_reassign: %s: dep_current[%s] going negative", 1233 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1234 ACQUIRE_GBLLOCK(&lk); 1235 dep_current[newtype]++; 1236 dep_current[item->wk_type]--; 1237 if (dep_current[newtype] > dep_highuse[newtype]) 1238 dep_highuse[newtype] = dep_current[newtype]; 1239 dep_total[newtype]++; 1240 FREE_GBLLOCK(&lk); 1241 item->wk_type = newtype; 1242} 1243 1244/* 1245 * Workitem queue management 1246 */ 1247static int max_softdeps; /* maximum number of structs before slowdown */ 1248static int tickdelay = 2; /* number of ticks to pause during slowdown */ 1249static int proc_waiting; /* tracks whether we have a timeout posted */ 1250static int *stat_countp; /* statistic to count in proc_waiting timeout */ 1251static struct callout softdep_callout; 1252static int req_clear_inodedeps; /* syncer process flush some inodedeps */ 1253static int req_clear_remove; /* syncer process flush some freeblks */ 1254static int softdep_flushcache = 0; /* Should we do BIO_FLUSH? */ 1255 1256/* 1257 * runtime statistics 1258 */ 1259static int stat_flush_threads; /* number of softdep flushing threads */ 1260static int stat_worklist_push; /* number of worklist cleanups */ 1261static int stat_blk_limit_push; /* number of times block limit neared */ 1262static int stat_ino_limit_push; /* number of times inode limit neared */ 1263static int stat_blk_limit_hit; /* number of times block slowdown imposed */ 1264static int stat_ino_limit_hit; /* number of times inode slowdown imposed */ 1265static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */ 1266static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */ 1267static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */ 1268static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */ 1269static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */ 1270static int stat_jaddref; /* bufs redirtied as ino bitmap can not write */ 1271static int stat_jnewblk; /* bufs redirtied as blk bitmap can not write */ 1272static int stat_journal_min; /* Times hit journal min threshold */ 1273static int stat_journal_low; /* Times hit journal low threshold */ 1274static int stat_journal_wait; /* Times blocked in jwait(). */ 1275static int stat_jwait_filepage; /* Times blocked in jwait() for filepage. */ 1276static int stat_jwait_freeblks; /* Times blocked in jwait() for freeblks. */ 1277static int stat_jwait_inode; /* Times blocked in jwait() for inodes. */ 1278static int stat_jwait_newblk; /* Times blocked in jwait() for newblks. */ 1279static int stat_cleanup_high_delay; /* Maximum cleanup delay (in ticks) */ 1280static int stat_cleanup_blkrequests; /* Number of block cleanup requests */ 1281static int stat_cleanup_inorequests; /* Number of inode cleanup requests */ 1282static int stat_cleanup_retries; /* Number of cleanups that needed to flush */ 1283static int stat_cleanup_failures; /* Number of cleanup requests that failed */ 1284static int stat_emptyjblocks; /* Number of potentially empty journal blocks */ 1285 1286SYSCTL_INT(_debug_softdep, OID_AUTO, max_softdeps, CTLFLAG_RW, 1287 &max_softdeps, 0, ""); 1288SYSCTL_INT(_debug_softdep, OID_AUTO, tickdelay, CTLFLAG_RW, 1289 &tickdelay, 0, ""); 1290SYSCTL_INT(_debug_softdep, OID_AUTO, flush_threads, CTLFLAG_RD, 1291 &stat_flush_threads, 0, ""); 1292SYSCTL_INT(_debug_softdep, OID_AUTO, worklist_push, CTLFLAG_RW, 1293 &stat_worklist_push, 0,""); 1294SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_push, CTLFLAG_RW, 1295 &stat_blk_limit_push, 0,""); 1296SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_push, CTLFLAG_RW, 1297 &stat_ino_limit_push, 0,""); 1298SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_hit, CTLFLAG_RW, 1299 &stat_blk_limit_hit, 0, ""); 1300SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_hit, CTLFLAG_RW, 1301 &stat_ino_limit_hit, 0, ""); 1302SYSCTL_INT(_debug_softdep, OID_AUTO, sync_limit_hit, CTLFLAG_RW, 1303 &stat_sync_limit_hit, 0, ""); 1304SYSCTL_INT(_debug_softdep, OID_AUTO, indir_blk_ptrs, CTLFLAG_RW, 1305 &stat_indir_blk_ptrs, 0, ""); 1306SYSCTL_INT(_debug_softdep, OID_AUTO, inode_bitmap, CTLFLAG_RW, 1307 &stat_inode_bitmap, 0, ""); 1308SYSCTL_INT(_debug_softdep, OID_AUTO, direct_blk_ptrs, CTLFLAG_RW, 1309 &stat_direct_blk_ptrs, 0, ""); 1310SYSCTL_INT(_debug_softdep, OID_AUTO, dir_entry, CTLFLAG_RW, 1311 &stat_dir_entry, 0, ""); 1312SYSCTL_INT(_debug_softdep, OID_AUTO, jaddref_rollback, CTLFLAG_RW, 1313 &stat_jaddref, 0, ""); 1314SYSCTL_INT(_debug_softdep, OID_AUTO, jnewblk_rollback, CTLFLAG_RW, 1315 &stat_jnewblk, 0, ""); 1316SYSCTL_INT(_debug_softdep, OID_AUTO, journal_low, CTLFLAG_RW, 1317 &stat_journal_low, 0, ""); 1318SYSCTL_INT(_debug_softdep, OID_AUTO, journal_min, CTLFLAG_RW, 1319 &stat_journal_min, 0, ""); 1320SYSCTL_INT(_debug_softdep, OID_AUTO, journal_wait, CTLFLAG_RW, 1321 &stat_journal_wait, 0, ""); 1322SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_filepage, CTLFLAG_RW, 1323 &stat_jwait_filepage, 0, ""); 1324SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_freeblks, CTLFLAG_RW, 1325 &stat_jwait_freeblks, 0, ""); 1326SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_inode, CTLFLAG_RW, 1327 &stat_jwait_inode, 0, ""); 1328SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_newblk, CTLFLAG_RW, 1329 &stat_jwait_newblk, 0, ""); 1330SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_blkrequests, CTLFLAG_RW, 1331 &stat_cleanup_blkrequests, 0, ""); 1332SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_inorequests, CTLFLAG_RW, 1333 &stat_cleanup_inorequests, 0, ""); 1334SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_high_delay, CTLFLAG_RW, 1335 &stat_cleanup_high_delay, 0, ""); 1336SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_retries, CTLFLAG_RW, 1337 &stat_cleanup_retries, 0, ""); 1338SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_failures, CTLFLAG_RW, 1339 &stat_cleanup_failures, 0, ""); 1340SYSCTL_INT(_debug_softdep, OID_AUTO, flushcache, CTLFLAG_RW, 1341 &softdep_flushcache, 0, ""); 1342SYSCTL_INT(_debug_softdep, OID_AUTO, emptyjblocks, CTLFLAG_RD, 1343 &stat_emptyjblocks, 0, ""); 1344 1345SYSCTL_DECL(_vfs_ffs); 1346 1347/* Whether to recompute the summary at mount time */ 1348static int compute_summary_at_mount = 0; 1349SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW, 1350 &compute_summary_at_mount, 0, "Recompute summary at mount"); 1351static int print_threads = 0; 1352SYSCTL_INT(_debug_softdep, OID_AUTO, print_threads, CTLFLAG_RW, 1353 &print_threads, 0, "Notify flusher thread start/stop"); 1354 1355/* List of all filesystems mounted with soft updates */ 1356static TAILQ_HEAD(, mount_softdeps) softdepmounts; 1357 1358/* 1359 * This function cleans the worklist for a filesystem. 1360 * Each filesystem running with soft dependencies gets its own 1361 * thread to run in this function. The thread is started up in 1362 * softdep_mount and shutdown in softdep_unmount. They show up 1363 * as part of the kernel "bufdaemon" process whose process 1364 * entry is available in bufdaemonproc. 1365 */ 1366static int searchfailed; 1367extern struct proc *bufdaemonproc; 1368static void 1369softdep_flush(addr) 1370 void *addr; 1371{ 1372 struct mount *mp; 1373 struct thread *td; 1374 struct ufsmount *ump; 1375 1376 td = curthread; 1377 td->td_pflags |= TDP_NORUNNINGBUF; 1378 mp = (struct mount *)addr; 1379 ump = VFSTOUFS(mp); 1380 atomic_add_int(&stat_flush_threads, 1); 1381 ACQUIRE_LOCK(ump); 1382 ump->softdep_flags &= ~FLUSH_STARTING; 1383 wakeup(&ump->softdep_flushtd); 1384 FREE_LOCK(ump); 1385 if (print_threads) { 1386 if (stat_flush_threads == 1) 1387 printf("Running %s at pid %d\n", bufdaemonproc->p_comm, 1388 bufdaemonproc->p_pid); 1389 printf("Start thread %s\n", td->td_name); 1390 } 1391 for (;;) { 1392 while (softdep_process_worklist(mp, 0) > 0 || 1393 (MOUNTEDSUJ(mp) && 1394 VFSTOUFS(mp)->softdep_jblocks->jb_suspended)) 1395 kthread_suspend_check(); 1396 ACQUIRE_LOCK(ump); 1397 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1398 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, 1399 "sdflush", hz / 2); 1400 ump->softdep_flags &= ~FLUSH_CLEANUP; 1401 /* 1402 * Check to see if we are done and need to exit. 1403 */ 1404 if ((ump->softdep_flags & FLUSH_EXIT) == 0) { 1405 FREE_LOCK(ump); 1406 continue; 1407 } 1408 ump->softdep_flags &= ~FLUSH_EXIT; 1409 FREE_LOCK(ump); 1410 wakeup(&ump->softdep_flags); 1411 if (print_threads) 1412 printf("Stop thread %s: searchfailed %d, did cleanups %d\n", td->td_name, searchfailed, ump->um_softdep->sd_cleanups); 1413 atomic_subtract_int(&stat_flush_threads, 1); 1414 kthread_exit(); 1415 panic("kthread_exit failed\n"); 1416 } 1417} 1418 1419static void 1420worklist_speedup(mp) 1421 struct mount *mp; 1422{ 1423 struct ufsmount *ump; 1424 1425 ump = VFSTOUFS(mp); 1426 LOCK_OWNED(ump); 1427 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1428 ump->softdep_flags |= FLUSH_CLEANUP; 1429 wakeup(&ump->softdep_flushtd); 1430} 1431 1432static int 1433softdep_speedup(ump) 1434 struct ufsmount *ump; 1435{ 1436 struct ufsmount *altump; 1437 struct mount_softdeps *sdp; 1438 1439 LOCK_OWNED(ump); 1440 worklist_speedup(ump->um_mountp); 1441 bd_speedup(); 1442 /* 1443 * If we have global shortages, then we need other 1444 * filesystems to help with the cleanup. Here we wakeup a 1445 * flusher thread for a filesystem that is over its fair 1446 * share of resources. 1447 */ 1448 if (req_clear_inodedeps || req_clear_remove) { 1449 ACQUIRE_GBLLOCK(&lk); 1450 TAILQ_FOREACH(sdp, &softdepmounts, sd_next) { 1451 if ((altump = sdp->sd_ump) == ump) 1452 continue; 1453 if (((req_clear_inodedeps && 1454 altump->softdep_curdeps[D_INODEDEP] > 1455 max_softdeps / stat_flush_threads) || 1456 (req_clear_remove && 1457 altump->softdep_curdeps[D_DIRREM] > 1458 (max_softdeps / 2) / stat_flush_threads)) && 1459 TRY_ACQUIRE_LOCK(altump)) 1460 break; 1461 } 1462 if (sdp == NULL) { 1463 searchfailed++; 1464 FREE_GBLLOCK(&lk); 1465 } else { 1466 /* 1467 * Move to the end of the list so we pick a 1468 * different one on out next try. 1469 */ 1470 TAILQ_REMOVE(&softdepmounts, sdp, sd_next); 1471 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next); 1472 FREE_GBLLOCK(&lk); 1473 if ((altump->softdep_flags & 1474 (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) 1475 altump->softdep_flags |= FLUSH_CLEANUP; 1476 altump->um_softdep->sd_cleanups++; 1477 wakeup(&altump->softdep_flushtd); 1478 FREE_LOCK(altump); 1479 } 1480 } 1481 return (speedup_syncer()); 1482} 1483 1484/* 1485 * Add an item to the end of the work queue. 1486 * This routine requires that the lock be held. 1487 * This is the only routine that adds items to the list. 1488 * The following routine is the only one that removes items 1489 * and does so in order from first to last. 1490 */ 1491 1492#define WK_HEAD 0x0001 /* Add to HEAD. */ 1493#define WK_NODELAY 0x0002 /* Process immediately. */ 1494 1495static void 1496add_to_worklist(wk, flags) 1497 struct worklist *wk; 1498 int flags; 1499{ 1500 struct ufsmount *ump; 1501 1502 ump = VFSTOUFS(wk->wk_mp); 1503 LOCK_OWNED(ump); 1504 if (wk->wk_state & ONWORKLIST) 1505 panic("add_to_worklist: %s(0x%X) already on list", 1506 TYPENAME(wk->wk_type), wk->wk_state); 1507 wk->wk_state |= ONWORKLIST; 1508 if (ump->softdep_on_worklist == 0) { 1509 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1510 ump->softdep_worklist_tail = wk; 1511 } else if (flags & WK_HEAD) { 1512 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1513 } else { 1514 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list); 1515 ump->softdep_worklist_tail = wk; 1516 } 1517 ump->softdep_on_worklist += 1; 1518 if (flags & WK_NODELAY) 1519 worklist_speedup(wk->wk_mp); 1520} 1521 1522/* 1523 * Remove the item to be processed. If we are removing the last 1524 * item on the list, we need to recalculate the tail pointer. 1525 */ 1526static void 1527remove_from_worklist(wk) 1528 struct worklist *wk; 1529{ 1530 struct ufsmount *ump; 1531 1532 ump = VFSTOUFS(wk->wk_mp); 1533 WORKLIST_REMOVE(wk); 1534 if (ump->softdep_worklist_tail == wk) 1535 ump->softdep_worklist_tail = 1536 (struct worklist *)wk->wk_list.le_prev; 1537 ump->softdep_on_worklist -= 1; 1538} 1539 1540static void 1541wake_worklist(wk) 1542 struct worklist *wk; 1543{ 1544 if (wk->wk_state & IOWAITING) { 1545 wk->wk_state &= ~IOWAITING; 1546 wakeup(wk); 1547 } 1548} 1549 1550static void 1551wait_worklist(wk, wmesg) 1552 struct worklist *wk; 1553 char *wmesg; 1554{ 1555 struct ufsmount *ump; 1556 1557 ump = VFSTOUFS(wk->wk_mp); 1558 wk->wk_state |= IOWAITING; 1559 msleep(wk, LOCK_PTR(ump), PVM, wmesg, 0); 1560} 1561 1562/* 1563 * Process that runs once per second to handle items in the background queue. 1564 * 1565 * Note that we ensure that everything is done in the order in which they 1566 * appear in the queue. The code below depends on this property to ensure 1567 * that blocks of a file are freed before the inode itself is freed. This 1568 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated 1569 * until all the old ones have been purged from the dependency lists. 1570 */ 1571static int 1572softdep_process_worklist(mp, full) 1573 struct mount *mp; 1574 int full; 1575{ 1576 int cnt, matchcnt; 1577 struct ufsmount *ump; 1578 long starttime; 1579 1580 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp")); 1581 if (MOUNTEDSOFTDEP(mp) == 0) 1582 return (0); 1583 matchcnt = 0; 1584 ump = VFSTOUFS(mp); 1585 ACQUIRE_LOCK(ump); 1586 starttime = time_second; 1587 softdep_process_journal(mp, NULL, full ? MNT_WAIT : 0); 1588 check_clear_deps(mp); 1589 while (ump->softdep_on_worklist > 0) { 1590 if ((cnt = process_worklist_item(mp, 10, LK_NOWAIT)) == 0) 1591 break; 1592 else 1593 matchcnt += cnt; 1594 check_clear_deps(mp); 1595 /* 1596 * We do not generally want to stop for buffer space, but if 1597 * we are really being a buffer hog, we will stop and wait. 1598 */ 1599 if (should_yield()) { 1600 FREE_LOCK(ump); 1601 kern_yield(PRI_USER); 1602 bwillwrite(); 1603 ACQUIRE_LOCK(ump); 1604 } 1605 /* 1606 * Never allow processing to run for more than one 1607 * second. This gives the syncer thread the opportunity 1608 * to pause if appropriate. 1609 */ 1610 if (!full && starttime != time_second) 1611 break; 1612 } 1613 if (full == 0) 1614 journal_unsuspend(ump); 1615 FREE_LOCK(ump); 1616 return (matchcnt); 1617} 1618 1619/* 1620 * Process all removes associated with a vnode if we are running out of 1621 * journal space. Any other process which attempts to flush these will 1622 * be unable as we have the vnodes locked. 1623 */ 1624static void 1625process_removes(vp) 1626 struct vnode *vp; 1627{ 1628 struct inodedep *inodedep; 1629 struct dirrem *dirrem; 1630 struct ufsmount *ump; 1631 struct mount *mp; 1632 ino_t inum; 1633 1634 mp = vp->v_mount; 1635 ump = VFSTOUFS(mp); 1636 LOCK_OWNED(ump); 1637 inum = VTOI(vp)->i_number; 1638 for (;;) { 1639top: 1640 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1641 return; 1642 LIST_FOREACH(dirrem, &inodedep->id_dirremhd, dm_inonext) { 1643 /* 1644 * If another thread is trying to lock this vnode 1645 * it will fail but we must wait for it to do so 1646 * before we can proceed. 1647 */ 1648 if (dirrem->dm_state & INPROGRESS) { 1649 wait_worklist(&dirrem->dm_list, "pwrwait"); 1650 goto top; 1651 } 1652 if ((dirrem->dm_state & (COMPLETE | ONWORKLIST)) == 1653 (COMPLETE | ONWORKLIST)) 1654 break; 1655 } 1656 if (dirrem == NULL) 1657 return; 1658 remove_from_worklist(&dirrem->dm_list); 1659 FREE_LOCK(ump); 1660 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1661 panic("process_removes: suspended filesystem"); 1662 handle_workitem_remove(dirrem, 0); 1663 vn_finished_secondary_write(mp); 1664 ACQUIRE_LOCK(ump); 1665 } 1666} 1667 1668/* 1669 * Process all truncations associated with a vnode if we are running out 1670 * of journal space. This is called when the vnode lock is already held 1671 * and no other process can clear the truncation. This function returns 1672 * a value greater than zero if it did any work. 1673 */ 1674static void 1675process_truncates(vp) 1676 struct vnode *vp; 1677{ 1678 struct inodedep *inodedep; 1679 struct freeblks *freeblks; 1680 struct ufsmount *ump; 1681 struct mount *mp; 1682 ino_t inum; 1683 int cgwait; 1684 1685 mp = vp->v_mount; 1686 ump = VFSTOUFS(mp); 1687 LOCK_OWNED(ump); 1688 inum = VTOI(vp)->i_number; 1689 for (;;) { 1690 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1691 return; 1692 cgwait = 0; 1693 TAILQ_FOREACH(freeblks, &inodedep->id_freeblklst, fb_next) { 1694 /* Journal entries not yet written. */ 1695 if (!LIST_EMPTY(&freeblks->fb_jblkdephd)) { 1696 jwait(&LIST_FIRST( 1697 &freeblks->fb_jblkdephd)->jb_list, 1698 MNT_WAIT); 1699 break; 1700 } 1701 /* Another thread is executing this item. */ 1702 if (freeblks->fb_state & INPROGRESS) { 1703 wait_worklist(&freeblks->fb_list, "ptrwait"); 1704 break; 1705 } 1706 /* Freeblks is waiting on a inode write. */ 1707 if ((freeblks->fb_state & COMPLETE) == 0) { 1708 FREE_LOCK(ump); 1709 ffs_update(vp, 1); 1710 ACQUIRE_LOCK(ump); 1711 break; 1712 } 1713 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST)) == 1714 (ALLCOMPLETE | ONWORKLIST)) { 1715 remove_from_worklist(&freeblks->fb_list); 1716 freeblks->fb_state |= INPROGRESS; 1717 FREE_LOCK(ump); 1718 if (vn_start_secondary_write(NULL, &mp, 1719 V_NOWAIT)) 1720 panic("process_truncates: " 1721 "suspended filesystem"); 1722 handle_workitem_freeblocks(freeblks, 0); 1723 vn_finished_secondary_write(mp); 1724 ACQUIRE_LOCK(ump); 1725 break; 1726 } 1727 if (freeblks->fb_cgwait) 1728 cgwait++; 1729 } 1730 if (cgwait) { 1731 FREE_LOCK(ump); 1732 sync_cgs(mp, MNT_WAIT); 1733 ffs_sync_snap(mp, MNT_WAIT); 1734 ACQUIRE_LOCK(ump); 1735 continue; 1736 } 1737 if (freeblks == NULL) 1738 break; 1739 } 1740 return; 1741} 1742 1743/* 1744 * Process one item on the worklist. 1745 */ 1746static int 1747process_worklist_item(mp, target, flags) 1748 struct mount *mp; 1749 int target; 1750 int flags; 1751{ 1752 struct worklist sentinel; 1753 struct worklist *wk; 1754 struct ufsmount *ump; 1755 int matchcnt; 1756 int error; 1757 1758 KASSERT(mp != NULL, ("process_worklist_item: NULL mp")); 1759 /* 1760 * If we are being called because of a process doing a 1761 * copy-on-write, then it is not safe to write as we may 1762 * recurse into the copy-on-write routine. 1763 */ 1764 if (curthread->td_pflags & TDP_COWINPROGRESS) 1765 return (-1); 1766 PHOLD(curproc); /* Don't let the stack go away. */ 1767 ump = VFSTOUFS(mp); 1768 LOCK_OWNED(ump); 1769 matchcnt = 0; 1770 sentinel.wk_mp = NULL; 1771 sentinel.wk_type = D_SENTINEL; 1772 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, &sentinel, wk_list); 1773 for (wk = LIST_NEXT(&sentinel, wk_list); wk != NULL; 1774 wk = LIST_NEXT(&sentinel, wk_list)) { 1775 if (wk->wk_type == D_SENTINEL) { 1776 LIST_REMOVE(&sentinel, wk_list); 1777 LIST_INSERT_AFTER(wk, &sentinel, wk_list); 1778 continue; 1779 } 1780 if (wk->wk_state & INPROGRESS) 1781 panic("process_worklist_item: %p already in progress.", 1782 wk); 1783 wk->wk_state |= INPROGRESS; 1784 remove_from_worklist(wk); 1785 FREE_LOCK(ump); 1786 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1787 panic("process_worklist_item: suspended filesystem"); 1788 switch (wk->wk_type) { 1789 case D_DIRREM: 1790 /* removal of a directory entry */ 1791 error = handle_workitem_remove(WK_DIRREM(wk), flags); 1792 break; 1793 1794 case D_FREEBLKS: 1795 /* releasing blocks and/or fragments from a file */ 1796 error = handle_workitem_freeblocks(WK_FREEBLKS(wk), 1797 flags); 1798 break; 1799 1800 case D_FREEFRAG: 1801 /* releasing a fragment when replaced as a file grows */ 1802 handle_workitem_freefrag(WK_FREEFRAG(wk)); 1803 error = 0; 1804 break; 1805 1806 case D_FREEFILE: 1807 /* releasing an inode when its link count drops to 0 */ 1808 handle_workitem_freefile(WK_FREEFILE(wk)); 1809 error = 0; 1810 break; 1811 1812 default: 1813 panic("%s_process_worklist: Unknown type %s", 1814 "softdep", TYPENAME(wk->wk_type)); 1815 /* NOTREACHED */ 1816 } 1817 vn_finished_secondary_write(mp); 1818 ACQUIRE_LOCK(ump); 1819 if (error == 0) { 1820 if (++matchcnt == target) 1821 break; 1822 continue; 1823 } 1824 /* 1825 * We have to retry the worklist item later. Wake up any 1826 * waiters who may be able to complete it immediately and 1827 * add the item back to the head so we don't try to execute 1828 * it again. 1829 */ 1830 wk->wk_state &= ~INPROGRESS; 1831 wake_worklist(wk); 1832 add_to_worklist(wk, WK_HEAD); 1833 } 1834 LIST_REMOVE(&sentinel, wk_list); 1835 /* Sentinal could've become the tail from remove_from_worklist. */ 1836 if (ump->softdep_worklist_tail == &sentinel) 1837 ump->softdep_worklist_tail = 1838 (struct worklist *)sentinel.wk_list.le_prev; 1839 PRELE(curproc); 1840 return (matchcnt); 1841} 1842 1843/* 1844 * Move dependencies from one buffer to another. 1845 */ 1846int 1847softdep_move_dependencies(oldbp, newbp) 1848 struct buf *oldbp; 1849 struct buf *newbp; 1850{ 1851 struct worklist *wk, *wktail; 1852 struct ufsmount *ump; 1853 int dirty; 1854 1855 if ((wk = LIST_FIRST(&oldbp->b_dep)) == NULL) 1856 return (0); 1857 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 1858 ("softdep_move_dependencies called on non-softdep filesystem")); 1859 dirty = 0; 1860 wktail = NULL; 1861 ump = VFSTOUFS(wk->wk_mp); 1862 ACQUIRE_LOCK(ump); 1863 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) { 1864 LIST_REMOVE(wk, wk_list); 1865 if (wk->wk_type == D_BMSAFEMAP && 1866 bmsafemap_backgroundwrite(WK_BMSAFEMAP(wk), newbp)) 1867 dirty = 1; 1868 if (wktail == 0) 1869 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list); 1870 else 1871 LIST_INSERT_AFTER(wktail, wk, wk_list); 1872 wktail = wk; 1873 } 1874 FREE_LOCK(ump); 1875 1876 return (dirty); 1877} 1878 1879/* 1880 * Purge the work list of all items associated with a particular mount point. 1881 */ 1882int 1883softdep_flushworklist(oldmnt, countp, td) 1884 struct mount *oldmnt; 1885 int *countp; 1886 struct thread *td; 1887{ 1888 struct vnode *devvp; 1889 struct ufsmount *ump; 1890 int count, error; 1891 1892 /* 1893 * Alternately flush the block device associated with the mount 1894 * point and process any dependencies that the flushing 1895 * creates. We continue until no more worklist dependencies 1896 * are found. 1897 */ 1898 *countp = 0; 1899 error = 0; 1900 ump = VFSTOUFS(oldmnt); 1901 devvp = ump->um_devvp; 1902 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) { 1903 *countp += count; 1904 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1905 error = VOP_FSYNC(devvp, MNT_WAIT, td); 1906 VOP_UNLOCK(devvp, 0); 1907 if (error != 0) 1908 break; 1909 } 1910 return (error); 1911} 1912 1913#define SU_WAITIDLE_RETRIES 20 1914static int 1915softdep_waitidle(struct mount *mp, int flags __unused) 1916{ 1917 struct ufsmount *ump; 1918 struct vnode *devvp; 1919 struct thread *td; 1920 int error, i; 1921 1922 ump = VFSTOUFS(mp); 1923 devvp = ump->um_devvp; 1924 td = curthread; 1925 error = 0; 1926 ACQUIRE_LOCK(ump); 1927 for (i = 0; i < SU_WAITIDLE_RETRIES && ump->softdep_deps != 0; i++) { 1928 ump->softdep_req = 1; 1929 KASSERT((flags & FORCECLOSE) == 0 || 1930 ump->softdep_on_worklist == 0, 1931 ("softdep_waitidle: work added after flush")); 1932 msleep(&ump->softdep_deps, LOCK_PTR(ump), PVM | PDROP, 1933 "softdeps", 10 * hz); 1934 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1935 error = VOP_FSYNC(devvp, MNT_WAIT, td); 1936 VOP_UNLOCK(devvp, 0); 1937 if (error != 0) 1938 break; 1939 ACQUIRE_LOCK(ump); 1940 } 1941 ump->softdep_req = 0; 1942 if (i == SU_WAITIDLE_RETRIES && error == 0 && ump->softdep_deps != 0) { 1943 error = EBUSY; 1944 printf("softdep_waitidle: Failed to flush worklist for %p\n", 1945 mp); 1946 } 1947 FREE_LOCK(ump); 1948 return (error); 1949} 1950 1951/* 1952 * Flush all vnodes and worklist items associated with a specified mount point. 1953 */ 1954int 1955softdep_flushfiles(oldmnt, flags, td) 1956 struct mount *oldmnt; 1957 int flags; 1958 struct thread *td; 1959{ 1960#ifdef QUOTA 1961 struct ufsmount *ump; 1962 int i; 1963#endif 1964 int error, early, depcount, loopcnt, retry_flush_count, retry; 1965 int morework; 1966 1967 KASSERT(MOUNTEDSOFTDEP(oldmnt) != 0, 1968 ("softdep_flushfiles called on non-softdep filesystem")); 1969 loopcnt = 10; 1970 retry_flush_count = 3; 1971retry_flush: 1972 error = 0; 1973 1974 /* 1975 * Alternately flush the vnodes associated with the mount 1976 * point and process any dependencies that the flushing 1977 * creates. In theory, this loop can happen at most twice, 1978 * but we give it a few extra just to be sure. 1979 */ 1980 for (; loopcnt > 0; loopcnt--) { 1981 /* 1982 * Do another flush in case any vnodes were brought in 1983 * as part of the cleanup operations. 1984 */ 1985 early = retry_flush_count == 1 || (oldmnt->mnt_kern_flag & 1986 MNTK_UNMOUNT) == 0 ? 0 : EARLYFLUSH; 1987 if ((error = ffs_flushfiles(oldmnt, flags | early, td)) != 0) 1988 break; 1989 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 || 1990 depcount == 0) 1991 break; 1992 } 1993 /* 1994 * If we are unmounting then it is an error to fail. If we 1995 * are simply trying to downgrade to read-only, then filesystem 1996 * activity can keep us busy forever, so we just fail with EBUSY. 1997 */ 1998 if (loopcnt == 0) { 1999 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) 2000 panic("softdep_flushfiles: looping"); 2001 error = EBUSY; 2002 } 2003 if (!error) 2004 error = softdep_waitidle(oldmnt, flags); 2005 if (!error) { 2006 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) { 2007 retry = 0; 2008 MNT_ILOCK(oldmnt); 2009 KASSERT((oldmnt->mnt_kern_flag & MNTK_NOINSMNTQ) != 0, 2010 ("softdep_flushfiles: !MNTK_NOINSMNTQ")); 2011 morework = oldmnt->mnt_nvnodelistsize > 0; 2012#ifdef QUOTA 2013 ump = VFSTOUFS(oldmnt); 2014 UFS_LOCK(ump); 2015 for (i = 0; i < MAXQUOTAS; i++) { 2016 if (ump->um_quotas[i] != NULLVP) 2017 morework = 1; 2018 } 2019 UFS_UNLOCK(ump); 2020#endif 2021 if (morework) { 2022 if (--retry_flush_count > 0) { 2023 retry = 1; 2024 loopcnt = 3; 2025 } else 2026 error = EBUSY; 2027 } 2028 MNT_IUNLOCK(oldmnt); 2029 if (retry) 2030 goto retry_flush; 2031 } 2032 } 2033 return (error); 2034} 2035 2036/* 2037 * Structure hashing. 2038 * 2039 * There are four types of structures that can be looked up: 2040 * 1) pagedep structures identified by mount point, inode number, 2041 * and logical block. 2042 * 2) inodedep structures identified by mount point and inode number. 2043 * 3) newblk structures identified by mount point and 2044 * physical block number. 2045 * 4) bmsafemap structures identified by mount point and 2046 * cylinder group number. 2047 * 2048 * The "pagedep" and "inodedep" dependency structures are hashed 2049 * separately from the file blocks and inodes to which they correspond. 2050 * This separation helps when the in-memory copy of an inode or 2051 * file block must be replaced. It also obviates the need to access 2052 * an inode or file page when simply updating (or de-allocating) 2053 * dependency structures. Lookup of newblk structures is needed to 2054 * find newly allocated blocks when trying to associate them with 2055 * their allocdirect or allocindir structure. 2056 * 2057 * The lookup routines optionally create and hash a new instance when 2058 * an existing entry is not found. The bmsafemap lookup routine always 2059 * allocates a new structure if an existing one is not found. 2060 */ 2061#define DEPALLOC 0x0001 /* allocate structure if lookup fails */ 2062#define NODELAY 0x0002 /* cannot do background work */ 2063 2064/* 2065 * Structures and routines associated with pagedep caching. 2066 */ 2067#define PAGEDEP_HASH(ump, inum, lbn) \ 2068 (&(ump)->pagedep_hashtbl[((inum) + (lbn)) & (ump)->pagedep_hash_size]) 2069 2070static int 2071pagedep_find(pagedephd, ino, lbn, pagedeppp) 2072 struct pagedep_hashhead *pagedephd; 2073 ino_t ino; 2074 ufs_lbn_t lbn; 2075 struct pagedep **pagedeppp; 2076{ 2077 struct pagedep *pagedep; 2078 2079 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 2080 if (ino == pagedep->pd_ino && lbn == pagedep->pd_lbn) { 2081 *pagedeppp = pagedep; 2082 return (1); 2083 } 2084 } 2085 *pagedeppp = NULL; 2086 return (0); 2087} 2088/* 2089 * Look up a pagedep. Return 1 if found, 0 otherwise. 2090 * If not found, allocate if DEPALLOC flag is passed. 2091 * Found or allocated entry is returned in pagedeppp. 2092 * This routine must be called with splbio interrupts blocked. 2093 */ 2094static int 2095pagedep_lookup(mp, bp, ino, lbn, flags, pagedeppp) 2096 struct mount *mp; 2097 struct buf *bp; 2098 ino_t ino; 2099 ufs_lbn_t lbn; 2100 int flags; 2101 struct pagedep **pagedeppp; 2102{ 2103 struct pagedep *pagedep; 2104 struct pagedep_hashhead *pagedephd; 2105 struct worklist *wk; 2106 struct ufsmount *ump; 2107 int ret; 2108 int i; 2109 2110 ump = VFSTOUFS(mp); 2111 LOCK_OWNED(ump); 2112 if (bp) { 2113 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 2114 if (wk->wk_type == D_PAGEDEP) { 2115 *pagedeppp = WK_PAGEDEP(wk); 2116 return (1); 2117 } 2118 } 2119 } 2120 pagedephd = PAGEDEP_HASH(ump, ino, lbn); 2121 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp); 2122 if (ret) { 2123 if (((*pagedeppp)->pd_state & ONWORKLIST) == 0 && bp) 2124 WORKLIST_INSERT(&bp->b_dep, &(*pagedeppp)->pd_list); 2125 return (1); 2126 } 2127 if ((flags & DEPALLOC) == 0) 2128 return (0); 2129 FREE_LOCK(ump); 2130 pagedep = malloc(sizeof(struct pagedep), 2131 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO); 2132 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp); 2133 ACQUIRE_LOCK(ump); 2134 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp); 2135 if (*pagedeppp) { 2136 /* 2137 * This should never happen since we only create pagedeps 2138 * with the vnode lock held. Could be an assert. 2139 */ 2140 WORKITEM_FREE(pagedep, D_PAGEDEP); 2141 return (ret); 2142 } 2143 pagedep->pd_ino = ino; 2144 pagedep->pd_lbn = lbn; 2145 LIST_INIT(&pagedep->pd_dirremhd); 2146 LIST_INIT(&pagedep->pd_pendinghd); 2147 for (i = 0; i < DAHASHSZ; i++) 2148 LIST_INIT(&pagedep->pd_diraddhd[i]); 2149 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash); 2150 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list); 2151 *pagedeppp = pagedep; 2152 return (0); 2153} 2154 2155/* 2156 * Structures and routines associated with inodedep caching. 2157 */ 2158#define INODEDEP_HASH(ump, inum) \ 2159 (&(ump)->inodedep_hashtbl[(inum) & (ump)->inodedep_hash_size]) 2160 2161static int 2162inodedep_find(inodedephd, inum, inodedeppp) 2163 struct inodedep_hashhead *inodedephd; 2164 ino_t inum; 2165 struct inodedep **inodedeppp; 2166{ 2167 struct inodedep *inodedep; 2168 2169 LIST_FOREACH(inodedep, inodedephd, id_hash) 2170 if (inum == inodedep->id_ino) 2171 break; 2172 if (inodedep) { 2173 *inodedeppp = inodedep; 2174 return (1); 2175 } 2176 *inodedeppp = NULL; 2177 2178 return (0); 2179} 2180/* 2181 * Look up an inodedep. Return 1 if found, 0 if not found. 2182 * If not found, allocate if DEPALLOC flag is passed. 2183 * Found or allocated entry is returned in inodedeppp. 2184 * This routine must be called with splbio interrupts blocked. 2185 */ 2186static int 2187inodedep_lookup(mp, inum, flags, inodedeppp) 2188 struct mount *mp; 2189 ino_t inum; 2190 int flags; 2191 struct inodedep **inodedeppp; 2192{ 2193 struct inodedep *inodedep; 2194 struct inodedep_hashhead *inodedephd; 2195 struct ufsmount *ump; 2196 struct fs *fs; 2197 2198 ump = VFSTOUFS(mp); 2199 LOCK_OWNED(ump); 2200 fs = ump->um_fs; 2201 inodedephd = INODEDEP_HASH(ump, inum); 2202 2203 if (inodedep_find(inodedephd, inum, inodedeppp)) 2204 return (1); 2205 if ((flags & DEPALLOC) == 0) 2206 return (0); 2207 /* 2208 * If the system is over its limit and our filesystem is 2209 * responsible for more than our share of that usage and 2210 * we are not in a rush, request some inodedep cleanup. 2211 */ 2212 while (dep_current[D_INODEDEP] > max_softdeps && 2213 (flags & NODELAY) == 0 && 2214 ump->softdep_curdeps[D_INODEDEP] > 2215 max_softdeps / stat_flush_threads) 2216 request_cleanup(mp, FLUSH_INODES); 2217 FREE_LOCK(ump); 2218 inodedep = malloc(sizeof(struct inodedep), 2219 M_INODEDEP, M_SOFTDEP_FLAGS); 2220 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp); 2221 ACQUIRE_LOCK(ump); 2222 if (inodedep_find(inodedephd, inum, inodedeppp)) { 2223 WORKITEM_FREE(inodedep, D_INODEDEP); 2224 return (1); 2225 } 2226 inodedep->id_fs = fs; 2227 inodedep->id_ino = inum; 2228 inodedep->id_state = ALLCOMPLETE; 2229 inodedep->id_nlinkdelta = 0; 2230 inodedep->id_savedino1 = NULL; 2231 inodedep->id_savedsize = -1; 2232 inodedep->id_savedextsize = -1; 2233 inodedep->id_savednlink = -1; 2234 inodedep->id_bmsafemap = NULL; 2235 inodedep->id_mkdiradd = NULL; 2236 LIST_INIT(&inodedep->id_dirremhd); 2237 LIST_INIT(&inodedep->id_pendinghd); 2238 LIST_INIT(&inodedep->id_inowait); 2239 LIST_INIT(&inodedep->id_bufwait); 2240 TAILQ_INIT(&inodedep->id_inoreflst); 2241 TAILQ_INIT(&inodedep->id_inoupdt); 2242 TAILQ_INIT(&inodedep->id_newinoupdt); 2243 TAILQ_INIT(&inodedep->id_extupdt); 2244 TAILQ_INIT(&inodedep->id_newextupdt); 2245 TAILQ_INIT(&inodedep->id_freeblklst); 2246 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash); 2247 *inodedeppp = inodedep; 2248 return (0); 2249} 2250 2251/* 2252 * Structures and routines associated with newblk caching. 2253 */ 2254#define NEWBLK_HASH(ump, inum) \ 2255 (&(ump)->newblk_hashtbl[(inum) & (ump)->newblk_hash_size]) 2256 2257static int 2258newblk_find(newblkhd, newblkno, flags, newblkpp) 2259 struct newblk_hashhead *newblkhd; 2260 ufs2_daddr_t newblkno; 2261 int flags; 2262 struct newblk **newblkpp; 2263{ 2264 struct newblk *newblk; 2265 2266 LIST_FOREACH(newblk, newblkhd, nb_hash) { 2267 if (newblkno != newblk->nb_newblkno) 2268 continue; 2269 /* 2270 * If we're creating a new dependency don't match those that 2271 * have already been converted to allocdirects. This is for 2272 * a frag extend. 2273 */ 2274 if ((flags & DEPALLOC) && newblk->nb_list.wk_type != D_NEWBLK) 2275 continue; 2276 break; 2277 } 2278 if (newblk) { 2279 *newblkpp = newblk; 2280 return (1); 2281 } 2282 *newblkpp = NULL; 2283 return (0); 2284} 2285 2286/* 2287 * Look up a newblk. Return 1 if found, 0 if not found. 2288 * If not found, allocate if DEPALLOC flag is passed. 2289 * Found or allocated entry is returned in newblkpp. 2290 */ 2291static int 2292newblk_lookup(mp, newblkno, flags, newblkpp) 2293 struct mount *mp; 2294 ufs2_daddr_t newblkno; 2295 int flags; 2296 struct newblk **newblkpp; 2297{ 2298 struct newblk *newblk; 2299 struct newblk_hashhead *newblkhd; 2300 struct ufsmount *ump; 2301 2302 ump = VFSTOUFS(mp); 2303 LOCK_OWNED(ump); 2304 newblkhd = NEWBLK_HASH(ump, newblkno); 2305 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) 2306 return (1); 2307 if ((flags & DEPALLOC) == 0) 2308 return (0); 2309 FREE_LOCK(ump); 2310 newblk = malloc(sizeof(union allblk), M_NEWBLK, 2311 M_SOFTDEP_FLAGS | M_ZERO); 2312 workitem_alloc(&newblk->nb_list, D_NEWBLK, mp); 2313 ACQUIRE_LOCK(ump); 2314 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) { 2315 WORKITEM_FREE(newblk, D_NEWBLK); 2316 return (1); 2317 } 2318 newblk->nb_freefrag = NULL; 2319 LIST_INIT(&newblk->nb_indirdeps); 2320 LIST_INIT(&newblk->nb_newdirblk); 2321 LIST_INIT(&newblk->nb_jwork); 2322 newblk->nb_state = ATTACHED; 2323 newblk->nb_newblkno = newblkno; 2324 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash); 2325 *newblkpp = newblk; 2326 return (0); 2327} 2328 2329/* 2330 * Structures and routines associated with freed indirect block caching. 2331 */ 2332#define INDIR_HASH(ump, blkno) \ 2333 (&(ump)->indir_hashtbl[(blkno) & (ump)->indir_hash_size]) 2334 2335/* 2336 * Lookup an indirect block in the indir hash table. The freework is 2337 * removed and potentially freed. The caller must do a blocking journal 2338 * write before writing to the blkno. 2339 */ 2340static int 2341indirblk_lookup(mp, blkno) 2342 struct mount *mp; 2343 ufs2_daddr_t blkno; 2344{ 2345 struct freework *freework; 2346 struct indir_hashhead *wkhd; 2347 struct ufsmount *ump; 2348 2349 ump = VFSTOUFS(mp); 2350 wkhd = INDIR_HASH(ump, blkno); 2351 TAILQ_FOREACH(freework, wkhd, fw_next) { 2352 if (freework->fw_blkno != blkno) 2353 continue; 2354 indirblk_remove(freework); 2355 return (1); 2356 } 2357 return (0); 2358} 2359 2360/* 2361 * Insert an indirect block represented by freework into the indirblk 2362 * hash table so that it may prevent the block from being re-used prior 2363 * to the journal being written. 2364 */ 2365static void 2366indirblk_insert(freework) 2367 struct freework *freework; 2368{ 2369 struct jblocks *jblocks; 2370 struct jseg *jseg; 2371 struct ufsmount *ump; 2372 2373 ump = VFSTOUFS(freework->fw_list.wk_mp); 2374 jblocks = ump->softdep_jblocks; 2375 jseg = TAILQ_LAST(&jblocks->jb_segs, jseglst); 2376 if (jseg == NULL) 2377 return; 2378 2379 LIST_INSERT_HEAD(&jseg->js_indirs, freework, fw_segs); 2380 TAILQ_INSERT_HEAD(INDIR_HASH(ump, freework->fw_blkno), freework, 2381 fw_next); 2382 freework->fw_state &= ~DEPCOMPLETE; 2383} 2384 2385static void 2386indirblk_remove(freework) 2387 struct freework *freework; 2388{ 2389 struct ufsmount *ump; 2390 2391 ump = VFSTOUFS(freework->fw_list.wk_mp); 2392 LIST_REMOVE(freework, fw_segs); 2393 TAILQ_REMOVE(INDIR_HASH(ump, freework->fw_blkno), freework, fw_next); 2394 freework->fw_state |= DEPCOMPLETE; 2395 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 2396 WORKITEM_FREE(freework, D_FREEWORK); 2397} 2398 2399/* 2400 * Executed during filesystem system initialization before 2401 * mounting any filesystems. 2402 */ 2403void 2404softdep_initialize() 2405{ 2406 2407 TAILQ_INIT(&softdepmounts); 2408 max_softdeps = desiredvnodes * 4; 2409 2410 /* initialise bioops hack */ 2411 bioops.io_start = softdep_disk_io_initiation; 2412 bioops.io_complete = softdep_disk_write_complete; 2413 bioops.io_deallocate = softdep_deallocate_dependencies; 2414 bioops.io_countdeps = softdep_count_dependencies; 2415 2416 /* Initialize the callout with an mtx. */ 2417 callout_init_mtx(&softdep_callout, &lk, 0); 2418} 2419 2420/* 2421 * Executed after all filesystems have been unmounted during 2422 * filesystem module unload. 2423 */ 2424void 2425softdep_uninitialize() 2426{ 2427 2428 /* clear bioops hack */ 2429 bioops.io_start = NULL; 2430 bioops.io_complete = NULL; 2431 bioops.io_deallocate = NULL; 2432 bioops.io_countdeps = NULL; 2433 2434 callout_drain(&softdep_callout); 2435} 2436 2437/* 2438 * Called at mount time to notify the dependency code that a 2439 * filesystem wishes to use it. 2440 */ 2441int 2442softdep_mount(devvp, mp, fs, cred) 2443 struct vnode *devvp; 2444 struct mount *mp; 2445 struct fs *fs; 2446 struct ucred *cred; 2447{ 2448 struct csum_total cstotal; 2449 struct mount_softdeps *sdp; 2450 struct ufsmount *ump; 2451 struct cg *cgp; 2452 struct buf *bp; 2453 int i, error, cyl; 2454 2455 sdp = malloc(sizeof(struct mount_softdeps), M_MOUNTDATA, 2456 M_WAITOK | M_ZERO); 2457 MNT_ILOCK(mp); 2458 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP; 2459 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) { 2460 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) | 2461 MNTK_SOFTDEP | MNTK_NOASYNC; 2462 } 2463 ump = VFSTOUFS(mp); 2464 ump->um_softdep = sdp; 2465 MNT_IUNLOCK(mp); 2466 rw_init(LOCK_PTR(ump), "Per-Filesystem Softdep Lock"); 2467 sdp->sd_ump = ump; 2468 LIST_INIT(&ump->softdep_workitem_pending); 2469 LIST_INIT(&ump->softdep_journal_pending); 2470 TAILQ_INIT(&ump->softdep_unlinked); 2471 LIST_INIT(&ump->softdep_dirtycg); 2472 ump->softdep_worklist_tail = NULL; 2473 ump->softdep_on_worklist = 0; 2474 ump->softdep_deps = 0; 2475 LIST_INIT(&ump->softdep_mkdirlisthd); 2476 ump->pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP, 2477 &ump->pagedep_hash_size); 2478 ump->pagedep_nextclean = 0; 2479 ump->inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP, 2480 &ump->inodedep_hash_size); 2481 ump->inodedep_nextclean = 0; 2482 ump->newblk_hashtbl = hashinit(max_softdeps / 2, M_NEWBLK, 2483 &ump->newblk_hash_size); 2484 ump->bmsafemap_hashtbl = hashinit(1024, M_BMSAFEMAP, 2485 &ump->bmsafemap_hash_size); 2486 i = 1 << (ffs(desiredvnodes / 10) - 1); 2487 ump->indir_hashtbl = malloc(i * sizeof(struct indir_hashhead), 2488 M_FREEWORK, M_WAITOK); 2489 ump->indir_hash_size = i - 1; 2490 for (i = 0; i <= ump->indir_hash_size; i++) 2491 TAILQ_INIT(&ump->indir_hashtbl[i]); 2492 ACQUIRE_GBLLOCK(&lk); 2493 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next); 2494 FREE_GBLLOCK(&lk); 2495 if ((fs->fs_flags & FS_SUJ) && 2496 (error = journal_mount(mp, fs, cred)) != 0) { 2497 printf("Failed to start journal: %d\n", error); 2498 softdep_unmount(mp); 2499 return (error); 2500 } 2501 /* 2502 * Start our flushing thread in the bufdaemon process. 2503 */ 2504 ACQUIRE_LOCK(ump); 2505 ump->softdep_flags |= FLUSH_STARTING; 2506 FREE_LOCK(ump); 2507 kproc_kthread_add(&softdep_flush, mp, &bufdaemonproc, 2508 &ump->softdep_flushtd, 0, 0, "softdepflush", "%s worker", 2509 mp->mnt_stat.f_mntonname); 2510 ACQUIRE_LOCK(ump); 2511 while ((ump->softdep_flags & FLUSH_STARTING) != 0) { 2512 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, "sdstart", 2513 hz / 2); 2514 } 2515 FREE_LOCK(ump); 2516 /* 2517 * When doing soft updates, the counters in the 2518 * superblock may have gotten out of sync. Recomputation 2519 * can take a long time and can be deferred for background 2520 * fsck. However, the old behavior of scanning the cylinder 2521 * groups and recalculating them at mount time is available 2522 * by setting vfs.ffs.compute_summary_at_mount to one. 2523 */ 2524 if (compute_summary_at_mount == 0 || fs->fs_clean != 0) 2525 return (0); 2526 bzero(&cstotal, sizeof cstotal); 2527 for (cyl = 0; cyl < fs->fs_ncg; cyl++) { 2528 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)), 2529 fs->fs_cgsize, cred, &bp)) != 0) { 2530 brelse(bp); 2531 softdep_unmount(mp); 2532 return (error); 2533 } 2534 cgp = (struct cg *)bp->b_data; 2535 cstotal.cs_nffree += cgp->cg_cs.cs_nffree; 2536 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree; 2537 cstotal.cs_nifree += cgp->cg_cs.cs_nifree; 2538 cstotal.cs_ndir += cgp->cg_cs.cs_ndir; 2539 fs->fs_cs(fs, cyl) = cgp->cg_cs; 2540 brelse(bp); 2541 } 2542#ifdef DEBUG 2543 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal)) 2544 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt); 2545#endif 2546 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal); 2547 return (0); 2548} 2549 2550void 2551softdep_unmount(mp) 2552 struct mount *mp; 2553{ 2554 struct ufsmount *ump; 2555#ifdef INVARIANTS 2556 int i; 2557#endif 2558 2559 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 2560 ("softdep_unmount called on non-softdep filesystem")); 2561 ump = VFSTOUFS(mp); 2562 MNT_ILOCK(mp); 2563 mp->mnt_flag &= ~MNT_SOFTDEP; 2564 if (MOUNTEDSUJ(mp) == 0) { 2565 MNT_IUNLOCK(mp); 2566 } else { 2567 mp->mnt_flag &= ~MNT_SUJ; 2568 MNT_IUNLOCK(mp); 2569 journal_unmount(ump); 2570 } 2571 /* 2572 * Shut down our flushing thread. Check for NULL is if 2573 * softdep_mount errors out before the thread has been created. 2574 */ 2575 if (ump->softdep_flushtd != NULL) { 2576 ACQUIRE_LOCK(ump); 2577 ump->softdep_flags |= FLUSH_EXIT; 2578 wakeup(&ump->softdep_flushtd); 2579 msleep(&ump->softdep_flags, LOCK_PTR(ump), PVM | PDROP, 2580 "sdwait", 0); 2581 KASSERT((ump->softdep_flags & FLUSH_EXIT) == 0, 2582 ("Thread shutdown failed")); 2583 } 2584 /* 2585 * Free up our resources. 2586 */ 2587 ACQUIRE_GBLLOCK(&lk); 2588 TAILQ_REMOVE(&softdepmounts, ump->um_softdep, sd_next); 2589 FREE_GBLLOCK(&lk); 2590 rw_destroy(LOCK_PTR(ump)); 2591 hashdestroy(ump->pagedep_hashtbl, M_PAGEDEP, ump->pagedep_hash_size); 2592 hashdestroy(ump->inodedep_hashtbl, M_INODEDEP, ump->inodedep_hash_size); 2593 hashdestroy(ump->newblk_hashtbl, M_NEWBLK, ump->newblk_hash_size); 2594 hashdestroy(ump->bmsafemap_hashtbl, M_BMSAFEMAP, 2595 ump->bmsafemap_hash_size); 2596 free(ump->indir_hashtbl, M_FREEWORK); 2597#ifdef INVARIANTS 2598 for (i = 0; i <= D_LAST; i++) 2599 KASSERT(ump->softdep_curdeps[i] == 0, 2600 ("Unmount %s: Dep type %s != 0 (%ld)", ump->um_fs->fs_fsmnt, 2601 TYPENAME(i), ump->softdep_curdeps[i])); 2602#endif 2603 free(ump->um_softdep, M_MOUNTDATA); 2604} 2605 2606static struct jblocks * 2607jblocks_create(void) 2608{ 2609 struct jblocks *jblocks; 2610 2611 jblocks = malloc(sizeof(*jblocks), M_JBLOCKS, M_WAITOK | M_ZERO); 2612 TAILQ_INIT(&jblocks->jb_segs); 2613 jblocks->jb_avail = 10; 2614 jblocks->jb_extent = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2615 M_JBLOCKS, M_WAITOK | M_ZERO); 2616 2617 return (jblocks); 2618} 2619 2620static ufs2_daddr_t 2621jblocks_alloc(jblocks, bytes, actual) 2622 struct jblocks *jblocks; 2623 int bytes; 2624 int *actual; 2625{ 2626 ufs2_daddr_t daddr; 2627 struct jextent *jext; 2628 int freecnt; 2629 int blocks; 2630 2631 blocks = bytes / DEV_BSIZE; 2632 jext = &jblocks->jb_extent[jblocks->jb_head]; 2633 freecnt = jext->je_blocks - jblocks->jb_off; 2634 if (freecnt == 0) { 2635 jblocks->jb_off = 0; 2636 if (++jblocks->jb_head > jblocks->jb_used) 2637 jblocks->jb_head = 0; 2638 jext = &jblocks->jb_extent[jblocks->jb_head]; 2639 freecnt = jext->je_blocks; 2640 } 2641 if (freecnt > blocks) 2642 freecnt = blocks; 2643 *actual = freecnt * DEV_BSIZE; 2644 daddr = jext->je_daddr + jblocks->jb_off; 2645 jblocks->jb_off += freecnt; 2646 jblocks->jb_free -= freecnt; 2647 2648 return (daddr); 2649} 2650 2651static void 2652jblocks_free(jblocks, mp, bytes) 2653 struct jblocks *jblocks; 2654 struct mount *mp; 2655 int bytes; 2656{ 2657 2658 LOCK_OWNED(VFSTOUFS(mp)); 2659 jblocks->jb_free += bytes / DEV_BSIZE; 2660 if (jblocks->jb_suspended) 2661 worklist_speedup(mp); 2662 wakeup(jblocks); 2663} 2664 2665static void 2666jblocks_destroy(jblocks) 2667 struct jblocks *jblocks; 2668{ 2669 2670 if (jblocks->jb_extent) 2671 free(jblocks->jb_extent, M_JBLOCKS); 2672 free(jblocks, M_JBLOCKS); 2673} 2674 2675static void 2676jblocks_add(jblocks, daddr, blocks) 2677 struct jblocks *jblocks; 2678 ufs2_daddr_t daddr; 2679 int blocks; 2680{ 2681 struct jextent *jext; 2682 2683 jblocks->jb_blocks += blocks; 2684 jblocks->jb_free += blocks; 2685 jext = &jblocks->jb_extent[jblocks->jb_used]; 2686 /* Adding the first block. */ 2687 if (jext->je_daddr == 0) { 2688 jext->je_daddr = daddr; 2689 jext->je_blocks = blocks; 2690 return; 2691 } 2692 /* Extending the last extent. */ 2693 if (jext->je_daddr + jext->je_blocks == daddr) { 2694 jext->je_blocks += blocks; 2695 return; 2696 } 2697 /* Adding a new extent. */ 2698 if (++jblocks->jb_used == jblocks->jb_avail) { 2699 jblocks->jb_avail *= 2; 2700 jext = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2701 M_JBLOCKS, M_WAITOK | M_ZERO); 2702 memcpy(jext, jblocks->jb_extent, 2703 sizeof(struct jextent) * jblocks->jb_used); 2704 free(jblocks->jb_extent, M_JBLOCKS); 2705 jblocks->jb_extent = jext; 2706 } 2707 jext = &jblocks->jb_extent[jblocks->jb_used]; 2708 jext->je_daddr = daddr; 2709 jext->je_blocks = blocks; 2710 return; 2711} 2712 2713int 2714softdep_journal_lookup(mp, vpp) 2715 struct mount *mp; 2716 struct vnode **vpp; 2717{ 2718 struct componentname cnp; 2719 struct vnode *dvp; 2720 ino_t sujournal; 2721 int error; 2722 2723 error = VFS_VGET(mp, ROOTINO, LK_EXCLUSIVE, &dvp); 2724 if (error) 2725 return (error); 2726 bzero(&cnp, sizeof(cnp)); 2727 cnp.cn_nameiop = LOOKUP; 2728 cnp.cn_flags = ISLASTCN; 2729 cnp.cn_thread = curthread; 2730 cnp.cn_cred = curthread->td_ucred; 2731 cnp.cn_pnbuf = SUJ_FILE; 2732 cnp.cn_nameptr = SUJ_FILE; 2733 cnp.cn_namelen = strlen(SUJ_FILE); 2734 error = ufs_lookup_ino(dvp, NULL, &cnp, &sujournal); 2735 vput(dvp); 2736 if (error != 0) 2737 return (error); 2738 error = VFS_VGET(mp, sujournal, LK_EXCLUSIVE, vpp); 2739 return (error); 2740} 2741 2742/* 2743 * Open and verify the journal file. 2744 */ 2745static int 2746journal_mount(mp, fs, cred) 2747 struct mount *mp; 2748 struct fs *fs; 2749 struct ucred *cred; 2750{ 2751 struct jblocks *jblocks; 2752 struct ufsmount *ump; 2753 struct vnode *vp; 2754 struct inode *ip; 2755 ufs2_daddr_t blkno; 2756 int bcount; 2757 int error; 2758 int i; 2759 2760 ump = VFSTOUFS(mp); 2761 ump->softdep_journal_tail = NULL; 2762 ump->softdep_on_journal = 0; 2763 ump->softdep_accdeps = 0; 2764 ump->softdep_req = 0; 2765 ump->softdep_jblocks = NULL; 2766 error = softdep_journal_lookup(mp, &vp); 2767 if (error != 0) { 2768 printf("Failed to find journal. Use tunefs to create one\n"); 2769 return (error); 2770 } 2771 ip = VTOI(vp); 2772 if (ip->i_size < SUJ_MIN) { 2773 error = ENOSPC; 2774 goto out; 2775 } 2776 bcount = lblkno(fs, ip->i_size); /* Only use whole blocks. */ 2777 jblocks = jblocks_create(); 2778 for (i = 0; i < bcount; i++) { 2779 error = ufs_bmaparray(vp, i, &blkno, NULL, NULL, NULL); 2780 if (error) 2781 break; 2782 jblocks_add(jblocks, blkno, fsbtodb(fs, fs->fs_frag)); 2783 } 2784 if (error) { 2785 jblocks_destroy(jblocks); 2786 goto out; 2787 } 2788 jblocks->jb_low = jblocks->jb_free / 3; /* Reserve 33%. */ 2789 jblocks->jb_min = jblocks->jb_free / 10; /* Suspend at 10%. */ 2790 ump->softdep_jblocks = jblocks; 2791out: 2792 if (error == 0) { 2793 MNT_ILOCK(mp); 2794 mp->mnt_flag |= MNT_SUJ; 2795 mp->mnt_flag &= ~MNT_SOFTDEP; 2796 MNT_IUNLOCK(mp); 2797 /* 2798 * Only validate the journal contents if the 2799 * filesystem is clean, otherwise we write the logs 2800 * but they'll never be used. If the filesystem was 2801 * still dirty when we mounted it the journal is 2802 * invalid and a new journal can only be valid if it 2803 * starts from a clean mount. 2804 */ 2805 if (fs->fs_clean) { 2806 DIP_SET(ip, i_modrev, fs->fs_mtime); 2807 ip->i_flags |= IN_MODIFIED; 2808 ffs_update(vp, 1); 2809 } 2810 } 2811 vput(vp); 2812 return (error); 2813} 2814 2815static void 2816journal_unmount(ump) 2817 struct ufsmount *ump; 2818{ 2819 2820 if (ump->softdep_jblocks) 2821 jblocks_destroy(ump->softdep_jblocks); 2822 ump->softdep_jblocks = NULL; 2823} 2824 2825/* 2826 * Called when a journal record is ready to be written. Space is allocated 2827 * and the journal entry is created when the journal is flushed to stable 2828 * store. 2829 */ 2830static void 2831add_to_journal(wk) 2832 struct worklist *wk; 2833{ 2834 struct ufsmount *ump; 2835 2836 ump = VFSTOUFS(wk->wk_mp); 2837 LOCK_OWNED(ump); 2838 if (wk->wk_state & ONWORKLIST) 2839 panic("add_to_journal: %s(0x%X) already on list", 2840 TYPENAME(wk->wk_type), wk->wk_state); 2841 wk->wk_state |= ONWORKLIST | DEPCOMPLETE; 2842 if (LIST_EMPTY(&ump->softdep_journal_pending)) { 2843 ump->softdep_jblocks->jb_age = ticks; 2844 LIST_INSERT_HEAD(&ump->softdep_journal_pending, wk, wk_list); 2845 } else 2846 LIST_INSERT_AFTER(ump->softdep_journal_tail, wk, wk_list); 2847 ump->softdep_journal_tail = wk; 2848 ump->softdep_on_journal += 1; 2849} 2850 2851/* 2852 * Remove an arbitrary item for the journal worklist maintain the tail 2853 * pointer. This happens when a new operation obviates the need to 2854 * journal an old operation. 2855 */ 2856static void 2857remove_from_journal(wk) 2858 struct worklist *wk; 2859{ 2860 struct ufsmount *ump; 2861 2862 ump = VFSTOUFS(wk->wk_mp); 2863 LOCK_OWNED(ump); 2864#ifdef SUJ_DEBUG 2865 { 2866 struct worklist *wkn; 2867 2868 LIST_FOREACH(wkn, &ump->softdep_journal_pending, wk_list) 2869 if (wkn == wk) 2870 break; 2871 if (wkn == NULL) 2872 panic("remove_from_journal: %p is not in journal", wk); 2873 } 2874#endif 2875 /* 2876 * We emulate a TAILQ to save space in most structures which do not 2877 * require TAILQ semantics. Here we must update the tail position 2878 * when removing the tail which is not the final entry. This works 2879 * only if the worklist linkage are at the beginning of the structure. 2880 */ 2881 if (ump->softdep_journal_tail == wk) 2882 ump->softdep_journal_tail = 2883 (struct worklist *)wk->wk_list.le_prev; 2884 2885 WORKLIST_REMOVE(wk); 2886 ump->softdep_on_journal -= 1; 2887} 2888 2889/* 2890 * Check for journal space as well as dependency limits so the prelink 2891 * code can throttle both journaled and non-journaled filesystems. 2892 * Threshold is 0 for low and 1 for min. 2893 */ 2894static int 2895journal_space(ump, thresh) 2896 struct ufsmount *ump; 2897 int thresh; 2898{ 2899 struct jblocks *jblocks; 2900 int limit, avail; 2901 2902 jblocks = ump->softdep_jblocks; 2903 if (jblocks == NULL) 2904 return (1); 2905 /* 2906 * We use a tighter restriction here to prevent request_cleanup() 2907 * running in threads from running into locks we currently hold. 2908 * We have to be over the limit and our filesystem has to be 2909 * responsible for more than our share of that usage. 2910 */ 2911 limit = (max_softdeps / 10) * 9; 2912 if (dep_current[D_INODEDEP] > limit && 2913 ump->softdep_curdeps[D_INODEDEP] > limit / stat_flush_threads) 2914 return (0); 2915 if (thresh) 2916 thresh = jblocks->jb_min; 2917 else 2918 thresh = jblocks->jb_low; 2919 avail = (ump->softdep_on_journal * JREC_SIZE) / DEV_BSIZE; 2920 avail = jblocks->jb_free - avail; 2921 2922 return (avail > thresh); 2923} 2924 2925static void 2926journal_suspend(ump) 2927 struct ufsmount *ump; 2928{ 2929 struct jblocks *jblocks; 2930 struct mount *mp; 2931 2932 mp = UFSTOVFS(ump); 2933 jblocks = ump->softdep_jblocks; 2934 MNT_ILOCK(mp); 2935 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) { 2936 stat_journal_min++; 2937 mp->mnt_kern_flag |= MNTK_SUSPEND; 2938 mp->mnt_susp_owner = ump->softdep_flushtd; 2939 } 2940 jblocks->jb_suspended = 1; 2941 MNT_IUNLOCK(mp); 2942} 2943 2944static int 2945journal_unsuspend(struct ufsmount *ump) 2946{ 2947 struct jblocks *jblocks; 2948 struct mount *mp; 2949 2950 mp = UFSTOVFS(ump); 2951 jblocks = ump->softdep_jblocks; 2952 2953 if (jblocks != NULL && jblocks->jb_suspended && 2954 journal_space(ump, jblocks->jb_min)) { 2955 jblocks->jb_suspended = 0; 2956 FREE_LOCK(ump); 2957 mp->mnt_susp_owner = curthread; 2958 vfs_write_resume(mp, 0); 2959 ACQUIRE_LOCK(ump); 2960 return (1); 2961 } 2962 return (0); 2963} 2964 2965/* 2966 * Called before any allocation function to be certain that there is 2967 * sufficient space in the journal prior to creating any new records. 2968 * Since in the case of block allocation we may have multiple locked 2969 * buffers at the time of the actual allocation we can not block 2970 * when the journal records are created. Doing so would create a deadlock 2971 * if any of these buffers needed to be flushed to reclaim space. Instead 2972 * we require a sufficiently large amount of available space such that 2973 * each thread in the system could have passed this allocation check and 2974 * still have sufficient free space. With 20% of a minimum journal size 2975 * of 1MB we have 6553 records available. 2976 */ 2977int 2978softdep_prealloc(vp, waitok) 2979 struct vnode *vp; 2980 int waitok; 2981{ 2982 struct ufsmount *ump; 2983 2984 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 2985 ("softdep_prealloc called on non-softdep filesystem")); 2986 /* 2987 * Nothing to do if we are not running journaled soft updates. 2988 * If we currently hold the snapshot lock, we must avoid handling 2989 * other resources that could cause deadlock. 2990 */ 2991 if (DOINGSUJ(vp) == 0 || IS_SNAPSHOT(VTOI(vp))) 2992 return (0); 2993 ump = VFSTOUFS(vp->v_mount); 2994 ACQUIRE_LOCK(ump); 2995 if (journal_space(ump, 0)) { 2996 FREE_LOCK(ump); 2997 return (0); 2998 } 2999 stat_journal_low++; 3000 FREE_LOCK(ump); 3001 if (waitok == MNT_NOWAIT) 3002 return (ENOSPC); 3003 /* 3004 * Attempt to sync this vnode once to flush any journal 3005 * work attached to it. 3006 */ 3007 if ((curthread->td_pflags & TDP_COWINPROGRESS) == 0) 3008 ffs_syncvnode(vp, waitok, 0); 3009 ACQUIRE_LOCK(ump); 3010 process_removes(vp); 3011 process_truncates(vp); 3012 if (journal_space(ump, 0) == 0) { 3013 softdep_speedup(ump); 3014 if (journal_space(ump, 1) == 0) 3015 journal_suspend(ump); 3016 } 3017 FREE_LOCK(ump); 3018 3019 return (0); 3020} 3021 3022/* 3023 * Before adjusting a link count on a vnode verify that we have sufficient 3024 * journal space. If not, process operations that depend on the currently 3025 * locked pair of vnodes to try to flush space as the syncer, buf daemon, 3026 * and softdep flush threads can not acquire these locks to reclaim space. 3027 */ 3028static void 3029softdep_prelink(dvp, vp) 3030 struct vnode *dvp; 3031 struct vnode *vp; 3032{ 3033 struct ufsmount *ump; 3034 3035 ump = VFSTOUFS(dvp->v_mount); 3036 LOCK_OWNED(ump); 3037 /* 3038 * Nothing to do if we have sufficient journal space. 3039 * If we currently hold the snapshot lock, we must avoid 3040 * handling other resources that could cause deadlock. 3041 */ 3042 if (journal_space(ump, 0) || (vp && IS_SNAPSHOT(VTOI(vp)))) 3043 return; 3044 stat_journal_low++; 3045 FREE_LOCK(ump); 3046 if (vp) 3047 ffs_syncvnode(vp, MNT_NOWAIT, 0); 3048 ffs_syncvnode(dvp, MNT_WAIT, 0); 3049 ACQUIRE_LOCK(ump); 3050 /* Process vp before dvp as it may create .. removes. */ 3051 if (vp) { 3052 process_removes(vp); 3053 process_truncates(vp); 3054 } 3055 process_removes(dvp); 3056 process_truncates(dvp); 3057 softdep_speedup(ump); 3058 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT); 3059 if (journal_space(ump, 0) == 0) { 3060 softdep_speedup(ump); 3061 if (journal_space(ump, 1) == 0) 3062 journal_suspend(ump); 3063 } 3064} 3065 3066static void 3067jseg_write(ump, jseg, data) 3068 struct ufsmount *ump; 3069 struct jseg *jseg; 3070 uint8_t *data; 3071{ 3072 struct jsegrec *rec; 3073 3074 rec = (struct jsegrec *)data; 3075 rec->jsr_seq = jseg->js_seq; 3076 rec->jsr_oldest = jseg->js_oldseq; 3077 rec->jsr_cnt = jseg->js_cnt; 3078 rec->jsr_blocks = jseg->js_size / ump->um_devvp->v_bufobj.bo_bsize; 3079 rec->jsr_crc = 0; 3080 rec->jsr_time = ump->um_fs->fs_mtime; 3081} 3082 3083static inline void 3084inoref_write(inoref, jseg, rec) 3085 struct inoref *inoref; 3086 struct jseg *jseg; 3087 struct jrefrec *rec; 3088{ 3089 3090 inoref->if_jsegdep->jd_seg = jseg; 3091 rec->jr_ino = inoref->if_ino; 3092 rec->jr_parent = inoref->if_parent; 3093 rec->jr_nlink = inoref->if_nlink; 3094 rec->jr_mode = inoref->if_mode; 3095 rec->jr_diroff = inoref->if_diroff; 3096} 3097 3098static void 3099jaddref_write(jaddref, jseg, data) 3100 struct jaddref *jaddref; 3101 struct jseg *jseg; 3102 uint8_t *data; 3103{ 3104 struct jrefrec *rec; 3105 3106 rec = (struct jrefrec *)data; 3107 rec->jr_op = JOP_ADDREF; 3108 inoref_write(&jaddref->ja_ref, jseg, rec); 3109} 3110 3111static void 3112jremref_write(jremref, jseg, data) 3113 struct jremref *jremref; 3114 struct jseg *jseg; 3115 uint8_t *data; 3116{ 3117 struct jrefrec *rec; 3118 3119 rec = (struct jrefrec *)data; 3120 rec->jr_op = JOP_REMREF; 3121 inoref_write(&jremref->jr_ref, jseg, rec); 3122} 3123 3124static void 3125jmvref_write(jmvref, jseg, data) 3126 struct jmvref *jmvref; 3127 struct jseg *jseg; 3128 uint8_t *data; 3129{ 3130 struct jmvrec *rec; 3131 3132 rec = (struct jmvrec *)data; 3133 rec->jm_op = JOP_MVREF; 3134 rec->jm_ino = jmvref->jm_ino; 3135 rec->jm_parent = jmvref->jm_parent; 3136 rec->jm_oldoff = jmvref->jm_oldoff; 3137 rec->jm_newoff = jmvref->jm_newoff; 3138} 3139 3140static void 3141jnewblk_write(jnewblk, jseg, data) 3142 struct jnewblk *jnewblk; 3143 struct jseg *jseg; 3144 uint8_t *data; 3145{ 3146 struct jblkrec *rec; 3147 3148 jnewblk->jn_jsegdep->jd_seg = jseg; 3149 rec = (struct jblkrec *)data; 3150 rec->jb_op = JOP_NEWBLK; 3151 rec->jb_ino = jnewblk->jn_ino; 3152 rec->jb_blkno = jnewblk->jn_blkno; 3153 rec->jb_lbn = jnewblk->jn_lbn; 3154 rec->jb_frags = jnewblk->jn_frags; 3155 rec->jb_oldfrags = jnewblk->jn_oldfrags; 3156} 3157 3158static void 3159jfreeblk_write(jfreeblk, jseg, data) 3160 struct jfreeblk *jfreeblk; 3161 struct jseg *jseg; 3162 uint8_t *data; 3163{ 3164 struct jblkrec *rec; 3165 3166 jfreeblk->jf_dep.jb_jsegdep->jd_seg = jseg; 3167 rec = (struct jblkrec *)data; 3168 rec->jb_op = JOP_FREEBLK; 3169 rec->jb_ino = jfreeblk->jf_ino; 3170 rec->jb_blkno = jfreeblk->jf_blkno; 3171 rec->jb_lbn = jfreeblk->jf_lbn; 3172 rec->jb_frags = jfreeblk->jf_frags; 3173 rec->jb_oldfrags = 0; 3174} 3175 3176static void 3177jfreefrag_write(jfreefrag, jseg, data) 3178 struct jfreefrag *jfreefrag; 3179 struct jseg *jseg; 3180 uint8_t *data; 3181{ 3182 struct jblkrec *rec; 3183 3184 jfreefrag->fr_jsegdep->jd_seg = jseg; 3185 rec = (struct jblkrec *)data; 3186 rec->jb_op = JOP_FREEBLK; 3187 rec->jb_ino = jfreefrag->fr_ino; 3188 rec->jb_blkno = jfreefrag->fr_blkno; 3189 rec->jb_lbn = jfreefrag->fr_lbn; 3190 rec->jb_frags = jfreefrag->fr_frags; 3191 rec->jb_oldfrags = 0; 3192} 3193 3194static void 3195jtrunc_write(jtrunc, jseg, data) 3196 struct jtrunc *jtrunc; 3197 struct jseg *jseg; 3198 uint8_t *data; 3199{ 3200 struct jtrncrec *rec; 3201 3202 jtrunc->jt_dep.jb_jsegdep->jd_seg = jseg; 3203 rec = (struct jtrncrec *)data; 3204 rec->jt_op = JOP_TRUNC; 3205 rec->jt_ino = jtrunc->jt_ino; 3206 rec->jt_size = jtrunc->jt_size; 3207 rec->jt_extsize = jtrunc->jt_extsize; 3208} 3209 3210static void 3211jfsync_write(jfsync, jseg, data) 3212 struct jfsync *jfsync; 3213 struct jseg *jseg; 3214 uint8_t *data; 3215{ 3216 struct jtrncrec *rec; 3217 3218 rec = (struct jtrncrec *)data; 3219 rec->jt_op = JOP_SYNC; 3220 rec->jt_ino = jfsync->jfs_ino; 3221 rec->jt_size = jfsync->jfs_size; 3222 rec->jt_extsize = jfsync->jfs_extsize; 3223} 3224 3225static void 3226softdep_flushjournal(mp) 3227 struct mount *mp; 3228{ 3229 struct jblocks *jblocks; 3230 struct ufsmount *ump; 3231 3232 if (MOUNTEDSUJ(mp) == 0) 3233 return; 3234 ump = VFSTOUFS(mp); 3235 jblocks = ump->softdep_jblocks; 3236 ACQUIRE_LOCK(ump); 3237 while (ump->softdep_on_journal) { 3238 jblocks->jb_needseg = 1; 3239 softdep_process_journal(mp, NULL, MNT_WAIT); 3240 } 3241 FREE_LOCK(ump); 3242} 3243 3244static void softdep_synchronize_completed(struct bio *); 3245static void softdep_synchronize(struct bio *, struct ufsmount *, void *); 3246 3247static void 3248softdep_synchronize_completed(bp) 3249 struct bio *bp; 3250{ 3251 struct jseg *oldest; 3252 struct jseg *jseg; 3253 struct ufsmount *ump; 3254 3255 /* 3256 * caller1 marks the last segment written before we issued the 3257 * synchronize cache. 3258 */ 3259 jseg = bp->bio_caller1; 3260 if (jseg == NULL) { 3261 g_destroy_bio(bp); 3262 return; 3263 } 3264 ump = VFSTOUFS(jseg->js_list.wk_mp); 3265 ACQUIRE_LOCK(ump); 3266 oldest = NULL; 3267 /* 3268 * Mark all the journal entries waiting on the synchronize cache 3269 * as completed so they may continue on. 3270 */ 3271 while (jseg != NULL && (jseg->js_state & COMPLETE) == 0) { 3272 jseg->js_state |= COMPLETE; 3273 oldest = jseg; 3274 jseg = TAILQ_PREV(jseg, jseglst, js_next); 3275 } 3276 /* 3277 * Restart deferred journal entry processing from the oldest 3278 * completed jseg. 3279 */ 3280 if (oldest) 3281 complete_jsegs(oldest); 3282 3283 FREE_LOCK(ump); 3284 g_destroy_bio(bp); 3285} 3286 3287/* 3288 * Send BIO_FLUSH/SYNCHRONIZE CACHE to the device to enforce write ordering 3289 * barriers. The journal must be written prior to any blocks that depend 3290 * on it and the journal can not be released until the blocks have be 3291 * written. This code handles both barriers simultaneously. 3292 */ 3293static void 3294softdep_synchronize(bp, ump, caller1) 3295 struct bio *bp; 3296 struct ufsmount *ump; 3297 void *caller1; 3298{ 3299 3300 bp->bio_cmd = BIO_FLUSH; 3301 bp->bio_flags |= BIO_ORDERED; 3302 bp->bio_data = NULL; 3303 bp->bio_offset = ump->um_cp->provider->mediasize; 3304 bp->bio_length = 0; 3305 bp->bio_done = softdep_synchronize_completed; 3306 bp->bio_caller1 = caller1; 3307 g_io_request(bp, 3308 (struct g_consumer *)ump->um_devvp->v_bufobj.bo_private); 3309} 3310 3311/* 3312 * Flush some journal records to disk. 3313 */ 3314static void 3315softdep_process_journal(mp, needwk, flags) 3316 struct mount *mp; 3317 struct worklist *needwk; 3318 int flags; 3319{ 3320 struct jblocks *jblocks; 3321 struct ufsmount *ump; 3322 struct worklist *wk; 3323 struct jseg *jseg; 3324 struct buf *bp; 3325 struct bio *bio; 3326 uint8_t *data; 3327 struct fs *fs; 3328 int shouldflush; 3329 int segwritten; 3330 int jrecmin; /* Minimum records per block. */ 3331 int jrecmax; /* Maximum records per block. */ 3332 int size; 3333 int cnt; 3334 int off; 3335 int devbsize; 3336 3337 if (MOUNTEDSUJ(mp) == 0) 3338 return; 3339 shouldflush = softdep_flushcache; 3340 bio = NULL; 3341 jseg = NULL; 3342 ump = VFSTOUFS(mp); 3343 LOCK_OWNED(ump); 3344 fs = ump->um_fs; 3345 jblocks = ump->softdep_jblocks; 3346 devbsize = ump->um_devvp->v_bufobj.bo_bsize; 3347 /* 3348 * We write anywhere between a disk block and fs block. The upper 3349 * bound is picked to prevent buffer cache fragmentation and limit 3350 * processing time per I/O. 3351 */ 3352 jrecmin = (devbsize / JREC_SIZE) - 1; /* -1 for seg header */ 3353 jrecmax = (fs->fs_bsize / devbsize) * jrecmin; 3354 segwritten = 0; 3355 for (;;) { 3356 cnt = ump->softdep_on_journal; 3357 /* 3358 * Criteria for writing a segment: 3359 * 1) We have a full block. 3360 * 2) We're called from jwait() and haven't found the 3361 * journal item yet. 3362 * 3) Always write if needseg is set. 3363 * 4) If we are called from process_worklist and have 3364 * not yet written anything we write a partial block 3365 * to enforce a 1 second maximum latency on journal 3366 * entries. 3367 */ 3368 if (cnt < (jrecmax - 1) && needwk == NULL && 3369 jblocks->jb_needseg == 0 && (segwritten || cnt == 0)) 3370 break; 3371 cnt++; 3372 /* 3373 * Verify some free journal space. softdep_prealloc() should 3374 * guarantee that we don't run out so this is indicative of 3375 * a problem with the flow control. Try to recover 3376 * gracefully in any event. 3377 */ 3378 while (jblocks->jb_free == 0) { 3379 if (flags != MNT_WAIT) 3380 break; 3381 printf("softdep: Out of journal space!\n"); 3382 softdep_speedup(ump); 3383 msleep(jblocks, LOCK_PTR(ump), PRIBIO, "jblocks", hz); 3384 } 3385 FREE_LOCK(ump); 3386 jseg = malloc(sizeof(*jseg), M_JSEG, M_SOFTDEP_FLAGS); 3387 workitem_alloc(&jseg->js_list, D_JSEG, mp); 3388 LIST_INIT(&jseg->js_entries); 3389 LIST_INIT(&jseg->js_indirs); 3390 jseg->js_state = ATTACHED; 3391 if (shouldflush == 0) 3392 jseg->js_state |= COMPLETE; 3393 else if (bio == NULL) 3394 bio = g_alloc_bio(); 3395 jseg->js_jblocks = jblocks; 3396 bp = geteblk(fs->fs_bsize, 0); 3397 ACQUIRE_LOCK(ump); 3398 /* 3399 * If there was a race while we were allocating the block 3400 * and jseg the entry we care about was likely written. 3401 * We bail out in both the WAIT and NOWAIT case and assume 3402 * the caller will loop if the entry it cares about is 3403 * not written. 3404 */ 3405 cnt = ump->softdep_on_journal; 3406 if (cnt + jblocks->jb_needseg == 0 || jblocks->jb_free == 0) { 3407 bp->b_flags |= B_INVAL | B_NOCACHE; 3408 WORKITEM_FREE(jseg, D_JSEG); 3409 FREE_LOCK(ump); 3410 brelse(bp); 3411 ACQUIRE_LOCK(ump); 3412 break; 3413 } 3414 /* 3415 * Calculate the disk block size required for the available 3416 * records rounded to the min size. 3417 */ 3418 if (cnt == 0) 3419 size = devbsize; 3420 else if (cnt < jrecmax) 3421 size = howmany(cnt, jrecmin) * devbsize; 3422 else 3423 size = fs->fs_bsize; 3424 /* 3425 * Allocate a disk block for this journal data and account 3426 * for truncation of the requested size if enough contiguous 3427 * space was not available. 3428 */ 3429 bp->b_blkno = jblocks_alloc(jblocks, size, &size); 3430 bp->b_lblkno = bp->b_blkno; 3431 bp->b_offset = bp->b_blkno * DEV_BSIZE; 3432 bp->b_bcount = size; 3433 bp->b_flags &= ~B_INVAL; 3434 bp->b_flags |= B_VALIDSUSPWRT | B_NOCOPY; 3435 /* 3436 * Initialize our jseg with cnt records. Assign the next 3437 * sequence number to it and link it in-order. 3438 */ 3439 cnt = MIN(cnt, (size / devbsize) * jrecmin); 3440 jseg->js_buf = bp; 3441 jseg->js_cnt = cnt; 3442 jseg->js_refs = cnt + 1; /* Self ref. */ 3443 jseg->js_size = size; 3444 jseg->js_seq = jblocks->jb_nextseq++; 3445 if (jblocks->jb_oldestseg == NULL) 3446 jblocks->jb_oldestseg = jseg; 3447 jseg->js_oldseq = jblocks->jb_oldestseg->js_seq; 3448 TAILQ_INSERT_TAIL(&jblocks->jb_segs, jseg, js_next); 3449 if (jblocks->jb_writeseg == NULL) 3450 jblocks->jb_writeseg = jseg; 3451 /* 3452 * Start filling in records from the pending list. 3453 */ 3454 data = bp->b_data; 3455 off = 0; 3456 3457 /* 3458 * Always put a header on the first block. 3459 * XXX As with below, there might not be a chance to get 3460 * into the loop. Ensure that something valid is written. 3461 */ 3462 jseg_write(ump, jseg, data); 3463 off += JREC_SIZE; 3464 data = bp->b_data + off; 3465 3466 /* 3467 * XXX Something is wrong here. There's no work to do, 3468 * but we need to perform and I/O and allow it to complete 3469 * anyways. 3470 */ 3471 if (LIST_EMPTY(&ump->softdep_journal_pending)) 3472 stat_emptyjblocks++; 3473 3474 while ((wk = LIST_FIRST(&ump->softdep_journal_pending)) 3475 != NULL) { 3476 if (cnt == 0) 3477 break; 3478 /* Place a segment header on every device block. */ 3479 if ((off % devbsize) == 0) { 3480 jseg_write(ump, jseg, data); 3481 off += JREC_SIZE; 3482 data = bp->b_data + off; 3483 } 3484 if (wk == needwk) 3485 needwk = NULL; 3486 remove_from_journal(wk); 3487 wk->wk_state |= INPROGRESS; 3488 WORKLIST_INSERT(&jseg->js_entries, wk); 3489 switch (wk->wk_type) { 3490 case D_JADDREF: 3491 jaddref_write(WK_JADDREF(wk), jseg, data); 3492 break; 3493 case D_JREMREF: 3494 jremref_write(WK_JREMREF(wk), jseg, data); 3495 break; 3496 case D_JMVREF: 3497 jmvref_write(WK_JMVREF(wk), jseg, data); 3498 break; 3499 case D_JNEWBLK: 3500 jnewblk_write(WK_JNEWBLK(wk), jseg, data); 3501 break; 3502 case D_JFREEBLK: 3503 jfreeblk_write(WK_JFREEBLK(wk), jseg, data); 3504 break; 3505 case D_JFREEFRAG: 3506 jfreefrag_write(WK_JFREEFRAG(wk), jseg, data); 3507 break; 3508 case D_JTRUNC: 3509 jtrunc_write(WK_JTRUNC(wk), jseg, data); 3510 break; 3511 case D_JFSYNC: 3512 jfsync_write(WK_JFSYNC(wk), jseg, data); 3513 break; 3514 default: 3515 panic("process_journal: Unknown type %s", 3516 TYPENAME(wk->wk_type)); 3517 /* NOTREACHED */ 3518 } 3519 off += JREC_SIZE; 3520 data = bp->b_data + off; 3521 cnt--; 3522 } 3523 3524 /* Clear any remaining space so we don't leak kernel data */ 3525 if (size > off) 3526 bzero(data, size - off); 3527 3528 /* 3529 * Write this one buffer and continue. 3530 */ 3531 segwritten = 1; 3532 jblocks->jb_needseg = 0; 3533 WORKLIST_INSERT(&bp->b_dep, &jseg->js_list); 3534 FREE_LOCK(ump); 3535 pbgetvp(ump->um_devvp, bp); 3536 /* 3537 * We only do the blocking wait once we find the journal 3538 * entry we're looking for. 3539 */ 3540 if (needwk == NULL && flags == MNT_WAIT) 3541 bwrite(bp); 3542 else 3543 bawrite(bp); 3544 ACQUIRE_LOCK(ump); 3545 } 3546 /* 3547 * If we wrote a segment issue a synchronize cache so the journal 3548 * is reflected on disk before the data is written. Since reclaiming 3549 * journal space also requires writing a journal record this 3550 * process also enforces a barrier before reclamation. 3551 */ 3552 if (segwritten && shouldflush) { 3553 softdep_synchronize(bio, ump, 3554 TAILQ_LAST(&jblocks->jb_segs, jseglst)); 3555 } else if (bio) 3556 g_destroy_bio(bio); 3557 /* 3558 * If we've suspended the filesystem because we ran out of journal 3559 * space either try to sync it here to make some progress or 3560 * unsuspend it if we already have. 3561 */ 3562 if (flags == 0 && jblocks->jb_suspended) { 3563 if (journal_unsuspend(ump)) 3564 return; 3565 FREE_LOCK(ump); 3566 VFS_SYNC(mp, MNT_NOWAIT); 3567 ffs_sbupdate(ump, MNT_WAIT, 0); 3568 ACQUIRE_LOCK(ump); 3569 } 3570} 3571 3572/* 3573 * Complete a jseg, allowing all dependencies awaiting journal writes 3574 * to proceed. Each journal dependency also attaches a jsegdep to dependent 3575 * structures so that the journal segment can be freed to reclaim space. 3576 */ 3577static void 3578complete_jseg(jseg) 3579 struct jseg *jseg; 3580{ 3581 struct worklist *wk; 3582 struct jmvref *jmvref; 3583 int waiting; 3584#ifdef INVARIANTS 3585 int i = 0; 3586#endif 3587 3588 while ((wk = LIST_FIRST(&jseg->js_entries)) != NULL) { 3589 WORKLIST_REMOVE(wk); 3590 waiting = wk->wk_state & IOWAITING; 3591 wk->wk_state &= ~(INPROGRESS | IOWAITING); 3592 wk->wk_state |= COMPLETE; 3593 KASSERT(i++ < jseg->js_cnt, 3594 ("handle_written_jseg: overflow %d >= %d", 3595 i - 1, jseg->js_cnt)); 3596 switch (wk->wk_type) { 3597 case D_JADDREF: 3598 handle_written_jaddref(WK_JADDREF(wk)); 3599 break; 3600 case D_JREMREF: 3601 handle_written_jremref(WK_JREMREF(wk)); 3602 break; 3603 case D_JMVREF: 3604 rele_jseg(jseg); /* No jsegdep. */ 3605 jmvref = WK_JMVREF(wk); 3606 LIST_REMOVE(jmvref, jm_deps); 3607 if ((jmvref->jm_pagedep->pd_state & ONWORKLIST) == 0) 3608 free_pagedep(jmvref->jm_pagedep); 3609 WORKITEM_FREE(jmvref, D_JMVREF); 3610 break; 3611 case D_JNEWBLK: 3612 handle_written_jnewblk(WK_JNEWBLK(wk)); 3613 break; 3614 case D_JFREEBLK: 3615 handle_written_jblkdep(&WK_JFREEBLK(wk)->jf_dep); 3616 break; 3617 case D_JTRUNC: 3618 handle_written_jblkdep(&WK_JTRUNC(wk)->jt_dep); 3619 break; 3620 case D_JFSYNC: 3621 rele_jseg(jseg); /* No jsegdep. */ 3622 WORKITEM_FREE(wk, D_JFSYNC); 3623 break; 3624 case D_JFREEFRAG: 3625 handle_written_jfreefrag(WK_JFREEFRAG(wk)); 3626 break; 3627 default: 3628 panic("handle_written_jseg: Unknown type %s", 3629 TYPENAME(wk->wk_type)); 3630 /* NOTREACHED */ 3631 } 3632 if (waiting) 3633 wakeup(wk); 3634 } 3635 /* Release the self reference so the structure may be freed. */ 3636 rele_jseg(jseg); 3637} 3638 3639/* 3640 * Determine which jsegs are ready for completion processing. Waits for 3641 * synchronize cache to complete as well as forcing in-order completion 3642 * of journal entries. 3643 */ 3644static void 3645complete_jsegs(jseg) 3646 struct jseg *jseg; 3647{ 3648 struct jblocks *jblocks; 3649 struct jseg *jsegn; 3650 3651 jblocks = jseg->js_jblocks; 3652 /* 3653 * Don't allow out of order completions. If this isn't the first 3654 * block wait for it to write before we're done. 3655 */ 3656 if (jseg != jblocks->jb_writeseg) 3657 return; 3658 /* Iterate through available jsegs processing their entries. */ 3659 while (jseg && (jseg->js_state & ALLCOMPLETE) == ALLCOMPLETE) { 3660 jblocks->jb_oldestwrseq = jseg->js_oldseq; 3661 jsegn = TAILQ_NEXT(jseg, js_next); 3662 complete_jseg(jseg); 3663 jseg = jsegn; 3664 } 3665 jblocks->jb_writeseg = jseg; 3666 /* 3667 * Attempt to free jsegs now that oldestwrseq may have advanced. 3668 */ 3669 free_jsegs(jblocks); 3670} 3671 3672/* 3673 * Mark a jseg as DEPCOMPLETE and throw away the buffer. Attempt to handle 3674 * the final completions. 3675 */ 3676static void 3677handle_written_jseg(jseg, bp) 3678 struct jseg *jseg; 3679 struct buf *bp; 3680{ 3681 3682 if (jseg->js_refs == 0) 3683 panic("handle_written_jseg: No self-reference on %p", jseg); 3684 jseg->js_state |= DEPCOMPLETE; 3685 /* 3686 * We'll never need this buffer again, set flags so it will be 3687 * discarded. 3688 */ 3689 bp->b_flags |= B_INVAL | B_NOCACHE; 3690 pbrelvp(bp); 3691 complete_jsegs(jseg); 3692} 3693 3694static inline struct jsegdep * 3695inoref_jseg(inoref) 3696 struct inoref *inoref; 3697{ 3698 struct jsegdep *jsegdep; 3699 3700 jsegdep = inoref->if_jsegdep; 3701 inoref->if_jsegdep = NULL; 3702 3703 return (jsegdep); 3704} 3705 3706/* 3707 * Called once a jremref has made it to stable store. The jremref is marked 3708 * complete and we attempt to free it. Any pagedeps writes sleeping waiting 3709 * for the jremref to complete will be awoken by free_jremref. 3710 */ 3711static void 3712handle_written_jremref(jremref) 3713 struct jremref *jremref; 3714{ 3715 struct inodedep *inodedep; 3716 struct jsegdep *jsegdep; 3717 struct dirrem *dirrem; 3718 3719 /* Grab the jsegdep. */ 3720 jsegdep = inoref_jseg(&jremref->jr_ref); 3721 /* 3722 * Remove us from the inoref list. 3723 */ 3724 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 3725 0, &inodedep) == 0) 3726 panic("handle_written_jremref: Lost inodedep"); 3727 TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 3728 /* 3729 * Complete the dirrem. 3730 */ 3731 dirrem = jremref->jr_dirrem; 3732 jremref->jr_dirrem = NULL; 3733 LIST_REMOVE(jremref, jr_deps); 3734 jsegdep->jd_state |= jremref->jr_state & MKDIR_PARENT; 3735 jwork_insert(&dirrem->dm_jwork, jsegdep); 3736 if (LIST_EMPTY(&dirrem->dm_jremrefhd) && 3737 (dirrem->dm_state & COMPLETE) != 0) 3738 add_to_worklist(&dirrem->dm_list, 0); 3739 free_jremref(jremref); 3740} 3741 3742/* 3743 * Called once a jaddref has made it to stable store. The dependency is 3744 * marked complete and any dependent structures are added to the inode 3745 * bufwait list to be completed as soon as it is written. If a bitmap write 3746 * depends on this entry we move the inode into the inodedephd of the 3747 * bmsafemap dependency and attempt to remove the jaddref from the bmsafemap. 3748 */ 3749static void 3750handle_written_jaddref(jaddref) 3751 struct jaddref *jaddref; 3752{ 3753 struct jsegdep *jsegdep; 3754 struct inodedep *inodedep; 3755 struct diradd *diradd; 3756 struct mkdir *mkdir; 3757 3758 /* Grab the jsegdep. */ 3759 jsegdep = inoref_jseg(&jaddref->ja_ref); 3760 mkdir = NULL; 3761 diradd = NULL; 3762 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 3763 0, &inodedep) == 0) 3764 panic("handle_written_jaddref: Lost inodedep."); 3765 if (jaddref->ja_diradd == NULL) 3766 panic("handle_written_jaddref: No dependency"); 3767 if (jaddref->ja_diradd->da_list.wk_type == D_DIRADD) { 3768 diradd = jaddref->ja_diradd; 3769 WORKLIST_INSERT(&inodedep->id_bufwait, &diradd->da_list); 3770 } else if (jaddref->ja_state & MKDIR_PARENT) { 3771 mkdir = jaddref->ja_mkdir; 3772 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir->md_list); 3773 } else if (jaddref->ja_state & MKDIR_BODY) 3774 mkdir = jaddref->ja_mkdir; 3775 else 3776 panic("handle_written_jaddref: Unknown dependency %p", 3777 jaddref->ja_diradd); 3778 jaddref->ja_diradd = NULL; /* also clears ja_mkdir */ 3779 /* 3780 * Remove us from the inode list. 3781 */ 3782 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps); 3783 /* 3784 * The mkdir may be waiting on the jaddref to clear before freeing. 3785 */ 3786 if (mkdir) { 3787 KASSERT(mkdir->md_list.wk_type == D_MKDIR, 3788 ("handle_written_jaddref: Incorrect type for mkdir %s", 3789 TYPENAME(mkdir->md_list.wk_type))); 3790 mkdir->md_jaddref = NULL; 3791 diradd = mkdir->md_diradd; 3792 mkdir->md_state |= DEPCOMPLETE; 3793 complete_mkdir(mkdir); 3794 } 3795 jwork_insert(&diradd->da_jwork, jsegdep); 3796 if (jaddref->ja_state & NEWBLOCK) { 3797 inodedep->id_state |= ONDEPLIST; 3798 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_inodedephd, 3799 inodedep, id_deps); 3800 } 3801 free_jaddref(jaddref); 3802} 3803 3804/* 3805 * Called once a jnewblk journal is written. The allocdirect or allocindir 3806 * is placed in the bmsafemap to await notification of a written bitmap. If 3807 * the operation was canceled we add the segdep to the appropriate 3808 * dependency to free the journal space once the canceling operation 3809 * completes. 3810 */ 3811static void 3812handle_written_jnewblk(jnewblk) 3813 struct jnewblk *jnewblk; 3814{ 3815 struct bmsafemap *bmsafemap; 3816 struct freefrag *freefrag; 3817 struct freework *freework; 3818 struct jsegdep *jsegdep; 3819 struct newblk *newblk; 3820 3821 /* Grab the jsegdep. */ 3822 jsegdep = jnewblk->jn_jsegdep; 3823 jnewblk->jn_jsegdep = NULL; 3824 if (jnewblk->jn_dep == NULL) 3825 panic("handle_written_jnewblk: No dependency for the segdep."); 3826 switch (jnewblk->jn_dep->wk_type) { 3827 case D_NEWBLK: 3828 case D_ALLOCDIRECT: 3829 case D_ALLOCINDIR: 3830 /* 3831 * Add the written block to the bmsafemap so it can 3832 * be notified when the bitmap is on disk. 3833 */ 3834 newblk = WK_NEWBLK(jnewblk->jn_dep); 3835 newblk->nb_jnewblk = NULL; 3836 if ((newblk->nb_state & GOINGAWAY) == 0) { 3837 bmsafemap = newblk->nb_bmsafemap; 3838 newblk->nb_state |= ONDEPLIST; 3839 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, 3840 nb_deps); 3841 } 3842 jwork_insert(&newblk->nb_jwork, jsegdep); 3843 break; 3844 case D_FREEFRAG: 3845 /* 3846 * A newblock being removed by a freefrag when replaced by 3847 * frag extension. 3848 */ 3849 freefrag = WK_FREEFRAG(jnewblk->jn_dep); 3850 freefrag->ff_jdep = NULL; 3851 jwork_insert(&freefrag->ff_jwork, jsegdep); 3852 break; 3853 case D_FREEWORK: 3854 /* 3855 * A direct block was removed by truncate. 3856 */ 3857 freework = WK_FREEWORK(jnewblk->jn_dep); 3858 freework->fw_jnewblk = NULL; 3859 jwork_insert(&freework->fw_freeblks->fb_jwork, jsegdep); 3860 break; 3861 default: 3862 panic("handle_written_jnewblk: Unknown type %d.", 3863 jnewblk->jn_dep->wk_type); 3864 } 3865 jnewblk->jn_dep = NULL; 3866 free_jnewblk(jnewblk); 3867} 3868 3869/* 3870 * Cancel a jfreefrag that won't be needed, probably due to colliding with 3871 * an in-flight allocation that has not yet been committed. Divorce us 3872 * from the freefrag and mark it DEPCOMPLETE so that it may be added 3873 * to the worklist. 3874 */ 3875static void 3876cancel_jfreefrag(jfreefrag) 3877 struct jfreefrag *jfreefrag; 3878{ 3879 struct freefrag *freefrag; 3880 3881 if (jfreefrag->fr_jsegdep) { 3882 free_jsegdep(jfreefrag->fr_jsegdep); 3883 jfreefrag->fr_jsegdep = NULL; 3884 } 3885 freefrag = jfreefrag->fr_freefrag; 3886 jfreefrag->fr_freefrag = NULL; 3887 free_jfreefrag(jfreefrag); 3888 freefrag->ff_state |= DEPCOMPLETE; 3889 CTR1(KTR_SUJ, "cancel_jfreefrag: blkno %jd", freefrag->ff_blkno); 3890} 3891 3892/* 3893 * Free a jfreefrag when the parent freefrag is rendered obsolete. 3894 */ 3895static void 3896free_jfreefrag(jfreefrag) 3897 struct jfreefrag *jfreefrag; 3898{ 3899 3900 if (jfreefrag->fr_state & INPROGRESS) 3901 WORKLIST_REMOVE(&jfreefrag->fr_list); 3902 else if (jfreefrag->fr_state & ONWORKLIST) 3903 remove_from_journal(&jfreefrag->fr_list); 3904 if (jfreefrag->fr_freefrag != NULL) 3905 panic("free_jfreefrag: Still attached to a freefrag."); 3906 WORKITEM_FREE(jfreefrag, D_JFREEFRAG); 3907} 3908 3909/* 3910 * Called when the journal write for a jfreefrag completes. The parent 3911 * freefrag is added to the worklist if this completes its dependencies. 3912 */ 3913static void 3914handle_written_jfreefrag(jfreefrag) 3915 struct jfreefrag *jfreefrag; 3916{ 3917 struct jsegdep *jsegdep; 3918 struct freefrag *freefrag; 3919 3920 /* Grab the jsegdep. */ 3921 jsegdep = jfreefrag->fr_jsegdep; 3922 jfreefrag->fr_jsegdep = NULL; 3923 freefrag = jfreefrag->fr_freefrag; 3924 if (freefrag == NULL) 3925 panic("handle_written_jfreefrag: No freefrag."); 3926 freefrag->ff_state |= DEPCOMPLETE; 3927 freefrag->ff_jdep = NULL; 3928 jwork_insert(&freefrag->ff_jwork, jsegdep); 3929 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 3930 add_to_worklist(&freefrag->ff_list, 0); 3931 jfreefrag->fr_freefrag = NULL; 3932 free_jfreefrag(jfreefrag); 3933} 3934 3935/* 3936 * Called when the journal write for a jfreeblk completes. The jfreeblk 3937 * is removed from the freeblks list of pending journal writes and the 3938 * jsegdep is moved to the freeblks jwork to be completed when all blocks 3939 * have been reclaimed. 3940 */ 3941static void 3942handle_written_jblkdep(jblkdep) 3943 struct jblkdep *jblkdep; 3944{ 3945 struct freeblks *freeblks; 3946 struct jsegdep *jsegdep; 3947 3948 /* Grab the jsegdep. */ 3949 jsegdep = jblkdep->jb_jsegdep; 3950 jblkdep->jb_jsegdep = NULL; 3951 freeblks = jblkdep->jb_freeblks; 3952 LIST_REMOVE(jblkdep, jb_deps); 3953 jwork_insert(&freeblks->fb_jwork, jsegdep); 3954 /* 3955 * If the freeblks is all journaled, we can add it to the worklist. 3956 */ 3957 if (LIST_EMPTY(&freeblks->fb_jblkdephd) && 3958 (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 3959 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 3960 3961 free_jblkdep(jblkdep); 3962} 3963 3964static struct jsegdep * 3965newjsegdep(struct worklist *wk) 3966{ 3967 struct jsegdep *jsegdep; 3968 3969 jsegdep = malloc(sizeof(*jsegdep), M_JSEGDEP, M_SOFTDEP_FLAGS); 3970 workitem_alloc(&jsegdep->jd_list, D_JSEGDEP, wk->wk_mp); 3971 jsegdep->jd_seg = NULL; 3972 3973 return (jsegdep); 3974} 3975 3976static struct jmvref * 3977newjmvref(dp, ino, oldoff, newoff) 3978 struct inode *dp; 3979 ino_t ino; 3980 off_t oldoff; 3981 off_t newoff; 3982{ 3983 struct jmvref *jmvref; 3984 3985 jmvref = malloc(sizeof(*jmvref), M_JMVREF, M_SOFTDEP_FLAGS); 3986 workitem_alloc(&jmvref->jm_list, D_JMVREF, UFSTOVFS(dp->i_ump)); 3987 jmvref->jm_list.wk_state = ATTACHED | DEPCOMPLETE; 3988 jmvref->jm_parent = dp->i_number; 3989 jmvref->jm_ino = ino; 3990 jmvref->jm_oldoff = oldoff; 3991 jmvref->jm_newoff = newoff; 3992 3993 return (jmvref); 3994} 3995 3996/* 3997 * Allocate a new jremref that tracks the removal of ip from dp with the 3998 * directory entry offset of diroff. Mark the entry as ATTACHED and 3999 * DEPCOMPLETE as we have all the information required for the journal write 4000 * and the directory has already been removed from the buffer. The caller 4001 * is responsible for linking the jremref into the pagedep and adding it 4002 * to the journal to write. The MKDIR_PARENT flag is set if we're doing 4003 * a DOTDOT addition so handle_workitem_remove() can properly assign 4004 * the jsegdep when we're done. 4005 */ 4006static struct jremref * 4007newjremref(struct dirrem *dirrem, struct inode *dp, struct inode *ip, 4008 off_t diroff, nlink_t nlink) 4009{ 4010 struct jremref *jremref; 4011 4012 jremref = malloc(sizeof(*jremref), M_JREMREF, M_SOFTDEP_FLAGS); 4013 workitem_alloc(&jremref->jr_list, D_JREMREF, UFSTOVFS(dp->i_ump)); 4014 jremref->jr_state = ATTACHED; 4015 newinoref(&jremref->jr_ref, ip->i_number, dp->i_number, diroff, 4016 nlink, ip->i_mode); 4017 jremref->jr_dirrem = dirrem; 4018 4019 return (jremref); 4020} 4021 4022static inline void 4023newinoref(struct inoref *inoref, ino_t ino, ino_t parent, off_t diroff, 4024 nlink_t nlink, uint16_t mode) 4025{ 4026 4027 inoref->if_jsegdep = newjsegdep(&inoref->if_list); 4028 inoref->if_diroff = diroff; 4029 inoref->if_ino = ino; 4030 inoref->if_parent = parent; 4031 inoref->if_nlink = nlink; 4032 inoref->if_mode = mode; 4033} 4034 4035/* 4036 * Allocate a new jaddref to track the addition of ino to dp at diroff. The 4037 * directory offset may not be known until later. The caller is responsible 4038 * adding the entry to the journal when this information is available. nlink 4039 * should be the link count prior to the addition and mode is only required 4040 * to have the correct FMT. 4041 */ 4042static struct jaddref * 4043newjaddref(struct inode *dp, ino_t ino, off_t diroff, int16_t nlink, 4044 uint16_t mode) 4045{ 4046 struct jaddref *jaddref; 4047 4048 jaddref = malloc(sizeof(*jaddref), M_JADDREF, M_SOFTDEP_FLAGS); 4049 workitem_alloc(&jaddref->ja_list, D_JADDREF, UFSTOVFS(dp->i_ump)); 4050 jaddref->ja_state = ATTACHED; 4051 jaddref->ja_mkdir = NULL; 4052 newinoref(&jaddref->ja_ref, ino, dp->i_number, diroff, nlink, mode); 4053 4054 return (jaddref); 4055} 4056 4057/* 4058 * Create a new free dependency for a freework. The caller is responsible 4059 * for adjusting the reference count when it has the lock held. The freedep 4060 * will track an outstanding bitmap write that will ultimately clear the 4061 * freework to continue. 4062 */ 4063static struct freedep * 4064newfreedep(struct freework *freework) 4065{ 4066 struct freedep *freedep; 4067 4068 freedep = malloc(sizeof(*freedep), M_FREEDEP, M_SOFTDEP_FLAGS); 4069 workitem_alloc(&freedep->fd_list, D_FREEDEP, freework->fw_list.wk_mp); 4070 freedep->fd_freework = freework; 4071 4072 return (freedep); 4073} 4074 4075/* 4076 * Free a freedep structure once the buffer it is linked to is written. If 4077 * this is the last reference to the freework schedule it for completion. 4078 */ 4079static void 4080free_freedep(freedep) 4081 struct freedep *freedep; 4082{ 4083 struct freework *freework; 4084 4085 freework = freedep->fd_freework; 4086 freework->fw_freeblks->fb_cgwait--; 4087 if (--freework->fw_ref == 0) 4088 freework_enqueue(freework); 4089 WORKITEM_FREE(freedep, D_FREEDEP); 4090} 4091 4092/* 4093 * Allocate a new freework structure that may be a level in an indirect 4094 * when parent is not NULL or a top level block when it is. The top level 4095 * freework structures are allocated without the per-filesystem lock held 4096 * and before the freeblks is visible outside of softdep_setup_freeblocks(). 4097 */ 4098static struct freework * 4099newfreework(ump, freeblks, parent, lbn, nb, frags, off, journal) 4100 struct ufsmount *ump; 4101 struct freeblks *freeblks; 4102 struct freework *parent; 4103 ufs_lbn_t lbn; 4104 ufs2_daddr_t nb; 4105 int frags; 4106 int off; 4107 int journal; 4108{ 4109 struct freework *freework; 4110 4111 freework = malloc(sizeof(*freework), M_FREEWORK, M_SOFTDEP_FLAGS); 4112 workitem_alloc(&freework->fw_list, D_FREEWORK, freeblks->fb_list.wk_mp); 4113 freework->fw_state = ATTACHED; 4114 freework->fw_jnewblk = NULL; 4115 freework->fw_freeblks = freeblks; 4116 freework->fw_parent = parent; 4117 freework->fw_lbn = lbn; 4118 freework->fw_blkno = nb; 4119 freework->fw_frags = frags; 4120 freework->fw_indir = NULL; 4121 freework->fw_ref = (MOUNTEDSUJ(UFSTOVFS(ump)) == 0 || lbn >= -NXADDR) 4122 ? 0 : NINDIR(ump->um_fs) + 1; 4123 freework->fw_start = freework->fw_off = off; 4124 if (journal) 4125 newjfreeblk(freeblks, lbn, nb, frags); 4126 if (parent == NULL) { 4127 ACQUIRE_LOCK(ump); 4128 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 4129 freeblks->fb_ref++; 4130 FREE_LOCK(ump); 4131 } 4132 4133 return (freework); 4134} 4135 4136/* 4137 * Eliminate a jfreeblk for a block that does not need journaling. 4138 */ 4139static void 4140cancel_jfreeblk(freeblks, blkno) 4141 struct freeblks *freeblks; 4142 ufs2_daddr_t blkno; 4143{ 4144 struct jfreeblk *jfreeblk; 4145 struct jblkdep *jblkdep; 4146 4147 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) { 4148 if (jblkdep->jb_list.wk_type != D_JFREEBLK) 4149 continue; 4150 jfreeblk = WK_JFREEBLK(&jblkdep->jb_list); 4151 if (jfreeblk->jf_blkno == blkno) 4152 break; 4153 } 4154 if (jblkdep == NULL) 4155 return; 4156 CTR1(KTR_SUJ, "cancel_jfreeblk: blkno %jd", blkno); 4157 free_jsegdep(jblkdep->jb_jsegdep); 4158 LIST_REMOVE(jblkdep, jb_deps); 4159 WORKITEM_FREE(jfreeblk, D_JFREEBLK); 4160} 4161 4162/* 4163 * Allocate a new jfreeblk to journal top level block pointer when truncating 4164 * a file. The caller must add this to the worklist when the per-filesystem 4165 * lock is held. 4166 */ 4167static struct jfreeblk * 4168newjfreeblk(freeblks, lbn, blkno, frags) 4169 struct freeblks *freeblks; 4170 ufs_lbn_t lbn; 4171 ufs2_daddr_t blkno; 4172 int frags; 4173{ 4174 struct jfreeblk *jfreeblk; 4175 4176 jfreeblk = malloc(sizeof(*jfreeblk), M_JFREEBLK, M_SOFTDEP_FLAGS); 4177 workitem_alloc(&jfreeblk->jf_dep.jb_list, D_JFREEBLK, 4178 freeblks->fb_list.wk_mp); 4179 jfreeblk->jf_dep.jb_jsegdep = newjsegdep(&jfreeblk->jf_dep.jb_list); 4180 jfreeblk->jf_dep.jb_freeblks = freeblks; 4181 jfreeblk->jf_ino = freeblks->fb_inum; 4182 jfreeblk->jf_lbn = lbn; 4183 jfreeblk->jf_blkno = blkno; 4184 jfreeblk->jf_frags = frags; 4185 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jfreeblk->jf_dep, jb_deps); 4186 4187 return (jfreeblk); 4188} 4189 4190/* 4191 * The journal is only prepared to handle full-size block numbers, so we 4192 * have to adjust the record to reflect the change to a full-size block. 4193 * For example, suppose we have a block made up of fragments 8-15 and 4194 * want to free its last two fragments. We are given a request that says: 4195 * FREEBLK ino=5, blkno=14, lbn=0, frags=2, oldfrags=0 4196 * where frags are the number of fragments to free and oldfrags are the 4197 * number of fragments to keep. To block align it, we have to change it to 4198 * have a valid full-size blkno, so it becomes: 4199 * FREEBLK ino=5, blkno=8, lbn=0, frags=2, oldfrags=6 4200 */ 4201static void 4202adjust_newfreework(freeblks, frag_offset) 4203 struct freeblks *freeblks; 4204 int frag_offset; 4205{ 4206 struct jfreeblk *jfreeblk; 4207 4208 KASSERT((LIST_FIRST(&freeblks->fb_jblkdephd) != NULL && 4209 LIST_FIRST(&freeblks->fb_jblkdephd)->jb_list.wk_type == D_JFREEBLK), 4210 ("adjust_newfreework: Missing freeblks dependency")); 4211 4212 jfreeblk = WK_JFREEBLK(LIST_FIRST(&freeblks->fb_jblkdephd)); 4213 jfreeblk->jf_blkno -= frag_offset; 4214 jfreeblk->jf_frags += frag_offset; 4215} 4216 4217/* 4218 * Allocate a new jtrunc to track a partial truncation. 4219 */ 4220static struct jtrunc * 4221newjtrunc(freeblks, size, extsize) 4222 struct freeblks *freeblks; 4223 off_t size; 4224 int extsize; 4225{ 4226 struct jtrunc *jtrunc; 4227 4228 jtrunc = malloc(sizeof(*jtrunc), M_JTRUNC, M_SOFTDEP_FLAGS); 4229 workitem_alloc(&jtrunc->jt_dep.jb_list, D_JTRUNC, 4230 freeblks->fb_list.wk_mp); 4231 jtrunc->jt_dep.jb_jsegdep = newjsegdep(&jtrunc->jt_dep.jb_list); 4232 jtrunc->jt_dep.jb_freeblks = freeblks; 4233 jtrunc->jt_ino = freeblks->fb_inum; 4234 jtrunc->jt_size = size; 4235 jtrunc->jt_extsize = extsize; 4236 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jtrunc->jt_dep, jb_deps); 4237 4238 return (jtrunc); 4239} 4240 4241/* 4242 * If we're canceling a new bitmap we have to search for another ref 4243 * to move into the bmsafemap dep. This might be better expressed 4244 * with another structure. 4245 */ 4246static void 4247move_newblock_dep(jaddref, inodedep) 4248 struct jaddref *jaddref; 4249 struct inodedep *inodedep; 4250{ 4251 struct inoref *inoref; 4252 struct jaddref *jaddrefn; 4253 4254 jaddrefn = NULL; 4255 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4256 inoref = TAILQ_NEXT(inoref, if_deps)) { 4257 if ((jaddref->ja_state & NEWBLOCK) && 4258 inoref->if_list.wk_type == D_JADDREF) { 4259 jaddrefn = (struct jaddref *)inoref; 4260 break; 4261 } 4262 } 4263 if (jaddrefn == NULL) 4264 return; 4265 jaddrefn->ja_state &= ~(ATTACHED | UNDONE); 4266 jaddrefn->ja_state |= jaddref->ja_state & 4267 (ATTACHED | UNDONE | NEWBLOCK); 4268 jaddref->ja_state &= ~(ATTACHED | UNDONE | NEWBLOCK); 4269 jaddref->ja_state |= ATTACHED; 4270 LIST_REMOVE(jaddref, ja_bmdeps); 4271 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_jaddrefhd, jaddrefn, 4272 ja_bmdeps); 4273} 4274 4275/* 4276 * Cancel a jaddref either before it has been written or while it is being 4277 * written. This happens when a link is removed before the add reaches 4278 * the disk. The jaddref dependency is kept linked into the bmsafemap 4279 * and inode to prevent the link count or bitmap from reaching the disk 4280 * until handle_workitem_remove() re-adjusts the counts and bitmaps as 4281 * required. 4282 * 4283 * Returns 1 if the canceled addref requires journaling of the remove and 4284 * 0 otherwise. 4285 */ 4286static int 4287cancel_jaddref(jaddref, inodedep, wkhd) 4288 struct jaddref *jaddref; 4289 struct inodedep *inodedep; 4290 struct workhead *wkhd; 4291{ 4292 struct inoref *inoref; 4293 struct jsegdep *jsegdep; 4294 int needsj; 4295 4296 KASSERT((jaddref->ja_state & COMPLETE) == 0, 4297 ("cancel_jaddref: Canceling complete jaddref")); 4298 if (jaddref->ja_state & (INPROGRESS | COMPLETE)) 4299 needsj = 1; 4300 else 4301 needsj = 0; 4302 if (inodedep == NULL) 4303 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 4304 0, &inodedep) == 0) 4305 panic("cancel_jaddref: Lost inodedep"); 4306 /* 4307 * We must adjust the nlink of any reference operation that follows 4308 * us so that it is consistent with the in-memory reference. This 4309 * ensures that inode nlink rollbacks always have the correct link. 4310 */ 4311 if (needsj == 0) { 4312 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4313 inoref = TAILQ_NEXT(inoref, if_deps)) { 4314 if (inoref->if_state & GOINGAWAY) 4315 break; 4316 inoref->if_nlink--; 4317 } 4318 } 4319 jsegdep = inoref_jseg(&jaddref->ja_ref); 4320 if (jaddref->ja_state & NEWBLOCK) 4321 move_newblock_dep(jaddref, inodedep); 4322 wake_worklist(&jaddref->ja_list); 4323 jaddref->ja_mkdir = NULL; 4324 if (jaddref->ja_state & INPROGRESS) { 4325 jaddref->ja_state &= ~INPROGRESS; 4326 WORKLIST_REMOVE(&jaddref->ja_list); 4327 jwork_insert(wkhd, jsegdep); 4328 } else { 4329 free_jsegdep(jsegdep); 4330 if (jaddref->ja_state & DEPCOMPLETE) 4331 remove_from_journal(&jaddref->ja_list); 4332 } 4333 jaddref->ja_state |= (GOINGAWAY | DEPCOMPLETE); 4334 /* 4335 * Leave NEWBLOCK jaddrefs on the inodedep so handle_workitem_remove 4336 * can arrange for them to be freed with the bitmap. Otherwise we 4337 * no longer need this addref attached to the inoreflst and it 4338 * will incorrectly adjust nlink if we leave it. 4339 */ 4340 if ((jaddref->ja_state & NEWBLOCK) == 0) { 4341 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 4342 if_deps); 4343 jaddref->ja_state |= COMPLETE; 4344 free_jaddref(jaddref); 4345 return (needsj); 4346 } 4347 /* 4348 * Leave the head of the list for jsegdeps for fast merging. 4349 */ 4350 if (LIST_FIRST(wkhd) != NULL) { 4351 jaddref->ja_state |= ONWORKLIST; 4352 LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list); 4353 } else 4354 WORKLIST_INSERT(wkhd, &jaddref->ja_list); 4355 4356 return (needsj); 4357} 4358 4359/* 4360 * Attempt to free a jaddref structure when some work completes. This 4361 * should only succeed once the entry is written and all dependencies have 4362 * been notified. 4363 */ 4364static void 4365free_jaddref(jaddref) 4366 struct jaddref *jaddref; 4367{ 4368 4369 if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE) 4370 return; 4371 if (jaddref->ja_ref.if_jsegdep) 4372 panic("free_jaddref: segdep attached to jaddref %p(0x%X)\n", 4373 jaddref, jaddref->ja_state); 4374 if (jaddref->ja_state & NEWBLOCK) 4375 LIST_REMOVE(jaddref, ja_bmdeps); 4376 if (jaddref->ja_state & (INPROGRESS | ONWORKLIST)) 4377 panic("free_jaddref: Bad state %p(0x%X)", 4378 jaddref, jaddref->ja_state); 4379 if (jaddref->ja_mkdir != NULL) 4380 panic("free_jaddref: Work pending, 0x%X\n", jaddref->ja_state); 4381 WORKITEM_FREE(jaddref, D_JADDREF); 4382} 4383 4384/* 4385 * Free a jremref structure once it has been written or discarded. 4386 */ 4387static void 4388free_jremref(jremref) 4389 struct jremref *jremref; 4390{ 4391 4392 if (jremref->jr_ref.if_jsegdep) 4393 free_jsegdep(jremref->jr_ref.if_jsegdep); 4394 if (jremref->jr_state & INPROGRESS) 4395 panic("free_jremref: IO still pending"); 4396 WORKITEM_FREE(jremref, D_JREMREF); 4397} 4398 4399/* 4400 * Free a jnewblk structure. 4401 */ 4402static void 4403free_jnewblk(jnewblk) 4404 struct jnewblk *jnewblk; 4405{ 4406 4407 if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE) 4408 return; 4409 LIST_REMOVE(jnewblk, jn_deps); 4410 if (jnewblk->jn_dep != NULL) 4411 panic("free_jnewblk: Dependency still attached."); 4412 WORKITEM_FREE(jnewblk, D_JNEWBLK); 4413} 4414 4415/* 4416 * Cancel a jnewblk which has been been made redundant by frag extension. 4417 */ 4418static void 4419cancel_jnewblk(jnewblk, wkhd) 4420 struct jnewblk *jnewblk; 4421 struct workhead *wkhd; 4422{ 4423 struct jsegdep *jsegdep; 4424 4425 CTR1(KTR_SUJ, "cancel_jnewblk: blkno %jd", jnewblk->jn_blkno); 4426 jsegdep = jnewblk->jn_jsegdep; 4427 if (jnewblk->jn_jsegdep == NULL || jnewblk->jn_dep == NULL) 4428 panic("cancel_jnewblk: Invalid state"); 4429 jnewblk->jn_jsegdep = NULL; 4430 jnewblk->jn_dep = NULL; 4431 jnewblk->jn_state |= GOINGAWAY; 4432 if (jnewblk->jn_state & INPROGRESS) { 4433 jnewblk->jn_state &= ~INPROGRESS; 4434 WORKLIST_REMOVE(&jnewblk->jn_list); 4435 jwork_insert(wkhd, jsegdep); 4436 } else { 4437 free_jsegdep(jsegdep); 4438 remove_from_journal(&jnewblk->jn_list); 4439 } 4440 wake_worklist(&jnewblk->jn_list); 4441 WORKLIST_INSERT(wkhd, &jnewblk->jn_list); 4442} 4443 4444static void 4445free_jblkdep(jblkdep) 4446 struct jblkdep *jblkdep; 4447{ 4448 4449 if (jblkdep->jb_list.wk_type == D_JFREEBLK) 4450 WORKITEM_FREE(jblkdep, D_JFREEBLK); 4451 else if (jblkdep->jb_list.wk_type == D_JTRUNC) 4452 WORKITEM_FREE(jblkdep, D_JTRUNC); 4453 else 4454 panic("free_jblkdep: Unexpected type %s", 4455 TYPENAME(jblkdep->jb_list.wk_type)); 4456} 4457 4458/* 4459 * Free a single jseg once it is no longer referenced in memory or on 4460 * disk. Reclaim journal blocks and dependencies waiting for the segment 4461 * to disappear. 4462 */ 4463static void 4464free_jseg(jseg, jblocks) 4465 struct jseg *jseg; 4466 struct jblocks *jblocks; 4467{ 4468 struct freework *freework; 4469 4470 /* 4471 * Free freework structures that were lingering to indicate freed 4472 * indirect blocks that forced journal write ordering on reallocate. 4473 */ 4474 while ((freework = LIST_FIRST(&jseg->js_indirs)) != NULL) 4475 indirblk_remove(freework); 4476 if (jblocks->jb_oldestseg == jseg) 4477 jblocks->jb_oldestseg = TAILQ_NEXT(jseg, js_next); 4478 TAILQ_REMOVE(&jblocks->jb_segs, jseg, js_next); 4479 jblocks_free(jblocks, jseg->js_list.wk_mp, jseg->js_size); 4480 KASSERT(LIST_EMPTY(&jseg->js_entries), 4481 ("free_jseg: Freed jseg has valid entries.")); 4482 WORKITEM_FREE(jseg, D_JSEG); 4483} 4484 4485/* 4486 * Free all jsegs that meet the criteria for being reclaimed and update 4487 * oldestseg. 4488 */ 4489static void 4490free_jsegs(jblocks) 4491 struct jblocks *jblocks; 4492{ 4493 struct jseg *jseg; 4494 4495 /* 4496 * Free only those jsegs which have none allocated before them to 4497 * preserve the journal space ordering. 4498 */ 4499 while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) { 4500 /* 4501 * Only reclaim space when nothing depends on this journal 4502 * set and another set has written that it is no longer 4503 * valid. 4504 */ 4505 if (jseg->js_refs != 0) { 4506 jblocks->jb_oldestseg = jseg; 4507 return; 4508 } 4509 if ((jseg->js_state & ALLCOMPLETE) != ALLCOMPLETE) 4510 break; 4511 if (jseg->js_seq > jblocks->jb_oldestwrseq) 4512 break; 4513 /* 4514 * We can free jsegs that didn't write entries when 4515 * oldestwrseq == js_seq. 4516 */ 4517 if (jseg->js_seq == jblocks->jb_oldestwrseq && 4518 jseg->js_cnt != 0) 4519 break; 4520 free_jseg(jseg, jblocks); 4521 } 4522 /* 4523 * If we exited the loop above we still must discover the 4524 * oldest valid segment. 4525 */ 4526 if (jseg) 4527 for (jseg = jblocks->jb_oldestseg; jseg != NULL; 4528 jseg = TAILQ_NEXT(jseg, js_next)) 4529 if (jseg->js_refs != 0) 4530 break; 4531 jblocks->jb_oldestseg = jseg; 4532 /* 4533 * The journal has no valid records but some jsegs may still be 4534 * waiting on oldestwrseq to advance. We force a small record 4535 * out to permit these lingering records to be reclaimed. 4536 */ 4537 if (jblocks->jb_oldestseg == NULL && !TAILQ_EMPTY(&jblocks->jb_segs)) 4538 jblocks->jb_needseg = 1; 4539} 4540 4541/* 4542 * Release one reference to a jseg and free it if the count reaches 0. This 4543 * should eventually reclaim journal space as well. 4544 */ 4545static void 4546rele_jseg(jseg) 4547 struct jseg *jseg; 4548{ 4549 4550 KASSERT(jseg->js_refs > 0, 4551 ("free_jseg: Invalid refcnt %d", jseg->js_refs)); 4552 if (--jseg->js_refs != 0) 4553 return; 4554 free_jsegs(jseg->js_jblocks); 4555} 4556 4557/* 4558 * Release a jsegdep and decrement the jseg count. 4559 */ 4560static void 4561free_jsegdep(jsegdep) 4562 struct jsegdep *jsegdep; 4563{ 4564 4565 if (jsegdep->jd_seg) 4566 rele_jseg(jsegdep->jd_seg); 4567 WORKITEM_FREE(jsegdep, D_JSEGDEP); 4568} 4569 4570/* 4571 * Wait for a journal item to make it to disk. Initiate journal processing 4572 * if required. 4573 */ 4574static int 4575jwait(wk, waitfor) 4576 struct worklist *wk; 4577 int waitfor; 4578{ 4579 4580 LOCK_OWNED(VFSTOUFS(wk->wk_mp)); 4581 /* 4582 * Blocking journal waits cause slow synchronous behavior. Record 4583 * stats on the frequency of these blocking operations. 4584 */ 4585 if (waitfor == MNT_WAIT) { 4586 stat_journal_wait++; 4587 switch (wk->wk_type) { 4588 case D_JREMREF: 4589 case D_JMVREF: 4590 stat_jwait_filepage++; 4591 break; 4592 case D_JTRUNC: 4593 case D_JFREEBLK: 4594 stat_jwait_freeblks++; 4595 break; 4596 case D_JNEWBLK: 4597 stat_jwait_newblk++; 4598 break; 4599 case D_JADDREF: 4600 stat_jwait_inode++; 4601 break; 4602 default: 4603 break; 4604 } 4605 } 4606 /* 4607 * If IO has not started we process the journal. We can't mark the 4608 * worklist item as IOWAITING because we drop the lock while 4609 * processing the journal and the worklist entry may be freed after 4610 * this point. The caller may call back in and re-issue the request. 4611 */ 4612 if ((wk->wk_state & INPROGRESS) == 0) { 4613 softdep_process_journal(wk->wk_mp, wk, waitfor); 4614 if (waitfor != MNT_WAIT) 4615 return (EBUSY); 4616 return (0); 4617 } 4618 if (waitfor != MNT_WAIT) 4619 return (EBUSY); 4620 wait_worklist(wk, "jwait"); 4621 return (0); 4622} 4623 4624/* 4625 * Lookup an inodedep based on an inode pointer and set the nlinkdelta as 4626 * appropriate. This is a convenience function to reduce duplicate code 4627 * for the setup and revert functions below. 4628 */ 4629static struct inodedep * 4630inodedep_lookup_ip(ip) 4631 struct inode *ip; 4632{ 4633 struct inodedep *inodedep; 4634 int dflags; 4635 4636 KASSERT(ip->i_nlink >= ip->i_effnlink, 4637 ("inodedep_lookup_ip: bad delta")); 4638 dflags = DEPALLOC; 4639 if (IS_SNAPSHOT(ip)) 4640 dflags |= NODELAY; 4641 (void) inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, dflags, 4642 &inodedep); 4643 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 4644 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 4645 4646 return (inodedep); 4647} 4648 4649/* 4650 * Called prior to creating a new inode and linking it to a directory. The 4651 * jaddref structure must already be allocated by softdep_setup_inomapdep 4652 * and it is discovered here so we can initialize the mode and update 4653 * nlinkdelta. 4654 */ 4655void 4656softdep_setup_create(dp, ip) 4657 struct inode *dp; 4658 struct inode *ip; 4659{ 4660 struct inodedep *inodedep; 4661 struct jaddref *jaddref; 4662 struct vnode *dvp; 4663 4664 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4665 ("softdep_setup_create called on non-softdep filesystem")); 4666 KASSERT(ip->i_nlink == 1, 4667 ("softdep_setup_create: Invalid link count.")); 4668 dvp = ITOV(dp); 4669 ACQUIRE_LOCK(dp->i_ump); 4670 inodedep = inodedep_lookup_ip(ip); 4671 if (DOINGSUJ(dvp)) { 4672 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4673 inoreflst); 4674 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 4675 ("softdep_setup_create: No addref structure present.")); 4676 } 4677 softdep_prelink(dvp, NULL); 4678 FREE_LOCK(dp->i_ump); 4679} 4680 4681/* 4682 * Create a jaddref structure to track the addition of a DOTDOT link when 4683 * we are reparenting an inode as part of a rename. This jaddref will be 4684 * found by softdep_setup_directory_change. Adjusts nlinkdelta for 4685 * non-journaling softdep. 4686 */ 4687void 4688softdep_setup_dotdot_link(dp, ip) 4689 struct inode *dp; 4690 struct inode *ip; 4691{ 4692 struct inodedep *inodedep; 4693 struct jaddref *jaddref; 4694 struct vnode *dvp; 4695 4696 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4697 ("softdep_setup_dotdot_link called on non-softdep filesystem")); 4698 dvp = ITOV(dp); 4699 jaddref = NULL; 4700 /* 4701 * We don't set MKDIR_PARENT as this is not tied to a mkdir and 4702 * is used as a normal link would be. 4703 */ 4704 if (DOINGSUJ(dvp)) 4705 jaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 4706 dp->i_effnlink - 1, dp->i_mode); 4707 ACQUIRE_LOCK(dp->i_ump); 4708 inodedep = inodedep_lookup_ip(dp); 4709 if (jaddref) 4710 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4711 if_deps); 4712 softdep_prelink(dvp, ITOV(ip)); 4713 FREE_LOCK(dp->i_ump); 4714} 4715 4716/* 4717 * Create a jaddref structure to track a new link to an inode. The directory 4718 * offset is not known until softdep_setup_directory_add or 4719 * softdep_setup_directory_change. Adjusts nlinkdelta for non-journaling 4720 * softdep. 4721 */ 4722void 4723softdep_setup_link(dp, ip) 4724 struct inode *dp; 4725 struct inode *ip; 4726{ 4727 struct inodedep *inodedep; 4728 struct jaddref *jaddref; 4729 struct vnode *dvp; 4730 4731 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4732 ("softdep_setup_link called on non-softdep filesystem")); 4733 dvp = ITOV(dp); 4734 jaddref = NULL; 4735 if (DOINGSUJ(dvp)) 4736 jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1, 4737 ip->i_mode); 4738 ACQUIRE_LOCK(dp->i_ump); 4739 inodedep = inodedep_lookup_ip(ip); 4740 if (jaddref) 4741 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4742 if_deps); 4743 softdep_prelink(dvp, ITOV(ip)); 4744 FREE_LOCK(dp->i_ump); 4745} 4746 4747/* 4748 * Called to create the jaddref structures to track . and .. references as 4749 * well as lookup and further initialize the incomplete jaddref created 4750 * by softdep_setup_inomapdep when the inode was allocated. Adjusts 4751 * nlinkdelta for non-journaling softdep. 4752 */ 4753void 4754softdep_setup_mkdir(dp, ip) 4755 struct inode *dp; 4756 struct inode *ip; 4757{ 4758 struct inodedep *inodedep; 4759 struct jaddref *dotdotaddref; 4760 struct jaddref *dotaddref; 4761 struct jaddref *jaddref; 4762 struct vnode *dvp; 4763 4764 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4765 ("softdep_setup_mkdir called on non-softdep filesystem")); 4766 dvp = ITOV(dp); 4767 dotaddref = dotdotaddref = NULL; 4768 if (DOINGSUJ(dvp)) { 4769 dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1, 4770 ip->i_mode); 4771 dotaddref->ja_state |= MKDIR_BODY; 4772 dotdotaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 4773 dp->i_effnlink - 1, dp->i_mode); 4774 dotdotaddref->ja_state |= MKDIR_PARENT; 4775 } 4776 ACQUIRE_LOCK(dp->i_ump); 4777 inodedep = inodedep_lookup_ip(ip); 4778 if (DOINGSUJ(dvp)) { 4779 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4780 inoreflst); 4781 KASSERT(jaddref != NULL, 4782 ("softdep_setup_mkdir: No addref structure present.")); 4783 KASSERT(jaddref->ja_parent == dp->i_number, 4784 ("softdep_setup_mkdir: bad parent %ju", 4785 (uintmax_t)jaddref->ja_parent)); 4786 TAILQ_INSERT_BEFORE(&jaddref->ja_ref, &dotaddref->ja_ref, 4787 if_deps); 4788 } 4789 inodedep = inodedep_lookup_ip(dp); 4790 if (DOINGSUJ(dvp)) 4791 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, 4792 &dotdotaddref->ja_ref, if_deps); 4793 softdep_prelink(ITOV(dp), NULL); 4794 FREE_LOCK(dp->i_ump); 4795} 4796 4797/* 4798 * Called to track nlinkdelta of the inode and parent directories prior to 4799 * unlinking a directory. 4800 */ 4801void 4802softdep_setup_rmdir(dp, ip) 4803 struct inode *dp; 4804 struct inode *ip; 4805{ 4806 struct vnode *dvp; 4807 4808 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4809 ("softdep_setup_rmdir called on non-softdep filesystem")); 4810 dvp = ITOV(dp); 4811 ACQUIRE_LOCK(dp->i_ump); 4812 (void) inodedep_lookup_ip(ip); 4813 (void) inodedep_lookup_ip(dp); 4814 softdep_prelink(dvp, ITOV(ip)); 4815 FREE_LOCK(dp->i_ump); 4816} 4817 4818/* 4819 * Called to track nlinkdelta of the inode and parent directories prior to 4820 * unlink. 4821 */ 4822void 4823softdep_setup_unlink(dp, ip) 4824 struct inode *dp; 4825 struct inode *ip; 4826{ 4827 struct vnode *dvp; 4828 4829 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4830 ("softdep_setup_unlink called on non-softdep filesystem")); 4831 dvp = ITOV(dp); 4832 ACQUIRE_LOCK(dp->i_ump); 4833 (void) inodedep_lookup_ip(ip); 4834 (void) inodedep_lookup_ip(dp); 4835 softdep_prelink(dvp, ITOV(ip)); 4836 FREE_LOCK(dp->i_ump); 4837} 4838 4839/* 4840 * Called to release the journal structures created by a failed non-directory 4841 * creation. Adjusts nlinkdelta for non-journaling softdep. 4842 */ 4843void 4844softdep_revert_create(dp, ip) 4845 struct inode *dp; 4846 struct inode *ip; 4847{ 4848 struct inodedep *inodedep; 4849 struct jaddref *jaddref; 4850 struct vnode *dvp; 4851 4852 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4853 ("softdep_revert_create called on non-softdep filesystem")); 4854 dvp = ITOV(dp); 4855 ACQUIRE_LOCK(dp->i_ump); 4856 inodedep = inodedep_lookup_ip(ip); 4857 if (DOINGSUJ(dvp)) { 4858 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4859 inoreflst); 4860 KASSERT(jaddref->ja_parent == dp->i_number, 4861 ("softdep_revert_create: addref parent mismatch")); 4862 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4863 } 4864 FREE_LOCK(dp->i_ump); 4865} 4866 4867/* 4868 * Called to release the journal structures created by a failed link 4869 * addition. Adjusts nlinkdelta for non-journaling softdep. 4870 */ 4871void 4872softdep_revert_link(dp, ip) 4873 struct inode *dp; 4874 struct inode *ip; 4875{ 4876 struct inodedep *inodedep; 4877 struct jaddref *jaddref; 4878 struct vnode *dvp; 4879 4880 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4881 ("softdep_revert_link called on non-softdep filesystem")); 4882 dvp = ITOV(dp); 4883 ACQUIRE_LOCK(dp->i_ump); 4884 inodedep = inodedep_lookup_ip(ip); 4885 if (DOINGSUJ(dvp)) { 4886 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4887 inoreflst); 4888 KASSERT(jaddref->ja_parent == dp->i_number, 4889 ("softdep_revert_link: addref parent mismatch")); 4890 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4891 } 4892 FREE_LOCK(dp->i_ump); 4893} 4894 4895/* 4896 * Called to release the journal structures created by a failed mkdir 4897 * attempt. Adjusts nlinkdelta for non-journaling softdep. 4898 */ 4899void 4900softdep_revert_mkdir(dp, ip) 4901 struct inode *dp; 4902 struct inode *ip; 4903{ 4904 struct inodedep *inodedep; 4905 struct jaddref *jaddref; 4906 struct jaddref *dotaddref; 4907 struct vnode *dvp; 4908 4909 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4910 ("softdep_revert_mkdir called on non-softdep filesystem")); 4911 dvp = ITOV(dp); 4912 4913 ACQUIRE_LOCK(dp->i_ump); 4914 inodedep = inodedep_lookup_ip(dp); 4915 if (DOINGSUJ(dvp)) { 4916 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4917 inoreflst); 4918 KASSERT(jaddref->ja_parent == ip->i_number, 4919 ("softdep_revert_mkdir: dotdot addref parent mismatch")); 4920 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4921 } 4922 inodedep = inodedep_lookup_ip(ip); 4923 if (DOINGSUJ(dvp)) { 4924 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4925 inoreflst); 4926 KASSERT(jaddref->ja_parent == dp->i_number, 4927 ("softdep_revert_mkdir: addref parent mismatch")); 4928 dotaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 4929 inoreflst, if_deps); 4930 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4931 KASSERT(dotaddref->ja_parent == ip->i_number, 4932 ("softdep_revert_mkdir: dot addref parent mismatch")); 4933 cancel_jaddref(dotaddref, inodedep, &inodedep->id_inowait); 4934 } 4935 FREE_LOCK(dp->i_ump); 4936} 4937 4938/* 4939 * Called to correct nlinkdelta after a failed rmdir. 4940 */ 4941void 4942softdep_revert_rmdir(dp, ip) 4943 struct inode *dp; 4944 struct inode *ip; 4945{ 4946 4947 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4948 ("softdep_revert_rmdir called on non-softdep filesystem")); 4949 ACQUIRE_LOCK(dp->i_ump); 4950 (void) inodedep_lookup_ip(ip); 4951 (void) inodedep_lookup_ip(dp); 4952 FREE_LOCK(dp->i_ump); 4953} 4954 4955/* 4956 * Protecting the freemaps (or bitmaps). 4957 * 4958 * To eliminate the need to execute fsck before mounting a filesystem 4959 * after a power failure, one must (conservatively) guarantee that the 4960 * on-disk copy of the bitmaps never indicate that a live inode or block is 4961 * free. So, when a block or inode is allocated, the bitmap should be 4962 * updated (on disk) before any new pointers. When a block or inode is 4963 * freed, the bitmap should not be updated until all pointers have been 4964 * reset. The latter dependency is handled by the delayed de-allocation 4965 * approach described below for block and inode de-allocation. The former 4966 * dependency is handled by calling the following procedure when a block or 4967 * inode is allocated. When an inode is allocated an "inodedep" is created 4968 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk. 4969 * Each "inodedep" is also inserted into the hash indexing structure so 4970 * that any additional link additions can be made dependent on the inode 4971 * allocation. 4972 * 4973 * The ufs filesystem maintains a number of free block counts (e.g., per 4974 * cylinder group, per cylinder and per <cylinder, rotational position> pair) 4975 * in addition to the bitmaps. These counts are used to improve efficiency 4976 * during allocation and therefore must be consistent with the bitmaps. 4977 * There is no convenient way to guarantee post-crash consistency of these 4978 * counts with simple update ordering, for two main reasons: (1) The counts 4979 * and bitmaps for a single cylinder group block are not in the same disk 4980 * sector. If a disk write is interrupted (e.g., by power failure), one may 4981 * be written and the other not. (2) Some of the counts are located in the 4982 * superblock rather than the cylinder group block. So, we focus our soft 4983 * updates implementation on protecting the bitmaps. When mounting a 4984 * filesystem, we recompute the auxiliary counts from the bitmaps. 4985 */ 4986 4987/* 4988 * Called just after updating the cylinder group block to allocate an inode. 4989 */ 4990void 4991softdep_setup_inomapdep(bp, ip, newinum, mode) 4992 struct buf *bp; /* buffer for cylgroup block with inode map */ 4993 struct inode *ip; /* inode related to allocation */ 4994 ino_t newinum; /* new inode number being allocated */ 4995 int mode; 4996{ 4997 struct inodedep *inodedep; 4998 struct bmsafemap *bmsafemap; 4999 struct jaddref *jaddref; 5000 struct mount *mp; 5001 struct fs *fs; 5002 5003 mp = UFSTOVFS(ip->i_ump); 5004 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5005 ("softdep_setup_inomapdep called on non-softdep filesystem")); 5006 fs = ip->i_ump->um_fs; 5007 jaddref = NULL; 5008 5009 /* 5010 * Allocate the journal reference add structure so that the bitmap 5011 * can be dependent on it. 5012 */ 5013 if (MOUNTEDSUJ(mp)) { 5014 jaddref = newjaddref(ip, newinum, 0, 0, mode); 5015 jaddref->ja_state |= NEWBLOCK; 5016 } 5017 5018 /* 5019 * Create a dependency for the newly allocated inode. 5020 * Panic if it already exists as something is seriously wrong. 5021 * Otherwise add it to the dependency list for the buffer holding 5022 * the cylinder group map from which it was allocated. 5023 * 5024 * We have to preallocate a bmsafemap entry in case it is needed 5025 * in bmsafemap_lookup since once we allocate the inodedep, we 5026 * have to finish initializing it before we can FREE_LOCK(). 5027 * By preallocating, we avoid FREE_LOCK() while doing a malloc 5028 * in bmsafemap_lookup. We cannot call bmsafemap_lookup before 5029 * creating the inodedep as it can be freed during the time 5030 * that we FREE_LOCK() while allocating the inodedep. We must 5031 * call workitem_alloc() before entering the locked section as 5032 * it also acquires the lock and we must avoid trying doing so 5033 * recursively. 5034 */ 5035 bmsafemap = malloc(sizeof(struct bmsafemap), 5036 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5037 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5038 ACQUIRE_LOCK(ip->i_ump); 5039 if ((inodedep_lookup(mp, newinum, DEPALLOC | NODELAY, &inodedep))) 5040 panic("softdep_setup_inomapdep: dependency %p for new" 5041 "inode already exists", inodedep); 5042 bmsafemap = bmsafemap_lookup(mp, bp, ino_to_cg(fs, newinum), bmsafemap); 5043 if (jaddref) { 5044 LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps); 5045 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 5046 if_deps); 5047 } else { 5048 inodedep->id_state |= ONDEPLIST; 5049 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps); 5050 } 5051 inodedep->id_bmsafemap = bmsafemap; 5052 inodedep->id_state &= ~DEPCOMPLETE; 5053 FREE_LOCK(ip->i_ump); 5054} 5055 5056/* 5057 * Called just after updating the cylinder group block to 5058 * allocate block or fragment. 5059 */ 5060void 5061softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags) 5062 struct buf *bp; /* buffer for cylgroup block with block map */ 5063 struct mount *mp; /* filesystem doing allocation */ 5064 ufs2_daddr_t newblkno; /* number of newly allocated block */ 5065 int frags; /* Number of fragments. */ 5066 int oldfrags; /* Previous number of fragments for extend. */ 5067{ 5068 struct newblk *newblk; 5069 struct bmsafemap *bmsafemap; 5070 struct jnewblk *jnewblk; 5071 struct ufsmount *ump; 5072 struct fs *fs; 5073 5074 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5075 ("softdep_setup_blkmapdep called on non-softdep filesystem")); 5076 ump = VFSTOUFS(mp); 5077 fs = ump->um_fs; 5078 jnewblk = NULL; 5079 /* 5080 * Create a dependency for the newly allocated block. 5081 * Add it to the dependency list for the buffer holding 5082 * the cylinder group map from which it was allocated. 5083 */ 5084 if (MOUNTEDSUJ(mp)) { 5085 jnewblk = malloc(sizeof(*jnewblk), M_JNEWBLK, M_SOFTDEP_FLAGS); 5086 workitem_alloc(&jnewblk->jn_list, D_JNEWBLK, mp); 5087 jnewblk->jn_jsegdep = newjsegdep(&jnewblk->jn_list); 5088 jnewblk->jn_state = ATTACHED; 5089 jnewblk->jn_blkno = newblkno; 5090 jnewblk->jn_frags = frags; 5091 jnewblk->jn_oldfrags = oldfrags; 5092#ifdef SUJ_DEBUG 5093 { 5094 struct cg *cgp; 5095 uint8_t *blksfree; 5096 long bno; 5097 int i; 5098 5099 cgp = (struct cg *)bp->b_data; 5100 blksfree = cg_blksfree(cgp); 5101 bno = dtogd(fs, jnewblk->jn_blkno); 5102 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; 5103 i++) { 5104 if (isset(blksfree, bno + i)) 5105 panic("softdep_setup_blkmapdep: " 5106 "free fragment %d from %d-%d " 5107 "state 0x%X dep %p", i, 5108 jnewblk->jn_oldfrags, 5109 jnewblk->jn_frags, 5110 jnewblk->jn_state, 5111 jnewblk->jn_dep); 5112 } 5113 } 5114#endif 5115 } 5116 5117 CTR3(KTR_SUJ, 5118 "softdep_setup_blkmapdep: blkno %jd frags %d oldfrags %d", 5119 newblkno, frags, oldfrags); 5120 ACQUIRE_LOCK(ump); 5121 if (newblk_lookup(mp, newblkno, DEPALLOC, &newblk) != 0) 5122 panic("softdep_setup_blkmapdep: found block"); 5123 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp, 5124 dtog(fs, newblkno), NULL); 5125 if (jnewblk) { 5126 jnewblk->jn_dep = (struct worklist *)newblk; 5127 LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps); 5128 } else { 5129 newblk->nb_state |= ONDEPLIST; 5130 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps); 5131 } 5132 newblk->nb_bmsafemap = bmsafemap; 5133 newblk->nb_jnewblk = jnewblk; 5134 FREE_LOCK(ump); 5135} 5136 5137#define BMSAFEMAP_HASH(ump, cg) \ 5138 (&(ump)->bmsafemap_hashtbl[(cg) & (ump)->bmsafemap_hash_size]) 5139 5140static int 5141bmsafemap_find(bmsafemaphd, cg, bmsafemapp) 5142 struct bmsafemap_hashhead *bmsafemaphd; 5143 int cg; 5144 struct bmsafemap **bmsafemapp; 5145{ 5146 struct bmsafemap *bmsafemap; 5147 5148 LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash) 5149 if (bmsafemap->sm_cg == cg) 5150 break; 5151 if (bmsafemap) { 5152 *bmsafemapp = bmsafemap; 5153 return (1); 5154 } 5155 *bmsafemapp = NULL; 5156 5157 return (0); 5158} 5159 5160/* 5161 * Find the bmsafemap associated with a cylinder group buffer. 5162 * If none exists, create one. The buffer must be locked when 5163 * this routine is called and this routine must be called with 5164 * the softdep lock held. To avoid giving up the lock while 5165 * allocating a new bmsafemap, a preallocated bmsafemap may be 5166 * provided. If it is provided but not needed, it is freed. 5167 */ 5168static struct bmsafemap * 5169bmsafemap_lookup(mp, bp, cg, newbmsafemap) 5170 struct mount *mp; 5171 struct buf *bp; 5172 int cg; 5173 struct bmsafemap *newbmsafemap; 5174{ 5175 struct bmsafemap_hashhead *bmsafemaphd; 5176 struct bmsafemap *bmsafemap, *collision; 5177 struct worklist *wk; 5178 struct ufsmount *ump; 5179 5180 ump = VFSTOUFS(mp); 5181 LOCK_OWNED(ump); 5182 KASSERT(bp != NULL, ("bmsafemap_lookup: missing buffer")); 5183 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 5184 if (wk->wk_type == D_BMSAFEMAP) { 5185 if (newbmsafemap) 5186 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5187 return (WK_BMSAFEMAP(wk)); 5188 } 5189 } 5190 bmsafemaphd = BMSAFEMAP_HASH(ump, cg); 5191 if (bmsafemap_find(bmsafemaphd, cg, &bmsafemap) == 1) { 5192 if (newbmsafemap) 5193 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5194 return (bmsafemap); 5195 } 5196 if (newbmsafemap) { 5197 bmsafemap = newbmsafemap; 5198 } else { 5199 FREE_LOCK(ump); 5200 bmsafemap = malloc(sizeof(struct bmsafemap), 5201 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5202 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5203 ACQUIRE_LOCK(ump); 5204 } 5205 bmsafemap->sm_buf = bp; 5206 LIST_INIT(&bmsafemap->sm_inodedephd); 5207 LIST_INIT(&bmsafemap->sm_inodedepwr); 5208 LIST_INIT(&bmsafemap->sm_newblkhd); 5209 LIST_INIT(&bmsafemap->sm_newblkwr); 5210 LIST_INIT(&bmsafemap->sm_jaddrefhd); 5211 LIST_INIT(&bmsafemap->sm_jnewblkhd); 5212 LIST_INIT(&bmsafemap->sm_freehd); 5213 LIST_INIT(&bmsafemap->sm_freewr); 5214 if (bmsafemap_find(bmsafemaphd, cg, &collision) == 1) { 5215 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 5216 return (collision); 5217 } 5218 bmsafemap->sm_cg = cg; 5219 LIST_INSERT_HEAD(bmsafemaphd, bmsafemap, sm_hash); 5220 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 5221 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list); 5222 return (bmsafemap); 5223} 5224 5225/* 5226 * Direct block allocation dependencies. 5227 * 5228 * When a new block is allocated, the corresponding disk locations must be 5229 * initialized (with zeros or new data) before the on-disk inode points to 5230 * them. Also, the freemap from which the block was allocated must be 5231 * updated (on disk) before the inode's pointer. These two dependencies are 5232 * independent of each other and are needed for all file blocks and indirect 5233 * blocks that are pointed to directly by the inode. Just before the 5234 * "in-core" version of the inode is updated with a newly allocated block 5235 * number, a procedure (below) is called to setup allocation dependency 5236 * structures. These structures are removed when the corresponding 5237 * dependencies are satisfied or when the block allocation becomes obsolete 5238 * (i.e., the file is deleted, the block is de-allocated, or the block is a 5239 * fragment that gets upgraded). All of these cases are handled in 5240 * procedures described later. 5241 * 5242 * When a file extension causes a fragment to be upgraded, either to a larger 5243 * fragment or to a full block, the on-disk location may change (if the 5244 * previous fragment could not simply be extended). In this case, the old 5245 * fragment must be de-allocated, but not until after the inode's pointer has 5246 * been updated. In most cases, this is handled by later procedures, which 5247 * will construct a "freefrag" structure to be added to the workitem queue 5248 * when the inode update is complete (or obsolete). The main exception to 5249 * this is when an allocation occurs while a pending allocation dependency 5250 * (for the same block pointer) remains. This case is handled in the main 5251 * allocation dependency setup procedure by immediately freeing the 5252 * unreferenced fragments. 5253 */ 5254void 5255softdep_setup_allocdirect(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 5256 struct inode *ip; /* inode to which block is being added */ 5257 ufs_lbn_t off; /* block pointer within inode */ 5258 ufs2_daddr_t newblkno; /* disk block number being added */ 5259 ufs2_daddr_t oldblkno; /* previous block number, 0 unless frag */ 5260 long newsize; /* size of new block */ 5261 long oldsize; /* size of new block */ 5262 struct buf *bp; /* bp for allocated block */ 5263{ 5264 struct allocdirect *adp, *oldadp; 5265 struct allocdirectlst *adphead; 5266 struct freefrag *freefrag; 5267 struct inodedep *inodedep; 5268 struct pagedep *pagedep; 5269 struct jnewblk *jnewblk; 5270 struct newblk *newblk; 5271 struct mount *mp; 5272 ufs_lbn_t lbn; 5273 5274 lbn = bp->b_lblkno; 5275 mp = UFSTOVFS(ip->i_ump); 5276 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5277 ("softdep_setup_allocdirect called on non-softdep filesystem")); 5278 if (oldblkno && oldblkno != newblkno) 5279 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn); 5280 else 5281 freefrag = NULL; 5282 5283 CTR6(KTR_SUJ, 5284 "softdep_setup_allocdirect: ino %d blkno %jd oldblkno %jd " 5285 "off %jd newsize %ld oldsize %d", 5286 ip->i_number, newblkno, oldblkno, off, newsize, oldsize); 5287 ACQUIRE_LOCK(ip->i_ump); 5288 if (off >= NDADDR) { 5289 if (lbn > 0) 5290 panic("softdep_setup_allocdirect: bad lbn %jd, off %jd", 5291 lbn, off); 5292 /* allocating an indirect block */ 5293 if (oldblkno != 0) 5294 panic("softdep_setup_allocdirect: non-zero indir"); 5295 } else { 5296 if (off != lbn) 5297 panic("softdep_setup_allocdirect: lbn %jd != off %jd", 5298 lbn, off); 5299 /* 5300 * Allocating a direct block. 5301 * 5302 * If we are allocating a directory block, then we must 5303 * allocate an associated pagedep to track additions and 5304 * deletions. 5305 */ 5306 if ((ip->i_mode & IFMT) == IFDIR) 5307 pagedep_lookup(mp, bp, ip->i_number, off, DEPALLOC, 5308 &pagedep); 5309 } 5310 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5311 panic("softdep_setup_allocdirect: lost block"); 5312 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5313 ("softdep_setup_allocdirect: newblk already initialized")); 5314 /* 5315 * Convert the newblk to an allocdirect. 5316 */ 5317 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT); 5318 adp = (struct allocdirect *)newblk; 5319 newblk->nb_freefrag = freefrag; 5320 adp->ad_offset = off; 5321 adp->ad_oldblkno = oldblkno; 5322 adp->ad_newsize = newsize; 5323 adp->ad_oldsize = oldsize; 5324 5325 /* 5326 * Finish initializing the journal. 5327 */ 5328 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5329 jnewblk->jn_ino = ip->i_number; 5330 jnewblk->jn_lbn = lbn; 5331 add_to_journal(&jnewblk->jn_list); 5332 } 5333 if (freefrag && freefrag->ff_jdep != NULL && 5334 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5335 add_to_journal(freefrag->ff_jdep); 5336 inodedep_lookup(mp, ip->i_number, DEPALLOC | NODELAY, &inodedep); 5337 adp->ad_inodedep = inodedep; 5338 5339 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5340 /* 5341 * The list of allocdirects must be kept in sorted and ascending 5342 * order so that the rollback routines can quickly determine the 5343 * first uncommitted block (the size of the file stored on disk 5344 * ends at the end of the lowest committed fragment, or if there 5345 * are no fragments, at the end of the highest committed block). 5346 * Since files generally grow, the typical case is that the new 5347 * block is to be added at the end of the list. We speed this 5348 * special case by checking against the last allocdirect in the 5349 * list before laboriously traversing the list looking for the 5350 * insertion point. 5351 */ 5352 adphead = &inodedep->id_newinoupdt; 5353 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5354 if (oldadp == NULL || oldadp->ad_offset <= off) { 5355 /* insert at end of list */ 5356 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5357 if (oldadp != NULL && oldadp->ad_offset == off) 5358 allocdirect_merge(adphead, adp, oldadp); 5359 FREE_LOCK(ip->i_ump); 5360 return; 5361 } 5362 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5363 if (oldadp->ad_offset >= off) 5364 break; 5365 } 5366 if (oldadp == NULL) 5367 panic("softdep_setup_allocdirect: lost entry"); 5368 /* insert in middle of list */ 5369 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5370 if (oldadp->ad_offset == off) 5371 allocdirect_merge(adphead, adp, oldadp); 5372 5373 FREE_LOCK(ip->i_ump); 5374} 5375 5376/* 5377 * Merge a newer and older journal record to be stored either in a 5378 * newblock or freefrag. This handles aggregating journal records for 5379 * fragment allocation into a second record as well as replacing a 5380 * journal free with an aborted journal allocation. A segment for the 5381 * oldest record will be placed on wkhd if it has been written. If not 5382 * the segment for the newer record will suffice. 5383 */ 5384static struct worklist * 5385jnewblk_merge(new, old, wkhd) 5386 struct worklist *new; 5387 struct worklist *old; 5388 struct workhead *wkhd; 5389{ 5390 struct jnewblk *njnewblk; 5391 struct jnewblk *jnewblk; 5392 5393 /* Handle NULLs to simplify callers. */ 5394 if (new == NULL) 5395 return (old); 5396 if (old == NULL) 5397 return (new); 5398 /* Replace a jfreefrag with a jnewblk. */ 5399 if (new->wk_type == D_JFREEFRAG) { 5400 if (WK_JNEWBLK(old)->jn_blkno != WK_JFREEFRAG(new)->fr_blkno) 5401 panic("jnewblk_merge: blkno mismatch: %p, %p", 5402 old, new); 5403 cancel_jfreefrag(WK_JFREEFRAG(new)); 5404 return (old); 5405 } 5406 if (old->wk_type != D_JNEWBLK || new->wk_type != D_JNEWBLK) 5407 panic("jnewblk_merge: Bad type: old %d new %d\n", 5408 old->wk_type, new->wk_type); 5409 /* 5410 * Handle merging of two jnewblk records that describe 5411 * different sets of fragments in the same block. 5412 */ 5413 jnewblk = WK_JNEWBLK(old); 5414 njnewblk = WK_JNEWBLK(new); 5415 if (jnewblk->jn_blkno != njnewblk->jn_blkno) 5416 panic("jnewblk_merge: Merging disparate blocks."); 5417 /* 5418 * The record may be rolled back in the cg. 5419 */ 5420 if (jnewblk->jn_state & UNDONE) { 5421 jnewblk->jn_state &= ~UNDONE; 5422 njnewblk->jn_state |= UNDONE; 5423 njnewblk->jn_state &= ~ATTACHED; 5424 } 5425 /* 5426 * We modify the newer addref and free the older so that if neither 5427 * has been written the most up-to-date copy will be on disk. If 5428 * both have been written but rolled back we only temporarily need 5429 * one of them to fix the bits when the cg write completes. 5430 */ 5431 jnewblk->jn_state |= ATTACHED | COMPLETE; 5432 njnewblk->jn_oldfrags = jnewblk->jn_oldfrags; 5433 cancel_jnewblk(jnewblk, wkhd); 5434 WORKLIST_REMOVE(&jnewblk->jn_list); 5435 free_jnewblk(jnewblk); 5436 return (new); 5437} 5438 5439/* 5440 * Replace an old allocdirect dependency with a newer one. 5441 * This routine must be called with splbio interrupts blocked. 5442 */ 5443static void 5444allocdirect_merge(adphead, newadp, oldadp) 5445 struct allocdirectlst *adphead; /* head of list holding allocdirects */ 5446 struct allocdirect *newadp; /* allocdirect being added */ 5447 struct allocdirect *oldadp; /* existing allocdirect being checked */ 5448{ 5449 struct worklist *wk; 5450 struct freefrag *freefrag; 5451 5452 freefrag = NULL; 5453 LOCK_OWNED(VFSTOUFS(newadp->ad_list.wk_mp)); 5454 if (newadp->ad_oldblkno != oldadp->ad_newblkno || 5455 newadp->ad_oldsize != oldadp->ad_newsize || 5456 newadp->ad_offset >= NDADDR) 5457 panic("%s %jd != new %jd || old size %ld != new %ld", 5458 "allocdirect_merge: old blkno", 5459 (intmax_t)newadp->ad_oldblkno, 5460 (intmax_t)oldadp->ad_newblkno, 5461 newadp->ad_oldsize, oldadp->ad_newsize); 5462 newadp->ad_oldblkno = oldadp->ad_oldblkno; 5463 newadp->ad_oldsize = oldadp->ad_oldsize; 5464 /* 5465 * If the old dependency had a fragment to free or had never 5466 * previously had a block allocated, then the new dependency 5467 * can immediately post its freefrag and adopt the old freefrag. 5468 * This action is done by swapping the freefrag dependencies. 5469 * The new dependency gains the old one's freefrag, and the 5470 * old one gets the new one and then immediately puts it on 5471 * the worklist when it is freed by free_newblk. It is 5472 * not possible to do this swap when the old dependency had a 5473 * non-zero size but no previous fragment to free. This condition 5474 * arises when the new block is an extension of the old block. 5475 * Here, the first part of the fragment allocated to the new 5476 * dependency is part of the block currently claimed on disk by 5477 * the old dependency, so cannot legitimately be freed until the 5478 * conditions for the new dependency are fulfilled. 5479 */ 5480 freefrag = newadp->ad_freefrag; 5481 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) { 5482 newadp->ad_freefrag = oldadp->ad_freefrag; 5483 oldadp->ad_freefrag = freefrag; 5484 } 5485 /* 5486 * If we are tracking a new directory-block allocation, 5487 * move it from the old allocdirect to the new allocdirect. 5488 */ 5489 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) { 5490 WORKLIST_REMOVE(wk); 5491 if (!LIST_EMPTY(&oldadp->ad_newdirblk)) 5492 panic("allocdirect_merge: extra newdirblk"); 5493 WORKLIST_INSERT(&newadp->ad_newdirblk, wk); 5494 } 5495 TAILQ_REMOVE(adphead, oldadp, ad_next); 5496 /* 5497 * We need to move any journal dependencies over to the freefrag 5498 * that releases this block if it exists. Otherwise we are 5499 * extending an existing block and we'll wait until that is 5500 * complete to release the journal space and extend the 5501 * new journal to cover this old space as well. 5502 */ 5503 if (freefrag == NULL) { 5504 if (oldadp->ad_newblkno != newadp->ad_newblkno) 5505 panic("allocdirect_merge: %jd != %jd", 5506 oldadp->ad_newblkno, newadp->ad_newblkno); 5507 newadp->ad_block.nb_jnewblk = (struct jnewblk *) 5508 jnewblk_merge(&newadp->ad_block.nb_jnewblk->jn_list, 5509 &oldadp->ad_block.nb_jnewblk->jn_list, 5510 &newadp->ad_block.nb_jwork); 5511 oldadp->ad_block.nb_jnewblk = NULL; 5512 cancel_newblk(&oldadp->ad_block, NULL, 5513 &newadp->ad_block.nb_jwork); 5514 } else { 5515 wk = (struct worklist *) cancel_newblk(&oldadp->ad_block, 5516 &freefrag->ff_list, &freefrag->ff_jwork); 5517 freefrag->ff_jdep = jnewblk_merge(freefrag->ff_jdep, wk, 5518 &freefrag->ff_jwork); 5519 } 5520 free_newblk(&oldadp->ad_block); 5521} 5522 5523/* 5524 * Allocate a jfreefrag structure to journal a single block free. 5525 */ 5526static struct jfreefrag * 5527newjfreefrag(freefrag, ip, blkno, size, lbn) 5528 struct freefrag *freefrag; 5529 struct inode *ip; 5530 ufs2_daddr_t blkno; 5531 long size; 5532 ufs_lbn_t lbn; 5533{ 5534 struct jfreefrag *jfreefrag; 5535 struct fs *fs; 5536 5537 fs = ip->i_fs; 5538 jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG, 5539 M_SOFTDEP_FLAGS); 5540 workitem_alloc(&jfreefrag->fr_list, D_JFREEFRAG, UFSTOVFS(ip->i_ump)); 5541 jfreefrag->fr_jsegdep = newjsegdep(&jfreefrag->fr_list); 5542 jfreefrag->fr_state = ATTACHED | DEPCOMPLETE; 5543 jfreefrag->fr_ino = ip->i_number; 5544 jfreefrag->fr_lbn = lbn; 5545 jfreefrag->fr_blkno = blkno; 5546 jfreefrag->fr_frags = numfrags(fs, size); 5547 jfreefrag->fr_freefrag = freefrag; 5548 5549 return (jfreefrag); 5550} 5551 5552/* 5553 * Allocate a new freefrag structure. 5554 */ 5555static struct freefrag * 5556newfreefrag(ip, blkno, size, lbn) 5557 struct inode *ip; 5558 ufs2_daddr_t blkno; 5559 long size; 5560 ufs_lbn_t lbn; 5561{ 5562 struct freefrag *freefrag; 5563 struct fs *fs; 5564 5565 CTR4(KTR_SUJ, "newfreefrag: ino %d blkno %jd size %ld lbn %jd", 5566 ip->i_number, blkno, size, lbn); 5567 fs = ip->i_fs; 5568 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag) 5569 panic("newfreefrag: frag size"); 5570 freefrag = malloc(sizeof(struct freefrag), 5571 M_FREEFRAG, M_SOFTDEP_FLAGS); 5572 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ip->i_ump)); 5573 freefrag->ff_state = ATTACHED; 5574 LIST_INIT(&freefrag->ff_jwork); 5575 freefrag->ff_inum = ip->i_number; 5576 freefrag->ff_vtype = ITOV(ip)->v_type; 5577 freefrag->ff_blkno = blkno; 5578 freefrag->ff_fragsize = size; 5579 5580 if (MOUNTEDSUJ(UFSTOVFS(ip->i_ump))) { 5581 freefrag->ff_jdep = (struct worklist *) 5582 newjfreefrag(freefrag, ip, blkno, size, lbn); 5583 } else { 5584 freefrag->ff_state |= DEPCOMPLETE; 5585 freefrag->ff_jdep = NULL; 5586 } 5587 5588 return (freefrag); 5589} 5590 5591/* 5592 * This workitem de-allocates fragments that were replaced during 5593 * file block allocation. 5594 */ 5595static void 5596handle_workitem_freefrag(freefrag) 5597 struct freefrag *freefrag; 5598{ 5599 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp); 5600 struct workhead wkhd; 5601 5602 CTR3(KTR_SUJ, 5603 "handle_workitem_freefrag: ino %d blkno %jd size %ld", 5604 freefrag->ff_inum, freefrag->ff_blkno, freefrag->ff_fragsize); 5605 /* 5606 * It would be illegal to add new completion items to the 5607 * freefrag after it was schedule to be done so it must be 5608 * safe to modify the list head here. 5609 */ 5610 LIST_INIT(&wkhd); 5611 ACQUIRE_LOCK(ump); 5612 LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list); 5613 /* 5614 * If the journal has not been written we must cancel it here. 5615 */ 5616 if (freefrag->ff_jdep) { 5617 if (freefrag->ff_jdep->wk_type != D_JNEWBLK) 5618 panic("handle_workitem_freefrag: Unexpected type %d\n", 5619 freefrag->ff_jdep->wk_type); 5620 cancel_jnewblk(WK_JNEWBLK(freefrag->ff_jdep), &wkhd); 5621 } 5622 FREE_LOCK(ump); 5623 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno, 5624 freefrag->ff_fragsize, freefrag->ff_inum, freefrag->ff_vtype, &wkhd); 5625 ACQUIRE_LOCK(ump); 5626 WORKITEM_FREE(freefrag, D_FREEFRAG); 5627 FREE_LOCK(ump); 5628} 5629 5630/* 5631 * Set up a dependency structure for an external attributes data block. 5632 * This routine follows much of the structure of softdep_setup_allocdirect. 5633 * See the description of softdep_setup_allocdirect above for details. 5634 */ 5635void 5636softdep_setup_allocext(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 5637 struct inode *ip; 5638 ufs_lbn_t off; 5639 ufs2_daddr_t newblkno; 5640 ufs2_daddr_t oldblkno; 5641 long newsize; 5642 long oldsize; 5643 struct buf *bp; 5644{ 5645 struct allocdirect *adp, *oldadp; 5646 struct allocdirectlst *adphead; 5647 struct freefrag *freefrag; 5648 struct inodedep *inodedep; 5649 struct jnewblk *jnewblk; 5650 struct newblk *newblk; 5651 struct mount *mp; 5652 ufs_lbn_t lbn; 5653 5654 mp = UFSTOVFS(ip->i_ump); 5655 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5656 ("softdep_setup_allocext called on non-softdep filesystem")); 5657 KASSERT(off < NXADDR, ("softdep_setup_allocext: lbn %lld > NXADDR", 5658 (long long)off)); 5659 5660 lbn = bp->b_lblkno; 5661 if (oldblkno && oldblkno != newblkno) 5662 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn); 5663 else 5664 freefrag = NULL; 5665 5666 ACQUIRE_LOCK(ip->i_ump); 5667 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5668 panic("softdep_setup_allocext: lost block"); 5669 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5670 ("softdep_setup_allocext: newblk already initialized")); 5671 /* 5672 * Convert the newblk to an allocdirect. 5673 */ 5674 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT); 5675 adp = (struct allocdirect *)newblk; 5676 newblk->nb_freefrag = freefrag; 5677 adp->ad_offset = off; 5678 adp->ad_oldblkno = oldblkno; 5679 adp->ad_newsize = newsize; 5680 adp->ad_oldsize = oldsize; 5681 adp->ad_state |= EXTDATA; 5682 5683 /* 5684 * Finish initializing the journal. 5685 */ 5686 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5687 jnewblk->jn_ino = ip->i_number; 5688 jnewblk->jn_lbn = lbn; 5689 add_to_journal(&jnewblk->jn_list); 5690 } 5691 if (freefrag && freefrag->ff_jdep != NULL && 5692 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5693 add_to_journal(freefrag->ff_jdep); 5694 inodedep_lookup(mp, ip->i_number, DEPALLOC | NODELAY, &inodedep); 5695 adp->ad_inodedep = inodedep; 5696 5697 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5698 /* 5699 * The list of allocdirects must be kept in sorted and ascending 5700 * order so that the rollback routines can quickly determine the 5701 * first uncommitted block (the size of the file stored on disk 5702 * ends at the end of the lowest committed fragment, or if there 5703 * are no fragments, at the end of the highest committed block). 5704 * Since files generally grow, the typical case is that the new 5705 * block is to be added at the end of the list. We speed this 5706 * special case by checking against the last allocdirect in the 5707 * list before laboriously traversing the list looking for the 5708 * insertion point. 5709 */ 5710 adphead = &inodedep->id_newextupdt; 5711 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5712 if (oldadp == NULL || oldadp->ad_offset <= off) { 5713 /* insert at end of list */ 5714 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5715 if (oldadp != NULL && oldadp->ad_offset == off) 5716 allocdirect_merge(adphead, adp, oldadp); 5717 FREE_LOCK(ip->i_ump); 5718 return; 5719 } 5720 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5721 if (oldadp->ad_offset >= off) 5722 break; 5723 } 5724 if (oldadp == NULL) 5725 panic("softdep_setup_allocext: lost entry"); 5726 /* insert in middle of list */ 5727 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5728 if (oldadp->ad_offset == off) 5729 allocdirect_merge(adphead, adp, oldadp); 5730 FREE_LOCK(ip->i_ump); 5731} 5732 5733/* 5734 * Indirect block allocation dependencies. 5735 * 5736 * The same dependencies that exist for a direct block also exist when 5737 * a new block is allocated and pointed to by an entry in a block of 5738 * indirect pointers. The undo/redo states described above are also 5739 * used here. Because an indirect block contains many pointers that 5740 * may have dependencies, a second copy of the entire in-memory indirect 5741 * block is kept. The buffer cache copy is always completely up-to-date. 5742 * The second copy, which is used only as a source for disk writes, 5743 * contains only the safe pointers (i.e., those that have no remaining 5744 * update dependencies). The second copy is freed when all pointers 5745 * are safe. The cache is not allowed to replace indirect blocks with 5746 * pending update dependencies. If a buffer containing an indirect 5747 * block with dependencies is written, these routines will mark it 5748 * dirty again. It can only be successfully written once all the 5749 * dependencies are removed. The ffs_fsync routine in conjunction with 5750 * softdep_sync_metadata work together to get all the dependencies 5751 * removed so that a file can be successfully written to disk. Three 5752 * procedures are used when setting up indirect block pointer 5753 * dependencies. The division is necessary because of the organization 5754 * of the "balloc" routine and because of the distinction between file 5755 * pages and file metadata blocks. 5756 */ 5757 5758/* 5759 * Allocate a new allocindir structure. 5760 */ 5761static struct allocindir * 5762newallocindir(ip, ptrno, newblkno, oldblkno, lbn) 5763 struct inode *ip; /* inode for file being extended */ 5764 int ptrno; /* offset of pointer in indirect block */ 5765 ufs2_daddr_t newblkno; /* disk block number being added */ 5766 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 5767 ufs_lbn_t lbn; 5768{ 5769 struct newblk *newblk; 5770 struct allocindir *aip; 5771 struct freefrag *freefrag; 5772 struct jnewblk *jnewblk; 5773 5774 if (oldblkno) 5775 freefrag = newfreefrag(ip, oldblkno, ip->i_fs->fs_bsize, lbn); 5776 else 5777 freefrag = NULL; 5778 ACQUIRE_LOCK(ip->i_ump); 5779 if (newblk_lookup(UFSTOVFS(ip->i_ump), newblkno, 0, &newblk) == 0) 5780 panic("new_allocindir: lost block"); 5781 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5782 ("newallocindir: newblk already initialized")); 5783 WORKITEM_REASSIGN(newblk, D_ALLOCINDIR); 5784 newblk->nb_freefrag = freefrag; 5785 aip = (struct allocindir *)newblk; 5786 aip->ai_offset = ptrno; 5787 aip->ai_oldblkno = oldblkno; 5788 aip->ai_lbn = lbn; 5789 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5790 jnewblk->jn_ino = ip->i_number; 5791 jnewblk->jn_lbn = lbn; 5792 add_to_journal(&jnewblk->jn_list); 5793 } 5794 if (freefrag && freefrag->ff_jdep != NULL && 5795 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5796 add_to_journal(freefrag->ff_jdep); 5797 return (aip); 5798} 5799 5800/* 5801 * Called just before setting an indirect block pointer 5802 * to a newly allocated file page. 5803 */ 5804void 5805softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 5806 struct inode *ip; /* inode for file being extended */ 5807 ufs_lbn_t lbn; /* allocated block number within file */ 5808 struct buf *bp; /* buffer with indirect blk referencing page */ 5809 int ptrno; /* offset of pointer in indirect block */ 5810 ufs2_daddr_t newblkno; /* disk block number being added */ 5811 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 5812 struct buf *nbp; /* buffer holding allocated page */ 5813{ 5814 struct inodedep *inodedep; 5815 struct freefrag *freefrag; 5816 struct allocindir *aip; 5817 struct pagedep *pagedep; 5818 struct mount *mp; 5819 int dflags; 5820 5821 mp = UFSTOVFS(ip->i_ump); 5822 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5823 ("softdep_setup_allocindir_page called on non-softdep filesystem")); 5824 KASSERT(lbn == nbp->b_lblkno, 5825 ("softdep_setup_allocindir_page: lbn %jd != lblkno %jd", 5826 lbn, bp->b_lblkno)); 5827 CTR4(KTR_SUJ, 5828 "softdep_setup_allocindir_page: ino %d blkno %jd oldblkno %jd " 5829 "lbn %jd", ip->i_number, newblkno, oldblkno, lbn); 5830 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page"); 5831 aip = newallocindir(ip, ptrno, newblkno, oldblkno, lbn); 5832 dflags = DEPALLOC; 5833 if (IS_SNAPSHOT(ip)) 5834 dflags |= NODELAY; 5835 (void) inodedep_lookup(mp, ip->i_number, dflags, &inodedep); 5836 /* 5837 * If we are allocating a directory page, then we must 5838 * allocate an associated pagedep to track additions and 5839 * deletions. 5840 */ 5841 if ((ip->i_mode & IFMT) == IFDIR) 5842 pagedep_lookup(mp, nbp, ip->i_number, lbn, DEPALLOC, &pagedep); 5843 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 5844 freefrag = setup_allocindir_phase2(bp, ip, inodedep, aip, lbn); 5845 FREE_LOCK(ip->i_ump); 5846 if (freefrag) 5847 handle_workitem_freefrag(freefrag); 5848} 5849 5850/* 5851 * Called just before setting an indirect block pointer to a 5852 * newly allocated indirect block. 5853 */ 5854void 5855softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 5856 struct buf *nbp; /* newly allocated indirect block */ 5857 struct inode *ip; /* inode for file being extended */ 5858 struct buf *bp; /* indirect block referencing allocated block */ 5859 int ptrno; /* offset of pointer in indirect block */ 5860 ufs2_daddr_t newblkno; /* disk block number being added */ 5861{ 5862 struct inodedep *inodedep; 5863 struct allocindir *aip; 5864 ufs_lbn_t lbn; 5865 int dflags; 5866 5867 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0, 5868 ("softdep_setup_allocindir_meta called on non-softdep filesystem")); 5869 CTR3(KTR_SUJ, 5870 "softdep_setup_allocindir_meta: ino %d blkno %jd ptrno %d", 5871 ip->i_number, newblkno, ptrno); 5872 lbn = nbp->b_lblkno; 5873 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta"); 5874 aip = newallocindir(ip, ptrno, newblkno, 0, lbn); 5875 dflags = DEPALLOC; 5876 if (IS_SNAPSHOT(ip)) 5877 dflags |= NODELAY; 5878 inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, dflags, &inodedep); 5879 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 5880 if (setup_allocindir_phase2(bp, ip, inodedep, aip, lbn)) 5881 panic("softdep_setup_allocindir_meta: Block already existed"); 5882 FREE_LOCK(ip->i_ump); 5883} 5884 5885static void 5886indirdep_complete(indirdep) 5887 struct indirdep *indirdep; 5888{ 5889 struct allocindir *aip; 5890 5891 LIST_REMOVE(indirdep, ir_next); 5892 indirdep->ir_state |= DEPCOMPLETE; 5893 5894 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) { 5895 LIST_REMOVE(aip, ai_next); 5896 free_newblk(&aip->ai_block); 5897 } 5898 /* 5899 * If this indirdep is not attached to a buf it was simply waiting 5900 * on completion to clear completehd. free_indirdep() asserts 5901 * that nothing is dangling. 5902 */ 5903 if ((indirdep->ir_state & ONWORKLIST) == 0) 5904 free_indirdep(indirdep); 5905} 5906 5907static struct indirdep * 5908indirdep_lookup(mp, ip, bp) 5909 struct mount *mp; 5910 struct inode *ip; 5911 struct buf *bp; 5912{ 5913 struct indirdep *indirdep, *newindirdep; 5914 struct newblk *newblk; 5915 struct ufsmount *ump; 5916 struct worklist *wk; 5917 struct fs *fs; 5918 ufs2_daddr_t blkno; 5919 5920 ump = VFSTOUFS(mp); 5921 LOCK_OWNED(ump); 5922 indirdep = NULL; 5923 newindirdep = NULL; 5924 fs = ip->i_fs; 5925 for (;;) { 5926 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 5927 if (wk->wk_type != D_INDIRDEP) 5928 continue; 5929 indirdep = WK_INDIRDEP(wk); 5930 break; 5931 } 5932 /* Found on the buffer worklist, no new structure to free. */ 5933 if (indirdep != NULL && newindirdep == NULL) 5934 return (indirdep); 5935 if (indirdep != NULL && newindirdep != NULL) 5936 panic("indirdep_lookup: simultaneous create"); 5937 /* None found on the buffer and a new structure is ready. */ 5938 if (indirdep == NULL && newindirdep != NULL) 5939 break; 5940 /* None found and no new structure available. */ 5941 FREE_LOCK(ump); 5942 newindirdep = malloc(sizeof(struct indirdep), 5943 M_INDIRDEP, M_SOFTDEP_FLAGS); 5944 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, mp); 5945 newindirdep->ir_state = ATTACHED; 5946 if (ip->i_ump->um_fstype == UFS1) 5947 newindirdep->ir_state |= UFS1FMT; 5948 TAILQ_INIT(&newindirdep->ir_trunc); 5949 newindirdep->ir_saveddata = NULL; 5950 LIST_INIT(&newindirdep->ir_deplisthd); 5951 LIST_INIT(&newindirdep->ir_donehd); 5952 LIST_INIT(&newindirdep->ir_writehd); 5953 LIST_INIT(&newindirdep->ir_completehd); 5954 if (bp->b_blkno == bp->b_lblkno) { 5955 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp, 5956 NULL, NULL); 5957 bp->b_blkno = blkno; 5958 } 5959 newindirdep->ir_freeblks = NULL; 5960 newindirdep->ir_savebp = 5961 getblk(ip->i_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0); 5962 newindirdep->ir_bp = bp; 5963 BUF_KERNPROC(newindirdep->ir_savebp); 5964 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount); 5965 ACQUIRE_LOCK(ump); 5966 } 5967 indirdep = newindirdep; 5968 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list); 5969 /* 5970 * If the block is not yet allocated we don't set DEPCOMPLETE so 5971 * that we don't free dependencies until the pointers are valid. 5972 * This could search b_dep for D_ALLOCDIRECT/D_ALLOCINDIR rather 5973 * than using the hash. 5974 */ 5975 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk)) 5976 LIST_INSERT_HEAD(&newblk->nb_indirdeps, indirdep, ir_next); 5977 else 5978 indirdep->ir_state |= DEPCOMPLETE; 5979 return (indirdep); 5980} 5981 5982/* 5983 * Called to finish the allocation of the "aip" allocated 5984 * by one of the two routines above. 5985 */ 5986static struct freefrag * 5987setup_allocindir_phase2(bp, ip, inodedep, aip, lbn) 5988 struct buf *bp; /* in-memory copy of the indirect block */ 5989 struct inode *ip; /* inode for file being extended */ 5990 struct inodedep *inodedep; /* Inodedep for ip */ 5991 struct allocindir *aip; /* allocindir allocated by the above routines */ 5992 ufs_lbn_t lbn; /* Logical block number for this block. */ 5993{ 5994 struct fs *fs; 5995 struct indirdep *indirdep; 5996 struct allocindir *oldaip; 5997 struct freefrag *freefrag; 5998 struct mount *mp; 5999 6000 LOCK_OWNED(ip->i_ump); 6001 mp = UFSTOVFS(ip->i_ump); 6002 fs = ip->i_fs; 6003 if (bp->b_lblkno >= 0) 6004 panic("setup_allocindir_phase2: not indir blk"); 6005 KASSERT(aip->ai_offset >= 0 && aip->ai_offset < NINDIR(fs), 6006 ("setup_allocindir_phase2: Bad offset %d", aip->ai_offset)); 6007 indirdep = indirdep_lookup(mp, ip, bp); 6008 KASSERT(indirdep->ir_savebp != NULL, 6009 ("setup_allocindir_phase2 NULL ir_savebp")); 6010 aip->ai_indirdep = indirdep; 6011 /* 6012 * Check for an unwritten dependency for this indirect offset. If 6013 * there is, merge the old dependency into the new one. This happens 6014 * as a result of reallocblk only. 6015 */ 6016 freefrag = NULL; 6017 if (aip->ai_oldblkno != 0) { 6018 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) { 6019 if (oldaip->ai_offset == aip->ai_offset) { 6020 freefrag = allocindir_merge(aip, oldaip); 6021 goto done; 6022 } 6023 } 6024 LIST_FOREACH(oldaip, &indirdep->ir_donehd, ai_next) { 6025 if (oldaip->ai_offset == aip->ai_offset) { 6026 freefrag = allocindir_merge(aip, oldaip); 6027 goto done; 6028 } 6029 } 6030 } 6031done: 6032 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next); 6033 return (freefrag); 6034} 6035 6036/* 6037 * Merge two allocindirs which refer to the same block. Move newblock 6038 * dependencies and setup the freefrags appropriately. 6039 */ 6040static struct freefrag * 6041allocindir_merge(aip, oldaip) 6042 struct allocindir *aip; 6043 struct allocindir *oldaip; 6044{ 6045 struct freefrag *freefrag; 6046 struct worklist *wk; 6047 6048 if (oldaip->ai_newblkno != aip->ai_oldblkno) 6049 panic("allocindir_merge: blkno"); 6050 aip->ai_oldblkno = oldaip->ai_oldblkno; 6051 freefrag = aip->ai_freefrag; 6052 aip->ai_freefrag = oldaip->ai_freefrag; 6053 oldaip->ai_freefrag = NULL; 6054 KASSERT(freefrag != NULL, ("setup_allocindir_phase2: No freefrag")); 6055 /* 6056 * If we are tracking a new directory-block allocation, 6057 * move it from the old allocindir to the new allocindir. 6058 */ 6059 if ((wk = LIST_FIRST(&oldaip->ai_newdirblk)) != NULL) { 6060 WORKLIST_REMOVE(wk); 6061 if (!LIST_EMPTY(&oldaip->ai_newdirblk)) 6062 panic("allocindir_merge: extra newdirblk"); 6063 WORKLIST_INSERT(&aip->ai_newdirblk, wk); 6064 } 6065 /* 6066 * We can skip journaling for this freefrag and just complete 6067 * any pending journal work for the allocindir that is being 6068 * removed after the freefrag completes. 6069 */ 6070 if (freefrag->ff_jdep) 6071 cancel_jfreefrag(WK_JFREEFRAG(freefrag->ff_jdep)); 6072 LIST_REMOVE(oldaip, ai_next); 6073 freefrag->ff_jdep = (struct worklist *)cancel_newblk(&oldaip->ai_block, 6074 &freefrag->ff_list, &freefrag->ff_jwork); 6075 free_newblk(&oldaip->ai_block); 6076 6077 return (freefrag); 6078} 6079 6080static inline void 6081setup_freedirect(freeblks, ip, i, needj) 6082 struct freeblks *freeblks; 6083 struct inode *ip; 6084 int i; 6085 int needj; 6086{ 6087 ufs2_daddr_t blkno; 6088 int frags; 6089 6090 blkno = DIP(ip, i_db[i]); 6091 if (blkno == 0) 6092 return; 6093 DIP_SET(ip, i_db[i], 0); 6094 frags = sblksize(ip->i_fs, ip->i_size, i); 6095 frags = numfrags(ip->i_fs, frags); 6096 newfreework(ip->i_ump, freeblks, NULL, i, blkno, frags, 0, needj); 6097} 6098 6099static inline void 6100setup_freeext(freeblks, ip, i, needj) 6101 struct freeblks *freeblks; 6102 struct inode *ip; 6103 int i; 6104 int needj; 6105{ 6106 ufs2_daddr_t blkno; 6107 int frags; 6108 6109 blkno = ip->i_din2->di_extb[i]; 6110 if (blkno == 0) 6111 return; 6112 ip->i_din2->di_extb[i] = 0; 6113 frags = sblksize(ip->i_fs, ip->i_din2->di_extsize, i); 6114 frags = numfrags(ip->i_fs, frags); 6115 newfreework(ip->i_ump, freeblks, NULL, -1 - i, blkno, frags, 0, needj); 6116} 6117 6118static inline void 6119setup_freeindir(freeblks, ip, i, lbn, needj) 6120 struct freeblks *freeblks; 6121 struct inode *ip; 6122 int i; 6123 ufs_lbn_t lbn; 6124 int needj; 6125{ 6126 ufs2_daddr_t blkno; 6127 6128 blkno = DIP(ip, i_ib[i]); 6129 if (blkno == 0) 6130 return; 6131 DIP_SET(ip, i_ib[i], 0); 6132 newfreework(ip->i_ump, freeblks, NULL, lbn, blkno, ip->i_fs->fs_frag, 6133 0, needj); 6134} 6135 6136static inline struct freeblks * 6137newfreeblks(mp, ip) 6138 struct mount *mp; 6139 struct inode *ip; 6140{ 6141 struct freeblks *freeblks; 6142 6143 freeblks = malloc(sizeof(struct freeblks), 6144 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO); 6145 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp); 6146 LIST_INIT(&freeblks->fb_jblkdephd); 6147 LIST_INIT(&freeblks->fb_jwork); 6148 freeblks->fb_ref = 0; 6149 freeblks->fb_cgwait = 0; 6150 freeblks->fb_state = ATTACHED; 6151 freeblks->fb_uid = ip->i_uid; 6152 freeblks->fb_inum = ip->i_number; 6153 freeblks->fb_vtype = ITOV(ip)->v_type; 6154 freeblks->fb_modrev = DIP(ip, i_modrev); 6155 freeblks->fb_devvp = ip->i_devvp; 6156 freeblks->fb_chkcnt = 0; 6157 freeblks->fb_len = 0; 6158 6159 return (freeblks); 6160} 6161 6162static void 6163trunc_indirdep(indirdep, freeblks, bp, off) 6164 struct indirdep *indirdep; 6165 struct freeblks *freeblks; 6166 struct buf *bp; 6167 int off; 6168{ 6169 struct allocindir *aip, *aipn; 6170 6171 /* 6172 * The first set of allocindirs won't be in savedbp. 6173 */ 6174 LIST_FOREACH_SAFE(aip, &indirdep->ir_deplisthd, ai_next, aipn) 6175 if (aip->ai_offset > off) 6176 cancel_allocindir(aip, bp, freeblks, 1); 6177 LIST_FOREACH_SAFE(aip, &indirdep->ir_donehd, ai_next, aipn) 6178 if (aip->ai_offset > off) 6179 cancel_allocindir(aip, bp, freeblks, 1); 6180 /* 6181 * These will exist in savedbp. 6182 */ 6183 LIST_FOREACH_SAFE(aip, &indirdep->ir_writehd, ai_next, aipn) 6184 if (aip->ai_offset > off) 6185 cancel_allocindir(aip, NULL, freeblks, 0); 6186 LIST_FOREACH_SAFE(aip, &indirdep->ir_completehd, ai_next, aipn) 6187 if (aip->ai_offset > off) 6188 cancel_allocindir(aip, NULL, freeblks, 0); 6189} 6190 6191/* 6192 * Follow the chain of indirects down to lastlbn creating a freework 6193 * structure for each. This will be used to start indir_trunc() at 6194 * the right offset and create the journal records for the parrtial 6195 * truncation. A second step will handle the truncated dependencies. 6196 */ 6197static int 6198setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno) 6199 struct freeblks *freeblks; 6200 struct inode *ip; 6201 ufs_lbn_t lbn; 6202 ufs_lbn_t lastlbn; 6203 ufs2_daddr_t blkno; 6204{ 6205 struct indirdep *indirdep; 6206 struct indirdep *indirn; 6207 struct freework *freework; 6208 struct newblk *newblk; 6209 struct mount *mp; 6210 struct buf *bp; 6211 uint8_t *start; 6212 uint8_t *end; 6213 ufs_lbn_t lbnadd; 6214 int level; 6215 int error; 6216 int off; 6217 6218 6219 freework = NULL; 6220 if (blkno == 0) 6221 return (0); 6222 mp = freeblks->fb_list.wk_mp; 6223 bp = getblk(ITOV(ip), lbn, mp->mnt_stat.f_iosize, 0, 0, 0); 6224 if ((bp->b_flags & B_CACHE) == 0) { 6225 bp->b_blkno = blkptrtodb(VFSTOUFS(mp), blkno); 6226 bp->b_iocmd = BIO_READ; 6227 bp->b_flags &= ~B_INVAL; 6228 bp->b_ioflags &= ~BIO_ERROR; 6229 vfs_busy_pages(bp, 0); 6230 bp->b_iooffset = dbtob(bp->b_blkno); 6231 bstrategy(bp); 6232 curthread->td_ru.ru_inblock++; 6233 error = bufwait(bp); 6234 if (error) { 6235 brelse(bp); 6236 return (error); 6237 } 6238 } 6239 level = lbn_level(lbn); 6240 lbnadd = lbn_offset(ip->i_fs, level); 6241 /* 6242 * Compute the offset of the last block we want to keep. Store 6243 * in the freework the first block we want to completely free. 6244 */ 6245 off = (lastlbn - -(lbn + level)) / lbnadd; 6246 if (off + 1 == NINDIR(ip->i_fs)) 6247 goto nowork; 6248 freework = newfreework(ip->i_ump, freeblks, NULL, lbn, blkno, 0, off+1, 6249 0); 6250 /* 6251 * Link the freework into the indirdep. This will prevent any new 6252 * allocations from proceeding until we are finished with the 6253 * truncate and the block is written. 6254 */ 6255 ACQUIRE_LOCK(ip->i_ump); 6256 indirdep = indirdep_lookup(mp, ip, bp); 6257 if (indirdep->ir_freeblks) 6258 panic("setup_trunc_indir: indirdep already truncated."); 6259 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, freework, fw_next); 6260 freework->fw_indir = indirdep; 6261 /* 6262 * Cancel any allocindirs that will not make it to disk. 6263 * We have to do this for all copies of the indirdep that 6264 * live on this newblk. 6265 */ 6266 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 6267 newblk_lookup(mp, dbtofsb(ip->i_fs, bp->b_blkno), 0, &newblk); 6268 LIST_FOREACH(indirn, &newblk->nb_indirdeps, ir_next) 6269 trunc_indirdep(indirn, freeblks, bp, off); 6270 } else 6271 trunc_indirdep(indirdep, freeblks, bp, off); 6272 FREE_LOCK(ip->i_ump); 6273 /* 6274 * Creation is protected by the buf lock. The saveddata is only 6275 * needed if a full truncation follows a partial truncation but it 6276 * is difficult to allocate in that case so we fetch it anyway. 6277 */ 6278 if (indirdep->ir_saveddata == NULL) 6279 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 6280 M_SOFTDEP_FLAGS); 6281nowork: 6282 /* Fetch the blkno of the child and the zero start offset. */ 6283 if (ip->i_ump->um_fstype == UFS1) { 6284 blkno = ((ufs1_daddr_t *)bp->b_data)[off]; 6285 start = (uint8_t *)&((ufs1_daddr_t *)bp->b_data)[off+1]; 6286 } else { 6287 blkno = ((ufs2_daddr_t *)bp->b_data)[off]; 6288 start = (uint8_t *)&((ufs2_daddr_t *)bp->b_data)[off+1]; 6289 } 6290 if (freework) { 6291 /* Zero the truncated pointers. */ 6292 end = bp->b_data + bp->b_bcount; 6293 bzero(start, end - start); 6294 bdwrite(bp); 6295 } else 6296 bqrelse(bp); 6297 if (level == 0) 6298 return (0); 6299 lbn++; /* adjust level */ 6300 lbn -= (off * lbnadd); 6301 return setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno); 6302} 6303 6304/* 6305 * Complete the partial truncation of an indirect block setup by 6306 * setup_trunc_indir(). This zeros the truncated pointers in the saved 6307 * copy and writes them to disk before the freeblks is allowed to complete. 6308 */ 6309static void 6310complete_trunc_indir(freework) 6311 struct freework *freework; 6312{ 6313 struct freework *fwn; 6314 struct indirdep *indirdep; 6315 struct ufsmount *ump; 6316 struct buf *bp; 6317 uintptr_t start; 6318 int count; 6319 6320 ump = VFSTOUFS(freework->fw_list.wk_mp); 6321 LOCK_OWNED(ump); 6322 indirdep = freework->fw_indir; 6323 for (;;) { 6324 bp = indirdep->ir_bp; 6325 /* See if the block was discarded. */ 6326 if (bp == NULL) 6327 break; 6328 /* Inline part of getdirtybuf(). We dont want bremfree. */ 6329 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) 6330 break; 6331 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 6332 LOCK_PTR(ump)) == 0) 6333 BUF_UNLOCK(bp); 6334 ACQUIRE_LOCK(ump); 6335 } 6336 freework->fw_state |= DEPCOMPLETE; 6337 TAILQ_REMOVE(&indirdep->ir_trunc, freework, fw_next); 6338 /* 6339 * Zero the pointers in the saved copy. 6340 */ 6341 if (indirdep->ir_state & UFS1FMT) 6342 start = sizeof(ufs1_daddr_t); 6343 else 6344 start = sizeof(ufs2_daddr_t); 6345 start *= freework->fw_start; 6346 count = indirdep->ir_savebp->b_bcount - start; 6347 start += (uintptr_t)indirdep->ir_savebp->b_data; 6348 bzero((char *)start, count); 6349 /* 6350 * We need to start the next truncation in the list if it has not 6351 * been started yet. 6352 */ 6353 fwn = TAILQ_FIRST(&indirdep->ir_trunc); 6354 if (fwn != NULL) { 6355 if (fwn->fw_freeblks == indirdep->ir_freeblks) 6356 TAILQ_REMOVE(&indirdep->ir_trunc, fwn, fw_next); 6357 if ((fwn->fw_state & ONWORKLIST) == 0) 6358 freework_enqueue(fwn); 6359 } 6360 /* 6361 * If bp is NULL the block was fully truncated, restore 6362 * the saved block list otherwise free it if it is no 6363 * longer needed. 6364 */ 6365 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 6366 if (bp == NULL) 6367 bcopy(indirdep->ir_saveddata, 6368 indirdep->ir_savebp->b_data, 6369 indirdep->ir_savebp->b_bcount); 6370 free(indirdep->ir_saveddata, M_INDIRDEP); 6371 indirdep->ir_saveddata = NULL; 6372 } 6373 /* 6374 * When bp is NULL there is a full truncation pending. We 6375 * must wait for this full truncation to be journaled before 6376 * we can release this freework because the disk pointers will 6377 * never be written as zero. 6378 */ 6379 if (bp == NULL) { 6380 if (LIST_EMPTY(&indirdep->ir_freeblks->fb_jblkdephd)) 6381 handle_written_freework(freework); 6382 else 6383 WORKLIST_INSERT(&indirdep->ir_freeblks->fb_freeworkhd, 6384 &freework->fw_list); 6385 } else { 6386 /* Complete when the real copy is written. */ 6387 WORKLIST_INSERT(&bp->b_dep, &freework->fw_list); 6388 BUF_UNLOCK(bp); 6389 } 6390} 6391 6392/* 6393 * Calculate the number of blocks we are going to release where datablocks 6394 * is the current total and length is the new file size. 6395 */ 6396static ufs2_daddr_t 6397blkcount(fs, datablocks, length) 6398 struct fs *fs; 6399 ufs2_daddr_t datablocks; 6400 off_t length; 6401{ 6402 off_t totblks, numblks; 6403 6404 totblks = 0; 6405 numblks = howmany(length, fs->fs_bsize); 6406 if (numblks <= NDADDR) { 6407 totblks = howmany(length, fs->fs_fsize); 6408 goto out; 6409 } 6410 totblks = blkstofrags(fs, numblks); 6411 numblks -= NDADDR; 6412 /* 6413 * Count all single, then double, then triple indirects required. 6414 * Subtracting one indirects worth of blocks for each pass 6415 * acknowledges one of each pointed to by the inode. 6416 */ 6417 for (;;) { 6418 totblks += blkstofrags(fs, howmany(numblks, NINDIR(fs))); 6419 numblks -= NINDIR(fs); 6420 if (numblks <= 0) 6421 break; 6422 numblks = howmany(numblks, NINDIR(fs)); 6423 } 6424out: 6425 totblks = fsbtodb(fs, totblks); 6426 /* 6427 * Handle sparse files. We can't reclaim more blocks than the inode 6428 * references. We will correct it later in handle_complete_freeblks() 6429 * when we know the real count. 6430 */ 6431 if (totblks > datablocks) 6432 return (0); 6433 return (datablocks - totblks); 6434} 6435 6436/* 6437 * Handle freeblocks for journaled softupdate filesystems. 6438 * 6439 * Contrary to normal softupdates, we must preserve the block pointers in 6440 * indirects until their subordinates are free. This is to avoid journaling 6441 * every block that is freed which may consume more space than the journal 6442 * itself. The recovery program will see the free block journals at the 6443 * base of the truncated area and traverse them to reclaim space. The 6444 * pointers in the inode may be cleared immediately after the journal 6445 * records are written because each direct and indirect pointer in the 6446 * inode is recorded in a journal. This permits full truncation to proceed 6447 * asynchronously. The write order is journal -> inode -> cgs -> indirects. 6448 * 6449 * The algorithm is as follows: 6450 * 1) Traverse the in-memory state and create journal entries to release 6451 * the relevant blocks and full indirect trees. 6452 * 2) Traverse the indirect block chain adding partial truncation freework 6453 * records to indirects in the path to lastlbn. The freework will 6454 * prevent new allocation dependencies from being satisfied in this 6455 * indirect until the truncation completes. 6456 * 3) Read and lock the inode block, performing an update with the new size 6457 * and pointers. This prevents truncated data from becoming valid on 6458 * disk through step 4. 6459 * 4) Reap unsatisfied dependencies that are beyond the truncated area, 6460 * eliminate journal work for those records that do not require it. 6461 * 5) Schedule the journal records to be written followed by the inode block. 6462 * 6) Allocate any necessary frags for the end of file. 6463 * 7) Zero any partially truncated blocks. 6464 * 6465 * From this truncation proceeds asynchronously using the freework and 6466 * indir_trunc machinery. The file will not be extended again into a 6467 * partially truncated indirect block until all work is completed but 6468 * the normal dependency mechanism ensures that it is rolled back/forward 6469 * as appropriate. Further truncation may occur without delay and is 6470 * serialized in indir_trunc(). 6471 */ 6472void 6473softdep_journal_freeblocks(ip, cred, length, flags) 6474 struct inode *ip; /* The inode whose length is to be reduced */ 6475 struct ucred *cred; 6476 off_t length; /* The new length for the file */ 6477 int flags; /* IO_EXT and/or IO_NORMAL */ 6478{ 6479 struct freeblks *freeblks, *fbn; 6480 struct worklist *wk, *wkn; 6481 struct inodedep *inodedep; 6482 struct jblkdep *jblkdep; 6483 struct allocdirect *adp, *adpn; 6484 struct ufsmount *ump; 6485 struct fs *fs; 6486 struct buf *bp; 6487 struct vnode *vp; 6488 struct mount *mp; 6489 ufs2_daddr_t extblocks, datablocks; 6490 ufs_lbn_t tmpval, lbn, lastlbn; 6491 int frags, lastoff, iboff, allocblock, needj, dflags, error, i; 6492 6493 fs = ip->i_fs; 6494 ump = ip->i_ump; 6495 mp = UFSTOVFS(ump); 6496 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6497 ("softdep_journal_freeblocks called on non-softdep filesystem")); 6498 vp = ITOV(ip); 6499 needj = 1; 6500 iboff = -1; 6501 allocblock = 0; 6502 extblocks = 0; 6503 datablocks = 0; 6504 frags = 0; 6505 freeblks = newfreeblks(mp, ip); 6506 ACQUIRE_LOCK(ump); 6507 /* 6508 * If we're truncating a removed file that will never be written 6509 * we don't need to journal the block frees. The canceled journals 6510 * for the allocations will suffice. 6511 */ 6512 dflags = DEPALLOC; 6513 if (IS_SNAPSHOT(ip)) 6514 dflags |= NODELAY; 6515 inodedep_lookup(mp, ip->i_number, dflags, &inodedep); 6516 if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED && 6517 length == 0) 6518 needj = 0; 6519 CTR3(KTR_SUJ, "softdep_journal_freeblks: ip %d length %ld needj %d", 6520 ip->i_number, length, needj); 6521 FREE_LOCK(ump); 6522 /* 6523 * Calculate the lbn that we are truncating to. This results in -1 6524 * if we're truncating the 0 bytes. So it is the last lbn we want 6525 * to keep, not the first lbn we want to truncate. 6526 */ 6527 lastlbn = lblkno(fs, length + fs->fs_bsize - 1) - 1; 6528 lastoff = blkoff(fs, length); 6529 /* 6530 * Compute frags we are keeping in lastlbn. 0 means all. 6531 */ 6532 if (lastlbn >= 0 && lastlbn < NDADDR) { 6533 frags = fragroundup(fs, lastoff); 6534 /* adp offset of last valid allocdirect. */ 6535 iboff = lastlbn; 6536 } else if (lastlbn > 0) 6537 iboff = NDADDR; 6538 if (fs->fs_magic == FS_UFS2_MAGIC) 6539 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 6540 /* 6541 * Handle normal data blocks and indirects. This section saves 6542 * values used after the inode update to complete frag and indirect 6543 * truncation. 6544 */ 6545 if ((flags & IO_NORMAL) != 0) { 6546 /* 6547 * Handle truncation of whole direct and indirect blocks. 6548 */ 6549 for (i = iboff + 1; i < NDADDR; i++) 6550 setup_freedirect(freeblks, ip, i, needj); 6551 for (i = 0, tmpval = NINDIR(fs), lbn = NDADDR; i < NIADDR; 6552 i++, lbn += tmpval, tmpval *= NINDIR(fs)) { 6553 /* Release a whole indirect tree. */ 6554 if (lbn > lastlbn) { 6555 setup_freeindir(freeblks, ip, i, -lbn -i, 6556 needj); 6557 continue; 6558 } 6559 iboff = i + NDADDR; 6560 /* 6561 * Traverse partially truncated indirect tree. 6562 */ 6563 if (lbn <= lastlbn && lbn + tmpval - 1 > lastlbn) 6564 setup_trunc_indir(freeblks, ip, -lbn - i, 6565 lastlbn, DIP(ip, i_ib[i])); 6566 } 6567 /* 6568 * Handle partial truncation to a frag boundary. 6569 */ 6570 if (frags) { 6571 ufs2_daddr_t blkno; 6572 long oldfrags; 6573 6574 oldfrags = blksize(fs, ip, lastlbn); 6575 blkno = DIP(ip, i_db[lastlbn]); 6576 if (blkno && oldfrags != frags) { 6577 oldfrags -= frags; 6578 oldfrags = numfrags(ip->i_fs, oldfrags); 6579 blkno += numfrags(ip->i_fs, frags); 6580 newfreework(ump, freeblks, NULL, lastlbn, 6581 blkno, oldfrags, 0, needj); 6582 if (needj) 6583 adjust_newfreework(freeblks, 6584 numfrags(ip->i_fs, frags)); 6585 } else if (blkno == 0) 6586 allocblock = 1; 6587 } 6588 /* 6589 * Add a journal record for partial truncate if we are 6590 * handling indirect blocks. Non-indirects need no extra 6591 * journaling. 6592 */ 6593 if (length != 0 && lastlbn >= NDADDR) { 6594 ip->i_flag |= IN_TRUNCATED; 6595 newjtrunc(freeblks, length, 0); 6596 } 6597 ip->i_size = length; 6598 DIP_SET(ip, i_size, ip->i_size); 6599 datablocks = DIP(ip, i_blocks) - extblocks; 6600 if (length != 0) 6601 datablocks = blkcount(ip->i_fs, datablocks, length); 6602 freeblks->fb_len = length; 6603 } 6604 if ((flags & IO_EXT) != 0) { 6605 for (i = 0; i < NXADDR; i++) 6606 setup_freeext(freeblks, ip, i, needj); 6607 ip->i_din2->di_extsize = 0; 6608 datablocks += extblocks; 6609 } 6610#ifdef QUOTA 6611 /* Reference the quotas in case the block count is wrong in the end. */ 6612 quotaref(vp, freeblks->fb_quota); 6613 (void) chkdq(ip, -datablocks, NOCRED, 0); 6614#endif 6615 freeblks->fb_chkcnt = -datablocks; 6616 UFS_LOCK(ump); 6617 fs->fs_pendingblocks += datablocks; 6618 UFS_UNLOCK(ump); 6619 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 6620 /* 6621 * Handle truncation of incomplete alloc direct dependencies. We 6622 * hold the inode block locked to prevent incomplete dependencies 6623 * from reaching the disk while we are eliminating those that 6624 * have been truncated. This is a partially inlined ffs_update(). 6625 */ 6626 ufs_itimes(vp); 6627 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED); 6628 error = bread(ip->i_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 6629 (int)fs->fs_bsize, cred, &bp); 6630 if (error) { 6631 brelse(bp); 6632 softdep_error("softdep_journal_freeblocks", error); 6633 return; 6634 } 6635 if (bp->b_bufsize == fs->fs_bsize) 6636 bp->b_flags |= B_CLUSTEROK; 6637 softdep_update_inodeblock(ip, bp, 0); 6638 if (ump->um_fstype == UFS1) 6639 *((struct ufs1_dinode *)bp->b_data + 6640 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1; 6641 else 6642 *((struct ufs2_dinode *)bp->b_data + 6643 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2; 6644 ACQUIRE_LOCK(ump); 6645 (void) inodedep_lookup(mp, ip->i_number, dflags, &inodedep); 6646 if ((inodedep->id_state & IOSTARTED) != 0) 6647 panic("softdep_setup_freeblocks: inode busy"); 6648 /* 6649 * Add the freeblks structure to the list of operations that 6650 * must await the zero'ed inode being written to disk. If we 6651 * still have a bitmap dependency (needj), then the inode 6652 * has never been written to disk, so we can process the 6653 * freeblks below once we have deleted the dependencies. 6654 */ 6655 if (needj) 6656 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 6657 else 6658 freeblks->fb_state |= COMPLETE; 6659 if ((flags & IO_NORMAL) != 0) { 6660 TAILQ_FOREACH_SAFE(adp, &inodedep->id_inoupdt, ad_next, adpn) { 6661 if (adp->ad_offset > iboff) 6662 cancel_allocdirect(&inodedep->id_inoupdt, adp, 6663 freeblks); 6664 /* 6665 * Truncate the allocdirect. We could eliminate 6666 * or modify journal records as well. 6667 */ 6668 else if (adp->ad_offset == iboff && frags) 6669 adp->ad_newsize = frags; 6670 } 6671 } 6672 if ((flags & IO_EXT) != 0) 6673 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != 0) 6674 cancel_allocdirect(&inodedep->id_extupdt, adp, 6675 freeblks); 6676 /* 6677 * Scan the bufwait list for newblock dependencies that will never 6678 * make it to disk. 6679 */ 6680 LIST_FOREACH_SAFE(wk, &inodedep->id_bufwait, wk_list, wkn) { 6681 if (wk->wk_type != D_ALLOCDIRECT) 6682 continue; 6683 adp = WK_ALLOCDIRECT(wk); 6684 if (((flags & IO_NORMAL) != 0 && (adp->ad_offset > iboff)) || 6685 ((flags & IO_EXT) != 0 && (adp->ad_state & EXTDATA))) { 6686 cancel_jfreeblk(freeblks, adp->ad_newblkno); 6687 cancel_newblk(WK_NEWBLK(wk), NULL, &freeblks->fb_jwork); 6688 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 6689 } 6690 } 6691 /* 6692 * Add journal work. 6693 */ 6694 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) 6695 add_to_journal(&jblkdep->jb_list); 6696 FREE_LOCK(ump); 6697 bdwrite(bp); 6698 /* 6699 * Truncate dependency structures beyond length. 6700 */ 6701 trunc_dependencies(ip, freeblks, lastlbn, frags, flags); 6702 /* 6703 * This is only set when we need to allocate a fragment because 6704 * none existed at the end of a frag-sized file. It handles only 6705 * allocating a new, zero filled block. 6706 */ 6707 if (allocblock) { 6708 ip->i_size = length - lastoff; 6709 DIP_SET(ip, i_size, ip->i_size); 6710 error = UFS_BALLOC(vp, length - 1, 1, cred, BA_CLRBUF, &bp); 6711 if (error != 0) { 6712 softdep_error("softdep_journal_freeblks", error); 6713 return; 6714 } 6715 ip->i_size = length; 6716 DIP_SET(ip, i_size, length); 6717 ip->i_flag |= IN_CHANGE | IN_UPDATE; 6718 allocbuf(bp, frags); 6719 ffs_update(vp, 0); 6720 bawrite(bp); 6721 } else if (lastoff != 0 && vp->v_type != VDIR) { 6722 int size; 6723 6724 /* 6725 * Zero the end of a truncated frag or block. 6726 */ 6727 size = sblksize(fs, length, lastlbn); 6728 error = bread(vp, lastlbn, size, cred, &bp); 6729 if (error) { 6730 softdep_error("softdep_journal_freeblks", error); 6731 return; 6732 } 6733 bzero((char *)bp->b_data + lastoff, size - lastoff); 6734 bawrite(bp); 6735 6736 } 6737 ACQUIRE_LOCK(ump); 6738 inodedep_lookup(mp, ip->i_number, dflags, &inodedep); 6739 TAILQ_INSERT_TAIL(&inodedep->id_freeblklst, freeblks, fb_next); 6740 freeblks->fb_state |= DEPCOMPLETE | ONDEPLIST; 6741 /* 6742 * We zero earlier truncations so they don't erroneously 6743 * update i_blocks. 6744 */ 6745 if (freeblks->fb_len == 0 && (flags & IO_NORMAL) != 0) 6746 TAILQ_FOREACH(fbn, &inodedep->id_freeblklst, fb_next) 6747 fbn->fb_len = 0; 6748 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE && 6749 LIST_EMPTY(&freeblks->fb_jblkdephd)) 6750 freeblks->fb_state |= INPROGRESS; 6751 else 6752 freeblks = NULL; 6753 FREE_LOCK(ump); 6754 if (freeblks) 6755 handle_workitem_freeblocks(freeblks, 0); 6756 trunc_pages(ip, length, extblocks, flags); 6757 6758} 6759 6760/* 6761 * Flush a JOP_SYNC to the journal. 6762 */ 6763void 6764softdep_journal_fsync(ip) 6765 struct inode *ip; 6766{ 6767 struct jfsync *jfsync; 6768 6769 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0, 6770 ("softdep_journal_fsync called on non-softdep filesystem")); 6771 if ((ip->i_flag & IN_TRUNCATED) == 0) 6772 return; 6773 ip->i_flag &= ~IN_TRUNCATED; 6774 jfsync = malloc(sizeof(*jfsync), M_JFSYNC, M_SOFTDEP_FLAGS | M_ZERO); 6775 workitem_alloc(&jfsync->jfs_list, D_JFSYNC, UFSTOVFS(ip->i_ump)); 6776 jfsync->jfs_size = ip->i_size; 6777 jfsync->jfs_ino = ip->i_number; 6778 ACQUIRE_LOCK(ip->i_ump); 6779 add_to_journal(&jfsync->jfs_list); 6780 jwait(&jfsync->jfs_list, MNT_WAIT); 6781 FREE_LOCK(ip->i_ump); 6782} 6783 6784/* 6785 * Block de-allocation dependencies. 6786 * 6787 * When blocks are de-allocated, the on-disk pointers must be nullified before 6788 * the blocks are made available for use by other files. (The true 6789 * requirement is that old pointers must be nullified before new on-disk 6790 * pointers are set. We chose this slightly more stringent requirement to 6791 * reduce complexity.) Our implementation handles this dependency by updating 6792 * the inode (or indirect block) appropriately but delaying the actual block 6793 * de-allocation (i.e., freemap and free space count manipulation) until 6794 * after the updated versions reach stable storage. After the disk is 6795 * updated, the blocks can be safely de-allocated whenever it is convenient. 6796 * This implementation handles only the common case of reducing a file's 6797 * length to zero. Other cases are handled by the conventional synchronous 6798 * write approach. 6799 * 6800 * The ffs implementation with which we worked double-checks 6801 * the state of the block pointers and file size as it reduces 6802 * a file's length. Some of this code is replicated here in our 6803 * soft updates implementation. The freeblks->fb_chkcnt field is 6804 * used to transfer a part of this information to the procedure 6805 * that eventually de-allocates the blocks. 6806 * 6807 * This routine should be called from the routine that shortens 6808 * a file's length, before the inode's size or block pointers 6809 * are modified. It will save the block pointer information for 6810 * later release and zero the inode so that the calling routine 6811 * can release it. 6812 */ 6813void 6814softdep_setup_freeblocks(ip, length, flags) 6815 struct inode *ip; /* The inode whose length is to be reduced */ 6816 off_t length; /* The new length for the file */ 6817 int flags; /* IO_EXT and/or IO_NORMAL */ 6818{ 6819 struct ufs1_dinode *dp1; 6820 struct ufs2_dinode *dp2; 6821 struct freeblks *freeblks; 6822 struct inodedep *inodedep; 6823 struct allocdirect *adp; 6824 struct ufsmount *ump; 6825 struct buf *bp; 6826 struct fs *fs; 6827 ufs2_daddr_t extblocks, datablocks; 6828 struct mount *mp; 6829 int i, delay, error, dflags; 6830 ufs_lbn_t tmpval; 6831 ufs_lbn_t lbn; 6832 6833 ump = ip->i_ump; 6834 mp = UFSTOVFS(ump); 6835 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6836 ("softdep_setup_freeblocks called on non-softdep filesystem")); 6837 CTR2(KTR_SUJ, "softdep_setup_freeblks: ip %d length %ld", 6838 ip->i_number, length); 6839 KASSERT(length == 0, ("softdep_setup_freeblocks: non-zero length")); 6840 fs = ip->i_fs; 6841 freeblks = newfreeblks(mp, ip); 6842 extblocks = 0; 6843 datablocks = 0; 6844 if (fs->fs_magic == FS_UFS2_MAGIC) 6845 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 6846 if ((flags & IO_NORMAL) != 0) { 6847 for (i = 0; i < NDADDR; i++) 6848 setup_freedirect(freeblks, ip, i, 0); 6849 for (i = 0, tmpval = NINDIR(fs), lbn = NDADDR; i < NIADDR; 6850 i++, lbn += tmpval, tmpval *= NINDIR(fs)) 6851 setup_freeindir(freeblks, ip, i, -lbn -i, 0); 6852 ip->i_size = 0; 6853 DIP_SET(ip, i_size, 0); 6854 datablocks = DIP(ip, i_blocks) - extblocks; 6855 } 6856 if ((flags & IO_EXT) != 0) { 6857 for (i = 0; i < NXADDR; i++) 6858 setup_freeext(freeblks, ip, i, 0); 6859 ip->i_din2->di_extsize = 0; 6860 datablocks += extblocks; 6861 } 6862#ifdef QUOTA 6863 /* Reference the quotas in case the block count is wrong in the end. */ 6864 quotaref(ITOV(ip), freeblks->fb_quota); 6865 (void) chkdq(ip, -datablocks, NOCRED, 0); 6866#endif 6867 freeblks->fb_chkcnt = -datablocks; 6868 UFS_LOCK(ump); 6869 fs->fs_pendingblocks += datablocks; 6870 UFS_UNLOCK(ump); 6871 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 6872 /* 6873 * Push the zero'ed inode to to its disk buffer so that we are free 6874 * to delete its dependencies below. Once the dependencies are gone 6875 * the buffer can be safely released. 6876 */ 6877 if ((error = bread(ip->i_devvp, 6878 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 6879 (int)fs->fs_bsize, NOCRED, &bp)) != 0) { 6880 brelse(bp); 6881 softdep_error("softdep_setup_freeblocks", error); 6882 } 6883 if (ump->um_fstype == UFS1) { 6884 dp1 = ((struct ufs1_dinode *)bp->b_data + 6885 ino_to_fsbo(fs, ip->i_number)); 6886 ip->i_din1->di_freelink = dp1->di_freelink; 6887 *dp1 = *ip->i_din1; 6888 } else { 6889 dp2 = ((struct ufs2_dinode *)bp->b_data + 6890 ino_to_fsbo(fs, ip->i_number)); 6891 ip->i_din2->di_freelink = dp2->di_freelink; 6892 *dp2 = *ip->i_din2; 6893 } 6894 /* 6895 * Find and eliminate any inode dependencies. 6896 */ 6897 ACQUIRE_LOCK(ump); 6898 dflags = DEPALLOC; 6899 if (IS_SNAPSHOT(ip)) 6900 dflags |= NODELAY; 6901 (void) inodedep_lookup(mp, ip->i_number, dflags, &inodedep); 6902 if ((inodedep->id_state & IOSTARTED) != 0) 6903 panic("softdep_setup_freeblocks: inode busy"); 6904 /* 6905 * Add the freeblks structure to the list of operations that 6906 * must await the zero'ed inode being written to disk. If we 6907 * still have a bitmap dependency (delay == 0), then the inode 6908 * has never been written to disk, so we can process the 6909 * freeblks below once we have deleted the dependencies. 6910 */ 6911 delay = (inodedep->id_state & DEPCOMPLETE); 6912 if (delay) 6913 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 6914 else 6915 freeblks->fb_state |= COMPLETE; 6916 /* 6917 * Because the file length has been truncated to zero, any 6918 * pending block allocation dependency structures associated 6919 * with this inode are obsolete and can simply be de-allocated. 6920 * We must first merge the two dependency lists to get rid of 6921 * any duplicate freefrag structures, then purge the merged list. 6922 * If we still have a bitmap dependency, then the inode has never 6923 * been written to disk, so we can free any fragments without delay. 6924 */ 6925 if (flags & IO_NORMAL) { 6926 merge_inode_lists(&inodedep->id_newinoupdt, 6927 &inodedep->id_inoupdt); 6928 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != 0) 6929 cancel_allocdirect(&inodedep->id_inoupdt, adp, 6930 freeblks); 6931 } 6932 if (flags & IO_EXT) { 6933 merge_inode_lists(&inodedep->id_newextupdt, 6934 &inodedep->id_extupdt); 6935 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != 0) 6936 cancel_allocdirect(&inodedep->id_extupdt, adp, 6937 freeblks); 6938 } 6939 FREE_LOCK(ump); 6940 bdwrite(bp); 6941 trunc_dependencies(ip, freeblks, -1, 0, flags); 6942 ACQUIRE_LOCK(ump); 6943 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 6944 (void) free_inodedep(inodedep); 6945 freeblks->fb_state |= DEPCOMPLETE; 6946 /* 6947 * If the inode with zeroed block pointers is now on disk 6948 * we can start freeing blocks. 6949 */ 6950 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 6951 freeblks->fb_state |= INPROGRESS; 6952 else 6953 freeblks = NULL; 6954 FREE_LOCK(ump); 6955 if (freeblks) 6956 handle_workitem_freeblocks(freeblks, 0); 6957 trunc_pages(ip, length, extblocks, flags); 6958} 6959 6960/* 6961 * Eliminate pages from the page cache that back parts of this inode and 6962 * adjust the vnode pager's idea of our size. This prevents stale data 6963 * from hanging around in the page cache. 6964 */ 6965static void 6966trunc_pages(ip, length, extblocks, flags) 6967 struct inode *ip; 6968 off_t length; 6969 ufs2_daddr_t extblocks; 6970 int flags; 6971{ 6972 struct vnode *vp; 6973 struct fs *fs; 6974 ufs_lbn_t lbn; 6975 off_t end, extend; 6976 6977 vp = ITOV(ip); 6978 fs = ip->i_fs; 6979 extend = OFF_TO_IDX(lblktosize(fs, -extblocks)); 6980 if ((flags & IO_EXT) != 0) 6981 vn_pages_remove(vp, extend, 0); 6982 if ((flags & IO_NORMAL) == 0) 6983 return; 6984 BO_LOCK(&vp->v_bufobj); 6985 drain_output(vp); 6986 BO_UNLOCK(&vp->v_bufobj); 6987 /* 6988 * The vnode pager eliminates file pages we eliminate indirects 6989 * below. 6990 */ 6991 vnode_pager_setsize(vp, length); 6992 /* 6993 * Calculate the end based on the last indirect we want to keep. If 6994 * the block extends into indirects we can just use the negative of 6995 * its lbn. Doubles and triples exist at lower numbers so we must 6996 * be careful not to remove those, if they exist. double and triple 6997 * indirect lbns do not overlap with others so it is not important 6998 * to verify how many levels are required. 6999 */ 7000 lbn = lblkno(fs, length); 7001 if (lbn >= NDADDR) { 7002 /* Calculate the virtual lbn of the triple indirect. */ 7003 lbn = -lbn - (NIADDR - 1); 7004 end = OFF_TO_IDX(lblktosize(fs, lbn)); 7005 } else 7006 end = extend; 7007 vn_pages_remove(vp, OFF_TO_IDX(OFF_MAX), end); 7008} 7009 7010/* 7011 * See if the buf bp is in the range eliminated by truncation. 7012 */ 7013static int 7014trunc_check_buf(bp, blkoffp, lastlbn, lastoff, flags) 7015 struct buf *bp; 7016 int *blkoffp; 7017 ufs_lbn_t lastlbn; 7018 int lastoff; 7019 int flags; 7020{ 7021 ufs_lbn_t lbn; 7022 7023 *blkoffp = 0; 7024 /* Only match ext/normal blocks as appropriate. */ 7025 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) || 7026 ((flags & IO_NORMAL) == 0 && (bp->b_xflags & BX_ALTDATA) == 0)) 7027 return (0); 7028 /* ALTDATA is always a full truncation. */ 7029 if ((bp->b_xflags & BX_ALTDATA) != 0) 7030 return (1); 7031 /* -1 is full truncation. */ 7032 if (lastlbn == -1) 7033 return (1); 7034 /* 7035 * If this is a partial truncate we only want those 7036 * blocks and indirect blocks that cover the range 7037 * we're after. 7038 */ 7039 lbn = bp->b_lblkno; 7040 if (lbn < 0) 7041 lbn = -(lbn + lbn_level(lbn)); 7042 if (lbn < lastlbn) 7043 return (0); 7044 /* Here we only truncate lblkno if it's partial. */ 7045 if (lbn == lastlbn) { 7046 if (lastoff == 0) 7047 return (0); 7048 *blkoffp = lastoff; 7049 } 7050 return (1); 7051} 7052 7053/* 7054 * Eliminate any dependencies that exist in memory beyond lblkno:off 7055 */ 7056static void 7057trunc_dependencies(ip, freeblks, lastlbn, lastoff, flags) 7058 struct inode *ip; 7059 struct freeblks *freeblks; 7060 ufs_lbn_t lastlbn; 7061 int lastoff; 7062 int flags; 7063{ 7064 struct bufobj *bo; 7065 struct vnode *vp; 7066 struct buf *bp; 7067 int blkoff; 7068 7069 /* 7070 * We must wait for any I/O in progress to finish so that 7071 * all potential buffers on the dirty list will be visible. 7072 * Once they are all there, walk the list and get rid of 7073 * any dependencies. 7074 */ 7075 vp = ITOV(ip); 7076 bo = &vp->v_bufobj; 7077 BO_LOCK(bo); 7078 drain_output(vp); 7079 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) 7080 bp->b_vflags &= ~BV_SCANNED; 7081restart: 7082 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) { 7083 if (bp->b_vflags & BV_SCANNED) 7084 continue; 7085 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 7086 bp->b_vflags |= BV_SCANNED; 7087 continue; 7088 } 7089 KASSERT(bp->b_bufobj == bo, ("Wrong object in buffer")); 7090 if ((bp = getdirtybuf(bp, BO_LOCKPTR(bo), MNT_WAIT)) == NULL) 7091 goto restart; 7092 BO_UNLOCK(bo); 7093 if (deallocate_dependencies(bp, freeblks, blkoff)) 7094 bqrelse(bp); 7095 else 7096 brelse(bp); 7097 BO_LOCK(bo); 7098 goto restart; 7099 } 7100 /* 7101 * Now do the work of vtruncbuf while also matching indirect blocks. 7102 */ 7103 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) 7104 bp->b_vflags &= ~BV_SCANNED; 7105cleanrestart: 7106 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) { 7107 if (bp->b_vflags & BV_SCANNED) 7108 continue; 7109 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 7110 bp->b_vflags |= BV_SCANNED; 7111 continue; 7112 } 7113 if (BUF_LOCK(bp, 7114 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 7115 BO_LOCKPTR(bo)) == ENOLCK) { 7116 BO_LOCK(bo); 7117 goto cleanrestart; 7118 } 7119 bp->b_vflags |= BV_SCANNED; 7120 bremfree(bp); 7121 if (blkoff != 0) { 7122 allocbuf(bp, blkoff); 7123 bqrelse(bp); 7124 } else { 7125 bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF; 7126 brelse(bp); 7127 } 7128 BO_LOCK(bo); 7129 goto cleanrestart; 7130 } 7131 drain_output(vp); 7132 BO_UNLOCK(bo); 7133} 7134 7135static int 7136cancel_pagedep(pagedep, freeblks, blkoff) 7137 struct pagedep *pagedep; 7138 struct freeblks *freeblks; 7139 int blkoff; 7140{ 7141 struct jremref *jremref; 7142 struct jmvref *jmvref; 7143 struct dirrem *dirrem, *tmp; 7144 int i; 7145 7146 /* 7147 * Copy any directory remove dependencies to the list 7148 * to be processed after the freeblks proceeds. If 7149 * directory entry never made it to disk they 7150 * can be dumped directly onto the work list. 7151 */ 7152 LIST_FOREACH_SAFE(dirrem, &pagedep->pd_dirremhd, dm_next, tmp) { 7153 /* Skip this directory removal if it is intended to remain. */ 7154 if (dirrem->dm_offset < blkoff) 7155 continue; 7156 /* 7157 * If there are any dirrems we wait for the journal write 7158 * to complete and then restart the buf scan as the lock 7159 * has been dropped. 7160 */ 7161 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) { 7162 jwait(&jremref->jr_list, MNT_WAIT); 7163 return (ERESTART); 7164 } 7165 LIST_REMOVE(dirrem, dm_next); 7166 dirrem->dm_dirinum = pagedep->pd_ino; 7167 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list); 7168 } 7169 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) { 7170 jwait(&jmvref->jm_list, MNT_WAIT); 7171 return (ERESTART); 7172 } 7173 /* 7174 * When we're partially truncating a pagedep we just want to flush 7175 * journal entries and return. There can not be any adds in the 7176 * truncated portion of the directory and newblk must remain if 7177 * part of the block remains. 7178 */ 7179 if (blkoff != 0) { 7180 struct diradd *dap; 7181 7182 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 7183 if (dap->da_offset > blkoff) 7184 panic("cancel_pagedep: diradd %p off %d > %d", 7185 dap, dap->da_offset, blkoff); 7186 for (i = 0; i < DAHASHSZ; i++) 7187 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) 7188 if (dap->da_offset > blkoff) 7189 panic("cancel_pagedep: diradd %p off %d > %d", 7190 dap, dap->da_offset, blkoff); 7191 return (0); 7192 } 7193 /* 7194 * There should be no directory add dependencies present 7195 * as the directory could not be truncated until all 7196 * children were removed. 7197 */ 7198 KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL, 7199 ("deallocate_dependencies: pendinghd != NULL")); 7200 for (i = 0; i < DAHASHSZ; i++) 7201 KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL, 7202 ("deallocate_dependencies: diraddhd != NULL")); 7203 if ((pagedep->pd_state & NEWBLOCK) != 0) 7204 free_newdirblk(pagedep->pd_newdirblk); 7205 if (free_pagedep(pagedep) == 0) 7206 panic("Failed to free pagedep %p", pagedep); 7207 return (0); 7208} 7209 7210/* 7211 * Reclaim any dependency structures from a buffer that is about to 7212 * be reallocated to a new vnode. The buffer must be locked, thus, 7213 * no I/O completion operations can occur while we are manipulating 7214 * its associated dependencies. The mutex is held so that other I/O's 7215 * associated with related dependencies do not occur. 7216 */ 7217static int 7218deallocate_dependencies(bp, freeblks, off) 7219 struct buf *bp; 7220 struct freeblks *freeblks; 7221 int off; 7222{ 7223 struct indirdep *indirdep; 7224 struct pagedep *pagedep; 7225 struct allocdirect *adp; 7226 struct worklist *wk, *wkn; 7227 struct ufsmount *ump; 7228 7229 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL) 7230 goto done; 7231 ump = VFSTOUFS(wk->wk_mp); 7232 ACQUIRE_LOCK(ump); 7233 LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) { 7234 switch (wk->wk_type) { 7235 case D_INDIRDEP: 7236 indirdep = WK_INDIRDEP(wk); 7237 if (bp->b_lblkno >= 0 || 7238 bp->b_blkno != indirdep->ir_savebp->b_lblkno) 7239 panic("deallocate_dependencies: not indir"); 7240 cancel_indirdep(indirdep, bp, freeblks); 7241 continue; 7242 7243 case D_PAGEDEP: 7244 pagedep = WK_PAGEDEP(wk); 7245 if (cancel_pagedep(pagedep, freeblks, off)) { 7246 FREE_LOCK(ump); 7247 return (ERESTART); 7248 } 7249 continue; 7250 7251 case D_ALLOCINDIR: 7252 /* 7253 * Simply remove the allocindir, we'll find it via 7254 * the indirdep where we can clear pointers if 7255 * needed. 7256 */ 7257 WORKLIST_REMOVE(wk); 7258 continue; 7259 7260 case D_FREEWORK: 7261 /* 7262 * A truncation is waiting for the zero'd pointers 7263 * to be written. It can be freed when the freeblks 7264 * is journaled. 7265 */ 7266 WORKLIST_REMOVE(wk); 7267 wk->wk_state |= ONDEPLIST; 7268 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 7269 break; 7270 7271 case D_ALLOCDIRECT: 7272 adp = WK_ALLOCDIRECT(wk); 7273 if (off != 0) 7274 continue; 7275 /* FALLTHROUGH */ 7276 default: 7277 panic("deallocate_dependencies: Unexpected type %s", 7278 TYPENAME(wk->wk_type)); 7279 /* NOTREACHED */ 7280 } 7281 } 7282 FREE_LOCK(ump); 7283done: 7284 /* 7285 * Don't throw away this buf, we were partially truncating and 7286 * some deps may always remain. 7287 */ 7288 if (off) { 7289 allocbuf(bp, off); 7290 bp->b_vflags |= BV_SCANNED; 7291 return (EBUSY); 7292 } 7293 bp->b_flags |= B_INVAL | B_NOCACHE; 7294 7295 return (0); 7296} 7297 7298/* 7299 * An allocdirect is being canceled due to a truncate. We must make sure 7300 * the journal entry is released in concert with the blkfree that releases 7301 * the storage. Completed journal entries must not be released until the 7302 * space is no longer pointed to by the inode or in the bitmap. 7303 */ 7304static void 7305cancel_allocdirect(adphead, adp, freeblks) 7306 struct allocdirectlst *adphead; 7307 struct allocdirect *adp; 7308 struct freeblks *freeblks; 7309{ 7310 struct freework *freework; 7311 struct newblk *newblk; 7312 struct worklist *wk; 7313 7314 TAILQ_REMOVE(adphead, adp, ad_next); 7315 newblk = (struct newblk *)adp; 7316 freework = NULL; 7317 /* 7318 * Find the correct freework structure. 7319 */ 7320 LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) { 7321 if (wk->wk_type != D_FREEWORK) 7322 continue; 7323 freework = WK_FREEWORK(wk); 7324 if (freework->fw_blkno == newblk->nb_newblkno) 7325 break; 7326 } 7327 if (freework == NULL) 7328 panic("cancel_allocdirect: Freework not found"); 7329 /* 7330 * If a newblk exists at all we still have the journal entry that 7331 * initiated the allocation so we do not need to journal the free. 7332 */ 7333 cancel_jfreeblk(freeblks, freework->fw_blkno); 7334 /* 7335 * If the journal hasn't been written the jnewblk must be passed 7336 * to the call to ffs_blkfree that reclaims the space. We accomplish 7337 * this by linking the journal dependency into the freework to be 7338 * freed when freework_freeblock() is called. If the journal has 7339 * been written we can simply reclaim the journal space when the 7340 * freeblks work is complete. 7341 */ 7342 freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list, 7343 &freeblks->fb_jwork); 7344 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 7345} 7346 7347 7348/* 7349 * Cancel a new block allocation. May be an indirect or direct block. We 7350 * remove it from various lists and return any journal record that needs to 7351 * be resolved by the caller. 7352 * 7353 * A special consideration is made for indirects which were never pointed 7354 * at on disk and will never be found once this block is released. 7355 */ 7356static struct jnewblk * 7357cancel_newblk(newblk, wk, wkhd) 7358 struct newblk *newblk; 7359 struct worklist *wk; 7360 struct workhead *wkhd; 7361{ 7362 struct jnewblk *jnewblk; 7363 7364 CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno); 7365 7366 newblk->nb_state |= GOINGAWAY; 7367 /* 7368 * Previously we traversed the completedhd on each indirdep 7369 * attached to this newblk to cancel them and gather journal 7370 * work. Since we need only the oldest journal segment and 7371 * the lowest point on the tree will always have the oldest 7372 * journal segment we are free to release the segments 7373 * of any subordinates and may leave the indirdep list to 7374 * indirdep_complete() when this newblk is freed. 7375 */ 7376 if (newblk->nb_state & ONDEPLIST) { 7377 newblk->nb_state &= ~ONDEPLIST; 7378 LIST_REMOVE(newblk, nb_deps); 7379 } 7380 if (newblk->nb_state & ONWORKLIST) 7381 WORKLIST_REMOVE(&newblk->nb_list); 7382 /* 7383 * If the journal entry hasn't been written we save a pointer to 7384 * the dependency that frees it until it is written or the 7385 * superseding operation completes. 7386 */ 7387 jnewblk = newblk->nb_jnewblk; 7388 if (jnewblk != NULL && wk != NULL) { 7389 newblk->nb_jnewblk = NULL; 7390 jnewblk->jn_dep = wk; 7391 } 7392 if (!LIST_EMPTY(&newblk->nb_jwork)) 7393 jwork_move(wkhd, &newblk->nb_jwork); 7394 /* 7395 * When truncating we must free the newdirblk early to remove 7396 * the pagedep from the hash before returning. 7397 */ 7398 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7399 free_newdirblk(WK_NEWDIRBLK(wk)); 7400 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7401 panic("cancel_newblk: extra newdirblk"); 7402 7403 return (jnewblk); 7404} 7405 7406/* 7407 * Schedule the freefrag associated with a newblk to be released once 7408 * the pointers are written and the previous block is no longer needed. 7409 */ 7410static void 7411newblk_freefrag(newblk) 7412 struct newblk *newblk; 7413{ 7414 struct freefrag *freefrag; 7415 7416 if (newblk->nb_freefrag == NULL) 7417 return; 7418 freefrag = newblk->nb_freefrag; 7419 newblk->nb_freefrag = NULL; 7420 freefrag->ff_state |= COMPLETE; 7421 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 7422 add_to_worklist(&freefrag->ff_list, 0); 7423} 7424 7425/* 7426 * Free a newblk. Generate a new freefrag work request if appropriate. 7427 * This must be called after the inode pointer and any direct block pointers 7428 * are valid or fully removed via truncate or frag extension. 7429 */ 7430static void 7431free_newblk(newblk) 7432 struct newblk *newblk; 7433{ 7434 struct indirdep *indirdep; 7435 struct worklist *wk; 7436 7437 KASSERT(newblk->nb_jnewblk == NULL, 7438 ("free_newblk: jnewblk %p still attached", newblk->nb_jnewblk)); 7439 KASSERT(newblk->nb_list.wk_type != D_NEWBLK, 7440 ("free_newblk: unclaimed newblk")); 7441 LOCK_OWNED(VFSTOUFS(newblk->nb_list.wk_mp)); 7442 newblk_freefrag(newblk); 7443 if (newblk->nb_state & ONDEPLIST) 7444 LIST_REMOVE(newblk, nb_deps); 7445 if (newblk->nb_state & ONWORKLIST) 7446 WORKLIST_REMOVE(&newblk->nb_list); 7447 LIST_REMOVE(newblk, nb_hash); 7448 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7449 free_newdirblk(WK_NEWDIRBLK(wk)); 7450 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7451 panic("free_newblk: extra newdirblk"); 7452 while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL) 7453 indirdep_complete(indirdep); 7454 handle_jwork(&newblk->nb_jwork); 7455 WORKITEM_FREE(newblk, D_NEWBLK); 7456} 7457 7458/* 7459 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep. 7460 * This routine must be called with splbio interrupts blocked. 7461 */ 7462static void 7463free_newdirblk(newdirblk) 7464 struct newdirblk *newdirblk; 7465{ 7466 struct pagedep *pagedep; 7467 struct diradd *dap; 7468 struct worklist *wk; 7469 7470 LOCK_OWNED(VFSTOUFS(newdirblk->db_list.wk_mp)); 7471 WORKLIST_REMOVE(&newdirblk->db_list); 7472 /* 7473 * If the pagedep is still linked onto the directory buffer 7474 * dependency chain, then some of the entries on the 7475 * pd_pendinghd list may not be committed to disk yet. In 7476 * this case, we will simply clear the NEWBLOCK flag and 7477 * let the pd_pendinghd list be processed when the pagedep 7478 * is next written. If the pagedep is no longer on the buffer 7479 * dependency chain, then all the entries on the pd_pending 7480 * list are committed to disk and we can free them here. 7481 */ 7482 pagedep = newdirblk->db_pagedep; 7483 pagedep->pd_state &= ~NEWBLOCK; 7484 if ((pagedep->pd_state & ONWORKLIST) == 0) { 7485 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 7486 free_diradd(dap, NULL); 7487 /* 7488 * If no dependencies remain, the pagedep will be freed. 7489 */ 7490 free_pagedep(pagedep); 7491 } 7492 /* Should only ever be one item in the list. */ 7493 while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) { 7494 WORKLIST_REMOVE(wk); 7495 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 7496 } 7497 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 7498} 7499 7500/* 7501 * Prepare an inode to be freed. The actual free operation is not 7502 * done until the zero'ed inode has been written to disk. 7503 */ 7504void 7505softdep_freefile(pvp, ino, mode) 7506 struct vnode *pvp; 7507 ino_t ino; 7508 int mode; 7509{ 7510 struct inode *ip = VTOI(pvp); 7511 struct inodedep *inodedep; 7512 struct freefile *freefile; 7513 struct freeblks *freeblks; 7514 struct ufsmount *ump; 7515 7516 ump = ip->i_ump; 7517 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 7518 ("softdep_freefile called on non-softdep filesystem")); 7519 /* 7520 * This sets up the inode de-allocation dependency. 7521 */ 7522 freefile = malloc(sizeof(struct freefile), 7523 M_FREEFILE, M_SOFTDEP_FLAGS); 7524 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount); 7525 freefile->fx_mode = mode; 7526 freefile->fx_oldinum = ino; 7527 freefile->fx_devvp = ip->i_devvp; 7528 LIST_INIT(&freefile->fx_jwork); 7529 UFS_LOCK(ump); 7530 ip->i_fs->fs_pendinginodes += 1; 7531 UFS_UNLOCK(ump); 7532 7533 /* 7534 * If the inodedep does not exist, then the zero'ed inode has 7535 * been written to disk. If the allocated inode has never been 7536 * written to disk, then the on-disk inode is zero'ed. In either 7537 * case we can free the file immediately. If the journal was 7538 * canceled before being written the inode will never make it to 7539 * disk and we must send the canceled journal entrys to 7540 * ffs_freefile() to be cleared in conjunction with the bitmap. 7541 * Any blocks waiting on the inode to write can be safely freed 7542 * here as it will never been written. 7543 */ 7544 ACQUIRE_LOCK(ump); 7545 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7546 if (inodedep) { 7547 /* 7548 * Clear out freeblks that no longer need to reference 7549 * this inode. 7550 */ 7551 while ((freeblks = 7552 TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) { 7553 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, 7554 fb_next); 7555 freeblks->fb_state &= ~ONDEPLIST; 7556 } 7557 /* 7558 * Remove this inode from the unlinked list. 7559 */ 7560 if (inodedep->id_state & UNLINKED) { 7561 /* 7562 * Save the journal work to be freed with the bitmap 7563 * before we clear UNLINKED. Otherwise it can be lost 7564 * if the inode block is written. 7565 */ 7566 handle_bufwait(inodedep, &freefile->fx_jwork); 7567 clear_unlinked_inodedep(inodedep); 7568 /* 7569 * Re-acquire inodedep as we've dropped the 7570 * per-filesystem lock in clear_unlinked_inodedep(). 7571 */ 7572 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7573 } 7574 } 7575 if (inodedep == NULL || check_inode_unwritten(inodedep)) { 7576 FREE_LOCK(ump); 7577 handle_workitem_freefile(freefile); 7578 return; 7579 } 7580 if ((inodedep->id_state & DEPCOMPLETE) == 0) 7581 inodedep->id_state |= GOINGAWAY; 7582 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list); 7583 FREE_LOCK(ump); 7584 if (ip->i_number == ino) 7585 ip->i_flag |= IN_MODIFIED; 7586} 7587 7588/* 7589 * Check to see if an inode has never been written to disk. If 7590 * so free the inodedep and return success, otherwise return failure. 7591 * This routine must be called with splbio interrupts blocked. 7592 * 7593 * If we still have a bitmap dependency, then the inode has never 7594 * been written to disk. Drop the dependency as it is no longer 7595 * necessary since the inode is being deallocated. We set the 7596 * ALLCOMPLETE flags since the bitmap now properly shows that the 7597 * inode is not allocated. Even if the inode is actively being 7598 * written, it has been rolled back to its zero'ed state, so we 7599 * are ensured that a zero inode is what is on the disk. For short 7600 * lived files, this change will usually result in removing all the 7601 * dependencies from the inode so that it can be freed immediately. 7602 */ 7603static int 7604check_inode_unwritten(inodedep) 7605 struct inodedep *inodedep; 7606{ 7607 7608 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7609 7610 if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 || 7611 !LIST_EMPTY(&inodedep->id_dirremhd) || 7612 !LIST_EMPTY(&inodedep->id_pendinghd) || 7613 !LIST_EMPTY(&inodedep->id_bufwait) || 7614 !LIST_EMPTY(&inodedep->id_inowait) || 7615 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 7616 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 7617 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 7618 !TAILQ_EMPTY(&inodedep->id_extupdt) || 7619 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 7620 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 7621 inodedep->id_mkdiradd != NULL || 7622 inodedep->id_nlinkdelta != 0) 7623 return (0); 7624 /* 7625 * Another process might be in initiate_write_inodeblock_ufs[12] 7626 * trying to allocate memory without holding "Softdep Lock". 7627 */ 7628 if ((inodedep->id_state & IOSTARTED) != 0 && 7629 inodedep->id_savedino1 == NULL) 7630 return (0); 7631 7632 if (inodedep->id_state & ONDEPLIST) 7633 LIST_REMOVE(inodedep, id_deps); 7634 inodedep->id_state &= ~ONDEPLIST; 7635 inodedep->id_state |= ALLCOMPLETE; 7636 inodedep->id_bmsafemap = NULL; 7637 if (inodedep->id_state & ONWORKLIST) 7638 WORKLIST_REMOVE(&inodedep->id_list); 7639 if (inodedep->id_savedino1 != NULL) { 7640 free(inodedep->id_savedino1, M_SAVEDINO); 7641 inodedep->id_savedino1 = NULL; 7642 } 7643 if (free_inodedep(inodedep) == 0) 7644 panic("check_inode_unwritten: busy inode"); 7645 return (1); 7646} 7647 7648static int 7649check_inodedep_free(inodedep) 7650 struct inodedep *inodedep; 7651{ 7652 7653 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7654 if ((inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE || 7655 !LIST_EMPTY(&inodedep->id_dirremhd) || 7656 !LIST_EMPTY(&inodedep->id_pendinghd) || 7657 !LIST_EMPTY(&inodedep->id_bufwait) || 7658 !LIST_EMPTY(&inodedep->id_inowait) || 7659 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 7660 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 7661 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 7662 !TAILQ_EMPTY(&inodedep->id_extupdt) || 7663 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 7664 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 7665 inodedep->id_mkdiradd != NULL || 7666 inodedep->id_nlinkdelta != 0 || 7667 inodedep->id_savedino1 != NULL) 7668 return (0); 7669 return (1); 7670} 7671 7672/* 7673 * Try to free an inodedep structure. Return 1 if it could be freed. 7674 */ 7675static int 7676free_inodedep(inodedep) 7677 struct inodedep *inodedep; 7678{ 7679 7680 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7681 if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 || 7682 !check_inodedep_free(inodedep)) 7683 return (0); 7684 if (inodedep->id_state & ONDEPLIST) 7685 LIST_REMOVE(inodedep, id_deps); 7686 LIST_REMOVE(inodedep, id_hash); 7687 WORKITEM_FREE(inodedep, D_INODEDEP); 7688 return (1); 7689} 7690 7691/* 7692 * Free the block referenced by a freework structure. The parent freeblks 7693 * structure is released and completed when the final cg bitmap reaches 7694 * the disk. This routine may be freeing a jnewblk which never made it to 7695 * disk in which case we do not have to wait as the operation is undone 7696 * in memory immediately. 7697 */ 7698static void 7699freework_freeblock(freework) 7700 struct freework *freework; 7701{ 7702 struct freeblks *freeblks; 7703 struct jnewblk *jnewblk; 7704 struct ufsmount *ump; 7705 struct workhead wkhd; 7706 struct fs *fs; 7707 int bsize; 7708 int needj; 7709 7710 ump = VFSTOUFS(freework->fw_list.wk_mp); 7711 LOCK_OWNED(ump); 7712 /* 7713 * Handle partial truncate separately. 7714 */ 7715 if (freework->fw_indir) { 7716 complete_trunc_indir(freework); 7717 return; 7718 } 7719 freeblks = freework->fw_freeblks; 7720 fs = ump->um_fs; 7721 needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0; 7722 bsize = lfragtosize(fs, freework->fw_frags); 7723 LIST_INIT(&wkhd); 7724 /* 7725 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives 7726 * on the indirblk hashtable and prevents premature freeing. 7727 */ 7728 freework->fw_state |= DEPCOMPLETE; 7729 /* 7730 * SUJ needs to wait for the segment referencing freed indirect 7731 * blocks to expire so that we know the checker will not confuse 7732 * a re-allocated indirect block with its old contents. 7733 */ 7734 if (needj && freework->fw_lbn <= -NDADDR) 7735 indirblk_insert(freework); 7736 /* 7737 * If we are canceling an existing jnewblk pass it to the free 7738 * routine, otherwise pass the freeblk which will ultimately 7739 * release the freeblks. If we're not journaling, we can just 7740 * free the freeblks immediately. 7741 */ 7742 jnewblk = freework->fw_jnewblk; 7743 if (jnewblk != NULL) { 7744 cancel_jnewblk(jnewblk, &wkhd); 7745 needj = 0; 7746 } else if (needj) { 7747 freework->fw_state |= DELAYEDFREE; 7748 freeblks->fb_cgwait++; 7749 WORKLIST_INSERT(&wkhd, &freework->fw_list); 7750 } 7751 FREE_LOCK(ump); 7752 freeblks_free(ump, freeblks, btodb(bsize)); 7753 CTR4(KTR_SUJ, 7754 "freework_freeblock: ino %d blkno %jd lbn %jd size %ld", 7755 freeblks->fb_inum, freework->fw_blkno, freework->fw_lbn, bsize); 7756 ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, bsize, 7757 freeblks->fb_inum, freeblks->fb_vtype, &wkhd); 7758 ACQUIRE_LOCK(ump); 7759 /* 7760 * The jnewblk will be discarded and the bits in the map never 7761 * made it to disk. We can immediately free the freeblk. 7762 */ 7763 if (needj == 0) 7764 handle_written_freework(freework); 7765} 7766 7767/* 7768 * We enqueue freework items that need processing back on the freeblks and 7769 * add the freeblks to the worklist. This makes it easier to find all work 7770 * required to flush a truncation in process_truncates(). 7771 */ 7772static void 7773freework_enqueue(freework) 7774 struct freework *freework; 7775{ 7776 struct freeblks *freeblks; 7777 7778 freeblks = freework->fw_freeblks; 7779 if ((freework->fw_state & INPROGRESS) == 0) 7780 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 7781 if ((freeblks->fb_state & 7782 (ONWORKLIST | INPROGRESS | ALLCOMPLETE)) == ALLCOMPLETE && 7783 LIST_EMPTY(&freeblks->fb_jblkdephd)) 7784 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 7785} 7786 7787/* 7788 * Start, continue, or finish the process of freeing an indirect block tree. 7789 * The free operation may be paused at any point with fw_off containing the 7790 * offset to restart from. This enables us to implement some flow control 7791 * for large truncates which may fan out and generate a huge number of 7792 * dependencies. 7793 */ 7794static void 7795handle_workitem_indirblk(freework) 7796 struct freework *freework; 7797{ 7798 struct freeblks *freeblks; 7799 struct ufsmount *ump; 7800 struct fs *fs; 7801 7802 freeblks = freework->fw_freeblks; 7803 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7804 fs = ump->um_fs; 7805 if (freework->fw_state & DEPCOMPLETE) { 7806 handle_written_freework(freework); 7807 return; 7808 } 7809 if (freework->fw_off == NINDIR(fs)) { 7810 freework_freeblock(freework); 7811 return; 7812 } 7813 freework->fw_state |= INPROGRESS; 7814 FREE_LOCK(ump); 7815 indir_trunc(freework, fsbtodb(fs, freework->fw_blkno), 7816 freework->fw_lbn); 7817 ACQUIRE_LOCK(ump); 7818} 7819 7820/* 7821 * Called when a freework structure attached to a cg buf is written. The 7822 * ref on either the parent or the freeblks structure is released and 7823 * the freeblks is added back to the worklist if there is more work to do. 7824 */ 7825static void 7826handle_written_freework(freework) 7827 struct freework *freework; 7828{ 7829 struct freeblks *freeblks; 7830 struct freework *parent; 7831 7832 freeblks = freework->fw_freeblks; 7833 parent = freework->fw_parent; 7834 if (freework->fw_state & DELAYEDFREE) 7835 freeblks->fb_cgwait--; 7836 freework->fw_state |= COMPLETE; 7837 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 7838 WORKITEM_FREE(freework, D_FREEWORK); 7839 if (parent) { 7840 if (--parent->fw_ref == 0) 7841 freework_enqueue(parent); 7842 return; 7843 } 7844 if (--freeblks->fb_ref != 0) 7845 return; 7846 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) == 7847 ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd)) 7848 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 7849} 7850 7851/* 7852 * This workitem routine performs the block de-allocation. 7853 * The workitem is added to the pending list after the updated 7854 * inode block has been written to disk. As mentioned above, 7855 * checks regarding the number of blocks de-allocated (compared 7856 * to the number of blocks allocated for the file) are also 7857 * performed in this function. 7858 */ 7859static int 7860handle_workitem_freeblocks(freeblks, flags) 7861 struct freeblks *freeblks; 7862 int flags; 7863{ 7864 struct freework *freework; 7865 struct newblk *newblk; 7866 struct allocindir *aip; 7867 struct ufsmount *ump; 7868 struct worklist *wk; 7869 7870 KASSERT(LIST_EMPTY(&freeblks->fb_jblkdephd), 7871 ("handle_workitem_freeblocks: Journal entries not written.")); 7872 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7873 ACQUIRE_LOCK(ump); 7874 while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) { 7875 WORKLIST_REMOVE(wk); 7876 switch (wk->wk_type) { 7877 case D_DIRREM: 7878 wk->wk_state |= COMPLETE; 7879 add_to_worklist(wk, 0); 7880 continue; 7881 7882 case D_ALLOCDIRECT: 7883 free_newblk(WK_NEWBLK(wk)); 7884 continue; 7885 7886 case D_ALLOCINDIR: 7887 aip = WK_ALLOCINDIR(wk); 7888 freework = NULL; 7889 if (aip->ai_state & DELAYEDFREE) { 7890 FREE_LOCK(ump); 7891 freework = newfreework(ump, freeblks, NULL, 7892 aip->ai_lbn, aip->ai_newblkno, 7893 ump->um_fs->fs_frag, 0, 0); 7894 ACQUIRE_LOCK(ump); 7895 } 7896 newblk = WK_NEWBLK(wk); 7897 if (newblk->nb_jnewblk) { 7898 freework->fw_jnewblk = newblk->nb_jnewblk; 7899 newblk->nb_jnewblk->jn_dep = &freework->fw_list; 7900 newblk->nb_jnewblk = NULL; 7901 } 7902 free_newblk(newblk); 7903 continue; 7904 7905 case D_FREEWORK: 7906 freework = WK_FREEWORK(wk); 7907 if (freework->fw_lbn <= -NDADDR) 7908 handle_workitem_indirblk(freework); 7909 else 7910 freework_freeblock(freework); 7911 continue; 7912 default: 7913 panic("handle_workitem_freeblocks: Unknown type %s", 7914 TYPENAME(wk->wk_type)); 7915 } 7916 } 7917 if (freeblks->fb_ref != 0) { 7918 freeblks->fb_state &= ~INPROGRESS; 7919 wake_worklist(&freeblks->fb_list); 7920 freeblks = NULL; 7921 } 7922 FREE_LOCK(ump); 7923 if (freeblks) 7924 return handle_complete_freeblocks(freeblks, flags); 7925 return (0); 7926} 7927 7928/* 7929 * Handle completion of block free via truncate. This allows fs_pending 7930 * to track the actual free block count more closely than if we only updated 7931 * it at the end. We must be careful to handle cases where the block count 7932 * on free was incorrect. 7933 */ 7934static void 7935freeblks_free(ump, freeblks, blocks) 7936 struct ufsmount *ump; 7937 struct freeblks *freeblks; 7938 int blocks; 7939{ 7940 struct fs *fs; 7941 ufs2_daddr_t remain; 7942 7943 UFS_LOCK(ump); 7944 remain = -freeblks->fb_chkcnt; 7945 freeblks->fb_chkcnt += blocks; 7946 if (remain > 0) { 7947 if (remain < blocks) 7948 blocks = remain; 7949 fs = ump->um_fs; 7950 fs->fs_pendingblocks -= blocks; 7951 } 7952 UFS_UNLOCK(ump); 7953} 7954 7955/* 7956 * Once all of the freework workitems are complete we can retire the 7957 * freeblocks dependency and any journal work awaiting completion. This 7958 * can not be called until all other dependencies are stable on disk. 7959 */ 7960static int 7961handle_complete_freeblocks(freeblks, flags) 7962 struct freeblks *freeblks; 7963 int flags; 7964{ 7965 struct inodedep *inodedep; 7966 struct inode *ip; 7967 struct vnode *vp; 7968 struct fs *fs; 7969 struct ufsmount *ump; 7970 ufs2_daddr_t spare; 7971 7972 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7973 fs = ump->um_fs; 7974 flags = LK_EXCLUSIVE | flags; 7975 spare = freeblks->fb_chkcnt; 7976 7977 /* 7978 * If we did not release the expected number of blocks we may have 7979 * to adjust the inode block count here. Only do so if it wasn't 7980 * a truncation to zero and the modrev still matches. 7981 */ 7982 if (spare && freeblks->fb_len != 0) { 7983 if (ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_inum, 7984 flags, &vp, FFSV_FORCEINSMQ) != 0) 7985 return (EBUSY); 7986 ip = VTOI(vp); 7987 if (DIP(ip, i_modrev) == freeblks->fb_modrev) { 7988 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare); 7989 ip->i_flag |= IN_CHANGE; 7990 /* 7991 * We must wait so this happens before the 7992 * journal is reclaimed. 7993 */ 7994 ffs_update(vp, 1); 7995 } 7996 vput(vp); 7997 } 7998 if (spare < 0) { 7999 UFS_LOCK(ump); 8000 fs->fs_pendingblocks += spare; 8001 UFS_UNLOCK(ump); 8002 } 8003#ifdef QUOTA 8004 /* Handle spare. */ 8005 if (spare) 8006 quotaadj(freeblks->fb_quota, ump, -spare); 8007 quotarele(freeblks->fb_quota); 8008#endif 8009 ACQUIRE_LOCK(ump); 8010 if (freeblks->fb_state & ONDEPLIST) { 8011 inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum, 8012 0, &inodedep); 8013 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next); 8014 freeblks->fb_state &= ~ONDEPLIST; 8015 if (TAILQ_EMPTY(&inodedep->id_freeblklst)) 8016 free_inodedep(inodedep); 8017 } 8018 /* 8019 * All of the freeblock deps must be complete prior to this call 8020 * so it's now safe to complete earlier outstanding journal entries. 8021 */ 8022 handle_jwork(&freeblks->fb_jwork); 8023 WORKITEM_FREE(freeblks, D_FREEBLKS); 8024 FREE_LOCK(ump); 8025 return (0); 8026} 8027 8028/* 8029 * Release blocks associated with the freeblks and stored in the indirect 8030 * block dbn. If level is greater than SINGLE, the block is an indirect block 8031 * and recursive calls to indirtrunc must be used to cleanse other indirect 8032 * blocks. 8033 * 8034 * This handles partial and complete truncation of blocks. Partial is noted 8035 * with goingaway == 0. In this case the freework is completed after the 8036 * zero'd indirects are written to disk. For full truncation the freework 8037 * is completed after the block is freed. 8038 */ 8039static void 8040indir_trunc(freework, dbn, lbn) 8041 struct freework *freework; 8042 ufs2_daddr_t dbn; 8043 ufs_lbn_t lbn; 8044{ 8045 struct freework *nfreework; 8046 struct workhead wkhd; 8047 struct freeblks *freeblks; 8048 struct buf *bp; 8049 struct fs *fs; 8050 struct indirdep *indirdep; 8051 struct ufsmount *ump; 8052 ufs1_daddr_t *bap1 = 0; 8053 ufs2_daddr_t nb, nnb, *bap2 = 0; 8054 ufs_lbn_t lbnadd, nlbn; 8055 int i, nblocks, ufs1fmt; 8056 int freedblocks; 8057 int goingaway; 8058 int freedeps; 8059 int needj; 8060 int level; 8061 int cnt; 8062 8063 freeblks = freework->fw_freeblks; 8064 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8065 fs = ump->um_fs; 8066 /* 8067 * Get buffer of block pointers to be freed. There are three cases: 8068 * 8069 * 1) Partial truncate caches the indirdep pointer in the freework 8070 * which provides us a back copy to the save bp which holds the 8071 * pointers we want to clear. When this completes the zero 8072 * pointers are written to the real copy. 8073 * 2) The indirect is being completely truncated, cancel_indirdep() 8074 * eliminated the real copy and placed the indirdep on the saved 8075 * copy. The indirdep and buf are discarded when this completes. 8076 * 3) The indirect was not in memory, we read a copy off of the disk 8077 * using the devvp and drop and invalidate the buffer when we're 8078 * done. 8079 */ 8080 goingaway = 1; 8081 indirdep = NULL; 8082 if (freework->fw_indir != NULL) { 8083 goingaway = 0; 8084 indirdep = freework->fw_indir; 8085 bp = indirdep->ir_savebp; 8086 if (bp == NULL || bp->b_blkno != dbn) 8087 panic("indir_trunc: Bad saved buf %p blkno %jd", 8088 bp, (intmax_t)dbn); 8089 } else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) { 8090 /* 8091 * The lock prevents the buf dep list from changing and 8092 * indirects on devvp should only ever have one dependency. 8093 */ 8094 indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep)); 8095 if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0) 8096 panic("indir_trunc: Bad indirdep %p from buf %p", 8097 indirdep, bp); 8098 } else if (bread(freeblks->fb_devvp, dbn, (int)fs->fs_bsize, 8099 NOCRED, &bp) != 0) { 8100 brelse(bp); 8101 return; 8102 } 8103 ACQUIRE_LOCK(ump); 8104 /* Protects against a race with complete_trunc_indir(). */ 8105 freework->fw_state &= ~INPROGRESS; 8106 /* 8107 * If we have an indirdep we need to enforce the truncation order 8108 * and discard it when it is complete. 8109 */ 8110 if (indirdep) { 8111 if (freework != TAILQ_FIRST(&indirdep->ir_trunc) && 8112 !TAILQ_EMPTY(&indirdep->ir_trunc)) { 8113 /* 8114 * Add the complete truncate to the list on the 8115 * indirdep to enforce in-order processing. 8116 */ 8117 if (freework->fw_indir == NULL) 8118 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, 8119 freework, fw_next); 8120 FREE_LOCK(ump); 8121 return; 8122 } 8123 /* 8124 * If we're goingaway, free the indirdep. Otherwise it will 8125 * linger until the write completes. 8126 */ 8127 if (goingaway) 8128 free_indirdep(indirdep); 8129 } 8130 FREE_LOCK(ump); 8131 /* Initialize pointers depending on block size. */ 8132 if (ump->um_fstype == UFS1) { 8133 bap1 = (ufs1_daddr_t *)bp->b_data; 8134 nb = bap1[freework->fw_off]; 8135 ufs1fmt = 1; 8136 } else { 8137 bap2 = (ufs2_daddr_t *)bp->b_data; 8138 nb = bap2[freework->fw_off]; 8139 ufs1fmt = 0; 8140 } 8141 level = lbn_level(lbn); 8142 needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0; 8143 lbnadd = lbn_offset(fs, level); 8144 nblocks = btodb(fs->fs_bsize); 8145 nfreework = freework; 8146 freedeps = 0; 8147 cnt = 0; 8148 /* 8149 * Reclaim blocks. Traverses into nested indirect levels and 8150 * arranges for the current level to be freed when subordinates 8151 * are free when journaling. 8152 */ 8153 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) { 8154 if (i != NINDIR(fs) - 1) { 8155 if (ufs1fmt) 8156 nnb = bap1[i+1]; 8157 else 8158 nnb = bap2[i+1]; 8159 } else 8160 nnb = 0; 8161 if (nb == 0) 8162 continue; 8163 cnt++; 8164 if (level != 0) { 8165 nlbn = (lbn + 1) - (i * lbnadd); 8166 if (needj != 0) { 8167 nfreework = newfreework(ump, freeblks, freework, 8168 nlbn, nb, fs->fs_frag, 0, 0); 8169 freedeps++; 8170 } 8171 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn); 8172 } else { 8173 struct freedep *freedep; 8174 8175 /* 8176 * Attempt to aggregate freedep dependencies for 8177 * all blocks being released to the same CG. 8178 */ 8179 LIST_INIT(&wkhd); 8180 if (needj != 0 && 8181 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) { 8182 freedep = newfreedep(freework); 8183 WORKLIST_INSERT_UNLOCKED(&wkhd, 8184 &freedep->fd_list); 8185 freedeps++; 8186 } 8187 CTR3(KTR_SUJ, 8188 "indir_trunc: ino %d blkno %jd size %ld", 8189 freeblks->fb_inum, nb, fs->fs_bsize); 8190 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb, 8191 fs->fs_bsize, freeblks->fb_inum, 8192 freeblks->fb_vtype, &wkhd); 8193 } 8194 } 8195 if (goingaway) { 8196 bp->b_flags |= B_INVAL | B_NOCACHE; 8197 brelse(bp); 8198 } 8199 freedblocks = 0; 8200 if (level == 0) 8201 freedblocks = (nblocks * cnt); 8202 if (needj == 0) 8203 freedblocks += nblocks; 8204 freeblks_free(ump, freeblks, freedblocks); 8205 /* 8206 * If we are journaling set up the ref counts and offset so this 8207 * indirect can be completed when its children are free. 8208 */ 8209 if (needj) { 8210 ACQUIRE_LOCK(ump); 8211 freework->fw_off = i; 8212 freework->fw_ref += freedeps; 8213 freework->fw_ref -= NINDIR(fs) + 1; 8214 if (level == 0) 8215 freeblks->fb_cgwait += freedeps; 8216 if (freework->fw_ref == 0) 8217 freework_freeblock(freework); 8218 FREE_LOCK(ump); 8219 return; 8220 } 8221 /* 8222 * If we're not journaling we can free the indirect now. 8223 */ 8224 dbn = dbtofsb(fs, dbn); 8225 CTR3(KTR_SUJ, 8226 "indir_trunc 2: ino %d blkno %jd size %ld", 8227 freeblks->fb_inum, dbn, fs->fs_bsize); 8228 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize, 8229 freeblks->fb_inum, freeblks->fb_vtype, NULL); 8230 /* Non SUJ softdep does single-threaded truncations. */ 8231 if (freework->fw_blkno == dbn) { 8232 freework->fw_state |= ALLCOMPLETE; 8233 ACQUIRE_LOCK(ump); 8234 handle_written_freework(freework); 8235 FREE_LOCK(ump); 8236 } 8237 return; 8238} 8239 8240/* 8241 * Cancel an allocindir when it is removed via truncation. When bp is not 8242 * NULL the indirect never appeared on disk and is scheduled to be freed 8243 * independently of the indir so we can more easily track journal work. 8244 */ 8245static void 8246cancel_allocindir(aip, bp, freeblks, trunc) 8247 struct allocindir *aip; 8248 struct buf *bp; 8249 struct freeblks *freeblks; 8250 int trunc; 8251{ 8252 struct indirdep *indirdep; 8253 struct freefrag *freefrag; 8254 struct newblk *newblk; 8255 8256 newblk = (struct newblk *)aip; 8257 LIST_REMOVE(aip, ai_next); 8258 /* 8259 * We must eliminate the pointer in bp if it must be freed on its 8260 * own due to partial truncate or pending journal work. 8261 */ 8262 if (bp && (trunc || newblk->nb_jnewblk)) { 8263 /* 8264 * Clear the pointer and mark the aip to be freed 8265 * directly if it never existed on disk. 8266 */ 8267 aip->ai_state |= DELAYEDFREE; 8268 indirdep = aip->ai_indirdep; 8269 if (indirdep->ir_state & UFS1FMT) 8270 ((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8271 else 8272 ((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8273 } 8274 /* 8275 * When truncating the previous pointer will be freed via 8276 * savedbp. Eliminate the freefrag which would dup free. 8277 */ 8278 if (trunc && (freefrag = newblk->nb_freefrag) != NULL) { 8279 newblk->nb_freefrag = NULL; 8280 if (freefrag->ff_jdep) 8281 cancel_jfreefrag( 8282 WK_JFREEFRAG(freefrag->ff_jdep)); 8283 jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork); 8284 WORKITEM_FREE(freefrag, D_FREEFRAG); 8285 } 8286 /* 8287 * If the journal hasn't been written the jnewblk must be passed 8288 * to the call to ffs_blkfree that reclaims the space. We accomplish 8289 * this by leaving the journal dependency on the newblk to be freed 8290 * when a freework is created in handle_workitem_freeblocks(). 8291 */ 8292 cancel_newblk(newblk, NULL, &freeblks->fb_jwork); 8293 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 8294} 8295 8296/* 8297 * Create the mkdir dependencies for . and .. in a new directory. Link them 8298 * in to a newdirblk so any subsequent additions are tracked properly. The 8299 * caller is responsible for adding the mkdir1 dependency to the journal 8300 * and updating id_mkdiradd. This function returns with the per-filesystem 8301 * lock held. 8302 */ 8303static struct mkdir * 8304setup_newdir(dap, newinum, dinum, newdirbp, mkdirp) 8305 struct diradd *dap; 8306 ino_t newinum; 8307 ino_t dinum; 8308 struct buf *newdirbp; 8309 struct mkdir **mkdirp; 8310{ 8311 struct newblk *newblk; 8312 struct pagedep *pagedep; 8313 struct inodedep *inodedep; 8314 struct newdirblk *newdirblk = 0; 8315 struct mkdir *mkdir1, *mkdir2; 8316 struct worklist *wk; 8317 struct jaddref *jaddref; 8318 struct ufsmount *ump; 8319 struct mount *mp; 8320 8321 mp = dap->da_list.wk_mp; 8322 ump = VFSTOUFS(mp); 8323 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK, 8324 M_SOFTDEP_FLAGS); 8325 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8326 LIST_INIT(&newdirblk->db_mkdir); 8327 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8328 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp); 8329 mkdir1->md_state = ATTACHED | MKDIR_BODY; 8330 mkdir1->md_diradd = dap; 8331 mkdir1->md_jaddref = NULL; 8332 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8333 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp); 8334 mkdir2->md_state = ATTACHED | MKDIR_PARENT; 8335 mkdir2->md_diradd = dap; 8336 mkdir2->md_jaddref = NULL; 8337 if (MOUNTEDSUJ(mp) == 0) { 8338 mkdir1->md_state |= DEPCOMPLETE; 8339 mkdir2->md_state |= DEPCOMPLETE; 8340 } 8341 /* 8342 * Dependency on "." and ".." being written to disk. 8343 */ 8344 mkdir1->md_buf = newdirbp; 8345 ACQUIRE_LOCK(VFSTOUFS(mp)); 8346 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir1, md_mkdirs); 8347 /* 8348 * We must link the pagedep, allocdirect, and newdirblk for 8349 * the initial file page so the pointer to the new directory 8350 * is not written until the directory contents are live and 8351 * any subsequent additions are not marked live until the 8352 * block is reachable via the inode. 8353 */ 8354 if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0) 8355 panic("setup_newdir: lost pagedep"); 8356 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list) 8357 if (wk->wk_type == D_ALLOCDIRECT) 8358 break; 8359 if (wk == NULL) 8360 panic("setup_newdir: lost allocdirect"); 8361 if (pagedep->pd_state & NEWBLOCK) 8362 panic("setup_newdir: NEWBLOCK already set"); 8363 newblk = WK_NEWBLK(wk); 8364 pagedep->pd_state |= NEWBLOCK; 8365 pagedep->pd_newdirblk = newdirblk; 8366 newdirblk->db_pagedep = pagedep; 8367 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8368 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list); 8369 /* 8370 * Look up the inodedep for the parent directory so that we 8371 * can link mkdir2 into the pending dotdot jaddref or 8372 * the inode write if there is none. If the inode is 8373 * ALLCOMPLETE and no jaddref is present all dependencies have 8374 * been satisfied and mkdir2 can be freed. 8375 */ 8376 inodedep_lookup(mp, dinum, 0, &inodedep); 8377 if (MOUNTEDSUJ(mp)) { 8378 if (inodedep == NULL) 8379 panic("setup_newdir: Lost parent."); 8380 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8381 inoreflst); 8382 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum && 8383 (jaddref->ja_state & MKDIR_PARENT), 8384 ("setup_newdir: bad dotdot jaddref %p", jaddref)); 8385 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8386 mkdir2->md_jaddref = jaddref; 8387 jaddref->ja_mkdir = mkdir2; 8388 } else if (inodedep == NULL || 8389 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 8390 dap->da_state &= ~MKDIR_PARENT; 8391 WORKITEM_FREE(mkdir2, D_MKDIR); 8392 mkdir2 = NULL; 8393 } else { 8394 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8395 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list); 8396 } 8397 *mkdirp = mkdir2; 8398 8399 return (mkdir1); 8400} 8401 8402/* 8403 * Directory entry addition dependencies. 8404 * 8405 * When adding a new directory entry, the inode (with its incremented link 8406 * count) must be written to disk before the directory entry's pointer to it. 8407 * Also, if the inode is newly allocated, the corresponding freemap must be 8408 * updated (on disk) before the directory entry's pointer. These requirements 8409 * are met via undo/redo on the directory entry's pointer, which consists 8410 * simply of the inode number. 8411 * 8412 * As directory entries are added and deleted, the free space within a 8413 * directory block can become fragmented. The ufs filesystem will compact 8414 * a fragmented directory block to make space for a new entry. When this 8415 * occurs, the offsets of previously added entries change. Any "diradd" 8416 * dependency structures corresponding to these entries must be updated with 8417 * the new offsets. 8418 */ 8419 8420/* 8421 * This routine is called after the in-memory inode's link 8422 * count has been incremented, but before the directory entry's 8423 * pointer to the inode has been set. 8424 */ 8425int 8426softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 8427 struct buf *bp; /* buffer containing directory block */ 8428 struct inode *dp; /* inode for directory */ 8429 off_t diroffset; /* offset of new entry in directory */ 8430 ino_t newinum; /* inode referenced by new directory entry */ 8431 struct buf *newdirbp; /* non-NULL => contents of new mkdir */ 8432 int isnewblk; /* entry is in a newly allocated block */ 8433{ 8434 int offset; /* offset of new entry within directory block */ 8435 ufs_lbn_t lbn; /* block in directory containing new entry */ 8436 struct fs *fs; 8437 struct diradd *dap; 8438 struct newblk *newblk; 8439 struct pagedep *pagedep; 8440 struct inodedep *inodedep; 8441 struct newdirblk *newdirblk = 0; 8442 struct mkdir *mkdir1, *mkdir2; 8443 struct jaddref *jaddref; 8444 struct ufsmount *ump; 8445 struct mount *mp; 8446 int isindir; 8447 8448 ump = dp->i_ump; 8449 mp = UFSTOVFS(ump); 8450 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 8451 ("softdep_setup_directory_add called on non-softdep filesystem")); 8452 /* 8453 * Whiteouts have no dependencies. 8454 */ 8455 if (newinum == WINO) { 8456 if (newdirbp != NULL) 8457 bdwrite(newdirbp); 8458 return (0); 8459 } 8460 jaddref = NULL; 8461 mkdir1 = mkdir2 = NULL; 8462 fs = dp->i_fs; 8463 lbn = lblkno(fs, diroffset); 8464 offset = blkoff(fs, diroffset); 8465 dap = malloc(sizeof(struct diradd), M_DIRADD, 8466 M_SOFTDEP_FLAGS|M_ZERO); 8467 workitem_alloc(&dap->da_list, D_DIRADD, mp); 8468 dap->da_offset = offset; 8469 dap->da_newinum = newinum; 8470 dap->da_state = ATTACHED; 8471 LIST_INIT(&dap->da_jwork); 8472 isindir = bp->b_lblkno >= NDADDR; 8473 if (isnewblk && 8474 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) { 8475 newdirblk = malloc(sizeof(struct newdirblk), 8476 M_NEWDIRBLK, M_SOFTDEP_FLAGS); 8477 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8478 LIST_INIT(&newdirblk->db_mkdir); 8479 } 8480 /* 8481 * If we're creating a new directory setup the dependencies and set 8482 * the dap state to wait for them. Otherwise it's COMPLETE and 8483 * we can move on. 8484 */ 8485 if (newdirbp == NULL) { 8486 dap->da_state |= DEPCOMPLETE; 8487 ACQUIRE_LOCK(ump); 8488 } else { 8489 dap->da_state |= MKDIR_BODY | MKDIR_PARENT; 8490 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp, 8491 &mkdir2); 8492 } 8493 /* 8494 * Link into parent directory pagedep to await its being written. 8495 */ 8496 pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep); 8497#ifdef DEBUG 8498 if (diradd_lookup(pagedep, offset) != NULL) 8499 panic("softdep_setup_directory_add: %p already at off %d\n", 8500 diradd_lookup(pagedep, offset), offset); 8501#endif 8502 dap->da_pagedep = pagedep; 8503 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap, 8504 da_pdlist); 8505 inodedep_lookup(mp, newinum, DEPALLOC | NODELAY, &inodedep); 8506 /* 8507 * If we're journaling, link the diradd into the jaddref so it 8508 * may be completed after the journal entry is written. Otherwise, 8509 * link the diradd into its inodedep. If the inode is not yet 8510 * written place it on the bufwait list, otherwise do the post-inode 8511 * write processing to put it on the id_pendinghd list. 8512 */ 8513 if (MOUNTEDSUJ(mp)) { 8514 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8515 inoreflst); 8516 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 8517 ("softdep_setup_directory_add: bad jaddref %p", jaddref)); 8518 jaddref->ja_diroff = diroffset; 8519 jaddref->ja_diradd = dap; 8520 add_to_journal(&jaddref->ja_list); 8521 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) 8522 diradd_inode_written(dap, inodedep); 8523 else 8524 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 8525 /* 8526 * Add the journal entries for . and .. links now that the primary 8527 * link is written. 8528 */ 8529 if (mkdir1 != NULL && MOUNTEDSUJ(mp)) { 8530 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 8531 inoreflst, if_deps); 8532 KASSERT(jaddref != NULL && 8533 jaddref->ja_ino == jaddref->ja_parent && 8534 (jaddref->ja_state & MKDIR_BODY), 8535 ("softdep_setup_directory_add: bad dot jaddref %p", 8536 jaddref)); 8537 mkdir1->md_jaddref = jaddref; 8538 jaddref->ja_mkdir = mkdir1; 8539 /* 8540 * It is important that the dotdot journal entry 8541 * is added prior to the dot entry since dot writes 8542 * both the dot and dotdot links. These both must 8543 * be added after the primary link for the journal 8544 * to remain consistent. 8545 */ 8546 add_to_journal(&mkdir2->md_jaddref->ja_list); 8547 add_to_journal(&jaddref->ja_list); 8548 } 8549 /* 8550 * If we are adding a new directory remember this diradd so that if 8551 * we rename it we can keep the dot and dotdot dependencies. If 8552 * we are adding a new name for an inode that has a mkdiradd we 8553 * must be in rename and we have to move the dot and dotdot 8554 * dependencies to this new name. The old name is being orphaned 8555 * soon. 8556 */ 8557 if (mkdir1 != NULL) { 8558 if (inodedep->id_mkdiradd != NULL) 8559 panic("softdep_setup_directory_add: Existing mkdir"); 8560 inodedep->id_mkdiradd = dap; 8561 } else if (inodedep->id_mkdiradd) 8562 merge_diradd(inodedep, dap); 8563 if (newdirblk) { 8564 /* 8565 * There is nothing to do if we are already tracking 8566 * this block. 8567 */ 8568 if ((pagedep->pd_state & NEWBLOCK) != 0) { 8569 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 8570 FREE_LOCK(ump); 8571 return (0); 8572 } 8573 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk) 8574 == 0) 8575 panic("softdep_setup_directory_add: lost entry"); 8576 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8577 pagedep->pd_state |= NEWBLOCK; 8578 pagedep->pd_newdirblk = newdirblk; 8579 newdirblk->db_pagedep = pagedep; 8580 FREE_LOCK(ump); 8581 /* 8582 * If we extended into an indirect signal direnter to sync. 8583 */ 8584 if (isindir) 8585 return (1); 8586 return (0); 8587 } 8588 FREE_LOCK(ump); 8589 return (0); 8590} 8591 8592/* 8593 * This procedure is called to change the offset of a directory 8594 * entry when compacting a directory block which must be owned 8595 * exclusively by the caller. Note that the actual entry movement 8596 * must be done in this procedure to ensure that no I/O completions 8597 * occur while the move is in progress. 8598 */ 8599void 8600softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 8601 struct buf *bp; /* Buffer holding directory block. */ 8602 struct inode *dp; /* inode for directory */ 8603 caddr_t base; /* address of dp->i_offset */ 8604 caddr_t oldloc; /* address of old directory location */ 8605 caddr_t newloc; /* address of new directory location */ 8606 int entrysize; /* size of directory entry */ 8607{ 8608 int offset, oldoffset, newoffset; 8609 struct pagedep *pagedep; 8610 struct jmvref *jmvref; 8611 struct diradd *dap; 8612 struct direct *de; 8613 struct mount *mp; 8614 ufs_lbn_t lbn; 8615 int flags; 8616 8617 mp = UFSTOVFS(dp->i_ump); 8618 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 8619 ("softdep_change_directoryentry_offset called on " 8620 "non-softdep filesystem")); 8621 de = (struct direct *)oldloc; 8622 jmvref = NULL; 8623 flags = 0; 8624 /* 8625 * Moves are always journaled as it would be too complex to 8626 * determine if any affected adds or removes are present in the 8627 * journal. 8628 */ 8629 if (MOUNTEDSUJ(mp)) { 8630 flags = DEPALLOC; 8631 jmvref = newjmvref(dp, de->d_ino, 8632 dp->i_offset + (oldloc - base), 8633 dp->i_offset + (newloc - base)); 8634 } 8635 lbn = lblkno(dp->i_fs, dp->i_offset); 8636 offset = blkoff(dp->i_fs, dp->i_offset); 8637 oldoffset = offset + (oldloc - base); 8638 newoffset = offset + (newloc - base); 8639 ACQUIRE_LOCK(dp->i_ump); 8640 if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0) 8641 goto done; 8642 dap = diradd_lookup(pagedep, oldoffset); 8643 if (dap) { 8644 dap->da_offset = newoffset; 8645 newoffset = DIRADDHASH(newoffset); 8646 oldoffset = DIRADDHASH(oldoffset); 8647 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE && 8648 newoffset != oldoffset) { 8649 LIST_REMOVE(dap, da_pdlist); 8650 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset], 8651 dap, da_pdlist); 8652 } 8653 } 8654done: 8655 if (jmvref) { 8656 jmvref->jm_pagedep = pagedep; 8657 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps); 8658 add_to_journal(&jmvref->jm_list); 8659 } 8660 bcopy(oldloc, newloc, entrysize); 8661 FREE_LOCK(dp->i_ump); 8662} 8663 8664/* 8665 * Move the mkdir dependencies and journal work from one diradd to another 8666 * when renaming a directory. The new name must depend on the mkdir deps 8667 * completing as the old name did. Directories can only have one valid link 8668 * at a time so one must be canonical. 8669 */ 8670static void 8671merge_diradd(inodedep, newdap) 8672 struct inodedep *inodedep; 8673 struct diradd *newdap; 8674{ 8675 struct diradd *olddap; 8676 struct mkdir *mkdir, *nextmd; 8677 struct ufsmount *ump; 8678 short state; 8679 8680 olddap = inodedep->id_mkdiradd; 8681 inodedep->id_mkdiradd = newdap; 8682 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8683 newdap->da_state &= ~DEPCOMPLETE; 8684 ump = VFSTOUFS(inodedep->id_list.wk_mp); 8685 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 8686 mkdir = nextmd) { 8687 nextmd = LIST_NEXT(mkdir, md_mkdirs); 8688 if (mkdir->md_diradd != olddap) 8689 continue; 8690 mkdir->md_diradd = newdap; 8691 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY); 8692 newdap->da_state |= state; 8693 olddap->da_state &= ~state; 8694 if ((olddap->da_state & 8695 (MKDIR_PARENT | MKDIR_BODY)) == 0) 8696 break; 8697 } 8698 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 8699 panic("merge_diradd: unfound ref"); 8700 } 8701 /* 8702 * Any mkdir related journal items are not safe to be freed until 8703 * the new name is stable. 8704 */ 8705 jwork_move(&newdap->da_jwork, &olddap->da_jwork); 8706 olddap->da_state |= DEPCOMPLETE; 8707 complete_diradd(olddap); 8708} 8709 8710/* 8711 * Move the diradd to the pending list when all diradd dependencies are 8712 * complete. 8713 */ 8714static void 8715complete_diradd(dap) 8716 struct diradd *dap; 8717{ 8718 struct pagedep *pagedep; 8719 8720 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 8721 if (dap->da_state & DIRCHG) 8722 pagedep = dap->da_previous->dm_pagedep; 8723 else 8724 pagedep = dap->da_pagedep; 8725 LIST_REMOVE(dap, da_pdlist); 8726 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 8727 } 8728} 8729 8730/* 8731 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal 8732 * add entries and conditonally journal the remove. 8733 */ 8734static void 8735cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref) 8736 struct diradd *dap; 8737 struct dirrem *dirrem; 8738 struct jremref *jremref; 8739 struct jremref *dotremref; 8740 struct jremref *dotdotremref; 8741{ 8742 struct inodedep *inodedep; 8743 struct jaddref *jaddref; 8744 struct inoref *inoref; 8745 struct ufsmount *ump; 8746 struct mkdir *mkdir; 8747 8748 /* 8749 * If no remove references were allocated we're on a non-journaled 8750 * filesystem and can skip the cancel step. 8751 */ 8752 if (jremref == NULL) { 8753 free_diradd(dap, NULL); 8754 return; 8755 } 8756 /* 8757 * Cancel the primary name an free it if it does not require 8758 * journaling. 8759 */ 8760 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum, 8761 0, &inodedep) != 0) { 8762 /* Abort the addref that reference this diradd. */ 8763 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 8764 if (inoref->if_list.wk_type != D_JADDREF) 8765 continue; 8766 jaddref = (struct jaddref *)inoref; 8767 if (jaddref->ja_diradd != dap) 8768 continue; 8769 if (cancel_jaddref(jaddref, inodedep, 8770 &dirrem->dm_jwork) == 0) { 8771 free_jremref(jremref); 8772 jremref = NULL; 8773 } 8774 break; 8775 } 8776 } 8777 /* 8778 * Cancel subordinate names and free them if they do not require 8779 * journaling. 8780 */ 8781 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8782 ump = VFSTOUFS(dap->da_list.wk_mp); 8783 LIST_FOREACH(mkdir, &ump->softdep_mkdirlisthd, md_mkdirs) { 8784 if (mkdir->md_diradd != dap) 8785 continue; 8786 if ((jaddref = mkdir->md_jaddref) == NULL) 8787 continue; 8788 mkdir->md_jaddref = NULL; 8789 if (mkdir->md_state & MKDIR_PARENT) { 8790 if (cancel_jaddref(jaddref, NULL, 8791 &dirrem->dm_jwork) == 0) { 8792 free_jremref(dotdotremref); 8793 dotdotremref = NULL; 8794 } 8795 } else { 8796 if (cancel_jaddref(jaddref, inodedep, 8797 &dirrem->dm_jwork) == 0) { 8798 free_jremref(dotremref); 8799 dotremref = NULL; 8800 } 8801 } 8802 } 8803 } 8804 8805 if (jremref) 8806 journal_jremref(dirrem, jremref, inodedep); 8807 if (dotremref) 8808 journal_jremref(dirrem, dotremref, inodedep); 8809 if (dotdotremref) 8810 journal_jremref(dirrem, dotdotremref, NULL); 8811 jwork_move(&dirrem->dm_jwork, &dap->da_jwork); 8812 free_diradd(dap, &dirrem->dm_jwork); 8813} 8814 8815/* 8816 * Free a diradd dependency structure. This routine must be called 8817 * with splbio interrupts blocked. 8818 */ 8819static void 8820free_diradd(dap, wkhd) 8821 struct diradd *dap; 8822 struct workhead *wkhd; 8823{ 8824 struct dirrem *dirrem; 8825 struct pagedep *pagedep; 8826 struct inodedep *inodedep; 8827 struct mkdir *mkdir, *nextmd; 8828 struct ufsmount *ump; 8829 8830 ump = VFSTOUFS(dap->da_list.wk_mp); 8831 LOCK_OWNED(ump); 8832 LIST_REMOVE(dap, da_pdlist); 8833 if (dap->da_state & ONWORKLIST) 8834 WORKLIST_REMOVE(&dap->da_list); 8835 if ((dap->da_state & DIRCHG) == 0) { 8836 pagedep = dap->da_pagedep; 8837 } else { 8838 dirrem = dap->da_previous; 8839 pagedep = dirrem->dm_pagedep; 8840 dirrem->dm_dirinum = pagedep->pd_ino; 8841 dirrem->dm_state |= COMPLETE; 8842 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 8843 add_to_worklist(&dirrem->dm_list, 0); 8844 } 8845 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum, 8846 0, &inodedep) != 0) 8847 if (inodedep->id_mkdiradd == dap) 8848 inodedep->id_mkdiradd = NULL; 8849 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8850 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 8851 mkdir = nextmd) { 8852 nextmd = LIST_NEXT(mkdir, md_mkdirs); 8853 if (mkdir->md_diradd != dap) 8854 continue; 8855 dap->da_state &= 8856 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 8857 LIST_REMOVE(mkdir, md_mkdirs); 8858 if (mkdir->md_state & ONWORKLIST) 8859 WORKLIST_REMOVE(&mkdir->md_list); 8860 if (mkdir->md_jaddref != NULL) 8861 panic("free_diradd: Unexpected jaddref"); 8862 WORKITEM_FREE(mkdir, D_MKDIR); 8863 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) 8864 break; 8865 } 8866 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 8867 panic("free_diradd: unfound ref"); 8868 } 8869 if (inodedep) 8870 free_inodedep(inodedep); 8871 /* 8872 * Free any journal segments waiting for the directory write. 8873 */ 8874 handle_jwork(&dap->da_jwork); 8875 WORKITEM_FREE(dap, D_DIRADD); 8876} 8877 8878/* 8879 * Directory entry removal dependencies. 8880 * 8881 * When removing a directory entry, the entry's inode pointer must be 8882 * zero'ed on disk before the corresponding inode's link count is decremented 8883 * (possibly freeing the inode for re-use). This dependency is handled by 8884 * updating the directory entry but delaying the inode count reduction until 8885 * after the directory block has been written to disk. After this point, the 8886 * inode count can be decremented whenever it is convenient. 8887 */ 8888 8889/* 8890 * This routine should be called immediately after removing 8891 * a directory entry. The inode's link count should not be 8892 * decremented by the calling procedure -- the soft updates 8893 * code will do this task when it is safe. 8894 */ 8895void 8896softdep_setup_remove(bp, dp, ip, isrmdir) 8897 struct buf *bp; /* buffer containing directory block */ 8898 struct inode *dp; /* inode for the directory being modified */ 8899 struct inode *ip; /* inode for directory entry being removed */ 8900 int isrmdir; /* indicates if doing RMDIR */ 8901{ 8902 struct dirrem *dirrem, *prevdirrem; 8903 struct inodedep *inodedep; 8904 int direct; 8905 8906 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0, 8907 ("softdep_setup_remove called on non-softdep filesystem")); 8908 /* 8909 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want 8910 * newdirrem() to setup the full directory remove which requires 8911 * isrmdir > 1. 8912 */ 8913 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 8914 /* 8915 * Add the dirrem to the inodedep's pending remove list for quick 8916 * discovery later. 8917 */ 8918 if (inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0, 8919 &inodedep) == 0) 8920 panic("softdep_setup_remove: Lost inodedep."); 8921 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 8922 dirrem->dm_state |= ONDEPLIST; 8923 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 8924 8925 /* 8926 * If the COMPLETE flag is clear, then there were no active 8927 * entries and we want to roll back to a zeroed entry until 8928 * the new inode is committed to disk. If the COMPLETE flag is 8929 * set then we have deleted an entry that never made it to 8930 * disk. If the entry we deleted resulted from a name change, 8931 * then the old name still resides on disk. We cannot delete 8932 * its inode (returned to us in prevdirrem) until the zeroed 8933 * directory entry gets to disk. The new inode has never been 8934 * referenced on the disk, so can be deleted immediately. 8935 */ 8936 if ((dirrem->dm_state & COMPLETE) == 0) { 8937 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem, 8938 dm_next); 8939 FREE_LOCK(ip->i_ump); 8940 } else { 8941 if (prevdirrem != NULL) 8942 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, 8943 prevdirrem, dm_next); 8944 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino; 8945 direct = LIST_EMPTY(&dirrem->dm_jremrefhd); 8946 FREE_LOCK(ip->i_ump); 8947 if (direct) 8948 handle_workitem_remove(dirrem, 0); 8949 } 8950} 8951 8952/* 8953 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the 8954 * pd_pendinghd list of a pagedep. 8955 */ 8956static struct diradd * 8957diradd_lookup(pagedep, offset) 8958 struct pagedep *pagedep; 8959 int offset; 8960{ 8961 struct diradd *dap; 8962 8963 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist) 8964 if (dap->da_offset == offset) 8965 return (dap); 8966 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 8967 if (dap->da_offset == offset) 8968 return (dap); 8969 return (NULL); 8970} 8971 8972/* 8973 * Search for a .. diradd dependency in a directory that is being removed. 8974 * If the directory was renamed to a new parent we have a diradd rather 8975 * than a mkdir for the .. entry. We need to cancel it now before 8976 * it is found in truncate(). 8977 */ 8978static struct jremref * 8979cancel_diradd_dotdot(ip, dirrem, jremref) 8980 struct inode *ip; 8981 struct dirrem *dirrem; 8982 struct jremref *jremref; 8983{ 8984 struct pagedep *pagedep; 8985 struct diradd *dap; 8986 struct worklist *wk; 8987 8988 if (pagedep_lookup(UFSTOVFS(ip->i_ump), NULL, ip->i_number, 0, 0, 8989 &pagedep) == 0) 8990 return (jremref); 8991 dap = diradd_lookup(pagedep, DOTDOT_OFFSET); 8992 if (dap == NULL) 8993 return (jremref); 8994 cancel_diradd(dap, dirrem, jremref, NULL, NULL); 8995 /* 8996 * Mark any journal work as belonging to the parent so it is freed 8997 * with the .. reference. 8998 */ 8999 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9000 wk->wk_state |= MKDIR_PARENT; 9001 return (NULL); 9002} 9003 9004/* 9005 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to 9006 * replace it with a dirrem/diradd pair as a result of re-parenting a 9007 * directory. This ensures that we don't simultaneously have a mkdir and 9008 * a diradd for the same .. entry. 9009 */ 9010static struct jremref * 9011cancel_mkdir_dotdot(ip, dirrem, jremref) 9012 struct inode *ip; 9013 struct dirrem *dirrem; 9014 struct jremref *jremref; 9015{ 9016 struct inodedep *inodedep; 9017 struct jaddref *jaddref; 9018 struct ufsmount *ump; 9019 struct mkdir *mkdir; 9020 struct diradd *dap; 9021 9022 if (inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0, 9023 &inodedep) == 0) 9024 return (jremref); 9025 dap = inodedep->id_mkdiradd; 9026 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0) 9027 return (jremref); 9028 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9029 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 9030 mkdir = LIST_NEXT(mkdir, md_mkdirs)) 9031 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT) 9032 break; 9033 if (mkdir == NULL) 9034 panic("cancel_mkdir_dotdot: Unable to find mkdir\n"); 9035 if ((jaddref = mkdir->md_jaddref) != NULL) { 9036 mkdir->md_jaddref = NULL; 9037 jaddref->ja_state &= ~MKDIR_PARENT; 9038 if (inodedep_lookup(UFSTOVFS(ip->i_ump), jaddref->ja_ino, 0, 9039 &inodedep) == 0) 9040 panic("cancel_mkdir_dotdot: Lost parent inodedep"); 9041 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) { 9042 journal_jremref(dirrem, jremref, inodedep); 9043 jremref = NULL; 9044 } 9045 } 9046 if (mkdir->md_state & ONWORKLIST) 9047 WORKLIST_REMOVE(&mkdir->md_list); 9048 mkdir->md_state |= ALLCOMPLETE; 9049 complete_mkdir(mkdir); 9050 return (jremref); 9051} 9052 9053static void 9054journal_jremref(dirrem, jremref, inodedep) 9055 struct dirrem *dirrem; 9056 struct jremref *jremref; 9057 struct inodedep *inodedep; 9058{ 9059 9060 if (inodedep == NULL) 9061 if (inodedep_lookup(jremref->jr_list.wk_mp, 9062 jremref->jr_ref.if_ino, 0, &inodedep) == 0) 9063 panic("journal_jremref: Lost inodedep"); 9064 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps); 9065 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 9066 add_to_journal(&jremref->jr_list); 9067} 9068 9069static void 9070dirrem_journal(dirrem, jremref, dotremref, dotdotremref) 9071 struct dirrem *dirrem; 9072 struct jremref *jremref; 9073 struct jremref *dotremref; 9074 struct jremref *dotdotremref; 9075{ 9076 struct inodedep *inodedep; 9077 9078 9079 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0, 9080 &inodedep) == 0) 9081 panic("dirrem_journal: Lost inodedep"); 9082 journal_jremref(dirrem, jremref, inodedep); 9083 if (dotremref) 9084 journal_jremref(dirrem, dotremref, inodedep); 9085 if (dotdotremref) 9086 journal_jremref(dirrem, dotdotremref, NULL); 9087} 9088 9089/* 9090 * Allocate a new dirrem if appropriate and return it along with 9091 * its associated pagedep. Called without a lock, returns with lock. 9092 */ 9093static struct dirrem * 9094newdirrem(bp, dp, ip, isrmdir, prevdirremp) 9095 struct buf *bp; /* buffer containing directory block */ 9096 struct inode *dp; /* inode for the directory being modified */ 9097 struct inode *ip; /* inode for directory entry being removed */ 9098 int isrmdir; /* indicates if doing RMDIR */ 9099 struct dirrem **prevdirremp; /* previously referenced inode, if any */ 9100{ 9101 int offset; 9102 ufs_lbn_t lbn; 9103 struct diradd *dap; 9104 struct dirrem *dirrem; 9105 struct pagedep *pagedep; 9106 struct jremref *jremref; 9107 struct jremref *dotremref; 9108 struct jremref *dotdotremref; 9109 struct vnode *dvp; 9110 9111 /* 9112 * Whiteouts have no deletion dependencies. 9113 */ 9114 if (ip == NULL) 9115 panic("newdirrem: whiteout"); 9116 dvp = ITOV(dp); 9117 /* 9118 * If the system is over its limit and our filesystem is 9119 * responsible for more than our share of that usage and 9120 * we are not a snapshot, request some inodedep cleanup. 9121 * Limiting the number of dirrem structures will also limit 9122 * the number of freefile and freeblks structures. 9123 */ 9124 ACQUIRE_LOCK(ip->i_ump); 9125 while (!IS_SNAPSHOT(ip) && dep_current[D_DIRREM] > max_softdeps / 2 && 9126 ip->i_ump->softdep_curdeps[D_DIRREM] > 9127 (max_softdeps / 2) / stat_flush_threads) 9128 (void) request_cleanup(ITOV(dp)->v_mount, FLUSH_BLOCKS); 9129 FREE_LOCK(ip->i_ump); 9130 dirrem = malloc(sizeof(struct dirrem), 9131 M_DIRREM, M_SOFTDEP_FLAGS|M_ZERO); 9132 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount); 9133 LIST_INIT(&dirrem->dm_jremrefhd); 9134 LIST_INIT(&dirrem->dm_jwork); 9135 dirrem->dm_state = isrmdir ? RMDIR : 0; 9136 dirrem->dm_oldinum = ip->i_number; 9137 *prevdirremp = NULL; 9138 /* 9139 * Allocate remove reference structures to track journal write 9140 * dependencies. We will always have one for the link and 9141 * when doing directories we will always have one more for dot. 9142 * When renaming a directory we skip the dotdot link change so 9143 * this is not needed. 9144 */ 9145 jremref = dotremref = dotdotremref = NULL; 9146 if (DOINGSUJ(dvp)) { 9147 if (isrmdir) { 9148 jremref = newjremref(dirrem, dp, ip, dp->i_offset, 9149 ip->i_effnlink + 2); 9150 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET, 9151 ip->i_effnlink + 1); 9152 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET, 9153 dp->i_effnlink + 1); 9154 dotdotremref->jr_state |= MKDIR_PARENT; 9155 } else 9156 jremref = newjremref(dirrem, dp, ip, dp->i_offset, 9157 ip->i_effnlink + 1); 9158 } 9159 ACQUIRE_LOCK(ip->i_ump); 9160 lbn = lblkno(dp->i_fs, dp->i_offset); 9161 offset = blkoff(dp->i_fs, dp->i_offset); 9162 pagedep_lookup(UFSTOVFS(dp->i_ump), bp, dp->i_number, lbn, DEPALLOC, 9163 &pagedep); 9164 dirrem->dm_pagedep = pagedep; 9165 dirrem->dm_offset = offset; 9166 /* 9167 * If we're renaming a .. link to a new directory, cancel any 9168 * existing MKDIR_PARENT mkdir. If it has already been canceled 9169 * the jremref is preserved for any potential diradd in this 9170 * location. This can not coincide with a rmdir. 9171 */ 9172 if (dp->i_offset == DOTDOT_OFFSET) { 9173 if (isrmdir) 9174 panic("newdirrem: .. directory change during remove?"); 9175 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref); 9176 } 9177 /* 9178 * If we're removing a directory search for the .. dependency now and 9179 * cancel it. Any pending journal work will be added to the dirrem 9180 * to be completed when the workitem remove completes. 9181 */ 9182 if (isrmdir) 9183 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref); 9184 /* 9185 * Check for a diradd dependency for the same directory entry. 9186 * If present, then both dependencies become obsolete and can 9187 * be de-allocated. 9188 */ 9189 dap = diradd_lookup(pagedep, offset); 9190 if (dap == NULL) { 9191 /* 9192 * Link the jremref structures into the dirrem so they are 9193 * written prior to the pagedep. 9194 */ 9195 if (jremref) 9196 dirrem_journal(dirrem, jremref, dotremref, 9197 dotdotremref); 9198 return (dirrem); 9199 } 9200 /* 9201 * Must be ATTACHED at this point. 9202 */ 9203 if ((dap->da_state & ATTACHED) == 0) 9204 panic("newdirrem: not ATTACHED"); 9205 if (dap->da_newinum != ip->i_number) 9206 panic("newdirrem: inum %ju should be %ju", 9207 (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum); 9208 /* 9209 * If we are deleting a changed name that never made it to disk, 9210 * then return the dirrem describing the previous inode (which 9211 * represents the inode currently referenced from this entry on disk). 9212 */ 9213 if ((dap->da_state & DIRCHG) != 0) { 9214 *prevdirremp = dap->da_previous; 9215 dap->da_state &= ~DIRCHG; 9216 dap->da_pagedep = pagedep; 9217 } 9218 /* 9219 * We are deleting an entry that never made it to disk. 9220 * Mark it COMPLETE so we can delete its inode immediately. 9221 */ 9222 dirrem->dm_state |= COMPLETE; 9223 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref); 9224#ifdef SUJ_DEBUG 9225 if (isrmdir == 0) { 9226 struct worklist *wk; 9227 9228 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9229 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT)) 9230 panic("bad wk %p (0x%X)\n", wk, wk->wk_state); 9231 } 9232#endif 9233 9234 return (dirrem); 9235} 9236 9237/* 9238 * Directory entry change dependencies. 9239 * 9240 * Changing an existing directory entry requires that an add operation 9241 * be completed first followed by a deletion. The semantics for the addition 9242 * are identical to the description of adding a new entry above except 9243 * that the rollback is to the old inode number rather than zero. Once 9244 * the addition dependency is completed, the removal is done as described 9245 * in the removal routine above. 9246 */ 9247 9248/* 9249 * This routine should be called immediately after changing 9250 * a directory entry. The inode's link count should not be 9251 * decremented by the calling procedure -- the soft updates 9252 * code will perform this task when it is safe. 9253 */ 9254void 9255softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 9256 struct buf *bp; /* buffer containing directory block */ 9257 struct inode *dp; /* inode for the directory being modified */ 9258 struct inode *ip; /* inode for directory entry being removed */ 9259 ino_t newinum; /* new inode number for changed entry */ 9260 int isrmdir; /* indicates if doing RMDIR */ 9261{ 9262 int offset; 9263 struct diradd *dap = NULL; 9264 struct dirrem *dirrem, *prevdirrem; 9265 struct pagedep *pagedep; 9266 struct inodedep *inodedep; 9267 struct jaddref *jaddref; 9268 struct mount *mp; 9269 9270 offset = blkoff(dp->i_fs, dp->i_offset); 9271 mp = UFSTOVFS(dp->i_ump); 9272 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 9273 ("softdep_setup_directory_change called on non-softdep filesystem")); 9274 9275 /* 9276 * Whiteouts do not need diradd dependencies. 9277 */ 9278 if (newinum != WINO) { 9279 dap = malloc(sizeof(struct diradd), 9280 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO); 9281 workitem_alloc(&dap->da_list, D_DIRADD, mp); 9282 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE; 9283 dap->da_offset = offset; 9284 dap->da_newinum = newinum; 9285 LIST_INIT(&dap->da_jwork); 9286 } 9287 9288 /* 9289 * Allocate a new dirrem and ACQUIRE_LOCK. 9290 */ 9291 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 9292 pagedep = dirrem->dm_pagedep; 9293 /* 9294 * The possible values for isrmdir: 9295 * 0 - non-directory file rename 9296 * 1 - directory rename within same directory 9297 * inum - directory rename to new directory of given inode number 9298 * When renaming to a new directory, we are both deleting and 9299 * creating a new directory entry, so the link count on the new 9300 * directory should not change. Thus we do not need the followup 9301 * dirrem which is usually done in handle_workitem_remove. We set 9302 * the DIRCHG flag to tell handle_workitem_remove to skip the 9303 * followup dirrem. 9304 */ 9305 if (isrmdir > 1) 9306 dirrem->dm_state |= DIRCHG; 9307 9308 /* 9309 * Whiteouts have no additional dependencies, 9310 * so just put the dirrem on the correct list. 9311 */ 9312 if (newinum == WINO) { 9313 if ((dirrem->dm_state & COMPLETE) == 0) { 9314 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem, 9315 dm_next); 9316 } else { 9317 dirrem->dm_dirinum = pagedep->pd_ino; 9318 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9319 add_to_worklist(&dirrem->dm_list, 0); 9320 } 9321 FREE_LOCK(dp->i_ump); 9322 return; 9323 } 9324 /* 9325 * Add the dirrem to the inodedep's pending remove list for quick 9326 * discovery later. A valid nlinkdelta ensures that this lookup 9327 * will not fail. 9328 */ 9329 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9330 panic("softdep_setup_directory_change: Lost inodedep."); 9331 dirrem->dm_state |= ONDEPLIST; 9332 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9333 9334 /* 9335 * If the COMPLETE flag is clear, then there were no active 9336 * entries and we want to roll back to the previous inode until 9337 * the new inode is committed to disk. If the COMPLETE flag is 9338 * set, then we have deleted an entry that never made it to disk. 9339 * If the entry we deleted resulted from a name change, then the old 9340 * inode reference still resides on disk. Any rollback that we do 9341 * needs to be to that old inode (returned to us in prevdirrem). If 9342 * the entry we deleted resulted from a create, then there is 9343 * no entry on the disk, so we want to roll back to zero rather 9344 * than the uncommitted inode. In either of the COMPLETE cases we 9345 * want to immediately free the unwritten and unreferenced inode. 9346 */ 9347 if ((dirrem->dm_state & COMPLETE) == 0) { 9348 dap->da_previous = dirrem; 9349 } else { 9350 if (prevdirrem != NULL) { 9351 dap->da_previous = prevdirrem; 9352 } else { 9353 dap->da_state &= ~DIRCHG; 9354 dap->da_pagedep = pagedep; 9355 } 9356 dirrem->dm_dirinum = pagedep->pd_ino; 9357 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9358 add_to_worklist(&dirrem->dm_list, 0); 9359 } 9360 /* 9361 * Lookup the jaddref for this journal entry. We must finish 9362 * initializing it and make the diradd write dependent on it. 9363 * If we're not journaling, put it on the id_bufwait list if the 9364 * inode is not yet written. If it is written, do the post-inode 9365 * write processing to put it on the id_pendinghd list. 9366 */ 9367 inodedep_lookup(mp, newinum, DEPALLOC | NODELAY, &inodedep); 9368 if (MOUNTEDSUJ(mp)) { 9369 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 9370 inoreflst); 9371 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 9372 ("softdep_setup_directory_change: bad jaddref %p", 9373 jaddref)); 9374 jaddref->ja_diroff = dp->i_offset; 9375 jaddref->ja_diradd = dap; 9376 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9377 dap, da_pdlist); 9378 add_to_journal(&jaddref->ja_list); 9379 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 9380 dap->da_state |= COMPLETE; 9381 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 9382 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 9383 } else { 9384 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9385 dap, da_pdlist); 9386 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 9387 } 9388 /* 9389 * If we're making a new name for a directory that has not been 9390 * committed when need to move the dot and dotdot references to 9391 * this new name. 9392 */ 9393 if (inodedep->id_mkdiradd && dp->i_offset != DOTDOT_OFFSET) 9394 merge_diradd(inodedep, dap); 9395 FREE_LOCK(dp->i_ump); 9396} 9397 9398/* 9399 * Called whenever the link count on an inode is changed. 9400 * It creates an inode dependency so that the new reference(s) 9401 * to the inode cannot be committed to disk until the updated 9402 * inode has been written. 9403 */ 9404void 9405softdep_change_linkcnt(ip) 9406 struct inode *ip; /* the inode with the increased link count */ 9407{ 9408 struct inodedep *inodedep; 9409 int dflags; 9410 9411 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0, 9412 ("softdep_change_linkcnt called on non-softdep filesystem")); 9413 ACQUIRE_LOCK(ip->i_ump); 9414 dflags = DEPALLOC; 9415 if (IS_SNAPSHOT(ip)) 9416 dflags |= NODELAY; 9417 inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, dflags, &inodedep); 9418 if (ip->i_nlink < ip->i_effnlink) 9419 panic("softdep_change_linkcnt: bad delta"); 9420 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9421 FREE_LOCK(ip->i_ump); 9422} 9423 9424/* 9425 * Attach a sbdep dependency to the superblock buf so that we can keep 9426 * track of the head of the linked list of referenced but unlinked inodes. 9427 */ 9428void 9429softdep_setup_sbupdate(ump, fs, bp) 9430 struct ufsmount *ump; 9431 struct fs *fs; 9432 struct buf *bp; 9433{ 9434 struct sbdep *sbdep; 9435 struct worklist *wk; 9436 9437 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9438 ("softdep_setup_sbupdate called on non-softdep filesystem")); 9439 LIST_FOREACH(wk, &bp->b_dep, wk_list) 9440 if (wk->wk_type == D_SBDEP) 9441 break; 9442 if (wk != NULL) 9443 return; 9444 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS); 9445 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump)); 9446 sbdep->sb_fs = fs; 9447 sbdep->sb_ump = ump; 9448 ACQUIRE_LOCK(ump); 9449 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list); 9450 FREE_LOCK(ump); 9451} 9452 9453/* 9454 * Return the first unlinked inodedep which is ready to be the head of the 9455 * list. The inodedep and all those after it must have valid next pointers. 9456 */ 9457static struct inodedep * 9458first_unlinked_inodedep(ump) 9459 struct ufsmount *ump; 9460{ 9461 struct inodedep *inodedep; 9462 struct inodedep *idp; 9463 9464 LOCK_OWNED(ump); 9465 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst); 9466 inodedep; inodedep = idp) { 9467 if ((inodedep->id_state & UNLINKNEXT) == 0) 9468 return (NULL); 9469 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9470 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0) 9471 break; 9472 if ((inodedep->id_state & UNLINKPREV) == 0) 9473 break; 9474 } 9475 return (inodedep); 9476} 9477 9478/* 9479 * Set the sujfree unlinked head pointer prior to writing a superblock. 9480 */ 9481static void 9482initiate_write_sbdep(sbdep) 9483 struct sbdep *sbdep; 9484{ 9485 struct inodedep *inodedep; 9486 struct fs *bpfs; 9487 struct fs *fs; 9488 9489 bpfs = sbdep->sb_fs; 9490 fs = sbdep->sb_ump->um_fs; 9491 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9492 if (inodedep) { 9493 fs->fs_sujfree = inodedep->id_ino; 9494 inodedep->id_state |= UNLINKPREV; 9495 } else 9496 fs->fs_sujfree = 0; 9497 bpfs->fs_sujfree = fs->fs_sujfree; 9498} 9499 9500/* 9501 * After a superblock is written determine whether it must be written again 9502 * due to a changing unlinked list head. 9503 */ 9504static int 9505handle_written_sbdep(sbdep, bp) 9506 struct sbdep *sbdep; 9507 struct buf *bp; 9508{ 9509 struct inodedep *inodedep; 9510 struct fs *fs; 9511 9512 LOCK_OWNED(sbdep->sb_ump); 9513 fs = sbdep->sb_fs; 9514 /* 9515 * If the superblock doesn't match the in-memory list start over. 9516 */ 9517 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9518 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) || 9519 (inodedep == NULL && fs->fs_sujfree != 0)) { 9520 bdirty(bp); 9521 return (1); 9522 } 9523 WORKITEM_FREE(sbdep, D_SBDEP); 9524 if (fs->fs_sujfree == 0) 9525 return (0); 9526 /* 9527 * Now that we have a record of this inode in stable store allow it 9528 * to be written to free up pending work. Inodes may see a lot of 9529 * write activity after they are unlinked which we must not hold up. 9530 */ 9531 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 9532 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS) 9533 panic("handle_written_sbdep: Bad inodedep %p (0x%X)", 9534 inodedep, inodedep->id_state); 9535 if (inodedep->id_state & UNLINKONLIST) 9536 break; 9537 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST; 9538 } 9539 9540 return (0); 9541} 9542 9543/* 9544 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list. 9545 */ 9546static void 9547unlinked_inodedep(mp, inodedep) 9548 struct mount *mp; 9549 struct inodedep *inodedep; 9550{ 9551 struct ufsmount *ump; 9552 9553 ump = VFSTOUFS(mp); 9554 LOCK_OWNED(ump); 9555 if (MOUNTEDSUJ(mp) == 0) 9556 return; 9557 ump->um_fs->fs_fmod = 1; 9558 if (inodedep->id_state & UNLINKED) 9559 panic("unlinked_inodedep: %p already unlinked\n", inodedep); 9560 inodedep->id_state |= UNLINKED; 9561 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked); 9562} 9563 9564/* 9565 * Remove an inodedep from the unlinked inodedep list. This may require 9566 * disk writes if the inode has made it that far. 9567 */ 9568static void 9569clear_unlinked_inodedep(inodedep) 9570 struct inodedep *inodedep; 9571{ 9572 struct ufsmount *ump; 9573 struct inodedep *idp; 9574 struct inodedep *idn; 9575 struct fs *fs; 9576 struct buf *bp; 9577 ino_t ino; 9578 ino_t nino; 9579 ino_t pino; 9580 int error; 9581 9582 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9583 fs = ump->um_fs; 9584 ino = inodedep->id_ino; 9585 error = 0; 9586 for (;;) { 9587 LOCK_OWNED(ump); 9588 KASSERT((inodedep->id_state & UNLINKED) != 0, 9589 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9590 inodedep)); 9591 /* 9592 * If nothing has yet been written simply remove us from 9593 * the in memory list and return. This is the most common 9594 * case where handle_workitem_remove() loses the final 9595 * reference. 9596 */ 9597 if ((inodedep->id_state & UNLINKLINKS) == 0) 9598 break; 9599 /* 9600 * If we have a NEXT pointer and no PREV pointer we can simply 9601 * clear NEXT's PREV and remove ourselves from the list. Be 9602 * careful not to clear PREV if the superblock points at 9603 * next as well. 9604 */ 9605 idn = TAILQ_NEXT(inodedep, id_unlinked); 9606 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) { 9607 if (idn && fs->fs_sujfree != idn->id_ino) 9608 idn->id_state &= ~UNLINKPREV; 9609 break; 9610 } 9611 /* 9612 * Here we have an inodedep which is actually linked into 9613 * the list. We must remove it by forcing a write to the 9614 * link before us, whether it be the superblock or an inode. 9615 * Unfortunately the list may change while we're waiting 9616 * on the buf lock for either resource so we must loop until 9617 * we lock the right one. If both the superblock and an 9618 * inode point to this inode we must clear the inode first 9619 * followed by the superblock. 9620 */ 9621 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9622 pino = 0; 9623 if (idp && (idp->id_state & UNLINKNEXT)) 9624 pino = idp->id_ino; 9625 FREE_LOCK(ump); 9626 if (pino == 0) { 9627 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9628 (int)fs->fs_sbsize, 0, 0, 0); 9629 } else { 9630 error = bread(ump->um_devvp, 9631 fsbtodb(fs, ino_to_fsba(fs, pino)), 9632 (int)fs->fs_bsize, NOCRED, &bp); 9633 if (error) 9634 brelse(bp); 9635 } 9636 ACQUIRE_LOCK(ump); 9637 if (error) 9638 break; 9639 /* If the list has changed restart the loop. */ 9640 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9641 nino = 0; 9642 if (idp && (idp->id_state & UNLINKNEXT)) 9643 nino = idp->id_ino; 9644 if (nino != pino || 9645 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) { 9646 FREE_LOCK(ump); 9647 brelse(bp); 9648 ACQUIRE_LOCK(ump); 9649 continue; 9650 } 9651 nino = 0; 9652 idn = TAILQ_NEXT(inodedep, id_unlinked); 9653 if (idn) 9654 nino = idn->id_ino; 9655 /* 9656 * Remove us from the in memory list. After this we cannot 9657 * access the inodedep. 9658 */ 9659 KASSERT((inodedep->id_state & UNLINKED) != 0, 9660 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9661 inodedep)); 9662 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9663 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9664 FREE_LOCK(ump); 9665 /* 9666 * The predecessor's next pointer is manually updated here 9667 * so that the NEXT flag is never cleared for an element 9668 * that is in the list. 9669 */ 9670 if (pino == 0) { 9671 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 9672 ffs_oldfscompat_write((struct fs *)bp->b_data, ump); 9673 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, 9674 bp); 9675 } else if (fs->fs_magic == FS_UFS1_MAGIC) 9676 ((struct ufs1_dinode *)bp->b_data + 9677 ino_to_fsbo(fs, pino))->di_freelink = nino; 9678 else 9679 ((struct ufs2_dinode *)bp->b_data + 9680 ino_to_fsbo(fs, pino))->di_freelink = nino; 9681 /* 9682 * If the bwrite fails we have no recourse to recover. The 9683 * filesystem is corrupted already. 9684 */ 9685 bwrite(bp); 9686 ACQUIRE_LOCK(ump); 9687 /* 9688 * If the superblock pointer still needs to be cleared force 9689 * a write here. 9690 */ 9691 if (fs->fs_sujfree == ino) { 9692 FREE_LOCK(ump); 9693 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9694 (int)fs->fs_sbsize, 0, 0, 0); 9695 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 9696 ffs_oldfscompat_write((struct fs *)bp->b_data, ump); 9697 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, 9698 bp); 9699 bwrite(bp); 9700 ACQUIRE_LOCK(ump); 9701 } 9702 9703 if (fs->fs_sujfree != ino) 9704 return; 9705 panic("clear_unlinked_inodedep: Failed to clear free head"); 9706 } 9707 if (inodedep->id_ino == fs->fs_sujfree) 9708 panic("clear_unlinked_inodedep: Freeing head of free list"); 9709 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9710 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9711 return; 9712} 9713 9714/* 9715 * This workitem decrements the inode's link count. 9716 * If the link count reaches zero, the file is removed. 9717 */ 9718static int 9719handle_workitem_remove(dirrem, flags) 9720 struct dirrem *dirrem; 9721 int flags; 9722{ 9723 struct inodedep *inodedep; 9724 struct workhead dotdotwk; 9725 struct worklist *wk; 9726 struct ufsmount *ump; 9727 struct mount *mp; 9728 struct vnode *vp; 9729 struct inode *ip; 9730 ino_t oldinum; 9731 9732 if (dirrem->dm_state & ONWORKLIST) 9733 panic("handle_workitem_remove: dirrem %p still on worklist", 9734 dirrem); 9735 oldinum = dirrem->dm_oldinum; 9736 mp = dirrem->dm_list.wk_mp; 9737 ump = VFSTOUFS(mp); 9738 flags |= LK_EXCLUSIVE; 9739 if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ) != 0) 9740 return (EBUSY); 9741 ip = VTOI(vp); 9742 ACQUIRE_LOCK(ump); 9743 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0) 9744 panic("handle_workitem_remove: lost inodedep"); 9745 if (dirrem->dm_state & ONDEPLIST) 9746 LIST_REMOVE(dirrem, dm_inonext); 9747 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 9748 ("handle_workitem_remove: Journal entries not written.")); 9749 9750 /* 9751 * Move all dependencies waiting on the remove to complete 9752 * from the dirrem to the inode inowait list to be completed 9753 * after the inode has been updated and written to disk. Any 9754 * marked MKDIR_PARENT are saved to be completed when the .. ref 9755 * is removed. 9756 */ 9757 LIST_INIT(&dotdotwk); 9758 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) { 9759 WORKLIST_REMOVE(wk); 9760 if (wk->wk_state & MKDIR_PARENT) { 9761 wk->wk_state &= ~MKDIR_PARENT; 9762 WORKLIST_INSERT(&dotdotwk, wk); 9763 continue; 9764 } 9765 WORKLIST_INSERT(&inodedep->id_inowait, wk); 9766 } 9767 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list); 9768 /* 9769 * Normal file deletion. 9770 */ 9771 if ((dirrem->dm_state & RMDIR) == 0) { 9772 ip->i_nlink--; 9773 DIP_SET(ip, i_nlink, ip->i_nlink); 9774 ip->i_flag |= IN_CHANGE; 9775 if (ip->i_nlink < ip->i_effnlink) 9776 panic("handle_workitem_remove: bad file delta"); 9777 if (ip->i_nlink == 0) 9778 unlinked_inodedep(mp, inodedep); 9779 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9780 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 9781 ("handle_workitem_remove: worklist not empty. %s", 9782 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type))); 9783 WORKITEM_FREE(dirrem, D_DIRREM); 9784 FREE_LOCK(ump); 9785 goto out; 9786 } 9787 /* 9788 * Directory deletion. Decrement reference count for both the 9789 * just deleted parent directory entry and the reference for ".". 9790 * Arrange to have the reference count on the parent decremented 9791 * to account for the loss of "..". 9792 */ 9793 ip->i_nlink -= 2; 9794 DIP_SET(ip, i_nlink, ip->i_nlink); 9795 ip->i_flag |= IN_CHANGE; 9796 if (ip->i_nlink < ip->i_effnlink) 9797 panic("handle_workitem_remove: bad dir delta"); 9798 if (ip->i_nlink == 0) 9799 unlinked_inodedep(mp, inodedep); 9800 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9801 /* 9802 * Rename a directory to a new parent. Since, we are both deleting 9803 * and creating a new directory entry, the link count on the new 9804 * directory should not change. Thus we skip the followup dirrem. 9805 */ 9806 if (dirrem->dm_state & DIRCHG) { 9807 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 9808 ("handle_workitem_remove: DIRCHG and worklist not empty.")); 9809 WORKITEM_FREE(dirrem, D_DIRREM); 9810 FREE_LOCK(ump); 9811 goto out; 9812 } 9813 dirrem->dm_state = ONDEPLIST; 9814 dirrem->dm_oldinum = dirrem->dm_dirinum; 9815 /* 9816 * Place the dirrem on the parent's diremhd list. 9817 */ 9818 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0) 9819 panic("handle_workitem_remove: lost dir inodedep"); 9820 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9821 /* 9822 * If the allocated inode has never been written to disk, then 9823 * the on-disk inode is zero'ed and we can remove the file 9824 * immediately. When journaling if the inode has been marked 9825 * unlinked and not DEPCOMPLETE we know it can never be written. 9826 */ 9827 inodedep_lookup(mp, oldinum, 0, &inodedep); 9828 if (inodedep == NULL || 9829 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED || 9830 check_inode_unwritten(inodedep)) { 9831 FREE_LOCK(ump); 9832 vput(vp); 9833 return handle_workitem_remove(dirrem, flags); 9834 } 9835 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list); 9836 FREE_LOCK(ump); 9837 ip->i_flag |= IN_CHANGE; 9838out: 9839 ffs_update(vp, 0); 9840 vput(vp); 9841 return (0); 9842} 9843 9844/* 9845 * Inode de-allocation dependencies. 9846 * 9847 * When an inode's link count is reduced to zero, it can be de-allocated. We 9848 * found it convenient to postpone de-allocation until after the inode is 9849 * written to disk with its new link count (zero). At this point, all of the 9850 * on-disk inode's block pointers are nullified and, with careful dependency 9851 * list ordering, all dependencies related to the inode will be satisfied and 9852 * the corresponding dependency structures de-allocated. So, if/when the 9853 * inode is reused, there will be no mixing of old dependencies with new 9854 * ones. This artificial dependency is set up by the block de-allocation 9855 * procedure above (softdep_setup_freeblocks) and completed by the 9856 * following procedure. 9857 */ 9858static void 9859handle_workitem_freefile(freefile) 9860 struct freefile *freefile; 9861{ 9862 struct workhead wkhd; 9863 struct fs *fs; 9864 struct inodedep *idp; 9865 struct ufsmount *ump; 9866 int error; 9867 9868 ump = VFSTOUFS(freefile->fx_list.wk_mp); 9869 fs = ump->um_fs; 9870#ifdef DEBUG 9871 ACQUIRE_LOCK(ump); 9872 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp); 9873 FREE_LOCK(ump); 9874 if (error) 9875 panic("handle_workitem_freefile: inodedep %p survived", idp); 9876#endif 9877 UFS_LOCK(ump); 9878 fs->fs_pendinginodes -= 1; 9879 UFS_UNLOCK(ump); 9880 LIST_INIT(&wkhd); 9881 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list); 9882 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp, 9883 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0) 9884 softdep_error("handle_workitem_freefile", error); 9885 ACQUIRE_LOCK(ump); 9886 WORKITEM_FREE(freefile, D_FREEFILE); 9887 FREE_LOCK(ump); 9888} 9889 9890 9891/* 9892 * Helper function which unlinks marker element from work list and returns 9893 * the next element on the list. 9894 */ 9895static __inline struct worklist * 9896markernext(struct worklist *marker) 9897{ 9898 struct worklist *next; 9899 9900 next = LIST_NEXT(marker, wk_list); 9901 LIST_REMOVE(marker, wk_list); 9902 return next; 9903} 9904 9905/* 9906 * Disk writes. 9907 * 9908 * The dependency structures constructed above are most actively used when file 9909 * system blocks are written to disk. No constraints are placed on when a 9910 * block can be written, but unsatisfied update dependencies are made safe by 9911 * modifying (or replacing) the source memory for the duration of the disk 9912 * write. When the disk write completes, the memory block is again brought 9913 * up-to-date. 9914 * 9915 * In-core inode structure reclamation. 9916 * 9917 * Because there are a finite number of "in-core" inode structures, they are 9918 * reused regularly. By transferring all inode-related dependencies to the 9919 * in-memory inode block and indexing them separately (via "inodedep"s), we 9920 * can allow "in-core" inode structures to be reused at any time and avoid 9921 * any increase in contention. 9922 * 9923 * Called just before entering the device driver to initiate a new disk I/O. 9924 * The buffer must be locked, thus, no I/O completion operations can occur 9925 * while we are manipulating its associated dependencies. 9926 */ 9927static void 9928softdep_disk_io_initiation(bp) 9929 struct buf *bp; /* structure describing disk write to occur */ 9930{ 9931 struct worklist *wk; 9932 struct worklist marker; 9933 struct inodedep *inodedep; 9934 struct freeblks *freeblks; 9935 struct jblkdep *jblkdep; 9936 struct newblk *newblk; 9937 struct ufsmount *ump; 9938 9939 /* 9940 * We only care about write operations. There should never 9941 * be dependencies for reads. 9942 */ 9943 if (bp->b_iocmd != BIO_WRITE) 9944 panic("softdep_disk_io_initiation: not write"); 9945 9946 if (bp->b_vflags & BV_BKGRDINPROG) 9947 panic("softdep_disk_io_initiation: Writing buffer with " 9948 "background write in progress: %p", bp); 9949 9950 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL) 9951 return; 9952 ump = VFSTOUFS(wk->wk_mp); 9953 9954 marker.wk_type = D_LAST + 1; /* Not a normal workitem */ 9955 PHOLD(curproc); /* Don't swap out kernel stack */ 9956 ACQUIRE_LOCK(ump); 9957 /* 9958 * Do any necessary pre-I/O processing. 9959 */ 9960 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL; 9961 wk = markernext(&marker)) { 9962 LIST_INSERT_AFTER(wk, &marker, wk_list); 9963 switch (wk->wk_type) { 9964 9965 case D_PAGEDEP: 9966 initiate_write_filepage(WK_PAGEDEP(wk), bp); 9967 continue; 9968 9969 case D_INODEDEP: 9970 inodedep = WK_INODEDEP(wk); 9971 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) 9972 initiate_write_inodeblock_ufs1(inodedep, bp); 9973 else 9974 initiate_write_inodeblock_ufs2(inodedep, bp); 9975 continue; 9976 9977 case D_INDIRDEP: 9978 initiate_write_indirdep(WK_INDIRDEP(wk), bp); 9979 continue; 9980 9981 case D_BMSAFEMAP: 9982 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp); 9983 continue; 9984 9985 case D_JSEG: 9986 WK_JSEG(wk)->js_buf = NULL; 9987 continue; 9988 9989 case D_FREEBLKS: 9990 freeblks = WK_FREEBLKS(wk); 9991 jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd); 9992 /* 9993 * We have to wait for the freeblks to be journaled 9994 * before we can write an inodeblock with updated 9995 * pointers. Be careful to arrange the marker so 9996 * we revisit the freeblks if it's not removed by 9997 * the first jwait(). 9998 */ 9999 if (jblkdep != NULL) { 10000 LIST_REMOVE(&marker, wk_list); 10001 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10002 jwait(&jblkdep->jb_list, MNT_WAIT); 10003 } 10004 continue; 10005 case D_ALLOCDIRECT: 10006 case D_ALLOCINDIR: 10007 /* 10008 * We have to wait for the jnewblk to be journaled 10009 * before we can write to a block if the contents 10010 * may be confused with an earlier file's indirect 10011 * at recovery time. Handle the marker as described 10012 * above. 10013 */ 10014 newblk = WK_NEWBLK(wk); 10015 if (newblk->nb_jnewblk != NULL && 10016 indirblk_lookup(newblk->nb_list.wk_mp, 10017 newblk->nb_newblkno)) { 10018 LIST_REMOVE(&marker, wk_list); 10019 LIST_INSERT_BEFORE(wk, &marker, wk_list); 10020 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 10021 } 10022 continue; 10023 10024 case D_SBDEP: 10025 initiate_write_sbdep(WK_SBDEP(wk)); 10026 continue; 10027 10028 case D_MKDIR: 10029 case D_FREEWORK: 10030 case D_FREEDEP: 10031 case D_JSEGDEP: 10032 continue; 10033 10034 default: 10035 panic("handle_disk_io_initiation: Unexpected type %s", 10036 TYPENAME(wk->wk_type)); 10037 /* NOTREACHED */ 10038 } 10039 } 10040 FREE_LOCK(ump); 10041 PRELE(curproc); /* Allow swapout of kernel stack */ 10042} 10043 10044/* 10045 * Called from within the procedure above to deal with unsatisfied 10046 * allocation dependencies in a directory. The buffer must be locked, 10047 * thus, no I/O completion operations can occur while we are 10048 * manipulating its associated dependencies. 10049 */ 10050static void 10051initiate_write_filepage(pagedep, bp) 10052 struct pagedep *pagedep; 10053 struct buf *bp; 10054{ 10055 struct jremref *jremref; 10056 struct jmvref *jmvref; 10057 struct dirrem *dirrem; 10058 struct diradd *dap; 10059 struct direct *ep; 10060 int i; 10061 10062 if (pagedep->pd_state & IOSTARTED) { 10063 /* 10064 * This can only happen if there is a driver that does not 10065 * understand chaining. Here biodone will reissue the call 10066 * to strategy for the incomplete buffers. 10067 */ 10068 printf("initiate_write_filepage: already started\n"); 10069 return; 10070 } 10071 pagedep->pd_state |= IOSTARTED; 10072 /* 10073 * Wait for all journal remove dependencies to hit the disk. 10074 * We can not allow any potentially conflicting directory adds 10075 * to be visible before removes and rollback is too difficult. 10076 * The per-filesystem lock may be dropped and re-acquired, however 10077 * we hold the buf locked so the dependency can not go away. 10078 */ 10079 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) 10080 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) 10081 jwait(&jremref->jr_list, MNT_WAIT); 10082 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) 10083 jwait(&jmvref->jm_list, MNT_WAIT); 10084 for (i = 0; i < DAHASHSZ; i++) { 10085 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 10086 ep = (struct direct *) 10087 ((char *)bp->b_data + dap->da_offset); 10088 if (ep->d_ino != dap->da_newinum) 10089 panic("%s: dir inum %ju != new %ju", 10090 "initiate_write_filepage", 10091 (uintmax_t)ep->d_ino, 10092 (uintmax_t)dap->da_newinum); 10093 if (dap->da_state & DIRCHG) 10094 ep->d_ino = dap->da_previous->dm_oldinum; 10095 else 10096 ep->d_ino = 0; 10097 dap->da_state &= ~ATTACHED; 10098 dap->da_state |= UNDONE; 10099 } 10100 } 10101} 10102 10103/* 10104 * Version of initiate_write_inodeblock that handles UFS1 dinodes. 10105 * Note that any bug fixes made to this routine must be done in the 10106 * version found below. 10107 * 10108 * Called from within the procedure above to deal with unsatisfied 10109 * allocation dependencies in an inodeblock. The buffer must be 10110 * locked, thus, no I/O completion operations can occur while we 10111 * are manipulating its associated dependencies. 10112 */ 10113static void 10114initiate_write_inodeblock_ufs1(inodedep, bp) 10115 struct inodedep *inodedep; 10116 struct buf *bp; /* The inode block */ 10117{ 10118 struct allocdirect *adp, *lastadp; 10119 struct ufs1_dinode *dp; 10120 struct ufs1_dinode *sip; 10121 struct inoref *inoref; 10122 struct ufsmount *ump; 10123 struct fs *fs; 10124 ufs_lbn_t i; 10125#ifdef INVARIANTS 10126 ufs_lbn_t prevlbn = 0; 10127#endif 10128 int deplist; 10129 10130 if (inodedep->id_state & IOSTARTED) 10131 panic("initiate_write_inodeblock_ufs1: already started"); 10132 inodedep->id_state |= IOSTARTED; 10133 fs = inodedep->id_fs; 10134 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10135 LOCK_OWNED(ump); 10136 dp = (struct ufs1_dinode *)bp->b_data + 10137 ino_to_fsbo(fs, inodedep->id_ino); 10138 10139 /* 10140 * If we're on the unlinked list but have not yet written our 10141 * next pointer initialize it here. 10142 */ 10143 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10144 struct inodedep *inon; 10145 10146 inon = TAILQ_NEXT(inodedep, id_unlinked); 10147 dp->di_freelink = inon ? inon->id_ino : 0; 10148 } 10149 /* 10150 * If the bitmap is not yet written, then the allocated 10151 * inode cannot be written to disk. 10152 */ 10153 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10154 if (inodedep->id_savedino1 != NULL) 10155 panic("initiate_write_inodeblock_ufs1: I/O underway"); 10156 FREE_LOCK(ump); 10157 sip = malloc(sizeof(struct ufs1_dinode), 10158 M_SAVEDINO, M_SOFTDEP_FLAGS); 10159 ACQUIRE_LOCK(ump); 10160 inodedep->id_savedino1 = sip; 10161 *inodedep->id_savedino1 = *dp; 10162 bzero((caddr_t)dp, sizeof(struct ufs1_dinode)); 10163 dp->di_gen = inodedep->id_savedino1->di_gen; 10164 dp->di_freelink = inodedep->id_savedino1->di_freelink; 10165 return; 10166 } 10167 /* 10168 * If no dependencies, then there is nothing to roll back. 10169 */ 10170 inodedep->id_savedsize = dp->di_size; 10171 inodedep->id_savedextsize = 0; 10172 inodedep->id_savednlink = dp->di_nlink; 10173 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10174 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10175 return; 10176 /* 10177 * Revert the link count to that of the first unwritten journal entry. 10178 */ 10179 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10180 if (inoref) 10181 dp->di_nlink = inoref->if_nlink; 10182 /* 10183 * Set the dependencies to busy. 10184 */ 10185 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10186 adp = TAILQ_NEXT(adp, ad_next)) { 10187#ifdef INVARIANTS 10188 if (deplist != 0 && prevlbn >= adp->ad_offset) 10189 panic("softdep_write_inodeblock: lbn order"); 10190 prevlbn = adp->ad_offset; 10191 if (adp->ad_offset < NDADDR && 10192 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10193 panic("%s: direct pointer #%jd mismatch %d != %jd", 10194 "softdep_write_inodeblock", 10195 (intmax_t)adp->ad_offset, 10196 dp->di_db[adp->ad_offset], 10197 (intmax_t)adp->ad_newblkno); 10198 if (adp->ad_offset >= NDADDR && 10199 dp->di_ib[adp->ad_offset - NDADDR] != adp->ad_newblkno) 10200 panic("%s: indirect pointer #%jd mismatch %d != %jd", 10201 "softdep_write_inodeblock", 10202 (intmax_t)adp->ad_offset - NDADDR, 10203 dp->di_ib[adp->ad_offset - NDADDR], 10204 (intmax_t)adp->ad_newblkno); 10205 deplist |= 1 << adp->ad_offset; 10206 if ((adp->ad_state & ATTACHED) == 0) 10207 panic("softdep_write_inodeblock: Unknown state 0x%x", 10208 adp->ad_state); 10209#endif /* INVARIANTS */ 10210 adp->ad_state &= ~ATTACHED; 10211 adp->ad_state |= UNDONE; 10212 } 10213 /* 10214 * The on-disk inode cannot claim to be any larger than the last 10215 * fragment that has been written. Otherwise, the on-disk inode 10216 * might have fragments that were not the last block in the file 10217 * which would corrupt the filesystem. 10218 */ 10219 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10220 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10221 if (adp->ad_offset >= NDADDR) 10222 break; 10223 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10224 /* keep going until hitting a rollback to a frag */ 10225 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10226 continue; 10227 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10228 for (i = adp->ad_offset + 1; i < NDADDR; i++) { 10229#ifdef INVARIANTS 10230 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10231 panic("softdep_write_inodeblock: lost dep1"); 10232#endif /* INVARIANTS */ 10233 dp->di_db[i] = 0; 10234 } 10235 for (i = 0; i < NIADDR; i++) { 10236#ifdef INVARIANTS 10237 if (dp->di_ib[i] != 0 && 10238 (deplist & ((1 << NDADDR) << i)) == 0) 10239 panic("softdep_write_inodeblock: lost dep2"); 10240#endif /* INVARIANTS */ 10241 dp->di_ib[i] = 0; 10242 } 10243 return; 10244 } 10245 /* 10246 * If we have zero'ed out the last allocated block of the file, 10247 * roll back the size to the last currently allocated block. 10248 * We know that this last allocated block is a full-sized as 10249 * we already checked for fragments in the loop above. 10250 */ 10251 if (lastadp != NULL && 10252 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10253 for (i = lastadp->ad_offset; i >= 0; i--) 10254 if (dp->di_db[i] != 0) 10255 break; 10256 dp->di_size = (i + 1) * fs->fs_bsize; 10257 } 10258 /* 10259 * The only dependencies are for indirect blocks. 10260 * 10261 * The file size for indirect block additions is not guaranteed. 10262 * Such a guarantee would be non-trivial to achieve. The conventional 10263 * synchronous write implementation also does not make this guarantee. 10264 * Fsck should catch and fix discrepancies. Arguably, the file size 10265 * can be over-estimated without destroying integrity when the file 10266 * moves into the indirect blocks (i.e., is large). If we want to 10267 * postpone fsck, we are stuck with this argument. 10268 */ 10269 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10270 dp->di_ib[adp->ad_offset - NDADDR] = 0; 10271} 10272 10273/* 10274 * Version of initiate_write_inodeblock that handles UFS2 dinodes. 10275 * Note that any bug fixes made to this routine must be done in the 10276 * version found above. 10277 * 10278 * Called from within the procedure above to deal with unsatisfied 10279 * allocation dependencies in an inodeblock. The buffer must be 10280 * locked, thus, no I/O completion operations can occur while we 10281 * are manipulating its associated dependencies. 10282 */ 10283static void 10284initiate_write_inodeblock_ufs2(inodedep, bp) 10285 struct inodedep *inodedep; 10286 struct buf *bp; /* The inode block */ 10287{ 10288 struct allocdirect *adp, *lastadp; 10289 struct ufs2_dinode *dp; 10290 struct ufs2_dinode *sip; 10291 struct inoref *inoref; 10292 struct ufsmount *ump; 10293 struct fs *fs; 10294 ufs_lbn_t i; 10295#ifdef INVARIANTS 10296 ufs_lbn_t prevlbn = 0; 10297#endif 10298 int deplist; 10299 10300 if (inodedep->id_state & IOSTARTED) 10301 panic("initiate_write_inodeblock_ufs2: already started"); 10302 inodedep->id_state |= IOSTARTED; 10303 fs = inodedep->id_fs; 10304 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10305 LOCK_OWNED(ump); 10306 dp = (struct ufs2_dinode *)bp->b_data + 10307 ino_to_fsbo(fs, inodedep->id_ino); 10308 10309 /* 10310 * If we're on the unlinked list but have not yet written our 10311 * next pointer initialize it here. 10312 */ 10313 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10314 struct inodedep *inon; 10315 10316 inon = TAILQ_NEXT(inodedep, id_unlinked); 10317 dp->di_freelink = inon ? inon->id_ino : 0; 10318 } 10319 /* 10320 * If the bitmap is not yet written, then the allocated 10321 * inode cannot be written to disk. 10322 */ 10323 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10324 if (inodedep->id_savedino2 != NULL) 10325 panic("initiate_write_inodeblock_ufs2: I/O underway"); 10326 FREE_LOCK(ump); 10327 sip = malloc(sizeof(struct ufs2_dinode), 10328 M_SAVEDINO, M_SOFTDEP_FLAGS); 10329 ACQUIRE_LOCK(ump); 10330 inodedep->id_savedino2 = sip; 10331 *inodedep->id_savedino2 = *dp; 10332 bzero((caddr_t)dp, sizeof(struct ufs2_dinode)); 10333 dp->di_gen = inodedep->id_savedino2->di_gen; 10334 dp->di_freelink = inodedep->id_savedino2->di_freelink; 10335 return; 10336 } 10337 /* 10338 * If no dependencies, then there is nothing to roll back. 10339 */ 10340 inodedep->id_savedsize = dp->di_size; 10341 inodedep->id_savedextsize = dp->di_extsize; 10342 inodedep->id_savednlink = dp->di_nlink; 10343 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10344 TAILQ_EMPTY(&inodedep->id_extupdt) && 10345 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10346 return; 10347 /* 10348 * Revert the link count to that of the first unwritten journal entry. 10349 */ 10350 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10351 if (inoref) 10352 dp->di_nlink = inoref->if_nlink; 10353 10354 /* 10355 * Set the ext data dependencies to busy. 10356 */ 10357 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10358 adp = TAILQ_NEXT(adp, ad_next)) { 10359#ifdef INVARIANTS 10360 if (deplist != 0 && prevlbn >= adp->ad_offset) 10361 panic("softdep_write_inodeblock: lbn order"); 10362 prevlbn = adp->ad_offset; 10363 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno) 10364 panic("%s: direct pointer #%jd mismatch %jd != %jd", 10365 "softdep_write_inodeblock", 10366 (intmax_t)adp->ad_offset, 10367 (intmax_t)dp->di_extb[adp->ad_offset], 10368 (intmax_t)adp->ad_newblkno); 10369 deplist |= 1 << adp->ad_offset; 10370 if ((adp->ad_state & ATTACHED) == 0) 10371 panic("softdep_write_inodeblock: Unknown state 0x%x", 10372 adp->ad_state); 10373#endif /* INVARIANTS */ 10374 adp->ad_state &= ~ATTACHED; 10375 adp->ad_state |= UNDONE; 10376 } 10377 /* 10378 * The on-disk inode cannot claim to be any larger than the last 10379 * fragment that has been written. Otherwise, the on-disk inode 10380 * might have fragments that were not the last block in the ext 10381 * data which would corrupt the filesystem. 10382 */ 10383 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10384 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10385 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno; 10386 /* keep going until hitting a rollback to a frag */ 10387 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10388 continue; 10389 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10390 for (i = adp->ad_offset + 1; i < NXADDR; i++) { 10391#ifdef INVARIANTS 10392 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0) 10393 panic("softdep_write_inodeblock: lost dep1"); 10394#endif /* INVARIANTS */ 10395 dp->di_extb[i] = 0; 10396 } 10397 lastadp = NULL; 10398 break; 10399 } 10400 /* 10401 * If we have zero'ed out the last allocated block of the ext 10402 * data, roll back the size to the last currently allocated block. 10403 * We know that this last allocated block is a full-sized as 10404 * we already checked for fragments in the loop above. 10405 */ 10406 if (lastadp != NULL && 10407 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10408 for (i = lastadp->ad_offset; i >= 0; i--) 10409 if (dp->di_extb[i] != 0) 10410 break; 10411 dp->di_extsize = (i + 1) * fs->fs_bsize; 10412 } 10413 /* 10414 * Set the file data dependencies to busy. 10415 */ 10416 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10417 adp = TAILQ_NEXT(adp, ad_next)) { 10418#ifdef INVARIANTS 10419 if (deplist != 0 && prevlbn >= adp->ad_offset) 10420 panic("softdep_write_inodeblock: lbn order"); 10421 if ((adp->ad_state & ATTACHED) == 0) 10422 panic("inodedep %p and adp %p not attached", inodedep, adp); 10423 prevlbn = adp->ad_offset; 10424 if (adp->ad_offset < NDADDR && 10425 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10426 panic("%s: direct pointer #%jd mismatch %jd != %jd", 10427 "softdep_write_inodeblock", 10428 (intmax_t)adp->ad_offset, 10429 (intmax_t)dp->di_db[adp->ad_offset], 10430 (intmax_t)adp->ad_newblkno); 10431 if (adp->ad_offset >= NDADDR && 10432 dp->di_ib[adp->ad_offset - NDADDR] != adp->ad_newblkno) 10433 panic("%s indirect pointer #%jd mismatch %jd != %jd", 10434 "softdep_write_inodeblock:", 10435 (intmax_t)adp->ad_offset - NDADDR, 10436 (intmax_t)dp->di_ib[adp->ad_offset - NDADDR], 10437 (intmax_t)adp->ad_newblkno); 10438 deplist |= 1 << adp->ad_offset; 10439 if ((adp->ad_state & ATTACHED) == 0) 10440 panic("softdep_write_inodeblock: Unknown state 0x%x", 10441 adp->ad_state); 10442#endif /* INVARIANTS */ 10443 adp->ad_state &= ~ATTACHED; 10444 adp->ad_state |= UNDONE; 10445 } 10446 /* 10447 * The on-disk inode cannot claim to be any larger than the last 10448 * fragment that has been written. Otherwise, the on-disk inode 10449 * might have fragments that were not the last block in the file 10450 * which would corrupt the filesystem. 10451 */ 10452 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10453 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10454 if (adp->ad_offset >= NDADDR) 10455 break; 10456 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10457 /* keep going until hitting a rollback to a frag */ 10458 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10459 continue; 10460 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10461 for (i = adp->ad_offset + 1; i < NDADDR; i++) { 10462#ifdef INVARIANTS 10463 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10464 panic("softdep_write_inodeblock: lost dep2"); 10465#endif /* INVARIANTS */ 10466 dp->di_db[i] = 0; 10467 } 10468 for (i = 0; i < NIADDR; i++) { 10469#ifdef INVARIANTS 10470 if (dp->di_ib[i] != 0 && 10471 (deplist & ((1 << NDADDR) << i)) == 0) 10472 panic("softdep_write_inodeblock: lost dep3"); 10473#endif /* INVARIANTS */ 10474 dp->di_ib[i] = 0; 10475 } 10476 return; 10477 } 10478 /* 10479 * If we have zero'ed out the last allocated block of the file, 10480 * roll back the size to the last currently allocated block. 10481 * We know that this last allocated block is a full-sized as 10482 * we already checked for fragments in the loop above. 10483 */ 10484 if (lastadp != NULL && 10485 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10486 for (i = lastadp->ad_offset; i >= 0; i--) 10487 if (dp->di_db[i] != 0) 10488 break; 10489 dp->di_size = (i + 1) * fs->fs_bsize; 10490 } 10491 /* 10492 * The only dependencies are for indirect blocks. 10493 * 10494 * The file size for indirect block additions is not guaranteed. 10495 * Such a guarantee would be non-trivial to achieve. The conventional 10496 * synchronous write implementation also does not make this guarantee. 10497 * Fsck should catch and fix discrepancies. Arguably, the file size 10498 * can be over-estimated without destroying integrity when the file 10499 * moves into the indirect blocks (i.e., is large). If we want to 10500 * postpone fsck, we are stuck with this argument. 10501 */ 10502 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10503 dp->di_ib[adp->ad_offset - NDADDR] = 0; 10504} 10505 10506/* 10507 * Cancel an indirdep as a result of truncation. Release all of the 10508 * children allocindirs and place their journal work on the appropriate 10509 * list. 10510 */ 10511static void 10512cancel_indirdep(indirdep, bp, freeblks) 10513 struct indirdep *indirdep; 10514 struct buf *bp; 10515 struct freeblks *freeblks; 10516{ 10517 struct allocindir *aip; 10518 10519 /* 10520 * None of the indirect pointers will ever be visible, 10521 * so they can simply be tossed. GOINGAWAY ensures 10522 * that allocated pointers will be saved in the buffer 10523 * cache until they are freed. Note that they will 10524 * only be able to be found by their physical address 10525 * since the inode mapping the logical address will 10526 * be gone. The save buffer used for the safe copy 10527 * was allocated in setup_allocindir_phase2 using 10528 * the physical address so it could be used for this 10529 * purpose. Hence we swap the safe copy with the real 10530 * copy, allowing the safe copy to be freed and holding 10531 * on to the real copy for later use in indir_trunc. 10532 */ 10533 if (indirdep->ir_state & GOINGAWAY) 10534 panic("cancel_indirdep: already gone"); 10535 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 10536 indirdep->ir_state |= DEPCOMPLETE; 10537 LIST_REMOVE(indirdep, ir_next); 10538 } 10539 indirdep->ir_state |= GOINGAWAY; 10540 /* 10541 * Pass in bp for blocks still have journal writes 10542 * pending so we can cancel them on their own. 10543 */ 10544 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != 0) 10545 cancel_allocindir(aip, bp, freeblks, 0); 10546 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0) 10547 cancel_allocindir(aip, NULL, freeblks, 0); 10548 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != 0) 10549 cancel_allocindir(aip, NULL, freeblks, 0); 10550 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != 0) 10551 cancel_allocindir(aip, NULL, freeblks, 0); 10552 /* 10553 * If there are pending partial truncations we need to keep the 10554 * old block copy around until they complete. This is because 10555 * the current b_data is not a perfect superset of the available 10556 * blocks. 10557 */ 10558 if (TAILQ_EMPTY(&indirdep->ir_trunc)) 10559 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount); 10560 else 10561 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10562 WORKLIST_REMOVE(&indirdep->ir_list); 10563 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list); 10564 indirdep->ir_bp = NULL; 10565 indirdep->ir_freeblks = freeblks; 10566} 10567 10568/* 10569 * Free an indirdep once it no longer has new pointers to track. 10570 */ 10571static void 10572free_indirdep(indirdep) 10573 struct indirdep *indirdep; 10574{ 10575 10576 KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc), 10577 ("free_indirdep: Indir trunc list not empty.")); 10578 KASSERT(LIST_EMPTY(&indirdep->ir_completehd), 10579 ("free_indirdep: Complete head not empty.")); 10580 KASSERT(LIST_EMPTY(&indirdep->ir_writehd), 10581 ("free_indirdep: write head not empty.")); 10582 KASSERT(LIST_EMPTY(&indirdep->ir_donehd), 10583 ("free_indirdep: done head not empty.")); 10584 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd), 10585 ("free_indirdep: deplist head not empty.")); 10586 KASSERT((indirdep->ir_state & DEPCOMPLETE), 10587 ("free_indirdep: %p still on newblk list.", indirdep)); 10588 KASSERT(indirdep->ir_saveddata == NULL, 10589 ("free_indirdep: %p still has saved data.", indirdep)); 10590 if (indirdep->ir_state & ONWORKLIST) 10591 WORKLIST_REMOVE(&indirdep->ir_list); 10592 WORKITEM_FREE(indirdep, D_INDIRDEP); 10593} 10594 10595/* 10596 * Called before a write to an indirdep. This routine is responsible for 10597 * rolling back pointers to a safe state which includes only those 10598 * allocindirs which have been completed. 10599 */ 10600static void 10601initiate_write_indirdep(indirdep, bp) 10602 struct indirdep *indirdep; 10603 struct buf *bp; 10604{ 10605 struct ufsmount *ump; 10606 10607 indirdep->ir_state |= IOSTARTED; 10608 if (indirdep->ir_state & GOINGAWAY) 10609 panic("disk_io_initiation: indirdep gone"); 10610 /* 10611 * If there are no remaining dependencies, this will be writing 10612 * the real pointers. 10613 */ 10614 if (LIST_EMPTY(&indirdep->ir_deplisthd) && 10615 TAILQ_EMPTY(&indirdep->ir_trunc)) 10616 return; 10617 /* 10618 * Replace up-to-date version with safe version. 10619 */ 10620 if (indirdep->ir_saveddata == NULL) { 10621 ump = VFSTOUFS(indirdep->ir_list.wk_mp); 10622 LOCK_OWNED(ump); 10623 FREE_LOCK(ump); 10624 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 10625 M_SOFTDEP_FLAGS); 10626 ACQUIRE_LOCK(ump); 10627 } 10628 indirdep->ir_state &= ~ATTACHED; 10629 indirdep->ir_state |= UNDONE; 10630 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10631 bcopy(indirdep->ir_savebp->b_data, bp->b_data, 10632 bp->b_bcount); 10633} 10634 10635/* 10636 * Called when an inode has been cleared in a cg bitmap. This finally 10637 * eliminates any canceled jaddrefs 10638 */ 10639void 10640softdep_setup_inofree(mp, bp, ino, wkhd) 10641 struct mount *mp; 10642 struct buf *bp; 10643 ino_t ino; 10644 struct workhead *wkhd; 10645{ 10646 struct worklist *wk, *wkn; 10647 struct inodedep *inodedep; 10648 struct ufsmount *ump; 10649 uint8_t *inosused; 10650 struct cg *cgp; 10651 struct fs *fs; 10652 10653 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 10654 ("softdep_setup_inofree called on non-softdep filesystem")); 10655 ump = VFSTOUFS(mp); 10656 ACQUIRE_LOCK(ump); 10657 fs = ump->um_fs; 10658 cgp = (struct cg *)bp->b_data; 10659 inosused = cg_inosused(cgp); 10660 if (isset(inosused, ino % fs->fs_ipg)) 10661 panic("softdep_setup_inofree: inode %ju not freed.", 10662 (uintmax_t)ino); 10663 if (inodedep_lookup(mp, ino, 0, &inodedep)) 10664 panic("softdep_setup_inofree: ino %ju has existing inodedep %p", 10665 (uintmax_t)ino, inodedep); 10666 if (wkhd) { 10667 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) { 10668 if (wk->wk_type != D_JADDREF) 10669 continue; 10670 WORKLIST_REMOVE(wk); 10671 /* 10672 * We can free immediately even if the jaddref 10673 * isn't attached in a background write as now 10674 * the bitmaps are reconciled. 10675 */ 10676 wk->wk_state |= COMPLETE | ATTACHED; 10677 free_jaddref(WK_JADDREF(wk)); 10678 } 10679 jwork_move(&bp->b_dep, wkhd); 10680 } 10681 FREE_LOCK(ump); 10682} 10683 10684 10685/* 10686 * Called via ffs_blkfree() after a set of frags has been cleared from a cg 10687 * map. Any dependencies waiting for the write to clear are added to the 10688 * buf's list and any jnewblks that are being canceled are discarded 10689 * immediately. 10690 */ 10691void 10692softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 10693 struct mount *mp; 10694 struct buf *bp; 10695 ufs2_daddr_t blkno; 10696 int frags; 10697 struct workhead *wkhd; 10698{ 10699 struct bmsafemap *bmsafemap; 10700 struct jnewblk *jnewblk; 10701 struct ufsmount *ump; 10702 struct worklist *wk; 10703 struct fs *fs; 10704#ifdef SUJ_DEBUG 10705 uint8_t *blksfree; 10706 struct cg *cgp; 10707 ufs2_daddr_t jstart; 10708 ufs2_daddr_t jend; 10709 ufs2_daddr_t end; 10710 long bno; 10711 int i; 10712#endif 10713 10714 CTR3(KTR_SUJ, 10715 "softdep_setup_blkfree: blkno %jd frags %d wk head %p", 10716 blkno, frags, wkhd); 10717 10718 ump = VFSTOUFS(mp); 10719 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 10720 ("softdep_setup_blkfree called on non-softdep filesystem")); 10721 ACQUIRE_LOCK(ump); 10722 /* Lookup the bmsafemap so we track when it is dirty. */ 10723 fs = ump->um_fs; 10724 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 10725 /* 10726 * Detach any jnewblks which have been canceled. They must linger 10727 * until the bitmap is cleared again by ffs_blkfree() to prevent 10728 * an unjournaled allocation from hitting the disk. 10729 */ 10730 if (wkhd) { 10731 while ((wk = LIST_FIRST(wkhd)) != NULL) { 10732 CTR2(KTR_SUJ, 10733 "softdep_setup_blkfree: blkno %jd wk type %d", 10734 blkno, wk->wk_type); 10735 WORKLIST_REMOVE(wk); 10736 if (wk->wk_type != D_JNEWBLK) { 10737 WORKLIST_INSERT(&bmsafemap->sm_freehd, wk); 10738 continue; 10739 } 10740 jnewblk = WK_JNEWBLK(wk); 10741 KASSERT(jnewblk->jn_state & GOINGAWAY, 10742 ("softdep_setup_blkfree: jnewblk not canceled.")); 10743#ifdef SUJ_DEBUG 10744 /* 10745 * Assert that this block is free in the bitmap 10746 * before we discard the jnewblk. 10747 */ 10748 cgp = (struct cg *)bp->b_data; 10749 blksfree = cg_blksfree(cgp); 10750 bno = dtogd(fs, jnewblk->jn_blkno); 10751 for (i = jnewblk->jn_oldfrags; 10752 i < jnewblk->jn_frags; i++) { 10753 if (isset(blksfree, bno + i)) 10754 continue; 10755 panic("softdep_setup_blkfree: not free"); 10756 } 10757#endif 10758 /* 10759 * Even if it's not attached we can free immediately 10760 * as the new bitmap is correct. 10761 */ 10762 wk->wk_state |= COMPLETE | ATTACHED; 10763 free_jnewblk(jnewblk); 10764 } 10765 } 10766 10767#ifdef SUJ_DEBUG 10768 /* 10769 * Assert that we are not freeing a block which has an outstanding 10770 * allocation dependency. 10771 */ 10772 fs = VFSTOUFS(mp)->um_fs; 10773 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 10774 end = blkno + frags; 10775 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 10776 /* 10777 * Don't match against blocks that will be freed when the 10778 * background write is done. 10779 */ 10780 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) == 10781 (COMPLETE | DEPCOMPLETE)) 10782 continue; 10783 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags; 10784 jend = jnewblk->jn_blkno + jnewblk->jn_frags; 10785 if ((blkno >= jstart && blkno < jend) || 10786 (end > jstart && end <= jend)) { 10787 printf("state 0x%X %jd - %d %d dep %p\n", 10788 jnewblk->jn_state, jnewblk->jn_blkno, 10789 jnewblk->jn_oldfrags, jnewblk->jn_frags, 10790 jnewblk->jn_dep); 10791 panic("softdep_setup_blkfree: " 10792 "%jd-%jd(%d) overlaps with %jd-%jd", 10793 blkno, end, frags, jstart, jend); 10794 } 10795 } 10796#endif 10797 FREE_LOCK(ump); 10798} 10799 10800/* 10801 * Revert a block allocation when the journal record that describes it 10802 * is not yet written. 10803 */ 10804static int 10805jnewblk_rollback(jnewblk, fs, cgp, blksfree) 10806 struct jnewblk *jnewblk; 10807 struct fs *fs; 10808 struct cg *cgp; 10809 uint8_t *blksfree; 10810{ 10811 ufs1_daddr_t fragno; 10812 long cgbno, bbase; 10813 int frags, blk; 10814 int i; 10815 10816 frags = 0; 10817 cgbno = dtogd(fs, jnewblk->jn_blkno); 10818 /* 10819 * We have to test which frags need to be rolled back. We may 10820 * be operating on a stale copy when doing background writes. 10821 */ 10822 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) 10823 if (isclr(blksfree, cgbno + i)) 10824 frags++; 10825 if (frags == 0) 10826 return (0); 10827 /* 10828 * This is mostly ffs_blkfree() sans some validation and 10829 * superblock updates. 10830 */ 10831 if (frags == fs->fs_frag) { 10832 fragno = fragstoblks(fs, cgbno); 10833 ffs_setblock(fs, blksfree, fragno); 10834 ffs_clusteracct(fs, cgp, fragno, 1); 10835 cgp->cg_cs.cs_nbfree++; 10836 } else { 10837 cgbno += jnewblk->jn_oldfrags; 10838 bbase = cgbno - fragnum(fs, cgbno); 10839 /* Decrement the old frags. */ 10840 blk = blkmap(fs, blksfree, bbase); 10841 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 10842 /* Deallocate the fragment */ 10843 for (i = 0; i < frags; i++) 10844 setbit(blksfree, cgbno + i); 10845 cgp->cg_cs.cs_nffree += frags; 10846 /* Add back in counts associated with the new frags */ 10847 blk = blkmap(fs, blksfree, bbase); 10848 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 10849 /* If a complete block has been reassembled, account for it. */ 10850 fragno = fragstoblks(fs, bbase); 10851 if (ffs_isblock(fs, blksfree, fragno)) { 10852 cgp->cg_cs.cs_nffree -= fs->fs_frag; 10853 ffs_clusteracct(fs, cgp, fragno, 1); 10854 cgp->cg_cs.cs_nbfree++; 10855 } 10856 } 10857 stat_jnewblk++; 10858 jnewblk->jn_state &= ~ATTACHED; 10859 jnewblk->jn_state |= UNDONE; 10860 10861 return (frags); 10862} 10863 10864static void 10865initiate_write_bmsafemap(bmsafemap, bp) 10866 struct bmsafemap *bmsafemap; 10867 struct buf *bp; /* The cg block. */ 10868{ 10869 struct jaddref *jaddref; 10870 struct jnewblk *jnewblk; 10871 uint8_t *inosused; 10872 uint8_t *blksfree; 10873 struct cg *cgp; 10874 struct fs *fs; 10875 ino_t ino; 10876 10877 if (bmsafemap->sm_state & IOSTARTED) 10878 return; 10879 bmsafemap->sm_state |= IOSTARTED; 10880 /* 10881 * Clear any inode allocations which are pending journal writes. 10882 */ 10883 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) { 10884 cgp = (struct cg *)bp->b_data; 10885 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 10886 inosused = cg_inosused(cgp); 10887 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) { 10888 ino = jaddref->ja_ino % fs->fs_ipg; 10889 if (isset(inosused, ino)) { 10890 if ((jaddref->ja_mode & IFMT) == IFDIR) 10891 cgp->cg_cs.cs_ndir--; 10892 cgp->cg_cs.cs_nifree++; 10893 clrbit(inosused, ino); 10894 jaddref->ja_state &= ~ATTACHED; 10895 jaddref->ja_state |= UNDONE; 10896 stat_jaddref++; 10897 } else 10898 panic("initiate_write_bmsafemap: inode %ju " 10899 "marked free", (uintmax_t)jaddref->ja_ino); 10900 } 10901 } 10902 /* 10903 * Clear any block allocations which are pending journal writes. 10904 */ 10905 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 10906 cgp = (struct cg *)bp->b_data; 10907 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 10908 blksfree = cg_blksfree(cgp); 10909 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 10910 if (jnewblk_rollback(jnewblk, fs, cgp, blksfree)) 10911 continue; 10912 panic("initiate_write_bmsafemap: block %jd " 10913 "marked free", jnewblk->jn_blkno); 10914 } 10915 } 10916 /* 10917 * Move allocation lists to the written lists so they can be 10918 * cleared once the block write is complete. 10919 */ 10920 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr, 10921 inodedep, id_deps); 10922 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 10923 newblk, nb_deps); 10924 LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist, 10925 wk_list); 10926} 10927 10928/* 10929 * This routine is called during the completion interrupt 10930 * service routine for a disk write (from the procedure called 10931 * by the device driver to inform the filesystem caches of 10932 * a request completion). It should be called early in this 10933 * procedure, before the block is made available to other 10934 * processes or other routines are called. 10935 * 10936 */ 10937static void 10938softdep_disk_write_complete(bp) 10939 struct buf *bp; /* describes the completed disk write */ 10940{ 10941 struct worklist *wk; 10942 struct worklist *owk; 10943 struct ufsmount *ump; 10944 struct workhead reattach; 10945 struct freeblks *freeblks; 10946 struct buf *sbp; 10947 10948 /* 10949 * If an error occurred while doing the write, then the data 10950 * has not hit the disk and the dependencies cannot be unrolled. 10951 */ 10952 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) 10953 return; 10954 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL) 10955 return; 10956 ump = VFSTOUFS(wk->wk_mp); 10957 LIST_INIT(&reattach); 10958 /* 10959 * This lock must not be released anywhere in this code segment. 10960 */ 10961 sbp = NULL; 10962 owk = NULL; 10963 ACQUIRE_LOCK(ump); 10964 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) { 10965 WORKLIST_REMOVE(wk); 10966 atomic_add_long(&dep_write[wk->wk_type], 1); 10967 if (wk == owk) 10968 panic("duplicate worklist: %p\n", wk); 10969 owk = wk; 10970 switch (wk->wk_type) { 10971 10972 case D_PAGEDEP: 10973 if (handle_written_filepage(WK_PAGEDEP(wk), bp)) 10974 WORKLIST_INSERT(&reattach, wk); 10975 continue; 10976 10977 case D_INODEDEP: 10978 if (handle_written_inodeblock(WK_INODEDEP(wk), bp)) 10979 WORKLIST_INSERT(&reattach, wk); 10980 continue; 10981 10982 case D_BMSAFEMAP: 10983 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp)) 10984 WORKLIST_INSERT(&reattach, wk); 10985 continue; 10986 10987 case D_MKDIR: 10988 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 10989 continue; 10990 10991 case D_ALLOCDIRECT: 10992 wk->wk_state |= COMPLETE; 10993 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL); 10994 continue; 10995 10996 case D_ALLOCINDIR: 10997 wk->wk_state |= COMPLETE; 10998 handle_allocindir_partdone(WK_ALLOCINDIR(wk)); 10999 continue; 11000 11001 case D_INDIRDEP: 11002 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp)) 11003 WORKLIST_INSERT(&reattach, wk); 11004 continue; 11005 11006 case D_FREEBLKS: 11007 wk->wk_state |= COMPLETE; 11008 freeblks = WK_FREEBLKS(wk); 11009 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE && 11010 LIST_EMPTY(&freeblks->fb_jblkdephd)) 11011 add_to_worklist(wk, WK_NODELAY); 11012 continue; 11013 11014 case D_FREEWORK: 11015 handle_written_freework(WK_FREEWORK(wk)); 11016 break; 11017 11018 case D_JSEGDEP: 11019 free_jsegdep(WK_JSEGDEP(wk)); 11020 continue; 11021 11022 case D_JSEG: 11023 handle_written_jseg(WK_JSEG(wk), bp); 11024 continue; 11025 11026 case D_SBDEP: 11027 if (handle_written_sbdep(WK_SBDEP(wk), bp)) 11028 WORKLIST_INSERT(&reattach, wk); 11029 continue; 11030 11031 case D_FREEDEP: 11032 free_freedep(WK_FREEDEP(wk)); 11033 continue; 11034 11035 default: 11036 panic("handle_disk_write_complete: Unknown type %s", 11037 TYPENAME(wk->wk_type)); 11038 /* NOTREACHED */ 11039 } 11040 } 11041 /* 11042 * Reattach any requests that must be redone. 11043 */ 11044 while ((wk = LIST_FIRST(&reattach)) != NULL) { 11045 WORKLIST_REMOVE(wk); 11046 WORKLIST_INSERT(&bp->b_dep, wk); 11047 } 11048 FREE_LOCK(ump); 11049 if (sbp) 11050 brelse(sbp); 11051} 11052 11053/* 11054 * Called from within softdep_disk_write_complete above. Note that 11055 * this routine is always called from interrupt level with further 11056 * splbio interrupts blocked. 11057 */ 11058static void 11059handle_allocdirect_partdone(adp, wkhd) 11060 struct allocdirect *adp; /* the completed allocdirect */ 11061 struct workhead *wkhd; /* Work to do when inode is writtne. */ 11062{ 11063 struct allocdirectlst *listhead; 11064 struct allocdirect *listadp; 11065 struct inodedep *inodedep; 11066 long bsize; 11067 11068 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11069 return; 11070 /* 11071 * The on-disk inode cannot claim to be any larger than the last 11072 * fragment that has been written. Otherwise, the on-disk inode 11073 * might have fragments that were not the last block in the file 11074 * which would corrupt the filesystem. Thus, we cannot free any 11075 * allocdirects after one whose ad_oldblkno claims a fragment as 11076 * these blocks must be rolled back to zero before writing the inode. 11077 * We check the currently active set of allocdirects in id_inoupdt 11078 * or id_extupdt as appropriate. 11079 */ 11080 inodedep = adp->ad_inodedep; 11081 bsize = inodedep->id_fs->fs_bsize; 11082 if (adp->ad_state & EXTDATA) 11083 listhead = &inodedep->id_extupdt; 11084 else 11085 listhead = &inodedep->id_inoupdt; 11086 TAILQ_FOREACH(listadp, listhead, ad_next) { 11087 /* found our block */ 11088 if (listadp == adp) 11089 break; 11090 /* continue if ad_oldlbn is not a fragment */ 11091 if (listadp->ad_oldsize == 0 || 11092 listadp->ad_oldsize == bsize) 11093 continue; 11094 /* hit a fragment */ 11095 return; 11096 } 11097 /* 11098 * If we have reached the end of the current list without 11099 * finding the just finished dependency, then it must be 11100 * on the future dependency list. Future dependencies cannot 11101 * be freed until they are moved to the current list. 11102 */ 11103 if (listadp == NULL) { 11104#ifdef DEBUG 11105 if (adp->ad_state & EXTDATA) 11106 listhead = &inodedep->id_newextupdt; 11107 else 11108 listhead = &inodedep->id_newinoupdt; 11109 TAILQ_FOREACH(listadp, listhead, ad_next) 11110 /* found our block */ 11111 if (listadp == adp) 11112 break; 11113 if (listadp == NULL) 11114 panic("handle_allocdirect_partdone: lost dep"); 11115#endif /* DEBUG */ 11116 return; 11117 } 11118 /* 11119 * If we have found the just finished dependency, then queue 11120 * it along with anything that follows it that is complete. 11121 * Since the pointer has not yet been written in the inode 11122 * as the dependency prevents it, place the allocdirect on the 11123 * bufwait list where it will be freed once the pointer is 11124 * valid. 11125 */ 11126 if (wkhd == NULL) 11127 wkhd = &inodedep->id_bufwait; 11128 for (; adp; adp = listadp) { 11129 listadp = TAILQ_NEXT(adp, ad_next); 11130 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11131 return; 11132 TAILQ_REMOVE(listhead, adp, ad_next); 11133 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list); 11134 } 11135} 11136 11137/* 11138 * Called from within softdep_disk_write_complete above. This routine 11139 * completes successfully written allocindirs. 11140 */ 11141static void 11142handle_allocindir_partdone(aip) 11143 struct allocindir *aip; /* the completed allocindir */ 11144{ 11145 struct indirdep *indirdep; 11146 11147 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE) 11148 return; 11149 indirdep = aip->ai_indirdep; 11150 LIST_REMOVE(aip, ai_next); 11151 /* 11152 * Don't set a pointer while the buffer is undergoing IO or while 11153 * we have active truncations. 11154 */ 11155 if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) { 11156 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next); 11157 return; 11158 } 11159 if (indirdep->ir_state & UFS1FMT) 11160 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11161 aip->ai_newblkno; 11162 else 11163 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11164 aip->ai_newblkno; 11165 /* 11166 * Await the pointer write before freeing the allocindir. 11167 */ 11168 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next); 11169} 11170 11171/* 11172 * Release segments held on a jwork list. 11173 */ 11174static void 11175handle_jwork(wkhd) 11176 struct workhead *wkhd; 11177{ 11178 struct worklist *wk; 11179 11180 while ((wk = LIST_FIRST(wkhd)) != NULL) { 11181 WORKLIST_REMOVE(wk); 11182 switch (wk->wk_type) { 11183 case D_JSEGDEP: 11184 free_jsegdep(WK_JSEGDEP(wk)); 11185 continue; 11186 case D_FREEDEP: 11187 free_freedep(WK_FREEDEP(wk)); 11188 continue; 11189 case D_FREEFRAG: 11190 rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep)); 11191 WORKITEM_FREE(wk, D_FREEFRAG); 11192 continue; 11193 case D_FREEWORK: 11194 handle_written_freework(WK_FREEWORK(wk)); 11195 continue; 11196 default: 11197 panic("handle_jwork: Unknown type %s\n", 11198 TYPENAME(wk->wk_type)); 11199 } 11200 } 11201} 11202 11203/* 11204 * Handle the bufwait list on an inode when it is safe to release items 11205 * held there. This normally happens after an inode block is written but 11206 * may be delayed and handled later if there are pending journal items that 11207 * are not yet safe to be released. 11208 */ 11209static struct freefile * 11210handle_bufwait(inodedep, refhd) 11211 struct inodedep *inodedep; 11212 struct workhead *refhd; 11213{ 11214 struct jaddref *jaddref; 11215 struct freefile *freefile; 11216 struct worklist *wk; 11217 11218 freefile = NULL; 11219 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) { 11220 WORKLIST_REMOVE(wk); 11221 switch (wk->wk_type) { 11222 case D_FREEFILE: 11223 /* 11224 * We defer adding freefile to the worklist 11225 * until all other additions have been made to 11226 * ensure that it will be done after all the 11227 * old blocks have been freed. 11228 */ 11229 if (freefile != NULL) 11230 panic("handle_bufwait: freefile"); 11231 freefile = WK_FREEFILE(wk); 11232 continue; 11233 11234 case D_MKDIR: 11235 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT); 11236 continue; 11237 11238 case D_DIRADD: 11239 diradd_inode_written(WK_DIRADD(wk), inodedep); 11240 continue; 11241 11242 case D_FREEFRAG: 11243 wk->wk_state |= COMPLETE; 11244 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE) 11245 add_to_worklist(wk, 0); 11246 continue; 11247 11248 case D_DIRREM: 11249 wk->wk_state |= COMPLETE; 11250 add_to_worklist(wk, 0); 11251 continue; 11252 11253 case D_ALLOCDIRECT: 11254 case D_ALLOCINDIR: 11255 free_newblk(WK_NEWBLK(wk)); 11256 continue; 11257 11258 case D_JNEWBLK: 11259 wk->wk_state |= COMPLETE; 11260 free_jnewblk(WK_JNEWBLK(wk)); 11261 continue; 11262 11263 /* 11264 * Save freed journal segments and add references on 11265 * the supplied list which will delay their release 11266 * until the cg bitmap is cleared on disk. 11267 */ 11268 case D_JSEGDEP: 11269 if (refhd == NULL) 11270 free_jsegdep(WK_JSEGDEP(wk)); 11271 else 11272 WORKLIST_INSERT(refhd, wk); 11273 continue; 11274 11275 case D_JADDREF: 11276 jaddref = WK_JADDREF(wk); 11277 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 11278 if_deps); 11279 /* 11280 * Transfer any jaddrefs to the list to be freed with 11281 * the bitmap if we're handling a removed file. 11282 */ 11283 if (refhd == NULL) { 11284 wk->wk_state |= COMPLETE; 11285 free_jaddref(jaddref); 11286 } else 11287 WORKLIST_INSERT(refhd, wk); 11288 continue; 11289 11290 default: 11291 panic("handle_bufwait: Unknown type %p(%s)", 11292 wk, TYPENAME(wk->wk_type)); 11293 /* NOTREACHED */ 11294 } 11295 } 11296 return (freefile); 11297} 11298/* 11299 * Called from within softdep_disk_write_complete above to restore 11300 * in-memory inode block contents to their most up-to-date state. Note 11301 * that this routine is always called from interrupt level with further 11302 * splbio interrupts blocked. 11303 */ 11304static int 11305handle_written_inodeblock(inodedep, bp) 11306 struct inodedep *inodedep; 11307 struct buf *bp; /* buffer containing the inode block */ 11308{ 11309 struct freefile *freefile; 11310 struct allocdirect *adp, *nextadp; 11311 struct ufs1_dinode *dp1 = NULL; 11312 struct ufs2_dinode *dp2 = NULL; 11313 struct workhead wkhd; 11314 int hadchanges, fstype; 11315 ino_t freelink; 11316 11317 LIST_INIT(&wkhd); 11318 hadchanges = 0; 11319 freefile = NULL; 11320 if ((inodedep->id_state & IOSTARTED) == 0) 11321 panic("handle_written_inodeblock: not started"); 11322 inodedep->id_state &= ~IOSTARTED; 11323 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) { 11324 fstype = UFS1; 11325 dp1 = (struct ufs1_dinode *)bp->b_data + 11326 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11327 freelink = dp1->di_freelink; 11328 } else { 11329 fstype = UFS2; 11330 dp2 = (struct ufs2_dinode *)bp->b_data + 11331 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11332 freelink = dp2->di_freelink; 11333 } 11334 /* 11335 * Leave this inodeblock dirty until it's in the list. 11336 */ 11337 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED) { 11338 struct inodedep *inon; 11339 11340 inon = TAILQ_NEXT(inodedep, id_unlinked); 11341 if ((inon == NULL && freelink == 0) || 11342 (inon && inon->id_ino == freelink)) { 11343 if (inon) 11344 inon->id_state |= UNLINKPREV; 11345 inodedep->id_state |= UNLINKNEXT; 11346 } 11347 hadchanges = 1; 11348 } 11349 /* 11350 * If we had to rollback the inode allocation because of 11351 * bitmaps being incomplete, then simply restore it. 11352 * Keep the block dirty so that it will not be reclaimed until 11353 * all associated dependencies have been cleared and the 11354 * corresponding updates written to disk. 11355 */ 11356 if (inodedep->id_savedino1 != NULL) { 11357 hadchanges = 1; 11358 if (fstype == UFS1) 11359 *dp1 = *inodedep->id_savedino1; 11360 else 11361 *dp2 = *inodedep->id_savedino2; 11362 free(inodedep->id_savedino1, M_SAVEDINO); 11363 inodedep->id_savedino1 = NULL; 11364 if ((bp->b_flags & B_DELWRI) == 0) 11365 stat_inode_bitmap++; 11366 bdirty(bp); 11367 /* 11368 * If the inode is clear here and GOINGAWAY it will never 11369 * be written. Process the bufwait and clear any pending 11370 * work which may include the freefile. 11371 */ 11372 if (inodedep->id_state & GOINGAWAY) 11373 goto bufwait; 11374 return (1); 11375 } 11376 inodedep->id_state |= COMPLETE; 11377 /* 11378 * Roll forward anything that had to be rolled back before 11379 * the inode could be updated. 11380 */ 11381 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) { 11382 nextadp = TAILQ_NEXT(adp, ad_next); 11383 if (adp->ad_state & ATTACHED) 11384 panic("handle_written_inodeblock: new entry"); 11385 if (fstype == UFS1) { 11386 if (adp->ad_offset < NDADDR) { 11387 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11388 panic("%s %s #%jd mismatch %d != %jd", 11389 "handle_written_inodeblock:", 11390 "direct pointer", 11391 (intmax_t)adp->ad_offset, 11392 dp1->di_db[adp->ad_offset], 11393 (intmax_t)adp->ad_oldblkno); 11394 dp1->di_db[adp->ad_offset] = adp->ad_newblkno; 11395 } else { 11396 if (dp1->di_ib[adp->ad_offset - NDADDR] != 0) 11397 panic("%s: %s #%jd allocated as %d", 11398 "handle_written_inodeblock", 11399 "indirect pointer", 11400 (intmax_t)adp->ad_offset - NDADDR, 11401 dp1->di_ib[adp->ad_offset - NDADDR]); 11402 dp1->di_ib[adp->ad_offset - NDADDR] = 11403 adp->ad_newblkno; 11404 } 11405 } else { 11406 if (adp->ad_offset < NDADDR) { 11407 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11408 panic("%s: %s #%jd %s %jd != %jd", 11409 "handle_written_inodeblock", 11410 "direct pointer", 11411 (intmax_t)adp->ad_offset, "mismatch", 11412 (intmax_t)dp2->di_db[adp->ad_offset], 11413 (intmax_t)adp->ad_oldblkno); 11414 dp2->di_db[adp->ad_offset] = adp->ad_newblkno; 11415 } else { 11416 if (dp2->di_ib[adp->ad_offset - NDADDR] != 0) 11417 panic("%s: %s #%jd allocated as %jd", 11418 "handle_written_inodeblock", 11419 "indirect pointer", 11420 (intmax_t)adp->ad_offset - NDADDR, 11421 (intmax_t) 11422 dp2->di_ib[adp->ad_offset - NDADDR]); 11423 dp2->di_ib[adp->ad_offset - NDADDR] = 11424 adp->ad_newblkno; 11425 } 11426 } 11427 adp->ad_state &= ~UNDONE; 11428 adp->ad_state |= ATTACHED; 11429 hadchanges = 1; 11430 } 11431 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) { 11432 nextadp = TAILQ_NEXT(adp, ad_next); 11433 if (adp->ad_state & ATTACHED) 11434 panic("handle_written_inodeblock: new entry"); 11435 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno) 11436 panic("%s: direct pointers #%jd %s %jd != %jd", 11437 "handle_written_inodeblock", 11438 (intmax_t)adp->ad_offset, "mismatch", 11439 (intmax_t)dp2->di_extb[adp->ad_offset], 11440 (intmax_t)adp->ad_oldblkno); 11441 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno; 11442 adp->ad_state &= ~UNDONE; 11443 adp->ad_state |= ATTACHED; 11444 hadchanges = 1; 11445 } 11446 if (hadchanges && (bp->b_flags & B_DELWRI) == 0) 11447 stat_direct_blk_ptrs++; 11448 /* 11449 * Reset the file size to its most up-to-date value. 11450 */ 11451 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1) 11452 panic("handle_written_inodeblock: bad size"); 11453 if (inodedep->id_savednlink > LINK_MAX) 11454 panic("handle_written_inodeblock: Invalid link count " 11455 "%d for inodedep %p", inodedep->id_savednlink, inodedep); 11456 if (fstype == UFS1) { 11457 if (dp1->di_nlink != inodedep->id_savednlink) { 11458 dp1->di_nlink = inodedep->id_savednlink; 11459 hadchanges = 1; 11460 } 11461 if (dp1->di_size != inodedep->id_savedsize) { 11462 dp1->di_size = inodedep->id_savedsize; 11463 hadchanges = 1; 11464 } 11465 } else { 11466 if (dp2->di_nlink != inodedep->id_savednlink) { 11467 dp2->di_nlink = inodedep->id_savednlink; 11468 hadchanges = 1; 11469 } 11470 if (dp2->di_size != inodedep->id_savedsize) { 11471 dp2->di_size = inodedep->id_savedsize; 11472 hadchanges = 1; 11473 } 11474 if (dp2->di_extsize != inodedep->id_savedextsize) { 11475 dp2->di_extsize = inodedep->id_savedextsize; 11476 hadchanges = 1; 11477 } 11478 } 11479 inodedep->id_savedsize = -1; 11480 inodedep->id_savedextsize = -1; 11481 inodedep->id_savednlink = -1; 11482 /* 11483 * If there were any rollbacks in the inode block, then it must be 11484 * marked dirty so that its will eventually get written back in 11485 * its correct form. 11486 */ 11487 if (hadchanges) 11488 bdirty(bp); 11489bufwait: 11490 /* 11491 * Process any allocdirects that completed during the update. 11492 */ 11493 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 11494 handle_allocdirect_partdone(adp, &wkhd); 11495 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 11496 handle_allocdirect_partdone(adp, &wkhd); 11497 /* 11498 * Process deallocations that were held pending until the 11499 * inode had been written to disk. Freeing of the inode 11500 * is delayed until after all blocks have been freed to 11501 * avoid creation of new <vfsid, inum, lbn> triples 11502 * before the old ones have been deleted. Completely 11503 * unlinked inodes are not processed until the unlinked 11504 * inode list is written or the last reference is removed. 11505 */ 11506 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) { 11507 freefile = handle_bufwait(inodedep, NULL); 11508 if (freefile && !LIST_EMPTY(&wkhd)) { 11509 WORKLIST_INSERT(&wkhd, &freefile->fx_list); 11510 freefile = NULL; 11511 } 11512 } 11513 /* 11514 * Move rolled forward dependency completions to the bufwait list 11515 * now that those that were already written have been processed. 11516 */ 11517 if (!LIST_EMPTY(&wkhd) && hadchanges == 0) 11518 panic("handle_written_inodeblock: bufwait but no changes"); 11519 jwork_move(&inodedep->id_bufwait, &wkhd); 11520 11521 if (freefile != NULL) { 11522 /* 11523 * If the inode is goingaway it was never written. Fake up 11524 * the state here so free_inodedep() can succeed. 11525 */ 11526 if (inodedep->id_state & GOINGAWAY) 11527 inodedep->id_state |= COMPLETE | DEPCOMPLETE; 11528 if (free_inodedep(inodedep) == 0) 11529 panic("handle_written_inodeblock: live inodedep %p", 11530 inodedep); 11531 add_to_worklist(&freefile->fx_list, 0); 11532 return (0); 11533 } 11534 11535 /* 11536 * If no outstanding dependencies, free it. 11537 */ 11538 if (free_inodedep(inodedep) || 11539 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 && 11540 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 && 11541 TAILQ_FIRST(&inodedep->id_extupdt) == 0 && 11542 LIST_FIRST(&inodedep->id_bufwait) == 0)) 11543 return (0); 11544 return (hadchanges); 11545} 11546 11547static int 11548handle_written_indirdep(indirdep, bp, bpp) 11549 struct indirdep *indirdep; 11550 struct buf *bp; 11551 struct buf **bpp; 11552{ 11553 struct allocindir *aip; 11554 struct buf *sbp; 11555 int chgs; 11556 11557 if (indirdep->ir_state & GOINGAWAY) 11558 panic("handle_written_indirdep: indirdep gone"); 11559 if ((indirdep->ir_state & IOSTARTED) == 0) 11560 panic("handle_written_indirdep: IO not started"); 11561 chgs = 0; 11562 /* 11563 * If there were rollbacks revert them here. 11564 */ 11565 if (indirdep->ir_saveddata) { 11566 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount); 11567 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11568 free(indirdep->ir_saveddata, M_INDIRDEP); 11569 indirdep->ir_saveddata = NULL; 11570 } 11571 chgs = 1; 11572 } 11573 indirdep->ir_state &= ~(UNDONE | IOSTARTED); 11574 indirdep->ir_state |= ATTACHED; 11575 /* 11576 * Move allocindirs with written pointers to the completehd if 11577 * the indirdep's pointer is not yet written. Otherwise 11578 * free them here. 11579 */ 11580 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != 0) { 11581 LIST_REMOVE(aip, ai_next); 11582 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 11583 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip, 11584 ai_next); 11585 newblk_freefrag(&aip->ai_block); 11586 continue; 11587 } 11588 free_newblk(&aip->ai_block); 11589 } 11590 /* 11591 * Move allocindirs that have finished dependency processing from 11592 * the done list to the write list after updating the pointers. 11593 */ 11594 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11595 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0) { 11596 handle_allocindir_partdone(aip); 11597 if (aip == LIST_FIRST(&indirdep->ir_donehd)) 11598 panic("disk_write_complete: not gone"); 11599 chgs = 1; 11600 } 11601 } 11602 /* 11603 * Preserve the indirdep if there were any changes or if it is not 11604 * yet valid on disk. 11605 */ 11606 if (chgs) { 11607 stat_indir_blk_ptrs++; 11608 bdirty(bp); 11609 return (1); 11610 } 11611 /* 11612 * If there were no changes we can discard the savedbp and detach 11613 * ourselves from the buf. We are only carrying completed pointers 11614 * in this case. 11615 */ 11616 sbp = indirdep->ir_savebp; 11617 sbp->b_flags |= B_INVAL | B_NOCACHE; 11618 indirdep->ir_savebp = NULL; 11619 indirdep->ir_bp = NULL; 11620 if (*bpp != NULL) 11621 panic("handle_written_indirdep: bp already exists."); 11622 *bpp = sbp; 11623 /* 11624 * The indirdep may not be freed until its parent points at it. 11625 */ 11626 if (indirdep->ir_state & DEPCOMPLETE) 11627 free_indirdep(indirdep); 11628 11629 return (0); 11630} 11631 11632/* 11633 * Process a diradd entry after its dependent inode has been written. 11634 * This routine must be called with splbio interrupts blocked. 11635 */ 11636static void 11637diradd_inode_written(dap, inodedep) 11638 struct diradd *dap; 11639 struct inodedep *inodedep; 11640{ 11641 11642 dap->da_state |= COMPLETE; 11643 complete_diradd(dap); 11644 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 11645} 11646 11647/* 11648 * Returns true if the bmsafemap will have rollbacks when written. Must only 11649 * be called with the per-filesystem lock and the buf lock on the cg held. 11650 */ 11651static int 11652bmsafemap_backgroundwrite(bmsafemap, bp) 11653 struct bmsafemap *bmsafemap; 11654 struct buf *bp; 11655{ 11656 int dirty; 11657 11658 LOCK_OWNED(VFSTOUFS(bmsafemap->sm_list.wk_mp)); 11659 dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) | 11660 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd); 11661 /* 11662 * If we're initiating a background write we need to process the 11663 * rollbacks as they exist now, not as they exist when IO starts. 11664 * No other consumers will look at the contents of the shadowed 11665 * buf so this is safe to do here. 11666 */ 11667 if (bp->b_xflags & BX_BKGRDMARKER) 11668 initiate_write_bmsafemap(bmsafemap, bp); 11669 11670 return (dirty); 11671} 11672 11673/* 11674 * Re-apply an allocation when a cg write is complete. 11675 */ 11676static int 11677jnewblk_rollforward(jnewblk, fs, cgp, blksfree) 11678 struct jnewblk *jnewblk; 11679 struct fs *fs; 11680 struct cg *cgp; 11681 uint8_t *blksfree; 11682{ 11683 ufs1_daddr_t fragno; 11684 ufs2_daddr_t blkno; 11685 long cgbno, bbase; 11686 int frags, blk; 11687 int i; 11688 11689 frags = 0; 11690 cgbno = dtogd(fs, jnewblk->jn_blkno); 11691 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) { 11692 if (isclr(blksfree, cgbno + i)) 11693 panic("jnewblk_rollforward: re-allocated fragment"); 11694 frags++; 11695 } 11696 if (frags == fs->fs_frag) { 11697 blkno = fragstoblks(fs, cgbno); 11698 ffs_clrblock(fs, blksfree, (long)blkno); 11699 ffs_clusteracct(fs, cgp, blkno, -1); 11700 cgp->cg_cs.cs_nbfree--; 11701 } else { 11702 bbase = cgbno - fragnum(fs, cgbno); 11703 cgbno += jnewblk->jn_oldfrags; 11704 /* If a complete block had been reassembled, account for it. */ 11705 fragno = fragstoblks(fs, bbase); 11706 if (ffs_isblock(fs, blksfree, fragno)) { 11707 cgp->cg_cs.cs_nffree += fs->fs_frag; 11708 ffs_clusteracct(fs, cgp, fragno, -1); 11709 cgp->cg_cs.cs_nbfree--; 11710 } 11711 /* Decrement the old frags. */ 11712 blk = blkmap(fs, blksfree, bbase); 11713 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 11714 /* Allocate the fragment */ 11715 for (i = 0; i < frags; i++) 11716 clrbit(blksfree, cgbno + i); 11717 cgp->cg_cs.cs_nffree -= frags; 11718 /* Add back in counts associated with the new frags */ 11719 blk = blkmap(fs, blksfree, bbase); 11720 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 11721 } 11722 return (frags); 11723} 11724 11725/* 11726 * Complete a write to a bmsafemap structure. Roll forward any bitmap 11727 * changes if it's not a background write. Set all written dependencies 11728 * to DEPCOMPLETE and free the structure if possible. 11729 */ 11730static int 11731handle_written_bmsafemap(bmsafemap, bp) 11732 struct bmsafemap *bmsafemap; 11733 struct buf *bp; 11734{ 11735 struct newblk *newblk; 11736 struct inodedep *inodedep; 11737 struct jaddref *jaddref, *jatmp; 11738 struct jnewblk *jnewblk, *jntmp; 11739 struct ufsmount *ump; 11740 uint8_t *inosused; 11741 uint8_t *blksfree; 11742 struct cg *cgp; 11743 struct fs *fs; 11744 ino_t ino; 11745 int foreground; 11746 int chgs; 11747 11748 if ((bmsafemap->sm_state & IOSTARTED) == 0) 11749 panic("initiate_write_bmsafemap: Not started\n"); 11750 ump = VFSTOUFS(bmsafemap->sm_list.wk_mp); 11751 chgs = 0; 11752 bmsafemap->sm_state &= ~IOSTARTED; 11753 foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0; 11754 /* 11755 * Release journal work that was waiting on the write. 11756 */ 11757 handle_jwork(&bmsafemap->sm_freewr); 11758 11759 /* 11760 * Restore unwritten inode allocation pending jaddref writes. 11761 */ 11762 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) { 11763 cgp = (struct cg *)bp->b_data; 11764 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11765 inosused = cg_inosused(cgp); 11766 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd, 11767 ja_bmdeps, jatmp) { 11768 if ((jaddref->ja_state & UNDONE) == 0) 11769 continue; 11770 ino = jaddref->ja_ino % fs->fs_ipg; 11771 if (isset(inosused, ino)) 11772 panic("handle_written_bmsafemap: " 11773 "re-allocated inode"); 11774 /* Do the roll-forward only if it's a real copy. */ 11775 if (foreground) { 11776 if ((jaddref->ja_mode & IFMT) == IFDIR) 11777 cgp->cg_cs.cs_ndir++; 11778 cgp->cg_cs.cs_nifree--; 11779 setbit(inosused, ino); 11780 chgs = 1; 11781 } 11782 jaddref->ja_state &= ~UNDONE; 11783 jaddref->ja_state |= ATTACHED; 11784 free_jaddref(jaddref); 11785 } 11786 } 11787 /* 11788 * Restore any block allocations which are pending journal writes. 11789 */ 11790 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 11791 cgp = (struct cg *)bp->b_data; 11792 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11793 blksfree = cg_blksfree(cgp); 11794 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps, 11795 jntmp) { 11796 if ((jnewblk->jn_state & UNDONE) == 0) 11797 continue; 11798 /* Do the roll-forward only if it's a real copy. */ 11799 if (foreground && 11800 jnewblk_rollforward(jnewblk, fs, cgp, blksfree)) 11801 chgs = 1; 11802 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK); 11803 jnewblk->jn_state |= ATTACHED; 11804 free_jnewblk(jnewblk); 11805 } 11806 } 11807 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) { 11808 newblk->nb_state |= DEPCOMPLETE; 11809 newblk->nb_state &= ~ONDEPLIST; 11810 newblk->nb_bmsafemap = NULL; 11811 LIST_REMOVE(newblk, nb_deps); 11812 if (newblk->nb_list.wk_type == D_ALLOCDIRECT) 11813 handle_allocdirect_partdone( 11814 WK_ALLOCDIRECT(&newblk->nb_list), NULL); 11815 else if (newblk->nb_list.wk_type == D_ALLOCINDIR) 11816 handle_allocindir_partdone( 11817 WK_ALLOCINDIR(&newblk->nb_list)); 11818 else if (newblk->nb_list.wk_type != D_NEWBLK) 11819 panic("handle_written_bmsafemap: Unexpected type: %s", 11820 TYPENAME(newblk->nb_list.wk_type)); 11821 } 11822 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) { 11823 inodedep->id_state |= DEPCOMPLETE; 11824 inodedep->id_state &= ~ONDEPLIST; 11825 LIST_REMOVE(inodedep, id_deps); 11826 inodedep->id_bmsafemap = NULL; 11827 } 11828 LIST_REMOVE(bmsafemap, sm_next); 11829 if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) && 11830 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) && 11831 LIST_EMPTY(&bmsafemap->sm_newblkhd) && 11832 LIST_EMPTY(&bmsafemap->sm_inodedephd) && 11833 LIST_EMPTY(&bmsafemap->sm_freehd)) { 11834 LIST_REMOVE(bmsafemap, sm_hash); 11835 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 11836 return (0); 11837 } 11838 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 11839 if (foreground) 11840 bdirty(bp); 11841 return (1); 11842} 11843 11844/* 11845 * Try to free a mkdir dependency. 11846 */ 11847static void 11848complete_mkdir(mkdir) 11849 struct mkdir *mkdir; 11850{ 11851 struct diradd *dap; 11852 11853 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE) 11854 return; 11855 LIST_REMOVE(mkdir, md_mkdirs); 11856 dap = mkdir->md_diradd; 11857 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 11858 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) { 11859 dap->da_state |= DEPCOMPLETE; 11860 complete_diradd(dap); 11861 } 11862 WORKITEM_FREE(mkdir, D_MKDIR); 11863} 11864 11865/* 11866 * Handle the completion of a mkdir dependency. 11867 */ 11868static void 11869handle_written_mkdir(mkdir, type) 11870 struct mkdir *mkdir; 11871 int type; 11872{ 11873 11874 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type) 11875 panic("handle_written_mkdir: bad type"); 11876 mkdir->md_state |= COMPLETE; 11877 complete_mkdir(mkdir); 11878} 11879 11880static int 11881free_pagedep(pagedep) 11882 struct pagedep *pagedep; 11883{ 11884 int i; 11885 11886 if (pagedep->pd_state & NEWBLOCK) 11887 return (0); 11888 if (!LIST_EMPTY(&pagedep->pd_dirremhd)) 11889 return (0); 11890 for (i = 0; i < DAHASHSZ; i++) 11891 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i])) 11892 return (0); 11893 if (!LIST_EMPTY(&pagedep->pd_pendinghd)) 11894 return (0); 11895 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd)) 11896 return (0); 11897 if (pagedep->pd_state & ONWORKLIST) 11898 WORKLIST_REMOVE(&pagedep->pd_list); 11899 LIST_REMOVE(pagedep, pd_hash); 11900 WORKITEM_FREE(pagedep, D_PAGEDEP); 11901 11902 return (1); 11903} 11904 11905/* 11906 * Called from within softdep_disk_write_complete above. 11907 * A write operation was just completed. Removed inodes can 11908 * now be freed and associated block pointers may be committed. 11909 * Note that this routine is always called from interrupt level 11910 * with further splbio interrupts blocked. 11911 */ 11912static int 11913handle_written_filepage(pagedep, bp) 11914 struct pagedep *pagedep; 11915 struct buf *bp; /* buffer containing the written page */ 11916{ 11917 struct dirrem *dirrem; 11918 struct diradd *dap, *nextdap; 11919 struct direct *ep; 11920 int i, chgs; 11921 11922 if ((pagedep->pd_state & IOSTARTED) == 0) 11923 panic("handle_written_filepage: not started"); 11924 pagedep->pd_state &= ~IOSTARTED; 11925 /* 11926 * Process any directory removals that have been committed. 11927 */ 11928 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) { 11929 LIST_REMOVE(dirrem, dm_next); 11930 dirrem->dm_state |= COMPLETE; 11931 dirrem->dm_dirinum = pagedep->pd_ino; 11932 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 11933 ("handle_written_filepage: Journal entries not written.")); 11934 add_to_worklist(&dirrem->dm_list, 0); 11935 } 11936 /* 11937 * Free any directory additions that have been committed. 11938 * If it is a newly allocated block, we have to wait until 11939 * the on-disk directory inode claims the new block. 11940 */ 11941 if ((pagedep->pd_state & NEWBLOCK) == 0) 11942 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 11943 free_diradd(dap, NULL); 11944 /* 11945 * Uncommitted directory entries must be restored. 11946 */ 11947 for (chgs = 0, i = 0; i < DAHASHSZ; i++) { 11948 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap; 11949 dap = nextdap) { 11950 nextdap = LIST_NEXT(dap, da_pdlist); 11951 if (dap->da_state & ATTACHED) 11952 panic("handle_written_filepage: attached"); 11953 ep = (struct direct *) 11954 ((char *)bp->b_data + dap->da_offset); 11955 ep->d_ino = dap->da_newinum; 11956 dap->da_state &= ~UNDONE; 11957 dap->da_state |= ATTACHED; 11958 chgs = 1; 11959 /* 11960 * If the inode referenced by the directory has 11961 * been written out, then the dependency can be 11962 * moved to the pending list. 11963 */ 11964 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 11965 LIST_REMOVE(dap, da_pdlist); 11966 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, 11967 da_pdlist); 11968 } 11969 } 11970 } 11971 /* 11972 * If there were any rollbacks in the directory, then it must be 11973 * marked dirty so that its will eventually get written back in 11974 * its correct form. 11975 */ 11976 if (chgs) { 11977 if ((bp->b_flags & B_DELWRI) == 0) 11978 stat_dir_entry++; 11979 bdirty(bp); 11980 return (1); 11981 } 11982 /* 11983 * If we are not waiting for a new directory block to be 11984 * claimed by its inode, then the pagedep will be freed. 11985 * Otherwise it will remain to track any new entries on 11986 * the page in case they are fsync'ed. 11987 */ 11988 free_pagedep(pagedep); 11989 return (0); 11990} 11991 11992/* 11993 * Writing back in-core inode structures. 11994 * 11995 * The filesystem only accesses an inode's contents when it occupies an 11996 * "in-core" inode structure. These "in-core" structures are separate from 11997 * the page frames used to cache inode blocks. Only the latter are 11998 * transferred to/from the disk. So, when the updated contents of the 11999 * "in-core" inode structure are copied to the corresponding in-memory inode 12000 * block, the dependencies are also transferred. The following procedure is 12001 * called when copying a dirty "in-core" inode to a cached inode block. 12002 */ 12003 12004/* 12005 * Called when an inode is loaded from disk. If the effective link count 12006 * differed from the actual link count when it was last flushed, then we 12007 * need to ensure that the correct effective link count is put back. 12008 */ 12009void 12010softdep_load_inodeblock(ip) 12011 struct inode *ip; /* the "in_core" copy of the inode */ 12012{ 12013 struct inodedep *inodedep; 12014 12015 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0, 12016 ("softdep_load_inodeblock called on non-softdep filesystem")); 12017 /* 12018 * Check for alternate nlink count. 12019 */ 12020 ip->i_effnlink = ip->i_nlink; 12021 ACQUIRE_LOCK(ip->i_ump); 12022 if (inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0, 12023 &inodedep) == 0) { 12024 FREE_LOCK(ip->i_ump); 12025 return; 12026 } 12027 ip->i_effnlink -= inodedep->id_nlinkdelta; 12028 FREE_LOCK(ip->i_ump); 12029} 12030 12031/* 12032 * This routine is called just before the "in-core" inode 12033 * information is to be copied to the in-memory inode block. 12034 * Recall that an inode block contains several inodes. If 12035 * the force flag is set, then the dependencies will be 12036 * cleared so that the update can always be made. Note that 12037 * the buffer is locked when this routine is called, so we 12038 * will never be in the middle of writing the inode block 12039 * to disk. 12040 */ 12041void 12042softdep_update_inodeblock(ip, bp, waitfor) 12043 struct inode *ip; /* the "in_core" copy of the inode */ 12044 struct buf *bp; /* the buffer containing the inode block */ 12045 int waitfor; /* nonzero => update must be allowed */ 12046{ 12047 struct inodedep *inodedep; 12048 struct inoref *inoref; 12049 struct ufsmount *ump; 12050 struct worklist *wk; 12051 struct mount *mp; 12052 struct buf *ibp; 12053 struct fs *fs; 12054 int error; 12055 12056 ump = ip->i_ump; 12057 mp = UFSTOVFS(ump); 12058 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 12059 ("softdep_update_inodeblock called on non-softdep filesystem")); 12060 fs = ip->i_fs; 12061 /* 12062 * Preserve the freelink that is on disk. clear_unlinked_inodedep() 12063 * does not have access to the in-core ip so must write directly into 12064 * the inode block buffer when setting freelink. 12065 */ 12066 if (fs->fs_magic == FS_UFS1_MAGIC) 12067 DIP_SET(ip, i_freelink, ((struct ufs1_dinode *)bp->b_data + 12068 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12069 else 12070 DIP_SET(ip, i_freelink, ((struct ufs2_dinode *)bp->b_data + 12071 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12072 /* 12073 * If the effective link count is not equal to the actual link 12074 * count, then we must track the difference in an inodedep while 12075 * the inode is (potentially) tossed out of the cache. Otherwise, 12076 * if there is no existing inodedep, then there are no dependencies 12077 * to track. 12078 */ 12079 ACQUIRE_LOCK(ump); 12080again: 12081 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12082 FREE_LOCK(ump); 12083 if (ip->i_effnlink != ip->i_nlink) 12084 panic("softdep_update_inodeblock: bad link count"); 12085 return; 12086 } 12087 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink) 12088 panic("softdep_update_inodeblock: bad delta"); 12089 /* 12090 * If we're flushing all dependencies we must also move any waiting 12091 * for journal writes onto the bufwait list prior to I/O. 12092 */ 12093 if (waitfor) { 12094 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12095 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12096 == DEPCOMPLETE) { 12097 jwait(&inoref->if_list, MNT_WAIT); 12098 goto again; 12099 } 12100 } 12101 } 12102 /* 12103 * Changes have been initiated. Anything depending on these 12104 * changes cannot occur until this inode has been written. 12105 */ 12106 inodedep->id_state &= ~COMPLETE; 12107 if ((inodedep->id_state & ONWORKLIST) == 0) 12108 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list); 12109 /* 12110 * Any new dependencies associated with the incore inode must 12111 * now be moved to the list associated with the buffer holding 12112 * the in-memory copy of the inode. Once merged process any 12113 * allocdirects that are completed by the merger. 12114 */ 12115 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt); 12116 if (!TAILQ_EMPTY(&inodedep->id_inoupdt)) 12117 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt), 12118 NULL); 12119 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt); 12120 if (!TAILQ_EMPTY(&inodedep->id_extupdt)) 12121 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt), 12122 NULL); 12123 /* 12124 * Now that the inode has been pushed into the buffer, the 12125 * operations dependent on the inode being written to disk 12126 * can be moved to the id_bufwait so that they will be 12127 * processed when the buffer I/O completes. 12128 */ 12129 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) { 12130 WORKLIST_REMOVE(wk); 12131 WORKLIST_INSERT(&inodedep->id_bufwait, wk); 12132 } 12133 /* 12134 * Newly allocated inodes cannot be written until the bitmap 12135 * that allocates them have been written (indicated by 12136 * DEPCOMPLETE being set in id_state). If we are doing a 12137 * forced sync (e.g., an fsync on a file), we force the bitmap 12138 * to be written so that the update can be done. 12139 */ 12140 if (waitfor == 0) { 12141 FREE_LOCK(ump); 12142 return; 12143 } 12144retry: 12145 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) { 12146 FREE_LOCK(ump); 12147 return; 12148 } 12149 ibp = inodedep->id_bmsafemap->sm_buf; 12150 ibp = getdirtybuf(ibp, LOCK_PTR(ump), MNT_WAIT); 12151 if (ibp == NULL) { 12152 /* 12153 * If ibp came back as NULL, the dependency could have been 12154 * freed while we slept. Look it up again, and check to see 12155 * that it has completed. 12156 */ 12157 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 12158 goto retry; 12159 FREE_LOCK(ump); 12160 return; 12161 } 12162 FREE_LOCK(ump); 12163 if ((error = bwrite(ibp)) != 0) 12164 softdep_error("softdep_update_inodeblock: bwrite", error); 12165} 12166 12167/* 12168 * Merge the a new inode dependency list (such as id_newinoupdt) into an 12169 * old inode dependency list (such as id_inoupdt). This routine must be 12170 * called with splbio interrupts blocked. 12171 */ 12172static void 12173merge_inode_lists(newlisthead, oldlisthead) 12174 struct allocdirectlst *newlisthead; 12175 struct allocdirectlst *oldlisthead; 12176{ 12177 struct allocdirect *listadp, *newadp; 12178 12179 newadp = TAILQ_FIRST(newlisthead); 12180 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) { 12181 if (listadp->ad_offset < newadp->ad_offset) { 12182 listadp = TAILQ_NEXT(listadp, ad_next); 12183 continue; 12184 } 12185 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12186 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next); 12187 if (listadp->ad_offset == newadp->ad_offset) { 12188 allocdirect_merge(oldlisthead, newadp, 12189 listadp); 12190 listadp = newadp; 12191 } 12192 newadp = TAILQ_FIRST(newlisthead); 12193 } 12194 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) { 12195 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12196 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next); 12197 } 12198} 12199 12200/* 12201 * If we are doing an fsync, then we must ensure that any directory 12202 * entries for the inode have been written after the inode gets to disk. 12203 */ 12204int 12205softdep_fsync(vp) 12206 struct vnode *vp; /* the "in_core" copy of the inode */ 12207{ 12208 struct inodedep *inodedep; 12209 struct pagedep *pagedep; 12210 struct inoref *inoref; 12211 struct ufsmount *ump; 12212 struct worklist *wk; 12213 struct diradd *dap; 12214 struct mount *mp; 12215 struct vnode *pvp; 12216 struct inode *ip; 12217 struct buf *bp; 12218 struct fs *fs; 12219 struct thread *td = curthread; 12220 int error, flushparent, pagedep_new_block; 12221 ino_t parentino; 12222 ufs_lbn_t lbn; 12223 12224 ip = VTOI(vp); 12225 fs = ip->i_fs; 12226 ump = ip->i_ump; 12227 mp = vp->v_mount; 12228 if (MOUNTEDSOFTDEP(mp) == 0) 12229 return (0); 12230 ACQUIRE_LOCK(ump); 12231restart: 12232 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12233 FREE_LOCK(ump); 12234 return (0); 12235 } 12236 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12237 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12238 == DEPCOMPLETE) { 12239 jwait(&inoref->if_list, MNT_WAIT); 12240 goto restart; 12241 } 12242 } 12243 if (!LIST_EMPTY(&inodedep->id_inowait) || 12244 !TAILQ_EMPTY(&inodedep->id_extupdt) || 12245 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 12246 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 12247 !TAILQ_EMPTY(&inodedep->id_newinoupdt)) 12248 panic("softdep_fsync: pending ops %p", inodedep); 12249 for (error = 0, flushparent = 0; ; ) { 12250 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL) 12251 break; 12252 if (wk->wk_type != D_DIRADD) 12253 panic("softdep_fsync: Unexpected type %s", 12254 TYPENAME(wk->wk_type)); 12255 dap = WK_DIRADD(wk); 12256 /* 12257 * Flush our parent if this directory entry has a MKDIR_PARENT 12258 * dependency or is contained in a newly allocated block. 12259 */ 12260 if (dap->da_state & DIRCHG) 12261 pagedep = dap->da_previous->dm_pagedep; 12262 else 12263 pagedep = dap->da_pagedep; 12264 parentino = pagedep->pd_ino; 12265 lbn = pagedep->pd_lbn; 12266 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) 12267 panic("softdep_fsync: dirty"); 12268 if ((dap->da_state & MKDIR_PARENT) || 12269 (pagedep->pd_state & NEWBLOCK)) 12270 flushparent = 1; 12271 else 12272 flushparent = 0; 12273 /* 12274 * If we are being fsync'ed as part of vgone'ing this vnode, 12275 * then we will not be able to release and recover the 12276 * vnode below, so we just have to give up on writing its 12277 * directory entry out. It will eventually be written, just 12278 * not now, but then the user was not asking to have it 12279 * written, so we are not breaking any promises. 12280 */ 12281 if (vp->v_iflag & VI_DOOMED) 12282 break; 12283 /* 12284 * We prevent deadlock by always fetching inodes from the 12285 * root, moving down the directory tree. Thus, when fetching 12286 * our parent directory, we first try to get the lock. If 12287 * that fails, we must unlock ourselves before requesting 12288 * the lock on our parent. See the comment in ufs_lookup 12289 * for details on possible races. 12290 */ 12291 FREE_LOCK(ump); 12292 if (ffs_vgetf(mp, parentino, LK_NOWAIT | LK_EXCLUSIVE, &pvp, 12293 FFSV_FORCEINSMQ)) { 12294 error = vfs_busy(mp, MBF_NOWAIT); 12295 if (error != 0) { 12296 vfs_ref(mp); 12297 VOP_UNLOCK(vp, 0); 12298 error = vfs_busy(mp, 0); 12299 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 12300 vfs_rel(mp); 12301 if (error != 0) 12302 return (ENOENT); 12303 if (vp->v_iflag & VI_DOOMED) { 12304 vfs_unbusy(mp); 12305 return (ENOENT); 12306 } 12307 } 12308 VOP_UNLOCK(vp, 0); 12309 error = ffs_vgetf(mp, parentino, LK_EXCLUSIVE, 12310 &pvp, FFSV_FORCEINSMQ); 12311 vfs_unbusy(mp); 12312 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 12313 if (vp->v_iflag & VI_DOOMED) { 12314 if (error == 0) 12315 vput(pvp); 12316 error = ENOENT; 12317 } 12318 if (error != 0) 12319 return (error); 12320 } 12321 /* 12322 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps 12323 * that are contained in direct blocks will be resolved by 12324 * doing a ffs_update. Pagedeps contained in indirect blocks 12325 * may require a complete sync'ing of the directory. So, we 12326 * try the cheap and fast ffs_update first, and if that fails, 12327 * then we do the slower ffs_syncvnode of the directory. 12328 */ 12329 if (flushparent) { 12330 int locked; 12331 12332 if ((error = ffs_update(pvp, 1)) != 0) { 12333 vput(pvp); 12334 return (error); 12335 } 12336 ACQUIRE_LOCK(ump); 12337 locked = 1; 12338 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) { 12339 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) { 12340 if (wk->wk_type != D_DIRADD) 12341 panic("softdep_fsync: Unexpected type %s", 12342 TYPENAME(wk->wk_type)); 12343 dap = WK_DIRADD(wk); 12344 if (dap->da_state & DIRCHG) 12345 pagedep = dap->da_previous->dm_pagedep; 12346 else 12347 pagedep = dap->da_pagedep; 12348 pagedep_new_block = pagedep->pd_state & NEWBLOCK; 12349 FREE_LOCK(ump); 12350 locked = 0; 12351 if (pagedep_new_block && (error = 12352 ffs_syncvnode(pvp, MNT_WAIT, 0))) { 12353 vput(pvp); 12354 return (error); 12355 } 12356 } 12357 } 12358 if (locked) 12359 FREE_LOCK(ump); 12360 } 12361 /* 12362 * Flush directory page containing the inode's name. 12363 */ 12364 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred, 12365 &bp); 12366 if (error == 0) 12367 error = bwrite(bp); 12368 else 12369 brelse(bp); 12370 vput(pvp); 12371 if (error != 0) 12372 return (error); 12373 ACQUIRE_LOCK(ump); 12374 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 12375 break; 12376 } 12377 FREE_LOCK(ump); 12378 return (0); 12379} 12380 12381/* 12382 * Flush all the dirty bitmaps associated with the block device 12383 * before flushing the rest of the dirty blocks so as to reduce 12384 * the number of dependencies that will have to be rolled back. 12385 * 12386 * XXX Unused? 12387 */ 12388void 12389softdep_fsync_mountdev(vp) 12390 struct vnode *vp; 12391{ 12392 struct buf *bp, *nbp; 12393 struct worklist *wk; 12394 struct bufobj *bo; 12395 12396 if (!vn_isdisk(vp, NULL)) 12397 panic("softdep_fsync_mountdev: vnode not a disk"); 12398 bo = &vp->v_bufobj; 12399restart: 12400 BO_LOCK(bo); 12401 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 12402 /* 12403 * If it is already scheduled, skip to the next buffer. 12404 */ 12405 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) 12406 continue; 12407 12408 if ((bp->b_flags & B_DELWRI) == 0) 12409 panic("softdep_fsync_mountdev: not dirty"); 12410 /* 12411 * We are only interested in bitmaps with outstanding 12412 * dependencies. 12413 */ 12414 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL || 12415 wk->wk_type != D_BMSAFEMAP || 12416 (bp->b_vflags & BV_BKGRDINPROG)) { 12417 BUF_UNLOCK(bp); 12418 continue; 12419 } 12420 BO_UNLOCK(bo); 12421 bremfree(bp); 12422 (void) bawrite(bp); 12423 goto restart; 12424 } 12425 drain_output(vp); 12426 BO_UNLOCK(bo); 12427} 12428 12429/* 12430 * Sync all cylinder groups that were dirty at the time this function is 12431 * called. Newly dirtied cgs will be inserted before the sentinel. This 12432 * is used to flush freedep activity that may be holding up writes to a 12433 * indirect block. 12434 */ 12435static int 12436sync_cgs(mp, waitfor) 12437 struct mount *mp; 12438 int waitfor; 12439{ 12440 struct bmsafemap *bmsafemap; 12441 struct bmsafemap *sentinel; 12442 struct ufsmount *ump; 12443 struct buf *bp; 12444 int error; 12445 12446 sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK); 12447 sentinel->sm_cg = -1; 12448 ump = VFSTOUFS(mp); 12449 error = 0; 12450 ACQUIRE_LOCK(ump); 12451 LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next); 12452 for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL; 12453 bmsafemap = LIST_NEXT(sentinel, sm_next)) { 12454 /* Skip sentinels and cgs with no work to release. */ 12455 if (bmsafemap->sm_cg == -1 || 12456 (LIST_EMPTY(&bmsafemap->sm_freehd) && 12457 LIST_EMPTY(&bmsafemap->sm_freewr))) { 12458 LIST_REMOVE(sentinel, sm_next); 12459 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 12460 continue; 12461 } 12462 /* 12463 * If we don't get the lock and we're waiting try again, if 12464 * not move on to the next buf and try to sync it. 12465 */ 12466 bp = getdirtybuf(bmsafemap->sm_buf, LOCK_PTR(ump), waitfor); 12467 if (bp == NULL && waitfor == MNT_WAIT) 12468 continue; 12469 LIST_REMOVE(sentinel, sm_next); 12470 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 12471 if (bp == NULL) 12472 continue; 12473 FREE_LOCK(ump); 12474 if (waitfor == MNT_NOWAIT) 12475 bawrite(bp); 12476 else 12477 error = bwrite(bp); 12478 ACQUIRE_LOCK(ump); 12479 if (error) 12480 break; 12481 } 12482 LIST_REMOVE(sentinel, sm_next); 12483 FREE_LOCK(ump); 12484 free(sentinel, M_BMSAFEMAP); 12485 return (error); 12486} 12487 12488/* 12489 * This routine is called when we are trying to synchronously flush a 12490 * file. This routine must eliminate any filesystem metadata dependencies 12491 * so that the syncing routine can succeed. 12492 */ 12493int 12494softdep_sync_metadata(struct vnode *vp) 12495{ 12496 struct inode *ip; 12497 int error; 12498 12499 ip = VTOI(vp); 12500 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0, 12501 ("softdep_sync_metadata called on non-softdep filesystem")); 12502 /* 12503 * Ensure that any direct block dependencies have been cleared, 12504 * truncations are started, and inode references are journaled. 12505 */ 12506 ACQUIRE_LOCK(ip->i_ump); 12507 /* 12508 * Write all journal records to prevent rollbacks on devvp. 12509 */ 12510 if (vp->v_type == VCHR) 12511 softdep_flushjournal(vp->v_mount); 12512 error = flush_inodedep_deps(vp, vp->v_mount, ip->i_number); 12513 /* 12514 * Ensure that all truncates are written so we won't find deps on 12515 * indirect blocks. 12516 */ 12517 process_truncates(vp); 12518 FREE_LOCK(ip->i_ump); 12519 12520 return (error); 12521} 12522 12523/* 12524 * This routine is called when we are attempting to sync a buf with 12525 * dependencies. If waitfor is MNT_NOWAIT it attempts to schedule any 12526 * other IO it can but returns EBUSY if the buffer is not yet able to 12527 * be written. Dependencies which will not cause rollbacks will always 12528 * return 0. 12529 */ 12530int 12531softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 12532{ 12533 struct indirdep *indirdep; 12534 struct pagedep *pagedep; 12535 struct allocindir *aip; 12536 struct newblk *newblk; 12537 struct ufsmount *ump; 12538 struct buf *nbp; 12539 struct worklist *wk; 12540 int i, error; 12541 12542 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 12543 ("softdep_sync_buf called on non-softdep filesystem")); 12544 /* 12545 * For VCHR we just don't want to force flush any dependencies that 12546 * will cause rollbacks. 12547 */ 12548 if (vp->v_type == VCHR) { 12549 if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0)) 12550 return (EBUSY); 12551 return (0); 12552 } 12553 ump = VTOI(vp)->i_ump; 12554 ACQUIRE_LOCK(ump); 12555 /* 12556 * As we hold the buffer locked, none of its dependencies 12557 * will disappear. 12558 */ 12559 error = 0; 12560top: 12561 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 12562 switch (wk->wk_type) { 12563 12564 case D_ALLOCDIRECT: 12565 case D_ALLOCINDIR: 12566 newblk = WK_NEWBLK(wk); 12567 if (newblk->nb_jnewblk != NULL) { 12568 if (waitfor == MNT_NOWAIT) { 12569 error = EBUSY; 12570 goto out_unlock; 12571 } 12572 jwait(&newblk->nb_jnewblk->jn_list, waitfor); 12573 goto top; 12574 } 12575 if (newblk->nb_state & DEPCOMPLETE || 12576 waitfor == MNT_NOWAIT) 12577 continue; 12578 nbp = newblk->nb_bmsafemap->sm_buf; 12579 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 12580 if (nbp == NULL) 12581 goto top; 12582 FREE_LOCK(ump); 12583 if ((error = bwrite(nbp)) != 0) 12584 goto out; 12585 ACQUIRE_LOCK(ump); 12586 continue; 12587 12588 case D_INDIRDEP: 12589 indirdep = WK_INDIRDEP(wk); 12590 if (waitfor == MNT_NOWAIT) { 12591 if (!TAILQ_EMPTY(&indirdep->ir_trunc) || 12592 !LIST_EMPTY(&indirdep->ir_deplisthd)) { 12593 error = EBUSY; 12594 goto out_unlock; 12595 } 12596 } 12597 if (!TAILQ_EMPTY(&indirdep->ir_trunc)) 12598 panic("softdep_sync_buf: truncation pending."); 12599 restart: 12600 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 12601 newblk = (struct newblk *)aip; 12602 if (newblk->nb_jnewblk != NULL) { 12603 jwait(&newblk->nb_jnewblk->jn_list, 12604 waitfor); 12605 goto restart; 12606 } 12607 if (newblk->nb_state & DEPCOMPLETE) 12608 continue; 12609 nbp = newblk->nb_bmsafemap->sm_buf; 12610 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 12611 if (nbp == NULL) 12612 goto restart; 12613 FREE_LOCK(ump); 12614 if ((error = bwrite(nbp)) != 0) 12615 goto out; 12616 ACQUIRE_LOCK(ump); 12617 goto restart; 12618 } 12619 continue; 12620 12621 case D_PAGEDEP: 12622 /* 12623 * Only flush directory entries in synchronous passes. 12624 */ 12625 if (waitfor != MNT_WAIT) { 12626 error = EBUSY; 12627 goto out_unlock; 12628 } 12629 /* 12630 * While syncing snapshots, we must allow recursive 12631 * lookups. 12632 */ 12633 BUF_AREC(bp); 12634 /* 12635 * We are trying to sync a directory that may 12636 * have dependencies on both its own metadata 12637 * and/or dependencies on the inodes of any 12638 * recently allocated files. We walk its diradd 12639 * lists pushing out the associated inode. 12640 */ 12641 pagedep = WK_PAGEDEP(wk); 12642 for (i = 0; i < DAHASHSZ; i++) { 12643 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0) 12644 continue; 12645 if ((error = flush_pagedep_deps(vp, wk->wk_mp, 12646 &pagedep->pd_diraddhd[i]))) { 12647 BUF_NOREC(bp); 12648 goto out_unlock; 12649 } 12650 } 12651 BUF_NOREC(bp); 12652 continue; 12653 12654 case D_FREEWORK: 12655 case D_FREEDEP: 12656 case D_JSEGDEP: 12657 case D_JNEWBLK: 12658 continue; 12659 12660 default: 12661 panic("softdep_sync_buf: Unknown type %s", 12662 TYPENAME(wk->wk_type)); 12663 /* NOTREACHED */ 12664 } 12665 } 12666out_unlock: 12667 FREE_LOCK(ump); 12668out: 12669 return (error); 12670} 12671 12672/* 12673 * Flush the dependencies associated with an inodedep. 12674 * Called with splbio blocked. 12675 */ 12676static int 12677flush_inodedep_deps(vp, mp, ino) 12678 struct vnode *vp; 12679 struct mount *mp; 12680 ino_t ino; 12681{ 12682 struct inodedep *inodedep; 12683 struct inoref *inoref; 12684 struct ufsmount *ump; 12685 int error, waitfor; 12686 12687 /* 12688 * This work is done in two passes. The first pass grabs most 12689 * of the buffers and begins asynchronously writing them. The 12690 * only way to wait for these asynchronous writes is to sleep 12691 * on the filesystem vnode which may stay busy for a long time 12692 * if the filesystem is active. So, instead, we make a second 12693 * pass over the dependencies blocking on each write. In the 12694 * usual case we will be blocking against a write that we 12695 * initiated, so when it is done the dependency will have been 12696 * resolved. Thus the second pass is expected to end quickly. 12697 * We give a brief window at the top of the loop to allow 12698 * any pending I/O to complete. 12699 */ 12700 ump = VFSTOUFS(mp); 12701 LOCK_OWNED(ump); 12702 for (error = 0, waitfor = MNT_NOWAIT; ; ) { 12703 if (error) 12704 return (error); 12705 FREE_LOCK(ump); 12706 ACQUIRE_LOCK(ump); 12707restart: 12708 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 12709 return (0); 12710 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12711 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12712 == DEPCOMPLETE) { 12713 jwait(&inoref->if_list, MNT_WAIT); 12714 goto restart; 12715 } 12716 } 12717 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) || 12718 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) || 12719 flush_deplist(&inodedep->id_extupdt, waitfor, &error) || 12720 flush_deplist(&inodedep->id_newextupdt, waitfor, &error)) 12721 continue; 12722 /* 12723 * If pass2, we are done, otherwise do pass 2. 12724 */ 12725 if (waitfor == MNT_WAIT) 12726 break; 12727 waitfor = MNT_WAIT; 12728 } 12729 /* 12730 * Try freeing inodedep in case all dependencies have been removed. 12731 */ 12732 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0) 12733 (void) free_inodedep(inodedep); 12734 return (0); 12735} 12736 12737/* 12738 * Flush an inode dependency list. 12739 * Called with splbio blocked. 12740 */ 12741static int 12742flush_deplist(listhead, waitfor, errorp) 12743 struct allocdirectlst *listhead; 12744 int waitfor; 12745 int *errorp; 12746{ 12747 struct allocdirect *adp; 12748 struct newblk *newblk; 12749 struct ufsmount *ump; 12750 struct buf *bp; 12751 12752 if ((adp = TAILQ_FIRST(listhead)) == NULL) 12753 return (0); 12754 ump = VFSTOUFS(adp->ad_list.wk_mp); 12755 LOCK_OWNED(ump); 12756 TAILQ_FOREACH(adp, listhead, ad_next) { 12757 newblk = (struct newblk *)adp; 12758 if (newblk->nb_jnewblk != NULL) { 12759 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 12760 return (1); 12761 } 12762 if (newblk->nb_state & DEPCOMPLETE) 12763 continue; 12764 bp = newblk->nb_bmsafemap->sm_buf; 12765 bp = getdirtybuf(bp, LOCK_PTR(ump), waitfor); 12766 if (bp == NULL) { 12767 if (waitfor == MNT_NOWAIT) 12768 continue; 12769 return (1); 12770 } 12771 FREE_LOCK(ump); 12772 if (waitfor == MNT_NOWAIT) 12773 bawrite(bp); 12774 else 12775 *errorp = bwrite(bp); 12776 ACQUIRE_LOCK(ump); 12777 return (1); 12778 } 12779 return (0); 12780} 12781 12782/* 12783 * Flush dependencies associated with an allocdirect block. 12784 */ 12785static int 12786flush_newblk_dep(vp, mp, lbn) 12787 struct vnode *vp; 12788 struct mount *mp; 12789 ufs_lbn_t lbn; 12790{ 12791 struct newblk *newblk; 12792 struct ufsmount *ump; 12793 struct bufobj *bo; 12794 struct inode *ip; 12795 struct buf *bp; 12796 ufs2_daddr_t blkno; 12797 int error; 12798 12799 error = 0; 12800 bo = &vp->v_bufobj; 12801 ip = VTOI(vp); 12802 blkno = DIP(ip, i_db[lbn]); 12803 if (blkno == 0) 12804 panic("flush_newblk_dep: Missing block"); 12805 ump = VFSTOUFS(mp); 12806 ACQUIRE_LOCK(ump); 12807 /* 12808 * Loop until all dependencies related to this block are satisfied. 12809 * We must be careful to restart after each sleep in case a write 12810 * completes some part of this process for us. 12811 */ 12812 for (;;) { 12813 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) { 12814 FREE_LOCK(ump); 12815 break; 12816 } 12817 if (newblk->nb_list.wk_type != D_ALLOCDIRECT) 12818 panic("flush_newblk_deps: Bad newblk %p", newblk); 12819 /* 12820 * Flush the journal. 12821 */ 12822 if (newblk->nb_jnewblk != NULL) { 12823 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 12824 continue; 12825 } 12826 /* 12827 * Write the bitmap dependency. 12828 */ 12829 if ((newblk->nb_state & DEPCOMPLETE) == 0) { 12830 bp = newblk->nb_bmsafemap->sm_buf; 12831 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 12832 if (bp == NULL) 12833 continue; 12834 FREE_LOCK(ump); 12835 error = bwrite(bp); 12836 if (error) 12837 break; 12838 ACQUIRE_LOCK(ump); 12839 continue; 12840 } 12841 /* 12842 * Write the buffer. 12843 */ 12844 FREE_LOCK(ump); 12845 BO_LOCK(bo); 12846 bp = gbincore(bo, lbn); 12847 if (bp != NULL) { 12848 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 12849 LK_INTERLOCK, BO_LOCKPTR(bo)); 12850 if (error == ENOLCK) { 12851 ACQUIRE_LOCK(ump); 12852 continue; /* Slept, retry */ 12853 } 12854 if (error != 0) 12855 break; /* Failed */ 12856 if (bp->b_flags & B_DELWRI) { 12857 bremfree(bp); 12858 error = bwrite(bp); 12859 if (error) 12860 break; 12861 } else 12862 BUF_UNLOCK(bp); 12863 } else 12864 BO_UNLOCK(bo); 12865 /* 12866 * We have to wait for the direct pointers to 12867 * point at the newdirblk before the dependency 12868 * will go away. 12869 */ 12870 error = ffs_update(vp, 1); 12871 if (error) 12872 break; 12873 ACQUIRE_LOCK(ump); 12874 } 12875 return (error); 12876} 12877 12878/* 12879 * Eliminate a pagedep dependency by flushing out all its diradd dependencies. 12880 * Called with splbio blocked. 12881 */ 12882static int 12883flush_pagedep_deps(pvp, mp, diraddhdp) 12884 struct vnode *pvp; 12885 struct mount *mp; 12886 struct diraddhd *diraddhdp; 12887{ 12888 struct inodedep *inodedep; 12889 struct inoref *inoref; 12890 struct ufsmount *ump; 12891 struct diradd *dap; 12892 struct vnode *vp; 12893 int error = 0; 12894 struct buf *bp; 12895 ino_t inum; 12896 struct diraddhd unfinished; 12897 12898 LIST_INIT(&unfinished); 12899 ump = VFSTOUFS(mp); 12900 LOCK_OWNED(ump); 12901restart: 12902 while ((dap = LIST_FIRST(diraddhdp)) != NULL) { 12903 /* 12904 * Flush ourselves if this directory entry 12905 * has a MKDIR_PARENT dependency. 12906 */ 12907 if (dap->da_state & MKDIR_PARENT) { 12908 FREE_LOCK(ump); 12909 if ((error = ffs_update(pvp, 1)) != 0) 12910 break; 12911 ACQUIRE_LOCK(ump); 12912 /* 12913 * If that cleared dependencies, go on to next. 12914 */ 12915 if (dap != LIST_FIRST(diraddhdp)) 12916 continue; 12917 /* 12918 * All MKDIR_PARENT dependencies and all the 12919 * NEWBLOCK pagedeps that are contained in direct 12920 * blocks were resolved by doing above ffs_update. 12921 * Pagedeps contained in indirect blocks may 12922 * require a complete sync'ing of the directory. 12923 * We are in the midst of doing a complete sync, 12924 * so if they are not resolved in this pass we 12925 * defer them for now as they will be sync'ed by 12926 * our caller shortly. 12927 */ 12928 LIST_REMOVE(dap, da_pdlist); 12929 LIST_INSERT_HEAD(&unfinished, dap, da_pdlist); 12930 continue; 12931 } 12932 /* 12933 * A newly allocated directory must have its "." and 12934 * ".." entries written out before its name can be 12935 * committed in its parent. 12936 */ 12937 inum = dap->da_newinum; 12938 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 12939 panic("flush_pagedep_deps: lost inode1"); 12940 /* 12941 * Wait for any pending journal adds to complete so we don't 12942 * cause rollbacks while syncing. 12943 */ 12944 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12945 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12946 == DEPCOMPLETE) { 12947 jwait(&inoref->if_list, MNT_WAIT); 12948 goto restart; 12949 } 12950 } 12951 if (dap->da_state & MKDIR_BODY) { 12952 FREE_LOCK(ump); 12953 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp, 12954 FFSV_FORCEINSMQ))) 12955 break; 12956 error = flush_newblk_dep(vp, mp, 0); 12957 /* 12958 * If we still have the dependency we might need to 12959 * update the vnode to sync the new link count to 12960 * disk. 12961 */ 12962 if (error == 0 && dap == LIST_FIRST(diraddhdp)) 12963 error = ffs_update(vp, 1); 12964 vput(vp); 12965 if (error != 0) 12966 break; 12967 ACQUIRE_LOCK(ump); 12968 /* 12969 * If that cleared dependencies, go on to next. 12970 */ 12971 if (dap != LIST_FIRST(diraddhdp)) 12972 continue; 12973 if (dap->da_state & MKDIR_BODY) { 12974 inodedep_lookup(UFSTOVFS(ump), inum, 0, 12975 &inodedep); 12976 panic("flush_pagedep_deps: MKDIR_BODY " 12977 "inodedep %p dap %p vp %p", 12978 inodedep, dap, vp); 12979 } 12980 } 12981 /* 12982 * Flush the inode on which the directory entry depends. 12983 * Having accounted for MKDIR_PARENT and MKDIR_BODY above, 12984 * the only remaining dependency is that the updated inode 12985 * count must get pushed to disk. The inode has already 12986 * been pushed into its inode buffer (via VOP_UPDATE) at 12987 * the time of the reference count change. So we need only 12988 * locate that buffer, ensure that there will be no rollback 12989 * caused by a bitmap dependency, then write the inode buffer. 12990 */ 12991retry: 12992 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 12993 panic("flush_pagedep_deps: lost inode"); 12994 /* 12995 * If the inode still has bitmap dependencies, 12996 * push them to disk. 12997 */ 12998 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) { 12999 bp = inodedep->id_bmsafemap->sm_buf; 13000 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 13001 if (bp == NULL) 13002 goto retry; 13003 FREE_LOCK(ump); 13004 if ((error = bwrite(bp)) != 0) 13005 break; 13006 ACQUIRE_LOCK(ump); 13007 if (dap != LIST_FIRST(diraddhdp)) 13008 continue; 13009 } 13010 /* 13011 * If the inode is still sitting in a buffer waiting 13012 * to be written or waiting for the link count to be 13013 * adjusted update it here to flush it to disk. 13014 */ 13015 if (dap == LIST_FIRST(diraddhdp)) { 13016 FREE_LOCK(ump); 13017 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp, 13018 FFSV_FORCEINSMQ))) 13019 break; 13020 error = ffs_update(vp, 1); 13021 vput(vp); 13022 if (error) 13023 break; 13024 ACQUIRE_LOCK(ump); 13025 } 13026 /* 13027 * If we have failed to get rid of all the dependencies 13028 * then something is seriously wrong. 13029 */ 13030 if (dap == LIST_FIRST(diraddhdp)) { 13031 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep); 13032 panic("flush_pagedep_deps: failed to flush " 13033 "inodedep %p ino %ju dap %p", 13034 inodedep, (uintmax_t)inum, dap); 13035 } 13036 } 13037 if (error) 13038 ACQUIRE_LOCK(ump); 13039 while ((dap = LIST_FIRST(&unfinished)) != NULL) { 13040 LIST_REMOVE(dap, da_pdlist); 13041 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist); 13042 } 13043 return (error); 13044} 13045 13046/* 13047 * A large burst of file addition or deletion activity can drive the 13048 * memory load excessively high. First attempt to slow things down 13049 * using the techniques below. If that fails, this routine requests 13050 * the offending operations to fall back to running synchronously 13051 * until the memory load returns to a reasonable level. 13052 */ 13053int 13054softdep_slowdown(vp) 13055 struct vnode *vp; 13056{ 13057 struct ufsmount *ump; 13058 int jlow; 13059 int max_softdeps_hard; 13060 13061 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 13062 ("softdep_slowdown called on non-softdep filesystem")); 13063 ump = VFSTOUFS(vp->v_mount); 13064 ACQUIRE_LOCK(ump); 13065 jlow = 0; 13066 /* 13067 * Check for journal space if needed. 13068 */ 13069 if (DOINGSUJ(vp)) { 13070 if (journal_space(ump, 0) == 0) 13071 jlow = 1; 13072 } 13073 /* 13074 * If the system is under its limits and our filesystem is 13075 * not responsible for more than our share of the usage and 13076 * we are not low on journal space, then no need to slow down. 13077 */ 13078 max_softdeps_hard = max_softdeps * 11 / 10; 13079 if (dep_current[D_DIRREM] < max_softdeps_hard / 2 && 13080 dep_current[D_INODEDEP] < max_softdeps_hard && 13081 dep_current[D_INDIRDEP] < max_softdeps_hard / 1000 && 13082 dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0 && 13083 ump->softdep_curdeps[D_DIRREM] < 13084 (max_softdeps_hard / 2) / stat_flush_threads && 13085 ump->softdep_curdeps[D_INODEDEP] < 13086 max_softdeps_hard / stat_flush_threads && 13087 ump->softdep_curdeps[D_INDIRDEP] < 13088 (max_softdeps_hard / 1000) / stat_flush_threads && 13089 ump->softdep_curdeps[D_FREEBLKS] < 13090 max_softdeps_hard / stat_flush_threads) { 13091 FREE_LOCK(ump); 13092 return (0); 13093 } 13094 /* 13095 * If the journal is low or our filesystem is over its limit 13096 * then speedup the cleanup. 13097 */ 13098 if (ump->softdep_curdeps[D_INDIRDEP] < 13099 (max_softdeps_hard / 1000) / stat_flush_threads || jlow) 13100 softdep_speedup(ump); 13101 stat_sync_limit_hit += 1; 13102 FREE_LOCK(ump); 13103 /* 13104 * We only slow down the rate at which new dependencies are 13105 * generated if we are not using journaling. With journaling, 13106 * the cleanup should always be sufficient to keep things 13107 * under control. 13108 */ 13109 if (DOINGSUJ(vp)) 13110 return (0); 13111 return (1); 13112} 13113 13114/* 13115 * Called by the allocation routines when they are about to fail 13116 * in the hope that we can free up the requested resource (inodes 13117 * or disk space). 13118 * 13119 * First check to see if the work list has anything on it. If it has, 13120 * clean up entries until we successfully free the requested resource. 13121 * Because this process holds inodes locked, we cannot handle any remove 13122 * requests that might block on a locked inode as that could lead to 13123 * deadlock. If the worklist yields none of the requested resource, 13124 * start syncing out vnodes to free up the needed space. 13125 */ 13126int 13127softdep_request_cleanup(fs, vp, cred, resource) 13128 struct fs *fs; 13129 struct vnode *vp; 13130 struct ucred *cred; 13131 int resource; 13132{ 13133 struct ufsmount *ump; 13134 struct mount *mp; 13135 struct vnode *lvp, *mvp; 13136 long starttime; 13137 ufs2_daddr_t needed; 13138 int error; 13139 13140 /* 13141 * If we are being called because of a process doing a 13142 * copy-on-write, then it is not safe to process any 13143 * worklist items as we will recurse into the copyonwrite 13144 * routine. This will result in an incoherent snapshot. 13145 * If the vnode that we hold is a snapshot, we must avoid 13146 * handling other resources that could cause deadlock. 13147 */ 13148 if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp))) 13149 return (0); 13150 13151 if (resource == FLUSH_BLOCKS_WAIT) 13152 stat_cleanup_blkrequests += 1; 13153 else 13154 stat_cleanup_inorequests += 1; 13155 13156 mp = vp->v_mount; 13157 ump = VFSTOUFS(mp); 13158 mtx_assert(UFS_MTX(ump), MA_OWNED); 13159 UFS_UNLOCK(ump); 13160 error = ffs_update(vp, 1); 13161 if (error != 0 || MOUNTEDSOFTDEP(mp) == 0) { 13162 UFS_LOCK(ump); 13163 return (0); 13164 } 13165 /* 13166 * If we are in need of resources, start by cleaning up 13167 * any block removals associated with our inode. 13168 */ 13169 ACQUIRE_LOCK(ump); 13170 process_removes(vp); 13171 process_truncates(vp); 13172 FREE_LOCK(ump); 13173 /* 13174 * Now clean up at least as many resources as we will need. 13175 * 13176 * When requested to clean up inodes, the number that are needed 13177 * is set by the number of simultaneous writers (mnt_writeopcount) 13178 * plus a bit of slop (2) in case some more writers show up while 13179 * we are cleaning. 13180 * 13181 * When requested to free up space, the amount of space that 13182 * we need is enough blocks to allocate a full-sized segment 13183 * (fs_contigsumsize). The number of such segments that will 13184 * be needed is set by the number of simultaneous writers 13185 * (mnt_writeopcount) plus a bit of slop (2) in case some more 13186 * writers show up while we are cleaning. 13187 * 13188 * Additionally, if we are unpriviledged and allocating space, 13189 * we need to ensure that we clean up enough blocks to get the 13190 * needed number of blocks over the threshhold of the minimum 13191 * number of blocks required to be kept free by the filesystem 13192 * (fs_minfree). 13193 */ 13194 if (resource == FLUSH_INODES_WAIT) { 13195 needed = vp->v_mount->mnt_writeopcount + 2; 13196 } else if (resource == FLUSH_BLOCKS_WAIT) { 13197 needed = (vp->v_mount->mnt_writeopcount + 2) * 13198 fs->fs_contigsumsize; 13199 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE, 0)) 13200 needed += fragstoblks(fs, 13201 roundup((fs->fs_dsize * fs->fs_minfree / 100) - 13202 fs->fs_cstotal.cs_nffree, fs->fs_frag)); 13203 } else { 13204 UFS_LOCK(ump); 13205 printf("softdep_request_cleanup: Unknown resource type %d\n", 13206 resource); 13207 return (0); 13208 } 13209 starttime = time_second; 13210retry: 13211 if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 && 13212 fs->fs_cstotal.cs_nbfree <= needed) || 13213 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13214 fs->fs_cstotal.cs_nifree <= needed)) { 13215 ACQUIRE_LOCK(ump); 13216 if (ump->softdep_on_worklist > 0 && 13217 process_worklist_item(UFSTOVFS(ump), 13218 ump->softdep_on_worklist, LK_NOWAIT) != 0) 13219 stat_worklist_push += 1; 13220 FREE_LOCK(ump); 13221 } 13222 /* 13223 * If we still need resources and there are no more worklist 13224 * entries to process to obtain them, we have to start flushing 13225 * the dirty vnodes to force the release of additional requests 13226 * to the worklist that we can then process to reap addition 13227 * resources. We walk the vnodes associated with the mount point 13228 * until we get the needed worklist requests that we can reap. 13229 */ 13230 if ((resource == FLUSH_BLOCKS_WAIT && 13231 fs->fs_cstotal.cs_nbfree <= needed) || 13232 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13233 fs->fs_cstotal.cs_nifree <= needed)) { 13234 MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) { 13235 if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) { 13236 VI_UNLOCK(lvp); 13237 continue; 13238 } 13239 if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT, 13240 curthread)) 13241 continue; 13242 if (lvp->v_vflag & VV_NOSYNC) { /* unlinked */ 13243 vput(lvp); 13244 continue; 13245 } 13246 (void) ffs_syncvnode(lvp, MNT_NOWAIT, 0); 13247 vput(lvp); 13248 } 13249 lvp = ump->um_devvp; 13250 if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) { 13251 VOP_FSYNC(lvp, MNT_NOWAIT, curthread); 13252 VOP_UNLOCK(lvp, 0); 13253 } 13254 if (ump->softdep_on_worklist > 0) { 13255 stat_cleanup_retries += 1; 13256 goto retry; 13257 } 13258 stat_cleanup_failures += 1; 13259 } 13260 if (time_second - starttime > stat_cleanup_high_delay) 13261 stat_cleanup_high_delay = time_second - starttime; 13262 UFS_LOCK(ump); 13263 return (1); 13264} 13265 13266/* 13267 * If memory utilization has gotten too high, deliberately slow things 13268 * down and speed up the I/O processing. 13269 */ 13270static int 13271request_cleanup(mp, resource) 13272 struct mount *mp; 13273 int resource; 13274{ 13275 struct thread *td = curthread; 13276 struct ufsmount *ump; 13277 13278 ump = VFSTOUFS(mp); 13279 LOCK_OWNED(ump); 13280 /* 13281 * We never hold up the filesystem syncer or buf daemon. 13282 */ 13283 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF)) 13284 return (0); 13285 /* 13286 * First check to see if the work list has gotten backlogged. 13287 * If it has, co-opt this process to help clean up two entries. 13288 * Because this process may hold inodes locked, we cannot 13289 * handle any remove requests that might block on a locked 13290 * inode as that could lead to deadlock. We set TDP_SOFTDEP 13291 * to avoid recursively processing the worklist. 13292 */ 13293 if (ump->softdep_on_worklist > max_softdeps / 10) { 13294 td->td_pflags |= TDP_SOFTDEP; 13295 process_worklist_item(mp, 2, LK_NOWAIT); 13296 td->td_pflags &= ~TDP_SOFTDEP; 13297 stat_worklist_push += 2; 13298 return(1); 13299 } 13300 /* 13301 * Next, we attempt to speed up the syncer process. If that 13302 * is successful, then we allow the process to continue. 13303 */ 13304 if (softdep_speedup(ump) && 13305 resource != FLUSH_BLOCKS_WAIT && 13306 resource != FLUSH_INODES_WAIT) 13307 return(0); 13308 /* 13309 * If we are resource constrained on inode dependencies, try 13310 * flushing some dirty inodes. Otherwise, we are constrained 13311 * by file deletions, so try accelerating flushes of directories 13312 * with removal dependencies. We would like to do the cleanup 13313 * here, but we probably hold an inode locked at this point and 13314 * that might deadlock against one that we try to clean. So, 13315 * the best that we can do is request the syncer daemon to do 13316 * the cleanup for us. 13317 */ 13318 switch (resource) { 13319 13320 case FLUSH_INODES: 13321 case FLUSH_INODES_WAIT: 13322 ACQUIRE_GBLLOCK(&lk); 13323 stat_ino_limit_push += 1; 13324 req_clear_inodedeps += 1; 13325 FREE_GBLLOCK(&lk); 13326 stat_countp = &stat_ino_limit_hit; 13327 break; 13328 13329 case FLUSH_BLOCKS: 13330 case FLUSH_BLOCKS_WAIT: 13331 ACQUIRE_GBLLOCK(&lk); 13332 stat_blk_limit_push += 1; 13333 req_clear_remove += 1; 13334 FREE_GBLLOCK(&lk); 13335 stat_countp = &stat_blk_limit_hit; 13336 break; 13337 13338 default: 13339 panic("request_cleanup: unknown type"); 13340 } 13341 /* 13342 * Hopefully the syncer daemon will catch up and awaken us. 13343 * We wait at most tickdelay before proceeding in any case. 13344 */ 13345 ACQUIRE_GBLLOCK(&lk); 13346 FREE_LOCK(ump); 13347 proc_waiting += 1; 13348 if (callout_pending(&softdep_callout) == FALSE) 13349 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2, 13350 pause_timer, 0); 13351 13352 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0); 13353 proc_waiting -= 1; 13354 FREE_GBLLOCK(&lk); 13355 ACQUIRE_LOCK(ump); 13356 return (1); 13357} 13358 13359/* 13360 * Awaken processes pausing in request_cleanup and clear proc_waiting 13361 * to indicate that there is no longer a timer running. Pause_timer 13362 * will be called with the global softdep mutex (&lk) locked. 13363 */ 13364static void 13365pause_timer(arg) 13366 void *arg; 13367{ 13368 13369 GBLLOCK_OWNED(&lk); 13370 /* 13371 * The callout_ API has acquired mtx and will hold it around this 13372 * function call. 13373 */ 13374 *stat_countp += proc_waiting; 13375 wakeup(&proc_waiting); 13376} 13377 13378/* 13379 * If requested, try removing inode or removal dependencies. 13380 */ 13381static void 13382check_clear_deps(mp) 13383 struct mount *mp; 13384{ 13385 13386 /* 13387 * If we are suspended, it may be because of our using 13388 * too many inodedeps, so help clear them out. 13389 */ 13390 if (MOUNTEDSUJ(mp) && VFSTOUFS(mp)->softdep_jblocks->jb_suspended) 13391 clear_inodedeps(mp); 13392 /* 13393 * General requests for cleanup of backed up dependencies 13394 */ 13395 ACQUIRE_GBLLOCK(&lk); 13396 if (req_clear_inodedeps) { 13397 req_clear_inodedeps -= 1; 13398 FREE_GBLLOCK(&lk); 13399 clear_inodedeps(mp); 13400 ACQUIRE_GBLLOCK(&lk); 13401 wakeup(&proc_waiting); 13402 } 13403 if (req_clear_remove) { 13404 req_clear_remove -= 1; 13405 FREE_GBLLOCK(&lk); 13406 clear_remove(mp); 13407 ACQUIRE_GBLLOCK(&lk); 13408 wakeup(&proc_waiting); 13409 } 13410 FREE_GBLLOCK(&lk); 13411} 13412 13413/* 13414 * Flush out a directory with at least one removal dependency in an effort to 13415 * reduce the number of dirrem, freefile, and freeblks dependency structures. 13416 */ 13417static void 13418clear_remove(mp) 13419 struct mount *mp; 13420{ 13421 struct pagedep_hashhead *pagedephd; 13422 struct pagedep *pagedep; 13423 struct ufsmount *ump; 13424 struct vnode *vp; 13425 struct bufobj *bo; 13426 int error, cnt; 13427 ino_t ino; 13428 13429 ump = VFSTOUFS(mp); 13430 LOCK_OWNED(ump); 13431 13432 for (cnt = 0; cnt <= ump->pagedep_hash_size; cnt++) { 13433 pagedephd = &ump->pagedep_hashtbl[ump->pagedep_nextclean++]; 13434 if (ump->pagedep_nextclean > ump->pagedep_hash_size) 13435 ump->pagedep_nextclean = 0; 13436 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 13437 if (LIST_EMPTY(&pagedep->pd_dirremhd)) 13438 continue; 13439 ino = pagedep->pd_ino; 13440 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 13441 continue; 13442 FREE_LOCK(ump); 13443 13444 /* 13445 * Let unmount clear deps 13446 */ 13447 error = vfs_busy(mp, MBF_NOWAIT); 13448 if (error != 0) 13449 goto finish_write; 13450 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 13451 FFSV_FORCEINSMQ); 13452 vfs_unbusy(mp); 13453 if (error != 0) { 13454 softdep_error("clear_remove: vget", error); 13455 goto finish_write; 13456 } 13457 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 13458 softdep_error("clear_remove: fsync", error); 13459 bo = &vp->v_bufobj; 13460 BO_LOCK(bo); 13461 drain_output(vp); 13462 BO_UNLOCK(bo); 13463 vput(vp); 13464 finish_write: 13465 vn_finished_write(mp); 13466 ACQUIRE_LOCK(ump); 13467 return; 13468 } 13469 } 13470} 13471 13472/* 13473 * Clear out a block of dirty inodes in an effort to reduce 13474 * the number of inodedep dependency structures. 13475 */ 13476static void 13477clear_inodedeps(mp) 13478 struct mount *mp; 13479{ 13480 struct inodedep_hashhead *inodedephd; 13481 struct inodedep *inodedep; 13482 struct ufsmount *ump; 13483 struct vnode *vp; 13484 struct fs *fs; 13485 int error, cnt; 13486 ino_t firstino, lastino, ino; 13487 13488 ump = VFSTOUFS(mp); 13489 fs = ump->um_fs; 13490 LOCK_OWNED(ump); 13491 /* 13492 * Pick a random inode dependency to be cleared. 13493 * We will then gather up all the inodes in its block 13494 * that have dependencies and flush them out. 13495 */ 13496 for (cnt = 0; cnt <= ump->inodedep_hash_size; cnt++) { 13497 inodedephd = &ump->inodedep_hashtbl[ump->inodedep_nextclean++]; 13498 if (ump->inodedep_nextclean > ump->inodedep_hash_size) 13499 ump->inodedep_nextclean = 0; 13500 if ((inodedep = LIST_FIRST(inodedephd)) != NULL) 13501 break; 13502 } 13503 if (inodedep == NULL) 13504 return; 13505 /* 13506 * Find the last inode in the block with dependencies. 13507 */ 13508 firstino = inodedep->id_ino & ~(INOPB(fs) - 1); 13509 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--) 13510 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0) 13511 break; 13512 /* 13513 * Asynchronously push all but the last inode with dependencies. 13514 * Synchronously push the last inode with dependencies to ensure 13515 * that the inode block gets written to free up the inodedeps. 13516 */ 13517 for (ino = firstino; ino <= lastino; ino++) { 13518 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 13519 continue; 13520 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 13521 continue; 13522 FREE_LOCK(ump); 13523 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */ 13524 if (error != 0) { 13525 vn_finished_write(mp); 13526 ACQUIRE_LOCK(ump); 13527 return; 13528 } 13529 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 13530 FFSV_FORCEINSMQ)) != 0) { 13531 softdep_error("clear_inodedeps: vget", error); 13532 vfs_unbusy(mp); 13533 vn_finished_write(mp); 13534 ACQUIRE_LOCK(ump); 13535 return; 13536 } 13537 vfs_unbusy(mp); 13538 if (ino == lastino) { 13539 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0))) 13540 softdep_error("clear_inodedeps: fsync1", error); 13541 } else { 13542 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 13543 softdep_error("clear_inodedeps: fsync2", error); 13544 BO_LOCK(&vp->v_bufobj); 13545 drain_output(vp); 13546 BO_UNLOCK(&vp->v_bufobj); 13547 } 13548 vput(vp); 13549 vn_finished_write(mp); 13550 ACQUIRE_LOCK(ump); 13551 } 13552} 13553 13554void 13555softdep_buf_append(bp, wkhd) 13556 struct buf *bp; 13557 struct workhead *wkhd; 13558{ 13559 struct worklist *wk; 13560 struct ufsmount *ump; 13561 13562 if ((wk = LIST_FIRST(wkhd)) == NULL) 13563 return; 13564 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 13565 ("softdep_buf_append called on non-softdep filesystem")); 13566 ump = VFSTOUFS(wk->wk_mp); 13567 ACQUIRE_LOCK(ump); 13568 while ((wk = LIST_FIRST(wkhd)) != NULL) { 13569 WORKLIST_REMOVE(wk); 13570 WORKLIST_INSERT(&bp->b_dep, wk); 13571 } 13572 FREE_LOCK(ump); 13573 13574} 13575 13576void 13577softdep_inode_append(ip, cred, wkhd) 13578 struct inode *ip; 13579 struct ucred *cred; 13580 struct workhead *wkhd; 13581{ 13582 struct buf *bp; 13583 struct fs *fs; 13584 int error; 13585 13586 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0, 13587 ("softdep_inode_append called on non-softdep filesystem")); 13588 fs = ip->i_fs; 13589 error = bread(ip->i_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 13590 (int)fs->fs_bsize, cred, &bp); 13591 if (error) { 13592 bqrelse(bp); 13593 softdep_freework(wkhd); 13594 return; 13595 } 13596 softdep_buf_append(bp, wkhd); 13597 bqrelse(bp); 13598} 13599 13600void 13601softdep_freework(wkhd) 13602 struct workhead *wkhd; 13603{ 13604 struct worklist *wk; 13605 struct ufsmount *ump; 13606 13607 if ((wk = LIST_FIRST(wkhd)) == NULL) 13608 return; 13609 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 13610 ("softdep_freework called on non-softdep filesystem")); 13611 ump = VFSTOUFS(wk->wk_mp); 13612 ACQUIRE_LOCK(ump); 13613 handle_jwork(wkhd); 13614 FREE_LOCK(ump); 13615} 13616 13617/* 13618 * Function to determine if the buffer has outstanding dependencies 13619 * that will cause a roll-back if the buffer is written. If wantcount 13620 * is set, return number of dependencies, otherwise just yes or no. 13621 */ 13622static int 13623softdep_count_dependencies(bp, wantcount) 13624 struct buf *bp; 13625 int wantcount; 13626{ 13627 struct worklist *wk; 13628 struct ufsmount *ump; 13629 struct bmsafemap *bmsafemap; 13630 struct freework *freework; 13631 struct inodedep *inodedep; 13632 struct indirdep *indirdep; 13633 struct freeblks *freeblks; 13634 struct allocindir *aip; 13635 struct pagedep *pagedep; 13636 struct dirrem *dirrem; 13637 struct newblk *newblk; 13638 struct mkdir *mkdir; 13639 struct diradd *dap; 13640 int i, retval; 13641 13642 retval = 0; 13643 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL) 13644 return (0); 13645 ump = VFSTOUFS(wk->wk_mp); 13646 ACQUIRE_LOCK(ump); 13647 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 13648 switch (wk->wk_type) { 13649 13650 case D_INODEDEP: 13651 inodedep = WK_INODEDEP(wk); 13652 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 13653 /* bitmap allocation dependency */ 13654 retval += 1; 13655 if (!wantcount) 13656 goto out; 13657 } 13658 if (TAILQ_FIRST(&inodedep->id_inoupdt)) { 13659 /* direct block pointer dependency */ 13660 retval += 1; 13661 if (!wantcount) 13662 goto out; 13663 } 13664 if (TAILQ_FIRST(&inodedep->id_extupdt)) { 13665 /* direct block pointer dependency */ 13666 retval += 1; 13667 if (!wantcount) 13668 goto out; 13669 } 13670 if (TAILQ_FIRST(&inodedep->id_inoreflst)) { 13671 /* Add reference dependency. */ 13672 retval += 1; 13673 if (!wantcount) 13674 goto out; 13675 } 13676 continue; 13677 13678 case D_INDIRDEP: 13679 indirdep = WK_INDIRDEP(wk); 13680 13681 TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) { 13682 /* indirect truncation dependency */ 13683 retval += 1; 13684 if (!wantcount) 13685 goto out; 13686 } 13687 13688 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 13689 /* indirect block pointer dependency */ 13690 retval += 1; 13691 if (!wantcount) 13692 goto out; 13693 } 13694 continue; 13695 13696 case D_PAGEDEP: 13697 pagedep = WK_PAGEDEP(wk); 13698 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) { 13699 if (LIST_FIRST(&dirrem->dm_jremrefhd)) { 13700 /* Journal remove ref dependency. */ 13701 retval += 1; 13702 if (!wantcount) 13703 goto out; 13704 } 13705 } 13706 for (i = 0; i < DAHASHSZ; i++) { 13707 13708 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 13709 /* directory entry dependency */ 13710 retval += 1; 13711 if (!wantcount) 13712 goto out; 13713 } 13714 } 13715 continue; 13716 13717 case D_BMSAFEMAP: 13718 bmsafemap = WK_BMSAFEMAP(wk); 13719 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) { 13720 /* Add reference dependency. */ 13721 retval += 1; 13722 if (!wantcount) 13723 goto out; 13724 } 13725 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) { 13726 /* Allocate block dependency. */ 13727 retval += 1; 13728 if (!wantcount) 13729 goto out; 13730 } 13731 continue; 13732 13733 case D_FREEBLKS: 13734 freeblks = WK_FREEBLKS(wk); 13735 if (LIST_FIRST(&freeblks->fb_jblkdephd)) { 13736 /* Freeblk journal dependency. */ 13737 retval += 1; 13738 if (!wantcount) 13739 goto out; 13740 } 13741 continue; 13742 13743 case D_ALLOCDIRECT: 13744 case D_ALLOCINDIR: 13745 newblk = WK_NEWBLK(wk); 13746 if (newblk->nb_jnewblk) { 13747 /* Journal allocate dependency. */ 13748 retval += 1; 13749 if (!wantcount) 13750 goto out; 13751 } 13752 continue; 13753 13754 case D_MKDIR: 13755 mkdir = WK_MKDIR(wk); 13756 if (mkdir->md_jaddref) { 13757 /* Journal reference dependency. */ 13758 retval += 1; 13759 if (!wantcount) 13760 goto out; 13761 } 13762 continue; 13763 13764 case D_FREEWORK: 13765 case D_FREEDEP: 13766 case D_JSEGDEP: 13767 case D_JSEG: 13768 case D_SBDEP: 13769 /* never a dependency on these blocks */ 13770 continue; 13771 13772 default: 13773 panic("softdep_count_dependencies: Unexpected type %s", 13774 TYPENAME(wk->wk_type)); 13775 /* NOTREACHED */ 13776 } 13777 } 13778out: 13779 FREE_LOCK(ump); 13780 return retval; 13781} 13782 13783/* 13784 * Acquire exclusive access to a buffer. 13785 * Must be called with a locked mtx parameter. 13786 * Return acquired buffer or NULL on failure. 13787 */ 13788static struct buf * 13789getdirtybuf(bp, lock, waitfor) 13790 struct buf *bp; 13791 struct rwlock *lock; 13792 int waitfor; 13793{ 13794 int error; 13795 13796 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) { 13797 if (waitfor != MNT_WAIT) 13798 return (NULL); 13799 error = BUF_LOCK(bp, 13800 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock); 13801 /* 13802 * Even if we sucessfully acquire bp here, we have dropped 13803 * lock, which may violates our guarantee. 13804 */ 13805 if (error == 0) 13806 BUF_UNLOCK(bp); 13807 else if (error != ENOLCK) 13808 panic("getdirtybuf: inconsistent lock: %d", error); 13809 rw_wlock(lock); 13810 return (NULL); 13811 } 13812 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 13813 if (lock != BO_LOCKPTR(bp->b_bufobj) && waitfor == MNT_WAIT) { 13814 rw_wunlock(lock); 13815 BO_LOCK(bp->b_bufobj); 13816 BUF_UNLOCK(bp); 13817 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 13818 bp->b_vflags |= BV_BKGRDWAIT; 13819 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), 13820 PRIBIO | PDROP, "getbuf", 0); 13821 } else 13822 BO_UNLOCK(bp->b_bufobj); 13823 rw_wlock(lock); 13824 return (NULL); 13825 } 13826 BUF_UNLOCK(bp); 13827 if (waitfor != MNT_WAIT) 13828 return (NULL); 13829 /* 13830 * The lock argument must be bp->b_vp's mutex in 13831 * this case. 13832 */ 13833#ifdef DEBUG_VFS_LOCKS 13834 if (bp->b_vp->v_type != VCHR) 13835 ASSERT_BO_WLOCKED(bp->b_bufobj); 13836#endif 13837 bp->b_vflags |= BV_BKGRDWAIT; 13838 rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0); 13839 return (NULL); 13840 } 13841 if ((bp->b_flags & B_DELWRI) == 0) { 13842 BUF_UNLOCK(bp); 13843 return (NULL); 13844 } 13845 bremfree(bp); 13846 return (bp); 13847} 13848 13849 13850/* 13851 * Check if it is safe to suspend the file system now. On entry, 13852 * the vnode interlock for devvp should be held. Return 0 with 13853 * the mount interlock held if the file system can be suspended now, 13854 * otherwise return EAGAIN with the mount interlock held. 13855 */ 13856int 13857softdep_check_suspend(struct mount *mp, 13858 struct vnode *devvp, 13859 int softdep_depcnt, 13860 int softdep_accdepcnt, 13861 int secondary_writes, 13862 int secondary_accwrites) 13863{ 13864 struct bufobj *bo; 13865 struct ufsmount *ump; 13866 struct inodedep *inodedep; 13867 int error, unlinked; 13868 13869 bo = &devvp->v_bufobj; 13870 ASSERT_BO_WLOCKED(bo); 13871 13872 /* 13873 * If we are not running with soft updates, then we need only 13874 * deal with secondary writes as we try to suspend. 13875 */ 13876 if (MOUNTEDSOFTDEP(mp) == 0) { 13877 MNT_ILOCK(mp); 13878 while (mp->mnt_secondary_writes != 0) { 13879 BO_UNLOCK(bo); 13880 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 13881 (PUSER - 1) | PDROP, "secwr", 0); 13882 BO_LOCK(bo); 13883 MNT_ILOCK(mp); 13884 } 13885 13886 /* 13887 * Reasons for needing more work before suspend: 13888 * - Dirty buffers on devvp. 13889 * - Secondary writes occurred after start of vnode sync loop 13890 */ 13891 error = 0; 13892 if (bo->bo_numoutput > 0 || 13893 bo->bo_dirty.bv_cnt > 0 || 13894 secondary_writes != 0 || 13895 mp->mnt_secondary_writes != 0 || 13896 secondary_accwrites != mp->mnt_secondary_accwrites) 13897 error = EAGAIN; 13898 BO_UNLOCK(bo); 13899 return (error); 13900 } 13901 13902 /* 13903 * If we are running with soft updates, then we need to coordinate 13904 * with them as we try to suspend. 13905 */ 13906 ump = VFSTOUFS(mp); 13907 for (;;) { 13908 if (!TRY_ACQUIRE_LOCK(ump)) { 13909 BO_UNLOCK(bo); 13910 ACQUIRE_LOCK(ump); 13911 FREE_LOCK(ump); 13912 BO_LOCK(bo); 13913 continue; 13914 } 13915 MNT_ILOCK(mp); 13916 if (mp->mnt_secondary_writes != 0) { 13917 FREE_LOCK(ump); 13918 BO_UNLOCK(bo); 13919 msleep(&mp->mnt_secondary_writes, 13920 MNT_MTX(mp), 13921 (PUSER - 1) | PDROP, "secwr", 0); 13922 BO_LOCK(bo); 13923 continue; 13924 } 13925 break; 13926 } 13927 13928 unlinked = 0; 13929 if (MOUNTEDSUJ(mp)) { 13930 for (inodedep = TAILQ_FIRST(&ump->softdep_unlinked); 13931 inodedep != NULL; 13932 inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 13933 if ((inodedep->id_state & (UNLINKED | UNLINKLINKS | 13934 UNLINKONLIST)) != (UNLINKED | UNLINKLINKS | 13935 UNLINKONLIST) || 13936 !check_inodedep_free(inodedep)) 13937 continue; 13938 unlinked++; 13939 } 13940 } 13941 13942 /* 13943 * Reasons for needing more work before suspend: 13944 * - Dirty buffers on devvp. 13945 * - Softdep activity occurred after start of vnode sync loop 13946 * - Secondary writes occurred after start of vnode sync loop 13947 */ 13948 error = 0; 13949 if (bo->bo_numoutput > 0 || 13950 bo->bo_dirty.bv_cnt > 0 || 13951 softdep_depcnt != unlinked || 13952 ump->softdep_deps != unlinked || 13953 softdep_accdepcnt != ump->softdep_accdeps || 13954 secondary_writes != 0 || 13955 mp->mnt_secondary_writes != 0 || 13956 secondary_accwrites != mp->mnt_secondary_accwrites) 13957 error = EAGAIN; 13958 FREE_LOCK(ump); 13959 BO_UNLOCK(bo); 13960 return (error); 13961} 13962 13963 13964/* 13965 * Get the number of dependency structures for the file system, both 13966 * the current number and the total number allocated. These will 13967 * later be used to detect that softdep processing has occurred. 13968 */ 13969void 13970softdep_get_depcounts(struct mount *mp, 13971 int *softdep_depsp, 13972 int *softdep_accdepsp) 13973{ 13974 struct ufsmount *ump; 13975 13976 if (MOUNTEDSOFTDEP(mp) == 0) { 13977 *softdep_depsp = 0; 13978 *softdep_accdepsp = 0; 13979 return; 13980 } 13981 ump = VFSTOUFS(mp); 13982 ACQUIRE_LOCK(ump); 13983 *softdep_depsp = ump->softdep_deps; 13984 *softdep_accdepsp = ump->softdep_accdeps; 13985 FREE_LOCK(ump); 13986} 13987 13988/* 13989 * Wait for pending output on a vnode to complete. 13990 * Must be called with vnode lock and interlock locked. 13991 * 13992 * XXX: Should just be a call to bufobj_wwait(). 13993 */ 13994static void 13995drain_output(vp) 13996 struct vnode *vp; 13997{ 13998 struct bufobj *bo; 13999 14000 bo = &vp->v_bufobj; 14001 ASSERT_VOP_LOCKED(vp, "drain_output"); 14002 ASSERT_BO_WLOCKED(bo); 14003 14004 while (bo->bo_numoutput) { 14005 bo->bo_flag |= BO_WWAIT; 14006 msleep((caddr_t)&bo->bo_numoutput, 14007 BO_LOCKPTR(bo), PRIBIO + 1, "drainvp", 0); 14008 } 14009} 14010 14011/* 14012 * Called whenever a buffer that is being invalidated or reallocated 14013 * contains dependencies. This should only happen if an I/O error has 14014 * occurred. The routine is called with the buffer locked. 14015 */ 14016static void 14017softdep_deallocate_dependencies(bp) 14018 struct buf *bp; 14019{ 14020 14021 if ((bp->b_ioflags & BIO_ERROR) == 0) 14022 panic("softdep_deallocate_dependencies: dangling deps"); 14023 if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL) 14024 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error); 14025 else 14026 printf("softdep_deallocate_dependencies: " 14027 "got error %d while accessing filesystem\n", bp->b_error); 14028 if (bp->b_error != ENXIO) 14029 panic("softdep_deallocate_dependencies: unrecovered I/O error"); 14030} 14031 14032/* 14033 * Function to handle asynchronous write errors in the filesystem. 14034 */ 14035static void 14036softdep_error(func, error) 14037 char *func; 14038 int error; 14039{ 14040 14041 /* XXX should do something better! */ 14042 printf("%s: got error %d while accessing filesystem\n", func, error); 14043} 14044 14045#ifdef DDB 14046 14047static void 14048inodedep_print(struct inodedep *inodedep, int verbose) 14049{ 14050 db_printf("%p fs %p st %x ino %jd inoblk %jd delta %d nlink %d" 14051 " saveino %p\n", 14052 inodedep, inodedep->id_fs, inodedep->id_state, 14053 (intmax_t)inodedep->id_ino, 14054 (intmax_t)fsbtodb(inodedep->id_fs, 14055 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)), 14056 inodedep->id_nlinkdelta, inodedep->id_savednlink, 14057 inodedep->id_savedino1); 14058 14059 if (verbose == 0) 14060 return; 14061 14062 db_printf("\tpendinghd %p, bufwait %p, inowait %p, inoreflst %p, " 14063 "mkdiradd %p\n", 14064 LIST_FIRST(&inodedep->id_pendinghd), 14065 LIST_FIRST(&inodedep->id_bufwait), 14066 LIST_FIRST(&inodedep->id_inowait), 14067 TAILQ_FIRST(&inodedep->id_inoreflst), 14068 inodedep->id_mkdiradd); 14069 db_printf("\tinoupdt %p, newinoupdt %p, extupdt %p, newextupdt %p\n", 14070 TAILQ_FIRST(&inodedep->id_inoupdt), 14071 TAILQ_FIRST(&inodedep->id_newinoupdt), 14072 TAILQ_FIRST(&inodedep->id_extupdt), 14073 TAILQ_FIRST(&inodedep->id_newextupdt)); 14074} 14075 14076DB_SHOW_COMMAND(inodedep, db_show_inodedep) 14077{ 14078 14079 if (have_addr == 0) { 14080 db_printf("Address required\n"); 14081 return; 14082 } 14083 inodedep_print((struct inodedep*)addr, 1); 14084} 14085 14086DB_SHOW_COMMAND(inodedeps, db_show_inodedeps) 14087{ 14088 struct inodedep_hashhead *inodedephd; 14089 struct inodedep *inodedep; 14090 struct ufsmount *ump; 14091 int cnt; 14092 14093 if (have_addr == 0) { 14094 db_printf("Address required\n"); 14095 return; 14096 } 14097 ump = (struct ufsmount *)addr; 14098 for (cnt = 0; cnt < ump->inodedep_hash_size; cnt++) { 14099 inodedephd = &ump->inodedep_hashtbl[cnt]; 14100 LIST_FOREACH(inodedep, inodedephd, id_hash) { 14101 inodedep_print(inodedep, 0); 14102 } 14103 } 14104} 14105 14106DB_SHOW_COMMAND(worklist, db_show_worklist) 14107{ 14108 struct worklist *wk; 14109 14110 if (have_addr == 0) { 14111 db_printf("Address required\n"); 14112 return; 14113 } 14114 wk = (struct worklist *)addr; 14115 printf("worklist: %p type %s state 0x%X\n", 14116 wk, TYPENAME(wk->wk_type), wk->wk_state); 14117} 14118 14119DB_SHOW_COMMAND(workhead, db_show_workhead) 14120{ 14121 struct workhead *wkhd; 14122 struct worklist *wk; 14123 int i; 14124 14125 if (have_addr == 0) { 14126 db_printf("Address required\n"); 14127 return; 14128 } 14129 wkhd = (struct workhead *)addr; 14130 wk = LIST_FIRST(wkhd); 14131 for (i = 0; i < 100 && wk != NULL; i++, wk = LIST_NEXT(wk, wk_list)) 14132 db_printf("worklist: %p type %s state 0x%X", 14133 wk, TYPENAME(wk->wk_type), wk->wk_state); 14134 if (i == 100) 14135 db_printf("workhead overflow"); 14136 printf("\n"); 14137} 14138 14139 14140DB_SHOW_COMMAND(mkdirs, db_show_mkdirs) 14141{ 14142 struct mkdirlist *mkdirlisthd; 14143 struct jaddref *jaddref; 14144 struct diradd *diradd; 14145 struct mkdir *mkdir; 14146 14147 if (have_addr == 0) { 14148 db_printf("Address required\n"); 14149 return; 14150 } 14151 mkdirlisthd = (struct mkdirlist *)addr; 14152 LIST_FOREACH(mkdir, mkdirlisthd, md_mkdirs) { 14153 diradd = mkdir->md_diradd; 14154 db_printf("mkdir: %p state 0x%X dap %p state 0x%X", 14155 mkdir, mkdir->md_state, diradd, diradd->da_state); 14156 if ((jaddref = mkdir->md_jaddref) != NULL) 14157 db_printf(" jaddref %p jaddref state 0x%X", 14158 jaddref, jaddref->ja_state); 14159 db_printf("\n"); 14160 } 14161} 14162 14163/* exported to ffs_vfsops.c */ 14164extern void db_print_ffs(struct ufsmount *ump); 14165void 14166db_print_ffs(struct ufsmount *ump) 14167{ 14168 db_printf("mp %p %s devvp %p fs %p su_wl %d su_deps %d su_req %d\n", 14169 ump->um_mountp, ump->um_mountp->mnt_stat.f_mntonname, 14170 ump->um_devvp, ump->um_fs, ump->softdep_on_worklist, 14171 ump->softdep_deps, ump->softdep_req); 14172} 14173 14174#endif /* DDB */ 14175 14176#endif /* SOFTUPDATES */ 14177