ffs_softdep.c revision 216817
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: head/sys/ufs/ffs/ffs_softdep.c 216817 2010-12-30 10:41:17Z kib $"); 44 45#include "opt_ffs.h" 46#include "opt_ddb.h" 47 48/* 49 * For now we want the safety net that the DEBUG flag provides. 50 */ 51#ifndef DEBUG 52#define DEBUG 53#endif 54 55#include <sys/param.h> 56#include <sys/kernel.h> 57#include <sys/systm.h> 58#include <sys/bio.h> 59#include <sys/buf.h> 60#include <sys/kdb.h> 61#include <sys/kthread.h> 62#include <sys/lock.h> 63#include <sys/malloc.h> 64#include <sys/mount.h> 65#include <sys/mutex.h> 66#include <sys/namei.h> 67#include <sys/proc.h> 68#include <sys/stat.h> 69#include <sys/sysctl.h> 70#include <sys/syslog.h> 71#include <sys/vnode.h> 72#include <sys/conf.h> 73#include <ufs/ufs/dir.h> 74#include <ufs/ufs/extattr.h> 75#include <ufs/ufs/quota.h> 76#include <ufs/ufs/inode.h> 77#include <ufs/ufs/ufsmount.h> 78#include <ufs/ffs/fs.h> 79#include <ufs/ffs/softdep.h> 80#include <ufs/ffs/ffs_extern.h> 81#include <ufs/ufs/ufs_extern.h> 82 83#include <vm/vm.h> 84 85#include <ddb/ddb.h> 86 87#ifndef SOFTUPDATES 88 89int 90softdep_flushfiles(oldmnt, flags, td) 91 struct mount *oldmnt; 92 int flags; 93 struct thread *td; 94{ 95 96 panic("softdep_flushfiles called"); 97} 98 99int 100softdep_mount(devvp, mp, fs, cred) 101 struct vnode *devvp; 102 struct mount *mp; 103 struct fs *fs; 104 struct ucred *cred; 105{ 106 107 return (0); 108} 109 110void 111softdep_initialize() 112{ 113 114 return; 115} 116 117void 118softdep_uninitialize() 119{ 120 121 return; 122} 123 124void 125softdep_unmount(mp) 126 struct mount *mp; 127{ 128 129} 130 131void 132softdep_setup_sbupdate(ump, fs, bp) 133 struct ufsmount *ump; 134 struct fs *fs; 135 struct buf *bp; 136{ 137} 138 139void 140softdep_setup_inomapdep(bp, ip, newinum) 141 struct buf *bp; 142 struct inode *ip; 143 ino_t newinum; 144{ 145 146 panic("softdep_setup_inomapdep called"); 147} 148 149void 150softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags) 151 struct buf *bp; 152 struct mount *mp; 153 ufs2_daddr_t newblkno; 154 int frags; 155 int oldfrags; 156{ 157 158 panic("softdep_setup_blkmapdep called"); 159} 160 161void 162softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 163 struct inode *ip; 164 ufs_lbn_t lbn; 165 ufs2_daddr_t newblkno; 166 ufs2_daddr_t oldblkno; 167 long newsize; 168 long oldsize; 169 struct buf *bp; 170{ 171 172 panic("softdep_setup_allocdirect called"); 173} 174 175void 176softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 177 struct inode *ip; 178 ufs_lbn_t lbn; 179 ufs2_daddr_t newblkno; 180 ufs2_daddr_t oldblkno; 181 long newsize; 182 long oldsize; 183 struct buf *bp; 184{ 185 186 panic("softdep_setup_allocext called"); 187} 188 189void 190softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 191 struct inode *ip; 192 ufs_lbn_t lbn; 193 struct buf *bp; 194 int ptrno; 195 ufs2_daddr_t newblkno; 196 ufs2_daddr_t oldblkno; 197 struct buf *nbp; 198{ 199 200 panic("softdep_setup_allocindir_page called"); 201} 202 203void 204softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 205 struct buf *nbp; 206 struct inode *ip; 207 struct buf *bp; 208 int ptrno; 209 ufs2_daddr_t newblkno; 210{ 211 212 panic("softdep_setup_allocindir_meta called"); 213} 214 215void 216softdep_setup_freeblocks(ip, length, flags) 217 struct inode *ip; 218 off_t length; 219 int flags; 220{ 221 222 panic("softdep_setup_freeblocks called"); 223} 224 225void 226softdep_freefile(pvp, ino, mode) 227 struct vnode *pvp; 228 ino_t ino; 229 int mode; 230{ 231 232 panic("softdep_freefile called"); 233} 234 235int 236softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 237 struct buf *bp; 238 struct inode *dp; 239 off_t diroffset; 240 ino_t newinum; 241 struct buf *newdirbp; 242 int isnewblk; 243{ 244 245 panic("softdep_setup_directory_add called"); 246} 247 248void 249softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 250 struct buf *bp; 251 struct inode *dp; 252 caddr_t base; 253 caddr_t oldloc; 254 caddr_t newloc; 255 int entrysize; 256{ 257 258 panic("softdep_change_directoryentry_offset called"); 259} 260 261void 262softdep_setup_remove(bp, dp, ip, isrmdir) 263 struct buf *bp; 264 struct inode *dp; 265 struct inode *ip; 266 int isrmdir; 267{ 268 269 panic("softdep_setup_remove called"); 270} 271 272void 273softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 274 struct buf *bp; 275 struct inode *dp; 276 struct inode *ip; 277 ino_t newinum; 278 int isrmdir; 279{ 280 281 panic("softdep_setup_directory_change called"); 282} 283 284void * 285softdep_setup_trunc(vp, length, flags) 286 struct vnode *vp; 287 off_t length; 288 int flags; 289{ 290 291 panic("%s called", __FUNCTION__); 292 293 return (NULL); 294} 295 296int 297softdep_complete_trunc(vp, cookie) 298 struct vnode *vp; 299 void *cookie; 300{ 301 302 panic("%s called", __FUNCTION__); 303 304 return (0); 305} 306 307void 308softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 309 struct mount *mp; 310 struct buf *bp; 311 ufs2_daddr_t blkno; 312 int frags; 313 struct workhead *wkhd; 314{ 315 316 panic("%s called", __FUNCTION__); 317} 318 319void 320softdep_setup_inofree(mp, bp, ino, wkhd) 321 struct mount *mp; 322 struct buf *bp; 323 ino_t ino; 324 struct workhead *wkhd; 325{ 326 327 panic("%s called", __FUNCTION__); 328} 329 330void 331softdep_setup_unlink(dp, ip) 332 struct inode *dp; 333 struct inode *ip; 334{ 335 336 panic("%s called", __FUNCTION__); 337} 338 339void 340softdep_setup_link(dp, ip) 341 struct inode *dp; 342 struct inode *ip; 343{ 344 345 panic("%s called", __FUNCTION__); 346} 347 348void 349softdep_revert_link(dp, ip) 350 struct inode *dp; 351 struct inode *ip; 352{ 353 354 panic("%s called", __FUNCTION__); 355} 356 357void 358softdep_setup_rmdir(dp, ip) 359 struct inode *dp; 360 struct inode *ip; 361{ 362 363 panic("%s called", __FUNCTION__); 364} 365 366void 367softdep_revert_rmdir(dp, ip) 368 struct inode *dp; 369 struct inode *ip; 370{ 371 372 panic("%s called", __FUNCTION__); 373} 374 375void 376softdep_setup_create(dp, ip) 377 struct inode *dp; 378 struct inode *ip; 379{ 380 381 panic("%s called", __FUNCTION__); 382} 383 384void 385softdep_revert_create(dp, ip) 386 struct inode *dp; 387 struct inode *ip; 388{ 389 390 panic("%s called", __FUNCTION__); 391} 392 393void 394softdep_setup_mkdir(dp, ip) 395 struct inode *dp; 396 struct inode *ip; 397{ 398 399 panic("%s called", __FUNCTION__); 400} 401 402void 403softdep_revert_mkdir(dp, ip) 404 struct inode *dp; 405 struct inode *ip; 406{ 407 408 panic("%s called", __FUNCTION__); 409} 410 411void 412softdep_setup_dotdot_link(dp, ip) 413 struct inode *dp; 414 struct inode *ip; 415{ 416 417 panic("%s called", __FUNCTION__); 418} 419 420int 421softdep_prealloc(vp, waitok) 422 struct vnode *vp; 423 int waitok; 424{ 425 426 panic("%s called", __FUNCTION__); 427 428 return (0); 429} 430 431int 432softdep_journal_lookup(mp, vpp) 433 struct mount *mp; 434 struct vnode **vpp; 435{ 436 437 return (ENOENT); 438} 439 440void 441softdep_change_linkcnt(ip) 442 struct inode *ip; 443{ 444 445 panic("softdep_change_linkcnt called"); 446} 447 448void 449softdep_load_inodeblock(ip) 450 struct inode *ip; 451{ 452 453 panic("softdep_load_inodeblock called"); 454} 455 456void 457softdep_update_inodeblock(ip, bp, waitfor) 458 struct inode *ip; 459 struct buf *bp; 460 int waitfor; 461{ 462 463 panic("softdep_update_inodeblock called"); 464} 465 466int 467softdep_fsync(vp) 468 struct vnode *vp; /* the "in_core" copy of the inode */ 469{ 470 471 return (0); 472} 473 474void 475softdep_fsync_mountdev(vp) 476 struct vnode *vp; 477{ 478 479 return; 480} 481 482int 483softdep_flushworklist(oldmnt, countp, td) 484 struct mount *oldmnt; 485 int *countp; 486 struct thread *td; 487{ 488 489 *countp = 0; 490 return (0); 491} 492 493int 494softdep_sync_metadata(struct vnode *vp) 495{ 496 497 return (0); 498} 499 500int 501softdep_slowdown(vp) 502 struct vnode *vp; 503{ 504 505 panic("softdep_slowdown called"); 506} 507 508void 509softdep_releasefile(ip) 510 struct inode *ip; /* inode with the zero effective link count */ 511{ 512 513 panic("softdep_releasefile called"); 514} 515 516int 517softdep_request_cleanup(fs, vp) 518 struct fs *fs; 519 struct vnode *vp; 520{ 521 522 return (0); 523} 524 525int 526softdep_check_suspend(struct mount *mp, 527 struct vnode *devvp, 528 int softdep_deps, 529 int softdep_accdeps, 530 int secondary_writes, 531 int secondary_accwrites) 532{ 533 struct bufobj *bo; 534 int error; 535 536 (void) softdep_deps, 537 (void) softdep_accdeps; 538 539 bo = &devvp->v_bufobj; 540 ASSERT_BO_LOCKED(bo); 541 542 MNT_ILOCK(mp); 543 while (mp->mnt_secondary_writes != 0) { 544 BO_UNLOCK(bo); 545 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 546 (PUSER - 1) | PDROP, "secwr", 0); 547 BO_LOCK(bo); 548 MNT_ILOCK(mp); 549 } 550 551 /* 552 * Reasons for needing more work before suspend: 553 * - Dirty buffers on devvp. 554 * - Secondary writes occurred after start of vnode sync loop 555 */ 556 error = 0; 557 if (bo->bo_numoutput > 0 || 558 bo->bo_dirty.bv_cnt > 0 || 559 secondary_writes != 0 || 560 mp->mnt_secondary_writes != 0 || 561 secondary_accwrites != mp->mnt_secondary_accwrites) 562 error = EAGAIN; 563 BO_UNLOCK(bo); 564 return (error); 565} 566 567void 568softdep_get_depcounts(struct mount *mp, 569 int *softdepactivep, 570 int *softdepactiveaccp) 571{ 572 (void) mp; 573 *softdepactivep = 0; 574 *softdepactiveaccp = 0; 575} 576 577#else 578/* 579 * These definitions need to be adapted to the system to which 580 * this file is being ported. 581 */ 582 583#define M_SOFTDEP_FLAGS (M_WAITOK) 584 585#define D_PAGEDEP 0 586#define D_INODEDEP 1 587#define D_BMSAFEMAP 2 588#define D_NEWBLK 3 589#define D_ALLOCDIRECT 4 590#define D_INDIRDEP 5 591#define D_ALLOCINDIR 6 592#define D_FREEFRAG 7 593#define D_FREEBLKS 8 594#define D_FREEFILE 9 595#define D_DIRADD 10 596#define D_MKDIR 11 597#define D_DIRREM 12 598#define D_NEWDIRBLK 13 599#define D_FREEWORK 14 600#define D_FREEDEP 15 601#define D_JADDREF 16 602#define D_JREMREF 17 603#define D_JMVREF 18 604#define D_JNEWBLK 19 605#define D_JFREEBLK 20 606#define D_JFREEFRAG 21 607#define D_JSEG 22 608#define D_JSEGDEP 23 609#define D_SBDEP 24 610#define D_JTRUNC 25 611#define D_LAST D_JTRUNC 612 613unsigned long dep_current[D_LAST + 1]; 614unsigned long dep_total[D_LAST + 1]; 615 616 617SYSCTL_NODE(_debug, OID_AUTO, softdep, CTLFLAG_RW, 0, "soft updates stats"); 618SYSCTL_NODE(_debug_softdep, OID_AUTO, total, CTLFLAG_RW, 0, 619 "total dependencies allocated"); 620SYSCTL_NODE(_debug_softdep, OID_AUTO, current, CTLFLAG_RW, 0, 621 "current dependencies allocated"); 622 623#define SOFTDEP_TYPE(type, str, long) \ 624 static MALLOC_DEFINE(M_ ## type, #str, long); \ 625 SYSCTL_LONG(_debug_softdep_total, OID_AUTO, str, CTLFLAG_RD, \ 626 &dep_total[D_ ## type], 0, ""); \ 627 SYSCTL_LONG(_debug_softdep_current, OID_AUTO, str, CTLFLAG_RD, \ 628 &dep_current[D_ ## type], 0, ""); 629 630SOFTDEP_TYPE(PAGEDEP, pagedep, "File page dependencies"); 631SOFTDEP_TYPE(INODEDEP, inodedep, "Inode dependencies"); 632SOFTDEP_TYPE(BMSAFEMAP, bmsafemap, 633 "Block or frag allocated from cyl group map"); 634SOFTDEP_TYPE(NEWBLK, newblk, "New block or frag allocation dependency"); 635SOFTDEP_TYPE(ALLOCDIRECT, allocdirect, "Block or frag dependency for an inode"); 636SOFTDEP_TYPE(INDIRDEP, indirdep, "Indirect block dependencies"); 637SOFTDEP_TYPE(ALLOCINDIR, allocindir, "Block dependency for an indirect block"); 638SOFTDEP_TYPE(FREEFRAG, freefrag, "Previously used frag for an inode"); 639SOFTDEP_TYPE(FREEBLKS, freeblks, "Blocks freed from an inode"); 640SOFTDEP_TYPE(FREEFILE, freefile, "Inode deallocated"); 641SOFTDEP_TYPE(DIRADD, diradd, "New directory entry"); 642SOFTDEP_TYPE(MKDIR, mkdir, "New directory"); 643SOFTDEP_TYPE(DIRREM, dirrem, "Directory entry deleted"); 644SOFTDEP_TYPE(NEWDIRBLK, newdirblk, "Unclaimed new directory block"); 645SOFTDEP_TYPE(FREEWORK, freework, "free an inode block"); 646SOFTDEP_TYPE(FREEDEP, freedep, "track a block free"); 647SOFTDEP_TYPE(JADDREF, jaddref, "Journal inode ref add"); 648SOFTDEP_TYPE(JREMREF, jremref, "Journal inode ref remove"); 649SOFTDEP_TYPE(JMVREF, jmvref, "Journal inode ref move"); 650SOFTDEP_TYPE(JNEWBLK, jnewblk, "Journal new block"); 651SOFTDEP_TYPE(JFREEBLK, jfreeblk, "Journal free block"); 652SOFTDEP_TYPE(JFREEFRAG, jfreefrag, "Journal free frag"); 653SOFTDEP_TYPE(JSEG, jseg, "Journal segment"); 654SOFTDEP_TYPE(JSEGDEP, jsegdep, "Journal segment complete"); 655SOFTDEP_TYPE(SBDEP, sbdep, "Superblock write dependency"); 656SOFTDEP_TYPE(JTRUNC, jtrunc, "Journal inode truncation"); 657 658static MALLOC_DEFINE(M_SAVEDINO, "savedino", "Saved inodes"); 659static MALLOC_DEFINE(M_JBLOCKS, "jblocks", "Journal block locations"); 660 661/* 662 * translate from workitem type to memory type 663 * MUST match the defines above, such that memtype[D_XXX] == M_XXX 664 */ 665static struct malloc_type *memtype[] = { 666 M_PAGEDEP, 667 M_INODEDEP, 668 M_BMSAFEMAP, 669 M_NEWBLK, 670 M_ALLOCDIRECT, 671 M_INDIRDEP, 672 M_ALLOCINDIR, 673 M_FREEFRAG, 674 M_FREEBLKS, 675 M_FREEFILE, 676 M_DIRADD, 677 M_MKDIR, 678 M_DIRREM, 679 M_NEWDIRBLK, 680 M_FREEWORK, 681 M_FREEDEP, 682 M_JADDREF, 683 M_JREMREF, 684 M_JMVREF, 685 M_JNEWBLK, 686 M_JFREEBLK, 687 M_JFREEFRAG, 688 M_JSEG, 689 M_JSEGDEP, 690 M_SBDEP, 691 M_JTRUNC 692}; 693 694static LIST_HEAD(mkdirlist, mkdir) mkdirlisthd; 695 696#define DtoM(type) (memtype[type]) 697 698/* 699 * Names of malloc types. 700 */ 701#define TYPENAME(type) \ 702 ((unsigned)(type) <= D_LAST ? memtype[type]->ks_shortdesc : "???") 703/* 704 * End system adaptation definitions. 705 */ 706 707#define DOTDOT_OFFSET offsetof(struct dirtemplate, dotdot_ino) 708#define DOT_OFFSET offsetof(struct dirtemplate, dot_ino) 709 710/* 711 * Forward declarations. 712 */ 713struct inodedep_hashhead; 714struct newblk_hashhead; 715struct pagedep_hashhead; 716struct bmsafemap_hashhead; 717 718/* 719 * Internal function prototypes. 720 */ 721static void softdep_error(char *, int); 722static void drain_output(struct vnode *); 723static struct buf *getdirtybuf(struct buf *, struct mtx *, int); 724static void clear_remove(struct thread *); 725static void clear_inodedeps(struct thread *); 726static void unlinked_inodedep(struct mount *, struct inodedep *); 727static void clear_unlinked_inodedep(struct inodedep *); 728static struct inodedep *first_unlinked_inodedep(struct ufsmount *); 729static int flush_pagedep_deps(struct vnode *, struct mount *, 730 struct diraddhd *); 731static void free_pagedep(struct pagedep *); 732static int flush_newblk_dep(struct vnode *, struct mount *, ufs_lbn_t); 733static int flush_inodedep_deps(struct mount *, ino_t); 734static int flush_deplist(struct allocdirectlst *, int, int *); 735static int handle_written_filepage(struct pagedep *, struct buf *); 736static int handle_written_sbdep(struct sbdep *, struct buf *); 737static void initiate_write_sbdep(struct sbdep *); 738static void diradd_inode_written(struct diradd *, struct inodedep *); 739static int handle_written_indirdep(struct indirdep *, struct buf *, 740 struct buf**); 741static int handle_written_inodeblock(struct inodedep *, struct buf *); 742static int handle_written_bmsafemap(struct bmsafemap *, struct buf *); 743static void handle_written_jaddref(struct jaddref *); 744static void handle_written_jremref(struct jremref *); 745static void handle_written_jseg(struct jseg *, struct buf *); 746static void handle_written_jnewblk(struct jnewblk *); 747static void handle_written_jfreeblk(struct jfreeblk *); 748static void handle_written_jfreefrag(struct jfreefrag *); 749static void complete_jseg(struct jseg *); 750static void jseg_write(struct fs *, struct jblocks *, struct jseg *, 751 uint8_t *); 752static void jaddref_write(struct jaddref *, struct jseg *, uint8_t *); 753static void jremref_write(struct jremref *, struct jseg *, uint8_t *); 754static void jmvref_write(struct jmvref *, struct jseg *, uint8_t *); 755static void jtrunc_write(struct jtrunc *, struct jseg *, uint8_t *); 756static void jnewblk_write(struct jnewblk *, struct jseg *, uint8_t *); 757static void jfreeblk_write(struct jfreeblk *, struct jseg *, uint8_t *); 758static void jfreefrag_write(struct jfreefrag *, struct jseg *, uint8_t *); 759static inline void inoref_write(struct inoref *, struct jseg *, 760 struct jrefrec *); 761static void handle_allocdirect_partdone(struct allocdirect *, 762 struct workhead *); 763static void cancel_newblk(struct newblk *, struct workhead *); 764static void indirdep_complete(struct indirdep *); 765static void handle_allocindir_partdone(struct allocindir *); 766static void initiate_write_filepage(struct pagedep *, struct buf *); 767static void initiate_write_indirdep(struct indirdep*, struct buf *); 768static void handle_written_mkdir(struct mkdir *, int); 769static void initiate_write_bmsafemap(struct bmsafemap *, struct buf *); 770static void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *); 771static void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *); 772static void handle_workitem_freefile(struct freefile *); 773static void handle_workitem_remove(struct dirrem *, struct vnode *); 774static struct dirrem *newdirrem(struct buf *, struct inode *, 775 struct inode *, int, struct dirrem **); 776static void cancel_indirdep(struct indirdep *, struct buf *, struct inodedep *, 777 struct freeblks *); 778static void free_indirdep(struct indirdep *); 779static void free_diradd(struct diradd *, struct workhead *); 780static void merge_diradd(struct inodedep *, struct diradd *); 781static void complete_diradd(struct diradd *); 782static struct diradd *diradd_lookup(struct pagedep *, int); 783static struct jremref *cancel_diradd_dotdot(struct inode *, struct dirrem *, 784 struct jremref *); 785static struct jremref *cancel_mkdir_dotdot(struct inode *, struct dirrem *, 786 struct jremref *); 787static void cancel_diradd(struct diradd *, struct dirrem *, struct jremref *, 788 struct jremref *, struct jremref *); 789static void dirrem_journal(struct dirrem *, struct jremref *, struct jremref *, 790 struct jremref *); 791static void cancel_allocindir(struct allocindir *, struct inodedep *, 792 struct freeblks *); 793static void complete_mkdir(struct mkdir *); 794static void free_newdirblk(struct newdirblk *); 795static void free_jremref(struct jremref *); 796static void free_jaddref(struct jaddref *); 797static void free_jsegdep(struct jsegdep *); 798static void free_jseg(struct jseg *); 799static void free_jnewblk(struct jnewblk *); 800static void free_jfreeblk(struct jfreeblk *); 801static void free_jfreefrag(struct jfreefrag *); 802static void free_freedep(struct freedep *); 803static void journal_jremref(struct dirrem *, struct jremref *, 804 struct inodedep *); 805static void cancel_jnewblk(struct jnewblk *, struct workhead *); 806static int cancel_jaddref(struct jaddref *, struct inodedep *, 807 struct workhead *); 808static void cancel_jfreefrag(struct jfreefrag *); 809static void indir_trunc(struct freework *, ufs2_daddr_t, ufs_lbn_t); 810static int deallocate_dependencies(struct buf *, struct inodedep *, 811 struct freeblks *); 812static void free_newblk(struct newblk *); 813static void cancel_allocdirect(struct allocdirectlst *, 814 struct allocdirect *, struct freeblks *, int); 815static int check_inode_unwritten(struct inodedep *); 816static int free_inodedep(struct inodedep *); 817static void freework_freeblock(struct freework *); 818static void handle_workitem_freeblocks(struct freeblks *, int); 819static void handle_complete_freeblocks(struct freeblks *); 820static void handle_workitem_indirblk(struct freework *); 821static void handle_written_freework(struct freework *); 822static void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *); 823static void setup_allocindir_phase2(struct buf *, struct inode *, 824 struct inodedep *, struct allocindir *, ufs_lbn_t); 825static struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t, 826 ufs2_daddr_t, ufs_lbn_t); 827static void handle_workitem_freefrag(struct freefrag *); 828static struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long, 829 ufs_lbn_t); 830static void allocdirect_merge(struct allocdirectlst *, 831 struct allocdirect *, struct allocdirect *); 832static struct freefrag *allocindir_merge(struct allocindir *, 833 struct allocindir *); 834static int bmsafemap_find(struct bmsafemap_hashhead *, struct mount *, int, 835 struct bmsafemap **); 836static struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *, 837 int cg); 838static int newblk_find(struct newblk_hashhead *, struct mount *, ufs2_daddr_t, 839 int, struct newblk **); 840static int newblk_lookup(struct mount *, ufs2_daddr_t, int, struct newblk **); 841static int inodedep_find(struct inodedep_hashhead *, struct fs *, ino_t, 842 struct inodedep **); 843static int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **); 844static int pagedep_lookup(struct mount *, ino_t, ufs_lbn_t, int, 845 struct pagedep **); 846static int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t, 847 struct mount *mp, int, struct pagedep **); 848static void pause_timer(void *); 849static int request_cleanup(struct mount *, int); 850static int process_worklist_item(struct mount *, int); 851static void process_removes(struct vnode *); 852static void jwork_move(struct workhead *, struct workhead *); 853static void add_to_worklist(struct worklist *, int); 854static void remove_from_worklist(struct worklist *); 855static void softdep_flush(void); 856static int softdep_speedup(void); 857static void worklist_speedup(void); 858static int journal_mount(struct mount *, struct fs *, struct ucred *); 859static void journal_unmount(struct mount *); 860static int journal_space(struct ufsmount *, int); 861static void journal_suspend(struct ufsmount *); 862static int journal_unsuspend(struct ufsmount *ump); 863static void softdep_prelink(struct vnode *, struct vnode *); 864static void add_to_journal(struct worklist *); 865static void remove_from_journal(struct worklist *); 866static void softdep_process_journal(struct mount *, int); 867static struct jremref *newjremref(struct dirrem *, struct inode *, 868 struct inode *ip, off_t, nlink_t); 869static struct jaddref *newjaddref(struct inode *, ino_t, off_t, int16_t, 870 uint16_t); 871static inline void newinoref(struct inoref *, ino_t, ino_t, off_t, nlink_t, 872 uint16_t); 873static inline struct jsegdep *inoref_jseg(struct inoref *); 874static struct jmvref *newjmvref(struct inode *, ino_t, off_t, off_t); 875static struct jfreeblk *newjfreeblk(struct freeblks *, ufs_lbn_t, 876 ufs2_daddr_t, int); 877static struct jfreefrag *newjfreefrag(struct freefrag *, struct inode *, 878 ufs2_daddr_t, long, ufs_lbn_t); 879static struct freework *newfreework(struct freeblks *, struct freework *, 880 ufs_lbn_t, ufs2_daddr_t, int, int); 881static void jwait(struct worklist *wk); 882static struct inodedep *inodedep_lookup_ip(struct inode *); 883static int bmsafemap_rollbacks(struct bmsafemap *); 884static struct freefile *handle_bufwait(struct inodedep *, struct workhead *); 885static void handle_jwork(struct workhead *); 886static struct mkdir *setup_newdir(struct diradd *, ino_t, ino_t, struct buf *, 887 struct mkdir **); 888static struct jblocks *jblocks_create(void); 889static ufs2_daddr_t jblocks_alloc(struct jblocks *, int, int *); 890static void jblocks_free(struct jblocks *, struct mount *, int); 891static void jblocks_destroy(struct jblocks *); 892static void jblocks_add(struct jblocks *, ufs2_daddr_t, int); 893 894/* 895 * Exported softdep operations. 896 */ 897static void softdep_disk_io_initiation(struct buf *); 898static void softdep_disk_write_complete(struct buf *); 899static void softdep_deallocate_dependencies(struct buf *); 900static int softdep_count_dependencies(struct buf *bp, int); 901 902static struct mtx lk; 903MTX_SYSINIT(softdep_lock, &lk, "Softdep Lock", MTX_DEF); 904 905#define TRY_ACQUIRE_LOCK(lk) mtx_trylock(lk) 906#define ACQUIRE_LOCK(lk) mtx_lock(lk) 907#define FREE_LOCK(lk) mtx_unlock(lk) 908 909#define BUF_AREC(bp) lockallowrecurse(&(bp)->b_lock) 910#define BUF_NOREC(bp) lockdisablerecurse(&(bp)->b_lock) 911 912/* 913 * Worklist queue management. 914 * These routines require that the lock be held. 915 */ 916#ifndef /* NOT */ DEBUG 917#define WORKLIST_INSERT(head, item) do { \ 918 (item)->wk_state |= ONWORKLIST; \ 919 LIST_INSERT_HEAD(head, item, wk_list); \ 920} while (0) 921#define WORKLIST_REMOVE(item) do { \ 922 (item)->wk_state &= ~ONWORKLIST; \ 923 LIST_REMOVE(item, wk_list); \ 924} while (0) 925#define WORKLIST_INSERT_UNLOCKED WORKLIST_INSERT 926#define WORKLIST_REMOVE_UNLOCKED WORKLIST_REMOVE 927 928#else /* DEBUG */ 929static void worklist_insert(struct workhead *, struct worklist *, int); 930static void worklist_remove(struct worklist *, int); 931 932#define WORKLIST_INSERT(head, item) worklist_insert(head, item, 1) 933#define WORKLIST_INSERT_UNLOCKED(head, item) worklist_insert(head, item, 0) 934#define WORKLIST_REMOVE(item) worklist_remove(item, 1) 935#define WORKLIST_REMOVE_UNLOCKED(item) worklist_remove(item, 0) 936 937static void 938worklist_insert(head, item, locked) 939 struct workhead *head; 940 struct worklist *item; 941 int locked; 942{ 943 944 if (locked) 945 mtx_assert(&lk, MA_OWNED); 946 if (item->wk_state & ONWORKLIST) 947 panic("worklist_insert: %p %s(0x%X) already on list", 948 item, TYPENAME(item->wk_type), item->wk_state); 949 item->wk_state |= ONWORKLIST; 950 LIST_INSERT_HEAD(head, item, wk_list); 951} 952 953static void 954worklist_remove(item, locked) 955 struct worklist *item; 956 int locked; 957{ 958 959 if (locked) 960 mtx_assert(&lk, MA_OWNED); 961 if ((item->wk_state & ONWORKLIST) == 0) 962 panic("worklist_remove: %p %s(0x%X) not on list", 963 item, TYPENAME(item->wk_type), item->wk_state); 964 item->wk_state &= ~ONWORKLIST; 965 LIST_REMOVE(item, wk_list); 966} 967#endif /* DEBUG */ 968 969/* 970 * Merge two jsegdeps keeping only the oldest one as newer references 971 * can't be discarded until after older references. 972 */ 973static inline struct jsegdep * 974jsegdep_merge(struct jsegdep *one, struct jsegdep *two) 975{ 976 struct jsegdep *swp; 977 978 if (two == NULL) 979 return (one); 980 981 if (one->jd_seg->js_seq > two->jd_seg->js_seq) { 982 swp = one; 983 one = two; 984 two = swp; 985 } 986 WORKLIST_REMOVE(&two->jd_list); 987 free_jsegdep(two); 988 989 return (one); 990} 991 992/* 993 * If two freedeps are compatible free one to reduce list size. 994 */ 995static inline struct freedep * 996freedep_merge(struct freedep *one, struct freedep *two) 997{ 998 if (two == NULL) 999 return (one); 1000 1001 if (one->fd_freework == two->fd_freework) { 1002 WORKLIST_REMOVE(&two->fd_list); 1003 free_freedep(two); 1004 } 1005 return (one); 1006} 1007 1008/* 1009 * Move journal work from one list to another. Duplicate freedeps and 1010 * jsegdeps are coalesced to keep the lists as small as possible. 1011 */ 1012static void 1013jwork_move(dst, src) 1014 struct workhead *dst; 1015 struct workhead *src; 1016{ 1017 struct freedep *freedep; 1018 struct jsegdep *jsegdep; 1019 struct worklist *wkn; 1020 struct worklist *wk; 1021 1022 KASSERT(dst != src, 1023 ("jwork_move: dst == src")); 1024 freedep = NULL; 1025 jsegdep = NULL; 1026 LIST_FOREACH_SAFE(wk, dst, wk_list, wkn) { 1027 if (wk->wk_type == D_JSEGDEP) 1028 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1029 if (wk->wk_type == D_FREEDEP) 1030 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1031 } 1032 1033 mtx_assert(&lk, MA_OWNED); 1034 while ((wk = LIST_FIRST(src)) != NULL) { 1035 WORKLIST_REMOVE(wk); 1036 WORKLIST_INSERT(dst, wk); 1037 if (wk->wk_type == D_JSEGDEP) { 1038 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1039 continue; 1040 } 1041 if (wk->wk_type == D_FREEDEP) 1042 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1043 } 1044} 1045 1046/* 1047 * Routines for tracking and managing workitems. 1048 */ 1049static void workitem_free(struct worklist *, int); 1050static void workitem_alloc(struct worklist *, int, struct mount *); 1051 1052#define WORKITEM_FREE(item, type) workitem_free((struct worklist *)(item), (type)) 1053 1054static void 1055workitem_free(item, type) 1056 struct worklist *item; 1057 int type; 1058{ 1059 struct ufsmount *ump; 1060 mtx_assert(&lk, MA_OWNED); 1061 1062#ifdef DEBUG 1063 if (item->wk_state & ONWORKLIST) 1064 panic("workitem_free: %s(0x%X) still on list", 1065 TYPENAME(item->wk_type), item->wk_state); 1066 if (item->wk_type != type) 1067 panic("workitem_free: type mismatch %s != %s", 1068 TYPENAME(item->wk_type), TYPENAME(type)); 1069#endif 1070 ump = VFSTOUFS(item->wk_mp); 1071 if (--ump->softdep_deps == 0 && ump->softdep_req) 1072 wakeup(&ump->softdep_deps); 1073 dep_current[type]--; 1074 free(item, DtoM(type)); 1075} 1076 1077static void 1078workitem_alloc(item, type, mp) 1079 struct worklist *item; 1080 int type; 1081 struct mount *mp; 1082{ 1083 item->wk_type = type; 1084 item->wk_mp = mp; 1085 item->wk_state = 0; 1086 ACQUIRE_LOCK(&lk); 1087 dep_current[type]++; 1088 dep_total[type]++; 1089 VFSTOUFS(mp)->softdep_deps++; 1090 VFSTOUFS(mp)->softdep_accdeps++; 1091 FREE_LOCK(&lk); 1092} 1093 1094/* 1095 * Workitem queue management 1096 */ 1097static int max_softdeps; /* maximum number of structs before slowdown */ 1098static int maxindirdeps = 50; /* max number of indirdeps before slowdown */ 1099static int tickdelay = 2; /* number of ticks to pause during slowdown */ 1100static int proc_waiting; /* tracks whether we have a timeout posted */ 1101static int *stat_countp; /* statistic to count in proc_waiting timeout */ 1102static struct callout softdep_callout; 1103static int req_pending; 1104static int req_clear_inodedeps; /* syncer process flush some inodedeps */ 1105#define FLUSH_INODES 1 1106static int req_clear_remove; /* syncer process flush some freeblks */ 1107#define FLUSH_REMOVE 2 1108#define FLUSH_REMOVE_WAIT 3 1109static long num_freeblkdep; /* number of freeblks workitems allocated */ 1110 1111/* 1112 * runtime statistics 1113 */ 1114static int stat_worklist_push; /* number of worklist cleanups */ 1115static int stat_blk_limit_push; /* number of times block limit neared */ 1116static int stat_ino_limit_push; /* number of times inode limit neared */ 1117static int stat_blk_limit_hit; /* number of times block slowdown imposed */ 1118static int stat_ino_limit_hit; /* number of times inode slowdown imposed */ 1119static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */ 1120static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */ 1121static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */ 1122static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */ 1123static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */ 1124static int stat_jaddref; /* bufs redirtied as ino bitmap can not write */ 1125static int stat_jnewblk; /* bufs redirtied as blk bitmap can not write */ 1126static int stat_journal_min; /* Times hit journal min threshold */ 1127static int stat_journal_low; /* Times hit journal low threshold */ 1128static int stat_journal_wait; /* Times blocked in jwait(). */ 1129static int stat_jwait_filepage; /* Times blocked in jwait() for filepage. */ 1130static int stat_jwait_freeblks; /* Times blocked in jwait() for freeblks. */ 1131static int stat_jwait_inode; /* Times blocked in jwait() for inodes. */ 1132static int stat_jwait_newblk; /* Times blocked in jwait() for newblks. */ 1133 1134SYSCTL_INT(_debug_softdep, OID_AUTO, max_softdeps, CTLFLAG_RW, 1135 &max_softdeps, 0, ""); 1136SYSCTL_INT(_debug_softdep, OID_AUTO, tickdelay, CTLFLAG_RW, 1137 &tickdelay, 0, ""); 1138SYSCTL_INT(_debug_softdep, OID_AUTO, maxindirdeps, CTLFLAG_RW, 1139 &maxindirdeps, 0, ""); 1140SYSCTL_INT(_debug_softdep, OID_AUTO, worklist_push, CTLFLAG_RW, 1141 &stat_worklist_push, 0,""); 1142SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_push, CTLFLAG_RW, 1143 &stat_blk_limit_push, 0,""); 1144SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_push, CTLFLAG_RW, 1145 &stat_ino_limit_push, 0,""); 1146SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_hit, CTLFLAG_RW, 1147 &stat_blk_limit_hit, 0, ""); 1148SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_hit, CTLFLAG_RW, 1149 &stat_ino_limit_hit, 0, ""); 1150SYSCTL_INT(_debug_softdep, OID_AUTO, sync_limit_hit, CTLFLAG_RW, 1151 &stat_sync_limit_hit, 0, ""); 1152SYSCTL_INT(_debug_softdep, OID_AUTO, indir_blk_ptrs, CTLFLAG_RW, 1153 &stat_indir_blk_ptrs, 0, ""); 1154SYSCTL_INT(_debug_softdep, OID_AUTO, inode_bitmap, CTLFLAG_RW, 1155 &stat_inode_bitmap, 0, ""); 1156SYSCTL_INT(_debug_softdep, OID_AUTO, direct_blk_ptrs, CTLFLAG_RW, 1157 &stat_direct_blk_ptrs, 0, ""); 1158SYSCTL_INT(_debug_softdep, OID_AUTO, dir_entry, CTLFLAG_RW, 1159 &stat_dir_entry, 0, ""); 1160SYSCTL_INT(_debug_softdep, OID_AUTO, jaddref_rollback, CTLFLAG_RW, 1161 &stat_jaddref, 0, ""); 1162SYSCTL_INT(_debug_softdep, OID_AUTO, jnewblk_rollback, CTLFLAG_RW, 1163 &stat_jnewblk, 0, ""); 1164SYSCTL_INT(_debug_softdep, OID_AUTO, journal_low, CTLFLAG_RW, 1165 &stat_journal_low, 0, ""); 1166SYSCTL_INT(_debug_softdep, OID_AUTO, journal_min, CTLFLAG_RW, 1167 &stat_journal_min, 0, ""); 1168SYSCTL_INT(_debug_softdep, OID_AUTO, journal_wait, CTLFLAG_RW, 1169 &stat_journal_wait, 0, ""); 1170SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_filepage, CTLFLAG_RW, 1171 &stat_jwait_filepage, 0, ""); 1172SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_freeblks, CTLFLAG_RW, 1173 &stat_jwait_freeblks, 0, ""); 1174SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_inode, CTLFLAG_RW, 1175 &stat_jwait_inode, 0, ""); 1176SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_newblk, CTLFLAG_RW, 1177 &stat_jwait_newblk, 0, ""); 1178 1179SYSCTL_DECL(_vfs_ffs); 1180 1181LIST_HEAD(bmsafemap_hashhead, bmsafemap) *bmsafemap_hashtbl; 1182static u_long bmsafemap_hash; /* size of hash table - 1 */ 1183 1184static int compute_summary_at_mount = 0; /* Whether to recompute the summary at mount time */ 1185SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW, 1186 &compute_summary_at_mount, 0, "Recompute summary at mount"); 1187 1188static struct proc *softdepproc; 1189static struct kproc_desc softdep_kp = { 1190 "softdepflush", 1191 softdep_flush, 1192 &softdepproc 1193}; 1194SYSINIT(sdproc, SI_SUB_KTHREAD_UPDATE, SI_ORDER_ANY, kproc_start, 1195 &softdep_kp); 1196 1197static void 1198softdep_flush(void) 1199{ 1200 struct mount *nmp; 1201 struct mount *mp; 1202 struct ufsmount *ump; 1203 struct thread *td; 1204 int remaining; 1205 int progress; 1206 int vfslocked; 1207 1208 td = curthread; 1209 td->td_pflags |= TDP_NORUNNINGBUF; 1210 1211 for (;;) { 1212 kproc_suspend_check(softdepproc); 1213 vfslocked = VFS_LOCK_GIANT((struct mount *)NULL); 1214 ACQUIRE_LOCK(&lk); 1215 /* 1216 * If requested, try removing inode or removal dependencies. 1217 */ 1218 if (req_clear_inodedeps) { 1219 clear_inodedeps(td); 1220 req_clear_inodedeps -= 1; 1221 wakeup_one(&proc_waiting); 1222 } 1223 if (req_clear_remove) { 1224 clear_remove(td); 1225 req_clear_remove -= 1; 1226 wakeup_one(&proc_waiting); 1227 } 1228 FREE_LOCK(&lk); 1229 VFS_UNLOCK_GIANT(vfslocked); 1230 remaining = progress = 0; 1231 mtx_lock(&mountlist_mtx); 1232 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 1233 nmp = TAILQ_NEXT(mp, mnt_list); 1234 if ((mp->mnt_flag & MNT_SOFTDEP) == 0) 1235 continue; 1236 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) 1237 continue; 1238 vfslocked = VFS_LOCK_GIANT(mp); 1239 progress += softdep_process_worklist(mp, 0); 1240 ump = VFSTOUFS(mp); 1241 remaining += ump->softdep_on_worklist - 1242 ump->softdep_on_worklist_inprogress; 1243 VFS_UNLOCK_GIANT(vfslocked); 1244 mtx_lock(&mountlist_mtx); 1245 nmp = TAILQ_NEXT(mp, mnt_list); 1246 vfs_unbusy(mp); 1247 } 1248 mtx_unlock(&mountlist_mtx); 1249 if (remaining && progress) 1250 continue; 1251 ACQUIRE_LOCK(&lk); 1252 if (!req_pending) 1253 msleep(&req_pending, &lk, PVM, "sdflush", hz); 1254 req_pending = 0; 1255 FREE_LOCK(&lk); 1256 } 1257} 1258 1259static void 1260worklist_speedup(void) 1261{ 1262 mtx_assert(&lk, MA_OWNED); 1263 if (req_pending == 0) { 1264 req_pending = 1; 1265 wakeup(&req_pending); 1266 } 1267} 1268 1269static int 1270softdep_speedup(void) 1271{ 1272 1273 worklist_speedup(); 1274 bd_speedup(); 1275 return speedup_syncer(); 1276} 1277 1278/* 1279 * Add an item to the end of the work queue. 1280 * This routine requires that the lock be held. 1281 * This is the only routine that adds items to the list. 1282 * The following routine is the only one that removes items 1283 * and does so in order from first to last. 1284 */ 1285static void 1286add_to_worklist(wk, nodelay) 1287 struct worklist *wk; 1288 int nodelay; 1289{ 1290 struct ufsmount *ump; 1291 1292 mtx_assert(&lk, MA_OWNED); 1293 ump = VFSTOUFS(wk->wk_mp); 1294 if (wk->wk_state & ONWORKLIST) 1295 panic("add_to_worklist: %s(0x%X) already on list", 1296 TYPENAME(wk->wk_type), wk->wk_state); 1297 wk->wk_state |= ONWORKLIST; 1298 if (LIST_EMPTY(&ump->softdep_workitem_pending)) 1299 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1300 else 1301 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list); 1302 ump->softdep_worklist_tail = wk; 1303 ump->softdep_on_worklist += 1; 1304 if (nodelay) 1305 worklist_speedup(); 1306} 1307 1308/* 1309 * Remove the item to be processed. If we are removing the last 1310 * item on the list, we need to recalculate the tail pointer. 1311 */ 1312static void 1313remove_from_worklist(wk) 1314 struct worklist *wk; 1315{ 1316 struct ufsmount *ump; 1317 struct worklist *wkend; 1318 1319 ump = VFSTOUFS(wk->wk_mp); 1320 WORKLIST_REMOVE(wk); 1321 if (wk == ump->softdep_worklist_tail) { 1322 LIST_FOREACH(wkend, &ump->softdep_workitem_pending, wk_list) 1323 if (LIST_NEXT(wkend, wk_list) == NULL) 1324 break; 1325 ump->softdep_worklist_tail = wkend; 1326 } 1327 ump->softdep_on_worklist -= 1; 1328} 1329 1330/* 1331 * Process that runs once per second to handle items in the background queue. 1332 * 1333 * Note that we ensure that everything is done in the order in which they 1334 * appear in the queue. The code below depends on this property to ensure 1335 * that blocks of a file are freed before the inode itself is freed. This 1336 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated 1337 * until all the old ones have been purged from the dependency lists. 1338 */ 1339int 1340softdep_process_worklist(mp, full) 1341 struct mount *mp; 1342 int full; 1343{ 1344 struct thread *td = curthread; 1345 int cnt, matchcnt, loopcount; 1346 struct ufsmount *ump; 1347 long starttime; 1348 1349 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp")); 1350 /* 1351 * Record the process identifier of our caller so that we can give 1352 * this process preferential treatment in request_cleanup below. 1353 */ 1354 matchcnt = 0; 1355 ump = VFSTOUFS(mp); 1356 ACQUIRE_LOCK(&lk); 1357 loopcount = 1; 1358 starttime = time_second; 1359 softdep_process_journal(mp, full?MNT_WAIT:0); 1360 while (ump->softdep_on_worklist > 0) { 1361 if ((cnt = process_worklist_item(mp, LK_NOWAIT)) == -1) 1362 break; 1363 else 1364 matchcnt += cnt; 1365 /* 1366 * If requested, try removing inode or removal dependencies. 1367 */ 1368 if (req_clear_inodedeps) { 1369 clear_inodedeps(td); 1370 req_clear_inodedeps -= 1; 1371 wakeup_one(&proc_waiting); 1372 } 1373 if (req_clear_remove) { 1374 clear_remove(td); 1375 req_clear_remove -= 1; 1376 wakeup_one(&proc_waiting); 1377 } 1378 /* 1379 * We do not generally want to stop for buffer space, but if 1380 * we are really being a buffer hog, we will stop and wait. 1381 */ 1382 if (loopcount++ % 128 == 0) { 1383 FREE_LOCK(&lk); 1384 uio_yield(); 1385 bwillwrite(); 1386 ACQUIRE_LOCK(&lk); 1387 } 1388 /* 1389 * Never allow processing to run for more than one 1390 * second. Otherwise the other mountpoints may get 1391 * excessively backlogged. 1392 */ 1393 if (!full && starttime != time_second) 1394 break; 1395 } 1396 if (full == 0) 1397 journal_unsuspend(ump); 1398 FREE_LOCK(&lk); 1399 return (matchcnt); 1400} 1401 1402/* 1403 * Process all removes associated with a vnode if we are running out of 1404 * journal space. Any other process which attempts to flush these will 1405 * be unable as we have the vnodes locked. 1406 */ 1407static void 1408process_removes(vp) 1409 struct vnode *vp; 1410{ 1411 struct inodedep *inodedep; 1412 struct dirrem *dirrem; 1413 struct mount *mp; 1414 ino_t inum; 1415 1416 mtx_assert(&lk, MA_OWNED); 1417 1418 mp = vp->v_mount; 1419 inum = VTOI(vp)->i_number; 1420 for (;;) { 1421 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1422 return; 1423 LIST_FOREACH(dirrem, &inodedep->id_dirremhd, dm_inonext) 1424 if ((dirrem->dm_state & (COMPLETE | ONWORKLIST)) == 1425 (COMPLETE | ONWORKLIST)) 1426 break; 1427 if (dirrem == NULL) 1428 return; 1429 /* 1430 * If another thread is trying to lock this vnode it will 1431 * fail but we must wait for it to do so before we can 1432 * proceed. 1433 */ 1434 if (dirrem->dm_state & INPROGRESS) { 1435 dirrem->dm_state |= IOWAITING; 1436 msleep(&dirrem->dm_list, &lk, PVM, "pwrwait", 0); 1437 continue; 1438 } 1439 remove_from_worklist(&dirrem->dm_list); 1440 FREE_LOCK(&lk); 1441 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1442 panic("process_removes: suspended filesystem"); 1443 handle_workitem_remove(dirrem, vp); 1444 vn_finished_secondary_write(mp); 1445 ACQUIRE_LOCK(&lk); 1446 } 1447} 1448 1449/* 1450 * Process one item on the worklist. 1451 */ 1452static int 1453process_worklist_item(mp, flags) 1454 struct mount *mp; 1455 int flags; 1456{ 1457 struct worklist *wk; 1458 struct ufsmount *ump; 1459 struct vnode *vp; 1460 int matchcnt = 0; 1461 1462 mtx_assert(&lk, MA_OWNED); 1463 KASSERT(mp != NULL, ("process_worklist_item: NULL mp")); 1464 /* 1465 * If we are being called because of a process doing a 1466 * copy-on-write, then it is not safe to write as we may 1467 * recurse into the copy-on-write routine. 1468 */ 1469 if (curthread->td_pflags & TDP_COWINPROGRESS) 1470 return (-1); 1471 /* 1472 * Normally we just process each item on the worklist in order. 1473 * However, if we are in a situation where we cannot lock any 1474 * inodes, we have to skip over any dirrem requests whose 1475 * vnodes are resident and locked. 1476 */ 1477 vp = NULL; 1478 ump = VFSTOUFS(mp); 1479 LIST_FOREACH(wk, &ump->softdep_workitem_pending, wk_list) { 1480 if (wk->wk_state & INPROGRESS) 1481 continue; 1482 if ((flags & LK_NOWAIT) == 0 || wk->wk_type != D_DIRREM) 1483 break; 1484 wk->wk_state |= INPROGRESS; 1485 ump->softdep_on_worklist_inprogress++; 1486 FREE_LOCK(&lk); 1487 ffs_vgetf(mp, WK_DIRREM(wk)->dm_oldinum, 1488 LK_NOWAIT | LK_EXCLUSIVE, &vp, FFSV_FORCEINSMQ); 1489 ACQUIRE_LOCK(&lk); 1490 if (wk->wk_state & IOWAITING) { 1491 wk->wk_state &= ~IOWAITING; 1492 wakeup(wk); 1493 } 1494 wk->wk_state &= ~INPROGRESS; 1495 ump->softdep_on_worklist_inprogress--; 1496 if (vp != NULL) 1497 break; 1498 } 1499 if (wk == 0) 1500 return (-1); 1501 remove_from_worklist(wk); 1502 FREE_LOCK(&lk); 1503 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1504 panic("process_worklist_item: suspended filesystem"); 1505 matchcnt++; 1506 switch (wk->wk_type) { 1507 1508 case D_DIRREM: 1509 /* removal of a directory entry */ 1510 handle_workitem_remove(WK_DIRREM(wk), vp); 1511 if (vp) 1512 vput(vp); 1513 break; 1514 1515 case D_FREEBLKS: 1516 /* releasing blocks and/or fragments from a file */ 1517 handle_workitem_freeblocks(WK_FREEBLKS(wk), flags & LK_NOWAIT); 1518 break; 1519 1520 case D_FREEFRAG: 1521 /* releasing a fragment when replaced as a file grows */ 1522 handle_workitem_freefrag(WK_FREEFRAG(wk)); 1523 break; 1524 1525 case D_FREEFILE: 1526 /* releasing an inode when its link count drops to 0 */ 1527 handle_workitem_freefile(WK_FREEFILE(wk)); 1528 break; 1529 1530 case D_FREEWORK: 1531 /* Final block in an indirect was freed. */ 1532 handle_workitem_indirblk(WK_FREEWORK(wk)); 1533 break; 1534 1535 default: 1536 panic("%s_process_worklist: Unknown type %s", 1537 "softdep", TYPENAME(wk->wk_type)); 1538 /* NOTREACHED */ 1539 } 1540 vn_finished_secondary_write(mp); 1541 ACQUIRE_LOCK(&lk); 1542 return (matchcnt); 1543} 1544 1545/* 1546 * Move dependencies from one buffer to another. 1547 */ 1548int 1549softdep_move_dependencies(oldbp, newbp) 1550 struct buf *oldbp; 1551 struct buf *newbp; 1552{ 1553 struct worklist *wk, *wktail; 1554 int dirty; 1555 1556 dirty = 0; 1557 wktail = NULL; 1558 ACQUIRE_LOCK(&lk); 1559 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) { 1560 LIST_REMOVE(wk, wk_list); 1561 if (wk->wk_type == D_BMSAFEMAP && 1562 bmsafemap_rollbacks(WK_BMSAFEMAP(wk))) 1563 dirty = 1; 1564 if (wktail == 0) 1565 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list); 1566 else 1567 LIST_INSERT_AFTER(wktail, wk, wk_list); 1568 wktail = wk; 1569 } 1570 FREE_LOCK(&lk); 1571 1572 return (dirty); 1573} 1574 1575/* 1576 * Purge the work list of all items associated with a particular mount point. 1577 */ 1578int 1579softdep_flushworklist(oldmnt, countp, td) 1580 struct mount *oldmnt; 1581 int *countp; 1582 struct thread *td; 1583{ 1584 struct vnode *devvp; 1585 int count, error = 0; 1586 struct ufsmount *ump; 1587 1588 /* 1589 * Alternately flush the block device associated with the mount 1590 * point and process any dependencies that the flushing 1591 * creates. We continue until no more worklist dependencies 1592 * are found. 1593 */ 1594 *countp = 0; 1595 ump = VFSTOUFS(oldmnt); 1596 devvp = ump->um_devvp; 1597 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) { 1598 *countp += count; 1599 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1600 error = VOP_FSYNC(devvp, MNT_WAIT, td); 1601 VOP_UNLOCK(devvp, 0); 1602 if (error) 1603 break; 1604 } 1605 return (error); 1606} 1607 1608int 1609softdep_waitidle(struct mount *mp) 1610{ 1611 struct ufsmount *ump; 1612 int error; 1613 int i; 1614 1615 ump = VFSTOUFS(mp); 1616 ACQUIRE_LOCK(&lk); 1617 for (i = 0; i < 10 && ump->softdep_deps; i++) { 1618 ump->softdep_req = 1; 1619 if (ump->softdep_on_worklist) 1620 panic("softdep_waitidle: work added after flush."); 1621 msleep(&ump->softdep_deps, &lk, PVM, "softdeps", 1); 1622 } 1623 ump->softdep_req = 0; 1624 FREE_LOCK(&lk); 1625 error = 0; 1626 if (i == 10) { 1627 error = EBUSY; 1628 printf("softdep_waitidle: Failed to flush worklist for %p\n", 1629 mp); 1630 } 1631 1632 return (error); 1633} 1634 1635/* 1636 * Flush all vnodes and worklist items associated with a specified mount point. 1637 */ 1638int 1639softdep_flushfiles(oldmnt, flags, td) 1640 struct mount *oldmnt; 1641 int flags; 1642 struct thread *td; 1643{ 1644 int error, depcount, loopcnt, retry_flush_count, retry; 1645 1646 loopcnt = 10; 1647 retry_flush_count = 3; 1648retry_flush: 1649 error = 0; 1650 1651 /* 1652 * Alternately flush the vnodes associated with the mount 1653 * point and process any dependencies that the flushing 1654 * creates. In theory, this loop can happen at most twice, 1655 * but we give it a few extra just to be sure. 1656 */ 1657 for (; loopcnt > 0; loopcnt--) { 1658 /* 1659 * Do another flush in case any vnodes were brought in 1660 * as part of the cleanup operations. 1661 */ 1662 if ((error = ffs_flushfiles(oldmnt, flags, td)) != 0) 1663 break; 1664 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 || 1665 depcount == 0) 1666 break; 1667 } 1668 /* 1669 * If we are unmounting then it is an error to fail. If we 1670 * are simply trying to downgrade to read-only, then filesystem 1671 * activity can keep us busy forever, so we just fail with EBUSY. 1672 */ 1673 if (loopcnt == 0) { 1674 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) 1675 panic("softdep_flushfiles: looping"); 1676 error = EBUSY; 1677 } 1678 if (!error) 1679 error = softdep_waitidle(oldmnt); 1680 if (!error) { 1681 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) { 1682 retry = 0; 1683 MNT_ILOCK(oldmnt); 1684 KASSERT((oldmnt->mnt_kern_flag & MNTK_NOINSMNTQ) != 0, 1685 ("softdep_flushfiles: !MNTK_NOINSMNTQ")); 1686 if (oldmnt->mnt_nvnodelistsize > 0) { 1687 if (--retry_flush_count > 0) { 1688 retry = 1; 1689 loopcnt = 3; 1690 } else 1691 error = EBUSY; 1692 } 1693 MNT_IUNLOCK(oldmnt); 1694 if (retry) 1695 goto retry_flush; 1696 } 1697 } 1698 return (error); 1699} 1700 1701/* 1702 * Structure hashing. 1703 * 1704 * There are three types of structures that can be looked up: 1705 * 1) pagedep structures identified by mount point, inode number, 1706 * and logical block. 1707 * 2) inodedep structures identified by mount point and inode number. 1708 * 3) newblk structures identified by mount point and 1709 * physical block number. 1710 * 1711 * The "pagedep" and "inodedep" dependency structures are hashed 1712 * separately from the file blocks and inodes to which they correspond. 1713 * This separation helps when the in-memory copy of an inode or 1714 * file block must be replaced. It also obviates the need to access 1715 * an inode or file page when simply updating (or de-allocating) 1716 * dependency structures. Lookup of newblk structures is needed to 1717 * find newly allocated blocks when trying to associate them with 1718 * their allocdirect or allocindir structure. 1719 * 1720 * The lookup routines optionally create and hash a new instance when 1721 * an existing entry is not found. 1722 */ 1723#define DEPALLOC 0x0001 /* allocate structure if lookup fails */ 1724#define NODELAY 0x0002 /* cannot do background work */ 1725 1726/* 1727 * Structures and routines associated with pagedep caching. 1728 */ 1729LIST_HEAD(pagedep_hashhead, pagedep) *pagedep_hashtbl; 1730u_long pagedep_hash; /* size of hash table - 1 */ 1731#define PAGEDEP_HASH(mp, inum, lbn) \ 1732 (&pagedep_hashtbl[((((register_t)(mp)) >> 13) + (inum) + (lbn)) & \ 1733 pagedep_hash]) 1734 1735static int 1736pagedep_find(pagedephd, ino, lbn, mp, flags, pagedeppp) 1737 struct pagedep_hashhead *pagedephd; 1738 ino_t ino; 1739 ufs_lbn_t lbn; 1740 struct mount *mp; 1741 int flags; 1742 struct pagedep **pagedeppp; 1743{ 1744 struct pagedep *pagedep; 1745 1746 LIST_FOREACH(pagedep, pagedephd, pd_hash) 1747 if (ino == pagedep->pd_ino && 1748 lbn == pagedep->pd_lbn && 1749 mp == pagedep->pd_list.wk_mp) 1750 break; 1751 if (pagedep) { 1752 *pagedeppp = pagedep; 1753 if ((flags & DEPALLOC) != 0 && 1754 (pagedep->pd_state & ONWORKLIST) == 0) 1755 return (0); 1756 return (1); 1757 } 1758 *pagedeppp = NULL; 1759 return (0); 1760} 1761/* 1762 * Look up a pagedep. Return 1 if found, 0 if not found or found 1763 * when asked to allocate but not associated with any buffer. 1764 * If not found, allocate if DEPALLOC flag is passed. 1765 * Found or allocated entry is returned in pagedeppp. 1766 * This routine must be called with splbio interrupts blocked. 1767 */ 1768static int 1769pagedep_lookup(mp, ino, lbn, flags, pagedeppp) 1770 struct mount *mp; 1771 ino_t ino; 1772 ufs_lbn_t lbn; 1773 int flags; 1774 struct pagedep **pagedeppp; 1775{ 1776 struct pagedep *pagedep; 1777 struct pagedep_hashhead *pagedephd; 1778 int ret; 1779 int i; 1780 1781 mtx_assert(&lk, MA_OWNED); 1782 pagedephd = PAGEDEP_HASH(mp, ino, lbn); 1783 1784 ret = pagedep_find(pagedephd, ino, lbn, mp, flags, pagedeppp); 1785 if (*pagedeppp || (flags & DEPALLOC) == 0) 1786 return (ret); 1787 FREE_LOCK(&lk); 1788 pagedep = malloc(sizeof(struct pagedep), 1789 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO); 1790 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp); 1791 ACQUIRE_LOCK(&lk); 1792 ret = pagedep_find(pagedephd, ino, lbn, mp, flags, pagedeppp); 1793 if (*pagedeppp) { 1794 WORKITEM_FREE(pagedep, D_PAGEDEP); 1795 return (ret); 1796 } 1797 pagedep->pd_ino = ino; 1798 pagedep->pd_lbn = lbn; 1799 LIST_INIT(&pagedep->pd_dirremhd); 1800 LIST_INIT(&pagedep->pd_pendinghd); 1801 for (i = 0; i < DAHASHSZ; i++) 1802 LIST_INIT(&pagedep->pd_diraddhd[i]); 1803 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash); 1804 *pagedeppp = pagedep; 1805 return (0); 1806} 1807 1808/* 1809 * Structures and routines associated with inodedep caching. 1810 */ 1811LIST_HEAD(inodedep_hashhead, inodedep) *inodedep_hashtbl; 1812static u_long inodedep_hash; /* size of hash table - 1 */ 1813static long num_inodedep; /* number of inodedep allocated */ 1814#define INODEDEP_HASH(fs, inum) \ 1815 (&inodedep_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & inodedep_hash]) 1816 1817static int 1818inodedep_find(inodedephd, fs, inum, inodedeppp) 1819 struct inodedep_hashhead *inodedephd; 1820 struct fs *fs; 1821 ino_t inum; 1822 struct inodedep **inodedeppp; 1823{ 1824 struct inodedep *inodedep; 1825 1826 LIST_FOREACH(inodedep, inodedephd, id_hash) 1827 if (inum == inodedep->id_ino && fs == inodedep->id_fs) 1828 break; 1829 if (inodedep) { 1830 *inodedeppp = inodedep; 1831 return (1); 1832 } 1833 *inodedeppp = NULL; 1834 1835 return (0); 1836} 1837/* 1838 * Look up an inodedep. Return 1 if found, 0 if not found. 1839 * If not found, allocate if DEPALLOC flag is passed. 1840 * Found or allocated entry is returned in inodedeppp. 1841 * This routine must be called with splbio interrupts blocked. 1842 */ 1843static int 1844inodedep_lookup(mp, inum, flags, inodedeppp) 1845 struct mount *mp; 1846 ino_t inum; 1847 int flags; 1848 struct inodedep **inodedeppp; 1849{ 1850 struct inodedep *inodedep; 1851 struct inodedep_hashhead *inodedephd; 1852 struct fs *fs; 1853 1854 mtx_assert(&lk, MA_OWNED); 1855 fs = VFSTOUFS(mp)->um_fs; 1856 inodedephd = INODEDEP_HASH(fs, inum); 1857 1858 if (inodedep_find(inodedephd, fs, inum, inodedeppp)) 1859 return (1); 1860 if ((flags & DEPALLOC) == 0) 1861 return (0); 1862 /* 1863 * If we are over our limit, try to improve the situation. 1864 */ 1865 if (num_inodedep > max_softdeps && (flags & NODELAY) == 0) 1866 request_cleanup(mp, FLUSH_INODES); 1867 FREE_LOCK(&lk); 1868 inodedep = malloc(sizeof(struct inodedep), 1869 M_INODEDEP, M_SOFTDEP_FLAGS); 1870 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp); 1871 ACQUIRE_LOCK(&lk); 1872 if (inodedep_find(inodedephd, fs, inum, inodedeppp)) { 1873 WORKITEM_FREE(inodedep, D_INODEDEP); 1874 return (1); 1875 } 1876 num_inodedep += 1; 1877 inodedep->id_fs = fs; 1878 inodedep->id_ino = inum; 1879 inodedep->id_state = ALLCOMPLETE; 1880 inodedep->id_nlinkdelta = 0; 1881 inodedep->id_savedino1 = NULL; 1882 inodedep->id_savedsize = -1; 1883 inodedep->id_savedextsize = -1; 1884 inodedep->id_savednlink = -1; 1885 inodedep->id_bmsafemap = NULL; 1886 inodedep->id_mkdiradd = NULL; 1887 LIST_INIT(&inodedep->id_dirremhd); 1888 LIST_INIT(&inodedep->id_pendinghd); 1889 LIST_INIT(&inodedep->id_inowait); 1890 LIST_INIT(&inodedep->id_bufwait); 1891 TAILQ_INIT(&inodedep->id_inoreflst); 1892 TAILQ_INIT(&inodedep->id_inoupdt); 1893 TAILQ_INIT(&inodedep->id_newinoupdt); 1894 TAILQ_INIT(&inodedep->id_extupdt); 1895 TAILQ_INIT(&inodedep->id_newextupdt); 1896 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash); 1897 *inodedeppp = inodedep; 1898 return (0); 1899} 1900 1901/* 1902 * Structures and routines associated with newblk caching. 1903 */ 1904LIST_HEAD(newblk_hashhead, newblk) *newblk_hashtbl; 1905u_long newblk_hash; /* size of hash table - 1 */ 1906#define NEWBLK_HASH(fs, inum) \ 1907 (&newblk_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & newblk_hash]) 1908 1909static int 1910newblk_find(newblkhd, mp, newblkno, flags, newblkpp) 1911 struct newblk_hashhead *newblkhd; 1912 struct mount *mp; 1913 ufs2_daddr_t newblkno; 1914 int flags; 1915 struct newblk **newblkpp; 1916{ 1917 struct newblk *newblk; 1918 1919 LIST_FOREACH(newblk, newblkhd, nb_hash) { 1920 if (newblkno != newblk->nb_newblkno) 1921 continue; 1922 if (mp != newblk->nb_list.wk_mp) 1923 continue; 1924 /* 1925 * If we're creating a new dependency don't match those that 1926 * have already been converted to allocdirects. This is for 1927 * a frag extend. 1928 */ 1929 if ((flags & DEPALLOC) && newblk->nb_list.wk_type != D_NEWBLK) 1930 continue; 1931 break; 1932 } 1933 if (newblk) { 1934 *newblkpp = newblk; 1935 return (1); 1936 } 1937 *newblkpp = NULL; 1938 return (0); 1939} 1940 1941/* 1942 * Look up a newblk. Return 1 if found, 0 if not found. 1943 * If not found, allocate if DEPALLOC flag is passed. 1944 * Found or allocated entry is returned in newblkpp. 1945 */ 1946static int 1947newblk_lookup(mp, newblkno, flags, newblkpp) 1948 struct mount *mp; 1949 ufs2_daddr_t newblkno; 1950 int flags; 1951 struct newblk **newblkpp; 1952{ 1953 struct newblk *newblk; 1954 struct newblk_hashhead *newblkhd; 1955 1956 newblkhd = NEWBLK_HASH(VFSTOUFS(mp)->um_fs, newblkno); 1957 if (newblk_find(newblkhd, mp, newblkno, flags, newblkpp)) 1958 return (1); 1959 if ((flags & DEPALLOC) == 0) 1960 return (0); 1961 FREE_LOCK(&lk); 1962 newblk = malloc(sizeof(union allblk), M_NEWBLK, 1963 M_SOFTDEP_FLAGS | M_ZERO); 1964 workitem_alloc(&newblk->nb_list, D_NEWBLK, mp); 1965 ACQUIRE_LOCK(&lk); 1966 if (newblk_find(newblkhd, mp, newblkno, flags, newblkpp)) { 1967 WORKITEM_FREE(newblk, D_NEWBLK); 1968 return (1); 1969 } 1970 newblk->nb_freefrag = NULL; 1971 LIST_INIT(&newblk->nb_indirdeps); 1972 LIST_INIT(&newblk->nb_newdirblk); 1973 LIST_INIT(&newblk->nb_jwork); 1974 newblk->nb_state = ATTACHED; 1975 newblk->nb_newblkno = newblkno; 1976 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash); 1977 *newblkpp = newblk; 1978 return (0); 1979} 1980 1981/* 1982 * Executed during filesystem system initialization before 1983 * mounting any filesystems. 1984 */ 1985void 1986softdep_initialize() 1987{ 1988 1989 LIST_INIT(&mkdirlisthd); 1990 max_softdeps = desiredvnodes * 4; 1991 pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP, &pagedep_hash); 1992 inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP, &inodedep_hash); 1993 newblk_hashtbl = hashinit(desiredvnodes / 5, M_NEWBLK, &newblk_hash); 1994 bmsafemap_hashtbl = hashinit(1024, M_BMSAFEMAP, &bmsafemap_hash); 1995 1996 /* initialise bioops hack */ 1997 bioops.io_start = softdep_disk_io_initiation; 1998 bioops.io_complete = softdep_disk_write_complete; 1999 bioops.io_deallocate = softdep_deallocate_dependencies; 2000 bioops.io_countdeps = softdep_count_dependencies; 2001 2002 /* Initialize the callout with an mtx. */ 2003 callout_init_mtx(&softdep_callout, &lk, 0); 2004} 2005 2006/* 2007 * Executed after all filesystems have been unmounted during 2008 * filesystem module unload. 2009 */ 2010void 2011softdep_uninitialize() 2012{ 2013 2014 callout_drain(&softdep_callout); 2015 hashdestroy(pagedep_hashtbl, M_PAGEDEP, pagedep_hash); 2016 hashdestroy(inodedep_hashtbl, M_INODEDEP, inodedep_hash); 2017 hashdestroy(newblk_hashtbl, M_NEWBLK, newblk_hash); 2018 hashdestroy(bmsafemap_hashtbl, M_BMSAFEMAP, bmsafemap_hash); 2019} 2020 2021/* 2022 * Called at mount time to notify the dependency code that a 2023 * filesystem wishes to use it. 2024 */ 2025int 2026softdep_mount(devvp, mp, fs, cred) 2027 struct vnode *devvp; 2028 struct mount *mp; 2029 struct fs *fs; 2030 struct ucred *cred; 2031{ 2032 struct csum_total cstotal; 2033 struct ufsmount *ump; 2034 struct cg *cgp; 2035 struct buf *bp; 2036 int error, cyl; 2037 2038 MNT_ILOCK(mp); 2039 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP; 2040 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) { 2041 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) | 2042 MNTK_SOFTDEP; 2043 mp->mnt_noasync++; 2044 } 2045 MNT_IUNLOCK(mp); 2046 ump = VFSTOUFS(mp); 2047 LIST_INIT(&ump->softdep_workitem_pending); 2048 LIST_INIT(&ump->softdep_journal_pending); 2049 TAILQ_INIT(&ump->softdep_unlinked); 2050 ump->softdep_worklist_tail = NULL; 2051 ump->softdep_on_worklist = 0; 2052 ump->softdep_deps = 0; 2053 if ((fs->fs_flags & FS_SUJ) && 2054 (error = journal_mount(mp, fs, cred)) != 0) { 2055 printf("Failed to start journal: %d\n", error); 2056 return (error); 2057 } 2058 /* 2059 * When doing soft updates, the counters in the 2060 * superblock may have gotten out of sync. Recomputation 2061 * can take a long time and can be deferred for background 2062 * fsck. However, the old behavior of scanning the cylinder 2063 * groups and recalculating them at mount time is available 2064 * by setting vfs.ffs.compute_summary_at_mount to one. 2065 */ 2066 if (compute_summary_at_mount == 0 || fs->fs_clean != 0) 2067 return (0); 2068 bzero(&cstotal, sizeof cstotal); 2069 for (cyl = 0; cyl < fs->fs_ncg; cyl++) { 2070 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)), 2071 fs->fs_cgsize, cred, &bp)) != 0) { 2072 brelse(bp); 2073 return (error); 2074 } 2075 cgp = (struct cg *)bp->b_data; 2076 cstotal.cs_nffree += cgp->cg_cs.cs_nffree; 2077 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree; 2078 cstotal.cs_nifree += cgp->cg_cs.cs_nifree; 2079 cstotal.cs_ndir += cgp->cg_cs.cs_ndir; 2080 fs->fs_cs(fs, cyl) = cgp->cg_cs; 2081 brelse(bp); 2082 } 2083#ifdef DEBUG 2084 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal)) 2085 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt); 2086#endif 2087 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal); 2088 return (0); 2089} 2090 2091void 2092softdep_unmount(mp) 2093 struct mount *mp; 2094{ 2095 2096 if (mp->mnt_kern_flag & MNTK_SUJ) 2097 journal_unmount(mp); 2098} 2099 2100struct jblocks { 2101 struct jseglst jb_segs; /* TAILQ of current segments. */ 2102 struct jseg *jb_writeseg; /* Next write to complete. */ 2103 struct jextent *jb_extent; /* Extent array. */ 2104 uint64_t jb_nextseq; /* Next sequence number. */ 2105 uint64_t jb_oldestseq; /* Oldest active sequence number. */ 2106 int jb_avail; /* Available extents. */ 2107 int jb_used; /* Last used extent. */ 2108 int jb_head; /* Allocator head. */ 2109 int jb_off; /* Allocator extent offset. */ 2110 int jb_blocks; /* Total disk blocks covered. */ 2111 int jb_free; /* Total disk blocks free. */ 2112 int jb_min; /* Minimum free space. */ 2113 int jb_low; /* Low on space. */ 2114 int jb_age; /* Insertion time of oldest rec. */ 2115 int jb_suspended; /* Did journal suspend writes? */ 2116}; 2117 2118struct jextent { 2119 ufs2_daddr_t je_daddr; /* Disk block address. */ 2120 int je_blocks; /* Disk block count. */ 2121}; 2122 2123static struct jblocks * 2124jblocks_create(void) 2125{ 2126 struct jblocks *jblocks; 2127 2128 jblocks = malloc(sizeof(*jblocks), M_JBLOCKS, M_WAITOK | M_ZERO); 2129 TAILQ_INIT(&jblocks->jb_segs); 2130 jblocks->jb_avail = 10; 2131 jblocks->jb_extent = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2132 M_JBLOCKS, M_WAITOK | M_ZERO); 2133 2134 return (jblocks); 2135} 2136 2137static ufs2_daddr_t 2138jblocks_alloc(jblocks, bytes, actual) 2139 struct jblocks *jblocks; 2140 int bytes; 2141 int *actual; 2142{ 2143 ufs2_daddr_t daddr; 2144 struct jextent *jext; 2145 int freecnt; 2146 int blocks; 2147 2148 blocks = bytes / DEV_BSIZE; 2149 jext = &jblocks->jb_extent[jblocks->jb_head]; 2150 freecnt = jext->je_blocks - jblocks->jb_off; 2151 if (freecnt == 0) { 2152 jblocks->jb_off = 0; 2153 if (++jblocks->jb_head > jblocks->jb_used) 2154 jblocks->jb_head = 0; 2155 jext = &jblocks->jb_extent[jblocks->jb_head]; 2156 freecnt = jext->je_blocks; 2157 } 2158 if (freecnt > blocks) 2159 freecnt = blocks; 2160 *actual = freecnt * DEV_BSIZE; 2161 daddr = jext->je_daddr + jblocks->jb_off; 2162 jblocks->jb_off += freecnt; 2163 jblocks->jb_free -= freecnt; 2164 2165 return (daddr); 2166} 2167 2168static void 2169jblocks_free(jblocks, mp, bytes) 2170 struct jblocks *jblocks; 2171 struct mount *mp; 2172 int bytes; 2173{ 2174 2175 jblocks->jb_free += bytes / DEV_BSIZE; 2176 if (jblocks->jb_suspended) 2177 worklist_speedup(); 2178 wakeup(jblocks); 2179} 2180 2181static void 2182jblocks_destroy(jblocks) 2183 struct jblocks *jblocks; 2184{ 2185 2186 if (jblocks->jb_extent) 2187 free(jblocks->jb_extent, M_JBLOCKS); 2188 free(jblocks, M_JBLOCKS); 2189} 2190 2191static void 2192jblocks_add(jblocks, daddr, blocks) 2193 struct jblocks *jblocks; 2194 ufs2_daddr_t daddr; 2195 int blocks; 2196{ 2197 struct jextent *jext; 2198 2199 jblocks->jb_blocks += blocks; 2200 jblocks->jb_free += blocks; 2201 jext = &jblocks->jb_extent[jblocks->jb_used]; 2202 /* Adding the first block. */ 2203 if (jext->je_daddr == 0) { 2204 jext->je_daddr = daddr; 2205 jext->je_blocks = blocks; 2206 return; 2207 } 2208 /* Extending the last extent. */ 2209 if (jext->je_daddr + jext->je_blocks == daddr) { 2210 jext->je_blocks += blocks; 2211 return; 2212 } 2213 /* Adding a new extent. */ 2214 if (++jblocks->jb_used == jblocks->jb_avail) { 2215 jblocks->jb_avail *= 2; 2216 jext = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2217 M_JBLOCKS, M_WAITOK | M_ZERO); 2218 memcpy(jext, jblocks->jb_extent, 2219 sizeof(struct jextent) * jblocks->jb_used); 2220 free(jblocks->jb_extent, M_JBLOCKS); 2221 jblocks->jb_extent = jext; 2222 } 2223 jext = &jblocks->jb_extent[jblocks->jb_used]; 2224 jext->je_daddr = daddr; 2225 jext->je_blocks = blocks; 2226 return; 2227} 2228 2229int 2230softdep_journal_lookup(mp, vpp) 2231 struct mount *mp; 2232 struct vnode **vpp; 2233{ 2234 struct componentname cnp; 2235 struct vnode *dvp; 2236 ino_t sujournal; 2237 int error; 2238 2239 error = VFS_VGET(mp, ROOTINO, LK_EXCLUSIVE, &dvp); 2240 if (error) 2241 return (error); 2242 bzero(&cnp, sizeof(cnp)); 2243 cnp.cn_nameiop = LOOKUP; 2244 cnp.cn_flags = ISLASTCN; 2245 cnp.cn_thread = curthread; 2246 cnp.cn_cred = curthread->td_ucred; 2247 cnp.cn_pnbuf = SUJ_FILE; 2248 cnp.cn_nameptr = SUJ_FILE; 2249 cnp.cn_namelen = strlen(SUJ_FILE); 2250 error = ufs_lookup_ino(dvp, NULL, &cnp, &sujournal); 2251 vput(dvp); 2252 if (error != 0) 2253 return (error); 2254 error = VFS_VGET(mp, sujournal, LK_EXCLUSIVE, vpp); 2255 return (error); 2256} 2257 2258/* 2259 * Open and verify the journal file. 2260 */ 2261static int 2262journal_mount(mp, fs, cred) 2263 struct mount *mp; 2264 struct fs *fs; 2265 struct ucred *cred; 2266{ 2267 struct jblocks *jblocks; 2268 struct vnode *vp; 2269 struct inode *ip; 2270 ufs2_daddr_t blkno; 2271 int bcount; 2272 int error; 2273 int i; 2274 2275 error = softdep_journal_lookup(mp, &vp); 2276 if (error != 0) { 2277 printf("Failed to find journal. Use tunefs to create one\n"); 2278 return (error); 2279 } 2280 ip = VTOI(vp); 2281 if (ip->i_size < SUJ_MIN) { 2282 error = ENOSPC; 2283 goto out; 2284 } 2285 bcount = lblkno(fs, ip->i_size); /* Only use whole blocks. */ 2286 jblocks = jblocks_create(); 2287 for (i = 0; i < bcount; i++) { 2288 error = ufs_bmaparray(vp, i, &blkno, NULL, NULL, NULL); 2289 if (error) 2290 break; 2291 jblocks_add(jblocks, blkno, fsbtodb(fs, fs->fs_frag)); 2292 } 2293 if (error) { 2294 jblocks_destroy(jblocks); 2295 goto out; 2296 } 2297 jblocks->jb_low = jblocks->jb_free / 3; /* Reserve 33%. */ 2298 jblocks->jb_min = jblocks->jb_free / 10; /* Suspend at 10%. */ 2299 VFSTOUFS(mp)->softdep_jblocks = jblocks; 2300out: 2301 if (error == 0) { 2302 MNT_ILOCK(mp); 2303 mp->mnt_kern_flag |= MNTK_SUJ; 2304 MNT_IUNLOCK(mp); 2305 /* 2306 * Only validate the journal contents if the 2307 * filesystem is clean, otherwise we write the logs 2308 * but they'll never be used. If the filesystem was 2309 * still dirty when we mounted it the journal is 2310 * invalid and a new journal can only be valid if it 2311 * starts from a clean mount. 2312 */ 2313 if (fs->fs_clean) { 2314 DIP_SET(ip, i_modrev, fs->fs_mtime); 2315 ip->i_flags |= IN_MODIFIED; 2316 ffs_update(vp, 1); 2317 } 2318 } 2319 vput(vp); 2320 return (error); 2321} 2322 2323static void 2324journal_unmount(mp) 2325 struct mount *mp; 2326{ 2327 struct ufsmount *ump; 2328 2329 ump = VFSTOUFS(mp); 2330 if (ump->softdep_jblocks) 2331 jblocks_destroy(ump->softdep_jblocks); 2332 ump->softdep_jblocks = NULL; 2333} 2334 2335/* 2336 * Called when a journal record is ready to be written. Space is allocated 2337 * and the journal entry is created when the journal is flushed to stable 2338 * store. 2339 */ 2340static void 2341add_to_journal(wk) 2342 struct worklist *wk; 2343{ 2344 struct ufsmount *ump; 2345 2346 mtx_assert(&lk, MA_OWNED); 2347 ump = VFSTOUFS(wk->wk_mp); 2348 if (wk->wk_state & ONWORKLIST) 2349 panic("add_to_journal: %s(0x%X) already on list", 2350 TYPENAME(wk->wk_type), wk->wk_state); 2351 wk->wk_state |= ONWORKLIST | DEPCOMPLETE; 2352 if (LIST_EMPTY(&ump->softdep_journal_pending)) { 2353 ump->softdep_jblocks->jb_age = ticks; 2354 LIST_INSERT_HEAD(&ump->softdep_journal_pending, wk, wk_list); 2355 } else 2356 LIST_INSERT_AFTER(ump->softdep_journal_tail, wk, wk_list); 2357 ump->softdep_journal_tail = wk; 2358 ump->softdep_on_journal += 1; 2359} 2360 2361/* 2362 * Remove an arbitrary item for the journal worklist maintain the tail 2363 * pointer. This happens when a new operation obviates the need to 2364 * journal an old operation. 2365 */ 2366static void 2367remove_from_journal(wk) 2368 struct worklist *wk; 2369{ 2370 struct ufsmount *ump; 2371 2372 mtx_assert(&lk, MA_OWNED); 2373 ump = VFSTOUFS(wk->wk_mp); 2374#ifdef SUJ_DEBUG 2375 { 2376 struct worklist *wkn; 2377 2378 LIST_FOREACH(wkn, &ump->softdep_journal_pending, wk_list) 2379 if (wkn == wk) 2380 break; 2381 if (wkn == NULL) 2382 panic("remove_from_journal: %p is not in journal", wk); 2383 } 2384#endif 2385 /* 2386 * We emulate a TAILQ to save space in most structures which do not 2387 * require TAILQ semantics. Here we must update the tail position 2388 * when removing the tail which is not the final entry. This works 2389 * only if the worklist linkage are at the beginning of the structure. 2390 */ 2391 if (ump->softdep_journal_tail == wk) 2392 ump->softdep_journal_tail = 2393 (struct worklist *)wk->wk_list.le_prev; 2394 2395 WORKLIST_REMOVE(wk); 2396 ump->softdep_on_journal -= 1; 2397} 2398 2399/* 2400 * Check for journal space as well as dependency limits so the prelink 2401 * code can throttle both journaled and non-journaled filesystems. 2402 * Threshold is 0 for low and 1 for min. 2403 */ 2404static int 2405journal_space(ump, thresh) 2406 struct ufsmount *ump; 2407 int thresh; 2408{ 2409 struct jblocks *jblocks; 2410 int avail; 2411 2412 jblocks = ump->softdep_jblocks; 2413 if (jblocks == NULL) 2414 return (1); 2415 /* 2416 * We use a tighter restriction here to prevent request_cleanup() 2417 * running in threads from running into locks we currently hold. 2418 */ 2419 if (num_inodedep > (max_softdeps / 10) * 9) 2420 return (0); 2421 if (thresh) 2422 thresh = jblocks->jb_min; 2423 else 2424 thresh = jblocks->jb_low; 2425 avail = (ump->softdep_on_journal * JREC_SIZE) / DEV_BSIZE; 2426 avail = jblocks->jb_free - avail; 2427 2428 return (avail > thresh); 2429} 2430 2431static void 2432journal_suspend(ump) 2433 struct ufsmount *ump; 2434{ 2435 struct jblocks *jblocks; 2436 struct mount *mp; 2437 2438 mp = UFSTOVFS(ump); 2439 jblocks = ump->softdep_jblocks; 2440 MNT_ILOCK(mp); 2441 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) { 2442 stat_journal_min++; 2443 mp->mnt_kern_flag |= MNTK_SUSPEND; 2444 mp->mnt_susp_owner = FIRST_THREAD_IN_PROC(softdepproc); 2445 } 2446 jblocks->jb_suspended = 1; 2447 MNT_IUNLOCK(mp); 2448} 2449 2450static int 2451journal_unsuspend(struct ufsmount *ump) 2452{ 2453 struct jblocks *jblocks; 2454 struct mount *mp; 2455 2456 mp = UFSTOVFS(ump); 2457 jblocks = ump->softdep_jblocks; 2458 2459 if (jblocks != NULL && jblocks->jb_suspended && 2460 journal_space(ump, jblocks->jb_min)) { 2461 jblocks->jb_suspended = 0; 2462 FREE_LOCK(&lk); 2463 mp->mnt_susp_owner = curthread; 2464 vfs_write_resume(mp); 2465 ACQUIRE_LOCK(&lk); 2466 return (1); 2467 } 2468 return (0); 2469} 2470 2471/* 2472 * Called before any allocation function to be certain that there is 2473 * sufficient space in the journal prior to creating any new records. 2474 * Since in the case of block allocation we may have multiple locked 2475 * buffers at the time of the actual allocation we can not block 2476 * when the journal records are created. Doing so would create a deadlock 2477 * if any of these buffers needed to be flushed to reclaim space. Instead 2478 * we require a sufficiently large amount of available space such that 2479 * each thread in the system could have passed this allocation check and 2480 * still have sufficient free space. With 20% of a minimum journal size 2481 * of 1MB we have 6553 records available. 2482 */ 2483int 2484softdep_prealloc(vp, waitok) 2485 struct vnode *vp; 2486 int waitok; 2487{ 2488 struct ufsmount *ump; 2489 2490 if (DOINGSUJ(vp) == 0) 2491 return (0); 2492 ump = VFSTOUFS(vp->v_mount); 2493 ACQUIRE_LOCK(&lk); 2494 if (journal_space(ump, 0)) { 2495 FREE_LOCK(&lk); 2496 return (0); 2497 } 2498 stat_journal_low++; 2499 FREE_LOCK(&lk); 2500 if (waitok == MNT_NOWAIT) 2501 return (ENOSPC); 2502 /* 2503 * Attempt to sync this vnode once to flush any journal 2504 * work attached to it. 2505 */ 2506 if ((curthread->td_pflags & TDP_COWINPROGRESS) == 0) 2507 ffs_syncvnode(vp, waitok); 2508 ACQUIRE_LOCK(&lk); 2509 process_removes(vp); 2510 if (journal_space(ump, 0) == 0) { 2511 softdep_speedup(); 2512 if (journal_space(ump, 1) == 0) 2513 journal_suspend(ump); 2514 } 2515 FREE_LOCK(&lk); 2516 2517 return (0); 2518} 2519 2520/* 2521 * Before adjusting a link count on a vnode verify that we have sufficient 2522 * journal space. If not, process operations that depend on the currently 2523 * locked pair of vnodes to try to flush space as the syncer, buf daemon, 2524 * and softdep flush threads can not acquire these locks to reclaim space. 2525 */ 2526static void 2527softdep_prelink(dvp, vp) 2528 struct vnode *dvp; 2529 struct vnode *vp; 2530{ 2531 struct ufsmount *ump; 2532 2533 ump = VFSTOUFS(dvp->v_mount); 2534 mtx_assert(&lk, MA_OWNED); 2535 if (journal_space(ump, 0)) 2536 return; 2537 stat_journal_low++; 2538 FREE_LOCK(&lk); 2539 if (vp) 2540 ffs_syncvnode(vp, MNT_NOWAIT); 2541 ffs_syncvnode(dvp, MNT_WAIT); 2542 ACQUIRE_LOCK(&lk); 2543 /* Process vp before dvp as it may create .. removes. */ 2544 if (vp) 2545 process_removes(vp); 2546 process_removes(dvp); 2547 softdep_speedup(); 2548 process_worklist_item(UFSTOVFS(ump), LK_NOWAIT); 2549 process_worklist_item(UFSTOVFS(ump), LK_NOWAIT); 2550 if (journal_space(ump, 0) == 0) { 2551 softdep_speedup(); 2552 if (journal_space(ump, 1) == 0) 2553 journal_suspend(ump); 2554 } 2555} 2556 2557static void 2558jseg_write(fs, jblocks, jseg, data) 2559 struct fs *fs; 2560 struct jblocks *jblocks; 2561 struct jseg *jseg; 2562 uint8_t *data; 2563{ 2564 struct jsegrec *rec; 2565 2566 rec = (struct jsegrec *)data; 2567 rec->jsr_seq = jseg->js_seq; 2568 rec->jsr_oldest = jblocks->jb_oldestseq; 2569 rec->jsr_cnt = jseg->js_cnt; 2570 rec->jsr_blocks = jseg->js_size / DEV_BSIZE; 2571 rec->jsr_crc = 0; 2572 rec->jsr_time = fs->fs_mtime; 2573} 2574 2575static inline void 2576inoref_write(inoref, jseg, rec) 2577 struct inoref *inoref; 2578 struct jseg *jseg; 2579 struct jrefrec *rec; 2580{ 2581 2582 inoref->if_jsegdep->jd_seg = jseg; 2583 rec->jr_ino = inoref->if_ino; 2584 rec->jr_parent = inoref->if_parent; 2585 rec->jr_nlink = inoref->if_nlink; 2586 rec->jr_mode = inoref->if_mode; 2587 rec->jr_diroff = inoref->if_diroff; 2588} 2589 2590static void 2591jaddref_write(jaddref, jseg, data) 2592 struct jaddref *jaddref; 2593 struct jseg *jseg; 2594 uint8_t *data; 2595{ 2596 struct jrefrec *rec; 2597 2598 rec = (struct jrefrec *)data; 2599 rec->jr_op = JOP_ADDREF; 2600 inoref_write(&jaddref->ja_ref, jseg, rec); 2601} 2602 2603static void 2604jremref_write(jremref, jseg, data) 2605 struct jremref *jremref; 2606 struct jseg *jseg; 2607 uint8_t *data; 2608{ 2609 struct jrefrec *rec; 2610 2611 rec = (struct jrefrec *)data; 2612 rec->jr_op = JOP_REMREF; 2613 inoref_write(&jremref->jr_ref, jseg, rec); 2614} 2615 2616static void 2617jmvref_write(jmvref, jseg, data) 2618 struct jmvref *jmvref; 2619 struct jseg *jseg; 2620 uint8_t *data; 2621{ 2622 struct jmvrec *rec; 2623 2624 rec = (struct jmvrec *)data; 2625 rec->jm_op = JOP_MVREF; 2626 rec->jm_ino = jmvref->jm_ino; 2627 rec->jm_parent = jmvref->jm_parent; 2628 rec->jm_oldoff = jmvref->jm_oldoff; 2629 rec->jm_newoff = jmvref->jm_newoff; 2630} 2631 2632static void 2633jnewblk_write(jnewblk, jseg, data) 2634 struct jnewblk *jnewblk; 2635 struct jseg *jseg; 2636 uint8_t *data; 2637{ 2638 struct jblkrec *rec; 2639 2640 jnewblk->jn_jsegdep->jd_seg = jseg; 2641 rec = (struct jblkrec *)data; 2642 rec->jb_op = JOP_NEWBLK; 2643 rec->jb_ino = jnewblk->jn_ino; 2644 rec->jb_blkno = jnewblk->jn_blkno; 2645 rec->jb_lbn = jnewblk->jn_lbn; 2646 rec->jb_frags = jnewblk->jn_frags; 2647 rec->jb_oldfrags = jnewblk->jn_oldfrags; 2648} 2649 2650static void 2651jfreeblk_write(jfreeblk, jseg, data) 2652 struct jfreeblk *jfreeblk; 2653 struct jseg *jseg; 2654 uint8_t *data; 2655{ 2656 struct jblkrec *rec; 2657 2658 jfreeblk->jf_jsegdep->jd_seg = jseg; 2659 rec = (struct jblkrec *)data; 2660 rec->jb_op = JOP_FREEBLK; 2661 rec->jb_ino = jfreeblk->jf_ino; 2662 rec->jb_blkno = jfreeblk->jf_blkno; 2663 rec->jb_lbn = jfreeblk->jf_lbn; 2664 rec->jb_frags = jfreeblk->jf_frags; 2665 rec->jb_oldfrags = 0; 2666} 2667 2668static void 2669jfreefrag_write(jfreefrag, jseg, data) 2670 struct jfreefrag *jfreefrag; 2671 struct jseg *jseg; 2672 uint8_t *data; 2673{ 2674 struct jblkrec *rec; 2675 2676 jfreefrag->fr_jsegdep->jd_seg = jseg; 2677 rec = (struct jblkrec *)data; 2678 rec->jb_op = JOP_FREEBLK; 2679 rec->jb_ino = jfreefrag->fr_ino; 2680 rec->jb_blkno = jfreefrag->fr_blkno; 2681 rec->jb_lbn = jfreefrag->fr_lbn; 2682 rec->jb_frags = jfreefrag->fr_frags; 2683 rec->jb_oldfrags = 0; 2684} 2685 2686static void 2687jtrunc_write(jtrunc, jseg, data) 2688 struct jtrunc *jtrunc; 2689 struct jseg *jseg; 2690 uint8_t *data; 2691{ 2692 struct jtrncrec *rec; 2693 2694 rec = (struct jtrncrec *)data; 2695 rec->jt_op = JOP_TRUNC; 2696 rec->jt_ino = jtrunc->jt_ino; 2697 rec->jt_size = jtrunc->jt_size; 2698 rec->jt_extsize = jtrunc->jt_extsize; 2699} 2700 2701/* 2702 * Flush some journal records to disk. 2703 */ 2704static void 2705softdep_process_journal(mp, flags) 2706 struct mount *mp; 2707 int flags; 2708{ 2709 struct jblocks *jblocks; 2710 struct ufsmount *ump; 2711 struct worklist *wk; 2712 struct jseg *jseg; 2713 struct buf *bp; 2714 uint8_t *data; 2715 struct fs *fs; 2716 int segwritten; 2717 int jrecmin; /* Minimum records per block. */ 2718 int jrecmax; /* Maximum records per block. */ 2719 int size; 2720 int cnt; 2721 int off; 2722 2723 if ((mp->mnt_kern_flag & MNTK_SUJ) == 0) 2724 return; 2725 ump = VFSTOUFS(mp); 2726 fs = ump->um_fs; 2727 jblocks = ump->softdep_jblocks; 2728 /* 2729 * We write anywhere between a disk block and fs block. The upper 2730 * bound is picked to prevent buffer cache fragmentation and limit 2731 * processing time per I/O. 2732 */ 2733 jrecmin = (DEV_BSIZE / JREC_SIZE) - 1; /* -1 for seg header */ 2734 jrecmax = (fs->fs_bsize / DEV_BSIZE) * jrecmin; 2735 segwritten = 0; 2736 while ((cnt = ump->softdep_on_journal) != 0) { 2737 /* 2738 * Create a new segment to hold as many as 'cnt' journal 2739 * entries and add them to the segment. Notice cnt is 2740 * off by one to account for the space required by the 2741 * jsegrec. If we don't have a full block to log skip it 2742 * unless we haven't written anything. 2743 */ 2744 cnt++; 2745 if (cnt < jrecmax && segwritten) 2746 break; 2747 /* 2748 * Verify some free journal space. softdep_prealloc() should 2749 * guarantee that we don't run out so this is indicative of 2750 * a problem with the flow control. Try to recover 2751 * gracefully in any event. 2752 */ 2753 while (jblocks->jb_free == 0) { 2754 if (flags != MNT_WAIT) 2755 break; 2756 printf("softdep: Out of journal space!\n"); 2757 softdep_speedup(); 2758 msleep(jblocks, &lk, PRIBIO, "jblocks", hz); 2759 } 2760 FREE_LOCK(&lk); 2761 jseg = malloc(sizeof(*jseg), M_JSEG, M_SOFTDEP_FLAGS); 2762 workitem_alloc(&jseg->js_list, D_JSEG, mp); 2763 LIST_INIT(&jseg->js_entries); 2764 jseg->js_state = ATTACHED; 2765 jseg->js_jblocks = jblocks; 2766 bp = geteblk(fs->fs_bsize, 0); 2767 ACQUIRE_LOCK(&lk); 2768 /* 2769 * If there was a race while we were allocating the block 2770 * and jseg the entry we care about was likely written. 2771 * We bail out in both the WAIT and NOWAIT case and assume 2772 * the caller will loop if the entry it cares about is 2773 * not written. 2774 */ 2775 if (ump->softdep_on_journal == 0 || jblocks->jb_free == 0) { 2776 bp->b_flags |= B_INVAL | B_NOCACHE; 2777 WORKITEM_FREE(jseg, D_JSEG); 2778 FREE_LOCK(&lk); 2779 brelse(bp); 2780 ACQUIRE_LOCK(&lk); 2781 break; 2782 } 2783 /* 2784 * Calculate the disk block size required for the available 2785 * records rounded to the min size. 2786 */ 2787 cnt = ump->softdep_on_journal; 2788 if (cnt < jrecmax) 2789 size = howmany(cnt, jrecmin) * DEV_BSIZE; 2790 else 2791 size = fs->fs_bsize; 2792 /* 2793 * Allocate a disk block for this journal data and account 2794 * for truncation of the requested size if enough contiguous 2795 * space was not available. 2796 */ 2797 bp->b_blkno = jblocks_alloc(jblocks, size, &size); 2798 bp->b_lblkno = bp->b_blkno; 2799 bp->b_offset = bp->b_blkno * DEV_BSIZE; 2800 bp->b_bcount = size; 2801 bp->b_bufobj = &ump->um_devvp->v_bufobj; 2802 bp->b_flags &= ~B_INVAL; 2803 bp->b_flags |= B_VALIDSUSPWRT | B_NOCOPY; 2804 /* 2805 * Initialize our jseg with cnt records. Assign the next 2806 * sequence number to it and link it in-order. 2807 */ 2808 cnt = MIN(ump->softdep_on_journal, 2809 (size / DEV_BSIZE) * jrecmin); 2810 jseg->js_buf = bp; 2811 jseg->js_cnt = cnt; 2812 jseg->js_refs = cnt + 1; /* Self ref. */ 2813 jseg->js_size = size; 2814 jseg->js_seq = jblocks->jb_nextseq++; 2815 if (TAILQ_EMPTY(&jblocks->jb_segs)) 2816 jblocks->jb_oldestseq = jseg->js_seq; 2817 TAILQ_INSERT_TAIL(&jblocks->jb_segs, jseg, js_next); 2818 if (jblocks->jb_writeseg == NULL) 2819 jblocks->jb_writeseg = jseg; 2820 /* 2821 * Start filling in records from the pending list. 2822 */ 2823 data = bp->b_data; 2824 off = 0; 2825 while ((wk = LIST_FIRST(&ump->softdep_journal_pending)) 2826 != NULL) { 2827 /* Place a segment header on every device block. */ 2828 if ((off % DEV_BSIZE) == 0) { 2829 jseg_write(fs, jblocks, jseg, data); 2830 off += JREC_SIZE; 2831 data = bp->b_data + off; 2832 } 2833 remove_from_journal(wk); 2834 wk->wk_state |= IOSTARTED; 2835 WORKLIST_INSERT(&jseg->js_entries, wk); 2836 switch (wk->wk_type) { 2837 case D_JADDREF: 2838 jaddref_write(WK_JADDREF(wk), jseg, data); 2839 break; 2840 case D_JREMREF: 2841 jremref_write(WK_JREMREF(wk), jseg, data); 2842 break; 2843 case D_JMVREF: 2844 jmvref_write(WK_JMVREF(wk), jseg, data); 2845 break; 2846 case D_JNEWBLK: 2847 jnewblk_write(WK_JNEWBLK(wk), jseg, data); 2848 break; 2849 case D_JFREEBLK: 2850 jfreeblk_write(WK_JFREEBLK(wk), jseg, data); 2851 break; 2852 case D_JFREEFRAG: 2853 jfreefrag_write(WK_JFREEFRAG(wk), jseg, data); 2854 break; 2855 case D_JTRUNC: 2856 jtrunc_write(WK_JTRUNC(wk), jseg, data); 2857 break; 2858 default: 2859 panic("process_journal: Unknown type %s", 2860 TYPENAME(wk->wk_type)); 2861 /* NOTREACHED */ 2862 } 2863 if (--cnt == 0) 2864 break; 2865 off += JREC_SIZE; 2866 data = bp->b_data + off; 2867 } 2868 /* 2869 * Write this one buffer and continue. 2870 */ 2871 WORKLIST_INSERT(&bp->b_dep, &jseg->js_list); 2872 FREE_LOCK(&lk); 2873 BO_LOCK(bp->b_bufobj); 2874 bgetvp(ump->um_devvp, bp); 2875 BO_UNLOCK(bp->b_bufobj); 2876 if (flags == MNT_NOWAIT) 2877 bawrite(bp); 2878 else 2879 bwrite(bp); 2880 ACQUIRE_LOCK(&lk); 2881 } 2882 /* 2883 * If we've suspended the filesystem because we ran out of journal 2884 * space either try to sync it here to make some progress or 2885 * unsuspend it if we already have. 2886 */ 2887 if (flags == 0 && jblocks->jb_suspended) { 2888 if (journal_unsuspend(ump)) 2889 return; 2890 FREE_LOCK(&lk); 2891 VFS_SYNC(mp, MNT_NOWAIT); 2892 ffs_sbupdate(ump, MNT_WAIT, 0); 2893 ACQUIRE_LOCK(&lk); 2894 } 2895} 2896 2897/* 2898 * Complete a jseg, allowing all dependencies awaiting journal writes 2899 * to proceed. Each journal dependency also attaches a jsegdep to dependent 2900 * structures so that the journal segment can be freed to reclaim space. 2901 */ 2902static void 2903complete_jseg(jseg) 2904 struct jseg *jseg; 2905{ 2906 struct worklist *wk; 2907 struct jmvref *jmvref; 2908 int waiting; 2909#ifdef INVARIANTS 2910 int i = 0; 2911#endif 2912 2913 while ((wk = LIST_FIRST(&jseg->js_entries)) != NULL) { 2914 WORKLIST_REMOVE(wk); 2915 waiting = wk->wk_state & IOWAITING; 2916 wk->wk_state &= ~(IOSTARTED | IOWAITING); 2917 wk->wk_state |= COMPLETE; 2918 KASSERT(i++ < jseg->js_cnt, 2919 ("handle_written_jseg: overflow %d >= %d", 2920 i - 1, jseg->js_cnt)); 2921 switch (wk->wk_type) { 2922 case D_JADDREF: 2923 handle_written_jaddref(WK_JADDREF(wk)); 2924 break; 2925 case D_JREMREF: 2926 handle_written_jremref(WK_JREMREF(wk)); 2927 break; 2928 case D_JMVREF: 2929 /* No jsegdep here. */ 2930 free_jseg(jseg); 2931 jmvref = WK_JMVREF(wk); 2932 LIST_REMOVE(jmvref, jm_deps); 2933 free_pagedep(jmvref->jm_pagedep); 2934 WORKITEM_FREE(jmvref, D_JMVREF); 2935 break; 2936 case D_JNEWBLK: 2937 handle_written_jnewblk(WK_JNEWBLK(wk)); 2938 break; 2939 case D_JFREEBLK: 2940 handle_written_jfreeblk(WK_JFREEBLK(wk)); 2941 break; 2942 case D_JFREEFRAG: 2943 handle_written_jfreefrag(WK_JFREEFRAG(wk)); 2944 break; 2945 case D_JTRUNC: 2946 WK_JTRUNC(wk)->jt_jsegdep->jd_seg = jseg; 2947 WORKITEM_FREE(wk, D_JTRUNC); 2948 break; 2949 default: 2950 panic("handle_written_jseg: Unknown type %s", 2951 TYPENAME(wk->wk_type)); 2952 /* NOTREACHED */ 2953 } 2954 if (waiting) 2955 wakeup(wk); 2956 } 2957 /* Release the self reference so the structure may be freed. */ 2958 free_jseg(jseg); 2959} 2960 2961/* 2962 * Mark a jseg as DEPCOMPLETE and throw away the buffer. Handle jseg 2963 * completions in order only. 2964 */ 2965static void 2966handle_written_jseg(jseg, bp) 2967 struct jseg *jseg; 2968 struct buf *bp; 2969{ 2970 struct jblocks *jblocks; 2971 struct jseg *jsegn; 2972 2973 if (jseg->js_refs == 0) 2974 panic("handle_written_jseg: No self-reference on %p", jseg); 2975 jseg->js_state |= DEPCOMPLETE; 2976 /* 2977 * We'll never need this buffer again, set flags so it will be 2978 * discarded. 2979 */ 2980 bp->b_flags |= B_INVAL | B_NOCACHE; 2981 jblocks = jseg->js_jblocks; 2982 /* 2983 * Don't allow out of order completions. If this isn't the first 2984 * block wait for it to write before we're done. 2985 */ 2986 if (jseg != jblocks->jb_writeseg) 2987 return; 2988 /* Iterate through available jsegs processing their entries. */ 2989 do { 2990 jsegn = TAILQ_NEXT(jseg, js_next); 2991 complete_jseg(jseg); 2992 jseg = jsegn; 2993 } while (jseg && jseg->js_state & DEPCOMPLETE); 2994 jblocks->jb_writeseg = jseg; 2995} 2996 2997static inline struct jsegdep * 2998inoref_jseg(inoref) 2999 struct inoref *inoref; 3000{ 3001 struct jsegdep *jsegdep; 3002 3003 jsegdep = inoref->if_jsegdep; 3004 inoref->if_jsegdep = NULL; 3005 3006 return (jsegdep); 3007} 3008 3009/* 3010 * Called once a jremref has made it to stable store. The jremref is marked 3011 * complete and we attempt to free it. Any pagedeps writes sleeping waiting 3012 * for the jremref to complete will be awoken by free_jremref. 3013 */ 3014static void 3015handle_written_jremref(jremref) 3016 struct jremref *jremref; 3017{ 3018 struct inodedep *inodedep; 3019 struct jsegdep *jsegdep; 3020 struct dirrem *dirrem; 3021 3022 /* Grab the jsegdep. */ 3023 jsegdep = inoref_jseg(&jremref->jr_ref); 3024 /* 3025 * Remove us from the inoref list. 3026 */ 3027 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 3028 0, &inodedep) == 0) 3029 panic("handle_written_jremref: Lost inodedep"); 3030 TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 3031 /* 3032 * Complete the dirrem. 3033 */ 3034 dirrem = jremref->jr_dirrem; 3035 jremref->jr_dirrem = NULL; 3036 LIST_REMOVE(jremref, jr_deps); 3037 jsegdep->jd_state |= jremref->jr_state & MKDIR_PARENT; 3038 WORKLIST_INSERT(&dirrem->dm_jwork, &jsegdep->jd_list); 3039 if (LIST_EMPTY(&dirrem->dm_jremrefhd) && 3040 (dirrem->dm_state & COMPLETE) != 0) 3041 add_to_worklist(&dirrem->dm_list, 0); 3042 free_jremref(jremref); 3043} 3044 3045/* 3046 * Called once a jaddref has made it to stable store. The dependency is 3047 * marked complete and any dependent structures are added to the inode 3048 * bufwait list to be completed as soon as it is written. If a bitmap write 3049 * depends on this entry we move the inode into the inodedephd of the 3050 * bmsafemap dependency and attempt to remove the jaddref from the bmsafemap. 3051 */ 3052static void 3053handle_written_jaddref(jaddref) 3054 struct jaddref *jaddref; 3055{ 3056 struct jsegdep *jsegdep; 3057 struct inodedep *inodedep; 3058 struct diradd *diradd; 3059 struct mkdir *mkdir; 3060 3061 /* Grab the jsegdep. */ 3062 jsegdep = inoref_jseg(&jaddref->ja_ref); 3063 mkdir = NULL; 3064 diradd = NULL; 3065 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 3066 0, &inodedep) == 0) 3067 panic("handle_written_jaddref: Lost inodedep."); 3068 if (jaddref->ja_diradd == NULL) 3069 panic("handle_written_jaddref: No dependency"); 3070 if (jaddref->ja_diradd->da_list.wk_type == D_DIRADD) { 3071 diradd = jaddref->ja_diradd; 3072 WORKLIST_INSERT(&inodedep->id_bufwait, &diradd->da_list); 3073 } else if (jaddref->ja_state & MKDIR_PARENT) { 3074 mkdir = jaddref->ja_mkdir; 3075 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir->md_list); 3076 } else if (jaddref->ja_state & MKDIR_BODY) 3077 mkdir = jaddref->ja_mkdir; 3078 else 3079 panic("handle_written_jaddref: Unknown dependency %p", 3080 jaddref->ja_diradd); 3081 jaddref->ja_diradd = NULL; /* also clears ja_mkdir */ 3082 /* 3083 * Remove us from the inode list. 3084 */ 3085 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps); 3086 /* 3087 * The mkdir may be waiting on the jaddref to clear before freeing. 3088 */ 3089 if (mkdir) { 3090 KASSERT(mkdir->md_list.wk_type == D_MKDIR, 3091 ("handle_written_jaddref: Incorrect type for mkdir %s", 3092 TYPENAME(mkdir->md_list.wk_type))); 3093 mkdir->md_jaddref = NULL; 3094 diradd = mkdir->md_diradd; 3095 mkdir->md_state |= DEPCOMPLETE; 3096 complete_mkdir(mkdir); 3097 } 3098 WORKLIST_INSERT(&diradd->da_jwork, &jsegdep->jd_list); 3099 if (jaddref->ja_state & NEWBLOCK) { 3100 inodedep->id_state |= ONDEPLIST; 3101 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_inodedephd, 3102 inodedep, id_deps); 3103 } 3104 free_jaddref(jaddref); 3105} 3106 3107/* 3108 * Called once a jnewblk journal is written. The allocdirect or allocindir 3109 * is placed in the bmsafemap to await notification of a written bitmap. 3110 */ 3111static void 3112handle_written_jnewblk(jnewblk) 3113 struct jnewblk *jnewblk; 3114{ 3115 struct bmsafemap *bmsafemap; 3116 struct jsegdep *jsegdep; 3117 struct newblk *newblk; 3118 3119 /* Grab the jsegdep. */ 3120 jsegdep = jnewblk->jn_jsegdep; 3121 jnewblk->jn_jsegdep = NULL; 3122 /* 3123 * Add the written block to the bmsafemap so it can be notified when 3124 * the bitmap is on disk. 3125 */ 3126 newblk = jnewblk->jn_newblk; 3127 jnewblk->jn_newblk = NULL; 3128 if (newblk == NULL) 3129 panic("handle_written_jnewblk: No dependency for the segdep."); 3130 3131 newblk->nb_jnewblk = NULL; 3132 bmsafemap = newblk->nb_bmsafemap; 3133 WORKLIST_INSERT(&newblk->nb_jwork, &jsegdep->jd_list); 3134 newblk->nb_state |= ONDEPLIST; 3135 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps); 3136 free_jnewblk(jnewblk); 3137} 3138 3139/* 3140 * Cancel a jfreefrag that won't be needed, probably due to colliding with 3141 * an in-flight allocation that has not yet been committed. Divorce us 3142 * from the freefrag and mark it DEPCOMPLETE so that it may be added 3143 * to the worklist. 3144 */ 3145static void 3146cancel_jfreefrag(jfreefrag) 3147 struct jfreefrag *jfreefrag; 3148{ 3149 struct freefrag *freefrag; 3150 3151 if (jfreefrag->fr_jsegdep) { 3152 free_jsegdep(jfreefrag->fr_jsegdep); 3153 jfreefrag->fr_jsegdep = NULL; 3154 } 3155 freefrag = jfreefrag->fr_freefrag; 3156 jfreefrag->fr_freefrag = NULL; 3157 freefrag->ff_jfreefrag = NULL; 3158 free_jfreefrag(jfreefrag); 3159 freefrag->ff_state |= DEPCOMPLETE; 3160} 3161 3162/* 3163 * Free a jfreefrag when the parent freefrag is rendered obsolete. 3164 */ 3165static void 3166free_jfreefrag(jfreefrag) 3167 struct jfreefrag *jfreefrag; 3168{ 3169 3170 if (jfreefrag->fr_state & IOSTARTED) 3171 WORKLIST_REMOVE(&jfreefrag->fr_list); 3172 else if (jfreefrag->fr_state & ONWORKLIST) 3173 remove_from_journal(&jfreefrag->fr_list); 3174 if (jfreefrag->fr_freefrag != NULL) 3175 panic("free_jfreefrag: Still attached to a freefrag."); 3176 WORKITEM_FREE(jfreefrag, D_JFREEFRAG); 3177} 3178 3179/* 3180 * Called when the journal write for a jfreefrag completes. The parent 3181 * freefrag is added to the worklist if this completes its dependencies. 3182 */ 3183static void 3184handle_written_jfreefrag(jfreefrag) 3185 struct jfreefrag *jfreefrag; 3186{ 3187 struct jsegdep *jsegdep; 3188 struct freefrag *freefrag; 3189 3190 /* Grab the jsegdep. */ 3191 jsegdep = jfreefrag->fr_jsegdep; 3192 jfreefrag->fr_jsegdep = NULL; 3193 freefrag = jfreefrag->fr_freefrag; 3194 if (freefrag == NULL) 3195 panic("handle_written_jfreefrag: No freefrag."); 3196 freefrag->ff_state |= DEPCOMPLETE; 3197 freefrag->ff_jfreefrag = NULL; 3198 WORKLIST_INSERT(&freefrag->ff_jwork, &jsegdep->jd_list); 3199 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 3200 add_to_worklist(&freefrag->ff_list, 0); 3201 jfreefrag->fr_freefrag = NULL; 3202 free_jfreefrag(jfreefrag); 3203} 3204 3205/* 3206 * Called when the journal write for a jfreeblk completes. The jfreeblk 3207 * is removed from the freeblks list of pending journal writes and the 3208 * jsegdep is moved to the freeblks jwork to be completed when all blocks 3209 * have been reclaimed. 3210 */ 3211static void 3212handle_written_jfreeblk(jfreeblk) 3213 struct jfreeblk *jfreeblk; 3214{ 3215 struct freeblks *freeblks; 3216 struct jsegdep *jsegdep; 3217 3218 /* Grab the jsegdep. */ 3219 jsegdep = jfreeblk->jf_jsegdep; 3220 jfreeblk->jf_jsegdep = NULL; 3221 freeblks = jfreeblk->jf_freeblks; 3222 LIST_REMOVE(jfreeblk, jf_deps); 3223 WORKLIST_INSERT(&freeblks->fb_jwork, &jsegdep->jd_list); 3224 /* 3225 * If the freeblks is all journaled, we can add it to the worklist. 3226 */ 3227 if (LIST_EMPTY(&freeblks->fb_jfreeblkhd) && 3228 (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) { 3229 /* Remove from the b_dep that is waiting on this write. */ 3230 if (freeblks->fb_state & ONWORKLIST) 3231 WORKLIST_REMOVE(&freeblks->fb_list); 3232 add_to_worklist(&freeblks->fb_list, 1); 3233 } 3234 3235 free_jfreeblk(jfreeblk); 3236} 3237 3238static struct jsegdep * 3239newjsegdep(struct worklist *wk) 3240{ 3241 struct jsegdep *jsegdep; 3242 3243 jsegdep = malloc(sizeof(*jsegdep), M_JSEGDEP, M_SOFTDEP_FLAGS); 3244 workitem_alloc(&jsegdep->jd_list, D_JSEGDEP, wk->wk_mp); 3245 jsegdep->jd_seg = NULL; 3246 3247 return (jsegdep); 3248} 3249 3250static struct jmvref * 3251newjmvref(dp, ino, oldoff, newoff) 3252 struct inode *dp; 3253 ino_t ino; 3254 off_t oldoff; 3255 off_t newoff; 3256{ 3257 struct jmvref *jmvref; 3258 3259 jmvref = malloc(sizeof(*jmvref), M_JMVREF, M_SOFTDEP_FLAGS); 3260 workitem_alloc(&jmvref->jm_list, D_JMVREF, UFSTOVFS(dp->i_ump)); 3261 jmvref->jm_list.wk_state = ATTACHED | DEPCOMPLETE; 3262 jmvref->jm_parent = dp->i_number; 3263 jmvref->jm_ino = ino; 3264 jmvref->jm_oldoff = oldoff; 3265 jmvref->jm_newoff = newoff; 3266 3267 return (jmvref); 3268} 3269 3270/* 3271 * Allocate a new jremref that tracks the removal of ip from dp with the 3272 * directory entry offset of diroff. Mark the entry as ATTACHED and 3273 * DEPCOMPLETE as we have all the information required for the journal write 3274 * and the directory has already been removed from the buffer. The caller 3275 * is responsible for linking the jremref into the pagedep and adding it 3276 * to the journal to write. The MKDIR_PARENT flag is set if we're doing 3277 * a DOTDOT addition so handle_workitem_remove() can properly assign 3278 * the jsegdep when we're done. 3279 */ 3280static struct jremref * 3281newjremref(struct dirrem *dirrem, struct inode *dp, struct inode *ip, 3282 off_t diroff, nlink_t nlink) 3283{ 3284 struct jremref *jremref; 3285 3286 jremref = malloc(sizeof(*jremref), M_JREMREF, M_SOFTDEP_FLAGS); 3287 workitem_alloc(&jremref->jr_list, D_JREMREF, UFSTOVFS(dp->i_ump)); 3288 jremref->jr_state = ATTACHED; 3289 newinoref(&jremref->jr_ref, ip->i_number, dp->i_number, diroff, 3290 nlink, ip->i_mode); 3291 jremref->jr_dirrem = dirrem; 3292 3293 return (jremref); 3294} 3295 3296static inline void 3297newinoref(struct inoref *inoref, ino_t ino, ino_t parent, off_t diroff, 3298 nlink_t nlink, uint16_t mode) 3299{ 3300 3301 inoref->if_jsegdep = newjsegdep(&inoref->if_list); 3302 inoref->if_diroff = diroff; 3303 inoref->if_ino = ino; 3304 inoref->if_parent = parent; 3305 inoref->if_nlink = nlink; 3306 inoref->if_mode = mode; 3307} 3308 3309/* 3310 * Allocate a new jaddref to track the addition of ino to dp at diroff. The 3311 * directory offset may not be known until later. The caller is responsible 3312 * adding the entry to the journal when this information is available. nlink 3313 * should be the link count prior to the addition and mode is only required 3314 * to have the correct FMT. 3315 */ 3316static struct jaddref * 3317newjaddref(struct inode *dp, ino_t ino, off_t diroff, int16_t nlink, 3318 uint16_t mode) 3319{ 3320 struct jaddref *jaddref; 3321 3322 jaddref = malloc(sizeof(*jaddref), M_JADDREF, M_SOFTDEP_FLAGS); 3323 workitem_alloc(&jaddref->ja_list, D_JADDREF, UFSTOVFS(dp->i_ump)); 3324 jaddref->ja_state = ATTACHED; 3325 jaddref->ja_mkdir = NULL; 3326 newinoref(&jaddref->ja_ref, ino, dp->i_number, diroff, nlink, mode); 3327 3328 return (jaddref); 3329} 3330 3331/* 3332 * Create a new free dependency for a freework. The caller is responsible 3333 * for adjusting the reference count when it has the lock held. The freedep 3334 * will track an outstanding bitmap write that will ultimately clear the 3335 * freework to continue. 3336 */ 3337static struct freedep * 3338newfreedep(struct freework *freework) 3339{ 3340 struct freedep *freedep; 3341 3342 freedep = malloc(sizeof(*freedep), M_FREEDEP, M_SOFTDEP_FLAGS); 3343 workitem_alloc(&freedep->fd_list, D_FREEDEP, freework->fw_list.wk_mp); 3344 freedep->fd_freework = freework; 3345 3346 return (freedep); 3347} 3348 3349/* 3350 * Free a freedep structure once the buffer it is linked to is written. If 3351 * this is the last reference to the freework schedule it for completion. 3352 */ 3353static void 3354free_freedep(freedep) 3355 struct freedep *freedep; 3356{ 3357 3358 if (--freedep->fd_freework->fw_ref == 0) 3359 add_to_worklist(&freedep->fd_freework->fw_list, 1); 3360 WORKITEM_FREE(freedep, D_FREEDEP); 3361} 3362 3363/* 3364 * Allocate a new freework structure that may be a level in an indirect 3365 * when parent is not NULL or a top level block when it is. The top level 3366 * freework structures are allocated without lk held and before the freeblks 3367 * is visible outside of softdep_setup_freeblocks(). 3368 */ 3369static struct freework * 3370newfreework(freeblks, parent, lbn, nb, frags, journal) 3371 struct freeblks *freeblks; 3372 struct freework *parent; 3373 ufs_lbn_t lbn; 3374 ufs2_daddr_t nb; 3375 int frags; 3376 int journal; 3377{ 3378 struct freework *freework; 3379 3380 freework = malloc(sizeof(*freework), M_FREEWORK, M_SOFTDEP_FLAGS); 3381 workitem_alloc(&freework->fw_list, D_FREEWORK, freeblks->fb_list.wk_mp); 3382 freework->fw_freeblks = freeblks; 3383 freework->fw_parent = parent; 3384 freework->fw_lbn = lbn; 3385 freework->fw_blkno = nb; 3386 freework->fw_frags = frags; 3387 freework->fw_ref = 0; 3388 freework->fw_off = 0; 3389 LIST_INIT(&freework->fw_jwork); 3390 3391 if (parent == NULL) { 3392 WORKLIST_INSERT_UNLOCKED(&freeblks->fb_freeworkhd, 3393 &freework->fw_list); 3394 freeblks->fb_ref++; 3395 } 3396 if (journal) 3397 newjfreeblk(freeblks, lbn, nb, frags); 3398 3399 return (freework); 3400} 3401 3402/* 3403 * Allocate a new jfreeblk to journal top level block pointer when truncating 3404 * a file. The caller must add this to the worklist when lk is held. 3405 */ 3406static struct jfreeblk * 3407newjfreeblk(freeblks, lbn, blkno, frags) 3408 struct freeblks *freeblks; 3409 ufs_lbn_t lbn; 3410 ufs2_daddr_t blkno; 3411 int frags; 3412{ 3413 struct jfreeblk *jfreeblk; 3414 3415 jfreeblk = malloc(sizeof(*jfreeblk), M_JFREEBLK, M_SOFTDEP_FLAGS); 3416 workitem_alloc(&jfreeblk->jf_list, D_JFREEBLK, freeblks->fb_list.wk_mp); 3417 jfreeblk->jf_jsegdep = newjsegdep(&jfreeblk->jf_list); 3418 jfreeblk->jf_state = ATTACHED | DEPCOMPLETE; 3419 jfreeblk->jf_ino = freeblks->fb_previousinum; 3420 jfreeblk->jf_lbn = lbn; 3421 jfreeblk->jf_blkno = blkno; 3422 jfreeblk->jf_frags = frags; 3423 jfreeblk->jf_freeblks = freeblks; 3424 LIST_INSERT_HEAD(&freeblks->fb_jfreeblkhd, jfreeblk, jf_deps); 3425 3426 return (jfreeblk); 3427} 3428 3429static void move_newblock_dep(struct jaddref *, struct inodedep *); 3430/* 3431 * If we're canceling a new bitmap we have to search for another ref 3432 * to move into the bmsafemap dep. This might be better expressed 3433 * with another structure. 3434 */ 3435static void 3436move_newblock_dep(jaddref, inodedep) 3437 struct jaddref *jaddref; 3438 struct inodedep *inodedep; 3439{ 3440 struct inoref *inoref; 3441 struct jaddref *jaddrefn; 3442 3443 jaddrefn = NULL; 3444 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 3445 inoref = TAILQ_NEXT(inoref, if_deps)) { 3446 if ((jaddref->ja_state & NEWBLOCK) && 3447 inoref->if_list.wk_type == D_JADDREF) { 3448 jaddrefn = (struct jaddref *)inoref; 3449 break; 3450 } 3451 } 3452 if (jaddrefn == NULL) 3453 return; 3454 jaddrefn->ja_state &= ~(ATTACHED | UNDONE); 3455 jaddrefn->ja_state |= jaddref->ja_state & 3456 (ATTACHED | UNDONE | NEWBLOCK); 3457 jaddref->ja_state &= ~(ATTACHED | UNDONE | NEWBLOCK); 3458 jaddref->ja_state |= ATTACHED; 3459 LIST_REMOVE(jaddref, ja_bmdeps); 3460 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_jaddrefhd, jaddrefn, 3461 ja_bmdeps); 3462} 3463 3464/* 3465 * Cancel a jaddref either before it has been written or while it is being 3466 * written. This happens when a link is removed before the add reaches 3467 * the disk. The jaddref dependency is kept linked into the bmsafemap 3468 * and inode to prevent the link count or bitmap from reaching the disk 3469 * until handle_workitem_remove() re-adjusts the counts and bitmaps as 3470 * required. 3471 * 3472 * Returns 1 if the canceled addref requires journaling of the remove and 3473 * 0 otherwise. 3474 */ 3475static int 3476cancel_jaddref(jaddref, inodedep, wkhd) 3477 struct jaddref *jaddref; 3478 struct inodedep *inodedep; 3479 struct workhead *wkhd; 3480{ 3481 struct inoref *inoref; 3482 struct jsegdep *jsegdep; 3483 int needsj; 3484 3485 KASSERT((jaddref->ja_state & COMPLETE) == 0, 3486 ("cancel_jaddref: Canceling complete jaddref")); 3487 if (jaddref->ja_state & (IOSTARTED | COMPLETE)) 3488 needsj = 1; 3489 else 3490 needsj = 0; 3491 if (inodedep == NULL) 3492 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 3493 0, &inodedep) == 0) 3494 panic("cancel_jaddref: Lost inodedep"); 3495 /* 3496 * We must adjust the nlink of any reference operation that follows 3497 * us so that it is consistent with the in-memory reference. This 3498 * ensures that inode nlink rollbacks always have the correct link. 3499 */ 3500 if (needsj == 0) 3501 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 3502 inoref = TAILQ_NEXT(inoref, if_deps)) 3503 inoref->if_nlink--; 3504 jsegdep = inoref_jseg(&jaddref->ja_ref); 3505 if (jaddref->ja_state & NEWBLOCK) 3506 move_newblock_dep(jaddref, inodedep); 3507 if (jaddref->ja_state & IOWAITING) { 3508 jaddref->ja_state &= ~IOWAITING; 3509 wakeup(&jaddref->ja_list); 3510 } 3511 jaddref->ja_mkdir = NULL; 3512 if (jaddref->ja_state & IOSTARTED) { 3513 jaddref->ja_state &= ~IOSTARTED; 3514 WORKLIST_REMOVE(&jaddref->ja_list); 3515 WORKLIST_INSERT(wkhd, &jsegdep->jd_list); 3516 } else { 3517 free_jsegdep(jsegdep); 3518 if (jaddref->ja_state & DEPCOMPLETE) 3519 remove_from_journal(&jaddref->ja_list); 3520 } 3521 /* 3522 * Leave NEWBLOCK jaddrefs on the inodedep so handle_workitem_remove 3523 * can arrange for them to be freed with the bitmap. Otherwise we 3524 * no longer need this addref attached to the inoreflst and it 3525 * will incorrectly adjust nlink if we leave it. 3526 */ 3527 if ((jaddref->ja_state & NEWBLOCK) == 0) { 3528 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 3529 if_deps); 3530 jaddref->ja_state |= COMPLETE; 3531 free_jaddref(jaddref); 3532 return (needsj); 3533 } 3534 jaddref->ja_state |= GOINGAWAY; 3535 /* 3536 * Leave the head of the list for jsegdeps for fast merging. 3537 */ 3538 if (LIST_FIRST(wkhd) != NULL) { 3539 jaddref->ja_state |= ONWORKLIST; 3540 LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list); 3541 } else 3542 WORKLIST_INSERT(wkhd, &jaddref->ja_list); 3543 3544 return (needsj); 3545} 3546 3547/* 3548 * Attempt to free a jaddref structure when some work completes. This 3549 * should only succeed once the entry is written and all dependencies have 3550 * been notified. 3551 */ 3552static void 3553free_jaddref(jaddref) 3554 struct jaddref *jaddref; 3555{ 3556 3557 if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE) 3558 return; 3559 if (jaddref->ja_ref.if_jsegdep) 3560 panic("free_jaddref: segdep attached to jaddref %p(0x%X)\n", 3561 jaddref, jaddref->ja_state); 3562 if (jaddref->ja_state & NEWBLOCK) 3563 LIST_REMOVE(jaddref, ja_bmdeps); 3564 if (jaddref->ja_state & (IOSTARTED | ONWORKLIST)) 3565 panic("free_jaddref: Bad state %p(0x%X)", 3566 jaddref, jaddref->ja_state); 3567 if (jaddref->ja_mkdir != NULL) 3568 panic("free_jaddref: Work pending, 0x%X\n", jaddref->ja_state); 3569 WORKITEM_FREE(jaddref, D_JADDREF); 3570} 3571 3572/* 3573 * Free a jremref structure once it has been written or discarded. 3574 */ 3575static void 3576free_jremref(jremref) 3577 struct jremref *jremref; 3578{ 3579 3580 if (jremref->jr_ref.if_jsegdep) 3581 free_jsegdep(jremref->jr_ref.if_jsegdep); 3582 if (jremref->jr_state & IOSTARTED) 3583 panic("free_jremref: IO still pending"); 3584 WORKITEM_FREE(jremref, D_JREMREF); 3585} 3586 3587/* 3588 * Free a jnewblk structure. 3589 */ 3590static void 3591free_jnewblk(jnewblk) 3592 struct jnewblk *jnewblk; 3593{ 3594 3595 if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE) 3596 return; 3597 LIST_REMOVE(jnewblk, jn_deps); 3598 if (jnewblk->jn_newblk != NULL) 3599 panic("free_jnewblk: Dependency still attached."); 3600 WORKITEM_FREE(jnewblk, D_JNEWBLK); 3601} 3602 3603/* 3604 * Cancel a jnewblk which has been superseded by a freeblk. The jnewblk 3605 * is kept linked into the bmsafemap until the free completes, thus 3606 * preventing the modified state from ever reaching disk. The free 3607 * routine must pass this structure via ffs_blkfree() to 3608 * softdep_setup_freeblks() so there is no race in releasing the space. 3609 */ 3610static void 3611cancel_jnewblk(jnewblk, wkhd) 3612 struct jnewblk *jnewblk; 3613 struct workhead *wkhd; 3614{ 3615 struct jsegdep *jsegdep; 3616 3617 jsegdep = jnewblk->jn_jsegdep; 3618 jnewblk->jn_jsegdep = NULL; 3619 free_jsegdep(jsegdep); 3620 jnewblk->jn_newblk = NULL; 3621 jnewblk->jn_state |= GOINGAWAY; 3622 if (jnewblk->jn_state & IOSTARTED) { 3623 jnewblk->jn_state &= ~IOSTARTED; 3624 WORKLIST_REMOVE(&jnewblk->jn_list); 3625 } else 3626 remove_from_journal(&jnewblk->jn_list); 3627 /* 3628 * Leave the head of the list for jsegdeps for fast merging. 3629 */ 3630 if (LIST_FIRST(wkhd) != NULL) { 3631 jnewblk->jn_state |= ONWORKLIST; 3632 LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jnewblk->jn_list, wk_list); 3633 } else 3634 WORKLIST_INSERT(wkhd, &jnewblk->jn_list); 3635 if (jnewblk->jn_state & IOWAITING) { 3636 jnewblk->jn_state &= ~IOWAITING; 3637 wakeup(&jnewblk->jn_list); 3638 } 3639} 3640 3641static void 3642free_jfreeblk(jfreeblk) 3643 struct jfreeblk *jfreeblk; 3644{ 3645 3646 WORKITEM_FREE(jfreeblk, D_JFREEBLK); 3647} 3648 3649/* 3650 * Release one reference to a jseg and free it if the count reaches 0. This 3651 * should eventually reclaim journal space as well. 3652 */ 3653static void 3654free_jseg(jseg) 3655 struct jseg *jseg; 3656{ 3657 struct jblocks *jblocks; 3658 3659 KASSERT(jseg->js_refs > 0, 3660 ("free_jseg: Invalid refcnt %d", jseg->js_refs)); 3661 if (--jseg->js_refs != 0) 3662 return; 3663 /* 3664 * Free only those jsegs which have none allocated before them to 3665 * preserve the journal space ordering. 3666 */ 3667 jblocks = jseg->js_jblocks; 3668 while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) { 3669 jblocks->jb_oldestseq = jseg->js_seq; 3670 if (jseg->js_refs != 0) 3671 break; 3672 TAILQ_REMOVE(&jblocks->jb_segs, jseg, js_next); 3673 jblocks_free(jblocks, jseg->js_list.wk_mp, jseg->js_size); 3674 KASSERT(LIST_EMPTY(&jseg->js_entries), 3675 ("free_jseg: Freed jseg has valid entries.")); 3676 WORKITEM_FREE(jseg, D_JSEG); 3677 } 3678} 3679 3680/* 3681 * Release a jsegdep and decrement the jseg count. 3682 */ 3683static void 3684free_jsegdep(jsegdep) 3685 struct jsegdep *jsegdep; 3686{ 3687 3688 if (jsegdep->jd_seg) 3689 free_jseg(jsegdep->jd_seg); 3690 WORKITEM_FREE(jsegdep, D_JSEGDEP); 3691} 3692 3693/* 3694 * Wait for a journal item to make it to disk. Initiate journal processing 3695 * if required. 3696 */ 3697static void 3698jwait(wk) 3699 struct worklist *wk; 3700{ 3701 3702 stat_journal_wait++; 3703 /* 3704 * If IO has not started we process the journal. We can't mark the 3705 * worklist item as IOWAITING because we drop the lock while 3706 * processing the journal and the worklist entry may be freed after 3707 * this point. The caller may call back in and re-issue the request. 3708 */ 3709 if ((wk->wk_state & IOSTARTED) == 0) { 3710 softdep_process_journal(wk->wk_mp, MNT_WAIT); 3711 return; 3712 } 3713 wk->wk_state |= IOWAITING; 3714 msleep(wk, &lk, PRIBIO, "jwait", 0); 3715} 3716 3717/* 3718 * Lookup an inodedep based on an inode pointer and set the nlinkdelta as 3719 * appropriate. This is a convenience function to reduce duplicate code 3720 * for the setup and revert functions below. 3721 */ 3722static struct inodedep * 3723inodedep_lookup_ip(ip) 3724 struct inode *ip; 3725{ 3726 struct inodedep *inodedep; 3727 3728 KASSERT(ip->i_nlink >= ip->i_effnlink, 3729 ("inodedep_lookup_ip: bad delta")); 3730 (void) inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 3731 DEPALLOC, &inodedep); 3732 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 3733 3734 return (inodedep); 3735} 3736 3737/* 3738 * Create a journal entry that describes a truncate that we're about to 3739 * perform. The inode allocations and frees between here and the completion 3740 * of the operation are done asynchronously and without journaling. At 3741 * the end of the operation the vnode is sync'd and the journal space 3742 * is released. Recovery will discover the partially completed truncate 3743 * and complete it. 3744 */ 3745void * 3746softdep_setup_trunc(vp, length, flags) 3747 struct vnode *vp; 3748 off_t length; 3749 int flags; 3750{ 3751 struct jsegdep *jsegdep; 3752 struct jtrunc *jtrunc; 3753 struct ufsmount *ump; 3754 struct inode *ip; 3755 3756 softdep_prealloc(vp, MNT_WAIT); 3757 ip = VTOI(vp); 3758 ump = VFSTOUFS(vp->v_mount); 3759 jtrunc = malloc(sizeof(*jtrunc), M_JTRUNC, M_SOFTDEP_FLAGS); 3760 workitem_alloc(&jtrunc->jt_list, D_JTRUNC, vp->v_mount); 3761 jsegdep = jtrunc->jt_jsegdep = newjsegdep(&jtrunc->jt_list); 3762 jtrunc->jt_ino = ip->i_number; 3763 jtrunc->jt_extsize = 0; 3764 jtrunc->jt_size = length; 3765 if ((flags & IO_EXT) == 0 && ump->um_fstype == UFS2) 3766 jtrunc->jt_extsize = ip->i_din2->di_extsize; 3767 if ((flags & IO_NORMAL) == 0) 3768 jtrunc->jt_size = DIP(ip, i_size); 3769 ACQUIRE_LOCK(&lk); 3770 add_to_journal(&jtrunc->jt_list); 3771 while (jsegdep->jd_seg == NULL) { 3772 stat_jwait_freeblks++; 3773 jwait(&jtrunc->jt_list); 3774 } 3775 FREE_LOCK(&lk); 3776 3777 return (jsegdep); 3778} 3779 3780/* 3781 * After synchronous truncation is complete we free sync the vnode and 3782 * release the jsegdep so the journal space can be freed. 3783 */ 3784int 3785softdep_complete_trunc(vp, cookie) 3786 struct vnode *vp; 3787 void *cookie; 3788{ 3789 int error; 3790 3791 error = ffs_syncvnode(vp, MNT_WAIT); 3792 ACQUIRE_LOCK(&lk); 3793 free_jsegdep((struct jsegdep *)cookie); 3794 FREE_LOCK(&lk); 3795 3796 return (error); 3797} 3798 3799/* 3800 * Called prior to creating a new inode and linking it to a directory. The 3801 * jaddref structure must already be allocated by softdep_setup_inomapdep 3802 * and it is discovered here so we can initialize the mode and update 3803 * nlinkdelta. 3804 */ 3805void 3806softdep_setup_create(dp, ip) 3807 struct inode *dp; 3808 struct inode *ip; 3809{ 3810 struct inodedep *inodedep; 3811 struct jaddref *jaddref; 3812 struct vnode *dvp; 3813 3814 KASSERT(ip->i_nlink == 1, 3815 ("softdep_setup_create: Invalid link count.")); 3816 dvp = ITOV(dp); 3817 ACQUIRE_LOCK(&lk); 3818 inodedep = inodedep_lookup_ip(ip); 3819 if (DOINGSUJ(dvp)) { 3820 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 3821 inoreflst); 3822 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 3823 ("softdep_setup_create: No addref structure present.")); 3824 jaddref->ja_mode = ip->i_mode; 3825 } 3826 softdep_prelink(dvp, NULL); 3827 FREE_LOCK(&lk); 3828} 3829 3830/* 3831 * Create a jaddref structure to track the addition of a DOTDOT link when 3832 * we are reparenting an inode as part of a rename. This jaddref will be 3833 * found by softdep_setup_directory_change. Adjusts nlinkdelta for 3834 * non-journaling softdep. 3835 */ 3836void 3837softdep_setup_dotdot_link(dp, ip) 3838 struct inode *dp; 3839 struct inode *ip; 3840{ 3841 struct inodedep *inodedep; 3842 struct jaddref *jaddref; 3843 struct vnode *dvp; 3844 struct vnode *vp; 3845 3846 dvp = ITOV(dp); 3847 vp = ITOV(ip); 3848 jaddref = NULL; 3849 /* 3850 * We don't set MKDIR_PARENT as this is not tied to a mkdir and 3851 * is used as a normal link would be. 3852 */ 3853 if (DOINGSUJ(dvp)) 3854 jaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 3855 dp->i_effnlink - 1, dp->i_mode); 3856 ACQUIRE_LOCK(&lk); 3857 inodedep = inodedep_lookup_ip(dp); 3858 if (jaddref) 3859 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 3860 if_deps); 3861 softdep_prelink(dvp, ITOV(ip)); 3862 FREE_LOCK(&lk); 3863} 3864 3865/* 3866 * Create a jaddref structure to track a new link to an inode. The directory 3867 * offset is not known until softdep_setup_directory_add or 3868 * softdep_setup_directory_change. Adjusts nlinkdelta for non-journaling 3869 * softdep. 3870 */ 3871void 3872softdep_setup_link(dp, ip) 3873 struct inode *dp; 3874 struct inode *ip; 3875{ 3876 struct inodedep *inodedep; 3877 struct jaddref *jaddref; 3878 struct vnode *dvp; 3879 3880 dvp = ITOV(dp); 3881 jaddref = NULL; 3882 if (DOINGSUJ(dvp)) 3883 jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1, 3884 ip->i_mode); 3885 ACQUIRE_LOCK(&lk); 3886 inodedep = inodedep_lookup_ip(ip); 3887 if (jaddref) 3888 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 3889 if_deps); 3890 softdep_prelink(dvp, ITOV(ip)); 3891 FREE_LOCK(&lk); 3892} 3893 3894/* 3895 * Called to create the jaddref structures to track . and .. references as 3896 * well as lookup and further initialize the incomplete jaddref created 3897 * by softdep_setup_inomapdep when the inode was allocated. Adjusts 3898 * nlinkdelta for non-journaling softdep. 3899 */ 3900void 3901softdep_setup_mkdir(dp, ip) 3902 struct inode *dp; 3903 struct inode *ip; 3904{ 3905 struct inodedep *inodedep; 3906 struct jaddref *dotdotaddref; 3907 struct jaddref *dotaddref; 3908 struct jaddref *jaddref; 3909 struct vnode *dvp; 3910 3911 dvp = ITOV(dp); 3912 dotaddref = dotdotaddref = NULL; 3913 if (DOINGSUJ(dvp)) { 3914 dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1, 3915 ip->i_mode); 3916 dotaddref->ja_state |= MKDIR_BODY; 3917 dotdotaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 3918 dp->i_effnlink - 1, dp->i_mode); 3919 dotdotaddref->ja_state |= MKDIR_PARENT; 3920 } 3921 ACQUIRE_LOCK(&lk); 3922 inodedep = inodedep_lookup_ip(ip); 3923 if (DOINGSUJ(dvp)) { 3924 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 3925 inoreflst); 3926 KASSERT(jaddref != NULL, 3927 ("softdep_setup_mkdir: No addref structure present.")); 3928 KASSERT(jaddref->ja_parent == dp->i_number, 3929 ("softdep_setup_mkdir: bad parent %d", 3930 jaddref->ja_parent)); 3931 jaddref->ja_mode = ip->i_mode; 3932 TAILQ_INSERT_BEFORE(&jaddref->ja_ref, &dotaddref->ja_ref, 3933 if_deps); 3934 } 3935 inodedep = inodedep_lookup_ip(dp); 3936 if (DOINGSUJ(dvp)) 3937 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, 3938 &dotdotaddref->ja_ref, if_deps); 3939 softdep_prelink(ITOV(dp), NULL); 3940 FREE_LOCK(&lk); 3941} 3942 3943/* 3944 * Called to track nlinkdelta of the inode and parent directories prior to 3945 * unlinking a directory. 3946 */ 3947void 3948softdep_setup_rmdir(dp, ip) 3949 struct inode *dp; 3950 struct inode *ip; 3951{ 3952 struct vnode *dvp; 3953 3954 dvp = ITOV(dp); 3955 ACQUIRE_LOCK(&lk); 3956 (void) inodedep_lookup_ip(ip); 3957 (void) inodedep_lookup_ip(dp); 3958 softdep_prelink(dvp, ITOV(ip)); 3959 FREE_LOCK(&lk); 3960} 3961 3962/* 3963 * Called to track nlinkdelta of the inode and parent directories prior to 3964 * unlink. 3965 */ 3966void 3967softdep_setup_unlink(dp, ip) 3968 struct inode *dp; 3969 struct inode *ip; 3970{ 3971 struct vnode *dvp; 3972 3973 dvp = ITOV(dp); 3974 ACQUIRE_LOCK(&lk); 3975 (void) inodedep_lookup_ip(ip); 3976 (void) inodedep_lookup_ip(dp); 3977 softdep_prelink(dvp, ITOV(ip)); 3978 FREE_LOCK(&lk); 3979} 3980 3981/* 3982 * Called to release the journal structures created by a failed non-directory 3983 * creation. Adjusts nlinkdelta for non-journaling softdep. 3984 */ 3985void 3986softdep_revert_create(dp, ip) 3987 struct inode *dp; 3988 struct inode *ip; 3989{ 3990 struct inodedep *inodedep; 3991 struct jaddref *jaddref; 3992 struct vnode *dvp; 3993 3994 dvp = ITOV(dp); 3995 ACQUIRE_LOCK(&lk); 3996 inodedep = inodedep_lookup_ip(ip); 3997 if (DOINGSUJ(dvp)) { 3998 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 3999 inoreflst); 4000 KASSERT(jaddref->ja_parent == dp->i_number, 4001 ("softdep_revert_create: addref parent mismatch")); 4002 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4003 } 4004 FREE_LOCK(&lk); 4005} 4006 4007/* 4008 * Called to release the journal structures created by a failed dotdot link 4009 * creation. Adjusts nlinkdelta for non-journaling softdep. 4010 */ 4011void 4012softdep_revert_dotdot_link(dp, ip) 4013 struct inode *dp; 4014 struct inode *ip; 4015{ 4016 struct inodedep *inodedep; 4017 struct jaddref *jaddref; 4018 struct vnode *dvp; 4019 4020 dvp = ITOV(dp); 4021 ACQUIRE_LOCK(&lk); 4022 inodedep = inodedep_lookup_ip(dp); 4023 if (DOINGSUJ(dvp)) { 4024 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4025 inoreflst); 4026 KASSERT(jaddref->ja_parent == ip->i_number, 4027 ("softdep_revert_dotdot_link: addref parent mismatch")); 4028 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4029 } 4030 FREE_LOCK(&lk); 4031} 4032 4033/* 4034 * Called to release the journal structures created by a failed link 4035 * addition. Adjusts nlinkdelta for non-journaling softdep. 4036 */ 4037void 4038softdep_revert_link(dp, ip) 4039 struct inode *dp; 4040 struct inode *ip; 4041{ 4042 struct inodedep *inodedep; 4043 struct jaddref *jaddref; 4044 struct vnode *dvp; 4045 4046 dvp = ITOV(dp); 4047 ACQUIRE_LOCK(&lk); 4048 inodedep = inodedep_lookup_ip(ip); 4049 if (DOINGSUJ(dvp)) { 4050 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4051 inoreflst); 4052 KASSERT(jaddref->ja_parent == dp->i_number, 4053 ("softdep_revert_link: addref parent mismatch")); 4054 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4055 } 4056 FREE_LOCK(&lk); 4057} 4058 4059/* 4060 * Called to release the journal structures created by a failed mkdir 4061 * attempt. Adjusts nlinkdelta for non-journaling softdep. 4062 */ 4063void 4064softdep_revert_mkdir(dp, ip) 4065 struct inode *dp; 4066 struct inode *ip; 4067{ 4068 struct inodedep *inodedep; 4069 struct jaddref *jaddref; 4070 struct vnode *dvp; 4071 4072 dvp = ITOV(dp); 4073 4074 ACQUIRE_LOCK(&lk); 4075 inodedep = inodedep_lookup_ip(dp); 4076 if (DOINGSUJ(dvp)) { 4077 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4078 inoreflst); 4079 KASSERT(jaddref->ja_parent == ip->i_number, 4080 ("softdep_revert_mkdir: dotdot addref parent mismatch")); 4081 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4082 } 4083 inodedep = inodedep_lookup_ip(ip); 4084 if (DOINGSUJ(dvp)) { 4085 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4086 inoreflst); 4087 KASSERT(jaddref->ja_parent == dp->i_number, 4088 ("softdep_revert_mkdir: addref parent mismatch")); 4089 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4090 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4091 inoreflst); 4092 KASSERT(jaddref->ja_parent == ip->i_number, 4093 ("softdep_revert_mkdir: dot addref parent mismatch")); 4094 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4095 } 4096 FREE_LOCK(&lk); 4097} 4098 4099/* 4100 * Called to correct nlinkdelta after a failed rmdir. 4101 */ 4102void 4103softdep_revert_rmdir(dp, ip) 4104 struct inode *dp; 4105 struct inode *ip; 4106{ 4107 4108 ACQUIRE_LOCK(&lk); 4109 (void) inodedep_lookup_ip(ip); 4110 (void) inodedep_lookup_ip(dp); 4111 FREE_LOCK(&lk); 4112} 4113 4114/* 4115 * Protecting the freemaps (or bitmaps). 4116 * 4117 * To eliminate the need to execute fsck before mounting a filesystem 4118 * after a power failure, one must (conservatively) guarantee that the 4119 * on-disk copy of the bitmaps never indicate that a live inode or block is 4120 * free. So, when a block or inode is allocated, the bitmap should be 4121 * updated (on disk) before any new pointers. When a block or inode is 4122 * freed, the bitmap should not be updated until all pointers have been 4123 * reset. The latter dependency is handled by the delayed de-allocation 4124 * approach described below for block and inode de-allocation. The former 4125 * dependency is handled by calling the following procedure when a block or 4126 * inode is allocated. When an inode is allocated an "inodedep" is created 4127 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk. 4128 * Each "inodedep" is also inserted into the hash indexing structure so 4129 * that any additional link additions can be made dependent on the inode 4130 * allocation. 4131 * 4132 * The ufs filesystem maintains a number of free block counts (e.g., per 4133 * cylinder group, per cylinder and per <cylinder, rotational position> pair) 4134 * in addition to the bitmaps. These counts are used to improve efficiency 4135 * during allocation and therefore must be consistent with the bitmaps. 4136 * There is no convenient way to guarantee post-crash consistency of these 4137 * counts with simple update ordering, for two main reasons: (1) The counts 4138 * and bitmaps for a single cylinder group block are not in the same disk 4139 * sector. If a disk write is interrupted (e.g., by power failure), one may 4140 * be written and the other not. (2) Some of the counts are located in the 4141 * superblock rather than the cylinder group block. So, we focus our soft 4142 * updates implementation on protecting the bitmaps. When mounting a 4143 * filesystem, we recompute the auxiliary counts from the bitmaps. 4144 */ 4145 4146/* 4147 * Called just after updating the cylinder group block to allocate an inode. 4148 */ 4149void 4150softdep_setup_inomapdep(bp, ip, newinum) 4151 struct buf *bp; /* buffer for cylgroup block with inode map */ 4152 struct inode *ip; /* inode related to allocation */ 4153 ino_t newinum; /* new inode number being allocated */ 4154{ 4155 struct inodedep *inodedep; 4156 struct bmsafemap *bmsafemap; 4157 struct jaddref *jaddref; 4158 struct mount *mp; 4159 struct fs *fs; 4160 4161 mp = UFSTOVFS(ip->i_ump); 4162 fs = ip->i_ump->um_fs; 4163 jaddref = NULL; 4164 4165 /* 4166 * Allocate the journal reference add structure so that the bitmap 4167 * can be dependent on it. 4168 */ 4169 if (mp->mnt_kern_flag & MNTK_SUJ) { 4170 jaddref = newjaddref(ip, newinum, 0, 0, 0); 4171 jaddref->ja_state |= NEWBLOCK; 4172 } 4173 4174 /* 4175 * Create a dependency for the newly allocated inode. 4176 * Panic if it already exists as something is seriously wrong. 4177 * Otherwise add it to the dependency list for the buffer holding 4178 * the cylinder group map from which it was allocated. 4179 */ 4180 ACQUIRE_LOCK(&lk); 4181 if ((inodedep_lookup(mp, newinum, DEPALLOC|NODELAY, &inodedep))) 4182 panic("softdep_setup_inomapdep: dependency %p for new" 4183 "inode already exists", inodedep); 4184 bmsafemap = bmsafemap_lookup(mp, bp, ino_to_cg(fs, newinum)); 4185 if (jaddref) { 4186 LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps); 4187 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4188 if_deps); 4189 } else { 4190 inodedep->id_state |= ONDEPLIST; 4191 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps); 4192 } 4193 inodedep->id_bmsafemap = bmsafemap; 4194 inodedep->id_state &= ~DEPCOMPLETE; 4195 FREE_LOCK(&lk); 4196} 4197 4198/* 4199 * Called just after updating the cylinder group block to 4200 * allocate block or fragment. 4201 */ 4202void 4203softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags) 4204 struct buf *bp; /* buffer for cylgroup block with block map */ 4205 struct mount *mp; /* filesystem doing allocation */ 4206 ufs2_daddr_t newblkno; /* number of newly allocated block */ 4207 int frags; /* Number of fragments. */ 4208 int oldfrags; /* Previous number of fragments for extend. */ 4209{ 4210 struct newblk *newblk; 4211 struct bmsafemap *bmsafemap; 4212 struct jnewblk *jnewblk; 4213 struct fs *fs; 4214 4215 fs = VFSTOUFS(mp)->um_fs; 4216 jnewblk = NULL; 4217 /* 4218 * Create a dependency for the newly allocated block. 4219 * Add it to the dependency list for the buffer holding 4220 * the cylinder group map from which it was allocated. 4221 */ 4222 if (mp->mnt_kern_flag & MNTK_SUJ) { 4223 jnewblk = malloc(sizeof(*jnewblk), M_JNEWBLK, M_SOFTDEP_FLAGS); 4224 workitem_alloc(&jnewblk->jn_list, D_JNEWBLK, mp); 4225 jnewblk->jn_jsegdep = newjsegdep(&jnewblk->jn_list); 4226 jnewblk->jn_state = ATTACHED; 4227 jnewblk->jn_blkno = newblkno; 4228 jnewblk->jn_frags = frags; 4229 jnewblk->jn_oldfrags = oldfrags; 4230#ifdef SUJ_DEBUG 4231 { 4232 struct cg *cgp; 4233 uint8_t *blksfree; 4234 long bno; 4235 int i; 4236 4237 cgp = (struct cg *)bp->b_data; 4238 blksfree = cg_blksfree(cgp); 4239 bno = dtogd(fs, jnewblk->jn_blkno); 4240 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; 4241 i++) { 4242 if (isset(blksfree, bno + i)) 4243 panic("softdep_setup_blkmapdep: " 4244 "free fragment %d from %d-%d " 4245 "state 0x%X dep %p", i, 4246 jnewblk->jn_oldfrags, 4247 jnewblk->jn_frags, 4248 jnewblk->jn_state, 4249 jnewblk->jn_newblk); 4250 } 4251 } 4252#endif 4253 } 4254 ACQUIRE_LOCK(&lk); 4255 if (newblk_lookup(mp, newblkno, DEPALLOC, &newblk) != 0) 4256 panic("softdep_setup_blkmapdep: found block"); 4257 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp, 4258 dtog(fs, newblkno)); 4259 if (jnewblk) { 4260 jnewblk->jn_newblk = newblk; 4261 LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps); 4262 } else { 4263 newblk->nb_state |= ONDEPLIST; 4264 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps); 4265 } 4266 newblk->nb_bmsafemap = bmsafemap; 4267 newblk->nb_jnewblk = jnewblk; 4268 FREE_LOCK(&lk); 4269} 4270 4271#define BMSAFEMAP_HASH(fs, cg) \ 4272 (&bmsafemap_hashtbl[((((register_t)(fs)) >> 13) + (cg)) & bmsafemap_hash]) 4273 4274static int 4275bmsafemap_find(bmsafemaphd, mp, cg, bmsafemapp) 4276 struct bmsafemap_hashhead *bmsafemaphd; 4277 struct mount *mp; 4278 int cg; 4279 struct bmsafemap **bmsafemapp; 4280{ 4281 struct bmsafemap *bmsafemap; 4282 4283 LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash) 4284 if (bmsafemap->sm_list.wk_mp == mp && bmsafemap->sm_cg == cg) 4285 break; 4286 if (bmsafemap) { 4287 *bmsafemapp = bmsafemap; 4288 return (1); 4289 } 4290 *bmsafemapp = NULL; 4291 4292 return (0); 4293} 4294 4295/* 4296 * Find the bmsafemap associated with a cylinder group buffer. 4297 * If none exists, create one. The buffer must be locked when 4298 * this routine is called and this routine must be called with 4299 * splbio interrupts blocked. 4300 */ 4301static struct bmsafemap * 4302bmsafemap_lookup(mp, bp, cg) 4303 struct mount *mp; 4304 struct buf *bp; 4305 int cg; 4306{ 4307 struct bmsafemap_hashhead *bmsafemaphd; 4308 struct bmsafemap *bmsafemap, *collision; 4309 struct worklist *wk; 4310 struct fs *fs; 4311 4312 mtx_assert(&lk, MA_OWNED); 4313 if (bp) 4314 LIST_FOREACH(wk, &bp->b_dep, wk_list) 4315 if (wk->wk_type == D_BMSAFEMAP) 4316 return (WK_BMSAFEMAP(wk)); 4317 fs = VFSTOUFS(mp)->um_fs; 4318 bmsafemaphd = BMSAFEMAP_HASH(fs, cg); 4319 if (bmsafemap_find(bmsafemaphd, mp, cg, &bmsafemap) == 1) 4320 return (bmsafemap); 4321 FREE_LOCK(&lk); 4322 bmsafemap = malloc(sizeof(struct bmsafemap), 4323 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 4324 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 4325 bmsafemap->sm_buf = bp; 4326 LIST_INIT(&bmsafemap->sm_inodedephd); 4327 LIST_INIT(&bmsafemap->sm_inodedepwr); 4328 LIST_INIT(&bmsafemap->sm_newblkhd); 4329 LIST_INIT(&bmsafemap->sm_newblkwr); 4330 LIST_INIT(&bmsafemap->sm_jaddrefhd); 4331 LIST_INIT(&bmsafemap->sm_jnewblkhd); 4332 ACQUIRE_LOCK(&lk); 4333 if (bmsafemap_find(bmsafemaphd, mp, cg, &collision) == 1) { 4334 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 4335 return (collision); 4336 } 4337 bmsafemap->sm_cg = cg; 4338 LIST_INSERT_HEAD(bmsafemaphd, bmsafemap, sm_hash); 4339 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list); 4340 return (bmsafemap); 4341} 4342 4343/* 4344 * Direct block allocation dependencies. 4345 * 4346 * When a new block is allocated, the corresponding disk locations must be 4347 * initialized (with zeros or new data) before the on-disk inode points to 4348 * them. Also, the freemap from which the block was allocated must be 4349 * updated (on disk) before the inode's pointer. These two dependencies are 4350 * independent of each other and are needed for all file blocks and indirect 4351 * blocks that are pointed to directly by the inode. Just before the 4352 * "in-core" version of the inode is updated with a newly allocated block 4353 * number, a procedure (below) is called to setup allocation dependency 4354 * structures. These structures are removed when the corresponding 4355 * dependencies are satisfied or when the block allocation becomes obsolete 4356 * (i.e., the file is deleted, the block is de-allocated, or the block is a 4357 * fragment that gets upgraded). All of these cases are handled in 4358 * procedures described later. 4359 * 4360 * When a file extension causes a fragment to be upgraded, either to a larger 4361 * fragment or to a full block, the on-disk location may change (if the 4362 * previous fragment could not simply be extended). In this case, the old 4363 * fragment must be de-allocated, but not until after the inode's pointer has 4364 * been updated. In most cases, this is handled by later procedures, which 4365 * will construct a "freefrag" structure to be added to the workitem queue 4366 * when the inode update is complete (or obsolete). The main exception to 4367 * this is when an allocation occurs while a pending allocation dependency 4368 * (for the same block pointer) remains. This case is handled in the main 4369 * allocation dependency setup procedure by immediately freeing the 4370 * unreferenced fragments. 4371 */ 4372void 4373softdep_setup_allocdirect(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 4374 struct inode *ip; /* inode to which block is being added */ 4375 ufs_lbn_t off; /* block pointer within inode */ 4376 ufs2_daddr_t newblkno; /* disk block number being added */ 4377 ufs2_daddr_t oldblkno; /* previous block number, 0 unless frag */ 4378 long newsize; /* size of new block */ 4379 long oldsize; /* size of new block */ 4380 struct buf *bp; /* bp for allocated block */ 4381{ 4382 struct allocdirect *adp, *oldadp; 4383 struct allocdirectlst *adphead; 4384 struct freefrag *freefrag; 4385 struct inodedep *inodedep; 4386 struct pagedep *pagedep; 4387 struct jnewblk *jnewblk; 4388 struct newblk *newblk; 4389 struct mount *mp; 4390 ufs_lbn_t lbn; 4391 4392 lbn = bp->b_lblkno; 4393 mp = UFSTOVFS(ip->i_ump); 4394 if (oldblkno && oldblkno != newblkno) 4395 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn); 4396 else 4397 freefrag = NULL; 4398 4399 ACQUIRE_LOCK(&lk); 4400 if (off >= NDADDR) { 4401 if (lbn > 0) 4402 panic("softdep_setup_allocdirect: bad lbn %jd, off %jd", 4403 lbn, off); 4404 /* allocating an indirect block */ 4405 if (oldblkno != 0) 4406 panic("softdep_setup_allocdirect: non-zero indir"); 4407 } else { 4408 if (off != lbn) 4409 panic("softdep_setup_allocdirect: lbn %jd != off %jd", 4410 lbn, off); 4411 /* 4412 * Allocating a direct block. 4413 * 4414 * If we are allocating a directory block, then we must 4415 * allocate an associated pagedep to track additions and 4416 * deletions. 4417 */ 4418 if ((ip->i_mode & IFMT) == IFDIR && 4419 pagedep_lookup(mp, ip->i_number, off, DEPALLOC, 4420 &pagedep) == 0) 4421 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list); 4422 } 4423 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 4424 panic("softdep_setup_allocdirect: lost block"); 4425 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 4426 ("softdep_setup_allocdirect: newblk already initialized")); 4427 /* 4428 * Convert the newblk to an allocdirect. 4429 */ 4430 newblk->nb_list.wk_type = D_ALLOCDIRECT; 4431 adp = (struct allocdirect *)newblk; 4432 newblk->nb_freefrag = freefrag; 4433 adp->ad_offset = off; 4434 adp->ad_oldblkno = oldblkno; 4435 adp->ad_newsize = newsize; 4436 adp->ad_oldsize = oldsize; 4437 4438 /* 4439 * Finish initializing the journal. 4440 */ 4441 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 4442 jnewblk->jn_ino = ip->i_number; 4443 jnewblk->jn_lbn = lbn; 4444 add_to_journal(&jnewblk->jn_list); 4445 } 4446 if (freefrag && freefrag->ff_jfreefrag != NULL) 4447 add_to_journal(&freefrag->ff_jfreefrag->fr_list); 4448 inodedep_lookup(mp, ip->i_number, DEPALLOC | NODELAY, &inodedep); 4449 adp->ad_inodedep = inodedep; 4450 4451 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 4452 /* 4453 * The list of allocdirects must be kept in sorted and ascending 4454 * order so that the rollback routines can quickly determine the 4455 * first uncommitted block (the size of the file stored on disk 4456 * ends at the end of the lowest committed fragment, or if there 4457 * are no fragments, at the end of the highest committed block). 4458 * Since files generally grow, the typical case is that the new 4459 * block is to be added at the end of the list. We speed this 4460 * special case by checking against the last allocdirect in the 4461 * list before laboriously traversing the list looking for the 4462 * insertion point. 4463 */ 4464 adphead = &inodedep->id_newinoupdt; 4465 oldadp = TAILQ_LAST(adphead, allocdirectlst); 4466 if (oldadp == NULL || oldadp->ad_offset <= off) { 4467 /* insert at end of list */ 4468 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 4469 if (oldadp != NULL && oldadp->ad_offset == off) 4470 allocdirect_merge(adphead, adp, oldadp); 4471 FREE_LOCK(&lk); 4472 return; 4473 } 4474 TAILQ_FOREACH(oldadp, adphead, ad_next) { 4475 if (oldadp->ad_offset >= off) 4476 break; 4477 } 4478 if (oldadp == NULL) 4479 panic("softdep_setup_allocdirect: lost entry"); 4480 /* insert in middle of list */ 4481 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 4482 if (oldadp->ad_offset == off) 4483 allocdirect_merge(adphead, adp, oldadp); 4484 4485 FREE_LOCK(&lk); 4486} 4487 4488/* 4489 * Replace an old allocdirect dependency with a newer one. 4490 * This routine must be called with splbio interrupts blocked. 4491 */ 4492static void 4493allocdirect_merge(adphead, newadp, oldadp) 4494 struct allocdirectlst *adphead; /* head of list holding allocdirects */ 4495 struct allocdirect *newadp; /* allocdirect being added */ 4496 struct allocdirect *oldadp; /* existing allocdirect being checked */ 4497{ 4498 struct worklist *wk; 4499 struct freefrag *freefrag; 4500 struct newdirblk *newdirblk; 4501 4502 freefrag = NULL; 4503 mtx_assert(&lk, MA_OWNED); 4504 if (newadp->ad_oldblkno != oldadp->ad_newblkno || 4505 newadp->ad_oldsize != oldadp->ad_newsize || 4506 newadp->ad_offset >= NDADDR) 4507 panic("%s %jd != new %jd || old size %ld != new %ld", 4508 "allocdirect_merge: old blkno", 4509 (intmax_t)newadp->ad_oldblkno, 4510 (intmax_t)oldadp->ad_newblkno, 4511 newadp->ad_oldsize, oldadp->ad_newsize); 4512 newadp->ad_oldblkno = oldadp->ad_oldblkno; 4513 newadp->ad_oldsize = oldadp->ad_oldsize; 4514 /* 4515 * If the old dependency had a fragment to free or had never 4516 * previously had a block allocated, then the new dependency 4517 * can immediately post its freefrag and adopt the old freefrag. 4518 * This action is done by swapping the freefrag dependencies. 4519 * The new dependency gains the old one's freefrag, and the 4520 * old one gets the new one and then immediately puts it on 4521 * the worklist when it is freed by free_newblk. It is 4522 * not possible to do this swap when the old dependency had a 4523 * non-zero size but no previous fragment to free. This condition 4524 * arises when the new block is an extension of the old block. 4525 * Here, the first part of the fragment allocated to the new 4526 * dependency is part of the block currently claimed on disk by 4527 * the old dependency, so cannot legitimately be freed until the 4528 * conditions for the new dependency are fulfilled. 4529 */ 4530 freefrag = newadp->ad_freefrag; 4531 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) { 4532 newadp->ad_freefrag = oldadp->ad_freefrag; 4533 oldadp->ad_freefrag = freefrag; 4534 } 4535 /* 4536 * If we are tracking a new directory-block allocation, 4537 * move it from the old allocdirect to the new allocdirect. 4538 */ 4539 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) { 4540 newdirblk = WK_NEWDIRBLK(wk); 4541 WORKLIST_REMOVE(&newdirblk->db_list); 4542 if (!LIST_EMPTY(&oldadp->ad_newdirblk)) 4543 panic("allocdirect_merge: extra newdirblk"); 4544 WORKLIST_INSERT(&newadp->ad_newdirblk, &newdirblk->db_list); 4545 } 4546 TAILQ_REMOVE(adphead, oldadp, ad_next); 4547 /* 4548 * We need to move any journal dependencies over to the freefrag 4549 * that releases this block if it exists. Otherwise we are 4550 * extending an existing block and we'll wait until that is 4551 * complete to release the journal space and extend the 4552 * new journal to cover this old space as well. 4553 */ 4554 if (freefrag == NULL) { 4555 struct jnewblk *jnewblk; 4556 struct jnewblk *njnewblk; 4557 4558 if (oldadp->ad_newblkno != newadp->ad_newblkno) 4559 panic("allocdirect_merge: %jd != %jd", 4560 oldadp->ad_newblkno, newadp->ad_newblkno); 4561 jnewblk = oldadp->ad_block.nb_jnewblk; 4562 cancel_newblk(&oldadp->ad_block, &newadp->ad_block.nb_jwork); 4563 /* 4564 * We have an unwritten jnewblk, we need to merge the 4565 * frag bits with our own. The newer adp's journal can not 4566 * be written prior to the old one so no need to check for 4567 * it here. 4568 */ 4569 if (jnewblk) { 4570 njnewblk = newadp->ad_block.nb_jnewblk; 4571 if (njnewblk == NULL) 4572 panic("allocdirect_merge: No jnewblk"); 4573 if (jnewblk->jn_state & UNDONE) { 4574 njnewblk->jn_state |= UNDONE | NEWBLOCK; 4575 njnewblk->jn_state &= ~ATTACHED; 4576 jnewblk->jn_state &= ~UNDONE; 4577 } 4578 njnewblk->jn_oldfrags = jnewblk->jn_oldfrags; 4579 WORKLIST_REMOVE(&jnewblk->jn_list); 4580 jnewblk->jn_state |= ATTACHED | COMPLETE; 4581 free_jnewblk(jnewblk); 4582 } 4583 } else { 4584 /* 4585 * We can skip journaling for this freefrag and just complete 4586 * any pending journal work for the allocdirect that is being 4587 * removed after the freefrag completes. 4588 */ 4589 if (freefrag->ff_jfreefrag) 4590 cancel_jfreefrag(freefrag->ff_jfreefrag); 4591 cancel_newblk(&oldadp->ad_block, &freefrag->ff_jwork); 4592 } 4593 free_newblk(&oldadp->ad_block); 4594} 4595 4596/* 4597 * Allocate a jfreefrag structure to journal a single block free. 4598 */ 4599static struct jfreefrag * 4600newjfreefrag(freefrag, ip, blkno, size, lbn) 4601 struct freefrag *freefrag; 4602 struct inode *ip; 4603 ufs2_daddr_t blkno; 4604 long size; 4605 ufs_lbn_t lbn; 4606{ 4607 struct jfreefrag *jfreefrag; 4608 struct fs *fs; 4609 4610 fs = ip->i_fs; 4611 jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG, 4612 M_SOFTDEP_FLAGS); 4613 workitem_alloc(&jfreefrag->fr_list, D_JFREEFRAG, UFSTOVFS(ip->i_ump)); 4614 jfreefrag->fr_jsegdep = newjsegdep(&jfreefrag->fr_list); 4615 jfreefrag->fr_state = ATTACHED | DEPCOMPLETE; 4616 jfreefrag->fr_ino = ip->i_number; 4617 jfreefrag->fr_lbn = lbn; 4618 jfreefrag->fr_blkno = blkno; 4619 jfreefrag->fr_frags = numfrags(fs, size); 4620 jfreefrag->fr_freefrag = freefrag; 4621 4622 return (jfreefrag); 4623} 4624 4625/* 4626 * Allocate a new freefrag structure. 4627 */ 4628static struct freefrag * 4629newfreefrag(ip, blkno, size, lbn) 4630 struct inode *ip; 4631 ufs2_daddr_t blkno; 4632 long size; 4633 ufs_lbn_t lbn; 4634{ 4635 struct freefrag *freefrag; 4636 struct fs *fs; 4637 4638 fs = ip->i_fs; 4639 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag) 4640 panic("newfreefrag: frag size"); 4641 freefrag = malloc(sizeof(struct freefrag), 4642 M_FREEFRAG, M_SOFTDEP_FLAGS); 4643 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ip->i_ump)); 4644 freefrag->ff_state = ATTACHED; 4645 LIST_INIT(&freefrag->ff_jwork); 4646 freefrag->ff_inum = ip->i_number; 4647 freefrag->ff_blkno = blkno; 4648 freefrag->ff_fragsize = size; 4649 4650 if (fs->fs_flags & FS_SUJ) { 4651 freefrag->ff_jfreefrag = 4652 newjfreefrag(freefrag, ip, blkno, size, lbn); 4653 } else { 4654 freefrag->ff_state |= DEPCOMPLETE; 4655 freefrag->ff_jfreefrag = NULL; 4656 } 4657 4658 return (freefrag); 4659} 4660 4661/* 4662 * This workitem de-allocates fragments that were replaced during 4663 * file block allocation. 4664 */ 4665static void 4666handle_workitem_freefrag(freefrag) 4667 struct freefrag *freefrag; 4668{ 4669 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp); 4670 struct workhead wkhd; 4671 4672 /* 4673 * It would be illegal to add new completion items to the 4674 * freefrag after it was schedule to be done so it must be 4675 * safe to modify the list head here. 4676 */ 4677 LIST_INIT(&wkhd); 4678 LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list); 4679 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno, 4680 freefrag->ff_fragsize, freefrag->ff_inum, &wkhd); 4681 ACQUIRE_LOCK(&lk); 4682 WORKITEM_FREE(freefrag, D_FREEFRAG); 4683 FREE_LOCK(&lk); 4684} 4685 4686/* 4687 * Set up a dependency structure for an external attributes data block. 4688 * This routine follows much of the structure of softdep_setup_allocdirect. 4689 * See the description of softdep_setup_allocdirect above for details. 4690 */ 4691void 4692softdep_setup_allocext(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 4693 struct inode *ip; 4694 ufs_lbn_t off; 4695 ufs2_daddr_t newblkno; 4696 ufs2_daddr_t oldblkno; 4697 long newsize; 4698 long oldsize; 4699 struct buf *bp; 4700{ 4701 struct allocdirect *adp, *oldadp; 4702 struct allocdirectlst *adphead; 4703 struct freefrag *freefrag; 4704 struct inodedep *inodedep; 4705 struct jnewblk *jnewblk; 4706 struct newblk *newblk; 4707 struct mount *mp; 4708 ufs_lbn_t lbn; 4709 4710 if (off >= NXADDR) 4711 panic("softdep_setup_allocext: lbn %lld > NXADDR", 4712 (long long)off); 4713 4714 lbn = bp->b_lblkno; 4715 mp = UFSTOVFS(ip->i_ump); 4716 if (oldblkno && oldblkno != newblkno) 4717 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn); 4718 else 4719 freefrag = NULL; 4720 4721 ACQUIRE_LOCK(&lk); 4722 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 4723 panic("softdep_setup_allocext: lost block"); 4724 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 4725 ("softdep_setup_allocext: newblk already initialized")); 4726 /* 4727 * Convert the newblk to an allocdirect. 4728 */ 4729 newblk->nb_list.wk_type = D_ALLOCDIRECT; 4730 adp = (struct allocdirect *)newblk; 4731 newblk->nb_freefrag = freefrag; 4732 adp->ad_offset = off; 4733 adp->ad_oldblkno = oldblkno; 4734 adp->ad_newsize = newsize; 4735 adp->ad_oldsize = oldsize; 4736 adp->ad_state |= EXTDATA; 4737 4738 /* 4739 * Finish initializing the journal. 4740 */ 4741 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 4742 jnewblk->jn_ino = ip->i_number; 4743 jnewblk->jn_lbn = lbn; 4744 add_to_journal(&jnewblk->jn_list); 4745 } 4746 if (freefrag && freefrag->ff_jfreefrag != NULL) 4747 add_to_journal(&freefrag->ff_jfreefrag->fr_list); 4748 inodedep_lookup(mp, ip->i_number, DEPALLOC | NODELAY, &inodedep); 4749 adp->ad_inodedep = inodedep; 4750 4751 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 4752 /* 4753 * The list of allocdirects must be kept in sorted and ascending 4754 * order so that the rollback routines can quickly determine the 4755 * first uncommitted block (the size of the file stored on disk 4756 * ends at the end of the lowest committed fragment, or if there 4757 * are no fragments, at the end of the highest committed block). 4758 * Since files generally grow, the typical case is that the new 4759 * block is to be added at the end of the list. We speed this 4760 * special case by checking against the last allocdirect in the 4761 * list before laboriously traversing the list looking for the 4762 * insertion point. 4763 */ 4764 adphead = &inodedep->id_newextupdt; 4765 oldadp = TAILQ_LAST(adphead, allocdirectlst); 4766 if (oldadp == NULL || oldadp->ad_offset <= off) { 4767 /* insert at end of list */ 4768 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 4769 if (oldadp != NULL && oldadp->ad_offset == off) 4770 allocdirect_merge(adphead, adp, oldadp); 4771 FREE_LOCK(&lk); 4772 return; 4773 } 4774 TAILQ_FOREACH(oldadp, adphead, ad_next) { 4775 if (oldadp->ad_offset >= off) 4776 break; 4777 } 4778 if (oldadp == NULL) 4779 panic("softdep_setup_allocext: lost entry"); 4780 /* insert in middle of list */ 4781 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 4782 if (oldadp->ad_offset == off) 4783 allocdirect_merge(adphead, adp, oldadp); 4784 FREE_LOCK(&lk); 4785} 4786 4787/* 4788 * Indirect block allocation dependencies. 4789 * 4790 * The same dependencies that exist for a direct block also exist when 4791 * a new block is allocated and pointed to by an entry in a block of 4792 * indirect pointers. The undo/redo states described above are also 4793 * used here. Because an indirect block contains many pointers that 4794 * may have dependencies, a second copy of the entire in-memory indirect 4795 * block is kept. The buffer cache copy is always completely up-to-date. 4796 * The second copy, which is used only as a source for disk writes, 4797 * contains only the safe pointers (i.e., those that have no remaining 4798 * update dependencies). The second copy is freed when all pointers 4799 * are safe. The cache is not allowed to replace indirect blocks with 4800 * pending update dependencies. If a buffer containing an indirect 4801 * block with dependencies is written, these routines will mark it 4802 * dirty again. It can only be successfully written once all the 4803 * dependencies are removed. The ffs_fsync routine in conjunction with 4804 * softdep_sync_metadata work together to get all the dependencies 4805 * removed so that a file can be successfully written to disk. Three 4806 * procedures are used when setting up indirect block pointer 4807 * dependencies. The division is necessary because of the organization 4808 * of the "balloc" routine and because of the distinction between file 4809 * pages and file metadata blocks. 4810 */ 4811 4812/* 4813 * Allocate a new allocindir structure. 4814 */ 4815static struct allocindir * 4816newallocindir(ip, ptrno, newblkno, oldblkno, lbn) 4817 struct inode *ip; /* inode for file being extended */ 4818 int ptrno; /* offset of pointer in indirect block */ 4819 ufs2_daddr_t newblkno; /* disk block number being added */ 4820 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 4821 ufs_lbn_t lbn; 4822{ 4823 struct newblk *newblk; 4824 struct allocindir *aip; 4825 struct freefrag *freefrag; 4826 struct jnewblk *jnewblk; 4827 4828 if (oldblkno) 4829 freefrag = newfreefrag(ip, oldblkno, ip->i_fs->fs_bsize, lbn); 4830 else 4831 freefrag = NULL; 4832 ACQUIRE_LOCK(&lk); 4833 if (newblk_lookup(UFSTOVFS(ip->i_ump), newblkno, 0, &newblk) == 0) 4834 panic("new_allocindir: lost block"); 4835 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 4836 ("newallocindir: newblk already initialized")); 4837 newblk->nb_list.wk_type = D_ALLOCINDIR; 4838 newblk->nb_freefrag = freefrag; 4839 aip = (struct allocindir *)newblk; 4840 aip->ai_offset = ptrno; 4841 aip->ai_oldblkno = oldblkno; 4842 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 4843 jnewblk->jn_ino = ip->i_number; 4844 jnewblk->jn_lbn = lbn; 4845 add_to_journal(&jnewblk->jn_list); 4846 } 4847 if (freefrag && freefrag->ff_jfreefrag != NULL) 4848 add_to_journal(&freefrag->ff_jfreefrag->fr_list); 4849 return (aip); 4850} 4851 4852/* 4853 * Called just before setting an indirect block pointer 4854 * to a newly allocated file page. 4855 */ 4856void 4857softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 4858 struct inode *ip; /* inode for file being extended */ 4859 ufs_lbn_t lbn; /* allocated block number within file */ 4860 struct buf *bp; /* buffer with indirect blk referencing page */ 4861 int ptrno; /* offset of pointer in indirect block */ 4862 ufs2_daddr_t newblkno; /* disk block number being added */ 4863 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 4864 struct buf *nbp; /* buffer holding allocated page */ 4865{ 4866 struct inodedep *inodedep; 4867 struct allocindir *aip; 4868 struct pagedep *pagedep; 4869 struct mount *mp; 4870 4871 if (lbn != nbp->b_lblkno) 4872 panic("softdep_setup_allocindir_page: lbn %jd != lblkno %jd", 4873 lbn, bp->b_lblkno); 4874 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page"); 4875 mp = UFSTOVFS(ip->i_ump); 4876 aip = newallocindir(ip, ptrno, newblkno, oldblkno, lbn); 4877 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 4878 /* 4879 * If we are allocating a directory page, then we must 4880 * allocate an associated pagedep to track additions and 4881 * deletions. 4882 */ 4883 if ((ip->i_mode & IFMT) == IFDIR && 4884 pagedep_lookup(mp, ip->i_number, lbn, DEPALLOC, &pagedep) == 0) 4885 WORKLIST_INSERT(&nbp->b_dep, &pagedep->pd_list); 4886 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 4887 setup_allocindir_phase2(bp, ip, inodedep, aip, lbn); 4888 FREE_LOCK(&lk); 4889} 4890 4891/* 4892 * Called just before setting an indirect block pointer to a 4893 * newly allocated indirect block. 4894 */ 4895void 4896softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 4897 struct buf *nbp; /* newly allocated indirect block */ 4898 struct inode *ip; /* inode for file being extended */ 4899 struct buf *bp; /* indirect block referencing allocated block */ 4900 int ptrno; /* offset of pointer in indirect block */ 4901 ufs2_daddr_t newblkno; /* disk block number being added */ 4902{ 4903 struct inodedep *inodedep; 4904 struct allocindir *aip; 4905 ufs_lbn_t lbn; 4906 4907 lbn = nbp->b_lblkno; 4908 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta"); 4909 aip = newallocindir(ip, ptrno, newblkno, 0, lbn); 4910 inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, DEPALLOC, &inodedep); 4911 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 4912 setup_allocindir_phase2(bp, ip, inodedep, aip, lbn); 4913 FREE_LOCK(&lk); 4914} 4915 4916static void 4917indirdep_complete(indirdep) 4918 struct indirdep *indirdep; 4919{ 4920 struct allocindir *aip; 4921 4922 LIST_REMOVE(indirdep, ir_next); 4923 indirdep->ir_state &= ~ONDEPLIST; 4924 4925 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) { 4926 LIST_REMOVE(aip, ai_next); 4927 free_newblk(&aip->ai_block); 4928 } 4929 /* 4930 * If this indirdep is not attached to a buf it was simply waiting 4931 * on completion to clear completehd. free_indirdep() asserts 4932 * that nothing is dangling. 4933 */ 4934 if ((indirdep->ir_state & ONWORKLIST) == 0) 4935 free_indirdep(indirdep); 4936} 4937 4938/* 4939 * Called to finish the allocation of the "aip" allocated 4940 * by one of the two routines above. 4941 */ 4942static void 4943setup_allocindir_phase2(bp, ip, inodedep, aip, lbn) 4944 struct buf *bp; /* in-memory copy of the indirect block */ 4945 struct inode *ip; /* inode for file being extended */ 4946 struct inodedep *inodedep; /* Inodedep for ip */ 4947 struct allocindir *aip; /* allocindir allocated by the above routines */ 4948 ufs_lbn_t lbn; /* Logical block number for this block. */ 4949{ 4950 struct worklist *wk; 4951 struct fs *fs; 4952 struct newblk *newblk; 4953 struct indirdep *indirdep, *newindirdep; 4954 struct allocindir *oldaip; 4955 struct freefrag *freefrag; 4956 struct mount *mp; 4957 ufs2_daddr_t blkno; 4958 4959 mp = UFSTOVFS(ip->i_ump); 4960 fs = ip->i_fs; 4961 mtx_assert(&lk, MA_OWNED); 4962 if (bp->b_lblkno >= 0) 4963 panic("setup_allocindir_phase2: not indir blk"); 4964 for (freefrag = NULL, indirdep = NULL, newindirdep = NULL; ; ) { 4965 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 4966 if (wk->wk_type != D_INDIRDEP) 4967 continue; 4968 indirdep = WK_INDIRDEP(wk); 4969 break; 4970 } 4971 if (indirdep == NULL && newindirdep) { 4972 indirdep = newindirdep; 4973 newindirdep = NULL; 4974 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list); 4975 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, 4976 &newblk)) { 4977 indirdep->ir_state |= ONDEPLIST; 4978 LIST_INSERT_HEAD(&newblk->nb_indirdeps, 4979 indirdep, ir_next); 4980 } else 4981 indirdep->ir_state |= DEPCOMPLETE; 4982 } 4983 if (indirdep) { 4984 aip->ai_indirdep = indirdep; 4985 /* 4986 * Check to see if there is an existing dependency 4987 * for this block. If there is, merge the old 4988 * dependency into the new one. This happens 4989 * as a result of reallocblk only. 4990 */ 4991 if (aip->ai_oldblkno == 0) 4992 oldaip = NULL; 4993 else 4994 4995 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, 4996 ai_next) 4997 if (oldaip->ai_offset == aip->ai_offset) 4998 break; 4999 if (oldaip != NULL) 5000 freefrag = allocindir_merge(aip, oldaip); 5001 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next); 5002 KASSERT(aip->ai_offset >= 0 && 5003 aip->ai_offset < NINDIR(ip->i_ump->um_fs), 5004 ("setup_allocindir_phase2: Bad offset %d", 5005 aip->ai_offset)); 5006 KASSERT(indirdep->ir_savebp != NULL, 5007 ("setup_allocindir_phase2 NULL ir_savebp")); 5008 if (ip->i_ump->um_fstype == UFS1) 5009 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data) 5010 [aip->ai_offset] = aip->ai_oldblkno; 5011 else 5012 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data) 5013 [aip->ai_offset] = aip->ai_oldblkno; 5014 FREE_LOCK(&lk); 5015 if (freefrag != NULL) 5016 handle_workitem_freefrag(freefrag); 5017 } else 5018 FREE_LOCK(&lk); 5019 if (newindirdep) { 5020 newindirdep->ir_savebp->b_flags |= B_INVAL | B_NOCACHE; 5021 brelse(newindirdep->ir_savebp); 5022 ACQUIRE_LOCK(&lk); 5023 WORKITEM_FREE((caddr_t)newindirdep, D_INDIRDEP); 5024 if (indirdep) 5025 break; 5026 FREE_LOCK(&lk); 5027 } 5028 if (indirdep) { 5029 ACQUIRE_LOCK(&lk); 5030 break; 5031 } 5032 newindirdep = malloc(sizeof(struct indirdep), 5033 M_INDIRDEP, M_SOFTDEP_FLAGS); 5034 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, mp); 5035 newindirdep->ir_state = ATTACHED; 5036 if (ip->i_ump->um_fstype == UFS1) 5037 newindirdep->ir_state |= UFS1FMT; 5038 newindirdep->ir_saveddata = NULL; 5039 LIST_INIT(&newindirdep->ir_deplisthd); 5040 LIST_INIT(&newindirdep->ir_donehd); 5041 LIST_INIT(&newindirdep->ir_writehd); 5042 LIST_INIT(&newindirdep->ir_completehd); 5043 LIST_INIT(&newindirdep->ir_jwork); 5044 if (bp->b_blkno == bp->b_lblkno) { 5045 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp, 5046 NULL, NULL); 5047 bp->b_blkno = blkno; 5048 } 5049 newindirdep->ir_savebp = 5050 getblk(ip->i_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0); 5051 BUF_KERNPROC(newindirdep->ir_savebp); 5052 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount); 5053 ACQUIRE_LOCK(&lk); 5054 } 5055} 5056 5057/* 5058 * Merge two allocindirs which refer to the same block. Move newblock 5059 * dependencies and setup the freefrags appropriately. 5060 */ 5061static struct freefrag * 5062allocindir_merge(aip, oldaip) 5063 struct allocindir *aip; 5064 struct allocindir *oldaip; 5065{ 5066 struct newdirblk *newdirblk; 5067 struct freefrag *freefrag; 5068 struct worklist *wk; 5069 5070 if (oldaip->ai_newblkno != aip->ai_oldblkno) 5071 panic("allocindir_merge: blkno"); 5072 aip->ai_oldblkno = oldaip->ai_oldblkno; 5073 freefrag = aip->ai_freefrag; 5074 aip->ai_freefrag = oldaip->ai_freefrag; 5075 oldaip->ai_freefrag = NULL; 5076 KASSERT(freefrag != NULL, ("setup_allocindir_phase2: No freefrag")); 5077 /* 5078 * If we are tracking a new directory-block allocation, 5079 * move it from the old allocindir to the new allocindir. 5080 */ 5081 if ((wk = LIST_FIRST(&oldaip->ai_newdirblk)) != NULL) { 5082 newdirblk = WK_NEWDIRBLK(wk); 5083 WORKLIST_REMOVE(&newdirblk->db_list); 5084 if (!LIST_EMPTY(&oldaip->ai_newdirblk)) 5085 panic("allocindir_merge: extra newdirblk"); 5086 WORKLIST_INSERT(&aip->ai_newdirblk, &newdirblk->db_list); 5087 } 5088 /* 5089 * We can skip journaling for this freefrag and just complete 5090 * any pending journal work for the allocindir that is being 5091 * removed after the freefrag completes. 5092 */ 5093 if (freefrag->ff_jfreefrag) 5094 cancel_jfreefrag(freefrag->ff_jfreefrag); 5095 LIST_REMOVE(oldaip, ai_next); 5096 cancel_newblk(&oldaip->ai_block, &freefrag->ff_jwork); 5097 free_newblk(&oldaip->ai_block); 5098 5099 return (freefrag); 5100} 5101 5102/* 5103 * Block de-allocation dependencies. 5104 * 5105 * When blocks are de-allocated, the on-disk pointers must be nullified before 5106 * the blocks are made available for use by other files. (The true 5107 * requirement is that old pointers must be nullified before new on-disk 5108 * pointers are set. We chose this slightly more stringent requirement to 5109 * reduce complexity.) Our implementation handles this dependency by updating 5110 * the inode (or indirect block) appropriately but delaying the actual block 5111 * de-allocation (i.e., freemap and free space count manipulation) until 5112 * after the updated versions reach stable storage. After the disk is 5113 * updated, the blocks can be safely de-allocated whenever it is convenient. 5114 * This implementation handles only the common case of reducing a file's 5115 * length to zero. Other cases are handled by the conventional synchronous 5116 * write approach. 5117 * 5118 * The ffs implementation with which we worked double-checks 5119 * the state of the block pointers and file size as it reduces 5120 * a file's length. Some of this code is replicated here in our 5121 * soft updates implementation. The freeblks->fb_chkcnt field is 5122 * used to transfer a part of this information to the procedure 5123 * that eventually de-allocates the blocks. 5124 * 5125 * This routine should be called from the routine that shortens 5126 * a file's length, before the inode's size or block pointers 5127 * are modified. It will save the block pointer information for 5128 * later release and zero the inode so that the calling routine 5129 * can release it. 5130 */ 5131void 5132softdep_setup_freeblocks(ip, length, flags) 5133 struct inode *ip; /* The inode whose length is to be reduced */ 5134 off_t length; /* The new length for the file */ 5135 int flags; /* IO_EXT and/or IO_NORMAL */ 5136{ 5137 struct ufs1_dinode *dp1; 5138 struct ufs2_dinode *dp2; 5139 struct freeblks *freeblks; 5140 struct inodedep *inodedep; 5141 struct allocdirect *adp; 5142 struct jfreeblk *jfreeblk; 5143 struct bufobj *bo; 5144 struct vnode *vp; 5145 struct buf *bp; 5146 struct fs *fs; 5147 ufs2_daddr_t extblocks, datablocks; 5148 struct mount *mp; 5149 int i, delay, error; 5150 ufs2_daddr_t blkno; 5151 ufs_lbn_t tmpval; 5152 ufs_lbn_t lbn; 5153 long oldextsize; 5154 long oldsize; 5155 int frags; 5156 int needj; 5157 5158 fs = ip->i_fs; 5159 mp = UFSTOVFS(ip->i_ump); 5160 if (length != 0) 5161 panic("softdep_setup_freeblocks: non-zero length"); 5162 freeblks = malloc(sizeof(struct freeblks), 5163 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO); 5164 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp); 5165 LIST_INIT(&freeblks->fb_jfreeblkhd); 5166 LIST_INIT(&freeblks->fb_jwork); 5167 freeblks->fb_state = ATTACHED; 5168 freeblks->fb_uid = ip->i_uid; 5169 freeblks->fb_previousinum = ip->i_number; 5170 freeblks->fb_devvp = ip->i_devvp; 5171 freeblks->fb_chkcnt = 0; 5172 ACQUIRE_LOCK(&lk); 5173 /* 5174 * If we're truncating a removed file that will never be written 5175 * we don't need to journal the block frees. The canceled journals 5176 * for the allocations will suffice. 5177 */ 5178 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 5179 if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED || 5180 (fs->fs_flags & FS_SUJ) == 0) 5181 needj = 0; 5182 else 5183 needj = 1; 5184 num_freeblkdep++; 5185 FREE_LOCK(&lk); 5186 extblocks = 0; 5187 if (fs->fs_magic == FS_UFS2_MAGIC) 5188 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 5189 datablocks = DIP(ip, i_blocks) - extblocks; 5190 if ((flags & IO_NORMAL) != 0) { 5191 oldsize = ip->i_size; 5192 ip->i_size = 0; 5193 DIP_SET(ip, i_size, 0); 5194 freeblks->fb_chkcnt = datablocks; 5195 for (i = 0; i < NDADDR; i++) { 5196 blkno = DIP(ip, i_db[i]); 5197 DIP_SET(ip, i_db[i], 0); 5198 if (blkno == 0) 5199 continue; 5200 frags = sblksize(fs, oldsize, i); 5201 frags = numfrags(fs, frags); 5202 newfreework(freeblks, NULL, i, blkno, frags, needj); 5203 } 5204 for (i = 0, tmpval = NINDIR(fs), lbn = NDADDR; i < NIADDR; 5205 i++, tmpval *= NINDIR(fs)) { 5206 blkno = DIP(ip, i_ib[i]); 5207 DIP_SET(ip, i_ib[i], 0); 5208 if (blkno) 5209 newfreework(freeblks, NULL, -lbn - i, blkno, 5210 fs->fs_frag, needj); 5211 lbn += tmpval; 5212 } 5213 UFS_LOCK(ip->i_ump); 5214 fs->fs_pendingblocks += datablocks; 5215 UFS_UNLOCK(ip->i_ump); 5216 } 5217 if ((flags & IO_EXT) != 0) { 5218 oldextsize = ip->i_din2->di_extsize; 5219 ip->i_din2->di_extsize = 0; 5220 freeblks->fb_chkcnt += extblocks; 5221 for (i = 0; i < NXADDR; i++) { 5222 blkno = ip->i_din2->di_extb[i]; 5223 ip->i_din2->di_extb[i] = 0; 5224 if (blkno == 0) 5225 continue; 5226 frags = sblksize(fs, oldextsize, i); 5227 frags = numfrags(fs, frags); 5228 newfreework(freeblks, NULL, -1 - i, blkno, frags, 5229 needj); 5230 } 5231 } 5232 if (LIST_EMPTY(&freeblks->fb_jfreeblkhd)) 5233 needj = 0; 5234 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - freeblks->fb_chkcnt); 5235 /* 5236 * Push the zero'ed inode to to its disk buffer so that we are free 5237 * to delete its dependencies below. Once the dependencies are gone 5238 * the buffer can be safely released. 5239 */ 5240 if ((error = bread(ip->i_devvp, 5241 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 5242 (int)fs->fs_bsize, NOCRED, &bp)) != 0) { 5243 brelse(bp); 5244 softdep_error("softdep_setup_freeblocks", error); 5245 } 5246 if (ip->i_ump->um_fstype == UFS1) { 5247 dp1 = ((struct ufs1_dinode *)bp->b_data + 5248 ino_to_fsbo(fs, ip->i_number)); 5249 ip->i_din1->di_freelink = dp1->di_freelink; 5250 *dp1 = *ip->i_din1; 5251 } else { 5252 dp2 = ((struct ufs2_dinode *)bp->b_data + 5253 ino_to_fsbo(fs, ip->i_number)); 5254 ip->i_din2->di_freelink = dp2->di_freelink; 5255 *dp2 = *ip->i_din2; 5256 } 5257 /* 5258 * Find and eliminate any inode dependencies. 5259 */ 5260 ACQUIRE_LOCK(&lk); 5261 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 5262 if ((inodedep->id_state & IOSTARTED) != 0) 5263 panic("softdep_setup_freeblocks: inode busy"); 5264 /* 5265 * Add the freeblks structure to the list of operations that 5266 * must await the zero'ed inode being written to disk. If we 5267 * still have a bitmap dependency (delay == 0), then the inode 5268 * has never been written to disk, so we can process the 5269 * freeblks below once we have deleted the dependencies. 5270 */ 5271 delay = (inodedep->id_state & DEPCOMPLETE); 5272 if (delay) 5273 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 5274 else if (needj) 5275 freeblks->fb_state |= COMPLETE; 5276 /* 5277 * Because the file length has been truncated to zero, any 5278 * pending block allocation dependency structures associated 5279 * with this inode are obsolete and can simply be de-allocated. 5280 * We must first merge the two dependency lists to get rid of 5281 * any duplicate freefrag structures, then purge the merged list. 5282 * If we still have a bitmap dependency, then the inode has never 5283 * been written to disk, so we can free any fragments without delay. 5284 */ 5285 if (flags & IO_NORMAL) { 5286 merge_inode_lists(&inodedep->id_newinoupdt, 5287 &inodedep->id_inoupdt); 5288 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != 0) 5289 cancel_allocdirect(&inodedep->id_inoupdt, adp, 5290 freeblks, delay); 5291 } 5292 if (flags & IO_EXT) { 5293 merge_inode_lists(&inodedep->id_newextupdt, 5294 &inodedep->id_extupdt); 5295 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != 0) 5296 cancel_allocdirect(&inodedep->id_extupdt, adp, 5297 freeblks, delay); 5298 } 5299 LIST_FOREACH(jfreeblk, &freeblks->fb_jfreeblkhd, jf_deps) 5300 add_to_journal(&jfreeblk->jf_list); 5301 5302 FREE_LOCK(&lk); 5303 bdwrite(bp); 5304 /* 5305 * We must wait for any I/O in progress to finish so that 5306 * all potential buffers on the dirty list will be visible. 5307 * Once they are all there, walk the list and get rid of 5308 * any dependencies. 5309 */ 5310 vp = ITOV(ip); 5311 bo = &vp->v_bufobj; 5312 BO_LOCK(bo); 5313 drain_output(vp); 5314restart: 5315 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) { 5316 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) || 5317 ((flags & IO_NORMAL) == 0 && 5318 (bp->b_xflags & BX_ALTDATA) == 0)) 5319 continue; 5320 if ((bp = getdirtybuf(bp, BO_MTX(bo), MNT_WAIT)) == NULL) 5321 goto restart; 5322 BO_UNLOCK(bo); 5323 ACQUIRE_LOCK(&lk); 5324 (void) inodedep_lookup(mp, ip->i_number, 0, &inodedep); 5325 if (deallocate_dependencies(bp, inodedep, freeblks)) 5326 bp->b_flags |= B_INVAL | B_NOCACHE; 5327 FREE_LOCK(&lk); 5328 brelse(bp); 5329 BO_LOCK(bo); 5330 goto restart; 5331 } 5332 BO_UNLOCK(bo); 5333 ACQUIRE_LOCK(&lk); 5334 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 5335 (void) free_inodedep(inodedep); 5336 5337 if (delay || needj) 5338 freeblks->fb_state |= DEPCOMPLETE; 5339 if (delay) { 5340 /* 5341 * If the inode with zeroed block pointers is now on disk 5342 * we can start freeing blocks. Add freeblks to the worklist 5343 * instead of calling handle_workitem_freeblocks directly as 5344 * it is more likely that additional IO is needed to complete 5345 * the request here than in the !delay case. 5346 */ 5347 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 5348 add_to_worklist(&freeblks->fb_list, 1); 5349 } 5350 if (needj && LIST_EMPTY(&freeblks->fb_jfreeblkhd)) 5351 needj = 0; 5352 5353 FREE_LOCK(&lk); 5354 /* 5355 * If the inode has never been written to disk (delay == 0) and 5356 * we're not waiting on any journal writes, then we can process the 5357 * freeblks now that we have deleted the dependencies. 5358 */ 5359 if (!delay && !needj) 5360 handle_workitem_freeblocks(freeblks, 0); 5361} 5362 5363/* 5364 * Reclaim any dependency structures from a buffer that is about to 5365 * be reallocated to a new vnode. The buffer must be locked, thus, 5366 * no I/O completion operations can occur while we are manipulating 5367 * its associated dependencies. The mutex is held so that other I/O's 5368 * associated with related dependencies do not occur. Returns 1 if 5369 * all dependencies were cleared, 0 otherwise. 5370 */ 5371static int 5372deallocate_dependencies(bp, inodedep, freeblks) 5373 struct buf *bp; 5374 struct inodedep *inodedep; 5375 struct freeblks *freeblks; 5376{ 5377 struct worklist *wk; 5378 struct indirdep *indirdep; 5379 struct newdirblk *newdirblk; 5380 struct allocindir *aip; 5381 struct pagedep *pagedep; 5382 struct jremref *jremref; 5383 struct jmvref *jmvref; 5384 struct dirrem *dirrem; 5385 int i; 5386 5387 mtx_assert(&lk, MA_OWNED); 5388 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) { 5389 switch (wk->wk_type) { 5390 5391 case D_INDIRDEP: 5392 indirdep = WK_INDIRDEP(wk); 5393 if (bp->b_lblkno >= 0 || 5394 bp->b_blkno != indirdep->ir_savebp->b_lblkno) 5395 panic("deallocate_dependencies: not indir"); 5396 cancel_indirdep(indirdep, bp, inodedep, freeblks); 5397 continue; 5398 5399 case D_PAGEDEP: 5400 pagedep = WK_PAGEDEP(wk); 5401 /* 5402 * There should be no directory add dependencies present 5403 * as the directory could not be truncated until all 5404 * children were removed. 5405 */ 5406 KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL, 5407 ("deallocate_dependencies: pendinghd != NULL")); 5408 for (i = 0; i < DAHASHSZ; i++) 5409 KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL, 5410 ("deallocate_dependencies: diraddhd != NULL")); 5411 /* 5412 * Copy any directory remove dependencies to the list 5413 * to be processed after the zero'ed inode is written. 5414 * If the inode has already been written, then they 5415 * can be dumped directly onto the work list. 5416 */ 5417 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) { 5418 /* 5419 * If there are any dirrems we wait for 5420 * the journal write to complete and 5421 * then restart the buf scan as the lock 5422 * has been dropped. 5423 */ 5424 while ((jremref = 5425 LIST_FIRST(&dirrem->dm_jremrefhd)) 5426 != NULL) { 5427 stat_jwait_filepage++; 5428 jwait(&jremref->jr_list); 5429 return (0); 5430 } 5431 LIST_REMOVE(dirrem, dm_next); 5432 dirrem->dm_dirinum = pagedep->pd_ino; 5433 if (inodedep == NULL || 5434 (inodedep->id_state & ALLCOMPLETE) == 5435 ALLCOMPLETE) { 5436 dirrem->dm_state |= COMPLETE; 5437 add_to_worklist(&dirrem->dm_list, 0); 5438 } else 5439 WORKLIST_INSERT(&inodedep->id_bufwait, 5440 &dirrem->dm_list); 5441 } 5442 if ((pagedep->pd_state & NEWBLOCK) != 0) { 5443 newdirblk = pagedep->pd_newdirblk; 5444 WORKLIST_REMOVE(&newdirblk->db_list); 5445 free_newdirblk(newdirblk); 5446 } 5447 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) 5448 != NULL) { 5449 stat_jwait_filepage++; 5450 jwait(&jmvref->jm_list); 5451 return (0); 5452 } 5453 WORKLIST_REMOVE(&pagedep->pd_list); 5454 LIST_REMOVE(pagedep, pd_hash); 5455 WORKITEM_FREE(pagedep, D_PAGEDEP); 5456 continue; 5457 5458 case D_ALLOCINDIR: 5459 aip = WK_ALLOCINDIR(wk); 5460 cancel_allocindir(aip, inodedep, freeblks); 5461 continue; 5462 5463 case D_ALLOCDIRECT: 5464 case D_INODEDEP: 5465 panic("deallocate_dependencies: Unexpected type %s", 5466 TYPENAME(wk->wk_type)); 5467 /* NOTREACHED */ 5468 5469 default: 5470 panic("deallocate_dependencies: Unknown type %s", 5471 TYPENAME(wk->wk_type)); 5472 /* NOTREACHED */ 5473 } 5474 } 5475 5476 return (1); 5477} 5478 5479/* 5480 * An allocdirect is being canceled due to a truncate. We must make sure 5481 * the journal entry is released in concert with the blkfree that releases 5482 * the storage. Completed journal entries must not be released until the 5483 * space is no longer pointed to by the inode or in the bitmap. 5484 */ 5485static void 5486cancel_allocdirect(adphead, adp, freeblks, delay) 5487 struct allocdirectlst *adphead; 5488 struct allocdirect *adp; 5489 struct freeblks *freeblks; 5490 int delay; 5491{ 5492 struct freework *freework; 5493 struct newblk *newblk; 5494 struct worklist *wk; 5495 ufs_lbn_t lbn; 5496 5497 TAILQ_REMOVE(adphead, adp, ad_next); 5498 newblk = (struct newblk *)adp; 5499 /* 5500 * If the journal hasn't been written the jnewblk must be passed 5501 * to the call to ffs_blkfree that reclaims the space. We accomplish 5502 * this by linking the journal dependency into the freework to be 5503 * freed when freework_freeblock() is called. If the journal has 5504 * been written we can simply reclaim the journal space when the 5505 * freeblks work is complete. 5506 */ 5507 if (newblk->nb_jnewblk == NULL) { 5508 cancel_newblk(newblk, &freeblks->fb_jwork); 5509 goto found; 5510 } 5511 lbn = newblk->nb_jnewblk->jn_lbn; 5512 /* 5513 * Find the correct freework structure so it releases the canceled 5514 * journal when the bitmap is cleared. This preserves rollback 5515 * until the allocation is reverted. 5516 */ 5517 LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) { 5518 freework = WK_FREEWORK(wk); 5519 if (freework->fw_lbn != lbn) 5520 continue; 5521 cancel_newblk(newblk, &freework->fw_jwork); 5522 goto found; 5523 } 5524 panic("cancel_allocdirect: Freework not found for lbn %jd\n", lbn); 5525found: 5526 if (delay) 5527 WORKLIST_INSERT(&adp->ad_inodedep->id_bufwait, 5528 &newblk->nb_list); 5529 else 5530 free_newblk(newblk); 5531 return; 5532} 5533 5534 5535static void 5536cancel_newblk(newblk, wkhd) 5537 struct newblk *newblk; 5538 struct workhead *wkhd; 5539{ 5540 struct indirdep *indirdep; 5541 struct allocindir *aip; 5542 5543 while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL) { 5544 indirdep->ir_state &= ~ONDEPLIST; 5545 LIST_REMOVE(indirdep, ir_next); 5546 /* 5547 * If an indirdep is not on the buf worklist we need to 5548 * free it here as deallocate_dependencies() will never 5549 * find it. These pointers were never visible on disk and 5550 * can be discarded immediately. 5551 */ 5552 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) { 5553 LIST_REMOVE(aip, ai_next); 5554 cancel_newblk(&aip->ai_block, wkhd); 5555 free_newblk(&aip->ai_block); 5556 } 5557 /* 5558 * If this indirdep is not attached to a buf it was simply 5559 * waiting on completion to clear completehd. free_indirdep() 5560 * asserts that nothing is dangling. 5561 */ 5562 if ((indirdep->ir_state & ONWORKLIST) == 0) 5563 free_indirdep(indirdep); 5564 } 5565 if (newblk->nb_state & ONDEPLIST) { 5566 newblk->nb_state &= ~ONDEPLIST; 5567 LIST_REMOVE(newblk, nb_deps); 5568 } 5569 if (newblk->nb_state & ONWORKLIST) 5570 WORKLIST_REMOVE(&newblk->nb_list); 5571 /* 5572 * If the journal entry hasn't been written we hold onto the dep 5573 * until it is safe to free along with the other journal work. 5574 */ 5575 if (newblk->nb_jnewblk != NULL) { 5576 cancel_jnewblk(newblk->nb_jnewblk, wkhd); 5577 newblk->nb_jnewblk = NULL; 5578 } 5579 if (!LIST_EMPTY(&newblk->nb_jwork)) 5580 jwork_move(wkhd, &newblk->nb_jwork); 5581} 5582 5583/* 5584 * Free a newblk. Generate a new freefrag work request if appropriate. 5585 * This must be called after the inode pointer and any direct block pointers 5586 * are valid or fully removed via truncate or frag extension. 5587 */ 5588static void 5589free_newblk(newblk) 5590 struct newblk *newblk; 5591{ 5592 struct indirdep *indirdep; 5593 struct newdirblk *newdirblk; 5594 struct freefrag *freefrag; 5595 struct worklist *wk; 5596 5597 mtx_assert(&lk, MA_OWNED); 5598 if (newblk->nb_state & ONDEPLIST) 5599 LIST_REMOVE(newblk, nb_deps); 5600 if (newblk->nb_state & ONWORKLIST) 5601 WORKLIST_REMOVE(&newblk->nb_list); 5602 LIST_REMOVE(newblk, nb_hash); 5603 if ((freefrag = newblk->nb_freefrag) != NULL) { 5604 freefrag->ff_state |= COMPLETE; 5605 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 5606 add_to_worklist(&freefrag->ff_list, 0); 5607 } 5608 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) { 5609 newdirblk = WK_NEWDIRBLK(wk); 5610 WORKLIST_REMOVE(&newdirblk->db_list); 5611 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 5612 panic("free_newblk: extra newdirblk"); 5613 free_newdirblk(newdirblk); 5614 } 5615 while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL) { 5616 indirdep->ir_state |= DEPCOMPLETE; 5617 indirdep_complete(indirdep); 5618 } 5619 KASSERT(newblk->nb_jnewblk == NULL, 5620 ("free_newblk; jnewblk %p still attached", newblk->nb_jnewblk)); 5621 handle_jwork(&newblk->nb_jwork); 5622 newblk->nb_list.wk_type = D_NEWBLK; 5623 WORKITEM_FREE(newblk, D_NEWBLK); 5624} 5625 5626/* 5627 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep. 5628 * This routine must be called with splbio interrupts blocked. 5629 */ 5630static void 5631free_newdirblk(newdirblk) 5632 struct newdirblk *newdirblk; 5633{ 5634 struct pagedep *pagedep; 5635 struct diradd *dap; 5636 struct worklist *wk; 5637 int i; 5638 5639 mtx_assert(&lk, MA_OWNED); 5640 /* 5641 * If the pagedep is still linked onto the directory buffer 5642 * dependency chain, then some of the entries on the 5643 * pd_pendinghd list may not be committed to disk yet. In 5644 * this case, we will simply clear the NEWBLOCK flag and 5645 * let the pd_pendinghd list be processed when the pagedep 5646 * is next written. If the pagedep is no longer on the buffer 5647 * dependency chain, then all the entries on the pd_pending 5648 * list are committed to disk and we can free them here. 5649 */ 5650 pagedep = newdirblk->db_pagedep; 5651 pagedep->pd_state &= ~NEWBLOCK; 5652 if ((pagedep->pd_state & ONWORKLIST) == 0) 5653 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 5654 free_diradd(dap, NULL); 5655 /* 5656 * If no dependencies remain, the pagedep will be freed. 5657 */ 5658 for (i = 0; i < DAHASHSZ; i++) 5659 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i])) 5660 break; 5661 if (i == DAHASHSZ && (pagedep->pd_state & ONWORKLIST) == 0 && 5662 LIST_EMPTY(&pagedep->pd_jmvrefhd)) { 5663 KASSERT(LIST_FIRST(&pagedep->pd_dirremhd) == NULL, 5664 ("free_newdirblk: Freeing non-free pagedep %p", pagedep)); 5665 LIST_REMOVE(pagedep, pd_hash); 5666 WORKITEM_FREE(pagedep, D_PAGEDEP); 5667 } 5668 /* Should only ever be one item in the list. */ 5669 while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) { 5670 WORKLIST_REMOVE(wk); 5671 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 5672 } 5673 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 5674} 5675 5676/* 5677 * Prepare an inode to be freed. The actual free operation is not 5678 * done until the zero'ed inode has been written to disk. 5679 */ 5680void 5681softdep_freefile(pvp, ino, mode) 5682 struct vnode *pvp; 5683 ino_t ino; 5684 int mode; 5685{ 5686 struct inode *ip = VTOI(pvp); 5687 struct inodedep *inodedep; 5688 struct freefile *freefile; 5689 5690 /* 5691 * This sets up the inode de-allocation dependency. 5692 */ 5693 freefile = malloc(sizeof(struct freefile), 5694 M_FREEFILE, M_SOFTDEP_FLAGS); 5695 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount); 5696 freefile->fx_mode = mode; 5697 freefile->fx_oldinum = ino; 5698 freefile->fx_devvp = ip->i_devvp; 5699 LIST_INIT(&freefile->fx_jwork); 5700 UFS_LOCK(ip->i_ump); 5701 ip->i_fs->fs_pendinginodes += 1; 5702 UFS_UNLOCK(ip->i_ump); 5703 5704 /* 5705 * If the inodedep does not exist, then the zero'ed inode has 5706 * been written to disk. If the allocated inode has never been 5707 * written to disk, then the on-disk inode is zero'ed. In either 5708 * case we can free the file immediately. If the journal was 5709 * canceled before being written the inode will never make it to 5710 * disk and we must send the canceled journal entrys to 5711 * ffs_freefile() to be cleared in conjunction with the bitmap. 5712 * Any blocks waiting on the inode to write can be safely freed 5713 * here as it will never been written. 5714 */ 5715 ACQUIRE_LOCK(&lk); 5716 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 5717 /* 5718 * Remove this inode from the unlinked list and set 5719 * GOINGAWAY as appropriate to indicate that this inode 5720 * will never be written. 5721 */ 5722 if (inodedep && inodedep->id_state & UNLINKED) { 5723 /* 5724 * Save the journal work to be freed with the bitmap 5725 * before we clear UNLINKED. Otherwise it can be lost 5726 * if the inode block is written. 5727 */ 5728 handle_bufwait(inodedep, &freefile->fx_jwork); 5729 clear_unlinked_inodedep(inodedep); 5730 /* Re-acquire inodedep as we've dropped lk. */ 5731 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 5732 if (inodedep && (inodedep->id_state & DEPCOMPLETE) == 0) 5733 inodedep->id_state |= GOINGAWAY; 5734 } 5735 if (inodedep == NULL || check_inode_unwritten(inodedep)) { 5736 FREE_LOCK(&lk); 5737 handle_workitem_freefile(freefile); 5738 return; 5739 } 5740 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list); 5741 FREE_LOCK(&lk); 5742 if (ip->i_number == ino) 5743 ip->i_flag |= IN_MODIFIED; 5744} 5745 5746/* 5747 * Check to see if an inode has never been written to disk. If 5748 * so free the inodedep and return success, otherwise return failure. 5749 * This routine must be called with splbio interrupts blocked. 5750 * 5751 * If we still have a bitmap dependency, then the inode has never 5752 * been written to disk. Drop the dependency as it is no longer 5753 * necessary since the inode is being deallocated. We set the 5754 * ALLCOMPLETE flags since the bitmap now properly shows that the 5755 * inode is not allocated. Even if the inode is actively being 5756 * written, it has been rolled back to its zero'ed state, so we 5757 * are ensured that a zero inode is what is on the disk. For short 5758 * lived files, this change will usually result in removing all the 5759 * dependencies from the inode so that it can be freed immediately. 5760 */ 5761static int 5762check_inode_unwritten(inodedep) 5763 struct inodedep *inodedep; 5764{ 5765 5766 mtx_assert(&lk, MA_OWNED); 5767 5768 if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 || 5769 !LIST_EMPTY(&inodedep->id_pendinghd) || 5770 !LIST_EMPTY(&inodedep->id_bufwait) || 5771 !LIST_EMPTY(&inodedep->id_inowait) || 5772 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 5773 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 5774 !TAILQ_EMPTY(&inodedep->id_extupdt) || 5775 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 5776 inodedep->id_mkdiradd != NULL || 5777 inodedep->id_nlinkdelta != 0) 5778 return (0); 5779 /* 5780 * Another process might be in initiate_write_inodeblock_ufs[12] 5781 * trying to allocate memory without holding "Softdep Lock". 5782 */ 5783 if ((inodedep->id_state & IOSTARTED) != 0 && 5784 inodedep->id_savedino1 == NULL) 5785 return (0); 5786 5787 if (inodedep->id_state & ONDEPLIST) 5788 LIST_REMOVE(inodedep, id_deps); 5789 inodedep->id_state &= ~ONDEPLIST; 5790 inodedep->id_state |= ALLCOMPLETE; 5791 inodedep->id_bmsafemap = NULL; 5792 if (inodedep->id_state & ONWORKLIST) 5793 WORKLIST_REMOVE(&inodedep->id_list); 5794 if (inodedep->id_savedino1 != NULL) { 5795 free(inodedep->id_savedino1, M_SAVEDINO); 5796 inodedep->id_savedino1 = NULL; 5797 } 5798 if (free_inodedep(inodedep) == 0) 5799 panic("check_inode_unwritten: busy inode"); 5800 return (1); 5801} 5802 5803/* 5804 * Try to free an inodedep structure. Return 1 if it could be freed. 5805 */ 5806static int 5807free_inodedep(inodedep) 5808 struct inodedep *inodedep; 5809{ 5810 5811 mtx_assert(&lk, MA_OWNED); 5812 if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 || 5813 (inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE || 5814 !LIST_EMPTY(&inodedep->id_dirremhd) || 5815 !LIST_EMPTY(&inodedep->id_pendinghd) || 5816 !LIST_EMPTY(&inodedep->id_bufwait) || 5817 !LIST_EMPTY(&inodedep->id_inowait) || 5818 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 5819 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 5820 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 5821 !TAILQ_EMPTY(&inodedep->id_extupdt) || 5822 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 5823 inodedep->id_mkdiradd != NULL || 5824 inodedep->id_nlinkdelta != 0 || 5825 inodedep->id_savedino1 != NULL) 5826 return (0); 5827 if (inodedep->id_state & ONDEPLIST) 5828 LIST_REMOVE(inodedep, id_deps); 5829 LIST_REMOVE(inodedep, id_hash); 5830 WORKITEM_FREE(inodedep, D_INODEDEP); 5831 num_inodedep -= 1; 5832 return (1); 5833} 5834 5835/* 5836 * Free the block referenced by a freework structure. The parent freeblks 5837 * structure is released and completed when the final cg bitmap reaches 5838 * the disk. This routine may be freeing a jnewblk which never made it to 5839 * disk in which case we do not have to wait as the operation is undone 5840 * in memory immediately. 5841 */ 5842static void 5843freework_freeblock(freework) 5844 struct freework *freework; 5845{ 5846 struct freeblks *freeblks; 5847 struct ufsmount *ump; 5848 struct workhead wkhd; 5849 struct fs *fs; 5850 int complete; 5851 int pending; 5852 int bsize; 5853 int needj; 5854 5855 freeblks = freework->fw_freeblks; 5856 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 5857 fs = ump->um_fs; 5858 needj = freeblks->fb_list.wk_mp->mnt_kern_flag & MNTK_SUJ; 5859 complete = 0; 5860 LIST_INIT(&wkhd); 5861 /* 5862 * If we are canceling an existing jnewblk pass it to the free 5863 * routine, otherwise pass the freeblk which will ultimately 5864 * release the freeblks. If we're not journaling, we can just 5865 * free the freeblks immediately. 5866 */ 5867 if (!LIST_EMPTY(&freework->fw_jwork)) { 5868 LIST_SWAP(&wkhd, &freework->fw_jwork, worklist, wk_list); 5869 complete = 1; 5870 } else if (needj) 5871 WORKLIST_INSERT_UNLOCKED(&wkhd, &freework->fw_list); 5872 bsize = lfragtosize(fs, freework->fw_frags); 5873 pending = btodb(bsize); 5874 ACQUIRE_LOCK(&lk); 5875 freeblks->fb_chkcnt -= pending; 5876 FREE_LOCK(&lk); 5877 /* 5878 * extattr blocks don't show up in pending blocks. XXX why? 5879 */ 5880 if (freework->fw_lbn >= 0 || freework->fw_lbn <= -NDADDR) { 5881 UFS_LOCK(ump); 5882 fs->fs_pendingblocks -= pending; 5883 UFS_UNLOCK(ump); 5884 } 5885 ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, 5886 bsize, freeblks->fb_previousinum, &wkhd); 5887 if (complete == 0 && needj) 5888 return; 5889 /* 5890 * The jnewblk will be discarded and the bits in the map never 5891 * made it to disk. We can immediately free the freeblk. 5892 */ 5893 ACQUIRE_LOCK(&lk); 5894 handle_written_freework(freework); 5895 FREE_LOCK(&lk); 5896} 5897 5898/* 5899 * Start, continue, or finish the process of freeing an indirect block tree. 5900 * The free operation may be paused at any point with fw_off containing the 5901 * offset to restart from. This enables us to implement some flow control 5902 * for large truncates which may fan out and generate a huge number of 5903 * dependencies. 5904 */ 5905static void 5906handle_workitem_indirblk(freework) 5907 struct freework *freework; 5908{ 5909 struct freeblks *freeblks; 5910 struct ufsmount *ump; 5911 struct fs *fs; 5912 5913 5914 freeblks = freework->fw_freeblks; 5915 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 5916 fs = ump->um_fs; 5917 if (freework->fw_off == NINDIR(fs)) 5918 freework_freeblock(freework); 5919 else 5920 indir_trunc(freework, fsbtodb(fs, freework->fw_blkno), 5921 freework->fw_lbn); 5922} 5923 5924/* 5925 * Called when a freework structure attached to a cg buf is written. The 5926 * ref on either the parent or the freeblks structure is released and 5927 * either may be added to the worklist if it is the final ref. 5928 */ 5929static void 5930handle_written_freework(freework) 5931 struct freework *freework; 5932{ 5933 struct freeblks *freeblks; 5934 struct freework *parent; 5935 5936 freeblks = freework->fw_freeblks; 5937 parent = freework->fw_parent; 5938 if (parent) { 5939 if (--parent->fw_ref != 0) 5940 parent = NULL; 5941 freeblks = NULL; 5942 } else if (--freeblks->fb_ref != 0) 5943 freeblks = NULL; 5944 WORKITEM_FREE(freework, D_FREEWORK); 5945 /* 5946 * Don't delay these block frees or it takes an intolerable amount 5947 * of time to process truncates and free their journal entries. 5948 */ 5949 if (freeblks) 5950 add_to_worklist(&freeblks->fb_list, 1); 5951 if (parent) 5952 add_to_worklist(&parent->fw_list, 1); 5953} 5954 5955/* 5956 * This workitem routine performs the block de-allocation. 5957 * The workitem is added to the pending list after the updated 5958 * inode block has been written to disk. As mentioned above, 5959 * checks regarding the number of blocks de-allocated (compared 5960 * to the number of blocks allocated for the file) are also 5961 * performed in this function. 5962 */ 5963static void 5964handle_workitem_freeblocks(freeblks, flags) 5965 struct freeblks *freeblks; 5966 int flags; 5967{ 5968 struct freework *freework; 5969 struct worklist *wk; 5970 5971 KASSERT(LIST_EMPTY(&freeblks->fb_jfreeblkhd), 5972 ("handle_workitem_freeblocks: Journal entries not written.")); 5973 if (LIST_EMPTY(&freeblks->fb_freeworkhd)) { 5974 handle_complete_freeblocks(freeblks); 5975 return; 5976 } 5977 freeblks->fb_ref++; 5978 while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) { 5979 KASSERT(wk->wk_type == D_FREEWORK, 5980 ("handle_workitem_freeblocks: Unknown type %s", 5981 TYPENAME(wk->wk_type))); 5982 WORKLIST_REMOVE_UNLOCKED(wk); 5983 freework = WK_FREEWORK(wk); 5984 if (freework->fw_lbn <= -NDADDR) 5985 handle_workitem_indirblk(freework); 5986 else 5987 freework_freeblock(freework); 5988 } 5989 ACQUIRE_LOCK(&lk); 5990 if (--freeblks->fb_ref != 0) 5991 freeblks = NULL; 5992 FREE_LOCK(&lk); 5993 if (freeblks) 5994 handle_complete_freeblocks(freeblks); 5995} 5996 5997/* 5998 * Once all of the freework workitems are complete we can retire the 5999 * freeblocks dependency and any journal work awaiting completion. This 6000 * can not be called until all other dependencies are stable on disk. 6001 */ 6002static void 6003handle_complete_freeblocks(freeblks) 6004 struct freeblks *freeblks; 6005{ 6006 struct inode *ip; 6007 struct vnode *vp; 6008 struct fs *fs; 6009 struct ufsmount *ump; 6010 int flags; 6011 6012 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 6013 fs = ump->um_fs; 6014 flags = LK_NOWAIT; 6015 6016 /* 6017 * If we still have not finished background cleanup, then check 6018 * to see if the block count needs to be adjusted. 6019 */ 6020 if (freeblks->fb_chkcnt != 0 && (fs->fs_flags & FS_UNCLEAN) != 0 && 6021 ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_previousinum, 6022 (flags & LK_NOWAIT) | LK_EXCLUSIVE, &vp, FFSV_FORCEINSMQ) == 0) { 6023 ip = VTOI(vp); 6024 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + freeblks->fb_chkcnt); 6025 ip->i_flag |= IN_CHANGE; 6026 vput(vp); 6027 } 6028 6029 if (!(freeblks->fb_chkcnt == 0 || 6030 ((fs->fs_flags & FS_UNCLEAN) != 0 && (flags & LK_NOWAIT) == 0))) 6031 printf( 6032 "handle_workitem_freeblocks: inode %ju block count %jd\n", 6033 (uintmax_t)freeblks->fb_previousinum, 6034 (intmax_t)freeblks->fb_chkcnt); 6035 6036 ACQUIRE_LOCK(&lk); 6037 /* 6038 * All of the freeblock deps must be complete prior to this call 6039 * so it's now safe to complete earlier outstanding journal entries. 6040 */ 6041 handle_jwork(&freeblks->fb_jwork); 6042 WORKITEM_FREE(freeblks, D_FREEBLKS); 6043 num_freeblkdep--; 6044 FREE_LOCK(&lk); 6045} 6046 6047/* 6048 * Release blocks associated with the inode ip and stored in the indirect 6049 * block dbn. If level is greater than SINGLE, the block is an indirect block 6050 * and recursive calls to indirtrunc must be used to cleanse other indirect 6051 * blocks. 6052 */ 6053static void 6054indir_trunc(freework, dbn, lbn) 6055 struct freework *freework; 6056 ufs2_daddr_t dbn; 6057 ufs_lbn_t lbn; 6058{ 6059 struct freework *nfreework; 6060 struct workhead wkhd; 6061 struct jnewblk *jnewblk; 6062 struct freeblks *freeblks; 6063 struct buf *bp; 6064 struct fs *fs; 6065 struct worklist *wkn; 6066 struct worklist *wk; 6067 struct indirdep *indirdep; 6068 struct ufsmount *ump; 6069 ufs1_daddr_t *bap1 = 0; 6070 ufs2_daddr_t nb, nnb, *bap2 = 0; 6071 ufs_lbn_t lbnadd; 6072 int i, nblocks, ufs1fmt; 6073 int fs_pendingblocks; 6074 int freedeps; 6075 int needj; 6076 int level; 6077 int cnt; 6078 6079 LIST_INIT(&wkhd); 6080 level = lbn_level(lbn); 6081 if (level == -1) 6082 panic("indir_trunc: Invalid lbn %jd\n", lbn); 6083 freeblks = freework->fw_freeblks; 6084 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 6085 fs = ump->um_fs; 6086 fs_pendingblocks = 0; 6087 freedeps = 0; 6088 needj = UFSTOVFS(ump)->mnt_kern_flag & MNTK_SUJ; 6089 lbnadd = lbn_offset(fs, level); 6090 /* 6091 * Get buffer of block pointers to be freed. This routine is not 6092 * called until the zero'ed inode has been written, so it is safe 6093 * to free blocks as they are encountered. Because the inode has 6094 * been zero'ed, calls to bmap on these blocks will fail. So, we 6095 * have to use the on-disk address and the block device for the 6096 * filesystem to look them up. If the file was deleted before its 6097 * indirect blocks were all written to disk, the routine that set 6098 * us up (deallocate_dependencies) will have arranged to leave 6099 * a complete copy of the indirect block in memory for our use. 6100 * Otherwise we have to read the blocks in from the disk. 6101 */ 6102#ifdef notyet 6103 bp = getblk(freeblks->fb_devvp, dbn, (int)fs->fs_bsize, 0, 0, 6104 GB_NOCREAT); 6105#else 6106 bp = incore(&freeblks->fb_devvp->v_bufobj, dbn); 6107#endif 6108 ACQUIRE_LOCK(&lk); 6109 if (bp != NULL && (wk = LIST_FIRST(&bp->b_dep)) != NULL) { 6110 if (wk->wk_type != D_INDIRDEP || 6111 (wk->wk_state & GOINGAWAY) == 0) 6112 panic("indir_trunc: lost indirdep %p", wk); 6113 indirdep = WK_INDIRDEP(wk); 6114 LIST_SWAP(&wkhd, &indirdep->ir_jwork, worklist, wk_list); 6115 free_indirdep(indirdep); 6116 if (!LIST_EMPTY(&bp->b_dep)) 6117 panic("indir_trunc: dangling dep %p", 6118 LIST_FIRST(&bp->b_dep)); 6119 ump->um_numindirdeps -= 1; 6120 FREE_LOCK(&lk); 6121 } else { 6122#ifdef notyet 6123 if (bp) 6124 brelse(bp); 6125#endif 6126 FREE_LOCK(&lk); 6127 if (bread(freeblks->fb_devvp, dbn, (int)fs->fs_bsize, 6128 NOCRED, &bp) != 0) { 6129 brelse(bp); 6130 return; 6131 } 6132 } 6133 /* 6134 * Recursively free indirect blocks. 6135 */ 6136 if (ump->um_fstype == UFS1) { 6137 ufs1fmt = 1; 6138 bap1 = (ufs1_daddr_t *)bp->b_data; 6139 } else { 6140 ufs1fmt = 0; 6141 bap2 = (ufs2_daddr_t *)bp->b_data; 6142 } 6143 6144 if (needj) 6145 freework->fw_ref += NINDIR(fs) + 1; 6146 6147 /* 6148 * Reclaim indirect blocks which never made it to disk. 6149 */ 6150 cnt = 0; 6151 LIST_FOREACH_SAFE(wk, &wkhd, wk_list, wkn) { 6152 if (wk->wk_type != D_JNEWBLK) 6153 continue; 6154 ACQUIRE_LOCK(&lk); 6155 WORKLIST_REMOVE(wk); 6156 FREE_LOCK(&lk); 6157 jnewblk = WK_JNEWBLK(wk); 6158 if (jnewblk->jn_lbn > 0) 6159 i = (jnewblk->jn_lbn - -lbn) / lbnadd; 6160 else 6161 i = (-(jnewblk->jn_lbn + level - 1) - -(lbn + level)) / 6162 lbnadd; 6163 KASSERT(i >= 0 && i < NINDIR(fs), 6164 ("indir_trunc: Index out of range %d parent %jd lbn %jd level %d", 6165 i, lbn, jnewblk->jn_lbn, level)); 6166 /* Clear the pointer so it isn't found below. */ 6167 if (ufs1fmt) { 6168 nb = bap1[i]; 6169 bap1[i] = 0; 6170 } else { 6171 nb = bap2[i]; 6172 bap2[i] = 0; 6173 } 6174 KASSERT(nb == jnewblk->jn_blkno, 6175 ("indir_trunc: Block mismatch %jd != %jd", 6176 nb, jnewblk->jn_blkno)); 6177 if (level != 0) { 6178 ufs_lbn_t nlbn; 6179 6180 nlbn = (lbn + 1) - (i * lbnadd); 6181 nfreework = newfreework(freeblks, freework, 6182 nlbn, nb, fs->fs_frag, 0); 6183 WORKLIST_INSERT_UNLOCKED(&nfreework->fw_jwork, wk); 6184 freedeps++; 6185 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn); 6186 } else { 6187 struct workhead freewk; 6188 6189 LIST_INIT(&freewk); 6190 ACQUIRE_LOCK(&lk); 6191 WORKLIST_INSERT(&freewk, wk); 6192 FREE_LOCK(&lk); 6193 ffs_blkfree(ump, fs, freeblks->fb_devvp, 6194 jnewblk->jn_blkno, fs->fs_bsize, 6195 freeblks->fb_previousinum, &freewk); 6196 } 6197 cnt++; 6198 } 6199 ACQUIRE_LOCK(&lk); 6200 /* Any remaining journal work can be completed with freeblks. */ 6201 jwork_move(&freeblks->fb_jwork, &wkhd); 6202 FREE_LOCK(&lk); 6203 nblocks = btodb(fs->fs_bsize); 6204 if (ufs1fmt) 6205 nb = bap1[0]; 6206 else 6207 nb = bap2[0]; 6208 nfreework = freework; 6209 /* 6210 * Reclaim on disk blocks. 6211 */ 6212 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) { 6213 if (i != NINDIR(fs) - 1) { 6214 if (ufs1fmt) 6215 nnb = bap1[i+1]; 6216 else 6217 nnb = bap2[i+1]; 6218 } else 6219 nnb = 0; 6220 if (nb == 0) 6221 continue; 6222 cnt++; 6223 if (level != 0) { 6224 ufs_lbn_t nlbn; 6225 6226 nlbn = (lbn + 1) - (i * lbnadd); 6227 if (needj != 0) { 6228 nfreework = newfreework(freeblks, freework, 6229 nlbn, nb, fs->fs_frag, 0); 6230 freedeps++; 6231 } 6232 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn); 6233 } else { 6234 struct freedep *freedep; 6235 6236 /* 6237 * Attempt to aggregate freedep dependencies for 6238 * all blocks being released to the same CG. 6239 */ 6240 LIST_INIT(&wkhd); 6241 if (needj != 0 && 6242 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) { 6243 freedep = newfreedep(freework); 6244 WORKLIST_INSERT_UNLOCKED(&wkhd, 6245 &freedep->fd_list); 6246 freedeps++; 6247 } 6248 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb, 6249 fs->fs_bsize, freeblks->fb_previousinum, &wkhd); 6250 } 6251 } 6252 if (level == 0) 6253 fs_pendingblocks = (nblocks * cnt); 6254 /* 6255 * If we're not journaling we can free the indirect now. Otherwise 6256 * setup the ref counts and offset so this indirect can be completed 6257 * when its children are free. 6258 */ 6259 if (needj == 0) { 6260 fs_pendingblocks += nblocks; 6261 dbn = dbtofsb(fs, dbn); 6262 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize, 6263 freeblks->fb_previousinum, NULL); 6264 ACQUIRE_LOCK(&lk); 6265 freeblks->fb_chkcnt -= fs_pendingblocks; 6266 if (freework->fw_blkno == dbn) 6267 handle_written_freework(freework); 6268 FREE_LOCK(&lk); 6269 freework = NULL; 6270 } else { 6271 ACQUIRE_LOCK(&lk); 6272 freework->fw_off = i; 6273 freework->fw_ref += freedeps; 6274 freework->fw_ref -= NINDIR(fs) + 1; 6275 if (freework->fw_ref != 0) 6276 freework = NULL; 6277 freeblks->fb_chkcnt -= fs_pendingblocks; 6278 FREE_LOCK(&lk); 6279 } 6280 if (fs_pendingblocks) { 6281 UFS_LOCK(ump); 6282 fs->fs_pendingblocks -= fs_pendingblocks; 6283 UFS_UNLOCK(ump); 6284 } 6285 bp->b_flags |= B_INVAL | B_NOCACHE; 6286 brelse(bp); 6287 if (freework) 6288 handle_workitem_indirblk(freework); 6289 return; 6290} 6291 6292/* 6293 * Cancel an allocindir when it is removed via truncation. 6294 */ 6295static void 6296cancel_allocindir(aip, inodedep, freeblks) 6297 struct allocindir *aip; 6298 struct inodedep *inodedep; 6299 struct freeblks *freeblks; 6300{ 6301 struct newblk *newblk; 6302 6303 /* 6304 * If the journal hasn't been written the jnewblk must be passed 6305 * to the call to ffs_blkfree that reclaims the space. We accomplish 6306 * this by linking the journal dependency into the indirdep to be 6307 * freed when indir_trunc() is called. If the journal has already 6308 * been written we can simply reclaim the journal space when the 6309 * freeblks work is complete. 6310 */ 6311 LIST_REMOVE(aip, ai_next); 6312 newblk = (struct newblk *)aip; 6313 if (newblk->nb_jnewblk == NULL) 6314 cancel_newblk(newblk, &freeblks->fb_jwork); 6315 else 6316 cancel_newblk(newblk, &aip->ai_indirdep->ir_jwork); 6317 if (inodedep && inodedep->id_state & DEPCOMPLETE) 6318 WORKLIST_INSERT(&inodedep->id_bufwait, &newblk->nb_list); 6319 else 6320 free_newblk(newblk); 6321} 6322 6323/* 6324 * Create the mkdir dependencies for . and .. in a new directory. Link them 6325 * in to a newdirblk so any subsequent additions are tracked properly. The 6326 * caller is responsible for adding the mkdir1 dependency to the journal 6327 * and updating id_mkdiradd. This function returns with lk held. 6328 */ 6329static struct mkdir * 6330setup_newdir(dap, newinum, dinum, newdirbp, mkdirp) 6331 struct diradd *dap; 6332 ino_t newinum; 6333 ino_t dinum; 6334 struct buf *newdirbp; 6335 struct mkdir **mkdirp; 6336{ 6337 struct newblk *newblk; 6338 struct pagedep *pagedep; 6339 struct inodedep *inodedep; 6340 struct newdirblk *newdirblk = 0; 6341 struct mkdir *mkdir1, *mkdir2; 6342 struct worklist *wk; 6343 struct jaddref *jaddref; 6344 struct mount *mp; 6345 6346 mp = dap->da_list.wk_mp; 6347 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK, 6348 M_SOFTDEP_FLAGS); 6349 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 6350 LIST_INIT(&newdirblk->db_mkdir); 6351 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 6352 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp); 6353 mkdir1->md_state = ATTACHED | MKDIR_BODY; 6354 mkdir1->md_diradd = dap; 6355 mkdir1->md_jaddref = NULL; 6356 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 6357 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp); 6358 mkdir2->md_state = ATTACHED | MKDIR_PARENT; 6359 mkdir2->md_diradd = dap; 6360 mkdir2->md_jaddref = NULL; 6361 if ((mp->mnt_kern_flag & MNTK_SUJ) == 0) { 6362 mkdir1->md_state |= DEPCOMPLETE; 6363 mkdir2->md_state |= DEPCOMPLETE; 6364 } 6365 /* 6366 * Dependency on "." and ".." being written to disk. 6367 */ 6368 mkdir1->md_buf = newdirbp; 6369 ACQUIRE_LOCK(&lk); 6370 LIST_INSERT_HEAD(&mkdirlisthd, mkdir1, md_mkdirs); 6371 /* 6372 * We must link the pagedep, allocdirect, and newdirblk for 6373 * the initial file page so the pointer to the new directory 6374 * is not written until the directory contents are live and 6375 * any subsequent additions are not marked live until the 6376 * block is reachable via the inode. 6377 */ 6378 if (pagedep_lookup(mp, newinum, 0, 0, &pagedep) == 0) 6379 panic("setup_newdir: lost pagedep"); 6380 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list) 6381 if (wk->wk_type == D_ALLOCDIRECT) 6382 break; 6383 if (wk == NULL) 6384 panic("setup_newdir: lost allocdirect"); 6385 newblk = WK_NEWBLK(wk); 6386 pagedep->pd_state |= NEWBLOCK; 6387 pagedep->pd_newdirblk = newdirblk; 6388 newdirblk->db_pagedep = pagedep; 6389 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 6390 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list); 6391 /* 6392 * Look up the inodedep for the parent directory so that we 6393 * can link mkdir2 into the pending dotdot jaddref or 6394 * the inode write if there is none. If the inode is 6395 * ALLCOMPLETE and no jaddref is present all dependencies have 6396 * been satisfied and mkdir2 can be freed. 6397 */ 6398 inodedep_lookup(mp, dinum, 0, &inodedep); 6399 if (mp->mnt_kern_flag & MNTK_SUJ) { 6400 if (inodedep == NULL) 6401 panic("setup_newdir: Lost parent."); 6402 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 6403 inoreflst); 6404 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum && 6405 (jaddref->ja_state & MKDIR_PARENT), 6406 ("setup_newdir: bad dotdot jaddref %p", jaddref)); 6407 LIST_INSERT_HEAD(&mkdirlisthd, mkdir2, md_mkdirs); 6408 mkdir2->md_jaddref = jaddref; 6409 jaddref->ja_mkdir = mkdir2; 6410 } else if (inodedep == NULL || 6411 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 6412 dap->da_state &= ~MKDIR_PARENT; 6413 WORKITEM_FREE(mkdir2, D_MKDIR); 6414 } else { 6415 LIST_INSERT_HEAD(&mkdirlisthd, mkdir2, md_mkdirs); 6416 WORKLIST_INSERT(&inodedep->id_bufwait,&mkdir2->md_list); 6417 } 6418 *mkdirp = mkdir2; 6419 6420 return (mkdir1); 6421} 6422 6423/* 6424 * Directory entry addition dependencies. 6425 * 6426 * When adding a new directory entry, the inode (with its incremented link 6427 * count) must be written to disk before the directory entry's pointer to it. 6428 * Also, if the inode is newly allocated, the corresponding freemap must be 6429 * updated (on disk) before the directory entry's pointer. These requirements 6430 * are met via undo/redo on the directory entry's pointer, which consists 6431 * simply of the inode number. 6432 * 6433 * As directory entries are added and deleted, the free space within a 6434 * directory block can become fragmented. The ufs filesystem will compact 6435 * a fragmented directory block to make space for a new entry. When this 6436 * occurs, the offsets of previously added entries change. Any "diradd" 6437 * dependency structures corresponding to these entries must be updated with 6438 * the new offsets. 6439 */ 6440 6441/* 6442 * This routine is called after the in-memory inode's link 6443 * count has been incremented, but before the directory entry's 6444 * pointer to the inode has been set. 6445 */ 6446int 6447softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 6448 struct buf *bp; /* buffer containing directory block */ 6449 struct inode *dp; /* inode for directory */ 6450 off_t diroffset; /* offset of new entry in directory */ 6451 ino_t newinum; /* inode referenced by new directory entry */ 6452 struct buf *newdirbp; /* non-NULL => contents of new mkdir */ 6453 int isnewblk; /* entry is in a newly allocated block */ 6454{ 6455 int offset; /* offset of new entry within directory block */ 6456 ufs_lbn_t lbn; /* block in directory containing new entry */ 6457 struct fs *fs; 6458 struct diradd *dap; 6459 struct newblk *newblk; 6460 struct pagedep *pagedep; 6461 struct inodedep *inodedep; 6462 struct newdirblk *newdirblk = 0; 6463 struct mkdir *mkdir1, *mkdir2; 6464 struct jaddref *jaddref; 6465 struct mount *mp; 6466 int isindir; 6467 6468 /* 6469 * Whiteouts have no dependencies. 6470 */ 6471 if (newinum == WINO) { 6472 if (newdirbp != NULL) 6473 bdwrite(newdirbp); 6474 return (0); 6475 } 6476 jaddref = NULL; 6477 mkdir1 = mkdir2 = NULL; 6478 mp = UFSTOVFS(dp->i_ump); 6479 fs = dp->i_fs; 6480 lbn = lblkno(fs, diroffset); 6481 offset = blkoff(fs, diroffset); 6482 dap = malloc(sizeof(struct diradd), M_DIRADD, 6483 M_SOFTDEP_FLAGS|M_ZERO); 6484 workitem_alloc(&dap->da_list, D_DIRADD, mp); 6485 dap->da_offset = offset; 6486 dap->da_newinum = newinum; 6487 dap->da_state = ATTACHED; 6488 LIST_INIT(&dap->da_jwork); 6489 isindir = bp->b_lblkno >= NDADDR; 6490 if (isnewblk && 6491 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) { 6492 newdirblk = malloc(sizeof(struct newdirblk), 6493 M_NEWDIRBLK, M_SOFTDEP_FLAGS); 6494 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 6495 LIST_INIT(&newdirblk->db_mkdir); 6496 } 6497 /* 6498 * If we're creating a new directory setup the dependencies and set 6499 * the dap state to wait for them. Otherwise it's COMPLETE and 6500 * we can move on. 6501 */ 6502 if (newdirbp == NULL) { 6503 dap->da_state |= DEPCOMPLETE; 6504 ACQUIRE_LOCK(&lk); 6505 } else { 6506 dap->da_state |= MKDIR_BODY | MKDIR_PARENT; 6507 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp, 6508 &mkdir2); 6509 } 6510 /* 6511 * Link into parent directory pagedep to await its being written. 6512 */ 6513 if (pagedep_lookup(mp, dp->i_number, lbn, DEPALLOC, &pagedep) == 0) 6514 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list); 6515#ifdef DEBUG 6516 if (diradd_lookup(pagedep, offset) != NULL) 6517 panic("softdep_setup_directory_add: %p already at off %d\n", 6518 diradd_lookup(pagedep, offset), offset); 6519#endif 6520 dap->da_pagedep = pagedep; 6521 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap, 6522 da_pdlist); 6523 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 6524 /* 6525 * If we're journaling, link the diradd into the jaddref so it 6526 * may be completed after the journal entry is written. Otherwise, 6527 * link the diradd into its inodedep. If the inode is not yet 6528 * written place it on the bufwait list, otherwise do the post-inode 6529 * write processing to put it on the id_pendinghd list. 6530 */ 6531 if (mp->mnt_kern_flag & MNTK_SUJ) { 6532 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 6533 inoreflst); 6534 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 6535 ("softdep_setup_directory_add: bad jaddref %p", jaddref)); 6536 jaddref->ja_diroff = diroffset; 6537 jaddref->ja_diradd = dap; 6538 add_to_journal(&jaddref->ja_list); 6539 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) 6540 diradd_inode_written(dap, inodedep); 6541 else 6542 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 6543 /* 6544 * Add the journal entries for . and .. links now that the primary 6545 * link is written. 6546 */ 6547 if (mkdir1 != NULL && mp->mnt_kern_flag & MNTK_SUJ) { 6548 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 6549 inoreflst, if_deps); 6550 KASSERT(jaddref != NULL && 6551 jaddref->ja_ino == jaddref->ja_parent && 6552 (jaddref->ja_state & MKDIR_BODY), 6553 ("softdep_setup_directory_add: bad dot jaddref %p", 6554 jaddref)); 6555 mkdir1->md_jaddref = jaddref; 6556 jaddref->ja_mkdir = mkdir1; 6557 /* 6558 * It is important that the dotdot journal entry 6559 * is added prior to the dot entry since dot writes 6560 * both the dot and dotdot links. These both must 6561 * be added after the primary link for the journal 6562 * to remain consistent. 6563 */ 6564 add_to_journal(&mkdir2->md_jaddref->ja_list); 6565 add_to_journal(&jaddref->ja_list); 6566 } 6567 /* 6568 * If we are adding a new directory remember this diradd so that if 6569 * we rename it we can keep the dot and dotdot dependencies. If 6570 * we are adding a new name for an inode that has a mkdiradd we 6571 * must be in rename and we have to move the dot and dotdot 6572 * dependencies to this new name. The old name is being orphaned 6573 * soon. 6574 */ 6575 if (mkdir1 != NULL) { 6576 if (inodedep->id_mkdiradd != NULL) 6577 panic("softdep_setup_directory_add: Existing mkdir"); 6578 inodedep->id_mkdiradd = dap; 6579 } else if (inodedep->id_mkdiradd) 6580 merge_diradd(inodedep, dap); 6581 if (newdirblk) { 6582 /* 6583 * There is nothing to do if we are already tracking 6584 * this block. 6585 */ 6586 if ((pagedep->pd_state & NEWBLOCK) != 0) { 6587 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 6588 FREE_LOCK(&lk); 6589 return (0); 6590 } 6591 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk) 6592 == 0) 6593 panic("softdep_setup_directory_add: lost entry"); 6594 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 6595 pagedep->pd_state |= NEWBLOCK; 6596 pagedep->pd_newdirblk = newdirblk; 6597 newdirblk->db_pagedep = pagedep; 6598 FREE_LOCK(&lk); 6599 /* 6600 * If we extended into an indirect signal direnter to sync. 6601 */ 6602 if (isindir) 6603 return (1); 6604 return (0); 6605 } 6606 FREE_LOCK(&lk); 6607 return (0); 6608} 6609 6610/* 6611 * This procedure is called to change the offset of a directory 6612 * entry when compacting a directory block which must be owned 6613 * exclusively by the caller. Note that the actual entry movement 6614 * must be done in this procedure to ensure that no I/O completions 6615 * occur while the move is in progress. 6616 */ 6617void 6618softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 6619 struct buf *bp; /* Buffer holding directory block. */ 6620 struct inode *dp; /* inode for directory */ 6621 caddr_t base; /* address of dp->i_offset */ 6622 caddr_t oldloc; /* address of old directory location */ 6623 caddr_t newloc; /* address of new directory location */ 6624 int entrysize; /* size of directory entry */ 6625{ 6626 int offset, oldoffset, newoffset; 6627 struct pagedep *pagedep; 6628 struct jmvref *jmvref; 6629 struct diradd *dap; 6630 struct direct *de; 6631 struct mount *mp; 6632 ufs_lbn_t lbn; 6633 int flags; 6634 6635 mp = UFSTOVFS(dp->i_ump); 6636 de = (struct direct *)oldloc; 6637 jmvref = NULL; 6638 flags = 0; 6639 /* 6640 * Moves are always journaled as it would be too complex to 6641 * determine if any affected adds or removes are present in the 6642 * journal. 6643 */ 6644 if (mp->mnt_kern_flag & MNTK_SUJ) { 6645 flags = DEPALLOC; 6646 jmvref = newjmvref(dp, de->d_ino, 6647 dp->i_offset + (oldloc - base), 6648 dp->i_offset + (newloc - base)); 6649 } 6650 lbn = lblkno(dp->i_fs, dp->i_offset); 6651 offset = blkoff(dp->i_fs, dp->i_offset); 6652 oldoffset = offset + (oldloc - base); 6653 newoffset = offset + (newloc - base); 6654 ACQUIRE_LOCK(&lk); 6655 if (pagedep_lookup(mp, dp->i_number, lbn, flags, &pagedep) == 0) { 6656 if (pagedep) 6657 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list); 6658 goto done; 6659 } 6660 dap = diradd_lookup(pagedep, oldoffset); 6661 if (dap) { 6662 dap->da_offset = newoffset; 6663 newoffset = DIRADDHASH(newoffset); 6664 oldoffset = DIRADDHASH(oldoffset); 6665 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE && 6666 newoffset != oldoffset) { 6667 LIST_REMOVE(dap, da_pdlist); 6668 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset], 6669 dap, da_pdlist); 6670 } 6671 } 6672done: 6673 if (jmvref) { 6674 jmvref->jm_pagedep = pagedep; 6675 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps); 6676 add_to_journal(&jmvref->jm_list); 6677 } 6678 bcopy(oldloc, newloc, entrysize); 6679 FREE_LOCK(&lk); 6680} 6681 6682/* 6683 * Move the mkdir dependencies and journal work from one diradd to another 6684 * when renaming a directory. The new name must depend on the mkdir deps 6685 * completing as the old name did. Directories can only have one valid link 6686 * at a time so one must be canonical. 6687 */ 6688static void 6689merge_diradd(inodedep, newdap) 6690 struct inodedep *inodedep; 6691 struct diradd *newdap; 6692{ 6693 struct diradd *olddap; 6694 struct mkdir *mkdir, *nextmd; 6695 short state; 6696 6697 olddap = inodedep->id_mkdiradd; 6698 inodedep->id_mkdiradd = newdap; 6699 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 6700 newdap->da_state &= ~DEPCOMPLETE; 6701 for (mkdir = LIST_FIRST(&mkdirlisthd); mkdir; mkdir = nextmd) { 6702 nextmd = LIST_NEXT(mkdir, md_mkdirs); 6703 if (mkdir->md_diradd != olddap) 6704 continue; 6705 mkdir->md_diradd = newdap; 6706 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY); 6707 newdap->da_state |= state; 6708 olddap->da_state &= ~state; 6709 if ((olddap->da_state & 6710 (MKDIR_PARENT | MKDIR_BODY)) == 0) 6711 break; 6712 } 6713 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 6714 panic("merge_diradd: unfound ref"); 6715 } 6716 /* 6717 * Any mkdir related journal items are not safe to be freed until 6718 * the new name is stable. 6719 */ 6720 jwork_move(&newdap->da_jwork, &olddap->da_jwork); 6721 olddap->da_state |= DEPCOMPLETE; 6722 complete_diradd(olddap); 6723} 6724 6725/* 6726 * Move the diradd to the pending list when all diradd dependencies are 6727 * complete. 6728 */ 6729static void 6730complete_diradd(dap) 6731 struct diradd *dap; 6732{ 6733 struct pagedep *pagedep; 6734 6735 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 6736 if (dap->da_state & DIRCHG) 6737 pagedep = dap->da_previous->dm_pagedep; 6738 else 6739 pagedep = dap->da_pagedep; 6740 LIST_REMOVE(dap, da_pdlist); 6741 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 6742 } 6743} 6744 6745/* 6746 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal 6747 * add entries and conditonally journal the remove. 6748 */ 6749static void 6750cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref) 6751 struct diradd *dap; 6752 struct dirrem *dirrem; 6753 struct jremref *jremref; 6754 struct jremref *dotremref; 6755 struct jremref *dotdotremref; 6756{ 6757 struct inodedep *inodedep; 6758 struct jaddref *jaddref; 6759 struct inoref *inoref; 6760 struct mkdir *mkdir; 6761 6762 /* 6763 * If no remove references were allocated we're on a non-journaled 6764 * filesystem and can skip the cancel step. 6765 */ 6766 if (jremref == NULL) { 6767 free_diradd(dap, NULL); 6768 return; 6769 } 6770 /* 6771 * Cancel the primary name an free it if it does not require 6772 * journaling. 6773 */ 6774 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum, 6775 0, &inodedep) != 0) { 6776 /* Abort the addref that reference this diradd. */ 6777 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 6778 if (inoref->if_list.wk_type != D_JADDREF) 6779 continue; 6780 jaddref = (struct jaddref *)inoref; 6781 if (jaddref->ja_diradd != dap) 6782 continue; 6783 if (cancel_jaddref(jaddref, inodedep, 6784 &dirrem->dm_jwork) == 0) { 6785 free_jremref(jremref); 6786 jremref = NULL; 6787 } 6788 break; 6789 } 6790 } 6791 /* 6792 * Cancel subordinate names and free them if they do not require 6793 * journaling. 6794 */ 6795 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 6796 LIST_FOREACH(mkdir, &mkdirlisthd, md_mkdirs) { 6797 if (mkdir->md_diradd != dap) 6798 continue; 6799 if ((jaddref = mkdir->md_jaddref) == NULL) 6800 continue; 6801 mkdir->md_jaddref = NULL; 6802 if (mkdir->md_state & MKDIR_PARENT) { 6803 if (cancel_jaddref(jaddref, NULL, 6804 &dirrem->dm_jwork) == 0) { 6805 free_jremref(dotdotremref); 6806 dotdotremref = NULL; 6807 } 6808 } else { 6809 if (cancel_jaddref(jaddref, inodedep, 6810 &dirrem->dm_jwork) == 0) { 6811 free_jremref(dotremref); 6812 dotremref = NULL; 6813 } 6814 } 6815 } 6816 } 6817 6818 if (jremref) 6819 journal_jremref(dirrem, jremref, inodedep); 6820 if (dotremref) 6821 journal_jremref(dirrem, dotremref, inodedep); 6822 if (dotdotremref) 6823 journal_jremref(dirrem, dotdotremref, NULL); 6824 jwork_move(&dirrem->dm_jwork, &dap->da_jwork); 6825 free_diradd(dap, &dirrem->dm_jwork); 6826} 6827 6828/* 6829 * Free a diradd dependency structure. This routine must be called 6830 * with splbio interrupts blocked. 6831 */ 6832static void 6833free_diradd(dap, wkhd) 6834 struct diradd *dap; 6835 struct workhead *wkhd; 6836{ 6837 struct dirrem *dirrem; 6838 struct pagedep *pagedep; 6839 struct inodedep *inodedep; 6840 struct mkdir *mkdir, *nextmd; 6841 6842 mtx_assert(&lk, MA_OWNED); 6843 LIST_REMOVE(dap, da_pdlist); 6844 if (dap->da_state & ONWORKLIST) 6845 WORKLIST_REMOVE(&dap->da_list); 6846 if ((dap->da_state & DIRCHG) == 0) { 6847 pagedep = dap->da_pagedep; 6848 } else { 6849 dirrem = dap->da_previous; 6850 pagedep = dirrem->dm_pagedep; 6851 dirrem->dm_dirinum = pagedep->pd_ino; 6852 dirrem->dm_state |= COMPLETE; 6853 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 6854 add_to_worklist(&dirrem->dm_list, 0); 6855 } 6856 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum, 6857 0, &inodedep) != 0) 6858 if (inodedep->id_mkdiradd == dap) 6859 inodedep->id_mkdiradd = NULL; 6860 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 6861 for (mkdir = LIST_FIRST(&mkdirlisthd); mkdir; mkdir = nextmd) { 6862 nextmd = LIST_NEXT(mkdir, md_mkdirs); 6863 if (mkdir->md_diradd != dap) 6864 continue; 6865 dap->da_state &= 6866 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 6867 LIST_REMOVE(mkdir, md_mkdirs); 6868 if (mkdir->md_state & ONWORKLIST) 6869 WORKLIST_REMOVE(&mkdir->md_list); 6870 if (mkdir->md_jaddref != NULL) 6871 panic("free_diradd: Unexpected jaddref"); 6872 WORKITEM_FREE(mkdir, D_MKDIR); 6873 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) 6874 break; 6875 } 6876 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 6877 panic("free_diradd: unfound ref"); 6878 } 6879 if (inodedep) 6880 free_inodedep(inodedep); 6881 /* 6882 * Free any journal segments waiting for the directory write. 6883 */ 6884 handle_jwork(&dap->da_jwork); 6885 WORKITEM_FREE(dap, D_DIRADD); 6886} 6887 6888/* 6889 * Directory entry removal dependencies. 6890 * 6891 * When removing a directory entry, the entry's inode pointer must be 6892 * zero'ed on disk before the corresponding inode's link count is decremented 6893 * (possibly freeing the inode for re-use). This dependency is handled by 6894 * updating the directory entry but delaying the inode count reduction until 6895 * after the directory block has been written to disk. After this point, the 6896 * inode count can be decremented whenever it is convenient. 6897 */ 6898 6899/* 6900 * This routine should be called immediately after removing 6901 * a directory entry. The inode's link count should not be 6902 * decremented by the calling procedure -- the soft updates 6903 * code will do this task when it is safe. 6904 */ 6905void 6906softdep_setup_remove(bp, dp, ip, isrmdir) 6907 struct buf *bp; /* buffer containing directory block */ 6908 struct inode *dp; /* inode for the directory being modified */ 6909 struct inode *ip; /* inode for directory entry being removed */ 6910 int isrmdir; /* indicates if doing RMDIR */ 6911{ 6912 struct dirrem *dirrem, *prevdirrem; 6913 struct inodedep *inodedep; 6914 int direct; 6915 6916 /* 6917 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want 6918 * newdirrem() to setup the full directory remove which requires 6919 * isrmdir > 1. 6920 */ 6921 dirrem = newdirrem(bp, dp, ip, isrmdir?2:0, &prevdirrem); 6922 /* 6923 * Add the dirrem to the inodedep's pending remove list for quick 6924 * discovery later. 6925 */ 6926 if (inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0, 6927 &inodedep) == 0) 6928 panic("softdep_setup_remove: Lost inodedep."); 6929 dirrem->dm_state |= ONDEPLIST; 6930 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 6931 6932 /* 6933 * If the COMPLETE flag is clear, then there were no active 6934 * entries and we want to roll back to a zeroed entry until 6935 * the new inode is committed to disk. If the COMPLETE flag is 6936 * set then we have deleted an entry that never made it to 6937 * disk. If the entry we deleted resulted from a name change, 6938 * then the old name still resides on disk. We cannot delete 6939 * its inode (returned to us in prevdirrem) until the zeroed 6940 * directory entry gets to disk. The new inode has never been 6941 * referenced on the disk, so can be deleted immediately. 6942 */ 6943 if ((dirrem->dm_state & COMPLETE) == 0) { 6944 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem, 6945 dm_next); 6946 FREE_LOCK(&lk); 6947 } else { 6948 if (prevdirrem != NULL) 6949 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, 6950 prevdirrem, dm_next); 6951 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino; 6952 direct = LIST_EMPTY(&dirrem->dm_jremrefhd); 6953 FREE_LOCK(&lk); 6954 if (direct) 6955 handle_workitem_remove(dirrem, NULL); 6956 } 6957} 6958 6959/* 6960 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the 6961 * pd_pendinghd list of a pagedep. 6962 */ 6963static struct diradd * 6964diradd_lookup(pagedep, offset) 6965 struct pagedep *pagedep; 6966 int offset; 6967{ 6968 struct diradd *dap; 6969 6970 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist) 6971 if (dap->da_offset == offset) 6972 return (dap); 6973 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 6974 if (dap->da_offset == offset) 6975 return (dap); 6976 return (NULL); 6977} 6978 6979/* 6980 * Search for a .. diradd dependency in a directory that is being removed. 6981 * If the directory was renamed to a new parent we have a diradd rather 6982 * than a mkdir for the .. entry. We need to cancel it now before 6983 * it is found in truncate(). 6984 */ 6985static struct jremref * 6986cancel_diradd_dotdot(ip, dirrem, jremref) 6987 struct inode *ip; 6988 struct dirrem *dirrem; 6989 struct jremref *jremref; 6990{ 6991 struct pagedep *pagedep; 6992 struct diradd *dap; 6993 struct worklist *wk; 6994 6995 if (pagedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0, 0, 6996 &pagedep) == 0) 6997 return (jremref); 6998 dap = diradd_lookup(pagedep, DOTDOT_OFFSET); 6999 if (dap == NULL) 7000 return (jremref); 7001 cancel_diradd(dap, dirrem, jremref, NULL, NULL); 7002 /* 7003 * Mark any journal work as belonging to the parent so it is freed 7004 * with the .. reference. 7005 */ 7006 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 7007 wk->wk_state |= MKDIR_PARENT; 7008 return (NULL); 7009} 7010 7011/* 7012 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to 7013 * replace it with a dirrem/diradd pair as a result of re-parenting a 7014 * directory. This ensures that we don't simultaneously have a mkdir and 7015 * a diradd for the same .. entry. 7016 */ 7017static struct jremref * 7018cancel_mkdir_dotdot(ip, dirrem, jremref) 7019 struct inode *ip; 7020 struct dirrem *dirrem; 7021 struct jremref *jremref; 7022{ 7023 struct inodedep *inodedep; 7024 struct jaddref *jaddref; 7025 struct mkdir *mkdir; 7026 struct diradd *dap; 7027 7028 if (inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0, 7029 &inodedep) == 0) 7030 panic("cancel_mkdir_dotdot: Lost inodedep"); 7031 dap = inodedep->id_mkdiradd; 7032 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0) 7033 return (jremref); 7034 for (mkdir = LIST_FIRST(&mkdirlisthd); mkdir; 7035 mkdir = LIST_NEXT(mkdir, md_mkdirs)) 7036 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT) 7037 break; 7038 if (mkdir == NULL) 7039 panic("cancel_mkdir_dotdot: Unable to find mkdir\n"); 7040 if ((jaddref = mkdir->md_jaddref) != NULL) { 7041 mkdir->md_jaddref = NULL; 7042 jaddref->ja_state &= ~MKDIR_PARENT; 7043 if (inodedep_lookup(UFSTOVFS(ip->i_ump), jaddref->ja_ino, 0, 7044 &inodedep) == 0) 7045 panic("cancel_mkdir_dotdot: Lost parent inodedep"); 7046 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) { 7047 journal_jremref(dirrem, jremref, inodedep); 7048 jremref = NULL; 7049 } 7050 } 7051 if (mkdir->md_state & ONWORKLIST) 7052 WORKLIST_REMOVE(&mkdir->md_list); 7053 mkdir->md_state |= ALLCOMPLETE; 7054 complete_mkdir(mkdir); 7055 return (jremref); 7056} 7057 7058static void 7059journal_jremref(dirrem, jremref, inodedep) 7060 struct dirrem *dirrem; 7061 struct jremref *jremref; 7062 struct inodedep *inodedep; 7063{ 7064 7065 if (inodedep == NULL) 7066 if (inodedep_lookup(jremref->jr_list.wk_mp, 7067 jremref->jr_ref.if_ino, 0, &inodedep) == 0) 7068 panic("journal_jremref: Lost inodedep"); 7069 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps); 7070 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 7071 add_to_journal(&jremref->jr_list); 7072} 7073 7074static void 7075dirrem_journal(dirrem, jremref, dotremref, dotdotremref) 7076 struct dirrem *dirrem; 7077 struct jremref *jremref; 7078 struct jremref *dotremref; 7079 struct jremref *dotdotremref; 7080{ 7081 struct inodedep *inodedep; 7082 7083 7084 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0, 7085 &inodedep) == 0) 7086 panic("dirrem_journal: Lost inodedep"); 7087 journal_jremref(dirrem, jremref, inodedep); 7088 if (dotremref) 7089 journal_jremref(dirrem, dotremref, inodedep); 7090 if (dotdotremref) 7091 journal_jremref(dirrem, dotdotremref, NULL); 7092} 7093 7094/* 7095 * Allocate a new dirrem if appropriate and return it along with 7096 * its associated pagedep. Called without a lock, returns with lock. 7097 */ 7098static long num_dirrem; /* number of dirrem allocated */ 7099static struct dirrem * 7100newdirrem(bp, dp, ip, isrmdir, prevdirremp) 7101 struct buf *bp; /* buffer containing directory block */ 7102 struct inode *dp; /* inode for the directory being modified */ 7103 struct inode *ip; /* inode for directory entry being removed */ 7104 int isrmdir; /* indicates if doing RMDIR */ 7105 struct dirrem **prevdirremp; /* previously referenced inode, if any */ 7106{ 7107 int offset; 7108 ufs_lbn_t lbn; 7109 struct diradd *dap; 7110 struct dirrem *dirrem; 7111 struct pagedep *pagedep; 7112 struct jremref *jremref; 7113 struct jremref *dotremref; 7114 struct jremref *dotdotremref; 7115 struct vnode *dvp; 7116 7117 /* 7118 * Whiteouts have no deletion dependencies. 7119 */ 7120 if (ip == NULL) 7121 panic("newdirrem: whiteout"); 7122 dvp = ITOV(dp); 7123 /* 7124 * If we are over our limit, try to improve the situation. 7125 * Limiting the number of dirrem structures will also limit 7126 * the number of freefile and freeblks structures. 7127 */ 7128 ACQUIRE_LOCK(&lk); 7129 if (!(ip->i_flags & SF_SNAPSHOT) && num_dirrem > max_softdeps / 2) 7130 (void) request_cleanup(ITOV(dp)->v_mount, FLUSH_REMOVE); 7131 num_dirrem += 1; 7132 FREE_LOCK(&lk); 7133 dirrem = malloc(sizeof(struct dirrem), 7134 M_DIRREM, M_SOFTDEP_FLAGS|M_ZERO); 7135 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount); 7136 LIST_INIT(&dirrem->dm_jremrefhd); 7137 LIST_INIT(&dirrem->dm_jwork); 7138 dirrem->dm_state = isrmdir ? RMDIR : 0; 7139 dirrem->dm_oldinum = ip->i_number; 7140 *prevdirremp = NULL; 7141 /* 7142 * Allocate remove reference structures to track journal write 7143 * dependencies. We will always have one for the link and 7144 * when doing directories we will always have one more for dot. 7145 * When renaming a directory we skip the dotdot link change so 7146 * this is not needed. 7147 */ 7148 jremref = dotremref = dotdotremref = NULL; 7149 if (DOINGSUJ(dvp)) { 7150 if (isrmdir) { 7151 jremref = newjremref(dirrem, dp, ip, dp->i_offset, 7152 ip->i_effnlink + 2); 7153 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET, 7154 ip->i_effnlink + 1); 7155 } else 7156 jremref = newjremref(dirrem, dp, ip, dp->i_offset, 7157 ip->i_effnlink + 1); 7158 if (isrmdir > 1) { 7159 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET, 7160 dp->i_effnlink + 1); 7161 dotdotremref->jr_state |= MKDIR_PARENT; 7162 } 7163 } 7164 ACQUIRE_LOCK(&lk); 7165 lbn = lblkno(dp->i_fs, dp->i_offset); 7166 offset = blkoff(dp->i_fs, dp->i_offset); 7167 if (pagedep_lookup(UFSTOVFS(dp->i_ump), dp->i_number, lbn, DEPALLOC, 7168 &pagedep) == 0) 7169 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list); 7170 dirrem->dm_pagedep = pagedep; 7171 /* 7172 * If we're renaming a .. link to a new directory, cancel any 7173 * existing MKDIR_PARENT mkdir. If it has already been canceled 7174 * the jremref is preserved for any potential diradd in this 7175 * location. This can not coincide with a rmdir. 7176 */ 7177 if (dp->i_offset == DOTDOT_OFFSET) { 7178 if (isrmdir) 7179 panic("newdirrem: .. directory change during remove?"); 7180 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref); 7181 } 7182 /* 7183 * If we're removing a directory search for the .. dependency now and 7184 * cancel it. Any pending journal work will be added to the dirrem 7185 * to be completed when the workitem remove completes. 7186 */ 7187 if (isrmdir > 1) 7188 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref); 7189 /* 7190 * Check for a diradd dependency for the same directory entry. 7191 * If present, then both dependencies become obsolete and can 7192 * be de-allocated. 7193 */ 7194 dap = diradd_lookup(pagedep, offset); 7195 if (dap == NULL) { 7196 /* 7197 * Link the jremref structures into the dirrem so they are 7198 * written prior to the pagedep. 7199 */ 7200 if (jremref) 7201 dirrem_journal(dirrem, jremref, dotremref, 7202 dotdotremref); 7203 return (dirrem); 7204 } 7205 /* 7206 * Must be ATTACHED at this point. 7207 */ 7208 if ((dap->da_state & ATTACHED) == 0) 7209 panic("newdirrem: not ATTACHED"); 7210 if (dap->da_newinum != ip->i_number) 7211 panic("newdirrem: inum %d should be %d", 7212 ip->i_number, dap->da_newinum); 7213 /* 7214 * If we are deleting a changed name that never made it to disk, 7215 * then return the dirrem describing the previous inode (which 7216 * represents the inode currently referenced from this entry on disk). 7217 */ 7218 if ((dap->da_state & DIRCHG) != 0) { 7219 *prevdirremp = dap->da_previous; 7220 dap->da_state &= ~DIRCHG; 7221 dap->da_pagedep = pagedep; 7222 } 7223 /* 7224 * We are deleting an entry that never made it to disk. 7225 * Mark it COMPLETE so we can delete its inode immediately. 7226 */ 7227 dirrem->dm_state |= COMPLETE; 7228 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref); 7229#ifdef SUJ_DEBUG 7230 if (isrmdir == 0) { 7231 struct worklist *wk; 7232 7233 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 7234 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT)) 7235 panic("bad wk %p (0x%X)\n", wk, wk->wk_state); 7236 } 7237#endif 7238 7239 return (dirrem); 7240} 7241 7242/* 7243 * Directory entry change dependencies. 7244 * 7245 * Changing an existing directory entry requires that an add operation 7246 * be completed first followed by a deletion. The semantics for the addition 7247 * are identical to the description of adding a new entry above except 7248 * that the rollback is to the old inode number rather than zero. Once 7249 * the addition dependency is completed, the removal is done as described 7250 * in the removal routine above. 7251 */ 7252 7253/* 7254 * This routine should be called immediately after changing 7255 * a directory entry. The inode's link count should not be 7256 * decremented by the calling procedure -- the soft updates 7257 * code will perform this task when it is safe. 7258 */ 7259void 7260softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 7261 struct buf *bp; /* buffer containing directory block */ 7262 struct inode *dp; /* inode for the directory being modified */ 7263 struct inode *ip; /* inode for directory entry being removed */ 7264 ino_t newinum; /* new inode number for changed entry */ 7265 int isrmdir; /* indicates if doing RMDIR */ 7266{ 7267 int offset; 7268 struct diradd *dap = NULL; 7269 struct dirrem *dirrem, *prevdirrem; 7270 struct pagedep *pagedep; 7271 struct inodedep *inodedep; 7272 struct jaddref *jaddref; 7273 struct mount *mp; 7274 7275 offset = blkoff(dp->i_fs, dp->i_offset); 7276 mp = UFSTOVFS(dp->i_ump); 7277 7278 /* 7279 * Whiteouts do not need diradd dependencies. 7280 */ 7281 if (newinum != WINO) { 7282 dap = malloc(sizeof(struct diradd), 7283 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO); 7284 workitem_alloc(&dap->da_list, D_DIRADD, mp); 7285 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE; 7286 dap->da_offset = offset; 7287 dap->da_newinum = newinum; 7288 LIST_INIT(&dap->da_jwork); 7289 } 7290 7291 /* 7292 * Allocate a new dirrem and ACQUIRE_LOCK. 7293 */ 7294 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 7295 pagedep = dirrem->dm_pagedep; 7296 /* 7297 * The possible values for isrmdir: 7298 * 0 - non-directory file rename 7299 * 1 - directory rename within same directory 7300 * inum - directory rename to new directory of given inode number 7301 * When renaming to a new directory, we are both deleting and 7302 * creating a new directory entry, so the link count on the new 7303 * directory should not change. Thus we do not need the followup 7304 * dirrem which is usually done in handle_workitem_remove. We set 7305 * the DIRCHG flag to tell handle_workitem_remove to skip the 7306 * followup dirrem. 7307 */ 7308 if (isrmdir > 1) 7309 dirrem->dm_state |= DIRCHG; 7310 7311 /* 7312 * Whiteouts have no additional dependencies, 7313 * so just put the dirrem on the correct list. 7314 */ 7315 if (newinum == WINO) { 7316 if ((dirrem->dm_state & COMPLETE) == 0) { 7317 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem, 7318 dm_next); 7319 } else { 7320 dirrem->dm_dirinum = pagedep->pd_ino; 7321 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 7322 add_to_worklist(&dirrem->dm_list, 0); 7323 } 7324 FREE_LOCK(&lk); 7325 return; 7326 } 7327 /* 7328 * Add the dirrem to the inodedep's pending remove list for quick 7329 * discovery later. A valid nlinkdelta ensures that this lookup 7330 * will not fail. 7331 */ 7332 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 7333 panic("softdep_setup_directory_change: Lost inodedep."); 7334 dirrem->dm_state |= ONDEPLIST; 7335 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 7336 7337 /* 7338 * If the COMPLETE flag is clear, then there were no active 7339 * entries and we want to roll back to the previous inode until 7340 * the new inode is committed to disk. If the COMPLETE flag is 7341 * set, then we have deleted an entry that never made it to disk. 7342 * If the entry we deleted resulted from a name change, then the old 7343 * inode reference still resides on disk. Any rollback that we do 7344 * needs to be to that old inode (returned to us in prevdirrem). If 7345 * the entry we deleted resulted from a create, then there is 7346 * no entry on the disk, so we want to roll back to zero rather 7347 * than the uncommitted inode. In either of the COMPLETE cases we 7348 * want to immediately free the unwritten and unreferenced inode. 7349 */ 7350 if ((dirrem->dm_state & COMPLETE) == 0) { 7351 dap->da_previous = dirrem; 7352 } else { 7353 if (prevdirrem != NULL) { 7354 dap->da_previous = prevdirrem; 7355 } else { 7356 dap->da_state &= ~DIRCHG; 7357 dap->da_pagedep = pagedep; 7358 } 7359 dirrem->dm_dirinum = pagedep->pd_ino; 7360 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 7361 add_to_worklist(&dirrem->dm_list, 0); 7362 } 7363 /* 7364 * Lookup the jaddref for this journal entry. We must finish 7365 * initializing it and make the diradd write dependent on it. 7366 * If we're not journaling Put it on the id_bufwait list if the inode 7367 * is not yet written. If it is written, do the post-inode write 7368 * processing to put it on the id_pendinghd list. 7369 */ 7370 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 7371 if (mp->mnt_kern_flag & MNTK_SUJ) { 7372 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 7373 inoreflst); 7374 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 7375 ("softdep_setup_directory_change: bad jaddref %p", 7376 jaddref)); 7377 jaddref->ja_diroff = dp->i_offset; 7378 jaddref->ja_diradd = dap; 7379 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 7380 dap, da_pdlist); 7381 add_to_journal(&jaddref->ja_list); 7382 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 7383 dap->da_state |= COMPLETE; 7384 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 7385 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 7386 } else { 7387 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 7388 dap, da_pdlist); 7389 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 7390 } 7391 /* 7392 * If we're making a new name for a directory that has not been 7393 * committed when need to move the dot and dotdot references to 7394 * this new name. 7395 */ 7396 if (inodedep->id_mkdiradd && dp->i_offset != DOTDOT_OFFSET) 7397 merge_diradd(inodedep, dap); 7398 FREE_LOCK(&lk); 7399} 7400 7401/* 7402 * Called whenever the link count on an inode is changed. 7403 * It creates an inode dependency so that the new reference(s) 7404 * to the inode cannot be committed to disk until the updated 7405 * inode has been written. 7406 */ 7407void 7408softdep_change_linkcnt(ip) 7409 struct inode *ip; /* the inode with the increased link count */ 7410{ 7411 struct inodedep *inodedep; 7412 7413 ACQUIRE_LOCK(&lk); 7414 inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, DEPALLOC, &inodedep); 7415 if (ip->i_nlink < ip->i_effnlink) 7416 panic("softdep_change_linkcnt: bad delta"); 7417 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 7418 FREE_LOCK(&lk); 7419} 7420 7421/* 7422 * Attach a sbdep dependency to the superblock buf so that we can keep 7423 * track of the head of the linked list of referenced but unlinked inodes. 7424 */ 7425void 7426softdep_setup_sbupdate(ump, fs, bp) 7427 struct ufsmount *ump; 7428 struct fs *fs; 7429 struct buf *bp; 7430{ 7431 struct sbdep *sbdep; 7432 struct worklist *wk; 7433 7434 if ((fs->fs_flags & FS_SUJ) == 0) 7435 return; 7436 LIST_FOREACH(wk, &bp->b_dep, wk_list) 7437 if (wk->wk_type == D_SBDEP) 7438 break; 7439 if (wk != NULL) 7440 return; 7441 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS); 7442 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump)); 7443 sbdep->sb_fs = fs; 7444 sbdep->sb_ump = ump; 7445 ACQUIRE_LOCK(&lk); 7446 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list); 7447 FREE_LOCK(&lk); 7448} 7449 7450/* 7451 * Return the first unlinked inodedep which is ready to be the head of the 7452 * list. The inodedep and all those after it must have valid next pointers. 7453 */ 7454static struct inodedep * 7455first_unlinked_inodedep(ump) 7456 struct ufsmount *ump; 7457{ 7458 struct inodedep *inodedep; 7459 struct inodedep *idp; 7460 7461 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst); 7462 inodedep; inodedep = idp) { 7463 if ((inodedep->id_state & UNLINKNEXT) == 0) 7464 return (NULL); 7465 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 7466 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0) 7467 break; 7468 if ((inodedep->id_state & UNLINKPREV) == 0) 7469 panic("first_unlinked_inodedep: prev != next"); 7470 } 7471 if (inodedep == NULL) 7472 return (NULL); 7473 7474 return (inodedep); 7475} 7476 7477/* 7478 * Set the sujfree unlinked head pointer prior to writing a superblock. 7479 */ 7480static void 7481initiate_write_sbdep(sbdep) 7482 struct sbdep *sbdep; 7483{ 7484 struct inodedep *inodedep; 7485 struct fs *bpfs; 7486 struct fs *fs; 7487 7488 bpfs = sbdep->sb_fs; 7489 fs = sbdep->sb_ump->um_fs; 7490 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 7491 if (inodedep) { 7492 fs->fs_sujfree = inodedep->id_ino; 7493 inodedep->id_state |= UNLINKPREV; 7494 } else 7495 fs->fs_sujfree = 0; 7496 bpfs->fs_sujfree = fs->fs_sujfree; 7497} 7498 7499/* 7500 * After a superblock is written determine whether it must be written again 7501 * due to a changing unlinked list head. 7502 */ 7503static int 7504handle_written_sbdep(sbdep, bp) 7505 struct sbdep *sbdep; 7506 struct buf *bp; 7507{ 7508 struct inodedep *inodedep; 7509 struct mount *mp; 7510 struct fs *fs; 7511 7512 fs = sbdep->sb_fs; 7513 mp = UFSTOVFS(sbdep->sb_ump); 7514 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 7515 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) || 7516 (inodedep == NULL && fs->fs_sujfree != 0)) { 7517 bdirty(bp); 7518 return (1); 7519 } 7520 WORKITEM_FREE(sbdep, D_SBDEP); 7521 if (fs->fs_sujfree == 0) 7522 return (0); 7523 if (inodedep_lookup(mp, fs->fs_sujfree, 0, &inodedep) == 0) 7524 panic("handle_written_sbdep: lost inodedep"); 7525 /* 7526 * Now that we have a record of this inode in stable store allow it 7527 * to be written to free up pending work. Inodes may see a lot of 7528 * write activity after they are unlinked which we must not hold up. 7529 */ 7530 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 7531 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS) 7532 panic("handle_written_sbdep: Bad inodedep %p (0x%X)", 7533 inodedep, inodedep->id_state); 7534 if (inodedep->id_state & UNLINKONLIST) 7535 break; 7536 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST; 7537 } 7538 7539 return (0); 7540} 7541 7542/* 7543 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list. 7544 */ 7545static void 7546unlinked_inodedep(mp, inodedep) 7547 struct mount *mp; 7548 struct inodedep *inodedep; 7549{ 7550 struct ufsmount *ump; 7551 7552 if ((mp->mnt_kern_flag & MNTK_SUJ) == 0) 7553 return; 7554 ump = VFSTOUFS(mp); 7555 ump->um_fs->fs_fmod = 1; 7556 inodedep->id_state |= UNLINKED; 7557 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked); 7558} 7559 7560/* 7561 * Remove an inodedep from the unlinked inodedep list. This may require 7562 * disk writes if the inode has made it that far. 7563 */ 7564static void 7565clear_unlinked_inodedep(inodedep) 7566 struct inodedep *inodedep; 7567{ 7568 struct ufsmount *ump; 7569 struct inodedep *idp; 7570 struct inodedep *idn; 7571 struct fs *fs; 7572 struct buf *bp; 7573 ino_t ino; 7574 ino_t nino; 7575 ino_t pino; 7576 int error; 7577 7578 ump = VFSTOUFS(inodedep->id_list.wk_mp); 7579 fs = ump->um_fs; 7580 ino = inodedep->id_ino; 7581 error = 0; 7582 for (;;) { 7583 /* 7584 * If nothing has yet been written simply remove us from 7585 * the in memory list and return. This is the most common 7586 * case where handle_workitem_remove() loses the final 7587 * reference. 7588 */ 7589 if ((inodedep->id_state & UNLINKLINKS) == 0) 7590 break; 7591 /* 7592 * If we have a NEXT pointer and no PREV pointer we can simply 7593 * clear NEXT's PREV and remove ourselves from the list. Be 7594 * careful not to clear PREV if the superblock points at 7595 * next as well. 7596 */ 7597 idn = TAILQ_NEXT(inodedep, id_unlinked); 7598 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) { 7599 if (idn && fs->fs_sujfree != idn->id_ino) 7600 idn->id_state &= ~UNLINKPREV; 7601 break; 7602 } 7603 /* 7604 * Here we have an inodedep which is actually linked into 7605 * the list. We must remove it by forcing a write to the 7606 * link before us, whether it be the superblock or an inode. 7607 * Unfortunately the list may change while we're waiting 7608 * on the buf lock for either resource so we must loop until 7609 * we lock the right one. If both the superblock and an 7610 * inode point to this inode we must clear the inode first 7611 * followed by the superblock. 7612 */ 7613 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 7614 pino = 0; 7615 if (idp && (idp->id_state & UNLINKNEXT)) 7616 pino = idp->id_ino; 7617 FREE_LOCK(&lk); 7618 if (pino == 0) 7619 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 7620 (int)fs->fs_sbsize, 0, 0, 0); 7621 else 7622 error = bread(ump->um_devvp, 7623 fsbtodb(fs, ino_to_fsba(fs, pino)), 7624 (int)fs->fs_bsize, NOCRED, &bp); 7625 ACQUIRE_LOCK(&lk); 7626 if (error) 7627 break; 7628 /* If the list has changed restart the loop. */ 7629 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 7630 nino = 0; 7631 if (idp && (idp->id_state & UNLINKNEXT)) 7632 nino = idp->id_ino; 7633 if (nino != pino || 7634 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) { 7635 FREE_LOCK(&lk); 7636 brelse(bp); 7637 ACQUIRE_LOCK(&lk); 7638 continue; 7639 } 7640 /* 7641 * Remove us from the in memory list. After this we cannot 7642 * access the inodedep. 7643 */ 7644 idn = TAILQ_NEXT(inodedep, id_unlinked); 7645 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS); 7646 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 7647 /* 7648 * Determine the next inode number. 7649 */ 7650 nino = 0; 7651 if (idn) { 7652 /* 7653 * If next isn't on the list we can just clear prev's 7654 * state and schedule it to be fixed later. No need 7655 * to synchronously write if we're not in the real 7656 * list. 7657 */ 7658 if ((idn->id_state & UNLINKPREV) == 0 && pino != 0) { 7659 idp->id_state &= ~UNLINKNEXT; 7660 if ((idp->id_state & ONWORKLIST) == 0) 7661 WORKLIST_INSERT(&bp->b_dep, 7662 &idp->id_list); 7663 FREE_LOCK(&lk); 7664 bawrite(bp); 7665 ACQUIRE_LOCK(&lk); 7666 return; 7667 } 7668 nino = idn->id_ino; 7669 } 7670 FREE_LOCK(&lk); 7671 /* 7672 * The predecessor's next pointer is manually updated here 7673 * so that the NEXT flag is never cleared for an element 7674 * that is in the list. 7675 */ 7676 if (pino == 0) { 7677 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 7678 ffs_oldfscompat_write((struct fs *)bp->b_data, ump); 7679 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, 7680 bp); 7681 } else if (fs->fs_magic == FS_UFS1_MAGIC) 7682 ((struct ufs1_dinode *)bp->b_data + 7683 ino_to_fsbo(fs, pino))->di_freelink = nino; 7684 else 7685 ((struct ufs2_dinode *)bp->b_data + 7686 ino_to_fsbo(fs, pino))->di_freelink = nino; 7687 /* 7688 * If the bwrite fails we have no recourse to recover. The 7689 * filesystem is corrupted already. 7690 */ 7691 bwrite(bp); 7692 ACQUIRE_LOCK(&lk); 7693 /* 7694 * If the superblock pointer still needs to be cleared force 7695 * a write here. 7696 */ 7697 if (fs->fs_sujfree == ino) { 7698 FREE_LOCK(&lk); 7699 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 7700 (int)fs->fs_sbsize, 0, 0, 0); 7701 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 7702 ffs_oldfscompat_write((struct fs *)bp->b_data, ump); 7703 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, 7704 bp); 7705 bwrite(bp); 7706 ACQUIRE_LOCK(&lk); 7707 } 7708 if (fs->fs_sujfree != ino) 7709 return; 7710 panic("clear_unlinked_inodedep: Failed to clear free head"); 7711 } 7712 if (inodedep->id_ino == fs->fs_sujfree) 7713 panic("clear_unlinked_inodedep: Freeing head of free list"); 7714 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS); 7715 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 7716 return; 7717} 7718 7719/* 7720 * This workitem decrements the inode's link count. 7721 * If the link count reaches zero, the file is removed. 7722 */ 7723static void 7724handle_workitem_remove(dirrem, xp) 7725 struct dirrem *dirrem; 7726 struct vnode *xp; 7727{ 7728 struct inodedep *inodedep; 7729 struct workhead dotdotwk; 7730 struct worklist *wk; 7731 struct ufsmount *ump; 7732 struct mount *mp; 7733 struct vnode *vp; 7734 struct inode *ip; 7735 ino_t oldinum; 7736 int error; 7737 7738 if (dirrem->dm_state & ONWORKLIST) 7739 panic("handle_workitem_remove: dirrem %p still on worklist", 7740 dirrem); 7741 oldinum = dirrem->dm_oldinum; 7742 mp = dirrem->dm_list.wk_mp; 7743 ump = VFSTOUFS(mp); 7744 if ((vp = xp) == NULL && 7745 (error = ffs_vgetf(mp, oldinum, LK_EXCLUSIVE, &vp, 7746 FFSV_FORCEINSMQ)) != 0) { 7747 softdep_error("handle_workitem_remove: vget", error); 7748 return; 7749 } 7750 ip = VTOI(vp); 7751 ACQUIRE_LOCK(&lk); 7752 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0) 7753 panic("handle_workitem_remove: lost inodedep"); 7754 if (dirrem->dm_state & ONDEPLIST) 7755 LIST_REMOVE(dirrem, dm_inonext); 7756 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 7757 ("handle_workitem_remove: Journal entries not written.")); 7758 7759 /* 7760 * Move all dependencies waiting on the remove to complete 7761 * from the dirrem to the inode inowait list to be completed 7762 * after the inode has been updated and written to disk. Any 7763 * marked MKDIR_PARENT are saved to be completed when the .. ref 7764 * is removed. 7765 */ 7766 LIST_INIT(&dotdotwk); 7767 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) { 7768 WORKLIST_REMOVE(wk); 7769 if (wk->wk_state & MKDIR_PARENT) { 7770 wk->wk_state &= ~MKDIR_PARENT; 7771 WORKLIST_INSERT(&dotdotwk, wk); 7772 continue; 7773 } 7774 WORKLIST_INSERT(&inodedep->id_inowait, wk); 7775 } 7776 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list); 7777 /* 7778 * Normal file deletion. 7779 */ 7780 if ((dirrem->dm_state & RMDIR) == 0) { 7781 ip->i_nlink--; 7782 DIP_SET(ip, i_nlink, ip->i_nlink); 7783 ip->i_flag |= IN_CHANGE; 7784 if (ip->i_nlink < ip->i_effnlink) 7785 panic("handle_workitem_remove: bad file delta"); 7786 if (ip->i_nlink == 0) 7787 unlinked_inodedep(mp, inodedep); 7788 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 7789 num_dirrem -= 1; 7790 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 7791 ("handle_workitem_remove: worklist not empty. %s", 7792 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type))); 7793 WORKITEM_FREE(dirrem, D_DIRREM); 7794 FREE_LOCK(&lk); 7795 goto out; 7796 } 7797 /* 7798 * Directory deletion. Decrement reference count for both the 7799 * just deleted parent directory entry and the reference for ".". 7800 * Arrange to have the reference count on the parent decremented 7801 * to account for the loss of "..". 7802 */ 7803 ip->i_nlink -= 2; 7804 DIP_SET(ip, i_nlink, ip->i_nlink); 7805 ip->i_flag |= IN_CHANGE; 7806 if (ip->i_nlink < ip->i_effnlink) 7807 panic("handle_workitem_remove: bad dir delta"); 7808 if (ip->i_nlink == 0) 7809 unlinked_inodedep(mp, inodedep); 7810 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 7811 /* 7812 * Rename a directory to a new parent. Since, we are both deleting 7813 * and creating a new directory entry, the link count on the new 7814 * directory should not change. Thus we skip the followup dirrem. 7815 */ 7816 if (dirrem->dm_state & DIRCHG) { 7817 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 7818 ("handle_workitem_remove: DIRCHG and worklist not empty.")); 7819 num_dirrem -= 1; 7820 WORKITEM_FREE(dirrem, D_DIRREM); 7821 FREE_LOCK(&lk); 7822 goto out; 7823 } 7824 dirrem->dm_state = ONDEPLIST; 7825 dirrem->dm_oldinum = dirrem->dm_dirinum; 7826 /* 7827 * Place the dirrem on the parent's diremhd list. 7828 */ 7829 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0) 7830 panic("handle_workitem_remove: lost dir inodedep"); 7831 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 7832 /* 7833 * If the allocated inode has never been written to disk, then 7834 * the on-disk inode is zero'ed and we can remove the file 7835 * immediately. When journaling if the inode has been marked 7836 * unlinked and not DEPCOMPLETE we know it can never be written. 7837 */ 7838 inodedep_lookup(mp, oldinum, 0, &inodedep); 7839 if (inodedep == NULL || 7840 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED || 7841 check_inode_unwritten(inodedep)) { 7842 if (xp != NULL) 7843 add_to_worklist(&dirrem->dm_list, 0); 7844 FREE_LOCK(&lk); 7845 if (xp == NULL) { 7846 vput(vp); 7847 handle_workitem_remove(dirrem, NULL); 7848 } 7849 return; 7850 } 7851 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list); 7852 FREE_LOCK(&lk); 7853 ip->i_flag |= IN_CHANGE; 7854out: 7855 ffs_update(vp, 0); 7856 if (xp == NULL) 7857 vput(vp); 7858} 7859 7860/* 7861 * Inode de-allocation dependencies. 7862 * 7863 * When an inode's link count is reduced to zero, it can be de-allocated. We 7864 * found it convenient to postpone de-allocation until after the inode is 7865 * written to disk with its new link count (zero). At this point, all of the 7866 * on-disk inode's block pointers are nullified and, with careful dependency 7867 * list ordering, all dependencies related to the inode will be satisfied and 7868 * the corresponding dependency structures de-allocated. So, if/when the 7869 * inode is reused, there will be no mixing of old dependencies with new 7870 * ones. This artificial dependency is set up by the block de-allocation 7871 * procedure above (softdep_setup_freeblocks) and completed by the 7872 * following procedure. 7873 */ 7874static void 7875handle_workitem_freefile(freefile) 7876 struct freefile *freefile; 7877{ 7878 struct workhead wkhd; 7879 struct fs *fs; 7880 struct inodedep *idp; 7881 struct ufsmount *ump; 7882 int error; 7883 7884 ump = VFSTOUFS(freefile->fx_list.wk_mp); 7885 fs = ump->um_fs; 7886#ifdef DEBUG 7887 ACQUIRE_LOCK(&lk); 7888 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp); 7889 FREE_LOCK(&lk); 7890 if (error) 7891 panic("handle_workitem_freefile: inodedep %p survived", idp); 7892#endif 7893 UFS_LOCK(ump); 7894 fs->fs_pendinginodes -= 1; 7895 UFS_UNLOCK(ump); 7896 LIST_INIT(&wkhd); 7897 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list); 7898 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp, 7899 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0) 7900 softdep_error("handle_workitem_freefile", error); 7901 ACQUIRE_LOCK(&lk); 7902 WORKITEM_FREE(freefile, D_FREEFILE); 7903 FREE_LOCK(&lk); 7904} 7905 7906 7907/* 7908 * Helper function which unlinks marker element from work list and returns 7909 * the next element on the list. 7910 */ 7911static __inline struct worklist * 7912markernext(struct worklist *marker) 7913{ 7914 struct worklist *next; 7915 7916 next = LIST_NEXT(marker, wk_list); 7917 LIST_REMOVE(marker, wk_list); 7918 return next; 7919} 7920 7921/* 7922 * Disk writes. 7923 * 7924 * The dependency structures constructed above are most actively used when file 7925 * system blocks are written to disk. No constraints are placed on when a 7926 * block can be written, but unsatisfied update dependencies are made safe by 7927 * modifying (or replacing) the source memory for the duration of the disk 7928 * write. When the disk write completes, the memory block is again brought 7929 * up-to-date. 7930 * 7931 * In-core inode structure reclamation. 7932 * 7933 * Because there are a finite number of "in-core" inode structures, they are 7934 * reused regularly. By transferring all inode-related dependencies to the 7935 * in-memory inode block and indexing them separately (via "inodedep"s), we 7936 * can allow "in-core" inode structures to be reused at any time and avoid 7937 * any increase in contention. 7938 * 7939 * Called just before entering the device driver to initiate a new disk I/O. 7940 * The buffer must be locked, thus, no I/O completion operations can occur 7941 * while we are manipulating its associated dependencies. 7942 */ 7943static void 7944softdep_disk_io_initiation(bp) 7945 struct buf *bp; /* structure describing disk write to occur */ 7946{ 7947 struct worklist *wk; 7948 struct worklist marker; 7949 struct inodedep *inodedep; 7950 struct freeblks *freeblks; 7951 struct jfreeblk *jfreeblk; 7952 struct newblk *newblk; 7953 7954 /* 7955 * We only care about write operations. There should never 7956 * be dependencies for reads. 7957 */ 7958 if (bp->b_iocmd != BIO_WRITE) 7959 panic("softdep_disk_io_initiation: not write"); 7960 7961 if (bp->b_vflags & BV_BKGRDINPROG) 7962 panic("softdep_disk_io_initiation: Writing buffer with " 7963 "background write in progress: %p", bp); 7964 7965 marker.wk_type = D_LAST + 1; /* Not a normal workitem */ 7966 PHOLD(curproc); /* Don't swap out kernel stack */ 7967 7968 ACQUIRE_LOCK(&lk); 7969 /* 7970 * Do any necessary pre-I/O processing. 7971 */ 7972 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL; 7973 wk = markernext(&marker)) { 7974 LIST_INSERT_AFTER(wk, &marker, wk_list); 7975 switch (wk->wk_type) { 7976 7977 case D_PAGEDEP: 7978 initiate_write_filepage(WK_PAGEDEP(wk), bp); 7979 continue; 7980 7981 case D_INODEDEP: 7982 inodedep = WK_INODEDEP(wk); 7983 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) 7984 initiate_write_inodeblock_ufs1(inodedep, bp); 7985 else 7986 initiate_write_inodeblock_ufs2(inodedep, bp); 7987 continue; 7988 7989 case D_INDIRDEP: 7990 initiate_write_indirdep(WK_INDIRDEP(wk), bp); 7991 continue; 7992 7993 case D_BMSAFEMAP: 7994 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp); 7995 continue; 7996 7997 case D_JSEG: 7998 WK_JSEG(wk)->js_buf = NULL; 7999 continue; 8000 8001 case D_FREEBLKS: 8002 freeblks = WK_FREEBLKS(wk); 8003 jfreeblk = LIST_FIRST(&freeblks->fb_jfreeblkhd); 8004 /* 8005 * We have to wait for the jfreeblks to be journaled 8006 * before we can write an inodeblock with updated 8007 * pointers. Be careful to arrange the marker so 8008 * we revisit the jfreeblk if it's not removed by 8009 * the first jwait(). 8010 */ 8011 if (jfreeblk != NULL) { 8012 LIST_REMOVE(&marker, wk_list); 8013 LIST_INSERT_BEFORE(wk, &marker, wk_list); 8014 jwait(&jfreeblk->jf_list); 8015 } 8016 continue; 8017 case D_ALLOCDIRECT: 8018 case D_ALLOCINDIR: 8019 /* 8020 * We have to wait for the jnewblk to be journaled 8021 * before we can write to a block otherwise the 8022 * contents may be confused with an earlier file 8023 * at recovery time. Handle the marker as described 8024 * above. 8025 */ 8026 newblk = WK_NEWBLK(wk); 8027 if (newblk->nb_jnewblk != NULL) { 8028 LIST_REMOVE(&marker, wk_list); 8029 LIST_INSERT_BEFORE(wk, &marker, wk_list); 8030 jwait(&newblk->nb_jnewblk->jn_list); 8031 } 8032 continue; 8033 8034 case D_SBDEP: 8035 initiate_write_sbdep(WK_SBDEP(wk)); 8036 continue; 8037 8038 case D_MKDIR: 8039 case D_FREEWORK: 8040 case D_FREEDEP: 8041 case D_JSEGDEP: 8042 continue; 8043 8044 default: 8045 panic("handle_disk_io_initiation: Unexpected type %s", 8046 TYPENAME(wk->wk_type)); 8047 /* NOTREACHED */ 8048 } 8049 } 8050 FREE_LOCK(&lk); 8051 PRELE(curproc); /* Allow swapout of kernel stack */ 8052} 8053 8054/* 8055 * Called from within the procedure above to deal with unsatisfied 8056 * allocation dependencies in a directory. The buffer must be locked, 8057 * thus, no I/O completion operations can occur while we are 8058 * manipulating its associated dependencies. 8059 */ 8060static void 8061initiate_write_filepage(pagedep, bp) 8062 struct pagedep *pagedep; 8063 struct buf *bp; 8064{ 8065 struct jremref *jremref; 8066 struct jmvref *jmvref; 8067 struct dirrem *dirrem; 8068 struct diradd *dap; 8069 struct direct *ep; 8070 int i; 8071 8072 if (pagedep->pd_state & IOSTARTED) { 8073 /* 8074 * This can only happen if there is a driver that does not 8075 * understand chaining. Here biodone will reissue the call 8076 * to strategy for the incomplete buffers. 8077 */ 8078 printf("initiate_write_filepage: already started\n"); 8079 return; 8080 } 8081 pagedep->pd_state |= IOSTARTED; 8082 /* 8083 * Wait for all journal remove dependencies to hit the disk. 8084 * We can not allow any potentially conflicting directory adds 8085 * to be visible before removes and rollback is too difficult. 8086 * lk may be dropped and re-acquired, however we hold the buf 8087 * locked so the dependency can not go away. 8088 */ 8089 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) 8090 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) { 8091 stat_jwait_filepage++; 8092 jwait(&jremref->jr_list); 8093 } 8094 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) { 8095 stat_jwait_filepage++; 8096 jwait(&jmvref->jm_list); 8097 } 8098 for (i = 0; i < DAHASHSZ; i++) { 8099 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 8100 ep = (struct direct *) 8101 ((char *)bp->b_data + dap->da_offset); 8102 if (ep->d_ino != dap->da_newinum) 8103 panic("%s: dir inum %d != new %d", 8104 "initiate_write_filepage", 8105 ep->d_ino, dap->da_newinum); 8106 if (dap->da_state & DIRCHG) 8107 ep->d_ino = dap->da_previous->dm_oldinum; 8108 else 8109 ep->d_ino = 0; 8110 dap->da_state &= ~ATTACHED; 8111 dap->da_state |= UNDONE; 8112 } 8113 } 8114} 8115 8116/* 8117 * Version of initiate_write_inodeblock that handles UFS1 dinodes. 8118 * Note that any bug fixes made to this routine must be done in the 8119 * version found below. 8120 * 8121 * Called from within the procedure above to deal with unsatisfied 8122 * allocation dependencies in an inodeblock. The buffer must be 8123 * locked, thus, no I/O completion operations can occur while we 8124 * are manipulating its associated dependencies. 8125 */ 8126static void 8127initiate_write_inodeblock_ufs1(inodedep, bp) 8128 struct inodedep *inodedep; 8129 struct buf *bp; /* The inode block */ 8130{ 8131 struct allocdirect *adp, *lastadp; 8132 struct ufs1_dinode *dp; 8133 struct ufs1_dinode *sip; 8134 struct inoref *inoref; 8135 struct fs *fs; 8136 ufs_lbn_t i; 8137#ifdef INVARIANTS 8138 ufs_lbn_t prevlbn = 0; 8139#endif 8140 int deplist; 8141 8142 if (inodedep->id_state & IOSTARTED) 8143 panic("initiate_write_inodeblock_ufs1: already started"); 8144 inodedep->id_state |= IOSTARTED; 8145 fs = inodedep->id_fs; 8146 dp = (struct ufs1_dinode *)bp->b_data + 8147 ino_to_fsbo(fs, inodedep->id_ino); 8148 8149 /* 8150 * If we're on the unlinked list but have not yet written our 8151 * next pointer initialize it here. 8152 */ 8153 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 8154 struct inodedep *inon; 8155 8156 inon = TAILQ_NEXT(inodedep, id_unlinked); 8157 dp->di_freelink = inon ? inon->id_ino : 0; 8158 } 8159 /* 8160 * If the bitmap is not yet written, then the allocated 8161 * inode cannot be written to disk. 8162 */ 8163 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 8164 if (inodedep->id_savedino1 != NULL) 8165 panic("initiate_write_inodeblock_ufs1: I/O underway"); 8166 FREE_LOCK(&lk); 8167 sip = malloc(sizeof(struct ufs1_dinode), 8168 M_SAVEDINO, M_SOFTDEP_FLAGS); 8169 ACQUIRE_LOCK(&lk); 8170 inodedep->id_savedino1 = sip; 8171 *inodedep->id_savedino1 = *dp; 8172 bzero((caddr_t)dp, sizeof(struct ufs1_dinode)); 8173 dp->di_gen = inodedep->id_savedino1->di_gen; 8174 dp->di_freelink = inodedep->id_savedino1->di_freelink; 8175 return; 8176 } 8177 /* 8178 * If no dependencies, then there is nothing to roll back. 8179 */ 8180 inodedep->id_savedsize = dp->di_size; 8181 inodedep->id_savedextsize = 0; 8182 inodedep->id_savednlink = dp->di_nlink; 8183 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 8184 TAILQ_EMPTY(&inodedep->id_inoreflst)) 8185 return; 8186 /* 8187 * Revert the link count to that of the first unwritten journal entry. 8188 */ 8189 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 8190 if (inoref) 8191 dp->di_nlink = inoref->if_nlink; 8192 /* 8193 * Set the dependencies to busy. 8194 */ 8195 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 8196 adp = TAILQ_NEXT(adp, ad_next)) { 8197#ifdef INVARIANTS 8198 if (deplist != 0 && prevlbn >= adp->ad_offset) 8199 panic("softdep_write_inodeblock: lbn order"); 8200 prevlbn = adp->ad_offset; 8201 if (adp->ad_offset < NDADDR && 8202 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 8203 panic("%s: direct pointer #%jd mismatch %d != %jd", 8204 "softdep_write_inodeblock", 8205 (intmax_t)adp->ad_offset, 8206 dp->di_db[adp->ad_offset], 8207 (intmax_t)adp->ad_newblkno); 8208 if (adp->ad_offset >= NDADDR && 8209 dp->di_ib[adp->ad_offset - NDADDR] != adp->ad_newblkno) 8210 panic("%s: indirect pointer #%jd mismatch %d != %jd", 8211 "softdep_write_inodeblock", 8212 (intmax_t)adp->ad_offset - NDADDR, 8213 dp->di_ib[adp->ad_offset - NDADDR], 8214 (intmax_t)adp->ad_newblkno); 8215 deplist |= 1 << adp->ad_offset; 8216 if ((adp->ad_state & ATTACHED) == 0) 8217 panic("softdep_write_inodeblock: Unknown state 0x%x", 8218 adp->ad_state); 8219#endif /* INVARIANTS */ 8220 adp->ad_state &= ~ATTACHED; 8221 adp->ad_state |= UNDONE; 8222 } 8223 /* 8224 * The on-disk inode cannot claim to be any larger than the last 8225 * fragment that has been written. Otherwise, the on-disk inode 8226 * might have fragments that were not the last block in the file 8227 * which would corrupt the filesystem. 8228 */ 8229 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 8230 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 8231 if (adp->ad_offset >= NDADDR) 8232 break; 8233 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 8234 /* keep going until hitting a rollback to a frag */ 8235 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 8236 continue; 8237 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 8238 for (i = adp->ad_offset + 1; i < NDADDR; i++) { 8239#ifdef INVARIANTS 8240 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 8241 panic("softdep_write_inodeblock: lost dep1"); 8242#endif /* INVARIANTS */ 8243 dp->di_db[i] = 0; 8244 } 8245 for (i = 0; i < NIADDR; i++) { 8246#ifdef INVARIANTS 8247 if (dp->di_ib[i] != 0 && 8248 (deplist & ((1 << NDADDR) << i)) == 0) 8249 panic("softdep_write_inodeblock: lost dep2"); 8250#endif /* INVARIANTS */ 8251 dp->di_ib[i] = 0; 8252 } 8253 return; 8254 } 8255 /* 8256 * If we have zero'ed out the last allocated block of the file, 8257 * roll back the size to the last currently allocated block. 8258 * We know that this last allocated block is a full-sized as 8259 * we already checked for fragments in the loop above. 8260 */ 8261 if (lastadp != NULL && 8262 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 8263 for (i = lastadp->ad_offset; i >= 0; i--) 8264 if (dp->di_db[i] != 0) 8265 break; 8266 dp->di_size = (i + 1) * fs->fs_bsize; 8267 } 8268 /* 8269 * The only dependencies are for indirect blocks. 8270 * 8271 * The file size for indirect block additions is not guaranteed. 8272 * Such a guarantee would be non-trivial to achieve. The conventional 8273 * synchronous write implementation also does not make this guarantee. 8274 * Fsck should catch and fix discrepancies. Arguably, the file size 8275 * can be over-estimated without destroying integrity when the file 8276 * moves into the indirect blocks (i.e., is large). If we want to 8277 * postpone fsck, we are stuck with this argument. 8278 */ 8279 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 8280 dp->di_ib[adp->ad_offset - NDADDR] = 0; 8281} 8282 8283/* 8284 * Version of initiate_write_inodeblock that handles UFS2 dinodes. 8285 * Note that any bug fixes made to this routine must be done in the 8286 * version found above. 8287 * 8288 * Called from within the procedure above to deal with unsatisfied 8289 * allocation dependencies in an inodeblock. The buffer must be 8290 * locked, thus, no I/O completion operations can occur while we 8291 * are manipulating its associated dependencies. 8292 */ 8293static void 8294initiate_write_inodeblock_ufs2(inodedep, bp) 8295 struct inodedep *inodedep; 8296 struct buf *bp; /* The inode block */ 8297{ 8298 struct allocdirect *adp, *lastadp; 8299 struct ufs2_dinode *dp; 8300 struct ufs2_dinode *sip; 8301 struct inoref *inoref; 8302 struct fs *fs; 8303 ufs_lbn_t i; 8304#ifdef INVARIANTS 8305 ufs_lbn_t prevlbn = 0; 8306#endif 8307 int deplist; 8308 8309 if (inodedep->id_state & IOSTARTED) 8310 panic("initiate_write_inodeblock_ufs2: already started"); 8311 inodedep->id_state |= IOSTARTED; 8312 fs = inodedep->id_fs; 8313 dp = (struct ufs2_dinode *)bp->b_data + 8314 ino_to_fsbo(fs, inodedep->id_ino); 8315 8316 /* 8317 * If we're on the unlinked list but have not yet written our 8318 * next pointer initialize it here. 8319 */ 8320 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 8321 struct inodedep *inon; 8322 8323 inon = TAILQ_NEXT(inodedep, id_unlinked); 8324 dp->di_freelink = inon ? inon->id_ino : 0; 8325 } 8326 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == 8327 (UNLINKED | UNLINKNEXT)) { 8328 struct inodedep *inon; 8329 ino_t freelink; 8330 8331 inon = TAILQ_NEXT(inodedep, id_unlinked); 8332 freelink = inon ? inon->id_ino : 0; 8333 if (freelink != dp->di_freelink) 8334 panic("ino %p(0x%X) %d, %d != %d", 8335 inodedep, inodedep->id_state, inodedep->id_ino, 8336 freelink, dp->di_freelink); 8337 } 8338 /* 8339 * If the bitmap is not yet written, then the allocated 8340 * inode cannot be written to disk. 8341 */ 8342 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 8343 if (inodedep->id_savedino2 != NULL) 8344 panic("initiate_write_inodeblock_ufs2: I/O underway"); 8345 FREE_LOCK(&lk); 8346 sip = malloc(sizeof(struct ufs2_dinode), 8347 M_SAVEDINO, M_SOFTDEP_FLAGS); 8348 ACQUIRE_LOCK(&lk); 8349 inodedep->id_savedino2 = sip; 8350 *inodedep->id_savedino2 = *dp; 8351 bzero((caddr_t)dp, sizeof(struct ufs2_dinode)); 8352 dp->di_gen = inodedep->id_savedino2->di_gen; 8353 dp->di_freelink = inodedep->id_savedino2->di_freelink; 8354 return; 8355 } 8356 /* 8357 * If no dependencies, then there is nothing to roll back. 8358 */ 8359 inodedep->id_savedsize = dp->di_size; 8360 inodedep->id_savedextsize = dp->di_extsize; 8361 inodedep->id_savednlink = dp->di_nlink; 8362 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 8363 TAILQ_EMPTY(&inodedep->id_extupdt) && 8364 TAILQ_EMPTY(&inodedep->id_inoreflst)) 8365 return; 8366 /* 8367 * Revert the link count to that of the first unwritten journal entry. 8368 */ 8369 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 8370 if (inoref) 8371 dp->di_nlink = inoref->if_nlink; 8372 8373 /* 8374 * Set the ext data dependencies to busy. 8375 */ 8376 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 8377 adp = TAILQ_NEXT(adp, ad_next)) { 8378#ifdef INVARIANTS 8379 if (deplist != 0 && prevlbn >= adp->ad_offset) 8380 panic("softdep_write_inodeblock: lbn order"); 8381 prevlbn = adp->ad_offset; 8382 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno) 8383 panic("%s: direct pointer #%jd mismatch %jd != %jd", 8384 "softdep_write_inodeblock", 8385 (intmax_t)adp->ad_offset, 8386 (intmax_t)dp->di_extb[adp->ad_offset], 8387 (intmax_t)adp->ad_newblkno); 8388 deplist |= 1 << adp->ad_offset; 8389 if ((adp->ad_state & ATTACHED) == 0) 8390 panic("softdep_write_inodeblock: Unknown state 0x%x", 8391 adp->ad_state); 8392#endif /* INVARIANTS */ 8393 adp->ad_state &= ~ATTACHED; 8394 adp->ad_state |= UNDONE; 8395 } 8396 /* 8397 * The on-disk inode cannot claim to be any larger than the last 8398 * fragment that has been written. Otherwise, the on-disk inode 8399 * might have fragments that were not the last block in the ext 8400 * data which would corrupt the filesystem. 8401 */ 8402 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 8403 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 8404 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno; 8405 /* keep going until hitting a rollback to a frag */ 8406 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 8407 continue; 8408 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 8409 for (i = adp->ad_offset + 1; i < NXADDR; i++) { 8410#ifdef INVARIANTS 8411 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0) 8412 panic("softdep_write_inodeblock: lost dep1"); 8413#endif /* INVARIANTS */ 8414 dp->di_extb[i] = 0; 8415 } 8416 lastadp = NULL; 8417 break; 8418 } 8419 /* 8420 * If we have zero'ed out the last allocated block of the ext 8421 * data, roll back the size to the last currently allocated block. 8422 * We know that this last allocated block is a full-sized as 8423 * we already checked for fragments in the loop above. 8424 */ 8425 if (lastadp != NULL && 8426 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 8427 for (i = lastadp->ad_offset; i >= 0; i--) 8428 if (dp->di_extb[i] != 0) 8429 break; 8430 dp->di_extsize = (i + 1) * fs->fs_bsize; 8431 } 8432 /* 8433 * Set the file data dependencies to busy. 8434 */ 8435 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 8436 adp = TAILQ_NEXT(adp, ad_next)) { 8437#ifdef INVARIANTS 8438 if (deplist != 0 && prevlbn >= adp->ad_offset) 8439 panic("softdep_write_inodeblock: lbn order"); 8440 prevlbn = adp->ad_offset; 8441 if (adp->ad_offset < NDADDR && 8442 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 8443 panic("%s: direct pointer #%jd mismatch %jd != %jd", 8444 "softdep_write_inodeblock", 8445 (intmax_t)adp->ad_offset, 8446 (intmax_t)dp->di_db[adp->ad_offset], 8447 (intmax_t)adp->ad_newblkno); 8448 if (adp->ad_offset >= NDADDR && 8449 dp->di_ib[adp->ad_offset - NDADDR] != adp->ad_newblkno) 8450 panic("%s indirect pointer #%jd mismatch %jd != %jd", 8451 "softdep_write_inodeblock:", 8452 (intmax_t)adp->ad_offset - NDADDR, 8453 (intmax_t)dp->di_ib[adp->ad_offset - NDADDR], 8454 (intmax_t)adp->ad_newblkno); 8455 deplist |= 1 << adp->ad_offset; 8456 if ((adp->ad_state & ATTACHED) == 0) 8457 panic("softdep_write_inodeblock: Unknown state 0x%x", 8458 adp->ad_state); 8459#endif /* INVARIANTS */ 8460 adp->ad_state &= ~ATTACHED; 8461 adp->ad_state |= UNDONE; 8462 } 8463 /* 8464 * The on-disk inode cannot claim to be any larger than the last 8465 * fragment that has been written. Otherwise, the on-disk inode 8466 * might have fragments that were not the last block in the file 8467 * which would corrupt the filesystem. 8468 */ 8469 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 8470 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 8471 if (adp->ad_offset >= NDADDR) 8472 break; 8473 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 8474 /* keep going until hitting a rollback to a frag */ 8475 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 8476 continue; 8477 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 8478 for (i = adp->ad_offset + 1; i < NDADDR; i++) { 8479#ifdef INVARIANTS 8480 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 8481 panic("softdep_write_inodeblock: lost dep2"); 8482#endif /* INVARIANTS */ 8483 dp->di_db[i] = 0; 8484 } 8485 for (i = 0; i < NIADDR; i++) { 8486#ifdef INVARIANTS 8487 if (dp->di_ib[i] != 0 && 8488 (deplist & ((1 << NDADDR) << i)) == 0) 8489 panic("softdep_write_inodeblock: lost dep3"); 8490#endif /* INVARIANTS */ 8491 dp->di_ib[i] = 0; 8492 } 8493 return; 8494 } 8495 /* 8496 * If we have zero'ed out the last allocated block of the file, 8497 * roll back the size to the last currently allocated block. 8498 * We know that this last allocated block is a full-sized as 8499 * we already checked for fragments in the loop above. 8500 */ 8501 if (lastadp != NULL && 8502 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 8503 for (i = lastadp->ad_offset; i >= 0; i--) 8504 if (dp->di_db[i] != 0) 8505 break; 8506 dp->di_size = (i + 1) * fs->fs_bsize; 8507 } 8508 /* 8509 * The only dependencies are for indirect blocks. 8510 * 8511 * The file size for indirect block additions is not guaranteed. 8512 * Such a guarantee would be non-trivial to achieve. The conventional 8513 * synchronous write implementation also does not make this guarantee. 8514 * Fsck should catch and fix discrepancies. Arguably, the file size 8515 * can be over-estimated without destroying integrity when the file 8516 * moves into the indirect blocks (i.e., is large). If we want to 8517 * postpone fsck, we are stuck with this argument. 8518 */ 8519 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 8520 dp->di_ib[adp->ad_offset - NDADDR] = 0; 8521} 8522 8523/* 8524 * Cancel an indirdep as a result of truncation. Release all of the 8525 * children allocindirs and place their journal work on the appropriate 8526 * list. 8527 */ 8528static void 8529cancel_indirdep(indirdep, bp, inodedep, freeblks) 8530 struct indirdep *indirdep; 8531 struct buf *bp; 8532 struct inodedep *inodedep; 8533 struct freeblks *freeblks; 8534{ 8535 struct allocindir *aip; 8536 8537 /* 8538 * None of the indirect pointers will ever be visible, 8539 * so they can simply be tossed. GOINGAWAY ensures 8540 * that allocated pointers will be saved in the buffer 8541 * cache until they are freed. Note that they will 8542 * only be able to be found by their physical address 8543 * since the inode mapping the logical address will 8544 * be gone. The save buffer used for the safe copy 8545 * was allocated in setup_allocindir_phase2 using 8546 * the physical address so it could be used for this 8547 * purpose. Hence we swap the safe copy with the real 8548 * copy, allowing the safe copy to be freed and holding 8549 * on to the real copy for later use in indir_trunc. 8550 */ 8551 if (indirdep->ir_state & GOINGAWAY) 8552 panic("cancel_indirdep: already gone"); 8553 if (indirdep->ir_state & ONDEPLIST) { 8554 indirdep->ir_state &= ~ONDEPLIST; 8555 LIST_REMOVE(indirdep, ir_next); 8556 } 8557 indirdep->ir_state |= GOINGAWAY; 8558 VFSTOUFS(indirdep->ir_list.wk_mp)->um_numindirdeps += 1; 8559 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != 0) 8560 cancel_allocindir(aip, inodedep, freeblks); 8561 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0) 8562 cancel_allocindir(aip, inodedep, freeblks); 8563 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != 0) 8564 cancel_allocindir(aip, inodedep, freeblks); 8565 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != 0) 8566 cancel_allocindir(aip, inodedep, freeblks); 8567 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount); 8568 WORKLIST_REMOVE(&indirdep->ir_list); 8569 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list); 8570 indirdep->ir_savebp = NULL; 8571} 8572 8573/* 8574 * Free an indirdep once it no longer has new pointers to track. 8575 */ 8576static void 8577free_indirdep(indirdep) 8578 struct indirdep *indirdep; 8579{ 8580 8581 KASSERT(LIST_EMPTY(&indirdep->ir_jwork), 8582 ("free_indirdep: Journal work not empty.")); 8583 KASSERT(LIST_EMPTY(&indirdep->ir_completehd), 8584 ("free_indirdep: Complete head not empty.")); 8585 KASSERT(LIST_EMPTY(&indirdep->ir_writehd), 8586 ("free_indirdep: write head not empty.")); 8587 KASSERT(LIST_EMPTY(&indirdep->ir_donehd), 8588 ("free_indirdep: done head not empty.")); 8589 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd), 8590 ("free_indirdep: deplist head not empty.")); 8591 KASSERT(indirdep->ir_savebp == NULL, 8592 ("free_indirdep: %p ir_savebp != NULL", indirdep)); 8593 KASSERT((indirdep->ir_state & ONDEPLIST) == 0, 8594 ("free_indirdep: %p still on deplist.", indirdep)); 8595 if (indirdep->ir_state & ONWORKLIST) 8596 WORKLIST_REMOVE(&indirdep->ir_list); 8597 WORKITEM_FREE(indirdep, D_INDIRDEP); 8598} 8599 8600/* 8601 * Called before a write to an indirdep. This routine is responsible for 8602 * rolling back pointers to a safe state which includes only those 8603 * allocindirs which have been completed. 8604 */ 8605static void 8606initiate_write_indirdep(indirdep, bp) 8607 struct indirdep *indirdep; 8608 struct buf *bp; 8609{ 8610 8611 if (indirdep->ir_state & GOINGAWAY) 8612 panic("disk_io_initiation: indirdep gone"); 8613 8614 /* 8615 * If there are no remaining dependencies, this will be writing 8616 * the real pointers. 8617 */ 8618 if (LIST_EMPTY(&indirdep->ir_deplisthd)) 8619 return; 8620 /* 8621 * Replace up-to-date version with safe version. 8622 */ 8623 FREE_LOCK(&lk); 8624 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 8625 M_SOFTDEP_FLAGS); 8626 ACQUIRE_LOCK(&lk); 8627 indirdep->ir_state &= ~ATTACHED; 8628 indirdep->ir_state |= UNDONE; 8629 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 8630 bcopy(indirdep->ir_savebp->b_data, bp->b_data, 8631 bp->b_bcount); 8632} 8633 8634/* 8635 * Called when an inode has been cleared in a cg bitmap. This finally 8636 * eliminates any canceled jaddrefs 8637 */ 8638void 8639softdep_setup_inofree(mp, bp, ino, wkhd) 8640 struct mount *mp; 8641 struct buf *bp; 8642 ino_t ino; 8643 struct workhead *wkhd; 8644{ 8645 struct worklist *wk, *wkn; 8646 struct inodedep *inodedep; 8647 uint8_t *inosused; 8648 struct cg *cgp; 8649 struct fs *fs; 8650 8651 ACQUIRE_LOCK(&lk); 8652 fs = VFSTOUFS(mp)->um_fs; 8653 cgp = (struct cg *)bp->b_data; 8654 inosused = cg_inosused(cgp); 8655 if (isset(inosused, ino % fs->fs_ipg)) 8656 panic("softdep_setup_inofree: inode %d not freed.", ino); 8657 if (inodedep_lookup(mp, ino, 0, &inodedep)) 8658 panic("softdep_setup_inofree: ino %d has existing inodedep %p", 8659 ino, inodedep); 8660 if (wkhd) { 8661 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) { 8662 if (wk->wk_type != D_JADDREF) 8663 continue; 8664 WORKLIST_REMOVE(wk); 8665 /* 8666 * We can free immediately even if the jaddref 8667 * isn't attached in a background write as now 8668 * the bitmaps are reconciled. 8669 */ 8670 wk->wk_state |= COMPLETE | ATTACHED; 8671 free_jaddref(WK_JADDREF(wk)); 8672 } 8673 jwork_move(&bp->b_dep, wkhd); 8674 } 8675 FREE_LOCK(&lk); 8676} 8677 8678 8679/* 8680 * Called via ffs_blkfree() after a set of frags has been cleared from a cg 8681 * map. Any dependencies waiting for the write to clear are added to the 8682 * buf's list and any jnewblks that are being canceled are discarded 8683 * immediately. 8684 */ 8685void 8686softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 8687 struct mount *mp; 8688 struct buf *bp; 8689 ufs2_daddr_t blkno; 8690 int frags; 8691 struct workhead *wkhd; 8692{ 8693 struct jnewblk *jnewblk; 8694 struct worklist *wk, *wkn; 8695#ifdef SUJ_DEBUG 8696 struct bmsafemap *bmsafemap; 8697 struct fs *fs; 8698 uint8_t *blksfree; 8699 struct cg *cgp; 8700 ufs2_daddr_t jstart; 8701 ufs2_daddr_t jend; 8702 ufs2_daddr_t end; 8703 long bno; 8704 int i; 8705#endif 8706 8707 ACQUIRE_LOCK(&lk); 8708 /* 8709 * Detach any jnewblks which have been canceled. They must linger 8710 * until the bitmap is cleared again by ffs_blkfree() to prevent 8711 * an unjournaled allocation from hitting the disk. 8712 */ 8713 if (wkhd) { 8714 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) { 8715 if (wk->wk_type != D_JNEWBLK) 8716 continue; 8717 jnewblk = WK_JNEWBLK(wk); 8718 KASSERT(jnewblk->jn_state & GOINGAWAY, 8719 ("softdep_setup_blkfree: jnewblk not canceled.")); 8720 WORKLIST_REMOVE(wk); 8721#ifdef SUJ_DEBUG 8722 /* 8723 * Assert that this block is free in the bitmap 8724 * before we discard the jnewblk. 8725 */ 8726 fs = VFSTOUFS(mp)->um_fs; 8727 cgp = (struct cg *)bp->b_data; 8728 blksfree = cg_blksfree(cgp); 8729 bno = dtogd(fs, jnewblk->jn_blkno); 8730 for (i = jnewblk->jn_oldfrags; 8731 i < jnewblk->jn_frags; i++) { 8732 if (isset(blksfree, bno + i)) 8733 continue; 8734 panic("softdep_setup_blkfree: not free"); 8735 } 8736#endif 8737 /* 8738 * Even if it's not attached we can free immediately 8739 * as the new bitmap is correct. 8740 */ 8741 wk->wk_state |= COMPLETE | ATTACHED; 8742 free_jnewblk(jnewblk); 8743 } 8744 /* 8745 * The buf must be locked by the caller otherwise these could 8746 * be added while it's being written and the write would 8747 * complete them before they made it to disk. 8748 */ 8749 jwork_move(&bp->b_dep, wkhd); 8750 } 8751 8752#ifdef SUJ_DEBUG 8753 /* 8754 * Assert that we are not freeing a block which has an outstanding 8755 * allocation dependency. 8756 */ 8757 fs = VFSTOUFS(mp)->um_fs; 8758 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno)); 8759 end = blkno + frags; 8760 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 8761 /* 8762 * Don't match against blocks that will be freed when the 8763 * background write is done. 8764 */ 8765 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) == 8766 (COMPLETE | DEPCOMPLETE)) 8767 continue; 8768 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags; 8769 jend = jnewblk->jn_blkno + jnewblk->jn_frags; 8770 if ((blkno >= jstart && blkno < jend) || 8771 (end > jstart && end <= jend)) { 8772 printf("state 0x%X %jd - %d %d dep %p\n", 8773 jnewblk->jn_state, jnewblk->jn_blkno, 8774 jnewblk->jn_oldfrags, jnewblk->jn_frags, 8775 jnewblk->jn_newblk); 8776 panic("softdep_setup_blkfree: " 8777 "%jd-%jd(%d) overlaps with %jd-%jd", 8778 blkno, end, frags, jstart, jend); 8779 } 8780 } 8781#endif 8782 FREE_LOCK(&lk); 8783} 8784 8785static void 8786initiate_write_bmsafemap(bmsafemap, bp) 8787 struct bmsafemap *bmsafemap; 8788 struct buf *bp; /* The cg block. */ 8789{ 8790 struct jaddref *jaddref; 8791 struct jnewblk *jnewblk; 8792 uint8_t *inosused; 8793 uint8_t *blksfree; 8794 struct cg *cgp; 8795 struct fs *fs; 8796 int cleared; 8797 ino_t ino; 8798 long bno; 8799 int i; 8800 8801 if (bmsafemap->sm_state & IOSTARTED) 8802 panic("initiate_write_bmsafemap: Already started\n"); 8803 bmsafemap->sm_state |= IOSTARTED; 8804 /* 8805 * Clear any inode allocations which are pending journal writes. 8806 */ 8807 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) { 8808 cgp = (struct cg *)bp->b_data; 8809 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 8810 inosused = cg_inosused(cgp); 8811 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) { 8812 ino = jaddref->ja_ino % fs->fs_ipg; 8813 /* 8814 * If this is a background copy the inode may not 8815 * be marked used yet. 8816 */ 8817 if (isset(inosused, ino)) { 8818 if ((jaddref->ja_mode & IFMT) == IFDIR) 8819 cgp->cg_cs.cs_ndir--; 8820 cgp->cg_cs.cs_nifree++; 8821 clrbit(inosused, ino); 8822 jaddref->ja_state &= ~ATTACHED; 8823 jaddref->ja_state |= UNDONE; 8824 stat_jaddref++; 8825 } else if ((bp->b_xflags & BX_BKGRDMARKER) == 0) 8826 panic("initiate_write_bmsafemap: inode %d " 8827 "marked free", jaddref->ja_ino); 8828 } 8829 } 8830 /* 8831 * Clear any block allocations which are pending journal writes. 8832 */ 8833 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 8834 cgp = (struct cg *)bp->b_data; 8835 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 8836 blksfree = cg_blksfree(cgp); 8837 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 8838 bno = dtogd(fs, jnewblk->jn_blkno); 8839 cleared = 0; 8840 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; 8841 i++) { 8842 if (isclr(blksfree, bno + i)) { 8843 cleared = 1; 8844 setbit(blksfree, bno + i); 8845 } 8846 } 8847 /* 8848 * We may not clear the block if it's a background 8849 * copy. In that case there is no reason to detach 8850 * it. 8851 */ 8852 if (cleared) { 8853 stat_jnewblk++; 8854 jnewblk->jn_state &= ~ATTACHED; 8855 jnewblk->jn_state |= UNDONE; 8856 } else if ((bp->b_xflags & BX_BKGRDMARKER) == 0) 8857 panic("initiate_write_bmsafemap: block %jd " 8858 "marked free", jnewblk->jn_blkno); 8859 } 8860 } 8861 /* 8862 * Move allocation lists to the written lists so they can be 8863 * cleared once the block write is complete. 8864 */ 8865 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr, 8866 inodedep, id_deps); 8867 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 8868 newblk, nb_deps); 8869} 8870 8871/* 8872 * This routine is called during the completion interrupt 8873 * service routine for a disk write (from the procedure called 8874 * by the device driver to inform the filesystem caches of 8875 * a request completion). It should be called early in this 8876 * procedure, before the block is made available to other 8877 * processes or other routines are called. 8878 * 8879 */ 8880static void 8881softdep_disk_write_complete(bp) 8882 struct buf *bp; /* describes the completed disk write */ 8883{ 8884 struct worklist *wk; 8885 struct worklist *owk; 8886 struct workhead reattach; 8887 struct buf *sbp; 8888 8889 /* 8890 * If an error occurred while doing the write, then the data 8891 * has not hit the disk and the dependencies cannot be unrolled. 8892 */ 8893 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) 8894 return; 8895 LIST_INIT(&reattach); 8896 /* 8897 * This lock must not be released anywhere in this code segment. 8898 */ 8899 sbp = NULL; 8900 owk = NULL; 8901 ACQUIRE_LOCK(&lk); 8902 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) { 8903 WORKLIST_REMOVE(wk); 8904 if (wk == owk) 8905 panic("duplicate worklist: %p\n", wk); 8906 owk = wk; 8907 switch (wk->wk_type) { 8908 8909 case D_PAGEDEP: 8910 if (handle_written_filepage(WK_PAGEDEP(wk), bp)) 8911 WORKLIST_INSERT(&reattach, wk); 8912 continue; 8913 8914 case D_INODEDEP: 8915 if (handle_written_inodeblock(WK_INODEDEP(wk), bp)) 8916 WORKLIST_INSERT(&reattach, wk); 8917 continue; 8918 8919 case D_BMSAFEMAP: 8920 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp)) 8921 WORKLIST_INSERT(&reattach, wk); 8922 continue; 8923 8924 case D_MKDIR: 8925 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 8926 continue; 8927 8928 case D_ALLOCDIRECT: 8929 wk->wk_state |= COMPLETE; 8930 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL); 8931 continue; 8932 8933 case D_ALLOCINDIR: 8934 wk->wk_state |= COMPLETE; 8935 handle_allocindir_partdone(WK_ALLOCINDIR(wk)); 8936 continue; 8937 8938 case D_INDIRDEP: 8939 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp)) 8940 WORKLIST_INSERT(&reattach, wk); 8941 continue; 8942 8943 case D_FREEBLKS: 8944 wk->wk_state |= COMPLETE; 8945 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE) 8946 add_to_worklist(wk, 1); 8947 continue; 8948 8949 case D_FREEWORK: 8950 handle_written_freework(WK_FREEWORK(wk)); 8951 break; 8952 8953 case D_FREEDEP: 8954 free_freedep(WK_FREEDEP(wk)); 8955 continue; 8956 8957 case D_JSEGDEP: 8958 free_jsegdep(WK_JSEGDEP(wk)); 8959 continue; 8960 8961 case D_JSEG: 8962 handle_written_jseg(WK_JSEG(wk), bp); 8963 continue; 8964 8965 case D_SBDEP: 8966 if (handle_written_sbdep(WK_SBDEP(wk), bp)) 8967 WORKLIST_INSERT(&reattach, wk); 8968 continue; 8969 8970 default: 8971 panic("handle_disk_write_complete: Unknown type %s", 8972 TYPENAME(wk->wk_type)); 8973 /* NOTREACHED */ 8974 } 8975 } 8976 /* 8977 * Reattach any requests that must be redone. 8978 */ 8979 while ((wk = LIST_FIRST(&reattach)) != NULL) { 8980 WORKLIST_REMOVE(wk); 8981 WORKLIST_INSERT(&bp->b_dep, wk); 8982 } 8983 FREE_LOCK(&lk); 8984 if (sbp) 8985 brelse(sbp); 8986} 8987 8988/* 8989 * Called from within softdep_disk_write_complete above. Note that 8990 * this routine is always called from interrupt level with further 8991 * splbio interrupts blocked. 8992 */ 8993static void 8994handle_allocdirect_partdone(adp, wkhd) 8995 struct allocdirect *adp; /* the completed allocdirect */ 8996 struct workhead *wkhd; /* Work to do when inode is writtne. */ 8997{ 8998 struct allocdirectlst *listhead; 8999 struct allocdirect *listadp; 9000 struct inodedep *inodedep; 9001 long bsize; 9002 9003 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 9004 return; 9005 /* 9006 * The on-disk inode cannot claim to be any larger than the last 9007 * fragment that has been written. Otherwise, the on-disk inode 9008 * might have fragments that were not the last block in the file 9009 * which would corrupt the filesystem. Thus, we cannot free any 9010 * allocdirects after one whose ad_oldblkno claims a fragment as 9011 * these blocks must be rolled back to zero before writing the inode. 9012 * We check the currently active set of allocdirects in id_inoupdt 9013 * or id_extupdt as appropriate. 9014 */ 9015 inodedep = adp->ad_inodedep; 9016 bsize = inodedep->id_fs->fs_bsize; 9017 if (adp->ad_state & EXTDATA) 9018 listhead = &inodedep->id_extupdt; 9019 else 9020 listhead = &inodedep->id_inoupdt; 9021 TAILQ_FOREACH(listadp, listhead, ad_next) { 9022 /* found our block */ 9023 if (listadp == adp) 9024 break; 9025 /* continue if ad_oldlbn is not a fragment */ 9026 if (listadp->ad_oldsize == 0 || 9027 listadp->ad_oldsize == bsize) 9028 continue; 9029 /* hit a fragment */ 9030 return; 9031 } 9032 /* 9033 * If we have reached the end of the current list without 9034 * finding the just finished dependency, then it must be 9035 * on the future dependency list. Future dependencies cannot 9036 * be freed until they are moved to the current list. 9037 */ 9038 if (listadp == NULL) { 9039#ifdef DEBUG 9040 if (adp->ad_state & EXTDATA) 9041 listhead = &inodedep->id_newextupdt; 9042 else 9043 listhead = &inodedep->id_newinoupdt; 9044 TAILQ_FOREACH(listadp, listhead, ad_next) 9045 /* found our block */ 9046 if (listadp == adp) 9047 break; 9048 if (listadp == NULL) 9049 panic("handle_allocdirect_partdone: lost dep"); 9050#endif /* DEBUG */ 9051 return; 9052 } 9053 /* 9054 * If we have found the just finished dependency, then queue 9055 * it along with anything that follows it that is complete. 9056 * Since the pointer has not yet been written in the inode 9057 * as the dependency prevents it, place the allocdirect on the 9058 * bufwait list where it will be freed once the pointer is 9059 * valid. 9060 */ 9061 if (wkhd == NULL) 9062 wkhd = &inodedep->id_bufwait; 9063 for (; adp; adp = listadp) { 9064 listadp = TAILQ_NEXT(adp, ad_next); 9065 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 9066 return; 9067 TAILQ_REMOVE(listhead, adp, ad_next); 9068 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list); 9069 } 9070} 9071 9072/* 9073 * Called from within softdep_disk_write_complete above. This routine 9074 * completes successfully written allocindirs. 9075 */ 9076static void 9077handle_allocindir_partdone(aip) 9078 struct allocindir *aip; /* the completed allocindir */ 9079{ 9080 struct indirdep *indirdep; 9081 9082 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE) 9083 return; 9084 indirdep = aip->ai_indirdep; 9085 LIST_REMOVE(aip, ai_next); 9086 if (indirdep->ir_state & UNDONE) { 9087 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next); 9088 return; 9089 } 9090 if (indirdep->ir_state & UFS1FMT) 9091 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 9092 aip->ai_newblkno; 9093 else 9094 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 9095 aip->ai_newblkno; 9096 /* 9097 * Await the pointer write before freeing the allocindir. 9098 */ 9099 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next); 9100} 9101 9102/* 9103 * Release segments held on a jwork list. 9104 */ 9105static void 9106handle_jwork(wkhd) 9107 struct workhead *wkhd; 9108{ 9109 struct worklist *wk; 9110 9111 while ((wk = LIST_FIRST(wkhd)) != NULL) { 9112 WORKLIST_REMOVE(wk); 9113 switch (wk->wk_type) { 9114 case D_JSEGDEP: 9115 free_jsegdep(WK_JSEGDEP(wk)); 9116 continue; 9117 default: 9118 panic("handle_jwork: Unknown type %s\n", 9119 TYPENAME(wk->wk_type)); 9120 } 9121 } 9122} 9123 9124/* 9125 * Handle the bufwait list on an inode when it is safe to release items 9126 * held there. This normally happens after an inode block is written but 9127 * may be delayed and handled later if there are pending journal items that 9128 * are not yet safe to be released. 9129 */ 9130static struct freefile * 9131handle_bufwait(inodedep, refhd) 9132 struct inodedep *inodedep; 9133 struct workhead *refhd; 9134{ 9135 struct jaddref *jaddref; 9136 struct freefile *freefile; 9137 struct worklist *wk; 9138 9139 freefile = NULL; 9140 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) { 9141 WORKLIST_REMOVE(wk); 9142 switch (wk->wk_type) { 9143 case D_FREEFILE: 9144 /* 9145 * We defer adding freefile to the worklist 9146 * until all other additions have been made to 9147 * ensure that it will be done after all the 9148 * old blocks have been freed. 9149 */ 9150 if (freefile != NULL) 9151 panic("handle_bufwait: freefile"); 9152 freefile = WK_FREEFILE(wk); 9153 continue; 9154 9155 case D_MKDIR: 9156 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT); 9157 continue; 9158 9159 case D_DIRADD: 9160 diradd_inode_written(WK_DIRADD(wk), inodedep); 9161 continue; 9162 9163 case D_FREEFRAG: 9164 wk->wk_state |= COMPLETE; 9165 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE) 9166 add_to_worklist(wk, 0); 9167 continue; 9168 9169 case D_DIRREM: 9170 wk->wk_state |= COMPLETE; 9171 add_to_worklist(wk, 0); 9172 continue; 9173 9174 case D_ALLOCDIRECT: 9175 case D_ALLOCINDIR: 9176 free_newblk(WK_NEWBLK(wk)); 9177 continue; 9178 9179 case D_JNEWBLK: 9180 wk->wk_state |= COMPLETE; 9181 free_jnewblk(WK_JNEWBLK(wk)); 9182 continue; 9183 9184 /* 9185 * Save freed journal segments and add references on 9186 * the supplied list which will delay their release 9187 * until the cg bitmap is cleared on disk. 9188 */ 9189 case D_JSEGDEP: 9190 if (refhd == NULL) 9191 free_jsegdep(WK_JSEGDEP(wk)); 9192 else 9193 WORKLIST_INSERT(refhd, wk); 9194 continue; 9195 9196 case D_JADDREF: 9197 jaddref = WK_JADDREF(wk); 9198 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 9199 if_deps); 9200 /* 9201 * Transfer any jaddrefs to the list to be freed with 9202 * the bitmap if we're handling a removed file. 9203 */ 9204 if (refhd == NULL) { 9205 wk->wk_state |= COMPLETE; 9206 free_jaddref(jaddref); 9207 } else 9208 WORKLIST_INSERT(refhd, wk); 9209 continue; 9210 9211 default: 9212 panic("handle_bufwait: Unknown type %p(%s)", 9213 wk, TYPENAME(wk->wk_type)); 9214 /* NOTREACHED */ 9215 } 9216 } 9217 return (freefile); 9218} 9219/* 9220 * Called from within softdep_disk_write_complete above to restore 9221 * in-memory inode block contents to their most up-to-date state. Note 9222 * that this routine is always called from interrupt level with further 9223 * splbio interrupts blocked. 9224 */ 9225static int 9226handle_written_inodeblock(inodedep, bp) 9227 struct inodedep *inodedep; 9228 struct buf *bp; /* buffer containing the inode block */ 9229{ 9230 struct freefile *freefile; 9231 struct allocdirect *adp, *nextadp; 9232 struct ufs1_dinode *dp1 = NULL; 9233 struct ufs2_dinode *dp2 = NULL; 9234 struct workhead wkhd; 9235 int hadchanges, fstype; 9236 ino_t freelink; 9237 9238 LIST_INIT(&wkhd); 9239 hadchanges = 0; 9240 freefile = NULL; 9241 if ((inodedep->id_state & IOSTARTED) == 0) 9242 panic("handle_written_inodeblock: not started"); 9243 inodedep->id_state &= ~IOSTARTED; 9244 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) { 9245 fstype = UFS1; 9246 dp1 = (struct ufs1_dinode *)bp->b_data + 9247 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 9248 freelink = dp1->di_freelink; 9249 } else { 9250 fstype = UFS2; 9251 dp2 = (struct ufs2_dinode *)bp->b_data + 9252 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 9253 freelink = dp2->di_freelink; 9254 } 9255 /* 9256 * If we wrote a valid freelink pointer during the last write 9257 * record it here. 9258 */ 9259 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 9260 struct inodedep *inon; 9261 9262 inon = TAILQ_NEXT(inodedep, id_unlinked); 9263 if ((inon == NULL && freelink == 0) || 9264 (inon && inon->id_ino == freelink)) { 9265 if (inon) 9266 inon->id_state |= UNLINKPREV; 9267 inodedep->id_state |= UNLINKNEXT; 9268 } else 9269 hadchanges = 1; 9270 } 9271 /* Leave this inodeblock dirty until it's in the list. */ 9272 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED) 9273 hadchanges = 1; 9274 /* 9275 * If we had to rollback the inode allocation because of 9276 * bitmaps being incomplete, then simply restore it. 9277 * Keep the block dirty so that it will not be reclaimed until 9278 * all associated dependencies have been cleared and the 9279 * corresponding updates written to disk. 9280 */ 9281 if (inodedep->id_savedino1 != NULL) { 9282 hadchanges = 1; 9283 if (fstype == UFS1) 9284 *dp1 = *inodedep->id_savedino1; 9285 else 9286 *dp2 = *inodedep->id_savedino2; 9287 free(inodedep->id_savedino1, M_SAVEDINO); 9288 inodedep->id_savedino1 = NULL; 9289 if ((bp->b_flags & B_DELWRI) == 0) 9290 stat_inode_bitmap++; 9291 bdirty(bp); 9292 /* 9293 * If the inode is clear here and GOINGAWAY it will never 9294 * be written. Process the bufwait and clear any pending 9295 * work which may include the freefile. 9296 */ 9297 if (inodedep->id_state & GOINGAWAY) 9298 goto bufwait; 9299 return (1); 9300 } 9301 inodedep->id_state |= COMPLETE; 9302 /* 9303 * Roll forward anything that had to be rolled back before 9304 * the inode could be updated. 9305 */ 9306 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) { 9307 nextadp = TAILQ_NEXT(adp, ad_next); 9308 if (adp->ad_state & ATTACHED) 9309 panic("handle_written_inodeblock: new entry"); 9310 if (fstype == UFS1) { 9311 if (adp->ad_offset < NDADDR) { 9312 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno) 9313 panic("%s %s #%jd mismatch %d != %jd", 9314 "handle_written_inodeblock:", 9315 "direct pointer", 9316 (intmax_t)adp->ad_offset, 9317 dp1->di_db[adp->ad_offset], 9318 (intmax_t)adp->ad_oldblkno); 9319 dp1->di_db[adp->ad_offset] = adp->ad_newblkno; 9320 } else { 9321 if (dp1->di_ib[adp->ad_offset - NDADDR] != 0) 9322 panic("%s: %s #%jd allocated as %d", 9323 "handle_written_inodeblock", 9324 "indirect pointer", 9325 (intmax_t)adp->ad_offset - NDADDR, 9326 dp1->di_ib[adp->ad_offset - NDADDR]); 9327 dp1->di_ib[adp->ad_offset - NDADDR] = 9328 adp->ad_newblkno; 9329 } 9330 } else { 9331 if (adp->ad_offset < NDADDR) { 9332 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno) 9333 panic("%s: %s #%jd %s %jd != %jd", 9334 "handle_written_inodeblock", 9335 "direct pointer", 9336 (intmax_t)adp->ad_offset, "mismatch", 9337 (intmax_t)dp2->di_db[adp->ad_offset], 9338 (intmax_t)adp->ad_oldblkno); 9339 dp2->di_db[adp->ad_offset] = adp->ad_newblkno; 9340 } else { 9341 if (dp2->di_ib[adp->ad_offset - NDADDR] != 0) 9342 panic("%s: %s #%jd allocated as %jd", 9343 "handle_written_inodeblock", 9344 "indirect pointer", 9345 (intmax_t)adp->ad_offset - NDADDR, 9346 (intmax_t) 9347 dp2->di_ib[adp->ad_offset - NDADDR]); 9348 dp2->di_ib[adp->ad_offset - NDADDR] = 9349 adp->ad_newblkno; 9350 } 9351 } 9352 adp->ad_state &= ~UNDONE; 9353 adp->ad_state |= ATTACHED; 9354 hadchanges = 1; 9355 } 9356 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) { 9357 nextadp = TAILQ_NEXT(adp, ad_next); 9358 if (adp->ad_state & ATTACHED) 9359 panic("handle_written_inodeblock: new entry"); 9360 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno) 9361 panic("%s: direct pointers #%jd %s %jd != %jd", 9362 "handle_written_inodeblock", 9363 (intmax_t)adp->ad_offset, "mismatch", 9364 (intmax_t)dp2->di_extb[adp->ad_offset], 9365 (intmax_t)adp->ad_oldblkno); 9366 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno; 9367 adp->ad_state &= ~UNDONE; 9368 adp->ad_state |= ATTACHED; 9369 hadchanges = 1; 9370 } 9371 if (hadchanges && (bp->b_flags & B_DELWRI) == 0) 9372 stat_direct_blk_ptrs++; 9373 /* 9374 * Reset the file size to its most up-to-date value. 9375 */ 9376 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1) 9377 panic("handle_written_inodeblock: bad size"); 9378 if (inodedep->id_savednlink > LINK_MAX) 9379 panic("handle_written_inodeblock: Invalid link count " 9380 "%d for inodedep %p", inodedep->id_savednlink, inodedep); 9381 if (fstype == UFS1) { 9382 if (dp1->di_nlink != inodedep->id_savednlink) { 9383 dp1->di_nlink = inodedep->id_savednlink; 9384 hadchanges = 1; 9385 } 9386 if (dp1->di_size != inodedep->id_savedsize) { 9387 dp1->di_size = inodedep->id_savedsize; 9388 hadchanges = 1; 9389 } 9390 } else { 9391 if (dp2->di_nlink != inodedep->id_savednlink) { 9392 dp2->di_nlink = inodedep->id_savednlink; 9393 hadchanges = 1; 9394 } 9395 if (dp2->di_size != inodedep->id_savedsize) { 9396 dp2->di_size = inodedep->id_savedsize; 9397 hadchanges = 1; 9398 } 9399 if (dp2->di_extsize != inodedep->id_savedextsize) { 9400 dp2->di_extsize = inodedep->id_savedextsize; 9401 hadchanges = 1; 9402 } 9403 } 9404 inodedep->id_savedsize = -1; 9405 inodedep->id_savedextsize = -1; 9406 inodedep->id_savednlink = -1; 9407 /* 9408 * If there were any rollbacks in the inode block, then it must be 9409 * marked dirty so that its will eventually get written back in 9410 * its correct form. 9411 */ 9412 if (hadchanges) 9413 bdirty(bp); 9414bufwait: 9415 /* 9416 * Process any allocdirects that completed during the update. 9417 */ 9418 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 9419 handle_allocdirect_partdone(adp, &wkhd); 9420 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 9421 handle_allocdirect_partdone(adp, &wkhd); 9422 /* 9423 * Process deallocations that were held pending until the 9424 * inode had been written to disk. Freeing of the inode 9425 * is delayed until after all blocks have been freed to 9426 * avoid creation of new <vfsid, inum, lbn> triples 9427 * before the old ones have been deleted. Completely 9428 * unlinked inodes are not processed until the unlinked 9429 * inode list is written or the last reference is removed. 9430 */ 9431 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) { 9432 freefile = handle_bufwait(inodedep, NULL); 9433 if (freefile && !LIST_EMPTY(&wkhd)) { 9434 WORKLIST_INSERT(&wkhd, &freefile->fx_list); 9435 freefile = NULL; 9436 } 9437 } 9438 /* 9439 * Move rolled forward dependency completions to the bufwait list 9440 * now that those that were already written have been processed. 9441 */ 9442 if (!LIST_EMPTY(&wkhd) && hadchanges == 0) 9443 panic("handle_written_inodeblock: bufwait but no changes"); 9444 jwork_move(&inodedep->id_bufwait, &wkhd); 9445 9446 if (freefile != NULL) { 9447 /* 9448 * If the inode is goingaway it was never written. Fake up 9449 * the state here so free_inodedep() can succeed. 9450 */ 9451 if (inodedep->id_state & GOINGAWAY) 9452 inodedep->id_state |= COMPLETE | DEPCOMPLETE; 9453 if (free_inodedep(inodedep) == 0) 9454 panic("handle_written_inodeblock: live inodedep %p", 9455 inodedep); 9456 add_to_worklist(&freefile->fx_list, 0); 9457 return (0); 9458 } 9459 9460 /* 9461 * If no outstanding dependencies, free it. 9462 */ 9463 if (free_inodedep(inodedep) || 9464 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 && 9465 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 && 9466 TAILQ_FIRST(&inodedep->id_extupdt) == 0 && 9467 LIST_FIRST(&inodedep->id_bufwait) == 0)) 9468 return (0); 9469 return (hadchanges); 9470} 9471 9472static int 9473handle_written_indirdep(indirdep, bp, bpp) 9474 struct indirdep *indirdep; 9475 struct buf *bp; 9476 struct buf **bpp; 9477{ 9478 struct allocindir *aip; 9479 int chgs; 9480 9481 if (indirdep->ir_state & GOINGAWAY) 9482 panic("disk_write_complete: indirdep gone"); 9483 chgs = 0; 9484 /* 9485 * If there were rollbacks revert them here. 9486 */ 9487 if (indirdep->ir_saveddata) { 9488 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount); 9489 free(indirdep->ir_saveddata, M_INDIRDEP); 9490 indirdep->ir_saveddata = 0; 9491 chgs = 1; 9492 } 9493 indirdep->ir_state &= ~UNDONE; 9494 indirdep->ir_state |= ATTACHED; 9495 /* 9496 * Move allocindirs with written pointers to the completehd if 9497 * the indirdep's pointer is not yet written. Otherwise 9498 * free them here. 9499 */ 9500 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != 0) { 9501 LIST_REMOVE(aip, ai_next); 9502 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 9503 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip, 9504 ai_next); 9505 continue; 9506 } 9507 free_newblk(&aip->ai_block); 9508 } 9509 /* 9510 * Move allocindirs that have finished dependency processing from 9511 * the done list to the write list after updating the pointers. 9512 */ 9513 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0) { 9514 handle_allocindir_partdone(aip); 9515 if (aip == LIST_FIRST(&indirdep->ir_donehd)) 9516 panic("disk_write_complete: not gone"); 9517 chgs = 1; 9518 } 9519 /* 9520 * If this indirdep has been detached from its newblk during 9521 * I/O we need to keep this dep attached to the buffer so 9522 * deallocate_dependencies can find it and properly resolve 9523 * any outstanding dependencies. 9524 */ 9525 if ((indirdep->ir_state & (ONDEPLIST | DEPCOMPLETE)) == 0) 9526 chgs = 1; 9527 if ((bp->b_flags & B_DELWRI) == 0) 9528 stat_indir_blk_ptrs++; 9529 /* 9530 * If there were no changes we can discard the savedbp and detach 9531 * ourselves from the buf. We are only carrying completed pointers 9532 * in this case. 9533 */ 9534 if (chgs == 0) { 9535 struct buf *sbp; 9536 9537 sbp = indirdep->ir_savebp; 9538 sbp->b_flags |= B_INVAL | B_NOCACHE; 9539 indirdep->ir_savebp = NULL; 9540 if (*bpp != NULL) 9541 panic("handle_written_indirdep: bp already exists."); 9542 *bpp = sbp; 9543 } else 9544 bdirty(bp); 9545 /* 9546 * If there are no fresh dependencies and none waiting on writes 9547 * we can free the indirdep. 9548 */ 9549 if ((indirdep->ir_state & DEPCOMPLETE) && chgs == 0) { 9550 if (indirdep->ir_state & ONDEPLIST) 9551 LIST_REMOVE(indirdep, ir_next); 9552 free_indirdep(indirdep); 9553 return (0); 9554 } 9555 9556 return (chgs); 9557} 9558 9559/* 9560 * Process a diradd entry after its dependent inode has been written. 9561 * This routine must be called with splbio interrupts blocked. 9562 */ 9563static void 9564diradd_inode_written(dap, inodedep) 9565 struct diradd *dap; 9566 struct inodedep *inodedep; 9567{ 9568 9569 dap->da_state |= COMPLETE; 9570 complete_diradd(dap); 9571 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 9572} 9573 9574/* 9575 * Returns true if the bmsafemap will have rollbacks when written. Must 9576 * only be called with lk and the buf lock on the cg held. 9577 */ 9578static int 9579bmsafemap_rollbacks(bmsafemap) 9580 struct bmsafemap *bmsafemap; 9581{ 9582 9583 return (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd) | 9584 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd)); 9585} 9586 9587/* 9588 * Complete a write to a bmsafemap structure. Roll forward any bitmap 9589 * changes if it's not a background write. Set all written dependencies 9590 * to DEPCOMPLETE and free the structure if possible. 9591 */ 9592static int 9593handle_written_bmsafemap(bmsafemap, bp) 9594 struct bmsafemap *bmsafemap; 9595 struct buf *bp; 9596{ 9597 struct newblk *newblk; 9598 struct inodedep *inodedep; 9599 struct jaddref *jaddref, *jatmp; 9600 struct jnewblk *jnewblk, *jntmp; 9601 uint8_t *inosused; 9602 uint8_t *blksfree; 9603 struct cg *cgp; 9604 struct fs *fs; 9605 ino_t ino; 9606 long bno; 9607 int chgs; 9608 int i; 9609 9610 if ((bmsafemap->sm_state & IOSTARTED) == 0) 9611 panic("initiate_write_bmsafemap: Not started\n"); 9612 chgs = 0; 9613 bmsafemap->sm_state &= ~IOSTARTED; 9614 /* 9615 * Restore unwritten inode allocation pending jaddref writes. 9616 */ 9617 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) { 9618 cgp = (struct cg *)bp->b_data; 9619 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 9620 inosused = cg_inosused(cgp); 9621 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd, 9622 ja_bmdeps, jatmp) { 9623 if ((jaddref->ja_state & UNDONE) == 0) 9624 continue; 9625 ino = jaddref->ja_ino % fs->fs_ipg; 9626 if (isset(inosused, ino)) 9627 panic("handle_written_bmsafemap: " 9628 "re-allocated inode"); 9629 if ((bp->b_xflags & BX_BKGRDMARKER) == 0) { 9630 if ((jaddref->ja_mode & IFMT) == IFDIR) 9631 cgp->cg_cs.cs_ndir++; 9632 cgp->cg_cs.cs_nifree--; 9633 setbit(inosused, ino); 9634 chgs = 1; 9635 } 9636 jaddref->ja_state &= ~UNDONE; 9637 jaddref->ja_state |= ATTACHED; 9638 free_jaddref(jaddref); 9639 } 9640 } 9641 /* 9642 * Restore any block allocations which are pending journal writes. 9643 */ 9644 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 9645 cgp = (struct cg *)bp->b_data; 9646 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 9647 blksfree = cg_blksfree(cgp); 9648 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps, 9649 jntmp) { 9650 if ((jnewblk->jn_state & UNDONE) == 0) 9651 continue; 9652 bno = dtogd(fs, jnewblk->jn_blkno); 9653 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; 9654 i++) { 9655 if (bp->b_xflags & BX_BKGRDMARKER) 9656 break; 9657 if ((jnewblk->jn_state & NEWBLOCK) == 0 && 9658 isclr(blksfree, bno + i)) 9659 panic("handle_written_bmsafemap: " 9660 "re-allocated fragment"); 9661 clrbit(blksfree, bno + i); 9662 chgs = 1; 9663 } 9664 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK); 9665 jnewblk->jn_state |= ATTACHED; 9666 free_jnewblk(jnewblk); 9667 } 9668 } 9669 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) { 9670 newblk->nb_state |= DEPCOMPLETE; 9671 newblk->nb_state &= ~ONDEPLIST; 9672 newblk->nb_bmsafemap = NULL; 9673 LIST_REMOVE(newblk, nb_deps); 9674 if (newblk->nb_list.wk_type == D_ALLOCDIRECT) 9675 handle_allocdirect_partdone( 9676 WK_ALLOCDIRECT(&newblk->nb_list), NULL); 9677 else if (newblk->nb_list.wk_type == D_ALLOCINDIR) 9678 handle_allocindir_partdone( 9679 WK_ALLOCINDIR(&newblk->nb_list)); 9680 else if (newblk->nb_list.wk_type != D_NEWBLK) 9681 panic("handle_written_bmsafemap: Unexpected type: %s", 9682 TYPENAME(newblk->nb_list.wk_type)); 9683 } 9684 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) { 9685 inodedep->id_state |= DEPCOMPLETE; 9686 inodedep->id_state &= ~ONDEPLIST; 9687 LIST_REMOVE(inodedep, id_deps); 9688 inodedep->id_bmsafemap = NULL; 9689 } 9690 if (LIST_EMPTY(&bmsafemap->sm_jaddrefhd) && 9691 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) && 9692 LIST_EMPTY(&bmsafemap->sm_newblkhd) && 9693 LIST_EMPTY(&bmsafemap->sm_inodedephd)) { 9694 if (chgs) 9695 bdirty(bp); 9696 LIST_REMOVE(bmsafemap, sm_hash); 9697 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 9698 return (0); 9699 } 9700 bdirty(bp); 9701 return (1); 9702} 9703 9704/* 9705 * Try to free a mkdir dependency. 9706 */ 9707static void 9708complete_mkdir(mkdir) 9709 struct mkdir *mkdir; 9710{ 9711 struct diradd *dap; 9712 9713 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE) 9714 return; 9715 LIST_REMOVE(mkdir, md_mkdirs); 9716 dap = mkdir->md_diradd; 9717 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 9718 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) { 9719 dap->da_state |= DEPCOMPLETE; 9720 complete_diradd(dap); 9721 } 9722 WORKITEM_FREE(mkdir, D_MKDIR); 9723} 9724 9725/* 9726 * Handle the completion of a mkdir dependency. 9727 */ 9728static void 9729handle_written_mkdir(mkdir, type) 9730 struct mkdir *mkdir; 9731 int type; 9732{ 9733 9734 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type) 9735 panic("handle_written_mkdir: bad type"); 9736 mkdir->md_state |= COMPLETE; 9737 complete_mkdir(mkdir); 9738} 9739 9740static void 9741free_pagedep(pagedep) 9742 struct pagedep *pagedep; 9743{ 9744 int i; 9745 9746 if (pagedep->pd_state & (NEWBLOCK | ONWORKLIST)) 9747 return; 9748 for (i = 0; i < DAHASHSZ; i++) 9749 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i])) 9750 return; 9751 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd)) 9752 return; 9753 if (!LIST_EMPTY(&pagedep->pd_dirremhd)) 9754 return; 9755 if (!LIST_EMPTY(&pagedep->pd_pendinghd)) 9756 return; 9757 LIST_REMOVE(pagedep, pd_hash); 9758 WORKITEM_FREE(pagedep, D_PAGEDEP); 9759} 9760 9761/* 9762 * Called from within softdep_disk_write_complete above. 9763 * A write operation was just completed. Removed inodes can 9764 * now be freed and associated block pointers may be committed. 9765 * Note that this routine is always called from interrupt level 9766 * with further splbio interrupts blocked. 9767 */ 9768static int 9769handle_written_filepage(pagedep, bp) 9770 struct pagedep *pagedep; 9771 struct buf *bp; /* buffer containing the written page */ 9772{ 9773 struct dirrem *dirrem; 9774 struct diradd *dap, *nextdap; 9775 struct direct *ep; 9776 int i, chgs; 9777 9778 if ((pagedep->pd_state & IOSTARTED) == 0) 9779 panic("handle_written_filepage: not started"); 9780 pagedep->pd_state &= ~IOSTARTED; 9781 /* 9782 * Process any directory removals that have been committed. 9783 */ 9784 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) { 9785 LIST_REMOVE(dirrem, dm_next); 9786 dirrem->dm_state |= COMPLETE; 9787 dirrem->dm_dirinum = pagedep->pd_ino; 9788 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 9789 ("handle_written_filepage: Journal entries not written.")); 9790 add_to_worklist(&dirrem->dm_list, 0); 9791 } 9792 /* 9793 * Free any directory additions that have been committed. 9794 * If it is a newly allocated block, we have to wait until 9795 * the on-disk directory inode claims the new block. 9796 */ 9797 if ((pagedep->pd_state & NEWBLOCK) == 0) 9798 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 9799 free_diradd(dap, NULL); 9800 /* 9801 * Uncommitted directory entries must be restored. 9802 */ 9803 for (chgs = 0, i = 0; i < DAHASHSZ; i++) { 9804 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap; 9805 dap = nextdap) { 9806 nextdap = LIST_NEXT(dap, da_pdlist); 9807 if (dap->da_state & ATTACHED) 9808 panic("handle_written_filepage: attached"); 9809 ep = (struct direct *) 9810 ((char *)bp->b_data + dap->da_offset); 9811 ep->d_ino = dap->da_newinum; 9812 dap->da_state &= ~UNDONE; 9813 dap->da_state |= ATTACHED; 9814 chgs = 1; 9815 /* 9816 * If the inode referenced by the directory has 9817 * been written out, then the dependency can be 9818 * moved to the pending list. 9819 */ 9820 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 9821 LIST_REMOVE(dap, da_pdlist); 9822 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, 9823 da_pdlist); 9824 } 9825 } 9826 } 9827 /* 9828 * If there were any rollbacks in the directory, then it must be 9829 * marked dirty so that its will eventually get written back in 9830 * its correct form. 9831 */ 9832 if (chgs) { 9833 if ((bp->b_flags & B_DELWRI) == 0) 9834 stat_dir_entry++; 9835 bdirty(bp); 9836 return (1); 9837 } 9838 /* 9839 * If we are not waiting for a new directory block to be 9840 * claimed by its inode, then the pagedep will be freed. 9841 * Otherwise it will remain to track any new entries on 9842 * the page in case they are fsync'ed. 9843 */ 9844 if ((pagedep->pd_state & NEWBLOCK) == 0 && 9845 LIST_EMPTY(&pagedep->pd_jmvrefhd)) { 9846 LIST_REMOVE(pagedep, pd_hash); 9847 WORKITEM_FREE(pagedep, D_PAGEDEP); 9848 } 9849 return (0); 9850} 9851 9852/* 9853 * Writing back in-core inode structures. 9854 * 9855 * The filesystem only accesses an inode's contents when it occupies an 9856 * "in-core" inode structure. These "in-core" structures are separate from 9857 * the page frames used to cache inode blocks. Only the latter are 9858 * transferred to/from the disk. So, when the updated contents of the 9859 * "in-core" inode structure are copied to the corresponding in-memory inode 9860 * block, the dependencies are also transferred. The following procedure is 9861 * called when copying a dirty "in-core" inode to a cached inode block. 9862 */ 9863 9864/* 9865 * Called when an inode is loaded from disk. If the effective link count 9866 * differed from the actual link count when it was last flushed, then we 9867 * need to ensure that the correct effective link count is put back. 9868 */ 9869void 9870softdep_load_inodeblock(ip) 9871 struct inode *ip; /* the "in_core" copy of the inode */ 9872{ 9873 struct inodedep *inodedep; 9874 9875 /* 9876 * Check for alternate nlink count. 9877 */ 9878 ip->i_effnlink = ip->i_nlink; 9879 ACQUIRE_LOCK(&lk); 9880 if (inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0, 9881 &inodedep) == 0) { 9882 FREE_LOCK(&lk); 9883 return; 9884 } 9885 ip->i_effnlink -= inodedep->id_nlinkdelta; 9886 FREE_LOCK(&lk); 9887} 9888 9889/* 9890 * This routine is called just before the "in-core" inode 9891 * information is to be copied to the in-memory inode block. 9892 * Recall that an inode block contains several inodes. If 9893 * the force flag is set, then the dependencies will be 9894 * cleared so that the update can always be made. Note that 9895 * the buffer is locked when this routine is called, so we 9896 * will never be in the middle of writing the inode block 9897 * to disk. 9898 */ 9899void 9900softdep_update_inodeblock(ip, bp, waitfor) 9901 struct inode *ip; /* the "in_core" copy of the inode */ 9902 struct buf *bp; /* the buffer containing the inode block */ 9903 int waitfor; /* nonzero => update must be allowed */ 9904{ 9905 struct inodedep *inodedep; 9906 struct inoref *inoref; 9907 struct worklist *wk; 9908 struct mount *mp; 9909 struct buf *ibp; 9910 struct fs *fs; 9911 int error; 9912 9913 mp = UFSTOVFS(ip->i_ump); 9914 fs = ip->i_fs; 9915 /* 9916 * Preserve the freelink that is on disk. clear_unlinked_inodedep() 9917 * does not have access to the in-core ip so must write directly into 9918 * the inode block buffer when setting freelink. 9919 */ 9920 if (fs->fs_magic == FS_UFS1_MAGIC) 9921 DIP_SET(ip, i_freelink, ((struct ufs1_dinode *)bp->b_data + 9922 ino_to_fsbo(fs, ip->i_number))->di_freelink); 9923 else 9924 DIP_SET(ip, i_freelink, ((struct ufs2_dinode *)bp->b_data + 9925 ino_to_fsbo(fs, ip->i_number))->di_freelink); 9926 /* 9927 * If the effective link count is not equal to the actual link 9928 * count, then we must track the difference in an inodedep while 9929 * the inode is (potentially) tossed out of the cache. Otherwise, 9930 * if there is no existing inodedep, then there are no dependencies 9931 * to track. 9932 */ 9933 ACQUIRE_LOCK(&lk); 9934again: 9935 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 9936 FREE_LOCK(&lk); 9937 if (ip->i_effnlink != ip->i_nlink) 9938 panic("softdep_update_inodeblock: bad link count"); 9939 return; 9940 } 9941 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink) 9942 panic("softdep_update_inodeblock: bad delta"); 9943 /* 9944 * If we're flushing all dependencies we must also move any waiting 9945 * for journal writes onto the bufwait list prior to I/O. 9946 */ 9947 if (waitfor) { 9948 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 9949 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 9950 == DEPCOMPLETE) { 9951 stat_jwait_inode++; 9952 jwait(&inoref->if_list); 9953 goto again; 9954 } 9955 } 9956 } 9957 /* 9958 * Changes have been initiated. Anything depending on these 9959 * changes cannot occur until this inode has been written. 9960 */ 9961 inodedep->id_state &= ~COMPLETE; 9962 if ((inodedep->id_state & ONWORKLIST) == 0) 9963 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list); 9964 /* 9965 * Any new dependencies associated with the incore inode must 9966 * now be moved to the list associated with the buffer holding 9967 * the in-memory copy of the inode. Once merged process any 9968 * allocdirects that are completed by the merger. 9969 */ 9970 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt); 9971 if (!TAILQ_EMPTY(&inodedep->id_inoupdt)) 9972 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt), 9973 NULL); 9974 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt); 9975 if (!TAILQ_EMPTY(&inodedep->id_extupdt)) 9976 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt), 9977 NULL); 9978 /* 9979 * Now that the inode has been pushed into the buffer, the 9980 * operations dependent on the inode being written to disk 9981 * can be moved to the id_bufwait so that they will be 9982 * processed when the buffer I/O completes. 9983 */ 9984 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) { 9985 WORKLIST_REMOVE(wk); 9986 WORKLIST_INSERT(&inodedep->id_bufwait, wk); 9987 } 9988 /* 9989 * Newly allocated inodes cannot be written until the bitmap 9990 * that allocates them have been written (indicated by 9991 * DEPCOMPLETE being set in id_state). If we are doing a 9992 * forced sync (e.g., an fsync on a file), we force the bitmap 9993 * to be written so that the update can be done. 9994 */ 9995 if (waitfor == 0) { 9996 FREE_LOCK(&lk); 9997 return; 9998 } 9999retry: 10000 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) { 10001 FREE_LOCK(&lk); 10002 return; 10003 } 10004 ibp = inodedep->id_bmsafemap->sm_buf; 10005 ibp = getdirtybuf(ibp, &lk, MNT_WAIT); 10006 if (ibp == NULL) { 10007 /* 10008 * If ibp came back as NULL, the dependency could have been 10009 * freed while we slept. Look it up again, and check to see 10010 * that it has completed. 10011 */ 10012 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 10013 goto retry; 10014 FREE_LOCK(&lk); 10015 return; 10016 } 10017 FREE_LOCK(&lk); 10018 if ((error = bwrite(ibp)) != 0) 10019 softdep_error("softdep_update_inodeblock: bwrite", error); 10020} 10021 10022/* 10023 * Merge the a new inode dependency list (such as id_newinoupdt) into an 10024 * old inode dependency list (such as id_inoupdt). This routine must be 10025 * called with splbio interrupts blocked. 10026 */ 10027static void 10028merge_inode_lists(newlisthead, oldlisthead) 10029 struct allocdirectlst *newlisthead; 10030 struct allocdirectlst *oldlisthead; 10031{ 10032 struct allocdirect *listadp, *newadp; 10033 10034 newadp = TAILQ_FIRST(newlisthead); 10035 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) { 10036 if (listadp->ad_offset < newadp->ad_offset) { 10037 listadp = TAILQ_NEXT(listadp, ad_next); 10038 continue; 10039 } 10040 TAILQ_REMOVE(newlisthead, newadp, ad_next); 10041 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next); 10042 if (listadp->ad_offset == newadp->ad_offset) { 10043 allocdirect_merge(oldlisthead, newadp, 10044 listadp); 10045 listadp = newadp; 10046 } 10047 newadp = TAILQ_FIRST(newlisthead); 10048 } 10049 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) { 10050 TAILQ_REMOVE(newlisthead, newadp, ad_next); 10051 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next); 10052 } 10053} 10054 10055/* 10056 * If we are doing an fsync, then we must ensure that any directory 10057 * entries for the inode have been written after the inode gets to disk. 10058 */ 10059int 10060softdep_fsync(vp) 10061 struct vnode *vp; /* the "in_core" copy of the inode */ 10062{ 10063 struct inodedep *inodedep; 10064 struct pagedep *pagedep; 10065 struct inoref *inoref; 10066 struct worklist *wk; 10067 struct diradd *dap; 10068 struct mount *mp; 10069 struct vnode *pvp; 10070 struct inode *ip; 10071 struct buf *bp; 10072 struct fs *fs; 10073 struct thread *td = curthread; 10074 int error, flushparent, pagedep_new_block; 10075 ino_t parentino; 10076 ufs_lbn_t lbn; 10077 10078 ip = VTOI(vp); 10079 fs = ip->i_fs; 10080 mp = vp->v_mount; 10081 ACQUIRE_LOCK(&lk); 10082restart: 10083 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 10084 FREE_LOCK(&lk); 10085 return (0); 10086 } 10087 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 10088 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 10089 == DEPCOMPLETE) { 10090 stat_jwait_inode++; 10091 jwait(&inoref->if_list); 10092 goto restart; 10093 } 10094 } 10095 if (!LIST_EMPTY(&inodedep->id_inowait) || 10096 !TAILQ_EMPTY(&inodedep->id_extupdt) || 10097 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 10098 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 10099 !TAILQ_EMPTY(&inodedep->id_newinoupdt)) 10100 panic("softdep_fsync: pending ops %p", inodedep); 10101 for (error = 0, flushparent = 0; ; ) { 10102 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL) 10103 break; 10104 if (wk->wk_type != D_DIRADD) 10105 panic("softdep_fsync: Unexpected type %s", 10106 TYPENAME(wk->wk_type)); 10107 dap = WK_DIRADD(wk); 10108 /* 10109 * Flush our parent if this directory entry has a MKDIR_PARENT 10110 * dependency or is contained in a newly allocated block. 10111 */ 10112 if (dap->da_state & DIRCHG) 10113 pagedep = dap->da_previous->dm_pagedep; 10114 else 10115 pagedep = dap->da_pagedep; 10116 parentino = pagedep->pd_ino; 10117 lbn = pagedep->pd_lbn; 10118 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) 10119 panic("softdep_fsync: dirty"); 10120 if ((dap->da_state & MKDIR_PARENT) || 10121 (pagedep->pd_state & NEWBLOCK)) 10122 flushparent = 1; 10123 else 10124 flushparent = 0; 10125 /* 10126 * If we are being fsync'ed as part of vgone'ing this vnode, 10127 * then we will not be able to release and recover the 10128 * vnode below, so we just have to give up on writing its 10129 * directory entry out. It will eventually be written, just 10130 * not now, but then the user was not asking to have it 10131 * written, so we are not breaking any promises. 10132 */ 10133 if (vp->v_iflag & VI_DOOMED) 10134 break; 10135 /* 10136 * We prevent deadlock by always fetching inodes from the 10137 * root, moving down the directory tree. Thus, when fetching 10138 * our parent directory, we first try to get the lock. If 10139 * that fails, we must unlock ourselves before requesting 10140 * the lock on our parent. See the comment in ufs_lookup 10141 * for details on possible races. 10142 */ 10143 FREE_LOCK(&lk); 10144 if (ffs_vgetf(mp, parentino, LK_NOWAIT | LK_EXCLUSIVE, &pvp, 10145 FFSV_FORCEINSMQ)) { 10146 error = vfs_busy(mp, MBF_NOWAIT); 10147 if (error != 0) { 10148 vfs_ref(mp); 10149 VOP_UNLOCK(vp, 0); 10150 error = vfs_busy(mp, 0); 10151 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 10152 vfs_rel(mp); 10153 if (error != 0) 10154 return (ENOENT); 10155 if (vp->v_iflag & VI_DOOMED) { 10156 vfs_unbusy(mp); 10157 return (ENOENT); 10158 } 10159 } 10160 VOP_UNLOCK(vp, 0); 10161 error = ffs_vgetf(mp, parentino, LK_EXCLUSIVE, 10162 &pvp, FFSV_FORCEINSMQ); 10163 vfs_unbusy(mp); 10164 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 10165 if (vp->v_iflag & VI_DOOMED) { 10166 if (error == 0) 10167 vput(pvp); 10168 error = ENOENT; 10169 } 10170 if (error != 0) 10171 return (error); 10172 } 10173 /* 10174 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps 10175 * that are contained in direct blocks will be resolved by 10176 * doing a ffs_update. Pagedeps contained in indirect blocks 10177 * may require a complete sync'ing of the directory. So, we 10178 * try the cheap and fast ffs_update first, and if that fails, 10179 * then we do the slower ffs_syncvnode of the directory. 10180 */ 10181 if (flushparent) { 10182 int locked; 10183 10184 if ((error = ffs_update(pvp, 1)) != 0) { 10185 vput(pvp); 10186 return (error); 10187 } 10188 ACQUIRE_LOCK(&lk); 10189 locked = 1; 10190 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) { 10191 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) { 10192 if (wk->wk_type != D_DIRADD) 10193 panic("softdep_fsync: Unexpected type %s", 10194 TYPENAME(wk->wk_type)); 10195 dap = WK_DIRADD(wk); 10196 if (dap->da_state & DIRCHG) 10197 pagedep = dap->da_previous->dm_pagedep; 10198 else 10199 pagedep = dap->da_pagedep; 10200 pagedep_new_block = pagedep->pd_state & NEWBLOCK; 10201 FREE_LOCK(&lk); 10202 locked = 0; 10203 if (pagedep_new_block && 10204 (error = ffs_syncvnode(pvp, MNT_WAIT))) { 10205 vput(pvp); 10206 return (error); 10207 } 10208 } 10209 } 10210 if (locked) 10211 FREE_LOCK(&lk); 10212 } 10213 /* 10214 * Flush directory page containing the inode's name. 10215 */ 10216 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred, 10217 &bp); 10218 if (error == 0) 10219 error = bwrite(bp); 10220 else 10221 brelse(bp); 10222 vput(pvp); 10223 if (error != 0) 10224 return (error); 10225 ACQUIRE_LOCK(&lk); 10226 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 10227 break; 10228 } 10229 FREE_LOCK(&lk); 10230 return (0); 10231} 10232 10233/* 10234 * Flush all the dirty bitmaps associated with the block device 10235 * before flushing the rest of the dirty blocks so as to reduce 10236 * the number of dependencies that will have to be rolled back. 10237 */ 10238void 10239softdep_fsync_mountdev(vp) 10240 struct vnode *vp; 10241{ 10242 struct buf *bp, *nbp; 10243 struct worklist *wk; 10244 struct bufobj *bo; 10245 10246 if (!vn_isdisk(vp, NULL)) 10247 panic("softdep_fsync_mountdev: vnode not a disk"); 10248 bo = &vp->v_bufobj; 10249restart: 10250 BO_LOCK(bo); 10251 ACQUIRE_LOCK(&lk); 10252 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 10253 /* 10254 * If it is already scheduled, skip to the next buffer. 10255 */ 10256 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) 10257 continue; 10258 10259 if ((bp->b_flags & B_DELWRI) == 0) 10260 panic("softdep_fsync_mountdev: not dirty"); 10261 /* 10262 * We are only interested in bitmaps with outstanding 10263 * dependencies. 10264 */ 10265 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL || 10266 wk->wk_type != D_BMSAFEMAP || 10267 (bp->b_vflags & BV_BKGRDINPROG)) { 10268 BUF_UNLOCK(bp); 10269 continue; 10270 } 10271 FREE_LOCK(&lk); 10272 BO_UNLOCK(bo); 10273 bremfree(bp); 10274 (void) bawrite(bp); 10275 goto restart; 10276 } 10277 FREE_LOCK(&lk); 10278 drain_output(vp); 10279 BO_UNLOCK(bo); 10280} 10281 10282/* 10283 * This routine is called when we are trying to synchronously flush a 10284 * file. This routine must eliminate any filesystem metadata dependencies 10285 * so that the syncing routine can succeed by pushing the dirty blocks 10286 * associated with the file. If any I/O errors occur, they are returned. 10287 */ 10288int 10289softdep_sync_metadata(struct vnode *vp) 10290{ 10291 struct pagedep *pagedep; 10292 struct allocindir *aip; 10293 struct newblk *newblk; 10294 struct buf *bp, *nbp; 10295 struct worklist *wk; 10296 struct bufobj *bo; 10297 int i, error, waitfor; 10298 10299 if (!DOINGSOFTDEP(vp)) 10300 return (0); 10301 /* 10302 * Ensure that any direct block dependencies have been cleared. 10303 */ 10304 ACQUIRE_LOCK(&lk); 10305 if ((error = flush_inodedep_deps(vp->v_mount, VTOI(vp)->i_number))) { 10306 FREE_LOCK(&lk); 10307 return (error); 10308 } 10309 FREE_LOCK(&lk); 10310 /* 10311 * For most files, the only metadata dependencies are the 10312 * cylinder group maps that allocate their inode or blocks. 10313 * The block allocation dependencies can be found by traversing 10314 * the dependency lists for any buffers that remain on their 10315 * dirty buffer list. The inode allocation dependency will 10316 * be resolved when the inode is updated with MNT_WAIT. 10317 * This work is done in two passes. The first pass grabs most 10318 * of the buffers and begins asynchronously writing them. The 10319 * only way to wait for these asynchronous writes is to sleep 10320 * on the filesystem vnode which may stay busy for a long time 10321 * if the filesystem is active. So, instead, we make a second 10322 * pass over the dependencies blocking on each write. In the 10323 * usual case we will be blocking against a write that we 10324 * initiated, so when it is done the dependency will have been 10325 * resolved. Thus the second pass is expected to end quickly. 10326 */ 10327 waitfor = MNT_NOWAIT; 10328 bo = &vp->v_bufobj; 10329 10330top: 10331 /* 10332 * We must wait for any I/O in progress to finish so that 10333 * all potential buffers on the dirty list will be visible. 10334 */ 10335 BO_LOCK(bo); 10336 drain_output(vp); 10337 while ((bp = TAILQ_FIRST(&bo->bo_dirty.bv_hd)) != NULL) { 10338 bp = getdirtybuf(bp, BO_MTX(bo), MNT_WAIT); 10339 if (bp) 10340 break; 10341 } 10342 BO_UNLOCK(bo); 10343 if (bp == NULL) 10344 return (0); 10345loop: 10346 /* While syncing snapshots, we must allow recursive lookups */ 10347 BUF_AREC(bp); 10348 ACQUIRE_LOCK(&lk); 10349 /* 10350 * As we hold the buffer locked, none of its dependencies 10351 * will disappear. 10352 */ 10353 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 10354 switch (wk->wk_type) { 10355 10356 case D_ALLOCDIRECT: 10357 case D_ALLOCINDIR: 10358 newblk = WK_NEWBLK(wk); 10359 if (newblk->nb_jnewblk != NULL) { 10360 stat_jwait_newblk++; 10361 jwait(&newblk->nb_jnewblk->jn_list); 10362 goto restart; 10363 } 10364 if (newblk->nb_state & DEPCOMPLETE) 10365 continue; 10366 nbp = newblk->nb_bmsafemap->sm_buf; 10367 nbp = getdirtybuf(nbp, &lk, waitfor); 10368 if (nbp == NULL) 10369 continue; 10370 FREE_LOCK(&lk); 10371 if (waitfor == MNT_NOWAIT) { 10372 bawrite(nbp); 10373 } else if ((error = bwrite(nbp)) != 0) { 10374 break; 10375 } 10376 ACQUIRE_LOCK(&lk); 10377 continue; 10378 10379 case D_INDIRDEP: 10380 restart: 10381 10382 LIST_FOREACH(aip, 10383 &WK_INDIRDEP(wk)->ir_deplisthd, ai_next) { 10384 newblk = (struct newblk *)aip; 10385 if (newblk->nb_jnewblk != NULL) { 10386 stat_jwait_newblk++; 10387 jwait(&newblk->nb_jnewblk->jn_list); 10388 goto restart; 10389 } 10390 if (newblk->nb_state & DEPCOMPLETE) 10391 continue; 10392 nbp = newblk->nb_bmsafemap->sm_buf; 10393 nbp = getdirtybuf(nbp, &lk, MNT_WAIT); 10394 if (nbp == NULL) 10395 goto restart; 10396 FREE_LOCK(&lk); 10397 if ((error = bwrite(nbp)) != 0) { 10398 goto loop_end; 10399 } 10400 ACQUIRE_LOCK(&lk); 10401 goto restart; 10402 } 10403 continue; 10404 10405 case D_PAGEDEP: 10406 /* 10407 * We are trying to sync a directory that may 10408 * have dependencies on both its own metadata 10409 * and/or dependencies on the inodes of any 10410 * recently allocated files. We walk its diradd 10411 * lists pushing out the associated inode. 10412 */ 10413 pagedep = WK_PAGEDEP(wk); 10414 for (i = 0; i < DAHASHSZ; i++) { 10415 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0) 10416 continue; 10417 if ((error = 10418 flush_pagedep_deps(vp, wk->wk_mp, 10419 &pagedep->pd_diraddhd[i]))) { 10420 FREE_LOCK(&lk); 10421 goto loop_end; 10422 } 10423 } 10424 continue; 10425 10426 default: 10427 panic("softdep_sync_metadata: Unknown type %s", 10428 TYPENAME(wk->wk_type)); 10429 /* NOTREACHED */ 10430 } 10431 loop_end: 10432 /* We reach here only in error and unlocked */ 10433 if (error == 0) 10434 panic("softdep_sync_metadata: zero error"); 10435 BUF_NOREC(bp); 10436 bawrite(bp); 10437 return (error); 10438 } 10439 FREE_LOCK(&lk); 10440 BO_LOCK(bo); 10441 while ((nbp = TAILQ_NEXT(bp, b_bobufs)) != NULL) { 10442 nbp = getdirtybuf(nbp, BO_MTX(bo), MNT_WAIT); 10443 if (nbp) 10444 break; 10445 } 10446 BO_UNLOCK(bo); 10447 BUF_NOREC(bp); 10448 bawrite(bp); 10449 if (nbp != NULL) { 10450 bp = nbp; 10451 goto loop; 10452 } 10453 /* 10454 * The brief unlock is to allow any pent up dependency 10455 * processing to be done. Then proceed with the second pass. 10456 */ 10457 if (waitfor == MNT_NOWAIT) { 10458 waitfor = MNT_WAIT; 10459 goto top; 10460 } 10461 10462 /* 10463 * If we have managed to get rid of all the dirty buffers, 10464 * then we are done. For certain directories and block 10465 * devices, we may need to do further work. 10466 * 10467 * We must wait for any I/O in progress to finish so that 10468 * all potential buffers on the dirty list will be visible. 10469 */ 10470 BO_LOCK(bo); 10471 drain_output(vp); 10472 BO_UNLOCK(bo); 10473 return ffs_update(vp, 1); 10474 /* return (0); */ 10475} 10476 10477/* 10478 * Flush the dependencies associated with an inodedep. 10479 * Called with splbio blocked. 10480 */ 10481static int 10482flush_inodedep_deps(mp, ino) 10483 struct mount *mp; 10484 ino_t ino; 10485{ 10486 struct inodedep *inodedep; 10487 struct inoref *inoref; 10488 int error, waitfor; 10489 10490 /* 10491 * This work is done in two passes. The first pass grabs most 10492 * of the buffers and begins asynchronously writing them. The 10493 * only way to wait for these asynchronous writes is to sleep 10494 * on the filesystem vnode which may stay busy for a long time 10495 * if the filesystem is active. So, instead, we make a second 10496 * pass over the dependencies blocking on each write. In the 10497 * usual case we will be blocking against a write that we 10498 * initiated, so when it is done the dependency will have been 10499 * resolved. Thus the second pass is expected to end quickly. 10500 * We give a brief window at the top of the loop to allow 10501 * any pending I/O to complete. 10502 */ 10503 for (error = 0, waitfor = MNT_NOWAIT; ; ) { 10504 if (error) 10505 return (error); 10506 FREE_LOCK(&lk); 10507 ACQUIRE_LOCK(&lk); 10508restart: 10509 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 10510 return (0); 10511 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 10512 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 10513 == DEPCOMPLETE) { 10514 stat_jwait_inode++; 10515 jwait(&inoref->if_list); 10516 goto restart; 10517 } 10518 } 10519 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) || 10520 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) || 10521 flush_deplist(&inodedep->id_extupdt, waitfor, &error) || 10522 flush_deplist(&inodedep->id_newextupdt, waitfor, &error)) 10523 continue; 10524 /* 10525 * If pass2, we are done, otherwise do pass 2. 10526 */ 10527 if (waitfor == MNT_WAIT) 10528 break; 10529 waitfor = MNT_WAIT; 10530 } 10531 /* 10532 * Try freeing inodedep in case all dependencies have been removed. 10533 */ 10534 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0) 10535 (void) free_inodedep(inodedep); 10536 return (0); 10537} 10538 10539/* 10540 * Flush an inode dependency list. 10541 * Called with splbio blocked. 10542 */ 10543static int 10544flush_deplist(listhead, waitfor, errorp) 10545 struct allocdirectlst *listhead; 10546 int waitfor; 10547 int *errorp; 10548{ 10549 struct allocdirect *adp; 10550 struct newblk *newblk; 10551 struct buf *bp; 10552 10553 mtx_assert(&lk, MA_OWNED); 10554 TAILQ_FOREACH(adp, listhead, ad_next) { 10555 newblk = (struct newblk *)adp; 10556 if (newblk->nb_jnewblk != NULL) { 10557 stat_jwait_newblk++; 10558 jwait(&newblk->nb_jnewblk->jn_list); 10559 return (1); 10560 } 10561 if (newblk->nb_state & DEPCOMPLETE) 10562 continue; 10563 bp = newblk->nb_bmsafemap->sm_buf; 10564 bp = getdirtybuf(bp, &lk, waitfor); 10565 if (bp == NULL) { 10566 if (waitfor == MNT_NOWAIT) 10567 continue; 10568 return (1); 10569 } 10570 FREE_LOCK(&lk); 10571 if (waitfor == MNT_NOWAIT) { 10572 bawrite(bp); 10573 } else if ((*errorp = bwrite(bp)) != 0) { 10574 ACQUIRE_LOCK(&lk); 10575 return (1); 10576 } 10577 ACQUIRE_LOCK(&lk); 10578 return (1); 10579 } 10580 return (0); 10581} 10582 10583/* 10584 * Flush dependencies associated with an allocdirect block. 10585 */ 10586static int 10587flush_newblk_dep(vp, mp, lbn) 10588 struct vnode *vp; 10589 struct mount *mp; 10590 ufs_lbn_t lbn; 10591{ 10592 struct newblk *newblk; 10593 struct bufobj *bo; 10594 struct inode *ip; 10595 struct buf *bp; 10596 ufs2_daddr_t blkno; 10597 int error; 10598 10599 error = 0; 10600 bo = &vp->v_bufobj; 10601 ip = VTOI(vp); 10602 blkno = DIP(ip, i_db[lbn]); 10603 if (blkno == 0) 10604 panic("flush_newblk_dep: Missing block"); 10605 ACQUIRE_LOCK(&lk); 10606 /* 10607 * Loop until all dependencies related to this block are satisfied. 10608 * We must be careful to restart after each sleep in case a write 10609 * completes some part of this process for us. 10610 */ 10611 for (;;) { 10612 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) { 10613 FREE_LOCK(&lk); 10614 break; 10615 } 10616 if (newblk->nb_list.wk_type != D_ALLOCDIRECT) 10617 panic("flush_newblk_deps: Bad newblk %p", newblk); 10618 /* 10619 * Flush the journal. 10620 */ 10621 if (newblk->nb_jnewblk != NULL) { 10622 stat_jwait_newblk++; 10623 jwait(&newblk->nb_jnewblk->jn_list); 10624 continue; 10625 } 10626 /* 10627 * Write the bitmap dependency. 10628 */ 10629 if ((newblk->nb_state & DEPCOMPLETE) == 0) { 10630 bp = newblk->nb_bmsafemap->sm_buf; 10631 bp = getdirtybuf(bp, &lk, MNT_WAIT); 10632 if (bp == NULL) 10633 continue; 10634 FREE_LOCK(&lk); 10635 error = bwrite(bp); 10636 if (error) 10637 break; 10638 ACQUIRE_LOCK(&lk); 10639 continue; 10640 } 10641 /* 10642 * Write the buffer. 10643 */ 10644 FREE_LOCK(&lk); 10645 BO_LOCK(bo); 10646 bp = gbincore(bo, lbn); 10647 if (bp != NULL) { 10648 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 10649 LK_INTERLOCK, BO_MTX(bo)); 10650 if (error == ENOLCK) { 10651 ACQUIRE_LOCK(&lk); 10652 continue; /* Slept, retry */ 10653 } 10654 if (error != 0) 10655 break; /* Failed */ 10656 if (bp->b_flags & B_DELWRI) { 10657 bremfree(bp); 10658 error = bwrite(bp); 10659 if (error) 10660 break; 10661 } else 10662 BUF_UNLOCK(bp); 10663 } else 10664 BO_UNLOCK(bo); 10665 /* 10666 * We have to wait for the direct pointers to 10667 * point at the newdirblk before the dependency 10668 * will go away. 10669 */ 10670 error = ffs_update(vp, MNT_WAIT); 10671 if (error) 10672 break; 10673 ACQUIRE_LOCK(&lk); 10674 } 10675 return (error); 10676} 10677 10678/* 10679 * Eliminate a pagedep dependency by flushing out all its diradd dependencies. 10680 * Called with splbio blocked. 10681 */ 10682static int 10683flush_pagedep_deps(pvp, mp, diraddhdp) 10684 struct vnode *pvp; 10685 struct mount *mp; 10686 struct diraddhd *diraddhdp; 10687{ 10688 struct inodedep *inodedep; 10689 struct inoref *inoref; 10690 struct ufsmount *ump; 10691 struct diradd *dap; 10692 struct vnode *vp; 10693 int error = 0; 10694 struct buf *bp; 10695 ino_t inum; 10696 10697 ump = VFSTOUFS(mp); 10698restart: 10699 while ((dap = LIST_FIRST(diraddhdp)) != NULL) { 10700 /* 10701 * Flush ourselves if this directory entry 10702 * has a MKDIR_PARENT dependency. 10703 */ 10704 if (dap->da_state & MKDIR_PARENT) { 10705 FREE_LOCK(&lk); 10706 if ((error = ffs_update(pvp, MNT_WAIT)) != 0) 10707 break; 10708 ACQUIRE_LOCK(&lk); 10709 /* 10710 * If that cleared dependencies, go on to next. 10711 */ 10712 if (dap != LIST_FIRST(diraddhdp)) 10713 continue; 10714 if (dap->da_state & MKDIR_PARENT) 10715 panic("flush_pagedep_deps: MKDIR_PARENT"); 10716 } 10717 /* 10718 * A newly allocated directory must have its "." and 10719 * ".." entries written out before its name can be 10720 * committed in its parent. 10721 */ 10722 inum = dap->da_newinum; 10723 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 10724 panic("flush_pagedep_deps: lost inode1"); 10725 /* 10726 * Wait for any pending journal adds to complete so we don't 10727 * cause rollbacks while syncing. 10728 */ 10729 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 10730 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 10731 == DEPCOMPLETE) { 10732 stat_jwait_inode++; 10733 jwait(&inoref->if_list); 10734 goto restart; 10735 } 10736 } 10737 if (dap->da_state & MKDIR_BODY) { 10738 FREE_LOCK(&lk); 10739 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp, 10740 FFSV_FORCEINSMQ))) 10741 break; 10742 error = flush_newblk_dep(vp, mp, 0); 10743 /* 10744 * If we still have the dependency we might need to 10745 * update the vnode to sync the new link count to 10746 * disk. 10747 */ 10748 if (error == 0 && dap == LIST_FIRST(diraddhdp)) 10749 error = ffs_update(vp, MNT_WAIT); 10750 vput(vp); 10751 if (error != 0) 10752 break; 10753 ACQUIRE_LOCK(&lk); 10754 /* 10755 * If that cleared dependencies, go on to next. 10756 */ 10757 if (dap != LIST_FIRST(diraddhdp)) 10758 continue; 10759 if (dap->da_state & MKDIR_BODY) { 10760 inodedep_lookup(UFSTOVFS(ump), inum, 0, 10761 &inodedep); 10762 panic("flush_pagedep_deps: MKDIR_BODY " 10763 "inodedep %p dap %p vp %p", 10764 inodedep, dap, vp); 10765 } 10766 } 10767 /* 10768 * Flush the inode on which the directory entry depends. 10769 * Having accounted for MKDIR_PARENT and MKDIR_BODY above, 10770 * the only remaining dependency is that the updated inode 10771 * count must get pushed to disk. The inode has already 10772 * been pushed into its inode buffer (via VOP_UPDATE) at 10773 * the time of the reference count change. So we need only 10774 * locate that buffer, ensure that there will be no rollback 10775 * caused by a bitmap dependency, then write the inode buffer. 10776 */ 10777retry: 10778 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 10779 panic("flush_pagedep_deps: lost inode"); 10780 /* 10781 * If the inode still has bitmap dependencies, 10782 * push them to disk. 10783 */ 10784 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) { 10785 bp = inodedep->id_bmsafemap->sm_buf; 10786 bp = getdirtybuf(bp, &lk, MNT_WAIT); 10787 if (bp == NULL) 10788 goto retry; 10789 FREE_LOCK(&lk); 10790 if ((error = bwrite(bp)) != 0) 10791 break; 10792 ACQUIRE_LOCK(&lk); 10793 if (dap != LIST_FIRST(diraddhdp)) 10794 continue; 10795 } 10796 /* 10797 * If the inode is still sitting in a buffer waiting 10798 * to be written or waiting for the link count to be 10799 * adjusted update it here to flush it to disk. 10800 */ 10801 if (dap == LIST_FIRST(diraddhdp)) { 10802 FREE_LOCK(&lk); 10803 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp, 10804 FFSV_FORCEINSMQ))) 10805 break; 10806 error = ffs_update(vp, MNT_WAIT); 10807 vput(vp); 10808 if (error) 10809 break; 10810 ACQUIRE_LOCK(&lk); 10811 } 10812 /* 10813 * If we have failed to get rid of all the dependencies 10814 * then something is seriously wrong. 10815 */ 10816 if (dap == LIST_FIRST(diraddhdp)) { 10817 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep); 10818 panic("flush_pagedep_deps: failed to flush " 10819 "inodedep %p ino %d dap %p", inodedep, inum, dap); 10820 } 10821 } 10822 if (error) 10823 ACQUIRE_LOCK(&lk); 10824 return (error); 10825} 10826 10827/* 10828 * A large burst of file addition or deletion activity can drive the 10829 * memory load excessively high. First attempt to slow things down 10830 * using the techniques below. If that fails, this routine requests 10831 * the offending operations to fall back to running synchronously 10832 * until the memory load returns to a reasonable level. 10833 */ 10834int 10835softdep_slowdown(vp) 10836 struct vnode *vp; 10837{ 10838 struct ufsmount *ump; 10839 int jlow; 10840 int max_softdeps_hard; 10841 10842 ACQUIRE_LOCK(&lk); 10843 jlow = 0; 10844 /* 10845 * Check for journal space if needed. 10846 */ 10847 if (DOINGSUJ(vp)) { 10848 ump = VFSTOUFS(vp->v_mount); 10849 if (journal_space(ump, 0) == 0) 10850 jlow = 1; 10851 } 10852 max_softdeps_hard = max_softdeps * 11 / 10; 10853 if (num_dirrem < max_softdeps_hard / 2 && 10854 num_inodedep < max_softdeps_hard && 10855 VFSTOUFS(vp->v_mount)->um_numindirdeps < maxindirdeps && 10856 num_freeblkdep < max_softdeps_hard && jlow == 0) { 10857 FREE_LOCK(&lk); 10858 return (0); 10859 } 10860 if (VFSTOUFS(vp->v_mount)->um_numindirdeps >= maxindirdeps || jlow) 10861 softdep_speedup(); 10862 stat_sync_limit_hit += 1; 10863 FREE_LOCK(&lk); 10864 return (1); 10865} 10866 10867/* 10868 * Called by the allocation routines when they are about to fail 10869 * in the hope that we can free up some disk space. 10870 * 10871 * First check to see if the work list has anything on it. If it has, 10872 * clean up entries until we successfully free some space. Because this 10873 * process holds inodes locked, we cannot handle any remove requests 10874 * that might block on a locked inode as that could lead to deadlock. 10875 * If the worklist yields no free space, encourage the syncer daemon 10876 * to help us. In no event will we try for longer than tickdelay seconds. 10877 */ 10878int 10879softdep_request_cleanup(fs, vp) 10880 struct fs *fs; 10881 struct vnode *vp; 10882{ 10883 struct ufsmount *ump; 10884 long starttime; 10885 ufs2_daddr_t needed; 10886 int error; 10887 10888 ump = VTOI(vp)->i_ump; 10889 mtx_assert(UFS_MTX(ump), MA_OWNED); 10890 needed = fs->fs_cstotal.cs_nbfree + fs->fs_contigsumsize; 10891 starttime = time_second + tickdelay; 10892 /* 10893 * If we are being called because of a process doing a 10894 * copy-on-write, then it is not safe to update the vnode 10895 * as we may recurse into the copy-on-write routine. 10896 */ 10897 if (!(curthread->td_pflags & TDP_COWINPROGRESS)) { 10898 UFS_UNLOCK(ump); 10899 error = ffs_update(vp, 1); 10900 UFS_LOCK(ump); 10901 if (error != 0) 10902 return (0); 10903 } 10904 while (fs->fs_pendingblocks > 0 && fs->fs_cstotal.cs_nbfree <= needed) { 10905 if (time_second > starttime) 10906 return (0); 10907 UFS_UNLOCK(ump); 10908 ACQUIRE_LOCK(&lk); 10909 process_removes(vp); 10910 if (ump->softdep_on_worklist > 0 && 10911 process_worklist_item(UFSTOVFS(ump), LK_NOWAIT) != -1) { 10912 stat_worklist_push += 1; 10913 FREE_LOCK(&lk); 10914 UFS_LOCK(ump); 10915 continue; 10916 } 10917 request_cleanup(UFSTOVFS(ump), FLUSH_REMOVE_WAIT); 10918 FREE_LOCK(&lk); 10919 UFS_LOCK(ump); 10920 } 10921 return (1); 10922} 10923 10924/* 10925 * If memory utilization has gotten too high, deliberately slow things 10926 * down and speed up the I/O processing. 10927 */ 10928extern struct thread *syncertd; 10929static int 10930request_cleanup(mp, resource) 10931 struct mount *mp; 10932 int resource; 10933{ 10934 struct thread *td = curthread; 10935 struct ufsmount *ump; 10936 10937 mtx_assert(&lk, MA_OWNED); 10938 /* 10939 * We never hold up the filesystem syncer or buf daemon. 10940 */ 10941 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF)) 10942 return (0); 10943 ump = VFSTOUFS(mp); 10944 /* 10945 * First check to see if the work list has gotten backlogged. 10946 * If it has, co-opt this process to help clean up two entries. 10947 * Because this process may hold inodes locked, we cannot 10948 * handle any remove requests that might block on a locked 10949 * inode as that could lead to deadlock. We set TDP_SOFTDEP 10950 * to avoid recursively processing the worklist. 10951 */ 10952 if (ump->softdep_on_worklist > max_softdeps / 10) { 10953 td->td_pflags |= TDP_SOFTDEP; 10954 process_worklist_item(mp, LK_NOWAIT); 10955 process_worklist_item(mp, LK_NOWAIT); 10956 td->td_pflags &= ~TDP_SOFTDEP; 10957 stat_worklist_push += 2; 10958 return(1); 10959 } 10960 /* 10961 * Next, we attempt to speed up the syncer process. If that 10962 * is successful, then we allow the process to continue. 10963 */ 10964 if (softdep_speedup() && resource != FLUSH_REMOVE_WAIT) 10965 return(0); 10966 /* 10967 * If we are resource constrained on inode dependencies, try 10968 * flushing some dirty inodes. Otherwise, we are constrained 10969 * by file deletions, so try accelerating flushes of directories 10970 * with removal dependencies. We would like to do the cleanup 10971 * here, but we probably hold an inode locked at this point and 10972 * that might deadlock against one that we try to clean. So, 10973 * the best that we can do is request the syncer daemon to do 10974 * the cleanup for us. 10975 */ 10976 switch (resource) { 10977 10978 case FLUSH_INODES: 10979 stat_ino_limit_push += 1; 10980 req_clear_inodedeps += 1; 10981 stat_countp = &stat_ino_limit_hit; 10982 break; 10983 10984 case FLUSH_REMOVE: 10985 case FLUSH_REMOVE_WAIT: 10986 stat_blk_limit_push += 1; 10987 req_clear_remove += 1; 10988 stat_countp = &stat_blk_limit_hit; 10989 break; 10990 10991 default: 10992 panic("request_cleanup: unknown type"); 10993 } 10994 /* 10995 * Hopefully the syncer daemon will catch up and awaken us. 10996 * We wait at most tickdelay before proceeding in any case. 10997 */ 10998 proc_waiting += 1; 10999 if (callout_pending(&softdep_callout) == FALSE) 11000 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2, 11001 pause_timer, 0); 11002 11003 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0); 11004 proc_waiting -= 1; 11005 return (1); 11006} 11007 11008/* 11009 * Awaken processes pausing in request_cleanup and clear proc_waiting 11010 * to indicate that there is no longer a timer running. 11011 */ 11012static void 11013pause_timer(arg) 11014 void *arg; 11015{ 11016 11017 /* 11018 * The callout_ API has acquired mtx and will hold it around this 11019 * function call. 11020 */ 11021 *stat_countp += 1; 11022 wakeup_one(&proc_waiting); 11023 if (proc_waiting > 0) 11024 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2, 11025 pause_timer, 0); 11026} 11027 11028/* 11029 * Flush out a directory with at least one removal dependency in an effort to 11030 * reduce the number of dirrem, freefile, and freeblks dependency structures. 11031 */ 11032static void 11033clear_remove(td) 11034 struct thread *td; 11035{ 11036 struct pagedep_hashhead *pagedephd; 11037 struct pagedep *pagedep; 11038 static int next = 0; 11039 struct mount *mp; 11040 struct vnode *vp; 11041 struct bufobj *bo; 11042 int error, cnt; 11043 ino_t ino; 11044 11045 mtx_assert(&lk, MA_OWNED); 11046 11047 for (cnt = 0; cnt < pagedep_hash; cnt++) { 11048 pagedephd = &pagedep_hashtbl[next++]; 11049 if (next >= pagedep_hash) 11050 next = 0; 11051 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 11052 if (LIST_EMPTY(&pagedep->pd_dirremhd)) 11053 continue; 11054 mp = pagedep->pd_list.wk_mp; 11055 ino = pagedep->pd_ino; 11056 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 11057 continue; 11058 FREE_LOCK(&lk); 11059 11060 /* 11061 * Let unmount clear deps 11062 */ 11063 error = vfs_busy(mp, MBF_NOWAIT); 11064 if (error != 0) 11065 goto finish_write; 11066 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 11067 FFSV_FORCEINSMQ); 11068 vfs_unbusy(mp); 11069 if (error != 0) { 11070 softdep_error("clear_remove: vget", error); 11071 goto finish_write; 11072 } 11073 if ((error = ffs_syncvnode(vp, MNT_NOWAIT))) 11074 softdep_error("clear_remove: fsync", error); 11075 bo = &vp->v_bufobj; 11076 BO_LOCK(bo); 11077 drain_output(vp); 11078 BO_UNLOCK(bo); 11079 vput(vp); 11080 finish_write: 11081 vn_finished_write(mp); 11082 ACQUIRE_LOCK(&lk); 11083 return; 11084 } 11085 } 11086} 11087 11088/* 11089 * Clear out a block of dirty inodes in an effort to reduce 11090 * the number of inodedep dependency structures. 11091 */ 11092static void 11093clear_inodedeps(td) 11094 struct thread *td; 11095{ 11096 struct inodedep_hashhead *inodedephd; 11097 struct inodedep *inodedep; 11098 static int next = 0; 11099 struct mount *mp; 11100 struct vnode *vp; 11101 struct fs *fs; 11102 int error, cnt; 11103 ino_t firstino, lastino, ino; 11104 11105 mtx_assert(&lk, MA_OWNED); 11106 /* 11107 * Pick a random inode dependency to be cleared. 11108 * We will then gather up all the inodes in its block 11109 * that have dependencies and flush them out. 11110 */ 11111 for (cnt = 0; cnt < inodedep_hash; cnt++) { 11112 inodedephd = &inodedep_hashtbl[next++]; 11113 if (next >= inodedep_hash) 11114 next = 0; 11115 if ((inodedep = LIST_FIRST(inodedephd)) != NULL) 11116 break; 11117 } 11118 if (inodedep == NULL) 11119 return; 11120 fs = inodedep->id_fs; 11121 mp = inodedep->id_list.wk_mp; 11122 /* 11123 * Find the last inode in the block with dependencies. 11124 */ 11125 firstino = inodedep->id_ino & ~(INOPB(fs) - 1); 11126 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--) 11127 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0) 11128 break; 11129 /* 11130 * Asynchronously push all but the last inode with dependencies. 11131 * Synchronously push the last inode with dependencies to ensure 11132 * that the inode block gets written to free up the inodedeps. 11133 */ 11134 for (ino = firstino; ino <= lastino; ino++) { 11135 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 11136 continue; 11137 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 11138 continue; 11139 FREE_LOCK(&lk); 11140 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */ 11141 if (error != 0) { 11142 vn_finished_write(mp); 11143 ACQUIRE_LOCK(&lk); 11144 return; 11145 } 11146 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 11147 FFSV_FORCEINSMQ)) != 0) { 11148 softdep_error("clear_inodedeps: vget", error); 11149 vfs_unbusy(mp); 11150 vn_finished_write(mp); 11151 ACQUIRE_LOCK(&lk); 11152 return; 11153 } 11154 vfs_unbusy(mp); 11155 if (ino == lastino) { 11156 if ((error = ffs_syncvnode(vp, MNT_WAIT))) 11157 softdep_error("clear_inodedeps: fsync1", error); 11158 } else { 11159 if ((error = ffs_syncvnode(vp, MNT_NOWAIT))) 11160 softdep_error("clear_inodedeps: fsync2", error); 11161 BO_LOCK(&vp->v_bufobj); 11162 drain_output(vp); 11163 BO_UNLOCK(&vp->v_bufobj); 11164 } 11165 vput(vp); 11166 vn_finished_write(mp); 11167 ACQUIRE_LOCK(&lk); 11168 } 11169} 11170 11171/* 11172 * Function to determine if the buffer has outstanding dependencies 11173 * that will cause a roll-back if the buffer is written. If wantcount 11174 * is set, return number of dependencies, otherwise just yes or no. 11175 */ 11176static int 11177softdep_count_dependencies(bp, wantcount) 11178 struct buf *bp; 11179 int wantcount; 11180{ 11181 struct worklist *wk; 11182 struct bmsafemap *bmsafemap; 11183 struct inodedep *inodedep; 11184 struct indirdep *indirdep; 11185 struct freeblks *freeblks; 11186 struct allocindir *aip; 11187 struct pagedep *pagedep; 11188 struct dirrem *dirrem; 11189 struct newblk *newblk; 11190 struct mkdir *mkdir; 11191 struct diradd *dap; 11192 int i, retval; 11193 11194 retval = 0; 11195 ACQUIRE_LOCK(&lk); 11196 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 11197 switch (wk->wk_type) { 11198 11199 case D_INODEDEP: 11200 inodedep = WK_INODEDEP(wk); 11201 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 11202 /* bitmap allocation dependency */ 11203 retval += 1; 11204 if (!wantcount) 11205 goto out; 11206 } 11207 if (TAILQ_FIRST(&inodedep->id_inoupdt)) { 11208 /* direct block pointer dependency */ 11209 retval += 1; 11210 if (!wantcount) 11211 goto out; 11212 } 11213 if (TAILQ_FIRST(&inodedep->id_extupdt)) { 11214 /* direct block pointer dependency */ 11215 retval += 1; 11216 if (!wantcount) 11217 goto out; 11218 } 11219 if (TAILQ_FIRST(&inodedep->id_inoreflst)) { 11220 /* Add reference dependency. */ 11221 retval += 1; 11222 if (!wantcount) 11223 goto out; 11224 } 11225 continue; 11226 11227 case D_INDIRDEP: 11228 indirdep = WK_INDIRDEP(wk); 11229 11230 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 11231 /* indirect block pointer dependency */ 11232 retval += 1; 11233 if (!wantcount) 11234 goto out; 11235 } 11236 continue; 11237 11238 case D_PAGEDEP: 11239 pagedep = WK_PAGEDEP(wk); 11240 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) { 11241 if (LIST_FIRST(&dirrem->dm_jremrefhd)) { 11242 /* Journal remove ref dependency. */ 11243 retval += 1; 11244 if (!wantcount) 11245 goto out; 11246 } 11247 } 11248 for (i = 0; i < DAHASHSZ; i++) { 11249 11250 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 11251 /* directory entry dependency */ 11252 retval += 1; 11253 if (!wantcount) 11254 goto out; 11255 } 11256 } 11257 continue; 11258 11259 case D_BMSAFEMAP: 11260 bmsafemap = WK_BMSAFEMAP(wk); 11261 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) { 11262 /* Add reference dependency. */ 11263 retval += 1; 11264 if (!wantcount) 11265 goto out; 11266 } 11267 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) { 11268 /* Allocate block dependency. */ 11269 retval += 1; 11270 if (!wantcount) 11271 goto out; 11272 } 11273 continue; 11274 11275 case D_FREEBLKS: 11276 freeblks = WK_FREEBLKS(wk); 11277 if (LIST_FIRST(&freeblks->fb_jfreeblkhd)) { 11278 /* Freeblk journal dependency. */ 11279 retval += 1; 11280 if (!wantcount) 11281 goto out; 11282 } 11283 continue; 11284 11285 case D_ALLOCDIRECT: 11286 case D_ALLOCINDIR: 11287 newblk = WK_NEWBLK(wk); 11288 if (newblk->nb_jnewblk) { 11289 /* Journal allocate dependency. */ 11290 retval += 1; 11291 if (!wantcount) 11292 goto out; 11293 } 11294 continue; 11295 11296 case D_MKDIR: 11297 mkdir = WK_MKDIR(wk); 11298 if (mkdir->md_jaddref) { 11299 /* Journal reference dependency. */ 11300 retval += 1; 11301 if (!wantcount) 11302 goto out; 11303 } 11304 continue; 11305 11306 case D_FREEWORK: 11307 case D_FREEDEP: 11308 case D_JSEGDEP: 11309 case D_JSEG: 11310 case D_SBDEP: 11311 /* never a dependency on these blocks */ 11312 continue; 11313 11314 default: 11315 panic("softdep_count_dependencies: Unexpected type %s", 11316 TYPENAME(wk->wk_type)); 11317 /* NOTREACHED */ 11318 } 11319 } 11320out: 11321 FREE_LOCK(&lk); 11322 return retval; 11323} 11324 11325/* 11326 * Acquire exclusive access to a buffer. 11327 * Must be called with a locked mtx parameter. 11328 * Return acquired buffer or NULL on failure. 11329 */ 11330static struct buf * 11331getdirtybuf(bp, mtx, waitfor) 11332 struct buf *bp; 11333 struct mtx *mtx; 11334 int waitfor; 11335{ 11336 int error; 11337 11338 mtx_assert(mtx, MA_OWNED); 11339 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) { 11340 if (waitfor != MNT_WAIT) 11341 return (NULL); 11342 error = BUF_LOCK(bp, 11343 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, mtx); 11344 /* 11345 * Even if we sucessfully acquire bp here, we have dropped 11346 * mtx, which may violates our guarantee. 11347 */ 11348 if (error == 0) 11349 BUF_UNLOCK(bp); 11350 else if (error != ENOLCK) 11351 panic("getdirtybuf: inconsistent lock: %d", error); 11352 mtx_lock(mtx); 11353 return (NULL); 11354 } 11355 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 11356 if (mtx == &lk && waitfor == MNT_WAIT) { 11357 mtx_unlock(mtx); 11358 BO_LOCK(bp->b_bufobj); 11359 BUF_UNLOCK(bp); 11360 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 11361 bp->b_vflags |= BV_BKGRDWAIT; 11362 msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), 11363 PRIBIO | PDROP, "getbuf", 0); 11364 } else 11365 BO_UNLOCK(bp->b_bufobj); 11366 mtx_lock(mtx); 11367 return (NULL); 11368 } 11369 BUF_UNLOCK(bp); 11370 if (waitfor != MNT_WAIT) 11371 return (NULL); 11372 /* 11373 * The mtx argument must be bp->b_vp's mutex in 11374 * this case. 11375 */ 11376#ifdef DEBUG_VFS_LOCKS 11377 if (bp->b_vp->v_type != VCHR) 11378 ASSERT_BO_LOCKED(bp->b_bufobj); 11379#endif 11380 bp->b_vflags |= BV_BKGRDWAIT; 11381 msleep(&bp->b_xflags, mtx, PRIBIO, "getbuf", 0); 11382 return (NULL); 11383 } 11384 if ((bp->b_flags & B_DELWRI) == 0) { 11385 BUF_UNLOCK(bp); 11386 return (NULL); 11387 } 11388 bremfree(bp); 11389 return (bp); 11390} 11391 11392 11393/* 11394 * Check if it is safe to suspend the file system now. On entry, 11395 * the vnode interlock for devvp should be held. Return 0 with 11396 * the mount interlock held if the file system can be suspended now, 11397 * otherwise return EAGAIN with the mount interlock held. 11398 */ 11399int 11400softdep_check_suspend(struct mount *mp, 11401 struct vnode *devvp, 11402 int softdep_deps, 11403 int softdep_accdeps, 11404 int secondary_writes, 11405 int secondary_accwrites) 11406{ 11407 struct bufobj *bo; 11408 struct ufsmount *ump; 11409 int error; 11410 11411 ump = VFSTOUFS(mp); 11412 bo = &devvp->v_bufobj; 11413 ASSERT_BO_LOCKED(bo); 11414 11415 for (;;) { 11416 if (!TRY_ACQUIRE_LOCK(&lk)) { 11417 BO_UNLOCK(bo); 11418 ACQUIRE_LOCK(&lk); 11419 FREE_LOCK(&lk); 11420 BO_LOCK(bo); 11421 continue; 11422 } 11423 MNT_ILOCK(mp); 11424 if (mp->mnt_secondary_writes != 0) { 11425 FREE_LOCK(&lk); 11426 BO_UNLOCK(bo); 11427 msleep(&mp->mnt_secondary_writes, 11428 MNT_MTX(mp), 11429 (PUSER - 1) | PDROP, "secwr", 0); 11430 BO_LOCK(bo); 11431 continue; 11432 } 11433 break; 11434 } 11435 11436 /* 11437 * Reasons for needing more work before suspend: 11438 * - Dirty buffers on devvp. 11439 * - Softdep activity occurred after start of vnode sync loop 11440 * - Secondary writes occurred after start of vnode sync loop 11441 */ 11442 error = 0; 11443 if (bo->bo_numoutput > 0 || 11444 bo->bo_dirty.bv_cnt > 0 || 11445 softdep_deps != 0 || 11446 ump->softdep_deps != 0 || 11447 softdep_accdeps != ump->softdep_accdeps || 11448 secondary_writes != 0 || 11449 mp->mnt_secondary_writes != 0 || 11450 secondary_accwrites != mp->mnt_secondary_accwrites) 11451 error = EAGAIN; 11452 FREE_LOCK(&lk); 11453 BO_UNLOCK(bo); 11454 return (error); 11455} 11456 11457 11458/* 11459 * Get the number of dependency structures for the file system, both 11460 * the current number and the total number allocated. These will 11461 * later be used to detect that softdep processing has occurred. 11462 */ 11463void 11464softdep_get_depcounts(struct mount *mp, 11465 int *softdep_depsp, 11466 int *softdep_accdepsp) 11467{ 11468 struct ufsmount *ump; 11469 11470 ump = VFSTOUFS(mp); 11471 ACQUIRE_LOCK(&lk); 11472 *softdep_depsp = ump->softdep_deps; 11473 *softdep_accdepsp = ump->softdep_accdeps; 11474 FREE_LOCK(&lk); 11475} 11476 11477/* 11478 * Wait for pending output on a vnode to complete. 11479 * Must be called with vnode lock and interlock locked. 11480 * 11481 * XXX: Should just be a call to bufobj_wwait(). 11482 */ 11483static void 11484drain_output(vp) 11485 struct vnode *vp; 11486{ 11487 struct bufobj *bo; 11488 11489 bo = &vp->v_bufobj; 11490 ASSERT_VOP_LOCKED(vp, "drain_output"); 11491 ASSERT_BO_LOCKED(bo); 11492 11493 while (bo->bo_numoutput) { 11494 bo->bo_flag |= BO_WWAIT; 11495 msleep((caddr_t)&bo->bo_numoutput, 11496 BO_MTX(bo), PRIBIO + 1, "drainvp", 0); 11497 } 11498} 11499 11500/* 11501 * Called whenever a buffer that is being invalidated or reallocated 11502 * contains dependencies. This should only happen if an I/O error has 11503 * occurred. The routine is called with the buffer locked. 11504 */ 11505static void 11506softdep_deallocate_dependencies(bp) 11507 struct buf *bp; 11508{ 11509 11510 if ((bp->b_ioflags & BIO_ERROR) == 0) 11511 panic("softdep_deallocate_dependencies: dangling deps"); 11512 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error); 11513 panic("softdep_deallocate_dependencies: unrecovered I/O error"); 11514} 11515 11516/* 11517 * Function to handle asynchronous write errors in the filesystem. 11518 */ 11519static void 11520softdep_error(func, error) 11521 char *func; 11522 int error; 11523{ 11524 11525 /* XXX should do something better! */ 11526 printf("%s: got error %d while accessing filesystem\n", func, error); 11527} 11528 11529#ifdef DDB 11530 11531static void 11532inodedep_print(struct inodedep *inodedep, int verbose) 11533{ 11534 db_printf("%p fs %p st %x ino %jd inoblk %jd delta %d nlink %d" 11535 " saveino %p\n", 11536 inodedep, inodedep->id_fs, inodedep->id_state, 11537 (intmax_t)inodedep->id_ino, 11538 (intmax_t)fsbtodb(inodedep->id_fs, 11539 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)), 11540 inodedep->id_nlinkdelta, inodedep->id_savednlink, 11541 inodedep->id_savedino1); 11542 11543 if (verbose == 0) 11544 return; 11545 11546 db_printf("\tpendinghd %p, bufwait %p, inowait %p, inoreflst %p, " 11547 "mkdiradd %p\n", 11548 LIST_FIRST(&inodedep->id_pendinghd), 11549 LIST_FIRST(&inodedep->id_bufwait), 11550 LIST_FIRST(&inodedep->id_inowait), 11551 TAILQ_FIRST(&inodedep->id_inoreflst), 11552 inodedep->id_mkdiradd); 11553 db_printf("\tinoupdt %p, newinoupdt %p, extupdt %p, newextupdt %p\n", 11554 TAILQ_FIRST(&inodedep->id_inoupdt), 11555 TAILQ_FIRST(&inodedep->id_newinoupdt), 11556 TAILQ_FIRST(&inodedep->id_extupdt), 11557 TAILQ_FIRST(&inodedep->id_newextupdt)); 11558} 11559 11560DB_SHOW_COMMAND(inodedep, db_show_inodedep) 11561{ 11562 11563 if (have_addr == 0) { 11564 db_printf("Address required\n"); 11565 return; 11566 } 11567 inodedep_print((struct inodedep*)addr, 1); 11568} 11569 11570DB_SHOW_COMMAND(inodedeps, db_show_inodedeps) 11571{ 11572 struct inodedep_hashhead *inodedephd; 11573 struct inodedep *inodedep; 11574 struct fs *fs; 11575 int cnt; 11576 11577 fs = have_addr ? (struct fs *)addr : NULL; 11578 for (cnt = 0; cnt < inodedep_hash; cnt++) { 11579 inodedephd = &inodedep_hashtbl[cnt]; 11580 LIST_FOREACH(inodedep, inodedephd, id_hash) { 11581 if (fs != NULL && fs != inodedep->id_fs) 11582 continue; 11583 inodedep_print(inodedep, 0); 11584 } 11585 } 11586} 11587 11588DB_SHOW_COMMAND(worklist, db_show_worklist) 11589{ 11590 struct worklist *wk; 11591 11592 if (have_addr == 0) { 11593 db_printf("Address required\n"); 11594 return; 11595 } 11596 wk = (struct worklist *)addr; 11597 printf("worklist: %p type %s state 0x%X\n", 11598 wk, TYPENAME(wk->wk_type), wk->wk_state); 11599} 11600 11601DB_SHOW_COMMAND(workhead, db_show_workhead) 11602{ 11603 struct workhead *wkhd; 11604 struct worklist *wk; 11605 int i; 11606 11607 if (have_addr == 0) { 11608 db_printf("Address required\n"); 11609 return; 11610 } 11611 wkhd = (struct workhead *)addr; 11612 wk = LIST_FIRST(wkhd); 11613 for (i = 0; i < 100 && wk != NULL; i++, wk = LIST_NEXT(wk, wk_list)) 11614 db_printf("worklist: %p type %s state 0x%X", 11615 wk, TYPENAME(wk->wk_type), wk->wk_state); 11616 if (i == 100) 11617 db_printf("workhead overflow"); 11618 printf("\n"); 11619} 11620 11621 11622DB_SHOW_COMMAND(mkdirs, db_show_mkdirs) 11623{ 11624 struct jaddref *jaddref; 11625 struct diradd *diradd; 11626 struct mkdir *mkdir; 11627 11628 LIST_FOREACH(mkdir, &mkdirlisthd, md_mkdirs) { 11629 diradd = mkdir->md_diradd; 11630 db_printf("mkdir: %p state 0x%X dap %p state 0x%X", 11631 mkdir, mkdir->md_state, diradd, diradd->da_state); 11632 if ((jaddref = mkdir->md_jaddref) != NULL) 11633 db_printf(" jaddref %p jaddref state 0x%X", 11634 jaddref, jaddref->ja_state); 11635 db_printf("\n"); 11636 } 11637} 11638 11639#endif /* DDB */ 11640 11641#endif /* SOFTUPDATES */ 11642