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