ffs_softdep.c revision 47131
1/* 2 * Copyright 1998 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 * The following are the copyrights and redistribution conditions that 10 * apply to this copy of the soft update software. For a license 11 * to use, redistribute or sell the soft update software under 12 * conditions other than those described here, please contact the 13 * author at one of the following addresses: 14 * 15 * Marshall Kirk McKusick mckusick@mckusick.com 16 * 1614 Oxford Street +1-510-843-9542 17 * Berkeley, CA 94709-1608 18 * USA 19 * 20 * Redistribution and use in source and binary forms, with or without 21 * modification, are permitted provided that the following conditions 22 * are met: 23 * 24 * 1. Redistributions of source code must retain the above copyright 25 * notice, this list of conditions and the following disclaimer. 26 * 2. Redistributions in binary form must reproduce the above copyright 27 * notice, this list of conditions and the following disclaimer in the 28 * documentation and/or other materials provided with the distribution. 29 * 3. None of the names of McKusick, Ganger, Patt, or the University of 30 * Michigan may be used to endorse or promote products derived from 31 * this software without specific prior written permission. 32 * 4. Redistributions in any form must be accompanied by information on 33 * how to obtain complete source code for any accompanying software 34 * that uses this software. This source code must either be included 35 * in the distribution or be available for no more than the cost of 36 * distribution plus a nominal fee, and must be freely redistributable 37 * under reasonable conditions. For an executable file, complete 38 * source code means the source code for all modules it contains. 39 * It does not mean source code for modules or files that typically 40 * accompany the operating system on which the executable file runs, 41 * e.g., standard library modules or system header files. 42 * 43 * THIS SOFTWARE IS PROVIDED BY MARSHALL KIRK MCKUSICK ``AS IS'' AND ANY 44 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 45 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 46 * DISCLAIMED. IN NO EVENT SHALL MARSHALL KIRK MCKUSICK BE LIABLE FOR 47 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 53 * SUCH DAMAGE. 54 * 55 * from: @(#)ffs_softdep.c 9.38 (McKusick) 5/13/99 56 * $Id: ffs_softdep.c,v 1.27 1999/05/09 19:39:54 mckusick Exp $ 57 */ 58 59/* 60 * For now we want the safety net that the DIAGNOSTIC and DEBUG flags provide. 61 */ 62#ifndef DIAGNOSTIC 63#define DIAGNOSTIC 64#endif 65#ifndef DEBUG 66#define DEBUG 67#endif 68 69#include <sys/param.h> 70#include <sys/buf.h> 71#include <sys/kernel.h> 72#include <sys/malloc.h> 73#include <sys/mount.h> 74#include <sys/proc.h> 75#include <sys/syslog.h> 76#include <sys/systm.h> 77#include <sys/vnode.h> 78#include <miscfs/specfs/specdev.h> 79#include <ufs/ufs/dir.h> 80#include <ufs/ufs/quota.h> 81#include <ufs/ufs/inode.h> 82#include <ufs/ufs/ufsmount.h> 83#include <ufs/ffs/fs.h> 84#include <ufs/ffs/softdep.h> 85#include <ufs/ffs/ffs_extern.h> 86#include <ufs/ufs/ufs_extern.h> 87 88/* 89 * These definitions need to be adapted to the system to which 90 * this file is being ported. 91 */ 92/* 93 * malloc types defined for the softdep system. 94 */ 95MALLOC_DEFINE(M_PAGEDEP, "pagedep","File page dependencies"); 96MALLOC_DEFINE(M_INODEDEP, "inodedep","Inode dependencies"); 97MALLOC_DEFINE(M_NEWBLK, "newblk","New block allocation"); 98MALLOC_DEFINE(M_BMSAFEMAP, "bmsafemap","Block or frag allocated from cyl group map"); 99MALLOC_DEFINE(M_ALLOCDIRECT, "allocdirect","Block or frag dependency for an inode"); 100MALLOC_DEFINE(M_INDIRDEP, "indirdep","Indirect block dependencies"); 101MALLOC_DEFINE(M_ALLOCINDIR, "allocindir","Block dependency for an indirect block"); 102MALLOC_DEFINE(M_FREEFRAG, "freefrag","Previously used frag for an inode"); 103MALLOC_DEFINE(M_FREEBLKS, "freeblks","Blocks freed from an inode"); 104MALLOC_DEFINE(M_FREEFILE, "freefile","Inode deallocated"); 105MALLOC_DEFINE(M_DIRADD, "diradd","New directory entry"); 106MALLOC_DEFINE(M_MKDIR, "mkdir","New directory"); 107MALLOC_DEFINE(M_DIRREM, "dirrem","Directory entry deleted"); 108 109#define D_PAGEDEP 0 110#define D_INODEDEP 1 111#define D_NEWBLK 2 112#define D_BMSAFEMAP 3 113#define D_ALLOCDIRECT 4 114#define D_INDIRDEP 5 115#define D_ALLOCINDIR 6 116#define D_FREEFRAG 7 117#define D_FREEBLKS 8 118#define D_FREEFILE 9 119#define D_DIRADD 10 120#define D_MKDIR 11 121#define D_DIRREM 12 122#define D_LAST D_DIRREM 123 124/* 125 * translate from workitem type to memory type 126 * MUST match the defines above, such that memtype[D_XXX] == M_XXX 127 */ 128static struct malloc_type *memtype[] = { 129 M_PAGEDEP, 130 M_INODEDEP, 131 M_NEWBLK, 132 M_BMSAFEMAP, 133 M_ALLOCDIRECT, 134 M_INDIRDEP, 135 M_ALLOCINDIR, 136 M_FREEFRAG, 137 M_FREEBLKS, 138 M_FREEFILE, 139 M_DIRADD, 140 M_MKDIR, 141 M_DIRREM 142}; 143 144#define DtoM(type) (memtype[type]) 145 146/* 147 * Names of malloc types. 148 */ 149#define TYPENAME(type) \ 150 ((unsigned)(type) < D_LAST ? memtype[type]->ks_shortdesc : "???") 151#define CURPROC curproc 152/* 153 * End system adaptaion definitions. 154 */ 155 156/* 157 * Internal function prototypes. 158 */ 159static void softdep_error __P((char *, int)); 160static void drain_output __P((struct vnode *, int)); 161static int getdirtybuf __P((struct buf **, int)); 162static void clear_remove __P((struct proc *)); 163static void clear_inodedeps __P((struct proc *)); 164static int flush_pagedep_deps __P((struct vnode *, struct mount *, 165 struct diraddhd *)); 166static int flush_inodedep_deps __P((struct fs *, ino_t)); 167static int handle_written_filepage __P((struct pagedep *, struct buf *)); 168static void diradd_inode_written __P((struct diradd *, struct inodedep *)); 169static int handle_written_inodeblock __P((struct inodedep *, struct buf *)); 170static void handle_allocdirect_partdone __P((struct allocdirect *)); 171static void handle_allocindir_partdone __P((struct allocindir *)); 172static void initiate_write_filepage __P((struct pagedep *, struct buf *)); 173static void handle_written_mkdir __P((struct mkdir *, int)); 174static void initiate_write_inodeblock __P((struct inodedep *, struct buf *)); 175static void handle_workitem_freefile __P((struct freefile *)); 176static void handle_workitem_remove __P((struct dirrem *)); 177static struct dirrem *newdirrem __P((struct buf *, struct inode *, 178 struct inode *, int)); 179static void free_diradd __P((struct diradd *)); 180static void free_allocindir __P((struct allocindir *, struct inodedep *)); 181static int indir_trunc __P((struct inode *, ufs_daddr_t, int, ufs_lbn_t, 182 long *)); 183static void deallocate_dependencies __P((struct buf *, struct inodedep *)); 184static void free_allocdirect __P((struct allocdirectlst *, 185 struct allocdirect *, int)); 186static int free_inodedep __P((struct inodedep *)); 187static void handle_workitem_freeblocks __P((struct freeblks *)); 188static void merge_inode_lists __P((struct inodedep *)); 189static void setup_allocindir_phase2 __P((struct buf *, struct inode *, 190 struct allocindir *)); 191static struct allocindir *newallocindir __P((struct inode *, int, ufs_daddr_t, 192 ufs_daddr_t)); 193static void handle_workitem_freefrag __P((struct freefrag *)); 194static struct freefrag *newfreefrag __P((struct inode *, ufs_daddr_t, long)); 195static void allocdirect_merge __P((struct allocdirectlst *, 196 struct allocdirect *, struct allocdirect *)); 197static struct bmsafemap *bmsafemap_lookup __P((struct buf *)); 198static int newblk_lookup __P((struct fs *, ufs_daddr_t, int, 199 struct newblk **)); 200static int inodedep_lookup __P((struct fs *, ino_t, int, struct inodedep **)); 201static int pagedep_lookup __P((struct inode *, ufs_lbn_t, int, 202 struct pagedep **)); 203static void pause_timer __P((void *)); 204static int checklimit __P((long *, int)); 205static void add_to_worklist __P((struct worklist *)); 206 207/* 208 * Exported softdep operations. 209 */ 210struct bio_ops bioops = { 211 softdep_disk_io_initiation, /* io_start */ 212 softdep_disk_write_complete, /* io_complete */ 213 softdep_deallocate_dependencies, /* io_deallocate */ 214 softdep_fsync, /* io_fsync */ 215 softdep_process_worklist, /* io_sync */ 216}; 217 218/* 219 * Locking primitives. 220 * 221 * For a uniprocessor, all we need to do is protect against disk 222 * interrupts. For a multiprocessor, this lock would have to be 223 * a mutex. A single mutex is used throughout this file, though 224 * finer grain locking could be used if contention warranted it. 225 * 226 * For a multiprocessor, the sleep call would accept a lock and 227 * release it after the sleep processing was complete. In a uniprocessor 228 * implementation there is no such interlock, so we simple mark 229 * the places where it needs to be done with the `interlocked' form 230 * of the lock calls. Since the uniprocessor sleep already interlocks 231 * the spl, there is nothing that really needs to be done. 232 */ 233#ifndef /* NOT */ DEBUG 234static struct lockit { 235 int lkt_spl; 236} lk = { 0 }; 237#define ACQUIRE_LOCK(lk) (lk)->lkt_spl = splbio() 238#define FREE_LOCK(lk) splx((lk)->lkt_spl) 239#define ACQUIRE_LOCK_INTERLOCKED(lk) 240#define FREE_LOCK_INTERLOCKED(lk) 241 242#else /* DEBUG */ 243static struct lockit { 244 int lkt_spl; 245 pid_t lkt_held; 246} lk = { 0, -1 }; 247static int lockcnt; 248 249static void acquire_lock __P((struct lockit *)); 250static void free_lock __P((struct lockit *)); 251static void acquire_lock_interlocked __P((struct lockit *)); 252static void free_lock_interlocked __P((struct lockit *)); 253 254#define ACQUIRE_LOCK(lk) acquire_lock(lk) 255#define FREE_LOCK(lk) free_lock(lk) 256#define ACQUIRE_LOCK_INTERLOCKED(lk) acquire_lock_interlocked(lk) 257#define FREE_LOCK_INTERLOCKED(lk) free_lock_interlocked(lk) 258 259static void 260acquire_lock(lk) 261 struct lockit *lk; 262{ 263 264 if (lk->lkt_held != -1) 265 if (lk->lkt_held == CURPROC->p_pid) 266 panic("softdep_lock: locking against myself"); 267 else 268 panic("softdep_lock: lock held by %d", lk->lkt_held); 269 lk->lkt_spl = splbio(); 270 lk->lkt_held = CURPROC->p_pid; 271 lockcnt++; 272} 273 274static void 275free_lock(lk) 276 struct lockit *lk; 277{ 278 279 if (lk->lkt_held == -1) 280 panic("softdep_unlock: lock not held"); 281 lk->lkt_held = -1; 282 splx(lk->lkt_spl); 283} 284 285static void 286acquire_lock_interlocked(lk) 287 struct lockit *lk; 288{ 289 290 if (lk->lkt_held != -1) 291 if (lk->lkt_held == CURPROC->p_pid) 292 panic("softdep_lock_interlocked: locking against self"); 293 else 294 panic("softdep_lock_interlocked: lock held by %d", 295 lk->lkt_held); 296 lk->lkt_held = CURPROC->p_pid; 297 lockcnt++; 298} 299 300static void 301free_lock_interlocked(lk) 302 struct lockit *lk; 303{ 304 305 if (lk->lkt_held == -1) 306 panic("softdep_unlock_interlocked: lock not held"); 307 lk->lkt_held = -1; 308} 309#endif /* DEBUG */ 310 311/* 312 * Place holder for real semaphores. 313 */ 314struct sema { 315 int value; 316 pid_t holder; 317 char *name; 318 int prio; 319 int timo; 320}; 321static void sema_init __P((struct sema *, char *, int, int)); 322static int sema_get __P((struct sema *, struct lockit *)); 323static void sema_release __P((struct sema *)); 324 325static void 326sema_init(semap, name, prio, timo) 327 struct sema *semap; 328 char *name; 329 int prio, timo; 330{ 331 332 semap->holder = -1; 333 semap->value = 0; 334 semap->name = name; 335 semap->prio = prio; 336 semap->timo = timo; 337} 338 339static int 340sema_get(semap, interlock) 341 struct sema *semap; 342 struct lockit *interlock; 343{ 344 345 if (semap->value++ > 0) { 346 if (interlock != NULL) 347 FREE_LOCK_INTERLOCKED(interlock); 348 tsleep((caddr_t)semap, semap->prio, semap->name, semap->timo); 349 if (interlock != NULL) { 350 ACQUIRE_LOCK_INTERLOCKED(interlock); 351 FREE_LOCK(interlock); 352 } 353 return (0); 354 } 355 semap->holder = CURPROC->p_pid; 356 if (interlock != NULL) 357 FREE_LOCK(interlock); 358 return (1); 359} 360 361static void 362sema_release(semap) 363 struct sema *semap; 364{ 365 366 if (semap->value <= 0 || semap->holder != CURPROC->p_pid) 367 panic("sema_release: not held"); 368 if (--semap->value > 0) { 369 semap->value = 0; 370 wakeup(semap); 371 } 372 semap->holder = -1; 373} 374 375/* 376 * Worklist queue management. 377 * These routines require that the lock be held. 378 */ 379#ifndef /* NOT */ DEBUG 380#define WORKLIST_INSERT(head, item) do { \ 381 (item)->wk_state |= ONWORKLIST; \ 382 LIST_INSERT_HEAD(head, item, wk_list); \ 383} while (0) 384#define WORKLIST_REMOVE(item) do { \ 385 (item)->wk_state &= ~ONWORKLIST; \ 386 LIST_REMOVE(item, wk_list); \ 387} while (0) 388#define WORKITEM_FREE(item, type) FREE(item, DtoM(type)) 389 390#else /* DEBUG */ 391static void worklist_insert __P((struct workhead *, struct worklist *)); 392static void worklist_remove __P((struct worklist *)); 393static void workitem_free __P((struct worklist *, int)); 394 395#define WORKLIST_INSERT(head, item) worklist_insert(head, item) 396#define WORKLIST_REMOVE(item) worklist_remove(item) 397#define WORKITEM_FREE(item, type) workitem_free((struct worklist *)item, type) 398 399static void 400worklist_insert(head, item) 401 struct workhead *head; 402 struct worklist *item; 403{ 404 405 if (lk.lkt_held == -1) 406 panic("worklist_insert: lock not held"); 407 if (item->wk_state & ONWORKLIST) 408 panic("worklist_insert: already on list"); 409 item->wk_state |= ONWORKLIST; 410 LIST_INSERT_HEAD(head, item, wk_list); 411} 412 413static void 414worklist_remove(item) 415 struct worklist *item; 416{ 417 418 if (lk.lkt_held == -1) 419 panic("worklist_remove: lock not held"); 420 if ((item->wk_state & ONWORKLIST) == 0) 421 panic("worklist_remove: not on list"); 422 item->wk_state &= ~ONWORKLIST; 423 LIST_REMOVE(item, wk_list); 424} 425 426static void 427workitem_free(item, type) 428 struct worklist *item; 429 int type; 430{ 431 432 if (item->wk_state & ONWORKLIST) 433 panic("workitem_free: still on list"); 434 if (item->wk_type != type) 435 panic("workitem_free: type mismatch"); 436 FREE(item, DtoM(type)); 437} 438#endif /* DEBUG */ 439 440/* 441 * Workitem queue management 442 */ 443static struct workhead softdep_workitem_pending; 444static int softdep_worklist_busy; 445static int max_softdeps; /* maximum number of structs before slowdown */ 446static int tickdelay = 2; /* number of ticks to pause during slowdown */ 447static int proc_waiting; /* tracks whether we have a timeout posted */ 448static struct proc *filesys_syncer; /* proc of filesystem syncer process */ 449static int req_clear_inodedeps; /* syncer process flush some inodedeps */ 450static int req_clear_remove; /* syncer process flush some freeblks */ 451/* 452 * runtime statistics 453 */ 454static int stat_rush_requests; /* number of times I/O speeded up */ 455static int stat_blk_limit_push; /* number of times block limit neared */ 456static int stat_ino_limit_push; /* number of times inode limit neared */ 457static int stat_blk_limit_hit; /* number of times block slowdown imposed */ 458static int stat_ino_limit_hit; /* number of times inode slowdown imposed */ 459static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */ 460static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */ 461static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */ 462static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */ 463#ifdef DEBUG 464#include <vm/vm.h> 465#include <sys/sysctl.h> 466#if defined(__FreeBSD__) 467SYSCTL_INT(_debug, OID_AUTO, max_softdeps, CTLFLAG_RW, &max_softdeps, 0, ""); 468SYSCTL_INT(_debug, OID_AUTO, tickdelay, CTLFLAG_RW, &tickdelay, 0, ""); 469SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, ""); 470SYSCTL_INT(_debug, OID_AUTO, blk_limit_push, CTLFLAG_RW, &stat_blk_limit_push, 0,""); 471SYSCTL_INT(_debug, OID_AUTO, ino_limit_push, CTLFLAG_RW, &stat_ino_limit_push, 0,""); 472SYSCTL_INT(_debug, OID_AUTO, blk_limit_hit, CTLFLAG_RW, &stat_blk_limit_hit, 0, ""); 473SYSCTL_INT(_debug, OID_AUTO, ino_limit_hit, CTLFLAG_RW, &stat_ino_limit_hit, 0, ""); 474SYSCTL_INT(_debug, OID_AUTO, indir_blk_ptrs, CTLFLAG_RW, &stat_indir_blk_ptrs, 0, ""); 475SYSCTL_INT(_debug, OID_AUTO, inode_bitmap, CTLFLAG_RW, &stat_inode_bitmap, 0, ""); 476SYSCTL_INT(_debug, OID_AUTO, direct_blk_ptrs, CTLFLAG_RW, &stat_direct_blk_ptrs, 0, ""); 477SYSCTL_INT(_debug, OID_AUTO, dir_entry, CTLFLAG_RW, &stat_dir_entry, 0, ""); 478#else /* !__FreeBSD__ */ 479struct ctldebug debug20 = { "max_softdeps", &max_softdeps }; 480struct ctldebug debug21 = { "tickdelay", &tickdelay }; 481struct ctldebug debug22 = { "rush_requests", &stat_rush_requests }; 482struct ctldebug debug23 = { "blk_limit_push", &stat_blk_limit_push }; 483struct ctldebug debug24 = { "ino_limit_push", &stat_ino_limit_push }; 484struct ctldebug debug25 = { "blk_limit_hit", &stat_blk_limit_hit }; 485struct ctldebug debug26 = { "ino_limit_hit", &stat_ino_limit_hit }; 486struct ctldebug debug27 = { "indir_blk_ptrs", &stat_indir_blk_ptrs }; 487struct ctldebug debug28 = { "inode_bitmap", &stat_inode_bitmap }; 488struct ctldebug debug29 = { "direct_blk_ptrs", &stat_direct_blk_ptrs }; 489struct ctldebug debug30 = { "dir_entry", &stat_dir_entry }; 490#endif /* !__FreeBSD__ */ 491 492#endif /* DEBUG */ 493 494/* 495 * Add an item to the end of the work queue. 496 * This routine requires that the lock be held. 497 * This is the only routine that adds items to the list. 498 * The following routine is the only one that removes items 499 * and does so in order from first to last. 500 */ 501static void 502add_to_worklist(wk) 503 struct worklist *wk; 504{ 505 static struct worklist *worklist_tail; 506 507 if (wk->wk_state & ONWORKLIST) 508 panic("add_to_worklist: already on list"); 509 wk->wk_state |= ONWORKLIST; 510 if (LIST_FIRST(&softdep_workitem_pending) == NULL) 511 LIST_INSERT_HEAD(&softdep_workitem_pending, wk, wk_list); 512 else 513 LIST_INSERT_AFTER(worklist_tail, wk, wk_list); 514 worklist_tail = wk; 515} 516 517/* 518 * Process that runs once per second to handle items in the background queue. 519 * 520 * Note that we ensure that everything is done in the order in which they 521 * appear in the queue. The code below depends on this property to ensure 522 * that blocks of a file are freed before the inode itself is freed. This 523 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated 524 * until all the old ones have been purged from the dependency lists. 525 */ 526int 527softdep_process_worklist(matchmnt) 528 struct mount *matchmnt; 529{ 530 struct proc *p = CURPROC; 531 struct worklist *wk; 532 struct fs *matchfs; 533 int matchcnt; 534 535 /* 536 * Record the process identifier of our caller so that we can 537 * give this process preferential treatment in checklimit below. 538 */ 539 filesys_syncer = p; 540 matchcnt = 0; 541 matchfs = NULL; 542 if (matchmnt != NULL) 543 matchfs = VFSTOUFS(matchmnt)->um_fs; 544 /* 545 * There is no danger of having multiple processes run this 546 * code. It is single threaded solely so that softdep_flushfiles 547 * (below) can get an accurate count of the number of items 548 * related to its mount point that are in the list. 549 */ 550 if (softdep_worklist_busy && matchmnt == NULL) 551 return (-1); 552 /* 553 * If requested, try removing inode or removal dependencies. 554 */ 555 if (req_clear_inodedeps) { 556 clear_inodedeps(p); 557 req_clear_inodedeps = 0; 558 wakeup(&proc_waiting); 559 } 560 if (req_clear_remove) { 561 clear_remove(p); 562 req_clear_remove = 0; 563 wakeup(&proc_waiting); 564 } 565 ACQUIRE_LOCK(&lk); 566 while ((wk = LIST_FIRST(&softdep_workitem_pending)) != 0) { 567 WORKLIST_REMOVE(wk); 568 FREE_LOCK(&lk); 569 switch (wk->wk_type) { 570 571 case D_DIRREM: 572 /* removal of a directory entry */ 573 if (WK_DIRREM(wk)->dm_mnt == matchmnt) 574 matchcnt += 1; 575 handle_workitem_remove(WK_DIRREM(wk)); 576 break; 577 578 case D_FREEBLKS: 579 /* releasing blocks and/or fragments from a file */ 580 if (WK_FREEBLKS(wk)->fb_fs == matchfs) 581 matchcnt += 1; 582 handle_workitem_freeblocks(WK_FREEBLKS(wk)); 583 break; 584 585 case D_FREEFRAG: 586 /* releasing a fragment when replaced as a file grows */ 587 if (WK_FREEFRAG(wk)->ff_fs == matchfs) 588 matchcnt += 1; 589 handle_workitem_freefrag(WK_FREEFRAG(wk)); 590 break; 591 592 case D_FREEFILE: 593 /* releasing an inode when its link count drops to 0 */ 594 if (WK_FREEFILE(wk)->fx_fs == matchfs) 595 matchcnt += 1; 596 handle_workitem_freefile(WK_FREEFILE(wk)); 597 break; 598 599 default: 600 panic("%s_process_worklist: Unknown type %s", 601 "softdep", TYPENAME(wk->wk_type)); 602 /* NOTREACHED */ 603 } 604 if (softdep_worklist_busy && matchmnt == NULL) 605 return (-1); 606 /* 607 * If requested, try removing inode or removal dependencies. 608 */ 609 if (req_clear_inodedeps) { 610 clear_inodedeps(p); 611 req_clear_inodedeps = 0; 612 wakeup(&proc_waiting); 613 } 614 if (req_clear_remove) { 615 clear_remove(p); 616 req_clear_remove = 0; 617 wakeup(&proc_waiting); 618 } 619 ACQUIRE_LOCK(&lk); 620 } 621 FREE_LOCK(&lk); 622 return (matchcnt); 623} 624 625/* 626 * Purge the work list of all items associated with a particular mount point. 627 */ 628int 629softdep_flushfiles(oldmnt, flags, p) 630 struct mount *oldmnt; 631 int flags; 632 struct proc *p; 633{ 634 struct vnode *devvp; 635 int error, loopcnt; 636 637 /* 638 * Await our turn to clear out the queue. 639 */ 640 while (softdep_worklist_busy) 641 tsleep(&lbolt, PRIBIO, "softflush", 0); 642 softdep_worklist_busy = 1; 643 if ((error = ffs_flushfiles(oldmnt, flags, p)) != 0) { 644 softdep_worklist_busy = 0; 645 return (error); 646 } 647 /* 648 * Alternately flush the block device associated with the mount 649 * point and process any dependencies that the flushing 650 * creates. In theory, this loop can happen at most twice, 651 * but we give it a few extra just to be sure. 652 */ 653 devvp = VFSTOUFS(oldmnt)->um_devvp; 654 for (loopcnt = 10; loopcnt > 0; loopcnt--) { 655 if (softdep_process_worklist(oldmnt) == 0) { 656 /* 657 * Do another flush in case any vnodes were brought in 658 * as part of the cleanup operations. 659 */ 660 if ((error = ffs_flushfiles(oldmnt, flags, p)) != 0) 661 break; 662 /* 663 * If we still found nothing to do, we are really done. 664 */ 665 if (softdep_process_worklist(oldmnt) == 0) 666 break; 667 } 668 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 669 error = VOP_FSYNC(devvp, p->p_ucred, MNT_WAIT, p); 670 VOP_UNLOCK(devvp, 0, p); 671 if (error) 672 break; 673 } 674 softdep_worklist_busy = 0; 675 /* 676 * If we are unmounting then it is an error to fail. If we 677 * are simply trying to downgrade to read-only, then filesystem 678 * activity can keep us busy forever, so we just fail with EBUSY. 679 */ 680 if (loopcnt == 0) { 681 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) 682 panic("softdep_flushfiles: looping"); 683 error = EBUSY; 684 } 685 return (error); 686} 687 688/* 689 * Structure hashing. 690 * 691 * There are three types of structures that can be looked up: 692 * 1) pagedep structures identified by mount point, inode number, 693 * and logical block. 694 * 2) inodedep structures identified by mount point and inode number. 695 * 3) newblk structures identified by mount point and 696 * physical block number. 697 * 698 * The "pagedep" and "inodedep" dependency structures are hashed 699 * separately from the file blocks and inodes to which they correspond. 700 * This separation helps when the in-memory copy of an inode or 701 * file block must be replaced. It also obviates the need to access 702 * an inode or file page when simply updating (or de-allocating) 703 * dependency structures. Lookup of newblk structures is needed to 704 * find newly allocated blocks when trying to associate them with 705 * their allocdirect or allocindir structure. 706 * 707 * The lookup routines optionally create and hash a new instance when 708 * an existing entry is not found. 709 */ 710#define DEPALLOC 0x0001 /* allocate structure if lookup fails */ 711 712/* 713 * Structures and routines associated with pagedep caching. 714 */ 715LIST_HEAD(pagedep_hashhead, pagedep) *pagedep_hashtbl; 716u_long pagedep_hash; /* size of hash table - 1 */ 717#define PAGEDEP_HASH(mp, inum, lbn) \ 718 (&pagedep_hashtbl[((((register_t)(mp)) >> 13) + (inum) + (lbn)) & \ 719 pagedep_hash]) 720static struct sema pagedep_in_progress; 721 722/* 723 * Look up a pagedep. Return 1 if found, 0 if not found. 724 * If not found, allocate if DEPALLOC flag is passed. 725 * Found or allocated entry is returned in pagedeppp. 726 * This routine must be called with splbio interrupts blocked. 727 */ 728static int 729pagedep_lookup(ip, lbn, flags, pagedeppp) 730 struct inode *ip; 731 ufs_lbn_t lbn; 732 int flags; 733 struct pagedep **pagedeppp; 734{ 735 struct pagedep *pagedep; 736 struct pagedep_hashhead *pagedephd; 737 struct mount *mp; 738 int i; 739 740#ifdef DEBUG 741 if (lk.lkt_held == -1) 742 panic("pagedep_lookup: lock not held"); 743#endif 744 mp = ITOV(ip)->v_mount; 745 pagedephd = PAGEDEP_HASH(mp, ip->i_number, lbn); 746top: 747 for (pagedep = LIST_FIRST(pagedephd); pagedep; 748 pagedep = LIST_NEXT(pagedep, pd_hash)) 749 if (ip->i_number == pagedep->pd_ino && 750 lbn == pagedep->pd_lbn && 751 mp == pagedep->pd_mnt) 752 break; 753 if (pagedep) { 754 *pagedeppp = pagedep; 755 return (1); 756 } 757 if ((flags & DEPALLOC) == 0) { 758 *pagedeppp = NULL; 759 return (0); 760 } 761 if (sema_get(&pagedep_in_progress, &lk) == 0) { 762 ACQUIRE_LOCK(&lk); 763 goto top; 764 } 765 MALLOC(pagedep, struct pagedep *, sizeof(struct pagedep), M_PAGEDEP, 766 M_WAITOK); 767 bzero(pagedep, sizeof(struct pagedep)); 768 pagedep->pd_list.wk_type = D_PAGEDEP; 769 pagedep->pd_mnt = mp; 770 pagedep->pd_ino = ip->i_number; 771 pagedep->pd_lbn = lbn; 772 LIST_INIT(&pagedep->pd_dirremhd); 773 LIST_INIT(&pagedep->pd_pendinghd); 774 for (i = 0; i < DAHASHSZ; i++) 775 LIST_INIT(&pagedep->pd_diraddhd[i]); 776 ACQUIRE_LOCK(&lk); 777 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash); 778 sema_release(&pagedep_in_progress); 779 *pagedeppp = pagedep; 780 return (0); 781} 782 783/* 784 * Structures and routines associated with inodedep caching. 785 */ 786LIST_HEAD(inodedep_hashhead, inodedep) *inodedep_hashtbl; 787static u_long inodedep_hash; /* size of hash table - 1 */ 788static long num_inodedep; /* number of inodedep allocated */ 789#define INODEDEP_HASH(fs, inum) \ 790 (&inodedep_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & inodedep_hash]) 791static struct sema inodedep_in_progress; 792 793/* 794 * Look up a inodedep. Return 1 if found, 0 if not found. 795 * If not found, allocate if DEPALLOC flag is passed. 796 * Found or allocated entry is returned in inodedeppp. 797 * This routine must be called with splbio interrupts blocked. 798 */ 799static int 800inodedep_lookup(fs, inum, flags, inodedeppp) 801 struct fs *fs; 802 ino_t inum; 803 int flags; 804 struct inodedep **inodedeppp; 805{ 806 struct inodedep *inodedep; 807 struct inodedep_hashhead *inodedephd; 808 int firsttry; 809 810#ifdef DEBUG 811 if (lk.lkt_held == -1) 812 panic("inodedep_lookup: lock not held"); 813#endif 814 firsttry = 1; 815 inodedephd = INODEDEP_HASH(fs, inum); 816top: 817 for (inodedep = LIST_FIRST(inodedephd); inodedep; 818 inodedep = LIST_NEXT(inodedep, id_hash)) 819 if (inum == inodedep->id_ino && fs == inodedep->id_fs) 820 break; 821 if (inodedep) { 822 *inodedeppp = inodedep; 823 return (1); 824 } 825 if ((flags & DEPALLOC) == 0) { 826 *inodedeppp = NULL; 827 return (0); 828 } 829 if (firsttry && checklimit(&num_inodedep, 1) == 1) { 830 firsttry = 0; 831 goto top; 832 } 833 if (sema_get(&inodedep_in_progress, &lk) == 0) { 834 ACQUIRE_LOCK(&lk); 835 goto top; 836 } 837 num_inodedep += 1; 838 MALLOC(inodedep, struct inodedep *, sizeof(struct inodedep), 839 M_INODEDEP, M_WAITOK); 840 inodedep->id_list.wk_type = D_INODEDEP; 841 inodedep->id_fs = fs; 842 inodedep->id_ino = inum; 843 inodedep->id_state = ALLCOMPLETE; 844 inodedep->id_nlinkdelta = 0; 845 inodedep->id_savedino = NULL; 846 inodedep->id_savedsize = -1; 847 inodedep->id_buf = NULL; 848 LIST_INIT(&inodedep->id_pendinghd); 849 LIST_INIT(&inodedep->id_inowait); 850 LIST_INIT(&inodedep->id_bufwait); 851 TAILQ_INIT(&inodedep->id_inoupdt); 852 TAILQ_INIT(&inodedep->id_newinoupdt); 853 ACQUIRE_LOCK(&lk); 854 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash); 855 sema_release(&inodedep_in_progress); 856 *inodedeppp = inodedep; 857 return (0); 858} 859 860/* 861 * Structures and routines associated with newblk caching. 862 */ 863LIST_HEAD(newblk_hashhead, newblk) *newblk_hashtbl; 864u_long newblk_hash; /* size of hash table - 1 */ 865#define NEWBLK_HASH(fs, inum) \ 866 (&newblk_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & newblk_hash]) 867static struct sema newblk_in_progress; 868 869/* 870 * Look up a newblk. Return 1 if found, 0 if not found. 871 * If not found, allocate if DEPALLOC flag is passed. 872 * Found or allocated entry is returned in newblkpp. 873 */ 874static int 875newblk_lookup(fs, newblkno, flags, newblkpp) 876 struct fs *fs; 877 ufs_daddr_t newblkno; 878 int flags; 879 struct newblk **newblkpp; 880{ 881 struct newblk *newblk; 882 struct newblk_hashhead *newblkhd; 883 884 newblkhd = NEWBLK_HASH(fs, newblkno); 885top: 886 for (newblk = LIST_FIRST(newblkhd); newblk; 887 newblk = LIST_NEXT(newblk, nb_hash)) 888 if (newblkno == newblk->nb_newblkno && fs == newblk->nb_fs) 889 break; 890 if (newblk) { 891 *newblkpp = newblk; 892 return (1); 893 } 894 if ((flags & DEPALLOC) == 0) { 895 *newblkpp = NULL; 896 return (0); 897 } 898 if (sema_get(&newblk_in_progress, 0) == 0) 899 goto top; 900 MALLOC(newblk, struct newblk *, sizeof(struct newblk), 901 M_NEWBLK, M_WAITOK); 902 newblk->nb_state = 0; 903 newblk->nb_fs = fs; 904 newblk->nb_newblkno = newblkno; 905 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash); 906 sema_release(&newblk_in_progress); 907 *newblkpp = newblk; 908 return (0); 909} 910 911/* 912 * Executed during filesystem system initialization before 913 * mounting any file systems. 914 */ 915void 916softdep_initialize() 917{ 918 919 LIST_INIT(&mkdirlisthd); 920 LIST_INIT(&softdep_workitem_pending); 921 max_softdeps = desiredvnodes * 8; 922 pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP, 923 &pagedep_hash); 924 sema_init(&pagedep_in_progress, "pagedep", PRIBIO, 0); 925 inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP, &inodedep_hash); 926 sema_init(&inodedep_in_progress, "inodedep", PRIBIO, 0); 927 newblk_hashtbl = hashinit(64, M_NEWBLK, &newblk_hash); 928 sema_init(&newblk_in_progress, "newblk", PRIBIO, 0); 929} 930 931/* 932 * Called at mount time to notify the dependency code that a 933 * filesystem wishes to use it. 934 */ 935int 936softdep_mount(devvp, mp, fs, cred) 937 struct vnode *devvp; 938 struct mount *mp; 939 struct fs *fs; 940 struct ucred *cred; 941{ 942 struct csum cstotal; 943 struct cg *cgp; 944 struct buf *bp; 945 int error, cyl; 946 947 mp->mnt_flag &= ~MNT_ASYNC; 948 mp->mnt_flag |= MNT_SOFTDEP; 949 /* 950 * When doing soft updates, the counters in the 951 * superblock may have gotten out of sync, so we have 952 * to scan the cylinder groups and recalculate them. 953 */ 954 if (fs->fs_clean != 0) 955 return (0); 956 bzero(&cstotal, sizeof cstotal); 957 for (cyl = 0; cyl < fs->fs_ncg; cyl++) { 958 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)), 959 fs->fs_cgsize, cred, &bp)) != 0) { 960 brelse(bp); 961 return (error); 962 } 963 cgp = (struct cg *)bp->b_data; 964 cstotal.cs_nffree += cgp->cg_cs.cs_nffree; 965 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree; 966 cstotal.cs_nifree += cgp->cg_cs.cs_nifree; 967 cstotal.cs_ndir += cgp->cg_cs.cs_ndir; 968 fs->fs_cs(fs, cyl) = cgp->cg_cs; 969 brelse(bp); 970 } 971#ifdef DEBUG 972 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal)) 973 printf("ffs_mountfs: superblock updated for soft updates\n"); 974#endif 975 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal); 976 return (0); 977} 978 979/* 980 * Protecting the freemaps (or bitmaps). 981 * 982 * To eliminate the need to execute fsck before mounting a file system 983 * after a power failure, one must (conservatively) guarantee that the 984 * on-disk copy of the bitmaps never indicate that a live inode or block is 985 * free. So, when a block or inode is allocated, the bitmap should be 986 * updated (on disk) before any new pointers. When a block or inode is 987 * freed, the bitmap should not be updated until all pointers have been 988 * reset. The latter dependency is handled by the delayed de-allocation 989 * approach described below for block and inode de-allocation. The former 990 * dependency is handled by calling the following procedure when a block or 991 * inode is allocated. When an inode is allocated an "inodedep" is created 992 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk. 993 * Each "inodedep" is also inserted into the hash indexing structure so 994 * that any additional link additions can be made dependent on the inode 995 * allocation. 996 * 997 * The ufs file system maintains a number of free block counts (e.g., per 998 * cylinder group, per cylinder and per <cylinder, rotational position> pair) 999 * in addition to the bitmaps. These counts are used to improve efficiency 1000 * during allocation and therefore must be consistent with the bitmaps. 1001 * There is no convenient way to guarantee post-crash consistency of these 1002 * counts with simple update ordering, for two main reasons: (1) The counts 1003 * and bitmaps for a single cylinder group block are not in the same disk 1004 * sector. If a disk write is interrupted (e.g., by power failure), one may 1005 * be written and the other not. (2) Some of the counts are located in the 1006 * superblock rather than the cylinder group block. So, we focus our soft 1007 * updates implementation on protecting the bitmaps. When mounting a 1008 * filesystem, we recompute the auxiliary counts from the bitmaps. 1009 */ 1010 1011/* 1012 * Called just after updating the cylinder group block to allocate an inode. 1013 */ 1014void 1015softdep_setup_inomapdep(bp, ip, newinum) 1016 struct buf *bp; /* buffer for cylgroup block with inode map */ 1017 struct inode *ip; /* inode related to allocation */ 1018 ino_t newinum; /* new inode number being allocated */ 1019{ 1020 struct inodedep *inodedep; 1021 struct bmsafemap *bmsafemap; 1022 1023 /* 1024 * Create a dependency for the newly allocated inode. 1025 * Panic if it already exists as something is seriously wrong. 1026 * Otherwise add it to the dependency list for the buffer holding 1027 * the cylinder group map from which it was allocated. 1028 */ 1029 ACQUIRE_LOCK(&lk); 1030 if (inodedep_lookup(ip->i_fs, newinum, DEPALLOC, &inodedep) != 0) 1031 panic("softdep_setup_inomapdep: found inode"); 1032 inodedep->id_buf = bp; 1033 inodedep->id_state &= ~DEPCOMPLETE; 1034 bmsafemap = bmsafemap_lookup(bp); 1035 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps); 1036 FREE_LOCK(&lk); 1037} 1038 1039/* 1040 * Called just after updating the cylinder group block to 1041 * allocate block or fragment. 1042 */ 1043void 1044softdep_setup_blkmapdep(bp, fs, newblkno) 1045 struct buf *bp; /* buffer for cylgroup block with block map */ 1046 struct fs *fs; /* filesystem doing allocation */ 1047 ufs_daddr_t newblkno; /* number of newly allocated block */ 1048{ 1049 struct newblk *newblk; 1050 struct bmsafemap *bmsafemap; 1051 1052 /* 1053 * Create a dependency for the newly allocated block. 1054 * Add it to the dependency list for the buffer holding 1055 * the cylinder group map from which it was allocated. 1056 */ 1057 if (newblk_lookup(fs, newblkno, DEPALLOC, &newblk) != 0) 1058 panic("softdep_setup_blkmapdep: found block"); 1059 ACQUIRE_LOCK(&lk); 1060 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(bp); 1061 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps); 1062 FREE_LOCK(&lk); 1063} 1064 1065/* 1066 * Find the bmsafemap associated with a cylinder group buffer. 1067 * If none exists, create one. The buffer must be locked when 1068 * this routine is called and this routine must be called with 1069 * splbio interrupts blocked. 1070 */ 1071static struct bmsafemap * 1072bmsafemap_lookup(bp) 1073 struct buf *bp; 1074{ 1075 struct bmsafemap *bmsafemap; 1076 struct worklist *wk; 1077 1078#ifdef DEBUG 1079 if (lk.lkt_held == -1) 1080 panic("bmsafemap_lookup: lock not held"); 1081#endif 1082 for (wk = LIST_FIRST(&bp->b_dep); wk; wk = LIST_NEXT(wk, wk_list)) 1083 if (wk->wk_type == D_BMSAFEMAP) 1084 return (WK_BMSAFEMAP(wk)); 1085 FREE_LOCK(&lk); 1086 MALLOC(bmsafemap, struct bmsafemap *, sizeof(struct bmsafemap), 1087 M_BMSAFEMAP, M_WAITOK); 1088 bmsafemap->sm_list.wk_type = D_BMSAFEMAP; 1089 bmsafemap->sm_list.wk_state = 0; 1090 bmsafemap->sm_buf = bp; 1091 LIST_INIT(&bmsafemap->sm_allocdirecthd); 1092 LIST_INIT(&bmsafemap->sm_allocindirhd); 1093 LIST_INIT(&bmsafemap->sm_inodedephd); 1094 LIST_INIT(&bmsafemap->sm_newblkhd); 1095 ACQUIRE_LOCK(&lk); 1096 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list); 1097 return (bmsafemap); 1098} 1099 1100/* 1101 * Direct block allocation dependencies. 1102 * 1103 * When a new block is allocated, the corresponding disk locations must be 1104 * initialized (with zeros or new data) before the on-disk inode points to 1105 * them. Also, the freemap from which the block was allocated must be 1106 * updated (on disk) before the inode's pointer. These two dependencies are 1107 * independent of each other and are needed for all file blocks and indirect 1108 * blocks that are pointed to directly by the inode. Just before the 1109 * "in-core" version of the inode is updated with a newly allocated block 1110 * number, a procedure (below) is called to setup allocation dependency 1111 * structures. These structures are removed when the corresponding 1112 * dependencies are satisfied or when the block allocation becomes obsolete 1113 * (i.e., the file is deleted, the block is de-allocated, or the block is a 1114 * fragment that gets upgraded). All of these cases are handled in 1115 * procedures described later. 1116 * 1117 * When a file extension causes a fragment to be upgraded, either to a larger 1118 * fragment or to a full block, the on-disk location may change (if the 1119 * previous fragment could not simply be extended). In this case, the old 1120 * fragment must be de-allocated, but not until after the inode's pointer has 1121 * been updated. In most cases, this is handled by later procedures, which 1122 * will construct a "freefrag" structure to be added to the workitem queue 1123 * when the inode update is complete (or obsolete). The main exception to 1124 * this is when an allocation occurs while a pending allocation dependency 1125 * (for the same block pointer) remains. This case is handled in the main 1126 * allocation dependency setup procedure by immediately freeing the 1127 * unreferenced fragments. 1128 */ 1129void 1130softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 1131 struct inode *ip; /* inode to which block is being added */ 1132 ufs_lbn_t lbn; /* block pointer within inode */ 1133 ufs_daddr_t newblkno; /* disk block number being added */ 1134 ufs_daddr_t oldblkno; /* previous block number, 0 unless frag */ 1135 long newsize; /* size of new block */ 1136 long oldsize; /* size of new block */ 1137 struct buf *bp; /* bp for allocated block */ 1138{ 1139 struct allocdirect *adp, *oldadp; 1140 struct allocdirectlst *adphead; 1141 struct bmsafemap *bmsafemap; 1142 struct inodedep *inodedep; 1143 struct pagedep *pagedep; 1144 struct newblk *newblk; 1145 1146 MALLOC(adp, struct allocdirect *, sizeof(struct allocdirect), 1147 M_ALLOCDIRECT, M_WAITOK); 1148 bzero(adp, sizeof(struct allocdirect)); 1149 adp->ad_list.wk_type = D_ALLOCDIRECT; 1150 adp->ad_lbn = lbn; 1151 adp->ad_newblkno = newblkno; 1152 adp->ad_oldblkno = oldblkno; 1153 adp->ad_newsize = newsize; 1154 adp->ad_oldsize = oldsize; 1155 adp->ad_state = ATTACHED; 1156 if (newblkno == oldblkno) 1157 adp->ad_freefrag = NULL; 1158 else 1159 adp->ad_freefrag = newfreefrag(ip, oldblkno, oldsize); 1160 1161 if (newblk_lookup(ip->i_fs, newblkno, 0, &newblk) == 0) 1162 panic("softdep_setup_allocdirect: lost block"); 1163 1164 ACQUIRE_LOCK(&lk); 1165 (void) inodedep_lookup(ip->i_fs, ip->i_number, DEPALLOC, &inodedep); 1166 adp->ad_inodedep = inodedep; 1167 1168 if (newblk->nb_state == DEPCOMPLETE) { 1169 adp->ad_state |= DEPCOMPLETE; 1170 adp->ad_buf = NULL; 1171 } else { 1172 bmsafemap = newblk->nb_bmsafemap; 1173 adp->ad_buf = bmsafemap->sm_buf; 1174 LIST_REMOVE(newblk, nb_deps); 1175 LIST_INSERT_HEAD(&bmsafemap->sm_allocdirecthd, adp, ad_deps); 1176 } 1177 LIST_REMOVE(newblk, nb_hash); 1178 FREE(newblk, M_NEWBLK); 1179 1180 WORKLIST_INSERT(&bp->b_dep, &adp->ad_list); 1181 if (lbn >= NDADDR) { 1182 /* allocating an indirect block */ 1183 if (oldblkno != 0) 1184 panic("softdep_setup_allocdirect: non-zero indir"); 1185 } else { 1186 /* 1187 * Allocating a direct block. 1188 * 1189 * If we are allocating a directory block, then we must 1190 * allocate an associated pagedep to track additions and 1191 * deletions. 1192 */ 1193 if ((ip->i_mode & IFMT) == IFDIR && 1194 pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0) 1195 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list); 1196 } 1197 /* 1198 * The list of allocdirects must be kept in sorted and ascending 1199 * order so that the rollback routines can quickly determine the 1200 * first uncommitted block (the size of the file stored on disk 1201 * ends at the end of the lowest committed fragment, or if there 1202 * are no fragments, at the end of the highest committed block). 1203 * Since files generally grow, the typical case is that the new 1204 * block is to be added at the end of the list. We speed this 1205 * special case by checking against the last allocdirect in the 1206 * list before laboriously traversing the list looking for the 1207 * insertion point. 1208 */ 1209 adphead = &inodedep->id_newinoupdt; 1210 oldadp = TAILQ_LAST(adphead, allocdirectlst); 1211 if (oldadp == NULL || oldadp->ad_lbn <= lbn) { 1212 /* insert at end of list */ 1213 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 1214 if (oldadp != NULL && oldadp->ad_lbn == lbn) 1215 allocdirect_merge(adphead, adp, oldadp); 1216 FREE_LOCK(&lk); 1217 return; 1218 } 1219 for (oldadp = TAILQ_FIRST(adphead); oldadp; 1220 oldadp = TAILQ_NEXT(oldadp, ad_next)) { 1221 if (oldadp->ad_lbn >= lbn) 1222 break; 1223 } 1224 if (oldadp == NULL) 1225 panic("softdep_setup_allocdirect: lost entry"); 1226 /* insert in middle of list */ 1227 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 1228 if (oldadp->ad_lbn == lbn) 1229 allocdirect_merge(adphead, adp, oldadp); 1230 FREE_LOCK(&lk); 1231} 1232 1233/* 1234 * Replace an old allocdirect dependency with a newer one. 1235 * This routine must be called with splbio interrupts blocked. 1236 */ 1237static void 1238allocdirect_merge(adphead, newadp, oldadp) 1239 struct allocdirectlst *adphead; /* head of list holding allocdirects */ 1240 struct allocdirect *newadp; /* allocdirect being added */ 1241 struct allocdirect *oldadp; /* existing allocdirect being checked */ 1242{ 1243 struct freefrag *freefrag; 1244 1245#ifdef DEBUG 1246 if (lk.lkt_held == -1) 1247 panic("allocdirect_merge: lock not held"); 1248#endif 1249 if (newadp->ad_oldblkno != oldadp->ad_newblkno || 1250 newadp->ad_oldsize != oldadp->ad_newsize || 1251 newadp->ad_lbn >= NDADDR) 1252 panic("allocdirect_check: old %d != new %d || lbn %ld >= %d", 1253 newadp->ad_oldblkno, oldadp->ad_newblkno, newadp->ad_lbn, 1254 NDADDR); 1255 newadp->ad_oldblkno = oldadp->ad_oldblkno; 1256 newadp->ad_oldsize = oldadp->ad_oldsize; 1257 /* 1258 * If the old dependency had a fragment to free or had never 1259 * previously had a block allocated, then the new dependency 1260 * can immediately post its freefrag and adopt the old freefrag. 1261 * This action is done by swapping the freefrag dependencies. 1262 * The new dependency gains the old one's freefrag, and the 1263 * old one gets the new one and then immediately puts it on 1264 * the worklist when it is freed by free_allocdirect. It is 1265 * not possible to do this swap when the old dependency had a 1266 * non-zero size but no previous fragment to free. This condition 1267 * arises when the new block is an extension of the old block. 1268 * Here, the first part of the fragment allocated to the new 1269 * dependency is part of the block currently claimed on disk by 1270 * the old dependency, so cannot legitimately be freed until the 1271 * conditions for the new dependency are fulfilled. 1272 */ 1273 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) { 1274 freefrag = newadp->ad_freefrag; 1275 newadp->ad_freefrag = oldadp->ad_freefrag; 1276 oldadp->ad_freefrag = freefrag; 1277 } 1278 free_allocdirect(adphead, oldadp, 0); 1279} 1280 1281/* 1282 * Allocate a new freefrag structure if needed. 1283 */ 1284static struct freefrag * 1285newfreefrag(ip, blkno, size) 1286 struct inode *ip; 1287 ufs_daddr_t blkno; 1288 long size; 1289{ 1290 struct freefrag *freefrag; 1291 struct fs *fs; 1292 1293 if (blkno == 0) 1294 return (NULL); 1295 fs = ip->i_fs; 1296 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag) 1297 panic("newfreefrag: frag size"); 1298 MALLOC(freefrag, struct freefrag *, sizeof(struct freefrag), 1299 M_FREEFRAG, M_WAITOK); 1300 freefrag->ff_list.wk_type = D_FREEFRAG; 1301 freefrag->ff_state = ip->i_uid & ~ONWORKLIST; /* XXX - used below */ 1302 freefrag->ff_inum = ip->i_number; 1303 freefrag->ff_fs = fs; 1304 freefrag->ff_devvp = ip->i_devvp; 1305 freefrag->ff_blkno = blkno; 1306 freefrag->ff_fragsize = size; 1307 return (freefrag); 1308} 1309 1310/* 1311 * This workitem de-allocates fragments that were replaced during 1312 * file block allocation. 1313 */ 1314static void 1315handle_workitem_freefrag(freefrag) 1316 struct freefrag *freefrag; 1317{ 1318 struct inode tip; 1319 1320 tip.i_fs = freefrag->ff_fs; 1321 tip.i_devvp = freefrag->ff_devvp; 1322 tip.i_dev = freefrag->ff_devvp->v_rdev; 1323 tip.i_number = freefrag->ff_inum; 1324 tip.i_uid = freefrag->ff_state & ~ONWORKLIST; /* XXX - set above */ 1325 ffs_blkfree(&tip, freefrag->ff_blkno, freefrag->ff_fragsize); 1326 FREE(freefrag, M_FREEFRAG); 1327} 1328 1329/* 1330 * Indirect block allocation dependencies. 1331 * 1332 * The same dependencies that exist for a direct block also exist when 1333 * a new block is allocated and pointed to by an entry in a block of 1334 * indirect pointers. The undo/redo states described above are also 1335 * used here. Because an indirect block contains many pointers that 1336 * may have dependencies, a second copy of the entire in-memory indirect 1337 * block is kept. The buffer cache copy is always completely up-to-date. 1338 * The second copy, which is used only as a source for disk writes, 1339 * contains only the safe pointers (i.e., those that have no remaining 1340 * update dependencies). The second copy is freed when all pointers 1341 * are safe. The cache is not allowed to replace indirect blocks with 1342 * pending update dependencies. If a buffer containing an indirect 1343 * block with dependencies is written, these routines will mark it 1344 * dirty again. It can only be successfully written once all the 1345 * dependencies are removed. The ffs_fsync routine in conjunction with 1346 * softdep_sync_metadata work together to get all the dependencies 1347 * removed so that a file can be successfully written to disk. Three 1348 * procedures are used when setting up indirect block pointer 1349 * dependencies. The division is necessary because of the organization 1350 * of the "balloc" routine and because of the distinction between file 1351 * pages and file metadata blocks. 1352 */ 1353 1354/* 1355 * Allocate a new allocindir structure. 1356 */ 1357static struct allocindir * 1358newallocindir(ip, ptrno, newblkno, oldblkno) 1359 struct inode *ip; /* inode for file being extended */ 1360 int ptrno; /* offset of pointer in indirect block */ 1361 ufs_daddr_t newblkno; /* disk block number being added */ 1362 ufs_daddr_t oldblkno; /* previous block number, 0 if none */ 1363{ 1364 struct allocindir *aip; 1365 1366 MALLOC(aip, struct allocindir *, sizeof(struct allocindir), 1367 M_ALLOCINDIR, M_WAITOK); 1368 bzero(aip, sizeof(struct allocindir)); 1369 aip->ai_list.wk_type = D_ALLOCINDIR; 1370 aip->ai_state = ATTACHED; 1371 aip->ai_offset = ptrno; 1372 aip->ai_newblkno = newblkno; 1373 aip->ai_oldblkno = oldblkno; 1374 aip->ai_freefrag = newfreefrag(ip, oldblkno, ip->i_fs->fs_bsize); 1375 return (aip); 1376} 1377 1378/* 1379 * Called just before setting an indirect block pointer 1380 * to a newly allocated file page. 1381 */ 1382void 1383softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 1384 struct inode *ip; /* inode for file being extended */ 1385 ufs_lbn_t lbn; /* allocated block number within file */ 1386 struct buf *bp; /* buffer with indirect blk referencing page */ 1387 int ptrno; /* offset of pointer in indirect block */ 1388 ufs_daddr_t newblkno; /* disk block number being added */ 1389 ufs_daddr_t oldblkno; /* previous block number, 0 if none */ 1390 struct buf *nbp; /* buffer holding allocated page */ 1391{ 1392 struct allocindir *aip; 1393 struct pagedep *pagedep; 1394 1395 aip = newallocindir(ip, ptrno, newblkno, oldblkno); 1396 ACQUIRE_LOCK(&lk); 1397 /* 1398 * If we are allocating a directory page, then we must 1399 * allocate an associated pagedep to track additions and 1400 * deletions. 1401 */ 1402 if ((ip->i_mode & IFMT) == IFDIR && 1403 pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0) 1404 WORKLIST_INSERT(&nbp->b_dep, &pagedep->pd_list); 1405 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_list); 1406 FREE_LOCK(&lk); 1407 setup_allocindir_phase2(bp, ip, aip); 1408} 1409 1410/* 1411 * Called just before setting an indirect block pointer to a 1412 * newly allocated indirect block. 1413 */ 1414void 1415softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 1416 struct buf *nbp; /* newly allocated indirect block */ 1417 struct inode *ip; /* inode for file being extended */ 1418 struct buf *bp; /* indirect block referencing allocated block */ 1419 int ptrno; /* offset of pointer in indirect block */ 1420 ufs_daddr_t newblkno; /* disk block number being added */ 1421{ 1422 struct allocindir *aip; 1423 1424 aip = newallocindir(ip, ptrno, newblkno, 0); 1425 ACQUIRE_LOCK(&lk); 1426 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_list); 1427 FREE_LOCK(&lk); 1428 setup_allocindir_phase2(bp, ip, aip); 1429} 1430 1431/* 1432 * Called to finish the allocation of the "aip" allocated 1433 * by one of the two routines above. 1434 */ 1435static void 1436setup_allocindir_phase2(bp, ip, aip) 1437 struct buf *bp; /* in-memory copy of the indirect block */ 1438 struct inode *ip; /* inode for file being extended */ 1439 struct allocindir *aip; /* allocindir allocated by the above routines */ 1440{ 1441 struct worklist *wk; 1442 struct indirdep *indirdep, *newindirdep; 1443 struct bmsafemap *bmsafemap; 1444 struct allocindir *oldaip; 1445 struct freefrag *freefrag; 1446 struct newblk *newblk; 1447 1448 if (bp->b_lblkno >= 0) 1449 panic("setup_allocindir_phase2: not indir blk"); 1450 for (indirdep = NULL, newindirdep = NULL; ; ) { 1451 ACQUIRE_LOCK(&lk); 1452 for (wk = LIST_FIRST(&bp->b_dep); wk; 1453 wk = LIST_NEXT(wk, wk_list)) { 1454 if (wk->wk_type != D_INDIRDEP) 1455 continue; 1456 indirdep = WK_INDIRDEP(wk); 1457 break; 1458 } 1459 if (indirdep == NULL && newindirdep) { 1460 indirdep = newindirdep; 1461 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list); 1462 newindirdep = NULL; 1463 } 1464 FREE_LOCK(&lk); 1465 if (indirdep) { 1466 if (newblk_lookup(ip->i_fs, aip->ai_newblkno, 0, 1467 &newblk) == 0) 1468 panic("setup_allocindir: lost block"); 1469 ACQUIRE_LOCK(&lk); 1470 if (newblk->nb_state == DEPCOMPLETE) { 1471 aip->ai_state |= DEPCOMPLETE; 1472 aip->ai_buf = NULL; 1473 } else { 1474 bmsafemap = newblk->nb_bmsafemap; 1475 aip->ai_buf = bmsafemap->sm_buf; 1476 LIST_REMOVE(newblk, nb_deps); 1477 LIST_INSERT_HEAD(&bmsafemap->sm_allocindirhd, 1478 aip, ai_deps); 1479 } 1480 LIST_REMOVE(newblk, nb_hash); 1481 FREE(newblk, M_NEWBLK); 1482 aip->ai_indirdep = indirdep; 1483 /* 1484 * Check to see if there is an existing dependency 1485 * for this block. If there is, merge the old 1486 * dependency into the new one. 1487 */ 1488 if (aip->ai_oldblkno == 0) 1489 oldaip = NULL; 1490 else 1491 for (oldaip=LIST_FIRST(&indirdep->ir_deplisthd); 1492 oldaip; oldaip = LIST_NEXT(oldaip, ai_next)) 1493 if (oldaip->ai_offset == aip->ai_offset) 1494 break; 1495 if (oldaip != NULL) { 1496 if (oldaip->ai_newblkno != aip->ai_oldblkno) 1497 panic("setup_allocindir_phase2: blkno"); 1498 aip->ai_oldblkno = oldaip->ai_oldblkno; 1499 freefrag = oldaip->ai_freefrag; 1500 oldaip->ai_freefrag = aip->ai_freefrag; 1501 aip->ai_freefrag = freefrag; 1502 free_allocindir(oldaip, NULL); 1503 } 1504 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next); 1505 ((ufs_daddr_t *)indirdep->ir_savebp->b_data) 1506 [aip->ai_offset] = aip->ai_oldblkno; 1507 FREE_LOCK(&lk); 1508 } 1509 if (newindirdep) { 1510 if (indirdep->ir_savebp != NULL) 1511 brelse(newindirdep->ir_savebp); 1512 WORKITEM_FREE((caddr_t)newindirdep, D_INDIRDEP); 1513 } 1514 if (indirdep) 1515 break; 1516 MALLOC(newindirdep, struct indirdep *, sizeof(struct indirdep), 1517 M_INDIRDEP, M_WAITOK); 1518 newindirdep->ir_list.wk_type = D_INDIRDEP; 1519 newindirdep->ir_state = ATTACHED; 1520 LIST_INIT(&newindirdep->ir_deplisthd); 1521 LIST_INIT(&newindirdep->ir_donehd); 1522 if (bp->b_blkno == bp->b_lblkno) { 1523 VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno, 1524 NULL, NULL); 1525 } 1526 newindirdep->ir_savebp = 1527 getblk(ip->i_devvp, bp->b_blkno, bp->b_bcount, 0, 0); 1528 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount); 1529 } 1530} 1531 1532/* 1533 * Block de-allocation dependencies. 1534 * 1535 * When blocks are de-allocated, the on-disk pointers must be nullified before 1536 * the blocks are made available for use by other files. (The true 1537 * requirement is that old pointers must be nullified before new on-disk 1538 * pointers are set. We chose this slightly more stringent requirement to 1539 * reduce complexity.) Our implementation handles this dependency by updating 1540 * the inode (or indirect block) appropriately but delaying the actual block 1541 * de-allocation (i.e., freemap and free space count manipulation) until 1542 * after the updated versions reach stable storage. After the disk is 1543 * updated, the blocks can be safely de-allocated whenever it is convenient. 1544 * This implementation handles only the common case of reducing a file's 1545 * length to zero. Other cases are handled by the conventional synchronous 1546 * write approach. 1547 * 1548 * The ffs implementation with which we worked double-checks 1549 * the state of the block pointers and file size as it reduces 1550 * a file's length. Some of this code is replicated here in our 1551 * soft updates implementation. The freeblks->fb_chkcnt field is 1552 * used to transfer a part of this information to the procedure 1553 * that eventually de-allocates the blocks. 1554 * 1555 * This routine should be called from the routine that shortens 1556 * a file's length, before the inode's size or block pointers 1557 * are modified. It will save the block pointer information for 1558 * later release and zero the inode so that the calling routine 1559 * can release it. 1560 */ 1561static long num_freeblks; /* number of freeblks allocated */ 1562void 1563softdep_setup_freeblocks(ip, length) 1564 struct inode *ip; /* The inode whose length is to be reduced */ 1565 off_t length; /* The new length for the file */ 1566{ 1567 struct freeblks *freeblks; 1568 struct inodedep *inodedep; 1569 struct allocdirect *adp; 1570 struct vnode *vp; 1571 struct buf *bp; 1572 struct fs *fs; 1573 int i, error; 1574 1575 fs = ip->i_fs; 1576 if (length != 0) 1577 panic("softde_setup_freeblocks: non-zero length"); 1578 (void) checklimit(&num_freeblks, 0); 1579 num_freeblks += 1; 1580 MALLOC(freeblks, struct freeblks *, sizeof(struct freeblks), 1581 M_FREEBLKS, M_WAITOK); 1582 bzero(freeblks, sizeof(struct freeblks)); 1583 freeblks->fb_list.wk_type = D_FREEBLKS; 1584 freeblks->fb_uid = ip->i_uid; 1585 freeblks->fb_previousinum = ip->i_number; 1586 freeblks->fb_devvp = ip->i_devvp; 1587 freeblks->fb_fs = fs; 1588 freeblks->fb_oldsize = ip->i_size; 1589 freeblks->fb_newsize = length; 1590 freeblks->fb_chkcnt = ip->i_blocks; 1591 for (i = 0; i < NDADDR; i++) { 1592 freeblks->fb_dblks[i] = ip->i_db[i]; 1593 ip->i_db[i] = 0; 1594 } 1595 for (i = 0; i < NIADDR; i++) { 1596 freeblks->fb_iblks[i] = ip->i_ib[i]; 1597 ip->i_ib[i] = 0; 1598 } 1599 ip->i_blocks = 0; 1600 ip->i_size = 0; 1601 /* 1602 * Push the zero'ed inode to to its disk buffer so that we are free 1603 * to delete its dependencies below. Once the dependencies are gone 1604 * the buffer can be safely released. 1605 */ 1606 if ((error = bread(ip->i_devvp, 1607 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 1608 (int)fs->fs_bsize, NOCRED, &bp)) != 0) 1609 softdep_error("softdep_setup_freeblocks", error); 1610 *((struct dinode *)bp->b_data + ino_to_fsbo(fs, ip->i_number)) = 1611 ip->i_din; 1612 /* 1613 * Find and eliminate any inode dependencies. 1614 */ 1615 ACQUIRE_LOCK(&lk); 1616 (void) inodedep_lookup(fs, ip->i_number, DEPALLOC, &inodedep); 1617 if ((inodedep->id_state & IOSTARTED) != 0) 1618 panic("softdep_setup_freeblocks: inode busy"); 1619 /* 1620 * Add the freeblks structure to the list of operations that 1621 * must await the zero'ed inode being written to disk. 1622 */ 1623 WORKLIST_INSERT(&inodedep->id_bufwait, &freeblks->fb_list); 1624 /* 1625 * Because the file length has been truncated to zero, any 1626 * pending block allocation dependency structures associated 1627 * with this inode are obsolete and can simply be de-allocated. 1628 * We must first merge the two dependency lists to get rid of 1629 * any duplicate freefrag structures, then purge the merged list. 1630 */ 1631 merge_inode_lists(inodedep); 1632 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != 0) 1633 free_allocdirect(&inodedep->id_inoupdt, adp, 1); 1634 FREE_LOCK(&lk); 1635 bdwrite(bp); 1636 /* 1637 * We must wait for any I/O in progress to finish so that 1638 * all potential buffers on the dirty list will be visible. 1639 * Once they are all there, walk the list and get rid of 1640 * any dependencies. 1641 */ 1642 vp = ITOV(ip); 1643 ACQUIRE_LOCK(&lk); 1644 drain_output(vp, 1); 1645 while (getdirtybuf(&TAILQ_FIRST(&vp->v_dirtyblkhd), MNT_WAIT)) { 1646 bp = TAILQ_FIRST(&vp->v_dirtyblkhd); 1647 (void) inodedep_lookup(fs, ip->i_number, 0, &inodedep); 1648 deallocate_dependencies(bp, inodedep); 1649 bp->b_flags |= B_INVAL | B_NOCACHE; 1650 FREE_LOCK(&lk); 1651 brelse(bp); 1652 ACQUIRE_LOCK(&lk); 1653 } 1654 /* 1655 * Try freeing the inodedep in case that was the last dependency. 1656 */ 1657 if ((inodedep_lookup(fs, ip->i_number, 0, &inodedep)) != 0) 1658 (void) free_inodedep(inodedep); 1659 FREE_LOCK(&lk); 1660} 1661 1662/* 1663 * Reclaim any dependency structures from a buffer that is about to 1664 * be reallocated to a new vnode. The buffer must be locked, thus, 1665 * no I/O completion operations can occur while we are manipulating 1666 * its associated dependencies. The mutex is held so that other I/O's 1667 * associated with related dependencies do not occur. 1668 */ 1669static void 1670deallocate_dependencies(bp, inodedep) 1671 struct buf *bp; 1672 struct inodedep *inodedep; 1673{ 1674 struct worklist *wk; 1675 struct indirdep *indirdep; 1676 struct allocindir *aip; 1677 struct pagedep *pagedep; 1678 struct dirrem *dirrem; 1679 struct diradd *dap; 1680 int i; 1681 1682 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) { 1683 switch (wk->wk_type) { 1684 1685 case D_INDIRDEP: 1686 indirdep = WK_INDIRDEP(wk); 1687 /* 1688 * None of the indirect pointers will ever be visible, 1689 * so they can simply be tossed. GOINGAWAY ensures 1690 * that allocated pointers will be saved in the buffer 1691 * cache until they are freed. Note that they will 1692 * only be able to be found by their physical address 1693 * since the inode mapping the logical address will 1694 * be gone. The save buffer used for the safe copy 1695 * was allocated in setup_allocindir_phase2 using 1696 * the physical address so it could be used for this 1697 * purpose. Hence we swap the safe copy with the real 1698 * copy, allowing the safe copy to be freed and holding 1699 * on to the real copy for later use in indir_trunc. 1700 */ 1701 if (indirdep->ir_state & GOINGAWAY) 1702 panic("deallocate_dependencies: already gone"); 1703 indirdep->ir_state |= GOINGAWAY; 1704 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != 0) 1705 free_allocindir(aip, inodedep); 1706 if (bp->b_lblkno >= 0 || 1707 bp->b_blkno != indirdep->ir_savebp->b_lblkno) 1708 panic("deallocate_dependencies: not indir"); 1709 bcopy(bp->b_data, indirdep->ir_savebp->b_data, 1710 bp->b_bcount); 1711 WORKLIST_REMOVE(wk); 1712 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, wk); 1713 continue; 1714 1715 case D_PAGEDEP: 1716 pagedep = WK_PAGEDEP(wk); 1717 /* 1718 * None of the directory additions will ever be 1719 * visible, so they can simply be tossed. 1720 */ 1721 for (i = 0; i < DAHASHSZ; i++) 1722 while (dap=LIST_FIRST(&pagedep->pd_diraddhd[i])) 1723 free_diradd(dap); 1724 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != 0) 1725 free_diradd(dap); 1726 /* 1727 * Copy any directory remove dependencies to the list 1728 * to be processed after the zero'ed inode is written. 1729 * If the inode has already been written, then they 1730 * can be dumped directly onto the work list. 1731 */ 1732 for (dirrem = LIST_FIRST(&pagedep->pd_dirremhd); dirrem; 1733 dirrem = LIST_NEXT(dirrem, dm_next)) { 1734 LIST_REMOVE(dirrem, dm_next); 1735 dirrem->dm_dirinum = pagedep->pd_ino; 1736 if (inodedep == NULL) 1737 add_to_worklist(&dirrem->dm_list); 1738 else 1739 WORKLIST_INSERT(&inodedep->id_bufwait, 1740 &dirrem->dm_list); 1741 } 1742 WORKLIST_REMOVE(&pagedep->pd_list); 1743 LIST_REMOVE(pagedep, pd_hash); 1744 WORKITEM_FREE(pagedep, D_PAGEDEP); 1745 continue; 1746 1747 case D_ALLOCINDIR: 1748 free_allocindir(WK_ALLOCINDIR(wk), inodedep); 1749 continue; 1750 1751 case D_ALLOCDIRECT: 1752 case D_INODEDEP: 1753 panic("deallocate_dependencies: Unexpected type %s", 1754 TYPENAME(wk->wk_type)); 1755 /* NOTREACHED */ 1756 1757 default: 1758 panic("deallocate_dependencies: Unknown type %s", 1759 TYPENAME(wk->wk_type)); 1760 /* NOTREACHED */ 1761 } 1762 } 1763} 1764 1765/* 1766 * Free an allocdirect. Generate a new freefrag work request if appropriate. 1767 * This routine must be called with splbio interrupts blocked. 1768 */ 1769static void 1770free_allocdirect(adphead, adp, delay) 1771 struct allocdirectlst *adphead; 1772 struct allocdirect *adp; 1773 int delay; 1774{ 1775 1776#ifdef DEBUG 1777 if (lk.lkt_held == -1) 1778 panic("free_allocdirect: lock not held"); 1779#endif 1780 if ((adp->ad_state & DEPCOMPLETE) == 0) 1781 LIST_REMOVE(adp, ad_deps); 1782 TAILQ_REMOVE(adphead, adp, ad_next); 1783 if ((adp->ad_state & COMPLETE) == 0) 1784 WORKLIST_REMOVE(&adp->ad_list); 1785 if (adp->ad_freefrag != NULL) { 1786 if (delay) 1787 WORKLIST_INSERT(&adp->ad_inodedep->id_bufwait, 1788 &adp->ad_freefrag->ff_list); 1789 else 1790 add_to_worklist(&adp->ad_freefrag->ff_list); 1791 } 1792 WORKITEM_FREE(adp, D_ALLOCDIRECT); 1793} 1794 1795/* 1796 * Prepare an inode to be freed. The actual free operation is not 1797 * done until the zero'ed inode has been written to disk. 1798 */ 1799static long num_freefile; /* number of freefile allocated */ 1800void 1801softdep_freefile(pvp, ino, mode) 1802 struct vnode *pvp; 1803 ino_t ino; 1804 int mode; 1805{ 1806 struct inode *ip = VTOI(pvp); 1807 struct inodedep *inodedep; 1808 struct freefile *freefile; 1809 1810 /* 1811 * This sets up the inode de-allocation dependency. 1812 */ 1813 (void) checklimit(&num_freefile, 0); 1814 num_freefile += 1; 1815 MALLOC(freefile, struct freefile *, sizeof(struct freefile), 1816 M_FREEFILE, M_WAITOK); 1817 freefile->fx_list.wk_type = D_FREEFILE; 1818 freefile->fx_list.wk_state = 0; 1819 freefile->fx_mode = mode; 1820 freefile->fx_oldinum = ino; 1821 freefile->fx_devvp = ip->i_devvp; 1822 freefile->fx_fs = ip->i_fs; 1823 1824 /* 1825 * If the inodedep does not exist, then the zero'ed inode has 1826 * been written to disk and we can free the file immediately. 1827 */ 1828 ACQUIRE_LOCK(&lk); 1829 if (inodedep_lookup(ip->i_fs, ino, 0, &inodedep) == 0) { 1830 add_to_worklist(&freefile->fx_list); 1831 FREE_LOCK(&lk); 1832 return; 1833 } 1834 1835 /* 1836 * If we still have a bitmap dependency, then the inode has never 1837 * been written to disk. Drop the dependency as it is no longer 1838 * necessary since the inode is being deallocated. We could process 1839 * the freefile immediately, but then we would have to clear the 1840 * id_inowait dependencies here and it is easier just to let the 1841 * zero'ed inode be written and let them be cleaned up in the 1842 * normal followup actions that follow the inode write. 1843 */ 1844 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 1845 inodedep->id_state |= DEPCOMPLETE; 1846 LIST_REMOVE(inodedep, id_deps); 1847 inodedep->id_buf = NULL; 1848 } 1849 /* 1850 * If the inodedep has no dependencies associated with it, 1851 * then we must free it here and free the file immediately. 1852 * This case arises when an early allocation fails (for 1853 * example, the user is over their file quota). 1854 */ 1855 if (free_inodedep(inodedep) == 0) 1856 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list); 1857 else 1858 add_to_worklist(&freefile->fx_list); 1859 FREE_LOCK(&lk); 1860} 1861 1862/* 1863 * Try to free an inodedep structure. Return 1 if it could be freed. 1864 */ 1865static int 1866free_inodedep(inodedep) 1867 struct inodedep *inodedep; 1868{ 1869 1870 if ((inodedep->id_state & ONWORKLIST) != 0 || 1871 (inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE || 1872 LIST_FIRST(&inodedep->id_pendinghd) != NULL || 1873 LIST_FIRST(&inodedep->id_bufwait) != NULL || 1874 LIST_FIRST(&inodedep->id_inowait) != NULL || 1875 TAILQ_FIRST(&inodedep->id_inoupdt) != NULL || 1876 TAILQ_FIRST(&inodedep->id_newinoupdt) != NULL || 1877 inodedep->id_nlinkdelta != 0 || inodedep->id_savedino != NULL) 1878 return (0); 1879 LIST_REMOVE(inodedep, id_hash); 1880 WORKITEM_FREE(inodedep, D_INODEDEP); 1881 num_inodedep -= 1; 1882 return (1); 1883} 1884 1885/* 1886 * This workitem routine performs the block de-allocation. 1887 * The workitem is added to the pending list after the updated 1888 * inode block has been written to disk. As mentioned above, 1889 * checks regarding the number of blocks de-allocated (compared 1890 * to the number of blocks allocated for the file) are also 1891 * performed in this function. 1892 */ 1893static void 1894handle_workitem_freeblocks(freeblks) 1895 struct freeblks *freeblks; 1896{ 1897 struct inode tip; 1898 ufs_daddr_t bn; 1899 struct fs *fs; 1900 int i, level, bsize; 1901 long nblocks, blocksreleased = 0; 1902 int error, allerror = 0; 1903 ufs_lbn_t baselbns[NIADDR], tmpval; 1904 1905 tip.i_number = freeblks->fb_previousinum; 1906 tip.i_devvp = freeblks->fb_devvp; 1907 tip.i_dev = freeblks->fb_devvp->v_rdev; 1908 tip.i_fs = freeblks->fb_fs; 1909 tip.i_size = freeblks->fb_oldsize; 1910 tip.i_uid = freeblks->fb_uid; 1911 fs = freeblks->fb_fs; 1912 tmpval = 1; 1913 baselbns[0] = NDADDR; 1914 for (i = 1; i < NIADDR; i++) { 1915 tmpval *= NINDIR(fs); 1916 baselbns[i] = baselbns[i - 1] + tmpval; 1917 } 1918 nblocks = btodb(fs->fs_bsize); 1919 blocksreleased = 0; 1920 /* 1921 * Indirect blocks first. 1922 */ 1923 for (level = (NIADDR - 1); level >= 0; level--) { 1924 if ((bn = freeblks->fb_iblks[level]) == 0) 1925 continue; 1926 if ((error = indir_trunc(&tip, fsbtodb(fs, bn), level, 1927 baselbns[level], &blocksreleased)) == 0) 1928 allerror = error; 1929 ffs_blkfree(&tip, bn, fs->fs_bsize); 1930 blocksreleased += nblocks; 1931 } 1932 /* 1933 * All direct blocks or frags. 1934 */ 1935 for (i = (NDADDR - 1); i >= 0; i--) { 1936 if ((bn = freeblks->fb_dblks[i]) == 0) 1937 continue; 1938 bsize = blksize(fs, &tip, i); 1939 ffs_blkfree(&tip, bn, bsize); 1940 blocksreleased += btodb(bsize); 1941 } 1942 1943#ifdef DIAGNOSTIC 1944 if (freeblks->fb_chkcnt != blocksreleased) 1945 panic("handle_workitem_freeblocks: block count"); 1946 if (allerror) 1947 softdep_error("handle_workitem_freeblks", allerror); 1948#endif /* DIAGNOSTIC */ 1949 WORKITEM_FREE(freeblks, D_FREEBLKS); 1950 num_freeblks -= 1; 1951} 1952 1953/* 1954 * Release blocks associated with the inode ip and stored in the indirect 1955 * block dbn. If level is greater than SINGLE, the block is an indirect block 1956 * and recursive calls to indirtrunc must be used to cleanse other indirect 1957 * blocks. 1958 */ 1959static int 1960indir_trunc(ip, dbn, level, lbn, countp) 1961 struct inode *ip; 1962 ufs_daddr_t dbn; 1963 int level; 1964 ufs_lbn_t lbn; 1965 long *countp; 1966{ 1967 struct buf *bp; 1968 ufs_daddr_t *bap; 1969 ufs_daddr_t nb; 1970 struct fs *fs; 1971 struct worklist *wk; 1972 struct indirdep *indirdep; 1973 int i, lbnadd, nblocks; 1974 int error, allerror = 0; 1975 1976 fs = ip->i_fs; 1977 lbnadd = 1; 1978 for (i = level; i > 0; i--) 1979 lbnadd *= NINDIR(fs); 1980 /* 1981 * Get buffer of block pointers to be freed. This routine is not 1982 * called until the zero'ed inode has been written, so it is safe 1983 * to free blocks as they are encountered. Because the inode has 1984 * been zero'ed, calls to bmap on these blocks will fail. So, we 1985 * have to use the on-disk address and the block device for the 1986 * filesystem to look them up. If the file was deleted before its 1987 * indirect blocks were all written to disk, the routine that set 1988 * us up (deallocate_dependencies) will have arranged to leave 1989 * a complete copy of the indirect block in memory for our use. 1990 * Otherwise we have to read the blocks in from the disk. 1991 */ 1992 ACQUIRE_LOCK(&lk); 1993 if ((bp = incore(ip->i_devvp, dbn)) != NULL && 1994 (wk = LIST_FIRST(&bp->b_dep)) != NULL) { 1995 if (wk->wk_type != D_INDIRDEP || 1996 (indirdep = WK_INDIRDEP(wk))->ir_savebp != bp || 1997 (indirdep->ir_state & GOINGAWAY) == 0) 1998 panic("indir_trunc: lost indirdep"); 1999 WORKLIST_REMOVE(wk); 2000 WORKITEM_FREE(indirdep, D_INDIRDEP); 2001 if (LIST_FIRST(&bp->b_dep) != NULL) 2002 panic("indir_trunc: dangling dep"); 2003 FREE_LOCK(&lk); 2004 } else { 2005 FREE_LOCK(&lk); 2006 error = bread(ip->i_devvp, dbn, (int)fs->fs_bsize, NOCRED, &bp); 2007 if (error) 2008 return (error); 2009 } 2010 /* 2011 * Recursively free indirect blocks. 2012 */ 2013 bap = (ufs_daddr_t *)bp->b_data; 2014 nblocks = btodb(fs->fs_bsize); 2015 for (i = NINDIR(fs) - 1; i >= 0; i--) { 2016 if ((nb = bap[i]) == 0) 2017 continue; 2018 if (level != 0) { 2019 if ((error = indir_trunc(ip, fsbtodb(fs, nb), 2020 level - 1, lbn + (i * lbnadd), countp)) != 0) 2021 allerror = error; 2022 } 2023 ffs_blkfree(ip, nb, fs->fs_bsize); 2024 *countp += nblocks; 2025 } 2026 bp->b_flags |= B_INVAL | B_NOCACHE; 2027 brelse(bp); 2028 return (allerror); 2029} 2030 2031/* 2032 * Free an allocindir. 2033 * This routine must be called with splbio interrupts blocked. 2034 */ 2035static void 2036free_allocindir(aip, inodedep) 2037 struct allocindir *aip; 2038 struct inodedep *inodedep; 2039{ 2040 struct freefrag *freefrag; 2041 2042#ifdef DEBUG 2043 if (lk.lkt_held == -1) 2044 panic("free_allocindir: lock not held"); 2045#endif 2046 if ((aip->ai_state & DEPCOMPLETE) == 0) 2047 LIST_REMOVE(aip, ai_deps); 2048 if (aip->ai_state & ONWORKLIST) 2049 WORKLIST_REMOVE(&aip->ai_list); 2050 LIST_REMOVE(aip, ai_next); 2051 if ((freefrag = aip->ai_freefrag) != NULL) { 2052 if (inodedep == NULL) 2053 add_to_worklist(&freefrag->ff_list); 2054 else 2055 WORKLIST_INSERT(&inodedep->id_bufwait, 2056 &freefrag->ff_list); 2057 } 2058 WORKITEM_FREE(aip, D_ALLOCINDIR); 2059} 2060 2061/* 2062 * Directory entry addition dependencies. 2063 * 2064 * When adding a new directory entry, the inode (with its incremented link 2065 * count) must be written to disk before the directory entry's pointer to it. 2066 * Also, if the inode is newly allocated, the corresponding freemap must be 2067 * updated (on disk) before the directory entry's pointer. These requirements 2068 * are met via undo/redo on the directory entry's pointer, which consists 2069 * simply of the inode number. 2070 * 2071 * As directory entries are added and deleted, the free space within a 2072 * directory block can become fragmented. The ufs file system will compact 2073 * a fragmented directory block to make space for a new entry. When this 2074 * occurs, the offsets of previously added entries change. Any "diradd" 2075 * dependency structures corresponding to these entries must be updated with 2076 * the new offsets. 2077 */ 2078 2079/* 2080 * This routine is called after the in-memory inode's link 2081 * count has been incremented, but before the directory entry's 2082 * pointer to the inode has been set. 2083 */ 2084void 2085softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp) 2086 struct buf *bp; /* buffer containing directory block */ 2087 struct inode *dp; /* inode for directory */ 2088 off_t diroffset; /* offset of new entry in directory */ 2089 long newinum; /* inode referenced by new directory entry */ 2090 struct buf *newdirbp; /* non-NULL => contents of new mkdir */ 2091{ 2092 int offset; /* offset of new entry within directory block */ 2093 ufs_lbn_t lbn; /* block in directory containing new entry */ 2094 struct fs *fs; 2095 struct diradd *dap; 2096 struct pagedep *pagedep; 2097 struct inodedep *inodedep; 2098 struct mkdir *mkdir1, *mkdir2; 2099 2100 /* 2101 * Whiteouts have no dependencies. 2102 */ 2103 if (newinum == WINO) { 2104 if (newdirbp != NULL) 2105 bdwrite(newdirbp); 2106 return; 2107 } 2108 2109 fs = dp->i_fs; 2110 lbn = lblkno(fs, diroffset); 2111 offset = blkoff(fs, diroffset); 2112 MALLOC(dap, struct diradd *, sizeof(struct diradd), M_DIRADD, M_WAITOK); 2113 bzero(dap, sizeof(struct diradd)); 2114 dap->da_list.wk_type = D_DIRADD; 2115 dap->da_offset = offset; 2116 dap->da_newinum = newinum; 2117 dap->da_state = ATTACHED; 2118 if (newdirbp == NULL) { 2119 dap->da_state |= DEPCOMPLETE; 2120 ACQUIRE_LOCK(&lk); 2121 } else { 2122 dap->da_state |= MKDIR_BODY | MKDIR_PARENT; 2123 MALLOC(mkdir1, struct mkdir *, sizeof(struct mkdir), M_MKDIR, 2124 M_WAITOK); 2125 mkdir1->md_list.wk_type = D_MKDIR; 2126 mkdir1->md_state = MKDIR_BODY; 2127 mkdir1->md_diradd = dap; 2128 MALLOC(mkdir2, struct mkdir *, sizeof(struct mkdir), M_MKDIR, 2129 M_WAITOK); 2130 mkdir2->md_list.wk_type = D_MKDIR; 2131 mkdir2->md_state = MKDIR_PARENT; 2132 mkdir2->md_diradd = dap; 2133 /* 2134 * Dependency on "." and ".." being written to disk. 2135 */ 2136 mkdir1->md_buf = newdirbp; 2137 ACQUIRE_LOCK(&lk); 2138 LIST_INSERT_HEAD(&mkdirlisthd, mkdir1, md_mkdirs); 2139 WORKLIST_INSERT(&newdirbp->b_dep, &mkdir1->md_list); 2140 FREE_LOCK(&lk); 2141 bdwrite(newdirbp); 2142 /* 2143 * Dependency on link count increase for parent directory 2144 */ 2145 ACQUIRE_LOCK(&lk); 2146 if (inodedep_lookup(dp->i_fs, dp->i_number, 0, &inodedep) == 0 2147 || (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 2148 dap->da_state &= ~MKDIR_PARENT; 2149 WORKITEM_FREE(mkdir2, D_MKDIR); 2150 } else { 2151 LIST_INSERT_HEAD(&mkdirlisthd, mkdir2, md_mkdirs); 2152 WORKLIST_INSERT(&inodedep->id_bufwait,&mkdir2->md_list); 2153 } 2154 } 2155 /* 2156 * Link into parent directory pagedep to await its being written. 2157 */ 2158 if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0) 2159 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list); 2160 dap->da_pagedep = pagedep; 2161 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap, 2162 da_pdlist); 2163 /* 2164 * Link into its inodedep. Put it on the id_bufwait list if the inode 2165 * is not yet written. If it is written, do the post-inode write 2166 * processing to put it on the id_pendinghd list. 2167 */ 2168 (void) inodedep_lookup(fs, newinum, DEPALLOC, &inodedep); 2169 if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) 2170 diradd_inode_written(dap, inodedep); 2171 else 2172 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 2173 FREE_LOCK(&lk); 2174} 2175 2176/* 2177 * This procedure is called to change the offset of a directory 2178 * entry when compacting a directory block which must be owned 2179 * exclusively by the caller. Note that the actual entry movement 2180 * must be done in this procedure to ensure that no I/O completions 2181 * occur while the move is in progress. 2182 */ 2183void 2184softdep_change_directoryentry_offset(dp, base, oldloc, newloc, entrysize) 2185 struct inode *dp; /* inode for directory */ 2186 caddr_t base; /* address of dp->i_offset */ 2187 caddr_t oldloc; /* address of old directory location */ 2188 caddr_t newloc; /* address of new directory location */ 2189 int entrysize; /* size of directory entry */ 2190{ 2191 int offset, oldoffset, newoffset; 2192 struct pagedep *pagedep; 2193 struct diradd *dap; 2194 ufs_lbn_t lbn; 2195 2196 ACQUIRE_LOCK(&lk); 2197 lbn = lblkno(dp->i_fs, dp->i_offset); 2198 offset = blkoff(dp->i_fs, dp->i_offset); 2199 if (pagedep_lookup(dp, lbn, 0, &pagedep) == 0) 2200 goto done; 2201 oldoffset = offset + (oldloc - base); 2202 newoffset = offset + (newloc - base); 2203 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[DIRADDHASH(oldoffset)]); 2204 dap; dap = LIST_NEXT(dap, da_pdlist)) { 2205 if (dap->da_offset != oldoffset) 2206 continue; 2207 dap->da_offset = newoffset; 2208 if (DIRADDHASH(newoffset) == DIRADDHASH(oldoffset)) 2209 break; 2210 LIST_REMOVE(dap, da_pdlist); 2211 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(newoffset)], 2212 dap, da_pdlist); 2213 break; 2214 } 2215 if (dap == NULL) { 2216 for (dap = LIST_FIRST(&pagedep->pd_pendinghd); 2217 dap; dap = LIST_NEXT(dap, da_pdlist)) { 2218 if (dap->da_offset == oldoffset) { 2219 dap->da_offset = newoffset; 2220 break; 2221 } 2222 } 2223 } 2224done: 2225 bcopy(oldloc, newloc, entrysize); 2226 FREE_LOCK(&lk); 2227} 2228 2229/* 2230 * Free a diradd dependency structure. This routine must be called 2231 * with splbio interrupts blocked. 2232 */ 2233static void 2234free_diradd(dap) 2235 struct diradd *dap; 2236{ 2237 struct dirrem *dirrem; 2238 struct pagedep *pagedep; 2239 struct inodedep *inodedep; 2240 struct mkdir *mkdir, *nextmd; 2241 2242#ifdef DEBUG 2243 if (lk.lkt_held == -1) 2244 panic("free_diradd: lock not held"); 2245#endif 2246 WORKLIST_REMOVE(&dap->da_list); 2247 LIST_REMOVE(dap, da_pdlist); 2248 if ((dap->da_state & DIRCHG) == 0) { 2249 pagedep = dap->da_pagedep; 2250 } else { 2251 dirrem = dap->da_previous; 2252 pagedep = dirrem->dm_pagedep; 2253 dirrem->dm_dirinum = pagedep->pd_ino; 2254 add_to_worklist(&dirrem->dm_list); 2255 } 2256 if (inodedep_lookup(VFSTOUFS(pagedep->pd_mnt)->um_fs, dap->da_newinum, 2257 0, &inodedep) != 0) 2258 (void) free_inodedep(inodedep); 2259 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 2260 for (mkdir = LIST_FIRST(&mkdirlisthd); mkdir; mkdir = nextmd) { 2261 nextmd = LIST_NEXT(mkdir, md_mkdirs); 2262 if (mkdir->md_diradd != dap) 2263 continue; 2264 dap->da_state &= ~mkdir->md_state; 2265 WORKLIST_REMOVE(&mkdir->md_list); 2266 LIST_REMOVE(mkdir, md_mkdirs); 2267 WORKITEM_FREE(mkdir, D_MKDIR); 2268 } 2269 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 2270 panic("free_diradd: unfound ref"); 2271 } 2272 WORKITEM_FREE(dap, D_DIRADD); 2273} 2274 2275/* 2276 * Directory entry removal dependencies. 2277 * 2278 * When removing a directory entry, the entry's inode pointer must be 2279 * zero'ed on disk before the corresponding inode's link count is decremented 2280 * (possibly freeing the inode for re-use). This dependency is handled by 2281 * updating the directory entry but delaying the inode count reduction until 2282 * after the directory block has been written to disk. After this point, the 2283 * inode count can be decremented whenever it is convenient. 2284 */ 2285 2286/* 2287 * This routine should be called immediately after removing 2288 * a directory entry. The inode's link count should not be 2289 * decremented by the calling procedure -- the soft updates 2290 * code will do this task when it is safe. 2291 */ 2292void 2293softdep_setup_remove(bp, dp, ip, isrmdir) 2294 struct buf *bp; /* buffer containing directory block */ 2295 struct inode *dp; /* inode for the directory being modified */ 2296 struct inode *ip; /* inode for directory entry being removed */ 2297 int isrmdir; /* indicates if doing RMDIR */ 2298{ 2299 struct dirrem *dirrem; 2300 2301 /* 2302 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. 2303 */ 2304 dirrem = newdirrem(bp, dp, ip, isrmdir); 2305 if ((dirrem->dm_state & COMPLETE) == 0) { 2306 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem, 2307 dm_next); 2308 } else { 2309 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino; 2310 add_to_worklist(&dirrem->dm_list); 2311 } 2312 FREE_LOCK(&lk); 2313} 2314 2315/* 2316 * Allocate a new dirrem if appropriate and return it along with 2317 * its associated pagedep. Called without a lock, returns with lock. 2318 */ 2319static struct dirrem * 2320newdirrem(bp, dp, ip, isrmdir) 2321 struct buf *bp; /* buffer containing directory block */ 2322 struct inode *dp; /* inode for the directory being modified */ 2323 struct inode *ip; /* inode for directory entry being removed */ 2324 int isrmdir; /* indicates if doing RMDIR */ 2325{ 2326 int offset; 2327 ufs_lbn_t lbn; 2328 struct diradd *dap; 2329 struct dirrem *dirrem; 2330 struct pagedep *pagedep; 2331 2332 /* 2333 * Whiteouts have no deletion dependencies. 2334 */ 2335 if (ip == NULL) 2336 panic("newdirrem: whiteout"); 2337 MALLOC(dirrem, struct dirrem *, sizeof(struct dirrem), 2338 M_DIRREM, M_WAITOK); 2339 bzero(dirrem, sizeof(struct dirrem)); 2340 dirrem->dm_list.wk_type = D_DIRREM; 2341 dirrem->dm_state = isrmdir ? RMDIR : 0; 2342 dirrem->dm_mnt = ITOV(ip)->v_mount; 2343 dirrem->dm_oldinum = ip->i_number; 2344 2345 ACQUIRE_LOCK(&lk); 2346 lbn = lblkno(dp->i_fs, dp->i_offset); 2347 offset = blkoff(dp->i_fs, dp->i_offset); 2348 if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0) 2349 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list); 2350 dirrem->dm_pagedep = pagedep; 2351 /* 2352 * Check for a diradd dependency for the same directory entry. 2353 * If present, then both dependencies become obsolete and can 2354 * be de-allocated. Check for an entry on both the pd_dirraddhd 2355 * list and the pd_pendinghd list. 2356 */ 2357 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[DIRADDHASH(offset)]); 2358 dap; dap = LIST_NEXT(dap, da_pdlist)) 2359 if (dap->da_offset == offset) 2360 break; 2361 if (dap == NULL) { 2362 for (dap = LIST_FIRST(&pagedep->pd_pendinghd); 2363 dap; dap = LIST_NEXT(dap, da_pdlist)) 2364 if (dap->da_offset == offset) 2365 break; 2366 if (dap == NULL) 2367 return (dirrem); 2368 } 2369 /* 2370 * Must be ATTACHED at this point, so just delete it. 2371 */ 2372 if ((dap->da_state & ATTACHED) == 0) 2373 panic("newdirrem: not ATTACHED"); 2374 if (dap->da_newinum != ip->i_number) 2375 panic("newdirrem: inum %d should be %d", 2376 ip->i_number, dap->da_newinum); 2377 free_diradd(dap); 2378 dirrem->dm_state |= COMPLETE; 2379 return (dirrem); 2380} 2381 2382/* 2383 * Directory entry change dependencies. 2384 * 2385 * Changing an existing directory entry requires that an add operation 2386 * be completed first followed by a deletion. The semantics for the addition 2387 * are identical to the description of adding a new entry above except 2388 * that the rollback is to the old inode number rather than zero. Once 2389 * the addition dependency is completed, the removal is done as described 2390 * in the removal routine above. 2391 */ 2392 2393/* 2394 * This routine should be called immediately after changing 2395 * a directory entry. The inode's link count should not be 2396 * decremented by the calling procedure -- the soft updates 2397 * code will perform this task when it is safe. 2398 */ 2399void 2400softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 2401 struct buf *bp; /* buffer containing directory block */ 2402 struct inode *dp; /* inode for the directory being modified */ 2403 struct inode *ip; /* inode for directory entry being removed */ 2404 long newinum; /* new inode number for changed entry */ 2405 int isrmdir; /* indicates if doing RMDIR */ 2406{ 2407 int offset; 2408 struct diradd *dap = NULL; 2409 struct dirrem *dirrem; 2410 struct pagedep *pagedep; 2411 struct inodedep *inodedep; 2412 2413 offset = blkoff(dp->i_fs, dp->i_offset); 2414 2415 /* 2416 * Whiteouts do not need diradd dependencies. 2417 */ 2418 if (newinum != WINO) { 2419 MALLOC(dap, struct diradd *, sizeof(struct diradd), 2420 M_DIRADD, M_WAITOK); 2421 bzero(dap, sizeof(struct diradd)); 2422 dap->da_list.wk_type = D_DIRADD; 2423 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE; 2424 dap->da_offset = offset; 2425 dap->da_newinum = newinum; 2426 } 2427 2428 /* 2429 * Allocate a new dirrem and ACQUIRE_LOCK. 2430 */ 2431 dirrem = newdirrem(bp, dp, ip, isrmdir); 2432 pagedep = dirrem->dm_pagedep; 2433 /* 2434 * The possible values for isrmdir: 2435 * 0 - non-directory file rename 2436 * 1 - directory rename within same directory 2437 * inum - directory rename to new directory of given inode number 2438 * When renaming to a new directory, we are both deleting and 2439 * creating a new directory entry, so the link count on the new 2440 * directory should not change. Thus we do not need the followup 2441 * dirrem which is usually done in handle_workitem_remove. We set 2442 * the DIRCHG flag to tell handle_workitem_remove to skip the 2443 * followup dirrem. 2444 */ 2445 if (isrmdir > 1) 2446 dirrem->dm_state |= DIRCHG; 2447 2448 /* 2449 * Whiteouts have no additional dependencies, 2450 * so just put the dirrem on the correct list. 2451 */ 2452 if (newinum == WINO) { 2453 if ((dirrem->dm_state & COMPLETE) == 0) { 2454 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem, 2455 dm_next); 2456 } else { 2457 dirrem->dm_dirinum = pagedep->pd_ino; 2458 add_to_worklist(&dirrem->dm_list); 2459 } 2460 FREE_LOCK(&lk); 2461 return; 2462 } 2463 2464 /* 2465 * Link into its inodedep. Put it on the id_bufwait list if the inode 2466 * is not yet written. If it is written, do the post-inode write 2467 * processing to put it on the id_pendinghd list. 2468 */ 2469 dap->da_previous = dirrem; 2470 if (inodedep_lookup(dp->i_fs, newinum, DEPALLOC, &inodedep) == 0 || 2471 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 2472 dap->da_state |= COMPLETE; 2473 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 2474 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 2475 } else { 2476 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 2477 dap, da_pdlist); 2478 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 2479 } 2480 /* 2481 * If the previous inode was never written or its previous directory 2482 * entry was never written, then we do not want to roll back to this 2483 * previous value. Instead we want to roll back to zero and immediately 2484 * free the unwritten or unreferenced inode. 2485 */ 2486 if (dirrem->dm_state & COMPLETE) { 2487 dap->da_state &= ~DIRCHG; 2488 dap->da_pagedep = pagedep; 2489 dirrem->dm_dirinum = pagedep->pd_ino; 2490 add_to_worklist(&dirrem->dm_list); 2491 } 2492 FREE_LOCK(&lk); 2493} 2494 2495/* 2496 * Called whenever the link count on an inode is increased. 2497 * It creates an inode dependency so that the new reference(s) 2498 * to the inode cannot be committed to disk until the updated 2499 * inode has been written. 2500 */ 2501void 2502softdep_increase_linkcnt(ip) 2503 struct inode *ip; /* the inode with the increased link count */ 2504{ 2505 struct inodedep *inodedep; 2506 2507 ACQUIRE_LOCK(&lk); 2508 (void) inodedep_lookup(ip->i_fs, ip->i_number, DEPALLOC, &inodedep); 2509 FREE_LOCK(&lk); 2510} 2511 2512/* 2513 * This workitem decrements the inode's link count. 2514 * If the link count reaches zero, the file is removed. 2515 */ 2516static void 2517handle_workitem_remove(dirrem) 2518 struct dirrem *dirrem; 2519{ 2520 struct proc *p = CURPROC; /* XXX */ 2521 struct inodedep *inodedep; 2522 struct vnode *vp; 2523 struct inode *ip; 2524 int error; 2525 2526 if ((error = VFS_VGET(dirrem->dm_mnt, dirrem->dm_oldinum, &vp)) != 0) { 2527 softdep_error("handle_workitem_remove: vget", error); 2528 return; 2529 } 2530 ip = VTOI(vp); 2531 /* 2532 * Normal file deletion. 2533 */ 2534 if ((dirrem->dm_state & RMDIR) == 0) { 2535 ip->i_nlink--; 2536 if (ip->i_nlink < ip->i_effnlink) 2537 panic("handle_workitem_remove: bad file delta"); 2538 ip->i_flag |= IN_CHANGE; 2539 vput(vp); 2540 WORKITEM_FREE(dirrem, D_DIRREM); 2541 return; 2542 } 2543 /* 2544 * Directory deletion. Decrement reference count for both the 2545 * just deleted parent directory entry and the reference for ".". 2546 * Next truncate the directory to length zero. When the 2547 * truncation completes, arrange to have the reference count on 2548 * the parent decremented to account for the loss of "..". 2549 */ 2550 ip->i_nlink -= 2; 2551 if (ip->i_nlink < ip->i_effnlink) 2552 panic("handle_workitem_remove: bad dir delta"); 2553 ip->i_flag |= IN_CHANGE; 2554 if ((error = UFS_TRUNCATE(vp, (off_t)0, 0, p->p_ucred, p)) != 0) 2555 softdep_error("handle_workitem_remove: truncate", error); 2556 /* 2557 * Rename a directory to a new parent. Since, we are both deleting 2558 * and creating a new directory entry, the link count on the new 2559 * directory should not change. Thus we skip the followup dirrem. 2560 */ 2561 if (dirrem->dm_state & DIRCHG) { 2562 vput(vp); 2563 WORKITEM_FREE(dirrem, D_DIRREM); 2564 return; 2565 } 2566 ACQUIRE_LOCK(&lk); 2567 (void) inodedep_lookup(ip->i_fs, dirrem->dm_oldinum, DEPALLOC, 2568 &inodedep); 2569 dirrem->dm_state = 0; 2570 dirrem->dm_oldinum = dirrem->dm_dirinum; 2571 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list); 2572 FREE_LOCK(&lk); 2573 vput(vp); 2574} 2575 2576/* 2577 * Inode de-allocation dependencies. 2578 * 2579 * When an inode's link count is reduced to zero, it can be de-allocated. We 2580 * found it convenient to postpone de-allocation until after the inode is 2581 * written to disk with its new link count (zero). At this point, all of the 2582 * on-disk inode's block pointers are nullified and, with careful dependency 2583 * list ordering, all dependencies related to the inode will be satisfied and 2584 * the corresponding dependency structures de-allocated. So, if/when the 2585 * inode is reused, there will be no mixing of old dependencies with new 2586 * ones. This artificial dependency is set up by the block de-allocation 2587 * procedure above (softdep_setup_freeblocks) and completed by the 2588 * following procedure. 2589 */ 2590static void 2591handle_workitem_freefile(freefile) 2592 struct freefile *freefile; 2593{ 2594 struct vnode vp; 2595 struct inode tip; 2596 struct inodedep *idp; 2597 int error; 2598 2599#ifdef DEBUG 2600 ACQUIRE_LOCK(&lk); 2601 if (inodedep_lookup(freefile->fx_fs, freefile->fx_oldinum, 0, &idp)) 2602 panic("handle_workitem_freefile: inodedep survived"); 2603 FREE_LOCK(&lk); 2604#endif 2605 tip.i_devvp = freefile->fx_devvp; 2606 tip.i_dev = freefile->fx_devvp->v_rdev; 2607 tip.i_fs = freefile->fx_fs; 2608 vp.v_data = &tip; 2609 if ((error = ffs_freefile(&vp, freefile->fx_oldinum, freefile->fx_mode)) != 0) 2610 softdep_error("handle_workitem_freefile", error); 2611 WORKITEM_FREE(freefile, D_FREEFILE); 2612 num_freefile -= 1; 2613} 2614 2615/* 2616 * Disk writes. 2617 * 2618 * The dependency structures constructed above are most actively used when file 2619 * system blocks are written to disk. No constraints are placed on when a 2620 * block can be written, but unsatisfied update dependencies are made safe by 2621 * modifying (or replacing) the source memory for the duration of the disk 2622 * write. When the disk write completes, the memory block is again brought 2623 * up-to-date. 2624 * 2625 * In-core inode structure reclamation. 2626 * 2627 * Because there are a finite number of "in-core" inode structures, they are 2628 * reused regularly. By transferring all inode-related dependencies to the 2629 * in-memory inode block and indexing them separately (via "inodedep"s), we 2630 * can allow "in-core" inode structures to be reused at any time and avoid 2631 * any increase in contention. 2632 * 2633 * Called just before entering the device driver to initiate a new disk I/O. 2634 * The buffer must be locked, thus, no I/O completion operations can occur 2635 * while we are manipulating its associated dependencies. 2636 */ 2637void 2638softdep_disk_io_initiation(bp) 2639 struct buf *bp; /* structure describing disk write to occur */ 2640{ 2641 struct worklist *wk, *nextwk; 2642 struct indirdep *indirdep; 2643 2644 /* 2645 * We only care about write operations. There should never 2646 * be dependencies for reads. 2647 */ 2648 if (bp->b_flags & B_READ) 2649 panic("softdep_disk_io_initiation: read"); 2650 /* 2651 * Do any necessary pre-I/O processing. 2652 */ 2653 for (wk = LIST_FIRST(&bp->b_dep); wk; wk = nextwk) { 2654 nextwk = LIST_NEXT(wk, wk_list); 2655 switch (wk->wk_type) { 2656 2657 case D_PAGEDEP: 2658 initiate_write_filepage(WK_PAGEDEP(wk), bp); 2659 continue; 2660 2661 case D_INODEDEP: 2662 initiate_write_inodeblock(WK_INODEDEP(wk), bp); 2663 continue; 2664 2665 case D_INDIRDEP: 2666 indirdep = WK_INDIRDEP(wk); 2667 if (indirdep->ir_state & GOINGAWAY) 2668 panic("disk_io_initiation: indirdep gone"); 2669 /* 2670 * If there are no remaining dependencies, this 2671 * will be writing the real pointers, so the 2672 * dependency can be freed. 2673 */ 2674 if (LIST_FIRST(&indirdep->ir_deplisthd) == NULL) { 2675 indirdep->ir_savebp->b_flags |= B_INVAL | B_NOCACHE; 2676 brelse(indirdep->ir_savebp); 2677 /* inline expand WORKLIST_REMOVE(wk); */ 2678 wk->wk_state &= ~ONWORKLIST; 2679 LIST_REMOVE(wk, wk_list); 2680 WORKITEM_FREE(indirdep, D_INDIRDEP); 2681 continue; 2682 } 2683 /* 2684 * Replace up-to-date version with safe version. 2685 */ 2686 ACQUIRE_LOCK(&lk); 2687 indirdep->ir_state &= ~ATTACHED; 2688 indirdep->ir_state |= UNDONE; 2689 MALLOC(indirdep->ir_saveddata, caddr_t, bp->b_bcount, 2690 M_INDIRDEP, M_WAITOK); 2691 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 2692 bcopy(indirdep->ir_savebp->b_data, bp->b_data, 2693 bp->b_bcount); 2694 FREE_LOCK(&lk); 2695 continue; 2696 2697 case D_MKDIR: 2698 case D_BMSAFEMAP: 2699 case D_ALLOCDIRECT: 2700 case D_ALLOCINDIR: 2701 continue; 2702 2703 default: 2704 panic("handle_disk_io_initiation: Unexpected type %s", 2705 TYPENAME(wk->wk_type)); 2706 /* NOTREACHED */ 2707 } 2708 } 2709} 2710 2711/* 2712 * Called from within the procedure above to deal with unsatisfied 2713 * allocation dependencies in a directory. The buffer must be locked, 2714 * thus, no I/O completion operations can occur while we are 2715 * manipulating its associated dependencies. 2716 */ 2717static void 2718initiate_write_filepage(pagedep, bp) 2719 struct pagedep *pagedep; 2720 struct buf *bp; 2721{ 2722 struct diradd *dap; 2723 struct direct *ep; 2724 int i; 2725 2726 if (pagedep->pd_state & IOSTARTED) { 2727 /* 2728 * This can only happen if there is a driver that does not 2729 * understand chaining. Here biodone will reissue the call 2730 * to strategy for the incomplete buffers. 2731 */ 2732 printf("initiate_write_filepage: already started\n"); 2733 return; 2734 } 2735 pagedep->pd_state |= IOSTARTED; 2736 ACQUIRE_LOCK(&lk); 2737 for (i = 0; i < DAHASHSZ; i++) { 2738 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap; 2739 dap = LIST_NEXT(dap, da_pdlist)) { 2740 ep = (struct direct *) 2741 ((char *)bp->b_data + dap->da_offset); 2742 if (ep->d_ino != dap->da_newinum) 2743 panic("%s: dir inum %d != new %d", 2744 "initiate_write_filepage", 2745 ep->d_ino, dap->da_newinum); 2746 if (dap->da_state & DIRCHG) 2747 ep->d_ino = dap->da_previous->dm_oldinum; 2748 else 2749 ep->d_ino = 0; 2750 dap->da_state &= ~ATTACHED; 2751 dap->da_state |= UNDONE; 2752 } 2753 } 2754 FREE_LOCK(&lk); 2755} 2756 2757/* 2758 * Called from within the procedure above to deal with unsatisfied 2759 * allocation dependencies in an inodeblock. The buffer must be 2760 * locked, thus, no I/O completion operations can occur while we 2761 * are manipulating its associated dependencies. 2762 */ 2763static void 2764initiate_write_inodeblock(inodedep, bp) 2765 struct inodedep *inodedep; 2766 struct buf *bp; /* The inode block */ 2767{ 2768 struct allocdirect *adp, *lastadp; 2769 struct dinode *dp; 2770 struct fs *fs; 2771 ufs_lbn_t prevlbn = 0; 2772 int i, deplist; 2773 2774 if (inodedep->id_state & IOSTARTED) 2775 panic("initiate_write_inodeblock: already started"); 2776 inodedep->id_state |= IOSTARTED; 2777 fs = inodedep->id_fs; 2778 dp = (struct dinode *)bp->b_data + 2779 ino_to_fsbo(fs, inodedep->id_ino); 2780 /* 2781 * If the bitmap is not yet written, then the allocated 2782 * inode cannot be written to disk. 2783 */ 2784 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 2785 if (inodedep->id_savedino != NULL) 2786 panic("initiate_write_inodeblock: already doing I/O"); 2787 MALLOC(inodedep->id_savedino, struct dinode *, 2788 sizeof(struct dinode), M_INODEDEP, M_WAITOK); 2789 *inodedep->id_savedino = *dp; 2790 bzero((caddr_t)dp, sizeof(struct dinode)); 2791 return; 2792 } 2793 /* 2794 * If no dependencies, then there is nothing to roll back. 2795 */ 2796 inodedep->id_savedsize = dp->di_size; 2797 if (TAILQ_FIRST(&inodedep->id_inoupdt) == NULL) 2798 return; 2799 /* 2800 * Set the dependencies to busy. 2801 */ 2802 ACQUIRE_LOCK(&lk); 2803 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 2804 adp = TAILQ_NEXT(adp, ad_next)) { 2805#ifdef DIAGNOSTIC 2806 if (deplist != 0 && prevlbn >= adp->ad_lbn) 2807 panic("softdep_write_inodeblock: lbn order"); 2808 prevlbn = adp->ad_lbn; 2809 if (adp->ad_lbn < NDADDR && 2810 dp->di_db[adp->ad_lbn] != adp->ad_newblkno) 2811 panic("%s: direct pointer #%ld mismatch %d != %d", 2812 "softdep_write_inodeblock", adp->ad_lbn, 2813 dp->di_db[adp->ad_lbn], adp->ad_newblkno); 2814 if (adp->ad_lbn >= NDADDR && 2815 dp->di_ib[adp->ad_lbn - NDADDR] != adp->ad_newblkno) 2816 panic("%s: indirect pointer #%ld mismatch %d != %d", 2817 "softdep_write_inodeblock", adp->ad_lbn - NDADDR, 2818 dp->di_ib[adp->ad_lbn - NDADDR], adp->ad_newblkno); 2819 deplist |= 1 << adp->ad_lbn; 2820 if ((adp->ad_state & ATTACHED) == 0) 2821 panic("softdep_write_inodeblock: Unknown state 0x%x", 2822 adp->ad_state); 2823#endif /* DIAGNOSTIC */ 2824 adp->ad_state &= ~ATTACHED; 2825 adp->ad_state |= UNDONE; 2826 } 2827 /* 2828 * The on-disk inode cannot claim to be any larger than the last 2829 * fragment that has been written. Otherwise, the on-disk inode 2830 * might have fragments that were not the last block in the file 2831 * which would corrupt the filesystem. 2832 */ 2833 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 2834 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 2835 if (adp->ad_lbn >= NDADDR) 2836 break; 2837 dp->di_db[adp->ad_lbn] = adp->ad_oldblkno; 2838 /* keep going until hitting a rollback to a frag */ 2839 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 2840 continue; 2841 dp->di_size = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize; 2842 for (i = adp->ad_lbn + 1; i < NDADDR; i++) { 2843#ifdef DIAGNOSTIC 2844 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 2845 panic("softdep_write_inodeblock: lost dep1"); 2846#endif /* DIAGNOSTIC */ 2847 dp->di_db[i] = 0; 2848 } 2849 for (i = 0; i < NIADDR; i++) { 2850#ifdef DIAGNOSTIC 2851 if (dp->di_ib[i] != 0 && 2852 (deplist & ((1 << NDADDR) << i)) == 0) 2853 panic("softdep_write_inodeblock: lost dep2"); 2854#endif /* DIAGNOSTIC */ 2855 dp->di_ib[i] = 0; 2856 } 2857 FREE_LOCK(&lk); 2858 return; 2859 } 2860 /* 2861 * If we have zero'ed out the last allocated block of the file, 2862 * roll back the size to the last currently allocated block. 2863 * We know that this last allocated block is a full-sized as 2864 * we already checked for fragments in the loop above. 2865 */ 2866 if (lastadp != NULL && 2867 dp->di_size <= (lastadp->ad_lbn + 1) * fs->fs_bsize) { 2868 for (i = lastadp->ad_lbn; i >= 0; i--) 2869 if (dp->di_db[i] != 0) 2870 break; 2871 dp->di_size = (i + 1) * fs->fs_bsize; 2872 } 2873 /* 2874 * The only dependencies are for indirect blocks. 2875 * 2876 * The file size for indirect block additions is not guaranteed. 2877 * Such a guarantee would be non-trivial to achieve. The conventional 2878 * synchronous write implementation also does not make this guarantee. 2879 * Fsck should catch and fix discrepancies. Arguably, the file size 2880 * can be over-estimated without destroying integrity when the file 2881 * moves into the indirect blocks (i.e., is large). If we want to 2882 * postpone fsck, we are stuck with this argument. 2883 */ 2884 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 2885 dp->di_ib[adp->ad_lbn - NDADDR] = 0; 2886 FREE_LOCK(&lk); 2887} 2888 2889/* 2890 * This routine is called during the completion interrupt 2891 * service routine for a disk write (from the procedure called 2892 * by the device driver to inform the file system caches of 2893 * a request completion). It should be called early in this 2894 * procedure, before the block is made available to other 2895 * processes or other routines are called. 2896 */ 2897void 2898softdep_disk_write_complete(bp) 2899 struct buf *bp; /* describes the completed disk write */ 2900{ 2901 struct worklist *wk; 2902 struct workhead reattach; 2903 struct newblk *newblk; 2904 struct allocindir *aip; 2905 struct allocdirect *adp; 2906 struct indirdep *indirdep; 2907 struct inodedep *inodedep; 2908 struct bmsafemap *bmsafemap; 2909 2910#ifdef DEBUG 2911 if (lk.lkt_held != -1) 2912 panic("softdep_disk_write_complete: lock is held"); 2913 lk.lkt_held = -2; 2914#endif 2915 LIST_INIT(&reattach); 2916 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) { 2917 WORKLIST_REMOVE(wk); 2918 switch (wk->wk_type) { 2919 2920 case D_PAGEDEP: 2921 if (handle_written_filepage(WK_PAGEDEP(wk), bp)) 2922 WORKLIST_INSERT(&reattach, wk); 2923 continue; 2924 2925 case D_INODEDEP: 2926 if (handle_written_inodeblock(WK_INODEDEP(wk), bp)) 2927 WORKLIST_INSERT(&reattach, wk); 2928 continue; 2929 2930 case D_BMSAFEMAP: 2931 bmsafemap = WK_BMSAFEMAP(wk); 2932 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkhd))) { 2933 newblk->nb_state |= DEPCOMPLETE; 2934 newblk->nb_bmsafemap = NULL; 2935 LIST_REMOVE(newblk, nb_deps); 2936 } 2937 while (adp = LIST_FIRST(&bmsafemap->sm_allocdirecthd)) { 2938 adp->ad_state |= DEPCOMPLETE; 2939 adp->ad_buf = NULL; 2940 LIST_REMOVE(adp, ad_deps); 2941 handle_allocdirect_partdone(adp); 2942 } 2943 while (aip = LIST_FIRST(&bmsafemap->sm_allocindirhd)) { 2944 aip->ai_state |= DEPCOMPLETE; 2945 aip->ai_buf = NULL; 2946 LIST_REMOVE(aip, ai_deps); 2947 handle_allocindir_partdone(aip); 2948 } 2949 while ((inodedep = 2950 LIST_FIRST(&bmsafemap->sm_inodedephd)) != NULL) { 2951 inodedep->id_state |= DEPCOMPLETE; 2952 LIST_REMOVE(inodedep, id_deps); 2953 inodedep->id_buf = NULL; 2954 } 2955 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 2956 continue; 2957 2958 case D_MKDIR: 2959 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 2960 continue; 2961 2962 case D_ALLOCDIRECT: 2963 adp = WK_ALLOCDIRECT(wk); 2964 adp->ad_state |= COMPLETE; 2965 handle_allocdirect_partdone(adp); 2966 continue; 2967 2968 case D_ALLOCINDIR: 2969 aip = WK_ALLOCINDIR(wk); 2970 aip->ai_state |= COMPLETE; 2971 handle_allocindir_partdone(aip); 2972 continue; 2973 2974 case D_INDIRDEP: 2975 indirdep = WK_INDIRDEP(wk); 2976 if (indirdep->ir_state & GOINGAWAY) 2977 panic("disk_write_complete: indirdep gone"); 2978 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount); 2979 FREE(indirdep->ir_saveddata, M_INDIRDEP); 2980 indirdep->ir_saveddata = 0; 2981 indirdep->ir_state &= ~UNDONE; 2982 indirdep->ir_state |= ATTACHED; 2983 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0) { 2984 handle_allocindir_partdone(aip); 2985 if (aip == LIST_FIRST(&indirdep->ir_donehd)) 2986 panic("disk_write_complete: not gone"); 2987 } 2988 WORKLIST_INSERT(&reattach, wk); 2989 if ((bp->b_flags & B_DELWRI) == 0) 2990 stat_indir_blk_ptrs++; 2991 bdirty(bp); 2992 continue; 2993 2994 default: 2995 panic("handle_disk_write_complete: Unknown type %s", 2996 TYPENAME(wk->wk_type)); 2997 /* NOTREACHED */ 2998 } 2999 } 3000 /* 3001 * Reattach any requests that must be redone. 3002 */ 3003 while ((wk = LIST_FIRST(&reattach)) != NULL) { 3004 WORKLIST_REMOVE(wk); 3005 WORKLIST_INSERT(&bp->b_dep, wk); 3006 } 3007#ifdef DEBUG 3008 if (lk.lkt_held != -2) 3009 panic("softdep_disk_write_complete: lock lost"); 3010 lk.lkt_held = -1; 3011#endif 3012} 3013 3014/* 3015 * Called from within softdep_disk_write_complete above. Note that 3016 * this routine is always called from interrupt level with further 3017 * splbio interrupts blocked. 3018 */ 3019static void 3020handle_allocdirect_partdone(adp) 3021 struct allocdirect *adp; /* the completed allocdirect */ 3022{ 3023 struct allocdirect *listadp; 3024 struct inodedep *inodedep; 3025 long bsize; 3026 3027 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 3028 return; 3029 if (adp->ad_buf != NULL) 3030 panic("handle_allocdirect_partdone: dangling dep"); 3031 /* 3032 * The on-disk inode cannot claim to be any larger than the last 3033 * fragment that has been written. Otherwise, the on-disk inode 3034 * might have fragments that were not the last block in the file 3035 * which would corrupt the filesystem. Thus, we cannot free any 3036 * allocdirects after one whose ad_oldblkno claims a fragment as 3037 * these blocks must be rolled back to zero before writing the inode. 3038 * We check the currently active set of allocdirects in id_inoupdt. 3039 */ 3040 inodedep = adp->ad_inodedep; 3041 bsize = inodedep->id_fs->fs_bsize; 3042 for (listadp = TAILQ_FIRST(&inodedep->id_inoupdt); listadp; 3043 listadp = TAILQ_NEXT(listadp, ad_next)) { 3044 /* found our block */ 3045 if (listadp == adp) 3046 break; 3047 /* continue if ad_oldlbn is not a fragment */ 3048 if (listadp->ad_oldsize == 0 || 3049 listadp->ad_oldsize == bsize) 3050 continue; 3051 /* hit a fragment */ 3052 return; 3053 } 3054 /* 3055 * If we have reached the end of the current list without 3056 * finding the just finished dependency, then it must be 3057 * on the future dependency list. Future dependencies cannot 3058 * be freed until they are moved to the current list. 3059 */ 3060 if (listadp == NULL) { 3061#ifdef DEBUG 3062 for (listadp = TAILQ_FIRST(&inodedep->id_newinoupdt); listadp; 3063 listadp = TAILQ_NEXT(listadp, ad_next)) 3064 /* found our block */ 3065 if (listadp == adp) 3066 break; 3067 if (listadp == NULL) 3068 panic("handle_allocdirect_partdone: lost dep"); 3069#endif /* DEBUG */ 3070 return; 3071 } 3072 /* 3073 * If we have found the just finished dependency, then free 3074 * it along with anything that follows it that is complete. 3075 */ 3076 for (; adp; adp = listadp) { 3077 listadp = TAILQ_NEXT(adp, ad_next); 3078 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 3079 return; 3080 free_allocdirect(&inodedep->id_inoupdt, adp, 1); 3081 } 3082} 3083 3084/* 3085 * Called from within softdep_disk_write_complete above. Note that 3086 * this routine is always called from interrupt level with further 3087 * splbio interrupts blocked. 3088 */ 3089static void 3090handle_allocindir_partdone(aip) 3091 struct allocindir *aip; /* the completed allocindir */ 3092{ 3093 struct indirdep *indirdep; 3094 3095 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE) 3096 return; 3097 if (aip->ai_buf != NULL) 3098 panic("handle_allocindir_partdone: dangling dependency"); 3099 indirdep = aip->ai_indirdep; 3100 if (indirdep->ir_state & UNDONE) { 3101 LIST_REMOVE(aip, ai_next); 3102 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next); 3103 return; 3104 } 3105 ((ufs_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 3106 aip->ai_newblkno; 3107 LIST_REMOVE(aip, ai_next); 3108 if (aip->ai_freefrag != NULL) 3109 add_to_worklist(&aip->ai_freefrag->ff_list); 3110 WORKITEM_FREE(aip, D_ALLOCINDIR); 3111} 3112 3113/* 3114 * Called from within softdep_disk_write_complete above to restore 3115 * in-memory inode block contents to their most up-to-date state. Note 3116 * that this routine is always called from interrupt level with further 3117 * splbio interrupts blocked. 3118 */ 3119static int 3120handle_written_inodeblock(inodedep, bp) 3121 struct inodedep *inodedep; 3122 struct buf *bp; /* buffer containing the inode block */ 3123{ 3124 struct worklist *wk, *filefree; 3125 struct allocdirect *adp, *nextadp; 3126 struct dinode *dp; 3127 int hadchanges; 3128 3129 if ((inodedep->id_state & IOSTARTED) == 0) 3130 panic("handle_written_inodeblock: not started"); 3131 inodedep->id_state &= ~IOSTARTED; 3132 inodedep->id_state |= COMPLETE; 3133 dp = (struct dinode *)bp->b_data + 3134 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 3135 /* 3136 * If we had to rollback the inode allocation because of 3137 * bitmaps being incomplete, then simply restore it. 3138 * Keep the block dirty so that it will not be reclaimed until 3139 * all associated dependencies have been cleared and the 3140 * corresponding updates written to disk. 3141 */ 3142 if (inodedep->id_savedino != NULL) { 3143 *dp = *inodedep->id_savedino; 3144 FREE(inodedep->id_savedino, M_INODEDEP); 3145 inodedep->id_savedino = NULL; 3146 if ((bp->b_flags & B_DELWRI) == 0) 3147 stat_inode_bitmap++; 3148 bdirty(bp); 3149 return (1); 3150 } 3151 /* 3152 * Roll forward anything that had to be rolled back before 3153 * the inode could be updated. 3154 */ 3155 hadchanges = 0; 3156 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) { 3157 nextadp = TAILQ_NEXT(adp, ad_next); 3158 if (adp->ad_state & ATTACHED) 3159 panic("handle_written_inodeblock: new entry"); 3160 if (adp->ad_lbn < NDADDR) { 3161 if (dp->di_db[adp->ad_lbn] != adp->ad_oldblkno) 3162 panic("%s: %s #%ld mismatch %d != %d", 3163 "handle_written_inodeblock", 3164 "direct pointer", adp->ad_lbn, 3165 dp->di_db[adp->ad_lbn], adp->ad_oldblkno); 3166 dp->di_db[adp->ad_lbn] = adp->ad_newblkno; 3167 } else { 3168 if (dp->di_ib[adp->ad_lbn - NDADDR] != 0) 3169 panic("%s: %s #%ld allocated as %d", 3170 "handle_written_inodeblock", 3171 "indirect pointer", adp->ad_lbn - NDADDR, 3172 dp->di_ib[adp->ad_lbn - NDADDR]); 3173 dp->di_ib[adp->ad_lbn - NDADDR] = adp->ad_newblkno; 3174 } 3175 adp->ad_state &= ~UNDONE; 3176 adp->ad_state |= ATTACHED; 3177 hadchanges = 1; 3178 } 3179 if (hadchanges && (bp->b_flags & B_DELWRI) == 0) 3180 stat_direct_blk_ptrs++; 3181 /* 3182 * Reset the file size to its most up-to-date value. 3183 */ 3184 if (inodedep->id_savedsize == -1) 3185 panic("handle_written_inodeblock: bad size"); 3186 if (dp->di_size != inodedep->id_savedsize) { 3187 dp->di_size = inodedep->id_savedsize; 3188 hadchanges = 1; 3189 } 3190 inodedep->id_savedsize = -1; 3191 /* 3192 * If there were any rollbacks in the inode block, then it must be 3193 * marked dirty so that its will eventually get written back in 3194 * its correct form. 3195 */ 3196 if (hadchanges) 3197 bdirty(bp); 3198 /* 3199 * Process any allocdirects that completed during the update. 3200 */ 3201 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 3202 handle_allocdirect_partdone(adp); 3203 /* 3204 * Process deallocations that were held pending until the 3205 * inode had been written to disk. Freeing of the inode 3206 * is delayed until after all blocks have been freed to 3207 * avoid creation of new <vfsid, inum, lbn> triples 3208 * before the old ones have been deleted. 3209 */ 3210 filefree = NULL; 3211 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) { 3212 WORKLIST_REMOVE(wk); 3213 switch (wk->wk_type) { 3214 3215 case D_FREEFILE: 3216 /* 3217 * We defer adding filefree to the worklist until 3218 * all other additions have been made to ensure 3219 * that it will be done after all the old blocks 3220 * have been freed. 3221 */ 3222 if (filefree != NULL) 3223 panic("handle_written_inodeblock: filefree"); 3224 filefree = wk; 3225 continue; 3226 3227 case D_MKDIR: 3228 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT); 3229 continue; 3230 3231 case D_DIRADD: 3232 diradd_inode_written(WK_DIRADD(wk), inodedep); 3233 continue; 3234 3235 case D_FREEBLKS: 3236 case D_FREEFRAG: 3237 case D_DIRREM: 3238 add_to_worklist(wk); 3239 continue; 3240 3241 default: 3242 panic("handle_written_inodeblock: Unknown type %s", 3243 TYPENAME(wk->wk_type)); 3244 /* NOTREACHED */ 3245 } 3246 } 3247 if (filefree != NULL) { 3248 if (free_inodedep(inodedep) == 0) 3249 panic("handle_written_inodeblock: live inodedep"); 3250 add_to_worklist(filefree); 3251 return (0); 3252 } 3253 3254 /* 3255 * If no outstanding dependencies, free it. 3256 */ 3257 if (free_inodedep(inodedep) || TAILQ_FIRST(&inodedep->id_inoupdt) == 0) 3258 return (0); 3259 return (hadchanges); 3260} 3261 3262/* 3263 * Process a diradd entry after its dependent inode has been written. 3264 * This routine must be called with splbio interrupts blocked. 3265 */ 3266static void 3267diradd_inode_written(dap, inodedep) 3268 struct diradd *dap; 3269 struct inodedep *inodedep; 3270{ 3271 struct pagedep *pagedep; 3272 3273 dap->da_state |= COMPLETE; 3274 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 3275 if (dap->da_state & DIRCHG) 3276 pagedep = dap->da_previous->dm_pagedep; 3277 else 3278 pagedep = dap->da_pagedep; 3279 LIST_REMOVE(dap, da_pdlist); 3280 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 3281 } 3282 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 3283} 3284 3285/* 3286 * Handle the completion of a mkdir dependency. 3287 */ 3288static void 3289handle_written_mkdir(mkdir, type) 3290 struct mkdir *mkdir; 3291 int type; 3292{ 3293 struct diradd *dap; 3294 struct pagedep *pagedep; 3295 3296 if (mkdir->md_state != type) 3297 panic("handle_written_mkdir: bad type"); 3298 dap = mkdir->md_diradd; 3299 dap->da_state &= ~type; 3300 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) 3301 dap->da_state |= DEPCOMPLETE; 3302 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 3303 if (dap->da_state & DIRCHG) 3304 pagedep = dap->da_previous->dm_pagedep; 3305 else 3306 pagedep = dap->da_pagedep; 3307 LIST_REMOVE(dap, da_pdlist); 3308 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 3309 } 3310 LIST_REMOVE(mkdir, md_mkdirs); 3311 WORKITEM_FREE(mkdir, D_MKDIR); 3312} 3313 3314/* 3315 * Called from within softdep_disk_write_complete above. 3316 * A write operation was just completed. Removed inodes can 3317 * now be freed and associated block pointers may be committed. 3318 * Note that this routine is always called from interrupt level 3319 * with further splbio interrupts blocked. 3320 */ 3321static int 3322handle_written_filepage(pagedep, bp) 3323 struct pagedep *pagedep; 3324 struct buf *bp; /* buffer containing the written page */ 3325{ 3326 struct dirrem *dirrem; 3327 struct diradd *dap, *nextdap; 3328 struct direct *ep; 3329 int i, chgs; 3330 3331 if ((pagedep->pd_state & IOSTARTED) == 0) 3332 panic("handle_written_filepage: not started"); 3333 pagedep->pd_state &= ~IOSTARTED; 3334 /* 3335 * Process any directory removals that have been committed. 3336 */ 3337 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) { 3338 LIST_REMOVE(dirrem, dm_next); 3339 dirrem->dm_dirinum = pagedep->pd_ino; 3340 add_to_worklist(&dirrem->dm_list); 3341 } 3342 /* 3343 * Free any directory additions that have been committed. 3344 */ 3345 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 3346 free_diradd(dap); 3347 /* 3348 * Uncommitted directory entries must be restored. 3349 */ 3350 for (chgs = 0, i = 0; i < DAHASHSZ; i++) { 3351 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap; 3352 dap = nextdap) { 3353 nextdap = LIST_NEXT(dap, da_pdlist); 3354 if (dap->da_state & ATTACHED) 3355 panic("handle_written_filepage: attached"); 3356 ep = (struct direct *) 3357 ((char *)bp->b_data + dap->da_offset); 3358 ep->d_ino = dap->da_newinum; 3359 dap->da_state &= ~UNDONE; 3360 dap->da_state |= ATTACHED; 3361 chgs = 1; 3362 /* 3363 * If the inode referenced by the directory has 3364 * been written out, then the dependency can be 3365 * moved to the pending list. 3366 */ 3367 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 3368 LIST_REMOVE(dap, da_pdlist); 3369 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, 3370 da_pdlist); 3371 } 3372 } 3373 } 3374 /* 3375 * If there were any rollbacks in the directory, then it must be 3376 * marked dirty so that its will eventually get written back in 3377 * its correct form. 3378 */ 3379 if (chgs) { 3380 if ((bp->b_flags & B_DELWRI) == 0) 3381 stat_dir_entry++; 3382 bdirty(bp); 3383 } 3384 /* 3385 * If no dependencies remain, the pagedep will be freed. 3386 * Otherwise it will remain to update the page before it 3387 * is written back to disk. 3388 */ 3389 if (LIST_FIRST(&pagedep->pd_pendinghd) == 0) { 3390 for (i = 0; i < DAHASHSZ; i++) 3391 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) != NULL) 3392 break; 3393 if (i == DAHASHSZ) { 3394 LIST_REMOVE(pagedep, pd_hash); 3395 WORKITEM_FREE(pagedep, D_PAGEDEP); 3396 return (0); 3397 } 3398 } 3399 return (1); 3400} 3401 3402/* 3403 * Writing back in-core inode structures. 3404 * 3405 * The file system only accesses an inode's contents when it occupies an 3406 * "in-core" inode structure. These "in-core" structures are separate from 3407 * the page frames used to cache inode blocks. Only the latter are 3408 * transferred to/from the disk. So, when the updated contents of the 3409 * "in-core" inode structure are copied to the corresponding in-memory inode 3410 * block, the dependencies are also transferred. The following procedure is 3411 * called when copying a dirty "in-core" inode to a cached inode block. 3412 */ 3413 3414/* 3415 * Called when an inode is loaded from disk. If the effective link count 3416 * differed from the actual link count when it was last flushed, then we 3417 * need to ensure that the correct effective link count is put back. 3418 */ 3419void 3420softdep_load_inodeblock(ip) 3421 struct inode *ip; /* the "in_core" copy of the inode */ 3422{ 3423 struct inodedep *inodedep; 3424 3425 /* 3426 * Check for alternate nlink count. 3427 */ 3428 ip->i_effnlink = ip->i_nlink; 3429 ACQUIRE_LOCK(&lk); 3430 if (inodedep_lookup(ip->i_fs, ip->i_number, 0, &inodedep) == 0) { 3431 FREE_LOCK(&lk); 3432 return; 3433 } 3434 if (inodedep->id_nlinkdelta != 0) { 3435 ip->i_effnlink -= inodedep->id_nlinkdelta; 3436 ip->i_flag |= IN_MODIFIED; 3437 inodedep->id_nlinkdelta = 0; 3438 (void) free_inodedep(inodedep); 3439 } 3440 FREE_LOCK(&lk); 3441} 3442 3443/* 3444 * This routine is called just before the "in-core" inode 3445 * information is to be copied to the in-memory inode block. 3446 * Recall that an inode block contains several inodes. If 3447 * the force flag is set, then the dependencies will be 3448 * cleared so that the update can always be made. Note that 3449 * the buffer is locked when this routine is called, so we 3450 * will never be in the middle of writing the inode block 3451 * to disk. 3452 */ 3453void 3454softdep_update_inodeblock(ip, bp, waitfor) 3455 struct inode *ip; /* the "in_core" copy of the inode */ 3456 struct buf *bp; /* the buffer containing the inode block */ 3457 int waitfor; /* nonzero => update must be allowed */ 3458{ 3459 struct inodedep *inodedep; 3460 struct worklist *wk; 3461 int error, gotit; 3462 3463 /* 3464 * If the effective link count is not equal to the actual link 3465 * count, then we must track the difference in an inodedep while 3466 * the inode is (potentially) tossed out of the cache. Otherwise, 3467 * if there is no existing inodedep, then there are no dependencies 3468 * to track. 3469 */ 3470 ACQUIRE_LOCK(&lk); 3471 if (ip->i_effnlink != ip->i_nlink) { 3472 (void) inodedep_lookup(ip->i_fs, ip->i_number, DEPALLOC, 3473 &inodedep); 3474 } else if (inodedep_lookup(ip->i_fs, ip->i_number, 0, &inodedep) == 0) { 3475 FREE_LOCK(&lk); 3476 return; 3477 } 3478 if (ip->i_nlink < ip->i_effnlink) 3479 panic("softdep_update_inodeblock: bad delta"); 3480 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 3481 /* 3482 * Changes have been initiated. Anything depending on these 3483 * changes cannot occur until this inode has been written. 3484 */ 3485 inodedep->id_state &= ~COMPLETE; 3486 if ((inodedep->id_state & ONWORKLIST) == 0) 3487 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list); 3488 /* 3489 * Any new dependencies associated with the incore inode must 3490 * now be moved to the list associated with the buffer holding 3491 * the in-memory copy of the inode. Once merged process any 3492 * allocdirects that are completed by the merger. 3493 */ 3494 merge_inode_lists(inodedep); 3495 if (TAILQ_FIRST(&inodedep->id_inoupdt) != NULL) 3496 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt)); 3497 /* 3498 * Now that the inode has been pushed into the buffer, the 3499 * operations dependent on the inode being written to disk 3500 * can be moved to the id_bufwait so that they will be 3501 * processed when the buffer I/O completes. 3502 */ 3503 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) { 3504 WORKLIST_REMOVE(wk); 3505 WORKLIST_INSERT(&inodedep->id_bufwait, wk); 3506 } 3507 /* 3508 * Newly allocated inodes cannot be written until the bitmap 3509 * that allocates them have been written (indicated by 3510 * DEPCOMPLETE being set in id_state). If we are doing a 3511 * forced sync (e.g., an fsync on a file), we force the bitmap 3512 * to be written so that the update can be done. 3513 */ 3514 if ((inodedep->id_state & DEPCOMPLETE) != 0 || waitfor == 0) { 3515 FREE_LOCK(&lk); 3516 return; 3517 } 3518 gotit = getdirtybuf(&inodedep->id_buf, MNT_WAIT); 3519 FREE_LOCK(&lk); 3520 if (gotit && (error = VOP_BWRITE(inodedep->id_buf)) != 0) 3521 softdep_error("softdep_update_inodeblock: bwrite", error); 3522 if ((inodedep->id_state & DEPCOMPLETE) == 0) 3523 panic("softdep_update_inodeblock: update failed"); 3524} 3525 3526/* 3527 * Merge the new inode dependency list (id_newinoupdt) into the old 3528 * inode dependency list (id_inoupdt). This routine must be called 3529 * with splbio interrupts blocked. 3530 */ 3531static void 3532merge_inode_lists(inodedep) 3533 struct inodedep *inodedep; 3534{ 3535 struct allocdirect *listadp, *newadp; 3536 3537 newadp = TAILQ_FIRST(&inodedep->id_newinoupdt); 3538 for (listadp = TAILQ_FIRST(&inodedep->id_inoupdt); listadp && newadp;) { 3539 if (listadp->ad_lbn < newadp->ad_lbn) { 3540 listadp = TAILQ_NEXT(listadp, ad_next); 3541 continue; 3542 } 3543 TAILQ_REMOVE(&inodedep->id_newinoupdt, newadp, ad_next); 3544 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next); 3545 if (listadp->ad_lbn == newadp->ad_lbn) { 3546 allocdirect_merge(&inodedep->id_inoupdt, newadp, 3547 listadp); 3548 listadp = newadp; 3549 } 3550 newadp = TAILQ_FIRST(&inodedep->id_newinoupdt); 3551 } 3552 while ((newadp = TAILQ_FIRST(&inodedep->id_newinoupdt)) != NULL) { 3553 TAILQ_REMOVE(&inodedep->id_newinoupdt, newadp, ad_next); 3554 TAILQ_INSERT_TAIL(&inodedep->id_inoupdt, newadp, ad_next); 3555 } 3556} 3557 3558/* 3559 * If we are doing an fsync, then we must ensure that any directory 3560 * entries for the inode have been written after the inode gets to disk. 3561 */ 3562int 3563softdep_fsync(vp) 3564 struct vnode *vp; /* the "in_core" copy of the inode */ 3565{ 3566 struct diradd *dap, *olddap; 3567 struct inodedep *inodedep; 3568 struct pagedep *pagedep; 3569 struct worklist *wk; 3570 struct mount *mnt; 3571 struct vnode *pvp; 3572 struct inode *ip; 3573 struct buf *bp; 3574 struct fs *fs; 3575 struct proc *p = CURPROC; /* XXX */ 3576 int error, ret, flushparent; 3577 ino_t parentino; 3578 ufs_lbn_t lbn; 3579 3580 ip = VTOI(vp); 3581 fs = ip->i_fs; 3582 for (error = 0, flushparent = 0, olddap = NULL; ; ) { 3583 ACQUIRE_LOCK(&lk); 3584 if (inodedep_lookup(fs, ip->i_number, 0, &inodedep) == 0) 3585 break; 3586 if (LIST_FIRST(&inodedep->id_inowait) != NULL || 3587 LIST_FIRST(&inodedep->id_bufwait) != NULL || 3588 TAILQ_FIRST(&inodedep->id_inoupdt) != NULL || 3589 TAILQ_FIRST(&inodedep->id_newinoupdt) != NULL) 3590 panic("softdep_fsync: pending ops"); 3591 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL) 3592 break; 3593 if (wk->wk_type != D_DIRADD) 3594 panic("softdep_fsync: Unexpected type %s", 3595 TYPENAME(wk->wk_type)); 3596 dap = WK_DIRADD(wk); 3597 /* 3598 * If we have failed to get rid of all the dependencies 3599 * then something is seriously wrong. 3600 */ 3601 if (dap == olddap) 3602 panic("softdep_fsync: flush failed"); 3603 olddap = dap; 3604 /* 3605 * Flush our parent if this directory entry 3606 * has a MKDIR_PARENT dependency. 3607 */ 3608 if (dap->da_state & DIRCHG) 3609 pagedep = dap->da_previous->dm_pagedep; 3610 else 3611 pagedep = dap->da_pagedep; 3612 mnt = pagedep->pd_mnt; 3613 parentino = pagedep->pd_ino; 3614 lbn = pagedep->pd_lbn; 3615 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) 3616 panic("softdep_fsync: dirty"); 3617 flushparent = dap->da_state & MKDIR_PARENT; 3618 /* 3619 * If we are being fsync'ed as part of vgone'ing this vnode, 3620 * then we will not be able to release and recover the 3621 * vnode below, so we just have to give up on writing its 3622 * directory entry out. It will eventually be written, just 3623 * not now, but then the user was not asking to have it 3624 * written, so we are not breaking any promises. 3625 */ 3626 if (vp->v_flag & VXLOCK) 3627 break; 3628 /* 3629 * We prevent deadlock by always fetching inodes from the 3630 * root, moving down the directory tree. Thus, when fetching 3631 * our parent directory, we must unlock ourselves before 3632 * requesting the lock on our parent. See the comment in 3633 * ufs_lookup for details on possible races. 3634 */ 3635 FREE_LOCK(&lk); 3636 VOP_UNLOCK(vp, 0, p); 3637 if ((error = VFS_VGET(mnt, parentino, &pvp)) != 0) { 3638 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 3639 return (error); 3640 } 3641 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 3642 if (flushparent) { 3643 if ((error = UFS_UPDATE(pvp, 1)) != 0) { 3644 vput(pvp); 3645 return (error); 3646 } 3647 } 3648 /* 3649 * Flush directory page containing the inode's name. 3650 */ 3651 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), p->p_ucred, 3652 &bp); 3653 ret = VOP_BWRITE(bp); 3654 vput(pvp); 3655 if (error != 0) 3656 return (error); 3657 if (ret != 0) 3658 return (ret); 3659 } 3660 FREE_LOCK(&lk); 3661 return (0); 3662} 3663 3664/* 3665 * Flush all the dirty bitmaps associated with the block device 3666 * before flushing the rest of the dirty blocks so as to reduce 3667 * the number of dependencies that will have to be rolled back. 3668 */ 3669void 3670softdep_fsync_mountdev(vp) 3671 struct vnode *vp; 3672{ 3673 struct buf *bp, *nbp; 3674 struct worklist *wk; 3675 3676 if (vp->v_type != VBLK) 3677 panic("softdep_fsync_mountdev: vnode not VBLK"); 3678 ACQUIRE_LOCK(&lk); 3679 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 3680 nbp = TAILQ_NEXT(bp, b_vnbufs); 3681 /* 3682 * If it is already scheduled, skip to the next buffer. 3683 */ 3684 if (bp->b_flags & B_BUSY) 3685 continue; 3686 if ((bp->b_flags & B_DELWRI) == 0) 3687 panic("softdep_fsync_mountdev: not dirty"); 3688 /* 3689 * We are only interested in bitmaps with outstanding 3690 * dependencies. 3691 */ 3692 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL || 3693 wk->wk_type != D_BMSAFEMAP) 3694 continue; 3695 bremfree(bp); 3696 bp->b_flags |= B_BUSY; 3697 FREE_LOCK(&lk); 3698 (void) bawrite(bp); 3699 ACQUIRE_LOCK(&lk); 3700 /* 3701 * Since we may have slept during the I/O, we need 3702 * to start from a known point. 3703 */ 3704 nbp = TAILQ_FIRST(&vp->v_dirtyblkhd); 3705 } 3706 drain_output(vp, 1); 3707 FREE_LOCK(&lk); 3708} 3709 3710/* 3711 * This routine is called when we are trying to synchronously flush a 3712 * file. This routine must eliminate any filesystem metadata dependencies 3713 * so that the syncing routine can succeed by pushing the dirty blocks 3714 * associated with the file. If any I/O errors occur, they are returned. 3715 */ 3716int 3717softdep_sync_metadata(ap) 3718 struct vop_fsync_args /* { 3719 struct vnode *a_vp; 3720 struct ucred *a_cred; 3721 int a_waitfor; 3722 struct proc *a_p; 3723 } */ *ap; 3724{ 3725 struct vnode *vp = ap->a_vp; 3726 struct pagedep *pagedep; 3727 struct allocdirect *adp; 3728 struct allocindir *aip; 3729 struct buf *bp, *nbp; 3730 struct worklist *wk; 3731 int i, error, waitfor; 3732 3733 /* 3734 * Check whether this vnode is involved in a filesystem 3735 * that is doing soft dependency processing. 3736 */ 3737 if (vp->v_type != VBLK) { 3738 if (!DOINGSOFTDEP(vp)) 3739 return (0); 3740 } else 3741 if (vp->v_specmountpoint == NULL || 3742 (vp->v_specmountpoint->mnt_flag & MNT_SOFTDEP) == 0) 3743 return (0); 3744 /* 3745 * Ensure that any direct block dependencies have been cleared. 3746 */ 3747 ACQUIRE_LOCK(&lk); 3748 if ((error = flush_inodedep_deps(VTOI(vp)->i_fs, VTOI(vp)->i_number))) { 3749 FREE_LOCK(&lk); 3750 return (error); 3751 } 3752 /* 3753 * For most files, the only metadata dependencies are the 3754 * cylinder group maps that allocate their inode or blocks. 3755 * The block allocation dependencies can be found by traversing 3756 * the dependency lists for any buffers that remain on their 3757 * dirty buffer list. The inode allocation dependency will 3758 * be resolved when the inode is updated with MNT_WAIT. 3759 * This work is done in two passes. The first pass grabs most 3760 * of the buffers and begins asynchronously writing them. The 3761 * only way to wait for these asynchronous writes is to sleep 3762 * on the filesystem vnode which may stay busy for a long time 3763 * if the filesystem is active. So, instead, we make a second 3764 * pass over the dependencies blocking on each write. In the 3765 * usual case we will be blocking against a write that we 3766 * initiated, so when it is done the dependency will have been 3767 * resolved. Thus the second pass is expected to end quickly. 3768 */ 3769 waitfor = MNT_NOWAIT; 3770top: 3771 if (getdirtybuf(&TAILQ_FIRST(&vp->v_dirtyblkhd), MNT_WAIT) == 0) { 3772 drain_output(vp, 1); 3773 FREE_LOCK(&lk); 3774 return (0); 3775 } 3776 bp = TAILQ_FIRST(&vp->v_dirtyblkhd); 3777loop: 3778 /* 3779 * As we hold the buffer locked, none of its dependencies 3780 * will disappear. 3781 */ 3782 for (wk = LIST_FIRST(&bp->b_dep); wk; 3783 wk = LIST_NEXT(wk, wk_list)) { 3784 switch (wk->wk_type) { 3785 3786 case D_ALLOCDIRECT: 3787 adp = WK_ALLOCDIRECT(wk); 3788 if (adp->ad_state & DEPCOMPLETE) 3789 break; 3790 nbp = adp->ad_buf; 3791 if (getdirtybuf(&nbp, waitfor) == 0) 3792 break; 3793 FREE_LOCK(&lk); 3794 if (waitfor == MNT_NOWAIT) { 3795 bawrite(nbp); 3796 } else if ((error = VOP_BWRITE(nbp)) != 0) { 3797 bawrite(bp); 3798 return (error); 3799 } 3800 ACQUIRE_LOCK(&lk); 3801 break; 3802 3803 case D_ALLOCINDIR: 3804 aip = WK_ALLOCINDIR(wk); 3805 if (aip->ai_state & DEPCOMPLETE) 3806 break; 3807 nbp = aip->ai_buf; 3808 if (getdirtybuf(&nbp, waitfor) == 0) 3809 break; 3810 FREE_LOCK(&lk); 3811 if (waitfor == MNT_NOWAIT) { 3812 bawrite(nbp); 3813 } else if ((error = VOP_BWRITE(nbp)) != 0) { 3814 bawrite(bp); 3815 return (error); 3816 } 3817 ACQUIRE_LOCK(&lk); 3818 break; 3819 3820 case D_INDIRDEP: 3821 restart: 3822 for (aip = LIST_FIRST(&WK_INDIRDEP(wk)->ir_deplisthd); 3823 aip; aip = LIST_NEXT(aip, ai_next)) { 3824 if (aip->ai_state & DEPCOMPLETE) 3825 continue; 3826 nbp = aip->ai_buf; 3827 if (getdirtybuf(&nbp, MNT_WAIT) == 0) 3828 goto restart; 3829 FREE_LOCK(&lk); 3830 if ((error = VOP_BWRITE(nbp)) != 0) { 3831 bawrite(bp); 3832 return (error); 3833 } 3834 ACQUIRE_LOCK(&lk); 3835 goto restart; 3836 } 3837 break; 3838 3839 case D_INODEDEP: 3840 if ((error = flush_inodedep_deps(WK_INODEDEP(wk)->id_fs, 3841 WK_INODEDEP(wk)->id_ino)) != 0) { 3842 FREE_LOCK(&lk); 3843 bawrite(bp); 3844 return (error); 3845 } 3846 break; 3847 3848 case D_PAGEDEP: 3849 /* 3850 * We are trying to sync a directory that may 3851 * have dependencies on both its own metadata 3852 * and/or dependencies on the inodes of any 3853 * recently allocated files. We walk its diradd 3854 * lists pushing out the associated inode. 3855 */ 3856 pagedep = WK_PAGEDEP(wk); 3857 for (i = 0; i < DAHASHSZ; i++) { 3858 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0) 3859 continue; 3860 if (error = flush_pagedep_deps(vp, 3861 pagedep->pd_mnt, &pagedep->pd_diraddhd[i])) { 3862 FREE_LOCK(&lk); 3863 bawrite(bp); 3864 return (error); 3865 } 3866 } 3867 break; 3868 3869 case D_MKDIR: 3870 /* 3871 * This case should never happen if the vnode has 3872 * been properly sync'ed. However, if this function 3873 * is used at a place where the vnode has not yet 3874 * been sync'ed, this dependency can show up. So, 3875 * rather than panic, just flush it. 3876 */ 3877 nbp = WK_MKDIR(wk)->md_buf; 3878 if (getdirtybuf(&nbp, waitfor) == 0) 3879 break; 3880 FREE_LOCK(&lk); 3881 if (waitfor == MNT_NOWAIT) { 3882 bawrite(nbp); 3883 } else if ((error = VOP_BWRITE(nbp)) != 0) { 3884 bawrite(bp); 3885 return (error); 3886 } 3887 ACQUIRE_LOCK(&lk); 3888 break; 3889 3890 case D_BMSAFEMAP: 3891 /* 3892 * This case should never happen if the vnode has 3893 * been properly sync'ed. However, if this function 3894 * is used at a place where the vnode has not yet 3895 * been sync'ed, this dependency can show up. So, 3896 * rather than panic, just flush it. 3897 */ 3898 nbp = WK_BMSAFEMAP(wk)->sm_buf; 3899 if (getdirtybuf(&nbp, waitfor) == 0) 3900 break; 3901 FREE_LOCK(&lk); 3902 if (waitfor == MNT_NOWAIT) { 3903 bawrite(nbp); 3904 } else if ((error = VOP_BWRITE(nbp)) != 0) { 3905 bawrite(bp); 3906 return (error); 3907 } 3908 ACQUIRE_LOCK(&lk); 3909 break; 3910 3911 default: 3912 panic("softdep_sync_metadata: Unknown type %s", 3913 TYPENAME(wk->wk_type)); 3914 /* NOTREACHED */ 3915 } 3916 } 3917 (void) getdirtybuf(&TAILQ_NEXT(bp, b_vnbufs), MNT_WAIT); 3918 nbp = TAILQ_NEXT(bp, b_vnbufs); 3919 FREE_LOCK(&lk); 3920 bawrite(bp); 3921 ACQUIRE_LOCK(&lk); 3922 if (nbp != NULL) { 3923 bp = nbp; 3924 goto loop; 3925 } 3926 /* 3927 * We must wait for any I/O in progress to finish so that 3928 * all potential buffers on the dirty list will be visible. 3929 * Once they are all there, proceed with the second pass 3930 * which will wait for the I/O as per above. 3931 */ 3932 drain_output(vp, 1); 3933 /* 3934 * The brief unlock is to allow any pent up dependency 3935 * processing to be done. 3936 */ 3937 if (waitfor == MNT_NOWAIT) { 3938 waitfor = MNT_WAIT; 3939 FREE_LOCK(&lk); 3940 ACQUIRE_LOCK(&lk); 3941 goto top; 3942 } 3943 3944 /* 3945 * If we have managed to get rid of all the dirty buffers, 3946 * then we are done. For certain directories and block 3947 * devices, we may need to do further work. 3948 */ 3949 if (TAILQ_FIRST(&vp->v_dirtyblkhd) == NULL) { 3950 FREE_LOCK(&lk); 3951 return (0); 3952 } 3953 3954 FREE_LOCK(&lk); 3955 /* 3956 * If we are trying to sync a block device, some of its buffers may 3957 * contain metadata that cannot be written until the contents of some 3958 * partially written files have been written to disk. The only easy 3959 * way to accomplish this is to sync the entire filesystem (luckily 3960 * this happens rarely). 3961 */ 3962 if (vp->v_type == VBLK && vp->v_specmountpoint && !VOP_ISLOCKED(vp) && 3963 (error = VFS_SYNC(vp->v_specmountpoint, MNT_WAIT, ap->a_cred, 3964 ap->a_p)) != 0) 3965 return (error); 3966 return (0); 3967} 3968 3969/* 3970 * Flush the dependencies associated with an inodedep. 3971 * Called with splbio blocked. 3972 */ 3973static int 3974flush_inodedep_deps(fs, ino) 3975 struct fs *fs; 3976 ino_t ino; 3977{ 3978 struct inodedep *inodedep; 3979 struct allocdirect *adp; 3980 int error, waitfor; 3981 struct buf *bp; 3982 3983 /* 3984 * This work is done in two passes. The first pass grabs most 3985 * of the buffers and begins asynchronously writing them. The 3986 * only way to wait for these asynchronous writes is to sleep 3987 * on the filesystem vnode which may stay busy for a long time 3988 * if the filesystem is active. So, instead, we make a second 3989 * pass over the dependencies blocking on each write. In the 3990 * usual case we will be blocking against a write that we 3991 * initiated, so when it is done the dependency will have been 3992 * resolved. Thus the second pass is expected to end quickly. 3993 * We give a brief window at the top of the loop to allow 3994 * any pending I/O to complete. 3995 */ 3996 for (waitfor = MNT_NOWAIT; ; ) { 3997 FREE_LOCK(&lk); 3998 ACQUIRE_LOCK(&lk); 3999 if (inodedep_lookup(fs, ino, 0, &inodedep) == 0) 4000 return (0); 4001 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 4002 adp = TAILQ_NEXT(adp, ad_next)) { 4003 if (adp->ad_state & DEPCOMPLETE) 4004 continue; 4005 bp = adp->ad_buf; 4006 if (getdirtybuf(&bp, waitfor) == 0) { 4007 if (waitfor == MNT_NOWAIT) 4008 continue; 4009 break; 4010 } 4011 FREE_LOCK(&lk); 4012 if (waitfor == MNT_NOWAIT) { 4013 bawrite(bp); 4014 } else if ((error = VOP_BWRITE(bp)) != 0) { 4015 ACQUIRE_LOCK(&lk); 4016 return (error); 4017 } 4018 ACQUIRE_LOCK(&lk); 4019 break; 4020 } 4021 if (adp != NULL) 4022 continue; 4023 for (adp = TAILQ_FIRST(&inodedep->id_newinoupdt); adp; 4024 adp = TAILQ_NEXT(adp, ad_next)) { 4025 if (adp->ad_state & DEPCOMPLETE) 4026 continue; 4027 bp = adp->ad_buf; 4028 if (getdirtybuf(&bp, waitfor) == 0) { 4029 if (waitfor == MNT_NOWAIT) 4030 continue; 4031 break; 4032 } 4033 FREE_LOCK(&lk); 4034 if (waitfor == MNT_NOWAIT) { 4035 bawrite(bp); 4036 } else if ((error = VOP_BWRITE(bp)) != 0) { 4037 ACQUIRE_LOCK(&lk); 4038 return (error); 4039 } 4040 ACQUIRE_LOCK(&lk); 4041 break; 4042 } 4043 if (adp != NULL) 4044 continue; 4045 /* 4046 * If pass2, we are done, otherwise do pass 2. 4047 */ 4048 if (waitfor == MNT_WAIT) 4049 break; 4050 waitfor = MNT_WAIT; 4051 } 4052 /* 4053 * Try freeing inodedep in case all dependencies have been removed. 4054 */ 4055 if (inodedep_lookup(fs, ino, 0, &inodedep) != 0) 4056 (void) free_inodedep(inodedep); 4057 return (0); 4058} 4059 4060/* 4061 * Eliminate a pagedep dependency by flushing out all its diradd dependencies. 4062 * Called with splbio blocked. 4063 */ 4064static int 4065flush_pagedep_deps(pvp, mp, diraddhdp) 4066 struct vnode *pvp; 4067 struct mount *mp; 4068 struct diraddhd *diraddhdp; 4069{ 4070 struct proc *p = CURPROC; /* XXX */ 4071 struct inodedep *inodedep; 4072 struct ufsmount *ump; 4073 struct diradd *dap; 4074 struct vnode *vp; 4075 int gotit, error = 0; 4076 struct buf *bp; 4077 ino_t inum; 4078 4079 ump = VFSTOUFS(mp); 4080 while ((dap = LIST_FIRST(diraddhdp)) != NULL) { 4081 /* 4082 * Flush ourselves if this directory entry 4083 * has a MKDIR_PARENT dependency. 4084 */ 4085 if (dap->da_state & MKDIR_PARENT) { 4086 FREE_LOCK(&lk); 4087 if ((error = UFS_UPDATE(pvp, 1)) != 0) 4088 break; 4089 ACQUIRE_LOCK(&lk); 4090 /* 4091 * If that cleared dependencies, go on to next. 4092 */ 4093 if (dap != LIST_FIRST(diraddhdp)) 4094 continue; 4095 if (dap->da_state & MKDIR_PARENT) 4096 panic("flush_pagedep_deps: MKDIR"); 4097 } 4098 /* 4099 * Flush the file on which the directory entry depends. 4100 * If the inode has already been pushed out of the cache, 4101 * then all the block dependencies will have been flushed 4102 * leaving only inode dependencies (e.g., bitmaps). Thus, 4103 * we do a ufs_ihashget to check for the vnode in the cache. 4104 * If it is there, we do a full flush. If it is no longer 4105 * there we need only dispose of any remaining bitmap 4106 * dependencies and write the inode to disk. 4107 */ 4108 inum = dap->da_newinum; 4109 FREE_LOCK(&lk); 4110 if ((vp = ufs_ihashget(ump->um_dev, inum)) == NULL) { 4111 ACQUIRE_LOCK(&lk); 4112 if (inodedep_lookup(ump->um_fs, inum, 0, &inodedep) == 0 4113 && dap == LIST_FIRST(diraddhdp)) 4114 panic("flush_pagedep_deps: flush 1 failed"); 4115 /* 4116 * If the inode still has bitmap dependencies, 4117 * push them to disk. 4118 */ 4119 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 4120 gotit = getdirtybuf(&inodedep->id_buf,MNT_WAIT); 4121 FREE_LOCK(&lk); 4122 if (gotit && 4123 (error = VOP_BWRITE(inodedep->id_buf)) != 0) 4124 break; 4125 ACQUIRE_LOCK(&lk); 4126 } 4127 if (dap != LIST_FIRST(diraddhdp)) 4128 continue; 4129 /* 4130 * If the inode is still sitting in a buffer waiting 4131 * to be written, push it to disk. 4132 */ 4133 FREE_LOCK(&lk); 4134 if ((error = bread(ump->um_devvp, 4135 fsbtodb(ump->um_fs, ino_to_fsba(ump->um_fs, inum)), 4136 (int)ump->um_fs->fs_bsize, NOCRED, &bp)) != 0) 4137 break; 4138 if ((error = VOP_BWRITE(bp)) != 0) 4139 break; 4140 ACQUIRE_LOCK(&lk); 4141 if (dap == LIST_FIRST(diraddhdp)) 4142 panic("flush_pagedep_deps: flush 2 failed"); 4143 continue; 4144 } 4145 if (vp->v_type == VDIR) { 4146 /* 4147 * A newly allocated directory must have its "." and 4148 * ".." entries written out before its name can be 4149 * committed in its parent. We do not want or need 4150 * the full semantics of a synchronous VOP_FSYNC as 4151 * that may end up here again, once for each directory 4152 * level in the filesystem. Instead, we push the blocks 4153 * and wait for them to clear. 4154 */ 4155 if (error = VOP_FSYNC(vp, p->p_ucred, MNT_NOWAIT, p)) { 4156 vput(vp); 4157 break; 4158 } 4159 drain_output(vp, 0); 4160 } 4161 error = UFS_UPDATE(vp, 1); 4162 vput(vp); 4163 if (error) 4164 break; 4165 /* 4166 * If we have failed to get rid of all the dependencies 4167 * then something is seriously wrong. 4168 */ 4169 if (dap == LIST_FIRST(diraddhdp)) 4170 panic("flush_pagedep_deps: flush 3 failed"); 4171 ACQUIRE_LOCK(&lk); 4172 } 4173 if (error) 4174 ACQUIRE_LOCK(&lk); 4175 return (error); 4176} 4177 4178/* 4179 * A large burst of file addition or deletion activity can drive the 4180 * memory load excessively high. Therefore we deliberately slow things 4181 * down and speed up the I/O processing if we find ourselves with too 4182 * many dependencies in progress. 4183 */ 4184static int 4185checklimit(resource, islocked) 4186 long *resource; 4187 int islocked; 4188{ 4189 struct callout_handle handle; 4190 struct proc *p = CURPROC; 4191 int s; 4192 4193 /* 4194 * If we are under our limit, just proceed. 4195 */ 4196 if (*resource < max_softdeps) 4197 return (0); 4198 /* 4199 * We never hold up the filesystem syncer process. 4200 */ 4201 if (p == filesys_syncer) 4202 return (0); 4203 /* 4204 * Our first approach is to speed up the syncer process. 4205 * We never push it to speed up more than half of its 4206 * normal turn time, otherwise it could take over the cpu. 4207 */ 4208 s = splhigh(); 4209 if (filesys_syncer->p_wchan == &lbolt) 4210 setrunnable(filesys_syncer); 4211 splx(s); 4212 if (rushjob < syncdelay / 2) { 4213 rushjob += 1; 4214 stat_rush_requests += 1; 4215 return (0); 4216 } 4217 /* 4218 * If we are resource constrained on inode dependencies, try 4219 * flushing some dirty inodes. Otherwise, we are constrained 4220 * by file deletions, so try accelerating flushes of directories 4221 * with removal dependencies. We would like to do the cleanup 4222 * here, but we probably hold an inode locked at this point and 4223 * that might deadlock against one that we try to clean. So, 4224 * the best that we can do is request the syncer daemon (kick 4225 * started above) to do the cleanup for us. 4226 */ 4227 if (resource == &num_inodedep) { 4228 stat_ino_limit_push += 1; 4229 req_clear_inodedeps = 1; 4230 } else { 4231 stat_blk_limit_push += 1; 4232 req_clear_remove = 1; 4233 } 4234 /* 4235 * Hopefully the syncer daemon will catch up and awaken us. 4236 * We wait at most tickdelay before proceeding in any case. 4237 */ 4238 if (islocked == 0) 4239 ACQUIRE_LOCK(&lk); 4240 if (proc_waiting == 0) { 4241 proc_waiting = 1; 4242 handle = timeout(pause_timer, NULL, 4243 tickdelay > 2 ? tickdelay : 2); 4244 } 4245 FREE_LOCK_INTERLOCKED(&lk); 4246 (void) tsleep((caddr_t)&proc_waiting, PPAUSE | PCATCH, "softupdate", 0); 4247 ACQUIRE_LOCK_INTERLOCKED(&lk); 4248 if (proc_waiting) { 4249 untimeout(pause_timer, NULL, handle); 4250 proc_waiting = 0; 4251 } else { 4252 if (resource == &num_inodedep) 4253 stat_ino_limit_hit += 1; 4254 else 4255 stat_blk_limit_hit += 1; 4256 } 4257 if (islocked == 0) 4258 FREE_LOCK(&lk); 4259 return (1); 4260} 4261 4262/* 4263 * Awaken processes pausing in checklimit and clear proc_waiting 4264 * to indicate that there is no longer a timer running. 4265 */ 4266void 4267pause_timer(arg) 4268 void *arg; 4269{ 4270 4271 proc_waiting = 0; 4272 wakeup(&proc_waiting); 4273} 4274 4275/* 4276 * Flush out a directory with at least one removal dependency in an effort 4277 * to reduce the number of freefile and freeblks dependency structures. 4278 */ 4279static void 4280clear_remove(p) 4281 struct proc *p; 4282{ 4283 struct pagedep_hashhead *pagedephd; 4284 struct pagedep *pagedep; 4285 static int next = 0; 4286 struct mount *mp; 4287 struct vnode *vp; 4288 int error, cnt; 4289 ino_t ino; 4290 4291 ACQUIRE_LOCK(&lk); 4292 for (cnt = 0; cnt < pagedep_hash; cnt++) { 4293 pagedephd = &pagedep_hashtbl[next++]; 4294 if (next >= pagedep_hash) 4295 next = 0; 4296 for (pagedep = LIST_FIRST(pagedephd); pagedep; 4297 pagedep = LIST_NEXT(pagedep, pd_hash)) { 4298 if (LIST_FIRST(&pagedep->pd_dirremhd) == NULL) 4299 continue; 4300 mp = pagedep->pd_mnt; 4301 ino = pagedep->pd_ino; 4302 FREE_LOCK(&lk); 4303 if ((error = VFS_VGET(mp, ino, &vp)) != 0) { 4304 softdep_error("clear_remove: vget", error); 4305 return; 4306 } 4307 if ((error = VOP_FSYNC(vp, p->p_ucred, MNT_NOWAIT, p))) 4308 softdep_error("clear_remove: fsync", error); 4309 drain_output(vp, 0); 4310 vput(vp); 4311 return; 4312 } 4313 } 4314 FREE_LOCK(&lk); 4315} 4316 4317/* 4318 * Clear out a block of dirty inodes in an effort to reduce 4319 * the number of inodedep dependency structures. 4320 */ 4321static void 4322clear_inodedeps(p) 4323 struct proc *p; 4324{ 4325 struct inodedep_hashhead *inodedephd; 4326 struct inodedep *inodedep; 4327 static int next = 0; 4328 struct mount *mp; 4329 struct vnode *vp; 4330 struct fs *fs; 4331 int error, cnt; 4332 ino_t firstino, lastino, ino; 4333 4334 ACQUIRE_LOCK(&lk); 4335 /* 4336 * Pick a random inode dependency to be cleared. 4337 * We will then gather up all the inodes in its block 4338 * that have dependencies and flush them out. 4339 */ 4340 for (cnt = 0; cnt < inodedep_hash; cnt++) { 4341 inodedephd = &inodedep_hashtbl[next++]; 4342 if (next >= inodedep_hash) 4343 next = 0; 4344 if ((inodedep = LIST_FIRST(inodedephd)) != NULL) 4345 break; 4346 } 4347 /* 4348 * Ugly code to find mount point given pointer to superblock. 4349 */ 4350 fs = inodedep->id_fs; 4351 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist; 4352 mp = CIRCLEQ_NEXT(mp, mnt_list)) 4353 if ((mp->mnt_flag & MNT_SOFTDEP) && fs == VFSTOUFS(mp)->um_fs) 4354 break; 4355 /* 4356 * Find the last inode in the block with dependencies. 4357 */ 4358 firstino = inodedep->id_ino & ~(INOPB(fs) - 1); 4359 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--) 4360 if (inodedep_lookup(fs, lastino, 0, &inodedep) != 0) 4361 break; 4362 /* 4363 * Asynchronously push all but the last inode with dependencies. 4364 * Synchronously push the last inode with dependencies to ensure 4365 * that the inode block gets written to free up the inodedeps. 4366 */ 4367 for (ino = firstino; ino <= lastino; ino++) { 4368 if (inodedep_lookup(fs, ino, 0, &inodedep) == 0) 4369 continue; 4370 FREE_LOCK(&lk); 4371 if ((error = VFS_VGET(mp, ino, &vp)) != 0) { 4372 softdep_error("clear_inodedeps: vget", error); 4373 return; 4374 } 4375 if (ino == lastino) { 4376 if ((error = VOP_FSYNC(vp, p->p_ucred, MNT_WAIT, p))) 4377 softdep_error("clear_inodedeps: fsync1", error); 4378 } else { 4379 if ((error = VOP_FSYNC(vp, p->p_ucred, MNT_NOWAIT, p))) 4380 softdep_error("clear_inodedeps: fsync2", error); 4381 drain_output(vp, 0); 4382 } 4383 vput(vp); 4384 ACQUIRE_LOCK(&lk); 4385 } 4386 FREE_LOCK(&lk); 4387} 4388 4389/* 4390 * Acquire exclusive access to a buffer. 4391 * Must be called with splbio blocked. 4392 * Return 1 if buffer was acquired. 4393 */ 4394static int 4395getdirtybuf(bpp, waitfor) 4396 struct buf **bpp; 4397 int waitfor; 4398{ 4399 struct buf *bp; 4400 4401 for (;;) { 4402 if ((bp = *bpp) == NULL) 4403 return (0); 4404 if ((bp->b_flags & B_BUSY) == 0) 4405 break; 4406 if (waitfor != MNT_WAIT) 4407 return (0); 4408 bp->b_flags |= B_WANTED; 4409 FREE_LOCK_INTERLOCKED(&lk); 4410 tsleep((caddr_t)bp, PRIBIO + 1, "sdsdty", 0); 4411 ACQUIRE_LOCK_INTERLOCKED(&lk); 4412 } 4413 if ((bp->b_flags & B_DELWRI) == 0) 4414 return (0); 4415 bremfree(bp); 4416 bp->b_flags |= B_BUSY; 4417 return (1); 4418} 4419 4420/* 4421 * Wait for pending output on a vnode to complete. 4422 * Must be called with vnode locked. 4423 */ 4424static void 4425drain_output(vp, islocked) 4426 struct vnode *vp; 4427 int islocked; 4428{ 4429 4430 if (!islocked) 4431 ACQUIRE_LOCK(&lk); 4432 while (vp->v_numoutput) { 4433 vp->v_flag |= VBWAIT; 4434 FREE_LOCK_INTERLOCKED(&lk); 4435 tsleep((caddr_t)&vp->v_numoutput, PRIBIO + 1, "drainvp", 0); 4436 ACQUIRE_LOCK_INTERLOCKED(&lk); 4437 } 4438 if (!islocked) 4439 FREE_LOCK(&lk); 4440} 4441 4442/* 4443 * Called whenever a buffer that is being invalidated or reallocated 4444 * contains dependencies. This should only happen if an I/O error has 4445 * occurred. The routine is called with the buffer locked. 4446 */ 4447void 4448softdep_deallocate_dependencies(bp) 4449 struct buf *bp; 4450{ 4451 4452 if ((bp->b_flags & B_ERROR) == 0) 4453 panic("softdep_deallocate_dependencies: dangling deps"); 4454 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error); 4455 panic("softdep_deallocate_dependencies: unrecovered I/O error"); 4456} 4457 4458/* 4459 * Function to handle asynchronous write errors in the filesystem. 4460 */ 4461void 4462softdep_error(func, error) 4463 char *func; 4464 int error; 4465{ 4466 4467 /* XXX should do something better! */ 4468 printf("%s: got error %d while accessing filesystem\n", func, error); 4469} 4470