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