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