1 /*- 2 * Copyright 1998, 2000 Marshall Kirk McKusick. All Rights Reserved. 3 * 4 * The soft updates code is derived from the appendix of a University 5 * of Michigan technical report (Gregory R. Ganger and Yale N. Patt, 6 * "Soft Updates: A Solution to the Metadata Update Problem in File 7 * Systems", CSE-TR-254-95, August 1995). 8 * 9 * Further information about soft updates can be obtained from: 10 * 11 * Marshall Kirk McKusick http://www.mckusick.com/softdep/ 12 * 1614 Oxford Street mckusick@mckusick.com 13 * Berkeley, CA 94709-1608 +1-510-843-9542 14 * USA 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 20 * 1. Redistributions of source code must retain the above copyright 21 * notice, this list of conditions and the following disclaimer. 22 * 2. Redistributions in binary form must reproduce the above copyright 23 * notice, this list of conditions and the following disclaimer in the 24 * documentation and/or other materials provided with the distribution. 25 * 26 * THIS SOFTWARE IS PROVIDED BY MARSHALL KIRK MCKUSICK ``AS IS'' AND ANY 27 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 29 * DISCLAIMED. IN NO EVENT SHALL MARSHALL KIRK MCKUSICK BE LIABLE FOR 30 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * from: @(#)ffs_softdep.c 9.59 (McKusick) 6/21/00 39 */ 40 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include "opt_ffs.h" 45 #include "opt_ddb.h" 46 47 /* 48 * For now we want the safety net that the DEBUG flag provides. 49 */ 50 #ifndef DEBUG 51 #define DEBUG 52 #endif 53 54 #include <sys/param.h> 55 #include <sys/kernel.h> 56 #include <sys/systm.h> 57 #include <sys/bio.h> 58 #include <sys/buf.h> 59 #include <sys/kdb.h> 60 #include <sys/kthread.h> 61 #include <sys/lock.h> 62 #include <sys/malloc.h> 63 #include <sys/mount.h> 64 #include <sys/mutex.h> 65 #include <sys/proc.h> 66 #include <sys/stat.h> 67 #include <sys/sysctl.h> 68 #include <sys/syslog.h> 69 #include <sys/vnode.h> 70 #include <sys/conf.h> 71 #include <ufs/ufs/dir.h> 72 #include <ufs/ufs/extattr.h> 73 #include <ufs/ufs/quota.h> 74 #include <ufs/ufs/inode.h> 75 #include <ufs/ufs/ufsmount.h> 76 #include <ufs/ffs/fs.h> 77 #include <ufs/ffs/softdep.h> 78 #include <ufs/ffs/ffs_extern.h> 79 #include <ufs/ufs/ufs_extern.h> 80 81 #include <vm/vm.h> 82 83 #include <ddb/ddb.h> 84 85 #ifndef SOFTUPDATES 86 87 int 88 softdep_flushfiles(oldmnt, flags, td) 89 struct mount *oldmnt; 90 int flags; 91 struct thread *td; 92 { 93 94 panic("softdep_flushfiles called"); 95 } 96 97 int 98 softdep_mount(devvp, mp, fs, cred) 99 struct vnode *devvp; 100 struct mount *mp; 101 struct fs *fs; 102 struct ucred *cred; 103 { 104 105 return (0); 106 } 107 108 void 109 softdep_initialize() 110 { 111 112 return; 113 } 114 115 void 116 softdep_uninitialize() 117 { 118 119 return; 120 } 121 122 void 123 softdep_setup_inomapdep(bp, ip, newinum) 124 struct buf *bp; 125 struct inode *ip; 126 ino_t newinum; 127 { 128 129 panic("softdep_setup_inomapdep called"); 130 } 131 132 void 133 softdep_setup_blkmapdep(bp, mp, newblkno) 134 struct buf *bp; 135 struct mount *mp; 136 ufs2_daddr_t newblkno; 137 { 138 139 panic("softdep_setup_blkmapdep called"); 140 } 141 142 void 143 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 144 struct inode *ip; 145 ufs_lbn_t lbn; 146 ufs2_daddr_t newblkno; 147 ufs2_daddr_t oldblkno; 148 long newsize; 149 long oldsize; 150 struct buf *bp; 151 { 152 153 panic("softdep_setup_allocdirect called"); 154 } 155 156 void 157 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 158 struct inode *ip; 159 ufs_lbn_t lbn; 160 ufs2_daddr_t newblkno; 161 ufs2_daddr_t oldblkno; 162 long newsize; 163 long oldsize; 164 struct buf *bp; 165 { 166 167 panic("softdep_setup_allocext called"); 168 } 169 170 void 171 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 172 struct inode *ip; 173 ufs_lbn_t lbn; 174 struct buf *bp; 175 int ptrno; 176 ufs2_daddr_t newblkno; 177 ufs2_daddr_t oldblkno; 178 struct buf *nbp; 179 { 180 181 panic("softdep_setup_allocindir_page called"); 182 } 183 184 void 185 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 186 struct buf *nbp; 187 struct inode *ip; 188 struct buf *bp; 189 int ptrno; 190 ufs2_daddr_t newblkno; 191 { 192 193 panic("softdep_setup_allocindir_meta called"); 194 } 195 196 void 197 softdep_setup_freeblocks(ip, length, flags) 198 struct inode *ip; 199 off_t length; 200 int flags; 201 { 202 203 panic("softdep_setup_freeblocks called"); 204 } 205 206 void 207 softdep_freefile(pvp, ino, mode) 208 struct vnode *pvp; 209 ino_t ino; 210 int mode; 211 { 212 213 panic("softdep_freefile called"); 214 } 215 216 int 217 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 218 struct buf *bp; 219 struct inode *dp; 220 off_t diroffset; 221 ino_t newinum; 222 struct buf *newdirbp; 223 int isnewblk; 224 { 225 226 panic("softdep_setup_directory_add called"); 227 } 228 229 void 230 softdep_change_directoryentry_offset(dp, base, oldloc, newloc, entrysize) 231 struct inode *dp; 232 caddr_t base; 233 caddr_t oldloc; 234 caddr_t newloc; 235 int entrysize; 236 { 237 238 panic("softdep_change_directoryentry_offset called"); 239 } 240 241 void 242 softdep_setup_remove(bp, dp, ip, isrmdir) 243 struct buf *bp; 244 struct inode *dp; 245 struct inode *ip; 246 int isrmdir; 247 { 248 249 panic("softdep_setup_remove called"); 250 } 251 252 void 253 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 254 struct buf *bp; 255 struct inode *dp; 256 struct inode *ip; 257 ino_t newinum; 258 int isrmdir; 259 { 260 261 panic("softdep_setup_directory_change called"); 262 } 263 264 void 265 softdep_change_linkcnt(ip) 266 struct inode *ip; 267 { 268 269 panic("softdep_change_linkcnt called"); 270 } 271 272 void 273 softdep_load_inodeblock(ip) 274 struct inode *ip; 275 { 276 277 panic("softdep_load_inodeblock called"); 278 } 279 280 void 281 softdep_update_inodeblock(ip, bp, waitfor) 282 struct inode *ip; 283 struct buf *bp; 284 int waitfor; 285 { 286 287 panic("softdep_update_inodeblock called"); 288 } 289 290 int 291 softdep_fsync(vp) 292 struct vnode *vp; /* the "in_core" copy of the inode */ 293 { 294 295 return (0); 296 } 297 298 void 299 softdep_fsync_mountdev(vp) 300 struct vnode *vp; 301 { 302 303 return; 304 } 305 306 int 307 softdep_flushworklist(oldmnt, countp, td) 308 struct mount *oldmnt; 309 int *countp; 310 struct thread *td; 311 { 312 313 *countp = 0; 314 return (0); 315 } 316 317 int 318 softdep_sync_metadata(struct vnode *vp) 319 { 320 321 return (0); 322 } 323 324 int 325 softdep_slowdown(vp) 326 struct vnode *vp; 327 { 328 329 panic("softdep_slowdown called"); 330 } 331 332 void 333 softdep_releasefile(ip) 334 struct inode *ip; /* inode with the zero effective link count */ 335 { 336 337 panic("softdep_releasefile called"); 338 } 339 340 int 341 softdep_request_cleanup(fs, vp) 342 struct fs *fs; 343 struct vnode *vp; 344 { 345 346 return (0); 347 } 348 349 int 350 softdep_check_suspend(struct mount *mp, 351 struct vnode *devvp, 352 int softdep_deps, 353 int softdep_accdeps, 354 int secondary_writes, 355 int secondary_accwrites) 356 { 357 struct bufobj *bo; 358 int error; 359 360 (void) softdep_deps, 361 (void) softdep_accdeps; 362 363 bo = &devvp->v_bufobj; 364 ASSERT_BO_LOCKED(bo); 365 366 MNT_ILOCK(mp); 367 while (mp->mnt_secondary_writes != 0) { 368 BO_UNLOCK(bo); 369 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 370 (PUSER - 1) | PDROP, "secwr", 0); 371 BO_LOCK(bo); 372 MNT_ILOCK(mp); 373 } 374 375 /* 376 * Reasons for needing more work before suspend: 377 * - Dirty buffers on devvp. 378 * - Secondary writes occurred after start of vnode sync loop 379 */ 380 error = 0; 381 if (bo->bo_numoutput > 0 || 382 bo->bo_dirty.bv_cnt > 0 || 383 secondary_writes != 0 || 384 mp->mnt_secondary_writes != 0 || 385 secondary_accwrites != mp->mnt_secondary_accwrites) 386 error = EAGAIN; 387 BO_UNLOCK(bo); 388 return (error); 389 } 390 391 void 392 softdep_get_depcounts(struct mount *mp, 393 int *softdepactivep, 394 int *softdepactiveaccp) 395 { 396 (void) mp; 397 *softdepactivep = 0; 398 *softdepactiveaccp = 0; 399 } 400 401 #else 402 /* 403 * These definitions need to be adapted to the system to which 404 * this file is being ported. 405 */ 406 /* 407 * malloc types defined for the softdep system. 408 */ 409 static MALLOC_DEFINE(M_PAGEDEP, "pagedep","File page dependencies"); 410 static MALLOC_DEFINE(M_INODEDEP, "inodedep","Inode dependencies"); 411 static MALLOC_DEFINE(M_NEWBLK, "newblk","New block allocation"); 412 static MALLOC_DEFINE(M_BMSAFEMAP, "bmsafemap","Block or frag allocated from cyl group map"); 413 static MALLOC_DEFINE(M_ALLOCDIRECT, "allocdirect","Block or frag dependency for an inode"); 414 static MALLOC_DEFINE(M_INDIRDEP, "indirdep","Indirect block dependencies"); 415 static MALLOC_DEFINE(M_ALLOCINDIR, "allocindir","Block dependency for an indirect block"); 416 static MALLOC_DEFINE(M_FREEFRAG, "freefrag","Previously used frag for an inode"); 417 static MALLOC_DEFINE(M_FREEBLKS, "freeblks","Blocks freed from an inode"); 418 static MALLOC_DEFINE(M_FREEFILE, "freefile","Inode deallocated"); 419 static MALLOC_DEFINE(M_DIRADD, "diradd","New directory entry"); 420 static MALLOC_DEFINE(M_MKDIR, "mkdir","New directory"); 421 static MALLOC_DEFINE(M_DIRREM, "dirrem","Directory entry deleted"); 422 static MALLOC_DEFINE(M_NEWDIRBLK, "newdirblk","Unclaimed new directory block"); 423 static MALLOC_DEFINE(M_SAVEDINO, "savedino","Saved inodes"); 424 425 #define M_SOFTDEP_FLAGS (M_WAITOK | M_USE_RESERVE) 426 427 #define D_PAGEDEP 0 428 #define D_INODEDEP 1 429 #define D_NEWBLK 2 430 #define D_BMSAFEMAP 3 431 #define D_ALLOCDIRECT 4 432 #define D_INDIRDEP 5 433 #define D_ALLOCINDIR 6 434 #define D_FREEFRAG 7 435 #define D_FREEBLKS 8 436 #define D_FREEFILE 9 437 #define D_DIRADD 10 438 #define D_MKDIR 11 439 #define D_DIRREM 12 440 #define D_NEWDIRBLK 13 441 #define D_LAST D_NEWDIRBLK 442 443 /* 444 * translate from workitem type to memory type 445 * MUST match the defines above, such that memtype[D_XXX] == M_XXX 446 */ 447 static struct malloc_type *memtype[] = { 448 M_PAGEDEP, 449 M_INODEDEP, 450 M_NEWBLK, 451 M_BMSAFEMAP, 452 M_ALLOCDIRECT, 453 M_INDIRDEP, 454 M_ALLOCINDIR, 455 M_FREEFRAG, 456 M_FREEBLKS, 457 M_FREEFILE, 458 M_DIRADD, 459 M_MKDIR, 460 M_DIRREM, 461 M_NEWDIRBLK 462 }; 463 464 #define DtoM(type) (memtype[type]) 465 466 /* 467 * Names of malloc types. 468 */ 469 #define TYPENAME(type) \ 470 ((unsigned)(type) < D_LAST ? memtype[type]->ks_shortdesc : "???") 471 /* 472 * End system adaptation definitions. 473 */ 474 475 /* 476 * Forward declarations. 477 */ 478 struct inodedep_hashhead; 479 struct newblk_hashhead; 480 struct pagedep_hashhead; 481 482 /* 483 * Internal function prototypes. 484 */ 485 static void softdep_error(char *, int); 486 static void drain_output(struct vnode *); 487 static struct buf *getdirtybuf(struct buf *, struct mtx *, int); 488 static void clear_remove(struct thread *); 489 static void clear_inodedeps(struct thread *); 490 static int flush_pagedep_deps(struct vnode *, struct mount *, 491 struct diraddhd *); 492 static int flush_inodedep_deps(struct mount *, ino_t); 493 static int flush_deplist(struct allocdirectlst *, int, int *); 494 static int handle_written_filepage(struct pagedep *, struct buf *); 495 static void diradd_inode_written(struct diradd *, struct inodedep *); 496 static int handle_written_inodeblock(struct inodedep *, struct buf *); 497 static void handle_allocdirect_partdone(struct allocdirect *); 498 static void handle_allocindir_partdone(struct allocindir *); 499 static void initiate_write_filepage(struct pagedep *, struct buf *); 500 static void handle_written_mkdir(struct mkdir *, int); 501 static void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *); 502 static void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *); 503 static void handle_workitem_freefile(struct freefile *); 504 static void handle_workitem_remove(struct dirrem *, struct vnode *); 505 static struct dirrem *newdirrem(struct buf *, struct inode *, 506 struct inode *, int, struct dirrem **); 507 static void free_diradd(struct diradd *); 508 static void free_allocindir(struct allocindir *, struct inodedep *); 509 static void free_newdirblk(struct newdirblk *); 510 static int indir_trunc(struct freeblks *, ufs2_daddr_t, int, ufs_lbn_t, 511 ufs2_daddr_t *); 512 static void deallocate_dependencies(struct buf *, struct inodedep *); 513 static void free_allocdirect(struct allocdirectlst *, 514 struct allocdirect *, int); 515 static int check_inode_unwritten(struct inodedep *); 516 static int free_inodedep(struct inodedep *); 517 static void handle_workitem_freeblocks(struct freeblks *, int); 518 static void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *); 519 static void setup_allocindir_phase2(struct buf *, struct inode *, 520 struct allocindir *); 521 static struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t, 522 ufs2_daddr_t); 523 static void handle_workitem_freefrag(struct freefrag *); 524 static struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long); 525 static void allocdirect_merge(struct allocdirectlst *, 526 struct allocdirect *, struct allocdirect *); 527 static struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *); 528 static int newblk_find(struct newblk_hashhead *, struct fs *, ufs2_daddr_t, 529 struct newblk **); 530 static int newblk_lookup(struct fs *, ufs2_daddr_t, int, struct newblk **); 531 static int inodedep_find(struct inodedep_hashhead *, struct fs *, ino_t, 532 struct inodedep **); 533 static int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **); 534 static int pagedep_lookup(struct inode *, ufs_lbn_t, int, struct pagedep **); 535 static int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t, 536 struct mount *mp, int, struct pagedep **); 537 static void pause_timer(void *); 538 static int request_cleanup(struct mount *, int); 539 static int process_worklist_item(struct mount *, int); 540 static void add_to_worklist(struct worklist *); 541 static void softdep_flush(void); 542 static int softdep_speedup(void); 543 544 /* 545 * Exported softdep operations. 546 */ 547 static void softdep_disk_io_initiation(struct buf *); 548 static void softdep_disk_write_complete(struct buf *); 549 static void softdep_deallocate_dependencies(struct buf *); 550 static int softdep_count_dependencies(struct buf *bp, int); 551 552 static struct mtx lk; 553 MTX_SYSINIT(softdep_lock, &lk, "Softdep Lock", MTX_DEF); 554 555 #define TRY_ACQUIRE_LOCK(lk) mtx_trylock(lk) 556 #define ACQUIRE_LOCK(lk) mtx_lock(lk) 557 #define FREE_LOCK(lk) mtx_unlock(lk) 558 559 #define BUF_AREC(bp) ((bp)->b_lock.lock_object.lo_flags |= LK_CANRECURSE) 560 #define BUF_NOREC(bp) ((bp)->b_lock.lock_object.lo_flags &= ~LK_CANRECURSE) 561 562 /* 563 * Worklist queue management. 564 * These routines require that the lock be held. 565 */ 566 #ifndef /* NOT */ DEBUG 567 #define WORKLIST_INSERT(head, item) do { \ 568 (item)->wk_state |= ONWORKLIST; \ 569 LIST_INSERT_HEAD(head, item, wk_list); \ 570 } while (0) 571 #define WORKLIST_REMOVE(item) do { \ 572 (item)->wk_state &= ~ONWORKLIST; \ 573 LIST_REMOVE(item, wk_list); \ 574 } while (0) 575 #else /* DEBUG */ 576 static void worklist_insert(struct workhead *, struct worklist *); 577 static void worklist_remove(struct worklist *); 578 579 #define WORKLIST_INSERT(head, item) worklist_insert(head, item) 580 #define WORKLIST_REMOVE(item) worklist_remove(item) 581 582 static void 583 worklist_insert(head, item) 584 struct workhead *head; 585 struct worklist *item; 586 { 587 588 mtx_assert(&lk, MA_OWNED); 589 if (item->wk_state & ONWORKLIST) 590 panic("worklist_insert: already on list"); 591 item->wk_state |= ONWORKLIST; 592 LIST_INSERT_HEAD(head, item, wk_list); 593 } 594 595 static void 596 worklist_remove(item) 597 struct worklist *item; 598 { 599 600 mtx_assert(&lk, MA_OWNED); 601 if ((item->wk_state & ONWORKLIST) == 0) 602 panic("worklist_remove: not on list"); 603 item->wk_state &= ~ONWORKLIST; 604 LIST_REMOVE(item, wk_list); 605 } 606 #endif /* DEBUG */ 607 608 /* 609 * Routines for tracking and managing workitems. 610 */ 611 static void workitem_free(struct worklist *, int); 612 static void workitem_alloc(struct worklist *, int, struct mount *); 613 614 #define WORKITEM_FREE(item, type) workitem_free((struct worklist *)(item), (type)) 615 616 static void 617 workitem_free(item, type) 618 struct worklist *item; 619 int type; 620 { 621 struct ufsmount *ump; 622 mtx_assert(&lk, MA_OWNED); 623 624 #ifdef DEBUG 625 if (item->wk_state & ONWORKLIST) 626 panic("workitem_free: still on list"); 627 if (item->wk_type != type) 628 panic("workitem_free: type mismatch"); 629 #endif 630 ump = VFSTOUFS(item->wk_mp); 631 if (--ump->softdep_deps == 0 && ump->softdep_req) 632 wakeup(&ump->softdep_deps); 633 free(item, DtoM(type)); 634 } 635 636 static void 637 workitem_alloc(item, type, mp) 638 struct worklist *item; 639 int type; 640 struct mount *mp; 641 { 642 item->wk_type = type; 643 item->wk_mp = mp; 644 item->wk_state = 0; 645 ACQUIRE_LOCK(&lk); 646 VFSTOUFS(mp)->softdep_deps++; 647 VFSTOUFS(mp)->softdep_accdeps++; 648 FREE_LOCK(&lk); 649 } 650 651 /* 652 * Workitem queue management 653 */ 654 static int max_softdeps; /* maximum number of structs before slowdown */ 655 static int maxindirdeps = 50; /* max number of indirdeps before slowdown */ 656 static int tickdelay = 2; /* number of ticks to pause during slowdown */ 657 static int proc_waiting; /* tracks whether we have a timeout posted */ 658 static int *stat_countp; /* statistic to count in proc_waiting timeout */ 659 static struct callout softdep_callout; 660 static int req_pending; 661 static int req_clear_inodedeps; /* syncer process flush some inodedeps */ 662 #define FLUSH_INODES 1 663 static int req_clear_remove; /* syncer process flush some freeblks */ 664 #define FLUSH_REMOVE 2 665 #define FLUSH_REMOVE_WAIT 3 666 /* 667 * runtime statistics 668 */ 669 static int stat_worklist_push; /* number of worklist cleanups */ 670 static int stat_blk_limit_push; /* number of times block limit neared */ 671 static int stat_ino_limit_push; /* number of times inode limit neared */ 672 static int stat_blk_limit_hit; /* number of times block slowdown imposed */ 673 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */ 674 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */ 675 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */ 676 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */ 677 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */ 678 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */ 679 680 SYSCTL_INT(_debug, OID_AUTO, max_softdeps, CTLFLAG_RW, &max_softdeps, 0, ""); 681 SYSCTL_INT(_debug, OID_AUTO, tickdelay, CTLFLAG_RW, &tickdelay, 0, ""); 682 SYSCTL_INT(_debug, OID_AUTO, maxindirdeps, CTLFLAG_RW, &maxindirdeps, 0, ""); 683 SYSCTL_INT(_debug, OID_AUTO, worklist_push, CTLFLAG_RW, &stat_worklist_push, 0,""); 684 SYSCTL_INT(_debug, OID_AUTO, blk_limit_push, CTLFLAG_RW, &stat_blk_limit_push, 0,""); 685 SYSCTL_INT(_debug, OID_AUTO, ino_limit_push, CTLFLAG_RW, &stat_ino_limit_push, 0,""); 686 SYSCTL_INT(_debug, OID_AUTO, blk_limit_hit, CTLFLAG_RW, &stat_blk_limit_hit, 0, ""); 687 SYSCTL_INT(_debug, OID_AUTO, ino_limit_hit, CTLFLAG_RW, &stat_ino_limit_hit, 0, ""); 688 SYSCTL_INT(_debug, OID_AUTO, sync_limit_hit, CTLFLAG_RW, &stat_sync_limit_hit, 0, ""); 689 SYSCTL_INT(_debug, OID_AUTO, indir_blk_ptrs, CTLFLAG_RW, &stat_indir_blk_ptrs, 0, ""); 690 SYSCTL_INT(_debug, OID_AUTO, inode_bitmap, CTLFLAG_RW, &stat_inode_bitmap, 0, ""); 691 SYSCTL_INT(_debug, OID_AUTO, direct_blk_ptrs, CTLFLAG_RW, &stat_direct_blk_ptrs, 0, ""); 692 SYSCTL_INT(_debug, OID_AUTO, dir_entry, CTLFLAG_RW, &stat_dir_entry, 0, ""); 693 /* SYSCTL_INT(_debug, OID_AUTO, worklist_num, CTLFLAG_RD, &softdep_on_worklist, 0, ""); */ 694 695 SYSCTL_DECL(_vfs_ffs); 696 697 static int compute_summary_at_mount = 0; /* Whether to recompute the summary at mount time */ 698 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW, 699 &compute_summary_at_mount, 0, "Recompute summary at mount"); 700 701 static struct proc *softdepproc; 702 static struct kproc_desc softdep_kp = { 703 "softdepflush", 704 softdep_flush, 705 &softdepproc 706 }; 707 SYSINIT(sdproc, SI_SUB_KTHREAD_UPDATE, SI_ORDER_ANY, kproc_start, 708 &softdep_kp); 709 710 static void 711 softdep_flush(void) 712 { 713 struct mount *nmp; 714 struct mount *mp; 715 struct ufsmount *ump; 716 struct thread *td; 717 int remaining; 718 int vfslocked; 719 720 td = curthread; 721 td->td_pflags |= TDP_NORUNNINGBUF; 722 723 for (;;) { 724 kproc_suspend_check(softdepproc); 725 vfslocked = VFS_LOCK_GIANT((struct mount *)NULL); 726 ACQUIRE_LOCK(&lk); 727 /* 728 * If requested, try removing inode or removal dependencies. 729 */ 730 if (req_clear_inodedeps) { 731 clear_inodedeps(td); 732 req_clear_inodedeps -= 1; 733 wakeup_one(&proc_waiting); 734 } 735 if (req_clear_remove) { 736 clear_remove(td); 737 req_clear_remove -= 1; 738 wakeup_one(&proc_waiting); 739 } 740 FREE_LOCK(&lk); 741 VFS_UNLOCK_GIANT(vfslocked); 742 remaining = 0; 743 mtx_lock(&mountlist_mtx); 744 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 745 nmp = TAILQ_NEXT(mp, mnt_list); 746 if ((mp->mnt_flag & MNT_SOFTDEP) == 0) 747 continue; 748 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) 749 continue; 750 vfslocked = VFS_LOCK_GIANT(mp); 751 softdep_process_worklist(mp, 0); 752 ump = VFSTOUFS(mp); 753 remaining += ump->softdep_on_worklist - 754 ump->softdep_on_worklist_inprogress; 755 VFS_UNLOCK_GIANT(vfslocked); 756 mtx_lock(&mountlist_mtx); 757 nmp = TAILQ_NEXT(mp, mnt_list); 758 vfs_unbusy(mp); 759 } 760 mtx_unlock(&mountlist_mtx); 761 if (remaining) 762 continue; 763 ACQUIRE_LOCK(&lk); 764 if (!req_pending) 765 msleep(&req_pending, &lk, PVM, "sdflush", hz); 766 req_pending = 0; 767 FREE_LOCK(&lk); 768 } 769 } 770 771 static int 772 softdep_speedup(void) 773 { 774 775 mtx_assert(&lk, MA_OWNED); 776 if (req_pending == 0) { 777 req_pending = 1; 778 wakeup(&req_pending); 779 } 780 781 return speedup_syncer(); 782 } 783 784 /* 785 * Add an item to the end of the work queue. 786 * This routine requires that the lock be held. 787 * This is the only routine that adds items to the list. 788 * The following routine is the only one that removes items 789 * and does so in order from first to last. 790 */ 791 static void 792 add_to_worklist(wk) 793 struct worklist *wk; 794 { 795 struct ufsmount *ump; 796 797 mtx_assert(&lk, MA_OWNED); 798 ump = VFSTOUFS(wk->wk_mp); 799 if (wk->wk_state & ONWORKLIST) 800 panic("add_to_worklist: already on list"); 801 wk->wk_state |= ONWORKLIST; 802 if (LIST_EMPTY(&ump->softdep_workitem_pending)) 803 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 804 else 805 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list); 806 ump->softdep_worklist_tail = wk; 807 ump->softdep_on_worklist += 1; 808 } 809 810 /* 811 * Process that runs once per second to handle items in the background queue. 812 * 813 * Note that we ensure that everything is done in the order in which they 814 * appear in the queue. The code below depends on this property to ensure 815 * that blocks of a file are freed before the inode itself is freed. This 816 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated 817 * until all the old ones have been purged from the dependency lists. 818 */ 819 int 820 softdep_process_worklist(mp, full) 821 struct mount *mp; 822 int full; 823 { 824 struct thread *td = curthread; 825 int cnt, matchcnt, loopcount; 826 struct ufsmount *ump; 827 long starttime; 828 829 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp")); 830 /* 831 * Record the process identifier of our caller so that we can give 832 * this process preferential treatment in request_cleanup below. 833 */ 834 matchcnt = 0; 835 ump = VFSTOUFS(mp); 836 ACQUIRE_LOCK(&lk); 837 loopcount = 1; 838 starttime = time_second; 839 while (ump->softdep_on_worklist > 0) { 840 if ((cnt = process_worklist_item(mp, 0)) == -1) 841 break; 842 else 843 matchcnt += cnt; 844 /* 845 * If requested, try removing inode or removal dependencies. 846 */ 847 if (req_clear_inodedeps) { 848 clear_inodedeps(td); 849 req_clear_inodedeps -= 1; 850 wakeup_one(&proc_waiting); 851 } 852 if (req_clear_remove) { 853 clear_remove(td); 854 req_clear_remove -= 1; 855 wakeup_one(&proc_waiting); 856 } 857 /* 858 * We do not generally want to stop for buffer space, but if 859 * we are really being a buffer hog, we will stop and wait. 860 */ 861 if (loopcount++ % 128 == 0) { 862 FREE_LOCK(&lk); 863 uio_yield(); 864 bwillwrite(); 865 ACQUIRE_LOCK(&lk); 866 } 867 /* 868 * Never allow processing to run for more than one 869 * second. Otherwise the other mountpoints may get 870 * excessively backlogged. 871 */ 872 if (!full && starttime != time_second) { 873 matchcnt = -1; 874 break; 875 } 876 } 877 FREE_LOCK(&lk); 878 return (matchcnt); 879 } 880 881 /* 882 * Process one item on the worklist. 883 */ 884 static int 885 process_worklist_item(mp, flags) 886 struct mount *mp; 887 int flags; 888 { 889 struct worklist *wk, *wkend; 890 struct ufsmount *ump; 891 struct vnode *vp; 892 int matchcnt = 0; 893 894 mtx_assert(&lk, MA_OWNED); 895 KASSERT(mp != NULL, ("process_worklist_item: NULL mp")); 896 /* 897 * If we are being called because of a process doing a 898 * copy-on-write, then it is not safe to write as we may 899 * recurse into the copy-on-write routine. 900 */ 901 if (curthread->td_pflags & TDP_COWINPROGRESS) 902 return (-1); 903 /* 904 * Normally we just process each item on the worklist in order. 905 * However, if we are in a situation where we cannot lock any 906 * inodes, we have to skip over any dirrem requests whose 907 * vnodes are resident and locked. 908 */ 909 ump = VFSTOUFS(mp); 910 vp = NULL; 911 LIST_FOREACH(wk, &ump->softdep_workitem_pending, wk_list) { 912 if (wk->wk_state & INPROGRESS) 913 continue; 914 if ((flags & LK_NOWAIT) == 0 || wk->wk_type != D_DIRREM) 915 break; 916 wk->wk_state |= INPROGRESS; 917 ump->softdep_on_worklist_inprogress++; 918 FREE_LOCK(&lk); 919 ffs_vgetf(mp, WK_DIRREM(wk)->dm_oldinum, 920 LK_NOWAIT | LK_EXCLUSIVE, &vp, FFSV_FORCEINSMQ); 921 ACQUIRE_LOCK(&lk); 922 wk->wk_state &= ~INPROGRESS; 923 ump->softdep_on_worklist_inprogress--; 924 if (vp != NULL) 925 break; 926 } 927 if (wk == 0) 928 return (-1); 929 /* 930 * Remove the item to be processed. If we are removing the last 931 * item on the list, we need to recalculate the tail pointer. 932 * As this happens rarely and usually when the list is short, 933 * we just run down the list to find it rather than tracking it 934 * in the above loop. 935 */ 936 WORKLIST_REMOVE(wk); 937 if (wk == ump->softdep_worklist_tail) { 938 LIST_FOREACH(wkend, &ump->softdep_workitem_pending, wk_list) 939 if (LIST_NEXT(wkend, wk_list) == NULL) 940 break; 941 ump->softdep_worklist_tail = wkend; 942 } 943 ump->softdep_on_worklist -= 1; 944 FREE_LOCK(&lk); 945 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 946 panic("process_worklist_item: suspended filesystem"); 947 matchcnt++; 948 switch (wk->wk_type) { 949 950 case D_DIRREM: 951 /* removal of a directory entry */ 952 handle_workitem_remove(WK_DIRREM(wk), vp); 953 break; 954 955 case D_FREEBLKS: 956 /* releasing blocks and/or fragments from a file */ 957 handle_workitem_freeblocks(WK_FREEBLKS(wk), flags & LK_NOWAIT); 958 break; 959 960 case D_FREEFRAG: 961 /* releasing a fragment when replaced as a file grows */ 962 handle_workitem_freefrag(WK_FREEFRAG(wk)); 963 break; 964 965 case D_FREEFILE: 966 /* releasing an inode when its link count drops to 0 */ 967 handle_workitem_freefile(WK_FREEFILE(wk)); 968 break; 969 970 default: 971 panic("%s_process_worklist: Unknown type %s", 972 "softdep", TYPENAME(wk->wk_type)); 973 /* NOTREACHED */ 974 } 975 vn_finished_secondary_write(mp); 976 ACQUIRE_LOCK(&lk); 977 return (matchcnt); 978 } 979 980 /* 981 * Move dependencies from one buffer to another. 982 */ 983 void 984 softdep_move_dependencies(oldbp, newbp) 985 struct buf *oldbp; 986 struct buf *newbp; 987 { 988 struct worklist *wk, *wktail; 989 990 if (!LIST_EMPTY(&newbp->b_dep)) 991 panic("softdep_move_dependencies: need merge code"); 992 wktail = 0; 993 ACQUIRE_LOCK(&lk); 994 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) { 995 LIST_REMOVE(wk, wk_list); 996 if (wktail == 0) 997 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list); 998 else 999 LIST_INSERT_AFTER(wktail, wk, wk_list); 1000 wktail = wk; 1001 } 1002 FREE_LOCK(&lk); 1003 } 1004 1005 /* 1006 * Purge the work list of all items associated with a particular mount point. 1007 */ 1008 int 1009 softdep_flushworklist(oldmnt, countp, td) 1010 struct mount *oldmnt; 1011 int *countp; 1012 struct thread *td; 1013 { 1014 struct vnode *devvp; 1015 int count, error = 0; 1016 struct ufsmount *ump; 1017 1018 /* 1019 * Alternately flush the block device associated with the mount 1020 * point and process any dependencies that the flushing 1021 * creates. We continue until no more worklist dependencies 1022 * are found. 1023 */ 1024 *countp = 0; 1025 ump = VFSTOUFS(oldmnt); 1026 devvp = ump->um_devvp; 1027 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) { 1028 *countp += count; 1029 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1030 error = VOP_FSYNC(devvp, MNT_WAIT, td); 1031 VOP_UNLOCK(devvp, 0); 1032 if (error) 1033 break; 1034 } 1035 return (error); 1036 } 1037 1038 int 1039 softdep_waitidle(struct mount *mp) 1040 { 1041 struct ufsmount *ump; 1042 int error; 1043 int i; 1044 1045 ump = VFSTOUFS(mp); 1046 ACQUIRE_LOCK(&lk); 1047 for (i = 0; i < 10 && ump->softdep_deps; i++) { 1048 ump->softdep_req = 1; 1049 if (ump->softdep_on_worklist) 1050 panic("softdep_waitidle: work added after flush."); 1051 msleep(&ump->softdep_deps, &lk, PVM, "softdeps", 1); 1052 } 1053 ump->softdep_req = 0; 1054 FREE_LOCK(&lk); 1055 error = 0; 1056 if (i == 10) { 1057 error = EBUSY; 1058 printf("softdep_waitidle: Failed to flush worklist for %p\n", 1059 mp); 1060 } 1061 1062 return (error); 1063 } 1064 1065 /* 1066 * Flush all vnodes and worklist items associated with a specified mount point. 1067 */ 1068 int 1069 softdep_flushfiles(oldmnt, flags, td) 1070 struct mount *oldmnt; 1071 int flags; 1072 struct thread *td; 1073 { 1074 int error, depcount, loopcnt, retry_flush_count, retry; 1075 1076 loopcnt = 10; 1077 retry_flush_count = 3; 1078 retry_flush: 1079 error = 0; 1080 1081 /* 1082 * Alternately flush the vnodes associated with the mount 1083 * point and process any dependencies that the flushing 1084 * creates. In theory, this loop can happen at most twice, 1085 * but we give it a few extra just to be sure. 1086 */ 1087 for (; loopcnt > 0; loopcnt--) { 1088 /* 1089 * Do another flush in case any vnodes were brought in 1090 * as part of the cleanup operations. 1091 */ 1092 if ((error = ffs_flushfiles(oldmnt, flags, td)) != 0) 1093 break; 1094 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 || 1095 depcount == 0) 1096 break; 1097 } 1098 /* 1099 * If we are unmounting then it is an error to fail. If we 1100 * are simply trying to downgrade to read-only, then filesystem 1101 * activity can keep us busy forever, so we just fail with EBUSY. 1102 */ 1103 if (loopcnt == 0) { 1104 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) 1105 panic("softdep_flushfiles: looping"); 1106 error = EBUSY; 1107 } 1108 if (!error) 1109 error = softdep_waitidle(oldmnt); 1110 if (!error) { 1111 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) { 1112 retry = 0; 1113 MNT_ILOCK(oldmnt); 1114 KASSERT((oldmnt->mnt_kern_flag & MNTK_NOINSMNTQ) != 0, 1115 ("softdep_flushfiles: !MNTK_NOINSMNTQ")); 1116 if (oldmnt->mnt_nvnodelistsize > 0) { 1117 if (--retry_flush_count > 0) { 1118 retry = 1; 1119 loopcnt = 3; 1120 } else 1121 error = EBUSY; 1122 } 1123 MNT_IUNLOCK(oldmnt); 1124 if (retry) 1125 goto retry_flush; 1126 } 1127 } 1128 return (error); 1129 } 1130 1131 /* 1132 * Structure hashing. 1133 * 1134 * There are three types of structures that can be looked up: 1135 * 1) pagedep structures identified by mount point, inode number, 1136 * and logical block. 1137 * 2) inodedep structures identified by mount point and inode number. 1138 * 3) newblk structures identified by mount point and 1139 * physical block number. 1140 * 1141 * The "pagedep" and "inodedep" dependency structures are hashed 1142 * separately from the file blocks and inodes to which they correspond. 1143 * This separation helps when the in-memory copy of an inode or 1144 * file block must be replaced. It also obviates the need to access 1145 * an inode or file page when simply updating (or de-allocating) 1146 * dependency structures. Lookup of newblk structures is needed to 1147 * find newly allocated blocks when trying to associate them with 1148 * their allocdirect or allocindir structure. 1149 * 1150 * The lookup routines optionally create and hash a new instance when 1151 * an existing entry is not found. 1152 */ 1153 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */ 1154 #define NODELAY 0x0002 /* cannot do background work */ 1155 1156 /* 1157 * Structures and routines associated with pagedep caching. 1158 */ 1159 LIST_HEAD(pagedep_hashhead, pagedep) *pagedep_hashtbl; 1160 u_long pagedep_hash; /* size of hash table - 1 */ 1161 #define PAGEDEP_HASH(mp, inum, lbn) \ 1162 (&pagedep_hashtbl[((((register_t)(mp)) >> 13) + (inum) + (lbn)) & \ 1163 pagedep_hash]) 1164 1165 static int 1166 pagedep_find(pagedephd, ino, lbn, mp, flags, pagedeppp) 1167 struct pagedep_hashhead *pagedephd; 1168 ino_t ino; 1169 ufs_lbn_t lbn; 1170 struct mount *mp; 1171 int flags; 1172 struct pagedep **pagedeppp; 1173 { 1174 struct pagedep *pagedep; 1175 1176 LIST_FOREACH(pagedep, pagedephd, pd_hash) 1177 if (ino == pagedep->pd_ino && 1178 lbn == pagedep->pd_lbn && 1179 mp == pagedep->pd_list.wk_mp) 1180 break; 1181 if (pagedep) { 1182 *pagedeppp = pagedep; 1183 if ((flags & DEPALLOC) != 0 && 1184 (pagedep->pd_state & ONWORKLIST) == 0) 1185 return (0); 1186 return (1); 1187 } 1188 *pagedeppp = NULL; 1189 return (0); 1190 } 1191 /* 1192 * Look up a pagedep. Return 1 if found, 0 if not found or found 1193 * when asked to allocate but not associated with any buffer. 1194 * If not found, allocate if DEPALLOC flag is passed. 1195 * Found or allocated entry is returned in pagedeppp. 1196 * This routine must be called with splbio interrupts blocked. 1197 */ 1198 static int 1199 pagedep_lookup(ip, lbn, flags, pagedeppp) 1200 struct inode *ip; 1201 ufs_lbn_t lbn; 1202 int flags; 1203 struct pagedep **pagedeppp; 1204 { 1205 struct pagedep *pagedep; 1206 struct pagedep_hashhead *pagedephd; 1207 struct mount *mp; 1208 int ret; 1209 int i; 1210 1211 mtx_assert(&lk, MA_OWNED); 1212 mp = ITOV(ip)->v_mount; 1213 pagedephd = PAGEDEP_HASH(mp, ip->i_number, lbn); 1214 1215 ret = pagedep_find(pagedephd, ip->i_number, lbn, mp, flags, pagedeppp); 1216 if (*pagedeppp || (flags & DEPALLOC) == 0) 1217 return (ret); 1218 FREE_LOCK(&lk); 1219 pagedep = malloc(sizeof(struct pagedep), 1220 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO); 1221 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp); 1222 ACQUIRE_LOCK(&lk); 1223 ret = pagedep_find(pagedephd, ip->i_number, lbn, mp, flags, pagedeppp); 1224 if (*pagedeppp) { 1225 WORKITEM_FREE(pagedep, D_PAGEDEP); 1226 return (ret); 1227 } 1228 pagedep->pd_ino = ip->i_number; 1229 pagedep->pd_lbn = lbn; 1230 LIST_INIT(&pagedep->pd_dirremhd); 1231 LIST_INIT(&pagedep->pd_pendinghd); 1232 for (i = 0; i < DAHASHSZ; i++) 1233 LIST_INIT(&pagedep->pd_diraddhd[i]); 1234 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash); 1235 *pagedeppp = pagedep; 1236 return (0); 1237 } 1238 1239 /* 1240 * Structures and routines associated with inodedep caching. 1241 */ 1242 LIST_HEAD(inodedep_hashhead, inodedep) *inodedep_hashtbl; 1243 static u_long inodedep_hash; /* size of hash table - 1 */ 1244 static long num_inodedep; /* number of inodedep allocated */ 1245 #define INODEDEP_HASH(fs, inum) \ 1246 (&inodedep_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & inodedep_hash]) 1247 1248 static int 1249 inodedep_find(inodedephd, fs, inum, inodedeppp) 1250 struct inodedep_hashhead *inodedephd; 1251 struct fs *fs; 1252 ino_t inum; 1253 struct inodedep **inodedeppp; 1254 { 1255 struct inodedep *inodedep; 1256 1257 LIST_FOREACH(inodedep, inodedephd, id_hash) 1258 if (inum == inodedep->id_ino && fs == inodedep->id_fs) 1259 break; 1260 if (inodedep) { 1261 *inodedeppp = inodedep; 1262 return (1); 1263 } 1264 *inodedeppp = NULL; 1265 1266 return (0); 1267 } 1268 /* 1269 * Look up an inodedep. Return 1 if found, 0 if not found. 1270 * If not found, allocate if DEPALLOC flag is passed. 1271 * Found or allocated entry is returned in inodedeppp. 1272 * This routine must be called with splbio interrupts blocked. 1273 */ 1274 static int 1275 inodedep_lookup(mp, inum, flags, inodedeppp) 1276 struct mount *mp; 1277 ino_t inum; 1278 int flags; 1279 struct inodedep **inodedeppp; 1280 { 1281 struct inodedep *inodedep; 1282 struct inodedep_hashhead *inodedephd; 1283 struct fs *fs; 1284 1285 mtx_assert(&lk, MA_OWNED); 1286 fs = VFSTOUFS(mp)->um_fs; 1287 inodedephd = INODEDEP_HASH(fs, inum); 1288 1289 if (inodedep_find(inodedephd, fs, inum, inodedeppp)) 1290 return (1); 1291 if ((flags & DEPALLOC) == 0) 1292 return (0); 1293 /* 1294 * If we are over our limit, try to improve the situation. 1295 */ 1296 if (num_inodedep > max_softdeps && (flags & NODELAY) == 0) 1297 request_cleanup(mp, FLUSH_INODES); 1298 FREE_LOCK(&lk); 1299 inodedep = malloc(sizeof(struct inodedep), 1300 M_INODEDEP, M_SOFTDEP_FLAGS); 1301 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp); 1302 ACQUIRE_LOCK(&lk); 1303 if (inodedep_find(inodedephd, fs, inum, inodedeppp)) { 1304 WORKITEM_FREE(inodedep, D_INODEDEP); 1305 return (1); 1306 } 1307 num_inodedep += 1; 1308 inodedep->id_fs = fs; 1309 inodedep->id_ino = inum; 1310 inodedep->id_state = ALLCOMPLETE; 1311 inodedep->id_nlinkdelta = 0; 1312 inodedep->id_savedino1 = NULL; 1313 inodedep->id_savedsize = -1; 1314 inodedep->id_savedextsize = -1; 1315 inodedep->id_buf = NULL; 1316 LIST_INIT(&inodedep->id_pendinghd); 1317 LIST_INIT(&inodedep->id_inowait); 1318 LIST_INIT(&inodedep->id_bufwait); 1319 TAILQ_INIT(&inodedep->id_inoupdt); 1320 TAILQ_INIT(&inodedep->id_newinoupdt); 1321 TAILQ_INIT(&inodedep->id_extupdt); 1322 TAILQ_INIT(&inodedep->id_newextupdt); 1323 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash); 1324 *inodedeppp = inodedep; 1325 return (0); 1326 } 1327 1328 /* 1329 * Structures and routines associated with newblk caching. 1330 */ 1331 LIST_HEAD(newblk_hashhead, newblk) *newblk_hashtbl; 1332 u_long newblk_hash; /* size of hash table - 1 */ 1333 #define NEWBLK_HASH(fs, inum) \ 1334 (&newblk_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & newblk_hash]) 1335 1336 static int 1337 newblk_find(newblkhd, fs, newblkno, newblkpp) 1338 struct newblk_hashhead *newblkhd; 1339 struct fs *fs; 1340 ufs2_daddr_t newblkno; 1341 struct newblk **newblkpp; 1342 { 1343 struct newblk *newblk; 1344 1345 LIST_FOREACH(newblk, newblkhd, nb_hash) 1346 if (newblkno == newblk->nb_newblkno && fs == newblk->nb_fs) 1347 break; 1348 if (newblk) { 1349 *newblkpp = newblk; 1350 return (1); 1351 } 1352 *newblkpp = NULL; 1353 return (0); 1354 } 1355 1356 /* 1357 * Look up a newblk. Return 1 if found, 0 if not found. 1358 * If not found, allocate if DEPALLOC flag is passed. 1359 * Found or allocated entry is returned in newblkpp. 1360 */ 1361 static int 1362 newblk_lookup(fs, newblkno, flags, newblkpp) 1363 struct fs *fs; 1364 ufs2_daddr_t newblkno; 1365 int flags; 1366 struct newblk **newblkpp; 1367 { 1368 struct newblk *newblk; 1369 struct newblk_hashhead *newblkhd; 1370 1371 newblkhd = NEWBLK_HASH(fs, newblkno); 1372 if (newblk_find(newblkhd, fs, newblkno, newblkpp)) 1373 return (1); 1374 if ((flags & DEPALLOC) == 0) 1375 return (0); 1376 FREE_LOCK(&lk); 1377 newblk = malloc(sizeof(struct newblk), 1378 M_NEWBLK, M_SOFTDEP_FLAGS); 1379 ACQUIRE_LOCK(&lk); 1380 if (newblk_find(newblkhd, fs, newblkno, newblkpp)) { 1381 free(newblk, M_NEWBLK); 1382 return (1); 1383 } 1384 newblk->nb_state = 0; 1385 newblk->nb_fs = fs; 1386 newblk->nb_newblkno = newblkno; 1387 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash); 1388 *newblkpp = newblk; 1389 return (0); 1390 } 1391 1392 /* 1393 * Executed during filesystem system initialization before 1394 * mounting any filesystems. 1395 */ 1396 void 1397 softdep_initialize() 1398 { 1399 1400 LIST_INIT(&mkdirlisthd); 1401 max_softdeps = desiredvnodes * 4; 1402 pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP, 1403 &pagedep_hash); 1404 inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP, &inodedep_hash); 1405 newblk_hashtbl = hashinit(64, M_NEWBLK, &newblk_hash); 1406 1407 /* initialise bioops hack */ 1408 bioops.io_start = softdep_disk_io_initiation; 1409 bioops.io_complete = softdep_disk_write_complete; 1410 bioops.io_deallocate = softdep_deallocate_dependencies; 1411 bioops.io_countdeps = softdep_count_dependencies; 1412 1413 /* Initialize the callout with an mtx. */ 1414 callout_init_mtx(&softdep_callout, &lk, 0); 1415 } 1416 1417 /* 1418 * Executed after all filesystems have been unmounted during 1419 * filesystem module unload. 1420 */ 1421 void 1422 softdep_uninitialize() 1423 { 1424 1425 callout_drain(&softdep_callout); 1426 hashdestroy(pagedep_hashtbl, M_PAGEDEP, pagedep_hash); 1427 hashdestroy(inodedep_hashtbl, M_INODEDEP, inodedep_hash); 1428 hashdestroy(newblk_hashtbl, M_NEWBLK, newblk_hash); 1429 } 1430 1431 /* 1432 * Called at mount time to notify the dependency code that a 1433 * filesystem wishes to use it. 1434 */ 1435 int 1436 softdep_mount(devvp, mp, fs, cred) 1437 struct vnode *devvp; 1438 struct mount *mp; 1439 struct fs *fs; 1440 struct ucred *cred; 1441 { 1442 struct csum_total cstotal; 1443 struct ufsmount *ump; 1444 struct cg *cgp; 1445 struct buf *bp; 1446 int error, cyl; 1447 1448 MNT_ILOCK(mp); 1449 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP; 1450 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) { 1451 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) | 1452 MNTK_SOFTDEP; 1453 mp->mnt_noasync++; 1454 } 1455 MNT_IUNLOCK(mp); 1456 ump = VFSTOUFS(mp); 1457 LIST_INIT(&ump->softdep_workitem_pending); 1458 ump->softdep_worklist_tail = NULL; 1459 ump->softdep_on_worklist = 0; 1460 ump->softdep_deps = 0; 1461 /* 1462 * When doing soft updates, the counters in the 1463 * superblock may have gotten out of sync. Recomputation 1464 * can take a long time and can be deferred for background 1465 * fsck. However, the old behavior of scanning the cylinder 1466 * groups and recalculating them at mount time is available 1467 * by setting vfs.ffs.compute_summary_at_mount to one. 1468 */ 1469 if (compute_summary_at_mount == 0 || fs->fs_clean != 0) 1470 return (0); 1471 bzero(&cstotal, sizeof cstotal); 1472 for (cyl = 0; cyl < fs->fs_ncg; cyl++) { 1473 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)), 1474 fs->fs_cgsize, cred, &bp)) != 0) { 1475 brelse(bp); 1476 return (error); 1477 } 1478 cgp = (struct cg *)bp->b_data; 1479 cstotal.cs_nffree += cgp->cg_cs.cs_nffree; 1480 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree; 1481 cstotal.cs_nifree += cgp->cg_cs.cs_nifree; 1482 cstotal.cs_ndir += cgp->cg_cs.cs_ndir; 1483 fs->fs_cs(fs, cyl) = cgp->cg_cs; 1484 brelse(bp); 1485 } 1486 #ifdef DEBUG 1487 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal)) 1488 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt); 1489 #endif 1490 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal); 1491 return (0); 1492 } 1493 1494 /* 1495 * Protecting the freemaps (or bitmaps). 1496 * 1497 * To eliminate the need to execute fsck before mounting a filesystem 1498 * after a power failure, one must (conservatively) guarantee that the 1499 * on-disk copy of the bitmaps never indicate that a live inode or block is 1500 * free. So, when a block or inode is allocated, the bitmap should be 1501 * updated (on disk) before any new pointers. When a block or inode is 1502 * freed, the bitmap should not be updated until all pointers have been 1503 * reset. The latter dependency is handled by the delayed de-allocation 1504 * approach described below for block and inode de-allocation. The former 1505 * dependency is handled by calling the following procedure when a block or 1506 * inode is allocated. When an inode is allocated an "inodedep" is created 1507 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk. 1508 * Each "inodedep" is also inserted into the hash indexing structure so 1509 * that any additional link additions can be made dependent on the inode 1510 * allocation. 1511 * 1512 * The ufs filesystem maintains a number of free block counts (e.g., per 1513 * cylinder group, per cylinder and per <cylinder, rotational position> pair) 1514 * in addition to the bitmaps. These counts are used to improve efficiency 1515 * during allocation and therefore must be consistent with the bitmaps. 1516 * There is no convenient way to guarantee post-crash consistency of these 1517 * counts with simple update ordering, for two main reasons: (1) The counts 1518 * and bitmaps for a single cylinder group block are not in the same disk 1519 * sector. If a disk write is interrupted (e.g., by power failure), one may 1520 * be written and the other not. (2) Some of the counts are located in the 1521 * superblock rather than the cylinder group block. So, we focus our soft 1522 * updates implementation on protecting the bitmaps. When mounting a 1523 * filesystem, we recompute the auxiliary counts from the bitmaps. 1524 */ 1525 1526 /* 1527 * Called just after updating the cylinder group block to allocate an inode. 1528 */ 1529 void 1530 softdep_setup_inomapdep(bp, ip, newinum) 1531 struct buf *bp; /* buffer for cylgroup block with inode map */ 1532 struct inode *ip; /* inode related to allocation */ 1533 ino_t newinum; /* new inode number being allocated */ 1534 { 1535 struct inodedep *inodedep; 1536 struct bmsafemap *bmsafemap; 1537 1538 /* 1539 * Create a dependency for the newly allocated inode. 1540 * Panic if it already exists as something is seriously wrong. 1541 * Otherwise add it to the dependency list for the buffer holding 1542 * the cylinder group map from which it was allocated. 1543 */ 1544 ACQUIRE_LOCK(&lk); 1545 if ((inodedep_lookup(UFSTOVFS(ip->i_ump), newinum, DEPALLOC|NODELAY, 1546 &inodedep))) 1547 panic("softdep_setup_inomapdep: dependency for new inode " 1548 "already exists"); 1549 inodedep->id_buf = bp; 1550 inodedep->id_state &= ~DEPCOMPLETE; 1551 bmsafemap = bmsafemap_lookup(inodedep->id_list.wk_mp, bp); 1552 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps); 1553 FREE_LOCK(&lk); 1554 } 1555 1556 /* 1557 * Called just after updating the cylinder group block to 1558 * allocate block or fragment. 1559 */ 1560 void 1561 softdep_setup_blkmapdep(bp, mp, newblkno) 1562 struct buf *bp; /* buffer for cylgroup block with block map */ 1563 struct mount *mp; /* filesystem doing allocation */ 1564 ufs2_daddr_t newblkno; /* number of newly allocated block */ 1565 { 1566 struct newblk *newblk; 1567 struct bmsafemap *bmsafemap; 1568 struct fs *fs; 1569 1570 fs = VFSTOUFS(mp)->um_fs; 1571 /* 1572 * Create a dependency for the newly allocated block. 1573 * Add it to the dependency list for the buffer holding 1574 * the cylinder group map from which it was allocated. 1575 */ 1576 ACQUIRE_LOCK(&lk); 1577 if (newblk_lookup(fs, newblkno, DEPALLOC, &newblk) != 0) 1578 panic("softdep_setup_blkmapdep: found block"); 1579 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp); 1580 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps); 1581 FREE_LOCK(&lk); 1582 } 1583 1584 /* 1585 * Find the bmsafemap associated with a cylinder group buffer. 1586 * If none exists, create one. The buffer must be locked when 1587 * this routine is called and this routine must be called with 1588 * splbio interrupts blocked. 1589 */ 1590 static struct bmsafemap * 1591 bmsafemap_lookup(mp, bp) 1592 struct mount *mp; 1593 struct buf *bp; 1594 { 1595 struct bmsafemap *bmsafemap; 1596 struct worklist *wk; 1597 1598 mtx_assert(&lk, MA_OWNED); 1599 LIST_FOREACH(wk, &bp->b_dep, wk_list) 1600 if (wk->wk_type == D_BMSAFEMAP) 1601 return (WK_BMSAFEMAP(wk)); 1602 FREE_LOCK(&lk); 1603 bmsafemap = malloc(sizeof(struct bmsafemap), 1604 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 1605 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 1606 bmsafemap->sm_buf = bp; 1607 LIST_INIT(&bmsafemap->sm_allocdirecthd); 1608 LIST_INIT(&bmsafemap->sm_allocindirhd); 1609 LIST_INIT(&bmsafemap->sm_inodedephd); 1610 LIST_INIT(&bmsafemap->sm_newblkhd); 1611 ACQUIRE_LOCK(&lk); 1612 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list); 1613 return (bmsafemap); 1614 } 1615 1616 /* 1617 * Direct block allocation dependencies. 1618 * 1619 * When a new block is allocated, the corresponding disk locations must be 1620 * initialized (with zeros or new data) before the on-disk inode points to 1621 * them. Also, the freemap from which the block was allocated must be 1622 * updated (on disk) before the inode's pointer. These two dependencies are 1623 * independent of each other and are needed for all file blocks and indirect 1624 * blocks that are pointed to directly by the inode. Just before the 1625 * "in-core" version of the inode is updated with a newly allocated block 1626 * number, a procedure (below) is called to setup allocation dependency 1627 * structures. These structures are removed when the corresponding 1628 * dependencies are satisfied or when the block allocation becomes obsolete 1629 * (i.e., the file is deleted, the block is de-allocated, or the block is a 1630 * fragment that gets upgraded). All of these cases are handled in 1631 * procedures described later. 1632 * 1633 * When a file extension causes a fragment to be upgraded, either to a larger 1634 * fragment or to a full block, the on-disk location may change (if the 1635 * previous fragment could not simply be extended). In this case, the old 1636 * fragment must be de-allocated, but not until after the inode's pointer has 1637 * been updated. In most cases, this is handled by later procedures, which 1638 * will construct a "freefrag" structure to be added to the workitem queue 1639 * when the inode update is complete (or obsolete). The main exception to 1640 * this is when an allocation occurs while a pending allocation dependency 1641 * (for the same block pointer) remains. This case is handled in the main 1642 * allocation dependency setup procedure by immediately freeing the 1643 * unreferenced fragments. 1644 */ 1645 void 1646 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 1647 struct inode *ip; /* inode to which block is being added */ 1648 ufs_lbn_t lbn; /* block pointer within inode */ 1649 ufs2_daddr_t newblkno; /* disk block number being added */ 1650 ufs2_daddr_t oldblkno; /* previous block number, 0 unless frag */ 1651 long newsize; /* size of new block */ 1652 long oldsize; /* size of new block */ 1653 struct buf *bp; /* bp for allocated block */ 1654 { 1655 struct allocdirect *adp, *oldadp; 1656 struct allocdirectlst *adphead; 1657 struct bmsafemap *bmsafemap; 1658 struct inodedep *inodedep; 1659 struct pagedep *pagedep; 1660 struct newblk *newblk; 1661 struct mount *mp; 1662 1663 mp = UFSTOVFS(ip->i_ump); 1664 adp = malloc(sizeof(struct allocdirect), 1665 M_ALLOCDIRECT, M_SOFTDEP_FLAGS|M_ZERO); 1666 workitem_alloc(&adp->ad_list, D_ALLOCDIRECT, mp); 1667 adp->ad_lbn = lbn; 1668 adp->ad_newblkno = newblkno; 1669 adp->ad_oldblkno = oldblkno; 1670 adp->ad_newsize = newsize; 1671 adp->ad_oldsize = oldsize; 1672 adp->ad_state = ATTACHED; 1673 LIST_INIT(&adp->ad_newdirblk); 1674 if (newblkno == oldblkno) 1675 adp->ad_freefrag = NULL; 1676 else 1677 adp->ad_freefrag = newfreefrag(ip, oldblkno, oldsize); 1678 1679 ACQUIRE_LOCK(&lk); 1680 if (lbn >= NDADDR) { 1681 /* allocating an indirect block */ 1682 if (oldblkno != 0) 1683 panic("softdep_setup_allocdirect: non-zero indir"); 1684 } else { 1685 /* 1686 * Allocating a direct block. 1687 * 1688 * If we are allocating a directory block, then we must 1689 * allocate an associated pagedep to track additions and 1690 * deletions. 1691 */ 1692 if ((ip->i_mode & IFMT) == IFDIR && 1693 pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0) 1694 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list); 1695 } 1696 if (newblk_lookup(ip->i_fs, newblkno, 0, &newblk) == 0) 1697 panic("softdep_setup_allocdirect: lost block"); 1698 if (newblk->nb_state == DEPCOMPLETE) { 1699 adp->ad_state |= DEPCOMPLETE; 1700 adp->ad_buf = NULL; 1701 } else { 1702 bmsafemap = newblk->nb_bmsafemap; 1703 adp->ad_buf = bmsafemap->sm_buf; 1704 LIST_REMOVE(newblk, nb_deps); 1705 LIST_INSERT_HEAD(&bmsafemap->sm_allocdirecthd, adp, ad_deps); 1706 } 1707 LIST_REMOVE(newblk, nb_hash); 1708 free(newblk, M_NEWBLK); 1709 1710 inodedep_lookup(mp, ip->i_number, DEPALLOC | NODELAY, &inodedep); 1711 adp->ad_inodedep = inodedep; 1712 WORKLIST_INSERT(&bp->b_dep, &adp->ad_list); 1713 /* 1714 * The list of allocdirects must be kept in sorted and ascending 1715 * order so that the rollback routines can quickly determine the 1716 * first uncommitted block (the size of the file stored on disk 1717 * ends at the end of the lowest committed fragment, or if there 1718 * are no fragments, at the end of the highest committed block). 1719 * Since files generally grow, the typical case is that the new 1720 * block is to be added at the end of the list. We speed this 1721 * special case by checking against the last allocdirect in the 1722 * list before laboriously traversing the list looking for the 1723 * insertion point. 1724 */ 1725 adphead = &inodedep->id_newinoupdt; 1726 oldadp = TAILQ_LAST(adphead, allocdirectlst); 1727 if (oldadp == NULL || oldadp->ad_lbn <= lbn) { 1728 /* insert at end of list */ 1729 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 1730 if (oldadp != NULL && oldadp->ad_lbn == lbn) 1731 allocdirect_merge(adphead, adp, oldadp); 1732 FREE_LOCK(&lk); 1733 return; 1734 } 1735 TAILQ_FOREACH(oldadp, adphead, ad_next) { 1736 if (oldadp->ad_lbn >= lbn) 1737 break; 1738 } 1739 if (oldadp == NULL) 1740 panic("softdep_setup_allocdirect: lost entry"); 1741 /* insert in middle of list */ 1742 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 1743 if (oldadp->ad_lbn == lbn) 1744 allocdirect_merge(adphead, adp, oldadp); 1745 FREE_LOCK(&lk); 1746 } 1747 1748 /* 1749 * Replace an old allocdirect dependency with a newer one. 1750 * This routine must be called with splbio interrupts blocked. 1751 */ 1752 static void 1753 allocdirect_merge(adphead, newadp, oldadp) 1754 struct allocdirectlst *adphead; /* head of list holding allocdirects */ 1755 struct allocdirect *newadp; /* allocdirect being added */ 1756 struct allocdirect *oldadp; /* existing allocdirect being checked */ 1757 { 1758 struct worklist *wk; 1759 struct freefrag *freefrag; 1760 struct newdirblk *newdirblk; 1761 1762 mtx_assert(&lk, MA_OWNED); 1763 if (newadp->ad_oldblkno != oldadp->ad_newblkno || 1764 newadp->ad_oldsize != oldadp->ad_newsize || 1765 newadp->ad_lbn >= NDADDR) 1766 panic("%s %jd != new %jd || old size %ld != new %ld", 1767 "allocdirect_merge: old blkno", 1768 (intmax_t)newadp->ad_oldblkno, 1769 (intmax_t)oldadp->ad_newblkno, 1770 newadp->ad_oldsize, oldadp->ad_newsize); 1771 newadp->ad_oldblkno = oldadp->ad_oldblkno; 1772 newadp->ad_oldsize = oldadp->ad_oldsize; 1773 /* 1774 * If the old dependency had a fragment to free or had never 1775 * previously had a block allocated, then the new dependency 1776 * can immediately post its freefrag and adopt the old freefrag. 1777 * This action is done by swapping the freefrag dependencies. 1778 * The new dependency gains the old one's freefrag, and the 1779 * old one gets the new one and then immediately puts it on 1780 * the worklist when it is freed by free_allocdirect. It is 1781 * not possible to do this swap when the old dependency had a 1782 * non-zero size but no previous fragment to free. This condition 1783 * arises when the new block is an extension of the old block. 1784 * Here, the first part of the fragment allocated to the new 1785 * dependency is part of the block currently claimed on disk by 1786 * the old dependency, so cannot legitimately be freed until the 1787 * conditions for the new dependency are fulfilled. 1788 */ 1789 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) { 1790 freefrag = newadp->ad_freefrag; 1791 newadp->ad_freefrag = oldadp->ad_freefrag; 1792 oldadp->ad_freefrag = freefrag; 1793 } 1794 /* 1795 * If we are tracking a new directory-block allocation, 1796 * move it from the old allocdirect to the new allocdirect. 1797 */ 1798 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) { 1799 newdirblk = WK_NEWDIRBLK(wk); 1800 WORKLIST_REMOVE(&newdirblk->db_list); 1801 if (!LIST_EMPTY(&oldadp->ad_newdirblk)) 1802 panic("allocdirect_merge: extra newdirblk"); 1803 WORKLIST_INSERT(&newadp->ad_newdirblk, &newdirblk->db_list); 1804 } 1805 free_allocdirect(adphead, oldadp, 0); 1806 } 1807 1808 /* 1809 * Allocate a new freefrag structure if needed. 1810 */ 1811 static struct freefrag * 1812 newfreefrag(ip, blkno, size) 1813 struct inode *ip; 1814 ufs2_daddr_t blkno; 1815 long size; 1816 { 1817 struct freefrag *freefrag; 1818 struct fs *fs; 1819 1820 if (blkno == 0) 1821 return (NULL); 1822 fs = ip->i_fs; 1823 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag) 1824 panic("newfreefrag: frag size"); 1825 freefrag = malloc(sizeof(struct freefrag), 1826 M_FREEFRAG, M_SOFTDEP_FLAGS); 1827 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ip->i_ump)); 1828 freefrag->ff_inum = ip->i_number; 1829 freefrag->ff_blkno = blkno; 1830 freefrag->ff_fragsize = size; 1831 return (freefrag); 1832 } 1833 1834 /* 1835 * This workitem de-allocates fragments that were replaced during 1836 * file block allocation. 1837 */ 1838 static void 1839 handle_workitem_freefrag(freefrag) 1840 struct freefrag *freefrag; 1841 { 1842 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp); 1843 1844 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno, 1845 freefrag->ff_fragsize, freefrag->ff_inum); 1846 ACQUIRE_LOCK(&lk); 1847 WORKITEM_FREE(freefrag, D_FREEFRAG); 1848 FREE_LOCK(&lk); 1849 } 1850 1851 /* 1852 * Set up a dependency structure for an external attributes data block. 1853 * This routine follows much of the structure of softdep_setup_allocdirect. 1854 * See the description of softdep_setup_allocdirect above for details. 1855 */ 1856 void 1857 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 1858 struct inode *ip; 1859 ufs_lbn_t lbn; 1860 ufs2_daddr_t newblkno; 1861 ufs2_daddr_t oldblkno; 1862 long newsize; 1863 long oldsize; 1864 struct buf *bp; 1865 { 1866 struct allocdirect *adp, *oldadp; 1867 struct allocdirectlst *adphead; 1868 struct bmsafemap *bmsafemap; 1869 struct inodedep *inodedep; 1870 struct newblk *newblk; 1871 struct mount *mp; 1872 1873 mp = UFSTOVFS(ip->i_ump); 1874 adp = malloc(sizeof(struct allocdirect), 1875 M_ALLOCDIRECT, M_SOFTDEP_FLAGS|M_ZERO); 1876 workitem_alloc(&adp->ad_list, D_ALLOCDIRECT, mp); 1877 adp->ad_lbn = lbn; 1878 adp->ad_newblkno = newblkno; 1879 adp->ad_oldblkno = oldblkno; 1880 adp->ad_newsize = newsize; 1881 adp->ad_oldsize = oldsize; 1882 adp->ad_state = ATTACHED | EXTDATA; 1883 LIST_INIT(&adp->ad_newdirblk); 1884 if (newblkno == oldblkno) 1885 adp->ad_freefrag = NULL; 1886 else 1887 adp->ad_freefrag = newfreefrag(ip, oldblkno, oldsize); 1888 1889 ACQUIRE_LOCK(&lk); 1890 if (newblk_lookup(ip->i_fs, newblkno, 0, &newblk) == 0) 1891 panic("softdep_setup_allocext: lost block"); 1892 1893 inodedep_lookup(mp, ip->i_number, DEPALLOC | NODELAY, &inodedep); 1894 adp->ad_inodedep = inodedep; 1895 1896 if (newblk->nb_state == DEPCOMPLETE) { 1897 adp->ad_state |= DEPCOMPLETE; 1898 adp->ad_buf = NULL; 1899 } else { 1900 bmsafemap = newblk->nb_bmsafemap; 1901 adp->ad_buf = bmsafemap->sm_buf; 1902 LIST_REMOVE(newblk, nb_deps); 1903 LIST_INSERT_HEAD(&bmsafemap->sm_allocdirecthd, adp, ad_deps); 1904 } 1905 LIST_REMOVE(newblk, nb_hash); 1906 free(newblk, M_NEWBLK); 1907 1908 WORKLIST_INSERT(&bp->b_dep, &adp->ad_list); 1909 if (lbn >= NXADDR) 1910 panic("softdep_setup_allocext: lbn %lld > NXADDR", 1911 (long long)lbn); 1912 /* 1913 * The list of allocdirects must be kept in sorted and ascending 1914 * order so that the rollback routines can quickly determine the 1915 * first uncommitted block (the size of the file stored on disk 1916 * ends at the end of the lowest committed fragment, or if there 1917 * are no fragments, at the end of the highest committed block). 1918 * Since files generally grow, the typical case is that the new 1919 * block is to be added at the end of the list. We speed this 1920 * special case by checking against the last allocdirect in the 1921 * list before laboriously traversing the list looking for the 1922 * insertion point. 1923 */ 1924 adphead = &inodedep->id_newextupdt; 1925 oldadp = TAILQ_LAST(adphead, allocdirectlst); 1926 if (oldadp == NULL || oldadp->ad_lbn <= lbn) { 1927 /* insert at end of list */ 1928 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 1929 if (oldadp != NULL && oldadp->ad_lbn == lbn) 1930 allocdirect_merge(adphead, adp, oldadp); 1931 FREE_LOCK(&lk); 1932 return; 1933 } 1934 TAILQ_FOREACH(oldadp, adphead, ad_next) { 1935 if (oldadp->ad_lbn >= lbn) 1936 break; 1937 } 1938 if (oldadp == NULL) 1939 panic("softdep_setup_allocext: lost entry"); 1940 /* insert in middle of list */ 1941 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 1942 if (oldadp->ad_lbn == lbn) 1943 allocdirect_merge(adphead, adp, oldadp); 1944 FREE_LOCK(&lk); 1945 } 1946 1947 /* 1948 * Indirect block allocation dependencies. 1949 * 1950 * The same dependencies that exist for a direct block also exist when 1951 * a new block is allocated and pointed to by an entry in a block of 1952 * indirect pointers. The undo/redo states described above are also 1953 * used here. Because an indirect block contains many pointers that 1954 * may have dependencies, a second copy of the entire in-memory indirect 1955 * block is kept. The buffer cache copy is always completely up-to-date. 1956 * The second copy, which is used only as a source for disk writes, 1957 * contains only the safe pointers (i.e., those that have no remaining 1958 * update dependencies). The second copy is freed when all pointers 1959 * are safe. The cache is not allowed to replace indirect blocks with 1960 * pending update dependencies. If a buffer containing an indirect 1961 * block with dependencies is written, these routines will mark it 1962 * dirty again. It can only be successfully written once all the 1963 * dependencies are removed. The ffs_fsync routine in conjunction with 1964 * softdep_sync_metadata work together to get all the dependencies 1965 * removed so that a file can be successfully written to disk. Three 1966 * procedures are used when setting up indirect block pointer 1967 * dependencies. The division is necessary because of the organization 1968 * of the "balloc" routine and because of the distinction between file 1969 * pages and file metadata blocks. 1970 */ 1971 1972 /* 1973 * Allocate a new allocindir structure. 1974 */ 1975 static struct allocindir * 1976 newallocindir(ip, ptrno, newblkno, oldblkno) 1977 struct inode *ip; /* inode for file being extended */ 1978 int ptrno; /* offset of pointer in indirect block */ 1979 ufs2_daddr_t newblkno; /* disk block number being added */ 1980 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 1981 { 1982 struct allocindir *aip; 1983 1984 aip = malloc(sizeof(struct allocindir), 1985 M_ALLOCINDIR, M_SOFTDEP_FLAGS|M_ZERO); 1986 workitem_alloc(&aip->ai_list, D_ALLOCINDIR, UFSTOVFS(ip->i_ump)); 1987 aip->ai_state = ATTACHED; 1988 aip->ai_offset = ptrno; 1989 aip->ai_newblkno = newblkno; 1990 aip->ai_oldblkno = oldblkno; 1991 aip->ai_freefrag = newfreefrag(ip, oldblkno, ip->i_fs->fs_bsize); 1992 return (aip); 1993 } 1994 1995 /* 1996 * Called just before setting an indirect block pointer 1997 * to a newly allocated file page. 1998 */ 1999 void 2000 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 2001 struct inode *ip; /* inode for file being extended */ 2002 ufs_lbn_t lbn; /* allocated block number within file */ 2003 struct buf *bp; /* buffer with indirect blk referencing page */ 2004 int ptrno; /* offset of pointer in indirect block */ 2005 ufs2_daddr_t newblkno; /* disk block number being added */ 2006 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 2007 struct buf *nbp; /* buffer holding allocated page */ 2008 { 2009 struct allocindir *aip; 2010 struct pagedep *pagedep; 2011 2012 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page"); 2013 aip = newallocindir(ip, ptrno, newblkno, oldblkno); 2014 ACQUIRE_LOCK(&lk); 2015 /* 2016 * If we are allocating a directory page, then we must 2017 * allocate an associated pagedep to track additions and 2018 * deletions. 2019 */ 2020 if ((ip->i_mode & IFMT) == IFDIR && 2021 pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0) 2022 WORKLIST_INSERT(&nbp->b_dep, &pagedep->pd_list); 2023 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_list); 2024 setup_allocindir_phase2(bp, ip, aip); 2025 FREE_LOCK(&lk); 2026 } 2027 2028 /* 2029 * Called just before setting an indirect block pointer to a 2030 * newly allocated indirect block. 2031 */ 2032 void 2033 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 2034 struct buf *nbp; /* newly allocated indirect block */ 2035 struct inode *ip; /* inode for file being extended */ 2036 struct buf *bp; /* indirect block referencing allocated block */ 2037 int ptrno; /* offset of pointer in indirect block */ 2038 ufs2_daddr_t newblkno; /* disk block number being added */ 2039 { 2040 struct allocindir *aip; 2041 2042 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta"); 2043 aip = newallocindir(ip, ptrno, newblkno, 0); 2044 ACQUIRE_LOCK(&lk); 2045 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_list); 2046 setup_allocindir_phase2(bp, ip, aip); 2047 FREE_LOCK(&lk); 2048 } 2049 2050 /* 2051 * Called to finish the allocation of the "aip" allocated 2052 * by one of the two routines above. 2053 */ 2054 static void 2055 setup_allocindir_phase2(bp, ip, aip) 2056 struct buf *bp; /* in-memory copy of the indirect block */ 2057 struct inode *ip; /* inode for file being extended */ 2058 struct allocindir *aip; /* allocindir allocated by the above routines */ 2059 { 2060 struct worklist *wk; 2061 struct indirdep *indirdep, *newindirdep; 2062 struct bmsafemap *bmsafemap; 2063 struct allocindir *oldaip; 2064 struct freefrag *freefrag; 2065 struct newblk *newblk; 2066 ufs2_daddr_t blkno; 2067 2068 mtx_assert(&lk, MA_OWNED); 2069 if (bp->b_lblkno >= 0) 2070 panic("setup_allocindir_phase2: not indir blk"); 2071 for (indirdep = NULL, newindirdep = NULL; ; ) { 2072 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 2073 if (wk->wk_type != D_INDIRDEP) 2074 continue; 2075 indirdep = WK_INDIRDEP(wk); 2076 break; 2077 } 2078 if (indirdep == NULL && newindirdep) { 2079 indirdep = newindirdep; 2080 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list); 2081 newindirdep = NULL; 2082 } 2083 if (indirdep) { 2084 if (newblk_lookup(ip->i_fs, aip->ai_newblkno, 0, 2085 &newblk) == 0) 2086 panic("setup_allocindir: lost block"); 2087 if (newblk->nb_state == DEPCOMPLETE) { 2088 aip->ai_state |= DEPCOMPLETE; 2089 aip->ai_buf = NULL; 2090 } else { 2091 bmsafemap = newblk->nb_bmsafemap; 2092 aip->ai_buf = bmsafemap->sm_buf; 2093 LIST_REMOVE(newblk, nb_deps); 2094 LIST_INSERT_HEAD(&bmsafemap->sm_allocindirhd, 2095 aip, ai_deps); 2096 } 2097 LIST_REMOVE(newblk, nb_hash); 2098 free(newblk, M_NEWBLK); 2099 aip->ai_indirdep = indirdep; 2100 /* 2101 * Check to see if there is an existing dependency 2102 * for this block. If there is, merge the old 2103 * dependency into the new one. 2104 */ 2105 if (aip->ai_oldblkno == 0) 2106 oldaip = NULL; 2107 else 2108 2109 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) 2110 if (oldaip->ai_offset == aip->ai_offset) 2111 break; 2112 freefrag = NULL; 2113 if (oldaip != NULL) { 2114 if (oldaip->ai_newblkno != aip->ai_oldblkno) 2115 panic("setup_allocindir_phase2: blkno"); 2116 aip->ai_oldblkno = oldaip->ai_oldblkno; 2117 freefrag = aip->ai_freefrag; 2118 aip->ai_freefrag = oldaip->ai_freefrag; 2119 oldaip->ai_freefrag = NULL; 2120 free_allocindir(oldaip, NULL); 2121 } 2122 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next); 2123 if (ip->i_ump->um_fstype == UFS1) 2124 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data) 2125 [aip->ai_offset] = aip->ai_oldblkno; 2126 else 2127 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data) 2128 [aip->ai_offset] = aip->ai_oldblkno; 2129 FREE_LOCK(&lk); 2130 if (freefrag != NULL) 2131 handle_workitem_freefrag(freefrag); 2132 } else 2133 FREE_LOCK(&lk); 2134 if (newindirdep) { 2135 newindirdep->ir_savebp->b_flags |= B_INVAL | B_NOCACHE; 2136 brelse(newindirdep->ir_savebp); 2137 ACQUIRE_LOCK(&lk); 2138 WORKITEM_FREE((caddr_t)newindirdep, D_INDIRDEP); 2139 if (indirdep) 2140 break; 2141 FREE_LOCK(&lk); 2142 } 2143 if (indirdep) { 2144 ACQUIRE_LOCK(&lk); 2145 break; 2146 } 2147 newindirdep = malloc(sizeof(struct indirdep), 2148 M_INDIRDEP, M_SOFTDEP_FLAGS); 2149 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, 2150 UFSTOVFS(ip->i_ump)); 2151 newindirdep->ir_state = ATTACHED; 2152 if (ip->i_ump->um_fstype == UFS1) 2153 newindirdep->ir_state |= UFS1FMT; 2154 LIST_INIT(&newindirdep->ir_deplisthd); 2155 LIST_INIT(&newindirdep->ir_donehd); 2156 if (bp->b_blkno == bp->b_lblkno) { 2157 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp, 2158 NULL, NULL); 2159 bp->b_blkno = blkno; 2160 } 2161 newindirdep->ir_savebp = 2162 getblk(ip->i_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0); 2163 BUF_KERNPROC(newindirdep->ir_savebp); 2164 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount); 2165 ACQUIRE_LOCK(&lk); 2166 } 2167 } 2168 2169 /* 2170 * Block de-allocation dependencies. 2171 * 2172 * When blocks are de-allocated, the on-disk pointers must be nullified before 2173 * the blocks are made available for use by other files. (The true 2174 * requirement is that old pointers must be nullified before new on-disk 2175 * pointers are set. We chose this slightly more stringent requirement to 2176 * reduce complexity.) Our implementation handles this dependency by updating 2177 * the inode (or indirect block) appropriately but delaying the actual block 2178 * de-allocation (i.e., freemap and free space count manipulation) until 2179 * after the updated versions reach stable storage. After the disk is 2180 * updated, the blocks can be safely de-allocated whenever it is convenient. 2181 * This implementation handles only the common case of reducing a file's 2182 * length to zero. Other cases are handled by the conventional synchronous 2183 * write approach. 2184 * 2185 * The ffs implementation with which we worked double-checks 2186 * the state of the block pointers and file size as it reduces 2187 * a file's length. Some of this code is replicated here in our 2188 * soft updates implementation. The freeblks->fb_chkcnt field is 2189 * used to transfer a part of this information to the procedure 2190 * that eventually de-allocates the blocks. 2191 * 2192 * This routine should be called from the routine that shortens 2193 * a file's length, before the inode's size or block pointers 2194 * are modified. It will save the block pointer information for 2195 * later release and zero the inode so that the calling routine 2196 * can release it. 2197 */ 2198 void 2199 softdep_setup_freeblocks(ip, length, flags) 2200 struct inode *ip; /* The inode whose length is to be reduced */ 2201 off_t length; /* The new length for the file */ 2202 int flags; /* IO_EXT and/or IO_NORMAL */ 2203 { 2204 struct freeblks *freeblks; 2205 struct inodedep *inodedep; 2206 struct allocdirect *adp; 2207 struct bufobj *bo; 2208 struct vnode *vp; 2209 struct buf *bp; 2210 struct fs *fs; 2211 ufs2_daddr_t extblocks, datablocks; 2212 struct mount *mp; 2213 int i, delay, error; 2214 2215 fs = ip->i_fs; 2216 mp = UFSTOVFS(ip->i_ump); 2217 if (length != 0) 2218 panic("softdep_setup_freeblocks: non-zero length"); 2219 freeblks = malloc(sizeof(struct freeblks), 2220 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO); 2221 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp); 2222 freeblks->fb_state = ATTACHED; 2223 freeblks->fb_uid = ip->i_uid; 2224 freeblks->fb_previousinum = ip->i_number; 2225 freeblks->fb_devvp = ip->i_devvp; 2226 extblocks = 0; 2227 if (fs->fs_magic == FS_UFS2_MAGIC) 2228 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 2229 datablocks = DIP(ip, i_blocks) - extblocks; 2230 if ((flags & IO_NORMAL) == 0) { 2231 freeblks->fb_oldsize = 0; 2232 freeblks->fb_chkcnt = 0; 2233 } else { 2234 freeblks->fb_oldsize = ip->i_size; 2235 ip->i_size = 0; 2236 DIP_SET(ip, i_size, 0); 2237 freeblks->fb_chkcnt = datablocks; 2238 for (i = 0; i < NDADDR; i++) { 2239 freeblks->fb_dblks[i] = DIP(ip, i_db[i]); 2240 DIP_SET(ip, i_db[i], 0); 2241 } 2242 for (i = 0; i < NIADDR; i++) { 2243 freeblks->fb_iblks[i] = DIP(ip, i_ib[i]); 2244 DIP_SET(ip, i_ib[i], 0); 2245 } 2246 /* 2247 * If the file was removed, then the space being freed was 2248 * accounted for then (see softdep_releasefile()). If the 2249 * file is merely being truncated, then we account for it now. 2250 */ 2251 if ((ip->i_flag & IN_SPACECOUNTED) == 0) { 2252 UFS_LOCK(ip->i_ump); 2253 fs->fs_pendingblocks += datablocks; 2254 UFS_UNLOCK(ip->i_ump); 2255 } 2256 } 2257 if ((flags & IO_EXT) == 0) { 2258 freeblks->fb_oldextsize = 0; 2259 } else { 2260 freeblks->fb_oldextsize = ip->i_din2->di_extsize; 2261 ip->i_din2->di_extsize = 0; 2262 freeblks->fb_chkcnt += extblocks; 2263 for (i = 0; i < NXADDR; i++) { 2264 freeblks->fb_eblks[i] = ip->i_din2->di_extb[i]; 2265 ip->i_din2->di_extb[i] = 0; 2266 } 2267 } 2268 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - freeblks->fb_chkcnt); 2269 /* 2270 * Push the zero'ed inode to to its disk buffer so that we are free 2271 * to delete its dependencies below. Once the dependencies are gone 2272 * the buffer can be safely released. 2273 */ 2274 if ((error = bread(ip->i_devvp, 2275 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 2276 (int)fs->fs_bsize, NOCRED, &bp)) != 0) { 2277 brelse(bp); 2278 softdep_error("softdep_setup_freeblocks", error); 2279 } 2280 if (ip->i_ump->um_fstype == UFS1) 2281 *((struct ufs1_dinode *)bp->b_data + 2282 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1; 2283 else 2284 *((struct ufs2_dinode *)bp->b_data + 2285 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2; 2286 /* 2287 * Find and eliminate any inode dependencies. 2288 */ 2289 ACQUIRE_LOCK(&lk); 2290 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep); 2291 if ((inodedep->id_state & IOSTARTED) != 0) 2292 panic("softdep_setup_freeblocks: inode busy"); 2293 /* 2294 * Add the freeblks structure to the list of operations that 2295 * must await the zero'ed inode being written to disk. If we 2296 * still have a bitmap dependency (delay == 0), then the inode 2297 * has never been written to disk, so we can process the 2298 * freeblks below once we have deleted the dependencies. 2299 */ 2300 delay = (inodedep->id_state & DEPCOMPLETE); 2301 if (delay) 2302 WORKLIST_INSERT(&inodedep->id_bufwait, &freeblks->fb_list); 2303 /* 2304 * Because the file length has been truncated to zero, any 2305 * pending block allocation dependency structures associated 2306 * with this inode are obsolete and can simply be de-allocated. 2307 * We must first merge the two dependency lists to get rid of 2308 * any duplicate freefrag structures, then purge the merged list. 2309 * If we still have a bitmap dependency, then the inode has never 2310 * been written to disk, so we can free any fragments without delay. 2311 */ 2312 if (flags & IO_NORMAL) { 2313 merge_inode_lists(&inodedep->id_newinoupdt, 2314 &inodedep->id_inoupdt); 2315 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != 0) 2316 free_allocdirect(&inodedep->id_inoupdt, adp, delay); 2317 } 2318 if (flags & IO_EXT) { 2319 merge_inode_lists(&inodedep->id_newextupdt, 2320 &inodedep->id_extupdt); 2321 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != 0) 2322 free_allocdirect(&inodedep->id_extupdt, adp, delay); 2323 } 2324 FREE_LOCK(&lk); 2325 bdwrite(bp); 2326 /* 2327 * We must wait for any I/O in progress to finish so that 2328 * all potential buffers on the dirty list will be visible. 2329 * Once they are all there, walk the list and get rid of 2330 * any dependencies. 2331 */ 2332 vp = ITOV(ip); 2333 bo = &vp->v_bufobj; 2334 BO_LOCK(bo); 2335 drain_output(vp); 2336 restart: 2337 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) { 2338 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) || 2339 ((flags & IO_NORMAL) == 0 && 2340 (bp->b_xflags & BX_ALTDATA) == 0)) 2341 continue; 2342 if ((bp = getdirtybuf(bp, BO_MTX(bo), MNT_WAIT)) == NULL) 2343 goto restart; 2344 BO_UNLOCK(bo); 2345 ACQUIRE_LOCK(&lk); 2346 (void) inodedep_lookup(mp, ip->i_number, 0, &inodedep); 2347 deallocate_dependencies(bp, inodedep); 2348 FREE_LOCK(&lk); 2349 bp->b_flags |= B_INVAL | B_NOCACHE; 2350 brelse(bp); 2351 BO_LOCK(bo); 2352 goto restart; 2353 } 2354 BO_UNLOCK(bo); 2355 ACQUIRE_LOCK(&lk); 2356 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 2357 (void) free_inodedep(inodedep); 2358 2359 if(delay) { 2360 freeblks->fb_state |= DEPCOMPLETE; 2361 /* 2362 * If the inode with zeroed block pointers is now on disk 2363 * we can start freeing blocks. Add freeblks to the worklist 2364 * instead of calling handle_workitem_freeblocks directly as 2365 * it is more likely that additional IO is needed to complete 2366 * the request here than in the !delay case. 2367 */ 2368 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 2369 add_to_worklist(&freeblks->fb_list); 2370 } 2371 2372 FREE_LOCK(&lk); 2373 /* 2374 * If the inode has never been written to disk (delay == 0), 2375 * then we can process the freeblks now that we have deleted 2376 * the dependencies. 2377 */ 2378 if (!delay) 2379 handle_workitem_freeblocks(freeblks, 0); 2380 } 2381 2382 /* 2383 * Reclaim any dependency structures from a buffer that is about to 2384 * be reallocated to a new vnode. The buffer must be locked, thus, 2385 * no I/O completion operations can occur while we are manipulating 2386 * its associated dependencies. The mutex is held so that other I/O's 2387 * associated with related dependencies do not occur. 2388 */ 2389 static void 2390 deallocate_dependencies(bp, inodedep) 2391 struct buf *bp; 2392 struct inodedep *inodedep; 2393 { 2394 struct worklist *wk; 2395 struct indirdep *indirdep; 2396 struct allocindir *aip; 2397 struct pagedep *pagedep; 2398 struct dirrem *dirrem; 2399 struct diradd *dap; 2400 int i; 2401 2402 mtx_assert(&lk, MA_OWNED); 2403 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) { 2404 switch (wk->wk_type) { 2405 2406 case D_INDIRDEP: 2407 indirdep = WK_INDIRDEP(wk); 2408 /* 2409 * None of the indirect pointers will ever be visible, 2410 * so they can simply be tossed. GOINGAWAY ensures 2411 * that allocated pointers will be saved in the buffer 2412 * cache until they are freed. Note that they will 2413 * only be able to be found by their physical address 2414 * since the inode mapping the logical address will 2415 * be gone. The save buffer used for the safe copy 2416 * was allocated in setup_allocindir_phase2 using 2417 * the physical address so it could be used for this 2418 * purpose. Hence we swap the safe copy with the real 2419 * copy, allowing the safe copy to be freed and holding 2420 * on to the real copy for later use in indir_trunc. 2421 */ 2422 if (indirdep->ir_state & GOINGAWAY) 2423 panic("deallocate_dependencies: already gone"); 2424 indirdep->ir_state |= GOINGAWAY; 2425 VFSTOUFS(bp->b_vp->v_mount)->um_numindirdeps += 1; 2426 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != 0) 2427 free_allocindir(aip, inodedep); 2428 if (bp->b_lblkno >= 0 || 2429 bp->b_blkno != indirdep->ir_savebp->b_lblkno) 2430 panic("deallocate_dependencies: not indir"); 2431 bcopy(bp->b_data, indirdep->ir_savebp->b_data, 2432 bp->b_bcount); 2433 WORKLIST_REMOVE(wk); 2434 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, wk); 2435 continue; 2436 2437 case D_PAGEDEP: 2438 pagedep = WK_PAGEDEP(wk); 2439 /* 2440 * None of the directory additions will ever be 2441 * visible, so they can simply be tossed. 2442 */ 2443 for (i = 0; i < DAHASHSZ; i++) 2444 while ((dap = 2445 LIST_FIRST(&pagedep->pd_diraddhd[i]))) 2446 free_diradd(dap); 2447 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != 0) 2448 free_diradd(dap); 2449 /* 2450 * Copy any directory remove dependencies to the list 2451 * to be processed after the zero'ed inode is written. 2452 * If the inode has already been written, then they 2453 * can be dumped directly onto the work list. 2454 */ 2455 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) { 2456 LIST_REMOVE(dirrem, dm_next); 2457 dirrem->dm_dirinum = pagedep->pd_ino; 2458 if (inodedep == NULL || 2459 (inodedep->id_state & ALLCOMPLETE) == 2460 ALLCOMPLETE) 2461 add_to_worklist(&dirrem->dm_list); 2462 else 2463 WORKLIST_INSERT(&inodedep->id_bufwait, 2464 &dirrem->dm_list); 2465 } 2466 if ((pagedep->pd_state & NEWBLOCK) != 0) { 2467 LIST_FOREACH(wk, &inodedep->id_bufwait, wk_list) 2468 if (wk->wk_type == D_NEWDIRBLK && 2469 WK_NEWDIRBLK(wk)->db_pagedep == 2470 pagedep) 2471 break; 2472 if (wk != NULL) { 2473 WORKLIST_REMOVE(wk); 2474 free_newdirblk(WK_NEWDIRBLK(wk)); 2475 } else 2476 panic("deallocate_dependencies: " 2477 "lost pagedep"); 2478 } 2479 WORKLIST_REMOVE(&pagedep->pd_list); 2480 LIST_REMOVE(pagedep, pd_hash); 2481 WORKITEM_FREE(pagedep, D_PAGEDEP); 2482 continue; 2483 2484 case D_ALLOCINDIR: 2485 free_allocindir(WK_ALLOCINDIR(wk), inodedep); 2486 continue; 2487 2488 case D_ALLOCDIRECT: 2489 case D_INODEDEP: 2490 panic("deallocate_dependencies: Unexpected type %s", 2491 TYPENAME(wk->wk_type)); 2492 /* NOTREACHED */ 2493 2494 default: 2495 panic("deallocate_dependencies: Unknown type %s", 2496 TYPENAME(wk->wk_type)); 2497 /* NOTREACHED */ 2498 } 2499 } 2500 } 2501 2502 /* 2503 * Free an allocdirect. Generate a new freefrag work request if appropriate. 2504 * This routine must be called with splbio interrupts blocked. 2505 */ 2506 static void 2507 free_allocdirect(adphead, adp, delay) 2508 struct allocdirectlst *adphead; 2509 struct allocdirect *adp; 2510 int delay; 2511 { 2512 struct newdirblk *newdirblk; 2513 struct worklist *wk; 2514 2515 mtx_assert(&lk, MA_OWNED); 2516 if ((adp->ad_state & DEPCOMPLETE) == 0) 2517 LIST_REMOVE(adp, ad_deps); 2518 TAILQ_REMOVE(adphead, adp, ad_next); 2519 if ((adp->ad_state & COMPLETE) == 0) 2520 WORKLIST_REMOVE(&adp->ad_list); 2521 if (adp->ad_freefrag != NULL) { 2522 if (delay) 2523 WORKLIST_INSERT(&adp->ad_inodedep->id_bufwait, 2524 &adp->ad_freefrag->ff_list); 2525 else 2526 add_to_worklist(&adp->ad_freefrag->ff_list); 2527 } 2528 if ((wk = LIST_FIRST(&adp->ad_newdirblk)) != NULL) { 2529 newdirblk = WK_NEWDIRBLK(wk); 2530 WORKLIST_REMOVE(&newdirblk->db_list); 2531 if (!LIST_EMPTY(&adp->ad_newdirblk)) 2532 panic("free_allocdirect: extra newdirblk"); 2533 if (delay) 2534 WORKLIST_INSERT(&adp->ad_inodedep->id_bufwait, 2535 &newdirblk->db_list); 2536 else 2537 free_newdirblk(newdirblk); 2538 } 2539 WORKITEM_FREE(adp, D_ALLOCDIRECT); 2540 } 2541 2542 /* 2543 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep. 2544 * This routine must be called with splbio interrupts blocked. 2545 */ 2546 static void 2547 free_newdirblk(newdirblk) 2548 struct newdirblk *newdirblk; 2549 { 2550 struct pagedep *pagedep; 2551 struct diradd *dap; 2552 int i; 2553 2554 mtx_assert(&lk, MA_OWNED); 2555 /* 2556 * If the pagedep is still linked onto the directory buffer 2557 * dependency chain, then some of the entries on the 2558 * pd_pendinghd list may not be committed to disk yet. In 2559 * this case, we will simply clear the NEWBLOCK flag and 2560 * let the pd_pendinghd list be processed when the pagedep 2561 * is next written. If the pagedep is no longer on the buffer 2562 * dependency chain, then all the entries on the pd_pending 2563 * list are committed to disk and we can free them here. 2564 */ 2565 pagedep = newdirblk->db_pagedep; 2566 pagedep->pd_state &= ~NEWBLOCK; 2567 if ((pagedep->pd_state & ONWORKLIST) == 0) 2568 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 2569 free_diradd(dap); 2570 /* 2571 * If no dependencies remain, the pagedep will be freed. 2572 */ 2573 for (i = 0; i < DAHASHSZ; i++) 2574 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i])) 2575 break; 2576 if (i == DAHASHSZ && (pagedep->pd_state & ONWORKLIST) == 0) { 2577 LIST_REMOVE(pagedep, pd_hash); 2578 WORKITEM_FREE(pagedep, D_PAGEDEP); 2579 } 2580 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 2581 } 2582 2583 /* 2584 * Prepare an inode to be freed. The actual free operation is not 2585 * done until the zero'ed inode has been written to disk. 2586 */ 2587 void 2588 softdep_freefile(pvp, ino, mode) 2589 struct vnode *pvp; 2590 ino_t ino; 2591 int mode; 2592 { 2593 struct inode *ip = VTOI(pvp); 2594 struct inodedep *inodedep; 2595 struct freefile *freefile; 2596 2597 /* 2598 * This sets up the inode de-allocation dependency. 2599 */ 2600 freefile = malloc(sizeof(struct freefile), 2601 M_FREEFILE, M_SOFTDEP_FLAGS); 2602 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount); 2603 freefile->fx_mode = mode; 2604 freefile->fx_oldinum = ino; 2605 freefile->fx_devvp = ip->i_devvp; 2606 if ((ip->i_flag & IN_SPACECOUNTED) == 0) { 2607 UFS_LOCK(ip->i_ump); 2608 ip->i_fs->fs_pendinginodes += 1; 2609 UFS_UNLOCK(ip->i_ump); 2610 } 2611 2612 /* 2613 * If the inodedep does not exist, then the zero'ed inode has 2614 * been written to disk. If the allocated inode has never been 2615 * written to disk, then the on-disk inode is zero'ed. In either 2616 * case we can free the file immediately. 2617 */ 2618 ACQUIRE_LOCK(&lk); 2619 if (inodedep_lookup(pvp->v_mount, ino, 0, &inodedep) == 0 || 2620 check_inode_unwritten(inodedep)) { 2621 FREE_LOCK(&lk); 2622 handle_workitem_freefile(freefile); 2623 return; 2624 } 2625 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list); 2626 FREE_LOCK(&lk); 2627 if (ip->i_number == ino) 2628 ip->i_flag |= IN_MODIFIED; 2629 } 2630 2631 /* 2632 * Check to see if an inode has never been written to disk. If 2633 * so free the inodedep and return success, otherwise return failure. 2634 * This routine must be called with splbio interrupts blocked. 2635 * 2636 * If we still have a bitmap dependency, then the inode has never 2637 * been written to disk. Drop the dependency as it is no longer 2638 * necessary since the inode is being deallocated. We set the 2639 * ALLCOMPLETE flags since the bitmap now properly shows that the 2640 * inode is not allocated. Even if the inode is actively being 2641 * written, it has been rolled back to its zero'ed state, so we 2642 * are ensured that a zero inode is what is on the disk. For short 2643 * lived files, this change will usually result in removing all the 2644 * dependencies from the inode so that it can be freed immediately. 2645 */ 2646 static int 2647 check_inode_unwritten(inodedep) 2648 struct inodedep *inodedep; 2649 { 2650 2651 mtx_assert(&lk, MA_OWNED); 2652 if ((inodedep->id_state & DEPCOMPLETE) != 0 || 2653 !LIST_EMPTY(&inodedep->id_pendinghd) || 2654 !LIST_EMPTY(&inodedep->id_bufwait) || 2655 !LIST_EMPTY(&inodedep->id_inowait) || 2656 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 2657 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 2658 !TAILQ_EMPTY(&inodedep->id_extupdt) || 2659 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 2660 inodedep->id_nlinkdelta != 0) 2661 return (0); 2662 2663 /* 2664 * Another process might be in initiate_write_inodeblock_ufs[12] 2665 * trying to allocate memory without holding "Softdep Lock". 2666 */ 2667 if ((inodedep->id_state & IOSTARTED) != 0 && 2668 inodedep->id_savedino1 == NULL) 2669 return (0); 2670 2671 inodedep->id_state |= ALLCOMPLETE; 2672 LIST_REMOVE(inodedep, id_deps); 2673 inodedep->id_buf = NULL; 2674 if (inodedep->id_state & ONWORKLIST) 2675 WORKLIST_REMOVE(&inodedep->id_list); 2676 if (inodedep->id_savedino1 != NULL) { 2677 free(inodedep->id_savedino1, M_SAVEDINO); 2678 inodedep->id_savedino1 = NULL; 2679 } 2680 if (free_inodedep(inodedep) == 0) 2681 panic("check_inode_unwritten: busy inode"); 2682 return (1); 2683 } 2684 2685 /* 2686 * Try to free an inodedep structure. Return 1 if it could be freed. 2687 */ 2688 static int 2689 free_inodedep(inodedep) 2690 struct inodedep *inodedep; 2691 { 2692 2693 mtx_assert(&lk, MA_OWNED); 2694 if ((inodedep->id_state & ONWORKLIST) != 0 || 2695 (inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE || 2696 !LIST_EMPTY(&inodedep->id_pendinghd) || 2697 !LIST_EMPTY(&inodedep->id_bufwait) || 2698 !LIST_EMPTY(&inodedep->id_inowait) || 2699 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 2700 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 2701 !TAILQ_EMPTY(&inodedep->id_extupdt) || 2702 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 2703 inodedep->id_nlinkdelta != 0 || inodedep->id_savedino1 != NULL) 2704 return (0); 2705 LIST_REMOVE(inodedep, id_hash); 2706 WORKITEM_FREE(inodedep, D_INODEDEP); 2707 num_inodedep -= 1; 2708 return (1); 2709 } 2710 2711 /* 2712 * This workitem routine performs the block de-allocation. 2713 * The workitem is added to the pending list after the updated 2714 * inode block has been written to disk. As mentioned above, 2715 * checks regarding the number of blocks de-allocated (compared 2716 * to the number of blocks allocated for the file) are also 2717 * performed in this function. 2718 */ 2719 static void 2720 handle_workitem_freeblocks(freeblks, flags) 2721 struct freeblks *freeblks; 2722 int flags; 2723 { 2724 struct inode *ip; 2725 struct vnode *vp; 2726 struct fs *fs; 2727 struct ufsmount *ump; 2728 int i, nblocks, level, bsize; 2729 ufs2_daddr_t bn, blocksreleased = 0; 2730 int error, allerror = 0; 2731 ufs_lbn_t baselbns[NIADDR], tmpval; 2732 int fs_pendingblocks; 2733 2734 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 2735 fs = ump->um_fs; 2736 fs_pendingblocks = 0; 2737 tmpval = 1; 2738 baselbns[0] = NDADDR; 2739 for (i = 1; i < NIADDR; i++) { 2740 tmpval *= NINDIR(fs); 2741 baselbns[i] = baselbns[i - 1] + tmpval; 2742 } 2743 nblocks = btodb(fs->fs_bsize); 2744 blocksreleased = 0; 2745 /* 2746 * Release all extended attribute blocks or frags. 2747 */ 2748 if (freeblks->fb_oldextsize > 0) { 2749 for (i = (NXADDR - 1); i >= 0; i--) { 2750 if ((bn = freeblks->fb_eblks[i]) == 0) 2751 continue; 2752 bsize = sblksize(fs, freeblks->fb_oldextsize, i); 2753 ffs_blkfree(ump, fs, freeblks->fb_devvp, bn, bsize, 2754 freeblks->fb_previousinum); 2755 blocksreleased += btodb(bsize); 2756 } 2757 } 2758 /* 2759 * Release all data blocks or frags. 2760 */ 2761 if (freeblks->fb_oldsize > 0) { 2762 /* 2763 * Indirect blocks first. 2764 */ 2765 for (level = (NIADDR - 1); level >= 0; level--) { 2766 if ((bn = freeblks->fb_iblks[level]) == 0) 2767 continue; 2768 if ((error = indir_trunc(freeblks, fsbtodb(fs, bn), 2769 level, baselbns[level], &blocksreleased)) != 0) 2770 allerror = error; 2771 ffs_blkfree(ump, fs, freeblks->fb_devvp, bn, 2772 fs->fs_bsize, freeblks->fb_previousinum); 2773 fs_pendingblocks += nblocks; 2774 blocksreleased += nblocks; 2775 } 2776 /* 2777 * All direct blocks or frags. 2778 */ 2779 for (i = (NDADDR - 1); i >= 0; i--) { 2780 if ((bn = freeblks->fb_dblks[i]) == 0) 2781 continue; 2782 bsize = sblksize(fs, freeblks->fb_oldsize, i); 2783 ffs_blkfree(ump, fs, freeblks->fb_devvp, bn, bsize, 2784 freeblks->fb_previousinum); 2785 fs_pendingblocks += btodb(bsize); 2786 blocksreleased += btodb(bsize); 2787 } 2788 } 2789 UFS_LOCK(ump); 2790 fs->fs_pendingblocks -= fs_pendingblocks; 2791 UFS_UNLOCK(ump); 2792 /* 2793 * If we still have not finished background cleanup, then check 2794 * to see if the block count needs to be adjusted. 2795 */ 2796 if (freeblks->fb_chkcnt != blocksreleased && 2797 (fs->fs_flags & FS_UNCLEAN) != 0 && 2798 ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_previousinum, 2799 (flags & LK_NOWAIT) | LK_EXCLUSIVE, &vp, FFSV_FORCEINSMQ) 2800 == 0) { 2801 ip = VTOI(vp); 2802 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + \ 2803 freeblks->fb_chkcnt - blocksreleased); 2804 ip->i_flag |= IN_CHANGE; 2805 vput(vp); 2806 } 2807 2808 #ifdef INVARIANTS 2809 if (freeblks->fb_chkcnt != blocksreleased && 2810 ((fs->fs_flags & FS_UNCLEAN) == 0 || (flags & LK_NOWAIT) != 0)) 2811 printf("handle_workitem_freeblocks: block count\n"); 2812 if (allerror) 2813 softdep_error("handle_workitem_freeblks", allerror); 2814 #endif /* INVARIANTS */ 2815 2816 ACQUIRE_LOCK(&lk); 2817 WORKITEM_FREE(freeblks, D_FREEBLKS); 2818 FREE_LOCK(&lk); 2819 } 2820 2821 /* 2822 * Release blocks associated with the inode ip and stored in the indirect 2823 * block dbn. If level is greater than SINGLE, the block is an indirect block 2824 * and recursive calls to indirtrunc must be used to cleanse other indirect 2825 * blocks. 2826 */ 2827 static int 2828 indir_trunc(freeblks, dbn, level, lbn, countp) 2829 struct freeblks *freeblks; 2830 ufs2_daddr_t dbn; 2831 int level; 2832 ufs_lbn_t lbn; 2833 ufs2_daddr_t *countp; 2834 { 2835 struct buf *bp; 2836 struct fs *fs; 2837 struct worklist *wk; 2838 struct indirdep *indirdep; 2839 struct ufsmount *ump; 2840 ufs1_daddr_t *bap1 = 0; 2841 ufs2_daddr_t nb, *bap2 = 0; 2842 ufs_lbn_t lbnadd; 2843 int i, nblocks, ufs1fmt; 2844 int error, allerror = 0; 2845 int fs_pendingblocks; 2846 2847 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 2848 fs = ump->um_fs; 2849 fs_pendingblocks = 0; 2850 lbnadd = 1; 2851 for (i = level; i > 0; i--) 2852 lbnadd *= NINDIR(fs); 2853 /* 2854 * Get buffer of block pointers to be freed. This routine is not 2855 * called until the zero'ed inode has been written, so it is safe 2856 * to free blocks as they are encountered. Because the inode has 2857 * been zero'ed, calls to bmap on these blocks will fail. So, we 2858 * have to use the on-disk address and the block device for the 2859 * filesystem to look them up. If the file was deleted before its 2860 * indirect blocks were all written to disk, the routine that set 2861 * us up (deallocate_dependencies) will have arranged to leave 2862 * a complete copy of the indirect block in memory for our use. 2863 * Otherwise we have to read the blocks in from the disk. 2864 */ 2865 #ifdef notyet 2866 bp = getblk(freeblks->fb_devvp, dbn, (int)fs->fs_bsize, 0, 0, 2867 GB_NOCREAT); 2868 #else 2869 bp = incore(&freeblks->fb_devvp->v_bufobj, dbn); 2870 #endif 2871 ACQUIRE_LOCK(&lk); 2872 if (bp != NULL && (wk = LIST_FIRST(&bp->b_dep)) != NULL) { 2873 if (wk->wk_type != D_INDIRDEP || 2874 (indirdep = WK_INDIRDEP(wk))->ir_savebp != bp || 2875 (indirdep->ir_state & GOINGAWAY) == 0) 2876 panic("indir_trunc: lost indirdep"); 2877 WORKLIST_REMOVE(wk); 2878 WORKITEM_FREE(indirdep, D_INDIRDEP); 2879 if (!LIST_EMPTY(&bp->b_dep)) 2880 panic("indir_trunc: dangling dep"); 2881 ump->um_numindirdeps -= 1; 2882 FREE_LOCK(&lk); 2883 } else { 2884 #ifdef notyet 2885 if (bp) 2886 brelse(bp); 2887 #endif 2888 FREE_LOCK(&lk); 2889 error = bread(freeblks->fb_devvp, dbn, (int)fs->fs_bsize, 2890 NOCRED, &bp); 2891 if (error) { 2892 brelse(bp); 2893 return (error); 2894 } 2895 } 2896 /* 2897 * Recursively free indirect blocks. 2898 */ 2899 if (ump->um_fstype == UFS1) { 2900 ufs1fmt = 1; 2901 bap1 = (ufs1_daddr_t *)bp->b_data; 2902 } else { 2903 ufs1fmt = 0; 2904 bap2 = (ufs2_daddr_t *)bp->b_data; 2905 } 2906 nblocks = btodb(fs->fs_bsize); 2907 for (i = NINDIR(fs) - 1; i >= 0; i--) { 2908 if (ufs1fmt) 2909 nb = bap1[i]; 2910 else 2911 nb = bap2[i]; 2912 if (nb == 0) 2913 continue; 2914 if (level != 0) { 2915 if ((error = indir_trunc(freeblks, fsbtodb(fs, nb), 2916 level - 1, lbn + (i * lbnadd), countp)) != 0) 2917 allerror = error; 2918 } 2919 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb, fs->fs_bsize, 2920 freeblks->fb_previousinum); 2921 fs_pendingblocks += nblocks; 2922 *countp += nblocks; 2923 } 2924 UFS_LOCK(ump); 2925 fs->fs_pendingblocks -= fs_pendingblocks; 2926 UFS_UNLOCK(ump); 2927 bp->b_flags |= B_INVAL | B_NOCACHE; 2928 brelse(bp); 2929 return (allerror); 2930 } 2931 2932 /* 2933 * Free an allocindir. 2934 * This routine must be called with splbio interrupts blocked. 2935 */ 2936 static void 2937 free_allocindir(aip, inodedep) 2938 struct allocindir *aip; 2939 struct inodedep *inodedep; 2940 { 2941 struct freefrag *freefrag; 2942 2943 mtx_assert(&lk, MA_OWNED); 2944 if ((aip->ai_state & DEPCOMPLETE) == 0) 2945 LIST_REMOVE(aip, ai_deps); 2946 if (aip->ai_state & ONWORKLIST) 2947 WORKLIST_REMOVE(&aip->ai_list); 2948 LIST_REMOVE(aip, ai_next); 2949 if ((freefrag = aip->ai_freefrag) != NULL) { 2950 if (inodedep == NULL) 2951 add_to_worklist(&freefrag->ff_list); 2952 else 2953 WORKLIST_INSERT(&inodedep->id_bufwait, 2954 &freefrag->ff_list); 2955 } 2956 WORKITEM_FREE(aip, D_ALLOCINDIR); 2957 } 2958 2959 /* 2960 * Directory entry addition dependencies. 2961 * 2962 * When adding a new directory entry, the inode (with its incremented link 2963 * count) must be written to disk before the directory entry's pointer to it. 2964 * Also, if the inode is newly allocated, the corresponding freemap must be 2965 * updated (on disk) before the directory entry's pointer. These requirements 2966 * are met via undo/redo on the directory entry's pointer, which consists 2967 * simply of the inode number. 2968 * 2969 * As directory entries are added and deleted, the free space within a 2970 * directory block can become fragmented. The ufs filesystem will compact 2971 * a fragmented directory block to make space for a new entry. When this 2972 * occurs, the offsets of previously added entries change. Any "diradd" 2973 * dependency structures corresponding to these entries must be updated with 2974 * the new offsets. 2975 */ 2976 2977 /* 2978 * This routine is called after the in-memory inode's link 2979 * count has been incremented, but before the directory entry's 2980 * pointer to the inode has been set. 2981 */ 2982 int 2983 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 2984 struct buf *bp; /* buffer containing directory block */ 2985 struct inode *dp; /* inode for directory */ 2986 off_t diroffset; /* offset of new entry in directory */ 2987 ino_t newinum; /* inode referenced by new directory entry */ 2988 struct buf *newdirbp; /* non-NULL => contents of new mkdir */ 2989 int isnewblk; /* entry is in a newly allocated block */ 2990 { 2991 int offset; /* offset of new entry within directory block */ 2992 ufs_lbn_t lbn; /* block in directory containing new entry */ 2993 struct fs *fs; 2994 struct diradd *dap; 2995 struct allocdirect *adp; 2996 struct pagedep *pagedep; 2997 struct inodedep *inodedep; 2998 struct newdirblk *newdirblk = 0; 2999 struct mkdir *mkdir1, *mkdir2; 3000 struct mount *mp; 3001 3002 /* 3003 * Whiteouts have no dependencies. 3004 */ 3005 if (newinum == WINO) { 3006 if (newdirbp != NULL) 3007 bdwrite(newdirbp); 3008 return (0); 3009 } 3010 mp = UFSTOVFS(dp->i_ump); 3011 fs = dp->i_fs; 3012 lbn = lblkno(fs, diroffset); 3013 offset = blkoff(fs, diroffset); 3014 dap = malloc(sizeof(struct diradd), M_DIRADD, 3015 M_SOFTDEP_FLAGS|M_ZERO); 3016 workitem_alloc(&dap->da_list, D_DIRADD, mp); 3017 dap->da_offset = offset; 3018 dap->da_newinum = newinum; 3019 dap->da_state = ATTACHED; 3020 if (isnewblk && lbn < NDADDR && fragoff(fs, diroffset) == 0) { 3021 newdirblk = malloc(sizeof(struct newdirblk), 3022 M_NEWDIRBLK, M_SOFTDEP_FLAGS); 3023 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 3024 } 3025 if (newdirbp == NULL) { 3026 dap->da_state |= DEPCOMPLETE; 3027 ACQUIRE_LOCK(&lk); 3028 } else { 3029 dap->da_state |= MKDIR_BODY | MKDIR_PARENT; 3030 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, 3031 M_SOFTDEP_FLAGS); 3032 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp); 3033 mkdir1->md_state = MKDIR_BODY; 3034 mkdir1->md_diradd = dap; 3035 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, 3036 M_SOFTDEP_FLAGS); 3037 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp); 3038 mkdir2->md_state = MKDIR_PARENT; 3039 mkdir2->md_diradd = dap; 3040 /* 3041 * Dependency on "." and ".." being written to disk. 3042 */ 3043 mkdir1->md_buf = newdirbp; 3044 ACQUIRE_LOCK(&lk); 3045 LIST_INSERT_HEAD(&mkdirlisthd, mkdir1, md_mkdirs); 3046 WORKLIST_INSERT(&newdirbp->b_dep, &mkdir1->md_list); 3047 FREE_LOCK(&lk); 3048 bdwrite(newdirbp); 3049 /* 3050 * Dependency on link count increase for parent directory 3051 */ 3052 ACQUIRE_LOCK(&lk); 3053 if (inodedep_lookup(mp, dp->i_number, 0, &inodedep) == 0 3054 || (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 3055 dap->da_state &= ~MKDIR_PARENT; 3056 WORKITEM_FREE(mkdir2, D_MKDIR); 3057 } else { 3058 LIST_INSERT_HEAD(&mkdirlisthd, mkdir2, md_mkdirs); 3059 WORKLIST_INSERT(&inodedep->id_bufwait,&mkdir2->md_list); 3060 } 3061 } 3062 /* 3063 * Link into parent directory pagedep to await its being written. 3064 */ 3065 if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0) 3066 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list); 3067 dap->da_pagedep = pagedep; 3068 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap, 3069 da_pdlist); 3070 /* 3071 * Link into its inodedep. Put it on the id_bufwait list if the inode 3072 * is not yet written. If it is written, do the post-inode write 3073 * processing to put it on the id_pendinghd list. 3074 */ 3075 (void) inodedep_lookup(mp, newinum, DEPALLOC, &inodedep); 3076 if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) 3077 diradd_inode_written(dap, inodedep); 3078 else 3079 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 3080 if (isnewblk) { 3081 /* 3082 * Directories growing into indirect blocks are rare 3083 * enough and the frequency of new block allocation 3084 * in those cases even more rare, that we choose not 3085 * to bother tracking them. Rather we simply force the 3086 * new directory entry to disk. 3087 */ 3088 if (lbn >= NDADDR) { 3089 FREE_LOCK(&lk); 3090 /* 3091 * We only have a new allocation when at the 3092 * beginning of a new block, not when we are 3093 * expanding into an existing block. 3094 */ 3095 if (blkoff(fs, diroffset) == 0) 3096 return (1); 3097 return (0); 3098 } 3099 /* 3100 * We only have a new allocation when at the beginning 3101 * of a new fragment, not when we are expanding into an 3102 * existing fragment. Also, there is nothing to do if we 3103 * are already tracking this block. 3104 */ 3105 if (fragoff(fs, diroffset) != 0) { 3106 FREE_LOCK(&lk); 3107 return (0); 3108 } 3109 if ((pagedep->pd_state & NEWBLOCK) != 0) { 3110 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 3111 FREE_LOCK(&lk); 3112 return (0); 3113 } 3114 /* 3115 * Find our associated allocdirect and have it track us. 3116 */ 3117 if (inodedep_lookup(mp, dp->i_number, 0, &inodedep) == 0) 3118 panic("softdep_setup_directory_add: lost inodedep"); 3119 adp = TAILQ_LAST(&inodedep->id_newinoupdt, allocdirectlst); 3120 if (adp == NULL || adp->ad_lbn != lbn) 3121 panic("softdep_setup_directory_add: lost entry"); 3122 pagedep->pd_state |= NEWBLOCK; 3123 newdirblk->db_pagedep = pagedep; 3124 WORKLIST_INSERT(&adp->ad_newdirblk, &newdirblk->db_list); 3125 } 3126 FREE_LOCK(&lk); 3127 return (0); 3128 } 3129 3130 /* 3131 * This procedure is called to change the offset of a directory 3132 * entry when compacting a directory block which must be owned 3133 * exclusively by the caller. Note that the actual entry movement 3134 * must be done in this procedure to ensure that no I/O completions 3135 * occur while the move is in progress. 3136 */ 3137 void 3138 softdep_change_directoryentry_offset(dp, base, oldloc, newloc, entrysize) 3139 struct inode *dp; /* inode for directory */ 3140 caddr_t base; /* address of dp->i_offset */ 3141 caddr_t oldloc; /* address of old directory location */ 3142 caddr_t newloc; /* address of new directory location */ 3143 int entrysize; /* size of directory entry */ 3144 { 3145 int offset, oldoffset, newoffset; 3146 struct pagedep *pagedep; 3147 struct diradd *dap; 3148 ufs_lbn_t lbn; 3149 3150 ACQUIRE_LOCK(&lk); 3151 lbn = lblkno(dp->i_fs, dp->i_offset); 3152 offset = blkoff(dp->i_fs, dp->i_offset); 3153 if (pagedep_lookup(dp, lbn, 0, &pagedep) == 0) 3154 goto done; 3155 oldoffset = offset + (oldloc - base); 3156 newoffset = offset + (newloc - base); 3157 3158 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(oldoffset)], da_pdlist) { 3159 if (dap->da_offset != oldoffset) 3160 continue; 3161 dap->da_offset = newoffset; 3162 if (DIRADDHASH(newoffset) == DIRADDHASH(oldoffset)) 3163 break; 3164 LIST_REMOVE(dap, da_pdlist); 3165 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(newoffset)], 3166 dap, da_pdlist); 3167 break; 3168 } 3169 if (dap == NULL) { 3170 3171 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) { 3172 if (dap->da_offset == oldoffset) { 3173 dap->da_offset = newoffset; 3174 break; 3175 } 3176 } 3177 } 3178 done: 3179 bcopy(oldloc, newloc, entrysize); 3180 FREE_LOCK(&lk); 3181 } 3182 3183 /* 3184 * Free a diradd dependency structure. This routine must be called 3185 * with splbio interrupts blocked. 3186 */ 3187 static void 3188 free_diradd(dap) 3189 struct diradd *dap; 3190 { 3191 struct dirrem *dirrem; 3192 struct pagedep *pagedep; 3193 struct inodedep *inodedep; 3194 struct mkdir *mkdir, *nextmd; 3195 3196 mtx_assert(&lk, MA_OWNED); 3197 WORKLIST_REMOVE(&dap->da_list); 3198 LIST_REMOVE(dap, da_pdlist); 3199 if ((dap->da_state & DIRCHG) == 0) { 3200 pagedep = dap->da_pagedep; 3201 } else { 3202 dirrem = dap->da_previous; 3203 pagedep = dirrem->dm_pagedep; 3204 dirrem->dm_dirinum = pagedep->pd_ino; 3205 add_to_worklist(&dirrem->dm_list); 3206 } 3207 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum, 3208 0, &inodedep) != 0) 3209 (void) free_inodedep(inodedep); 3210 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 3211 for (mkdir = LIST_FIRST(&mkdirlisthd); mkdir; mkdir = nextmd) { 3212 nextmd = LIST_NEXT(mkdir, md_mkdirs); 3213 if (mkdir->md_diradd != dap) 3214 continue; 3215 dap->da_state &= ~mkdir->md_state; 3216 WORKLIST_REMOVE(&mkdir->md_list); 3217 LIST_REMOVE(mkdir, md_mkdirs); 3218 WORKITEM_FREE(mkdir, D_MKDIR); 3219 } 3220 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 3221 panic("free_diradd: unfound ref"); 3222 } 3223 WORKITEM_FREE(dap, D_DIRADD); 3224 } 3225 3226 /* 3227 * Directory entry removal dependencies. 3228 * 3229 * When removing a directory entry, the entry's inode pointer must be 3230 * zero'ed on disk before the corresponding inode's link count is decremented 3231 * (possibly freeing the inode for re-use). This dependency is handled by 3232 * updating the directory entry but delaying the inode count reduction until 3233 * after the directory block has been written to disk. After this point, the 3234 * inode count can be decremented whenever it is convenient. 3235 */ 3236 3237 /* 3238 * This routine should be called immediately after removing 3239 * a directory entry. The inode's link count should not be 3240 * decremented by the calling procedure -- the soft updates 3241 * code will do this task when it is safe. 3242 */ 3243 void 3244 softdep_setup_remove(bp, dp, ip, isrmdir) 3245 struct buf *bp; /* buffer containing directory block */ 3246 struct inode *dp; /* inode for the directory being modified */ 3247 struct inode *ip; /* inode for directory entry being removed */ 3248 int isrmdir; /* indicates if doing RMDIR */ 3249 { 3250 struct dirrem *dirrem, *prevdirrem; 3251 3252 /* 3253 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. 3254 */ 3255 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 3256 3257 /* 3258 * If the COMPLETE flag is clear, then there were no active 3259 * entries and we want to roll back to a zeroed entry until 3260 * the new inode is committed to disk. If the COMPLETE flag is 3261 * set then we have deleted an entry that never made it to 3262 * disk. If the entry we deleted resulted from a name change, 3263 * then the old name still resides on disk. We cannot delete 3264 * its inode (returned to us in prevdirrem) until the zeroed 3265 * directory entry gets to disk. The new inode has never been 3266 * referenced on the disk, so can be deleted immediately. 3267 */ 3268 if ((dirrem->dm_state & COMPLETE) == 0) { 3269 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem, 3270 dm_next); 3271 FREE_LOCK(&lk); 3272 } else { 3273 if (prevdirrem != NULL) 3274 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, 3275 prevdirrem, dm_next); 3276 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino; 3277 FREE_LOCK(&lk); 3278 handle_workitem_remove(dirrem, NULL); 3279 } 3280 } 3281 3282 /* 3283 * Allocate a new dirrem if appropriate and return it along with 3284 * its associated pagedep. Called without a lock, returns with lock. 3285 */ 3286 static long num_dirrem; /* number of dirrem allocated */ 3287 static struct dirrem * 3288 newdirrem(bp, dp, ip, isrmdir, prevdirremp) 3289 struct buf *bp; /* buffer containing directory block */ 3290 struct inode *dp; /* inode for the directory being modified */ 3291 struct inode *ip; /* inode for directory entry being removed */ 3292 int isrmdir; /* indicates if doing RMDIR */ 3293 struct dirrem **prevdirremp; /* previously referenced inode, if any */ 3294 { 3295 int offset; 3296 ufs_lbn_t lbn; 3297 struct diradd *dap; 3298 struct dirrem *dirrem; 3299 struct pagedep *pagedep; 3300 3301 /* 3302 * Whiteouts have no deletion dependencies. 3303 */ 3304 if (ip == NULL) 3305 panic("newdirrem: whiteout"); 3306 /* 3307 * If we are over our limit, try to improve the situation. 3308 * Limiting the number of dirrem structures will also limit 3309 * the number of freefile and freeblks structures. 3310 */ 3311 ACQUIRE_LOCK(&lk); 3312 if (!(ip->i_flags & SF_SNAPSHOT) && num_dirrem > max_softdeps / 2) 3313 (void) request_cleanup(ITOV(dp)->v_mount, FLUSH_REMOVE); 3314 num_dirrem += 1; 3315 FREE_LOCK(&lk); 3316 dirrem = malloc(sizeof(struct dirrem), 3317 M_DIRREM, M_SOFTDEP_FLAGS|M_ZERO); 3318 workitem_alloc(&dirrem->dm_list, D_DIRREM, ITOV(dp)->v_mount); 3319 dirrem->dm_state = isrmdir ? RMDIR : 0; 3320 dirrem->dm_oldinum = ip->i_number; 3321 *prevdirremp = NULL; 3322 3323 ACQUIRE_LOCK(&lk); 3324 lbn = lblkno(dp->i_fs, dp->i_offset); 3325 offset = blkoff(dp->i_fs, dp->i_offset); 3326 if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0) 3327 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list); 3328 dirrem->dm_pagedep = pagedep; 3329 /* 3330 * Check for a diradd dependency for the same directory entry. 3331 * If present, then both dependencies become obsolete and can 3332 * be de-allocated. Check for an entry on both the pd_dirraddhd 3333 * list and the pd_pendinghd list. 3334 */ 3335 3336 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist) 3337 if (dap->da_offset == offset) 3338 break; 3339 if (dap == NULL) { 3340 3341 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 3342 if (dap->da_offset == offset) 3343 break; 3344 if (dap == NULL) 3345 return (dirrem); 3346 } 3347 /* 3348 * Must be ATTACHED at this point. 3349 */ 3350 if ((dap->da_state & ATTACHED) == 0) 3351 panic("newdirrem: not ATTACHED"); 3352 if (dap->da_newinum != ip->i_number) 3353 panic("newdirrem: inum %d should be %d", 3354 ip->i_number, dap->da_newinum); 3355 /* 3356 * If we are deleting a changed name that never made it to disk, 3357 * then return the dirrem describing the previous inode (which 3358 * represents the inode currently referenced from this entry on disk). 3359 */ 3360 if ((dap->da_state & DIRCHG) != 0) { 3361 *prevdirremp = dap->da_previous; 3362 dap->da_state &= ~DIRCHG; 3363 dap->da_pagedep = pagedep; 3364 } 3365 /* 3366 * We are deleting an entry that never made it to disk. 3367 * Mark it COMPLETE so we can delete its inode immediately. 3368 */ 3369 dirrem->dm_state |= COMPLETE; 3370 free_diradd(dap); 3371 return (dirrem); 3372 } 3373 3374 /* 3375 * Directory entry change dependencies. 3376 * 3377 * Changing an existing directory entry requires that an add operation 3378 * be completed first followed by a deletion. The semantics for the addition 3379 * are identical to the description of adding a new entry above except 3380 * that the rollback is to the old inode number rather than zero. Once 3381 * the addition dependency is completed, the removal is done as described 3382 * in the removal routine above. 3383 */ 3384 3385 /* 3386 * This routine should be called immediately after changing 3387 * a directory entry. The inode's link count should not be 3388 * decremented by the calling procedure -- the soft updates 3389 * code will perform this task when it is safe. 3390 */ 3391 void 3392 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 3393 struct buf *bp; /* buffer containing directory block */ 3394 struct inode *dp; /* inode for the directory being modified */ 3395 struct inode *ip; /* inode for directory entry being removed */ 3396 ino_t newinum; /* new inode number for changed entry */ 3397 int isrmdir; /* indicates if doing RMDIR */ 3398 { 3399 int offset; 3400 struct diradd *dap = NULL; 3401 struct dirrem *dirrem, *prevdirrem; 3402 struct pagedep *pagedep; 3403 struct inodedep *inodedep; 3404 struct mount *mp; 3405 3406 offset = blkoff(dp->i_fs, dp->i_offset); 3407 mp = UFSTOVFS(dp->i_ump); 3408 3409 /* 3410 * Whiteouts do not need diradd dependencies. 3411 */ 3412 if (newinum != WINO) { 3413 dap = malloc(sizeof(struct diradd), 3414 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO); 3415 workitem_alloc(&dap->da_list, D_DIRADD, mp); 3416 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE; 3417 dap->da_offset = offset; 3418 dap->da_newinum = newinum; 3419 } 3420 3421 /* 3422 * Allocate a new dirrem and ACQUIRE_LOCK. 3423 */ 3424 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 3425 pagedep = dirrem->dm_pagedep; 3426 /* 3427 * The possible values for isrmdir: 3428 * 0 - non-directory file rename 3429 * 1 - directory rename within same directory 3430 * inum - directory rename to new directory of given inode number 3431 * When renaming to a new directory, we are both deleting and 3432 * creating a new directory entry, so the link count on the new 3433 * directory should not change. Thus we do not need the followup 3434 * dirrem which is usually done in handle_workitem_remove. We set 3435 * the DIRCHG flag to tell handle_workitem_remove to skip the 3436 * followup dirrem. 3437 */ 3438 if (isrmdir > 1) 3439 dirrem->dm_state |= DIRCHG; 3440 3441 /* 3442 * Whiteouts have no additional dependencies, 3443 * so just put the dirrem on the correct list. 3444 */ 3445 if (newinum == WINO) { 3446 if ((dirrem->dm_state & COMPLETE) == 0) { 3447 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem, 3448 dm_next); 3449 } else { 3450 dirrem->dm_dirinum = pagedep->pd_ino; 3451 add_to_worklist(&dirrem->dm_list); 3452 } 3453 FREE_LOCK(&lk); 3454 return; 3455 } 3456 3457 /* 3458 * If the COMPLETE flag is clear, then there were no active 3459 * entries and we want to roll back to the previous inode until 3460 * the new inode is committed to disk. If the COMPLETE flag is 3461 * set, then we have deleted an entry that never made it to disk. 3462 * If the entry we deleted resulted from a name change, then the old 3463 * inode reference still resides on disk. Any rollback that we do 3464 * needs to be to that old inode (returned to us in prevdirrem). If 3465 * the entry we deleted resulted from a create, then there is 3466 * no entry on the disk, so we want to roll back to zero rather 3467 * than the uncommitted inode. In either of the COMPLETE cases we 3468 * want to immediately free the unwritten and unreferenced inode. 3469 */ 3470 if ((dirrem->dm_state & COMPLETE) == 0) { 3471 dap->da_previous = dirrem; 3472 } else { 3473 if (prevdirrem != NULL) { 3474 dap->da_previous = prevdirrem; 3475 } else { 3476 dap->da_state &= ~DIRCHG; 3477 dap->da_pagedep = pagedep; 3478 } 3479 dirrem->dm_dirinum = pagedep->pd_ino; 3480 add_to_worklist(&dirrem->dm_list); 3481 } 3482 /* 3483 * Link into its inodedep. Put it on the id_bufwait list if the inode 3484 * is not yet written. If it is written, do the post-inode write 3485 * processing to put it on the id_pendinghd list. 3486 */ 3487 if (inodedep_lookup(mp, newinum, DEPALLOC, &inodedep) == 0 || 3488 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 3489 dap->da_state |= COMPLETE; 3490 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 3491 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 3492 } else { 3493 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 3494 dap, da_pdlist); 3495 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 3496 } 3497 FREE_LOCK(&lk); 3498 } 3499 3500 /* 3501 * Called whenever the link count on an inode is changed. 3502 * It creates an inode dependency so that the new reference(s) 3503 * to the inode cannot be committed to disk until the updated 3504 * inode has been written. 3505 */ 3506 void 3507 softdep_change_linkcnt(ip) 3508 struct inode *ip; /* the inode with the increased link count */ 3509 { 3510 struct inodedep *inodedep; 3511 3512 ACQUIRE_LOCK(&lk); 3513 (void) inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 3514 DEPALLOC, &inodedep); 3515 if (ip->i_nlink < ip->i_effnlink) 3516 panic("softdep_change_linkcnt: bad delta"); 3517 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 3518 FREE_LOCK(&lk); 3519 } 3520 3521 /* 3522 * Called when the effective link count and the reference count 3523 * on an inode drops to zero. At this point there are no names 3524 * referencing the file in the filesystem and no active file 3525 * references. The space associated with the file will be freed 3526 * as soon as the necessary soft dependencies are cleared. 3527 */ 3528 void 3529 softdep_releasefile(ip) 3530 struct inode *ip; /* inode with the zero effective link count */ 3531 { 3532 struct inodedep *inodedep; 3533 struct fs *fs; 3534 int extblocks; 3535 3536 if (ip->i_effnlink > 0) 3537 panic("softdep_releasefile: file still referenced"); 3538 /* 3539 * We may be called several times as the on-disk link count 3540 * drops to zero. We only want to account for the space once. 3541 */ 3542 if (ip->i_flag & IN_SPACECOUNTED) 3543 return; 3544 /* 3545 * We have to deactivate a snapshot otherwise copyonwrites may 3546 * add blocks and the cleanup may remove blocks after we have 3547 * tried to account for them. 3548 */ 3549 if ((ip->i_flags & SF_SNAPSHOT) != 0) 3550 ffs_snapremove(ITOV(ip)); 3551 /* 3552 * If we are tracking an nlinkdelta, we have to also remember 3553 * whether we accounted for the freed space yet. 3554 */ 3555 ACQUIRE_LOCK(&lk); 3556 if ((inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0, &inodedep))) 3557 inodedep->id_state |= SPACECOUNTED; 3558 FREE_LOCK(&lk); 3559 fs = ip->i_fs; 3560 extblocks = 0; 3561 if (fs->fs_magic == FS_UFS2_MAGIC) 3562 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 3563 UFS_LOCK(ip->i_ump); 3564 ip->i_fs->fs_pendingblocks += DIP(ip, i_blocks) - extblocks; 3565 ip->i_fs->fs_pendinginodes += 1; 3566 UFS_UNLOCK(ip->i_ump); 3567 ip->i_flag |= IN_SPACECOUNTED; 3568 } 3569 3570 /* 3571 * This workitem decrements the inode's link count. 3572 * If the link count reaches zero, the file is removed. 3573 */ 3574 static void 3575 handle_workitem_remove(dirrem, xp) 3576 struct dirrem *dirrem; 3577 struct vnode *xp; 3578 { 3579 struct thread *td = curthread; 3580 struct inodedep *inodedep; 3581 struct vnode *vp; 3582 struct inode *ip; 3583 ino_t oldinum; 3584 int error; 3585 3586 if ((vp = xp) == NULL && 3587 (error = ffs_vgetf(dirrem->dm_list.wk_mp, 3588 dirrem->dm_oldinum, LK_EXCLUSIVE, &vp, FFSV_FORCEINSMQ)) != 0) { 3589 softdep_error("handle_workitem_remove: vget", error); 3590 return; 3591 } 3592 ip = VTOI(vp); 3593 ACQUIRE_LOCK(&lk); 3594 if ((inodedep_lookup(dirrem->dm_list.wk_mp, 3595 dirrem->dm_oldinum, 0, &inodedep)) == 0) 3596 panic("handle_workitem_remove: lost inodedep"); 3597 /* 3598 * Normal file deletion. 3599 */ 3600 if ((dirrem->dm_state & RMDIR) == 0) { 3601 ip->i_nlink--; 3602 DIP_SET(ip, i_nlink, ip->i_nlink); 3603 ip->i_flag |= IN_CHANGE; 3604 if (ip->i_nlink < ip->i_effnlink) 3605 panic("handle_workitem_remove: bad file delta"); 3606 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 3607 num_dirrem -= 1; 3608 WORKITEM_FREE(dirrem, D_DIRREM); 3609 FREE_LOCK(&lk); 3610 vput(vp); 3611 return; 3612 } 3613 /* 3614 * Directory deletion. Decrement reference count for both the 3615 * just deleted parent directory entry and the reference for ".". 3616 * Next truncate the directory to length zero. When the 3617 * truncation completes, arrange to have the reference count on 3618 * the parent decremented to account for the loss of "..". 3619 */ 3620 ip->i_nlink -= 2; 3621 DIP_SET(ip, i_nlink, ip->i_nlink); 3622 ip->i_flag |= IN_CHANGE; 3623 if (ip->i_nlink < ip->i_effnlink) 3624 panic("handle_workitem_remove: bad dir delta"); 3625 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 3626 FREE_LOCK(&lk); 3627 if ((error = ffs_truncate(vp, (off_t)0, 0, td->td_ucred, td)) != 0) 3628 softdep_error("handle_workitem_remove: truncate", error); 3629 ACQUIRE_LOCK(&lk); 3630 /* 3631 * Rename a directory to a new parent. Since, we are both deleting 3632 * and creating a new directory entry, the link count on the new 3633 * directory should not change. Thus we skip the followup dirrem. 3634 */ 3635 if (dirrem->dm_state & DIRCHG) { 3636 num_dirrem -= 1; 3637 WORKITEM_FREE(dirrem, D_DIRREM); 3638 FREE_LOCK(&lk); 3639 vput(vp); 3640 return; 3641 } 3642 /* 3643 * If the inodedep does not exist, then the zero'ed inode has 3644 * been written to disk. If the allocated inode has never been 3645 * written to disk, then the on-disk inode is zero'ed. In either 3646 * case we can remove the file immediately. 3647 */ 3648 dirrem->dm_state = 0; 3649 oldinum = dirrem->dm_oldinum; 3650 dirrem->dm_oldinum = dirrem->dm_dirinum; 3651 if (inodedep_lookup(dirrem->dm_list.wk_mp, oldinum, 3652 0, &inodedep) == 0 || check_inode_unwritten(inodedep)) { 3653 if (xp != NULL) 3654 add_to_worklist(&dirrem->dm_list); 3655 FREE_LOCK(&lk); 3656 vput(vp); 3657 if (xp == NULL) 3658 handle_workitem_remove(dirrem, NULL); 3659 return; 3660 } 3661 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list); 3662 FREE_LOCK(&lk); 3663 ip->i_flag |= IN_CHANGE; 3664 ffs_update(vp, 0); 3665 vput(vp); 3666 } 3667 3668 /* 3669 * Inode de-allocation dependencies. 3670 * 3671 * When an inode's link count is reduced to zero, it can be de-allocated. We 3672 * found it convenient to postpone de-allocation until after the inode is 3673 * written to disk with its new link count (zero). At this point, all of the 3674 * on-disk inode's block pointers are nullified and, with careful dependency 3675 * list ordering, all dependencies related to the inode will be satisfied and 3676 * the corresponding dependency structures de-allocated. So, if/when the 3677 * inode is reused, there will be no mixing of old dependencies with new 3678 * ones. This artificial dependency is set up by the block de-allocation 3679 * procedure above (softdep_setup_freeblocks) and completed by the 3680 * following procedure. 3681 */ 3682 static void 3683 handle_workitem_freefile(freefile) 3684 struct freefile *freefile; 3685 { 3686 struct fs *fs; 3687 struct inodedep *idp; 3688 struct ufsmount *ump; 3689 int error; 3690 3691 ump = VFSTOUFS(freefile->fx_list.wk_mp); 3692 fs = ump->um_fs; 3693 #ifdef DEBUG 3694 ACQUIRE_LOCK(&lk); 3695 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp); 3696 FREE_LOCK(&lk); 3697 if (error) 3698 panic("handle_workitem_freefile: inodedep survived"); 3699 #endif 3700 UFS_LOCK(ump); 3701 fs->fs_pendinginodes -= 1; 3702 UFS_UNLOCK(ump); 3703 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp, 3704 freefile->fx_oldinum, freefile->fx_mode)) != 0) 3705 softdep_error("handle_workitem_freefile", error); 3706 ACQUIRE_LOCK(&lk); 3707 WORKITEM_FREE(freefile, D_FREEFILE); 3708 FREE_LOCK(&lk); 3709 } 3710 3711 3712 /* 3713 * Helper function which unlinks marker element from work list and returns 3714 * the next element on the list. 3715 */ 3716 static __inline struct worklist * 3717 markernext(struct worklist *marker) 3718 { 3719 struct worklist *next; 3720 3721 next = LIST_NEXT(marker, wk_list); 3722 LIST_REMOVE(marker, wk_list); 3723 return next; 3724 } 3725 3726 /* 3727 * Disk writes. 3728 * 3729 * The dependency structures constructed above are most actively used when file 3730 * system blocks are written to disk. No constraints are placed on when a 3731 * block can be written, but unsatisfied update dependencies are made safe by 3732 * modifying (or replacing) the source memory for the duration of the disk 3733 * write. When the disk write completes, the memory block is again brought 3734 * up-to-date. 3735 * 3736 * In-core inode structure reclamation. 3737 * 3738 * Because there are a finite number of "in-core" inode structures, they are 3739 * reused regularly. By transferring all inode-related dependencies to the 3740 * in-memory inode block and indexing them separately (via "inodedep"s), we 3741 * can allow "in-core" inode structures to be reused at any time and avoid 3742 * any increase in contention. 3743 * 3744 * Called just before entering the device driver to initiate a new disk I/O. 3745 * The buffer must be locked, thus, no I/O completion operations can occur 3746 * while we are manipulating its associated dependencies. 3747 */ 3748 static void 3749 softdep_disk_io_initiation(bp) 3750 struct buf *bp; /* structure describing disk write to occur */ 3751 { 3752 struct worklist *wk; 3753 struct worklist marker; 3754 struct indirdep *indirdep; 3755 struct inodedep *inodedep; 3756 3757 /* 3758 * We only care about write operations. There should never 3759 * be dependencies for reads. 3760 */ 3761 if (bp->b_iocmd != BIO_WRITE) 3762 panic("softdep_disk_io_initiation: not write"); 3763 3764 marker.wk_type = D_LAST + 1; /* Not a normal workitem */ 3765 PHOLD(curproc); /* Don't swap out kernel stack */ 3766 3767 ACQUIRE_LOCK(&lk); 3768 /* 3769 * Do any necessary pre-I/O processing. 3770 */ 3771 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL; 3772 wk = markernext(&marker)) { 3773 LIST_INSERT_AFTER(wk, &marker, wk_list); 3774 switch (wk->wk_type) { 3775 3776 case D_PAGEDEP: 3777 initiate_write_filepage(WK_PAGEDEP(wk), bp); 3778 continue; 3779 3780 case D_INODEDEP: 3781 inodedep = WK_INODEDEP(wk); 3782 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) 3783 initiate_write_inodeblock_ufs1(inodedep, bp); 3784 else 3785 initiate_write_inodeblock_ufs2(inodedep, bp); 3786 continue; 3787 3788 case D_INDIRDEP: 3789 indirdep = WK_INDIRDEP(wk); 3790 if (indirdep->ir_state & GOINGAWAY) 3791 panic("disk_io_initiation: indirdep gone"); 3792 /* 3793 * If there are no remaining dependencies, this 3794 * will be writing the real pointers, so the 3795 * dependency can be freed. 3796 */ 3797 if (LIST_EMPTY(&indirdep->ir_deplisthd)) { 3798 struct buf *bp; 3799 3800 bp = indirdep->ir_savebp; 3801 bp->b_flags |= B_INVAL | B_NOCACHE; 3802 /* inline expand WORKLIST_REMOVE(wk); */ 3803 wk->wk_state &= ~ONWORKLIST; 3804 LIST_REMOVE(wk, wk_list); 3805 WORKITEM_FREE(indirdep, D_INDIRDEP); 3806 FREE_LOCK(&lk); 3807 brelse(bp); 3808 ACQUIRE_LOCK(&lk); 3809 continue; 3810 } 3811 /* 3812 * Replace up-to-date version with safe version. 3813 */ 3814 FREE_LOCK(&lk); 3815 indirdep->ir_saveddata = malloc(bp->b_bcount, 3816 M_INDIRDEP, M_SOFTDEP_FLAGS); 3817 ACQUIRE_LOCK(&lk); 3818 indirdep->ir_state &= ~ATTACHED; 3819 indirdep->ir_state |= UNDONE; 3820 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 3821 bcopy(indirdep->ir_savebp->b_data, bp->b_data, 3822 bp->b_bcount); 3823 continue; 3824 3825 case D_MKDIR: 3826 case D_BMSAFEMAP: 3827 case D_ALLOCDIRECT: 3828 case D_ALLOCINDIR: 3829 continue; 3830 3831 default: 3832 panic("handle_disk_io_initiation: Unexpected type %s", 3833 TYPENAME(wk->wk_type)); 3834 /* NOTREACHED */ 3835 } 3836 } 3837 FREE_LOCK(&lk); 3838 PRELE(curproc); /* Allow swapout of kernel stack */ 3839 } 3840 3841 /* 3842 * Called from within the procedure above to deal with unsatisfied 3843 * allocation dependencies in a directory. The buffer must be locked, 3844 * thus, no I/O completion operations can occur while we are 3845 * manipulating its associated dependencies. 3846 */ 3847 static void 3848 initiate_write_filepage(pagedep, bp) 3849 struct pagedep *pagedep; 3850 struct buf *bp; 3851 { 3852 struct diradd *dap; 3853 struct direct *ep; 3854 int i; 3855 3856 if (pagedep->pd_state & IOSTARTED) { 3857 /* 3858 * This can only happen if there is a driver that does not 3859 * understand chaining. Here biodone will reissue the call 3860 * to strategy for the incomplete buffers. 3861 */ 3862 printf("initiate_write_filepage: already started\n"); 3863 return; 3864 } 3865 pagedep->pd_state |= IOSTARTED; 3866 for (i = 0; i < DAHASHSZ; i++) { 3867 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 3868 ep = (struct direct *) 3869 ((char *)bp->b_data + dap->da_offset); 3870 if (ep->d_ino != dap->da_newinum) 3871 panic("%s: dir inum %d != new %d", 3872 "initiate_write_filepage", 3873 ep->d_ino, dap->da_newinum); 3874 if (dap->da_state & DIRCHG) 3875 ep->d_ino = dap->da_previous->dm_oldinum; 3876 else 3877 ep->d_ino = 0; 3878 dap->da_state &= ~ATTACHED; 3879 dap->da_state |= UNDONE; 3880 } 3881 } 3882 } 3883 3884 /* 3885 * Version of initiate_write_inodeblock that handles UFS1 dinodes. 3886 * Note that any bug fixes made to this routine must be done in the 3887 * version found below. 3888 * 3889 * Called from within the procedure above to deal with unsatisfied 3890 * allocation dependencies in an inodeblock. The buffer must be 3891 * locked, thus, no I/O completion operations can occur while we 3892 * are manipulating its associated dependencies. 3893 */ 3894 static void 3895 initiate_write_inodeblock_ufs1(inodedep, bp) 3896 struct inodedep *inodedep; 3897 struct buf *bp; /* The inode block */ 3898 { 3899 struct allocdirect *adp, *lastadp; 3900 struct ufs1_dinode *dp; 3901 struct ufs1_dinode *sip; 3902 struct fs *fs; 3903 ufs_lbn_t i; 3904 #ifdef INVARIANTS 3905 ufs_lbn_t prevlbn = 0; 3906 #endif 3907 int deplist; 3908 3909 if (inodedep->id_state & IOSTARTED) 3910 panic("initiate_write_inodeblock_ufs1: already started"); 3911 inodedep->id_state |= IOSTARTED; 3912 fs = inodedep->id_fs; 3913 dp = (struct ufs1_dinode *)bp->b_data + 3914 ino_to_fsbo(fs, inodedep->id_ino); 3915 /* 3916 * If the bitmap is not yet written, then the allocated 3917 * inode cannot be written to disk. 3918 */ 3919 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 3920 if (inodedep->id_savedino1 != NULL) 3921 panic("initiate_write_inodeblock_ufs1: I/O underway"); 3922 FREE_LOCK(&lk); 3923 sip = malloc(sizeof(struct ufs1_dinode), 3924 M_SAVEDINO, M_SOFTDEP_FLAGS); 3925 ACQUIRE_LOCK(&lk); 3926 inodedep->id_savedino1 = sip; 3927 *inodedep->id_savedino1 = *dp; 3928 bzero((caddr_t)dp, sizeof(struct ufs1_dinode)); 3929 dp->di_gen = inodedep->id_savedino1->di_gen; 3930 return; 3931 } 3932 /* 3933 * If no dependencies, then there is nothing to roll back. 3934 */ 3935 inodedep->id_savedsize = dp->di_size; 3936 inodedep->id_savedextsize = 0; 3937 if (TAILQ_EMPTY(&inodedep->id_inoupdt)) 3938 return; 3939 /* 3940 * Set the dependencies to busy. 3941 */ 3942 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 3943 adp = TAILQ_NEXT(adp, ad_next)) { 3944 #ifdef INVARIANTS 3945 if (deplist != 0 && prevlbn >= adp->ad_lbn) 3946 panic("softdep_write_inodeblock: lbn order"); 3947 prevlbn = adp->ad_lbn; 3948 if (adp->ad_lbn < NDADDR && 3949 dp->di_db[adp->ad_lbn] != adp->ad_newblkno) 3950 panic("%s: direct pointer #%jd mismatch %d != %jd", 3951 "softdep_write_inodeblock", 3952 (intmax_t)adp->ad_lbn, 3953 dp->di_db[adp->ad_lbn], 3954 (intmax_t)adp->ad_newblkno); 3955 if (adp->ad_lbn >= NDADDR && 3956 dp->di_ib[adp->ad_lbn - NDADDR] != adp->ad_newblkno) 3957 panic("%s: indirect pointer #%jd mismatch %d != %jd", 3958 "softdep_write_inodeblock", 3959 (intmax_t)adp->ad_lbn - NDADDR, 3960 dp->di_ib[adp->ad_lbn - NDADDR], 3961 (intmax_t)adp->ad_newblkno); 3962 deplist |= 1 << adp->ad_lbn; 3963 if ((adp->ad_state & ATTACHED) == 0) 3964 panic("softdep_write_inodeblock: Unknown state 0x%x", 3965 adp->ad_state); 3966 #endif /* INVARIANTS */ 3967 adp->ad_state &= ~ATTACHED; 3968 adp->ad_state |= UNDONE; 3969 } 3970 /* 3971 * The on-disk inode cannot claim to be any larger than the last 3972 * fragment that has been written. Otherwise, the on-disk inode 3973 * might have fragments that were not the last block in the file 3974 * which would corrupt the filesystem. 3975 */ 3976 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 3977 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 3978 if (adp->ad_lbn >= NDADDR) 3979 break; 3980 dp->di_db[adp->ad_lbn] = adp->ad_oldblkno; 3981 /* keep going until hitting a rollback to a frag */ 3982 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 3983 continue; 3984 dp->di_size = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize; 3985 for (i = adp->ad_lbn + 1; i < NDADDR; i++) { 3986 #ifdef INVARIANTS 3987 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 3988 panic("softdep_write_inodeblock: lost dep1"); 3989 #endif /* INVARIANTS */ 3990 dp->di_db[i] = 0; 3991 } 3992 for (i = 0; i < NIADDR; i++) { 3993 #ifdef INVARIANTS 3994 if (dp->di_ib[i] != 0 && 3995 (deplist & ((1 << NDADDR) << i)) == 0) 3996 panic("softdep_write_inodeblock: lost dep2"); 3997 #endif /* INVARIANTS */ 3998 dp->di_ib[i] = 0; 3999 } 4000 return; 4001 } 4002 /* 4003 * If we have zero'ed out the last allocated block of the file, 4004 * roll back the size to the last currently allocated block. 4005 * We know that this last allocated block is a full-sized as 4006 * we already checked for fragments in the loop above. 4007 */ 4008 if (lastadp != NULL && 4009 dp->di_size <= (lastadp->ad_lbn + 1) * fs->fs_bsize) { 4010 for (i = lastadp->ad_lbn; i >= 0; i--) 4011 if (dp->di_db[i] != 0) 4012 break; 4013 dp->di_size = (i + 1) * fs->fs_bsize; 4014 } 4015 /* 4016 * The only dependencies are for indirect blocks. 4017 * 4018 * The file size for indirect block additions is not guaranteed. 4019 * Such a guarantee would be non-trivial to achieve. The conventional 4020 * synchronous write implementation also does not make this guarantee. 4021 * Fsck should catch and fix discrepancies. Arguably, the file size 4022 * can be over-estimated without destroying integrity when the file 4023 * moves into the indirect blocks (i.e., is large). If we want to 4024 * postpone fsck, we are stuck with this argument. 4025 */ 4026 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 4027 dp->di_ib[adp->ad_lbn - NDADDR] = 0; 4028 } 4029 4030 /* 4031 * Version of initiate_write_inodeblock that handles UFS2 dinodes. 4032 * Note that any bug fixes made to this routine must be done in the 4033 * version found above. 4034 * 4035 * Called from within the procedure above to deal with unsatisfied 4036 * allocation dependencies in an inodeblock. The buffer must be 4037 * locked, thus, no I/O completion operations can occur while we 4038 * are manipulating its associated dependencies. 4039 */ 4040 static void 4041 initiate_write_inodeblock_ufs2(inodedep, bp) 4042 struct inodedep *inodedep; 4043 struct buf *bp; /* The inode block */ 4044 { 4045 struct allocdirect *adp, *lastadp; 4046 struct ufs2_dinode *dp; 4047 struct ufs2_dinode *sip; 4048 struct fs *fs; 4049 ufs_lbn_t i; 4050 #ifdef INVARIANTS 4051 ufs_lbn_t prevlbn = 0; 4052 #endif 4053 int deplist; 4054 4055 if (inodedep->id_state & IOSTARTED) 4056 panic("initiate_write_inodeblock_ufs2: already started"); 4057 inodedep->id_state |= IOSTARTED; 4058 fs = inodedep->id_fs; 4059 dp = (struct ufs2_dinode *)bp->b_data + 4060 ino_to_fsbo(fs, inodedep->id_ino); 4061 /* 4062 * If the bitmap is not yet written, then the allocated 4063 * inode cannot be written to disk. 4064 */ 4065 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 4066 if (inodedep->id_savedino2 != NULL) 4067 panic("initiate_write_inodeblock_ufs2: I/O underway"); 4068 FREE_LOCK(&lk); 4069 sip = malloc(sizeof(struct ufs2_dinode), 4070 M_SAVEDINO, M_SOFTDEP_FLAGS); 4071 ACQUIRE_LOCK(&lk); 4072 inodedep->id_savedino2 = sip; 4073 *inodedep->id_savedino2 = *dp; 4074 bzero((caddr_t)dp, sizeof(struct ufs2_dinode)); 4075 dp->di_gen = inodedep->id_savedino2->di_gen; 4076 return; 4077 } 4078 /* 4079 * If no dependencies, then there is nothing to roll back. 4080 */ 4081 inodedep->id_savedsize = dp->di_size; 4082 inodedep->id_savedextsize = dp->di_extsize; 4083 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 4084 TAILQ_EMPTY(&inodedep->id_extupdt)) 4085 return; 4086 /* 4087 * Set the ext data dependencies to busy. 4088 */ 4089 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 4090 adp = TAILQ_NEXT(adp, ad_next)) { 4091 #ifdef INVARIANTS 4092 if (deplist != 0 && prevlbn >= adp->ad_lbn) 4093 panic("softdep_write_inodeblock: lbn order"); 4094 prevlbn = adp->ad_lbn; 4095 if (dp->di_extb[adp->ad_lbn] != adp->ad_newblkno) 4096 panic("%s: direct pointer #%jd mismatch %jd != %jd", 4097 "softdep_write_inodeblock", 4098 (intmax_t)adp->ad_lbn, 4099 (intmax_t)dp->di_extb[adp->ad_lbn], 4100 (intmax_t)adp->ad_newblkno); 4101 deplist |= 1 << adp->ad_lbn; 4102 if ((adp->ad_state & ATTACHED) == 0) 4103 panic("softdep_write_inodeblock: Unknown state 0x%x", 4104 adp->ad_state); 4105 #endif /* INVARIANTS */ 4106 adp->ad_state &= ~ATTACHED; 4107 adp->ad_state |= UNDONE; 4108 } 4109 /* 4110 * The on-disk inode cannot claim to be any larger than the last 4111 * fragment that has been written. Otherwise, the on-disk inode 4112 * might have fragments that were not the last block in the ext 4113 * data which would corrupt the filesystem. 4114 */ 4115 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 4116 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 4117 dp->di_extb[adp->ad_lbn] = adp->ad_oldblkno; 4118 /* keep going until hitting a rollback to a frag */ 4119 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 4120 continue; 4121 dp->di_extsize = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize; 4122 for (i = adp->ad_lbn + 1; i < NXADDR; i++) { 4123 #ifdef INVARIANTS 4124 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0) 4125 panic("softdep_write_inodeblock: lost dep1"); 4126 #endif /* INVARIANTS */ 4127 dp->di_extb[i] = 0; 4128 } 4129 lastadp = NULL; 4130 break; 4131 } 4132 /* 4133 * If we have zero'ed out the last allocated block of the ext 4134 * data, roll back the size to the last currently allocated block. 4135 * We know that this last allocated block is a full-sized as 4136 * we already checked for fragments in the loop above. 4137 */ 4138 if (lastadp != NULL && 4139 dp->di_extsize <= (lastadp->ad_lbn + 1) * fs->fs_bsize) { 4140 for (i = lastadp->ad_lbn; i >= 0; i--) 4141 if (dp->di_extb[i] != 0) 4142 break; 4143 dp->di_extsize = (i + 1) * fs->fs_bsize; 4144 } 4145 /* 4146 * Set the file data dependencies to busy. 4147 */ 4148 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 4149 adp = TAILQ_NEXT(adp, ad_next)) { 4150 #ifdef INVARIANTS 4151 if (deplist != 0 && prevlbn >= adp->ad_lbn) 4152 panic("softdep_write_inodeblock: lbn order"); 4153 prevlbn = adp->ad_lbn; 4154 if (adp->ad_lbn < NDADDR && 4155 dp->di_db[adp->ad_lbn] != adp->ad_newblkno) 4156 panic("%s: direct pointer #%jd mismatch %jd != %jd", 4157 "softdep_write_inodeblock", 4158 (intmax_t)adp->ad_lbn, 4159 (intmax_t)dp->di_db[adp->ad_lbn], 4160 (intmax_t)adp->ad_newblkno); 4161 if (adp->ad_lbn >= NDADDR && 4162 dp->di_ib[adp->ad_lbn - NDADDR] != adp->ad_newblkno) 4163 panic("%s indirect pointer #%jd mismatch %jd != %jd", 4164 "softdep_write_inodeblock:", 4165 (intmax_t)adp->ad_lbn - NDADDR, 4166 (intmax_t)dp->di_ib[adp->ad_lbn - NDADDR], 4167 (intmax_t)adp->ad_newblkno); 4168 deplist |= 1 << adp->ad_lbn; 4169 if ((adp->ad_state & ATTACHED) == 0) 4170 panic("softdep_write_inodeblock: Unknown state 0x%x", 4171 adp->ad_state); 4172 #endif /* INVARIANTS */ 4173 adp->ad_state &= ~ATTACHED; 4174 adp->ad_state |= UNDONE; 4175 } 4176 /* 4177 * The on-disk inode cannot claim to be any larger than the last 4178 * fragment that has been written. Otherwise, the on-disk inode 4179 * might have fragments that were not the last block in the file 4180 * which would corrupt the filesystem. 4181 */ 4182 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 4183 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 4184 if (adp->ad_lbn >= NDADDR) 4185 break; 4186 dp->di_db[adp->ad_lbn] = adp->ad_oldblkno; 4187 /* keep going until hitting a rollback to a frag */ 4188 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 4189 continue; 4190 dp->di_size = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize; 4191 for (i = adp->ad_lbn + 1; i < NDADDR; i++) { 4192 #ifdef INVARIANTS 4193 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 4194 panic("softdep_write_inodeblock: lost dep2"); 4195 #endif /* INVARIANTS */ 4196 dp->di_db[i] = 0; 4197 } 4198 for (i = 0; i < NIADDR; i++) { 4199 #ifdef INVARIANTS 4200 if (dp->di_ib[i] != 0 && 4201 (deplist & ((1 << NDADDR) << i)) == 0) 4202 panic("softdep_write_inodeblock: lost dep3"); 4203 #endif /* INVARIANTS */ 4204 dp->di_ib[i] = 0; 4205 } 4206 return; 4207 } 4208 /* 4209 * If we have zero'ed out the last allocated block of the file, 4210 * roll back the size to the last currently allocated block. 4211 * We know that this last allocated block is a full-sized as 4212 * we already checked for fragments in the loop above. 4213 */ 4214 if (lastadp != NULL && 4215 dp->di_size <= (lastadp->ad_lbn + 1) * fs->fs_bsize) { 4216 for (i = lastadp->ad_lbn; i >= 0; i--) 4217 if (dp->di_db[i] != 0) 4218 break; 4219 dp->di_size = (i + 1) * fs->fs_bsize; 4220 } 4221 /* 4222 * The only dependencies are for indirect blocks. 4223 * 4224 * The file size for indirect block additions is not guaranteed. 4225 * Such a guarantee would be non-trivial to achieve. The conventional 4226 * synchronous write implementation also does not make this guarantee. 4227 * Fsck should catch and fix discrepancies. Arguably, the file size 4228 * can be over-estimated without destroying integrity when the file 4229 * moves into the indirect blocks (i.e., is large). If we want to 4230 * postpone fsck, we are stuck with this argument. 4231 */ 4232 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 4233 dp->di_ib[adp->ad_lbn - NDADDR] = 0; 4234 } 4235 4236 /* 4237 * This routine is called during the completion interrupt 4238 * service routine for a disk write (from the procedure called 4239 * by the device driver to inform the filesystem caches of 4240 * a request completion). It should be called early in this 4241 * procedure, before the block is made available to other 4242 * processes or other routines are called. 4243 */ 4244 static void 4245 softdep_disk_write_complete(bp) 4246 struct buf *bp; /* describes the completed disk write */ 4247 { 4248 struct worklist *wk; 4249 struct worklist *owk; 4250 struct workhead reattach; 4251 struct newblk *newblk; 4252 struct allocindir *aip; 4253 struct allocdirect *adp; 4254 struct indirdep *indirdep; 4255 struct inodedep *inodedep; 4256 struct bmsafemap *bmsafemap; 4257 4258 /* 4259 * If an error occurred while doing the write, then the data 4260 * has not hit the disk and the dependencies cannot be unrolled. 4261 */ 4262 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) 4263 return; 4264 LIST_INIT(&reattach); 4265 /* 4266 * This lock must not be released anywhere in this code segment. 4267 */ 4268 ACQUIRE_LOCK(&lk); 4269 owk = NULL; 4270 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) { 4271 WORKLIST_REMOVE(wk); 4272 if (wk == owk) 4273 panic("duplicate worklist: %p\n", wk); 4274 owk = wk; 4275 switch (wk->wk_type) { 4276 4277 case D_PAGEDEP: 4278 if (handle_written_filepage(WK_PAGEDEP(wk), bp)) 4279 WORKLIST_INSERT(&reattach, wk); 4280 continue; 4281 4282 case D_INODEDEP: 4283 if (handle_written_inodeblock(WK_INODEDEP(wk), bp)) 4284 WORKLIST_INSERT(&reattach, wk); 4285 continue; 4286 4287 case D_BMSAFEMAP: 4288 bmsafemap = WK_BMSAFEMAP(wk); 4289 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkhd))) { 4290 newblk->nb_state |= DEPCOMPLETE; 4291 newblk->nb_bmsafemap = NULL; 4292 LIST_REMOVE(newblk, nb_deps); 4293 } 4294 while ((adp = 4295 LIST_FIRST(&bmsafemap->sm_allocdirecthd))) { 4296 adp->ad_state |= DEPCOMPLETE; 4297 adp->ad_buf = NULL; 4298 LIST_REMOVE(adp, ad_deps); 4299 handle_allocdirect_partdone(adp); 4300 } 4301 while ((aip = 4302 LIST_FIRST(&bmsafemap->sm_allocindirhd))) { 4303 aip->ai_state |= DEPCOMPLETE; 4304 aip->ai_buf = NULL; 4305 LIST_REMOVE(aip, ai_deps); 4306 handle_allocindir_partdone(aip); 4307 } 4308 while ((inodedep = 4309 LIST_FIRST(&bmsafemap->sm_inodedephd)) != NULL) { 4310 inodedep->id_state |= DEPCOMPLETE; 4311 LIST_REMOVE(inodedep, id_deps); 4312 inodedep->id_buf = NULL; 4313 } 4314 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 4315 continue; 4316 4317 case D_MKDIR: 4318 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 4319 continue; 4320 4321 case D_ALLOCDIRECT: 4322 adp = WK_ALLOCDIRECT(wk); 4323 adp->ad_state |= COMPLETE; 4324 handle_allocdirect_partdone(adp); 4325 continue; 4326 4327 case D_ALLOCINDIR: 4328 aip = WK_ALLOCINDIR(wk); 4329 aip->ai_state |= COMPLETE; 4330 handle_allocindir_partdone(aip); 4331 continue; 4332 4333 case D_INDIRDEP: 4334 indirdep = WK_INDIRDEP(wk); 4335 if (indirdep->ir_state & GOINGAWAY) 4336 panic("disk_write_complete: indirdep gone"); 4337 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount); 4338 free(indirdep->ir_saveddata, M_INDIRDEP); 4339 indirdep->ir_saveddata = 0; 4340 indirdep->ir_state &= ~UNDONE; 4341 indirdep->ir_state |= ATTACHED; 4342 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0) { 4343 handle_allocindir_partdone(aip); 4344 if (aip == LIST_FIRST(&indirdep->ir_donehd)) 4345 panic("disk_write_complete: not gone"); 4346 } 4347 WORKLIST_INSERT(&reattach, wk); 4348 if ((bp->b_flags & B_DELWRI) == 0) 4349 stat_indir_blk_ptrs++; 4350 bdirty(bp); 4351 continue; 4352 4353 default: 4354 panic("handle_disk_write_complete: Unknown type %s", 4355 TYPENAME(wk->wk_type)); 4356 /* NOTREACHED */ 4357 } 4358 } 4359 /* 4360 * Reattach any requests that must be redone. 4361 */ 4362 while ((wk = LIST_FIRST(&reattach)) != NULL) { 4363 WORKLIST_REMOVE(wk); 4364 WORKLIST_INSERT(&bp->b_dep, wk); 4365 } 4366 FREE_LOCK(&lk); 4367 } 4368 4369 /* 4370 * Called from within softdep_disk_write_complete above. Note that 4371 * this routine is always called from interrupt level with further 4372 * splbio interrupts blocked. 4373 */ 4374 static void 4375 handle_allocdirect_partdone(adp) 4376 struct allocdirect *adp; /* the completed allocdirect */ 4377 { 4378 struct allocdirectlst *listhead; 4379 struct allocdirect *listadp; 4380 struct inodedep *inodedep; 4381 long bsize, delay; 4382 4383 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 4384 return; 4385 if (adp->ad_buf != NULL) 4386 panic("handle_allocdirect_partdone: dangling dep"); 4387 /* 4388 * The on-disk inode cannot claim to be any larger than the last 4389 * fragment that has been written. Otherwise, the on-disk inode 4390 * might have fragments that were not the last block in the file 4391 * which would corrupt the filesystem. Thus, we cannot free any 4392 * allocdirects after one whose ad_oldblkno claims a fragment as 4393 * these blocks must be rolled back to zero before writing the inode. 4394 * We check the currently active set of allocdirects in id_inoupdt 4395 * or id_extupdt as appropriate. 4396 */ 4397 inodedep = adp->ad_inodedep; 4398 bsize = inodedep->id_fs->fs_bsize; 4399 if (adp->ad_state & EXTDATA) 4400 listhead = &inodedep->id_extupdt; 4401 else 4402 listhead = &inodedep->id_inoupdt; 4403 TAILQ_FOREACH(listadp, listhead, ad_next) { 4404 /* found our block */ 4405 if (listadp == adp) 4406 break; 4407 /* continue if ad_oldlbn is not a fragment */ 4408 if (listadp->ad_oldsize == 0 || 4409 listadp->ad_oldsize == bsize) 4410 continue; 4411 /* hit a fragment */ 4412 return; 4413 } 4414 /* 4415 * If we have reached the end of the current list without 4416 * finding the just finished dependency, then it must be 4417 * on the future dependency list. Future dependencies cannot 4418 * be freed until they are moved to the current list. 4419 */ 4420 if (listadp == NULL) { 4421 #ifdef DEBUG 4422 if (adp->ad_state & EXTDATA) 4423 listhead = &inodedep->id_newextupdt; 4424 else 4425 listhead = &inodedep->id_newinoupdt; 4426 TAILQ_FOREACH(listadp, listhead, ad_next) 4427 /* found our block */ 4428 if (listadp == adp) 4429 break; 4430 if (listadp == NULL) 4431 panic("handle_allocdirect_partdone: lost dep"); 4432 #endif /* DEBUG */ 4433 return; 4434 } 4435 /* 4436 * If we have found the just finished dependency, then free 4437 * it along with anything that follows it that is complete. 4438 * If the inode still has a bitmap dependency, then it has 4439 * never been written to disk, hence the on-disk inode cannot 4440 * reference the old fragment so we can free it without delay. 4441 */ 4442 delay = (inodedep->id_state & DEPCOMPLETE); 4443 for (; adp; adp = listadp) { 4444 listadp = TAILQ_NEXT(adp, ad_next); 4445 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 4446 return; 4447 free_allocdirect(listhead, adp, delay); 4448 } 4449 } 4450 4451 /* 4452 * Called from within softdep_disk_write_complete above. Note that 4453 * this routine is always called from interrupt level with further 4454 * splbio interrupts blocked. 4455 */ 4456 static void 4457 handle_allocindir_partdone(aip) 4458 struct allocindir *aip; /* the completed allocindir */ 4459 { 4460 struct indirdep *indirdep; 4461 4462 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE) 4463 return; 4464 if (aip->ai_buf != NULL) 4465 panic("handle_allocindir_partdone: dangling dependency"); 4466 indirdep = aip->ai_indirdep; 4467 if (indirdep->ir_state & UNDONE) { 4468 LIST_REMOVE(aip, ai_next); 4469 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next); 4470 return; 4471 } 4472 if (indirdep->ir_state & UFS1FMT) 4473 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 4474 aip->ai_newblkno; 4475 else 4476 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 4477 aip->ai_newblkno; 4478 LIST_REMOVE(aip, ai_next); 4479 if (aip->ai_freefrag != NULL) 4480 add_to_worklist(&aip->ai_freefrag->ff_list); 4481 WORKITEM_FREE(aip, D_ALLOCINDIR); 4482 } 4483 4484 /* 4485 * Called from within softdep_disk_write_complete above to restore 4486 * in-memory inode block contents to their most up-to-date state. Note 4487 * that this routine is always called from interrupt level with further 4488 * splbio interrupts blocked. 4489 */ 4490 static int 4491 handle_written_inodeblock(inodedep, bp) 4492 struct inodedep *inodedep; 4493 struct buf *bp; /* buffer containing the inode block */ 4494 { 4495 struct worklist *wk, *filefree; 4496 struct allocdirect *adp, *nextadp; 4497 struct ufs1_dinode *dp1 = NULL; 4498 struct ufs2_dinode *dp2 = NULL; 4499 int hadchanges, fstype; 4500 4501 if ((inodedep->id_state & IOSTARTED) == 0) 4502 panic("handle_written_inodeblock: not started"); 4503 inodedep->id_state &= ~IOSTARTED; 4504 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) { 4505 fstype = UFS1; 4506 dp1 = (struct ufs1_dinode *)bp->b_data + 4507 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 4508 } else { 4509 fstype = UFS2; 4510 dp2 = (struct ufs2_dinode *)bp->b_data + 4511 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 4512 } 4513 /* 4514 * If we had to rollback the inode allocation because of 4515 * bitmaps being incomplete, then simply restore it. 4516 * Keep the block dirty so that it will not be reclaimed until 4517 * all associated dependencies have been cleared and the 4518 * corresponding updates written to disk. 4519 */ 4520 if (inodedep->id_savedino1 != NULL) { 4521 if (fstype == UFS1) 4522 *dp1 = *inodedep->id_savedino1; 4523 else 4524 *dp2 = *inodedep->id_savedino2; 4525 free(inodedep->id_savedino1, M_SAVEDINO); 4526 inodedep->id_savedino1 = NULL; 4527 if ((bp->b_flags & B_DELWRI) == 0) 4528 stat_inode_bitmap++; 4529 bdirty(bp); 4530 return (1); 4531 } 4532 inodedep->id_state |= COMPLETE; 4533 /* 4534 * Roll forward anything that had to be rolled back before 4535 * the inode could be updated. 4536 */ 4537 hadchanges = 0; 4538 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) { 4539 nextadp = TAILQ_NEXT(adp, ad_next); 4540 if (adp->ad_state & ATTACHED) 4541 panic("handle_written_inodeblock: new entry"); 4542 if (fstype == UFS1) { 4543 if (adp->ad_lbn < NDADDR) { 4544 if (dp1->di_db[adp->ad_lbn]!=adp->ad_oldblkno) 4545 panic("%s %s #%jd mismatch %d != %jd", 4546 "handle_written_inodeblock:", 4547 "direct pointer", 4548 (intmax_t)adp->ad_lbn, 4549 dp1->di_db[adp->ad_lbn], 4550 (intmax_t)adp->ad_oldblkno); 4551 dp1->di_db[adp->ad_lbn] = adp->ad_newblkno; 4552 } else { 4553 if (dp1->di_ib[adp->ad_lbn - NDADDR] != 0) 4554 panic("%s: %s #%jd allocated as %d", 4555 "handle_written_inodeblock", 4556 "indirect pointer", 4557 (intmax_t)adp->ad_lbn - NDADDR, 4558 dp1->di_ib[adp->ad_lbn - NDADDR]); 4559 dp1->di_ib[adp->ad_lbn - NDADDR] = 4560 adp->ad_newblkno; 4561 } 4562 } else { 4563 if (adp->ad_lbn < NDADDR) { 4564 if (dp2->di_db[adp->ad_lbn]!=adp->ad_oldblkno) 4565 panic("%s: %s #%jd %s %jd != %jd", 4566 "handle_written_inodeblock", 4567 "direct pointer", 4568 (intmax_t)adp->ad_lbn, "mismatch", 4569 (intmax_t)dp2->di_db[adp->ad_lbn], 4570 (intmax_t)adp->ad_oldblkno); 4571 dp2->di_db[adp->ad_lbn] = adp->ad_newblkno; 4572 } else { 4573 if (dp2->di_ib[adp->ad_lbn - NDADDR] != 0) 4574 panic("%s: %s #%jd allocated as %jd", 4575 "handle_written_inodeblock", 4576 "indirect pointer", 4577 (intmax_t)adp->ad_lbn - NDADDR, 4578 (intmax_t) 4579 dp2->di_ib[adp->ad_lbn - NDADDR]); 4580 dp2->di_ib[adp->ad_lbn - NDADDR] = 4581 adp->ad_newblkno; 4582 } 4583 } 4584 adp->ad_state &= ~UNDONE; 4585 adp->ad_state |= ATTACHED; 4586 hadchanges = 1; 4587 } 4588 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) { 4589 nextadp = TAILQ_NEXT(adp, ad_next); 4590 if (adp->ad_state & ATTACHED) 4591 panic("handle_written_inodeblock: new entry"); 4592 if (dp2->di_extb[adp->ad_lbn] != adp->ad_oldblkno) 4593 panic("%s: direct pointers #%jd %s %jd != %jd", 4594 "handle_written_inodeblock", 4595 (intmax_t)adp->ad_lbn, "mismatch", 4596 (intmax_t)dp2->di_extb[adp->ad_lbn], 4597 (intmax_t)adp->ad_oldblkno); 4598 dp2->di_extb[adp->ad_lbn] = adp->ad_newblkno; 4599 adp->ad_state &= ~UNDONE; 4600 adp->ad_state |= ATTACHED; 4601 hadchanges = 1; 4602 } 4603 if (hadchanges && (bp->b_flags & B_DELWRI) == 0) 4604 stat_direct_blk_ptrs++; 4605 /* 4606 * Reset the file size to its most up-to-date value. 4607 */ 4608 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1) 4609 panic("handle_written_inodeblock: bad size"); 4610 if (fstype == UFS1) { 4611 if (dp1->di_size != inodedep->id_savedsize) { 4612 dp1->di_size = inodedep->id_savedsize; 4613 hadchanges = 1; 4614 } 4615 } else { 4616 if (dp2->di_size != inodedep->id_savedsize) { 4617 dp2->di_size = inodedep->id_savedsize; 4618 hadchanges = 1; 4619 } 4620 if (dp2->di_extsize != inodedep->id_savedextsize) { 4621 dp2->di_extsize = inodedep->id_savedextsize; 4622 hadchanges = 1; 4623 } 4624 } 4625 inodedep->id_savedsize = -1; 4626 inodedep->id_savedextsize = -1; 4627 /* 4628 * If there were any rollbacks in the inode block, then it must be 4629 * marked dirty so that its will eventually get written back in 4630 * its correct form. 4631 */ 4632 if (hadchanges) 4633 bdirty(bp); 4634 /* 4635 * Process any allocdirects that completed during the update. 4636 */ 4637 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 4638 handle_allocdirect_partdone(adp); 4639 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 4640 handle_allocdirect_partdone(adp); 4641 /* 4642 * Process deallocations that were held pending until the 4643 * inode had been written to disk. Freeing of the inode 4644 * is delayed until after all blocks have been freed to 4645 * avoid creation of new <vfsid, inum, lbn> triples 4646 * before the old ones have been deleted. 4647 */ 4648 filefree = NULL; 4649 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) { 4650 WORKLIST_REMOVE(wk); 4651 switch (wk->wk_type) { 4652 4653 case D_FREEFILE: 4654 /* 4655 * We defer adding filefree to the worklist until 4656 * all other additions have been made to ensure 4657 * that it will be done after all the old blocks 4658 * have been freed. 4659 */ 4660 if (filefree != NULL) 4661 panic("handle_written_inodeblock: filefree"); 4662 filefree = wk; 4663 continue; 4664 4665 case D_MKDIR: 4666 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT); 4667 continue; 4668 4669 case D_DIRADD: 4670 diradd_inode_written(WK_DIRADD(wk), inodedep); 4671 continue; 4672 4673 case D_FREEBLKS: 4674 wk->wk_state |= COMPLETE; 4675 if ((wk->wk_state & ALLCOMPLETE) != ALLCOMPLETE) 4676 continue; 4677 /* -- fall through -- */ 4678 case D_FREEFRAG: 4679 case D_DIRREM: 4680 add_to_worklist(wk); 4681 continue; 4682 4683 case D_NEWDIRBLK: 4684 free_newdirblk(WK_NEWDIRBLK(wk)); 4685 continue; 4686 4687 default: 4688 panic("handle_written_inodeblock: Unknown type %s", 4689 TYPENAME(wk->wk_type)); 4690 /* NOTREACHED */ 4691 } 4692 } 4693 if (filefree != NULL) { 4694 if (free_inodedep(inodedep) == 0) 4695 panic("handle_written_inodeblock: live inodedep"); 4696 add_to_worklist(filefree); 4697 return (0); 4698 } 4699 4700 /* 4701 * If no outstanding dependencies, free it. 4702 */ 4703 if (free_inodedep(inodedep) || 4704 (TAILQ_FIRST(&inodedep->id_inoupdt) == 0 && 4705 TAILQ_FIRST(&inodedep->id_extupdt) == 0)) 4706 return (0); 4707 return (hadchanges); 4708 } 4709 4710 /* 4711 * Process a diradd entry after its dependent inode has been written. 4712 * This routine must be called with splbio interrupts blocked. 4713 */ 4714 static void 4715 diradd_inode_written(dap, inodedep) 4716 struct diradd *dap; 4717 struct inodedep *inodedep; 4718 { 4719 struct pagedep *pagedep; 4720 4721 dap->da_state |= COMPLETE; 4722 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 4723 if (dap->da_state & DIRCHG) 4724 pagedep = dap->da_previous->dm_pagedep; 4725 else 4726 pagedep = dap->da_pagedep; 4727 LIST_REMOVE(dap, da_pdlist); 4728 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 4729 } 4730 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 4731 } 4732 4733 /* 4734 * Handle the completion of a mkdir dependency. 4735 */ 4736 static void 4737 handle_written_mkdir(mkdir, type) 4738 struct mkdir *mkdir; 4739 int type; 4740 { 4741 struct diradd *dap; 4742 struct pagedep *pagedep; 4743 4744 if (mkdir->md_state != type) 4745 panic("handle_written_mkdir: bad type"); 4746 dap = mkdir->md_diradd; 4747 dap->da_state &= ~type; 4748 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) 4749 dap->da_state |= DEPCOMPLETE; 4750 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 4751 if (dap->da_state & DIRCHG) 4752 pagedep = dap->da_previous->dm_pagedep; 4753 else 4754 pagedep = dap->da_pagedep; 4755 LIST_REMOVE(dap, da_pdlist); 4756 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 4757 } 4758 LIST_REMOVE(mkdir, md_mkdirs); 4759 WORKITEM_FREE(mkdir, D_MKDIR); 4760 } 4761 4762 /* 4763 * Called from within softdep_disk_write_complete above. 4764 * A write operation was just completed. Removed inodes can 4765 * now be freed and associated block pointers may be committed. 4766 * Note that this routine is always called from interrupt level 4767 * with further splbio interrupts blocked. 4768 */ 4769 static int 4770 handle_written_filepage(pagedep, bp) 4771 struct pagedep *pagedep; 4772 struct buf *bp; /* buffer containing the written page */ 4773 { 4774 struct dirrem *dirrem; 4775 struct diradd *dap, *nextdap; 4776 struct direct *ep; 4777 int i, chgs; 4778 4779 if ((pagedep->pd_state & IOSTARTED) == 0) 4780 panic("handle_written_filepage: not started"); 4781 pagedep->pd_state &= ~IOSTARTED; 4782 /* 4783 * Process any directory removals that have been committed. 4784 */ 4785 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) { 4786 LIST_REMOVE(dirrem, dm_next); 4787 dirrem->dm_dirinum = pagedep->pd_ino; 4788 add_to_worklist(&dirrem->dm_list); 4789 } 4790 /* 4791 * Free any directory additions that have been committed. 4792 * If it is a newly allocated block, we have to wait until 4793 * the on-disk directory inode claims the new block. 4794 */ 4795 if ((pagedep->pd_state & NEWBLOCK) == 0) 4796 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 4797 free_diradd(dap); 4798 /* 4799 * Uncommitted directory entries must be restored. 4800 */ 4801 for (chgs = 0, i = 0; i < DAHASHSZ; i++) { 4802 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap; 4803 dap = nextdap) { 4804 nextdap = LIST_NEXT(dap, da_pdlist); 4805 if (dap->da_state & ATTACHED) 4806 panic("handle_written_filepage: attached"); 4807 ep = (struct direct *) 4808 ((char *)bp->b_data + dap->da_offset); 4809 ep->d_ino = dap->da_newinum; 4810 dap->da_state &= ~UNDONE; 4811 dap->da_state |= ATTACHED; 4812 chgs = 1; 4813 /* 4814 * If the inode referenced by the directory has 4815 * been written out, then the dependency can be 4816 * moved to the pending list. 4817 */ 4818 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 4819 LIST_REMOVE(dap, da_pdlist); 4820 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, 4821 da_pdlist); 4822 } 4823 } 4824 } 4825 /* 4826 * If there were any rollbacks in the directory, then it must be 4827 * marked dirty so that its will eventually get written back in 4828 * its correct form. 4829 */ 4830 if (chgs) { 4831 if ((bp->b_flags & B_DELWRI) == 0) 4832 stat_dir_entry++; 4833 bdirty(bp); 4834 return (1); 4835 } 4836 /* 4837 * If we are not waiting for a new directory block to be 4838 * claimed by its inode, then the pagedep will be freed. 4839 * Otherwise it will remain to track any new entries on 4840 * the page in case they are fsync'ed. 4841 */ 4842 if ((pagedep->pd_state & NEWBLOCK) == 0) { 4843 LIST_REMOVE(pagedep, pd_hash); 4844 WORKITEM_FREE(pagedep, D_PAGEDEP); 4845 } 4846 return (0); 4847 } 4848 4849 /* 4850 * Writing back in-core inode structures. 4851 * 4852 * The filesystem only accesses an inode's contents when it occupies an 4853 * "in-core" inode structure. These "in-core" structures are separate from 4854 * the page frames used to cache inode blocks. Only the latter are 4855 * transferred to/from the disk. So, when the updated contents of the 4856 * "in-core" inode structure are copied to the corresponding in-memory inode 4857 * block, the dependencies are also transferred. The following procedure is 4858 * called when copying a dirty "in-core" inode to a cached inode block. 4859 */ 4860 4861 /* 4862 * Called when an inode is loaded from disk. If the effective link count 4863 * differed from the actual link count when it was last flushed, then we 4864 * need to ensure that the correct effective link count is put back. 4865 */ 4866 void 4867 softdep_load_inodeblock(ip) 4868 struct inode *ip; /* the "in_core" copy of the inode */ 4869 { 4870 struct inodedep *inodedep; 4871 4872 /* 4873 * Check for alternate nlink count. 4874 */ 4875 ip->i_effnlink = ip->i_nlink; 4876 ACQUIRE_LOCK(&lk); 4877 if (inodedep_lookup(UFSTOVFS(ip->i_ump), 4878 ip->i_number, 0, &inodedep) == 0) { 4879 FREE_LOCK(&lk); 4880 return; 4881 } 4882 ip->i_effnlink -= inodedep->id_nlinkdelta; 4883 if (inodedep->id_state & SPACECOUNTED) 4884 ip->i_flag |= IN_SPACECOUNTED; 4885 FREE_LOCK(&lk); 4886 } 4887 4888 /* 4889 * This routine is called just before the "in-core" inode 4890 * information is to be copied to the in-memory inode block. 4891 * Recall that an inode block contains several inodes. If 4892 * the force flag is set, then the dependencies will be 4893 * cleared so that the update can always be made. Note that 4894 * the buffer is locked when this routine is called, so we 4895 * will never be in the middle of writing the inode block 4896 * to disk. 4897 */ 4898 void 4899 softdep_update_inodeblock(ip, bp, waitfor) 4900 struct inode *ip; /* the "in_core" copy of the inode */ 4901 struct buf *bp; /* the buffer containing the inode block */ 4902 int waitfor; /* nonzero => update must be allowed */ 4903 { 4904 struct inodedep *inodedep; 4905 struct worklist *wk; 4906 struct mount *mp; 4907 struct buf *ibp; 4908 int error; 4909 4910 /* 4911 * If the effective link count is not equal to the actual link 4912 * count, then we must track the difference in an inodedep while 4913 * the inode is (potentially) tossed out of the cache. Otherwise, 4914 * if there is no existing inodedep, then there are no dependencies 4915 * to track. 4916 */ 4917 mp = UFSTOVFS(ip->i_ump); 4918 ACQUIRE_LOCK(&lk); 4919 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 4920 FREE_LOCK(&lk); 4921 if (ip->i_effnlink != ip->i_nlink) 4922 panic("softdep_update_inodeblock: bad link count"); 4923 return; 4924 } 4925 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink) 4926 panic("softdep_update_inodeblock: bad delta"); 4927 /* 4928 * Changes have been initiated. Anything depending on these 4929 * changes cannot occur until this inode has been written. 4930 */ 4931 inodedep->id_state &= ~COMPLETE; 4932 if ((inodedep->id_state & ONWORKLIST) == 0) 4933 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list); 4934 /* 4935 * Any new dependencies associated with the incore inode must 4936 * now be moved to the list associated with the buffer holding 4937 * the in-memory copy of the inode. Once merged process any 4938 * allocdirects that are completed by the merger. 4939 */ 4940 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt); 4941 if (!TAILQ_EMPTY(&inodedep->id_inoupdt)) 4942 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt)); 4943 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt); 4944 if (!TAILQ_EMPTY(&inodedep->id_extupdt)) 4945 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt)); 4946 /* 4947 * Now that the inode has been pushed into the buffer, the 4948 * operations dependent on the inode being written to disk 4949 * can be moved to the id_bufwait so that they will be 4950 * processed when the buffer I/O completes. 4951 */ 4952 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) { 4953 WORKLIST_REMOVE(wk); 4954 WORKLIST_INSERT(&inodedep->id_bufwait, wk); 4955 } 4956 /* 4957 * Newly allocated inodes cannot be written until the bitmap 4958 * that allocates them have been written (indicated by 4959 * DEPCOMPLETE being set in id_state). If we are doing a 4960 * forced sync (e.g., an fsync on a file), we force the bitmap 4961 * to be written so that the update can be done. 4962 */ 4963 if (waitfor == 0) { 4964 FREE_LOCK(&lk); 4965 return; 4966 } 4967 retry: 4968 if ((inodedep->id_state & DEPCOMPLETE) != 0) { 4969 FREE_LOCK(&lk); 4970 return; 4971 } 4972 ibp = inodedep->id_buf; 4973 ibp = getdirtybuf(ibp, &lk, MNT_WAIT); 4974 if (ibp == NULL) { 4975 /* 4976 * If ibp came back as NULL, the dependency could have been 4977 * freed while we slept. Look it up again, and check to see 4978 * that it has completed. 4979 */ 4980 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 4981 goto retry; 4982 FREE_LOCK(&lk); 4983 return; 4984 } 4985 FREE_LOCK(&lk); 4986 if ((error = bwrite(ibp)) != 0) 4987 softdep_error("softdep_update_inodeblock: bwrite", error); 4988 } 4989 4990 /* 4991 * Merge the a new inode dependency list (such as id_newinoupdt) into an 4992 * old inode dependency list (such as id_inoupdt). This routine must be 4993 * called with splbio interrupts blocked. 4994 */ 4995 static void 4996 merge_inode_lists(newlisthead, oldlisthead) 4997 struct allocdirectlst *newlisthead; 4998 struct allocdirectlst *oldlisthead; 4999 { 5000 struct allocdirect *listadp, *newadp; 5001 5002 newadp = TAILQ_FIRST(newlisthead); 5003 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) { 5004 if (listadp->ad_lbn < newadp->ad_lbn) { 5005 listadp = TAILQ_NEXT(listadp, ad_next); 5006 continue; 5007 } 5008 TAILQ_REMOVE(newlisthead, newadp, ad_next); 5009 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next); 5010 if (listadp->ad_lbn == newadp->ad_lbn) { 5011 allocdirect_merge(oldlisthead, newadp, 5012 listadp); 5013 listadp = newadp; 5014 } 5015 newadp = TAILQ_FIRST(newlisthead); 5016 } 5017 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) { 5018 TAILQ_REMOVE(newlisthead, newadp, ad_next); 5019 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next); 5020 } 5021 } 5022 5023 /* 5024 * If we are doing an fsync, then we must ensure that any directory 5025 * entries for the inode have been written after the inode gets to disk. 5026 */ 5027 int 5028 softdep_fsync(vp) 5029 struct vnode *vp; /* the "in_core" copy of the inode */ 5030 { 5031 struct inodedep *inodedep; 5032 struct pagedep *pagedep; 5033 struct worklist *wk; 5034 struct diradd *dap; 5035 struct mount *mp; 5036 struct vnode *pvp; 5037 struct inode *ip; 5038 struct buf *bp; 5039 struct fs *fs; 5040 struct thread *td = curthread; 5041 int error, flushparent, pagedep_new_block; 5042 ino_t parentino; 5043 ufs_lbn_t lbn; 5044 5045 ip = VTOI(vp); 5046 fs = ip->i_fs; 5047 mp = vp->v_mount; 5048 ACQUIRE_LOCK(&lk); 5049 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 5050 FREE_LOCK(&lk); 5051 return (0); 5052 } 5053 if (!LIST_EMPTY(&inodedep->id_inowait) || 5054 !LIST_EMPTY(&inodedep->id_bufwait) || 5055 !TAILQ_EMPTY(&inodedep->id_extupdt) || 5056 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 5057 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 5058 !TAILQ_EMPTY(&inodedep->id_newinoupdt)) 5059 panic("softdep_fsync: pending ops"); 5060 for (error = 0, flushparent = 0; ; ) { 5061 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL) 5062 break; 5063 if (wk->wk_type != D_DIRADD) 5064 panic("softdep_fsync: Unexpected type %s", 5065 TYPENAME(wk->wk_type)); 5066 dap = WK_DIRADD(wk); 5067 /* 5068 * Flush our parent if this directory entry has a MKDIR_PARENT 5069 * dependency or is contained in a newly allocated block. 5070 */ 5071 if (dap->da_state & DIRCHG) 5072 pagedep = dap->da_previous->dm_pagedep; 5073 else 5074 pagedep = dap->da_pagedep; 5075 parentino = pagedep->pd_ino; 5076 lbn = pagedep->pd_lbn; 5077 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) 5078 panic("softdep_fsync: dirty"); 5079 if ((dap->da_state & MKDIR_PARENT) || 5080 (pagedep->pd_state & NEWBLOCK)) 5081 flushparent = 1; 5082 else 5083 flushparent = 0; 5084 /* 5085 * If we are being fsync'ed as part of vgone'ing this vnode, 5086 * then we will not be able to release and recover the 5087 * vnode below, so we just have to give up on writing its 5088 * directory entry out. It will eventually be written, just 5089 * not now, but then the user was not asking to have it 5090 * written, so we are not breaking any promises. 5091 */ 5092 if (vp->v_iflag & VI_DOOMED) 5093 break; 5094 /* 5095 * We prevent deadlock by always fetching inodes from the 5096 * root, moving down the directory tree. Thus, when fetching 5097 * our parent directory, we first try to get the lock. If 5098 * that fails, we must unlock ourselves before requesting 5099 * the lock on our parent. See the comment in ufs_lookup 5100 * for details on possible races. 5101 */ 5102 FREE_LOCK(&lk); 5103 if (ffs_vgetf(mp, parentino, LK_NOWAIT | LK_EXCLUSIVE, &pvp, 5104 FFSV_FORCEINSMQ)) { 5105 VOP_UNLOCK(vp, 0); 5106 error = ffs_vgetf(mp, parentino, LK_EXCLUSIVE, 5107 &pvp, FFSV_FORCEINSMQ); 5108 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 5109 if (error != 0) 5110 return (error); 5111 } 5112 /* 5113 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps 5114 * that are contained in direct blocks will be resolved by 5115 * doing a ffs_update. Pagedeps contained in indirect blocks 5116 * may require a complete sync'ing of the directory. So, we 5117 * try the cheap and fast ffs_update first, and if that fails, 5118 * then we do the slower ffs_syncvnode of the directory. 5119 */ 5120 if (flushparent) { 5121 int locked; 5122 5123 if ((error = ffs_update(pvp, 1)) != 0) { 5124 vput(pvp); 5125 return (error); 5126 } 5127 ACQUIRE_LOCK(&lk); 5128 locked = 1; 5129 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) { 5130 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) { 5131 if (wk->wk_type != D_DIRADD) 5132 panic("softdep_fsync: Unexpected type %s", 5133 TYPENAME(wk->wk_type)); 5134 dap = WK_DIRADD(wk); 5135 if (dap->da_state & DIRCHG) 5136 pagedep = dap->da_previous->dm_pagedep; 5137 else 5138 pagedep = dap->da_pagedep; 5139 pagedep_new_block = pagedep->pd_state & NEWBLOCK; 5140 FREE_LOCK(&lk); 5141 locked = 0; 5142 if (pagedep_new_block && 5143 (error = ffs_syncvnode(pvp, MNT_WAIT))) { 5144 vput(pvp); 5145 return (error); 5146 } 5147 } 5148 } 5149 if (locked) 5150 FREE_LOCK(&lk); 5151 } 5152 /* 5153 * Flush directory page containing the inode's name. 5154 */ 5155 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred, 5156 &bp); 5157 if (error == 0) 5158 error = bwrite(bp); 5159 else 5160 brelse(bp); 5161 vput(pvp); 5162 if (error != 0) 5163 return (error); 5164 ACQUIRE_LOCK(&lk); 5165 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 5166 break; 5167 } 5168 FREE_LOCK(&lk); 5169 return (0); 5170 } 5171 5172 /* 5173 * Flush all the dirty bitmaps associated with the block device 5174 * before flushing the rest of the dirty blocks so as to reduce 5175 * the number of dependencies that will have to be rolled back. 5176 */ 5177 void 5178 softdep_fsync_mountdev(vp) 5179 struct vnode *vp; 5180 { 5181 struct buf *bp, *nbp; 5182 struct worklist *wk; 5183 struct bufobj *bo; 5184 5185 if (!vn_isdisk(vp, NULL)) 5186 panic("softdep_fsync_mountdev: vnode not a disk"); 5187 bo = &vp->v_bufobj; 5188 restart: 5189 BO_LOCK(bo); 5190 ACQUIRE_LOCK(&lk); 5191 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 5192 /* 5193 * If it is already scheduled, skip to the next buffer. 5194 */ 5195 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) 5196 continue; 5197 5198 if ((bp->b_flags & B_DELWRI) == 0) 5199 panic("softdep_fsync_mountdev: not dirty"); 5200 /* 5201 * We are only interested in bitmaps with outstanding 5202 * dependencies. 5203 */ 5204 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL || 5205 wk->wk_type != D_BMSAFEMAP || 5206 (bp->b_vflags & BV_BKGRDINPROG)) { 5207 BUF_UNLOCK(bp); 5208 continue; 5209 } 5210 FREE_LOCK(&lk); 5211 BO_UNLOCK(bo); 5212 bremfree(bp); 5213 (void) bawrite(bp); 5214 goto restart; 5215 } 5216 FREE_LOCK(&lk); 5217 drain_output(vp); 5218 BO_UNLOCK(bo); 5219 } 5220 5221 /* 5222 * This routine is called when we are trying to synchronously flush a 5223 * file. This routine must eliminate any filesystem metadata dependencies 5224 * so that the syncing routine can succeed by pushing the dirty blocks 5225 * associated with the file. If any I/O errors occur, they are returned. 5226 */ 5227 int 5228 softdep_sync_metadata(struct vnode *vp) 5229 { 5230 struct pagedep *pagedep; 5231 struct allocdirect *adp; 5232 struct allocindir *aip; 5233 struct buf *bp, *nbp; 5234 struct worklist *wk; 5235 struct bufobj *bo; 5236 int i, error, waitfor; 5237 5238 if (!DOINGSOFTDEP(vp)) 5239 return (0); 5240 /* 5241 * Ensure that any direct block dependencies have been cleared. 5242 */ 5243 ACQUIRE_LOCK(&lk); 5244 if ((error = flush_inodedep_deps(vp->v_mount, VTOI(vp)->i_number))) { 5245 FREE_LOCK(&lk); 5246 return (error); 5247 } 5248 FREE_LOCK(&lk); 5249 /* 5250 * For most files, the only metadata dependencies are the 5251 * cylinder group maps that allocate their inode or blocks. 5252 * The block allocation dependencies can be found by traversing 5253 * the dependency lists for any buffers that remain on their 5254 * dirty buffer list. The inode allocation dependency will 5255 * be resolved when the inode is updated with MNT_WAIT. 5256 * This work is done in two passes. The first pass grabs most 5257 * of the buffers and begins asynchronously writing them. The 5258 * only way to wait for these asynchronous writes is to sleep 5259 * on the filesystem vnode which may stay busy for a long time 5260 * if the filesystem is active. So, instead, we make a second 5261 * pass over the dependencies blocking on each write. In the 5262 * usual case we will be blocking against a write that we 5263 * initiated, so when it is done the dependency will have been 5264 * resolved. Thus the second pass is expected to end quickly. 5265 */ 5266 waitfor = MNT_NOWAIT; 5267 bo = &vp->v_bufobj; 5268 5269 top: 5270 /* 5271 * We must wait for any I/O in progress to finish so that 5272 * all potential buffers on the dirty list will be visible. 5273 */ 5274 BO_LOCK(bo); 5275 drain_output(vp); 5276 while ((bp = TAILQ_FIRST(&bo->bo_dirty.bv_hd)) != NULL) { 5277 bp = getdirtybuf(bp, BO_MTX(bo), MNT_WAIT); 5278 if (bp) 5279 break; 5280 } 5281 BO_UNLOCK(bo); 5282 if (bp == NULL) 5283 return (0); 5284 loop: 5285 /* While syncing snapshots, we must allow recursive lookups */ 5286 BUF_AREC(bp); 5287 ACQUIRE_LOCK(&lk); 5288 /* 5289 * As we hold the buffer locked, none of its dependencies 5290 * will disappear. 5291 */ 5292 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 5293 switch (wk->wk_type) { 5294 5295 case D_ALLOCDIRECT: 5296 adp = WK_ALLOCDIRECT(wk); 5297 if (adp->ad_state & DEPCOMPLETE) 5298 continue; 5299 nbp = adp->ad_buf; 5300 nbp = getdirtybuf(nbp, &lk, waitfor); 5301 if (nbp == NULL) 5302 continue; 5303 FREE_LOCK(&lk); 5304 if (waitfor == MNT_NOWAIT) { 5305 bawrite(nbp); 5306 } else if ((error = bwrite(nbp)) != 0) { 5307 break; 5308 } 5309 ACQUIRE_LOCK(&lk); 5310 continue; 5311 5312 case D_ALLOCINDIR: 5313 aip = WK_ALLOCINDIR(wk); 5314 if (aip->ai_state & DEPCOMPLETE) 5315 continue; 5316 nbp = aip->ai_buf; 5317 nbp = getdirtybuf(nbp, &lk, waitfor); 5318 if (nbp == NULL) 5319 continue; 5320 FREE_LOCK(&lk); 5321 if (waitfor == MNT_NOWAIT) { 5322 bawrite(nbp); 5323 } else if ((error = bwrite(nbp)) != 0) { 5324 break; 5325 } 5326 ACQUIRE_LOCK(&lk); 5327 continue; 5328 5329 case D_INDIRDEP: 5330 restart: 5331 5332 LIST_FOREACH(aip, &WK_INDIRDEP(wk)->ir_deplisthd, ai_next) { 5333 if (aip->ai_state & DEPCOMPLETE) 5334 continue; 5335 nbp = aip->ai_buf; 5336 nbp = getdirtybuf(nbp, &lk, MNT_WAIT); 5337 if (nbp == NULL) 5338 goto restart; 5339 FREE_LOCK(&lk); 5340 if ((error = bwrite(nbp)) != 0) { 5341 goto loop_end; 5342 } 5343 ACQUIRE_LOCK(&lk); 5344 goto restart; 5345 } 5346 continue; 5347 5348 case D_INODEDEP: 5349 if ((error = flush_inodedep_deps(wk->wk_mp, 5350 WK_INODEDEP(wk)->id_ino)) != 0) { 5351 FREE_LOCK(&lk); 5352 break; 5353 } 5354 continue; 5355 5356 case D_PAGEDEP: 5357 /* 5358 * We are trying to sync a directory that may 5359 * have dependencies on both its own metadata 5360 * and/or dependencies on the inodes of any 5361 * recently allocated files. We walk its diradd 5362 * lists pushing out the associated inode. 5363 */ 5364 pagedep = WK_PAGEDEP(wk); 5365 for (i = 0; i < DAHASHSZ; i++) { 5366 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0) 5367 continue; 5368 if ((error = 5369 flush_pagedep_deps(vp, wk->wk_mp, 5370 &pagedep->pd_diraddhd[i]))) { 5371 FREE_LOCK(&lk); 5372 goto loop_end; 5373 } 5374 } 5375 continue; 5376 5377 case D_MKDIR: 5378 /* 5379 * This case should never happen if the vnode has 5380 * been properly sync'ed. However, if this function 5381 * is used at a place where the vnode has not yet 5382 * been sync'ed, this dependency can show up. So, 5383 * rather than panic, just flush it. 5384 */ 5385 nbp = WK_MKDIR(wk)->md_buf; 5386 nbp = getdirtybuf(nbp, &lk, waitfor); 5387 if (nbp == NULL) 5388 continue; 5389 FREE_LOCK(&lk); 5390 if (waitfor == MNT_NOWAIT) { 5391 bawrite(nbp); 5392 } else if ((error = bwrite(nbp)) != 0) { 5393 break; 5394 } 5395 ACQUIRE_LOCK(&lk); 5396 continue; 5397 5398 case D_BMSAFEMAP: 5399 /* 5400 * This case should never happen if the vnode has 5401 * been properly sync'ed. However, if this function 5402 * is used at a place where the vnode has not yet 5403 * been sync'ed, this dependency can show up. So, 5404 * rather than panic, just flush it. 5405 */ 5406 nbp = WK_BMSAFEMAP(wk)->sm_buf; 5407 nbp = getdirtybuf(nbp, &lk, waitfor); 5408 if (nbp == NULL) 5409 continue; 5410 FREE_LOCK(&lk); 5411 if (waitfor == MNT_NOWAIT) { 5412 bawrite(nbp); 5413 } else if ((error = bwrite(nbp)) != 0) { 5414 break; 5415 } 5416 ACQUIRE_LOCK(&lk); 5417 continue; 5418 5419 default: 5420 panic("softdep_sync_metadata: Unknown type %s", 5421 TYPENAME(wk->wk_type)); 5422 /* NOTREACHED */ 5423 } 5424 loop_end: 5425 /* We reach here only in error and unlocked */ 5426 if (error == 0) 5427 panic("softdep_sync_metadata: zero error"); 5428 BUF_NOREC(bp); 5429 bawrite(bp); 5430 return (error); 5431 } 5432 FREE_LOCK(&lk); 5433 BO_LOCK(bo); 5434 while ((nbp = TAILQ_NEXT(bp, b_bobufs)) != NULL) { 5435 nbp = getdirtybuf(nbp, BO_MTX(bo), MNT_WAIT); 5436 if (nbp) 5437 break; 5438 } 5439 BO_UNLOCK(bo); 5440 BUF_NOREC(bp); 5441 bawrite(bp); 5442 if (nbp != NULL) { 5443 bp = nbp; 5444 goto loop; 5445 } 5446 /* 5447 * The brief unlock is to allow any pent up dependency 5448 * processing to be done. Then proceed with the second pass. 5449 */ 5450 if (waitfor == MNT_NOWAIT) { 5451 waitfor = MNT_WAIT; 5452 goto top; 5453 } 5454 5455 /* 5456 * If we have managed to get rid of all the dirty buffers, 5457 * then we are done. For certain directories and block 5458 * devices, we may need to do further work. 5459 * 5460 * We must wait for any I/O in progress to finish so that 5461 * all potential buffers on the dirty list will be visible. 5462 */ 5463 BO_LOCK(bo); 5464 drain_output(vp); 5465 BO_UNLOCK(bo); 5466 return (0); 5467 } 5468 5469 /* 5470 * Flush the dependencies associated with an inodedep. 5471 * Called with splbio blocked. 5472 */ 5473 static int 5474 flush_inodedep_deps(mp, ino) 5475 struct mount *mp; 5476 ino_t ino; 5477 { 5478 struct inodedep *inodedep; 5479 int error, waitfor; 5480 5481 /* 5482 * This work is done in two passes. The first pass grabs most 5483 * of the buffers and begins asynchronously writing them. The 5484 * only way to wait for these asynchronous writes is to sleep 5485 * on the filesystem vnode which may stay busy for a long time 5486 * if the filesystem is active. So, instead, we make a second 5487 * pass over the dependencies blocking on each write. In the 5488 * usual case we will be blocking against a write that we 5489 * initiated, so when it is done the dependency will have been 5490 * resolved. Thus the second pass is expected to end quickly. 5491 * We give a brief window at the top of the loop to allow 5492 * any pending I/O to complete. 5493 */ 5494 for (error = 0, waitfor = MNT_NOWAIT; ; ) { 5495 if (error) 5496 return (error); 5497 FREE_LOCK(&lk); 5498 ACQUIRE_LOCK(&lk); 5499 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 5500 return (0); 5501 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) || 5502 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) || 5503 flush_deplist(&inodedep->id_extupdt, waitfor, &error) || 5504 flush_deplist(&inodedep->id_newextupdt, waitfor, &error)) 5505 continue; 5506 /* 5507 * If pass2, we are done, otherwise do pass 2. 5508 */ 5509 if (waitfor == MNT_WAIT) 5510 break; 5511 waitfor = MNT_WAIT; 5512 } 5513 /* 5514 * Try freeing inodedep in case all dependencies have been removed. 5515 */ 5516 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0) 5517 (void) free_inodedep(inodedep); 5518 return (0); 5519 } 5520 5521 /* 5522 * Flush an inode dependency list. 5523 * Called with splbio blocked. 5524 */ 5525 static int 5526 flush_deplist(listhead, waitfor, errorp) 5527 struct allocdirectlst *listhead; 5528 int waitfor; 5529 int *errorp; 5530 { 5531 struct allocdirect *adp; 5532 struct buf *bp; 5533 5534 mtx_assert(&lk, MA_OWNED); 5535 TAILQ_FOREACH(adp, listhead, ad_next) { 5536 if (adp->ad_state & DEPCOMPLETE) 5537 continue; 5538 bp = adp->ad_buf; 5539 bp = getdirtybuf(bp, &lk, waitfor); 5540 if (bp == NULL) { 5541 if (waitfor == MNT_NOWAIT) 5542 continue; 5543 return (1); 5544 } 5545 FREE_LOCK(&lk); 5546 if (waitfor == MNT_NOWAIT) { 5547 bawrite(bp); 5548 } else if ((*errorp = bwrite(bp)) != 0) { 5549 ACQUIRE_LOCK(&lk); 5550 return (1); 5551 } 5552 ACQUIRE_LOCK(&lk); 5553 return (1); 5554 } 5555 return (0); 5556 } 5557 5558 /* 5559 * Eliminate a pagedep dependency by flushing out all its diradd dependencies. 5560 * Called with splbio blocked. 5561 */ 5562 static int 5563 flush_pagedep_deps(pvp, mp, diraddhdp) 5564 struct vnode *pvp; 5565 struct mount *mp; 5566 struct diraddhd *diraddhdp; 5567 { 5568 struct inodedep *inodedep; 5569 struct ufsmount *ump; 5570 struct diradd *dap; 5571 struct vnode *vp; 5572 struct bufobj *bo; 5573 int error = 0; 5574 struct buf *bp; 5575 ino_t inum; 5576 struct worklist *wk; 5577 5578 ump = VFSTOUFS(mp); 5579 while ((dap = LIST_FIRST(diraddhdp)) != NULL) { 5580 /* 5581 * Flush ourselves if this directory entry 5582 * has a MKDIR_PARENT dependency. 5583 */ 5584 if (dap->da_state & MKDIR_PARENT) { 5585 FREE_LOCK(&lk); 5586 if ((error = ffs_update(pvp, 1)) != 0) 5587 break; 5588 ACQUIRE_LOCK(&lk); 5589 /* 5590 * If that cleared dependencies, go on to next. 5591 */ 5592 if (dap != LIST_FIRST(diraddhdp)) 5593 continue; 5594 if (dap->da_state & MKDIR_PARENT) 5595 panic("flush_pagedep_deps: MKDIR_PARENT"); 5596 } 5597 /* 5598 * A newly allocated directory must have its "." and 5599 * ".." entries written out before its name can be 5600 * committed in its parent. We do not want or need 5601 * the full semantics of a synchronous ffs_syncvnode as 5602 * that may end up here again, once for each directory 5603 * level in the filesystem. Instead, we push the blocks 5604 * and wait for them to clear. We have to fsync twice 5605 * because the first call may choose to defer blocks 5606 * that still have dependencies, but deferral will 5607 * happen at most once. 5608 */ 5609 inum = dap->da_newinum; 5610 if (dap->da_state & MKDIR_BODY) { 5611 FREE_LOCK(&lk); 5612 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp, 5613 FFSV_FORCEINSMQ))) 5614 break; 5615 if ((error=ffs_syncvnode(vp, MNT_NOWAIT)) || 5616 (error=ffs_syncvnode(vp, MNT_NOWAIT))) { 5617 vput(vp); 5618 break; 5619 } 5620 bo = &vp->v_bufobj; 5621 BO_LOCK(bo); 5622 drain_output(vp); 5623 /* 5624 * If first block is still dirty with a D_MKDIR 5625 * dependency then it needs to be written now. 5626 */ 5627 for (;;) { 5628 error = 0; 5629 bp = gbincore(bo, 0); 5630 if (bp == NULL) 5631 break; /* First block not present */ 5632 error = BUF_LOCK(bp, 5633 LK_EXCLUSIVE | 5634 LK_SLEEPFAIL | 5635 LK_INTERLOCK, 5636 BO_MTX(bo)); 5637 BO_LOCK(bo); 5638 if (error == ENOLCK) 5639 continue; /* Slept, retry */ 5640 if (error != 0) 5641 break; /* Failed */ 5642 if ((bp->b_flags & B_DELWRI) == 0) { 5643 BUF_UNLOCK(bp); 5644 break; /* Buffer not dirty */ 5645 } 5646 for (wk = LIST_FIRST(&bp->b_dep); 5647 wk != NULL; 5648 wk = LIST_NEXT(wk, wk_list)) 5649 if (wk->wk_type == D_MKDIR) 5650 break; 5651 if (wk == NULL) 5652 BUF_UNLOCK(bp); /* Dependency gone */ 5653 else { 5654 /* 5655 * D_MKDIR dependency remains, 5656 * must write buffer to stable 5657 * storage. 5658 */ 5659 BO_UNLOCK(bo); 5660 bremfree(bp); 5661 error = bwrite(bp); 5662 BO_LOCK(bo); 5663 } 5664 break; 5665 } 5666 BO_UNLOCK(bo); 5667 vput(vp); 5668 if (error != 0) 5669 break; /* Flushing of first block failed */ 5670 ACQUIRE_LOCK(&lk); 5671 /* 5672 * If that cleared dependencies, go on to next. 5673 */ 5674 if (dap != LIST_FIRST(diraddhdp)) 5675 continue; 5676 if (dap->da_state & MKDIR_BODY) 5677 panic("flush_pagedep_deps: MKDIR_BODY"); 5678 } 5679 /* 5680 * Flush the inode on which the directory entry depends. 5681 * Having accounted for MKDIR_PARENT and MKDIR_BODY above, 5682 * the only remaining dependency is that the updated inode 5683 * count must get pushed to disk. The inode has already 5684 * been pushed into its inode buffer (via VOP_UPDATE) at 5685 * the time of the reference count change. So we need only 5686 * locate that buffer, ensure that there will be no rollback 5687 * caused by a bitmap dependency, then write the inode buffer. 5688 */ 5689 retry: 5690 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 5691 panic("flush_pagedep_deps: lost inode"); 5692 /* 5693 * If the inode still has bitmap dependencies, 5694 * push them to disk. 5695 */ 5696 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 5697 bp = inodedep->id_buf; 5698 bp = getdirtybuf(bp, &lk, MNT_WAIT); 5699 if (bp == NULL) 5700 goto retry; 5701 FREE_LOCK(&lk); 5702 if ((error = bwrite(bp)) != 0) 5703 break; 5704 ACQUIRE_LOCK(&lk); 5705 if (dap != LIST_FIRST(diraddhdp)) 5706 continue; 5707 } 5708 /* 5709 * If the inode is still sitting in a buffer waiting 5710 * to be written, push it to disk. 5711 */ 5712 FREE_LOCK(&lk); 5713 if ((error = bread(ump->um_devvp, 5714 fsbtodb(ump->um_fs, ino_to_fsba(ump->um_fs, inum)), 5715 (int)ump->um_fs->fs_bsize, NOCRED, &bp)) != 0) { 5716 brelse(bp); 5717 break; 5718 } 5719 if ((error = bwrite(bp)) != 0) 5720 break; 5721 ACQUIRE_LOCK(&lk); 5722 /* 5723 * If we have failed to get rid of all the dependencies 5724 * then something is seriously wrong. 5725 */ 5726 if (dap == LIST_FIRST(diraddhdp)) 5727 panic("flush_pagedep_deps: flush failed"); 5728 } 5729 if (error) 5730 ACQUIRE_LOCK(&lk); 5731 return (error); 5732 } 5733 5734 /* 5735 * A large burst of file addition or deletion activity can drive the 5736 * memory load excessively high. First attempt to slow things down 5737 * using the techniques below. If that fails, this routine requests 5738 * the offending operations to fall back to running synchronously 5739 * until the memory load returns to a reasonable level. 5740 */ 5741 int 5742 softdep_slowdown(vp) 5743 struct vnode *vp; 5744 { 5745 int max_softdeps_hard; 5746 5747 ACQUIRE_LOCK(&lk); 5748 max_softdeps_hard = max_softdeps * 11 / 10; 5749 if (num_dirrem < max_softdeps_hard / 2 && 5750 num_inodedep < max_softdeps_hard && 5751 VFSTOUFS(vp->v_mount)->um_numindirdeps < maxindirdeps) { 5752 FREE_LOCK(&lk); 5753 return (0); 5754 } 5755 if (VFSTOUFS(vp->v_mount)->um_numindirdeps >= maxindirdeps) 5756 softdep_speedup(); 5757 stat_sync_limit_hit += 1; 5758 FREE_LOCK(&lk); 5759 return (1); 5760 } 5761 5762 /* 5763 * Called by the allocation routines when they are about to fail 5764 * in the hope that we can free up some disk space. 5765 * 5766 * First check to see if the work list has anything on it. If it has, 5767 * clean up entries until we successfully free some space. Because this 5768 * process holds inodes locked, we cannot handle any remove requests 5769 * that might block on a locked inode as that could lead to deadlock. 5770 * If the worklist yields no free space, encourage the syncer daemon 5771 * to help us. In no event will we try for longer than tickdelay seconds. 5772 */ 5773 int 5774 softdep_request_cleanup(fs, vp) 5775 struct fs *fs; 5776 struct vnode *vp; 5777 { 5778 struct ufsmount *ump; 5779 long starttime; 5780 ufs2_daddr_t needed; 5781 int error; 5782 5783 ump = VTOI(vp)->i_ump; 5784 mtx_assert(UFS_MTX(ump), MA_OWNED); 5785 needed = fs->fs_cstotal.cs_nbfree + fs->fs_contigsumsize; 5786 starttime = time_second + tickdelay; 5787 /* 5788 * If we are being called because of a process doing a 5789 * copy-on-write, then it is not safe to update the vnode 5790 * as we may recurse into the copy-on-write routine. 5791 */ 5792 if (!(curthread->td_pflags & TDP_COWINPROGRESS)) { 5793 UFS_UNLOCK(ump); 5794 error = ffs_update(vp, 1); 5795 UFS_LOCK(ump); 5796 if (error != 0) 5797 return (0); 5798 } 5799 while (fs->fs_pendingblocks > 0 && fs->fs_cstotal.cs_nbfree <= needed) { 5800 if (time_second > starttime) 5801 return (0); 5802 UFS_UNLOCK(ump); 5803 ACQUIRE_LOCK(&lk); 5804 if (ump->softdep_on_worklist > 0 && 5805 process_worklist_item(UFSTOVFS(ump), LK_NOWAIT) != -1) { 5806 stat_worklist_push += 1; 5807 FREE_LOCK(&lk); 5808 UFS_LOCK(ump); 5809 continue; 5810 } 5811 request_cleanup(UFSTOVFS(ump), FLUSH_REMOVE_WAIT); 5812 FREE_LOCK(&lk); 5813 UFS_LOCK(ump); 5814 } 5815 return (1); 5816 } 5817 5818 /* 5819 * If memory utilization has gotten too high, deliberately slow things 5820 * down and speed up the I/O processing. 5821 */ 5822 extern struct thread *syncertd; 5823 static int 5824 request_cleanup(mp, resource) 5825 struct mount *mp; 5826 int resource; 5827 { 5828 struct thread *td = curthread; 5829 struct ufsmount *ump; 5830 5831 mtx_assert(&lk, MA_OWNED); 5832 /* 5833 * We never hold up the filesystem syncer or buf daemon. 5834 */ 5835 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF)) 5836 return (0); 5837 ump = VFSTOUFS(mp); 5838 /* 5839 * First check to see if the work list has gotten backlogged. 5840 * If it has, co-opt this process to help clean up two entries. 5841 * Because this process may hold inodes locked, we cannot 5842 * handle any remove requests that might block on a locked 5843 * inode as that could lead to deadlock. We set TDP_SOFTDEP 5844 * to avoid recursively processing the worklist. 5845 */ 5846 if (ump->softdep_on_worklist > max_softdeps / 10) { 5847 td->td_pflags |= TDP_SOFTDEP; 5848 process_worklist_item(mp, LK_NOWAIT); 5849 process_worklist_item(mp, LK_NOWAIT); 5850 td->td_pflags &= ~TDP_SOFTDEP; 5851 stat_worklist_push += 2; 5852 return(1); 5853 } 5854 /* 5855 * Next, we attempt to speed up the syncer process. If that 5856 * is successful, then we allow the process to continue. 5857 */ 5858 if (softdep_speedup() && resource != FLUSH_REMOVE_WAIT) 5859 return(0); 5860 /* 5861 * If we are resource constrained on inode dependencies, try 5862 * flushing some dirty inodes. Otherwise, we are constrained 5863 * by file deletions, so try accelerating flushes of directories 5864 * with removal dependencies. We would like to do the cleanup 5865 * here, but we probably hold an inode locked at this point and 5866 * that might deadlock against one that we try to clean. So, 5867 * the best that we can do is request the syncer daemon to do 5868 * the cleanup for us. 5869 */ 5870 switch (resource) { 5871 5872 case FLUSH_INODES: 5873 stat_ino_limit_push += 1; 5874 req_clear_inodedeps += 1; 5875 stat_countp = &stat_ino_limit_hit; 5876 break; 5877 5878 case FLUSH_REMOVE: 5879 case FLUSH_REMOVE_WAIT: 5880 stat_blk_limit_push += 1; 5881 req_clear_remove += 1; 5882 stat_countp = &stat_blk_limit_hit; 5883 break; 5884 5885 default: 5886 panic("request_cleanup: unknown type"); 5887 } 5888 /* 5889 * Hopefully the syncer daemon will catch up and awaken us. 5890 * We wait at most tickdelay before proceeding in any case. 5891 */ 5892 proc_waiting += 1; 5893 if (callout_pending(&softdep_callout) == FALSE) 5894 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2, 5895 pause_timer, 0); 5896 5897 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0); 5898 proc_waiting -= 1; 5899 return (1); 5900 } 5901 5902 /* 5903 * Awaken processes pausing in request_cleanup and clear proc_waiting 5904 * to indicate that there is no longer a timer running. 5905 */ 5906 static void 5907 pause_timer(arg) 5908 void *arg; 5909 { 5910 5911 /* 5912 * The callout_ API has acquired mtx and will hold it around this 5913 * function call. 5914 */ 5915 *stat_countp += 1; 5916 wakeup_one(&proc_waiting); 5917 if (proc_waiting > 0) 5918 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2, 5919 pause_timer, 0); 5920 } 5921 5922 /* 5923 * Flush out a directory with at least one removal dependency in an effort to 5924 * reduce the number of dirrem, freefile, and freeblks dependency structures. 5925 */ 5926 static void 5927 clear_remove(td) 5928 struct thread *td; 5929 { 5930 struct pagedep_hashhead *pagedephd; 5931 struct pagedep *pagedep; 5932 static int next = 0; 5933 struct mount *mp; 5934 struct vnode *vp; 5935 struct bufobj *bo; 5936 int error, cnt; 5937 ino_t ino; 5938 5939 mtx_assert(&lk, MA_OWNED); 5940 5941 for (cnt = 0; cnt < pagedep_hash; cnt++) { 5942 pagedephd = &pagedep_hashtbl[next++]; 5943 if (next >= pagedep_hash) 5944 next = 0; 5945 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 5946 if (LIST_EMPTY(&pagedep->pd_dirremhd)) 5947 continue; 5948 mp = pagedep->pd_list.wk_mp; 5949 ino = pagedep->pd_ino; 5950 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 5951 continue; 5952 FREE_LOCK(&lk); 5953 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 5954 FFSV_FORCEINSMQ))) { 5955 softdep_error("clear_remove: vget", error); 5956 vn_finished_write(mp); 5957 ACQUIRE_LOCK(&lk); 5958 return; 5959 } 5960 if ((error = ffs_syncvnode(vp, MNT_NOWAIT))) 5961 softdep_error("clear_remove: fsync", error); 5962 bo = &vp->v_bufobj; 5963 BO_LOCK(bo); 5964 drain_output(vp); 5965 BO_UNLOCK(bo); 5966 vput(vp); 5967 vn_finished_write(mp); 5968 ACQUIRE_LOCK(&lk); 5969 return; 5970 } 5971 } 5972 } 5973 5974 /* 5975 * Clear out a block of dirty inodes in an effort to reduce 5976 * the number of inodedep dependency structures. 5977 */ 5978 static void 5979 clear_inodedeps(td) 5980 struct thread *td; 5981 { 5982 struct inodedep_hashhead *inodedephd; 5983 struct inodedep *inodedep; 5984 static int next = 0; 5985 struct mount *mp; 5986 struct vnode *vp; 5987 struct fs *fs; 5988 int error, cnt; 5989 ino_t firstino, lastino, ino; 5990 5991 mtx_assert(&lk, MA_OWNED); 5992 /* 5993 * Pick a random inode dependency to be cleared. 5994 * We will then gather up all the inodes in its block 5995 * that have dependencies and flush them out. 5996 */ 5997 for (cnt = 0; cnt < inodedep_hash; cnt++) { 5998 inodedephd = &inodedep_hashtbl[next++]; 5999 if (next >= inodedep_hash) 6000 next = 0; 6001 if ((inodedep = LIST_FIRST(inodedephd)) != NULL) 6002 break; 6003 } 6004 if (inodedep == NULL) 6005 return; 6006 fs = inodedep->id_fs; 6007 mp = inodedep->id_list.wk_mp; 6008 /* 6009 * Find the last inode in the block with dependencies. 6010 */ 6011 firstino = inodedep->id_ino & ~(INOPB(fs) - 1); 6012 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--) 6013 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0) 6014 break; 6015 /* 6016 * Asynchronously push all but the last inode with dependencies. 6017 * Synchronously push the last inode with dependencies to ensure 6018 * that the inode block gets written to free up the inodedeps. 6019 */ 6020 for (ino = firstino; ino <= lastino; ino++) { 6021 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 6022 continue; 6023 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 6024 continue; 6025 FREE_LOCK(&lk); 6026 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 6027 FFSV_FORCEINSMQ)) != 0) { 6028 softdep_error("clear_inodedeps: vget", error); 6029 vn_finished_write(mp); 6030 ACQUIRE_LOCK(&lk); 6031 return; 6032 } 6033 if (ino == lastino) { 6034 if ((error = ffs_syncvnode(vp, MNT_WAIT))) 6035 softdep_error("clear_inodedeps: fsync1", error); 6036 } else { 6037 if ((error = ffs_syncvnode(vp, MNT_NOWAIT))) 6038 softdep_error("clear_inodedeps: fsync2", error); 6039 BO_LOCK(&vp->v_bufobj); 6040 drain_output(vp); 6041 BO_UNLOCK(&vp->v_bufobj); 6042 } 6043 vput(vp); 6044 vn_finished_write(mp); 6045 ACQUIRE_LOCK(&lk); 6046 } 6047 } 6048 6049 /* 6050 * Function to determine if the buffer has outstanding dependencies 6051 * that will cause a roll-back if the buffer is written. If wantcount 6052 * is set, return number of dependencies, otherwise just yes or no. 6053 */ 6054 static int 6055 softdep_count_dependencies(bp, wantcount) 6056 struct buf *bp; 6057 int wantcount; 6058 { 6059 struct worklist *wk; 6060 struct inodedep *inodedep; 6061 struct indirdep *indirdep; 6062 struct allocindir *aip; 6063 struct pagedep *pagedep; 6064 struct diradd *dap; 6065 int i, retval; 6066 6067 retval = 0; 6068 ACQUIRE_LOCK(&lk); 6069 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 6070 switch (wk->wk_type) { 6071 6072 case D_INODEDEP: 6073 inodedep = WK_INODEDEP(wk); 6074 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 6075 /* bitmap allocation dependency */ 6076 retval += 1; 6077 if (!wantcount) 6078 goto out; 6079 } 6080 if (TAILQ_FIRST(&inodedep->id_inoupdt)) { 6081 /* direct block pointer dependency */ 6082 retval += 1; 6083 if (!wantcount) 6084 goto out; 6085 } 6086 if (TAILQ_FIRST(&inodedep->id_extupdt)) { 6087 /* direct block pointer dependency */ 6088 retval += 1; 6089 if (!wantcount) 6090 goto out; 6091 } 6092 continue; 6093 6094 case D_INDIRDEP: 6095 indirdep = WK_INDIRDEP(wk); 6096 6097 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 6098 /* indirect block pointer dependency */ 6099 retval += 1; 6100 if (!wantcount) 6101 goto out; 6102 } 6103 continue; 6104 6105 case D_PAGEDEP: 6106 pagedep = WK_PAGEDEP(wk); 6107 for (i = 0; i < DAHASHSZ; i++) { 6108 6109 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 6110 /* directory entry dependency */ 6111 retval += 1; 6112 if (!wantcount) 6113 goto out; 6114 } 6115 } 6116 continue; 6117 6118 case D_BMSAFEMAP: 6119 case D_ALLOCDIRECT: 6120 case D_ALLOCINDIR: 6121 case D_MKDIR: 6122 /* never a dependency on these blocks */ 6123 continue; 6124 6125 default: 6126 panic("softdep_check_for_rollback: Unexpected type %s", 6127 TYPENAME(wk->wk_type)); 6128 /* NOTREACHED */ 6129 } 6130 } 6131 out: 6132 FREE_LOCK(&lk); 6133 return retval; 6134 } 6135 6136 /* 6137 * Acquire exclusive access to a buffer. 6138 * Must be called with a locked mtx parameter. 6139 * Return acquired buffer or NULL on failure. 6140 */ 6141 static struct buf * 6142 getdirtybuf(bp, mtx, waitfor) 6143 struct buf *bp; 6144 struct mtx *mtx; 6145 int waitfor; 6146 { 6147 int error; 6148 6149 mtx_assert(mtx, MA_OWNED); 6150 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) { 6151 if (waitfor != MNT_WAIT) 6152 return (NULL); 6153 error = BUF_LOCK(bp, 6154 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, mtx); 6155 /* 6156 * Even if we sucessfully acquire bp here, we have dropped 6157 * mtx, which may violates our guarantee. 6158 */ 6159 if (error == 0) 6160 BUF_UNLOCK(bp); 6161 else if (error != ENOLCK) 6162 panic("getdirtybuf: inconsistent lock: %d", error); 6163 mtx_lock(mtx); 6164 return (NULL); 6165 } 6166 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 6167 if (mtx == &lk && waitfor == MNT_WAIT) { 6168 mtx_unlock(mtx); 6169 BO_LOCK(bp->b_bufobj); 6170 BUF_UNLOCK(bp); 6171 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 6172 bp->b_vflags |= BV_BKGRDWAIT; 6173 msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), 6174 PRIBIO | PDROP, "getbuf", 0); 6175 } else 6176 BO_UNLOCK(bp->b_bufobj); 6177 mtx_lock(mtx); 6178 return (NULL); 6179 } 6180 BUF_UNLOCK(bp); 6181 if (waitfor != MNT_WAIT) 6182 return (NULL); 6183 /* 6184 * The mtx argument must be bp->b_vp's mutex in 6185 * this case. 6186 */ 6187 #ifdef DEBUG_VFS_LOCKS 6188 if (bp->b_vp->v_type != VCHR) 6189 ASSERT_BO_LOCKED(bp->b_bufobj); 6190 #endif 6191 bp->b_vflags |= BV_BKGRDWAIT; 6192 msleep(&bp->b_xflags, mtx, PRIBIO, "getbuf", 0); 6193 return (NULL); 6194 } 6195 if ((bp->b_flags & B_DELWRI) == 0) { 6196 BUF_UNLOCK(bp); 6197 return (NULL); 6198 } 6199 bremfree(bp); 6200 return (bp); 6201 } 6202 6203 6204 /* 6205 * Check if it is safe to suspend the file system now. On entry, 6206 * the vnode interlock for devvp should be held. Return 0 with 6207 * the mount interlock held if the file system can be suspended now, 6208 * otherwise return EAGAIN with the mount interlock held. 6209 */ 6210 int 6211 softdep_check_suspend(struct mount *mp, 6212 struct vnode *devvp, 6213 int softdep_deps, 6214 int softdep_accdeps, 6215 int secondary_writes, 6216 int secondary_accwrites) 6217 { 6218 struct bufobj *bo; 6219 struct ufsmount *ump; 6220 int error; 6221 6222 ump = VFSTOUFS(mp); 6223 bo = &devvp->v_bufobj; 6224 ASSERT_BO_LOCKED(bo); 6225 6226 for (;;) { 6227 if (!TRY_ACQUIRE_LOCK(&lk)) { 6228 BO_UNLOCK(bo); 6229 ACQUIRE_LOCK(&lk); 6230 FREE_LOCK(&lk); 6231 BO_LOCK(bo); 6232 continue; 6233 } 6234 MNT_ILOCK(mp); 6235 if (mp->mnt_secondary_writes != 0) { 6236 FREE_LOCK(&lk); 6237 BO_UNLOCK(bo); 6238 msleep(&mp->mnt_secondary_writes, 6239 MNT_MTX(mp), 6240 (PUSER - 1) | PDROP, "secwr", 0); 6241 BO_LOCK(bo); 6242 continue; 6243 } 6244 break; 6245 } 6246 6247 /* 6248 * Reasons for needing more work before suspend: 6249 * - Dirty buffers on devvp. 6250 * - Softdep activity occurred after start of vnode sync loop 6251 * - Secondary writes occurred after start of vnode sync loop 6252 */ 6253 error = 0; 6254 if (bo->bo_numoutput > 0 || 6255 bo->bo_dirty.bv_cnt > 0 || 6256 softdep_deps != 0 || 6257 ump->softdep_deps != 0 || 6258 softdep_accdeps != ump->softdep_accdeps || 6259 secondary_writes != 0 || 6260 mp->mnt_secondary_writes != 0 || 6261 secondary_accwrites != mp->mnt_secondary_accwrites) 6262 error = EAGAIN; 6263 FREE_LOCK(&lk); 6264 BO_UNLOCK(bo); 6265 return (error); 6266 } 6267 6268 6269 /* 6270 * Get the number of dependency structures for the file system, both 6271 * the current number and the total number allocated. These will 6272 * later be used to detect that softdep processing has occurred. 6273 */ 6274 void 6275 softdep_get_depcounts(struct mount *mp, 6276 int *softdep_depsp, 6277 int *softdep_accdepsp) 6278 { 6279 struct ufsmount *ump; 6280 6281 ump = VFSTOUFS(mp); 6282 ACQUIRE_LOCK(&lk); 6283 *softdep_depsp = ump->softdep_deps; 6284 *softdep_accdepsp = ump->softdep_accdeps; 6285 FREE_LOCK(&lk); 6286 } 6287 6288 /* 6289 * Wait for pending output on a vnode to complete. 6290 * Must be called with vnode lock and interlock locked. 6291 * 6292 * XXX: Should just be a call to bufobj_wwait(). 6293 */ 6294 static void 6295 drain_output(vp) 6296 struct vnode *vp; 6297 { 6298 struct bufobj *bo; 6299 6300 bo = &vp->v_bufobj; 6301 ASSERT_VOP_LOCKED(vp, "drain_output"); 6302 ASSERT_BO_LOCKED(bo); 6303 6304 while (bo->bo_numoutput) { 6305 bo->bo_flag |= BO_WWAIT; 6306 msleep((caddr_t)&bo->bo_numoutput, 6307 BO_MTX(bo), PRIBIO + 1, "drainvp", 0); 6308 } 6309 } 6310 6311 /* 6312 * Called whenever a buffer that is being invalidated or reallocated 6313 * contains dependencies. This should only happen if an I/O error has 6314 * occurred. The routine is called with the buffer locked. 6315 */ 6316 static void 6317 softdep_deallocate_dependencies(bp) 6318 struct buf *bp; 6319 { 6320 6321 if ((bp->b_ioflags & BIO_ERROR) == 0) 6322 panic("softdep_deallocate_dependencies: dangling deps"); 6323 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error); 6324 panic("softdep_deallocate_dependencies: unrecovered I/O error"); 6325 } 6326 6327 /* 6328 * Function to handle asynchronous write errors in the filesystem. 6329 */ 6330 static void 6331 softdep_error(func, error) 6332 char *func; 6333 int error; 6334 { 6335 6336 /* XXX should do something better! */ 6337 printf("%s: got error %d while accessing filesystem\n", func, error); 6338 } 6339 6340 #ifdef DDB 6341 6342 DB_SHOW_COMMAND(inodedeps, db_show_inodedeps) 6343 { 6344 struct inodedep_hashhead *inodedephd; 6345 struct inodedep *inodedep; 6346 struct fs *fs; 6347 int cnt; 6348 6349 fs = have_addr ? (struct fs *)addr : NULL; 6350 for (cnt = 0; cnt < inodedep_hash; cnt++) { 6351 inodedephd = &inodedep_hashtbl[cnt]; 6352 LIST_FOREACH(inodedep, inodedephd, id_hash) { 6353 if (fs != NULL && fs != inodedep->id_fs) 6354 continue; 6355 db_printf("%p fs %p st %x ino %jd inoblk %jd\n", 6356 inodedep, inodedep->id_fs, inodedep->id_state, 6357 (intmax_t)inodedep->id_ino, 6358 (intmax_t)fsbtodb(inodedep->id_fs, 6359 ino_to_fsba(inodedep->id_fs, inodedep->id_ino))); 6360 } 6361 } 6362 } 6363 6364 #endif /* DDB */ 6365 6366 #endif /* SOFTUPDATES */ 6367