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