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