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